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