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