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