Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-function state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 30 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 31 | Target(CGM.getContext().Target), |
Owen Anderson | aac8705 | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 32 | Builder(cgm.getModule().getContext()), |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 33 | DebugInfo(0), IndirectBranch(0), |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 34 | SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 35 | CXXThisDecl(0), CXXThisValue(0), CXXVTTDecl(0), CXXVTTValue(0), |
Anders Carlsson | 5687a5c | 2010-06-08 22:30:17 +0000 | [diff] [blame] | 36 | ConditionalBranchLevel(0), TerminateHandler(0), TrapBB(0) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | // Get some frequently used types. |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 39 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 40 | llvm::LLVMContext &LLVMContext = CGM.getLLVMContext(); |
| 41 | IntPtrTy = llvm::IntegerType::get(LLVMContext, LLVMPointerWidth); |
| 42 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 43 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
| 44 | |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 45 | Exceptions = getContext().getLangOptions().Exceptions; |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 46 | CatchUndefined = getContext().getLangOptions().CatchUndefined; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 47 | CGM.getMangleContext().startNewFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 48 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | |
| 50 | ASTContext &CodeGenFunction::getContext() const { |
| 51 | return CGM.getContext(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 56 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 57 | if (BB) return BB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | // Create, but don't insert, the new block. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 60 | return BB = createBasicBlock(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 63 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { |
| 64 | llvm::Value *Res = LocalDeclMap[VD]; |
| 65 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 66 | return Res; |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 67 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 69 | llvm::Constant * |
| 70 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 71 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 74 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 75 | return CGM.getTypes().ConvertTypeForMem(T); |
| 76 | } |
| 77 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 79 | return CGM.getTypes().ConvertType(T); |
| 80 | } |
| 81 | |
| 82 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | e9d34dc | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 83 | return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || |
| 84 | T->isMemberFunctionPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 87 | void CodeGenFunction::EmitReturnBlock() { |
| 88 | // For cleanliness, we try to avoid emitting the return block for |
| 89 | // simple cases. |
| 90 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 91 | |
| 92 | if (CurBB) { |
| 93 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 94 | |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 95 | // We have a valid insert point, reuse it if it is empty or there are no |
| 96 | // explicit jumps to the return block. |
| 97 | if (CurBB->empty() || ReturnBlock->use_empty()) { |
| 98 | ReturnBlock->replaceAllUsesWith(CurBB); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 99 | delete ReturnBlock; |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 100 | } else |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 101 | EmitBlock(ReturnBlock); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Otherwise, if the return block is the target of a single direct |
| 106 | // branch then we can just put the code in that block instead. This |
| 107 | // cleans up functions which started with a unified return block. |
| 108 | if (ReturnBlock->hasOneUse()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | llvm::BranchInst *BI = |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 110 | dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); |
| 111 | if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { |
| 112 | // Reset insertion point and delete the branch. |
| 113 | Builder.SetInsertPoint(BI->getParent()); |
| 114 | BI->eraseFromParent(); |
| 115 | delete ReturnBlock; |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 120 | // FIXME: We are at an unreachable point, there is no reason to emit the block |
| 121 | // unless it has uses. However, we still need a place to put the debug |
| 122 | // region.end for now. |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 123 | |
| 124 | EmitBlock(ReturnBlock); |
| 125 | } |
| 126 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 127 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 128 | assert(BreakContinueStack.empty() && |
| 129 | "mismatched push/pop in break/continue stack!"); |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 130 | assert(BlockScopes.empty() && |
| 131 | "did not remove all blocks from block scope map!"); |
| 132 | assert(CleanupEntries.empty() && |
| 133 | "mismatched push/pop in cleanup stack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
| 135 | // Emit function epilog (to return). |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 136 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 138 | EmitFunctionInstrumentation("__cyg_profile_func_exit"); |
| 139 | |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 140 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 141 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 142 | DI->setLocation(EndLoc); |
| 143 | DI->EmitRegionEnd(CurFn, Builder); |
| 144 | } |
| 145 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 146 | EmitFunctionEpilog(*CurFnInfo); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 147 | EmitEndEHSpec(CurCodeDecl); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 148 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 149 | // If someone did an indirect goto, emit the indirect goto block at the end of |
| 150 | // the function. |
| 151 | if (IndirectBranch) { |
| 152 | EmitBlock(IndirectBranch->getParent()); |
| 153 | Builder.ClearInsertionPoint(); |
| 154 | } |
| 155 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 156 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 157 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 158 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 159 | Ptr->eraseFromParent(); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 160 | |
| 161 | // If someone took the address of a label but never did an indirect goto, we |
| 162 | // made a zero entry PHI node, which is illegal, zap it now. |
| 163 | if (IndirectBranch) { |
| 164 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 165 | if (PN->getNumIncomingValues() == 0) { |
| 166 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 167 | PN->eraseFromParent(); |
| 168 | } |
| 169 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 172 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 173 | /// instrumented with __cyg_profile_func_* calls |
| 174 | bool CodeGenFunction::ShouldInstrumentFunction() { |
| 175 | if (!CGM.getCodeGenOpts().InstrumentFunctions) |
| 176 | return false; |
| 177 | if (CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) |
| 178 | return false; |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 183 | /// instrumentation function with the current function and the call site, if |
| 184 | /// function instrumentation is enabled. |
| 185 | void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { |
| 186 | if (!ShouldInstrumentFunction()) |
| 187 | return; |
| 188 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 189 | const llvm::PointerType *PointerTy; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 190 | const llvm::FunctionType *FunctionTy; |
| 191 | std::vector<const llvm::Type*> ProfileFuncArgs; |
| 192 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 193 | // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); |
| 194 | PointerTy = llvm::Type::getInt8PtrTy(VMContext); |
| 195 | ProfileFuncArgs.push_back(PointerTy); |
| 196 | ProfileFuncArgs.push_back(PointerTy); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 197 | FunctionTy = llvm::FunctionType::get( |
| 198 | llvm::Type::getVoidTy(VMContext), |
| 199 | ProfileFuncArgs, false); |
| 200 | |
| 201 | llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); |
| 202 | llvm::CallInst *CallSite = Builder.CreateCall( |
| 203 | CGM.getIntrinsic(llvm::Intrinsic::returnaddress, 0, 0), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 204 | llvm::ConstantInt::get(Int32Ty, 0), |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 205 | "callsite"); |
| 206 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 207 | Builder.CreateCall2(F, |
| 208 | llvm::ConstantExpr::getBitCast(CurFn, PointerTy), |
| 209 | CallSite); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 212 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 213 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 214 | const FunctionArgList &Args, |
| 215 | SourceLocation StartLoc) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 216 | const Decl *D = GD.getDecl(); |
| 217 | |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 218 | DidCallStackSave = false; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 219 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 220 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 221 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 222 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 223 | |
Jakob Stoklund Olesen | a3fe284 | 2010-02-09 00:10:00 +0000 | [diff] [blame] | 224 | // Pass inline keyword to optimizer if it appears explicitly on any |
| 225 | // declaration. |
| 226 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 227 | for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 228 | RE = FD->redecls_end(); RI != RE; ++RI) |
| 229 | if (RI->isInlineSpecified()) { |
| 230 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 231 | break; |
| 232 | } |
| 233 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 234 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 236 | // Create a marker to make it easy to insert allocas into the entryblock |
| 237 | // later. Don't create this with the builder, because we don't want it |
| 238 | // folded. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 239 | llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); |
| 240 | AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 241 | if (Builder.isNamePreserving()) |
| 242 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 244 | ReturnBlock = createBasicBlock("return"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 246 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 248 | QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0, |
| 249 | false, false, 0, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 250 | /*FIXME?*/ |
| 251 | FunctionType::ExtInfo()); |
Mike Stump | 91cc815 | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 252 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 253 | // Emit subprogram debug descriptor. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 254 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 255 | DI->setLocation(StartLoc); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 256 | DI->EmitFunctionStart(GD, FnType, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 259 | EmitFunctionInstrumentation("__cyg_profile_func_enter"); |
| 260 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 261 | // FIXME: Leaked. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 262 | // CC info is ignored, hopefully? |
| 263 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 264 | FunctionType::ExtInfo()); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 265 | |
| 266 | if (RetTy->isVoidType()) { |
| 267 | // Void type; nothing to return. |
| 268 | ReturnValue = 0; |
| 269 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
| 270 | hasAggregateLLVMType(CurFnInfo->getReturnType())) { |
| 271 | // Indirect aggregate return; emit returned value directly into sret slot. |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 272 | // This reduces code size, and affects correctness in C++. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 273 | ReturnValue = CurFn->arg_begin(); |
| 274 | } else { |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 275 | ReturnValue = CreateIRTemp(RetTy, "retval"); |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 278 | EmitStartEHSpec(CurCodeDecl); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 279 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 281 | if (CXXThisDecl) |
| 282 | CXXThisValue = Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 283 | if (CXXVTTDecl) |
| 284 | CXXVTTValue = Builder.CreateLoad(LocalDeclMap[CXXVTTDecl], "vtt"); |
| 285 | |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 286 | // If any of the arguments have a variably modified type, make sure to |
| 287 | // emit the type size. |
| 288 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 289 | i != e; ++i) { |
| 290 | QualType Ty = i->second; |
| 291 | |
| 292 | if (Ty->isVariablyModifiedType()) |
| 293 | EmitVLASize(Ty); |
| 294 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 295 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 296 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 297 | void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { |
| 298 | const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 299 | assert(FD->getBody()); |
| 300 | EmitStmt(FD->getBody()); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 303 | void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 304 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 305 | |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 306 | // Check if we should generate debug info for this function. |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 307 | if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 308 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 310 | FunctionArgList Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 312 | CurGD = GD; |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 313 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 314 | if (MD->isInstance()) { |
| 315 | // Create the implicit 'this' decl. |
| 316 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 317 | // generation. Maybe we can come up with a better way? |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 318 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, |
| 319 | FD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | &getContext().Idents.get("this"), |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 321 | MD->getThisType(getContext())); |
| 322 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 323 | |
| 324 | // Check if we need a VTT parameter as well. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 325 | if (CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 326 | // FIXME: The comment about using a fake decl above applies here too. |
| 327 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 328 | CXXVTTDecl = |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 329 | ImplicitParamDecl::Create(getContext(), 0, FD->getLocation(), |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 330 | &getContext().Idents.get("vtt"), T); |
| 331 | Args.push_back(std::make_pair(CXXVTTDecl, CXXVTTDecl->getType())); |
| 332 | } |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 336 | if (FD->getNumParams()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 337 | const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 338 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 339 | |
| 340 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 342 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 343 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 344 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 345 | SourceRange BodyRange; |
| 346 | if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); |
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 | // Emit the standard function prologue. |
| 349 | StartFunction(GD, FD->getResultType(), Fn, Args, BodyRange.getBegin()); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 350 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 351 | // Generate the body of the function. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 352 | if (isa<CXXDestructorDecl>(FD)) |
| 353 | EmitDestructorBody(Args); |
| 354 | else if (isa<CXXConstructorDecl>(FD)) |
| 355 | EmitConstructorBody(Args); |
| 356 | else |
| 357 | EmitFunctionBody(Args); |
Anders Carlsson | 1851a12 | 2010-02-07 19:45:40 +0000 | [diff] [blame] | 358 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 359 | // Emit the standard function epilogue. |
| 360 | FinishFunction(BodyRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 362 | // Destroy the 'this' declaration. |
| 363 | if (CXXThisDecl) |
| 364 | CXXThisDecl->Destroy(getContext()); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 365 | |
| 366 | // Destroy the VTT declaration. |
| 367 | if (CXXVTTDecl) |
| 368 | CXXVTTDecl->Destroy(getContext()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 371 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 372 | /// this statement is not executed normally, it not containing a label means |
| 373 | /// that we can just remove the code. |
| 374 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 375 | // Null statement, not a label! |
| 376 | if (S == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 378 | // If this is a label, we have to emit the code, consider something like: |
| 379 | // if (0) { ... foo: bar(); } goto foo; |
| 380 | if (isa<LabelStmt>(S)) |
| 381 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 383 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 384 | // to emit the code. |
| 385 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 386 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 388 | // If this is a switch statement, we want to ignore cases below it. |
| 389 | if (isa<SwitchStmt>(S)) |
| 390 | IgnoreCaseStmts = 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 | // Scan subexpressions for verboten labels. |
| 393 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 394 | I != E; ++I) |
| 395 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 396 | return 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 | return false; |
| 399 | } |
| 400 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 401 | |
| 402 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 403 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 404 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 405 | /// to 'false' and does not contain a label, return -1. |
| 406 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 407 | // FIXME: Rename and handle conversion of other evaluatable things |
| 408 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 409 | Expr::EvalResult Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 411 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 412 | return 0; // Not foldable, not integer or not fully evaluatable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 414 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 415 | return 0; // Contains a label. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 417 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
| 421 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 422 | /// statement) to the specified blocks. Based on the condition, this might try |
| 423 | /// to simplify the codegen of the conditional based on the branch. |
| 424 | /// |
| 425 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 426 | llvm::BasicBlock *TrueBlock, |
| 427 | llvm::BasicBlock *FalseBlock) { |
| 428 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 429 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 431 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 432 | // Handle X && Y in a condition. |
| 433 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 434 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 435 | // folded if the case was simple enough. |
| 436 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 437 | // br(1 && X) -> br(X). |
| 438 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 441 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 442 | // "X && 0" would have been constant folded to 0. |
| 443 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 444 | // br(X && 1) -> br(X). |
| 445 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 448 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 449 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 450 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 451 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 452 | EmitBlock(LHSTrue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 454 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 455 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 456 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 457 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 459 | return; |
| 460 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 461 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 462 | // folded if the case was simple enough. |
| 463 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 464 | // br(0 || X) -> br(X). |
| 465 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 468 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 469 | // "X || 1" would have been constant folded to 1. |
| 470 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 471 | // br(X || 0) -> br(X). |
| 472 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 475 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 476 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 477 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 478 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 479 | EmitBlock(LHSFalse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 481 | // Any temporaries created here are conditional. |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 482 | BeginConditionalBranch(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 483 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 484 | EndConditionalBranch(); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 490 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 491 | // br(!x, t, f) -> br(x, f, t) |
| 492 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 493 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 496 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 497 | // Handle ?: operator. |
| 498 | |
| 499 | // Just ignore GNU ?: extension. |
| 500 | if (CondOp->getLHS()) { |
| 501 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 502 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 503 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 504 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 505 | EmitBlock(LHSBlock); |
| 506 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 507 | EmitBlock(RHSBlock); |
| 508 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 509 | return; |
| 510 | } |
| 511 | } |
| 512 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 513 | // Emit the code with the fully general case. |
| 514 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 515 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 516 | } |
| 517 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 518 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 519 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 520 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 521 | bool OmitOnError) { |
| 522 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 525 | void |
| 526 | CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { |
| 527 | // If the type contains a pointer to data member we can't memset it to zero. |
| 528 | // Instead, create a null constant and copy it to the destination. |
| 529 | if (CGM.getTypes().ContainsPointerToDataMember(Ty)) { |
| 530 | llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); |
| 531 | |
| 532 | llvm::GlobalVariable *NullVariable = |
| 533 | new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), |
| 534 | /*isConstant=*/true, |
| 535 | llvm::GlobalVariable::PrivateLinkage, |
| 536 | NullConstant, llvm::Twine()); |
| 537 | EmitAggregateCopy(DestPtr, NullVariable, Ty, /*isVolatile=*/false); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 542 | // Ignore empty classes in C++. |
| 543 | if (getContext().getLangOptions().CPlusPlus) { |
| 544 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 545 | if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) |
| 546 | return; |
| 547 | } |
| 548 | } |
| 549 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 550 | // Otherwise, just memset the whole thing to zero. This is legal |
| 551 | // because in LLVM, all default initializers (other than the ones we just |
| 552 | // handled above) are guaranteed to have a bit pattern of all zeros. |
Chris Lattner | 36afd38 | 2009-10-13 06:02:42 +0000 | [diff] [blame] | 553 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 554 | if (DestPtr->getType() != BP) |
| 555 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 556 | |
| 557 | // Get size and alignment info for this aggregate. |
| 558 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 559 | |
Chris Lattner | 88207c9 | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 560 | // Don't bother emitting a zero-byte memset. |
| 561 | if (TypeInfo.first == 0) |
| 562 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 564 | // FIXME: Handle variable sized types. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 565 | Builder.CreateCall5(CGM.getMemSetFn(BP, IntPtrTy), DestPtr, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 566 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 567 | // TypeInfo.first describes size in bits. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 568 | llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), |
| 569 | llvm::ConstantInt::get(Int32Ty, TypeInfo.second/8), |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 570 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), |
| 571 | 0)); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 574 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { |
| 575 | // Make sure that there is a block for the indirect goto. |
| 576 | if (IndirectBranch == 0) |
| 577 | GetIndirectGotoBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 578 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 579 | llvm::BasicBlock *BB = getBasicBlockForLabel(L); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 580 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 581 | // Make sure the indirect branch includes all of the address-taken blocks. |
| 582 | IndirectBranch->addDestination(BB); |
| 583 | return llvm::BlockAddress::get(CurFn, BB); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 587 | // If we already made the indirect branch for indirect goto, return its block. |
| 588 | if (IndirectBranch) return IndirectBranch->getParent(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 589 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 590 | CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 591 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 592 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | 85e74ac | 2009-10-28 20:36:47 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 594 | // Create the PHI node that indirect gotos will add entries to. |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 595 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 596 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 597 | // Create the indirect branch instruction. |
| 598 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 599 | return IndirectBranch->getParent(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 600 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 602 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 603 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 605 | assert(SizeEntry && "Did not emit size for type"); |
| 606 | return SizeEntry; |
| 607 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 608 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 609 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 610 | assert(Ty->isVariablyModifiedType() && |
| 611 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 613 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 615 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 616 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 618 | if (!SizeEntry) { |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 619 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 621 | // Get the element size; |
| 622 | QualType ElemTy = VAT->getElementType(); |
| 623 | llvm::Value *ElemSize; |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 624 | if (ElemTy->isVariableArrayType()) |
| 625 | ElemSize = EmitVLASize(ElemTy); |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 626 | else |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 627 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 628 | getContext().getTypeSizeInChars(ElemTy).getQuantity()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 630 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 631 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 633 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 636 | return SizeEntry; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 639 | if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 640 | EmitVLASize(AT->getElementType()); |
| 641 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | ec18ddd | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 644 | const PointerType *PT = Ty->getAs<PointerType>(); |
| 645 | assert(PT && "unknown VM type!"); |
| 646 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 647 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 648 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 649 | |
| 650 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
Chris Lattner | fbe02ff | 2010-06-27 07:40:06 +0000 | [diff] [blame] | 651 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 652 | return EmitScalarExpr(E); |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 653 | return EmitLValue(E).getAddress(); |
| 654 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 655 | |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 656 | void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock, |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 657 | llvm::BasicBlock *CleanupExitBlock, |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 658 | llvm::BasicBlock *PreviousInvokeDest, |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 659 | bool EHOnly) { |
| 660 | CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock, |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 661 | PreviousInvokeDest, EHOnly)); |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 662 | } |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 663 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) { |
| 665 | assert(CleanupEntries.size() >= OldCleanupStackSize && |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 666 | "Cleanup stack mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | c71c845 | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 668 | while (CleanupEntries.size() > OldCleanupStackSize) |
| 669 | EmitCleanupBlock(); |
| 670 | } |
| 671 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 673 | CleanupEntry &CE = CleanupEntries.back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 675 | llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 677 | std::vector<llvm::BasicBlock *> Blocks; |
| 678 | std::swap(Blocks, CE.Blocks); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 680 | std::vector<llvm::BranchInst *> BranchFixups; |
| 681 | std::swap(BranchFixups, CE.BranchFixups); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 683 | bool EHOnly = CE.EHOnly; |
| 684 | |
Mike Stump | d88ea56 | 2009-12-09 03:35:49 +0000 | [diff] [blame] | 685 | setInvokeDest(CE.PreviousInvokeDest); |
| 686 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 687 | CleanupEntries.pop_back(); |
| 688 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 689 | // Check if any branch fixups pointed to the scope we just popped. If so, |
| 690 | // we can remove them. |
| 691 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 692 | llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); |
| 693 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 695 | if (I == BlockScopes.end()) |
| 696 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 698 | assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Anders Carlsson | ad9d00e | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 700 | if (I->second == CleanupEntries.size()) { |
| 701 | // We don't need to do this branch fixup. |
| 702 | BranchFixups[i] = BranchFixups.back(); |
| 703 | BranchFixups.pop_back(); |
| 704 | i--; |
| 705 | e--; |
| 706 | continue; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 710 | llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock; |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 711 | llvm::BasicBlock *EndBlock = 0; |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 712 | if (!BranchFixups.empty()) { |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 713 | if (!SwitchBlock) |
| 714 | SwitchBlock = createBasicBlock("cleanup.switch"); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 715 | EndBlock = createBasicBlock("cleanup.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 717 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 719 | Builder.SetInsertPoint(SwitchBlock); |
| 720 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 721 | llvm::Value *DestCodePtr = CreateTempAlloca(Int32Ty, "cleanup.dst"); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 722 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 724 | // Create a switch instruction to determine where to jump next. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 726 | BranchFixups.size()); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 727 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 728 | // Restore the current basic block (if any) |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 729 | if (CurBB) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 730 | Builder.SetInsertPoint(CurBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 732 | // If we had a current basic block, we also need to emit an instruction |
| 733 | // to initialize the cleanup destination. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 734 | Builder.CreateStore(llvm::Constant::getNullValue(Int32Ty), |
Anders Carlsson | 0ae7b2b | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 735 | DestCodePtr); |
| 736 | } else |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 737 | Builder.ClearInsertionPoint(); |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 738 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 739 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 740 | llvm::BranchInst *BI = BranchFixups[i]; |
| 741 | llvm::BasicBlock *Dest = BI->getSuccessor(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 743 | // Fixup the branch instruction to point to the cleanup block. |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 744 | BI->setSuccessor(0, CleanupEntryBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 746 | if (CleanupEntries.empty()) { |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 747 | llvm::ConstantInt *ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 749 | // Check if we already have a destination for this block. |
| 750 | if (Dest == SI->getDefaultDest()) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 751 | ID = llvm::ConstantInt::get(Int32Ty, 0); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 752 | else { |
| 753 | ID = SI->findCaseDest(Dest); |
| 754 | if (!ID) { |
| 755 | // No code found, get a new unique one by using the number of |
| 756 | // switch successors. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 757 | ID = llvm::ConstantInt::get(Int32Ty, SI->getNumSuccessors()); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 758 | SI->addCase(ID, Dest); |
| 759 | } |
| 760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 762 | // Store the jump destination before the branch instruction. |
| 763 | new llvm::StoreInst(ID, DestCodePtr, BI); |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 764 | } else { |
| 765 | // We need to jump through another cleanup block. Create a pad block |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 766 | // with a branch instruction that jumps to the final destination and add |
| 767 | // it as a branch fixup to the current cleanup scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 769 | // Create the pad block. |
| 770 | llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 771 | |
| 772 | // Create a unique case ID. |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 773 | llvm::ConstantInt *ID |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 774 | = llvm::ConstantInt::get(Int32Ty, SI->getNumSuccessors()); |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 775 | |
| 776 | // Store the jump destination before the branch instruction. |
| 777 | new llvm::StoreInst(ID, DestCodePtr, BI); |
| 778 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 779 | // Add it as the destination. |
Anders Carlsson | cc89920 | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 780 | SI->addCase(ID, CleanupPad); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 782 | // Create the branch to the final destination. |
| 783 | llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); |
| 784 | CleanupPad->getInstList().push_back(BI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Anders Carlsson | 1093c2c | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 786 | // And add it as a branch fixup. |
| 787 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 788 | } |
| 789 | } |
| 790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 792 | // Remove all blocks from the block scope map. |
| 793 | for (size_t i = 0, e = Blocks.size(); i != e; ++i) { |
| 794 | assert(BlockScopes.count(Blocks[i]) && |
| 795 | "Did not find block in scope map!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Anders Carlsson | bd6fa3d | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 797 | BlockScopes.erase(Blocks[i]); |
| 798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 800 | return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock, EHOnly); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | void CodeGenFunction::EmitCleanupBlock() { |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 804 | CleanupBlockInfo Info = PopCleanupBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Mike Stump | 9953383 | 2009-12-02 07:41:41 +0000 | [diff] [blame] | 806 | if (Info.EHOnly) { |
| 807 | // FIXME: Add this to the exceptional edge |
| 808 | if (Info.CleanupBlock->getNumUses() == 0) |
| 809 | delete Info.CleanupBlock; |
| 810 | return; |
| 811 | } |
| 812 | |
Devang Patel | cd9199e | 2010-04-13 00:08:43 +0000 | [diff] [blame] | 813 | // Scrub debug location info. |
| 814 | for (llvm::BasicBlock::iterator LBI = Info.CleanupBlock->begin(), |
| 815 | LBE = Info.CleanupBlock->end(); LBI != LBE; ++LBI) |
| 816 | Builder.SetInstDebugLocation(LBI); |
| 817 | |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 818 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | if (CurBB && !CurBB->getTerminator() && |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 820 | Info.CleanupBlock->getNumUses() == 0) { |
| 821 | CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); |
| 822 | delete Info.CleanupBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | } else |
Anders Carlsson | eb6437a | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 824 | EmitBlock(Info.CleanupBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Anders Carlsson | bb66f9f | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 826 | if (Info.SwitchBlock) |
| 827 | EmitBlock(Info.SwitchBlock); |
| 828 | if (Info.EndBlock) |
| 829 | EmitBlock(Info.EndBlock); |
Anders Carlsson | d66a9f9 | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) { |
| 833 | assert(!CleanupEntries.empty() && |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 834 | "Trying to add branch fixup without cleanup block!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 836 | // FIXME: We could be more clever here and check if there's already a branch |
| 837 | // fixup for this destination and recycle it. |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 838 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 839 | } |
| 840 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) { |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 842 | if (!HaveInsertPoint()) |
| 843 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 845 | llvm::BranchInst* BI = Builder.CreateBr(Dest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Anders Carlsson | 46831a9 | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 847 | Builder.ClearInsertionPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 849 | // The stack is empty, no need to do any cleanup. |
| 850 | if (CleanupEntries.empty()) |
| 851 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 853 | if (!Dest->getParent()) { |
| 854 | // We are trying to branch to a block that hasn't been inserted yet. |
| 855 | AddBranchFixup(BI); |
| 856 | return; |
| 857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 859 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
| 860 | if (I == BlockScopes.end()) { |
| 861 | // We are trying to jump to a block that is outside of any cleanup scope. |
| 862 | AddBranchFixup(BI); |
| 863 | return; |
| 864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 866 | assert(I->second < CleanupEntries.size() && |
| 867 | "Trying to branch into cleanup region"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 869 | if (I->second == CleanupEntries.size() - 1) { |
| 870 | // We have a branch to a block in the same scope. |
| 871 | return; |
| 872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Anders Carlsson | 87eaf17 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 874 | AddBranchFixup(BI); |
| 875 | } |