CGStmt.cpp 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  1. //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This contains code to emit Stmt nodes as LLVM code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "CGDebugInfo.h"
  15. #include "CodeGenModule.h"
  16. #include "TargetInfo.h"
  17. #include "clang/AST/StmtVisitor.h"
  18. #include "clang/Basic/PrettyStackTrace.h"
  19. #include "clang/Basic/TargetInfo.h"
  20. #include "clang/Sema/LoopHint.h"
  21. #include "clang/Sema/SemaDiagnostic.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/IR/CallSite.h"
  24. #include "llvm/IR/DataLayout.h"
  25. #include "llvm/IR/InlineAsm.h"
  26. #include "llvm/IR/Intrinsics.h"
  27. #include "CGHLSLRuntime.h" // HLSL Change
  28. using namespace clang;
  29. using namespace CodeGen;
  30. //===----------------------------------------------------------------------===//
  31. // Statement Emission
  32. //===----------------------------------------------------------------------===//
  33. void CodeGenFunction::EmitStopPoint(const Stmt *S) {
  34. if (CGDebugInfo *DI = getDebugInfo()) {
  35. SourceLocation Loc;
  36. Loc = S->getLocStart();
  37. DI->EmitLocation(Builder, Loc);
  38. LastStopPoint = Loc;
  39. }
  40. }
  41. void CodeGenFunction::EmitStmt(const Stmt *S) {
  42. assert(S && "Null statement?");
  43. PGO.setCurrentStmt(S);
  44. // These statements have their own debug info handling.
  45. if (EmitSimpleStmt(S))
  46. return;
  47. // Check if we are generating unreachable code.
  48. if (!HaveInsertPoint()) {
  49. // If so, and the statement doesn't contain a label, then we do not need to
  50. // generate actual code. This is safe because (1) the current point is
  51. // unreachable, so we don't need to execute the code, and (2) we've already
  52. // handled the statements which update internal data structures (like the
  53. // local variable map) which could be used by subsequent statements.
  54. if (!ContainsLabel(S)) {
  55. // Verify that any decl statements were handled as simple, they may be in
  56. // scope of subsequent reachable statements.
  57. assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
  58. return;
  59. }
  60. // Otherwise, make a new block to hold the code.
  61. EnsureInsertPoint();
  62. }
  63. // Generate a stoppoint if we are emitting debug info.
  64. EmitStopPoint(S);
  65. switch (S->getStmtClass()) {
  66. case Stmt::NoStmtClass:
  67. case Stmt::CXXCatchStmtClass:
  68. case Stmt::SEHExceptStmtClass:
  69. case Stmt::SEHFinallyStmtClass:
  70. case Stmt::MSDependentExistsStmtClass:
  71. llvm_unreachable("invalid statement class to emit generically");
  72. case Stmt::NullStmtClass:
  73. case Stmt::CompoundStmtClass:
  74. case Stmt::DeclStmtClass:
  75. case Stmt::LabelStmtClass:
  76. case Stmt::AttributedStmtClass:
  77. case Stmt::GotoStmtClass:
  78. case Stmt::BreakStmtClass:
  79. case Stmt::ContinueStmtClass:
  80. case Stmt::DefaultStmtClass:
  81. case Stmt::CaseStmtClass:
  82. case Stmt::SEHLeaveStmtClass:
  83. llvm_unreachable("should have emitted these statements as simple");
  84. #define STMT(Type, Base)
  85. #define ABSTRACT_STMT(Op)
  86. #define EXPR(Type, Base) \
  87. case Stmt::Type##Class:
  88. #include "clang/AST/StmtNodes.inc"
  89. {
  90. // Remember the block we came in on.
  91. llvm::BasicBlock *incoming = Builder.GetInsertBlock();
  92. assert(incoming && "expression emission must have an insertion point");
  93. EmitIgnoredExpr(cast<Expr>(S));
  94. llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
  95. assert(outgoing && "expression emission cleared block!");
  96. // The expression emitters assume (reasonably!) that the insertion
  97. // point is always set. To maintain that, the call-emission code
  98. // for noreturn functions has to enter a new block with no
  99. // predecessors. We want to kill that block and mark the current
  100. // insertion point unreachable in the common case of a call like
  101. // "exit();". Since expression emission doesn't otherwise create
  102. // blocks with no predecessors, we can just test for that.
  103. // However, we must be careful not to do this to our incoming
  104. // block, because *statement* emission does sometimes create
  105. // reachable blocks which will have no predecessors until later in
  106. // the function. This occurs with, e.g., labels that are not
  107. // reachable by fallthrough.
  108. if (incoming != outgoing && outgoing->use_empty()) {
  109. outgoing->eraseFromParent();
  110. Builder.ClearInsertionPoint();
  111. }
  112. break;
  113. }
  114. case Stmt::IndirectGotoStmtClass:
  115. EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
  116. case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
  117. case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
  118. case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
  119. case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
  120. case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
  121. case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
  122. #if 1 // HLSL Change - no support for assembler, captures, ObjC, exception handling, for-range, openmp
  123. default:
  124. break;
  125. #else
  126. case Stmt::GCCAsmStmtClass: // Intentional fall-through.
  127. case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
  128. case Stmt::CapturedStmtClass: {
  129. const CapturedStmt *CS = cast<CapturedStmt>(S);
  130. EmitCapturedStmt(*CS, CS->getCapturedRegionKind());
  131. }
  132. break;
  133. case Stmt::ObjCAtTryStmtClass:
  134. EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
  135. break;
  136. case Stmt::ObjCAtCatchStmtClass:
  137. llvm_unreachable(
  138. "@catch statements should be handled by EmitObjCAtTryStmt");
  139. case Stmt::ObjCAtFinallyStmtClass:
  140. llvm_unreachable(
  141. "@finally statements should be handled by EmitObjCAtTryStmt");
  142. case Stmt::ObjCAtThrowStmtClass:
  143. EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
  144. break;
  145. case Stmt::ObjCAtSynchronizedStmtClass:
  146. EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
  147. break;
  148. case Stmt::ObjCForCollectionStmtClass:
  149. EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
  150. break;
  151. case Stmt::ObjCAutoreleasePoolStmtClass:
  152. EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
  153. break;
  154. case Stmt::CXXTryStmtClass:
  155. EmitCXXTryStmt(cast<CXXTryStmt>(*S));
  156. break;
  157. case Stmt::CXXForRangeStmtClass:
  158. EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S));
  159. break;
  160. case Stmt::SEHTryStmtClass:
  161. EmitSEHTryStmt(cast<SEHTryStmt>(*S));
  162. break;
  163. case Stmt::OMPParallelDirectiveClass:
  164. EmitOMPParallelDirective(cast<OMPParallelDirective>(*S));
  165. break;
  166. case Stmt::OMPSimdDirectiveClass:
  167. EmitOMPSimdDirective(cast<OMPSimdDirective>(*S));
  168. break;
  169. case Stmt::OMPForDirectiveClass:
  170. EmitOMPForDirective(cast<OMPForDirective>(*S));
  171. break;
  172. case Stmt::OMPForSimdDirectiveClass:
  173. EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S));
  174. break;
  175. case Stmt::OMPSectionsDirectiveClass:
  176. EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S));
  177. break;
  178. case Stmt::OMPSectionDirectiveClass:
  179. EmitOMPSectionDirective(cast<OMPSectionDirective>(*S));
  180. break;
  181. case Stmt::OMPSingleDirectiveClass:
  182. EmitOMPSingleDirective(cast<OMPSingleDirective>(*S));
  183. break;
  184. case Stmt::OMPMasterDirectiveClass:
  185. EmitOMPMasterDirective(cast<OMPMasterDirective>(*S));
  186. break;
  187. case Stmt::OMPCriticalDirectiveClass:
  188. EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S));
  189. break;
  190. case Stmt::OMPParallelForDirectiveClass:
  191. EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S));
  192. break;
  193. case Stmt::OMPParallelForSimdDirectiveClass:
  194. EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S));
  195. break;
  196. case Stmt::OMPParallelSectionsDirectiveClass:
  197. EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S));
  198. break;
  199. case Stmt::OMPTaskDirectiveClass:
  200. EmitOMPTaskDirective(cast<OMPTaskDirective>(*S));
  201. break;
  202. case Stmt::OMPTaskyieldDirectiveClass:
  203. EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S));
  204. break;
  205. case Stmt::OMPBarrierDirectiveClass:
  206. EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S));
  207. break;
  208. case Stmt::OMPTaskwaitDirectiveClass:
  209. EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S));
  210. break;
  211. case Stmt::OMPTaskgroupDirectiveClass:
  212. EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S));
  213. break;
  214. case Stmt::OMPFlushDirectiveClass:
  215. EmitOMPFlushDirective(cast<OMPFlushDirective>(*S));
  216. break;
  217. case Stmt::OMPOrderedDirectiveClass:
  218. EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S));
  219. break;
  220. case Stmt::OMPAtomicDirectiveClass:
  221. EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S));
  222. break;
  223. case Stmt::OMPTargetDirectiveClass:
  224. EmitOMPTargetDirective(cast<OMPTargetDirective>(*S));
  225. break;
  226. case Stmt::OMPTeamsDirectiveClass:
  227. EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S));
  228. break;
  229. case Stmt::OMPCancellationPointDirectiveClass:
  230. EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S));
  231. break;
  232. case Stmt::OMPCancelDirectiveClass:
  233. EmitOMPCancelDirective(cast<OMPCancelDirective>(*S));
  234. break;
  235. #endif // HLSL Change - no support for assembler, captures, ObjC, exception handling, for-range, openmp
  236. }
  237. }
  238. bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
  239. switch (S->getStmtClass()) {
  240. default: return false;
  241. case Stmt::NullStmtClass: break;
  242. case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
  243. case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
  244. case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
  245. case Stmt::AttributedStmtClass:
  246. EmitAttributedStmt(cast<AttributedStmt>(*S)); break;
  247. case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
  248. case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break;
  249. case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
  250. case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
  251. case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
  252. // HLSL Change Begins.
  253. case Stmt::DiscardStmtClass: EmitDiscardStmt(cast<DiscardStmt>(*S)); break;
  254. // HLSL Change Ends.
  255. #if 0 // HLSL Change - no support for exception handling
  256. case Stmt::SEHLeaveStmtClass: EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); break;
  257. #endif
  258. }
  259. return true;
  260. }
  261. /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
  262. /// this captures the expression result of the last sub-statement and returns it
  263. /// (for use by the statement expression extension).
  264. llvm::Value* CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
  265. AggValueSlot AggSlot) {
  266. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
  267. "LLVM IR generation of compound statement ('{}')");
  268. // Keep track of the current cleanup stack depth, including debug scopes.
  269. LexicalScope Scope(*this, S.getSourceRange());
  270. return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
  271. }
  272. llvm::Value*
  273. CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
  274. bool GetLast,
  275. AggValueSlot AggSlot) {
  276. for (CompoundStmt::const_body_iterator I = S.body_begin(),
  277. E = S.body_end()-GetLast; I != E; ++I)
  278. EmitStmt(*I);
  279. llvm::Value *RetAlloca = nullptr;
  280. if (GetLast) {
  281. // We have to special case labels here. They are statements, but when put
  282. // at the end of a statement expression, they yield the value of their
  283. // subexpression. Handle this by walking through all labels we encounter,
  284. // emitting them before we evaluate the subexpr.
  285. const Stmt *LastStmt = S.body_back();
  286. while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
  287. EmitLabel(LS->getDecl());
  288. LastStmt = LS->getSubStmt();
  289. }
  290. EnsureInsertPoint();
  291. QualType ExprTy = cast<Expr>(LastStmt)->getType();
  292. if (hasAggregateEvaluationKind(ExprTy)) {
  293. EmitAggExpr(cast<Expr>(LastStmt), AggSlot);
  294. } else {
  295. // We can't return an RValue here because there might be cleanups at
  296. // the end of the StmtExpr. Because of that, we have to emit the result
  297. // here into a temporary alloca.
  298. RetAlloca = CreateMemTemp(ExprTy);
  299. EmitAnyExprToMem(cast<Expr>(LastStmt), RetAlloca, Qualifiers(),
  300. /*IsInit*/false);
  301. }
  302. }
  303. return RetAlloca;
  304. }
  305. void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
  306. llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
  307. // If there is a cleanup stack, then we it isn't worth trying to
  308. // simplify this block (we would need to remove it from the scope map
  309. // and cleanup entry).
  310. if (!EHStack.empty())
  311. return;
  312. // Can only simplify direct branches.
  313. if (!BI || !BI->isUnconditional())
  314. return;
  315. // Can only simplify empty blocks.
  316. if (BI != BB->begin())
  317. return;
  318. BB->replaceAllUsesWith(BI->getSuccessor(0));
  319. BI->eraseFromParent();
  320. BB->eraseFromParent();
  321. }
  322. void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
  323. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  324. // Fall out of the current block (if necessary).
  325. EmitBranch(BB);
  326. if (IsFinished && BB->use_empty()) {
  327. delete BB;
  328. return;
  329. }
  330. // Place the block after the current block, if possible, or else at
  331. // the end of the function.
  332. if (CurBB && CurBB->getParent())
  333. CurFn->getBasicBlockList().insertAfter(CurBB, BB);
  334. else
  335. CurFn->getBasicBlockList().push_back(BB);
  336. Builder.SetInsertPoint(BB);
  337. }
  338. void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
  339. // Emit a branch from the current block to the target one if this
  340. // was a real block. If this was just a fall-through block after a
  341. // terminator, don't emit it.
  342. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  343. if (!CurBB || CurBB->getTerminator()) {
  344. // If there is no insert point or the previous block is already
  345. // terminated, don't touch it.
  346. } else {
  347. // Otherwise, create a fall-through branch.
  348. Builder.CreateBr(Target);
  349. }
  350. Builder.ClearInsertionPoint();
  351. }
  352. void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
  353. bool inserted = false;
  354. for (llvm::User *u : block->users()) {
  355. if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) {
  356. CurFn->getBasicBlockList().insertAfter(insn->getParent(), block);
  357. inserted = true;
  358. break;
  359. }
  360. }
  361. if (!inserted)
  362. CurFn->getBasicBlockList().push_back(block);
  363. Builder.SetInsertPoint(block);
  364. }
  365. CodeGenFunction::JumpDest
  366. CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
  367. JumpDest &Dest = LabelMap[D];
  368. if (Dest.isValid()) return Dest;
  369. // Create, but don't insert, the new block.
  370. Dest = JumpDest(createBasicBlock(D->getName()),
  371. EHScopeStack::stable_iterator::invalid(),
  372. NextCleanupDestIndex++);
  373. return Dest;
  374. }
  375. void CodeGenFunction::EmitLabel(const LabelDecl *D) {
  376. // Add this label to the current lexical scope if we're within any
  377. // normal cleanups. Jumps "in" to this label --- when permitted by
  378. // the language --- may need to be routed around such cleanups.
  379. if (EHStack.hasNormalCleanups() && CurLexicalScope)
  380. CurLexicalScope->addLabel(D);
  381. JumpDest &Dest = LabelMap[D];
  382. // If we didn't need a forward reference to this label, just go
  383. // ahead and create a destination at the current scope.
  384. if (!Dest.isValid()) {
  385. Dest = getJumpDestInCurrentScope(D->getName());
  386. // Otherwise, we need to give this label a target depth and remove
  387. // it from the branch-fixups list.
  388. } else {
  389. assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
  390. Dest.setScopeDepth(EHStack.stable_begin());
  391. ResolveBranchFixups(Dest.getBlock());
  392. }
  393. EmitBlock(Dest.getBlock());
  394. incrementProfileCounter(D->getStmt());
  395. }
  396. /// Change the cleanup scope of the labels in this lexical scope to
  397. /// match the scope of the enclosing context.
  398. void CodeGenFunction::LexicalScope::rescopeLabels() {
  399. assert(!Labels.empty());
  400. EHScopeStack::stable_iterator innermostScope
  401. = CGF.EHStack.getInnermostNormalCleanup();
  402. // Change the scope depth of all the labels.
  403. for (SmallVectorImpl<const LabelDecl*>::const_iterator
  404. i = Labels.begin(), e = Labels.end(); i != e; ++i) {
  405. assert(CGF.LabelMap.count(*i));
  406. JumpDest &dest = CGF.LabelMap.find(*i)->second;
  407. assert(dest.getScopeDepth().isValid());
  408. assert(innermostScope.encloses(dest.getScopeDepth()));
  409. dest.setScopeDepth(innermostScope);
  410. }
  411. // Reparent the labels if the new scope also has cleanups.
  412. if (innermostScope != EHScopeStack::stable_end() && ParentScope) {
  413. ParentScope->Labels.append(Labels.begin(), Labels.end());
  414. }
  415. }
  416. void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
  417. EmitLabel(S.getDecl());
  418. EmitStmt(S.getSubStmt());
  419. }
  420. void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
  421. const Stmt *SubStmt = S.getSubStmt();
  422. switch (SubStmt->getStmtClass()) {
  423. case Stmt::DoStmtClass:
  424. EmitDoStmt(cast<DoStmt>(*SubStmt), S.getAttrs());
  425. break;
  426. case Stmt::ForStmtClass:
  427. EmitForStmt(cast<ForStmt>(*SubStmt), S.getAttrs());
  428. break;
  429. case Stmt::WhileStmtClass:
  430. EmitWhileStmt(cast<WhileStmt>(*SubStmt), S.getAttrs());
  431. break;
  432. case Stmt::CXXForRangeStmtClass:
  433. EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*SubStmt), S.getAttrs());
  434. break;
  435. // HLSL Change Begins.
  436. case Stmt::IfStmtClass:
  437. EmitIfStmt(cast<IfStmt>(*SubStmt), S.getAttrs());
  438. break;
  439. case Stmt::SwitchStmtClass:
  440. EmitSwitchStmt(cast<SwitchStmt>(*SubStmt), S.getAttrs());
  441. break;
  442. // HLSL Change Ends.
  443. default:
  444. EmitStmt(SubStmt);
  445. }
  446. }
  447. void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
  448. // If this code is reachable then emit a stop point (if generating
  449. // debug info). We have to do this ourselves because we are on the
  450. // "simple" statement path.
  451. if (HaveInsertPoint())
  452. EmitStopPoint(&S);
  453. EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
  454. }
  455. // HLSL Change Begins.
  456. void CodeGenFunction::EmitDiscardStmt(const DiscardStmt &S) {
  457. CGM.getHLSLRuntime().EmitHLSLDiscard(*this);
  458. }
  459. // HLSL Change Ends.
  460. void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
  461. if (const LabelDecl *Target = S.getConstantTarget()) {
  462. EmitBranchThroughCleanup(getJumpDestForLabel(Target));
  463. return;
  464. }
  465. // Ensure that we have an i8* for our PHI node.
  466. llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
  467. Int8PtrTy, "addr");
  468. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  469. // Get the basic block for the indirect goto.
  470. llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
  471. // The first instruction in the block has to be the PHI for the switch dest,
  472. // add an entry for this branch.
  473. cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
  474. EmitBranch(IndGotoBB);
  475. }
  476. void CodeGenFunction::EmitIfStmt(const IfStmt &S,
  477. ArrayRef<const Attr *> Attrs) { // HLSL Change
  478. // HLSL Change Begins
  479. // Skip unreachable if.
  480. if (!HaveInsertPoint())
  481. return;
  482. // HLSL Change Ends
  483. // C99 6.8.4.1: The first substatement is executed if the expression compares
  484. // unequal to 0. The condition must be a scalar type.
  485. LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
  486. if (S.getConditionVariable())
  487. EmitAutoVarDecl(*S.getConditionVariable());
  488. // If the condition constant folds and can be elided, try to avoid emitting
  489. // the condition and the dead arm of the if/else.
  490. bool CondConstant;
  491. if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) {
  492. // Figure out which block (then or else) is executed.
  493. const Stmt *Executed = S.getThen();
  494. const Stmt *Skipped = S.getElse();
  495. if (!CondConstant) // Condition false?
  496. std::swap(Executed, Skipped);
  497. // If the skipped block has no labels in it, just emit the executed block.
  498. // This avoids emitting dead code and simplifies the CFG substantially.
  499. if (!ContainsLabel(Skipped)) {
  500. if (CondConstant)
  501. incrementProfileCounter(&S);
  502. // HLSL Change Begin.
  503. if (getLangOpts().HLSL) {
  504. // Emit Cond to make sure not short circuiting.
  505. EmitScalarExpr(S.getCond());
  506. }
  507. // HLSL Change End.
  508. if (Executed) {
  509. RunCleanupsScope ExecutedScope(*this);
  510. EmitStmt(Executed);
  511. }
  512. return;
  513. }
  514. }
  515. // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
  516. // the conditional branch.
  517. llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
  518. llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
  519. llvm::BasicBlock *ElseBlock = ContBlock;
  520. if (S.getElse())
  521. ElseBlock = createBasicBlock("if.else");
  522. EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock,
  523. getProfileCount(S.getThen()));
  524. // HLSL Change Begins
  525. llvm::TerminatorInst *TI =
  526. cast<llvm::TerminatorInst>(*ThenBlock->user_begin());
  527. CGM.getHLSLRuntime().AddControlFlowHint(*this, S, TI, Attrs);
  528. CGM.getHLSLRuntime().MarkIfStmt(*this, ContBlock);
  529. // HLSL Change Ends
  530. // Emit the 'then' code.
  531. EmitBlock(ThenBlock);
  532. incrementProfileCounter(&S);
  533. {
  534. RunCleanupsScope ThenScope(*this);
  535. EmitStmt(S.getThen());
  536. }
  537. EmitBranch(ContBlock);
  538. // Emit the 'else' code if present.
  539. if (const Stmt *Else = S.getElse()) {
  540. {
  541. // There is no need to emit line number for an unconditional branch.
  542. auto NL = ApplyDebugLocation::CreateEmpty(*this);
  543. EmitBlock(ElseBlock);
  544. }
  545. {
  546. RunCleanupsScope ElseScope(*this);
  547. EmitStmt(Else);
  548. }
  549. {
  550. // There is no need to emit line number for an unconditional branch.
  551. auto NL = ApplyDebugLocation::CreateEmpty(*this);
  552. EmitBranch(ContBlock);
  553. }
  554. }
  555. // HLSL Change Begin.
  556. CGM.getHLSLRuntime().MarkScopeEnd(*this);
  557. // HLSL Change End.
  558. // Emit the continuation block for code after the if.
  559. EmitBlock(ContBlock, true);
  560. }
  561. void CodeGenFunction::EmitCondBrHints(llvm::LLVMContext &Context,
  562. llvm::BranchInst *CondBr,
  563. ArrayRef<const Attr *> Attrs) {
  564. // Return if there are no hints.
  565. if (Attrs.empty())
  566. return;
  567. // Add vectorize and unroll hints to the metadata on the conditional branch.
  568. //
  569. // FIXME: Should this really start with a size of 1?
  570. SmallVector<llvm::Metadata *, 2> Metadata(1);
  571. for (const auto *Attr : Attrs) {
  572. const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(Attr);
  573. // Skip non loop hint attributes
  574. if (!LH)
  575. continue;
  576. LoopHintAttr::OptionType Option = LH->getOption();
  577. LoopHintAttr::LoopHintState State = LH->getState();
  578. const char *MetadataName;
  579. switch (Option) {
  580. case LoopHintAttr::Vectorize:
  581. case LoopHintAttr::VectorizeWidth:
  582. MetadataName = "llvm.loop.vectorize.width";
  583. break;
  584. case LoopHintAttr::Interleave:
  585. case LoopHintAttr::InterleaveCount:
  586. MetadataName = "llvm.loop.interleave.count";
  587. break;
  588. case LoopHintAttr::Unroll:
  589. // With the unroll loop hint, a non-zero value indicates full unrolling.
  590. MetadataName = State == LoopHintAttr::Disable ? "llvm.loop.unroll.disable"
  591. : "llvm.loop.unroll.full";
  592. break;
  593. case LoopHintAttr::UnrollCount:
  594. MetadataName = "llvm.loop.unroll.count";
  595. break;
  596. }
  597. Expr *ValueExpr = LH->getValue();
  598. int ValueInt = 1;
  599. if (ValueExpr) {
  600. llvm::APSInt ValueAPS =
  601. ValueExpr->EvaluateKnownConstInt(CGM.getContext());
  602. ValueInt = static_cast<int>(ValueAPS.getSExtValue());
  603. }
  604. llvm::Constant *Value;
  605. llvm::MDString *Name;
  606. switch (Option) {
  607. case LoopHintAttr::Vectorize:
  608. case LoopHintAttr::Interleave:
  609. if (State != LoopHintAttr::Disable) {
  610. // FIXME: In the future I will modifiy the behavior of the metadata
  611. // so we can enable/disable vectorization and interleaving separately.
  612. Name = llvm::MDString::get(Context, "llvm.loop.vectorize.enable");
  613. Value = Builder.getTrue();
  614. break;
  615. }
  616. // Vectorization/interleaving is disabled, set width/count to 1.
  617. ValueInt = 1;
  618. // Fallthrough.
  619. case LoopHintAttr::VectorizeWidth:
  620. case LoopHintAttr::InterleaveCount:
  621. case LoopHintAttr::UnrollCount:
  622. Name = llvm::MDString::get(Context, MetadataName);
  623. Value = llvm::ConstantInt::get(Int32Ty, ValueInt);
  624. break;
  625. case LoopHintAttr::Unroll:
  626. Name = llvm::MDString::get(Context, MetadataName);
  627. Value = nullptr;
  628. break;
  629. }
  630. SmallVector<llvm::Metadata *, 2> OpValues;
  631. OpValues.push_back(Name);
  632. if (Value)
  633. OpValues.push_back(llvm::ConstantAsMetadata::get(Value));
  634. // Set or overwrite metadata indicated by Name.
  635. Metadata.push_back(llvm::MDNode::get(Context, OpValues));
  636. }
  637. // FIXME: This condition is never false. Should it be an assert?
  638. if ( // HLSL Change Begin.
  639. // We only want to enter this if we found a llvm loop attribute and we
  640. // know we found an llvm attribute if the metadata size > 1.
  641. Metadata.size() > 1
  642. // HLSL Change End.
  643. ) {
  644. // Add llvm.loop MDNode to CondBr.
  645. llvm::MDNode *LoopID = llvm::MDNode::get(Context, Metadata);
  646. LoopID->replaceOperandWith(0, LoopID); // First op points to itself.
  647. CondBr->setMetadata("llvm.loop", LoopID);
  648. }
  649. }
  650. void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
  651. ArrayRef<const Attr *> WhileAttrs) {
  652. // Emit the header for the loop, which will also become
  653. // the continue target.
  654. JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
  655. EmitBlock(LoopHeader.getBlock());
  656. LoopStack.push(LoopHeader.getBlock(), WhileAttrs);
  657. // Create an exit block for when the condition fails, which will
  658. // also become the break target.
  659. JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
  660. // Store the blocks to use for break and continue.
  661. BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
  662. // HLSL Change Begin.
  663. CGM.getHLSLRuntime().MarkLoopStmt(*this, LoopHeader.getBlock(),
  664. LoopExit.getBlock());
  665. // HLSL Change End.
  666. // C++ [stmt.while]p2:
  667. // When the condition of a while statement is a declaration, the
  668. // scope of the variable that is declared extends from its point
  669. // of declaration (3.3.2) to the end of the while statement.
  670. // [...]
  671. // The object created in a condition is destroyed and created
  672. // with each iteration of the loop.
  673. RunCleanupsScope ConditionScope(*this);
  674. if (S.getConditionVariable())
  675. EmitAutoVarDecl(*S.getConditionVariable());
  676. // Evaluate the conditional in the while header. C99 6.8.5.1: The
  677. // evaluation of the controlling expression takes place before each
  678. // execution of the loop body.
  679. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  680. // while(1) is common, avoid extra exit blocks. Be sure
  681. // to correctly handle break/continue though.
  682. bool EmitBoolCondBranch = true;
  683. if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
  684. if (C->isOne())
  685. EmitBoolCondBranch = false;
  686. // As long as the condition is true, go to the loop body.
  687. llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
  688. if (EmitBoolCondBranch) {
  689. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  690. if (ConditionScope.requiresCleanups())
  691. ExitBlock = createBasicBlock("while.exit");
  692. llvm::BranchInst *CondBr = Builder.CreateCondBr(
  693. BoolCondVal, LoopBody, ExitBlock,
  694. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
  695. if (ExitBlock != LoopExit.getBlock()) {
  696. EmitBlock(ExitBlock);
  697. EmitBranchThroughCleanup(LoopExit);
  698. }
  699. // Attach metadata to loop body conditional branch.
  700. EmitCondBrHints(LoopBody->getContext(), CondBr, WhileAttrs);
  701. }
  702. // Emit the loop body. We have to emit this in a cleanup scope
  703. // because it might be a singleton DeclStmt.
  704. {
  705. RunCleanupsScope BodyScope(*this);
  706. EmitBlock(LoopBody);
  707. incrementProfileCounter(&S);
  708. EmitStmt(S.getBody());
  709. }
  710. BreakContinueStack.pop_back();
  711. // Immediately force cleanup.
  712. ConditionScope.ForceCleanup();
  713. EmitStopPoint(&S);
  714. // Branch to the loop header again.
  715. EmitBranch(LoopHeader.getBlock());
  716. LoopStack.pop();
  717. // HLSL Change Begin.
  718. CGM.getHLSLRuntime().MarkScopeEnd(*this);
  719. // HLSL Change End.
  720. // Emit the exit block.
  721. EmitBlock(LoopExit.getBlock(), true);
  722. // The LoopHeader typically is just a branch if we skipped emitting
  723. // a branch, try to erase it.
  724. if (!EmitBoolCondBranch)
  725. SimplifyForwardingBlocks(LoopHeader.getBlock());
  726. }
  727. void CodeGenFunction::EmitDoStmt(const DoStmt &S,
  728. ArrayRef<const Attr *> DoAttrs) {
  729. JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
  730. JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
  731. uint64_t ParentCount = getCurrentProfileCount();
  732. // Store the blocks to use for break and continue.
  733. BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
  734. // HLSL Change Begin.
  735. CGM.getHLSLRuntime().MarkLoopStmt(*this, LoopCond.getBlock(),
  736. LoopExit.getBlock());
  737. // HLSL Change End.
  738. // Emit the body of the loop.
  739. llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
  740. LoopStack.push(LoopBody, DoAttrs);
  741. EmitBlockWithFallThrough(LoopBody, &S);
  742. {
  743. RunCleanupsScope BodyScope(*this);
  744. EmitStmt(S.getBody());
  745. }
  746. EmitBlock(LoopCond.getBlock());
  747. // C99 6.8.5.2: "The evaluation of the controlling expression takes place
  748. // after each execution of the loop body."
  749. // Evaluate the conditional in the while header.
  750. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  751. // compares unequal to 0. The condition must be a scalar type.
  752. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  753. BreakContinueStack.pop_back();
  754. // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
  755. // to correctly handle break/continue though.
  756. bool EmitBoolCondBranch = true;
  757. if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
  758. if (C->isZero())
  759. EmitBoolCondBranch = false;
  760. // As long as the condition is true, iterate the loop.
  761. if (EmitBoolCondBranch) {
  762. uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
  763. llvm::BranchInst *CondBr = Builder.CreateCondBr(
  764. BoolCondVal, LoopBody, LoopExit.getBlock(),
  765. createProfileWeightsForLoop(S.getCond(), BackedgeCount));
  766. // Attach metadata to loop body conditional branch.
  767. EmitCondBrHints(LoopBody->getContext(), CondBr, DoAttrs);
  768. }
  769. LoopStack.pop();
  770. // HLSL Change Begin.
  771. CGM.getHLSLRuntime().MarkScopeEnd(*this);
  772. // HLSL Change End.
  773. // Emit the exit block.
  774. EmitBlock(LoopExit.getBlock());
  775. // The DoCond block typically is just a branch if we skipped
  776. // emitting a branch, try to erase it.
  777. if (!EmitBoolCondBranch)
  778. SimplifyForwardingBlocks(LoopCond.getBlock());
  779. }
  780. void CodeGenFunction::EmitForStmt(const ForStmt &S,
  781. ArrayRef<const Attr *> ForAttrs) {
  782. JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
  783. LexicalScope ForScope(*this, S.getSourceRange());
  784. // Evaluate the first part before the loop.
  785. if (S.getInit())
  786. EmitStmt(S.getInit());
  787. // Start the loop with a block that tests the condition.
  788. // If there's an increment, the continue scope will be overwritten
  789. // later.
  790. JumpDest Continue = getJumpDestInCurrentScope("for.cond");
  791. llvm::BasicBlock *CondBlock = Continue.getBlock();
  792. EmitBlock(CondBlock);
  793. LoopStack.push(CondBlock, ForAttrs);
  794. // If the for loop doesn't have an increment we can just use the
  795. // condition as the continue block. Otherwise we'll need to create
  796. // a block for it (in the current scope, i.e. in the scope of the
  797. // condition), and that we will become our continue block.
  798. if (S.getInc())
  799. Continue = getJumpDestInCurrentScope("for.inc");
  800. // Store the blocks to use for break and continue.
  801. BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
  802. // HLSL Change Begin.
  803. CGM.getHLSLRuntime().MarkLoopStmt(*this, Continue.getBlock(), LoopExit.getBlock());
  804. // HLSL Change End.
  805. // Create a cleanup scope for the condition variable cleanups.
  806. LexicalScope ConditionScope(*this, S.getSourceRange());
  807. if (S.getCond()) {
  808. // If the for statement has a condition scope, emit the local variable
  809. // declaration.
  810. if (S.getConditionVariable()) {
  811. EmitAutoVarDecl(*S.getConditionVariable());
  812. }
  813. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  814. // If there are any cleanups between here and the loop-exit scope,
  815. // create a block to stage a loop exit along.
  816. if (ForScope.requiresCleanups())
  817. ExitBlock = createBasicBlock("for.cond.cleanup");
  818. // As long as the condition is true, iterate the loop.
  819. llvm::BasicBlock *ForBody = createBasicBlock("for.body");
  820. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  821. // compares unequal to 0. The condition must be a scalar type.
  822. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  823. llvm::BranchInst *CondBr = Builder.CreateCondBr(
  824. BoolCondVal, ForBody, ExitBlock,
  825. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
  826. // Attach metadata to loop body conditional branch.
  827. EmitCondBrHints(ForBody->getContext(), CondBr, ForAttrs);
  828. if (ExitBlock != LoopExit.getBlock()) {
  829. EmitBlock(ExitBlock);
  830. EmitBranchThroughCleanup(LoopExit);
  831. }
  832. EmitBlock(ForBody);
  833. } else {
  834. // Treat it as a non-zero constant. Don't even create a new block for the
  835. // body, just fall into it.
  836. }
  837. incrementProfileCounter(&S);
  838. {
  839. // Create a separate cleanup scope for the body, in case it is not
  840. // a compound statement.
  841. RunCleanupsScope BodyScope(*this);
  842. EmitStmt(S.getBody());
  843. }
  844. // If there is an increment, emit it next.
  845. if (S.getInc()) {
  846. EmitBlock(Continue.getBlock());
  847. EmitStmt(S.getInc());
  848. }
  849. BreakContinueStack.pop_back();
  850. ConditionScope.ForceCleanup();
  851. EmitStopPoint(&S);
  852. EmitBranch(CondBlock);
  853. ForScope.ForceCleanup();
  854. LoopStack.pop();
  855. // HLSL Change Begin.
  856. CGM.getHLSLRuntime().MarkScopeEnd(*this);
  857. // HLSL Change End.
  858. // Emit the fall-through block.
  859. EmitBlock(LoopExit.getBlock(), true);
  860. }
  861. void
  862. CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
  863. ArrayRef<const Attr *> ForAttrs) {
  864. JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
  865. LexicalScope ForScope(*this, S.getSourceRange());
  866. // Evaluate the first pieces before the loop.
  867. EmitStmt(S.getRangeStmt());
  868. EmitStmt(S.getBeginEndStmt());
  869. // Start the loop with a block that tests the condition.
  870. // If there's an increment, the continue scope will be overwritten
  871. // later.
  872. llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
  873. EmitBlock(CondBlock);
  874. LoopStack.push(CondBlock, ForAttrs);
  875. // If there are any cleanups between here and the loop-exit scope,
  876. // create a block to stage a loop exit along.
  877. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  878. if (ForScope.requiresCleanups())
  879. ExitBlock = createBasicBlock("for.cond.cleanup");
  880. // The loop body, consisting of the specified body and the loop variable.
  881. llvm::BasicBlock *ForBody = createBasicBlock("for.body");
  882. // The body is executed if the expression, contextually converted
  883. // to bool, is true.
  884. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  885. llvm::BranchInst *CondBr = Builder.CreateCondBr(
  886. BoolCondVal, ForBody, ExitBlock,
  887. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
  888. // Attach metadata to loop body conditional branch.
  889. EmitCondBrHints(ForBody->getContext(), CondBr, ForAttrs);
  890. if (ExitBlock != LoopExit.getBlock()) {
  891. EmitBlock(ExitBlock);
  892. EmitBranchThroughCleanup(LoopExit);
  893. }
  894. EmitBlock(ForBody);
  895. incrementProfileCounter(&S);
  896. // Create a block for the increment. In case of a 'continue', we jump there.
  897. JumpDest Continue = getJumpDestInCurrentScope("for.inc");
  898. // Store the blocks to use for break and continue.
  899. BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
  900. {
  901. // Create a separate cleanup scope for the loop variable and body.
  902. LexicalScope BodyScope(*this, S.getSourceRange());
  903. EmitStmt(S.getLoopVarStmt());
  904. EmitStmt(S.getBody());
  905. }
  906. EmitStopPoint(&S);
  907. // If there is an increment, emit it next.
  908. EmitBlock(Continue.getBlock());
  909. EmitStmt(S.getInc());
  910. BreakContinueStack.pop_back();
  911. EmitBranch(CondBlock);
  912. ForScope.ForceCleanup();
  913. LoopStack.pop();
  914. // Emit the fall-through block.
  915. EmitBlock(LoopExit.getBlock(), true);
  916. }
  917. void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
  918. if (RV.isScalar()) {
  919. Builder.CreateStore(RV.getScalarVal(), ReturnValue);
  920. } else if (RV.isAggregate()) {
  921. EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
  922. } else {
  923. EmitStoreOfComplex(RV.getComplexVal(),
  924. MakeNaturalAlignAddrLValue(ReturnValue, Ty),
  925. /*init*/ true);
  926. }
  927. EmitBranchThroughCleanup(ReturnBlock);
  928. }
  929. /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
  930. /// if the function returns void, or may be missing one if the function returns
  931. /// non-void. Fun stuff :).
  932. void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
  933. // Returning from an outlined SEH helper is UB, and we already warn on it.
  934. if (IsOutlinedSEHHelper) {
  935. Builder.CreateUnreachable();
  936. Builder.ClearInsertionPoint();
  937. }
  938. // Emit the result value, even if unused, to evalute the side effects.
  939. const Expr *RV = S.getRetValue();
  940. // Treat block literals in a return expression as if they appeared
  941. // in their own scope. This permits a small, easily-implemented
  942. // exception to our over-conservative rules about not jumping to
  943. // statements following block literals with non-trivial cleanups.
  944. RunCleanupsScope cleanupScope(*this);
  945. if (const ExprWithCleanups *cleanups =
  946. dyn_cast_or_null<ExprWithCleanups>(RV)) {
  947. enterFullExpression(cleanups);
  948. RV = cleanups->getSubExpr();
  949. }
  950. // FIXME: Clean this up by using an LValue for ReturnTemp,
  951. // EmitStoreThroughLValue, and EmitAnyExpr.
  952. if (getLangOpts().ElideConstructors &&
  953. S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) {
  954. // Apply the named return value optimization for this return statement,
  955. // which means doing nothing: the appropriate result has already been
  956. // constructed into the NRVO variable.
  957. // If there is an NRVO flag for this variable, set it to 1 into indicate
  958. // that the cleanup code should not destroy the variable.
  959. if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
  960. Builder.CreateStore(Builder.getTrue(), NRVOFlag);
  961. } else if (!ReturnValue || (RV && RV->getType()->isVoidType())) {
  962. // Make sure not to return anything, but evaluate the expression
  963. // for side effects.
  964. if (RV)
  965. EmitAnyExpr(RV);
  966. } else if (!RV) {
  967. // Do nothing (return value is left uninitialized)
  968. } else if (FnRetTy->isReferenceType()) {
  969. // If this function returns a reference, take the address of the expression
  970. // rather than the value.
  971. RValue Result = EmitReferenceBindingToExpr(RV);
  972. Builder.CreateStore(Result.getScalarVal(), ReturnValue);
  973. } else {
  974. switch (getEvaluationKind(RV->getType())) {
  975. case TEK_Scalar:
  976. // HLSL Change Begins.
  977. if (hlsl::IsHLSLMatType(RV->getType())) {
  978. CGM.getHLSLRuntime().EmitHLSLMatrixStore(*this, EmitScalarExpr(RV), ReturnValue, FnRetTy);
  979. } else
  980. // HLSL Change Ends.
  981. Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
  982. break;
  983. case TEK_Complex:
  984. EmitComplexExprIntoLValue(RV,
  985. MakeNaturalAlignAddrLValue(ReturnValue, RV->getType()),
  986. /*isInit*/ true);
  987. break;
  988. case TEK_Aggregate: {
  989. CharUnits Alignment = getContext().getTypeAlignInChars(RV->getType());
  990. EmitAggExpr(RV, AggValueSlot::forAddr(ReturnValue, Alignment,
  991. Qualifiers(),
  992. AggValueSlot::IsDestructed,
  993. AggValueSlot::DoesNotNeedGCBarriers,
  994. AggValueSlot::IsNotAliased));
  995. break;
  996. }
  997. }
  998. }
  999. ++NumReturnExprs;
  1000. if (!RV || RV->isEvaluatable(getContext()))
  1001. ++NumSimpleReturnExprs;
  1002. cleanupScope.ForceCleanup();
  1003. // HLSL Change Begin.
  1004. CGM.getHLSLRuntime().MarkReturnStmt(*this, Builder.GetInsertBlock());
  1005. // HLSL Change End.
  1006. EmitBranchThroughCleanup(ReturnBlock);
  1007. }
  1008. void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
  1009. // As long as debug info is modeled with instructions, we have to ensure we
  1010. // have a place to insert here and write the stop point here.
  1011. if (HaveInsertPoint())
  1012. EmitStopPoint(&S);
  1013. for (const auto *I : S.decls())
  1014. EmitDecl(*I);
  1015. }
  1016. void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
  1017. assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
  1018. // If this code is reachable then emit a stop point (if generating
  1019. // debug info). We have to do this ourselves because we are on the
  1020. // "simple" statement path.
  1021. if (HaveInsertPoint())
  1022. EmitStopPoint(&S);
  1023. // HLSL Change Begin - incorporate unconditional branch blocks into loops
  1024. // If it has a continue location, it's a loop
  1025. llvm::BasicBlock *lastContinueBlock = BreakContinueStack.back().ContinueBlock.getBlock();
  1026. if (lastContinueBlock && (BreakContinueStack.size() < 2 ||
  1027. lastContinueBlock != BreakContinueStack.end()[-2].ContinueBlock.getBlock())) {
  1028. // We execute this if
  1029. // - we are in an unnested loop, or
  1030. // - we are in a nested control construct but the continue block of the enclosing loop is different from the current continue block.
  1031. // The second condition can happen for switch statements inside loops, which share the same continue block.
  1032. llvm::BasicBlock *lastBreakBlock = BreakContinueStack.back().BreakBlock.getBlock();
  1033. llvm::BranchInst *condBr = CGM.getHLSLRuntime().EmitHLSLCondBreak(*this, CurFn, lastBreakBlock, lastContinueBlock);
  1034. // Insertion of lifetime.start/end intrinsics may require a cleanup, so we
  1035. // pass the branch that we already generated into the handler.
  1036. EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock, condBr);
  1037. Builder.ClearInsertionPoint();
  1038. } else
  1039. // HLSL Change End - incorporate unconditional branch blocks into loops
  1040. EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock);
  1041. }
  1042. void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
  1043. assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
  1044. // If this code is reachable then emit a stop point (if generating
  1045. // debug info). We have to do this ourselves because we are on the
  1046. // "simple" statement path.
  1047. if (HaveInsertPoint())
  1048. EmitStopPoint(&S);
  1049. EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock);
  1050. }
  1051. /// EmitCaseStmtRange - If case statement range is not too big then
  1052. /// add multiple cases to switch instruction, one for each value within
  1053. /// the range. If range is too big then emit "if" condition check.
  1054. void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
  1055. assert(S.getRHS() && "Expected RHS value in CaseStmt");
  1056. llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
  1057. llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
  1058. // Emit the code for this case. We do this first to make sure it is
  1059. // properly chained from our predecessor before generating the
  1060. // switch machinery to enter this block.
  1061. llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
  1062. EmitBlockWithFallThrough(CaseDest, &S);
  1063. EmitStmt(S.getSubStmt());
  1064. // If range is empty, do nothing.
  1065. if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
  1066. return;
  1067. llvm::APInt Range = RHS - LHS;
  1068. // FIXME: parameters such as this should not be hardcoded.
  1069. if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
  1070. // Range is small enough to add multiple switch instruction cases.
  1071. uint64_t Total = getProfileCount(&S);
  1072. unsigned NCases = Range.getZExtValue() + 1;
  1073. // We only have one region counter for the entire set of cases here, so we
  1074. // need to divide the weights evenly between the generated cases, ensuring
  1075. // that the total weight is preserved. E.g., a weight of 5 over three cases
  1076. // will be distributed as weights of 2, 2, and 1.
  1077. uint64_t Weight = Total / NCases, Rem = Total % NCases;
  1078. for (unsigned I = 0; I != NCases; ++I) {
  1079. if (SwitchWeights)
  1080. SwitchWeights->push_back(Weight + (Rem ? 1 : 0));
  1081. if (Rem)
  1082. Rem--;
  1083. SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
  1084. LHS++;
  1085. }
  1086. return;
  1087. }
  1088. // The range is too big. Emit "if" condition into a new block,
  1089. // making sure to save and restore the current insertion point.
  1090. llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
  1091. // Push this test onto the chain of range checks (which terminates
  1092. // in the default basic block). The switch's default will be changed
  1093. // to the top of this chain after switch emission is complete.
  1094. llvm::BasicBlock *FalseDest = CaseRangeBlock;
  1095. CaseRangeBlock = createBasicBlock("sw.caserange");
  1096. CurFn->getBasicBlockList().push_back(CaseRangeBlock);
  1097. Builder.SetInsertPoint(CaseRangeBlock);
  1098. // Emit range check.
  1099. llvm::Value *Diff =
  1100. Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
  1101. llvm::Value *Cond =
  1102. Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
  1103. llvm::MDNode *Weights = nullptr;
  1104. if (SwitchWeights) {
  1105. uint64_t ThisCount = getProfileCount(&S);
  1106. uint64_t DefaultCount = (*SwitchWeights)[0];
  1107. Weights = createProfileWeights(ThisCount, DefaultCount);
  1108. // Since we're chaining the switch default through each large case range, we
  1109. // need to update the weight for the default, ie, the first case, to include
  1110. // this case.
  1111. (*SwitchWeights)[0] += ThisCount;
  1112. }
  1113. Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights);
  1114. // Restore the appropriate insertion point.
  1115. if (RestoreBB)
  1116. Builder.SetInsertPoint(RestoreBB);
  1117. else
  1118. Builder.ClearInsertionPoint();
  1119. }
  1120. void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
  1121. // If there is no enclosing switch instance that we're aware of, then this
  1122. // case statement and its block can be elided. This situation only happens
  1123. // when we've constant-folded the switch, are emitting the constant case,
  1124. // and part of the constant case includes another case statement. For
  1125. // instance: switch (4) { case 4: do { case 5: } while (1); }
  1126. if (!SwitchInsn) {
  1127. EmitStmt(S.getSubStmt());
  1128. return;
  1129. }
  1130. // Handle case ranges.
  1131. if (S.getRHS()) {
  1132. EmitCaseStmtRange(S);
  1133. return;
  1134. }
  1135. llvm::ConstantInt *CaseVal =
  1136. Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
  1137. // If the body of the case is just a 'break', try to not emit an empty block.
  1138. // If we're profiling or we're not optimizing, leave the block in for better
  1139. // debug and coverage analysis.
  1140. if (!CGM.getCodeGenOpts().ProfileInstrGenerate &&
  1141. CGM.getCodeGenOpts().OptimizationLevel > 0 &&
  1142. isa<BreakStmt>(S.getSubStmt())) {
  1143. JumpDest Block = BreakContinueStack.back().BreakBlock;
  1144. // Only do this optimization if there are no cleanups that need emitting.
  1145. if (isObviouslyBranchWithoutCleanups(Block)) {
  1146. if (SwitchWeights)
  1147. SwitchWeights->push_back(getProfileCount(&S));
  1148. SwitchInsn->addCase(CaseVal, Block.getBlock());
  1149. // If there was a fallthrough into this case, make sure to redirect it to
  1150. // the end of the switch as well.
  1151. if (Builder.GetInsertBlock()) {
  1152. Builder.CreateBr(Block.getBlock());
  1153. Builder.ClearInsertionPoint();
  1154. }
  1155. return;
  1156. }
  1157. }
  1158. llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
  1159. EmitBlockWithFallThrough(CaseDest, &S);
  1160. if (SwitchWeights)
  1161. SwitchWeights->push_back(getProfileCount(&S));
  1162. SwitchInsn->addCase(CaseVal, CaseDest);
  1163. // Recursively emitting the statement is acceptable, but is not wonderful for
  1164. // code where we have many case statements nested together, i.e.:
  1165. // case 1:
  1166. // case 2:
  1167. // case 3: etc.
  1168. // Handling this recursively will create a new block for each case statement
  1169. // that falls through to the next case which is IR intensive. It also causes
  1170. // deep recursion which can run into stack depth limitations. Handle
  1171. // sequential non-range case statements specially.
  1172. const CaseStmt *CurCase = &S;
  1173. const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
  1174. // Otherwise, iteratively add consecutive cases to this switch stmt.
  1175. while (NextCase && NextCase->getRHS() == nullptr) {
  1176. CurCase = NextCase;
  1177. llvm::ConstantInt *CaseVal =
  1178. Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
  1179. if (SwitchWeights)
  1180. SwitchWeights->push_back(getProfileCount(NextCase));
  1181. if (CGM.getCodeGenOpts().ProfileInstrGenerate) {
  1182. CaseDest = createBasicBlock("sw.bb");
  1183. EmitBlockWithFallThrough(CaseDest, &S);
  1184. }
  1185. SwitchInsn->addCase(CaseVal, CaseDest);
  1186. NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
  1187. }
  1188. // Normal default recursion for non-cases.
  1189. EmitStmt(CurCase->getSubStmt());
  1190. }
  1191. void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
  1192. llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
  1193. assert(DefaultBlock->empty() &&
  1194. "EmitDefaultStmt: Default block already defined?");
  1195. EmitBlockWithFallThrough(DefaultBlock, &S);
  1196. EmitStmt(S.getSubStmt());
  1197. }
  1198. /// CollectStatementsForCase - Given the body of a 'switch' statement and a
  1199. /// constant value that is being switched on, see if we can dead code eliminate
  1200. /// the body of the switch to a simple series of statements to emit. Basically,
  1201. /// on a switch (5) we want to find these statements:
  1202. /// case 5:
  1203. /// printf(...); <--
  1204. /// ++i; <--
  1205. /// break;
  1206. ///
  1207. /// and add them to the ResultStmts vector. If it is unsafe to do this
  1208. /// transformation (for example, one of the elided statements contains a label
  1209. /// that might be jumped to), return CSFC_Failure. If we handled it and 'S'
  1210. /// should include statements after it (e.g. the printf() line is a substmt of
  1211. /// the case) then return CSFC_FallThrough. If we handled it and found a break
  1212. /// statement, then return CSFC_Success.
  1213. ///
  1214. /// If Case is non-null, then we are looking for the specified case, checking
  1215. /// that nothing we jump over contains labels. If Case is null, then we found
  1216. /// the case and are looking for the break.
  1217. ///
  1218. /// If the recursive walk actually finds our Case, then we set FoundCase to
  1219. /// true.
  1220. ///
  1221. enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
  1222. static CSFC_Result CollectStatementsForCase(const Stmt *S,
  1223. const SwitchCase *Case,
  1224. bool &FoundCase,
  1225. SmallVectorImpl<const Stmt*> &ResultStmts) {
  1226. // If this is a null statement, just succeed.
  1227. if (!S)
  1228. return Case ? CSFC_Success : CSFC_FallThrough;
  1229. // If this is the switchcase (case 4: or default) that we're looking for, then
  1230. // we're in business. Just add the substatement.
  1231. if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
  1232. if (S == Case) {
  1233. FoundCase = true;
  1234. return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase,
  1235. ResultStmts);
  1236. }
  1237. // Otherwise, this is some other case or default statement, just ignore it.
  1238. return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
  1239. ResultStmts);
  1240. }
  1241. // If we are in the live part of the code and we found our break statement,
  1242. // return a success!
  1243. if (!Case && isa<BreakStmt>(S))
  1244. return CSFC_Success;
  1245. // If this is a switch statement, then it might contain the SwitchCase, the
  1246. // break, or neither.
  1247. if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
  1248. // Handle this as two cases: we might be looking for the SwitchCase (if so
  1249. // the skipped statements must be skippable) or we might already have it.
  1250. CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
  1251. if (Case) {
  1252. // Keep track of whether we see a skipped declaration. The code could be
  1253. // using the declaration even if it is skipped, so we can't optimize out
  1254. // the decl if the kept statements might refer to it.
  1255. bool HadSkippedDecl = false;
  1256. // If we're looking for the case, just see if we can skip each of the
  1257. // substatements.
  1258. for (; Case && I != E; ++I) {
  1259. HadSkippedDecl |= isa<DeclStmt>(*I);
  1260. switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
  1261. case CSFC_Failure: return CSFC_Failure;
  1262. case CSFC_Success:
  1263. // A successful result means that either 1) that the statement doesn't
  1264. // have the case and is skippable, or 2) does contain the case value
  1265. // and also contains the break to exit the switch. In the later case,
  1266. // we just verify the rest of the statements are elidable.
  1267. if (FoundCase) {
  1268. // If we found the case and skipped declarations, we can't do the
  1269. // optimization.
  1270. if (HadSkippedDecl)
  1271. return CSFC_Failure;
  1272. for (++I; I != E; ++I)
  1273. if (CodeGenFunction::ContainsLabel(*I, true))
  1274. return CSFC_Failure;
  1275. return CSFC_Success;
  1276. }
  1277. break;
  1278. case CSFC_FallThrough:
  1279. // If we have a fallthrough condition, then we must have found the
  1280. // case started to include statements. Consider the rest of the
  1281. // statements in the compound statement as candidates for inclusion.
  1282. assert(FoundCase && "Didn't find case but returned fallthrough?");
  1283. // We recursively found Case, so we're not looking for it anymore.
  1284. Case = nullptr;
  1285. // If we found the case and skipped declarations, we can't do the
  1286. // optimization.
  1287. if (HadSkippedDecl)
  1288. return CSFC_Failure;
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. // If we have statements in our range, then we know that the statements are
  1294. // live and need to be added to the set of statements we're tracking.
  1295. for (; I != E; ++I) {
  1296. switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) {
  1297. case CSFC_Failure: return CSFC_Failure;
  1298. case CSFC_FallThrough:
  1299. // A fallthrough result means that the statement was simple and just
  1300. // included in ResultStmt, keep adding them afterwards.
  1301. break;
  1302. case CSFC_Success:
  1303. // A successful result means that we found the break statement and
  1304. // stopped statement inclusion. We just ensure that any leftover stmts
  1305. // are skippable and return success ourselves.
  1306. for (++I; I != E; ++I)
  1307. if (CodeGenFunction::ContainsLabel(*I, true))
  1308. return CSFC_Failure;
  1309. return CSFC_Success;
  1310. }
  1311. }
  1312. return Case ? CSFC_Success : CSFC_FallThrough;
  1313. }
  1314. // Okay, this is some other statement that we don't handle explicitly, like a
  1315. // for statement or increment etc. If we are skipping over this statement,
  1316. // just verify it doesn't have labels, which would make it invalid to elide.
  1317. if (Case) {
  1318. if (CodeGenFunction::ContainsLabel(S, true))
  1319. return CSFC_Failure;
  1320. return CSFC_Success;
  1321. }
  1322. // Otherwise, we want to include this statement. Everything is cool with that
  1323. // so long as it doesn't contain a break out of the switch we're in.
  1324. if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
  1325. // Otherwise, everything is great. Include the statement and tell the caller
  1326. // that we fall through and include the next statement as well.
  1327. ResultStmts.push_back(S);
  1328. return CSFC_FallThrough;
  1329. }
  1330. /// FindCaseStatementsForValue - Find the case statement being jumped to and
  1331. /// then invoke CollectStatementsForCase to find the list of statements to emit
  1332. /// for a switch on constant. See the comment above CollectStatementsForCase
  1333. /// for more details.
  1334. static bool FindCaseStatementsForValue(const SwitchStmt &S,
  1335. const llvm::APSInt &ConstantCondValue,
  1336. SmallVectorImpl<const Stmt*> &ResultStmts,
  1337. ASTContext &C,
  1338. const SwitchCase *&ResultCase) {
  1339. // First step, find the switch case that is being branched to. We can do this
  1340. // efficiently by scanning the SwitchCase list.
  1341. const SwitchCase *Case = S.getSwitchCaseList();
  1342. const DefaultStmt *DefaultCase = nullptr;
  1343. for (; Case; Case = Case->getNextSwitchCase()) {
  1344. // It's either a default or case. Just remember the default statement in
  1345. // case we're not jumping to any numbered cases.
  1346. if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
  1347. DefaultCase = DS;
  1348. continue;
  1349. }
  1350. // Check to see if this case is the one we're looking for.
  1351. const CaseStmt *CS = cast<CaseStmt>(Case);
  1352. // Don't handle case ranges yet.
  1353. if (CS->getRHS()) return false;
  1354. // If we found our case, remember it as 'case'.
  1355. if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
  1356. break;
  1357. }
  1358. // If we didn't find a matching case, we use a default if it exists, or we
  1359. // elide the whole switch body!
  1360. if (!Case) {
  1361. // It is safe to elide the body of the switch if it doesn't contain labels
  1362. // etc. If it is safe, return successfully with an empty ResultStmts list.
  1363. if (!DefaultCase)
  1364. return !CodeGenFunction::ContainsLabel(&S);
  1365. Case = DefaultCase;
  1366. }
  1367. // Ok, we know which case is being jumped to, try to collect all the
  1368. // statements that follow it. This can fail for a variety of reasons. Also,
  1369. // check to see that the recursive walk actually found our case statement.
  1370. // Insane cases like this can fail to find it in the recursive walk since we
  1371. // don't handle every stmt kind:
  1372. // switch (4) {
  1373. // while (1) {
  1374. // case 4: ...
  1375. bool FoundCase = false;
  1376. ResultCase = Case;
  1377. return CollectStatementsForCase(S.getBody(), Case, FoundCase,
  1378. ResultStmts) != CSFC_Failure &&
  1379. FoundCase;
  1380. }
  1381. void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S,
  1382. ArrayRef<const Attr *> Attrs) { // HLSL Change
  1383. // HLSL Change Begins
  1384. // Skip unreachable switch.
  1385. if (!HaveInsertPoint())
  1386. return;
  1387. // HLSL Change Ends
  1388. // Handle nested switch statements.
  1389. llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
  1390. SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights;
  1391. llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
  1392. // See if we can constant fold the condition of the switch and therefore only
  1393. // emit the live case statement (if any) of the switch.
  1394. llvm::APSInt ConstantCondValue;
  1395. if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
  1396. SmallVector<const Stmt*, 4> CaseStmts;
  1397. const SwitchCase *Case = nullptr;
  1398. if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
  1399. getContext(), Case)) {
  1400. if (Case)
  1401. incrementProfileCounter(Case);
  1402. RunCleanupsScope ExecutedScope(*this);
  1403. // Emit the condition variable if needed inside the entire cleanup scope
  1404. // used by this special case for constant folded switches.
  1405. if (S.getConditionVariable())
  1406. EmitAutoVarDecl(*S.getConditionVariable());
  1407. // At this point, we are no longer "within" a switch instance, so
  1408. // we can temporarily enforce this to ensure that any embedded case
  1409. // statements are not emitted.
  1410. SwitchInsn = nullptr;
  1411. // Okay, we can dead code eliminate everything except this case. Emit the
  1412. // specified series of statements and we're good.
  1413. for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
  1414. EmitStmt(CaseStmts[i]);
  1415. incrementProfileCounter(&S);
  1416. // Now we want to restore the saved switch instance so that nested
  1417. // switches continue to function properly
  1418. SwitchInsn = SavedSwitchInsn;
  1419. return;
  1420. }
  1421. }
  1422. JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
  1423. RunCleanupsScope ConditionScope(*this);
  1424. if (S.getConditionVariable())
  1425. EmitAutoVarDecl(*S.getConditionVariable());
  1426. llvm::Value *CondV = EmitScalarExpr(S.getCond());
  1427. // Create basic block to hold stuff that comes after switch
  1428. // statement. We also need to create a default block now so that
  1429. // explicit case ranges tests can have a place to jump to on
  1430. // failure.
  1431. llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
  1432. SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
  1433. // HLSL Change Begins
  1434. llvm::TerminatorInst *TI = cast<llvm::TerminatorInst>(SwitchInsn);
  1435. CGM.getHLSLRuntime().AddControlFlowHint(*this, S, TI, Attrs);
  1436. CGM.getHLSLRuntime().MarkSwitchStmt(*this, SwitchInsn, SwitchExit.getBlock());
  1437. // HLSL Change Ends
  1438. if (PGO.haveRegionCounts()) {
  1439. // Walk the SwitchCase list to find how many there are.
  1440. uint64_t DefaultCount = 0;
  1441. unsigned NumCases = 0;
  1442. for (const SwitchCase *Case = S.getSwitchCaseList();
  1443. Case;
  1444. Case = Case->getNextSwitchCase()) {
  1445. if (isa<DefaultStmt>(Case))
  1446. DefaultCount = getProfileCount(Case);
  1447. NumCases += 1;
  1448. }
  1449. SwitchWeights = new SmallVector<uint64_t, 16>();
  1450. SwitchWeights->reserve(NumCases);
  1451. // The default needs to be first. We store the edge count, so we already
  1452. // know the right weight.
  1453. SwitchWeights->push_back(DefaultCount);
  1454. }
  1455. CaseRangeBlock = DefaultBlock;
  1456. // Clear the insertion point to indicate we are in unreachable code.
  1457. Builder.ClearInsertionPoint();
  1458. // All break statements jump to NextBlock. If BreakContinueStack is non-empty
  1459. // then reuse last ContinueBlock.
  1460. JumpDest OuterContinue;
  1461. if (!BreakContinueStack.empty())
  1462. OuterContinue = BreakContinueStack.back().ContinueBlock;
  1463. BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
  1464. // Emit switch body.
  1465. EmitStmt(S.getBody());
  1466. BreakContinueStack.pop_back();
  1467. // Update the default block in case explicit case range tests have
  1468. // been chained on top.
  1469. SwitchInsn->setDefaultDest(CaseRangeBlock);
  1470. // If a default was never emitted:
  1471. if (!DefaultBlock->getParent()) {
  1472. // If we have cleanups, emit the default block so that there's a
  1473. // place to jump through the cleanups from.
  1474. if (ConditionScope.requiresCleanups()) {
  1475. EmitBlock(DefaultBlock);
  1476. // Otherwise, just forward the default block to the switch end.
  1477. } else {
  1478. DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
  1479. delete DefaultBlock;
  1480. }
  1481. }
  1482. ConditionScope.ForceCleanup();
  1483. // HLSL Change Begin.
  1484. CGM.getHLSLRuntime().MarkScopeEnd(*this);
  1485. // HLSL Change End.
  1486. // Emit continuation.
  1487. EmitBlock(SwitchExit.getBlock(), true);
  1488. incrementProfileCounter(&S);
  1489. if (SwitchWeights) {
  1490. assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() &&
  1491. "switch weights do not match switch cases");
  1492. // If there's only one jump destination there's no sense weighting it.
  1493. if (SwitchWeights->size() > 1)
  1494. SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
  1495. createProfileWeights(*SwitchWeights));
  1496. delete SwitchWeights;
  1497. }
  1498. SwitchInsn = SavedSwitchInsn;
  1499. SwitchWeights = SavedSwitchWeights;
  1500. CaseRangeBlock = SavedCRBlock;
  1501. }
  1502. static std::string
  1503. SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
  1504. SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) {
  1505. std::string Result;
  1506. while (*Constraint) {
  1507. switch (*Constraint) {
  1508. default:
  1509. Result += Target.convertConstraint(Constraint);
  1510. break;
  1511. // Ignore these
  1512. case '*':
  1513. case '?':
  1514. case '!':
  1515. case '=': // Will see this and the following in mult-alt constraints.
  1516. case '+':
  1517. break;
  1518. case '#': // Ignore the rest of the constraint alternative.
  1519. while (Constraint[1] && Constraint[1] != ',')
  1520. Constraint++;
  1521. break;
  1522. case '&':
  1523. case '%':
  1524. Result += *Constraint;
  1525. while (Constraint[1] && Constraint[1] == *Constraint)
  1526. Constraint++;
  1527. break;
  1528. case ',':
  1529. Result += "|";
  1530. break;
  1531. case 'g':
  1532. Result += "imr";
  1533. break;
  1534. case '[': {
  1535. assert(OutCons &&
  1536. "Must pass output names to constraints with a symbolic name");
  1537. unsigned Index;
  1538. bool result = Target.resolveSymbolicName(Constraint,
  1539. &(*OutCons)[0],
  1540. OutCons->size(), Index);
  1541. assert(result && "Could not resolve symbolic name"); (void)result;
  1542. Result += llvm::utostr(Index);
  1543. break;
  1544. }
  1545. }
  1546. Constraint++;
  1547. }
  1548. return Result;
  1549. }
  1550. /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
  1551. /// as using a particular register add that as a constraint that will be used
  1552. /// in this asm stmt.
  1553. static std::string
  1554. AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
  1555. const TargetInfo &Target, CodeGenModule &CGM,
  1556. const AsmStmt &Stmt, const bool EarlyClobber) {
  1557. const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
  1558. if (!AsmDeclRef)
  1559. return Constraint;
  1560. const ValueDecl &Value = *AsmDeclRef->getDecl();
  1561. const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
  1562. if (!Variable)
  1563. return Constraint;
  1564. if (Variable->getStorageClass() != SC_Register)
  1565. return Constraint;
  1566. AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
  1567. if (!Attr)
  1568. return Constraint;
  1569. StringRef Register = Attr->getLabel();
  1570. assert(Target.isValidGCCRegisterName(Register));
  1571. // We're using validateOutputConstraint here because we only care if
  1572. // this is a register constraint.
  1573. TargetInfo::ConstraintInfo Info(Constraint, "");
  1574. if (Target.validateOutputConstraint(Info) &&
  1575. !Info.allowsRegister()) {
  1576. CGM.ErrorUnsupported(&Stmt, "__asm__");
  1577. return Constraint;
  1578. }
  1579. // Canonicalize the register here before returning it.
  1580. Register = Target.getNormalizedGCCRegisterName(Register);
  1581. return (EarlyClobber ? "&{" : "{") + Register.str() + "}";
  1582. }
  1583. llvm::Value*
  1584. CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
  1585. LValue InputValue, QualType InputType,
  1586. std::string &ConstraintStr,
  1587. SourceLocation Loc) {
  1588. llvm::Value *Arg;
  1589. if (Info.allowsRegister() || !Info.allowsMemory()) {
  1590. if (CodeGenFunction::hasScalarEvaluationKind(InputType)) {
  1591. Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal();
  1592. } else {
  1593. llvm::Type *Ty = ConvertType(InputType);
  1594. uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
  1595. if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
  1596. Ty = llvm::IntegerType::get(getLLVMContext(), Size);
  1597. Ty = llvm::PointerType::getUnqual(Ty);
  1598. Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
  1599. Ty));
  1600. } else {
  1601. Arg = InputValue.getAddress();
  1602. ConstraintStr += '*';
  1603. }
  1604. }
  1605. } else {
  1606. Arg = InputValue.getAddress();
  1607. ConstraintStr += '*';
  1608. }
  1609. return Arg;
  1610. }
  1611. llvm::Value* CodeGenFunction::EmitAsmInput(
  1612. const TargetInfo::ConstraintInfo &Info,
  1613. const Expr *InputExpr,
  1614. std::string &ConstraintStr) {
  1615. // If this can't be a register or memory, i.e., has to be a constant
  1616. // (immediate or symbolic), try to emit it as such.
  1617. if (!Info.allowsRegister() && !Info.allowsMemory()) {
  1618. llvm::APSInt Result;
  1619. if (InputExpr->EvaluateAsInt(Result, getContext()))
  1620. return llvm::ConstantInt::get(getLLVMContext(), Result);
  1621. assert(!Info.requiresImmediateConstant() &&
  1622. "Required-immediate inlineasm arg isn't constant?");
  1623. }
  1624. if (Info.allowsRegister() || !Info.allowsMemory())
  1625. if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
  1626. return EmitScalarExpr(InputExpr);
  1627. InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
  1628. LValue Dest = EmitLValue(InputExpr);
  1629. return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
  1630. InputExpr->getExprLoc());
  1631. }
  1632. /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
  1633. /// asm call instruction. The !srcloc MDNode contains a list of constant
  1634. /// integers which are the source locations of the start of each line in the
  1635. /// asm.
  1636. static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
  1637. CodeGenFunction &CGF) {
  1638. SmallVector<llvm::Metadata *, 8> Locs;
  1639. // Add the location of the first line to the MDNode.
  1640. Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
  1641. CGF.Int32Ty, Str->getLocStart().getRawEncoding())));
  1642. StringRef StrVal = Str->getString();
  1643. if (!StrVal.empty()) {
  1644. const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
  1645. const LangOptions &LangOpts = CGF.CGM.getLangOpts();
  1646. // Add the location of the start of each subsequent line of the asm to the
  1647. // MDNode.
  1648. for (unsigned i = 0, e = StrVal.size()-1; i != e; ++i) {
  1649. if (StrVal[i] != '\n') continue;
  1650. SourceLocation LineLoc = Str->getLocationOfByte(i+1, SM, LangOpts,
  1651. CGF.getTarget());
  1652. Locs.push_back(llvm::ConstantAsMetadata::get(
  1653. llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding())));
  1654. }
  1655. }
  1656. return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
  1657. }
  1658. void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
  1659. // Assemble the final asm string.
  1660. std::string AsmString = S.generateAsmString(getContext());
  1661. // Get all the output and input constraints together.
  1662. SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
  1663. SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
  1664. for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
  1665. StringRef Name;
  1666. if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
  1667. Name = GAS->getOutputName(i);
  1668. TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name);
  1669. bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid;
  1670. assert(IsValid && "Failed to parse output constraint");
  1671. OutputConstraintInfos.push_back(Info);
  1672. }
  1673. for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
  1674. StringRef Name;
  1675. if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
  1676. Name = GAS->getInputName(i);
  1677. TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name);
  1678. bool IsValid =
  1679. getTarget().validateInputConstraint(OutputConstraintInfos.data(),
  1680. S.getNumOutputs(), Info);
  1681. assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
  1682. InputConstraintInfos.push_back(Info);
  1683. }
  1684. std::string Constraints;
  1685. std::vector<LValue> ResultRegDests;
  1686. std::vector<QualType> ResultRegQualTys;
  1687. std::vector<llvm::Type *> ResultRegTypes;
  1688. std::vector<llvm::Type *> ResultTruncRegTypes;
  1689. std::vector<llvm::Type *> ArgTypes;
  1690. std::vector<llvm::Value*> Args;
  1691. // Keep track of inout constraints.
  1692. std::string InOutConstraints;
  1693. std::vector<llvm::Value*> InOutArgs;
  1694. std::vector<llvm::Type*> InOutArgTypes;
  1695. // An inline asm can be marked readonly if it meets the following conditions:
  1696. // - it doesn't have any sideeffects
  1697. // - it doesn't clobber memory
  1698. // - it doesn't return a value by-reference
  1699. // It can be marked readnone if it doesn't have any input memory constraints
  1700. // in addition to meeting the conditions listed above.
  1701. bool ReadOnly = true, ReadNone = true;
  1702. for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
  1703. TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
  1704. // Simplify the output constraint.
  1705. std::string OutputConstraint(S.getOutputConstraint(i));
  1706. OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
  1707. getTarget());
  1708. const Expr *OutExpr = S.getOutputExpr(i);
  1709. OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
  1710. OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
  1711. getTarget(), CGM, S,
  1712. Info.earlyClobber());
  1713. LValue Dest = EmitLValue(OutExpr);
  1714. if (!Constraints.empty())
  1715. Constraints += ',';
  1716. // If this is a register output, then make the inline asm return it
  1717. // by-value. If this is a memory result, return the value by-reference.
  1718. if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) {
  1719. Constraints += "=" + OutputConstraint;
  1720. ResultRegQualTys.push_back(OutExpr->getType());
  1721. ResultRegDests.push_back(Dest);
  1722. ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
  1723. ResultTruncRegTypes.push_back(ResultRegTypes.back());
  1724. // If this output is tied to an input, and if the input is larger, then
  1725. // we need to set the actual result type of the inline asm node to be the
  1726. // same as the input type.
  1727. if (Info.hasMatchingInput()) {
  1728. unsigned InputNo;
  1729. for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
  1730. TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
  1731. if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
  1732. break;
  1733. }
  1734. assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
  1735. QualType InputTy = S.getInputExpr(InputNo)->getType();
  1736. QualType OutputType = OutExpr->getType();
  1737. uint64_t InputSize = getContext().getTypeSize(InputTy);
  1738. if (getContext().getTypeSize(OutputType) < InputSize) {
  1739. // Form the asm to return the value as a larger integer or fp type.
  1740. ResultRegTypes.back() = ConvertType(InputTy);
  1741. }
  1742. }
  1743. if (llvm::Type* AdjTy =
  1744. getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
  1745. ResultRegTypes.back()))
  1746. ResultRegTypes.back() = AdjTy;
  1747. else {
  1748. CGM.getDiags().Report(S.getAsmLoc(),
  1749. diag::err_asm_invalid_type_in_input)
  1750. << OutExpr->getType() << OutputConstraint;
  1751. }
  1752. } else {
  1753. ArgTypes.push_back(Dest.getAddress()->getType());
  1754. Args.push_back(Dest.getAddress());
  1755. Constraints += "=*";
  1756. Constraints += OutputConstraint;
  1757. ReadOnly = ReadNone = false;
  1758. }
  1759. if (Info.isReadWrite()) {
  1760. InOutConstraints += ',';
  1761. const Expr *InputExpr = S.getOutputExpr(i);
  1762. llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
  1763. InOutConstraints,
  1764. InputExpr->getExprLoc());
  1765. if (llvm::Type* AdjTy =
  1766. getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
  1767. Arg->getType()))
  1768. Arg = Builder.CreateBitCast(Arg, AdjTy);
  1769. if (Info.allowsRegister())
  1770. InOutConstraints += llvm::utostr(i);
  1771. else
  1772. InOutConstraints += OutputConstraint;
  1773. InOutArgTypes.push_back(Arg->getType());
  1774. InOutArgs.push_back(Arg);
  1775. }
  1776. }
  1777. // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX)
  1778. // to the return value slot. Only do this when returning in registers.
  1779. if (isa<MSAsmStmt>(&S)) {
  1780. const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
  1781. if (RetAI.isDirect() || RetAI.isExtend()) {
  1782. // Make a fake lvalue for the return value slot.
  1783. LValue ReturnSlot = MakeAddrLValue(ReturnValue, FnRetTy);
  1784. CGM.getTargetCodeGenInfo().addReturnRegisterOutputs(
  1785. *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes,
  1786. ResultRegDests, AsmString, S.getNumOutputs());
  1787. SawAsmBlock = true;
  1788. }
  1789. }
  1790. for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
  1791. const Expr *InputExpr = S.getInputExpr(i);
  1792. TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
  1793. if (Info.allowsMemory())
  1794. ReadNone = false;
  1795. if (!Constraints.empty())
  1796. Constraints += ',';
  1797. // Simplify the input constraint.
  1798. std::string InputConstraint(S.getInputConstraint(i));
  1799. InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(),
  1800. &OutputConstraintInfos);
  1801. InputConstraint = AddVariableConstraints(
  1802. InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()),
  1803. getTarget(), CGM, S, false /* No EarlyClobber */);
  1804. llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
  1805. // If this input argument is tied to a larger output result, extend the
  1806. // input to be the same size as the output. The LLVM backend wants to see
  1807. // the input and output of a matching constraint be the same size. Note
  1808. // that GCC does not define what the top bits are here. We use zext because
  1809. // that is usually cheaper, but LLVM IR should really get an anyext someday.
  1810. if (Info.hasTiedOperand()) {
  1811. unsigned Output = Info.getTiedOperand();
  1812. QualType OutputType = S.getOutputExpr(Output)->getType();
  1813. QualType InputTy = InputExpr->getType();
  1814. if (getContext().getTypeSize(OutputType) >
  1815. getContext().getTypeSize(InputTy)) {
  1816. // Use ptrtoint as appropriate so that we can do our extension.
  1817. if (isa<llvm::PointerType>(Arg->getType()))
  1818. Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
  1819. llvm::Type *OutputTy = ConvertType(OutputType);
  1820. if (isa<llvm::IntegerType>(OutputTy))
  1821. Arg = Builder.CreateZExt(Arg, OutputTy);
  1822. else if (isa<llvm::PointerType>(OutputTy))
  1823. Arg = Builder.CreateZExt(Arg, IntPtrTy);
  1824. else {
  1825. assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
  1826. Arg = Builder.CreateFPExt(Arg, OutputTy);
  1827. }
  1828. }
  1829. }
  1830. if (llvm::Type* AdjTy =
  1831. getTargetHooks().adjustInlineAsmType(*this, InputConstraint,
  1832. Arg->getType()))
  1833. Arg = Builder.CreateBitCast(Arg, AdjTy);
  1834. else
  1835. CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input)
  1836. << InputExpr->getType() << InputConstraint;
  1837. ArgTypes.push_back(Arg->getType());
  1838. Args.push_back(Arg);
  1839. Constraints += InputConstraint;
  1840. }
  1841. // Append the "input" part of inout constraints last.
  1842. for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
  1843. ArgTypes.push_back(InOutArgTypes[i]);
  1844. Args.push_back(InOutArgs[i]);
  1845. }
  1846. Constraints += InOutConstraints;
  1847. // Clobbers
  1848. for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
  1849. StringRef Clobber = S.getClobber(i);
  1850. if (Clobber == "memory")
  1851. ReadOnly = ReadNone = false;
  1852. else if (Clobber != "cc")
  1853. Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
  1854. if (!Constraints.empty())
  1855. Constraints += ',';
  1856. Constraints += "~{";
  1857. Constraints += Clobber;
  1858. Constraints += '}';
  1859. }
  1860. // Add machine specific clobbers
  1861. std::string MachineClobbers = getTarget().getClobbers();
  1862. if (!MachineClobbers.empty()) {
  1863. if (!Constraints.empty())
  1864. Constraints += ',';
  1865. Constraints += MachineClobbers;
  1866. }
  1867. llvm::Type *ResultType;
  1868. if (ResultRegTypes.empty())
  1869. ResultType = VoidTy;
  1870. else if (ResultRegTypes.size() == 1)
  1871. ResultType = ResultRegTypes[0];
  1872. else
  1873. ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
  1874. llvm::FunctionType *FTy =
  1875. llvm::FunctionType::get(ResultType, ArgTypes, false);
  1876. bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
  1877. llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
  1878. llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT;
  1879. llvm::InlineAsm *IA =
  1880. llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
  1881. /* IsAlignStack */ false, AsmDialect);
  1882. llvm::CallInst *Result = Builder.CreateCall(IA, Args);
  1883. Result->addAttribute(llvm::AttributeSet::FunctionIndex,
  1884. llvm::Attribute::NoUnwind);
  1885. // Attach readnone and readonly attributes.
  1886. if (!HasSideEffect) {
  1887. if (ReadNone)
  1888. Result->addAttribute(llvm::AttributeSet::FunctionIndex,
  1889. llvm::Attribute::ReadNone);
  1890. else if (ReadOnly)
  1891. Result->addAttribute(llvm::AttributeSet::FunctionIndex,
  1892. llvm::Attribute::ReadOnly);
  1893. }
  1894. // Slap the source location of the inline asm into a !srcloc metadata on the
  1895. // call.
  1896. if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) {
  1897. Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(),
  1898. *this));
  1899. } else {
  1900. // At least put the line number on MS inline asm blobs.
  1901. auto Loc = llvm::ConstantInt::get(Int32Ty, S.getAsmLoc().getRawEncoding());
  1902. Result->setMetadata("srcloc",
  1903. llvm::MDNode::get(getLLVMContext(),
  1904. llvm::ConstantAsMetadata::get(Loc)));
  1905. }
  1906. // Extract all of the register value results from the asm.
  1907. std::vector<llvm::Value*> RegResults;
  1908. if (ResultRegTypes.size() == 1) {
  1909. RegResults.push_back(Result);
  1910. } else {
  1911. for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
  1912. llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult");
  1913. RegResults.push_back(Tmp);
  1914. }
  1915. }
  1916. assert(RegResults.size() == ResultRegTypes.size());
  1917. assert(RegResults.size() == ResultTruncRegTypes.size());
  1918. assert(RegResults.size() == ResultRegDests.size());
  1919. for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
  1920. llvm::Value *Tmp = RegResults[i];
  1921. // If the result type of the LLVM IR asm doesn't match the result type of
  1922. // the expression, do the conversion.
  1923. if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
  1924. llvm::Type *TruncTy = ResultTruncRegTypes[i];
  1925. // Truncate the integer result to the right size, note that TruncTy can be
  1926. // a pointer.
  1927. if (TruncTy->isFloatingPointTy())
  1928. Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
  1929. else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
  1930. uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
  1931. Tmp = Builder.CreateTrunc(Tmp,
  1932. llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
  1933. Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
  1934. } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
  1935. uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
  1936. Tmp = Builder.CreatePtrToInt(Tmp,
  1937. llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
  1938. Tmp = Builder.CreateTrunc(Tmp, TruncTy);
  1939. } else if (TruncTy->isIntegerTy()) {
  1940. Tmp = Builder.CreateTrunc(Tmp, TruncTy);
  1941. } else if (TruncTy->isVectorTy()) {
  1942. Tmp = Builder.CreateBitCast(Tmp, TruncTy);
  1943. }
  1944. }
  1945. EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
  1946. }
  1947. }
  1948. LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) {
  1949. const RecordDecl *RD = S.getCapturedRecordDecl();
  1950. QualType RecordTy = getContext().getRecordType(RD);
  1951. // Initialize the captured struct.
  1952. LValue SlotLV = MakeNaturalAlignAddrLValue(
  1953. CreateMemTemp(RecordTy, "agg.captured"), RecordTy);
  1954. RecordDecl::field_iterator CurField = RD->field_begin();
  1955. for (CapturedStmt::capture_init_iterator I = S.capture_init_begin(),
  1956. E = S.capture_init_end();
  1957. I != E; ++I, ++CurField) {
  1958. LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
  1959. if (CurField->hasCapturedVLAType()) {
  1960. auto VAT = CurField->getCapturedVLAType();
  1961. EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
  1962. } else {
  1963. EmitInitializerForField(*CurField, LV, *I, None);
  1964. }
  1965. }
  1966. return SlotLV;
  1967. }
  1968. /// Generate an outlined function for the body of a CapturedStmt, store any
  1969. /// captured variables into the captured struct, and call the outlined function.
  1970. llvm::Function *
  1971. CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
  1972. LValue CapStruct = InitCapturedStruct(S);
  1973. // Emit the CapturedDecl
  1974. CodeGenFunction CGF(CGM, true);
  1975. CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K));
  1976. llvm::Function *F = CGF.GenerateCapturedStmtFunction(S);
  1977. delete CGF.CapturedStmtInfo;
  1978. // Emit call to the helper function.
  1979. EmitCallOrInvoke(F, CapStruct.getAddress());
  1980. return F;
  1981. }
  1982. llvm::Value *
  1983. CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) {
  1984. LValue CapStruct = InitCapturedStruct(S);
  1985. return CapStruct.getAddress();
  1986. }
  1987. /// Creates the outlined function for a CapturedStmt.
  1988. llvm::Function *
  1989. CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) {
  1990. assert(CapturedStmtInfo &&
  1991. "CapturedStmtInfo should be set when generating the captured function");
  1992. const CapturedDecl *CD = S.getCapturedDecl();
  1993. const RecordDecl *RD = S.getCapturedRecordDecl();
  1994. SourceLocation Loc = S.getLocStart();
  1995. assert(CD->hasBody() && "missing CapturedDecl body");
  1996. // Build the argument list.
  1997. ASTContext &Ctx = CGM.getContext();
  1998. FunctionArgList Args;
  1999. Args.append(CD->param_begin(), CD->param_end());
  2000. // Create the function declaration.
  2001. FunctionType::ExtInfo ExtInfo;
  2002. const CGFunctionInfo &FuncInfo =
  2003. CGM.getTypes().arrangeFreeFunctionDeclaration(Ctx.VoidTy, Args, ExtInfo,
  2004. /*IsVariadic=*/false);
  2005. llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
  2006. llvm::Function *F =
  2007. llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
  2008. CapturedStmtInfo->getHelperName(), &CGM.getModule());
  2009. CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
  2010. if (CD->isNothrow())
  2011. F->addFnAttr(llvm::Attribute::NoUnwind);
  2012. // Generate the function.
  2013. StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args,
  2014. CD->getLocation(),
  2015. CD->getBody()->getLocStart());
  2016. // Set the context parameter in CapturedStmtInfo.
  2017. llvm::Value *DeclPtr = LocalDeclMap[CD->getContextParam()];
  2018. assert(DeclPtr && "missing context parameter for CapturedStmt");
  2019. CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr));
  2020. // Initialize variable-length arrays.
  2021. LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(),
  2022. Ctx.getTagDeclType(RD));
  2023. for (auto *FD : RD->fields()) {
  2024. if (FD->hasCapturedVLAType()) {
  2025. auto *ExprArg = EmitLoadOfLValue(EmitLValueForField(Base, FD),
  2026. S.getLocStart()).getScalarVal();
  2027. auto VAT = FD->getCapturedVLAType();
  2028. VLASizeMap[VAT->getSizeExpr()] = ExprArg;
  2029. }
  2030. }
  2031. // If 'this' is captured, load it into CXXThisValue.
  2032. if (CapturedStmtInfo->isCXXThisExprCaptured()) {
  2033. FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl();
  2034. LValue ThisLValue = EmitLValueForField(Base, FD);
  2035. CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal();
  2036. }
  2037. PGO.assignRegionCounters(CD, F);
  2038. CapturedStmtInfo->EmitBody(*this, CD->getBody());
  2039. FinishFunction(CD->getBodyRBrace());
  2040. return F;
  2041. }