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