CGStmt.cpp 84 KB

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