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