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