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), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 30 | DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
| 31 | CXXThisDecl(0) { |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 32 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 33 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 34 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | |
| 36 | ASTContext &CodeGenFunction::getContext() const { |
| 37 | return CGM.getContext(); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 42 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 43 | if (BB) return BB; |
| 44 | |
| 45 | // Create, but don't insert, the new block. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 46 | return BB = createBasicBlock(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 49 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { |
| 50 | llvm::Value *Res = LocalDeclMap[VD]; |
| 51 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 52 | return Res; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 53 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 55 | llvm::Constant * |
| 56 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 57 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 60 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 61 | return CGM.getTypes().ConvertTypeForMem(T); |
| 62 | } |
| 63 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 65 | return CGM.getTypes().ConvertType(T); |
| 66 | } |
| 67 | |
| 68 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Daniel Dunbar | a782ca7 | 2009-01-09 02:44:18 +0000 | [diff] [blame] | 69 | // FIXME: Use positive checks instead of negative ones to be more |
| 70 | // robust in the face of extension. |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 71 | return !T->hasPointerRepresentation() &&!T->isRealType() && |
| 72 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() && |
Daniel Dunbar | a782ca7 | 2009-01-09 02:44:18 +0000 | [diff] [blame] | 73 | !T->isBlockPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 76 | void CodeGenFunction::EmitReturnBlock() { |
| 77 | // For cleanliness, we try to avoid emitting the return block for |
| 78 | // simple cases. |
| 79 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 80 | |
| 81 | if (CurBB) { |
| 82 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 83 | |
| 84 | // We have a valid insert point, reuse it if there are no explicit |
| 85 | // jumps to the return block. |
| 86 | if (ReturnBlock->use_empty()) |
| 87 | delete ReturnBlock; |
| 88 | else |
| 89 | EmitBlock(ReturnBlock); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Otherwise, if the return block is the target of a single direct |
| 94 | // branch then we can just put the code in that block instead. This |
| 95 | // cleans up functions which started with a unified return block. |
| 96 | if (ReturnBlock->hasOneUse()) { |
| 97 | llvm::BranchInst *BI = |
| 98 | dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); |
| 99 | if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { |
| 100 | // Reset insertion point and delete the branch. |
| 101 | Builder.SetInsertPoint(BI->getParent()); |
| 102 | BI->eraseFromParent(); |
| 103 | delete ReturnBlock; |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // FIXME: We are at an unreachable point, there is no reason to emit |
| 109 | // the block unless it has uses. However, we still need a place to |
| 110 | // put the debug region.end for now. |
| 111 | |
| 112 | EmitBlock(ReturnBlock); |
| 113 | } |
| 114 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 115 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 116 | // Finish emission of indirect switches. |
| 117 | EmitIndirectSwitches(); |
| 118 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 119 | assert(BreakContinueStack.empty() && |
| 120 | "mismatched push/pop in break/continue stack!"); |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 121 | assert(BlockScopes.empty() && |
| 122 | "did not remove all blocks from block scope map!"); |
| 123 | assert(CleanupEntries.empty() && |
| 124 | "mismatched push/pop in cleanup stack!"); |
| 125 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 126 | // Emit function epilog (to return). |
| 127 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 128 | |
| 129 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 130 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 131 | DI->setLocation(EndLoc); |
| 132 | DI->EmitRegionEnd(CurFn, Builder); |
| 133 | } |
| 134 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 135 | EmitFunctionEpilog(*CurFnInfo, ReturnValue); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 137 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 138 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 139 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 140 | Ptr->eraseFromParent(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 143 | void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, |
| 144 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 145 | const FunctionArgList &Args, |
| 146 | SourceLocation StartLoc) { |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 147 | DidCallStackSave = false; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 148 | CurFuncDecl = D; |
| 149 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 150 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 151 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 152 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 153 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 155 | // Create a marker to make it easy to insert allocas into the entryblock |
| 156 | // later. Don't create this with the builder, because we don't want it |
| 157 | // folded. |
| 158 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 159 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "", |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 160 | EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 161 | if (Builder.isNamePreserving()) |
| 162 | AllocaInsertPt->setName("allocapt"); |
| 163 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 164 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 165 | ReturnValue = 0; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 166 | if (!RetTy->isVoidType()) |
| 167 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 169 | Builder.SetInsertPoint(EntryBB); |
| 170 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 171 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 172 | // FIXME: The cast here is a huge hack. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 173 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 174 | DI->setLocation(StartLoc); |
| 175 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 176 | DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder); |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 177 | } else { |
| 178 | // Just use LLVM function name. |
| 179 | DI->EmitFunctionStart(Fn->getName().c_str(), |
| 180 | RetTy, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 181 | } |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 184 | // FIXME: Leaked. |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 185 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 186 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 187 | |
| 188 | // If any of the arguments have a variably modified type, make sure to |
| 189 | // emit the type size. |
| 190 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 191 | i != e; ++i) { |
| 192 | QualType Ty = i->second; |
| 193 | |
| 194 | if (Ty->isVariablyModifiedType()) |
| 195 | EmitVLASize(Ty); |
| 196 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 197 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 199 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 200 | llvm::Function *Fn) { |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 201 | // Check if we should generate debug info for this function. |
| 202 | if (CGM.getDebugInfo() && !FD->getAttr<NodebugAttr>()) |
| 203 | DebugInfo = CGM.getDebugInfo(); |
| 204 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 205 | FunctionArgList Args; |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 206 | |
| 207 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 208 | if (MD->isInstance()) { |
| 209 | // Create the implicit 'this' decl. |
| 210 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 211 | // generation. Maybe we can come up with a better way? |
| 212 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), |
| 213 | &getContext().Idents.get("this"), |
| 214 | MD->getThisType(getContext())); |
| 215 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
| 216 | } |
| 217 | } |
| 218 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 219 | if (FD->getNumParams()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 220 | const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 221 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 222 | |
| 223 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
| 224 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
| 225 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 226 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 228 | StartFunction(FD, FD->getResultType(), Fn, Args, |
| 229 | cast<CompoundStmt>(FD->getBody())->getLBracLoc()); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 230 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 231 | EmitStmt(FD->getBody()); |
| 232 | |
| 233 | const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); |
Chris Lattner | b75863d | 2009-04-10 00:35:59 +0000 | [diff] [blame^] | 234 | if (S) |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 235 | FinishFunction(S->getRBracLoc()); |
Chris Lattner | b75863d | 2009-04-10 00:35:59 +0000 | [diff] [blame^] | 236 | else |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 237 | FinishFunction(); |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 238 | |
| 239 | // Destroy the 'this' declaration. |
| 240 | if (CXXThisDecl) |
| 241 | CXXThisDecl->Destroy(getContext()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 244 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 245 | /// this statement is not executed normally, it not containing a label means |
| 246 | /// that we can just remove the code. |
| 247 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 248 | // Null statement, not a label! |
| 249 | if (S == 0) return false; |
| 250 | |
| 251 | // If this is a label, we have to emit the code, consider something like: |
| 252 | // if (0) { ... foo: bar(); } goto foo; |
| 253 | if (isa<LabelStmt>(S)) |
| 254 | return true; |
| 255 | |
| 256 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 257 | // to emit the code. |
| 258 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 259 | return true; |
| 260 | |
| 261 | // If this is a switch statement, we want to ignore cases below it. |
| 262 | if (isa<SwitchStmt>(S)) |
| 263 | IgnoreCaseStmts = true; |
| 264 | |
| 265 | // Scan subexpressions for verboten labels. |
| 266 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 267 | I != E; ++I) |
| 268 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 269 | return true; |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 274 | |
| 275 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 276 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 277 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 278 | /// to 'false' and does not contain a label, return -1. |
| 279 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 280 | // FIXME: Rename and handle conversion of other evaluatable things |
| 281 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 282 | Expr::EvalResult Result; |
| 283 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
| 284 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 285 | return 0; // Not foldable, not integer or not fully evaluatable. |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 286 | |
| 287 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 288 | return 0; // Contains a label. |
| 289 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 290 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
| 294 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 295 | /// statement) to the specified blocks. Based on the condition, this might try |
| 296 | /// to simplify the codegen of the conditional based on the branch. |
| 297 | /// |
| 298 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 299 | llvm::BasicBlock *TrueBlock, |
| 300 | llvm::BasicBlock *FalseBlock) { |
| 301 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 302 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
| 303 | |
| 304 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 305 | // Handle X && Y in a condition. |
| 306 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 307 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 308 | // folded if the case was simple enough. |
| 309 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 310 | // br(1 && X) -> br(X). |
| 311 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 312 | } |
| 313 | |
| 314 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 315 | // "X && 0" would have been constant folded to 0. |
| 316 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 317 | // br(X && 1) -> br(X). |
| 318 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 319 | } |
| 320 | |
| 321 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 322 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 323 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 324 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 325 | EmitBlock(LHSTrue); |
| 326 | |
| 327 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 328 | return; |
| 329 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 330 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 331 | // folded if the case was simple enough. |
| 332 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 333 | // br(0 || X) -> br(X). |
| 334 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 335 | } |
| 336 | |
| 337 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 338 | // "X || 1" would have been constant folded to 1. |
| 339 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 340 | // br(X || 0) -> br(X). |
| 341 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 342 | } |
| 343 | |
| 344 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 345 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 346 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 347 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 348 | EmitBlock(LHSFalse); |
| 349 | |
| 350 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 351 | return; |
| 352 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 356 | // br(!x, t, f) -> br(x, f, t) |
| 357 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 358 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 361 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 362 | // Handle ?: operator. |
| 363 | |
| 364 | // Just ignore GNU ?: extension. |
| 365 | if (CondOp->getLHS()) { |
| 366 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 367 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 368 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 369 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 370 | EmitBlock(LHSBlock); |
| 371 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 372 | EmitBlock(RHSBlock); |
| 373 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 374 | return; |
| 375 | } |
| 376 | } |
| 377 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 378 | // Emit the code with the fully general case. |
| 379 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 380 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 381 | } |
| 382 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 383 | /// getCGRecordLayout - Return record layout info. |
| 384 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 385 | QualType Ty) { |
| 386 | const RecordType *RTy = Ty->getAsRecordType(); |
| 387 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 388 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 389 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 392 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 393 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 394 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 395 | bool OmitOnError) { |
| 396 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 399 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 400 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 401 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 402 | } |
| 403 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 404 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) |
| 405 | { |
| 406 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 407 | if (DestPtr->getType() != BP) |
| 408 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 409 | |
| 410 | // Get size and alignment info for this aggregate. |
| 411 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 412 | |
| 413 | // FIXME: Handle variable sized types. |
| 414 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
| 415 | |
| 416 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
| 417 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 418 | // TypeInfo.first describes size in bits. |
| 419 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 420 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 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) { |
| 446 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 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 { |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 480 | ElemSize = llvm::ConstantInt::get(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; |
| 491 | } else if (const PointerType *PT = Ty->getAsPointerType()) |
| 492 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 493 | else { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 494 | assert(0 && "unknown VM type!"); |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 495 | } |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 496 | |
| 497 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 498 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 499 | |
| 500 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 501 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { |
| 502 | return EmitScalarExpr(E); |
| 503 | } |
| 504 | return EmitLValue(E).getAddress(); |
| 505 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 506 | |
Anders Carlsson | 6fc5591 | 2009-02-08 03:22:36 +0000 | [diff] [blame] | 507 | void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock) |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 508 | { |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 509 | CleanupEntries.push_back(CleanupEntry(CleanupBlock)); |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 510 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 511 | |
| 512 | void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) |
| 513 | { |
| 514 | assert(CleanupEntries.size() >= OldCleanupStackSize && |
| 515 | "Cleanup stack mismatch!"); |
| 516 | |
| 517 | while (CleanupEntries.size() > OldCleanupStackSize) |
| 518 | EmitCleanupBlock(); |
| 519 | } |
| 520 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 521 | CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 522 | { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 523 | CleanupEntry &CE = CleanupEntries.back(); |
| 524 | |
| 525 | llvm::BasicBlock *CleanupBlock = CE.CleanupBlock; |
| 526 | |
| 527 | std::vector<llvm::BasicBlock *> Blocks; |
| 528 | std::swap(Blocks, CE.Blocks); |
| 529 | |
| 530 | std::vector<llvm::BranchInst *> BranchFixups; |
| 531 | std::swap(BranchFixups, CE.BranchFixups); |
| 532 | |
| 533 | CleanupEntries.pop_back(); |
| 534 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 535 | // Check if any branch fixups pointed to the scope we just popped. If so, |
| 536 | // we can remove them. |
| 537 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 538 | llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); |
| 539 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 540 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 541 | if (I == BlockScopes.end()) |
| 542 | continue; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 544 | assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 545 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 546 | if (I->second == CleanupEntries.size()) { |
| 547 | // We don't need to do this branch fixup. |
| 548 | BranchFixups[i] = BranchFixups.back(); |
| 549 | BranchFixups.pop_back(); |
| 550 | i--; |
| 551 | e--; |
| 552 | continue; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 556 | llvm::BasicBlock *SwitchBlock = 0; |
| 557 | llvm::BasicBlock *EndBlock = 0; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 558 | if (!BranchFixups.empty()) { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 559 | SwitchBlock = createBasicBlock("cleanup.switch"); |
| 560 | EndBlock = createBasicBlock("cleanup.end"); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 561 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 562 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 563 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 564 | Builder.SetInsertPoint(SwitchBlock); |
| 565 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 566 | llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty, |
| 567 | "cleanup.dst"); |
| 568 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
| 569 | |
| 570 | // Create a switch instruction to determine where to jump next. |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 571 | llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 572 | BranchFixups.size()); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 573 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 574 | // Restore the current basic block (if any) |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 575 | if (CurBB) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 576 | Builder.SetInsertPoint(CurBB); |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 577 | |
| 578 | // If we had a current basic block, we also need to emit an instruction |
| 579 | // to initialize the cleanup destination. |
| 580 | Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
| 581 | DestCodePtr); |
| 582 | } else |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 583 | Builder.ClearInsertionPoint(); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 584 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 585 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 586 | llvm::BranchInst *BI = BranchFixups[i]; |
| 587 | llvm::BasicBlock *Dest = BI->getSuccessor(0); |
| 588 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 589 | // Fixup the branch instruction to point to the cleanup block. |
| 590 | BI->setSuccessor(0, CleanupBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 591 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 592 | if (CleanupEntries.empty()) { |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 593 | llvm::ConstantInt *ID; |
| 594 | |
| 595 | // Check if we already have a destination for this block. |
| 596 | if (Dest == SI->getDefaultDest()) |
| 597 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 598 | else { |
| 599 | ID = SI->findCaseDest(Dest); |
| 600 | if (!ID) { |
| 601 | // No code found, get a new unique one by using the number of |
| 602 | // switch successors. |
| 603 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 604 | SI->getNumSuccessors()); |
| 605 | SI->addCase(ID, Dest); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Store the jump destination before the branch instruction. |
| 610 | new llvm::StoreInst(ID, DestCodePtr, BI); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 611 | } else { |
| 612 | // We need to jump through another cleanup block. Create a pad block |
| 613 | // with a branch instruction that jumps to the final destination and |
| 614 | // add it as a branch fixup to the current cleanup scope. |
| 615 | |
| 616 | // Create the pad block. |
| 617 | llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 618 | |
| 619 | // Create a unique case ID. |
| 620 | llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 621 | SI->getNumSuccessors()); |
| 622 | |
| 623 | // Store the jump destination before the branch instruction. |
| 624 | new llvm::StoreInst(ID, DestCodePtr, BI); |
| 625 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 626 | // Add it as the destination. |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 627 | SI->addCase(ID, CleanupPad); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 628 | |
| 629 | // Create the branch to the final destination. |
| 630 | llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); |
| 631 | CleanupPad->getInstList().push_back(BI); |
| 632 | |
| 633 | // And add it as a branch fixup. |
| 634 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 635 | } |
| 636 | } |
| 637 | } |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 639 | // Remove all blocks from the block scope map. |
| 640 | for (size_t i = 0, e = Blocks.size(); i != e; ++i) { |
| 641 | assert(BlockScopes.count(Blocks[i]) && |
| 642 | "Did not find block in scope map!"); |
| 643 | |
| 644 | BlockScopes.erase(Blocks[i]); |
| 645 | } |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 646 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 647 | return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void CodeGenFunction::EmitCleanupBlock() |
| 651 | { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 652 | CleanupBlockInfo Info = PopCleanupBlock(); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 653 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 654 | EmitBlock(Info.CleanupBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 655 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 656 | if (Info.SwitchBlock) |
| 657 | EmitBlock(Info.SwitchBlock); |
| 658 | if (Info.EndBlock) |
| 659 | EmitBlock(Info.EndBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 662 | void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) |
| 663 | { |
| 664 | assert(!CleanupEntries.empty() && |
| 665 | "Trying to add branch fixup without cleanup block!"); |
| 666 | |
| 667 | // FIXME: We could be more clever here and check if there's already a |
| 668 | // branch fixup for this destination and recycle it. |
| 669 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 670 | } |
| 671 | |
| 672 | void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) |
| 673 | { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 674 | if (!HaveInsertPoint()) |
| 675 | return; |
| 676 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 677 | llvm::BranchInst* BI = Builder.CreateBr(Dest); |
| 678 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 679 | Builder.ClearInsertionPoint(); |
| 680 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 681 | // The stack is empty, no need to do any cleanup. |
| 682 | if (CleanupEntries.empty()) |
| 683 | return; |
| 684 | |
| 685 | if (!Dest->getParent()) { |
| 686 | // We are trying to branch to a block that hasn't been inserted yet. |
| 687 | AddBranchFixup(BI); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
| 692 | if (I == BlockScopes.end()) { |
| 693 | // We are trying to jump to a block that is outside of any cleanup scope. |
| 694 | AddBranchFixup(BI); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | assert(I->second < CleanupEntries.size() && |
| 699 | "Trying to branch into cleanup region"); |
| 700 | |
| 701 | if (I->second == CleanupEntries.size() - 1) { |
| 702 | // We have a branch to a block in the same scope. |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | AddBranchFixup(BI); |
| 707 | } |