CGStmt.cpp 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842
  1. //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This contains code to emit Stmt nodes as LLVM code.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CGDebugInfo.h"
  13. #include "CGOpenMPRuntime.h"
  14. #include "CodeGenFunction.h"
  15. #include "CodeGenModule.h"
  16. #include "TargetInfo.h"
  17. #include "clang/AST/Attr.h"
  18. #include "clang/AST/Expr.h"
  19. #include "clang/AST/Stmt.h"
  20. #include "clang/AST/StmtVisitor.h"
  21. #include "clang/Basic/Builtins.h"
  22. #include "clang/Basic/DiagnosticSema.h"
  23. #include "clang/Basic/PrettyStackTrace.h"
  24. #include "clang/Basic/SourceManager.h"
  25. #include "clang/Basic/TargetInfo.h"
  26. #include "llvm/ADT/SmallSet.h"
  27. #include "llvm/ADT/StringExtras.h"
  28. #include "llvm/IR/Assumptions.h"
  29. #include "llvm/IR/DataLayout.h"
  30. #include "llvm/IR/InlineAsm.h"
  31. #include "llvm/IR/Intrinsics.h"
  32. #include "llvm/IR/MDBuilder.h"
  33. #include "llvm/Support/SaveAndRestore.h"
  34. using namespace clang;
  35. using namespace CodeGen;
  36. //===----------------------------------------------------------------------===//
  37. // Statement Emission
  38. //===----------------------------------------------------------------------===//
  39. void CodeGenFunction::EmitStopPoint(const Stmt *S) {
  40. if (CGDebugInfo *DI = getDebugInfo()) {
  41. SourceLocation Loc;
  42. Loc = S->getBeginLoc();
  43. DI->EmitLocation(Builder, Loc);
  44. LastStopPoint = Loc;
  45. }
  46. }
  47. void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
  48. assert(S && "Null statement?");
  49. PGO.setCurrentStmt(S);
  50. // These statements have their own debug info handling.
  51. if (EmitSimpleStmt(S, Attrs))
  52. return;
  53. // Check if we are generating unreachable code.
  54. if (!HaveInsertPoint()) {
  55. // If so, and the statement doesn't contain a label, then we do not need to
  56. // generate actual code. This is safe because (1) the current point is
  57. // unreachable, so we don't need to execute the code, and (2) we've already
  58. // handled the statements which update internal data structures (like the
  59. // local variable map) which could be used by subsequent statements.
  60. if (!ContainsLabel(S)) {
  61. // Verify that any decl statements were handled as simple, they may be in
  62. // scope of subsequent reachable statements.
  63. assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
  64. return;
  65. }
  66. // Otherwise, make a new block to hold the code.
  67. EnsureInsertPoint();
  68. }
  69. // Generate a stoppoint if we are emitting debug info.
  70. EmitStopPoint(S);
  71. // Ignore all OpenMP directives except for simd if OpenMP with Simd is
  72. // enabled.
  73. if (getLangOpts().OpenMP && getLangOpts().OpenMPSimd) {
  74. if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) {
  75. EmitSimpleOMPExecutableDirective(*D);
  76. return;
  77. }
  78. }
  79. switch (S->getStmtClass()) {
  80. case Stmt::NoStmtClass:
  81. case Stmt::CXXCatchStmtClass:
  82. case Stmt::SEHExceptStmtClass:
  83. case Stmt::SEHFinallyStmtClass:
  84. case Stmt::MSDependentExistsStmtClass:
  85. llvm_unreachable("invalid statement class to emit generically");
  86. case Stmt::NullStmtClass:
  87. case Stmt::CompoundStmtClass:
  88. case Stmt::DeclStmtClass:
  89. case Stmt::LabelStmtClass:
  90. case Stmt::AttributedStmtClass:
  91. case Stmt::GotoStmtClass:
  92. case Stmt::BreakStmtClass:
  93. case Stmt::ContinueStmtClass:
  94. case Stmt::DefaultStmtClass:
  95. case Stmt::CaseStmtClass:
  96. case Stmt::SEHLeaveStmtClass:
  97. llvm_unreachable("should have emitted these statements as simple");
  98. #define STMT(Type, Base)
  99. #define ABSTRACT_STMT(Op)
  100. #define EXPR(Type, Base) \
  101. case Stmt::Type##Class:
  102. #include "clang/AST/StmtNodes.inc"
  103. {
  104. // Remember the block we came in on.
  105. llvm::BasicBlock *incoming = Builder.GetInsertBlock();
  106. assert(incoming && "expression emission must have an insertion point");
  107. EmitIgnoredExpr(cast<Expr>(S));
  108. llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
  109. assert(outgoing && "expression emission cleared block!");
  110. // The expression emitters assume (reasonably!) that the insertion
  111. // point is always set. To maintain that, the call-emission code
  112. // for noreturn functions has to enter a new block with no
  113. // predecessors. We want to kill that block and mark the current
  114. // insertion point unreachable in the common case of a call like
  115. // "exit();". Since expression emission doesn't otherwise create
  116. // blocks with no predecessors, we can just test for that.
  117. // However, we must be careful not to do this to our incoming
  118. // block, because *statement* emission does sometimes create
  119. // reachable blocks which will have no predecessors until later in
  120. // the function. This occurs with, e.g., labels that are not
  121. // reachable by fallthrough.
  122. if (incoming != outgoing && outgoing->use_empty()) {
  123. outgoing->eraseFromParent();
  124. Builder.ClearInsertionPoint();
  125. }
  126. break;
  127. }
  128. case Stmt::IndirectGotoStmtClass:
  129. EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
  130. case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
  131. case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break;
  132. case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break;
  133. case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break;
  134. case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
  135. case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
  136. case Stmt::GCCAsmStmtClass: // Intentional fall-through.
  137. case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
  138. case Stmt::CoroutineBodyStmtClass:
  139. EmitCoroutineBody(cast<CoroutineBodyStmt>(*S));
  140. break;
  141. case Stmt::CoreturnStmtClass:
  142. EmitCoreturnStmt(cast<CoreturnStmt>(*S));
  143. break;
  144. case Stmt::CapturedStmtClass: {
  145. const CapturedStmt *CS = cast<CapturedStmt>(S);
  146. EmitCapturedStmt(*CS, CS->getCapturedRegionKind());
  147. }
  148. break;
  149. case Stmt::ObjCAtTryStmtClass:
  150. EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
  151. break;
  152. case Stmt::ObjCAtCatchStmtClass:
  153. llvm_unreachable(
  154. "@catch statements should be handled by EmitObjCAtTryStmt");
  155. case Stmt::ObjCAtFinallyStmtClass:
  156. llvm_unreachable(
  157. "@finally statements should be handled by EmitObjCAtTryStmt");
  158. case Stmt::ObjCAtThrowStmtClass:
  159. EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
  160. break;
  161. case Stmt::ObjCAtSynchronizedStmtClass:
  162. EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
  163. break;
  164. case Stmt::ObjCForCollectionStmtClass:
  165. EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
  166. break;
  167. case Stmt::ObjCAutoreleasePoolStmtClass:
  168. EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
  169. break;
  170. case Stmt::CXXTryStmtClass:
  171. EmitCXXTryStmt(cast<CXXTryStmt>(*S));
  172. break;
  173. case Stmt::CXXForRangeStmtClass:
  174. EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs);
  175. break;
  176. case Stmt::SEHTryStmtClass:
  177. EmitSEHTryStmt(cast<SEHTryStmt>(*S));
  178. break;
  179. case Stmt::OMPMetaDirectiveClass:
  180. EmitOMPMetaDirective(cast<OMPMetaDirective>(*S));
  181. break;
  182. case Stmt::OMPCanonicalLoopClass:
  183. EmitOMPCanonicalLoop(cast<OMPCanonicalLoop>(S));
  184. break;
  185. case Stmt::OMPParallelDirectiveClass:
  186. EmitOMPParallelDirective(cast<OMPParallelDirective>(*S));
  187. break;
  188. case Stmt::OMPSimdDirectiveClass:
  189. EmitOMPSimdDirective(cast<OMPSimdDirective>(*S));
  190. break;
  191. case Stmt::OMPTileDirectiveClass:
  192. EmitOMPTileDirective(cast<OMPTileDirective>(*S));
  193. break;
  194. case Stmt::OMPUnrollDirectiveClass:
  195. EmitOMPUnrollDirective(cast<OMPUnrollDirective>(*S));
  196. break;
  197. case Stmt::OMPForDirectiveClass:
  198. EmitOMPForDirective(cast<OMPForDirective>(*S));
  199. break;
  200. case Stmt::OMPForSimdDirectiveClass:
  201. EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S));
  202. break;
  203. case Stmt::OMPSectionsDirectiveClass:
  204. EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S));
  205. break;
  206. case Stmt::OMPSectionDirectiveClass:
  207. EmitOMPSectionDirective(cast<OMPSectionDirective>(*S));
  208. break;
  209. case Stmt::OMPSingleDirectiveClass:
  210. EmitOMPSingleDirective(cast<OMPSingleDirective>(*S));
  211. break;
  212. case Stmt::OMPMasterDirectiveClass:
  213. EmitOMPMasterDirective(cast<OMPMasterDirective>(*S));
  214. break;
  215. case Stmt::OMPCriticalDirectiveClass:
  216. EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S));
  217. break;
  218. case Stmt::OMPParallelForDirectiveClass:
  219. EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S));
  220. break;
  221. case Stmt::OMPParallelForSimdDirectiveClass:
  222. EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S));
  223. break;
  224. case Stmt::OMPParallelMasterDirectiveClass:
  225. EmitOMPParallelMasterDirective(cast<OMPParallelMasterDirective>(*S));
  226. break;
  227. case Stmt::OMPParallelSectionsDirectiveClass:
  228. EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S));
  229. break;
  230. case Stmt::OMPTaskDirectiveClass:
  231. EmitOMPTaskDirective(cast<OMPTaskDirective>(*S));
  232. break;
  233. case Stmt::OMPTaskyieldDirectiveClass:
  234. EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S));
  235. break;
  236. case Stmt::OMPBarrierDirectiveClass:
  237. EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S));
  238. break;
  239. case Stmt::OMPTaskwaitDirectiveClass:
  240. EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S));
  241. break;
  242. case Stmt::OMPTaskgroupDirectiveClass:
  243. EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S));
  244. break;
  245. case Stmt::OMPFlushDirectiveClass:
  246. EmitOMPFlushDirective(cast<OMPFlushDirective>(*S));
  247. break;
  248. case Stmt::OMPDepobjDirectiveClass:
  249. EmitOMPDepobjDirective(cast<OMPDepobjDirective>(*S));
  250. break;
  251. case Stmt::OMPScanDirectiveClass:
  252. EmitOMPScanDirective(cast<OMPScanDirective>(*S));
  253. break;
  254. case Stmt::OMPOrderedDirectiveClass:
  255. EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S));
  256. break;
  257. case Stmt::OMPAtomicDirectiveClass:
  258. EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S));
  259. break;
  260. case Stmt::OMPTargetDirectiveClass:
  261. EmitOMPTargetDirective(cast<OMPTargetDirective>(*S));
  262. break;
  263. case Stmt::OMPTeamsDirectiveClass:
  264. EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S));
  265. break;
  266. case Stmt::OMPCancellationPointDirectiveClass:
  267. EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S));
  268. break;
  269. case Stmt::OMPCancelDirectiveClass:
  270. EmitOMPCancelDirective(cast<OMPCancelDirective>(*S));
  271. break;
  272. case Stmt::OMPTargetDataDirectiveClass:
  273. EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S));
  274. break;
  275. case Stmt::OMPTargetEnterDataDirectiveClass:
  276. EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S));
  277. break;
  278. case Stmt::OMPTargetExitDataDirectiveClass:
  279. EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S));
  280. break;
  281. case Stmt::OMPTargetParallelDirectiveClass:
  282. EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S));
  283. break;
  284. case Stmt::OMPTargetParallelForDirectiveClass:
  285. EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S));
  286. break;
  287. case Stmt::OMPTaskLoopDirectiveClass:
  288. EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S));
  289. break;
  290. case Stmt::OMPTaskLoopSimdDirectiveClass:
  291. EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S));
  292. break;
  293. case Stmt::OMPMasterTaskLoopDirectiveClass:
  294. EmitOMPMasterTaskLoopDirective(cast<OMPMasterTaskLoopDirective>(*S));
  295. break;
  296. case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
  297. EmitOMPMasterTaskLoopSimdDirective(
  298. cast<OMPMasterTaskLoopSimdDirective>(*S));
  299. break;
  300. case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
  301. EmitOMPParallelMasterTaskLoopDirective(
  302. cast<OMPParallelMasterTaskLoopDirective>(*S));
  303. break;
  304. case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
  305. EmitOMPParallelMasterTaskLoopSimdDirective(
  306. cast<OMPParallelMasterTaskLoopSimdDirective>(*S));
  307. break;
  308. case Stmt::OMPDistributeDirectiveClass:
  309. EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S));
  310. break;
  311. case Stmt::OMPTargetUpdateDirectiveClass:
  312. EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S));
  313. break;
  314. case Stmt::OMPDistributeParallelForDirectiveClass:
  315. EmitOMPDistributeParallelForDirective(
  316. cast<OMPDistributeParallelForDirective>(*S));
  317. break;
  318. case Stmt::OMPDistributeParallelForSimdDirectiveClass:
  319. EmitOMPDistributeParallelForSimdDirective(
  320. cast<OMPDistributeParallelForSimdDirective>(*S));
  321. break;
  322. case Stmt::OMPDistributeSimdDirectiveClass:
  323. EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S));
  324. break;
  325. case Stmt::OMPTargetParallelForSimdDirectiveClass:
  326. EmitOMPTargetParallelForSimdDirective(
  327. cast<OMPTargetParallelForSimdDirective>(*S));
  328. break;
  329. case Stmt::OMPTargetSimdDirectiveClass:
  330. EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S));
  331. break;
  332. case Stmt::OMPTeamsDistributeDirectiveClass:
  333. EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S));
  334. break;
  335. case Stmt::OMPTeamsDistributeSimdDirectiveClass:
  336. EmitOMPTeamsDistributeSimdDirective(
  337. cast<OMPTeamsDistributeSimdDirective>(*S));
  338. break;
  339. case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
  340. EmitOMPTeamsDistributeParallelForSimdDirective(
  341. cast<OMPTeamsDistributeParallelForSimdDirective>(*S));
  342. break;
  343. case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
  344. EmitOMPTeamsDistributeParallelForDirective(
  345. cast<OMPTeamsDistributeParallelForDirective>(*S));
  346. break;
  347. case Stmt::OMPTargetTeamsDirectiveClass:
  348. EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S));
  349. break;
  350. case Stmt::OMPTargetTeamsDistributeDirectiveClass:
  351. EmitOMPTargetTeamsDistributeDirective(
  352. cast<OMPTargetTeamsDistributeDirective>(*S));
  353. break;
  354. case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
  355. EmitOMPTargetTeamsDistributeParallelForDirective(
  356. cast<OMPTargetTeamsDistributeParallelForDirective>(*S));
  357. break;
  358. case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
  359. EmitOMPTargetTeamsDistributeParallelForSimdDirective(
  360. cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S));
  361. break;
  362. case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
  363. EmitOMPTargetTeamsDistributeSimdDirective(
  364. cast<OMPTargetTeamsDistributeSimdDirective>(*S));
  365. break;
  366. case Stmt::OMPInteropDirectiveClass:
  367. EmitOMPInteropDirective(cast<OMPInteropDirective>(*S));
  368. break;
  369. case Stmt::OMPDispatchDirectiveClass:
  370. llvm_unreachable("Dispatch directive not supported yet.");
  371. break;
  372. case Stmt::OMPMaskedDirectiveClass:
  373. EmitOMPMaskedDirective(cast<OMPMaskedDirective>(*S));
  374. break;
  375. case Stmt::OMPGenericLoopDirectiveClass:
  376. EmitOMPGenericLoopDirective(cast<OMPGenericLoopDirective>(*S));
  377. break;
  378. }
  379. }
  380. bool CodeGenFunction::EmitSimpleStmt(const Stmt *S,
  381. ArrayRef<const Attr *> Attrs) {
  382. switch (S->getStmtClass()) {
  383. default:
  384. return false;
  385. case Stmt::NullStmtClass:
  386. break;
  387. case Stmt::CompoundStmtClass:
  388. EmitCompoundStmt(cast<CompoundStmt>(*S));
  389. break;
  390. case Stmt::DeclStmtClass:
  391. EmitDeclStmt(cast<DeclStmt>(*S));
  392. break;
  393. case Stmt::LabelStmtClass:
  394. EmitLabelStmt(cast<LabelStmt>(*S));
  395. break;
  396. case Stmt::AttributedStmtClass:
  397. EmitAttributedStmt(cast<AttributedStmt>(*S));
  398. break;
  399. case Stmt::GotoStmtClass:
  400. EmitGotoStmt(cast<GotoStmt>(*S));
  401. break;
  402. case Stmt::BreakStmtClass:
  403. EmitBreakStmt(cast<BreakStmt>(*S));
  404. break;
  405. case Stmt::ContinueStmtClass:
  406. EmitContinueStmt(cast<ContinueStmt>(*S));
  407. break;
  408. case Stmt::DefaultStmtClass:
  409. EmitDefaultStmt(cast<DefaultStmt>(*S), Attrs);
  410. break;
  411. case Stmt::CaseStmtClass:
  412. EmitCaseStmt(cast<CaseStmt>(*S), Attrs);
  413. break;
  414. case Stmt::SEHLeaveStmtClass:
  415. EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S));
  416. break;
  417. }
  418. return true;
  419. }
  420. /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
  421. /// this captures the expression result of the last sub-statement and returns it
  422. /// (for use by the statement expression extension).
  423. Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
  424. AggValueSlot AggSlot) {
  425. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
  426. "LLVM IR generation of compound statement ('{}')");
  427. // Keep track of the current cleanup stack depth, including debug scopes.
  428. LexicalScope Scope(*this, S.getSourceRange());
  429. return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
  430. }
  431. Address
  432. CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
  433. bool GetLast,
  434. AggValueSlot AggSlot) {
  435. const Stmt *ExprResult = S.getStmtExprResult();
  436. assert((!GetLast || (GetLast && ExprResult)) &&
  437. "If GetLast is true then the CompoundStmt must have a StmtExprResult");
  438. Address RetAlloca = Address::invalid();
  439. for (auto *CurStmt : S.body()) {
  440. if (GetLast && ExprResult == CurStmt) {
  441. // We have to special case labels here. They are statements, but when put
  442. // at the end of a statement expression, they yield the value of their
  443. // subexpression. Handle this by walking through all labels we encounter,
  444. // emitting them before we evaluate the subexpr.
  445. // Similar issues arise for attributed statements.
  446. while (!isa<Expr>(ExprResult)) {
  447. if (const auto *LS = dyn_cast<LabelStmt>(ExprResult)) {
  448. EmitLabel(LS->getDecl());
  449. ExprResult = LS->getSubStmt();
  450. } else if (const auto *AS = dyn_cast<AttributedStmt>(ExprResult)) {
  451. // FIXME: Update this if we ever have attributes that affect the
  452. // semantics of an expression.
  453. ExprResult = AS->getSubStmt();
  454. } else {
  455. llvm_unreachable("unknown value statement");
  456. }
  457. }
  458. EnsureInsertPoint();
  459. const Expr *E = cast<Expr>(ExprResult);
  460. QualType ExprTy = E->getType();
  461. if (hasAggregateEvaluationKind(ExprTy)) {
  462. EmitAggExpr(E, AggSlot);
  463. } else {
  464. // We can't return an RValue here because there might be cleanups at
  465. // the end of the StmtExpr. Because of that, we have to emit the result
  466. // here into a temporary alloca.
  467. RetAlloca = CreateMemTemp(ExprTy);
  468. EmitAnyExprToMem(E, RetAlloca, Qualifiers(),
  469. /*IsInit*/ false);
  470. }
  471. } else {
  472. EmitStmt(CurStmt);
  473. }
  474. }
  475. return RetAlloca;
  476. }
  477. void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
  478. llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
  479. // If there is a cleanup stack, then we it isn't worth trying to
  480. // simplify this block (we would need to remove it from the scope map
  481. // and cleanup entry).
  482. if (!EHStack.empty())
  483. return;
  484. // Can only simplify direct branches.
  485. if (!BI || !BI->isUnconditional())
  486. return;
  487. // Can only simplify empty blocks.
  488. if (BI->getIterator() != BB->begin())
  489. return;
  490. BB->replaceAllUsesWith(BI->getSuccessor(0));
  491. BI->eraseFromParent();
  492. BB->eraseFromParent();
  493. }
  494. void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
  495. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  496. // Fall out of the current block (if necessary).
  497. EmitBranch(BB);
  498. if (IsFinished && BB->use_empty()) {
  499. delete BB;
  500. return;
  501. }
  502. // Place the block after the current block, if possible, or else at
  503. // the end of the function.
  504. if (CurBB && CurBB->getParent())
  505. CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB);
  506. else
  507. CurFn->getBasicBlockList().push_back(BB);
  508. Builder.SetInsertPoint(BB);
  509. }
  510. void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
  511. // Emit a branch from the current block to the target one if this
  512. // was a real block. If this was just a fall-through block after a
  513. // terminator, don't emit it.
  514. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  515. if (!CurBB || CurBB->getTerminator()) {
  516. // If there is no insert point or the previous block is already
  517. // terminated, don't touch it.
  518. } else {
  519. // Otherwise, create a fall-through branch.
  520. Builder.CreateBr(Target);
  521. }
  522. Builder.ClearInsertionPoint();
  523. }
  524. void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
  525. bool inserted = false;
  526. for (llvm::User *u : block->users()) {
  527. if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) {
  528. CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(),
  529. block);
  530. inserted = true;
  531. break;
  532. }
  533. }
  534. if (!inserted)
  535. CurFn->getBasicBlockList().push_back(block);
  536. Builder.SetInsertPoint(block);
  537. }
  538. CodeGenFunction::JumpDest
  539. CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
  540. JumpDest &Dest = LabelMap[D];
  541. if (Dest.isValid()) return Dest;
  542. // Create, but don't insert, the new block.
  543. Dest = JumpDest(createBasicBlock(D->getName()),
  544. EHScopeStack::stable_iterator::invalid(),
  545. NextCleanupDestIndex++);
  546. return Dest;
  547. }
  548. void CodeGenFunction::EmitLabel(const LabelDecl *D) {
  549. // Add this label to the current lexical scope if we're within any
  550. // normal cleanups. Jumps "in" to this label --- when permitted by
  551. // the language --- may need to be routed around such cleanups.
  552. if (EHStack.hasNormalCleanups() && CurLexicalScope)
  553. CurLexicalScope->addLabel(D);
  554. JumpDest &Dest = LabelMap[D];
  555. // If we didn't need a forward reference to this label, just go
  556. // ahead and create a destination at the current scope.
  557. if (!Dest.isValid()) {
  558. Dest = getJumpDestInCurrentScope(D->getName());
  559. // Otherwise, we need to give this label a target depth and remove
  560. // it from the branch-fixups list.
  561. } else {
  562. assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
  563. Dest.setScopeDepth(EHStack.stable_begin());
  564. ResolveBranchFixups(Dest.getBlock());
  565. }
  566. EmitBlock(Dest.getBlock());
  567. // Emit debug info for labels.
  568. if (CGDebugInfo *DI = getDebugInfo()) {
  569. if (CGM.getCodeGenOpts().hasReducedDebugInfo()) {
  570. DI->setLocation(D->getLocation());
  571. DI->EmitLabel(D, Builder);
  572. }
  573. }
  574. incrementProfileCounter(D->getStmt());
  575. }
  576. /// Change the cleanup scope of the labels in this lexical scope to
  577. /// match the scope of the enclosing context.
  578. void CodeGenFunction::LexicalScope::rescopeLabels() {
  579. assert(!Labels.empty());
  580. EHScopeStack::stable_iterator innermostScope
  581. = CGF.EHStack.getInnermostNormalCleanup();
  582. // Change the scope depth of all the labels.
  583. for (SmallVectorImpl<const LabelDecl*>::const_iterator
  584. i = Labels.begin(), e = Labels.end(); i != e; ++i) {
  585. assert(CGF.LabelMap.count(*i));
  586. JumpDest &dest = CGF.LabelMap.find(*i)->second;
  587. assert(dest.getScopeDepth().isValid());
  588. assert(innermostScope.encloses(dest.getScopeDepth()));
  589. dest.setScopeDepth(innermostScope);
  590. }
  591. // Reparent the labels if the new scope also has cleanups.
  592. if (innermostScope != EHScopeStack::stable_end() && ParentScope) {
  593. ParentScope->Labels.append(Labels.begin(), Labels.end());
  594. }
  595. }
  596. void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
  597. EmitLabel(S.getDecl());
  598. // IsEHa - emit eha.scope.begin if it's a side entry of a scope
  599. if (getLangOpts().EHAsynch && S.isSideEntry())
  600. EmitSehCppScopeBegin();
  601. EmitStmt(S.getSubStmt());
  602. }
  603. void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
  604. bool nomerge = false;
  605. const CallExpr *musttail = nullptr;
  606. for (const auto *A : S.getAttrs()) {
  607. if (A->getKind() == attr::NoMerge) {
  608. nomerge = true;
  609. }
  610. if (A->getKind() == attr::MustTail) {
  611. const Stmt *Sub = S.getSubStmt();
  612. const ReturnStmt *R = cast<ReturnStmt>(Sub);
  613. musttail = cast<CallExpr>(R->getRetValue()->IgnoreParens());
  614. }
  615. }
  616. SaveAndRestore<bool> save_nomerge(InNoMergeAttributedStmt, nomerge);
  617. SaveAndRestore<const CallExpr *> save_musttail(MustTailCall, musttail);
  618. EmitStmt(S.getSubStmt(), S.getAttrs());
  619. }
  620. void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
  621. // If this code is reachable then emit a stop point (if generating
  622. // debug info). We have to do this ourselves because we are on the
  623. // "simple" statement path.
  624. if (HaveInsertPoint())
  625. EmitStopPoint(&S);
  626. EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
  627. }
  628. void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
  629. if (const LabelDecl *Target = S.getConstantTarget()) {
  630. EmitBranchThroughCleanup(getJumpDestForLabel(Target));
  631. return;
  632. }
  633. // Ensure that we have an i8* for our PHI node.
  634. llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
  635. Int8PtrTy, "addr");
  636. llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
  637. // Get the basic block for the indirect goto.
  638. llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
  639. // The first instruction in the block has to be the PHI for the switch dest,
  640. // add an entry for this branch.
  641. cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
  642. EmitBranch(IndGotoBB);
  643. }
  644. void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
  645. // The else branch of a consteval if statement is always the only branch that
  646. // can be runtime evaluated.
  647. if (S.isConsteval()) {
  648. const Stmt *Executed = S.isNegatedConsteval() ? S.getThen() : S.getElse();
  649. if (Executed) {
  650. RunCleanupsScope ExecutedScope(*this);
  651. EmitStmt(Executed);
  652. }
  653. return;
  654. }
  655. // C99 6.8.4.1: The first substatement is executed if the expression compares
  656. // unequal to 0. The condition must be a scalar type.
  657. LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
  658. if (S.getInit())
  659. EmitStmt(S.getInit());
  660. if (S.getConditionVariable())
  661. EmitDecl(*S.getConditionVariable());
  662. // If the condition constant folds and can be elided, try to avoid emitting
  663. // the condition and the dead arm of the if/else.
  664. bool CondConstant;
  665. if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant,
  666. S.isConstexpr())) {
  667. // Figure out which block (then or else) is executed.
  668. const Stmt *Executed = S.getThen();
  669. const Stmt *Skipped = S.getElse();
  670. if (!CondConstant) // Condition false?
  671. std::swap(Executed, Skipped);
  672. // If the skipped block has no labels in it, just emit the executed block.
  673. // This avoids emitting dead code and simplifies the CFG substantially.
  674. if (S.isConstexpr() || !ContainsLabel(Skipped)) {
  675. if (CondConstant)
  676. incrementProfileCounter(&S);
  677. if (Executed) {
  678. RunCleanupsScope ExecutedScope(*this);
  679. EmitStmt(Executed);
  680. }
  681. return;
  682. }
  683. }
  684. // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
  685. // the conditional branch.
  686. llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
  687. llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
  688. llvm::BasicBlock *ElseBlock = ContBlock;
  689. if (S.getElse())
  690. ElseBlock = createBasicBlock("if.else");
  691. // Prefer the PGO based weights over the likelihood attribute.
  692. // When the build isn't optimized the metadata isn't used, so don't generate
  693. // it.
  694. Stmt::Likelihood LH = Stmt::LH_None;
  695. uint64_t Count = getProfileCount(S.getThen());
  696. if (!Count && CGM.getCodeGenOpts().OptimizationLevel)
  697. LH = Stmt::getLikelihood(S.getThen(), S.getElse());
  698. EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, Count, LH);
  699. // Emit the 'then' code.
  700. EmitBlock(ThenBlock);
  701. incrementProfileCounter(&S);
  702. {
  703. RunCleanupsScope ThenScope(*this);
  704. EmitStmt(S.getThen());
  705. }
  706. EmitBranch(ContBlock);
  707. // Emit the 'else' code if present.
  708. if (const Stmt *Else = S.getElse()) {
  709. {
  710. // There is no need to emit line number for an unconditional branch.
  711. auto NL = ApplyDebugLocation::CreateEmpty(*this);
  712. EmitBlock(ElseBlock);
  713. }
  714. {
  715. RunCleanupsScope ElseScope(*this);
  716. EmitStmt(Else);
  717. }
  718. {
  719. // There is no need to emit line number for an unconditional branch.
  720. auto NL = ApplyDebugLocation::CreateEmpty(*this);
  721. EmitBranch(ContBlock);
  722. }
  723. }
  724. // Emit the continuation block for code after the if.
  725. EmitBlock(ContBlock, true);
  726. }
  727. void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
  728. ArrayRef<const Attr *> WhileAttrs) {
  729. // Emit the header for the loop, which will also become
  730. // the continue target.
  731. JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
  732. EmitBlock(LoopHeader.getBlock());
  733. // Create an exit block for when the condition fails, which will
  734. // also become the break target.
  735. JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
  736. // Store the blocks to use for break and continue.
  737. BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
  738. // C++ [stmt.while]p2:
  739. // When the condition of a while statement is a declaration, the
  740. // scope of the variable that is declared extends from its point
  741. // of declaration (3.3.2) to the end of the while statement.
  742. // [...]
  743. // The object created in a condition is destroyed and created
  744. // with each iteration of the loop.
  745. RunCleanupsScope ConditionScope(*this);
  746. if (S.getConditionVariable())
  747. EmitDecl(*S.getConditionVariable());
  748. // Evaluate the conditional in the while header. C99 6.8.5.1: The
  749. // evaluation of the controlling expression takes place before each
  750. // execution of the loop body.
  751. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  752. // while(1) is common, avoid extra exit blocks. Be sure
  753. // to correctly handle break/continue though.
  754. llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal);
  755. bool CondIsConstInt = C != nullptr;
  756. bool EmitBoolCondBranch = !CondIsConstInt || !C->isOne();
  757. const SourceRange &R = S.getSourceRange();
  758. LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), CGM.getCodeGenOpts(),
  759. WhileAttrs, SourceLocToDebugLoc(R.getBegin()),
  760. SourceLocToDebugLoc(R.getEnd()),
  761. checkIfLoopMustProgress(CondIsConstInt));
  762. // As long as the condition is true, go to the loop body.
  763. llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
  764. if (EmitBoolCondBranch) {
  765. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  766. if (ConditionScope.requiresCleanups())
  767. ExitBlock = createBasicBlock("while.exit");
  768. llvm::MDNode *Weights =
  769. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()));
  770. if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
  771. BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
  772. BoolCondVal, Stmt::getLikelihood(S.getBody()));
  773. Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights);
  774. if (ExitBlock != LoopExit.getBlock()) {
  775. EmitBlock(ExitBlock);
  776. EmitBranchThroughCleanup(LoopExit);
  777. }
  778. } else if (const Attr *A = Stmt::getLikelihoodAttr(S.getBody())) {
  779. CGM.getDiags().Report(A->getLocation(),
  780. diag::warn_attribute_has_no_effect_on_infinite_loop)
  781. << A << A->getRange();
  782. CGM.getDiags().Report(
  783. S.getWhileLoc(),
  784. diag::note_attribute_has_no_effect_on_infinite_loop_here)
  785. << SourceRange(S.getWhileLoc(), S.getRParenLoc());
  786. }
  787. // Emit the loop body. We have to emit this in a cleanup scope
  788. // because it might be a singleton DeclStmt.
  789. {
  790. RunCleanupsScope BodyScope(*this);
  791. EmitBlock(LoopBody);
  792. incrementProfileCounter(&S);
  793. EmitStmt(S.getBody());
  794. }
  795. BreakContinueStack.pop_back();
  796. // Immediately force cleanup.
  797. ConditionScope.ForceCleanup();
  798. EmitStopPoint(&S);
  799. // Branch to the loop header again.
  800. EmitBranch(LoopHeader.getBlock());
  801. LoopStack.pop();
  802. // Emit the exit block.
  803. EmitBlock(LoopExit.getBlock(), true);
  804. // The LoopHeader typically is just a branch if we skipped emitting
  805. // a branch, try to erase it.
  806. if (!EmitBoolCondBranch)
  807. SimplifyForwardingBlocks(LoopHeader.getBlock());
  808. }
  809. void CodeGenFunction::EmitDoStmt(const DoStmt &S,
  810. ArrayRef<const Attr *> DoAttrs) {
  811. JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
  812. JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
  813. uint64_t ParentCount = getCurrentProfileCount();
  814. // Store the blocks to use for break and continue.
  815. BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
  816. // Emit the body of the loop.
  817. llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
  818. EmitBlockWithFallThrough(LoopBody, &S);
  819. {
  820. RunCleanupsScope BodyScope(*this);
  821. EmitStmt(S.getBody());
  822. }
  823. EmitBlock(LoopCond.getBlock());
  824. // C99 6.8.5.2: "The evaluation of the controlling expression takes place
  825. // after each execution of the loop body."
  826. // Evaluate the conditional in the while header.
  827. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  828. // compares unequal to 0. The condition must be a scalar type.
  829. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  830. BreakContinueStack.pop_back();
  831. // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
  832. // to correctly handle break/continue though.
  833. llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal);
  834. bool CondIsConstInt = C;
  835. bool EmitBoolCondBranch = !C || !C->isZero();
  836. const SourceRange &R = S.getSourceRange();
  837. LoopStack.push(LoopBody, CGM.getContext(), CGM.getCodeGenOpts(), DoAttrs,
  838. SourceLocToDebugLoc(R.getBegin()),
  839. SourceLocToDebugLoc(R.getEnd()),
  840. checkIfLoopMustProgress(CondIsConstInt));
  841. // As long as the condition is true, iterate the loop.
  842. if (EmitBoolCondBranch) {
  843. uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
  844. Builder.CreateCondBr(
  845. BoolCondVal, LoopBody, LoopExit.getBlock(),
  846. createProfileWeightsForLoop(S.getCond(), BackedgeCount));
  847. }
  848. LoopStack.pop();
  849. // Emit the exit block.
  850. EmitBlock(LoopExit.getBlock());
  851. // The DoCond block typically is just a branch if we skipped
  852. // emitting a branch, try to erase it.
  853. if (!EmitBoolCondBranch)
  854. SimplifyForwardingBlocks(LoopCond.getBlock());
  855. }
  856. void CodeGenFunction::EmitForStmt(const ForStmt &S,
  857. ArrayRef<const Attr *> ForAttrs) {
  858. JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
  859. LexicalScope ForScope(*this, S.getSourceRange());
  860. // Evaluate the first part before the loop.
  861. if (S.getInit())
  862. EmitStmt(S.getInit());
  863. // Start the loop with a block that tests the condition.
  864. // If there's an increment, the continue scope will be overwritten
  865. // later.
  866. JumpDest CondDest = getJumpDestInCurrentScope("for.cond");
  867. llvm::BasicBlock *CondBlock = CondDest.getBlock();
  868. EmitBlock(CondBlock);
  869. Expr::EvalResult Result;
  870. bool CondIsConstInt =
  871. !S.getCond() || S.getCond()->EvaluateAsInt(Result, getContext());
  872. const SourceRange &R = S.getSourceRange();
  873. LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs,
  874. SourceLocToDebugLoc(R.getBegin()),
  875. SourceLocToDebugLoc(R.getEnd()),
  876. checkIfLoopMustProgress(CondIsConstInt));
  877. // Create a cleanup scope for the condition variable cleanups.
  878. LexicalScope ConditionScope(*this, S.getSourceRange());
  879. // If the for loop doesn't have an increment we can just use the condition as
  880. // the continue block. Otherwise, if there is no condition variable, we can
  881. // form the continue block now. If there is a condition variable, we can't
  882. // form the continue block until after we've emitted the condition, because
  883. // the condition is in scope in the increment, but Sema's jump diagnostics
  884. // ensure that there are no continues from the condition variable that jump
  885. // to the loop increment.
  886. JumpDest Continue;
  887. if (!S.getInc())
  888. Continue = CondDest;
  889. else if (!S.getConditionVariable())
  890. Continue = getJumpDestInCurrentScope("for.inc");
  891. BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
  892. if (S.getCond()) {
  893. // If the for statement has a condition scope, emit the local variable
  894. // declaration.
  895. if (S.getConditionVariable()) {
  896. EmitDecl(*S.getConditionVariable());
  897. // We have entered the condition variable's scope, so we're now able to
  898. // jump to the continue block.
  899. Continue = S.getInc() ? getJumpDestInCurrentScope("for.inc") : CondDest;
  900. BreakContinueStack.back().ContinueBlock = Continue;
  901. }
  902. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  903. // If there are any cleanups between here and the loop-exit scope,
  904. // create a block to stage a loop exit along.
  905. if (ForScope.requiresCleanups())
  906. ExitBlock = createBasicBlock("for.cond.cleanup");
  907. // As long as the condition is true, iterate the loop.
  908. llvm::BasicBlock *ForBody = createBasicBlock("for.body");
  909. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  910. // compares unequal to 0. The condition must be a scalar type.
  911. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  912. llvm::MDNode *Weights =
  913. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()));
  914. if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
  915. BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
  916. BoolCondVal, Stmt::getLikelihood(S.getBody()));
  917. Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
  918. if (ExitBlock != LoopExit.getBlock()) {
  919. EmitBlock(ExitBlock);
  920. EmitBranchThroughCleanup(LoopExit);
  921. }
  922. EmitBlock(ForBody);
  923. } else {
  924. // Treat it as a non-zero constant. Don't even create a new block for the
  925. // body, just fall into it.
  926. }
  927. incrementProfileCounter(&S);
  928. {
  929. // Create a separate cleanup scope for the body, in case it is not
  930. // a compound statement.
  931. RunCleanupsScope BodyScope(*this);
  932. EmitStmt(S.getBody());
  933. }
  934. // If there is an increment, emit it next.
  935. if (S.getInc()) {
  936. EmitBlock(Continue.getBlock());
  937. EmitStmt(S.getInc());
  938. }
  939. BreakContinueStack.pop_back();
  940. ConditionScope.ForceCleanup();
  941. EmitStopPoint(&S);
  942. EmitBranch(CondBlock);
  943. ForScope.ForceCleanup();
  944. LoopStack.pop();
  945. // Emit the fall-through block.
  946. EmitBlock(LoopExit.getBlock(), true);
  947. }
  948. void
  949. CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
  950. ArrayRef<const Attr *> ForAttrs) {
  951. JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
  952. LexicalScope ForScope(*this, S.getSourceRange());
  953. // Evaluate the first pieces before the loop.
  954. if (S.getInit())
  955. EmitStmt(S.getInit());
  956. EmitStmt(S.getRangeStmt());
  957. EmitStmt(S.getBeginStmt());
  958. EmitStmt(S.getEndStmt());
  959. // Start the loop with a block that tests the condition.
  960. // If there's an increment, the continue scope will be overwritten
  961. // later.
  962. llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
  963. EmitBlock(CondBlock);
  964. const SourceRange &R = S.getSourceRange();
  965. LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs,
  966. SourceLocToDebugLoc(R.getBegin()),
  967. SourceLocToDebugLoc(R.getEnd()));
  968. // If there are any cleanups between here and the loop-exit scope,
  969. // create a block to stage a loop exit along.
  970. llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
  971. if (ForScope.requiresCleanups())
  972. ExitBlock = createBasicBlock("for.cond.cleanup");
  973. // The loop body, consisting of the specified body and the loop variable.
  974. llvm::BasicBlock *ForBody = createBasicBlock("for.body");
  975. // The body is executed if the expression, contextually converted
  976. // to bool, is true.
  977. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  978. llvm::MDNode *Weights =
  979. createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()));
  980. if (!Weights && CGM.getCodeGenOpts().OptimizationLevel)
  981. BoolCondVal = emitCondLikelihoodViaExpectIntrinsic(
  982. BoolCondVal, Stmt::getLikelihood(S.getBody()));
  983. Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights);
  984. if (ExitBlock != LoopExit.getBlock()) {
  985. EmitBlock(ExitBlock);
  986. EmitBranchThroughCleanup(LoopExit);
  987. }
  988. EmitBlock(ForBody);
  989. incrementProfileCounter(&S);
  990. // Create a block for the increment. In case of a 'continue', we jump there.
  991. JumpDest Continue = getJumpDestInCurrentScope("for.inc");
  992. // Store the blocks to use for break and continue.
  993. BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
  994. {
  995. // Create a separate cleanup scope for the loop variable and body.
  996. LexicalScope BodyScope(*this, S.getSourceRange());
  997. EmitStmt(S.getLoopVarStmt());
  998. EmitStmt(S.getBody());
  999. }
  1000. EmitStopPoint(&S);
  1001. // If there is an increment, emit it next.
  1002. EmitBlock(Continue.getBlock());
  1003. EmitStmt(S.getInc());
  1004. BreakContinueStack.pop_back();
  1005. EmitBranch(CondBlock);
  1006. ForScope.ForceCleanup();
  1007. LoopStack.pop();
  1008. // Emit the fall-through block.
  1009. EmitBlock(LoopExit.getBlock(), true);
  1010. }
  1011. void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
  1012. if (RV.isScalar()) {
  1013. Builder.CreateStore(RV.getScalarVal(), ReturnValue);
  1014. } else if (RV.isAggregate()) {
  1015. LValue Dest = MakeAddrLValue(ReturnValue, Ty);
  1016. LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty);
  1017. EmitAggregateCopy(Dest, Src, Ty, getOverlapForReturnValue());
  1018. } else {
  1019. EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty),
  1020. /*init*/ true);
  1021. }
  1022. EmitBranchThroughCleanup(ReturnBlock);
  1023. }
  1024. namespace {
  1025. // RAII struct used to save and restore a return statment's result expression.
  1026. struct SaveRetExprRAII {
  1027. SaveRetExprRAII(const Expr *RetExpr, CodeGenFunction &CGF)
  1028. : OldRetExpr(CGF.RetExpr), CGF(CGF) {
  1029. CGF.RetExpr = RetExpr;
  1030. }
  1031. ~SaveRetExprRAII() { CGF.RetExpr = OldRetExpr; }
  1032. const Expr *OldRetExpr;
  1033. CodeGenFunction &CGF;
  1034. };
  1035. } // namespace
  1036. /// If we have 'return f(...);', where both caller and callee are SwiftAsync,
  1037. /// codegen it as 'tail call ...; ret void;'.
  1038. static void makeTailCallIfSwiftAsync(const CallExpr *CE, CGBuilderTy &Builder,
  1039. const CGFunctionInfo *CurFnInfo) {
  1040. auto calleeQualType = CE->getCallee()->getType();
  1041. const FunctionType *calleeType = nullptr;
  1042. if (calleeQualType->isFunctionPointerType() ||
  1043. calleeQualType->isFunctionReferenceType() ||
  1044. calleeQualType->isBlockPointerType() ||
  1045. calleeQualType->isMemberFunctionPointerType()) {
  1046. calleeType = calleeQualType->getPointeeType()->castAs<FunctionType>();
  1047. } else if (auto *ty = dyn_cast<FunctionType>(calleeQualType)) {
  1048. calleeType = ty;
  1049. } else if (auto CMCE = dyn_cast<CXXMemberCallExpr>(CE)) {
  1050. if (auto methodDecl = CMCE->getMethodDecl()) {
  1051. // getMethodDecl() doesn't handle member pointers at the moment.
  1052. calleeType = methodDecl->getType()->castAs<FunctionType>();
  1053. } else {
  1054. return;
  1055. }
  1056. } else {
  1057. return;
  1058. }
  1059. if (calleeType->getCallConv() == CallingConv::CC_SwiftAsync &&
  1060. (CurFnInfo->getASTCallingConvention() == CallingConv::CC_SwiftAsync)) {
  1061. auto CI = cast<llvm::CallInst>(&Builder.GetInsertBlock()->back());
  1062. CI->setTailCallKind(llvm::CallInst::TCK_MustTail);
  1063. Builder.CreateRetVoid();
  1064. Builder.ClearInsertionPoint();
  1065. }
  1066. }
  1067. /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
  1068. /// if the function returns void, or may be missing one if the function returns
  1069. /// non-void. Fun stuff :).
  1070. void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
  1071. if (requiresReturnValueCheck()) {
  1072. llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc());
  1073. auto *SLocPtr =
  1074. new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false,
  1075. llvm::GlobalVariable::PrivateLinkage, SLoc);
  1076. SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  1077. CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr);
  1078. assert(ReturnLocation.isValid() && "No valid return location");
  1079. Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy),
  1080. ReturnLocation);
  1081. }
  1082. // Returning from an outlined SEH helper is UB, and we already warn on it.
  1083. if (IsOutlinedSEHHelper) {
  1084. Builder.CreateUnreachable();
  1085. Builder.ClearInsertionPoint();
  1086. }
  1087. // Emit the result value, even if unused, to evaluate the side effects.
  1088. const Expr *RV = S.getRetValue();
  1089. // Record the result expression of the return statement. The recorded
  1090. // expression is used to determine whether a block capture's lifetime should
  1091. // end at the end of the full expression as opposed to the end of the scope
  1092. // enclosing the block expression.
  1093. //
  1094. // This permits a small, easily-implemented exception to our over-conservative
  1095. // rules about not jumping to statements following block literals with
  1096. // non-trivial cleanups.
  1097. SaveRetExprRAII SaveRetExpr(RV, *this);
  1098. RunCleanupsScope cleanupScope(*this);
  1099. if (const auto *EWC = dyn_cast_or_null<ExprWithCleanups>(RV))
  1100. RV = EWC->getSubExpr();
  1101. // FIXME: Clean this up by using an LValue for ReturnTemp,
  1102. // EmitStoreThroughLValue, and EmitAnyExpr.
  1103. // Check if the NRVO candidate was not globalized in OpenMP mode.
  1104. if (getLangOpts().ElideConstructors && S.getNRVOCandidate() &&
  1105. S.getNRVOCandidate()->isNRVOVariable() &&
  1106. (!getLangOpts().OpenMP ||
  1107. !CGM.getOpenMPRuntime()
  1108. .getAddressOfLocalVariable(*this, S.getNRVOCandidate())
  1109. .isValid())) {
  1110. // Apply the named return value optimization for this return statement,
  1111. // which means doing nothing: the appropriate result has already been
  1112. // constructed into the NRVO variable.
  1113. // If there is an NRVO flag for this variable, set it to 1 into indicate
  1114. // that the cleanup code should not destroy the variable.
  1115. if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
  1116. Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag);
  1117. } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) {
  1118. // Make sure not to return anything, but evaluate the expression
  1119. // for side effects.
  1120. if (RV) {
  1121. EmitAnyExpr(RV);
  1122. if (auto *CE = dyn_cast<CallExpr>(RV))
  1123. makeTailCallIfSwiftAsync(CE, Builder, CurFnInfo);
  1124. }
  1125. } else if (!RV) {
  1126. // Do nothing (return value is left uninitialized)
  1127. } else if (FnRetTy->isReferenceType()) {
  1128. // If this function returns a reference, take the address of the expression
  1129. // rather than the value.
  1130. RValue Result = EmitReferenceBindingToExpr(RV);
  1131. Builder.CreateStore(Result.getScalarVal(), ReturnValue);
  1132. } else {
  1133. switch (getEvaluationKind(RV->getType())) {
  1134. case TEK_Scalar:
  1135. Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
  1136. break;
  1137. case TEK_Complex:
  1138. EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()),
  1139. /*isInit*/ true);
  1140. break;
  1141. case TEK_Aggregate:
  1142. EmitAggExpr(RV, AggValueSlot::forAddr(
  1143. ReturnValue, Qualifiers(),
  1144. AggValueSlot::IsDestructed,
  1145. AggValueSlot::DoesNotNeedGCBarriers,
  1146. AggValueSlot::IsNotAliased,
  1147. getOverlapForReturnValue()));
  1148. break;
  1149. }
  1150. }
  1151. ++NumReturnExprs;
  1152. if (!RV || RV->isEvaluatable(getContext()))
  1153. ++NumSimpleReturnExprs;
  1154. cleanupScope.ForceCleanup();
  1155. EmitBranchThroughCleanup(ReturnBlock);
  1156. }
  1157. void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
  1158. // As long as debug info is modeled with instructions, we have to ensure we
  1159. // have a place to insert here and write the stop point here.
  1160. if (HaveInsertPoint())
  1161. EmitStopPoint(&S);
  1162. for (const auto *I : S.decls())
  1163. EmitDecl(*I);
  1164. }
  1165. void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
  1166. assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
  1167. // If this code is reachable then emit a stop point (if generating
  1168. // debug info). We have to do this ourselves because we are on the
  1169. // "simple" statement path.
  1170. if (HaveInsertPoint())
  1171. EmitStopPoint(&S);
  1172. EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock);
  1173. }
  1174. void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
  1175. assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
  1176. // If this code is reachable then emit a stop point (if generating
  1177. // debug info). We have to do this ourselves because we are on the
  1178. // "simple" statement path.
  1179. if (HaveInsertPoint())
  1180. EmitStopPoint(&S);
  1181. EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock);
  1182. }
  1183. /// EmitCaseStmtRange - If case statement range is not too big then
  1184. /// add multiple cases to switch instruction, one for each value within
  1185. /// the range. If range is too big then emit "if" condition check.
  1186. void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S,
  1187. ArrayRef<const Attr *> Attrs) {
  1188. assert(S.getRHS() && "Expected RHS value in CaseStmt");
  1189. llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
  1190. llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
  1191. // Emit the code for this case. We do this first to make sure it is
  1192. // properly chained from our predecessor before generating the
  1193. // switch machinery to enter this block.
  1194. llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
  1195. EmitBlockWithFallThrough(CaseDest, &S);
  1196. EmitStmt(S.getSubStmt());
  1197. // If range is empty, do nothing.
  1198. if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
  1199. return;
  1200. Stmt::Likelihood LH = Stmt::getLikelihood(Attrs);
  1201. llvm::APInt Range = RHS - LHS;
  1202. // FIXME: parameters such as this should not be hardcoded.
  1203. if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
  1204. // Range is small enough to add multiple switch instruction cases.
  1205. uint64_t Total = getProfileCount(&S);
  1206. unsigned NCases = Range.getZExtValue() + 1;
  1207. // We only have one region counter for the entire set of cases here, so we
  1208. // need to divide the weights evenly between the generated cases, ensuring
  1209. // that the total weight is preserved. E.g., a weight of 5 over three cases
  1210. // will be distributed as weights of 2, 2, and 1.
  1211. uint64_t Weight = Total / NCases, Rem = Total % NCases;
  1212. for (unsigned I = 0; I != NCases; ++I) {
  1213. if (SwitchWeights)
  1214. SwitchWeights->push_back(Weight + (Rem ? 1 : 0));
  1215. else if (SwitchLikelihood)
  1216. SwitchLikelihood->push_back(LH);
  1217. if (Rem)
  1218. Rem--;
  1219. SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
  1220. ++LHS;
  1221. }
  1222. return;
  1223. }
  1224. // The range is too big. Emit "if" condition into a new block,
  1225. // making sure to save and restore the current insertion point.
  1226. llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
  1227. // Push this test onto the chain of range checks (which terminates
  1228. // in the default basic block). The switch's default will be changed
  1229. // to the top of this chain after switch emission is complete.
  1230. llvm::BasicBlock *FalseDest = CaseRangeBlock;
  1231. CaseRangeBlock = createBasicBlock("sw.caserange");
  1232. CurFn->getBasicBlockList().push_back(CaseRangeBlock);
  1233. Builder.SetInsertPoint(CaseRangeBlock);
  1234. // Emit range check.
  1235. llvm::Value *Diff =
  1236. Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
  1237. llvm::Value *Cond =
  1238. Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
  1239. llvm::MDNode *Weights = nullptr;
  1240. if (SwitchWeights) {
  1241. uint64_t ThisCount = getProfileCount(&S);
  1242. uint64_t DefaultCount = (*SwitchWeights)[0];
  1243. Weights = createProfileWeights(ThisCount, DefaultCount);
  1244. // Since we're chaining the switch default through each large case range, we
  1245. // need to update the weight for the default, ie, the first case, to include
  1246. // this case.
  1247. (*SwitchWeights)[0] += ThisCount;
  1248. } else if (SwitchLikelihood)
  1249. Cond = emitCondLikelihoodViaExpectIntrinsic(Cond, LH);
  1250. Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights);
  1251. // Restore the appropriate insertion point.
  1252. if (RestoreBB)
  1253. Builder.SetInsertPoint(RestoreBB);
  1254. else
  1255. Builder.ClearInsertionPoint();
  1256. }
  1257. void CodeGenFunction::EmitCaseStmt(const CaseStmt &S,
  1258. ArrayRef<const Attr *> Attrs) {
  1259. // If there is no enclosing switch instance that we're aware of, then this
  1260. // case statement and its block can be elided. This situation only happens
  1261. // when we've constant-folded the switch, are emitting the constant case,
  1262. // and part of the constant case includes another case statement. For
  1263. // instance: switch (4) { case 4: do { case 5: } while (1); }
  1264. if (!SwitchInsn) {
  1265. EmitStmt(S.getSubStmt());
  1266. return;
  1267. }
  1268. // Handle case ranges.
  1269. if (S.getRHS()) {
  1270. EmitCaseStmtRange(S, Attrs);
  1271. return;
  1272. }
  1273. llvm::ConstantInt *CaseVal =
  1274. Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
  1275. if (SwitchLikelihood)
  1276. SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
  1277. // If the body of the case is just a 'break', try to not emit an empty block.
  1278. // If we're profiling or we're not optimizing, leave the block in for better
  1279. // debug and coverage analysis.
  1280. if (!CGM.getCodeGenOpts().hasProfileClangInstr() &&
  1281. CGM.getCodeGenOpts().OptimizationLevel > 0 &&
  1282. isa<BreakStmt>(S.getSubStmt())) {
  1283. JumpDest Block = BreakContinueStack.back().BreakBlock;
  1284. // Only do this optimization if there are no cleanups that need emitting.
  1285. if (isObviouslyBranchWithoutCleanups(Block)) {
  1286. if (SwitchWeights)
  1287. SwitchWeights->push_back(getProfileCount(&S));
  1288. SwitchInsn->addCase(CaseVal, Block.getBlock());
  1289. // If there was a fallthrough into this case, make sure to redirect it to
  1290. // the end of the switch as well.
  1291. if (Builder.GetInsertBlock()) {
  1292. Builder.CreateBr(Block.getBlock());
  1293. Builder.ClearInsertionPoint();
  1294. }
  1295. return;
  1296. }
  1297. }
  1298. llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
  1299. EmitBlockWithFallThrough(CaseDest, &S);
  1300. if (SwitchWeights)
  1301. SwitchWeights->push_back(getProfileCount(&S));
  1302. SwitchInsn->addCase(CaseVal, CaseDest);
  1303. // Recursively emitting the statement is acceptable, but is not wonderful for
  1304. // code where we have many case statements nested together, i.e.:
  1305. // case 1:
  1306. // case 2:
  1307. // case 3: etc.
  1308. // Handling this recursively will create a new block for each case statement
  1309. // that falls through to the next case which is IR intensive. It also causes
  1310. // deep recursion which can run into stack depth limitations. Handle
  1311. // sequential non-range case statements specially.
  1312. //
  1313. // TODO When the next case has a likelihood attribute the code returns to the
  1314. // recursive algorithm. Maybe improve this case if it becomes common practice
  1315. // to use a lot of attributes.
  1316. const CaseStmt *CurCase = &S;
  1317. const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
  1318. // Otherwise, iteratively add consecutive cases to this switch stmt.
  1319. while (NextCase && NextCase->getRHS() == nullptr) {
  1320. CurCase = NextCase;
  1321. llvm::ConstantInt *CaseVal =
  1322. Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
  1323. if (SwitchWeights)
  1324. SwitchWeights->push_back(getProfileCount(NextCase));
  1325. if (CGM.getCodeGenOpts().hasProfileClangInstr()) {
  1326. CaseDest = createBasicBlock("sw.bb");
  1327. EmitBlockWithFallThrough(CaseDest, CurCase);
  1328. }
  1329. // Since this loop is only executed when the CaseStmt has no attributes
  1330. // use a hard-coded value.
  1331. if (SwitchLikelihood)
  1332. SwitchLikelihood->push_back(Stmt::LH_None);
  1333. SwitchInsn->addCase(CaseVal, CaseDest);
  1334. NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
  1335. }
  1336. // Generate a stop point for debug info if the case statement is
  1337. // followed by a default statement. A fallthrough case before a
  1338. // default case gets its own branch target.
  1339. if (CurCase->getSubStmt()->getStmtClass() == Stmt::DefaultStmtClass)
  1340. EmitStopPoint(CurCase);
  1341. // Normal default recursion for non-cases.
  1342. EmitStmt(CurCase->getSubStmt());
  1343. }
  1344. void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S,
  1345. ArrayRef<const Attr *> Attrs) {
  1346. // If there is no enclosing switch instance that we're aware of, then this
  1347. // default statement can be elided. This situation only happens when we've
  1348. // constant-folded the switch.
  1349. if (!SwitchInsn) {
  1350. EmitStmt(S.getSubStmt());
  1351. return;
  1352. }
  1353. llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
  1354. assert(DefaultBlock->empty() &&
  1355. "EmitDefaultStmt: Default block already defined?");
  1356. if (SwitchLikelihood)
  1357. SwitchLikelihood->front() = Stmt::getLikelihood(Attrs);
  1358. EmitBlockWithFallThrough(DefaultBlock, &S);
  1359. EmitStmt(S.getSubStmt());
  1360. }
  1361. /// CollectStatementsForCase - Given the body of a 'switch' statement and a
  1362. /// constant value that is being switched on, see if we can dead code eliminate
  1363. /// the body of the switch to a simple series of statements to emit. Basically,
  1364. /// on a switch (5) we want to find these statements:
  1365. /// case 5:
  1366. /// printf(...); <--
  1367. /// ++i; <--
  1368. /// break;
  1369. ///
  1370. /// and add them to the ResultStmts vector. If it is unsafe to do this
  1371. /// transformation (for example, one of the elided statements contains a label
  1372. /// that might be jumped to), return CSFC_Failure. If we handled it and 'S'
  1373. /// should include statements after it (e.g. the printf() line is a substmt of
  1374. /// the case) then return CSFC_FallThrough. If we handled it and found a break
  1375. /// statement, then return CSFC_Success.
  1376. ///
  1377. /// If Case is non-null, then we are looking for the specified case, checking
  1378. /// that nothing we jump over contains labels. If Case is null, then we found
  1379. /// the case and are looking for the break.
  1380. ///
  1381. /// If the recursive walk actually finds our Case, then we set FoundCase to
  1382. /// true.
  1383. ///
  1384. enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
  1385. static CSFC_Result CollectStatementsForCase(const Stmt *S,
  1386. const SwitchCase *Case,
  1387. bool &FoundCase,
  1388. SmallVectorImpl<const Stmt*> &ResultStmts) {
  1389. // If this is a null statement, just succeed.
  1390. if (!S)
  1391. return Case ? CSFC_Success : CSFC_FallThrough;
  1392. // If this is the switchcase (case 4: or default) that we're looking for, then
  1393. // we're in business. Just add the substatement.
  1394. if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
  1395. if (S == Case) {
  1396. FoundCase = true;
  1397. return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase,
  1398. ResultStmts);
  1399. }
  1400. // Otherwise, this is some other case or default statement, just ignore it.
  1401. return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
  1402. ResultStmts);
  1403. }
  1404. // If we are in the live part of the code and we found our break statement,
  1405. // return a success!
  1406. if (!Case && isa<BreakStmt>(S))
  1407. return CSFC_Success;
  1408. // If this is a switch statement, then it might contain the SwitchCase, the
  1409. // break, or neither.
  1410. if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
  1411. // Handle this as two cases: we might be looking for the SwitchCase (if so
  1412. // the skipped statements must be skippable) or we might already have it.
  1413. CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
  1414. bool StartedInLiveCode = FoundCase;
  1415. unsigned StartSize = ResultStmts.size();
  1416. // If we've not found the case yet, scan through looking for it.
  1417. if (Case) {
  1418. // Keep track of whether we see a skipped declaration. The code could be
  1419. // using the declaration even if it is skipped, so we can't optimize out
  1420. // the decl if the kept statements might refer to it.
  1421. bool HadSkippedDecl = false;
  1422. // If we're looking for the case, just see if we can skip each of the
  1423. // substatements.
  1424. for (; Case && I != E; ++I) {
  1425. HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I);
  1426. switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
  1427. case CSFC_Failure: return CSFC_Failure;
  1428. case CSFC_Success:
  1429. // A successful result means that either 1) that the statement doesn't
  1430. // have the case and is skippable, or 2) does contain the case value
  1431. // and also contains the break to exit the switch. In the later case,
  1432. // we just verify the rest of the statements are elidable.
  1433. if (FoundCase) {
  1434. // If we found the case and skipped declarations, we can't do the
  1435. // optimization.
  1436. if (HadSkippedDecl)
  1437. return CSFC_Failure;
  1438. for (++I; I != E; ++I)
  1439. if (CodeGenFunction::ContainsLabel(*I, true))
  1440. return CSFC_Failure;
  1441. return CSFC_Success;
  1442. }
  1443. break;
  1444. case CSFC_FallThrough:
  1445. // If we have a fallthrough condition, then we must have found the
  1446. // case started to include statements. Consider the rest of the
  1447. // statements in the compound statement as candidates for inclusion.
  1448. assert(FoundCase && "Didn't find case but returned fallthrough?");
  1449. // We recursively found Case, so we're not looking for it anymore.
  1450. Case = nullptr;
  1451. // If we found the case and skipped declarations, we can't do the
  1452. // optimization.
  1453. if (HadSkippedDecl)
  1454. return CSFC_Failure;
  1455. break;
  1456. }
  1457. }
  1458. if (!FoundCase)
  1459. return CSFC_Success;
  1460. assert(!HadSkippedDecl && "fallthrough after skipping decl");
  1461. }
  1462. // If we have statements in our range, then we know that the statements are
  1463. // live and need to be added to the set of statements we're tracking.
  1464. bool AnyDecls = false;
  1465. for (; I != E; ++I) {
  1466. AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I);
  1467. switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) {
  1468. case CSFC_Failure: return CSFC_Failure;
  1469. case CSFC_FallThrough:
  1470. // A fallthrough result means that the statement was simple and just
  1471. // included in ResultStmt, keep adding them afterwards.
  1472. break;
  1473. case CSFC_Success:
  1474. // A successful result means that we found the break statement and
  1475. // stopped statement inclusion. We just ensure that any leftover stmts
  1476. // are skippable and return success ourselves.
  1477. for (++I; I != E; ++I)
  1478. if (CodeGenFunction::ContainsLabel(*I, true))
  1479. return CSFC_Failure;
  1480. return CSFC_Success;
  1481. }
  1482. }
  1483. // If we're about to fall out of a scope without hitting a 'break;', we
  1484. // can't perform the optimization if there were any decls in that scope
  1485. // (we'd lose their end-of-lifetime).
  1486. if (AnyDecls) {
  1487. // If the entire compound statement was live, there's one more thing we
  1488. // can try before giving up: emit the whole thing as a single statement.
  1489. // We can do that unless the statement contains a 'break;'.
  1490. // FIXME: Such a break must be at the end of a construct within this one.
  1491. // We could emit this by just ignoring the BreakStmts entirely.
  1492. if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) {
  1493. ResultStmts.resize(StartSize);
  1494. ResultStmts.push_back(S);
  1495. } else {
  1496. return CSFC_Failure;
  1497. }
  1498. }
  1499. return CSFC_FallThrough;
  1500. }
  1501. // Okay, this is some other statement that we don't handle explicitly, like a
  1502. // for statement or increment etc. If we are skipping over this statement,
  1503. // just verify it doesn't have labels, which would make it invalid to elide.
  1504. if (Case) {
  1505. if (CodeGenFunction::ContainsLabel(S, true))
  1506. return CSFC_Failure;
  1507. return CSFC_Success;
  1508. }
  1509. // Otherwise, we want to include this statement. Everything is cool with that
  1510. // so long as it doesn't contain a break out of the switch we're in.
  1511. if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
  1512. // Otherwise, everything is great. Include the statement and tell the caller
  1513. // that we fall through and include the next statement as well.
  1514. ResultStmts.push_back(S);
  1515. return CSFC_FallThrough;
  1516. }
  1517. /// FindCaseStatementsForValue - Find the case statement being jumped to and
  1518. /// then invoke CollectStatementsForCase to find the list of statements to emit
  1519. /// for a switch on constant. See the comment above CollectStatementsForCase
  1520. /// for more details.
  1521. static bool FindCaseStatementsForValue(const SwitchStmt &S,
  1522. const llvm::APSInt &ConstantCondValue,
  1523. SmallVectorImpl<const Stmt*> &ResultStmts,
  1524. ASTContext &C,
  1525. const SwitchCase *&ResultCase) {
  1526. // First step, find the switch case that is being branched to. We can do this
  1527. // efficiently by scanning the SwitchCase list.
  1528. const SwitchCase *Case = S.getSwitchCaseList();
  1529. const DefaultStmt *DefaultCase = nullptr;
  1530. for (; Case; Case = Case->getNextSwitchCase()) {
  1531. // It's either a default or case. Just remember the default statement in
  1532. // case we're not jumping to any numbered cases.
  1533. if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
  1534. DefaultCase = DS;
  1535. continue;
  1536. }
  1537. // Check to see if this case is the one we're looking for.
  1538. const CaseStmt *CS = cast<CaseStmt>(Case);
  1539. // Don't handle case ranges yet.
  1540. if (CS->getRHS()) return false;
  1541. // If we found our case, remember it as 'case'.
  1542. if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
  1543. break;
  1544. }
  1545. // If we didn't find a matching case, we use a default if it exists, or we
  1546. // elide the whole switch body!
  1547. if (!Case) {
  1548. // It is safe to elide the body of the switch if it doesn't contain labels
  1549. // etc. If it is safe, return successfully with an empty ResultStmts list.
  1550. if (!DefaultCase)
  1551. return !CodeGenFunction::ContainsLabel(&S);
  1552. Case = DefaultCase;
  1553. }
  1554. // Ok, we know which case is being jumped to, try to collect all the
  1555. // statements that follow it. This can fail for a variety of reasons. Also,
  1556. // check to see that the recursive walk actually found our case statement.
  1557. // Insane cases like this can fail to find it in the recursive walk since we
  1558. // don't handle every stmt kind:
  1559. // switch (4) {
  1560. // while (1) {
  1561. // case 4: ...
  1562. bool FoundCase = false;
  1563. ResultCase = Case;
  1564. return CollectStatementsForCase(S.getBody(), Case, FoundCase,
  1565. ResultStmts) != CSFC_Failure &&
  1566. FoundCase;
  1567. }
  1568. static Optional<SmallVector<uint64_t, 16>>
  1569. getLikelihoodWeights(ArrayRef<Stmt::Likelihood> Likelihoods) {
  1570. // Are there enough branches to weight them?
  1571. if (Likelihoods.size() <= 1)
  1572. return None;
  1573. uint64_t NumUnlikely = 0;
  1574. uint64_t NumNone = 0;
  1575. uint64_t NumLikely = 0;
  1576. for (const auto LH : Likelihoods) {
  1577. switch (LH) {
  1578. case Stmt::LH_Unlikely:
  1579. ++NumUnlikely;
  1580. break;
  1581. case Stmt::LH_None:
  1582. ++NumNone;
  1583. break;
  1584. case Stmt::LH_Likely:
  1585. ++NumLikely;
  1586. break;
  1587. }
  1588. }
  1589. // Is there a likelihood attribute used?
  1590. if (NumUnlikely == 0 && NumLikely == 0)
  1591. return None;
  1592. // When multiple cases share the same code they can be combined during
  1593. // optimization. In that case the weights of the branch will be the sum of
  1594. // the individual weights. Make sure the combined sum of all neutral cases
  1595. // doesn't exceed the value of a single likely attribute.
  1596. // The additions both avoid divisions by 0 and make sure the weights of None
  1597. // don't exceed the weight of Likely.
  1598. const uint64_t Likely = INT32_MAX / (NumLikely + 2);
  1599. const uint64_t None = Likely / (NumNone + 1);
  1600. const uint64_t Unlikely = 0;
  1601. SmallVector<uint64_t, 16> Result;
  1602. Result.reserve(Likelihoods.size());
  1603. for (const auto LH : Likelihoods) {
  1604. switch (LH) {
  1605. case Stmt::LH_Unlikely:
  1606. Result.push_back(Unlikely);
  1607. break;
  1608. case Stmt::LH_None:
  1609. Result.push_back(None);
  1610. break;
  1611. case Stmt::LH_Likely:
  1612. Result.push_back(Likely);
  1613. break;
  1614. }
  1615. }
  1616. return Result;
  1617. }
  1618. void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
  1619. // Handle nested switch statements.
  1620. llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
  1621. SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights;
  1622. SmallVector<Stmt::Likelihood, 16> *SavedSwitchLikelihood = SwitchLikelihood;
  1623. llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
  1624. // See if we can constant fold the condition of the switch and therefore only
  1625. // emit the live case statement (if any) of the switch.
  1626. llvm::APSInt ConstantCondValue;
  1627. if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
  1628. SmallVector<const Stmt*, 4> CaseStmts;
  1629. const SwitchCase *Case = nullptr;
  1630. if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
  1631. getContext(), Case)) {
  1632. if (Case)
  1633. incrementProfileCounter(Case);
  1634. RunCleanupsScope ExecutedScope(*this);
  1635. if (S.getInit())
  1636. EmitStmt(S.getInit());
  1637. // Emit the condition variable if needed inside the entire cleanup scope
  1638. // used by this special case for constant folded switches.
  1639. if (S.getConditionVariable())
  1640. EmitDecl(*S.getConditionVariable());
  1641. // At this point, we are no longer "within" a switch instance, so
  1642. // we can temporarily enforce this to ensure that any embedded case
  1643. // statements are not emitted.
  1644. SwitchInsn = nullptr;
  1645. // Okay, we can dead code eliminate everything except this case. Emit the
  1646. // specified series of statements and we're good.
  1647. for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
  1648. EmitStmt(CaseStmts[i]);
  1649. incrementProfileCounter(&S);
  1650. // Now we want to restore the saved switch instance so that nested
  1651. // switches continue to function properly
  1652. SwitchInsn = SavedSwitchInsn;
  1653. return;
  1654. }
  1655. }
  1656. JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
  1657. RunCleanupsScope ConditionScope(*this);
  1658. if (S.getInit())
  1659. EmitStmt(S.getInit());
  1660. if (S.getConditionVariable())
  1661. EmitDecl(*S.getConditionVariable());
  1662. llvm::Value *CondV = EmitScalarExpr(S.getCond());
  1663. // Create basic block to hold stuff that comes after switch
  1664. // statement. We also need to create a default block now so that
  1665. // explicit case ranges tests can have a place to jump to on
  1666. // failure.
  1667. llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
  1668. SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
  1669. if (PGO.haveRegionCounts()) {
  1670. // Walk the SwitchCase list to find how many there are.
  1671. uint64_t DefaultCount = 0;
  1672. unsigned NumCases = 0;
  1673. for (const SwitchCase *Case = S.getSwitchCaseList();
  1674. Case;
  1675. Case = Case->getNextSwitchCase()) {
  1676. if (isa<DefaultStmt>(Case))
  1677. DefaultCount = getProfileCount(Case);
  1678. NumCases += 1;
  1679. }
  1680. SwitchWeights = new SmallVector<uint64_t, 16>();
  1681. SwitchWeights->reserve(NumCases);
  1682. // The default needs to be first. We store the edge count, so we already
  1683. // know the right weight.
  1684. SwitchWeights->push_back(DefaultCount);
  1685. } else if (CGM.getCodeGenOpts().OptimizationLevel) {
  1686. SwitchLikelihood = new SmallVector<Stmt::Likelihood, 16>();
  1687. // Initialize the default case.
  1688. SwitchLikelihood->push_back(Stmt::LH_None);
  1689. }
  1690. CaseRangeBlock = DefaultBlock;
  1691. // Clear the insertion point to indicate we are in unreachable code.
  1692. Builder.ClearInsertionPoint();
  1693. // All break statements jump to NextBlock. If BreakContinueStack is non-empty
  1694. // then reuse last ContinueBlock.
  1695. JumpDest OuterContinue;
  1696. if (!BreakContinueStack.empty())
  1697. OuterContinue = BreakContinueStack.back().ContinueBlock;
  1698. BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
  1699. // Emit switch body.
  1700. EmitStmt(S.getBody());
  1701. BreakContinueStack.pop_back();
  1702. // Update the default block in case explicit case range tests have
  1703. // been chained on top.
  1704. SwitchInsn->setDefaultDest(CaseRangeBlock);
  1705. // If a default was never emitted:
  1706. if (!DefaultBlock->getParent()) {
  1707. // If we have cleanups, emit the default block so that there's a
  1708. // place to jump through the cleanups from.
  1709. if (ConditionScope.requiresCleanups()) {
  1710. EmitBlock(DefaultBlock);
  1711. // Otherwise, just forward the default block to the switch end.
  1712. } else {
  1713. DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
  1714. delete DefaultBlock;
  1715. }
  1716. }
  1717. ConditionScope.ForceCleanup();
  1718. // Emit continuation.
  1719. EmitBlock(SwitchExit.getBlock(), true);
  1720. incrementProfileCounter(&S);
  1721. // If the switch has a condition wrapped by __builtin_unpredictable,
  1722. // create metadata that specifies that the switch is unpredictable.
  1723. // Don't bother if not optimizing because that metadata would not be used.
  1724. auto *Call = dyn_cast<CallExpr>(S.getCond());
  1725. if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
  1726. auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
  1727. if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
  1728. llvm::MDBuilder MDHelper(getLLVMContext());
  1729. SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable,
  1730. MDHelper.createUnpredictable());
  1731. }
  1732. }
  1733. if (SwitchWeights) {
  1734. assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() &&
  1735. "switch weights do not match switch cases");
  1736. // If there's only one jump destination there's no sense weighting it.
  1737. if (SwitchWeights->size() > 1)
  1738. SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
  1739. createProfileWeights(*SwitchWeights));
  1740. delete SwitchWeights;
  1741. } else if (SwitchLikelihood) {
  1742. assert(SwitchLikelihood->size() == 1 + SwitchInsn->getNumCases() &&
  1743. "switch likelihoods do not match switch cases");
  1744. Optional<SmallVector<uint64_t, 16>> LHW =
  1745. getLikelihoodWeights(*SwitchLikelihood);
  1746. if (LHW) {
  1747. llvm::MDBuilder MDHelper(CGM.getLLVMContext());
  1748. SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
  1749. createProfileWeights(*LHW));
  1750. }
  1751. delete SwitchLikelihood;
  1752. }
  1753. SwitchInsn = SavedSwitchInsn;
  1754. SwitchWeights = SavedSwitchWeights;
  1755. SwitchLikelihood = SavedSwitchLikelihood;
  1756. CaseRangeBlock = SavedCRBlock;
  1757. }
  1758. static std::string
  1759. SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
  1760. SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) {
  1761. std::string Result;
  1762. while (*Constraint) {
  1763. switch (*Constraint) {
  1764. default:
  1765. Result += Target.convertConstraint(Constraint);
  1766. break;
  1767. // Ignore these
  1768. case '*':
  1769. case '?':
  1770. case '!':
  1771. case '=': // Will see this and the following in mult-alt constraints.
  1772. case '+':
  1773. break;
  1774. case '#': // Ignore the rest of the constraint alternative.
  1775. while (Constraint[1] && Constraint[1] != ',')
  1776. Constraint++;
  1777. break;
  1778. case '&':
  1779. case '%':
  1780. Result += *Constraint;
  1781. while (Constraint[1] && Constraint[1] == *Constraint)
  1782. Constraint++;
  1783. break;
  1784. case ',':
  1785. Result += "|";
  1786. break;
  1787. case 'g':
  1788. Result += "imr";
  1789. break;
  1790. case '[': {
  1791. assert(OutCons &&
  1792. "Must pass output names to constraints with a symbolic name");
  1793. unsigned Index;
  1794. bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index);
  1795. assert(result && "Could not resolve symbolic name"); (void)result;
  1796. Result += llvm::utostr(Index);
  1797. break;
  1798. }
  1799. }
  1800. Constraint++;
  1801. }
  1802. return Result;
  1803. }
  1804. /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
  1805. /// as using a particular register add that as a constraint that will be used
  1806. /// in this asm stmt.
  1807. static std::string
  1808. AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
  1809. const TargetInfo &Target, CodeGenModule &CGM,
  1810. const AsmStmt &Stmt, const bool EarlyClobber,
  1811. std::string *GCCReg = nullptr) {
  1812. const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
  1813. if (!AsmDeclRef)
  1814. return Constraint;
  1815. const ValueDecl &Value = *AsmDeclRef->getDecl();
  1816. const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
  1817. if (!Variable)
  1818. return Constraint;
  1819. if (Variable->getStorageClass() != SC_Register)
  1820. return Constraint;
  1821. AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
  1822. if (!Attr)
  1823. return Constraint;
  1824. StringRef Register = Attr->getLabel();
  1825. assert(Target.isValidGCCRegisterName(Register));
  1826. // We're using validateOutputConstraint here because we only care if
  1827. // this is a register constraint.
  1828. TargetInfo::ConstraintInfo Info(Constraint, "");
  1829. if (Target.validateOutputConstraint(Info) &&
  1830. !Info.allowsRegister()) {
  1831. CGM.ErrorUnsupported(&Stmt, "__asm__");
  1832. return Constraint;
  1833. }
  1834. // Canonicalize the register here before returning it.
  1835. Register = Target.getNormalizedGCCRegisterName(Register);
  1836. if (GCCReg != nullptr)
  1837. *GCCReg = Register.str();
  1838. return (EarlyClobber ? "&{" : "{") + Register.str() + "}";
  1839. }
  1840. std::pair<llvm::Value*, llvm::Type *> CodeGenFunction::EmitAsmInputLValue(
  1841. const TargetInfo::ConstraintInfo &Info, LValue InputValue,
  1842. QualType InputType, std::string &ConstraintStr, SourceLocation Loc) {
  1843. if (Info.allowsRegister() || !Info.allowsMemory()) {
  1844. if (CodeGenFunction::hasScalarEvaluationKind(InputType))
  1845. return {EmitLoadOfLValue(InputValue, Loc).getScalarVal(), nullptr};
  1846. llvm::Type *Ty = ConvertType(InputType);
  1847. uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
  1848. if ((Size <= 64 && llvm::isPowerOf2_64(Size)) ||
  1849. getTargetHooks().isScalarizableAsmOperand(*this, Ty)) {
  1850. Ty = llvm::IntegerType::get(getLLVMContext(), Size);
  1851. Ty = llvm::PointerType::getUnqual(Ty);
  1852. return {Builder.CreateLoad(
  1853. Builder.CreateBitCast(InputValue.getAddress(*this), Ty)),
  1854. nullptr};
  1855. }
  1856. }
  1857. Address Addr = InputValue.getAddress(*this);
  1858. ConstraintStr += '*';
  1859. return {Addr.getPointer(), Addr.getElementType()};
  1860. }
  1861. std::pair<llvm::Value *, llvm::Type *>
  1862. CodeGenFunction::EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
  1863. const Expr *InputExpr,
  1864. std::string &ConstraintStr) {
  1865. // If this can't be a register or memory, i.e., has to be a constant
  1866. // (immediate or symbolic), try to emit it as such.
  1867. if (!Info.allowsRegister() && !Info.allowsMemory()) {
  1868. if (Info.requiresImmediateConstant()) {
  1869. Expr::EvalResult EVResult;
  1870. InputExpr->EvaluateAsRValue(EVResult, getContext(), true);
  1871. llvm::APSInt IntResult;
  1872. if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
  1873. getContext()))
  1874. return {llvm::ConstantInt::get(getLLVMContext(), IntResult), nullptr};
  1875. }
  1876. Expr::EvalResult Result;
  1877. if (InputExpr->EvaluateAsInt(Result, getContext()))
  1878. return {llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt()),
  1879. nullptr};
  1880. }
  1881. if (Info.allowsRegister() || !Info.allowsMemory())
  1882. if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
  1883. return {EmitScalarExpr(InputExpr), nullptr};
  1884. if (InputExpr->getStmtClass() == Expr::CXXThisExprClass)
  1885. return {EmitScalarExpr(InputExpr), nullptr};
  1886. InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
  1887. LValue Dest = EmitLValue(InputExpr);
  1888. return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
  1889. InputExpr->getExprLoc());
  1890. }
  1891. /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
  1892. /// asm call instruction. The !srcloc MDNode contains a list of constant
  1893. /// integers which are the source locations of the start of each line in the
  1894. /// asm.
  1895. static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
  1896. CodeGenFunction &CGF) {
  1897. SmallVector<llvm::Metadata *, 8> Locs;
  1898. // Add the location of the first line to the MDNode.
  1899. Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
  1900. CGF.Int64Ty, Str->getBeginLoc().getRawEncoding())));
  1901. StringRef StrVal = Str->getString();
  1902. if (!StrVal.empty()) {
  1903. const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
  1904. const LangOptions &LangOpts = CGF.CGM.getLangOpts();
  1905. unsigned StartToken = 0;
  1906. unsigned ByteOffset = 0;
  1907. // Add the location of the start of each subsequent line of the asm to the
  1908. // MDNode.
  1909. for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) {
  1910. if (StrVal[i] != '\n') continue;
  1911. SourceLocation LineLoc = Str->getLocationOfByte(
  1912. i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
  1913. Locs.push_back(llvm::ConstantAsMetadata::get(
  1914. llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
  1915. }
  1916. }
  1917. return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
  1918. }
  1919. static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect,
  1920. bool HasUnwindClobber, bool ReadOnly,
  1921. bool ReadNone, bool NoMerge, const AsmStmt &S,
  1922. const std::vector<llvm::Type *> &ResultRegTypes,
  1923. const std::vector<llvm::Type *> &ArgElemTypes,
  1924. CodeGenFunction &CGF,
  1925. std::vector<llvm::Value *> &RegResults) {
  1926. if (!HasUnwindClobber)
  1927. Result.addFnAttr(llvm::Attribute::NoUnwind);
  1928. if (NoMerge)
  1929. Result.addFnAttr(llvm::Attribute::NoMerge);
  1930. // Attach readnone and readonly attributes.
  1931. if (!HasSideEffect) {
  1932. if (ReadNone)
  1933. Result.addFnAttr(llvm::Attribute::ReadNone);
  1934. else if (ReadOnly)
  1935. Result.addFnAttr(llvm::Attribute::ReadOnly);
  1936. }
  1937. // Add elementtype attribute for indirect constraints.
  1938. for (auto Pair : llvm::enumerate(ArgElemTypes)) {
  1939. if (Pair.value()) {
  1940. auto Attr = llvm::Attribute::get(
  1941. CGF.getLLVMContext(), llvm::Attribute::ElementType, Pair.value());
  1942. Result.addParamAttr(Pair.index(), Attr);
  1943. }
  1944. }
  1945. // Slap the source location of the inline asm into a !srcloc metadata on the
  1946. // call.
  1947. if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
  1948. Result.setMetadata("srcloc",
  1949. getAsmSrcLocInfo(gccAsmStmt->getAsmString(), CGF));
  1950. else {
  1951. // At least put the line number on MS inline asm blobs.
  1952. llvm::Constant *Loc =
  1953. llvm::ConstantInt::get(CGF.Int64Ty, S.getAsmLoc().getRawEncoding());
  1954. Result.setMetadata("srcloc",
  1955. llvm::MDNode::get(CGF.getLLVMContext(),
  1956. llvm::ConstantAsMetadata::get(Loc)));
  1957. }
  1958. if (CGF.getLangOpts().assumeFunctionsAreConvergent())
  1959. // Conservatively, mark all inline asm blocks in CUDA or OpenCL as
  1960. // convergent (meaning, they may call an intrinsically convergent op, such
  1961. // as bar.sync, and so can't have certain optimizations applied around
  1962. // them).
  1963. Result.addFnAttr(llvm::Attribute::Convergent);
  1964. // Extract all of the register value results from the asm.
  1965. if (ResultRegTypes.size() == 1) {
  1966. RegResults.push_back(&Result);
  1967. } else {
  1968. for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
  1969. llvm::Value *Tmp = CGF.Builder.CreateExtractValue(&Result, i, "asmresult");
  1970. RegResults.push_back(Tmp);
  1971. }
  1972. }
  1973. }
  1974. void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
  1975. // Assemble the final asm string.
  1976. std::string AsmString = S.generateAsmString(getContext());
  1977. // Get all the output and input constraints together.
  1978. SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
  1979. SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
  1980. for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
  1981. StringRef Name;
  1982. if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
  1983. Name = GAS->getOutputName(i);
  1984. TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name);
  1985. bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid;
  1986. assert(IsValid && "Failed to parse output constraint");
  1987. OutputConstraintInfos.push_back(Info);
  1988. }
  1989. for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
  1990. StringRef Name;
  1991. if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
  1992. Name = GAS->getInputName(i);
  1993. TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name);
  1994. bool IsValid =
  1995. getTarget().validateInputConstraint(OutputConstraintInfos, Info);
  1996. assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
  1997. InputConstraintInfos.push_back(Info);
  1998. }
  1999. std::string Constraints;
  2000. std::vector<LValue> ResultRegDests;
  2001. std::vector<QualType> ResultRegQualTys;
  2002. std::vector<llvm::Type *> ResultRegTypes;
  2003. std::vector<llvm::Type *> ResultTruncRegTypes;
  2004. std::vector<llvm::Type *> ArgTypes;
  2005. std::vector<llvm::Type *> ArgElemTypes;
  2006. std::vector<llvm::Value*> Args;
  2007. llvm::BitVector ResultTypeRequiresCast;
  2008. // Keep track of inout constraints.
  2009. std::string InOutConstraints;
  2010. std::vector<llvm::Value*> InOutArgs;
  2011. std::vector<llvm::Type*> InOutArgTypes;
  2012. std::vector<llvm::Type*> InOutArgElemTypes;
  2013. // Keep track of out constraints for tied input operand.
  2014. std::vector<std::string> OutputConstraints;
  2015. // Keep track of defined physregs.
  2016. llvm::SmallSet<std::string, 8> PhysRegOutputs;
  2017. // An inline asm can be marked readonly if it meets the following conditions:
  2018. // - it doesn't have any sideeffects
  2019. // - it doesn't clobber memory
  2020. // - it doesn't return a value by-reference
  2021. // It can be marked readnone if it doesn't have any input memory constraints
  2022. // in addition to meeting the conditions listed above.
  2023. bool ReadOnly = true, ReadNone = true;
  2024. for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
  2025. TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
  2026. // Simplify the output constraint.
  2027. std::string OutputConstraint(S.getOutputConstraint(i));
  2028. OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
  2029. getTarget(), &OutputConstraintInfos);
  2030. const Expr *OutExpr = S.getOutputExpr(i);
  2031. OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
  2032. std::string GCCReg;
  2033. OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
  2034. getTarget(), CGM, S,
  2035. Info.earlyClobber(),
  2036. &GCCReg);
  2037. // Give an error on multiple outputs to same physreg.
  2038. if (!GCCReg.empty() && !PhysRegOutputs.insert(GCCReg).second)
  2039. CGM.Error(S.getAsmLoc(), "multiple outputs to hard register: " + GCCReg);
  2040. OutputConstraints.push_back(OutputConstraint);
  2041. LValue Dest = EmitLValue(OutExpr);
  2042. if (!Constraints.empty())
  2043. Constraints += ',';
  2044. // If this is a register output, then make the inline asm return it
  2045. // by-value. If this is a memory result, return the value by-reference.
  2046. QualType QTy = OutExpr->getType();
  2047. const bool IsScalarOrAggregate = hasScalarEvaluationKind(QTy) ||
  2048. hasAggregateEvaluationKind(QTy);
  2049. if (!Info.allowsMemory() && IsScalarOrAggregate) {
  2050. Constraints += "=" + OutputConstraint;
  2051. ResultRegQualTys.push_back(QTy);
  2052. ResultRegDests.push_back(Dest);
  2053. llvm::Type *Ty = ConvertTypeForMem(QTy);
  2054. const bool RequiresCast = Info.allowsRegister() &&
  2055. (getTargetHooks().isScalarizableAsmOperand(*this, Ty) ||
  2056. Ty->isAggregateType());
  2057. ResultTruncRegTypes.push_back(Ty);
  2058. ResultTypeRequiresCast.push_back(RequiresCast);
  2059. if (RequiresCast) {
  2060. unsigned Size = getContext().getTypeSize(QTy);
  2061. Ty = llvm::IntegerType::get(getLLVMContext(), Size);
  2062. }
  2063. ResultRegTypes.push_back(Ty);
  2064. // If this output is tied to an input, and if the input is larger, then
  2065. // we need to set the actual result type of the inline asm node to be the
  2066. // same as the input type.
  2067. if (Info.hasMatchingInput()) {
  2068. unsigned InputNo;
  2069. for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
  2070. TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
  2071. if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
  2072. break;
  2073. }
  2074. assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
  2075. QualType InputTy = S.getInputExpr(InputNo)->getType();
  2076. QualType OutputType = OutExpr->getType();
  2077. uint64_t InputSize = getContext().getTypeSize(InputTy);
  2078. if (getContext().getTypeSize(OutputType) < InputSize) {
  2079. // Form the asm to return the value as a larger integer or fp type.
  2080. ResultRegTypes.back() = ConvertType(InputTy);
  2081. }
  2082. }
  2083. if (llvm::Type* AdjTy =
  2084. getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
  2085. ResultRegTypes.back()))
  2086. ResultRegTypes.back() = AdjTy;
  2087. else {
  2088. CGM.getDiags().Report(S.getAsmLoc(),
  2089. diag::err_asm_invalid_type_in_input)
  2090. << OutExpr->getType() << OutputConstraint;
  2091. }
  2092. // Update largest vector width for any vector types.
  2093. if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back()))
  2094. LargestVectorWidth =
  2095. std::max((uint64_t)LargestVectorWidth,
  2096. VT->getPrimitiveSizeInBits().getKnownMinSize());
  2097. } else {
  2098. Address DestAddr = Dest.getAddress(*this);
  2099. // Matrix types in memory are represented by arrays, but accessed through
  2100. // vector pointers, with the alignment specified on the access operation.
  2101. // For inline assembly, update pointer arguments to use vector pointers.
  2102. // Otherwise there will be a mis-match if the matrix is also an
  2103. // input-argument which is represented as vector.
  2104. if (isa<MatrixType>(OutExpr->getType().getCanonicalType()))
  2105. DestAddr = Builder.CreateElementBitCast(
  2106. DestAddr, ConvertType(OutExpr->getType()));
  2107. ArgTypes.push_back(DestAddr.getType());
  2108. ArgElemTypes.push_back(DestAddr.getElementType());
  2109. Args.push_back(DestAddr.getPointer());
  2110. Constraints += "=*";
  2111. Constraints += OutputConstraint;
  2112. ReadOnly = ReadNone = false;
  2113. }
  2114. if (Info.isReadWrite()) {
  2115. InOutConstraints += ',';
  2116. const Expr *InputExpr = S.getOutputExpr(i);
  2117. llvm::Value *Arg;
  2118. llvm::Type *ArgElemType;
  2119. std::tie(Arg, ArgElemType) = EmitAsmInputLValue(
  2120. Info, Dest, InputExpr->getType(), InOutConstraints,
  2121. InputExpr->getExprLoc());
  2122. if (llvm::Type* AdjTy =
  2123. getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
  2124. Arg->getType()))
  2125. Arg = Builder.CreateBitCast(Arg, AdjTy);
  2126. // Update largest vector width for any vector types.
  2127. if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
  2128. LargestVectorWidth =
  2129. std::max((uint64_t)LargestVectorWidth,
  2130. VT->getPrimitiveSizeInBits().getKnownMinSize());
  2131. // Only tie earlyclobber physregs.
  2132. if (Info.allowsRegister() && (GCCReg.empty() || Info.earlyClobber()))
  2133. InOutConstraints += llvm::utostr(i);
  2134. else
  2135. InOutConstraints += OutputConstraint;
  2136. InOutArgTypes.push_back(Arg->getType());
  2137. InOutArgElemTypes.push_back(ArgElemType);
  2138. InOutArgs.push_back(Arg);
  2139. }
  2140. }
  2141. // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX)
  2142. // to the return value slot. Only do this when returning in registers.
  2143. if (isa<MSAsmStmt>(&S)) {
  2144. const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
  2145. if (RetAI.isDirect() || RetAI.isExtend()) {
  2146. // Make a fake lvalue for the return value slot.
  2147. LValue ReturnSlot = MakeAddrLValueWithoutTBAA(ReturnValue, FnRetTy);
  2148. CGM.getTargetCodeGenInfo().addReturnRegisterOutputs(
  2149. *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes,
  2150. ResultRegDests, AsmString, S.getNumOutputs());
  2151. SawAsmBlock = true;
  2152. }
  2153. }
  2154. for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
  2155. const Expr *InputExpr = S.getInputExpr(i);
  2156. TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
  2157. if (Info.allowsMemory())
  2158. ReadNone = false;
  2159. if (!Constraints.empty())
  2160. Constraints += ',';
  2161. // Simplify the input constraint.
  2162. std::string InputConstraint(S.getInputConstraint(i));
  2163. InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(),
  2164. &OutputConstraintInfos);
  2165. InputConstraint = AddVariableConstraints(
  2166. InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()),
  2167. getTarget(), CGM, S, false /* No EarlyClobber */);
  2168. std::string ReplaceConstraint (InputConstraint);
  2169. llvm::Value *Arg;
  2170. llvm::Type *ArgElemType;
  2171. std::tie(Arg, ArgElemType) = EmitAsmInput(Info, InputExpr, Constraints);
  2172. // If this input argument is tied to a larger output result, extend the
  2173. // input to be the same size as the output. The LLVM backend wants to see
  2174. // the input and output of a matching constraint be the same size. Note
  2175. // that GCC does not define what the top bits are here. We use zext because
  2176. // that is usually cheaper, but LLVM IR should really get an anyext someday.
  2177. if (Info.hasTiedOperand()) {
  2178. unsigned Output = Info.getTiedOperand();
  2179. QualType OutputType = S.getOutputExpr(Output)->getType();
  2180. QualType InputTy = InputExpr->getType();
  2181. if (getContext().getTypeSize(OutputType) >
  2182. getContext().getTypeSize(InputTy)) {
  2183. // Use ptrtoint as appropriate so that we can do our extension.
  2184. if (isa<llvm::PointerType>(Arg->getType()))
  2185. Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
  2186. llvm::Type *OutputTy = ConvertType(OutputType);
  2187. if (isa<llvm::IntegerType>(OutputTy))
  2188. Arg = Builder.CreateZExt(Arg, OutputTy);
  2189. else if (isa<llvm::PointerType>(OutputTy))
  2190. Arg = Builder.CreateZExt(Arg, IntPtrTy);
  2191. else {
  2192. assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
  2193. Arg = Builder.CreateFPExt(Arg, OutputTy);
  2194. }
  2195. }
  2196. // Deal with the tied operands' constraint code in adjustInlineAsmType.
  2197. ReplaceConstraint = OutputConstraints[Output];
  2198. }
  2199. if (llvm::Type* AdjTy =
  2200. getTargetHooks().adjustInlineAsmType(*this, ReplaceConstraint,
  2201. Arg->getType()))
  2202. Arg = Builder.CreateBitCast(Arg, AdjTy);
  2203. else
  2204. CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input)
  2205. << InputExpr->getType() << InputConstraint;
  2206. // Update largest vector width for any vector types.
  2207. if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
  2208. LargestVectorWidth =
  2209. std::max((uint64_t)LargestVectorWidth,
  2210. VT->getPrimitiveSizeInBits().getKnownMinSize());
  2211. ArgTypes.push_back(Arg->getType());
  2212. ArgElemTypes.push_back(ArgElemType);
  2213. Args.push_back(Arg);
  2214. Constraints += InputConstraint;
  2215. }
  2216. // Append the "input" part of inout constraints.
  2217. for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
  2218. ArgTypes.push_back(InOutArgTypes[i]);
  2219. ArgElemTypes.push_back(InOutArgElemTypes[i]);
  2220. Args.push_back(InOutArgs[i]);
  2221. }
  2222. Constraints += InOutConstraints;
  2223. // Labels
  2224. SmallVector<llvm::BasicBlock *, 16> Transfer;
  2225. llvm::BasicBlock *Fallthrough = nullptr;
  2226. bool IsGCCAsmGoto = false;
  2227. if (const auto *GS = dyn_cast<GCCAsmStmt>(&S)) {
  2228. IsGCCAsmGoto = GS->isAsmGoto();
  2229. if (IsGCCAsmGoto) {
  2230. for (const auto *E : GS->labels()) {
  2231. JumpDest Dest = getJumpDestForLabel(E->getLabel());
  2232. Transfer.push_back(Dest.getBlock());
  2233. llvm::BlockAddress *BA =
  2234. llvm::BlockAddress::get(CurFn, Dest.getBlock());
  2235. Args.push_back(BA);
  2236. ArgTypes.push_back(BA->getType());
  2237. ArgElemTypes.push_back(nullptr);
  2238. if (!Constraints.empty())
  2239. Constraints += ',';
  2240. Constraints += 'i';
  2241. }
  2242. Fallthrough = createBasicBlock("asm.fallthrough");
  2243. }
  2244. }
  2245. bool HasUnwindClobber = false;
  2246. // Clobbers
  2247. for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
  2248. StringRef Clobber = S.getClobber(i);
  2249. if (Clobber == "memory")
  2250. ReadOnly = ReadNone = false;
  2251. else if (Clobber == "unwind") {
  2252. HasUnwindClobber = true;
  2253. continue;
  2254. } else if (Clobber != "cc") {
  2255. Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
  2256. if (CGM.getCodeGenOpts().StackClashProtector &&
  2257. getTarget().isSPRegName(Clobber)) {
  2258. CGM.getDiags().Report(S.getAsmLoc(),
  2259. diag::warn_stack_clash_protection_inline_asm);
  2260. }
  2261. }
  2262. if (isa<MSAsmStmt>(&S)) {
  2263. if (Clobber == "eax" || Clobber == "edx") {
  2264. if (Constraints.find("=&A") != std::string::npos)
  2265. continue;
  2266. std::string::size_type position1 =
  2267. Constraints.find("={" + Clobber.str() + "}");
  2268. if (position1 != std::string::npos) {
  2269. Constraints.insert(position1 + 1, "&");
  2270. continue;
  2271. }
  2272. std::string::size_type position2 = Constraints.find("=A");
  2273. if (position2 != std::string::npos) {
  2274. Constraints.insert(position2 + 1, "&");
  2275. continue;
  2276. }
  2277. }
  2278. }
  2279. if (!Constraints.empty())
  2280. Constraints += ',';
  2281. Constraints += "~{";
  2282. Constraints += Clobber;
  2283. Constraints += '}';
  2284. }
  2285. assert(!(HasUnwindClobber && IsGCCAsmGoto) &&
  2286. "unwind clobber can't be used with asm goto");
  2287. // Add machine specific clobbers
  2288. std::string MachineClobbers = getTarget().getClobbers();
  2289. if (!MachineClobbers.empty()) {
  2290. if (!Constraints.empty())
  2291. Constraints += ',';
  2292. Constraints += MachineClobbers;
  2293. }
  2294. llvm::Type *ResultType;
  2295. if (ResultRegTypes.empty())
  2296. ResultType = VoidTy;
  2297. else if (ResultRegTypes.size() == 1)
  2298. ResultType = ResultRegTypes[0];
  2299. else
  2300. ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
  2301. llvm::FunctionType *FTy =
  2302. llvm::FunctionType::get(ResultType, ArgTypes, false);
  2303. bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
  2304. llvm::InlineAsm::AsmDialect GnuAsmDialect =
  2305. CGM.getCodeGenOpts().getInlineAsmDialect() == CodeGenOptions::IAD_ATT
  2306. ? llvm::InlineAsm::AD_ATT
  2307. : llvm::InlineAsm::AD_Intel;
  2308. llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
  2309. llvm::InlineAsm::AD_Intel : GnuAsmDialect;
  2310. llvm::InlineAsm *IA = llvm::InlineAsm::get(
  2311. FTy, AsmString, Constraints, HasSideEffect,
  2312. /* IsAlignStack */ false, AsmDialect, HasUnwindClobber);
  2313. std::vector<llvm::Value*> RegResults;
  2314. if (IsGCCAsmGoto) {
  2315. llvm::CallBrInst *Result =
  2316. Builder.CreateCallBr(IA, Fallthrough, Transfer, Args);
  2317. EmitBlock(Fallthrough);
  2318. UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, false,
  2319. ReadOnly, ReadNone, InNoMergeAttributedStmt, S,
  2320. ResultRegTypes, ArgElemTypes, *this, RegResults);
  2321. } else if (HasUnwindClobber) {
  2322. llvm::CallBase *Result = EmitCallOrInvoke(IA, Args, "");
  2323. UpdateAsmCallInst(*Result, HasSideEffect, true, ReadOnly, ReadNone,
  2324. InNoMergeAttributedStmt, S, ResultRegTypes, ArgElemTypes,
  2325. *this, RegResults);
  2326. } else {
  2327. llvm::CallInst *Result =
  2328. Builder.CreateCall(IA, Args, getBundlesForFunclet(IA));
  2329. UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, false,
  2330. ReadOnly, ReadNone, InNoMergeAttributedStmt, S,
  2331. ResultRegTypes, ArgElemTypes, *this, RegResults);
  2332. }
  2333. assert(RegResults.size() == ResultRegTypes.size());
  2334. assert(RegResults.size() == ResultTruncRegTypes.size());
  2335. assert(RegResults.size() == ResultRegDests.size());
  2336. // ResultRegDests can be also populated by addReturnRegisterOutputs() above,
  2337. // in which case its size may grow.
  2338. assert(ResultTypeRequiresCast.size() <= ResultRegDests.size());
  2339. for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
  2340. llvm::Value *Tmp = RegResults[i];
  2341. llvm::Type *TruncTy = ResultTruncRegTypes[i];
  2342. // If the result type of the LLVM IR asm doesn't match the result type of
  2343. // the expression, do the conversion.
  2344. if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
  2345. // Truncate the integer result to the right size, note that TruncTy can be
  2346. // a pointer.
  2347. if (TruncTy->isFloatingPointTy())
  2348. Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
  2349. else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
  2350. uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
  2351. Tmp = Builder.CreateTrunc(Tmp,
  2352. llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
  2353. Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
  2354. } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
  2355. uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
  2356. Tmp = Builder.CreatePtrToInt(Tmp,
  2357. llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
  2358. Tmp = Builder.CreateTrunc(Tmp, TruncTy);
  2359. } else if (TruncTy->isIntegerTy()) {
  2360. Tmp = Builder.CreateZExtOrTrunc(Tmp, TruncTy);
  2361. } else if (TruncTy->isVectorTy()) {
  2362. Tmp = Builder.CreateBitCast(Tmp, TruncTy);
  2363. }
  2364. }
  2365. LValue Dest = ResultRegDests[i];
  2366. // ResultTypeRequiresCast elements correspond to the first
  2367. // ResultTypeRequiresCast.size() elements of RegResults.
  2368. if ((i < ResultTypeRequiresCast.size()) && ResultTypeRequiresCast[i]) {
  2369. unsigned Size = getContext().getTypeSize(ResultRegQualTys[i]);
  2370. Address A = Builder.CreateBitCast(Dest.getAddress(*this),
  2371. ResultRegTypes[i]->getPointerTo());
  2372. if (getTargetHooks().isScalarizableAsmOperand(*this, TruncTy)) {
  2373. Builder.CreateStore(Tmp, A);
  2374. continue;
  2375. }
  2376. QualType Ty = getContext().getIntTypeForBitwidth(Size, /*Signed*/ false);
  2377. if (Ty.isNull()) {
  2378. const Expr *OutExpr = S.getOutputExpr(i);
  2379. CGM.Error(
  2380. OutExpr->getExprLoc(),
  2381. "impossible constraint in asm: can't store value into a register");
  2382. return;
  2383. }
  2384. Dest = MakeAddrLValue(A, Ty);
  2385. }
  2386. EmitStoreThroughLValue(RValue::get(Tmp), Dest);
  2387. }
  2388. }
  2389. LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) {
  2390. const RecordDecl *RD = S.getCapturedRecordDecl();
  2391. QualType RecordTy = getContext().getRecordType(RD);
  2392. // Initialize the captured struct.
  2393. LValue SlotLV =
  2394. MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy);
  2395. RecordDecl::field_iterator CurField = RD->field_begin();
  2396. for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
  2397. E = S.capture_init_end();
  2398. I != E; ++I, ++CurField) {
  2399. LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
  2400. if (CurField->hasCapturedVLAType()) {
  2401. EmitLambdaVLACapture(CurField->getCapturedVLAType(), LV);
  2402. } else {
  2403. EmitInitializerForField(*CurField, LV, *I);
  2404. }
  2405. }
  2406. return SlotLV;
  2407. }
  2408. /// Generate an outlined function for the body of a CapturedStmt, store any
  2409. /// captured variables into the captured struct, and call the outlined function.
  2410. llvm::Function *
  2411. CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
  2412. LValue CapStruct = InitCapturedStruct(S);
  2413. // Emit the CapturedDecl
  2414. CodeGenFunction CGF(CGM, true);
  2415. CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K));
  2416. llvm::Function *F = CGF.GenerateCapturedStmtFunction(S);
  2417. delete CGF.CapturedStmtInfo;
  2418. // Emit call to the helper function.
  2419. EmitCallOrInvoke(F, CapStruct.getPointer(*this));
  2420. return F;
  2421. }
  2422. Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) {
  2423. LValue CapStruct = InitCapturedStruct(S);
  2424. return CapStruct.getAddress(*this);
  2425. }
  2426. /// Creates the outlined function for a CapturedStmt.
  2427. llvm::Function *
  2428. CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) {
  2429. assert(CapturedStmtInfo &&
  2430. "CapturedStmtInfo should be set when generating the captured function");
  2431. const CapturedDecl *CD = S.getCapturedDecl();
  2432. const RecordDecl *RD = S.getCapturedRecordDecl();
  2433. SourceLocation Loc = S.getBeginLoc();
  2434. assert(CD->hasBody() && "missing CapturedDecl body");
  2435. // Build the argument list.
  2436. ASTContext &Ctx = CGM.getContext();
  2437. FunctionArgList Args;
  2438. Args.append(CD->param_begin(), CD->param_end());
  2439. // Create the function declaration.
  2440. const CGFunctionInfo &FuncInfo =
  2441. CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
  2442. llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
  2443. llvm::Function *F =
  2444. llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
  2445. CapturedStmtInfo->getHelperName(), &CGM.getModule());
  2446. CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
  2447. if (CD->isNothrow())
  2448. F->addFnAttr(llvm::Attribute::NoUnwind);
  2449. // Generate the function.
  2450. StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
  2451. CD->getBody()->getBeginLoc());
  2452. // Set the context parameter in CapturedStmtInfo.
  2453. Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam());
  2454. CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr));
  2455. // Initialize variable-length arrays.
  2456. LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(),
  2457. Ctx.getTagDeclType(RD));
  2458. for (auto *FD : RD->fields()) {
  2459. if (FD->hasCapturedVLAType()) {
  2460. auto *ExprArg =
  2461. EmitLoadOfLValue(EmitLValueForField(Base, FD), S.getBeginLoc())
  2462. .getScalarVal();
  2463. auto VAT = FD->getCapturedVLAType();
  2464. VLASizeMap[VAT->getSizeExpr()] = ExprArg;
  2465. }
  2466. }
  2467. // If 'this' is captured, load it into CXXThisValue.
  2468. if (CapturedStmtInfo->isCXXThisExprCaptured()) {
  2469. FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl();
  2470. LValue ThisLValue = EmitLValueForField(Base, FD);
  2471. CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal();
  2472. }
  2473. PGO.assignRegionCounters(GlobalDecl(CD), F);
  2474. CapturedStmtInfo->EmitBody(*this, CD->getBody());
  2475. FinishFunction(CD->getBodyRBrace());
  2476. return F;
  2477. }