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