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