CGStmt.cpp 85 KB

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