Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 9bc7c8d | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | eee5cd1 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | aa3afc7 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | ecd7942 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 27 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 28 | Target(CGM.getContext().Target), |
Owen Anderson | 215780a | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 29 | Builder(cgm.getModule().getContext()), |
Anders Carlsson | aa3afc7 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 30 | DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
| 31 | CXXThisDecl(0) { |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 32 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 33 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 34 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 46 | return BB = createBasicBlock(S->getName()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | dea5921 | 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 | 934fb02 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 53 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | dea5921 | 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 | 75d8673 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | 706059f | 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 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | ba2cb0e | 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 | fc096bf | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 71 | return !T->hasPointerRepresentation() &&!T->isRealType() && |
| 72 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() && |
Daniel Dunbar | 9689124 | 2009-01-09 02:44:18 +0000 | [diff] [blame] | 73 | !T->isBlockPointerType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 924f4ea | 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 | ac8dae2 | 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 | 924f4ea | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 88 | delete ReturnBlock; |
Daniel Dunbar | ac8dae2 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 89 | } else |
Daniel Dunbar | 924f4ea | 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 | ba2cb0e | 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 | 924f4ea | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 112 | |
| 113 | EmitBlock(ReturnBlock); |
| 114 | } |
| 115 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 116 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 117 | // Finish emission of indirect switches. |
| 118 | EmitIndirectSwitches(); |
| 119 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 120 | assert(BreakContinueStack.empty() && |
| 121 | "mismatched push/pop in break/continue stack!"); |
Anders Carlsson | e7de352 | 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 | 924f4ea | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 127 | // Emit function epilog (to return). |
| 128 | EmitReturnBlock(); |
Daniel Dunbar | 03f7ae1 | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 129 | |
| 130 | // Emit debug descriptor for function end. |
Anders Carlsson | 7300779 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 131 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 03f7ae1 | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 132 | DI->setLocation(EndLoc); |
| 133 | DI->EmitRegionEnd(CurFn, Builder); |
| 134 | } |
| 135 | |
Daniel Dunbar | 6ee022b | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 136 | EmitFunctionEpilog(*CurFnInfo, ReturnValue); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 137 | |
Chris Lattner | ca92981 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 138 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | fea1a66 | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 139 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | ca92981 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 140 | AllocaInsertPt = 0; |
Chris Lattner | fea1a66 | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 141 | Ptr->eraseFromParent(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 144 | void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, |
| 145 | llvm::Function *Fn, |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 146 | const FunctionArgList &Args, |
| 147 | SourceLocation StartLoc) { |
Anders Carlsson | 60c4c40 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 148 | DidCallStackSave = false; |
Chris Lattner | f6279ae | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 149 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 150 | FnRetTy = RetTy; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 151 | CurFn = Fn; |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 152 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 153 | |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 154 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 8c7c6a1 | 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 | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 159 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
Chris Lattner | 7fbb70d | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 160 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "", |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 161 | EntryBB); |
Chris Lattner | 7fbb70d | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 162 | if (Builder.isNamePreserving()) |
| 163 | AllocaInsertPt->setName("allocapt"); |
| 164 | |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 165 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 166 | ReturnValue = 0; |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 167 | if (!RetTy->isVoidType()) |
| 168 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Daniel Dunbar | 9fb751f | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 170 | Builder.SetInsertPoint(EntryBB); |
| 171 | |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 172 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 173 | // FIXME: The cast here is a huge hack. |
Anders Carlsson | 7300779 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 174 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 175 | DI->setLocation(StartLoc); |
| 176 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3c3c454 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 177 | DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder); |
Daniel Dunbar | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 178 | } else { |
| 179 | // Just use LLVM function name. |
Daniel Dunbar | 6bf47dc | 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 | 54968bf | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 183 | RetTy, CurFn, Builder); |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 184 | } |
Sanjiv Gupta | 1d340eb | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Daniel Dunbar | 6ee022b | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 187 | // FIXME: Leaked. |
Daniel Dunbar | 34bda88 | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 188 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); |
Daniel Dunbar | 6ee022b | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 189 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Anders Carlsson | 21cecd4 | 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 | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 200 | } |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 202 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 203 | llvm::Function *Fn) { |
Anders Carlsson | 7300779 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 204 | // Check if we should generate debug info for this function. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 205 | if (CGM.getDebugInfo() && !FD->hasAttr<NodebugAttr>()) |
Anders Carlsson | 7300779 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 206 | DebugInfo = CGM.getDebugInfo(); |
| 207 | |
Daniel Dunbar | 9681683 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 208 | FunctionArgList Args; |
Anders Carlsson | aa3afc7 | 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 | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 222 | if (FD->getNumParams()) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 223 | const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType(); |
Eli Friedman | 769e730 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 224 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 9681683 | 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 | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 229 | } |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 230 | |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 231 | // FIXME: Support CXXTryStmt here, too. |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 232 | if (const CompoundStmt *S = FD->getCompoundBody()) { |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 233 | StartFunction(FD, FD->getResultType(), Fn, Args, S->getLBracLoc()); |
Fariborz Jahanian | 77cf9fa | 2009-07-20 22:35:22 +0000 | [diff] [blame] | 234 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 235 | EmitCtorPrologue(CD); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 236 | EmitStmt(S); |
Fariborz Jahanian | 36a0ec0 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 237 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
| 238 | EmitDtorEpilogue(DD); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 239 | FinishFunction(S->getRBracLoc()); |
| 240 | } |
Fariborz Jahanian | 35d1364 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 241 | else |
| 242 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Fariborz Jahanian | fb658a2 | 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"); |
Fariborz Jahanian | ab840aa | 2009-08-08 19:31:03 +0000 | [diff] [blame^] | 249 | SynthesizeCXXCopyConstructor(CD, FD, Fn, Args); |
Fariborz Jahanian | fb658a2 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 250 | } |
| 251 | else { |
| 252 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 253 | "bogus constructor is being synthesize"); |
| 254 | StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); |
| 255 | EmitCtorPrologue(CD); |
| 256 | FinishFunction(); |
| 257 | } |
Fariborz Jahanian | 35d1364 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Anders Carlsson | aa3afc7 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 260 | // Destroy the 'this' declaration. |
| 261 | if (CXXThisDecl) |
| 262 | CXXThisDecl->Destroy(getContext()); |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 265 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 266 | /// this statement is not executed normally, it not containing a label means |
| 267 | /// that we can just remove the code. |
| 268 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 269 | // Null statement, not a label! |
| 270 | if (S == 0) return false; |
| 271 | |
| 272 | // If this is a label, we have to emit the code, consider something like: |
| 273 | // if (0) { ... foo: bar(); } goto foo; |
| 274 | if (isa<LabelStmt>(S)) |
| 275 | return true; |
| 276 | |
| 277 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 278 | // to emit the code. |
| 279 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 280 | return true; |
| 281 | |
| 282 | // If this is a switch statement, we want to ignore cases below it. |
| 283 | if (isa<SwitchStmt>(S)) |
| 284 | IgnoreCaseStmts = true; |
| 285 | |
| 286 | // Scan subexpressions for verboten labels. |
| 287 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 288 | I != E; ++I) |
| 289 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 290 | return true; |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 295 | |
| 296 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 297 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 298 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 299 | /// to 'false' and does not contain a label, return -1. |
| 300 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 1493702 | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 301 | // FIXME: Rename and handle conversion of other evaluatable things |
| 302 | // to bool. |
Anders Carlsson | 90d85f3 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 303 | Expr::EvalResult Result; |
| 304 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
| 305 | Result.HasSideEffects) |
Anders Carlsson | ea3c6ab | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 306 | return 0; // Not foldable, not integer or not fully evaluatable. |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 307 | |
| 308 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 309 | return 0; // Contains a label. |
| 310 | |
Anders Carlsson | 90d85f3 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 311 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 316 | /// statement) to the specified blocks. Based on the condition, this might try |
| 317 | /// to simplify the codegen of the conditional based on the branch. |
| 318 | /// |
| 319 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 320 | llvm::BasicBlock *TrueBlock, |
| 321 | llvm::BasicBlock *FalseBlock) { |
| 322 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 323 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
| 324 | |
| 325 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 326 | // Handle X && Y in a condition. |
| 327 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 328 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 329 | // folded if the case was simple enough. |
| 330 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 331 | // br(1 && X) -> br(X). |
| 332 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 333 | } |
| 334 | |
| 335 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 336 | // "X && 0" would have been constant folded to 0. |
| 337 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 338 | // br(X && 1) -> br(X). |
| 339 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 340 | } |
| 341 | |
| 342 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 343 | // want to jump to the FalseBlock. |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 344 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 345 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 346 | EmitBlock(LHSTrue); |
| 347 | |
| 348 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 349 | return; |
| 350 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 351 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 352 | // folded if the case was simple enough. |
| 353 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 354 | // br(0 || X) -> br(X). |
| 355 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 356 | } |
| 357 | |
| 358 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 359 | // "X || 1" would have been constant folded to 1. |
| 360 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 361 | // br(X || 0) -> br(X). |
| 362 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 363 | } |
| 364 | |
| 365 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 366 | // want to jump to the TrueBlock. |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 367 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 368 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 369 | EmitBlock(LHSFalse); |
| 370 | |
| 371 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 372 | return; |
| 373 | } |
Chris Lattner | cfbfe91 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 377 | // br(!x, t, f) -> br(x, f, t) |
| 378 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 379 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Daniel Dunbar | ab81c63 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 382 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 383 | // Handle ?: operator. |
| 384 | |
| 385 | // Just ignore GNU ?: extension. |
| 386 | if (CondOp->getLHS()) { |
| 387 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 388 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 389 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 390 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 391 | EmitBlock(LHSBlock); |
| 392 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 393 | EmitBlock(RHSBlock); |
| 394 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 399 | // Emit the code with the fully general case. |
| 400 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 401 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 402 | } |
| 403 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 404 | /// getCGRecordLayout - Return record layout info. |
| 405 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 406 | QualType Ty) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 407 | const RecordType *RTy = Ty->getAs<RecordType>(); |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 408 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 410 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 412 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 413 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 414 | /// specified stmt yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 415 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 416 | bool OmitOnError) { |
| 417 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 420 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 421 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 422 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 423 | } |
| 424 | |
Chris Lattner | 96086d8 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 425 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) { |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 426 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 427 | if (DestPtr->getType() != BP) |
| 428 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 429 | |
| 430 | // Get size and alignment info for this aggregate. |
| 431 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 432 | |
Chris Lattner | 96086d8 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 433 | // Don't bother emitting a zero-byte memset. |
| 434 | if (TypeInfo.first == 0) |
| 435 | return; |
| 436 | |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 437 | // FIXME: Handle variable sized types. |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 438 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 439 | |
| 440 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 441 | llvm::Constant::getNullValue(llvm::Type::Int8Ty), |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 442 | // TypeInfo.first describes size in bits. |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 443 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 444 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Anders Carlsson | 82b0d0c | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 445 | TypeInfo.second/8)); |
| 446 | } |
| 447 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 448 | void CodeGenFunction::EmitIndirectSwitches() { |
| 449 | llvm::BasicBlock *Default; |
| 450 | |
Daniel Dunbar | 8ccfa80 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 451 | if (IndirectSwitches.empty()) |
| 452 | return; |
| 453 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 454 | if (!LabelIDs.empty()) { |
| 455 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 456 | } else { |
| 457 | // No possible targets for indirect goto, just emit an infinite |
| 458 | // loop. |
Daniel Dunbar | 72f9655 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 459 | Default = createBasicBlock("indirectgoto.loop", CurFn); |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 460 | llvm::BranchInst::Create(Default, Default); |
| 461 | } |
| 462 | |
| 463 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 464 | e = IndirectSwitches.end(); i != e; ++i) { |
| 465 | llvm::SwitchInst *I = *i; |
| 466 | |
| 467 | I->setSuccessor(0, Default); |
| 468 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 469 | LE = LabelIDs.end(); LI != LE; ++LI) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 470 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 471 | LI->second), |
| 472 | getBasicBlockForLabel(LI->first)); |
| 473 | } |
| 474 | } |
| 475 | } |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 476 | |
Daniel Dunbar | e94e486 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 477 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Anders Carlsson | 32aa0c2 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 478 | llvm::Value *&SizeEntry = VLASizeMap[VAT]; |
Anders Carlsson | 32aa0c2 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 479 | |
Anders Carlsson | f860e02 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 480 | assert(SizeEntry && "Did not emit size for type"); |
| 481 | return SizeEntry; |
| 482 | } |
Anders Carlsson | 32aa0c2 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | e94e486 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 484 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 485 | assert(Ty->isVariablyModifiedType() && |
| 486 | "Must pass variably modified type to EmitVLASizes!"); |
Anders Carlsson | f860e02 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 487 | |
Daniel Dunbar | e94e486 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 488 | EnsureInsertPoint(); |
| 489 | |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 490 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
| 491 | llvm::Value *&SizeEntry = VLASizeMap[VAT]; |
| 492 | |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 493 | if (!SizeEntry) { |
| 494 | // Get the element size; |
| 495 | llvm::Value *ElemSize; |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 496 | |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 497 | QualType ElemTy = VAT->getElementType(); |
Anders Carlsson | 8f30de9 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 498 | |
| 499 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 500 | |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 501 | if (ElemTy->isVariableArrayType()) |
| 502 | ElemSize = EmitVLASize(ElemTy); |
| 503 | else { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 504 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 505 | getContext().getTypeSize(ElemTy) / 8); |
| 506 | } |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 507 | |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 508 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 8f30de9 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 509 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
| 510 | |
Anders Carlsson | ef2f7df | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 511 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 514 | return SizeEntry; |
Eli Friedman | 16444cf | 2009-05-29 19:23:46 +0000 | [diff] [blame] | 515 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 516 | EmitVLASize(AT->getElementType()); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 517 | } else if (const PointerType *PT = Ty->getAs<PointerType>()) |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 518 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | f860e02 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 519 | else { |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 520 | assert(0 && "unknown VM type!"); |
Anders Carlsson | 32aa0c2 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 521 | } |
Anders Carlsson | d976761 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 522 | |
| 523 | return 0; |
Anders Carlsson | 32aa0c2 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 524 | } |
Eli Friedman | 8f5e878 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 525 | |
| 526 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 527 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { |
| 528 | return EmitScalarExpr(E); |
| 529 | } |
| 530 | return EmitLValue(E).getAddress(); |
| 531 | } |
Anders Carlsson | 9c5b2a4 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 532 | |
Anders Carlsson | 459ee36 | 2009-02-08 03:22:36 +0000 | [diff] [blame] | 533 | void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock) |
Anders Carlsson | 9c5b2a4 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 534 | { |
Anders Carlsson | 9c5b2a4 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 535 | CleanupEntries.push_back(CleanupEntry(CleanupBlock)); |
Anders Carlsson | 9c5b2a4 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 536 | } |
Anders Carlsson | 883fa55 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 537 | |
| 538 | void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) |
| 539 | { |
| 540 | assert(CleanupEntries.size() >= OldCleanupStackSize && |
| 541 | "Cleanup stack mismatch!"); |
| 542 | |
| 543 | while (CleanupEntries.size() > OldCleanupStackSize) |
| 544 | EmitCleanupBlock(); |
| 545 | } |
| 546 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 547 | CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() |
Anders Carlsson | 883fa55 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 548 | { |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 549 | CleanupEntry &CE = CleanupEntries.back(); |
| 550 | |
| 551 | llvm::BasicBlock *CleanupBlock = CE.CleanupBlock; |
| 552 | |
| 553 | std::vector<llvm::BasicBlock *> Blocks; |
| 554 | std::swap(Blocks, CE.Blocks); |
| 555 | |
| 556 | std::vector<llvm::BranchInst *> BranchFixups; |
| 557 | std::swap(BranchFixups, CE.BranchFixups); |
| 558 | |
| 559 | CleanupEntries.pop_back(); |
| 560 | |
Anders Carlsson | 98720f0 | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 561 | // Check if any branch fixups pointed to the scope we just popped. If so, |
| 562 | // we can remove them. |
| 563 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 564 | llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); |
| 565 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 566 | |
Anders Carlsson | 98720f0 | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 567 | if (I == BlockScopes.end()) |
| 568 | continue; |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 569 | |
Anders Carlsson | 98720f0 | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 570 | assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 571 | |
Anders Carlsson | 98720f0 | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 572 | if (I->second == CleanupEntries.size()) { |
| 573 | // We don't need to do this branch fixup. |
| 574 | BranchFixups[i] = BranchFixups.back(); |
| 575 | BranchFixups.pop_back(); |
| 576 | i--; |
| 577 | e--; |
| 578 | continue; |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 581 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 582 | llvm::BasicBlock *SwitchBlock = 0; |
| 583 | llvm::BasicBlock *EndBlock = 0; |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 584 | if (!BranchFixups.empty()) { |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 585 | SwitchBlock = createBasicBlock("cleanup.switch"); |
| 586 | EndBlock = createBasicBlock("cleanup.end"); |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 587 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 588 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Anders Carlsson | 031eb3e | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 589 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 590 | Builder.SetInsertPoint(SwitchBlock); |
| 591 | |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 592 | llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty, |
| 593 | "cleanup.dst"); |
| 594 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
| 595 | |
| 596 | // Create a switch instruction to determine where to jump next. |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 597 | llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 598 | BranchFixups.size()); |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 599 | |
Anders Carlsson | 32fffb7 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 600 | // Restore the current basic block (if any) |
Anders Carlsson | 031eb3e | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 601 | if (CurBB) { |
Anders Carlsson | 32fffb7 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 602 | Builder.SetInsertPoint(CurBB); |
Anders Carlsson | 031eb3e | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 603 | |
| 604 | // If we had a current basic block, we also need to emit an instruction |
| 605 | // to initialize the cleanup destination. |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 606 | Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
Anders Carlsson | 031eb3e | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 607 | DestCodePtr); |
| 608 | } else |
Anders Carlsson | 32fffb7 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 609 | Builder.ClearInsertionPoint(); |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 611 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 612 | llvm::BranchInst *BI = BranchFixups[i]; |
| 613 | llvm::BasicBlock *Dest = BI->getSuccessor(0); |
| 614 | |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 615 | // Fixup the branch instruction to point to the cleanup block. |
| 616 | BI->setSuccessor(0, CleanupBlock); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 618 | if (CleanupEntries.empty()) { |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 619 | llvm::ConstantInt *ID; |
| 620 | |
| 621 | // Check if we already have a destination for this block. |
| 622 | if (Dest == SI->getDefaultDest()) |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 623 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 624 | else { |
| 625 | ID = SI->findCaseDest(Dest); |
| 626 | if (!ID) { |
| 627 | // No code found, get a new unique one by using the number of |
| 628 | // switch successors. |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 629 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 630 | SI->getNumSuccessors()); |
| 631 | SI->addCase(ID, Dest); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Store the jump destination before the branch instruction. |
| 636 | new llvm::StoreInst(ID, DestCodePtr, BI); |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 637 | } else { |
| 638 | // We need to jump through another cleanup block. Create a pad block |
| 639 | // with a branch instruction that jumps to the final destination and |
| 640 | // add it as a branch fixup to the current cleanup scope. |
| 641 | |
| 642 | // Create the pad block. |
| 643 | llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 644 | |
| 645 | // Create a unique case ID. |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 646 | llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 647 | SI->getNumSuccessors()); |
| 648 | |
| 649 | // Store the jump destination before the branch instruction. |
| 650 | new llvm::StoreInst(ID, DestCodePtr, BI); |
| 651 | |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 652 | // Add it as the destination. |
Anders Carlsson | 8be6340 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 653 | SI->addCase(ID, CleanupPad); |
Anders Carlsson | 9b39979 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 654 | |
| 655 | // Create the branch to the final destination. |
| 656 | llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); |
| 657 | CleanupPad->getInstList().push_back(BI); |
| 658 | |
| 659 | // And add it as a branch fixup. |
| 660 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 661 | } |
| 662 | } |
| 663 | } |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 664 | |
Anders Carlsson | e7de352 | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 665 | // Remove all blocks from the block scope map. |
| 666 | for (size_t i = 0, e = Blocks.size(); i != e; ++i) { |
| 667 | assert(BlockScopes.count(Blocks[i]) && |
| 668 | "Did not find block in scope map!"); |
| 669 | |
| 670 | BlockScopes.erase(Blocks[i]); |
| 671 | } |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 672 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 673 | return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void CodeGenFunction::EmitCleanupBlock() |
| 677 | { |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 678 | CleanupBlockInfo Info = PopCleanupBlock(); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 679 | |
Anders Carlsson | 93690d6 | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 680 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 681 | if (CurBB && !CurBB->getTerminator() && |
| 682 | Info.CleanupBlock->getNumUses() == 0) { |
| 683 | CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); |
| 684 | delete Info.CleanupBlock; |
| 685 | } else |
| 686 | EmitBlock(Info.CleanupBlock); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | df27419 | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 688 | if (Info.SwitchBlock) |
| 689 | EmitBlock(Info.SwitchBlock); |
| 690 | if (Info.EndBlock) |
| 691 | EmitBlock(Info.EndBlock); |
Anders Carlsson | e75ae4d | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Anders Carlsson | ed536bc | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 694 | void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) |
| 695 | { |
| 696 | assert(!CleanupEntries.empty() && |
| 697 | "Trying to add branch fixup without cleanup block!"); |
| 698 | |
Mike Stump | ba2cb0e | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 699 | // FIXME: We could be more clever here and check if there's already a branch |
| 700 | // fixup for this destination and recycle it. |
Anders Carlsson | ed536bc | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 701 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 702 | } |
| 703 | |
| 704 | void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) |
| 705 | { |
Anders Carlsson | 32fffb7 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 706 | if (!HaveInsertPoint()) |
| 707 | return; |
| 708 | |
Anders Carlsson | ed536bc | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 709 | llvm::BranchInst* BI = Builder.CreateBr(Dest); |
| 710 | |
Anders Carlsson | 32fffb7 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 711 | Builder.ClearInsertionPoint(); |
| 712 | |
Anders Carlsson | ed536bc | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 713 | // The stack is empty, no need to do any cleanup. |
| 714 | if (CleanupEntries.empty()) |
| 715 | return; |
| 716 | |
| 717 | if (!Dest->getParent()) { |
| 718 | // We are trying to branch to a block that hasn't been inserted yet. |
| 719 | AddBranchFixup(BI); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
| 724 | if (I == BlockScopes.end()) { |
| 725 | // We are trying to jump to a block that is outside of any cleanup scope. |
| 726 | AddBranchFixup(BI); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | assert(I->second < CleanupEntries.size() && |
| 731 | "Trying to branch into cleanup region"); |
| 732 | |
| 733 | if (I->second == CleanupEntries.size() - 1) { |
| 734 | // We have a branch to a block in the same scope. |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | AddBranchFixup(BI); |
| 739 | } |