Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +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 | 1763075 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Mike Stump | 0c74327 | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 27 | : BlockFunction(cgm, *this, Builder), CGM(cgm), |
| 28 | Target(CGM.getContext().Target), |
Owen Anderson | c9673d5 | 2009-07-08 20:52:20 +0000 | [diff] [blame] | 29 | Builder(cgm.getModule().getContext()), |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 30 | DebugInfo(0), IndirectBranch(0), |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 31 | SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0), |
Anders Carlsson | 0a66c26 | 2009-11-20 17:27:56 +0000 | [diff] [blame^] | 32 | CXXThisDecl(0), |
| 33 | ConditionalBranchLevel(0) { |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 34 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 35 | LLVMPointerWidth = Target.getPointerWidth(0); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 36 | } |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 38 | ASTContext &CodeGenFunction::getContext() const { |
| 39 | return CGM.getContext(); |
| 40 | } |
| 41 | |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 42 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 43 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 44 | llvm::BasicBlock *&BB = LabelMap[S]; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 45 | if (BB) return BB; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 47 | // Create, but don't insert, the new block. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 48 | return BB = createBasicBlock(S->getName()); |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 51 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) { |
| 52 | llvm::Value *Res = LocalDeclMap[VD]; |
| 53 | assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!"); |
| 54 | return Res; |
Lauro Ramos Venancio | 01a72ff | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 57 | llvm::Constant * |
| 58 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
| 59 | return cast<llvm::Constant>(GetAddrOfLocalVar(BVD)); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Daniel Dunbar | ee3da87 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 62 | const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 63 | return CGM.getTypes().ConvertTypeForMem(T); |
| 64 | } |
| 65 | |
Chris Lattner | f033c14 | 2007-06-22 19:05:19 +0000 | [diff] [blame] | 66 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 67 | return CGM.getTypes().ConvertType(T); |
Chris Lattner | 45bb914 | 2007-06-09 02:28:57 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 54fb19e | 2007-06-22 22:02:34 +0000 | [diff] [blame] | 70 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | b05a3e5 | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 71 | return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() || |
| 72 | T->isMemberFunctionPointerType(); |
Chris Lattner | 54fb19e | 2007-06-22 22:02:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 75 | void CodeGenFunction::EmitReturnBlock() { |
| 76 | // For cleanliness, we try to avoid emitting the return block for |
| 77 | // simple cases. |
| 78 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 79 | |
| 80 | if (CurBB) { |
| 81 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 82 | |
Daniel Dunbar | ea3060a | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 83 | // We have a valid insert point, reuse it if it is empty or there are no |
| 84 | // explicit jumps to the return block. |
| 85 | if (CurBB->empty() || ReturnBlock->use_empty()) { |
| 86 | ReturnBlock->replaceAllUsesWith(CurBB); |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 87 | delete ReturnBlock; |
Daniel Dunbar | ea3060a | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 88 | } else |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 89 | EmitBlock(ReturnBlock); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Otherwise, if the return block is the target of a single direct |
| 94 | // branch then we can just put the code in that block instead. This |
| 95 | // cleans up functions which started with a unified return block. |
| 96 | if (ReturnBlock->hasOneUse()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | llvm::BranchInst *BI = |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 98 | dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); |
| 99 | if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { |
| 100 | // Reset insertion point and delete the branch. |
| 101 | Builder.SetInsertPoint(BI->getParent()); |
| 102 | BI->eraseFromParent(); |
| 103 | delete ReturnBlock; |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 108 | // FIXME: We are at an unreachable point, there is no reason to emit the block |
| 109 | // unless it has uses. However, we still need a place to put the debug |
| 110 | // region.end for now. |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 111 | |
| 112 | EmitBlock(ReturnBlock); |
| 113 | } |
| 114 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 115 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Chris Lattner | e73e432 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 116 | assert(BreakContinueStack.empty() && |
| 117 | "mismatched push/pop in break/continue stack!"); |
Anders Carlsson | fbfb5e6 | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 118 | assert(BlockScopes.empty() && |
| 119 | "did not remove all blocks from block scope map!"); |
| 120 | assert(CleanupEntries.empty() && |
| 121 | "mismatched push/pop in cleanup stack!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
| 123 | // Emit function epilog (to return). |
Daniel Dunbar | fd346a3 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 124 | EmitReturnBlock(); |
Daniel Dunbar | fab3f93 | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 125 | |
| 126 | // Emit debug descriptor for function end. |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 127 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | fab3f93 | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 128 | DI->setLocation(EndLoc); |
| 129 | DI->EmitRegionEnd(CurFn, Builder); |
| 130 | } |
| 131 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 132 | EmitFunctionEpilog(*CurFnInfo, ReturnValue); |
Daniel Dunbar | 54bb193 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 134 | // If someone did an indirect goto, emit the indirect goto block at the end of |
| 135 | // the function. |
| 136 | if (IndirectBranch) { |
| 137 | EmitBlock(IndirectBranch->getParent()); |
| 138 | Builder.ClearInsertionPoint(); |
| 139 | } |
| 140 | |
Chris Lattner | c48023b | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 141 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 2739d2b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 142 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | c48023b | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 143 | AllocaInsertPt = 0; |
Chris Lattner | 2739d2b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 144 | Ptr->eraseFromParent(); |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 145 | |
| 146 | // If someone took the address of a label but never did an indirect goto, we |
| 147 | // made a zero entry PHI node, which is illegal, zap it now. |
| 148 | if (IndirectBranch) { |
| 149 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 150 | if (PN->getNumIncomingValues() == 0) { |
| 151 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 152 | PN->eraseFromParent(); |
| 153 | } |
| 154 | } |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 155 | } |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 157 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 158 | llvm::Function *Fn, |
Daniel Dunbar | 354d278 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 159 | const FunctionArgList &Args, |
| 160 | SourceLocation StartLoc) { |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 161 | const Decl *D = GD.getDecl(); |
| 162 | |
Anders Carlsson | f4478e9 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 163 | DidCallStackSave = false; |
Chris Lattner | 28ec0cf | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 164 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 165 | FnRetTy = RetTy; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 166 | CurFn = Fn; |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 167 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 168 | |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 169 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 54bb193 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 171 | // Create a marker to make it easy to insert allocas into the entryblock |
| 172 | // later. Don't create this with the builder, because we don't want it |
| 173 | // folded. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 174 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)); |
Mike Stump | 1dbb8f7 | 2009-09-25 18:11:00 +0000 | [diff] [blame] | 175 | AllocaInsertPt = new llvm::BitCastInst(Undef, |
| 176 | llvm::Type::getInt32Ty(VMContext), "", |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 177 | EntryBB); |
Chris Lattner | 4764022 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 178 | if (Builder.isNamePreserving()) |
| 179 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 181 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 54bb193 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 182 | ReturnValue = 0; |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 183 | if (!RetTy->isVoidType()) |
| 184 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 186 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Mike Stump | ae2559a | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 188 | QualType FnType = getContext().getFunctionType(RetTy, 0, 0, false, 0); |
| 189 | |
Sanjiv Gupta | 1e8b608 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 190 | // Emit subprogram debug descriptor. |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 191 | // FIXME: The cast here is a huge hack. |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 192 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 354d278 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 193 | DI->setLocation(StartLoc); |
Anders Carlsson | fd384d8 | 2009-09-11 00:11:35 +0000 | [diff] [blame] | 194 | if (isa<FunctionDecl>(D)) { |
Mike Stump | ae2559a | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 195 | DI->EmitFunctionStart(CGM.getMangledName(GD), FnType, CurFn, Builder); |
Daniel Dunbar | 354d278 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 196 | } else { |
| 197 | // Just use LLVM function name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 7c02cf6 | 2009-07-23 05:30:36 +0000 | [diff] [blame] | 199 | // FIXME: Remove unnecessary conversion to std::string when API settles. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | DI->EmitFunctionStart(std::string(Fn->getName()).c_str(), |
Mike Stump | ae2559a | 2009-10-23 01:52:13 +0000 | [diff] [blame] | 201 | FnType, CurFn, Builder); |
Sanjiv Gupta | 1e8b608 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 202 | } |
Sanjiv Gupta | 1e8b608 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 205 | // FIXME: Leaked. |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 206 | CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args); |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 207 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | c20879a | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 209 | // If any of the arguments have a variably modified type, make sure to |
| 210 | // emit the type size. |
| 211 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 212 | i != e; ++i) { |
| 213 | QualType Ty = i->second; |
| 214 | |
| 215 | if (Ty->isVariablyModifiedType()) |
| 216 | EmitVLASize(Ty); |
| 217 | } |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 218 | } |
Eli Friedman | 3d421e1 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 220 | void CodeGenFunction::GenerateCode(GlobalDecl GD, |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 221 | llvm::Function *Fn) { |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 222 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 223 | |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 224 | // Check if we should generate debug info for this function. |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 225 | if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 226 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 228 | FunctionArgList Args; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 230 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 231 | if (MD->isInstance()) { |
| 232 | // Create the implicit 'this' decl. |
| 233 | // FIXME: I'm not entirely sure I like using a fake decl just for code |
| 234 | // generation. Maybe we can come up with a better way? |
| 235 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | &getContext().Idents.get("this"), |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 237 | MD->getThisType(getContext())); |
| 238 | Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
| 239 | } |
| 240 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Eli Friedman | 3d421e1 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 242 | if (FD->getNumParams()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 243 | const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | 3d421e1 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 244 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 245 | |
| 246 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 248 | FProto->getArgType(i))); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 249 | } |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 250 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 251 | // FIXME: Support CXXTryStmt here, too. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 252 | if (const CompoundStmt *S = FD->getCompoundBody()) { |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 253 | StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc()); |
Anders Carlsson | 438cf92 | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 254 | |
| 255 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Anders Carlsson | ddf57d3 | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 256 | EmitCtorPrologue(CD, GD.getCtorType()); |
Anders Carlsson | 438cf92 | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 257 | EmitStmt(S); |
Anders Carlsson | 421a5c9 | 2009-11-06 04:19:02 +0000 | [diff] [blame] | 258 | |
| 259 | // If any of the member initializers are temporaries bound to references |
| 260 | // make sure to emit their destructors. |
| 261 | EmitCleanupBlocks(0); |
| 262 | |
Anders Carlsson | 438cf92 | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 263 | } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
| 264 | llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue"); |
| 265 | PushCleanupBlock(DtorEpilogue); |
| 266 | |
| 267 | EmitStmt(S); |
Anders Carlsson | 6b7378b | 2009-10-06 18:09:57 +0000 | [diff] [blame] | 268 | |
Anders Carlsson | 6b7378b | 2009-10-06 18:09:57 +0000 | [diff] [blame] | 269 | CleanupBlockInfo Info = PopCleanupBlock(); |
| 270 | |
| 271 | assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!"); |
| 272 | EmitBlock(DtorEpilogue); |
Anders Carlsson | ddf57d3 | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 273 | EmitDtorEpilogue(DD, GD.getDtorType()); |
Anders Carlsson | 6b7378b | 2009-10-06 18:09:57 +0000 | [diff] [blame] | 274 | |
| 275 | if (Info.SwitchBlock) |
| 276 | EmitBlock(Info.SwitchBlock); |
| 277 | if (Info.EndBlock) |
| 278 | EmitBlock(Info.EndBlock); |
Anders Carlsson | 438cf92 | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 279 | } else { |
| 280 | // Just a regular function, emit its body. |
| 281 | EmitStmt(S); |
Anders Carlsson | 6b7378b | 2009-10-06 18:09:57 +0000 | [diff] [blame] | 282 | } |
Anders Carlsson | 438cf92 | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 283 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 284 | FinishFunction(S->getRBracLoc()); |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 285 | } else if (FD->isImplicit()) { |
| 286 | const CXXRecordDecl *ClassDecl = |
| 287 | cast<CXXRecordDecl>(FD->getDeclContext()); |
| 288 | (void) ClassDecl; |
Fariborz Jahanian | 6f14c73 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 289 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 290 | // FIXME: For C++0x, we want to look for implicit *definitions* of |
| 291 | // these special member functions, rather than implicit *declarations*. |
Fariborz Jahanian | 9301b24 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 292 | if (CD->isCopyConstructor(getContext())) { |
| 293 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 294 | "Cannot synthesize a non-implicit copy constructor"); |
Anders Carlsson | ddf57d3 | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 295 | SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args); |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 296 | } else if (CD->isDefaultConstructor()) { |
Fariborz Jahanian | 9301b24 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 297 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 298 | "Cannot synthesize a non-implicit default constructor."); |
Anders Carlsson | ddf57d3 | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 299 | SynthesizeDefaultConstructor(CD, GD.getCtorType(), Fn, Args); |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 300 | } else { |
| 301 | assert(false && "Implicit constructor cannot be synthesized"); |
Fariborz Jahanian | 9301b24 | 2009-08-06 23:38:16 +0000 | [diff] [blame] | 302 | } |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 303 | } else if (const CXXDestructorDecl *CD = dyn_cast<CXXDestructorDecl>(FD)) { |
| 304 | assert(!ClassDecl->hasUserDeclaredDestructor() && |
| 305 | "Cannot synthesize a non-implicit destructor"); |
| 306 | SynthesizeDefaultDestructor(CD, GD.getDtorType(), Fn, Args); |
| 307 | } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 308 | assert(MD->isCopyAssignment() && |
| 309 | !ClassDecl->hasUserDeclaredCopyAssignment() && |
| 310 | "Cannot synthesize a method that is not an implicit-defined " |
| 311 | "copy constructor"); |
Anders Carlsson | ddf57d3 | 2009-09-14 05:32:02 +0000 | [diff] [blame] | 312 | SynthesizeCXXCopyAssignment(MD, Fn, Args); |
Douglas Gregor | 369acf9 | 2009-10-01 20:44:19 +0000 | [diff] [blame] | 313 | } else { |
| 314 | assert(false && "Cannot synthesize unknown implicit function"); |
| 315 | } |
Anders Carlsson | 73fcc95 | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 318 | // Destroy the 'this' declaration. |
| 319 | if (CXXThisDecl) |
| 320 | CXXThisDecl->Destroy(getContext()); |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 323 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 324 | /// this statement is not executed normally, it not containing a label means |
| 325 | /// that we can just remove the code. |
| 326 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 327 | // Null statement, not a label! |
| 328 | if (S == 0) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 330 | // If this is a label, we have to emit the code, consider something like: |
| 331 | // if (0) { ... foo: bar(); } goto foo; |
| 332 | if (isa<LabelStmt>(S)) |
| 333 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 335 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 336 | // to emit the code. |
| 337 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 338 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 340 | // If this is a switch statement, we want to ignore cases below it. |
| 341 | if (isa<SwitchStmt>(S)) |
| 342 | IgnoreCaseStmts = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 344 | // Scan subexpressions for verboten labels. |
| 345 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 346 | I != E; ++I) |
| 347 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 348 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 353 | |
| 354 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 355 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 356 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 357 | /// to 'false' and does not contain a label, return -1. |
| 358 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | f32443c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 359 | // FIXME: Rename and handle conversion of other evaluatable things |
| 360 | // to bool. |
Anders Carlsson | 8628645 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 361 | Expr::EvalResult Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
Anders Carlsson | 8628645 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 363 | Result.HasSideEffects) |
Anders Carlsson | 4046e65 | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 364 | return 0; // Not foldable, not integer or not fully evaluatable. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 366 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 367 | return 0; // Contains a label. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | 8628645 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 369 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 374 | /// statement) to the specified blocks. Based on the condition, this might try |
| 375 | /// to simplify the codegen of the conditional based on the branch. |
| 376 | /// |
| 377 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 378 | llvm::BasicBlock *TrueBlock, |
| 379 | llvm::BasicBlock *FalseBlock) { |
| 380 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 381 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 383 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 384 | // Handle X && Y in a condition. |
| 385 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 386 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 387 | // folded if the case was simple enough. |
| 388 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 389 | // br(1 && X) -> br(X). |
| 390 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 393 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 394 | // "X && 0" would have been constant folded to 0. |
| 395 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 396 | // br(X && 1) -> br(X). |
| 397 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 398 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 400 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 401 | // want to jump to the FalseBlock. |
Daniel Dunbar | a612e79 | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 402 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 403 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 404 | EmitBlock(LHSTrue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 406 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 407 | return; |
| 408 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 409 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 410 | // folded if the case was simple enough. |
| 411 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 412 | // br(0 || X) -> br(X). |
| 413 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 414 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 416 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 417 | // "X || 1" would have been constant folded to 1. |
| 418 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 419 | // br(X || 0) -> br(X). |
| 420 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 423 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 424 | // want to jump to the TrueBlock. |
Daniel Dunbar | a612e79 | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 425 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 426 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 427 | EmitBlock(LHSFalse); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 429 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 430 | return; |
| 431 | } |
Chris Lattner | d953773 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Chris Lattner | d953773 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 434 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 435 | // br(!x, t, f) -> br(x, f, t) |
| 436 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 437 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 438 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Daniel Dunbar | bf3c22e | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 440 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 441 | // Handle ?: operator. |
| 442 | |
| 443 | // Just ignore GNU ?: extension. |
| 444 | if (CondOp->getLHS()) { |
| 445 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 446 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 447 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 448 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 449 | EmitBlock(LHSBlock); |
| 450 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 451 | EmitBlock(RHSBlock); |
| 452 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 453 | return; |
| 454 | } |
| 455 | } |
| 456 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 457 | // Emit the code with the fully general case. |
| 458 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 459 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 460 | } |
| 461 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 462 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | fc94434 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 463 | /// specified stmt yet. |
Daniel Dunbar | f2cf6d1 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 464 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 465 | bool OmitOnError) { |
| 466 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | fc94434 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Chris Lattner | b534f6a | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 469 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) { |
Chris Lattner | dd7eaad | 2009-10-13 06:02:42 +0000 | [diff] [blame] | 470 | const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 471 | if (DestPtr->getType() != BP) |
| 472 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 473 | |
| 474 | // Get size and alignment info for this aggregate. |
| 475 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 476 | |
Chris Lattner | b534f6a | 2009-04-21 17:59:23 +0000 | [diff] [blame] | 477 | // Don't bother emitting a zero-byte memset. |
| 478 | if (TypeInfo.first == 0) |
| 479 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 481 | // FIXME: Handle variable sized types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 483 | LLVMPointerWidth); |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 484 | |
| 485 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 486 | llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)), |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 487 | // TypeInfo.first describes size in bits. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 488 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 2e744e8 | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 490 | TypeInfo.second/8)); |
| 491 | } |
| 492 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 493 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelStmt *L) { |
| 494 | // Make sure that there is a block for the indirect goto. |
| 495 | if (IndirectBranch == 0) |
| 496 | GetIndirectGotoBlock(); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 498 | llvm::BasicBlock *BB = getBasicBlockForLabel(L); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 500 | // Make sure the indirect branch includes all of the address-taken blocks. |
| 501 | IndirectBranch->addDestination(BB); |
| 502 | return llvm::BlockAddress::get(CurFn, BB); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 506 | // If we already made the indirect branch for indirect goto, return its block. |
| 507 | if (IndirectBranch) return IndirectBranch->getParent(); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 509 | CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 511 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | a0c0d88 | 2009-10-28 20:36:47 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 513 | // Create the PHI node that indirect gotos will add entries to. |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 514 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, "indirect.goto.dest"); |
Chris Lattner | 2bb5cb4 | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 516 | // Create the indirect branch instruction. |
| 517 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 518 | return IndirectBranch->getParent(); |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 519 | } |
Anders Carlsson | 13abd7e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 520 | |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 521 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) { |
Eli Friedman | 04fddf0 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 522 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Anders Carlsson | e388a5b | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 524 | assert(SizeEntry && "Did not emit size for type"); |
| 525 | return SizeEntry; |
| 526 | } |
Anders Carlsson | ccbe920 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 527 | |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 528 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) { |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 529 | assert(Ty->isVariablyModifiedType() && |
| 530 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 532 | EnsureInsertPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 534 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
Eli Friedman | 04fddf0 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 535 | llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 537 | if (!SizeEntry) { |
Anders Carlsson | 31f8649 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 538 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 8dc7626 | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 540 | // Get the element size; |
| 541 | QualType ElemTy = VAT->getElementType(); |
| 542 | llvm::Value *ElemSize; |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 543 | if (ElemTy->isVariableArrayType()) |
| 544 | ElemSize = EmitVLASize(ElemTy); |
Chris Lattner | 8dc7626 | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 545 | else |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 546 | ElemSize = llvm::ConstantInt::get(SizeTy, |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 547 | getContext().getTypeSize(ElemTy) / 8); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 549 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
Anders Carlsson | 31f8649 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 550 | NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 552 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 553 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 555 | return SizeEntry; |
Anders Carlsson | ccbe920 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 8dc7626 | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 558 | if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| 559 | EmitVLASize(AT->getElementType()); |
| 560 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Chris Lattner | 8dc7626 | 2009-08-15 00:03:43 +0000 | [diff] [blame] | 563 | const PointerType *PT = Ty->getAs<PointerType>(); |
| 564 | assert(PT && "unknown VM type!"); |
| 565 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 566 | return 0; |
Anders Carlsson | ccbe920 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 567 | } |
Eli Friedman | ddea0ad | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 568 | |
| 569 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 570 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { |
| 571 | return EmitScalarExpr(E); |
| 572 | } |
| 573 | return EmitLValue(E).getAddress(); |
| 574 | } |
Anders Carlsson | 15cb75a | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 575 | |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 576 | void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock, |
| 577 | llvm::BasicBlock *CleanupExitBlock) { |
| 578 | CleanupEntries.push_back(CleanupEntry(CleanupEntryBlock, CleanupExitBlock)); |
Anders Carlsson | 15cb75a | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 579 | } |
Anders Carlsson | be0f76a | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 580 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) { |
| 582 | assert(CleanupEntries.size() >= OldCleanupStackSize && |
Anders Carlsson | be0f76a | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 583 | "Cleanup stack mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Anders Carlsson | be0f76a | 2009-02-07 23:50:39 +0000 | [diff] [blame] | 585 | while (CleanupEntries.size() > OldCleanupStackSize) |
| 586 | EmitCleanupBlock(); |
| 587 | } |
| 588 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() { |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 590 | CleanupEntry &CE = CleanupEntries.back(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 592 | llvm::BasicBlock *CleanupEntryBlock = CE.CleanupEntryBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 594 | std::vector<llvm::BasicBlock *> Blocks; |
| 595 | std::swap(Blocks, CE.Blocks); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 597 | std::vector<llvm::BranchInst *> BranchFixups; |
| 598 | std::swap(BranchFixups, CE.BranchFixups); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 600 | CleanupEntries.pop_back(); |
| 601 | |
Anders Carlsson | f57b9ee | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 602 | // Check if any branch fixups pointed to the scope we just popped. If so, |
| 603 | // we can remove them. |
| 604 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 605 | llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0); |
| 606 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Anders Carlsson | f57b9ee | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 608 | if (I == BlockScopes.end()) |
| 609 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Anders Carlsson | f57b9ee | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 611 | assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Anders Carlsson | f57b9ee | 2009-02-08 22:45:15 +0000 | [diff] [blame] | 613 | if (I->second == CleanupEntries.size()) { |
| 614 | // We don't need to do this branch fixup. |
| 615 | BranchFixups[i] = BranchFixups.back(); |
| 616 | BranchFixups.pop_back(); |
| 617 | i--; |
| 618 | e--; |
| 619 | continue; |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 623 | llvm::BasicBlock *SwitchBlock = CE.CleanupExitBlock; |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 624 | llvm::BasicBlock *EndBlock = 0; |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 625 | if (!BranchFixups.empty()) { |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 626 | if (!SwitchBlock) |
| 627 | SwitchBlock = createBasicBlock("cleanup.switch"); |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 628 | EndBlock = createBasicBlock("cleanup.end"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 630 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 632 | Builder.SetInsertPoint(SwitchBlock); |
| 633 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 635 | "cleanup.dst"); |
| 636 | llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 638 | // Create a switch instruction to determine where to jump next. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock, |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 640 | BranchFixups.size()); |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 641 | |
Anders Carlsson | 76180ea | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 642 | // Restore the current basic block (if any) |
Anders Carlsson | e73e3ec | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 643 | if (CurBB) { |
Anders Carlsson | 76180ea | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 644 | Builder.SetInsertPoint(CurBB); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | |
Anders Carlsson | e73e3ec | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 646 | // If we had a current basic block, we also need to emit an instruction |
| 647 | // to initialize the cleanup destination. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 648 | Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)), |
Anders Carlsson | e73e3ec | 2009-03-17 05:53:35 +0000 | [diff] [blame] | 649 | DestCodePtr); |
| 650 | } else |
Anders Carlsson | 76180ea | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 651 | Builder.ClearInsertionPoint(); |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 652 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 653 | for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) { |
| 654 | llvm::BranchInst *BI = BranchFixups[i]; |
| 655 | llvm::BasicBlock *Dest = BI->getSuccessor(0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 657 | // Fixup the branch instruction to point to the cleanup block. |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 658 | BI->setSuccessor(0, CleanupEntryBlock); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 660 | if (CleanupEntries.empty()) { |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 661 | llvm::ConstantInt *ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 663 | // Check if we already have a destination for this block. |
| 664 | if (Dest == SI->getDefaultDest()) |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 665 | ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 666 | else { |
| 667 | ID = SI->findCaseDest(Dest); |
| 668 | if (!ID) { |
| 669 | // No code found, get a new unique one by using the number of |
| 670 | // switch successors. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 672 | SI->getNumSuccessors()); |
| 673 | SI->addCase(ID, Dest); |
| 674 | } |
| 675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 677 | // Store the jump destination before the branch instruction. |
| 678 | new llvm::StoreInst(ID, DestCodePtr, BI); |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 679 | } else { |
| 680 | // We need to jump through another cleanup block. Create a pad block |
| 681 | // with a branch instruction that jumps to the final destination and |
| 682 | // add it as a branch fixup to the current cleanup scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 684 | // Create the pad block. |
| 685 | llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn); |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 686 | |
| 687 | // Create a unique case ID. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 689 | SI->getNumSuccessors()); |
| 690 | |
| 691 | // Store the jump destination before the branch instruction. |
| 692 | new llvm::StoreInst(ID, DestCodePtr, BI); |
| 693 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 694 | // Add it as the destination. |
Anders Carlsson | 9c964ac | 2009-02-08 22:46:50 +0000 | [diff] [blame] | 695 | SI->addCase(ID, CleanupPad); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 697 | // Create the branch to the final destination. |
| 698 | llvm::BranchInst *BI = llvm::BranchInst::Create(Dest); |
| 699 | CleanupPad->getInstList().push_back(BI); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | 3c21dd5 | 2009-02-08 01:23:05 +0000 | [diff] [blame] | 701 | // And add it as a branch fixup. |
| 702 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 703 | } |
| 704 | } |
| 705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Anders Carlsson | fbfb5e6 | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 707 | // Remove all blocks from the block scope map. |
| 708 | for (size_t i = 0, e = Blocks.size(); i != e; ++i) { |
| 709 | assert(BlockScopes.count(Blocks[i]) && |
| 710 | "Did not find block in scope map!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Anders Carlsson | fbfb5e6 | 2009-02-08 00:16:35 +0000 | [diff] [blame] | 712 | BlockScopes.erase(Blocks[i]); |
| 713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 715 | return CleanupBlockInfo(CleanupEntryBlock, SwitchBlock, EndBlock); |
Anders Carlsson | ae91d9b | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | void CodeGenFunction::EmitCleanupBlock() { |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 719 | CleanupBlockInfo Info = PopCleanupBlock(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | f3f91ce | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 721 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | if (CurBB && !CurBB->getTerminator() && |
Anders Carlsson | f3f91ce | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 723 | Info.CleanupBlock->getNumUses() == 0) { |
| 724 | CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList()); |
| 725 | delete Info.CleanupBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | } else |
Anders Carlsson | f3f91ce | 2009-05-31 00:33:20 +0000 | [diff] [blame] | 727 | EmitBlock(Info.CleanupBlock); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Anders Carlsson | 66c384a | 2009-02-08 07:46:24 +0000 | [diff] [blame] | 729 | if (Info.SwitchBlock) |
| 730 | EmitBlock(Info.SwitchBlock); |
| 731 | if (Info.EndBlock) |
| 732 | EmitBlock(Info.EndBlock); |
Anders Carlsson | ae91d9b | 2009-02-08 03:55:35 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) { |
| 736 | assert(!CleanupEntries.empty() && |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 737 | "Trying to add branch fixup without cleanup block!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 739 | // FIXME: We could be more clever here and check if there's already a branch |
| 740 | // fixup for this destination and recycle it. |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 741 | CleanupEntries.back().BranchFixups.push_back(BI); |
| 742 | } |
| 743 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) { |
Anders Carlsson | 76180ea | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 745 | if (!HaveInsertPoint()) |
| 746 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 748 | llvm::BranchInst* BI = Builder.CreateBr(Dest); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Anders Carlsson | 76180ea | 2009-02-08 22:13:37 +0000 | [diff] [blame] | 750 | Builder.ClearInsertionPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 752 | // The stack is empty, no need to do any cleanup. |
| 753 | if (CleanupEntries.empty()) |
| 754 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 756 | if (!Dest->getParent()) { |
| 757 | // We are trying to branch to a block that hasn't been inserted yet. |
| 758 | AddBranchFixup(BI); |
| 759 | return; |
| 760 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 762 | BlockScopeMap::iterator I = BlockScopes.find(Dest); |
| 763 | if (I == BlockScopes.end()) { |
| 764 | // We are trying to jump to a block that is outside of any cleanup scope. |
| 765 | AddBranchFixup(BI); |
| 766 | return; |
| 767 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 769 | assert(I->second < CleanupEntries.size() && |
| 770 | "Trying to branch into cleanup region"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 772 | if (I->second == CleanupEntries.size() - 1) { |
| 773 | // We have a branch to a block in the same scope. |
| 774 | return; |
| 775 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | |
Anders Carlsson | 7d70fd2 | 2009-02-08 00:50:42 +0000 | [diff] [blame] | 777 | AddBranchFixup(BI); |
| 778 | } |