1 //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This contains code to emit Stmt nodes as LLVM code.
11 //===----------------------------------------------------------------------===//
13 #include "CGDebugInfo.h"
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "TargetInfo.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "clang/Basic/Builtins.h"
20 #include "clang/Basic/PrettyStackTrace.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/MDBuilder.h"
28 using namespace clang;
29 using namespace CodeGen;
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 void CodeGenFunction::EmitStopPoint(const Stmt *S) {
36 if (CGDebugInfo *DI = getDebugInfo()) {
38 Loc = S->getBeginLoc();
39 DI->EmitLocation(Builder, Loc);
45 void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
46 assert(S && "Null statement?");
47 PGO.setCurrentStmt(S);
49 // These statements have their own debug info handling.
50 if (EmitSimpleStmt(S))
53 // Check if we are generating unreachable code.
54 if (!HaveInsertPoint()) {
55 // If so, and the statement doesn't contain a label, then we do not need to
56 // generate actual code. This is safe because (1) the current point is
57 // unreachable, so we don't need to execute the code, and (2) we've already
58 // handled the statements which update internal data structures (like the
59 // local variable map) which could be used by subsequent statements.
60 if (!ContainsLabel(S)) {
61 // Verify that any decl statements were handled as simple, they may be in
62 // scope of subsequent reachable statements.
63 assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
67 // Otherwise, make a new block to hold the code.
71 // Generate a stoppoint if we are emitting debug info.
74 // Ignore all OpenMP directives except for simd if OpenMP with Simd is
76 if (getLangOpts().OpenMP && getLangOpts().OpenMPSimd) {
77 if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) {
78 EmitSimpleOMPExecutableDirective(*D);
83 switch (S->getStmtClass()) {
84 case Stmt::NoStmtClass:
85 case Stmt::CXXCatchStmtClass:
86 case Stmt::SEHExceptStmtClass:
87 case Stmt::SEHFinallyStmtClass:
88 case Stmt::MSDependentExistsStmtClass:
89 llvm_unreachable("invalid statement class to emit generically");
90 case Stmt::NullStmtClass:
91 case Stmt::CompoundStmtClass:
92 case Stmt::DeclStmtClass:
93 case Stmt::LabelStmtClass:
94 case Stmt::AttributedStmtClass:
95 case Stmt::GotoStmtClass:
96 case Stmt::BreakStmtClass:
97 case Stmt::ContinueStmtClass:
98 case Stmt::DefaultStmtClass:
99 case Stmt::CaseStmtClass:
100 case Stmt::SEHLeaveStmtClass:
101 llvm_unreachable("should have emitted these statements as simple");
103 #define STMT(Type, Base)
104 #define ABSTRACT_STMT(Op)
105 #define EXPR(Type, Base) \
106 case Stmt::Type##Class:
107 #include "clang/AST/StmtNodes.inc"
109 // Remember the block we came in on.
110 llvm::BasicBlock *incoming = Builder.GetInsertBlock();
111 assert(incoming && "expression emission must have an insertion point");
113 EmitIgnoredExpr(cast<Expr>(S));
115 llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
116 assert(outgoing && "expression emission cleared block!");
118 // The expression emitters assume (reasonably!) that the insertion
119 // point is always set. To maintain that, the call-emission code
120 // for noreturn functions has to enter a new block with no
121 // predecessors. We want to kill that block and mark the current
122 // insertion point unreachable in the common case of a call like
123 // "exit();". Since expression emission doesn't otherwise create
124 // blocks with no predecessors, we can just test for that.
125 // However, we must be careful not to do this to our incoming
126 // block, because *statement* emission does sometimes create
127 // reachable blocks which will have no predecessors until later in
128 // the function. This occurs with, e.g., labels that are not
129 // reachable by fallthrough.
130 if (incoming != outgoing && outgoing->use_empty()) {
131 outgoing->eraseFromParent();
132 Builder.ClearInsertionPoint();
137 case Stmt::IndirectGotoStmtClass:
138 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
140 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
141 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break;
142 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break;
143 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break;
145 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
147 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
148 case Stmt::GCCAsmStmtClass: // Intentional fall-through.
149 case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
150 case Stmt::CoroutineBodyStmtClass:
151 EmitCoroutineBody(cast<CoroutineBodyStmt>(*S));
153 case Stmt::CoreturnStmtClass:
154 EmitCoreturnStmt(cast<CoreturnStmt>(*S));
156 case Stmt::CapturedStmtClass: {
157 const CapturedStmt *CS = cast<CapturedStmt>(S);
158 EmitCapturedStmt(*CS, CS->getCapturedRegionKind());
161 case Stmt::ObjCAtTryStmtClass:
162 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
164 case Stmt::ObjCAtCatchStmtClass:
166 "@catch statements should be handled by EmitObjCAtTryStmt");
167 case Stmt::ObjCAtFinallyStmtClass:
169 "@finally statements should be handled by EmitObjCAtTryStmt");
170 case Stmt::ObjCAtThrowStmtClass:
171 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
173 case Stmt::ObjCAtSynchronizedStmtClass:
174 EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
176 case Stmt::ObjCForCollectionStmtClass:
177 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
179 case Stmt::ObjCAutoreleasePoolStmtClass:
180 EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
183 case Stmt::CXXTryStmtClass:
184 EmitCXXTryStmt(cast<CXXTryStmt>(*S));
186 case Stmt::CXXForRangeStmtClass:
187 EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs);
189 case Stmt::SEHTryStmtClass:
190 EmitSEHTryStmt(cast<SEHTryStmt>(*S));
192 case Stmt::OMPParallelDirectiveClass:
193 EmitOMPParallelDirective(cast<OMPParallelDirective>(*S));
195 case Stmt::OMPSimdDirectiveClass:
196 EmitOMPSimdDirective(cast<OMPSimdDirective>(*S));
198 case Stmt::OMPForDirectiveClass:
199 EmitOMPForDirective(cast<OMPForDirective>(*S));
201 case Stmt::OMPForSimdDirectiveClass:
202 EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S));
204 case Stmt::OMPSectionsDirectiveClass:
205 EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S));
207 case Stmt::OMPSectionDirectiveClass:
208 EmitOMPSectionDirective(cast<OMPSectionDirective>(*S));
210 case Stmt::OMPSingleDirectiveClass:
211 EmitOMPSingleDirective(cast<OMPSingleDirective>(*S));
213 case Stmt::OMPMasterDirectiveClass:
214 EmitOMPMasterDirective(cast<OMPMasterDirective>(*S));
216 case Stmt::OMPCriticalDirectiveClass:
217 EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S));
219 case Stmt::OMPParallelForDirectiveClass:
220 EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S));
222 case Stmt::OMPParallelForSimdDirectiveClass:
223 EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S));
225 case Stmt::OMPParallelMasterDirectiveClass:
226 EmitOMPParallelMasterDirective(cast<OMPParallelMasterDirective>(*S));
228 case Stmt::OMPParallelSectionsDirectiveClass:
229 EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S));
231 case Stmt::OMPTaskDirectiveClass:
232 EmitOMPTaskDirective(cast<OMPTaskDirective>(*S));
234 case Stmt::OMPTaskyieldDirectiveClass:
235 EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S));
237 case Stmt::OMPBarrierDirectiveClass:
238 EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S));
240 case Stmt::OMPTaskwaitDirectiveClass:
241 EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S));
243 case Stmt::OMPTaskgroupDirectiveClass:
244 EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S));
246 case Stmt::OMPFlushDirectiveClass:
247 EmitOMPFlushDirective(cast<OMPFlushDirective>(*S));
249 case Stmt::OMPOrderedDirectiveClass:
250 EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S));
252 case Stmt::OMPAtomicDirectiveClass:
253 EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S));
255 case Stmt::OMPTargetDirectiveClass:
256 EmitOMPTargetDirective(cast<OMPTargetDirective>(*S));
258 case Stmt::OMPTeamsDirectiveClass:
259 EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S));
261 case Stmt::OMPCancellationPointDirectiveClass:
262 EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S));
264 case Stmt::OMPCancelDirectiveClass:
265 EmitOMPCancelDirective(cast<OMPCancelDirective>(*S));
267 case Stmt::OMPTargetDataDirectiveClass:
268 EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S));
270 case Stmt::OMPTargetEnterDataDirectiveClass:
271 EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S));
273 case Stmt::OMPTargetExitDataDirectiveClass:
274 EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S));
276 case Stmt::OMPTargetParallelDirectiveClass:
277 EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S));
279 case Stmt::OMPTargetParallelForDirectiveClass:
280 EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S));
282 case Stmt::OMPTaskLoopDirectiveClass:
283 EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S));
285 case Stmt::OMPTaskLoopSimdDirectiveClass:
286 EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S));
288 case Stmt::OMPMasterTaskLoopDirectiveClass:
289 EmitOMPMasterTaskLoopDirective(cast<OMPMasterTaskLoopDirective>(*S));
291 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
292 EmitOMPMasterTaskLoopSimdDirective(
293 cast<OMPMasterTaskLoopSimdDirective>(*S));
295 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
296 EmitOMPParallelMasterTaskLoopDirective(
297 cast<OMPParallelMasterTaskLoopDirective>(*S));
299 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
300 EmitOMPParallelMasterTaskLoopSimdDirective(
301 cast<OMPParallelMasterTaskLoopSimdDirective>(*S));
303 case Stmt::OMPDistributeDirectiveClass:
304 EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S));
306 case Stmt::OMPTargetUpdateDirectiveClass:
307 EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S));
309 case Stmt::OMPDistributeParallelForDirectiveClass:
310 EmitOMPDistributeParallelForDirective(
311 cast<OMPDistributeParallelForDirective>(*S));
313 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
314 EmitOMPDistributeParallelForSimdDirective(
315 cast<OMPDistributeParallelForSimdDirective>(*S));
317 case Stmt::OMPDistributeSimdDirectiveClass:
318 EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S));
320 case Stmt::OMPTargetParallelForSimdDirectiveClass:
321 EmitOMPTargetParallelForSimdDirective(
322 cast<OMPTargetParallelForSimdDirective>(*S));
324 case Stmt::OMPTargetSimdDirectiveClass:
325 EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S));
327 case Stmt::OMPTeamsDistributeDirectiveClass:
328 EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S));
330 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
331 EmitOMPTeamsDistributeSimdDirective(
332 cast<OMPTeamsDistributeSimdDirective>(*S));
334 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
335 EmitOMPTeamsDistributeParallelForSimdDirective(
336 cast<OMPTeamsDistributeParallelForSimdDirective>(*S));
338 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
339 EmitOMPTeamsDistributeParallelForDirective(
340 cast<OMPTeamsDistributeParallelForDirective>(*S));
342 case Stmt::OMPTargetTeamsDirectiveClass:
343 EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S));
345 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
346 EmitOMPTargetTeamsDistributeDirective(
347 cast<OMPTargetTeamsDistributeDirective>(*S));
349 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
350 EmitOMPTargetTeamsDistributeParallelForDirective(
351 cast<OMPTargetTeamsDistributeParallelForDirective>(*S));
353 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
354 EmitOMPTargetTeamsDistributeParallelForSimdDirective(
355 cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S));
357 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
358 EmitOMPTargetTeamsDistributeSimdDirective(
359 cast<OMPTargetTeamsDistributeSimdDirective>(*S));
364 bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
365 switch (S->getStmtClass()) {
366 default: return false;
367 case Stmt::NullStmtClass: break;
368 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
369 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
370 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
371 case Stmt::AttributedStmtClass:
372 EmitAttributedStmt(cast<AttributedStmt>(*S)); break;
373 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
374 case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break;
375 case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break;
376 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
377 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
378 case Stmt::SEHLeaveStmtClass: EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); break;
384 /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
385 /// this captures the expression result of the last sub-statement and returns it
386 /// (for use by the statement expression extension).
387 Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
388 AggValueSlot AggSlot) {
389 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
390 "LLVM IR generation of compound statement ('{}')");
392 // Keep track of the current cleanup stack depth, including debug scopes.
393 LexicalScope Scope(*this, S.getSourceRange());
395 return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
399 CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
401 AggValueSlot AggSlot) {
403 const Stmt *ExprResult = S.getStmtExprResult();
404 assert((!GetLast || (GetLast && ExprResult)) &&
405 "If GetLast is true then the CompoundStmt must have a StmtExprResult");
407 Address RetAlloca = Address::invalid();
409 for (auto *CurStmt : S.body()) {
410 if (GetLast && ExprResult == CurStmt) {
411 // We have to special case labels here. They are statements, but when put
412 // at the end of a statement expression, they yield the value of their
413 // subexpression. Handle this by walking through all labels we encounter,
414 // emitting them before we evaluate the subexpr.
415 // Similar issues arise for attributed statements.
416 while (!isa<Expr>(ExprResult)) {
417 if (const auto *LS = dyn_cast<LabelStmt>(ExprResult)) {
418 EmitLabel(LS->getDecl());
419 ExprResult = LS->getSubStmt();
420 } else if (const auto *AS = dyn_cast<AttributedStmt>(ExprResult)) {
421 // FIXME: Update this if we ever have attributes that affect the
422 // semantics of an expression.
423 ExprResult = AS->getSubStmt();
425 llvm_unreachable("unknown value statement");
431 const Expr *E = cast<Expr>(ExprResult);
432 QualType ExprTy = E->getType();
433 if (hasAggregateEvaluationKind(ExprTy)) {
434 EmitAggExpr(E, AggSlot);
436 // We can't return an RValue here because there might be cleanups at
437 // the end of the StmtExpr. Because of that, we have to emit the result
438 // here into a temporary alloca.
439 RetAlloca = CreateMemTemp(ExprTy);
440 EmitAnyExprToMem(E, RetAlloca, Qualifiers(),
451 void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
452 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
454 // If there is a cleanup stack, then we it isn't worth trying to
455 // simplify this block (we would need to remove it from the scope map
456 // and cleanup entry).
457 if (!EHStack.empty())
460 // Can only simplify direct branches.
461 if (!BI || !BI->isUnconditional())
464 // Can only simplify empty blocks.
465 if (BI->getIterator() != BB->begin())
468 BB->replaceAllUsesWith(BI->getSuccessor(0));
469 BI->eraseFromParent();
470 BB->eraseFromParent();
473 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
474 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
476 // Fall out of the current block (if necessary).
479 if (IsFinished && BB->use_empty()) {
484 // Place the block after the current block, if possible, or else at
485 // the end of the function.
486 if (CurBB && CurBB->getParent())
487 CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB);
489 CurFn->getBasicBlockList().push_back(BB);
490 Builder.SetInsertPoint(BB);
493 void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
494 // Emit a branch from the current block to the target one if this
495 // was a real block. If this was just a fall-through block after a
496 // terminator, don't emit it.
497 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
499 if (!CurBB || CurBB->getTerminator()) {
500 // If there is no insert point or the previous block is already
501 // terminated, don't touch it.
503 // Otherwise, create a fall-through branch.
504 Builder.CreateBr(Target);
507 Builder.ClearInsertionPoint();
510 void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
511 bool inserted = false;
512 for (llvm::User *u : block->users()) {
513 if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) {
514 CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(),
522 CurFn->getBasicBlockList().push_back(block);
524 Builder.SetInsertPoint(block);
527 CodeGenFunction::JumpDest
528 CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
529 JumpDest &Dest = LabelMap[D];
530 if (Dest.isValid()) return Dest;
532 // Create, but don't insert, the new block.
533 Dest = JumpDest(createBasicBlock(D->getName()),
534 EHScopeStack::stable_iterator::invalid(),
535 NextCleanupDestIndex++);
539 void CodeGenFunction::EmitLabel(const LabelDecl *D) {
540 // Add this label to the current lexical scope if we're within any
541 // normal cleanups. Jumps "in" to this label --- when permitted by
542 // the language --- may need to be routed around such cleanups.
543 if (EHStack.hasNormalCleanups() && CurLexicalScope)
544 CurLexicalScope->addLabel(D);
546 JumpDest &Dest = LabelMap[D];
548 // If we didn't need a forward reference to this label, just go
549 // ahead and create a destination at the current scope.
550 if (!Dest.isValid()) {
551 Dest = getJumpDestInCurrentScope(D->getName());
553 // Otherwise, we need to give this label a target depth and remove
554 // it from the branch-fixups list.
556 assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
557 Dest.setScopeDepth(EHStack.stable_begin());
558 ResolveBranchFixups(Dest.getBlock());
561 EmitBlock(Dest.getBlock());
563 // Emit debug info for labels.
564 if (CGDebugInfo *DI = getDebugInfo()) {
565 if (CGM.getCodeGenOpts().hasReducedDebugInfo()) {
566 DI->setLocation(D->getLocation());
567 DI->EmitLabel(D, Builder);
571 incrementProfileCounter(D->getStmt());
574 /// Change the cleanup scope of the labels in this lexical scope to
575 /// match the scope of the enclosing context.
576 void CodeGenFunction::LexicalScope::rescopeLabels() {
577 assert(!Labels.empty());
578 EHScopeStack::stable_iterator innermostScope
579 = CGF.EHStack.getInnermostNormalCleanup();
581 // Change the scope depth of all the labels.
582 for (SmallVectorImpl<const LabelDecl*>::const_iterator
583 i = Labels.begin(), e = Labels.end(); i != e; ++i) {
584 assert(CGF.LabelMap.count(*i));
585 JumpDest &dest = CGF.LabelMap.find(*i)->second;
586 assert(dest.getScopeDepth().isValid());
587 assert(innermostScope.encloses(dest.getScopeDepth()));
588 dest.setScopeDepth(innermostScope);
591 // Reparent the labels if the new scope also has cleanups.
592 if (innermostScope != EHScopeStack::stable_end() && ParentScope) {
593 ParentScope->Labels.append(Labels.begin(), Labels.end());
598 void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
599 EmitLabel(S.getDecl());
600 EmitStmt(S.getSubStmt());
603 void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
604 EmitStmt(S.getSubStmt(), S.getAttrs());
607 void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
608 // If this code is reachable then emit a stop point (if generating
609 // debug info). We have to do this ourselves because we are on the
610 // "simple" statement path.
611 if (HaveInsertPoint())
614 EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
618 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
619 if (const LabelDecl *Target = S.getConstantTarget()) {
620 EmitBranchThroughCleanup(getJumpDestForLabel(Target));
624 // Ensure that we have an i8* for our PHI node.
625 llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
627 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
629 // Get the basic block for the indirect goto.
630 llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
632 // The first instruction in the block has to be the PHI for the switch dest,
633 // add an entry for this branch.
634 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
636 EmitBranch(IndGotoBB);
639 void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
640 // C99 6.8.4.1: The first substatement is executed if the expression compares
641 // unequal to 0. The condition must be a scalar type.
642 LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
645 EmitStmt(S.getInit());
647 if (S.getConditionVariable())
648 EmitDecl(*S.getConditionVariable());
650 // If the condition constant folds and can be elided, try to avoid emitting
651 // the condition and the dead arm of the if/else.
653 if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant,
655 // Figure out which block (then or else) is executed.
656 const Stmt *Executed = S.getThen();
657 const Stmt *Skipped = S.getElse();
658 if (!CondConstant) // Condition false?
659 std::swap(Executed, Skipped);
661 // If the skipped block has no labels in it, just emit the executed block.
662 // This avoids emitting dead code and simplifies the CFG substantially.
663 if (S.isConstexpr() || !ContainsLabel(Skipped)) {
665 incrementProfileCounter(&S);
667 RunCleanupsScope ExecutedScope(*this);
674 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
675 // the conditional branch.
676 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
677 llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
678 llvm::BasicBlock *ElseBlock = ContBlock;
680 ElseBlock = createBasicBlock("if.else");
682 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock,
683 getProfileCount(S.getThen()));
685 // Emit the 'then' code.
686 EmitBlock(ThenBlock);
687 incrementProfileCounter(&S);
689 RunCleanupsScope ThenScope(*this);
690 EmitStmt(S.getThen());
692 EmitBranch(ContBlock);
694 // Emit the 'else' code if present.
695 if (const Stmt *Else = S.getElse()) {
697 // There is no need to emit line number for an unconditional branch.
698 auto NL = ApplyDebugLocation::CreateEmpty(*this);
699 EmitBlock(ElseBlock);
702 RunCleanupsScope ElseScope(*this);
706 // There is no need to emit line number for an unconditional branch.
707 auto NL = ApplyDebugLocation::CreateEmpty(*this);
708 EmitBranch(ContBlock);
712 // Emit the continuation block for code after the if.
713 EmitBlock(ContBlock, true);
716 void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
717 ArrayRef<const Attr *> WhileAttrs) {
718 // Emit the header for the loop, which will also become
719 // the continue target.
720 JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
721 EmitBlock(LoopHeader.getBlock());
723 const SourceRange &R = S.getSourceRange();
724 LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), WhileAttrs,
725 SourceLocToDebugLoc(R.getBegin()),
726 SourceLocToDebugLoc(R.getEnd()));
728 // Create an exit block for when the condition fails, which will
729 // also become the break target.
730 JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
732 // Store the blocks to use for break and continue.
733 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
735 // C++ [stmt.while]p2:
736 // When the condition of a while statement is a declaration, the
737 // scope of the variable that is declared extends from its point
738 // of declaration (3.3.2) to the end of the while statement.
740 // The object created in a condition is destroyed and created
741 // with each iteration of the loop.
742 RunCleanupsScope ConditionScope(*this);
744 if (S.getConditionVariable())
745 EmitDecl(*S.getConditionVariable());
747 // Evaluate the conditional in the while header. C99 6.8.5.1: The
748 // evaluation of the controlling expression takes place before each
749 // execution of the loop body.
750 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
752 // while(1) is common, avoid extra exit blocks. Be sure
753 // to correctly handle break/continue though.
754 bool EmitBoolCondBranch = true;
755 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
757 EmitBoolCondBranch = false;
759 // As long as the condition is true, go to the loop body.
760 llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
761 if (EmitBoolCondBranch) {
762 llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
763 if (ConditionScope.requiresCleanups())
764 ExitBlock = createBasicBlock("while.exit");
765 Builder.CreateCondBr(
766 BoolCondVal, LoopBody, ExitBlock,
767 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
769 if (ExitBlock != LoopExit.getBlock()) {
770 EmitBlock(ExitBlock);
771 EmitBranchThroughCleanup(LoopExit);
775 // Emit the loop body. We have to emit this in a cleanup scope
776 // because it might be a singleton DeclStmt.
778 RunCleanupsScope BodyScope(*this);
780 incrementProfileCounter(&S);
781 EmitStmt(S.getBody());
784 BreakContinueStack.pop_back();
786 // Immediately force cleanup.
787 ConditionScope.ForceCleanup();
790 // Branch to the loop header again.
791 EmitBranch(LoopHeader.getBlock());
795 // Emit the exit block.
796 EmitBlock(LoopExit.getBlock(), true);
798 // The LoopHeader typically is just a branch if we skipped emitting
799 // a branch, try to erase it.
800 if (!EmitBoolCondBranch)
801 SimplifyForwardingBlocks(LoopHeader.getBlock());
804 void CodeGenFunction::EmitDoStmt(const DoStmt &S,
805 ArrayRef<const Attr *> DoAttrs) {
806 JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
807 JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
809 uint64_t ParentCount = getCurrentProfileCount();
811 // Store the blocks to use for break and continue.
812 BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
814 // Emit the body of the loop.
815 llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
817 EmitBlockWithFallThrough(LoopBody, &S);
819 RunCleanupsScope BodyScope(*this);
820 EmitStmt(S.getBody());
823 EmitBlock(LoopCond.getBlock());
825 const SourceRange &R = S.getSourceRange();
826 LoopStack.push(LoopBody, CGM.getContext(), DoAttrs,
827 SourceLocToDebugLoc(R.getBegin()),
828 SourceLocToDebugLoc(R.getEnd()));
830 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
831 // after each execution of the loop body."
833 // Evaluate the conditional in the while header.
834 // C99 6.8.5p2/p4: The first substatement is executed if the expression
835 // compares unequal to 0. The condition must be a scalar type.
836 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
838 BreakContinueStack.pop_back();
840 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
841 // to correctly handle break/continue though.
842 bool EmitBoolCondBranch = true;
843 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
845 EmitBoolCondBranch = false;
847 // As long as the condition is true, iterate the loop.
848 if (EmitBoolCondBranch) {
849 uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
850 Builder.CreateCondBr(
851 BoolCondVal, LoopBody, LoopExit.getBlock(),
852 createProfileWeightsForLoop(S.getCond(), BackedgeCount));
857 // Emit the exit block.
858 EmitBlock(LoopExit.getBlock());
860 // The DoCond block typically is just a branch if we skipped
861 // emitting a branch, try to erase it.
862 if (!EmitBoolCondBranch)
863 SimplifyForwardingBlocks(LoopCond.getBlock());
866 void CodeGenFunction::EmitForStmt(const ForStmt &S,
867 ArrayRef<const Attr *> ForAttrs) {
868 JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
870 LexicalScope ForScope(*this, S.getSourceRange());
872 // Evaluate the first part before the loop.
874 EmitStmt(S.getInit());
876 // Start the loop with a block that tests the condition.
877 // If there's an increment, the continue scope will be overwritten
879 JumpDest Continue = getJumpDestInCurrentScope("for.cond");
880 llvm::BasicBlock *CondBlock = Continue.getBlock();
881 EmitBlock(CondBlock);
883 const SourceRange &R = S.getSourceRange();
884 LoopStack.push(CondBlock, CGM.getContext(), ForAttrs,
885 SourceLocToDebugLoc(R.getBegin()),
886 SourceLocToDebugLoc(R.getEnd()));
888 // If the for loop doesn't have an increment we can just use the
889 // condition as the continue block. Otherwise we'll need to create
890 // a block for it (in the current scope, i.e. in the scope of the
891 // condition), and that we will become our continue block.
893 Continue = getJumpDestInCurrentScope("for.inc");
895 // Store the blocks to use for break and continue.
896 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
898 // Create a cleanup scope for the condition variable cleanups.
899 LexicalScope ConditionScope(*this, S.getSourceRange());
902 // If the for statement has a condition scope, emit the local variable
904 if (S.getConditionVariable()) {
905 EmitDecl(*S.getConditionVariable());
908 llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
909 // If there are any cleanups between here and the loop-exit scope,
910 // create a block to stage a loop exit along.
911 if (ForScope.requiresCleanups())
912 ExitBlock = createBasicBlock("for.cond.cleanup");
914 // As long as the condition is true, iterate the loop.
915 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
917 // C99 6.8.5p2/p4: The first substatement is executed if the expression
918 // compares unequal to 0. The condition must be a scalar type.
919 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
920 Builder.CreateCondBr(
921 BoolCondVal, ForBody, ExitBlock,
922 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
924 if (ExitBlock != LoopExit.getBlock()) {
925 EmitBlock(ExitBlock);
926 EmitBranchThroughCleanup(LoopExit);
931 // Treat it as a non-zero constant. Don't even create a new block for the
932 // body, just fall into it.
934 incrementProfileCounter(&S);
937 // Create a separate cleanup scope for the body, in case it is not
938 // a compound statement.
939 RunCleanupsScope BodyScope(*this);
940 EmitStmt(S.getBody());
943 // If there is an increment, emit it next.
945 EmitBlock(Continue.getBlock());
946 EmitStmt(S.getInc());
949 BreakContinueStack.pop_back();
951 ConditionScope.ForceCleanup();
954 EmitBranch(CondBlock);
956 ForScope.ForceCleanup();
960 // Emit the fall-through block.
961 EmitBlock(LoopExit.getBlock(), true);
965 CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
966 ArrayRef<const Attr *> ForAttrs) {
967 JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
969 LexicalScope ForScope(*this, S.getSourceRange());
971 // Evaluate the first pieces before the loop.
973 EmitStmt(S.getInit());
974 EmitStmt(S.getRangeStmt());
975 EmitStmt(S.getBeginStmt());
976 EmitStmt(S.getEndStmt());
978 // Start the loop with a block that tests the condition.
979 // If there's an increment, the continue scope will be overwritten
981 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
982 EmitBlock(CondBlock);
984 const SourceRange &R = S.getSourceRange();
985 LoopStack.push(CondBlock, CGM.getContext(), ForAttrs,
986 SourceLocToDebugLoc(R.getBegin()),
987 SourceLocToDebugLoc(R.getEnd()));
989 // If there are any cleanups between here and the loop-exit scope,
990 // create a block to stage a loop exit along.
991 llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
992 if (ForScope.requiresCleanups())
993 ExitBlock = createBasicBlock("for.cond.cleanup");
995 // The loop body, consisting of the specified body and the loop variable.
996 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
998 // The body is executed if the expression, contextually converted
1000 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
1001 Builder.CreateCondBr(
1002 BoolCondVal, ForBody, ExitBlock,
1003 createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
1005 if (ExitBlock != LoopExit.getBlock()) {
1006 EmitBlock(ExitBlock);
1007 EmitBranchThroughCleanup(LoopExit);
1011 incrementProfileCounter(&S);
1013 // Create a block for the increment. In case of a 'continue', we jump there.
1014 JumpDest Continue = getJumpDestInCurrentScope("for.inc");
1016 // Store the blocks to use for break and continue.
1017 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1020 // Create a separate cleanup scope for the loop variable and body.
1021 LexicalScope BodyScope(*this, S.getSourceRange());
1022 EmitStmt(S.getLoopVarStmt());
1023 EmitStmt(S.getBody());
1027 // If there is an increment, emit it next.
1028 EmitBlock(Continue.getBlock());
1029 EmitStmt(S.getInc());
1031 BreakContinueStack.pop_back();
1033 EmitBranch(CondBlock);
1035 ForScope.ForceCleanup();
1039 // Emit the fall-through block.
1040 EmitBlock(LoopExit.getBlock(), true);
1043 void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
1044 if (RV.isScalar()) {
1045 Builder.CreateStore(RV.getScalarVal(), ReturnValue);
1046 } else if (RV.isAggregate()) {
1047 LValue Dest = MakeAddrLValue(ReturnValue, Ty);
1048 LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty);
1049 EmitAggregateCopy(Dest, Src, Ty, getOverlapForReturnValue());
1051 EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty),
1054 EmitBranchThroughCleanup(ReturnBlock);
1057 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
1058 /// if the function returns void, or may be missing one if the function returns
1059 /// non-void. Fun stuff :).
1060 void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
1061 if (requiresReturnValueCheck()) {
1062 llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc());
1064 new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false,
1065 llvm::GlobalVariable::PrivateLinkage, SLoc);
1066 SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1067 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr);
1068 assert(ReturnLocation.isValid() && "No valid return location");
1069 Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy),
1073 // Returning from an outlined SEH helper is UB, and we already warn on it.
1074 if (IsOutlinedSEHHelper) {
1075 Builder.CreateUnreachable();
1076 Builder.ClearInsertionPoint();
1079 // Emit the result value, even if unused, to evaluate the side effects.
1080 const Expr *RV = S.getRetValue();
1082 // Treat block literals in a return expression as if they appeared
1083 // in their own scope. This permits a small, easily-implemented
1084 // exception to our over-conservative rules about not jumping to
1085 // statements following block literals with non-trivial cleanups.
1086 RunCleanupsScope cleanupScope(*this);
1087 if (const FullExpr *fe = dyn_cast_or_null<FullExpr>(RV)) {
1088 enterFullExpression(fe);
1089 RV = fe->getSubExpr();
1092 // FIXME: Clean this up by using an LValue for ReturnTemp,
1093 // EmitStoreThroughLValue, and EmitAnyExpr.
1094 if (getLangOpts().ElideConstructors &&
1095 S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) {
1096 // Apply the named return value optimization for this return statement,
1097 // which means doing nothing: the appropriate result has already been
1098 // constructed into the NRVO variable.
1100 // If there is an NRVO flag for this variable, set it to 1 into indicate
1101 // that the cleanup code should not destroy the variable.
1102 if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
1103 Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag);
1104 } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) {
1105 // Make sure not to return anything, but evaluate the expression
1106 // for side effects.
1110 // Do nothing (return value is left uninitialized)
1111 } else if (FnRetTy->isReferenceType()) {
1112 // If this function returns a reference, take the address of the expression
1113 // rather than the value.
1114 RValue Result = EmitReferenceBindingToExpr(RV);
1115 Builder.CreateStore(Result.getScalarVal(), ReturnValue);
1117 switch (getEvaluationKind(RV->getType())) {
1119 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
1122 EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()),
1126 EmitAggExpr(RV, AggValueSlot::forAddr(
1127 ReturnValue, Qualifiers(),
1128 AggValueSlot::IsDestructed,
1129 AggValueSlot::DoesNotNeedGCBarriers,
1130 AggValueSlot::IsNotAliased,
1131 getOverlapForReturnValue()));
1137 if (!RV || RV->isEvaluatable(getContext()))
1138 ++NumSimpleReturnExprs;
1140 cleanupScope.ForceCleanup();
1141 EmitBranchThroughCleanup(ReturnBlock);
1144 void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
1145 // As long as debug info is modeled with instructions, we have to ensure we
1146 // have a place to insert here and write the stop point here.
1147 if (HaveInsertPoint())
1150 for (const auto *I : S.decls())
1154 void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
1155 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
1157 // If this code is reachable then emit a stop point (if generating
1158 // debug info). We have to do this ourselves because we are on the
1159 // "simple" statement path.
1160 if (HaveInsertPoint())
1163 EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock);
1166 void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
1167 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
1169 // If this code is reachable then emit a stop point (if generating
1170 // debug info). We have to do this ourselves because we are on the
1171 // "simple" statement path.
1172 if (HaveInsertPoint())
1175 EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock);
1178 /// EmitCaseStmtRange - If case statement range is not too big then
1179 /// add multiple cases to switch instruction, one for each value within
1180 /// the range. If range is too big then emit "if" condition check.
1181 void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
1182 assert(S.getRHS() && "Expected RHS value in CaseStmt");
1184 llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
1185 llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
1187 // Emit the code for this case. We do this first to make sure it is
1188 // properly chained from our predecessor before generating the
1189 // switch machinery to enter this block.
1190 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
1191 EmitBlockWithFallThrough(CaseDest, &S);
1192 EmitStmt(S.getSubStmt());
1194 // If range is empty, do nothing.
1195 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
1198 llvm::APInt Range = RHS - LHS;
1199 // FIXME: parameters such as this should not be hardcoded.
1200 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
1201 // Range is small enough to add multiple switch instruction cases.
1202 uint64_t Total = getProfileCount(&S);
1203 unsigned NCases = Range.getZExtValue() + 1;
1204 // We only have one region counter for the entire set of cases here, so we
1205 // need to divide the weights evenly between the generated cases, ensuring
1206 // that the total weight is preserved. E.g., a weight of 5 over three cases
1207 // will be distributed as weights of 2, 2, and 1.
1208 uint64_t Weight = Total / NCases, Rem = Total % NCases;
1209 for (unsigned I = 0; I != NCases; ++I) {
1211 SwitchWeights->push_back(Weight + (Rem ? 1 : 0));
1214 SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
1220 // The range is too big. Emit "if" condition into a new block,
1221 // making sure to save and restore the current insertion point.
1222 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
1224 // Push this test onto the chain of range checks (which terminates
1225 // in the default basic block). The switch's default will be changed
1226 // to the top of this chain after switch emission is complete.
1227 llvm::BasicBlock *FalseDest = CaseRangeBlock;
1228 CaseRangeBlock = createBasicBlock("sw.caserange");
1230 CurFn->getBasicBlockList().push_back(CaseRangeBlock);
1231 Builder.SetInsertPoint(CaseRangeBlock);
1233 // Emit range check.
1235 Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
1237 Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
1239 llvm::MDNode *Weights = nullptr;
1240 if (SwitchWeights) {
1241 uint64_t ThisCount = getProfileCount(&S);
1242 uint64_t DefaultCount = (*SwitchWeights)[0];
1243 Weights = createProfileWeights(ThisCount, DefaultCount);
1245 // Since we're chaining the switch default through each large case range, we
1246 // need to update the weight for the default, ie, the first case, to include
1248 (*SwitchWeights)[0] += ThisCount;
1250 Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights);
1252 // Restore the appropriate insertion point.
1254 Builder.SetInsertPoint(RestoreBB);
1256 Builder.ClearInsertionPoint();
1259 void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
1260 // If there is no enclosing switch instance that we're aware of, then this
1261 // case statement and its block can be elided. This situation only happens
1262 // when we've constant-folded the switch, are emitting the constant case,
1263 // and part of the constant case includes another case statement. For
1264 // instance: switch (4) { case 4: do { case 5: } while (1); }
1266 EmitStmt(S.getSubStmt());
1270 // Handle case ranges.
1272 EmitCaseStmtRange(S);
1276 llvm::ConstantInt *CaseVal =
1277 Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
1279 // If the body of the case is just a 'break', try to not emit an empty block.
1280 // If we're profiling or we're not optimizing, leave the block in for better
1281 // debug and coverage analysis.
1282 if (!CGM.getCodeGenOpts().hasProfileClangInstr() &&
1283 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1284 isa<BreakStmt>(S.getSubStmt())) {
1285 JumpDest Block = BreakContinueStack.back().BreakBlock;
1287 // Only do this optimization if there are no cleanups that need emitting.
1288 if (isObviouslyBranchWithoutCleanups(Block)) {
1290 SwitchWeights->push_back(getProfileCount(&S));
1291 SwitchInsn->addCase(CaseVal, Block.getBlock());
1293 // If there was a fallthrough into this case, make sure to redirect it to
1294 // the end of the switch as well.
1295 if (Builder.GetInsertBlock()) {
1296 Builder.CreateBr(Block.getBlock());
1297 Builder.ClearInsertionPoint();
1303 llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
1304 EmitBlockWithFallThrough(CaseDest, &S);
1306 SwitchWeights->push_back(getProfileCount(&S));
1307 SwitchInsn->addCase(CaseVal, CaseDest);
1309 // Recursively emitting the statement is acceptable, but is not wonderful for
1310 // code where we have many case statements nested together, i.e.:
1314 // Handling this recursively will create a new block for each case statement
1315 // that falls through to the next case which is IR intensive. It also causes
1316 // deep recursion which can run into stack depth limitations. Handle
1317 // sequential non-range case statements specially.
1318 const CaseStmt *CurCase = &S;
1319 const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
1321 // Otherwise, iteratively add consecutive cases to this switch stmt.
1322 while (NextCase && NextCase->getRHS() == nullptr) {
1324 llvm::ConstantInt *CaseVal =
1325 Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
1328 SwitchWeights->push_back(getProfileCount(NextCase));
1329 if (CGM.getCodeGenOpts().hasProfileClangInstr()) {
1330 CaseDest = createBasicBlock("sw.bb");
1331 EmitBlockWithFallThrough(CaseDest, &S);
1334 SwitchInsn->addCase(CaseVal, CaseDest);
1335 NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
1338 // Normal default recursion for non-cases.
1339 EmitStmt(CurCase->getSubStmt());
1342 void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
1343 // If there is no enclosing switch instance that we're aware of, then this
1344 // default statement can be elided. This situation only happens when we've
1345 // constant-folded the switch.
1347 EmitStmt(S.getSubStmt());
1351 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
1352 assert(DefaultBlock->empty() &&
1353 "EmitDefaultStmt: Default block already defined?");
1355 EmitBlockWithFallThrough(DefaultBlock, &S);
1357 EmitStmt(S.getSubStmt());
1360 /// CollectStatementsForCase - Given the body of a 'switch' statement and a
1361 /// constant value that is being switched on, see if we can dead code eliminate
1362 /// the body of the switch to a simple series of statements to emit. Basically,
1363 /// on a switch (5) we want to find these statements:
1365 /// printf(...); <--
1369 /// and add them to the ResultStmts vector. If it is unsafe to do this
1370 /// transformation (for example, one of the elided statements contains a label
1371 /// that might be jumped to), return CSFC_Failure. If we handled it and 'S'
1372 /// should include statements after it (e.g. the printf() line is a substmt of
1373 /// the case) then return CSFC_FallThrough. If we handled it and found a break
1374 /// statement, then return CSFC_Success.
1376 /// If Case is non-null, then we are looking for the specified case, checking
1377 /// that nothing we jump over contains labels. If Case is null, then we found
1378 /// the case and are looking for the break.
1380 /// If the recursive walk actually finds our Case, then we set FoundCase to
1383 enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
1384 static CSFC_Result CollectStatementsForCase(const Stmt *S,
1385 const SwitchCase *Case,
1387 SmallVectorImpl<const Stmt*> &ResultStmts) {
1388 // If this is a null statement, just succeed.
1390 return Case ? CSFC_Success : CSFC_FallThrough;
1392 // If this is the switchcase (case 4: or default) that we're looking for, then
1393 // we're in business. Just add the substatement.
1394 if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
1397 return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase,
1401 // Otherwise, this is some other case or default statement, just ignore it.
1402 return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
1406 // If we are in the live part of the code and we found our break statement,
1407 // return a success!
1408 if (!Case && isa<BreakStmt>(S))
1409 return CSFC_Success;
1411 // If this is a switch statement, then it might contain the SwitchCase, the
1412 // break, or neither.
1413 if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
1414 // Handle this as two cases: we might be looking for the SwitchCase (if so
1415 // the skipped statements must be skippable) or we might already have it.
1416 CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
1417 bool StartedInLiveCode = FoundCase;
1418 unsigned StartSize = ResultStmts.size();
1420 // If we've not found the case yet, scan through looking for it.
1422 // Keep track of whether we see a skipped declaration. The code could be
1423 // using the declaration even if it is skipped, so we can't optimize out
1424 // the decl if the kept statements might refer to it.
1425 bool HadSkippedDecl = false;
1427 // If we're looking for the case, just see if we can skip each of the
1429 for (; Case && I != E; ++I) {
1430 HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I);
1432 switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
1433 case CSFC_Failure: return CSFC_Failure;
1435 // A successful result means that either 1) that the statement doesn't
1436 // have the case and is skippable, or 2) does contain the case value
1437 // and also contains the break to exit the switch. In the later case,
1438 // we just verify the rest of the statements are elidable.
1440 // If we found the case and skipped declarations, we can't do the
1443 return CSFC_Failure;
1445 for (++I; I != E; ++I)
1446 if (CodeGenFunction::ContainsLabel(*I, true))
1447 return CSFC_Failure;
1448 return CSFC_Success;
1451 case CSFC_FallThrough:
1452 // If we have a fallthrough condition, then we must have found the
1453 // case started to include statements. Consider the rest of the
1454 // statements in the compound statement as candidates for inclusion.
1455 assert(FoundCase && "Didn't find case but returned fallthrough?");
1456 // We recursively found Case, so we're not looking for it anymore.
1459 // If we found the case and skipped declarations, we can't do the
1462 return CSFC_Failure;
1468 return CSFC_Success;
1470 assert(!HadSkippedDecl && "fallthrough after skipping decl");
1473 // If we have statements in our range, then we know that the statements are
1474 // live and need to be added to the set of statements we're tracking.
1475 bool AnyDecls = false;
1476 for (; I != E; ++I) {
1477 AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I);
1479 switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) {
1480 case CSFC_Failure: return CSFC_Failure;
1481 case CSFC_FallThrough:
1482 // A fallthrough result means that the statement was simple and just
1483 // included in ResultStmt, keep adding them afterwards.
1486 // A successful result means that we found the break statement and
1487 // stopped statement inclusion. We just ensure that any leftover stmts
1488 // are skippable and return success ourselves.
1489 for (++I; I != E; ++I)
1490 if (CodeGenFunction::ContainsLabel(*I, true))
1491 return CSFC_Failure;
1492 return CSFC_Success;
1496 // If we're about to fall out of a scope without hitting a 'break;', we
1497 // can't perform the optimization if there were any decls in that scope
1498 // (we'd lose their end-of-lifetime).
1500 // If the entire compound statement was live, there's one more thing we
1501 // can try before giving up: emit the whole thing as a single statement.
1502 // We can do that unless the statement contains a 'break;'.
1503 // FIXME: Such a break must be at the end of a construct within this one.
1504 // We could emit this by just ignoring the BreakStmts entirely.
1505 if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) {
1506 ResultStmts.resize(StartSize);
1507 ResultStmts.push_back(S);
1509 return CSFC_Failure;
1513 return CSFC_FallThrough;
1516 // Okay, this is some other statement that we don't handle explicitly, like a
1517 // for statement or increment etc. If we are skipping over this statement,
1518 // just verify it doesn't have labels, which would make it invalid to elide.
1520 if (CodeGenFunction::ContainsLabel(S, true))
1521 return CSFC_Failure;
1522 return CSFC_Success;
1525 // Otherwise, we want to include this statement. Everything is cool with that
1526 // so long as it doesn't contain a break out of the switch we're in.
1527 if (CodeGenFunction::containsBreak(S)) return CSFC_Failure;
1529 // Otherwise, everything is great. Include the statement and tell the caller
1530 // that we fall through and include the next statement as well.
1531 ResultStmts.push_back(S);
1532 return CSFC_FallThrough;
1535 /// FindCaseStatementsForValue - Find the case statement being jumped to and
1536 /// then invoke CollectStatementsForCase to find the list of statements to emit
1537 /// for a switch on constant. See the comment above CollectStatementsForCase
1538 /// for more details.
1539 static bool FindCaseStatementsForValue(const SwitchStmt &S,
1540 const llvm::APSInt &ConstantCondValue,
1541 SmallVectorImpl<const Stmt*> &ResultStmts,
1543 const SwitchCase *&ResultCase) {
1544 // First step, find the switch case that is being branched to. We can do this
1545 // efficiently by scanning the SwitchCase list.
1546 const SwitchCase *Case = S.getSwitchCaseList();
1547 const DefaultStmt *DefaultCase = nullptr;
1549 for (; Case; Case = Case->getNextSwitchCase()) {
1550 // It's either a default or case. Just remember the default statement in
1551 // case we're not jumping to any numbered cases.
1552 if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
1557 // Check to see if this case is the one we're looking for.
1558 const CaseStmt *CS = cast<CaseStmt>(Case);
1559 // Don't handle case ranges yet.
1560 if (CS->getRHS()) return false;
1562 // If we found our case, remember it as 'case'.
1563 if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
1567 // If we didn't find a matching case, we use a default if it exists, or we
1568 // elide the whole switch body!
1570 // It is safe to elide the body of the switch if it doesn't contain labels
1571 // etc. If it is safe, return successfully with an empty ResultStmts list.
1573 return !CodeGenFunction::ContainsLabel(&S);
1577 // Ok, we know which case is being jumped to, try to collect all the
1578 // statements that follow it. This can fail for a variety of reasons. Also,
1579 // check to see that the recursive walk actually found our case statement.
1580 // Insane cases like this can fail to find it in the recursive walk since we
1581 // don't handle every stmt kind:
1585 bool FoundCase = false;
1587 return CollectStatementsForCase(S.getBody(), Case, FoundCase,
1588 ResultStmts) != CSFC_Failure &&
1592 void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
1593 // Handle nested switch statements.
1594 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
1595 SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights;
1596 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
1598 // See if we can constant fold the condition of the switch and therefore only
1599 // emit the live case statement (if any) of the switch.
1600 llvm::APSInt ConstantCondValue;
1601 if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
1602 SmallVector<const Stmt*, 4> CaseStmts;
1603 const SwitchCase *Case = nullptr;
1604 if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
1605 getContext(), Case)) {
1607 incrementProfileCounter(Case);
1608 RunCleanupsScope ExecutedScope(*this);
1611 EmitStmt(S.getInit());
1613 // Emit the condition variable if needed inside the entire cleanup scope
1614 // used by this special case for constant folded switches.
1615 if (S.getConditionVariable())
1616 EmitDecl(*S.getConditionVariable());
1618 // At this point, we are no longer "within" a switch instance, so
1619 // we can temporarily enforce this to ensure that any embedded case
1620 // statements are not emitted.
1621 SwitchInsn = nullptr;
1623 // Okay, we can dead code eliminate everything except this case. Emit the
1624 // specified series of statements and we're good.
1625 for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
1626 EmitStmt(CaseStmts[i]);
1627 incrementProfileCounter(&S);
1629 // Now we want to restore the saved switch instance so that nested
1630 // switches continue to function properly
1631 SwitchInsn = SavedSwitchInsn;
1637 JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
1639 RunCleanupsScope ConditionScope(*this);
1642 EmitStmt(S.getInit());
1644 if (S.getConditionVariable())
1645 EmitDecl(*S.getConditionVariable());
1646 llvm::Value *CondV = EmitScalarExpr(S.getCond());
1648 // Create basic block to hold stuff that comes after switch
1649 // statement. We also need to create a default block now so that
1650 // explicit case ranges tests can have a place to jump to on
1652 llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
1653 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
1654 if (PGO.haveRegionCounts()) {
1655 // Walk the SwitchCase list to find how many there are.
1656 uint64_t DefaultCount = 0;
1657 unsigned NumCases = 0;
1658 for (const SwitchCase *Case = S.getSwitchCaseList();
1660 Case = Case->getNextSwitchCase()) {
1661 if (isa<DefaultStmt>(Case))
1662 DefaultCount = getProfileCount(Case);
1665 SwitchWeights = new SmallVector<uint64_t, 16>();
1666 SwitchWeights->reserve(NumCases);
1667 // The default needs to be first. We store the edge count, so we already
1668 // know the right weight.
1669 SwitchWeights->push_back(DefaultCount);
1671 CaseRangeBlock = DefaultBlock;
1673 // Clear the insertion point to indicate we are in unreachable code.
1674 Builder.ClearInsertionPoint();
1676 // All break statements jump to NextBlock. If BreakContinueStack is non-empty
1677 // then reuse last ContinueBlock.
1678 JumpDest OuterContinue;
1679 if (!BreakContinueStack.empty())
1680 OuterContinue = BreakContinueStack.back().ContinueBlock;
1682 BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
1684 // Emit switch body.
1685 EmitStmt(S.getBody());
1687 BreakContinueStack.pop_back();
1689 // Update the default block in case explicit case range tests have
1690 // been chained on top.
1691 SwitchInsn->setDefaultDest(CaseRangeBlock);
1693 // If a default was never emitted:
1694 if (!DefaultBlock->getParent()) {
1695 // If we have cleanups, emit the default block so that there's a
1696 // place to jump through the cleanups from.
1697 if (ConditionScope.requiresCleanups()) {
1698 EmitBlock(DefaultBlock);
1700 // Otherwise, just forward the default block to the switch end.
1702 DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
1703 delete DefaultBlock;
1707 ConditionScope.ForceCleanup();
1709 // Emit continuation.
1710 EmitBlock(SwitchExit.getBlock(), true);
1711 incrementProfileCounter(&S);
1713 // If the switch has a condition wrapped by __builtin_unpredictable,
1714 // create metadata that specifies that the switch is unpredictable.
1715 // Don't bother if not optimizing because that metadata would not be used.
1716 auto *Call = dyn_cast<CallExpr>(S.getCond());
1717 if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
1718 auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
1719 if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
1720 llvm::MDBuilder MDHelper(getLLVMContext());
1721 SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable,
1722 MDHelper.createUnpredictable());
1726 if (SwitchWeights) {
1727 assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() &&
1728 "switch weights do not match switch cases");
1729 // If there's only one jump destination there's no sense weighting it.
1730 if (SwitchWeights->size() > 1)
1731 SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
1732 createProfileWeights(*SwitchWeights));
1733 delete SwitchWeights;
1735 SwitchInsn = SavedSwitchInsn;
1736 SwitchWeights = SavedSwitchWeights;
1737 CaseRangeBlock = SavedCRBlock;
1741 SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
1742 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) {
1745 while (*Constraint) {
1746 switch (*Constraint) {
1748 Result += Target.convertConstraint(Constraint);
1754 case '=': // Will see this and the following in mult-alt constraints.
1757 case '#': // Ignore the rest of the constraint alternative.
1758 while (Constraint[1] && Constraint[1] != ',')
1763 Result += *Constraint;
1764 while (Constraint[1] && Constraint[1] == *Constraint)
1775 "Must pass output names to constraints with a symbolic name");
1777 bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index);
1778 assert(result && "Could not resolve symbolic name"); (void)result;
1779 Result += llvm::utostr(Index);
1790 /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
1791 /// as using a particular register add that as a constraint that will be used
1792 /// in this asm stmt.
1794 AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
1795 const TargetInfo &Target, CodeGenModule &CGM,
1796 const AsmStmt &Stmt, const bool EarlyClobber) {
1797 const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
1800 const ValueDecl &Value = *AsmDeclRef->getDecl();
1801 const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
1804 if (Variable->getStorageClass() != SC_Register)
1806 AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
1809 StringRef Register = Attr->getLabel();
1810 assert(Target.isValidGCCRegisterName(Register));
1811 // We're using validateOutputConstraint here because we only care if
1812 // this is a register constraint.
1813 TargetInfo::ConstraintInfo Info(Constraint, "");
1814 if (Target.validateOutputConstraint(Info) &&
1815 !Info.allowsRegister()) {
1816 CGM.ErrorUnsupported(&Stmt, "__asm__");
1819 // Canonicalize the register here before returning it.
1820 Register = Target.getNormalizedGCCRegisterName(Register);
1821 return (EarlyClobber ? "&{" : "{") + Register.str() + "}";
1825 CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
1826 LValue InputValue, QualType InputType,
1827 std::string &ConstraintStr,
1828 SourceLocation Loc) {
1830 if (Info.allowsRegister() || !Info.allowsMemory()) {
1831 if (CodeGenFunction::hasScalarEvaluationKind(InputType)) {
1832 Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal();
1834 llvm::Type *Ty = ConvertType(InputType);
1835 uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
1836 if (Size <= 64 && llvm::isPowerOf2_64(Size)) {
1837 Ty = llvm::IntegerType::get(getLLVMContext(), Size);
1838 Ty = llvm::PointerType::getUnqual(Ty);
1840 Arg = Builder.CreateLoad(
1841 Builder.CreateBitCast(InputValue.getAddress(*this), Ty));
1843 Arg = InputValue.getPointer(*this);
1844 ConstraintStr += '*';
1848 Arg = InputValue.getPointer(*this);
1849 ConstraintStr += '*';
1855 llvm::Value* CodeGenFunction::EmitAsmInput(
1856 const TargetInfo::ConstraintInfo &Info,
1857 const Expr *InputExpr,
1858 std::string &ConstraintStr) {
1859 // If this can't be a register or memory, i.e., has to be a constant
1860 // (immediate or symbolic), try to emit it as such.
1861 if (!Info.allowsRegister() && !Info.allowsMemory()) {
1862 if (Info.requiresImmediateConstant()) {
1863 Expr::EvalResult EVResult;
1864 InputExpr->EvaluateAsRValue(EVResult, getContext(), true);
1866 llvm::APSInt IntResult;
1867 if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
1869 return llvm::ConstantInt::get(getLLVMContext(), IntResult);
1872 Expr::EvalResult Result;
1873 if (InputExpr->EvaluateAsInt(Result, getContext()))
1874 return llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt());
1877 if (Info.allowsRegister() || !Info.allowsMemory())
1878 if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
1879 return EmitScalarExpr(InputExpr);
1880 if (InputExpr->getStmtClass() == Expr::CXXThisExprClass)
1881 return EmitScalarExpr(InputExpr);
1882 InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
1883 LValue Dest = EmitLValue(InputExpr);
1884 return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
1885 InputExpr->getExprLoc());
1888 /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
1889 /// asm call instruction. The !srcloc MDNode contains a list of constant
1890 /// integers which are the source locations of the start of each line in the
1892 static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
1893 CodeGenFunction &CGF) {
1894 SmallVector<llvm::Metadata *, 8> Locs;
1895 // Add the location of the first line to the MDNode.
1896 Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1897 CGF.Int32Ty, Str->getBeginLoc().getRawEncoding())));
1898 StringRef StrVal = Str->getString();
1899 if (!StrVal.empty()) {
1900 const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
1901 const LangOptions &LangOpts = CGF.CGM.getLangOpts();
1902 unsigned StartToken = 0;
1903 unsigned ByteOffset = 0;
1905 // Add the location of the start of each subsequent line of the asm to the
1907 for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) {
1908 if (StrVal[i] != '\n') continue;
1909 SourceLocation LineLoc = Str->getLocationOfByte(
1910 i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
1911 Locs.push_back(llvm::ConstantAsMetadata::get(
1912 llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding())));
1916 return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
1919 static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect,
1920 bool ReadOnly, bool ReadNone, const AsmStmt &S,
1921 const std::vector<llvm::Type *> &ResultRegTypes,
1922 CodeGenFunction &CGF,
1923 std::vector<llvm::Value *> &RegResults) {
1924 Result.addAttribute(llvm::AttributeList::FunctionIndex,
1925 llvm::Attribute::NoUnwind);
1926 // Attach readnone and readonly attributes.
1927 if (!HasSideEffect) {
1929 Result.addAttribute(llvm::AttributeList::FunctionIndex,
1930 llvm::Attribute::ReadNone);
1932 Result.addAttribute(llvm::AttributeList::FunctionIndex,
1933 llvm::Attribute::ReadOnly);
1936 // Slap the source location of the inline asm into a !srcloc metadata on the
1938 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
1939 Result.setMetadata("srcloc",
1940 getAsmSrcLocInfo(gccAsmStmt->getAsmString(), CGF));
1942 // At least put the line number on MS inline asm blobs.
1943 llvm::Constant *Loc = llvm::ConstantInt::get(CGF.Int32Ty,
1944 S.getAsmLoc().getRawEncoding());
1945 Result.setMetadata("srcloc",
1946 llvm::MDNode::get(CGF.getLLVMContext(),
1947 llvm::ConstantAsMetadata::get(Loc)));
1950 if (CGF.getLangOpts().assumeFunctionsAreConvergent())
1951 // Conservatively, mark all inline asm blocks in CUDA or OpenCL as
1952 // convergent (meaning, they may call an intrinsically convergent op, such
1953 // as bar.sync, and so can't have certain optimizations applied around
1955 Result.addAttribute(llvm::AttributeList::FunctionIndex,
1956 llvm::Attribute::Convergent);
1957 // Extract all of the register value results from the asm.
1958 if (ResultRegTypes.size() == 1) {
1959 RegResults.push_back(&Result);
1961 for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) {
1962 llvm::Value *Tmp = CGF.Builder.CreateExtractValue(&Result, i, "asmresult");
1963 RegResults.push_back(Tmp);
1968 void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1969 // Assemble the final asm string.
1970 std::string AsmString = S.generateAsmString(getContext());
1972 // Get all the output and input constraints together.
1973 SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
1974 SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1976 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
1978 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1979 Name = GAS->getOutputName(i);
1980 TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name);
1981 bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid;
1982 assert(IsValid && "Failed to parse output constraint");
1983 OutputConstraintInfos.push_back(Info);
1986 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
1988 if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1989 Name = GAS->getInputName(i);
1990 TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name);
1992 getTarget().validateInputConstraint(OutputConstraintInfos, Info);
1993 assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1994 InputConstraintInfos.push_back(Info);
1997 std::string Constraints;
1999 std::vector<LValue> ResultRegDests;
2000 std::vector<QualType> ResultRegQualTys;
2001 std::vector<llvm::Type *> ResultRegTypes;
2002 std::vector<llvm::Type *> ResultTruncRegTypes;
2003 std::vector<llvm::Type *> ArgTypes;
2004 std::vector<llvm::Value*> Args;
2005 llvm::BitVector ResultTypeRequiresCast;
2007 // Keep track of inout constraints.
2008 std::string InOutConstraints;
2009 std::vector<llvm::Value*> InOutArgs;
2010 std::vector<llvm::Type*> InOutArgTypes;
2012 // Keep track of out constraints for tied input operand.
2013 std::vector<std::string> OutputConstraints;
2015 // An inline asm can be marked readonly if it meets the following conditions:
2016 // - it doesn't have any sideeffects
2017 // - it doesn't clobber memory
2018 // - it doesn't return a value by-reference
2019 // It can be marked readnone if it doesn't have any input memory constraints
2020 // in addition to meeting the conditions listed above.
2021 bool ReadOnly = true, ReadNone = true;
2023 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
2024 TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
2026 // Simplify the output constraint.
2027 std::string OutputConstraint(S.getOutputConstraint(i));
2028 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
2029 getTarget(), &OutputConstraintInfos);
2031 const Expr *OutExpr = S.getOutputExpr(i);
2032 OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
2034 OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
2035 getTarget(), CGM, S,
2036 Info.earlyClobber());
2037 OutputConstraints.push_back(OutputConstraint);
2038 LValue Dest = EmitLValue(OutExpr);
2039 if (!Constraints.empty())
2042 // If this is a register output, then make the inline asm return it
2043 // by-value. If this is a memory result, return the value by-reference.
2044 bool isScalarizableAggregate =
2045 hasAggregateEvaluationKind(OutExpr->getType());
2046 if (!Info.allowsMemory() && (hasScalarEvaluationKind(OutExpr->getType()) ||
2047 isScalarizableAggregate)) {
2048 Constraints += "=" + OutputConstraint;
2049 ResultRegQualTys.push_back(OutExpr->getType());
2050 ResultRegDests.push_back(Dest);
2051 ResultTruncRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
2052 if (Info.allowsRegister() && isScalarizableAggregate) {
2053 ResultTypeRequiresCast.push_back(true);
2054 unsigned Size = getContext().getTypeSize(OutExpr->getType());
2055 llvm::Type *ConvTy = llvm::IntegerType::get(getLLVMContext(), Size);
2056 ResultRegTypes.push_back(ConvTy);
2058 ResultTypeRequiresCast.push_back(false);
2059 ResultRegTypes.push_back(ResultTruncRegTypes.back());
2061 // If this output is tied to an input, and if the input is larger, then
2062 // we need to set the actual result type of the inline asm node to be the
2063 // same as the input type.
2064 if (Info.hasMatchingInput()) {
2066 for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) {
2067 TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
2068 if (Input.hasTiedOperand() && Input.getTiedOperand() == i)
2071 assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
2073 QualType InputTy = S.getInputExpr(InputNo)->getType();
2074 QualType OutputType = OutExpr->getType();
2076 uint64_t InputSize = getContext().getTypeSize(InputTy);
2077 if (getContext().getTypeSize(OutputType) < InputSize) {
2078 // Form the asm to return the value as a larger integer or fp type.
2079 ResultRegTypes.back() = ConvertType(InputTy);
2082 if (llvm::Type* AdjTy =
2083 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
2084 ResultRegTypes.back()))
2085 ResultRegTypes.back() = AdjTy;
2087 CGM.getDiags().Report(S.getAsmLoc(),
2088 diag::err_asm_invalid_type_in_input)
2089 << OutExpr->getType() << OutputConstraint;
2092 // Update largest vector width for any vector types.
2093 if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back()))
2094 LargestVectorWidth = std::max((uint64_t)LargestVectorWidth,
2095 VT->getPrimitiveSizeInBits().getFixedSize());
2097 ArgTypes.push_back(Dest.getAddress(*this).getType());
2098 Args.push_back(Dest.getPointer(*this));
2099 Constraints += "=*";
2100 Constraints += OutputConstraint;
2101 ReadOnly = ReadNone = false;
2104 if (Info.isReadWrite()) {
2105 InOutConstraints += ',';
2107 const Expr *InputExpr = S.getOutputExpr(i);
2108 llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
2110 InputExpr->getExprLoc());
2112 if (llvm::Type* AdjTy =
2113 getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
2115 Arg = Builder.CreateBitCast(Arg, AdjTy);
2117 // Update largest vector width for any vector types.
2118 if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
2119 LargestVectorWidth = std::max((uint64_t)LargestVectorWidth,
2120 VT->getPrimitiveSizeInBits().getFixedSize());
2121 if (Info.allowsRegister())
2122 InOutConstraints += llvm::utostr(i);
2124 InOutConstraints += OutputConstraint;
2126 InOutArgTypes.push_back(Arg->getType());
2127 InOutArgs.push_back(Arg);
2131 // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX)
2132 // to the return value slot. Only do this when returning in registers.
2133 if (isa<MSAsmStmt>(&S)) {
2134 const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
2135 if (RetAI.isDirect() || RetAI.isExtend()) {
2136 // Make a fake lvalue for the return value slot.
2137 LValue ReturnSlot = MakeAddrLValue(ReturnValue, FnRetTy);
2138 CGM.getTargetCodeGenInfo().addReturnRegisterOutputs(
2139 *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes,
2140 ResultRegDests, AsmString, S.getNumOutputs());
2145 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
2146 const Expr *InputExpr = S.getInputExpr(i);
2148 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
2150 if (Info.allowsMemory())
2153 if (!Constraints.empty())
2156 // Simplify the input constraint.
2157 std::string InputConstraint(S.getInputConstraint(i));
2158 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(),
2159 &OutputConstraintInfos);
2161 InputConstraint = AddVariableConstraints(
2162 InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()),
2163 getTarget(), CGM, S, false /* No EarlyClobber */);
2165 std::string ReplaceConstraint (InputConstraint);
2166 llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
2168 // If this input argument is tied to a larger output result, extend the
2169 // input to be the same size as the output. The LLVM backend wants to see
2170 // the input and output of a matching constraint be the same size. Note
2171 // that GCC does not define what the top bits are here. We use zext because
2172 // that is usually cheaper, but LLVM IR should really get an anyext someday.
2173 if (Info.hasTiedOperand()) {
2174 unsigned Output = Info.getTiedOperand();
2175 QualType OutputType = S.getOutputExpr(Output)->getType();
2176 QualType InputTy = InputExpr->getType();
2178 if (getContext().getTypeSize(OutputType) >
2179 getContext().getTypeSize(InputTy)) {
2180 // Use ptrtoint as appropriate so that we can do our extension.
2181 if (isa<llvm::PointerType>(Arg->getType()))
2182 Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
2183 llvm::Type *OutputTy = ConvertType(OutputType);
2184 if (isa<llvm::IntegerType>(OutputTy))
2185 Arg = Builder.CreateZExt(Arg, OutputTy);
2186 else if (isa<llvm::PointerType>(OutputTy))
2187 Arg = Builder.CreateZExt(Arg, IntPtrTy);
2189 assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
2190 Arg = Builder.CreateFPExt(Arg, OutputTy);
2193 // Deal with the tied operands' constraint code in adjustInlineAsmType.
2194 ReplaceConstraint = OutputConstraints[Output];
2196 if (llvm::Type* AdjTy =
2197 getTargetHooks().adjustInlineAsmType(*this, ReplaceConstraint,
2199 Arg = Builder.CreateBitCast(Arg, AdjTy);
2201 CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input)
2202 << InputExpr->getType() << InputConstraint;
2204 // Update largest vector width for any vector types.
2205 if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
2206 LargestVectorWidth = std::max((uint64_t)LargestVectorWidth,
2207 VT->getPrimitiveSizeInBits().getFixedSize());
2209 ArgTypes.push_back(Arg->getType());
2210 Args.push_back(Arg);
2211 Constraints += InputConstraint;
2214 // Append the "input" part of inout constraints last.
2215 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
2216 ArgTypes.push_back(InOutArgTypes[i]);
2217 Args.push_back(InOutArgs[i]);
2219 Constraints += InOutConstraints;
2222 SmallVector<llvm::BasicBlock *, 16> Transfer;
2223 llvm::BasicBlock *Fallthrough = nullptr;
2224 bool IsGCCAsmGoto = false;
2225 if (const auto *GS = dyn_cast<GCCAsmStmt>(&S)) {
2226 IsGCCAsmGoto = GS->isAsmGoto();
2228 for (auto *E : GS->labels()) {
2229 JumpDest Dest = getJumpDestForLabel(E->getLabel());
2230 Transfer.push_back(Dest.getBlock());
2231 llvm::BlockAddress *BA =
2232 llvm::BlockAddress::get(CurFn, Dest.getBlock());
2234 ArgTypes.push_back(BA->getType());
2235 if (!Constraints.empty())
2239 StringRef Name = "asm.fallthrough";
2240 Fallthrough = createBasicBlock(Name);
2245 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
2246 StringRef Clobber = S.getClobber(i);
2248 if (Clobber == "memory")
2249 ReadOnly = ReadNone = false;
2250 else if (Clobber != "cc")
2251 Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
2253 if (!Constraints.empty())
2256 Constraints += "~{";
2257 Constraints += Clobber;
2261 // Add machine specific clobbers
2262 std::string MachineClobbers = getTarget().getClobbers();
2263 if (!MachineClobbers.empty()) {
2264 if (!Constraints.empty())
2266 Constraints += MachineClobbers;
2269 llvm::Type *ResultType;
2270 if (ResultRegTypes.empty())
2271 ResultType = VoidTy;
2272 else if (ResultRegTypes.size() == 1)
2273 ResultType = ResultRegTypes[0];
2275 ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
2277 llvm::FunctionType *FTy =
2278 llvm::FunctionType::get(ResultType, ArgTypes, false);
2280 bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0;
2281 llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
2282 llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT;
2283 llvm::InlineAsm *IA =
2284 llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
2285 /* IsAlignStack */ false, AsmDialect);
2286 std::vector<llvm::Value*> RegResults;
2288 llvm::CallBrInst *Result =
2289 Builder.CreateCallBr(IA, Fallthrough, Transfer, Args);
2290 UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2291 ReadNone, S, ResultRegTypes, *this, RegResults);
2292 EmitBlock(Fallthrough);
2294 llvm::CallInst *Result =
2295 Builder.CreateCall(IA, Args, getBundlesForFunclet(IA));
2296 UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2297 ReadNone, S, ResultRegTypes, *this, RegResults);
2300 assert(RegResults.size() == ResultRegTypes.size());
2301 assert(RegResults.size() == ResultTruncRegTypes.size());
2302 assert(RegResults.size() == ResultRegDests.size());
2303 // ResultRegDests can be also populated by addReturnRegisterOutputs() above,
2304 // in which case its size may grow.
2305 assert(ResultTypeRequiresCast.size() <= ResultRegDests.size());
2306 for (unsigned i = 0, e = RegResults.size(); i != e; ++i) {
2307 llvm::Value *Tmp = RegResults[i];
2309 // If the result type of the LLVM IR asm doesn't match the result type of
2310 // the expression, do the conversion.
2311 if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
2312 llvm::Type *TruncTy = ResultTruncRegTypes[i];
2314 // Truncate the integer result to the right size, note that TruncTy can be
2316 if (TruncTy->isFloatingPointTy())
2317 Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
2318 else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) {
2319 uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
2320 Tmp = Builder.CreateTrunc(Tmp,
2321 llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
2322 Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
2323 } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
2324 uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
2325 Tmp = Builder.CreatePtrToInt(Tmp,
2326 llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
2327 Tmp = Builder.CreateTrunc(Tmp, TruncTy);
2328 } else if (TruncTy->isIntegerTy()) {
2329 Tmp = Builder.CreateZExtOrTrunc(Tmp, TruncTy);
2330 } else if (TruncTy->isVectorTy()) {
2331 Tmp = Builder.CreateBitCast(Tmp, TruncTy);
2335 LValue Dest = ResultRegDests[i];
2336 // ResultTypeRequiresCast elements correspond to the first
2337 // ResultTypeRequiresCast.size() elements of RegResults.
2338 if ((i < ResultTypeRequiresCast.size()) && ResultTypeRequiresCast[i]) {
2339 unsigned Size = getContext().getTypeSize(ResultRegQualTys[i]);
2340 Address A = Builder.CreateBitCast(Dest.getAddress(*this),
2341 ResultRegTypes[i]->getPointerTo());
2342 QualType Ty = getContext().getIntTypeForBitwidth(Size, /*Signed*/ false);
2344 const Expr *OutExpr = S.getOutputExpr(i);
2346 OutExpr->getExprLoc(),
2347 "impossible constraint in asm: can't store value into a register");
2350 Dest = MakeAddrLValue(A, Ty);
2352 EmitStoreThroughLValue(RValue::get(Tmp), Dest);
2356 LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) {
2357 const RecordDecl *RD = S.getCapturedRecordDecl();
2358 QualType RecordTy = getContext().getRecordType(RD);
2360 // Initialize the captured struct.
2362 MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy);
2364 RecordDecl::field_iterator CurField = RD->field_begin();
2365 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
2366 E = S.capture_init_end();
2367 I != E; ++I, ++CurField) {
2368 LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
2369 if (CurField->hasCapturedVLAType()) {
2370 auto VAT = CurField->getCapturedVLAType();
2371 EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
2373 EmitInitializerForField(*CurField, LV, *I);
2380 /// Generate an outlined function for the body of a CapturedStmt, store any
2381 /// captured variables into the captured struct, and call the outlined function.
2383 CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
2384 LValue CapStruct = InitCapturedStruct(S);
2386 // Emit the CapturedDecl
2387 CodeGenFunction CGF(CGM, true);
2388 CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K));
2389 llvm::Function *F = CGF.GenerateCapturedStmtFunction(S);
2390 delete CGF.CapturedStmtInfo;
2392 // Emit call to the helper function.
2393 EmitCallOrInvoke(F, CapStruct.getPointer(*this));
2398 Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) {
2399 LValue CapStruct = InitCapturedStruct(S);
2400 return CapStruct.getAddress(*this);
2403 /// Creates the outlined function for a CapturedStmt.
2405 CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) {
2406 assert(CapturedStmtInfo &&
2407 "CapturedStmtInfo should be set when generating the captured function");
2408 const CapturedDecl *CD = S.getCapturedDecl();
2409 const RecordDecl *RD = S.getCapturedRecordDecl();
2410 SourceLocation Loc = S.getBeginLoc();
2411 assert(CD->hasBody() && "missing CapturedDecl body");
2413 // Build the argument list.
2414 ASTContext &Ctx = CGM.getContext();
2415 FunctionArgList Args;
2416 Args.append(CD->param_begin(), CD->param_end());
2418 // Create the function declaration.
2419 const CGFunctionInfo &FuncInfo =
2420 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
2421 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
2424 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
2425 CapturedStmtInfo->getHelperName(), &CGM.getModule());
2426 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
2427 if (CD->isNothrow())
2428 F->addFnAttr(llvm::Attribute::NoUnwind);
2430 // Generate the function.
2431 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
2432 CD->getBody()->getBeginLoc());
2433 // Set the context parameter in CapturedStmtInfo.
2434 Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam());
2435 CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr));
2437 // Initialize variable-length arrays.
2438 LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(),
2439 Ctx.getTagDeclType(RD));
2440 for (auto *FD : RD->fields()) {
2441 if (FD->hasCapturedVLAType()) {
2443 EmitLoadOfLValue(EmitLValueForField(Base, FD), S.getBeginLoc())
2445 auto VAT = FD->getCapturedVLAType();
2446 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
2450 // If 'this' is captured, load it into CXXThisValue.
2451 if (CapturedStmtInfo->isCXXThisExprCaptured()) {
2452 FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl();
2453 LValue ThisLValue = EmitLValueForField(Base, FD);
2454 CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal();
2457 PGO.assignRegionCounters(GlobalDecl(CD), F);
2458 CapturedStmtInfo->EmitBody(*this, CD->getBody());
2459 FinishFunction(CD->getBodyRBrace());