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