blob: 1e8d05261db2d861a4e740247b49c43b9138ce9b [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerbed31442007-05-28 01:07:47 +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 Friedman17630752008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnercd439292008-11-12 08:04:58 +000018#include "clang/AST/APValue.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Anders Carlsson468fa632009-04-04 20:47:02 +000021#include "clang/AST/DeclCXX.h"
Mike Stumpcb2fbcb2009-02-21 20:00:35 +000022#include "llvm/Target/TargetData.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattnerd1af2d22007-05-29 23:17:50 +000026CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Mike Stump0c743272009-03-06 01:33:24 +000027 : BlockFunction(cgm, *this, Builder), CGM(cgm),
28 Target(CGM.getContext().Target),
Owen Andersonc9673d52009-07-08 20:52:20 +000029 Builder(cgm.getModule().getContext()),
Anders Carlsson468fa632009-04-04 20:47:02 +000030 DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
31 CXXThisDecl(0) {
Mike Stumpcb2fbcb2009-02-21 20:00:35 +000032 LLVMIntTy = ConvertType(getContext().IntTy);
33 LLVMPointerWidth = Target.getPointerWidth(0);
Chris Lattner5696e7b2008-06-17 18:05:57 +000034}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000035
Chris Lattner6db1fb82007-06-02 22:49:07 +000036ASTContext &CodeGenFunction::getContext() const {
37 return CGM.getContext();
38}
39
Chris Lattnerd1af2d22007-05-29 23:17:50 +000040
Chris Lattnerac248202007-05-30 00:13:02 +000041llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000042 llvm::BasicBlock *&BB = LabelMap[S];
Chris Lattnerac248202007-05-30 00:13:02 +000043 if (BB) return BB;
44
45 // Create, but don't insert, the new block.
Daniel Dunbar75283ff2008-11-11 02:29:29 +000046 return BB = createBasicBlock(S->getName());
Chris Lattnerac248202007-05-30 00:13:02 +000047}
48
Daniel Dunbar22a87f92009-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 Venancio01a72ff2008-02-26 21:41:45 +000053}
Chris Lattnerac248202007-05-30 00:13:02 +000054
Daniel Dunbar22a87f92009-02-25 19:24:29 +000055llvm::Constant *
56CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
57 return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
Anders Carlsson9396a892008-09-11 09:15:33 +000058}
59
Daniel Dunbaree3da872009-02-03 23:03:55 +000060const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
61 return CGM.getTypes().ConvertTypeForMem(T);
62}
63
Chris Lattnerf033c142007-06-22 19:05:19 +000064const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
65 return CGM.getTypes().ConvertType(T);
Chris Lattner45bb9142007-06-09 02:28:57 +000066}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000067
Chris Lattner54fb19e2007-06-22 22:02:34 +000068bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Mike Stump18bb9282009-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 Carlssonf48123b2009-08-09 18:26:27 +000071 return !T->hasPointerRepresentation() && !T->isRealType() &&
Daniel Dunbar76ba41c2009-02-26 20:52:22 +000072 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
Anders Carlssonf48123b2009-08-09 18:26:27 +000073 !T->isBlockPointerType() && !T->isMemberPointerType();
Chris Lattner54fb19e2007-06-22 22:02:34 +000074}
75
Daniel Dunbarfd346a32009-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 Dunbarea3060a2009-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 Dunbarfd346a32009-01-26 23:27:52 +000088 delete ReturnBlock;
Daniel Dunbarea3060a2009-07-19 08:24:34 +000089 } else
Daniel Dunbarfd346a32009-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 Stump18bb9282009-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 Dunbarfd346a32009-01-26 23:27:52 +0000112
113 EmitBlock(ReturnBlock);
114}
115
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000116void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000117 // Finish emission of indirect switches.
118 EmitIndirectSwitches();
119
Chris Lattnere73e4322007-07-16 21:28:45 +0000120 assert(BreakContinueStack.empty() &&
121 "mismatched push/pop in break/continue stack!");
Anders Carlssonfbfb5e62009-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 Dunbarfd346a32009-01-26 23:27:52 +0000127 // Emit function epilog (to return).
128 EmitReturnBlock();
Daniel Dunbarfab3f932008-11-11 20:59:54 +0000129
130 // Emit debug descriptor for function end.
Anders Carlsson63784f42009-02-13 08:11:52 +0000131 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbarfab3f932008-11-11 20:59:54 +0000132 DI->setLocation(EndLoc);
133 DI->EmitRegionEnd(CurFn, Builder);
134 }
135
Daniel Dunbard931a872009-02-02 22:03:45 +0000136 EmitFunctionEpilog(*CurFnInfo, ReturnValue);
Daniel Dunbar54bb1932008-09-09 21:00:17 +0000137
Chris Lattnerc48023b2007-12-02 06:32:24 +0000138 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
Chris Lattner2739d2b2009-03-31 22:17:44 +0000139 llvm::Instruction *Ptr = AllocaInsertPt;
Chris Lattnerc48023b2007-12-02 06:32:24 +0000140 AllocaInsertPt = 0;
Chris Lattner2739d2b2009-03-31 22:17:44 +0000141 Ptr->eraseFromParent();
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000142}
Chris Lattner308f4312007-05-29 23:50:05 +0000143
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000144void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
145 llvm::Function *Fn,
Daniel Dunbar354d2782008-10-18 18:22:23 +0000146 const FunctionArgList &Args,
147 SourceLocation StartLoc) {
Anders Carlssonf4478e92009-02-09 20:20:56 +0000148 DidCallStackSave = false;
Chris Lattner28ec0cf2009-04-23 05:30:27 +0000149 CurCodeDecl = CurFuncDecl = D;
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000150 FnRetTy = RetTy;
Daniel Dunbar9c426522008-07-29 23:18:29 +0000151 CurFn = Fn;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000152 assert(CurFn->isDeclaration() && "Function already has body?");
153
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000154 llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
Daniel Dunbar54bb1932008-09-09 21:00:17 +0000155
Chris Lattner5696e7b2008-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 Anderson41a75022009-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 Lattner5696e7b2008-06-17 18:05:57 +0000161 EntryBB);
Chris Lattner47640222009-03-22 00:24:14 +0000162 if (Builder.isNamePreserving())
163 AllocaInsertPt->setName("allocapt");
164
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000165 ReturnBlock = createBasicBlock("return");
Daniel Dunbar54bb1932008-09-09 21:00:17 +0000166 ReturnValue = 0;
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000167 if (!RetTy->isVoidType())
168 ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
Daniel Dunbar54bb1932008-09-09 21:00:17 +0000169
Chris Lattner5696e7b2008-06-17 18:05:57 +0000170 Builder.SetInsertPoint(EntryBB);
171
Sanjiv Gupta1e8b6082008-07-04 11:04:26 +0000172 // Emit subprogram debug descriptor.
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000173 // FIXME: The cast here is a huge hack.
Anders Carlsson63784f42009-02-13 08:11:52 +0000174 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar354d2782008-10-18 18:22:23 +0000175 DI->setLocation(StartLoc);
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000176 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Douglas Gregor5f361c92009-02-18 23:53:56 +0000177 DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
Daniel Dunbar354d2782008-10-18 18:22:23 +0000178 } else {
179 // Just use LLVM function name.
Daniel Dunbar7c02cf62009-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 Dunbar354d2782008-10-18 18:22:23 +0000183 RetTy, CurFn, Builder);
Sanjiv Gupta1e8b6082008-07-04 11:04:26 +0000184 }
Sanjiv Gupta1e8b6082008-07-04 11:04:26 +0000185 }
186
Daniel Dunbard931a872009-02-02 22:03:45 +0000187 // FIXME: Leaked.
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000188 CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
Daniel Dunbard931a872009-02-02 22:03:45 +0000189 EmitFunctionProlog(*CurFnInfo, CurFn, Args);
Anders Carlssonc20879a2008-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 Dunbarbc915f42008-09-09 23:14:03 +0000200}
Eli Friedman3d421e12008-08-25 21:31:01 +0000201
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000202void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
203 llvm::Function *Fn) {
Anders Carlsson63784f42009-02-13 08:11:52 +0000204 // Check if we should generate debug info for this function.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000205 if (CGM.getDebugInfo() && !FD->hasAttr<NodebugAttr>())
Anders Carlsson63784f42009-02-13 08:11:52 +0000206 DebugInfo = CGM.getDebugInfo();
207
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000208 FunctionArgList Args;
Anders Carlsson468fa632009-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 Friedman3d421e12008-08-25 21:31:01 +0000222 if (FD->getNumParams()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000223 const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType();
Eli Friedman3d421e12008-08-25 21:31:01 +0000224 assert(FProto && "Function def must have prototype!");
Daniel Dunbarbc915f42008-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 Lattner5696e7b2008-06-17 18:05:57 +0000229 }
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000230
Sebastian Redla7b98a72009-04-26 20:35:05 +0000231 // FIXME: Support CXXTryStmt here, too.
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000232 if (const CompoundStmt *S = FD->getCompoundBody()) {
Sebastian Redla7b98a72009-04-26 20:35:05 +0000233 StartFunction(FD, FD->getResultType(), Fn, Args, S->getLBracLoc());
Fariborz Jahanian127059c2009-07-20 22:35:22 +0000234 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
235 EmitCtorPrologue(CD);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000236 EmitStmt(S);
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +0000237 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
238 EmitDtorEpilogue(DD);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000239 FinishFunction(S->getRBracLoc());
240 }
Fariborz Jahanian6f14c732009-07-30 23:22:00 +0000241 else
242 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Fariborz Jahanian9301b242009-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 Jahanianf6bda5d2009-08-08 19:31:03 +0000249 SynthesizeCXXCopyConstructor(CD, FD, Fn, Args);
Fariborz Jahanian9301b242009-08-06 23:38:16 +0000250 }
251 else {
252 assert(!ClassDecl->hasUserDeclaredConstructor() &&
253 "bogus constructor is being synthesize");
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +0000254 SynthesizeDefaultConstructor(CD, FD, Fn, Args);
Fariborz Jahanian9301b242009-08-06 23:38:16 +0000255 }
Fariborz Jahanian6f14c732009-07-30 23:22:00 +0000256 }
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +0000257 else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
258 if (MD->isCopyAssignment())
259 SynthesizeCXXCopyAssignment(MD, FD, Fn, Args);
Fariborz Jahanian6f14c732009-07-30 23:22:00 +0000260
Anders Carlsson468fa632009-04-04 20:47:02 +0000261 // Destroy the 'this' declaration.
262 if (CXXThisDecl)
263 CXXThisDecl->Destroy(getContext());
Chris Lattner5696e7b2008-06-17 18:05:57 +0000264}
265
Chris Lattner5b1964b2008-11-11 07:41:27 +0000266/// ContainsLabel - Return true if the statement contains a label in it. If
267/// this statement is not executed normally, it not containing a label means
268/// that we can just remove the code.
269bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
270 // Null statement, not a label!
271 if (S == 0) return false;
272
273 // If this is a label, we have to emit the code, consider something like:
274 // if (0) { ... foo: bar(); } goto foo;
275 if (isa<LabelStmt>(S))
276 return true;
277
278 // If this is a case/default statement, and we haven't seen a switch, we have
279 // to emit the code.
280 if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
281 return true;
282
283 // If this is a switch statement, we want to ignore cases below it.
284 if (isa<SwitchStmt>(S))
285 IgnoreCaseStmts = true;
286
287 // Scan subexpressions for verboten labels.
288 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
289 I != E; ++I)
290 if (ContainsLabel(*I, IgnoreCaseStmts))
291 return true;
292
293 return false;
294}
295
Chris Lattnercd439292008-11-12 08:04:58 +0000296
297/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
298/// a constant, or if it does but contains a label, return 0. If it constant
299/// folds to 'true' and does not contain a label, return 1, if it constant folds
300/// to 'false' and does not contain a label, return -1.
301int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
Daniel Dunbarf32443c2008-11-12 22:37:10 +0000302 // FIXME: Rename and handle conversion of other evaluatable things
303 // to bool.
Anders Carlsson86286452008-12-01 02:46:24 +0000304 Expr::EvalResult Result;
305 if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
306 Result.HasSideEffects)
Anders Carlsson4046e652008-11-22 22:32:07 +0000307 return 0; // Not foldable, not integer or not fully evaluatable.
Chris Lattnercd439292008-11-12 08:04:58 +0000308
309 if (CodeGenFunction::ContainsLabel(Cond))
310 return 0; // Contains a label.
311
Anders Carlsson86286452008-12-01 02:46:24 +0000312 return Result.Val.getInt().getBoolValue() ? 1 : -1;
Chris Lattnercd439292008-11-12 08:04:58 +0000313}
314
315
316/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
317/// statement) to the specified blocks. Based on the condition, this might try
318/// to simplify the codegen of the conditional based on the branch.
319///
320void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
321 llvm::BasicBlock *TrueBlock,
322 llvm::BasicBlock *FalseBlock) {
323 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
324 return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
325
326 if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
327 // Handle X && Y in a condition.
328 if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
329 // If we have "1 && X", simplify the code. "0 && X" would have constant
330 // folded if the case was simple enough.
331 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
332 // br(1 && X) -> br(X).
333 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
334 }
335
336 // If we have "X && 1", simplify the code to use an uncond branch.
337 // "X && 0" would have been constant folded to 0.
338 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
339 // br(X && 1) -> br(X).
340 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
341 }
342
343 // Emit the LHS as a conditional. If the LHS conditional is false, we
344 // want to jump to the FalseBlock.
Daniel Dunbara612e792008-11-13 01:38:36 +0000345 llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
Chris Lattnercd439292008-11-12 08:04:58 +0000346 EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
347 EmitBlock(LHSTrue);
348
349 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
350 return;
351 } else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
352 // If we have "0 || X", simplify the code. "1 || X" would have constant
353 // folded if the case was simple enough.
354 if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
355 // br(0 || X) -> br(X).
356 return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
357 }
358
359 // If we have "X || 0", simplify the code to use an uncond branch.
360 // "X || 1" would have been constant folded to 1.
361 if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
362 // br(X || 0) -> br(X).
363 return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
364 }
365
366 // Emit the LHS as a conditional. If the LHS conditional is true, we
367 // want to jump to the TrueBlock.
Daniel Dunbara612e792008-11-13 01:38:36 +0000368 llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
Chris Lattnercd439292008-11-12 08:04:58 +0000369 EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
370 EmitBlock(LHSFalse);
371
372 EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
373 return;
374 }
Chris Lattnerd9537732008-11-12 08:13:36 +0000375 }
376
377 if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
378 // br(!x, t, f) -> br(x, f, t)
379 if (CondUOp->getOpcode() == UnaryOperator::LNot)
380 return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
Chris Lattnercd439292008-11-12 08:04:58 +0000381 }
382
Daniel Dunbarbf3c22e2008-11-12 10:30:32 +0000383 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
384 // Handle ?: operator.
385
386 // Just ignore GNU ?: extension.
387 if (CondOp->getLHS()) {
388 // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
389 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
390 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
391 EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
392 EmitBlock(LHSBlock);
393 EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
394 EmitBlock(RHSBlock);
395 EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
396 return;
397 }
398 }
399
Chris Lattnercd439292008-11-12 08:04:58 +0000400 // Emit the code with the fully general case.
401 llvm::Value *CondV = EvaluateExprAsBool(Cond);
402 Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
403}
404
Devang Patele11664a2007-11-01 19:11:01 +0000405/// getCGRecordLayout - Return record layout info.
406const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattner86964a92008-02-05 06:55:31 +0000407 QualType Ty) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000408 const RecordType *RTy = Ty->getAs<RecordType>();
Chris Lattner86964a92008-02-05 06:55:31 +0000409 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patel3e11cce2007-10-23 02:10:49 +0000410
Chris Lattner86964a92008-02-05 06:55:31 +0000411 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patel3e11cce2007-10-23 02:10:49 +0000412}
Chris Lattnerfc944342007-12-02 01:43:38 +0000413
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000414/// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerfc944342007-12-02 01:43:38 +0000415/// specified stmt yet.
Daniel Dunbarf2cf6d12008-09-04 03:43:08 +0000416void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
417 bool OmitOnError) {
418 CGM.ErrorUnsupported(S, Type, OmitOnError);
Chris Lattnerfc944342007-12-02 01:43:38 +0000419}
420
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000421unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
422 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
423 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
424}
425
Chris Lattnerb534f6a2009-04-21 17:59:23 +0000426void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
Owen Anderson41a75022009-08-13 21:57:51 +0000427 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson2e744e82008-08-30 19:51:14 +0000428 if (DestPtr->getType() != BP)
429 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
430
431 // Get size and alignment info for this aggregate.
432 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
433
Chris Lattnerb534f6a2009-04-21 17:59:23 +0000434 // Don't bother emitting a zero-byte memset.
435 if (TypeInfo.first == 0)
436 return;
437
Anders Carlsson2e744e82008-08-30 19:51:14 +0000438 // FIXME: Handle variable sized types.
Owen Anderson41a75022009-08-13 21:57:51 +0000439 const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
440 LLVMPointerWidth);
Anders Carlsson2e744e82008-08-30 19:51:14 +0000441
442 Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
Owen Anderson41a75022009-08-13 21:57:51 +0000443 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
Anders Carlsson2e744e82008-08-30 19:51:14 +0000444 // TypeInfo.first describes size in bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000445 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Owen Anderson41a75022009-08-13 21:57:51 +0000446 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson2e744e82008-08-30 19:51:14 +0000447 TypeInfo.second/8));
448}
449
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000450void CodeGenFunction::EmitIndirectSwitches() {
451 llvm::BasicBlock *Default;
452
Daniel Dunbard27262f2008-08-04 17:24:44 +0000453 if (IndirectSwitches.empty())
454 return;
455
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000456 if (!LabelIDs.empty()) {
457 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
458 } else {
459 // No possible targets for indirect goto, just emit an infinite
460 // loop.
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000461 Default = createBasicBlock("indirectgoto.loop", CurFn);
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000462 llvm::BranchInst::Create(Default, Default);
463 }
464
465 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
466 e = IndirectSwitches.end(); i != e; ++i) {
467 llvm::SwitchInst *I = *i;
468
469 I->setSuccessor(0, Default);
470 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
471 LE = LabelIDs.end(); LI != LE; ++LI) {
Owen Anderson41a75022009-08-13 21:57:51 +0000472 I->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar88402ce2008-08-04 16:51:22 +0000473 LI->second),
474 getBasicBlockForLabel(LI->first));
475 }
476 }
477}
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000478
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000479llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
Eli Friedman04fddf02009-08-15 02:50:32 +0000480 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
Anders Carlssonccbe9202008-12-12 07:19:02 +0000481
Anders Carlssone388a5b2008-12-20 20:27:15 +0000482 assert(SizeEntry && "Did not emit size for type");
483 return SizeEntry;
484}
Anders Carlssonccbe9202008-12-12 07:19:02 +0000485
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000486llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
Anders Carlsson8a01b792008-12-20 20:46:34 +0000487 assert(Ty->isVariablyModifiedType() &&
488 "Must pass variably modified type to EmitVLASizes!");
Anders Carlssone388a5b2008-12-20 20:27:15 +0000489
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000490 EnsureInsertPoint();
491
Anders Carlsson8a01b792008-12-20 20:46:34 +0000492 if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
Eli Friedman04fddf02009-08-15 02:50:32 +0000493 llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
Anders Carlsson8a01b792008-12-20 20:46:34 +0000494
Anders Carlsson5d985f52008-12-20 21:51:53 +0000495 if (!SizeEntry) {
Anders Carlsson31f86492009-02-05 19:43:10 +0000496 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
497
Chris Lattner8dc76262009-08-15 00:03:43 +0000498 // Get the element size;
499 QualType ElemTy = VAT->getElementType();
500 llvm::Value *ElemSize;
Anders Carlsson5d985f52008-12-20 21:51:53 +0000501 if (ElemTy->isVariableArrayType())
502 ElemSize = EmitVLASize(ElemTy);
Chris Lattner8dc76262009-08-15 00:03:43 +0000503 else
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000504 ElemSize = llvm::ConstantInt::get(SizeTy,
Anders Carlsson5d985f52008-12-20 21:51:53 +0000505 getContext().getTypeSize(ElemTy) / 8);
Anders Carlsson8a01b792008-12-20 20:46:34 +0000506
Anders Carlsson5d985f52008-12-20 21:51:53 +0000507 llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
Anders Carlsson31f86492009-02-05 19:43:10 +0000508 NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
509
Anders Carlsson5d985f52008-12-20 21:51:53 +0000510 SizeEntry = Builder.CreateMul(ElemSize, NumElements);
Anders Carlsson8a01b792008-12-20 20:46:34 +0000511 }
512
Anders Carlsson8a01b792008-12-20 20:46:34 +0000513 return SizeEntry;
Anders Carlssonccbe9202008-12-12 07:19:02 +0000514 }
Anders Carlsson8a01b792008-12-20 20:46:34 +0000515
Chris Lattner8dc76262009-08-15 00:03:43 +0000516 if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
517 EmitVLASize(AT->getElementType());
518 return 0;
519 }
520
521 const PointerType *PT = Ty->getAs<PointerType>();
522 assert(PT && "unknown VM type!");
523 EmitVLASize(PT->getPointeeType());
Anders Carlsson8a01b792008-12-20 20:46:34 +0000524 return 0;
Anders Carlssonccbe9202008-12-12 07:19:02 +0000525}
Eli Friedmanddea0ad2009-01-20 17:46:04 +0000526
527llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
528 if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
529 return EmitScalarExpr(E);
530 }
531 return EmitLValue(E).getAddress();
532}
Anders Carlsson15cb75a2009-02-07 22:53:43 +0000533
Anders Carlssona586ad72009-02-08 03:22:36 +0000534void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
Anders Carlsson15cb75a2009-02-07 22:53:43 +0000535{
Anders Carlsson15cb75a2009-02-07 22:53:43 +0000536 CleanupEntries.push_back(CleanupEntry(CleanupBlock));
Anders Carlsson15cb75a2009-02-07 22:53:43 +0000537}
Anders Carlssonbe0f76a2009-02-07 23:50:39 +0000538
539void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
540{
541 assert(CleanupEntries.size() >= OldCleanupStackSize &&
542 "Cleanup stack mismatch!");
543
544 while (CleanupEntries.size() > OldCleanupStackSize)
545 EmitCleanupBlock();
546}
547
Anders Carlsson66c384a2009-02-08 07:46:24 +0000548CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
Anders Carlssonbe0f76a2009-02-07 23:50:39 +0000549{
Anders Carlsson66c384a2009-02-08 07:46:24 +0000550 CleanupEntry &CE = CleanupEntries.back();
551
552 llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
553
554 std::vector<llvm::BasicBlock *> Blocks;
555 std::swap(Blocks, CE.Blocks);
556
557 std::vector<llvm::BranchInst *> BranchFixups;
558 std::swap(BranchFixups, CE.BranchFixups);
559
560 CleanupEntries.pop_back();
561
Anders Carlssonf57b9ee2009-02-08 22:45:15 +0000562 // Check if any branch fixups pointed to the scope we just popped. If so,
563 // we can remove them.
564 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
565 llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
566 BlockScopeMap::iterator I = BlockScopes.find(Dest);
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000567
Anders Carlssonf57b9ee2009-02-08 22:45:15 +0000568 if (I == BlockScopes.end())
569 continue;
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000570
Anders Carlssonf57b9ee2009-02-08 22:45:15 +0000571 assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000572
Anders Carlssonf57b9ee2009-02-08 22:45:15 +0000573 if (I->second == CleanupEntries.size()) {
574 // We don't need to do this branch fixup.
575 BranchFixups[i] = BranchFixups.back();
576 BranchFixups.pop_back();
577 i--;
578 e--;
579 continue;
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000580 }
581 }
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000582
Anders Carlsson66c384a2009-02-08 07:46:24 +0000583 llvm::BasicBlock *SwitchBlock = 0;
584 llvm::BasicBlock *EndBlock = 0;
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000585 if (!BranchFixups.empty()) {
Anders Carlsson66c384a2009-02-08 07:46:24 +0000586 SwitchBlock = createBasicBlock("cleanup.switch");
587 EndBlock = createBasicBlock("cleanup.end");
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000588
Anders Carlsson66c384a2009-02-08 07:46:24 +0000589 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
Anders Carlssone73e3ec2009-03-17 05:53:35 +0000590
Anders Carlsson66c384a2009-02-08 07:46:24 +0000591 Builder.SetInsertPoint(SwitchBlock);
592
Owen Anderson41a75022009-08-13 21:57:51 +0000593 llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000594 "cleanup.dst");
595 llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
596
597 // Create a switch instruction to determine where to jump next.
Anders Carlsson66c384a2009-02-08 07:46:24 +0000598 llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000599 BranchFixups.size());
Anders Carlsson66c384a2009-02-08 07:46:24 +0000600
Anders Carlsson76180ea2009-02-08 22:13:37 +0000601 // Restore the current basic block (if any)
Anders Carlssone73e3ec2009-03-17 05:53:35 +0000602 if (CurBB) {
Anders Carlsson76180ea2009-02-08 22:13:37 +0000603 Builder.SetInsertPoint(CurBB);
Anders Carlssone73e3ec2009-03-17 05:53:35 +0000604
605 // If we had a current basic block, we also need to emit an instruction
606 // to initialize the cleanup destination.
Owen Anderson41a75022009-08-13 21:57:51 +0000607 Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
Anders Carlssone73e3ec2009-03-17 05:53:35 +0000608 DestCodePtr);
609 } else
Anders Carlsson76180ea2009-02-08 22:13:37 +0000610 Builder.ClearInsertionPoint();
Anders Carlsson66c384a2009-02-08 07:46:24 +0000611
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000612 for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
613 llvm::BranchInst *BI = BranchFixups[i];
614 llvm::BasicBlock *Dest = BI->getSuccessor(0);
615
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000616 // Fixup the branch instruction to point to the cleanup block.
617 BI->setSuccessor(0, CleanupBlock);
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000618
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000619 if (CleanupEntries.empty()) {
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000620 llvm::ConstantInt *ID;
621
622 // Check if we already have a destination for this block.
623 if (Dest == SI->getDefaultDest())
Owen Anderson41a75022009-08-13 21:57:51 +0000624 ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000625 else {
626 ID = SI->findCaseDest(Dest);
627 if (!ID) {
628 // No code found, get a new unique one by using the number of
629 // switch successors.
Owen Anderson41a75022009-08-13 21:57:51 +0000630 ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000631 SI->getNumSuccessors());
632 SI->addCase(ID, Dest);
633 }
634 }
635
636 // Store the jump destination before the branch instruction.
637 new llvm::StoreInst(ID, DestCodePtr, BI);
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000638 } else {
639 // We need to jump through another cleanup block. Create a pad block
640 // with a branch instruction that jumps to the final destination and
641 // add it as a branch fixup to the current cleanup scope.
642
643 // Create the pad block.
644 llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000645
646 // Create a unique case ID.
Owen Anderson41a75022009-08-13 21:57:51 +0000647 llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000648 SI->getNumSuccessors());
649
650 // Store the jump destination before the branch instruction.
651 new llvm::StoreInst(ID, DestCodePtr, BI);
652
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000653 // Add it as the destination.
Anders Carlsson9c964ac2009-02-08 22:46:50 +0000654 SI->addCase(ID, CleanupPad);
Anders Carlsson3c21dd52009-02-08 01:23:05 +0000655
656 // Create the branch to the final destination.
657 llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
658 CleanupPad->getInstList().push_back(BI);
659
660 // And add it as a branch fixup.
661 CleanupEntries.back().BranchFixups.push_back(BI);
662 }
663 }
664 }
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000665
Anders Carlssonfbfb5e62009-02-08 00:16:35 +0000666 // Remove all blocks from the block scope map.
667 for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
668 assert(BlockScopes.count(Blocks[i]) &&
669 "Did not find block in scope map!");
670
671 BlockScopes.erase(Blocks[i]);
672 }
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000673
Anders Carlsson66c384a2009-02-08 07:46:24 +0000674 return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000675}
676
677void CodeGenFunction::EmitCleanupBlock()
678{
Anders Carlsson66c384a2009-02-08 07:46:24 +0000679 CleanupBlockInfo Info = PopCleanupBlock();
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000680
Anders Carlssonf3f91ce2009-05-31 00:33:20 +0000681 llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
682 if (CurBB && !CurBB->getTerminator() &&
683 Info.CleanupBlock->getNumUses() == 0) {
684 CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
685 delete Info.CleanupBlock;
686 } else
687 EmitBlock(Info.CleanupBlock);
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000688
Anders Carlsson66c384a2009-02-08 07:46:24 +0000689 if (Info.SwitchBlock)
690 EmitBlock(Info.SwitchBlock);
691 if (Info.EndBlock)
692 EmitBlock(Info.EndBlock);
Anders Carlssonae91d9b2009-02-08 03:55:35 +0000693}
694
Anders Carlsson7d70fd22009-02-08 00:50:42 +0000695void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
696{
697 assert(!CleanupEntries.empty() &&
698 "Trying to add branch fixup without cleanup block!");
699
Mike Stump18bb9282009-05-16 07:57:57 +0000700 // FIXME: We could be more clever here and check if there's already a branch
701 // fixup for this destination and recycle it.
Anders Carlsson7d70fd22009-02-08 00:50:42 +0000702 CleanupEntries.back().BranchFixups.push_back(BI);
703}
704
705void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
706{
Anders Carlsson76180ea2009-02-08 22:13:37 +0000707 if (!HaveInsertPoint())
708 return;
709
Anders Carlsson7d70fd22009-02-08 00:50:42 +0000710 llvm::BranchInst* BI = Builder.CreateBr(Dest);
711
Anders Carlsson76180ea2009-02-08 22:13:37 +0000712 Builder.ClearInsertionPoint();
713
Anders Carlsson7d70fd22009-02-08 00:50:42 +0000714 // The stack is empty, no need to do any cleanup.
715 if (CleanupEntries.empty())
716 return;
717
718 if (!Dest->getParent()) {
719 // We are trying to branch to a block that hasn't been inserted yet.
720 AddBranchFixup(BI);
721 return;
722 }
723
724 BlockScopeMap::iterator I = BlockScopes.find(Dest);
725 if (I == BlockScopes.end()) {
726 // We are trying to jump to a block that is outside of any cleanup scope.
727 AddBranchFixup(BI);
728 return;
729 }
730
731 assert(I->second < CleanupEntries.size() &&
732 "Trying to branch into cleanup region");
733
734 if (I->second == CleanupEntries.size() - 1) {
735 // We have a branch to a block in the same scope.
736 return;
737 }
738
739 AddBranchFixup(BI);
740}