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