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