Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 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 Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 27 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 28 | Target(CGM.getContext().Target), |
Owen Anderson | aac8705 | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 29 | Builder(cgm.getModule().getContext()), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 31 | CXXThisDecl(0) { |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 32 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 33 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 34 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | |
| 36 | ASTContext &CodeGenFunction::getContext() const { |
| 37 | return CGM.getContext(); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 42 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 43 | if (BB) return BB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | // Create, but don't insert, the new block. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 46 | return BB = createBasicBlock(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 49 | llvm::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 Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 53 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 55 | llvm::Constant * |
| 56 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 57 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 60 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 61 | return CGM.getTypes().ConvertTypeForMem(T); |
| 62 | } |
| 63 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 65 | return CGM.getTypes().ConvertType(T); |
| 66 | } |
| 67 | |
| 68 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 69 | // FIXME: Use positive checks instead of negative ones to be more robust in |
| 70 | // the face of extension. |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 71 | return !T->hasPointerRepresentation() && !T->isRealType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() && |
Anders Carlsson | 237957c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 73 | !T->isBlockPointerType() && !T->isMemberPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 76 | void 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 Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 84 | // 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 Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 88 | delete ReturnBlock; |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 89 | } else |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 90 | 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()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | llvm::BranchInst *BI = |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 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 Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 109 | // 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 Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 112 | |
| 113 | EmitBlock(ReturnBlock); |
| 114 | } |
| 115 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 116 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 117 | // Finish emission of indirect switches. |
| 118 | EmitIndirectSwitches(); |
| 119 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 120 | assert(BreakContinueStack.empty() && |
| 121 | "mismatched push/pop in break/continue stack!"); |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 122 | assert(BlockScopes.empty() && |
| 123 | "did not remove all blocks from block scope map!"); |
| 124 | assert(CleanupEntries.empty() && |
| 125 | "mismatched push/pop in cleanup stack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
| 127 | // Emit function epilog (to return). |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 128 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 129 | |
| 130 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 131 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 132 | DI->setLocation(EndLoc); |
| 133 | DI->EmitRegionEnd(CurFn, Builder); |
| 134 | } |
| 135 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 136 | EmitFunctionEpilog(*CurFnInfo, ReturnValue); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 138 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 139 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 140 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 141 | Ptr->eraseFromParent(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 144 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 145 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 146 | const FunctionArgList &Args, |
| 147 | SourceLocation StartLoc) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 148 | const Decl *D = GD.getDecl(); |
| 149 | |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 150 | DidCallStackSave = false; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 151 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 152 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 153 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 154 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 155 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 156 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 158 | // Create a marker to make it easy to insert allocas into the entryblock |
| 159 | // later. Don't create this with the builder, because we don't want it |
| 160 | // folded. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 161 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)); |
| 162 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::getInt32Ty(VMContext), "", |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 163 | EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 164 | if (Builder.isNamePreserving()) |
| 165 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 167 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 168 | ReturnValue = 0; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 169 | if (!RetTy->isVoidType()) |
| 170 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 172 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 174 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 175 | // FIXME: The cast here is a huge hack. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 176 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 177 | DI->setLocation(StartLoc); |
Anders Carlsson | 1860a31 | 2009-09-11 00:11:35 +0000 | [diff] [blame] | 178 | if (isa<FunctionDecl>(D)) { |
| 179 | DI->EmitFunctionStart(CGM.getMangledName(GD), RetTy, CurFn, Builder); |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 180 | } else { |
| 181 | // Just use LLVM function name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 42719fc | 2009-07-23 05:30:36 +0000 | [diff] [blame] | 183 | // FIXME: Remove unnecessary conversion to std::string when API settles. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | DI->EmitFunctionStart(std::string(Fn->getName()).c_str(), |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 185 | RetTy, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 186 | } |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 189 | // FIXME: Leaked. |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 190 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 191 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 193 | // If any of the arguments have a variably modified type, make sure to |
| 194 | // emit the type size. |
| 195 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 196 | i != e; ++i) { |
| 197 | QualType Ty = i->second; |
| 198 | |
| 199 | if (Ty->isVariablyModifiedType()) |
| 200 | EmitVLASize(Ty); |
| 201 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 202 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 204 | void CodeGenFunction::GenerateCode(GlobalDecl GD, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 205 | llvm::Function *Fn) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 206 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 207 | |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 208 | // Check if we should generate debug info for this function. |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 209 | if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 210 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 212 | FunctionArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 214 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 215 | if (MD->isInstance()) { |
| 216 | // Create the implicit 'this' decl. |
| 217 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 218 | // generation. Maybe we can come up with a better way? |
| 219 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | &getContext().Idents.get("this"), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 221 | MD->getThisType(getContext())); |
| 222 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
| 223 | } |
| 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 226 | if (FD->getNumParams()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 227 | const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 228 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 229 | |
| 230 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 232 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 234 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 235 | // FIXME: Support CXXTryStmt here, too. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 236 | if (const CompoundStmt *S = FD->getCompoundBody()) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 237 | StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc()); |
Fariborz Jahanian | ab3c0a2 | 2009-07-20 22:35:22 +0000 | [diff] [blame] | 238 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 239 | EmitCtorPrologue(CD); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 240 | EmitStmt(S); |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 241 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
| 242 | EmitDtorEpilogue(DD); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 243 | FinishFunction(S->getRBracLoc()); |
| 244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | else |
Fariborz Jahanian | c7ff8e1 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 246 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | const CXXRecordDecl *ClassDecl = |
Fariborz Jahanian | 9889652 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 248 | cast<CXXRecordDecl>(CD->getDeclContext()); |
| 249 | (void) ClassDecl; |
| 250 | if (CD->isCopyConstructor(getContext())) { |
| 251 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 252 | "bogus constructor is being synthesize"); |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 253 | SynthesizeCXXCopyConstructor(GD, FD, Fn, Args); |
Fariborz Jahanian | 9889652 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 254 | } |
| 255 | else { |
| 256 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 257 | "bogus constructor is being synthesize"); |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 258 | SynthesizeDefaultConstructor(GD, FD, Fn, Args); |
Fariborz Jahanian | 9889652 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 259 | } |
Fariborz Jahanian | c7ff8e1 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 260 | } |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 261 | else if (isa<CXXDestructorDecl>(FD)) |
| 262 | SynthesizeDefaultDestructor(GD, FD, Fn, Args); |
| 263 | else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 264 | if (MD->isCopyAssignment()) |
| 265 | SynthesizeCXXCopyAssignment(MD, FD, Fn, Args); |
| 266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 268 | // Destroy the 'this' declaration. |
| 269 | if (CXXThisDecl) |
| 270 | CXXThisDecl->Destroy(getContext()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 273 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 274 | /// this statement is not executed normally, it not containing a label means |
| 275 | /// that we can just remove the code. |
| 276 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 277 | // Null statement, not a label! |
| 278 | if (S == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 280 | // If this is a label, we have to emit the code, consider something like: |
| 281 | // if (0) { ... foo: bar(); } goto foo; |
| 282 | if (isa<LabelStmt>(S)) |
| 283 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 285 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 286 | // to emit the code. |
| 287 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 288 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 290 | // If this is a switch statement, we want to ignore cases below it. |
| 291 | if (isa<SwitchStmt>(S)) |
| 292 | IgnoreCaseStmts = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 294 | // Scan subexpressions for verboten labels. |
| 295 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 296 | I != E; ++I) |
| 297 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 298 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 303 | |
| 304 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 305 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 306 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 307 | /// to 'false' and does not contain a label, return -1. |
| 308 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 309 | // FIXME: Rename and handle conversion of other evaluatable things |
| 310 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 311 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 313 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 314 | return 0; // Not foldable, not integer or not fully evaluatable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 316 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 317 | return 0; // Contains a label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 319 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 324 | /// statement) to the specified blocks. Based on the condition, this might try |
| 325 | /// to simplify the codegen of the conditional based on the branch. |
| 326 | /// |
| 327 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 328 | llvm::BasicBlock *TrueBlock, |
| 329 | llvm::BasicBlock *FalseBlock) { |
| 330 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 331 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 333 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 334 | // Handle X && Y in a condition. |
| 335 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 336 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 337 | // folded if the case was simple enough. |
| 338 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 339 | // br(1 && X) -> br(X). |
| 340 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 343 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 344 | // "X && 0" would have been constant folded to 0. |
| 345 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 346 | // br(X && 1) -> br(X). |
| 347 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 350 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 351 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 352 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 353 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 354 | EmitBlock(LHSTrue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 356 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 357 | return; |
| 358 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 359 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 360 | // folded if the case was simple enough. |
| 361 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 362 | // br(0 || X) -> br(X). |
| 363 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 366 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 367 | // "X || 1" would have been constant folded to 1. |
| 368 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 369 | // br(X || 0) -> br(X). |
| 370 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 373 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 374 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 375 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 376 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 377 | EmitBlock(LHSFalse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 379 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 380 | return; |
| 381 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 382 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 384 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 385 | // br(!x, t, f) -> br(x, f, t) |
| 386 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 387 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 390 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 391 | // Handle ?: operator. |
| 392 | |
| 393 | // Just ignore GNU ?: extension. |
| 394 | if (CondOp->getLHS()) { |
| 395 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 396 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 397 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 398 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 399 | EmitBlock(LHSBlock); |
| 400 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 401 | EmitBlock(RHSBlock); |
| 402 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 407 | // Emit the code with the fully general case. |
| 408 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 409 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 410 | } |
| 411 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 412 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 413 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 414 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 415 | bool OmitOnError) { |
| 416 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 419 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 420 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 421 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 422 | } |
| 423 | |
Chris Lattner | 88207c9 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 424 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 425 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 426 | if (DestPtr->getType() != BP) |
| 427 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 428 | |
| 429 | // Get size and alignment info for this aggregate. |
| 430 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 431 | |
Chris Lattner | 88207c9 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 432 | // Don't bother emitting a zero-byte memset. |
| 433 | if (TypeInfo.first == 0) |
| 434 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 436 | // FIXME: Handle variable sized types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 438 | LLVMPointerWidth); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 439 | |
| 440 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 441 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 442 | // TypeInfo.first describes size in bits. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 443 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 445 | TypeInfo.second/8)); |
| 446 | } |
| 447 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 448 | void CodeGenFunction::EmitIndirectSwitches() { |
| 449 | llvm::BasicBlock *Default; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Daniel Dunbar | 76526a5 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 451 | if (IndirectSwitches.empty()) |
| 452 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 454 | if (!LabelIDs.empty()) { |
| 455 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 456 | } else { |
| 457 | // No possible targets for indirect goto, just emit an infinite |
| 458 | // loop. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 459 | Default = createBasicBlock("indirectgoto.loop", CurFn); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 460 | llvm::BranchInst::Create(Default, Default); |
| 461 | } |
| 462 | |
| 463 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 464 | e = IndirectSwitches.end(); i != e; ++i) { |
| 465 | llvm::SwitchInst *I = *i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 467 | I->setSuccessor(0, Default); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 469 | LE = LabelIDs.end(); LI != LE; ++LI) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 470 | I->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | LI->second), |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 472 | getBasicBlockForLabel(LI->first)); |
| 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | } |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 475 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 476 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 477 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 478 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 480 | assert(SizeEntry && "Did not emit size for type"); |
| 481 | return SizeEntry; |
| 482 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 484 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 485 | assert(Ty->isVariablyModifiedType() && |
| 486 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 488 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 490 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 491 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 493 | if (!SizeEntry) { |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 494 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 496 | // Get the element size; |
| 497 | QualType ElemTy = VAT->getElementType(); |
| 498 | llvm::Value *ElemSize; |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 499 | if (ElemTy->isVariableArrayType()) |
| 500 | ElemSize = EmitVLASize(ElemTy); |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 501 | else |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 502 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 503 | getContext().getTypeSize(ElemTy) / 8); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 505 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 506 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 508 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 511 | return SizeEntry; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 514 | if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 515 | EmitVLASize(AT->getElementType()); |
| 516 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 519 | const PointerType *PT = Ty->getAs<PointerType>(); |
| 520 | assert(PT && "unknown VM type!"); |
| 521 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 522 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 523 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 524 | |
| 525 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 526 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { |
| 527 | return EmitScalarExpr(E); |
| 528 | } |
| 529 | return EmitLValue(E).getAddress(); |
| 530 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 531 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock) { |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 533 | CleanupEntries.push_back(CleanupEntry(CleanupBlock)); |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 534 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) { |
| 537 | assert(CleanupEntries.size() >= OldCleanupStackSize && |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 538 | "Cleanup stack mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 540 | while (CleanupEntries.size() > OldCleanupStackSize) |
| 541 | EmitCleanupBlock(); |
| 542 | } |
| 543 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 545 | CleanupEntry &CE = CleanupEntries.back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 547 | llvm::BasicBlock *CleanupBlock = CE.CleanupBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 549 | std::vector<llvm::BasicBlock *> Blocks; |
| 550 | std::swap(Blocks, CE.Blocks); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 552 | std::vector<llvm::BranchInst *> BranchFixups; |
| 553 | std::swap(BranchFixups, CE.BranchFixups); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 555 | CleanupEntries.pop_back(); |
| 556 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 557 | // Check if any branch fixups pointed to the scope we just popped. If so, |
| 558 | // we can remove them. |
| 559 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 560 | llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); |
| 561 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 563 | if (I == BlockScopes.end()) |
| 564 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 566 | assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 568 | if (I->second == CleanupEntries.size()) { |
| 569 | // We don't need to do this branch fixup. |
| 570 | BranchFixups[i] = BranchFixups.back(); |
| 571 | BranchFixups.pop_back(); |
| 572 | i--; |
| 573 | e--; |
| 574 | continue; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 578 | llvm::BasicBlock *SwitchBlock = 0; |
| 579 | llvm::BasicBlock *EndBlock = 0; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 580 | if (!BranchFixups.empty()) { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 581 | SwitchBlock = createBasicBlock("cleanup.switch"); |
| 582 | EndBlock = createBasicBlock("cleanup.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 584 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 586 | Builder.SetInsertPoint(SwitchBlock); |
| 587 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 589 | "cleanup.dst"); |
| 590 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 592 | // Create a switch instruction to determine where to jump next. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 594 | BranchFixups.size()); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 595 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 596 | // Restore the current basic block (if any) |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 597 | if (CurBB) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 598 | Builder.SetInsertPoint(CurBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 600 | // If we had a current basic block, we also need to emit an instruction |
| 601 | // to initialize the cleanup destination. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 602 | Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)), |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 603 | DestCodePtr); |
| 604 | } else |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 605 | Builder.ClearInsertionPoint(); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 606 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 607 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 608 | llvm::BranchInst *BI = BranchFixups[i]; |
| 609 | llvm::BasicBlock *Dest = BI->getSuccessor(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 611 | // Fixup the branch instruction to point to the cleanup block. |
| 612 | BI->setSuccessor(0, CleanupBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 614 | if (CleanupEntries.empty()) { |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 615 | llvm::ConstantInt *ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 617 | // Check if we already have a destination for this block. |
| 618 | if (Dest == SI->getDefaultDest()) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 619 | ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 620 | else { |
| 621 | ID = SI->findCaseDest(Dest); |
| 622 | if (!ID) { |
| 623 | // No code found, get a new unique one by using the number of |
| 624 | // switch successors. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 626 | SI->getNumSuccessors()); |
| 627 | SI->addCase(ID, Dest); |
| 628 | } |
| 629 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 631 | // Store the jump destination before the branch instruction. |
| 632 | new llvm::StoreInst(ID, DestCodePtr, BI); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 633 | } else { |
| 634 | // We need to jump through another cleanup block. Create a pad block |
| 635 | // with a branch instruction that jumps to the final destination and |
| 636 | // add it as a branch fixup to the current cleanup scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 638 | // Create the pad block. |
| 639 | llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 640 | |
| 641 | // Create a unique case ID. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 643 | SI->getNumSuccessors()); |
| 644 | |
| 645 | // Store the jump destination before the branch instruction. |
| 646 | new llvm::StoreInst(ID, DestCodePtr, BI); |
| 647 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 648 | // Add it as the destination. |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 649 | SI->addCase(ID, CleanupPad); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 651 | // Create the branch to the final destination. |
| 652 | llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); |
| 653 | CleanupPad->getInstList().push_back(BI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 655 | // And add it as a branch fixup. |
| 656 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 657 | } |
| 658 | } |
| 659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 661 | // Remove all blocks from the block scope map. |
| 662 | for (size_t i = 0, e = Blocks.size(); i != e; ++i) { |
| 663 | assert(BlockScopes.count(Blocks[i]) && |
| 664 | "Did not find block in scope map!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 666 | BlockScopes.erase(Blocks[i]); |
| 667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 669 | return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | void CodeGenFunction::EmitCleanupBlock() { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 673 | CleanupBlockInfo Info = PopCleanupBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 675 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | if (CurBB && !CurBB->getTerminator() && |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 677 | Info.CleanupBlock->getNumUses() == 0) { |
| 678 | CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); |
| 679 | delete Info.CleanupBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | } else |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 681 | EmitBlock(Info.CleanupBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 683 | if (Info.SwitchBlock) |
| 684 | EmitBlock(Info.SwitchBlock); |
| 685 | if (Info.EndBlock) |
| 686 | EmitBlock(Info.EndBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) { |
| 690 | assert(!CleanupEntries.empty() && |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 691 | "Trying to add branch fixup without cleanup block!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 693 | // FIXME: We could be more clever here and check if there's already a branch |
| 694 | // fixup for this destination and recycle it. |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 695 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 696 | } |
| 697 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 699 | if (!HaveInsertPoint()) |
| 700 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 702 | llvm::BranchInst* BI = Builder.CreateBr(Dest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 704 | Builder.ClearInsertionPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 706 | // The stack is empty, no need to do any cleanup. |
| 707 | if (CleanupEntries.empty()) |
| 708 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 710 | if (!Dest->getParent()) { |
| 711 | // We are trying to branch to a block that hasn't been inserted yet. |
| 712 | AddBranchFixup(BI); |
| 713 | return; |
| 714 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 716 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
| 717 | if (I == BlockScopes.end()) { |
| 718 | // We are trying to jump to a block that is outside of any cleanup scope. |
| 719 | AddBranchFixup(BI); |
| 720 | return; |
| 721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 723 | assert(I->second < CleanupEntries.size() && |
| 724 | "Trying to branch into cleanup region"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 726 | if (I->second == CleanupEntries.size() - 1) { |
| 727 | // We have a branch to a block in the same scope. |
| 728 | return; |
| 729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 731 | AddBranchFixup(BI); |
| 732 | } |