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" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 17 | #include "CGException.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 19 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 31 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 32 | Target(CGM.getContext().Target), |
Owen Anderson | aac8705 | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 33 | Builder(cgm.getModule().getContext()), |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 34 | NormalCleanupDest(0), EHCleanupDest(0), NextCleanupDestIndex(1), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 35 | ExceptionSlot(0), DebugInfo(0), IndirectBranch(0), |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 36 | SwitchInsn(0), CaseRangeBlock(0), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 37 | DidCallStackSave(false), UnreachableBlock(0), |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 38 | CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 39 | ConditionalBranchLevel(0), TerminateLandingPad(0), TerminateHandler(0), |
Chris Lattner | 83252dc | 2010-07-20 21:07:09 +0000 | [diff] [blame] | 40 | TrapBB(0) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 41 | |
| 42 | // Get some frequently used types. |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 43 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 44 | llvm::LLVMContext &LLVMContext = CGM.getLLVMContext(); |
| 45 | IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth); |
| 46 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 47 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
| 48 | |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 49 | Exceptions = getContext().getLangOptions().Exceptions; |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 50 | CatchUndefined = getContext().getLangOptions().CatchUndefined; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 51 | CGM.getMangleContext().startNewFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 52 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | |
| 54 | ASTContext &CodeGenFunction::getContext() const { |
| 55 | return CGM.getContext(); |
| 56 | } |
| 57 | |
| 58 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 59 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { |
| 60 | llvm::Value *Res = LocalDeclMap[VD]; |
| 61 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 62 | return Res; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 63 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 65 | llvm::Constant * |
| 66 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 67 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 70 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 71 | return CGM.getTypes().ConvertTypeForMem(T); |
| 72 | } |
| 73 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 75 | return CGM.getTypes().ConvertType(T); |
| 76 | } |
| 77 | |
| 78 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | e9d34dc | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 79 | return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || |
| 80 | T->isMemberFunctionPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 83 | void CodeGenFunction::EmitReturnBlock() { |
| 84 | // For cleanliness, we try to avoid emitting the return block for |
| 85 | // simple cases. |
| 86 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 87 | |
| 88 | if (CurBB) { |
| 89 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 90 | |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 91 | // We have a valid insert point, reuse it if it is empty or there are no |
| 92 | // explicit jumps to the return block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 93 | if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { |
| 94 | ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); |
| 95 | delete ReturnBlock.getBlock(); |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 96 | } else |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 97 | EmitBlock(ReturnBlock.getBlock()); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 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. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 104 | if (ReturnBlock.getBlock()->hasOneUse()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | llvm::BranchInst *BI = |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 106 | dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 107 | if (BI && BI->isUnconditional() && |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 108 | BI->getSuccessor(0) == ReturnBlock.getBlock()) { |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 109 | // Reset insertion point and delete the branch. |
| 110 | Builder.SetInsertPoint(BI->getParent()); |
| 111 | BI->eraseFromParent(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 112 | delete ReturnBlock.getBlock(); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 117 | // FIXME: We are at an unreachable point, there is no reason to emit the block |
| 118 | // unless it has uses. However, we still need a place to put the debug |
| 119 | // region.end for now. |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 120 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 121 | EmitBlock(ReturnBlock.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { |
| 125 | if (!BB) return; |
| 126 | if (!BB->use_empty()) |
| 127 | return CGF.CurFn->getBasicBlockList().push_back(BB); |
| 128 | delete BB; |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 131 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 132 | assert(BreakContinueStack.empty() && |
| 133 | "mismatched push/pop in break/continue stack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
| 135 | // Emit function epilog (to return). |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 136 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 138 | EmitFunctionInstrumentation("__cyg_profile_func_exit"); |
| 139 | |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 140 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 141 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 142 | DI->setLocation(EndLoc); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 143 | DI->EmitFunctionEnd(Builder); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 146 | EmitFunctionEpilog(*CurFnInfo); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 147 | EmitEndEHSpec(CurCodeDecl); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 148 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 149 | assert(EHStack.empty() && |
| 150 | "did not remove all scopes from cleanup stack!"); |
| 151 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 152 | // If someone did an indirect goto, emit the indirect goto block at the end of |
| 153 | // the function. |
| 154 | if (IndirectBranch) { |
| 155 | EmitBlock(IndirectBranch->getParent()); |
| 156 | Builder.ClearInsertionPoint(); |
| 157 | } |
| 158 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 159 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 160 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 161 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 162 | Ptr->eraseFromParent(); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 163 | |
| 164 | // If someone took the address of a label but never did an indirect goto, we |
| 165 | // made a zero entry PHI node, which is illegal, zap it now. |
| 166 | if (IndirectBranch) { |
| 167 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 168 | if (PN->getNumIncomingValues() == 0) { |
| 169 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 170 | PN->eraseFromParent(); |
| 171 | } |
| 172 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 173 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 174 | EmitIfUsed(*this, RethrowBlock.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 175 | EmitIfUsed(*this, TerminateLandingPad); |
| 176 | EmitIfUsed(*this, TerminateHandler); |
| 177 | EmitIfUsed(*this, UnreachableBlock); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 178 | |
| 179 | if (CGM.getCodeGenOpts().EmitDeclMetadata) |
| 180 | EmitDeclMetadata(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 183 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 184 | /// instrumented with __cyg_profile_func_* calls |
| 185 | bool CodeGenFunction::ShouldInstrumentFunction() { |
| 186 | if (!CGM.getCodeGenOpts().InstrumentFunctions) |
| 187 | return false; |
| 188 | if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) |
| 189 | return false; |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 194 | /// instrumentation function with the current function and the call site, if |
| 195 | /// function instrumentation is enabled. |
| 196 | void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { |
| 197 | if (!ShouldInstrumentFunction()) |
| 198 | return; |
| 199 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 200 | const llvm::PointerType *PointerTy; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 201 | const llvm::FunctionType *FunctionTy; |
| 202 | std::vector<const llvm::Type*> ProfileFuncArgs; |
| 203 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 204 | // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); |
| 205 | PointerTy = llvm::Type::getInt8PtrTy(VMContext); |
| 206 | ProfileFuncArgs.push_back(PointerTy); |
| 207 | ProfileFuncArgs.push_back(PointerTy); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 208 | FunctionTy = llvm::FunctionType::get( |
| 209 | llvm::Type::getVoidTy(VMContext), |
| 210 | ProfileFuncArgs, false); |
| 211 | |
| 212 | llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); |
| 213 | llvm::CallInst *CallSite = Builder.CreateCall( |
| 214 | CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 215 | llvm::ConstantInt::get(Int32Ty, 0), |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 216 | "callsite"); |
| 217 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 218 | Builder.CreateCall2(F, |
| 219 | llvm::ConstantExpr::getBitCast(CurFn, PointerTy), |
| 220 | CallSite); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 223 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 224 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 225 | const FunctionArgList &Args, |
| 226 | SourceLocation StartLoc) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 227 | const Decl *D = GD.getDecl(); |
| 228 | |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 229 | DidCallStackSave = false; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 230 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 231 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 232 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 233 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 234 | |
Jakob Stoklund Olesen | a3fe284 | 2010-02-09 00:10:00 +0000 | [diff] [blame] | 235 | // Pass inline keyword to optimizer if it appears explicitly on any |
| 236 | // declaration. |
| 237 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 238 | for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 239 | RE = FD->redecls_end(); RI != RE; ++RI) |
| 240 | if (RI->isInlineSpecified()) { |
| 241 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 242 | break; |
| 243 | } |
| 244 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 245 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 247 | // Create a marker to make it easy to insert allocas into the entryblock |
| 248 | // later. Don't create this with the builder, because we don't want it |
| 249 | // folded. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 250 | llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); |
| 251 | AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 252 | if (Builder.isNamePreserving()) |
| 253 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 255 | ReturnBlock = getJumpDestInCurrentScope("return"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 257 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 259 | QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0, |
| 260 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 261 | /*FIXME?*/ |
| 262 | FunctionType::ExtInfo()); |
Mike Stump | 91cc815 | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 263 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 264 | // Emit subprogram debug descriptor. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 265 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 266 | DI->setLocation(StartLoc); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 267 | DI->EmitFunctionStart(GD, FnType, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 270 | EmitFunctionInstrumentation("__cyg_profile_func_enter"); |
| 271 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 272 | // FIXME: Leaked. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 273 | // CC info is ignored, hopefully? |
| 274 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 275 | FunctionType::ExtInfo()); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 276 | |
| 277 | if (RetTy->isVoidType()) { |
| 278 | // Void type; nothing to return. |
| 279 | ReturnValue = 0; |
| 280 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
| 281 | hasAggregateLLVMType(CurFnInfo->getReturnType())) { |
| 282 | // Indirect aggregate return; emit returned value directly into sret slot. |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 283 | // This reduces code size, and affects correctness in C++. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 284 | ReturnValue = CurFn->arg_begin(); |
| 285 | } else { |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 286 | ReturnValue = CreateIRTemp(RetTy, "retval"); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 289 | EmitStartEHSpec(CurCodeDecl); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 290 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 292 | if (CXXThisDecl) |
| 293 | CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 294 | if (CXXVTTDecl) |
| 295 | CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt"); |
| 296 | |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 297 | // If any of the arguments have a variably modified type, make sure to |
| 298 | // emit the type size. |
| 299 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 300 | i != e; ++i) { |
| 301 | QualType Ty = i->second; |
| 302 | |
| 303 | if (Ty->isVariablyModifiedType()) |
| 304 | EmitVLASize(Ty); |
| 305 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 306 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 307 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 308 | void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { |
| 309 | const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 310 | assert(FD->getBody()); |
| 311 | EmitStmt(FD->getBody()); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 314 | void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 315 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 316 | |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 317 | // Check if we should generate debug info for this function. |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 318 | if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 319 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 321 | FunctionArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 323 | CurGD = GD; |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 324 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 325 | if (MD->isInstance()) { |
| 326 | // Create the implicit 'this' decl. |
| 327 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 328 | // generation. Maybe we can come up with a better way? |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 329 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, |
| 330 | FD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | &getContext().Idents.get("this"), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 332 | MD->getThisType(getContext())); |
| 333 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 334 | |
| 335 | // Check if we need a VTT parameter as well. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 336 | if (CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 337 | // FIXME: The comment about using a fake decl above applies here too. |
| 338 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 339 | CXXVTTDecl = |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 340 | ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(), |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 341 | &getContext().Idents.get("vtt"), T); |
| 342 | Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType())); |
| 343 | } |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 347 | if (FD->getNumParams()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 348 | const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 349 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 350 | |
| 351 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 353 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 354 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 355 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 356 | SourceRange BodyRange; |
| 357 | if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 358 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 359 | // Emit the standard function prologue. |
| 360 | StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin()); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 361 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 362 | // Generate the body of the function. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 363 | if (isa<CXXDestructorDecl>(FD)) |
| 364 | EmitDestructorBody(Args); |
| 365 | else if (isa<CXXConstructorDecl>(FD)) |
| 366 | EmitConstructorBody(Args); |
| 367 | else |
| 368 | EmitFunctionBody(Args); |
Anders Carlsson | 1851a12 | 2010-02-07 19:45:40 +0000 | [diff] [blame] | 369 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 370 | // Emit the standard function epilogue. |
| 371 | FinishFunction(BodyRange.getEnd()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 374 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 375 | /// this statement is not executed normally, it not containing a label means |
| 376 | /// that we can just remove the code. |
| 377 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 378 | // Null statement, not a label! |
| 379 | if (S == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 381 | // If this is a label, we have to emit the code, consider something like: |
| 382 | // if (0) { ... foo: bar(); } goto foo; |
| 383 | if (isa<LabelStmt>(S)) |
| 384 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 386 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 387 | // to emit the code. |
| 388 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 389 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 391 | // If this is a switch statement, we want to ignore cases below it. |
| 392 | if (isa<SwitchStmt>(S)) |
| 393 | IgnoreCaseStmts = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 395 | // Scan subexpressions for verboten labels. |
| 396 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 397 | I != E; ++I) |
| 398 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 399 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 404 | |
| 405 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 406 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 407 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 408 | /// to 'false' and does not contain a label, return -1. |
| 409 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 410 | // FIXME: Rename and handle conversion of other evaluatable things |
| 411 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 412 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 414 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 415 | return 0; // Not foldable, not integer or not fully evaluatable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 417 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 418 | return 0; // Contains a label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 420 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
| 424 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 425 | /// statement) to the specified blocks. Based on the condition, this might try |
| 426 | /// to simplify the codegen of the conditional based on the branch. |
| 427 | /// |
| 428 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 429 | llvm::BasicBlock *TrueBlock, |
| 430 | llvm::BasicBlock *FalseBlock) { |
| 431 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 432 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 434 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 435 | // Handle X && Y in a condition. |
| 436 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 437 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 438 | // folded if the case was simple enough. |
| 439 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 440 | // br(1 && X) -> br(X). |
| 441 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 444 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 445 | // "X && 0" would have been constant folded to 0. |
| 446 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 447 | // br(X && 1) -> br(X). |
| 448 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 451 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 452 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 453 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 454 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 455 | EmitBlock(LHSTrue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 457 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 458 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 459 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 460 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 462 | return; |
| 463 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 464 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 465 | // folded if the case was simple enough. |
| 466 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 467 | // br(0 || X) -> br(X). |
| 468 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 471 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 472 | // "X || 1" would have been constant folded to 1. |
| 473 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 474 | // br(X || 0) -> br(X). |
| 475 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 478 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 479 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 480 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 481 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 482 | EmitBlock(LHSFalse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 484 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 485 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 486 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 487 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 493 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 494 | // br(!x, t, f) -> br(x, f, t) |
| 495 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 496 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 499 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 500 | // Handle ?: operator. |
| 501 | |
| 502 | // Just ignore GNU ?: extension. |
| 503 | if (CondOp->getLHS()) { |
| 504 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 505 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 506 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 507 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 508 | EmitBlock(LHSBlock); |
| 509 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 510 | EmitBlock(RHSBlock); |
| 511 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 512 | return; |
| 513 | } |
| 514 | } |
| 515 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 516 | // Emit the code with the fully general case. |
| 517 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 518 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 519 | } |
| 520 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 521 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 522 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 523 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 524 | bool OmitOnError) { |
| 525 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 528 | void |
| 529 | CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { |
| 530 | // If the type contains a pointer to data member we can't memset it to zero. |
| 531 | // Instead, create a null constant and copy it to the destination. |
| 532 | if (CGM.getTypes().ContainsPointerToDataMember(Ty)) { |
| 533 | llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); |
| 534 | |
| 535 | llvm::GlobalVariable *NullVariable = |
| 536 | new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), |
| 537 | /*isConstant=*/true, |
| 538 | llvm::GlobalVariable::PrivateLinkage, |
| 539 | NullConstant, llvm::Twine()); |
| 540 | EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 545 | // Ignore empty classes in C++. |
| 546 | if (getContext().getLangOptions().CPlusPlus) { |
| 547 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 548 | if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) |
| 549 | return; |
| 550 | } |
| 551 | } |
| 552 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 553 | // Otherwise, just memset the whole thing to zero. This is legal |
| 554 | // because in LLVM, all default initializers (other than the ones we just |
| 555 | // handled above) are guaranteed to have a bit pattern of all zeros. |
Chris Lattner | 36afd38 | 2009-10-13 06:02:42 +0000 | [diff] [blame] | 556 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 557 | if (DestPtr->getType() != BP) |
| 558 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 559 | |
| 560 | // Get size and alignment info for this aggregate. |
| 561 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 562 | |
Chris Lattner | 88207c9 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 563 | // Don't bother emitting a zero-byte memset. |
| 564 | if (TypeInfo.first == 0) |
| 565 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 567 | // FIXME: Handle variable sized types. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 568 | Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 569 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 570 | // TypeInfo.first describes size in bits. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 571 | llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), |
| 572 | llvm::ConstantInt::get(Int32Ty, TypeInfo.second/8), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 573 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), |
| 574 | 0)); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 577 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { |
| 578 | // Make sure that there is a block for the indirect goto. |
| 579 | if (IndirectBranch == 0) |
| 580 | GetIndirectGotoBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 581 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 582 | llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 583 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 584 | // Make sure the indirect branch includes all of the address-taken blocks. |
| 585 | IndirectBranch->addDestination(BB); |
| 586 | return llvm::BlockAddress::get(CurFn, BB); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 590 | // If we already made the indirect branch for indirect goto, return its block. |
| 591 | if (IndirectBranch) return IndirectBranch->getParent(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 592 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 593 | CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 594 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 595 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 85e74ac | 2009-10-28 20:36:47 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 597 | // Create the PHI node that indirect gotos will add entries to. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 598 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 599 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 600 | // Create the indirect branch instruction. |
| 601 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 602 | return IndirectBranch->getParent(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 603 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 604 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 605 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 606 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 608 | assert(SizeEntry && "Did not emit size for type"); |
| 609 | return SizeEntry; |
| 610 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 611 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 612 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 613 | assert(Ty->isVariablyModifiedType() && |
| 614 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 616 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 618 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 619 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 621 | if (!SizeEntry) { |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 622 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 624 | // Get the element size; |
| 625 | QualType ElemTy = VAT->getElementType(); |
| 626 | llvm::Value *ElemSize; |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 627 | if (ElemTy->isVariableArrayType()) |
| 628 | ElemSize = EmitVLASize(ElemTy); |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 629 | else |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 630 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 631 | getContext().getTypeSizeInChars(ElemTy).getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 633 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 634 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 636 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 639 | return SizeEntry; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 642 | if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 643 | EmitVLASize(AT->getElementType()); |
| 644 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 647 | const PointerType *PT = Ty->getAs<PointerType>(); |
| 648 | assert(PT && "unknown VM type!"); |
| 649 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 650 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 651 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 652 | |
| 653 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
Chris Lattner | fbe02ff | 2010-06-27 07:40:06 +0000 | [diff] [blame] | 654 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 655 | return EmitScalarExpr(E); |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 656 | return EmitLValue(E).getAddress(); |
| 657 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 658 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 659 | /// Pops cleanup blocks until the given savepoint is reached. |
| 660 | void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) { |
| 661 | assert(Old.isValid()); |
| 662 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 663 | while (EHStack.stable_begin() != Old) { |
| 664 | EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); |
| 665 | |
| 666 | // As long as Old strictly encloses the scope's enclosing normal |
| 667 | // cleanup, we're going to emit another normal cleanup which |
| 668 | // fallthrough can propagate through. |
| 669 | bool FallThroughIsBranchThrough = |
| 670 | Old.strictlyEncloses(Scope.getEnclosingNormalCleanup()); |
| 671 | |
| 672 | PopCleanupBlock(FallThroughIsBranchThrough); |
| 673 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 674 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 675 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 676 | static llvm::BasicBlock *CreateNormalEntry(CodeGenFunction &CGF, |
| 677 | EHCleanupScope &Scope) { |
| 678 | assert(Scope.isNormalCleanup()); |
| 679 | llvm::BasicBlock *Entry = Scope.getNormalBlock(); |
| 680 | if (!Entry) { |
| 681 | Entry = CGF.createBasicBlock("cleanup"); |
| 682 | Scope.setNormalBlock(Entry); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 683 | } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 684 | return Entry; |
| 685 | } |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 686 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 687 | static llvm::BasicBlock *CreateEHEntry(CodeGenFunction &CGF, |
| 688 | EHCleanupScope &Scope) { |
| 689 | assert(Scope.isEHCleanup()); |
| 690 | llvm::BasicBlock *Entry = Scope.getEHBlock(); |
| 691 | if (!Entry) { |
| 692 | Entry = CGF.createBasicBlock("eh.cleanup"); |
| 693 | Scope.setEHBlock(Entry); |
| 694 | } |
| 695 | return Entry; |
| 696 | } |
Devang Patel | cd9199e | 2010-04-13 00:08:43 +0000 | [diff] [blame] | 697 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 698 | /// Transitions the terminator of the given exit-block of a cleanup to |
| 699 | /// be a cleanup switch. |
| 700 | static llvm::SwitchInst *TransitionToCleanupSwitch(CodeGenFunction &CGF, |
| 701 | llvm::BasicBlock *Block) { |
| 702 | // If it's a branch, turn it into a switch whose default |
| 703 | // destination is its original target. |
| 704 | llvm::TerminatorInst *Term = Block->getTerminator(); |
| 705 | assert(Term && "can't transition block without terminator"); |
| 706 | |
| 707 | if (llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Term)) { |
| 708 | assert(Br->isUnconditional()); |
| 709 | llvm::LoadInst *Load = |
| 710 | new llvm::LoadInst(CGF.getNormalCleanupDestSlot(), "cleanup.dest", Term); |
| 711 | llvm::SwitchInst *Switch = |
| 712 | llvm::SwitchInst::Create(Load, Br->getSuccessor(0), 4, Block); |
| 713 | Br->eraseFromParent(); |
| 714 | return Switch; |
| 715 | } else { |
| 716 | return cast<llvm::SwitchInst>(Term); |
| 717 | } |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 718 | } |
| 719 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 720 | /// Attempts to reduce a cleanup's entry block to a fallthrough. This |
| 721 | /// is basically llvm::MergeBlockIntoPredecessor, except |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 722 | /// simplified/optimized for the tighter constraints on cleanup blocks. |
| 723 | /// |
| 724 | /// Returns the new block, whatever it is. |
| 725 | static llvm::BasicBlock *SimplifyCleanupEntry(CodeGenFunction &CGF, |
| 726 | llvm::BasicBlock *Entry) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 727 | llvm::BasicBlock *Pred = Entry->getSinglePredecessor(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 728 | if (!Pred) return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 730 | llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator()); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 731 | if (!Br || Br->isConditional()) return Entry; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 732 | assert(Br->getSuccessor(0) == Entry); |
| 733 | |
| 734 | // If we were previously inserting at the end of the cleanup entry |
| 735 | // block, we'll need to continue inserting at the end of the |
| 736 | // predecessor. |
| 737 | bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry; |
| 738 | assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end()); |
| 739 | |
| 740 | // Kill the branch. |
| 741 | Br->eraseFromParent(); |
| 742 | |
| 743 | // Merge the blocks. |
| 744 | Pred->getInstList().splice(Pred->end(), Entry->getInstList()); |
| 745 | |
| 746 | // Kill the entry block. |
| 747 | Entry->eraseFromParent(); |
| 748 | |
| 749 | if (WasInsertBlock) |
| 750 | CGF.Builder.SetInsertPoint(Pred); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 751 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 752 | return Pred; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 753 | } |
| 754 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 755 | static void EmitCleanup(CodeGenFunction &CGF, |
| 756 | EHScopeStack::Cleanup *Fn, |
| 757 | bool ForEH) { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 758 | if (ForEH) CGF.EHStack.pushTerminate(); |
| 759 | Fn->Emit(CGF, ForEH); |
| 760 | if (ForEH) CGF.EHStack.popTerminate(); |
| 761 | assert(CGF.HaveInsertPoint() && "cleanup ended with no insertion point?"); |
| 762 | } |
| 763 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 764 | /// Pops a cleanup block. If the block includes a normal cleanup, the |
| 765 | /// current insertion point is threaded through the cleanup, as are |
| 766 | /// any branch fixups on the cleanup. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 767 | void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 768 | assert(!EHStack.empty() && "cleanup stack is empty!"); |
| 769 | assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!"); |
| 770 | EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); |
| 771 | assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups()); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 772 | |
| 773 | // Check whether we need an EH cleanup. This is only true if we've |
| 774 | // generated a lazy EH cleanup block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 775 | bool RequiresEHCleanup = Scope.hasEHBranches(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 776 | |
| 777 | // Check the three conditions which might require a normal cleanup: |
| 778 | |
| 779 | // - whether there are branch fix-ups through this cleanup |
| 780 | unsigned FixupDepth = Scope.getFixupDepth(); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 781 | bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth; |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 782 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 783 | // - whether there are branch-throughs or branch-afters |
| 784 | bool HasExistingBranches = Scope.hasBranches(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 785 | |
| 786 | // - whether there's a fallthrough |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 787 | llvm::BasicBlock *FallthroughSource = Builder.GetInsertBlock(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 788 | bool HasFallthrough = (FallthroughSource != 0); |
| 789 | |
| 790 | bool RequiresNormalCleanup = false; |
| 791 | if (Scope.isNormalCleanup() && |
| 792 | (HasFixups || HasExistingBranches || HasFallthrough)) { |
| 793 | RequiresNormalCleanup = true; |
| 794 | } |
| 795 | |
| 796 | // If we don't need the cleanup at all, we're done. |
| 797 | if (!RequiresNormalCleanup && !RequiresEHCleanup) { |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 798 | EHStack.popCleanup(); // safe because there are no fixups |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 799 | assert(EHStack.getNumBranchFixups() == 0 || |
| 800 | EHStack.hasNormalCleanups()); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
| 804 | // Copy the cleanup emission data out. Note that SmallVector |
| 805 | // guarantees maximal alignment for its buffer regardless of its |
| 806 | // type parameter. |
| 807 | llvm::SmallVector<char, 8*sizeof(void*)> CleanupBuffer; |
| 808 | CleanupBuffer.reserve(Scope.getCleanupSize()); |
| 809 | memcpy(CleanupBuffer.data(), |
| 810 | Scope.getCleanupBuffer(), Scope.getCleanupSize()); |
| 811 | CleanupBuffer.set_size(Scope.getCleanupSize()); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 812 | EHScopeStack::Cleanup *Fn = |
| 813 | reinterpret_cast<EHScopeStack::Cleanup*>(CleanupBuffer.data()); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 814 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 815 | // We want to emit the EH cleanup after the normal cleanup, but go |
| 816 | // ahead and do the setup for the EH cleanup while the scope is still |
| 817 | // alive. |
| 818 | llvm::BasicBlock *EHEntry = 0; |
| 819 | llvm::SmallVector<llvm::Instruction*, 2> EHInstsToAppend; |
| 820 | if (RequiresEHCleanup) { |
| 821 | EHEntry = CreateEHEntry(*this, Scope); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 822 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 823 | // Figure out the branch-through dest if necessary. |
| 824 | llvm::BasicBlock *EHBranchThroughDest = 0; |
| 825 | if (Scope.hasEHBranchThroughs()) { |
| 826 | assert(Scope.getEnclosingEHCleanup() != EHStack.stable_end()); |
| 827 | EHScope &S = *EHStack.find(Scope.getEnclosingEHCleanup()); |
| 828 | EHBranchThroughDest = CreateEHEntry(*this, cast<EHCleanupScope>(S)); |
| 829 | } |
| 830 | |
| 831 | // If we have exactly one branch-after and no branch-throughs, we |
| 832 | // can dispatch it without a switch. |
John McCall | 7cd4b06 | 2010-07-26 22:44:58 +0000 | [diff] [blame^] | 833 | if (!Scope.hasEHBranchThroughs() && |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 834 | Scope.getNumEHBranchAfters() == 1) { |
| 835 | assert(!EHBranchThroughDest); |
| 836 | |
| 837 | // TODO: remove the spurious eh.cleanup.dest stores if this edge |
| 838 | // never went through any switches. |
| 839 | llvm::BasicBlock *BranchAfterDest = Scope.getEHBranchAfterBlock(0); |
| 840 | EHInstsToAppend.push_back(llvm::BranchInst::Create(BranchAfterDest)); |
| 841 | |
| 842 | // Otherwise, if we have any branch-afters, we need a switch. |
| 843 | } else if (Scope.getNumEHBranchAfters()) { |
| 844 | // The default of the switch belongs to the branch-throughs if |
| 845 | // they exist. |
| 846 | llvm::BasicBlock *Default = |
| 847 | (EHBranchThroughDest ? EHBranchThroughDest : getUnreachableBlock()); |
| 848 | |
| 849 | const unsigned SwitchCapacity = Scope.getNumEHBranchAfters(); |
| 850 | |
| 851 | llvm::LoadInst *Load = |
| 852 | new llvm::LoadInst(getEHCleanupDestSlot(), "cleanup.dest"); |
| 853 | llvm::SwitchInst *Switch = |
| 854 | llvm::SwitchInst::Create(Load, Default, SwitchCapacity); |
| 855 | |
| 856 | EHInstsToAppend.push_back(Load); |
| 857 | EHInstsToAppend.push_back(Switch); |
| 858 | |
| 859 | for (unsigned I = 0, E = Scope.getNumEHBranchAfters(); I != E; ++I) |
| 860 | Switch->addCase(Scope.getEHBranchAfterIndex(I), |
| 861 | Scope.getEHBranchAfterBlock(I)); |
| 862 | |
| 863 | // Otherwise, we have only branch-throughs; jump to the next EH |
| 864 | // cleanup. |
| 865 | } else { |
| 866 | assert(EHBranchThroughDest); |
| 867 | EHInstsToAppend.push_back(llvm::BranchInst::Create(EHBranchThroughDest)); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (!RequiresNormalCleanup) { |
| 872 | EHStack.popCleanup(); |
| 873 | } else { |
| 874 | // As a kindof crazy internal case, branch-through fall-throughs |
| 875 | // leave the insertion point set to the end of the last cleanup. |
| 876 | bool HasPrebranchedFallthrough = |
| 877 | (HasFallthrough && FallthroughSource->getTerminator()); |
| 878 | assert(!HasPrebranchedFallthrough || |
| 879 | FallthroughSource->getTerminator()->getSuccessor(0) |
| 880 | == Scope.getNormalBlock()); |
| 881 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 882 | // If we have a fallthrough and no other need for the cleanup, |
| 883 | // emit it directly. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 884 | if (HasFallthrough && !HasPrebranchedFallthrough && |
| 885 | !HasFixups && !HasExistingBranches) { |
| 886 | |
| 887 | // Fixups can cause us to optimistically create a normal block, |
| 888 | // only to later have no real uses for it. Just delete it in |
| 889 | // this case. |
| 890 | // TODO: we can potentially simplify all the uses after this. |
| 891 | if (Scope.getNormalBlock()) { |
| 892 | Scope.getNormalBlock()->replaceAllUsesWith(getUnreachableBlock()); |
| 893 | delete Scope.getNormalBlock(); |
| 894 | } |
| 895 | |
| 896 | EHStack.popCleanup(); |
| 897 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 898 | EmitCleanup(*this, Fn, /*ForEH*/ false); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 899 | |
| 900 | // Otherwise, the best approach is to thread everything through |
| 901 | // the cleanup block and then try to clean up after ourselves. |
| 902 | } else { |
| 903 | // Force the entry block to exist. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 904 | llvm::BasicBlock *NormalEntry = CreateNormalEntry(*this, Scope); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 905 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 906 | // If there's a fallthrough, we need to store the cleanup |
| 907 | // destination index. For fall-throughs this is always zero. |
| 908 | if (HasFallthrough && !HasPrebranchedFallthrough) |
| 909 | Builder.CreateStore(Builder.getInt32(0), getNormalCleanupDestSlot()); |
| 910 | |
| 911 | // Emit the entry block. This implicitly branches to it if we |
| 912 | // have fallthrough. All the fixups and existing branches should |
| 913 | // already be branched to it. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 914 | EmitBlock(NormalEntry); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 915 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 916 | bool HasEnclosingCleanups = |
| 917 | (Scope.getEnclosingNormalCleanup() != EHStack.stable_end()); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 918 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 919 | // Compute the branch-through dest if we need it: |
| 920 | // - if there are branch-throughs threaded through the scope |
| 921 | // - if fall-through is a branch-through |
| 922 | // - if there are fixups that will be optimistically forwarded |
| 923 | // to the enclosing cleanup |
| 924 | llvm::BasicBlock *BranchThroughDest = 0; |
| 925 | if (Scope.hasBranchThroughs() || |
| 926 | (HasFallthrough && FallthroughIsBranchThrough) || |
| 927 | (HasFixups && HasEnclosingCleanups)) { |
| 928 | assert(HasEnclosingCleanups); |
| 929 | EHScope &S = *EHStack.find(Scope.getEnclosingNormalCleanup()); |
| 930 | BranchThroughDest = CreateNormalEntry(*this, cast<EHCleanupScope>(S)); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 931 | } |
| 932 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 933 | llvm::BasicBlock *FallthroughDest = 0; |
| 934 | llvm::SmallVector<llvm::Instruction*, 2> InstsToAppend; |
| 935 | |
| 936 | // If there's exactly one branch-after and no other threads, |
| 937 | // we can route it without a switch. |
| 938 | if (!Scope.hasBranchThroughs() && !HasFixups && !HasFallthrough && |
| 939 | Scope.getNumBranchAfters() == 1) { |
| 940 | assert(!BranchThroughDest); |
| 941 | |
| 942 | // TODO: clean up the possibly dead stores to the cleanup dest slot. |
| 943 | llvm::BasicBlock *BranchAfter = Scope.getBranchAfterBlock(0); |
| 944 | InstsToAppend.push_back(llvm::BranchInst::Create(BranchAfter)); |
| 945 | |
| 946 | // Build a switch-out if we need it: |
| 947 | // - if there are branch-afters threaded through the scope |
| 948 | // - if fall-through is a branch-after |
| 949 | // - if there are fixups that have nowhere left to go and |
| 950 | // so must be immediately resolved |
| 951 | } else if (Scope.getNumBranchAfters() || |
| 952 | (HasFallthrough && !FallthroughIsBranchThrough) || |
| 953 | (HasFixups && !HasEnclosingCleanups)) { |
| 954 | |
| 955 | llvm::BasicBlock *Default = |
| 956 | (BranchThroughDest ? BranchThroughDest : getUnreachableBlock()); |
| 957 | |
| 958 | // TODO: base this on the number of branch-afters and fixups |
| 959 | const unsigned SwitchCapacity = 10; |
| 960 | |
| 961 | llvm::LoadInst *Load = |
| 962 | new llvm::LoadInst(getNormalCleanupDestSlot(), "cleanup.dest"); |
| 963 | llvm::SwitchInst *Switch = |
| 964 | llvm::SwitchInst::Create(Load, Default, SwitchCapacity); |
| 965 | |
| 966 | InstsToAppend.push_back(Load); |
| 967 | InstsToAppend.push_back(Switch); |
| 968 | |
| 969 | // Branch-after fallthrough. |
| 970 | if (HasFallthrough && !FallthroughIsBranchThrough) { |
| 971 | FallthroughDest = createBasicBlock("cleanup.cont"); |
| 972 | Switch->addCase(Builder.getInt32(0), FallthroughDest); |
| 973 | } |
| 974 | |
| 975 | for (unsigned I = 0, E = Scope.getNumBranchAfters(); I != E; ++I) { |
| 976 | Switch->addCase(Scope.getBranchAfterIndex(I), |
| 977 | Scope.getBranchAfterBlock(I)); |
| 978 | } |
| 979 | |
| 980 | if (HasFixups && !HasEnclosingCleanups) |
| 981 | ResolveAllBranchFixups(Switch); |
| 982 | } else { |
| 983 | // We should always have a branch-through destination in this case. |
| 984 | assert(BranchThroughDest); |
| 985 | InstsToAppend.push_back(llvm::BranchInst::Create(BranchThroughDest)); |
| 986 | } |
| 987 | |
| 988 | // We're finally ready to pop the cleanup. |
| 989 | EHStack.popCleanup(); |
| 990 | assert(EHStack.hasNormalCleanups() == HasEnclosingCleanups); |
| 991 | |
| 992 | EmitCleanup(*this, Fn, /*ForEH*/ false); |
| 993 | |
| 994 | // Append the prepared cleanup prologue from above. |
| 995 | llvm::BasicBlock *NormalExit = Builder.GetInsertBlock(); |
| 996 | for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I) |
| 997 | NormalExit->getInstList().push_back(InstsToAppend[I]); |
| 998 | |
| 999 | // Optimistically hope that any fixups will continue falling through. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1000 | for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1001 | I < E; ++I) { |
| 1002 | BranchFixup &Fixup = CGF.EHStack.getBranchFixup(I); |
| 1003 | if (!Fixup.Destination) continue; |
| 1004 | if (!Fixup.OptimisticBranchBlock) { |
| 1005 | new llvm::StoreInst(Builder.getInt32(Fixup.DestinationIndex), |
| 1006 | getNormalCleanupDestSlot(), |
| 1007 | Fixup.InitialBranch); |
| 1008 | Fixup.InitialBranch->setSuccessor(0, NormalEntry); |
| 1009 | } |
| 1010 | Fixup.OptimisticBranchBlock = NormalExit; |
| 1011 | } |
| 1012 | |
| 1013 | if (FallthroughDest) |
| 1014 | EmitBlock(FallthroughDest); |
| 1015 | else if (!HasFallthrough) |
| 1016 | Builder.ClearInsertionPoint(); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1017 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1018 | // Check whether we can merge NormalEntry into a single predecessor. |
| 1019 | // This might invalidate (non-IR) pointers to NormalEntry. |
| 1020 | llvm::BasicBlock *NewNormalEntry = |
| 1021 | SimplifyCleanupEntry(*this, NormalEntry); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1022 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1023 | // If it did invalidate those pointers, and NormalEntry was the same |
| 1024 | // as NormalExit, go back and patch up the fixups. |
| 1025 | if (NewNormalEntry != NormalEntry && NormalEntry == NormalExit) |
| 1026 | for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); |
| 1027 | I < E; ++I) |
| 1028 | CGF.EHStack.getBranchFixup(I).OptimisticBranchBlock = NewNormalEntry; |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1032 | assert(EHStack.hasNormalCleanups() || EHStack.getNumBranchFixups() == 0); |
| 1033 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1034 | // Emit the EH cleanup if required. |
| 1035 | if (RequiresEHCleanup) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1036 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1037 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1038 | EmitBlock(EHEntry); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1039 | EmitCleanup(*this, Fn, /*ForEH*/ true); |
| 1040 | |
| 1041 | // Append the prepared cleanup prologue from above. |
| 1042 | llvm::BasicBlock *EHExit = Builder.GetInsertBlock(); |
| 1043 | for (unsigned I = 0, E = EHInstsToAppend.size(); I != E; ++I) |
| 1044 | EHExit->getInstList().push_back(EHInstsToAppend[I]); |
| 1045 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1046 | Builder.restoreIP(SavedIP); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1047 | |
| 1048 | SimplifyCleanupEntry(*this, EHEntry); |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1052 | /// Terminate the current block by emitting a branch which might leave |
| 1053 | /// the current cleanup-protected scope. The target scope may not yet |
| 1054 | /// be known, in which case this will require a fixup. |
| 1055 | /// |
| 1056 | /// As a side-effect, this method clears the insertion point. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1057 | void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 1058 | if (!HaveInsertPoint()) |
| 1059 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1061 | // Create the branch. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1062 | llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1064 | // If we're not in a cleanup scope, or if the destination scope is |
| 1065 | // the current normal-cleanup scope, we don't need to worry about |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1066 | // fixups. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1067 | if (!EHStack.hasNormalCleanups() || |
| 1068 | Dest.getScopeDepth() == EHStack.getInnermostNormalCleanup()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1069 | Builder.ClearInsertionPoint(); |
| 1070 | return; |
| 1071 | } |
| 1072 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1073 | // If we can't resolve the destination cleanup scope, just add this |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1074 | // to the current cleanup scope as a branch fixup. |
| 1075 | if (!Dest.getScopeDepth().isValid()) { |
| 1076 | BranchFixup &Fixup = EHStack.addBranchFixup(); |
| 1077 | Fixup.Destination = Dest.getBlock(); |
| 1078 | Fixup.DestinationIndex = Dest.getDestIndex(); |
| 1079 | Fixup.InitialBranch = BI; |
| 1080 | Fixup.OptimisticBranchBlock = 0; |
| 1081 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1082 | Builder.ClearInsertionPoint(); |
| 1083 | return; |
| 1084 | } |
| 1085 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1086 | // Otherwise, thread through all the normal cleanups in scope. |
| 1087 | |
| 1088 | // Store the index at the start. |
| 1089 | llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex()); |
| 1090 | new llvm::StoreInst(Index, getNormalCleanupDestSlot(), BI); |
| 1091 | |
| 1092 | // Adjust BI to point to the first cleanup block. |
| 1093 | { |
| 1094 | EHCleanupScope &Scope = |
| 1095 | cast<EHCleanupScope>(*EHStack.find(EHStack.getInnermostNormalCleanup())); |
| 1096 | BI->setSuccessor(0, CreateNormalEntry(*this, Scope)); |
| 1097 | } |
| 1098 | |
| 1099 | // Add this destination to all the scopes involved. |
| 1100 | EHScopeStack::stable_iterator I = EHStack.getInnermostNormalCleanup(); |
| 1101 | EHScopeStack::stable_iterator E = Dest.getScopeDepth(); |
| 1102 | if (E.strictlyEncloses(I)) { |
| 1103 | while (true) { |
| 1104 | EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I)); |
| 1105 | assert(Scope.isNormalCleanup()); |
| 1106 | I = Scope.getEnclosingNormalCleanup(); |
| 1107 | |
| 1108 | // If this is the last cleanup we're propagating through, tell it |
| 1109 | // that there's a resolved jump moving through it. |
| 1110 | if (!E.strictlyEncloses(I)) { |
| 1111 | Scope.addBranchAfter(Index, Dest.getBlock()); |
| 1112 | break; |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 1113 | } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Otherwise, tell the scope that there's a jump propoagating |
| 1116 | // through it. If this isn't new information, all the rest of |
| 1117 | // the work has been done before. |
| 1118 | if (!Scope.addBranchThrough(Dest.getBlock())) |
| 1119 | break; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 1123 | Builder.ClearInsertionPoint(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1126 | void CodeGenFunction::EmitBranchThroughEHCleanup(UnwindDest Dest) { |
| 1127 | // We should never get invalid scope depths for an UnwindDest; that |
| 1128 | // implies that the destination wasn't set up correctly. |
| 1129 | assert(Dest.getScopeDepth().isValid() && "invalid scope depth on EH dest?"); |
| 1130 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1131 | if (!HaveInsertPoint()) |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1132 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1134 | // Create the branch. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1135 | llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1136 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1137 | // If the destination is in the same EH cleanup scope as us, we |
| 1138 | // don't need to thread through anything. |
| 1139 | if (Dest.getScopeDepth() == EHStack.getInnermostEHCleanup()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1140 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1143 | assert(EHStack.hasEHCleanups()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1145 | // Store the index at the start. |
| 1146 | llvm::ConstantInt *Index = Builder.getInt32(Dest.getDestIndex()); |
| 1147 | new llvm::StoreInst(Index, getEHCleanupDestSlot(), BI); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1148 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1149 | // Adjust BI to point to the first cleanup block. |
| 1150 | { |
| 1151 | EHCleanupScope &Scope = |
| 1152 | cast<EHCleanupScope>(*EHStack.find(EHStack.getInnermostEHCleanup())); |
| 1153 | BI->setSuccessor(0, CreateEHEntry(*this, Scope)); |
| 1154 | } |
| 1155 | |
| 1156 | // Add this destination to all the scopes involved. |
| 1157 | for (EHScopeStack::stable_iterator |
| 1158 | I = EHStack.getInnermostEHCleanup(), |
| 1159 | E = Dest.getScopeDepth(); ; ) { |
| 1160 | assert(E.strictlyEncloses(I)); |
| 1161 | EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.find(I)); |
| 1162 | assert(Scope.isEHCleanup()); |
| 1163 | I = Scope.getEnclosingEHCleanup(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1164 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1165 | // If this is the last cleanup we're propagating through, add this |
| 1166 | // as a branch-after. |
| 1167 | if (I == E) { |
| 1168 | Scope.addEHBranchAfter(Index, Dest.getBlock()); |
| 1169 | break; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1170 | } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Otherwise, add it as a branch-through. If this isn't new |
| 1173 | // information, all the rest of the work has been done before. |
| 1174 | if (!Scope.addEHBranchThrough(Dest.getBlock())) |
| 1175 | break; |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1176 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1177 | |
| 1178 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1179 | } |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1180 | |
| 1181 | /// All the branch fixups on the EH stack have propagated out past the |
| 1182 | /// outermost normal cleanup; resolve them all by adding cases to the |
| 1183 | /// given switch instruction. |
| 1184 | void CodeGenFunction::ResolveAllBranchFixups(llvm::SwitchInst *Switch) { |
| 1185 | llvm::SmallPtrSet<llvm::BasicBlock*, 4> CasesAdded; |
| 1186 | |
| 1187 | for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) { |
| 1188 | // Skip this fixup if its destination isn't set or if we've |
| 1189 | // already treated it. |
| 1190 | BranchFixup &Fixup = EHStack.getBranchFixup(I); |
| 1191 | if (Fixup.Destination == 0) continue; |
| 1192 | if (!CasesAdded.insert(Fixup.Destination)) continue; |
| 1193 | |
| 1194 | Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), |
| 1195 | Fixup.Destination); |
| 1196 | } |
| 1197 | |
| 1198 | EHStack.clearFixups(); |
| 1199 | } |
| 1200 | |
| 1201 | void CodeGenFunction::ResolveBranchFixups(llvm::BasicBlock *Block) { |
| 1202 | assert(Block && "resolving a null target block"); |
| 1203 | if (!EHStack.getNumBranchFixups()) return; |
| 1204 | |
| 1205 | assert(EHStack.hasNormalCleanups() && |
| 1206 | "branch fixups exist with no normal cleanups on stack"); |
| 1207 | |
| 1208 | llvm::SmallPtrSet<llvm::BasicBlock*, 4> ModifiedOptimisticBlocks; |
| 1209 | bool ResolvedAny = false; |
| 1210 | |
| 1211 | for (unsigned I = 0, E = EHStack.getNumBranchFixups(); I != E; ++I) { |
| 1212 | // Skip this fixup if its destination doesn't match. |
| 1213 | BranchFixup &Fixup = EHStack.getBranchFixup(I); |
| 1214 | if (Fixup.Destination != Block) continue; |
| 1215 | |
| 1216 | Fixup.Destination = 0; |
| 1217 | ResolvedAny = true; |
| 1218 | |
| 1219 | // If it doesn't have an optimistic branch block, LatestBranch is |
| 1220 | // already pointing to the right place. |
| 1221 | llvm::BasicBlock *BranchBB = Fixup.OptimisticBranchBlock; |
| 1222 | if (!BranchBB) |
| 1223 | continue; |
| 1224 | |
| 1225 | // Don't process the same optimistic branch block twice. |
| 1226 | if (!ModifiedOptimisticBlocks.insert(BranchBB)) |
| 1227 | continue; |
| 1228 | |
| 1229 | llvm::SwitchInst *Switch = TransitionToCleanupSwitch(*this, BranchBB); |
| 1230 | |
| 1231 | // Add a case to the switch. |
| 1232 | Switch->addCase(Builder.getInt32(Fixup.DestinationIndex), Block); |
| 1233 | } |
| 1234 | |
| 1235 | if (ResolvedAny) |
| 1236 | EHStack.popNullFixups(); |
| 1237 | } |
| 1238 | |
| 1239 | llvm::Value *CodeGenFunction::getNormalCleanupDestSlot() { |
| 1240 | if (!NormalCleanupDest) |
| 1241 | NormalCleanupDest = |
| 1242 | CreateTempAlloca(Builder.getInt32Ty(), "cleanup.dest.slot"); |
| 1243 | return NormalCleanupDest; |
| 1244 | } |
| 1245 | |
| 1246 | llvm::Value *CodeGenFunction::getEHCleanupDestSlot() { |
| 1247 | if (!EHCleanupDest) |
| 1248 | EHCleanupDest = |
| 1249 | CreateTempAlloca(Builder.getInt32Ty(), "eh.cleanup.dest.slot"); |
| 1250 | return EHCleanupDest; |
| 1251 | } |