blob: d0c722ecfb21890f0aad8475f2a9e24f832f9d51 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-function state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Eli Friedman9bc7c8d2008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner3d6606b2008-11-12 08:04:58 +000018#include "clang/AST/APValue.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Anders Carlssonaa3afc72009-04-04 20:47:02 +000021#include "clang/AST/DeclCXX.h"
Mike Stumpfca5da02009-02-21 20:00:35 +000022#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace CodeGen;
25
26CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Mike Stumpecd79422009-03-06 01:33:24 +000027 : BlockFunction(cgm, *this, Builder), CGM(cgm),
28 Target(CGM.getContext().Target),
Owen Anderson215780a2009-07-08 20:52:20 +000029 Builder(cgm.getModule().getContext()),
Anders Carlssonaa3afc72009-04-04 20:47:02 +000030 DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
31 CXXThisDecl(0) {
Mike Stumpfca5da02009-02-21 20:00:35 +000032 LLVMIntTy = ConvertType(getContext().IntTy);
33 LLVMPointerWidth = Target.getPointerWidth(0);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000034}
Chris Lattner4b009652007-07-25 00:24:17 +000035
36ASTContext &CodeGenFunction::getContext() const {
37 return CGM.getContext();
38}
39
40
41llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
42 llvm::BasicBlock *&BB = LabelMap[S];
43 if (BB) return BB;
44
45 // Create, but don't insert, the new block.
Daniel Dunbar72f96552008-11-11 02:29:29 +000046 return BB = createBasicBlock(S->getName());
Chris Lattner4b009652007-07-25 00:24:17 +000047}
48
Daniel Dunbardea59212009-02-25 19:24:29 +000049llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
50 llvm::Value *Res = LocalDeclMap[VD];
51 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
52 return Res;
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000053}
Chris Lattner4b009652007-07-25 00:24:17 +000054
Daniel Dunbardea59212009-02-25 19:24:29 +000055llvm::Constant *
56CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
57 return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
Anders Carlsson75d86732008-09-11 09:15:33 +000058}
59
Daniel Dunbar706059f2009-02-03 23:03:55 +000060const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
61 return CGM.getTypes().ConvertTypeForMem(T);
62}
63
Chris Lattner4b009652007-07-25 00:24:17 +000064const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
65 return CGM.getTypes().ConvertType(T);
66}
67
68bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +000069 // FIXME: Use positive checks instead of negative ones to be more robust in
70 // the face of extension.
Anders Carlssone3ee66c2009-08-09 18:26:27 +000071 return !T->hasPointerRepresentation() && !T->isRealType() &&
Daniel Dunbarfc096bf2009-02-26 20:52:22 +000072 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
Anders Carlssone3ee66c2009-08-09 18:26:27 +000073 !T->isBlockPointerType() && !T->isMemberPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +000074}
75
Daniel Dunbar924f4ea2009-01-26 23:27:52 +000076void CodeGenFunction::EmitReturnBlock() {
77 // For cleanliness, we try to avoid emitting the return block for
78 // simple cases.
79 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
80
81 if (CurBB) {
82 assert(!CurBB->getTerminator() && "Unexpected terminated block.");
83
Daniel Dunbarac8dae22009-07-19 08:24:34 +000084 // We have a valid insert point, reuse it if it is empty or there are no
85 // explicit jumps to the return block.
86 if (CurBB->empty() || ReturnBlock->use_empty()) {
87 ReturnBlock->replaceAllUsesWith(CurBB);
Daniel Dunbar924f4ea2009-01-26 23:27:52 +000088 delete ReturnBlock;
Daniel Dunbarac8dae22009-07-19 08:24:34 +000089 } else
Daniel Dunbar924f4ea2009-01-26 23:27:52 +000090 EmitBlock(ReturnBlock);
91 return;
92 }
93
94 // Otherwise, if the return block is the target of a single direct
95 // branch then we can just put the code in that block instead. This
96 // cleans up functions which started with a unified return block.
97 if (ReturnBlock->hasOneUse()) {
98 llvm::BranchInst *BI =
99 dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
100 if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
101 // Reset insertion point and delete the branch.
102 Builder.SetInsertPoint(BI->getParent());
103 BI->eraseFromParent();
104 delete ReturnBlock;
105 return;
106 }
107 }
108
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000109 // FIXME: We are at an unreachable point, there is no reason to emit the block
110 // unless it has uses. However, we still need a place to put the debug
111 // region.end for now.
Daniel Dunbar924f4ea2009-01-26 23:27:52 +0000112
113 EmitBlock(ReturnBlock);
114}
115
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000116void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar879788d2008-08-04 16:51:22 +0000117 // Finish emission of indirect switches.
118 EmitIndirectSwitches();
119
Chris Lattner4b009652007-07-25 00:24:17 +0000120 assert(BreakContinueStack.empty() &&
121 "mismatched push/pop in break/continue stack!");
Anders Carlssone7de3522009-02-08 00:16:35 +0000122 assert(BlockScopes.empty() &&
123 "did not remove all blocks from block scope map!");
124 assert(CleanupEntries.empty() &&
125 "mismatched push/pop in cleanup stack!");
126
Daniel Dunbar924f4ea2009-01-26 23:27:52 +0000127 // Emit function epilog (to return).
128 EmitReturnBlock();
Daniel Dunbar03f7ae12008-11-11 20:59:54 +0000129
130 // Emit debug descriptor for function end.
Anders Carlsson73007792009-02-13 08:11:52 +0000131 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar03f7ae12008-11-11 20:59:54 +0000132 DI->setLocation(EndLoc);
133 DI->EmitRegionEnd(CurFn, Builder);
134 }
135
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000136 EmitFunctionEpilog(*CurFnInfo, ReturnValue);
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000137
Chris Lattnerca929812007-12-02 06:32:24 +0000138 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
Chris Lattnerfea1a662009-03-31 22:17:44 +0000139 llvm::Instruction *Ptr = AllocaInsertPt;
Chris Lattnerca929812007-12-02 06:32:24 +0000140 AllocaInsertPt = 0;
Chris Lattnerfea1a662009-03-31 22:17:44 +0000141 Ptr->eraseFromParent();
Chris Lattner4b009652007-07-25 00:24:17 +0000142}
143
Daniel Dunbar96816832008-09-09 23:14:03 +0000144void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
145 llvm::Function *Fn,
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000146 const FunctionArgList &Args,
147 SourceLocation StartLoc) {
Anders Carlsson60c4c402009-02-09 20:20:56 +0000148 DidCallStackSave = false;
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000149 CurCodeDecl = CurFuncDecl = D;
Daniel Dunbar96816832008-09-09 23:14:03 +0000150 FnRetTy = RetTy;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000151 CurFn = Fn;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000152 assert(CurFn->isDeclaration() && "Function already has body?");
153
Daniel Dunbar72f96552008-11-11 02:29:29 +0000154 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000155
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000156 // Create a marker to make it easy to insert allocas into the entryblock
157 // later. Don't create this with the builder, because we don't want it
158 // folded.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000159 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
160 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::getInt32Ty(VMContext), "",
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000161 EntryBB);
Chris Lattner7fbb70d2009-03-22 00:24:14 +0000162 if (Builder.isNamePreserving())
163 AllocaInsertPt->setName("allocapt");
164
Daniel Dunbar72f96552008-11-11 02:29:29 +0000165 ReturnBlock = createBasicBlock("return");
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000166 ReturnValue = 0;
Daniel Dunbar96816832008-09-09 23:14:03 +0000167 if (!RetTy->isVoidType())
168 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000169
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000170 Builder.SetInsertPoint(EntryBB);
171
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000172 // Emit subprogram debug descriptor.
Daniel Dunbar96816832008-09-09 23:14:03 +0000173 // FIXME: The cast here is a huge hack.
Anders Carlsson73007792009-02-13 08:11:52 +0000174 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000175 DI->setLocation(StartLoc);
Anders Carlssoncde4a862009-08-08 23:24:23 +0000176 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Douglas Gregor3c3c4542009-02-18 23:53:56 +0000177 DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000178 } else {
179 // Just use LLVM function name.
Daniel Dunbar6bf47dc2009-07-23 05:30:36 +0000180
181 // FIXME: Remove unnecessary conversion to std::string when API settles.
182 DI->EmitFunctionStart(std::string(Fn->getName()).c_str(),
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000183 RetTy, CurFn, Builder);
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000184 }
Sanjiv Gupta1d340eb2008-07-04 11:04:26 +0000185 }
186
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000187 // FIXME: Leaked.
Daniel Dunbar34bda882009-02-02 23:23:47 +0000188 CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000189 EmitFunctionProlog(*CurFnInfo, CurFn, Args);
Anders Carlsson21cecd42008-12-20 21:28:43 +0000190
191 // If any of the arguments have a variably modified type, make sure to
192 // emit the type size.
193 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
194 i != e; ++i) {
195 QualType Ty = i->second;
196
197 if (Ty->isVariablyModifiedType())
198 EmitVLASize(Ty);
199 }
Daniel Dunbar96816832008-09-09 23:14:03 +0000200}
Eli Friedman769e7302008-08-25 21:31:01 +0000201
Daniel Dunbar96816832008-09-09 23:14:03 +0000202void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
203 llvm::Function *Fn) {
Anders Carlsson73007792009-02-13 08:11:52 +0000204 // Check if we should generate debug info for this function.
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000205 if (CGM.getDebugInfo() && !FD->hasAttr<NodebugAttr>())
Anders Carlsson73007792009-02-13 08:11:52 +0000206 DebugInfo = CGM.getDebugInfo();
207
Daniel Dunbar96816832008-09-09 23:14:03 +0000208 FunctionArgList Args;
Anders Carlssonaa3afc72009-04-04 20:47:02 +0000209
210 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
211 if (MD->isInstance()) {
212 // Create the implicit 'this' decl.
213 // FIXME: I'm not entirely sure I like using a fake decl just for code
214 // generation. Maybe we can come up with a better way?
215 CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(),
216 &getContext().Idents.get("this"),
217 MD->getThisType(getContext()));
218 Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
219 }
220 }
221
Eli Friedman769e7302008-08-25 21:31:01 +0000222 if (FD->getNumParams()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000223 const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType();
Eli Friedman769e7302008-08-25 21:31:01 +0000224 assert(FProto && "Function def must have prototype!");
Daniel Dunbar96816832008-09-09 23:14:03 +0000225
226 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
227 Args.push_back(std::make_pair(FD->getParamDecl(i),
228 FProto->getArgType(i)));
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000229 }
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000230
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000231 // FIXME: Support CXXTryStmt here, too.
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000232 if (const CompoundStmt *S = FD->getCompoundBody()) {
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000233 StartFunction(FD, FD->getResultType(), Fn, Args, S->getLBracLoc());
Fariborz Jahanian77cf9fa2009-07-20 22:35:22 +0000234 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
235 EmitCtorPrologue(CD);
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000236 EmitStmt(S);
Fariborz Jahanian36a0ec02009-07-30 17:49:11 +0000237 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
238 EmitDtorEpilogue(DD);
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000239 FinishFunction(S->getRBracLoc());
240 }
Fariborz Jahanian35d13642009-07-30 23:22:00 +0000241 else
242 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Fariborz Jahanianfb658a22009-08-06 23:38:16 +0000243 const CXXRecordDecl *ClassDecl =
244 cast<CXXRecordDecl>(CD->getDeclContext());
245 (void) ClassDecl;
246 if (CD->isCopyConstructor(getContext())) {
247 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
248 "bogus constructor is being synthesize");
Fariborz Jahanianab840aa2009-08-08 19:31:03 +0000249 SynthesizeCXXCopyConstructor(CD, FD, Fn, Args);
Fariborz Jahanianfb658a22009-08-06 23:38:16 +0000250 }
251 else {
252 assert(!ClassDecl->hasUserDeclaredConstructor() &&
253 "bogus constructor is being synthesize");
Fariborz Jahaniane39fab62009-08-10 18:46:38 +0000254 SynthesizeDefaultConstructor(CD, FD, Fn, Args);
Fariborz Jahanianfb658a22009-08-06 23:38:16 +0000255 }
Fariborz Jahanian35d13642009-07-30 23:22:00 +0000256 }
Fariborz Jahanian4252dbc2009-08-17 19:04:50 +0000257 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
258 SynthesizeDefaultDestructor(DD, FD, Fn, Args);
Fariborz Jahanian651efb72009-08-12 21:14:35 +0000259 else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
260 if (MD->isCopyAssignment())
261 SynthesizeCXXCopyAssignment(MD, FD, Fn, Args);
Fariborz Jahanian35d13642009-07-30 23:22:00 +0000262
Anders Carlssonaa3afc72009-04-04 20:47:02 +0000263 // Destroy the 'this' declaration.
264 if (CXXThisDecl)
265 CXXThisDecl->Destroy(getContext());
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000266}
267
Chris Lattner3f73d0d2008-11-11 07:41:27 +0000268/// ContainsLabel - Return true if the statement contains a label in it. If
269/// this statement is not executed normally, it not containing a label means
270/// that we can just remove the code.
271bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
272 // Null statement, not a label!
273 if (S == 0) return false;
274
275 // If this is a label, we have to emit the code, consider something like:
276 // if (0) { ... foo: bar(); } goto foo;
277 if (isa<LabelStmt>(S))
278 return true;
279
280 // If this is a case/default statement, and we haven't seen a switch, we have
281 // to emit the code.
282 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
283 return true;
284
285 // If this is a switch statement, we want to ignore cases below it.
286 if (isa<SwitchStmt>(S))
287 IgnoreCaseStmts = true;
288
289 // Scan subexpressions for verboten labels.
290 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
291 I != E; ++I)
292 if (ContainsLabel(*I, IgnoreCaseStmts))
293 return true;
294
295 return false;
296}
297
Chris Lattner3d6606b2008-11-12 08:04:58 +0000298
299/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
300/// a constant, or if it does but contains a label, return 0. If it constant
301/// folds to 'true' and does not contain a label, return 1, if it constant folds
302/// to 'false' and does not contain a label, return -1.
303int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbar14937022008-11-12 22:37:10 +0000304 // FIXME: Rename and handle conversion of other evaluatable things
305 // to bool.
Anders Carlsson90d85f32008-12-01 02:46:24 +0000306 Expr::EvalResult Result;
307 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
308 Result.HasSideEffects)
Anders Carlssonea3c6ab2008-11-22 22:32:07 +0000309 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattner3d6606b2008-11-12 08:04:58 +0000310
311 if (CodeGenFunction::ContainsLabel(Cond))
312 return 0; // Contains a label.
313
Anders Carlsson90d85f32008-12-01 02:46:24 +0000314 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattner3d6606b2008-11-12 08:04:58 +0000315}
316
317
318/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
319/// statement) to the specified blocks. Based on the condition, this might try
320/// to simplify the codegen of the conditional based on the branch.
321///
322void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
323 llvm::BasicBlock *TrueBlock,
324 llvm::BasicBlock *FalseBlock) {
325 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
326 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
327
328 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
329 // Handle X && Y in a condition.
330 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
331 // If we have "1 && X", simplify the code. "0 && X" would have constant
332 // folded if the case was simple enough.
333 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
334 // br(1 && X) -> br(X).
335 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
336 }
337
338 // If we have "X && 1", simplify the code to use an uncond branch.
339 // "X && 0" would have been constant folded to 0.
340 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
341 // br(X && 1) -> br(X).
342 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
343 }
344
345 // Emit the LHS as a conditional. If the LHS conditional is false, we
346 // want to jump to the FalseBlock.
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000347 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattner3d6606b2008-11-12 08:04:58 +0000348 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
349 EmitBlock(LHSTrue);
350
351 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
352 return;
353 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
354 // If we have "0 || X", simplify the code. "1 || X" would have constant
355 // folded if the case was simple enough.
356 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
357 // br(0 || X) -> br(X).
358 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
359 }
360
361 // If we have "X || 0", simplify the code to use an uncond branch.
362 // "X || 1" would have been constant folded to 1.
363 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
364 // br(X || 0) -> br(X).
365 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
366 }
367
368 // Emit the LHS as a conditional. If the LHS conditional is true, we
369 // want to jump to the TrueBlock.
Daniel Dunbar6e3a10c2008-11-13 01:38:36 +0000370 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattner3d6606b2008-11-12 08:04:58 +0000371 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
372 EmitBlock(LHSFalse);
373
374 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
375 return;
376 }
Chris Lattnercfbfe912008-11-12 08:13:36 +0000377 }
378
379 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
380 // br(!x, t, f) -> br(x, f, t)
381 if (CondUOp->getOpcode() == UnaryOperator::LNot)
382 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattner3d6606b2008-11-12 08:04:58 +0000383 }
384
Daniel Dunbarab81c632008-11-12 10:30:32 +0000385 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
386 // Handle ?: operator.
387
388 // Just ignore GNU ?: extension.
389 if (CondOp->getLHS()) {
390 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
391 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
392 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
393 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
394 EmitBlock(LHSBlock);
395 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
396 EmitBlock(RHSBlock);
397 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
398 return;
399 }
400 }
401
Chris Lattner3d6606b2008-11-12 08:04:58 +0000402 // Emit the code with the fully general case.
403 llvm::Value *CondV = EvaluateExprAsBool(Cond);
404 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
405}
406
Devang Patel7a78e432007-11-01 19:11:01 +0000407/// getCGRecordLayout - Return record layout info.
408const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattner7b2543e2008-02-05 06:55:31 +0000409 QualType Ty) {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000410 const RecordType *RTy = Ty->getAs<RecordType>();
Chris Lattner7b2543e2008-02-05 06:55:31 +0000411 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelaebd83f2007-10-23 02:10:49 +0000412
Chris Lattner7b2543e2008-02-05 06:55:31 +0000413 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelaebd83f2007-10-23 02:10:49 +0000414}
Chris Lattner9d4e6202007-12-02 01:43:38 +0000415
Daniel Dunbar9503b782008-08-16 00:56:44 +0000416/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattner9d4e6202007-12-02 01:43:38 +0000417/// specified stmt yet.
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000418void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
419 bool OmitOnError) {
420 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattner9d4e6202007-12-02 01:43:38 +0000421}
422
Daniel Dunbar879788d2008-08-04 16:51:22 +0000423unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
424 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
425 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
426}
427
Chris Lattner96086d82009-04-21 17:59:23 +0000428void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000429 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000430 if (DestPtr->getType() != BP)
431 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
432
433 // Get size and alignment info for this aggregate.
434 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
435
Chris Lattner96086d82009-04-21 17:59:23 +0000436 // Don't bother emitting a zero-byte memset.
437 if (TypeInfo.first == 0)
438 return;
439
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000440 // FIXME: Handle variable sized types.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000441 const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
442 LLVMPointerWidth);
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000443
444 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000445 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000446 // TypeInfo.first describes size in bits.
Owen Andersonb17ec712009-07-24 23:12:58 +0000447 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000448 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson82b0d0c2008-08-30 19:51:14 +0000449 TypeInfo.second/8));
450}
451
Daniel Dunbar879788d2008-08-04 16:51:22 +0000452void CodeGenFunction::EmitIndirectSwitches() {
453 llvm::BasicBlock *Default;
454
Daniel Dunbar8ccfa802008-08-04 17:24:44 +0000455 if (IndirectSwitches.empty())
456 return;
457
Daniel Dunbar879788d2008-08-04 16:51:22 +0000458 if (!LabelIDs.empty()) {
459 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
460 } else {
461 // No possible targets for indirect goto, just emit an infinite
462 // loop.
Daniel Dunbar72f96552008-11-11 02:29:29 +0000463 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar879788d2008-08-04 16:51:22 +0000464 llvm::BranchInst::Create(Default, Default);
465 }
466
467 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
468 e = IndirectSwitches.end(); i != e; ++i) {
469 llvm::SwitchInst *I = *i;
470
471 I->setSuccessor(0, Default);
472 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
473 LE = LabelIDs.end(); LI != LE; ++LI) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000474 I->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar879788d2008-08-04 16:51:22 +0000475 LI->second),
476 getBasicBlockForLabel(LI->first));
477 }
478 }
479}
Anders Carlsson285611e2008-11-04 05:30:00 +0000480
Daniel Dunbare94e4862009-07-19 06:58:07 +0000481llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
Eli Friedman4712db02009-08-15 02:50:32 +0000482 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000483
Anders Carlssonf860e022008-12-20 20:27:15 +0000484 assert(SizeEntry && "Did not emit size for type");
485 return SizeEntry;
486}
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000487
Daniel Dunbare94e4862009-07-19 06:58:07 +0000488llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
Anders Carlssond9767612008-12-20 20:46:34 +0000489 assert(Ty->isVariablyModifiedType() &&
490 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssonf860e022008-12-20 20:27:15 +0000491
Daniel Dunbare94e4862009-07-19 06:58:07 +0000492 EnsureInsertPoint();
493
Anders Carlssond9767612008-12-20 20:46:34 +0000494 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
Eli Friedman4712db02009-08-15 02:50:32 +0000495 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
Anders Carlssond9767612008-12-20 20:46:34 +0000496
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000497 if (!SizeEntry) {
Anders Carlsson8f30de92009-02-05 19:43:10 +0000498 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
499
Chris Lattner2b8eec12009-08-15 00:03:43 +0000500 // Get the element size;
501 QualType ElemTy = VAT->getElementType();
502 llvm::Value *ElemSize;
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000503 if (ElemTy->isVariableArrayType())
504 ElemSize = EmitVLASize(ElemTy);
Chris Lattner2b8eec12009-08-15 00:03:43 +0000505 else
Owen Andersonb17ec712009-07-24 23:12:58 +0000506 ElemSize = llvm::ConstantInt::get(SizeTy,
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000507 getContext().getTypeSize(ElemTy) / 8);
Anders Carlssond9767612008-12-20 20:46:34 +0000508
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000509 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
Anders Carlsson8f30de92009-02-05 19:43:10 +0000510 NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
511
Anders Carlssonef2f7df2008-12-20 21:51:53 +0000512 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlssond9767612008-12-20 20:46:34 +0000513 }
514
Anders Carlssond9767612008-12-20 20:46:34 +0000515 return SizeEntry;
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000516 }
Anders Carlssond9767612008-12-20 20:46:34 +0000517
Chris Lattner2b8eec12009-08-15 00:03:43 +0000518 if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
519 EmitVLASize(AT->getElementType());
520 return 0;
521 }
522
523 const PointerType *PT = Ty->getAs<PointerType>();
524 assert(PT && "unknown VM type!");
525 EmitVLASize(PT->getPointeeType());
Anders Carlssond9767612008-12-20 20:46:34 +0000526 return 0;
Anders Carlsson32aa0c22008-12-12 07:19:02 +0000527}
Eli Friedman8f5e8782009-01-20 17:46:04 +0000528
529llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
530 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
531 return EmitScalarExpr(E);
532 }
533 return EmitLValue(E).getAddress();
534}
Anders Carlsson9c5b2a42009-02-07 22:53:43 +0000535
Anders Carlsson459ee362009-02-08 03:22:36 +0000536void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
Anders Carlsson9c5b2a42009-02-07 22:53:43 +0000537{
Anders Carlsson9c5b2a42009-02-07 22:53:43 +0000538 CleanupEntries.push_back(CleanupEntry(CleanupBlock));
Anders Carlsson9c5b2a42009-02-07 22:53:43 +0000539}
Anders Carlsson883fa552009-02-07 23:50:39 +0000540
541void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
542{
543 assert(CleanupEntries.size() >= OldCleanupStackSize &&
544 "Cleanup stack mismatch!");
545
546 while (CleanupEntries.size() > OldCleanupStackSize)
547 EmitCleanupBlock();
548}
549
Anders Carlssondf274192009-02-08 07:46:24 +0000550CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
Anders Carlsson883fa552009-02-07 23:50:39 +0000551{
Anders Carlssondf274192009-02-08 07:46:24 +0000552 CleanupEntry &CE = CleanupEntries.back();
553
554 llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
555
556 std::vector<llvm::BasicBlock *> Blocks;
557 std::swap(Blocks, CE.Blocks);
558
559 std::vector<llvm::BranchInst *> BranchFixups;
560 std::swap(BranchFixups, CE.BranchFixups);
561
562 CleanupEntries.pop_back();
563
Anders Carlsson98720f02009-02-08 22:45:15 +0000564 // Check if any branch fixups pointed to the scope we just popped. If so,
565 // we can remove them.
566 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
567 llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
568 BlockScopeMap::iterator I = BlockScopes.find(Dest);
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000569
Anders Carlsson98720f02009-02-08 22:45:15 +0000570 if (I == BlockScopes.end())
571 continue;
Anders Carlsson9b399792009-02-08 01:23:05 +0000572
Anders Carlsson98720f02009-02-08 22:45:15 +0000573 assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000574
Anders Carlsson98720f02009-02-08 22:45:15 +0000575 if (I->second == CleanupEntries.size()) {
576 // We don't need to do this branch fixup.
577 BranchFixups[i] = BranchFixups.back();
578 BranchFixups.pop_back();
579 i--;
580 e--;
581 continue;
Anders Carlsson9b399792009-02-08 01:23:05 +0000582 }
583 }
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000584
Anders Carlssondf274192009-02-08 07:46:24 +0000585 llvm::BasicBlock *SwitchBlock = 0;
586 llvm::BasicBlock *EndBlock = 0;
Anders Carlsson9b399792009-02-08 01:23:05 +0000587 if (!BranchFixups.empty()) {
Anders Carlssondf274192009-02-08 07:46:24 +0000588 SwitchBlock = createBasicBlock("cleanup.switch");
589 EndBlock = createBasicBlock("cleanup.end");
Anders Carlsson9b399792009-02-08 01:23:05 +0000590
Anders Carlssondf274192009-02-08 07:46:24 +0000591 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
Anders Carlsson031eb3e2009-03-17 05:53:35 +0000592
Anders Carlssondf274192009-02-08 07:46:24 +0000593 Builder.SetInsertPoint(SwitchBlock);
594
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000595 llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson9b399792009-02-08 01:23:05 +0000596 "cleanup.dst");
597 llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
598
599 // Create a switch instruction to determine where to jump next.
Anders Carlssondf274192009-02-08 07:46:24 +0000600 llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
Anders Carlsson9b399792009-02-08 01:23:05 +0000601 BranchFixups.size());
Anders Carlssondf274192009-02-08 07:46:24 +0000602
Anders Carlsson32fffb72009-02-08 22:13:37 +0000603 // Restore the current basic block (if any)
Anders Carlsson031eb3e2009-03-17 05:53:35 +0000604 if (CurBB) {
Anders Carlsson32fffb72009-02-08 22:13:37 +0000605 Builder.SetInsertPoint(CurBB);
Anders Carlsson031eb3e2009-03-17 05:53:35 +0000606
607 // If we had a current basic block, we also need to emit an instruction
608 // to initialize the cleanup destination.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000609 Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
Anders Carlsson031eb3e2009-03-17 05:53:35 +0000610 DestCodePtr);
611 } else
Anders Carlsson32fffb72009-02-08 22:13:37 +0000612 Builder.ClearInsertionPoint();
Anders Carlssondf274192009-02-08 07:46:24 +0000613
Anders Carlsson9b399792009-02-08 01:23:05 +0000614 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
615 llvm::BranchInst *BI = BranchFixups[i];
616 llvm::BasicBlock *Dest = BI->getSuccessor(0);
617
Anders Carlsson9b399792009-02-08 01:23:05 +0000618 // Fixup the branch instruction to point to the cleanup block.
619 BI->setSuccessor(0, CleanupBlock);
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000620
Anders Carlsson9b399792009-02-08 01:23:05 +0000621 if (CleanupEntries.empty()) {
Anders Carlsson8be63402009-02-08 22:46:50 +0000622 llvm::ConstantInt *ID;
623
624 // Check if we already have a destination for this block.
625 if (Dest == SI->getDefaultDest())
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000626 ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Anders Carlsson8be63402009-02-08 22:46:50 +0000627 else {
628 ID = SI->findCaseDest(Dest);
629 if (!ID) {
630 // No code found, get a new unique one by using the number of
631 // switch successors.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000632 ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson8be63402009-02-08 22:46:50 +0000633 SI->getNumSuccessors());
634 SI->addCase(ID, Dest);
635 }
636 }
637
638 // Store the jump destination before the branch instruction.
639 new llvm::StoreInst(ID, DestCodePtr, BI);
Anders Carlsson9b399792009-02-08 01:23:05 +0000640 } else {
641 // We need to jump through another cleanup block. Create a pad block
642 // with a branch instruction that jumps to the final destination and
643 // add it as a branch fixup to the current cleanup scope.
644
645 // Create the pad block.
646 llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
Anders Carlsson8be63402009-02-08 22:46:50 +0000647
648 // Create a unique case ID.
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000649 llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson8be63402009-02-08 22:46:50 +0000650 SI->getNumSuccessors());
651
652 // Store the jump destination before the branch instruction.
653 new llvm::StoreInst(ID, DestCodePtr, BI);
654
Anders Carlsson9b399792009-02-08 01:23:05 +0000655 // Add it as the destination.
Anders Carlsson8be63402009-02-08 22:46:50 +0000656 SI->addCase(ID, CleanupPad);
Anders Carlsson9b399792009-02-08 01:23:05 +0000657
658 // Create the branch to the final destination.
659 llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
660 CleanupPad->getInstList().push_back(BI);
661
662 // And add it as a branch fixup.
663 CleanupEntries.back().BranchFixups.push_back(BI);
664 }
665 }
666 }
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000667
Anders Carlssone7de3522009-02-08 00:16:35 +0000668 // Remove all blocks from the block scope map.
669 for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
670 assert(BlockScopes.count(Blocks[i]) &&
671 "Did not find block in scope map!");
672
673 BlockScopes.erase(Blocks[i]);
674 }
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000675
Anders Carlssondf274192009-02-08 07:46:24 +0000676 return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000677}
678
679void CodeGenFunction::EmitCleanupBlock()
680{
Anders Carlssondf274192009-02-08 07:46:24 +0000681 CleanupBlockInfo Info = PopCleanupBlock();
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000682
Anders Carlsson93690d62009-05-31 00:33:20 +0000683 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
684 if (CurBB && !CurBB->getTerminator() &&
685 Info.CleanupBlock->getNumUses() == 0) {
686 CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
687 delete Info.CleanupBlock;
688 } else
689 EmitBlock(Info.CleanupBlock);
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000690
Anders Carlssondf274192009-02-08 07:46:24 +0000691 if (Info.SwitchBlock)
692 EmitBlock(Info.SwitchBlock);
693 if (Info.EndBlock)
694 EmitBlock(Info.EndBlock);
Anders Carlssone75ae4d2009-02-08 03:55:35 +0000695}
696
Anders Carlssoned536bc2009-02-08 00:50:42 +0000697void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
698{
699 assert(!CleanupEntries.empty() &&
700 "Trying to add branch fixup without cleanup block!");
701
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000702 // FIXME: We could be more clever here and check if there's already a branch
703 // fixup for this destination and recycle it.
Anders Carlssoned536bc2009-02-08 00:50:42 +0000704 CleanupEntries.back().BranchFixups.push_back(BI);
705}
706
707void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
708{
Anders Carlsson32fffb72009-02-08 22:13:37 +0000709 if (!HaveInsertPoint())
710 return;
711
Anders Carlssoned536bc2009-02-08 00:50:42 +0000712 llvm::BranchInst* BI = Builder.CreateBr(Dest);
713
Anders Carlsson32fffb72009-02-08 22:13:37 +0000714 Builder.ClearInsertionPoint();
715
Anders Carlssoned536bc2009-02-08 00:50:42 +0000716 // The stack is empty, no need to do any cleanup.
717 if (CleanupEntries.empty())
718 return;
719
720 if (!Dest->getParent()) {
721 // We are trying to branch to a block that hasn't been inserted yet.
722 AddBranchFixup(BI);
723 return;
724 }
725
726 BlockScopeMap::iterator I = BlockScopes.find(Dest);
727 if (I == BlockScopes.end()) {
728 // We are trying to jump to a block that is outside of any cleanup scope.
729 AddBranchFixup(BI);
730 return;
731 }
732
733 assert(I->second < CleanupEntries.size() &&
734 "Trying to branch into cleanup region");
735
736 if (I->second == CleanupEntries.size() - 1) {
737 // We have a branch to a block in the same scope.
738 return;
739 }
740
741 AddBranchFixup(BI);
742}