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