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