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