Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-function state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 17 | #include "CGException.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 19 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 31 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 32 | Target(CGM.getContext().Target), |
Owen Anderson | aac8705 | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 33 | Builder(cgm.getModule().getContext()), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 34 | ExceptionSlot(0), DebugInfo(0), IndirectBranch(0), |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 35 | SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 36 | DidCallStackSave(false), UnreachableBlock(0), |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 37 | CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0), |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 38 | ConditionalBranchLevel(0), TerminateLandingPad(0), TerminateHandler(0), |
| 39 | TrapBB(0) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | // Get some frequently used types. |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 42 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 43 | llvm::LLVMContext &LLVMContext = CGM.getLLVMContext(); |
| 44 | IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth); |
| 45 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 46 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
| 47 | |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 48 | Exceptions = getContext().getLangOptions().Exceptions; |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 49 | CatchUndefined = getContext().getLangOptions().CatchUndefined; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 50 | CGM.getMangleContext().startNewFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 51 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | |
| 53 | ASTContext &CodeGenFunction::getContext() const { |
| 54 | return CGM.getContext(); |
| 55 | } |
| 56 | |
| 57 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 58 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { |
| 59 | llvm::Value *Res = LocalDeclMap[VD]; |
| 60 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 61 | return Res; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 62 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 64 | llvm::Constant * |
| 65 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 66 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 69 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 70 | return CGM.getTypes().ConvertTypeForMem(T); |
| 71 | } |
| 72 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 74 | return CGM.getTypes().ConvertType(T); |
| 75 | } |
| 76 | |
| 77 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | e9d34dc | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 78 | return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || |
| 79 | T->isMemberFunctionPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 82 | void CodeGenFunction::EmitReturnBlock() { |
| 83 | // For cleanliness, we try to avoid emitting the return block for |
| 84 | // simple cases. |
| 85 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 86 | |
| 87 | if (CurBB) { |
| 88 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 89 | |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 90 | // We have a valid insert point, reuse it if it is empty or there are no |
| 91 | // explicit jumps to the return block. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 92 | if (CurBB->empty() || ReturnBlock.Block->use_empty()) { |
| 93 | ReturnBlock.Block->replaceAllUsesWith(CurBB); |
| 94 | delete ReturnBlock.Block; |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 95 | } else |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 96 | EmitBlock(ReturnBlock.Block); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Otherwise, if the return block is the target of a single direct |
| 101 | // branch then we can just put the code in that block instead. This |
| 102 | // cleans up functions which started with a unified return block. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 103 | if (ReturnBlock.Block->hasOneUse()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | llvm::BranchInst *BI = |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 105 | dyn_cast<llvm::BranchInst>(*ReturnBlock.Block->use_begin()); |
| 106 | if (BI && BI->isUnconditional() && |
| 107 | BI->getSuccessor(0) == ReturnBlock.Block) { |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 108 | // Reset insertion point and delete the branch. |
| 109 | Builder.SetInsertPoint(BI->getParent()); |
| 110 | BI->eraseFromParent(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 111 | delete ReturnBlock.Block; |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 116 | // FIXME: We are at an unreachable point, there is no reason to emit the block |
| 117 | // unless it has uses. However, we still need a place to put the debug |
| 118 | // region.end for now. |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 119 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 120 | EmitBlock(ReturnBlock.Block); |
| 121 | } |
| 122 | |
| 123 | static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { |
| 124 | if (!BB) return; |
| 125 | if (!BB->use_empty()) |
| 126 | return CGF.CurFn->getBasicBlockList().push_back(BB); |
| 127 | delete BB; |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 130 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 131 | assert(BreakContinueStack.empty() && |
| 132 | "mismatched push/pop in break/continue stack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
| 134 | // Emit function epilog (to return). |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 135 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 137 | EmitFunctionInstrumentation("__cyg_profile_func_exit"); |
| 138 | |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 139 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 140 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 141 | DI->setLocation(EndLoc); |
| 142 | DI->EmitRegionEnd(CurFn, Builder); |
| 143 | } |
| 144 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 145 | EmitFunctionEpilog(*CurFnInfo); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 146 | EmitEndEHSpec(CurCodeDecl); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 147 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 148 | assert(EHStack.empty() && |
| 149 | "did not remove all scopes from cleanup stack!"); |
| 150 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 151 | // If someone did an indirect goto, emit the indirect goto block at the end of |
| 152 | // the function. |
| 153 | if (IndirectBranch) { |
| 154 | EmitBlock(IndirectBranch->getParent()); |
| 155 | Builder.ClearInsertionPoint(); |
| 156 | } |
| 157 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 158 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 159 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 160 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 161 | Ptr->eraseFromParent(); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 162 | |
| 163 | // If someone took the address of a label but never did an indirect goto, we |
| 164 | // made a zero entry PHI node, which is illegal, zap it now. |
| 165 | if (IndirectBranch) { |
| 166 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 167 | if (PN->getNumIncomingValues() == 0) { |
| 168 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 169 | PN->eraseFromParent(); |
| 170 | } |
| 171 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 172 | |
| 173 | EmitIfUsed(*this, TerminateLandingPad); |
| 174 | EmitIfUsed(*this, TerminateHandler); |
| 175 | EmitIfUsed(*this, UnreachableBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 178 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 179 | /// instrumented with __cyg_profile_func_* calls |
| 180 | bool CodeGenFunction::ShouldInstrumentFunction() { |
| 181 | if (!CGM.getCodeGenOpts().InstrumentFunctions) |
| 182 | return false; |
| 183 | if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) |
| 184 | return false; |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 189 | /// instrumentation function with the current function and the call site, if |
| 190 | /// function instrumentation is enabled. |
| 191 | void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { |
| 192 | if (!ShouldInstrumentFunction()) |
| 193 | return; |
| 194 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 195 | const llvm::PointerType *PointerTy; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 196 | const llvm::FunctionType *FunctionTy; |
| 197 | std::vector<const llvm::Type*> ProfileFuncArgs; |
| 198 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 199 | // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); |
| 200 | PointerTy = llvm::Type::getInt8PtrTy(VMContext); |
| 201 | ProfileFuncArgs.push_back(PointerTy); |
| 202 | ProfileFuncArgs.push_back(PointerTy); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 203 | FunctionTy = llvm::FunctionType::get( |
| 204 | llvm::Type::getVoidTy(VMContext), |
| 205 | ProfileFuncArgs, false); |
| 206 | |
| 207 | llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); |
| 208 | llvm::CallInst *CallSite = Builder.CreateCall( |
| 209 | CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 210 | llvm::ConstantInt::get(Int32Ty, 0), |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 211 | "callsite"); |
| 212 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 213 | Builder.CreateCall2(F, |
| 214 | llvm::ConstantExpr::getBitCast(CurFn, PointerTy), |
| 215 | CallSite); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 218 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 219 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 220 | const FunctionArgList &Args, |
| 221 | SourceLocation StartLoc) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 222 | const Decl *D = GD.getDecl(); |
| 223 | |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 224 | DidCallStackSave = false; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 225 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 226 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 227 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 228 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 229 | |
Jakob Stoklund Olesen | a3fe284 | 2010-02-09 00:10:00 +0000 | [diff] [blame] | 230 | // Pass inline keyword to optimizer if it appears explicitly on any |
| 231 | // declaration. |
| 232 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 233 | for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 234 | RE = FD->redecls_end(); RI != RE; ++RI) |
| 235 | if (RI->isInlineSpecified()) { |
| 236 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 237 | break; |
| 238 | } |
| 239 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 240 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 242 | // Create a marker to make it easy to insert allocas into the entryblock |
| 243 | // later. Don't create this with the builder, because we don't want it |
| 244 | // folded. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 245 | llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); |
| 246 | AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 247 | if (Builder.isNamePreserving()) |
| 248 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 250 | ReturnBlock = getJumpDestInCurrentScope("return"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 252 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 254 | QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0, |
| 255 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 256 | /*FIXME?*/ |
| 257 | FunctionType::ExtInfo()); |
Mike Stump | 91cc815 | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 258 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 259 | // Emit subprogram debug descriptor. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 260 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 261 | DI->setLocation(StartLoc); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 262 | DI->EmitFunctionStart(GD, FnType, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 265 | EmitFunctionInstrumentation("__cyg_profile_func_enter"); |
| 266 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 267 | // FIXME: Leaked. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 268 | // CC info is ignored, hopefully? |
| 269 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 270 | FunctionType::ExtInfo()); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 271 | |
| 272 | if (RetTy->isVoidType()) { |
| 273 | // Void type; nothing to return. |
| 274 | ReturnValue = 0; |
| 275 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
| 276 | hasAggregateLLVMType(CurFnInfo->getReturnType())) { |
| 277 | // Indirect aggregate return; emit returned value directly into sret slot. |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 278 | // This reduces code size, and affects correctness in C++. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 279 | ReturnValue = CurFn->arg_begin(); |
| 280 | } else { |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 281 | ReturnValue = CreateIRTemp(RetTy, "retval"); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 284 | EmitStartEHSpec(CurCodeDecl); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 285 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 287 | if (CXXThisDecl) |
| 288 | CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 289 | if (CXXVTTDecl) |
| 290 | CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt"); |
| 291 | |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 292 | // If any of the arguments have a variably modified type, make sure to |
| 293 | // emit the type size. |
| 294 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 295 | i != e; ++i) { |
| 296 | QualType Ty = i->second; |
| 297 | |
| 298 | if (Ty->isVariablyModifiedType()) |
| 299 | EmitVLASize(Ty); |
| 300 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 301 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 302 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 303 | void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { |
| 304 | const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 305 | assert(FD->getBody()); |
| 306 | EmitStmt(FD->getBody()); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 309 | void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 310 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 311 | |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 312 | // Check if we should generate debug info for this function. |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 313 | if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 314 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 316 | FunctionArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 318 | CurGD = GD; |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 319 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 320 | if (MD->isInstance()) { |
| 321 | // Create the implicit 'this' decl. |
| 322 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 323 | // generation. Maybe we can come up with a better way? |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 324 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, |
| 325 | FD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | &getContext().Idents.get("this"), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 327 | MD->getThisType(getContext())); |
| 328 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 329 | |
| 330 | // Check if we need a VTT parameter as well. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 331 | if (CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 332 | // FIXME: The comment about using a fake decl above applies here too. |
| 333 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 334 | CXXVTTDecl = |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 335 | ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(), |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 336 | &getContext().Idents.get("vtt"), T); |
| 337 | Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType())); |
| 338 | } |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 342 | if (FD->getNumParams()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 343 | const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 344 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 345 | |
| 346 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 348 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 349 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 350 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 351 | SourceRange BodyRange; |
| 352 | if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 353 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 354 | // Emit the standard function prologue. |
| 355 | StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin()); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 356 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 357 | // Generate the body of the function. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 358 | if (isa<CXXDestructorDecl>(FD)) |
| 359 | EmitDestructorBody(Args); |
| 360 | else if (isa<CXXConstructorDecl>(FD)) |
| 361 | EmitConstructorBody(Args); |
| 362 | else |
| 363 | EmitFunctionBody(Args); |
Anders Carlsson | 1851a12 | 2010-02-07 19:45:40 +0000 | [diff] [blame] | 364 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 365 | // Emit the standard function epilogue. |
| 366 | FinishFunction(BodyRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 368 | // Destroy the 'this' declaration. |
| 369 | if (CXXThisDecl) |
| 370 | CXXThisDecl->Destroy(getContext()); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 371 | |
| 372 | // Destroy the VTT declaration. |
| 373 | if (CXXVTTDecl) |
| 374 | CXXVTTDecl->Destroy(getContext()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 377 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 378 | /// this statement is not executed normally, it not containing a label means |
| 379 | /// that we can just remove the code. |
| 380 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 381 | // Null statement, not a label! |
| 382 | if (S == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 384 | // If this is a label, we have to emit the code, consider something like: |
| 385 | // if (0) { ... foo: bar(); } goto foo; |
| 386 | if (isa<LabelStmt>(S)) |
| 387 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 389 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 390 | // to emit the code. |
| 391 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 392 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 394 | // If this is a switch statement, we want to ignore cases below it. |
| 395 | if (isa<SwitchStmt>(S)) |
| 396 | IgnoreCaseStmts = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 398 | // Scan subexpressions for verboten labels. |
| 399 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 400 | I != E; ++I) |
| 401 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 402 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 407 | |
| 408 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 409 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 410 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 411 | /// to 'false' and does not contain a label, return -1. |
| 412 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 413 | // FIXME: Rename and handle conversion of other evaluatable things |
| 414 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 415 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 417 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 418 | return 0; // Not foldable, not integer or not fully evaluatable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 420 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 421 | return 0; // Contains a label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 423 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 428 | /// statement) to the specified blocks. Based on the condition, this might try |
| 429 | /// to simplify the codegen of the conditional based on the branch. |
| 430 | /// |
| 431 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 432 | llvm::BasicBlock *TrueBlock, |
| 433 | llvm::BasicBlock *FalseBlock) { |
| 434 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 435 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 437 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 438 | // Handle X && Y in a condition. |
| 439 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 440 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 441 | // folded if the case was simple enough. |
| 442 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 443 | // br(1 && X) -> br(X). |
| 444 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 447 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 448 | // "X && 0" would have been constant folded to 0. |
| 449 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 450 | // br(X && 1) -> br(X). |
| 451 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 454 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 455 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 456 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 457 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 458 | EmitBlock(LHSTrue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 460 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 461 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 462 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 463 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 465 | return; |
| 466 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 467 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 468 | // folded if the case was simple enough. |
| 469 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 470 | // br(0 || X) -> br(X). |
| 471 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 474 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 475 | // "X || 1" would have been constant folded to 1. |
| 476 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 477 | // br(X || 0) -> br(X). |
| 478 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 481 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 482 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 483 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 484 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 485 | EmitBlock(LHSFalse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 487 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 488 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 489 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 490 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 492 | return; |
| 493 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 496 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 497 | // br(!x, t, f) -> br(x, f, t) |
| 498 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 499 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 502 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 503 | // Handle ?: operator. |
| 504 | |
| 505 | // Just ignore GNU ?: extension. |
| 506 | if (CondOp->getLHS()) { |
| 507 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 508 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 509 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 510 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 511 | EmitBlock(LHSBlock); |
| 512 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 513 | EmitBlock(RHSBlock); |
| 514 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 515 | return; |
| 516 | } |
| 517 | } |
| 518 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 519 | // Emit the code with the fully general case. |
| 520 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 521 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 522 | } |
| 523 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 524 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 525 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 526 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 527 | bool OmitOnError) { |
| 528 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 531 | void |
| 532 | CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { |
| 533 | // If the type contains a pointer to data member we can't memset it to zero. |
| 534 | // Instead, create a null constant and copy it to the destination. |
| 535 | if (CGM.getTypes().ContainsPointerToDataMember(Ty)) { |
| 536 | llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); |
| 537 | |
| 538 | llvm::GlobalVariable *NullVariable = |
| 539 | new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), |
| 540 | /*isConstant=*/true, |
| 541 | llvm::GlobalVariable::PrivateLinkage, |
| 542 | NullConstant, llvm::Twine()); |
| 543 | EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 548 | // Ignore empty classes in C++. |
| 549 | if (getContext().getLangOptions().CPlusPlus) { |
| 550 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 551 | if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) |
| 552 | return; |
| 553 | } |
| 554 | } |
| 555 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 556 | // Otherwise, just memset the whole thing to zero. This is legal |
| 557 | // because in LLVM, all default initializers (other than the ones we just |
| 558 | // handled above) are guaranteed to have a bit pattern of all zeros. |
Chris Lattner | 36afd38 | 2009-10-13 06:02:42 +0000 | [diff] [blame] | 559 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 560 | if (DestPtr->getType() != BP) |
| 561 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 562 | |
| 563 | // Get size and alignment info for this aggregate. |
| 564 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 565 | |
Chris Lattner | 88207c9 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 566 | // Don't bother emitting a zero-byte memset. |
| 567 | if (TypeInfo.first == 0) |
| 568 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 570 | // FIXME: Handle variable sized types. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 571 | Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 572 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 573 | // TypeInfo.first describes size in bits. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 574 | llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), |
| 575 | llvm::ConstantInt::get(Int32Ty, TypeInfo.second/8), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 576 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), |
| 577 | 0)); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 580 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { |
| 581 | // Make sure that there is a block for the indirect goto. |
| 582 | if (IndirectBranch == 0) |
| 583 | GetIndirectGotoBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 584 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 585 | llvm::BasicBlock *BB = getJumpDestForLabel(L).Block; |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 586 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 587 | // Make sure the indirect branch includes all of the address-taken blocks. |
| 588 | IndirectBranch->addDestination(BB); |
| 589 | return llvm::BlockAddress::get(CurFn, BB); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 593 | // If we already made the indirect branch for indirect goto, return its block. |
| 594 | if (IndirectBranch) return IndirectBranch->getParent(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 595 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 596 | CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); |
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 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 85e74ac | 2009-10-28 20:36:47 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 600 | // Create the PHI node that indirect gotos will add entries to. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 601 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 602 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 603 | // Create the indirect branch instruction. |
| 604 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 605 | return IndirectBranch->getParent(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 606 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 607 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 608 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 609 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 611 | assert(SizeEntry && "Did not emit size for type"); |
| 612 | return SizeEntry; |
| 613 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 615 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 616 | assert(Ty->isVariablyModifiedType() && |
| 617 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 619 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 621 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 622 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 624 | if (!SizeEntry) { |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 625 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 627 | // Get the element size; |
| 628 | QualType ElemTy = VAT->getElementType(); |
| 629 | llvm::Value *ElemSize; |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 630 | if (ElemTy->isVariableArrayType()) |
| 631 | ElemSize = EmitVLASize(ElemTy); |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 632 | else |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 633 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 634 | getContext().getTypeSizeInChars(ElemTy).getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 636 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 637 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 639 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 642 | return SizeEntry; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 643 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 645 | if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 646 | EmitVLASize(AT->getElementType()); |
| 647 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 650 | const PointerType *PT = Ty->getAs<PointerType>(); |
| 651 | assert(PT && "unknown VM type!"); |
| 652 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 653 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 654 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 655 | |
| 656 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
Chris Lattner | fbe02ff | 2010-06-27 07:40:06 +0000 | [diff] [blame] | 657 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 658 | return EmitScalarExpr(E); |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 659 | return EmitLValue(E).getAddress(); |
| 660 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 661 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 662 | /// Pops cleanup blocks until the given savepoint is reached. |
| 663 | void CodeGenFunction::PopCleanupBlocks(EHScopeStack::stable_iterator Old) { |
| 664 | assert(Old.isValid()); |
| 665 | |
| 666 | EHScopeStack::iterator E = EHStack.find(Old); |
| 667 | while (EHStack.begin() != E) |
| 668 | PopCleanupBlock(); |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 669 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 670 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 671 | /// Destroys a cleanup if it was unused. |
| 672 | static void DestroyCleanup(CodeGenFunction &CGF, |
| 673 | llvm::BasicBlock *Entry, |
| 674 | llvm::BasicBlock *Exit) { |
| 675 | assert(Entry->use_empty() && "destroying cleanup with uses!"); |
| 676 | assert(Exit->getTerminator() == 0 && |
| 677 | "exit has terminator but entry has no predecessors!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 679 | // This doesn't always remove the entire cleanup, but it's much |
| 680 | // safer as long as we don't know what blocks belong to the cleanup. |
| 681 | // A *much* better approach if we care about this inefficiency would |
| 682 | // be to lazily emit the cleanup. |
| 683 | |
| 684 | // If the exit block is distinct from the entry, give it a branch to |
| 685 | // an unreachable destination. This preserves the well-formedness |
| 686 | // of the IR. |
| 687 | if (Entry != Exit) |
| 688 | llvm::BranchInst::Create(CGF.getUnreachableBlock(), Exit); |
| 689 | |
| 690 | assert(!Entry->getParent() && "cleanup entry already positioned?"); |
| 691 | delete Entry; |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 692 | } |
| 693 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 694 | /// Creates a switch instruction to thread branches out of the given |
| 695 | /// block (which is the exit block of a cleanup). |
| 696 | static void CreateCleanupSwitch(CodeGenFunction &CGF, |
| 697 | llvm::BasicBlock *Block) { |
| 698 | if (Block->getTerminator()) { |
| 699 | assert(isa<llvm::SwitchInst>(Block->getTerminator()) && |
| 700 | "cleanup block already has a terminator, but it isn't a switch"); |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 701 | return; |
| 702 | } |
| 703 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 704 | llvm::Value *DestCodePtr |
| 705 | = CGF.CreateTempAlloca(CGF.Builder.getInt32Ty(), "cleanup.dst"); |
| 706 | CGBuilderTy Builder(Block); |
| 707 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
Devang Patel | cd9199e | 2010-04-13 00:08:43 +0000 | [diff] [blame] | 708 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 709 | // Create a switch instruction to determine where to jump next. |
| 710 | Builder.CreateSwitch(DestCode, CGF.getUnreachableBlock()); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 711 | } |
| 712 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 713 | /// Attempts to reduce a cleanup's entry block to a fallthrough. This |
| 714 | /// is basically llvm::MergeBlockIntoPredecessor, except |
| 715 | /// simplified/optimized for the tighter constraints on cleanup |
| 716 | /// blocks. |
| 717 | static void SimplifyCleanupEntry(CodeGenFunction &CGF, |
| 718 | llvm::BasicBlock *Entry) { |
| 719 | llvm::BasicBlock *Pred = Entry->getSinglePredecessor(); |
| 720 | if (!Pred) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 722 | llvm::BranchInst *Br = dyn_cast<llvm::BranchInst>(Pred->getTerminator()); |
| 723 | if (!Br || Br->isConditional()) return; |
| 724 | assert(Br->getSuccessor(0) == Entry); |
| 725 | |
| 726 | // If we were previously inserting at the end of the cleanup entry |
| 727 | // block, we'll need to continue inserting at the end of the |
| 728 | // predecessor. |
| 729 | bool WasInsertBlock = CGF.Builder.GetInsertBlock() == Entry; |
| 730 | assert(!WasInsertBlock || CGF.Builder.GetInsertPoint() == Entry->end()); |
| 731 | |
| 732 | // Kill the branch. |
| 733 | Br->eraseFromParent(); |
| 734 | |
| 735 | // Merge the blocks. |
| 736 | Pred->getInstList().splice(Pred->end(), Entry->getInstList()); |
| 737 | |
| 738 | // Kill the entry block. |
| 739 | Entry->eraseFromParent(); |
| 740 | |
| 741 | if (WasInsertBlock) |
| 742 | CGF.Builder.SetInsertPoint(Pred); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 743 | } |
| 744 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 745 | /// Attempts to reduce an cleanup's exit switch to an unconditional |
| 746 | /// branch. |
| 747 | static void SimplifyCleanupExit(llvm::BasicBlock *Exit) { |
| 748 | llvm::TerminatorInst *Terminator = Exit->getTerminator(); |
| 749 | assert(Terminator && "completed cleanup exit has no terminator"); |
| 750 | |
| 751 | llvm::SwitchInst *Switch = dyn_cast<llvm::SwitchInst>(Terminator); |
| 752 | if (!Switch) return; |
| 753 | if (Switch->getNumCases() != 2) return; // default + 1 |
| 754 | |
| 755 | llvm::LoadInst *Cond = cast<llvm::LoadInst>(Switch->getCondition()); |
| 756 | llvm::AllocaInst *CondVar = cast<llvm::AllocaInst>(Cond->getPointerOperand()); |
| 757 | |
| 758 | // Replace the switch instruction with an unconditional branch. |
| 759 | llvm::BasicBlock *Dest = Switch->getSuccessor(1); // default is 0 |
| 760 | Switch->eraseFromParent(); |
| 761 | llvm::BranchInst::Create(Dest, Exit); |
| 762 | |
| 763 | // Delete all uses of the condition variable. |
| 764 | Cond->eraseFromParent(); |
| 765 | while (!CondVar->use_empty()) |
| 766 | cast<llvm::StoreInst>(*CondVar->use_begin())->eraseFromParent(); |
| 767 | |
| 768 | // Delete the condition variable itself. |
| 769 | CondVar->eraseFromParent(); |
| 770 | } |
| 771 | |
| 772 | /// Threads a branch fixup through a cleanup block. |
| 773 | static void ThreadFixupThroughCleanup(CodeGenFunction &CGF, |
| 774 | BranchFixup &Fixup, |
| 775 | llvm::BasicBlock *Entry, |
| 776 | llvm::BasicBlock *Exit) { |
| 777 | if (!Exit->getTerminator()) |
| 778 | CreateCleanupSwitch(CGF, Exit); |
| 779 | |
| 780 | // Find the switch and its destination index alloca. |
| 781 | llvm::SwitchInst *Switch = cast<llvm::SwitchInst>(Exit->getTerminator()); |
| 782 | llvm::Value *DestCodePtr = |
| 783 | cast<llvm::LoadInst>(Switch->getCondition())->getPointerOperand(); |
| 784 | |
| 785 | // Compute the index of the new case we're adding to the switch. |
| 786 | unsigned Index = Switch->getNumCases(); |
| 787 | |
| 788 | const llvm::IntegerType *i32 = llvm::Type::getInt32Ty(CGF.getLLVMContext()); |
| 789 | llvm::ConstantInt *IndexV = llvm::ConstantInt::get(i32, Index); |
| 790 | |
| 791 | // Set the index in the origin block. |
| 792 | new llvm::StoreInst(IndexV, DestCodePtr, Fixup.Origin); |
| 793 | |
| 794 | // Add a case to the switch. |
| 795 | Switch->addCase(IndexV, Fixup.Destination); |
| 796 | |
| 797 | // Change the last branch to point to the cleanup entry block. |
| 798 | Fixup.LatestBranch->setSuccessor(Fixup.LatestBranchIndex, Entry); |
| 799 | |
| 800 | // And finally, update the fixup. |
| 801 | Fixup.LatestBranch = Switch; |
| 802 | Fixup.LatestBranchIndex = Index; |
| 803 | } |
| 804 | |
| 805 | /// Try to simplify both the entry and exit edges of a cleanup. |
| 806 | static void SimplifyCleanupEdges(CodeGenFunction &CGF, |
| 807 | llvm::BasicBlock *Entry, |
| 808 | llvm::BasicBlock *Exit) { |
| 809 | |
| 810 | // Given their current implementations, it's important to run these |
| 811 | // in this order: SimplifyCleanupEntry will delete Entry if it can |
| 812 | // be merged into its predecessor, which will then break |
| 813 | // SimplifyCleanupExit if (as is common) Entry == Exit. |
| 814 | |
| 815 | SimplifyCleanupExit(Exit); |
| 816 | SimplifyCleanupEntry(CGF, Entry); |
| 817 | } |
| 818 | |
| 819 | /// Pops a cleanup block. If the block includes a normal cleanup, the |
| 820 | /// current insertion point is threaded through the cleanup, as are |
| 821 | /// any branch fixups on the cleanup. |
| 822 | void CodeGenFunction::PopCleanupBlock() { |
| 823 | assert(!EHStack.empty() && "cleanup stack is empty!"); |
| 824 | assert(isa<EHCleanupScope>(*EHStack.begin()) && "top not a cleanup!"); |
| 825 | EHCleanupScope &Scope = cast<EHCleanupScope>(*EHStack.begin()); |
| 826 | assert(Scope.getFixupDepth() <= EHStack.getNumBranchFixups()); |
| 827 | |
| 828 | // Handle the EH cleanup if (1) there is one and (2) it's different |
| 829 | // from the normal cleanup. |
| 830 | if (Scope.isEHCleanup() && |
| 831 | Scope.getEHEntry() != Scope.getNormalEntry()) { |
| 832 | llvm::BasicBlock *EHEntry = Scope.getEHEntry(); |
| 833 | llvm::BasicBlock *EHExit = Scope.getEHExit(); |
| 834 | |
| 835 | if (EHEntry->use_empty()) { |
| 836 | DestroyCleanup(*this, EHEntry, EHExit); |
| 837 | } else { |
| 838 | // TODO: this isn't really the ideal location to put this EH |
| 839 | // cleanup, but lazy emission is a better solution than trying |
| 840 | // to pick a better spot. |
| 841 | CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP(); |
| 842 | EmitBlock(EHEntry); |
| 843 | Builder.restoreIP(SavedIP); |
| 844 | |
| 845 | SimplifyCleanupEdges(*this, EHEntry, EHExit); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | // If we only have an EH cleanup, we don't really need to do much |
| 850 | // here. Branch fixups just naturally drop down to the enclosing |
| 851 | // cleanup scope. |
| 852 | if (!Scope.isNormalCleanup()) { |
| 853 | EHStack.popCleanup(); |
| 854 | assert(EHStack.getNumBranchFixups() == 0 || EHStack.hasNormalCleanups()); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | // Check whether the scope has any fixups that need to be threaded. |
| 859 | unsigned FixupDepth = Scope.getFixupDepth(); |
| 860 | bool HasFixups = EHStack.getNumBranchFixups() != FixupDepth; |
| 861 | |
| 862 | // Grab the entry and exit blocks. |
| 863 | llvm::BasicBlock *Entry = Scope.getNormalEntry(); |
| 864 | llvm::BasicBlock *Exit = Scope.getNormalExit(); |
| 865 | |
| 866 | // Check whether anything's been threaded through the cleanup already. |
| 867 | assert((Exit->getTerminator() == 0) == Entry->use_empty() && |
| 868 | "cleanup entry/exit mismatch"); |
| 869 | bool HasExistingBranches = !Entry->use_empty(); |
| 870 | |
| 871 | // Check whether we need to emit a "fallthrough" branch through the |
| 872 | // cleanup for the current insertion point. |
| 873 | llvm::BasicBlock *FallThrough = Builder.GetInsertBlock(); |
| 874 | if (FallThrough && FallThrough->getTerminator()) |
| 875 | FallThrough = 0; |
| 876 | |
| 877 | // If *nothing* is using the cleanup, kill it. |
| 878 | if (!FallThrough && !HasFixups && !HasExistingBranches) { |
| 879 | EHStack.popCleanup(); |
| 880 | DestroyCleanup(*this, Entry, Exit); |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | // Otherwise, add the block to the function. |
| 885 | EmitBlock(Entry); |
| 886 | |
| 887 | if (FallThrough) |
| 888 | Builder.SetInsertPoint(Exit); |
| 889 | else |
| 890 | Builder.ClearInsertionPoint(); |
| 891 | |
| 892 | // Fast case: if we don't have to add any fixups, and either |
| 893 | // we don't have a fallthrough or the cleanup wasn't previously |
| 894 | // used, then the setup above is sufficient. |
| 895 | if (!HasFixups) { |
| 896 | if (!FallThrough) { |
| 897 | assert(HasExistingBranches && "no reason for cleanup but didn't kill before"); |
| 898 | EHStack.popCleanup(); |
| 899 | SimplifyCleanupEdges(*this, Entry, Exit); |
| 900 | return; |
| 901 | } else if (!HasExistingBranches) { |
| 902 | assert(FallThrough && "no reason for cleanup but didn't kill before"); |
| 903 | // We can't simplify the exit edge in this case because we're |
| 904 | // already inserting at the end of the exit block. |
| 905 | EHStack.popCleanup(); |
| 906 | SimplifyCleanupEntry(*this, Entry); |
| 907 | return; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // Otherwise we're going to have to thread things through the cleanup. |
| 912 | llvm::SmallVector<BranchFixup*, 8> Fixups; |
| 913 | |
| 914 | // Synthesize a fixup for the current insertion point. |
| 915 | BranchFixup Cur; |
| 916 | if (FallThrough) { |
| 917 | Cur.Destination = createBasicBlock("cleanup.cont"); |
| 918 | Cur.LatestBranch = FallThrough->getTerminator(); |
| 919 | Cur.LatestBranchIndex = 0; |
| 920 | Cur.Origin = Cur.LatestBranch; |
| 921 | |
| 922 | // Restore fixup invariant. EmitBlock added a branch to the cleanup |
| 923 | // which we need to redirect to the destination. |
| 924 | cast<llvm::BranchInst>(Cur.LatestBranch)->setSuccessor(0, Cur.Destination); |
| 925 | |
| 926 | Fixups.push_back(&Cur); |
| 927 | } else { |
| 928 | Cur.Destination = 0; |
| 929 | } |
| 930 | |
| 931 | // Collect any "real" fixups we need to thread. |
| 932 | for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups(); |
| 933 | I != E; ++I) |
| 934 | if (EHStack.getBranchFixup(I).Destination) |
| 935 | Fixups.push_back(&EHStack.getBranchFixup(I)); |
| 936 | |
| 937 | assert(!Fixups.empty() && "no fixups, invariants broken!"); |
| 938 | |
| 939 | // If there's only a single fixup to thread through, do so with |
| 940 | // unconditional branches. This only happens if there's a single |
| 941 | // branch and no fallthrough. |
| 942 | if (Fixups.size() == 1 && !HasExistingBranches) { |
| 943 | Fixups[0]->LatestBranch->setSuccessor(Fixups[0]->LatestBranchIndex, Entry); |
| 944 | llvm::BranchInst *Br = |
| 945 | llvm::BranchInst::Create(Fixups[0]->Destination, Exit); |
| 946 | Fixups[0]->LatestBranch = Br; |
| 947 | Fixups[0]->LatestBranchIndex = 0; |
| 948 | |
| 949 | // Otherwise, force a switch statement and thread everything through |
| 950 | // the switch. |
| 951 | } else { |
| 952 | CreateCleanupSwitch(*this, Exit); |
| 953 | for (unsigned I = 0, E = Fixups.size(); I != E; ++I) |
| 954 | ThreadFixupThroughCleanup(*this, *Fixups[I], Entry, Exit); |
| 955 | } |
| 956 | |
| 957 | // Emit the fallthrough destination block if necessary. |
| 958 | if (Cur.Destination) |
| 959 | EmitBlock(Cur.Destination); |
| 960 | |
| 961 | // We're finally done with the cleanup. |
| 962 | EHStack.popCleanup(); |
| 963 | } |
| 964 | |
| 965 | void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 966 | if (!HaveInsertPoint()) |
| 967 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 969 | // Create the branch. |
| 970 | llvm::BranchInst *BI = Builder.CreateBr(Dest.Block); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 972 | // If we're not in a cleanup scope, we don't need to worry about |
| 973 | // fixups. |
| 974 | if (!EHStack.hasNormalCleanups()) { |
| 975 | Builder.ClearInsertionPoint(); |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | // Initialize a fixup. |
| 980 | BranchFixup Fixup; |
| 981 | Fixup.Destination = Dest.Block; |
| 982 | Fixup.Origin = BI; |
| 983 | Fixup.LatestBranch = BI; |
| 984 | Fixup.LatestBranchIndex = 0; |
| 985 | |
| 986 | // If we can't resolve the destination cleanup scope, just add this |
| 987 | // to the current cleanup scope. |
| 988 | if (!Dest.ScopeDepth.isValid()) { |
| 989 | EHStack.addBranchFixup() = Fixup; |
| 990 | Builder.ClearInsertionPoint(); |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | for (EHScopeStack::iterator I = EHStack.begin(), |
| 995 | E = EHStack.find(Dest.ScopeDepth); I != E; ++I) { |
| 996 | if (isa<EHCleanupScope>(*I)) { |
| 997 | EHCleanupScope &Scope = cast<EHCleanupScope>(*I); |
| 998 | if (Scope.isNormalCleanup()) |
| 999 | ThreadFixupThroughCleanup(*this, Fixup, Scope.getNormalEntry(), |
| 1000 | Scope.getNormalExit()); |
| 1001 | } |
| 1002 | } |
| 1003 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 1004 | Builder.ClearInsertionPoint(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1007 | void CodeGenFunction::EmitBranchThroughEHCleanup(JumpDest Dest) { |
| 1008 | if (!HaveInsertPoint()) |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1009 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1011 | // Create the branch. |
| 1012 | llvm::BranchInst *BI = Builder.CreateBr(Dest.Block); |
| 1013 | |
| 1014 | // If we're not in a cleanup scope, we don't need to worry about |
| 1015 | // fixups. |
| 1016 | if (!EHStack.hasEHCleanups()) { |
| 1017 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1018 | return; |
| 1019 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1021 | // Initialize a fixup. |
| 1022 | BranchFixup Fixup; |
| 1023 | Fixup.Destination = Dest.Block; |
| 1024 | Fixup.Origin = BI; |
| 1025 | Fixup.LatestBranch = BI; |
| 1026 | Fixup.LatestBranchIndex = 0; |
| 1027 | |
| 1028 | // We should never get invalid scope depths for these: invalid scope |
| 1029 | // depths only arise for as-yet-unemitted labels, and we can't do an |
| 1030 | // EH-unwind to one of those. |
| 1031 | assert(Dest.ScopeDepth.isValid() && "invalid scope depth on EH dest?"); |
| 1032 | |
| 1033 | for (EHScopeStack::iterator I = EHStack.begin(), |
| 1034 | E = EHStack.find(Dest.ScopeDepth); I != E; ++I) { |
| 1035 | if (isa<EHCleanupScope>(*I)) { |
| 1036 | EHCleanupScope &Scope = cast<EHCleanupScope>(*I); |
| 1037 | if (Scope.isEHCleanup()) |
| 1038 | ThreadFixupThroughCleanup(*this, Fixup, Scope.getEHEntry(), |
| 1039 | Scope.getEHExit()); |
| 1040 | } |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1041 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1042 | |
| 1043 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 1044 | } |