Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 842ddd0 | 2010-05-20 21:38:57 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 16 | using namespace clang; |
| 17 | using namespace CodeGen; |
| 18 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 19 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 20 | llvm::Value *Callee, |
| 21 | ReturnValueSlot ReturnValue, |
| 22 | llvm::Value *This, |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 23 | llvm::Value *VTT, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 24 | CallExpr::const_arg_iterator ArgBeg, |
| 25 | CallExpr::const_arg_iterator ArgEnd) { |
| 26 | assert(MD->isInstance() && |
| 27 | "Trying to emit a member call expr on a static method!"); |
| 28 | |
| 29 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 30 | |
| 31 | CallArgList Args; |
| 32 | |
| 33 | // Push the this ptr. |
| 34 | Args.push_back(std::make_pair(RValue::get(This), |
| 35 | MD->getThisType(getContext()))); |
| 36 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 37 | // If there is a VTT parameter, emit it. |
| 38 | if (VTT) { |
| 39 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 40 | Args.push_back(std::make_pair(RValue::get(VTT), T)); |
| 41 | } |
| 42 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 43 | // And the rest of the call args |
| 44 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 45 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 46 | QualType ResultType = FPT->getResultType(); |
| 47 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 48 | FPT->getExtInfo()), |
| 49 | Callee, ReturnValue, Args, MD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given |
| 53 | /// expr can be devirtualized. |
| 54 | static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { |
| 55 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 56 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 57 | // This is a record decl. We know the type and can devirtualize it. |
| 58 | return VD->getType()->isRecordType(); |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // We can always devirtualize calls on temporary object expressions. |
Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 65 | if (isa<CXXConstructExpr>(Base)) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 66 | return true; |
| 67 | |
| 68 | // And calls on bound temporaries. |
| 69 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 70 | return true; |
| 71 | |
| 72 | // Check if this is a call expr that returns a record type. |
| 73 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 74 | return CE->getCallReturnType()->isRecordType(); |
| 75 | |
| 76 | // We can't devirtualize the call. |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 81 | ReturnValueSlot ReturnValue) { |
| 82 | if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens())) |
| 83 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
| 84 | |
| 85 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens()); |
| 86 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 87 | |
| 88 | if (MD->isStatic()) { |
| 89 | // The method is static, emit it as we would a regular call. |
| 90 | llvm::Value *Callee = CGM.GetAddrOfFunction(MD); |
| 91 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
| 92 | ReturnValue, CE->arg_begin(), CE->arg_end()); |
| 93 | } |
| 94 | |
| 95 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 96 | |
| 97 | const llvm::Type *Ty = |
| 98 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 99 | FPT->isVariadic()); |
| 100 | llvm::Value *This; |
| 101 | |
| 102 | if (ME->isArrow()) |
| 103 | This = EmitScalarExpr(ME->getBase()); |
| 104 | else { |
| 105 | LValue BaseLV = EmitLValue(ME->getBase()); |
| 106 | This = BaseLV.getAddress(); |
| 107 | } |
| 108 | |
| 109 | if (MD->isCopyAssignment() && MD->isTrivial()) { |
| 110 | // We don't like to generate the trivial copy assignment operator when |
| 111 | // it isn't necessary; just produce the proper effect here. |
| 112 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 113 | EmitAggregateCopy(This, RHS, CE->getType()); |
| 114 | return RValue::get(This); |
| 115 | } |
| 116 | |
| 117 | // C++ [class.virtual]p12: |
| 118 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 119 | // virtual call mechanism. |
| 120 | // |
| 121 | // We also don't emit a virtual call if the base expression has a record type |
| 122 | // because then we know what the type is. |
| 123 | llvm::Value *Callee; |
| 124 | if (const CXXDestructorDecl *Destructor |
| 125 | = dyn_cast<CXXDestructorDecl>(MD)) { |
| 126 | if (Destructor->isTrivial()) |
| 127 | return RValue::get(0); |
| 128 | if (MD->isVirtual() && !ME->hasQualifier() && |
| 129 | !canDevirtualizeMemberFunctionCalls(ME->getBase())) { |
| 130 | Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty); |
| 131 | } else { |
| 132 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); |
| 133 | } |
| 134 | } else if (MD->isVirtual() && !ME->hasQualifier() && |
| 135 | !canDevirtualizeMemberFunctionCalls(ME->getBase())) { |
| 136 | Callee = BuildVirtualCall(MD, This, Ty); |
| 137 | } else { |
| 138 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
| 139 | } |
| 140 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 141 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 142 | CE->arg_begin(), CE->arg_end()); |
| 143 | } |
| 144 | |
| 145 | RValue |
| 146 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 147 | ReturnValueSlot ReturnValue) { |
| 148 | const BinaryOperator *BO = |
| 149 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 150 | const Expr *BaseExpr = BO->getLHS(); |
| 151 | const Expr *MemFnExpr = BO->getRHS(); |
| 152 | |
| 153 | const MemberPointerType *MPT = |
| 154 | MemFnExpr->getType()->getAs<MemberPointerType>(); |
| 155 | const FunctionProtoType *FPT = |
| 156 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 157 | const CXXRecordDecl *RD = |
| 158 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 159 | |
| 160 | const llvm::FunctionType *FTy = |
| 161 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 162 | FPT->isVariadic()); |
| 163 | |
Anders Carlsson | 51591be | 2010-02-04 17:08:48 +0000 | [diff] [blame] | 164 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 165 | |
| 166 | // Get the member function pointer. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 167 | llvm::Value *MemFnPtr = CreateMemTemp(MemFnExpr->getType(), "mem.fn"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 168 | EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false); |
| 169 | |
| 170 | // Emit the 'this' pointer. |
| 171 | llvm::Value *This; |
| 172 | |
| 173 | if (BO->getOpcode() == BinaryOperator::PtrMemI) |
| 174 | This = EmitScalarExpr(BaseExpr); |
| 175 | else |
| 176 | This = EmitLValue(BaseExpr).getAddress(); |
| 177 | |
| 178 | // Adjust it. |
| 179 | llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1); |
| 180 | Adj = Builder.CreateLoad(Adj, "mem.fn.adj"); |
| 181 | |
| 182 | llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr"); |
| 183 | Ptr = Builder.CreateGEP(Ptr, Adj, "adj"); |
| 184 | |
| 185 | This = Builder.CreateBitCast(Ptr, This->getType(), "this"); |
| 186 | |
| 187 | llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr"); |
| 188 | |
| 189 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 190 | |
| 191 | llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn"); |
| 192 | |
| 193 | // If the LSB in the function pointer is 1, the function pointer points to |
| 194 | // a virtual function. |
| 195 | llvm::Value *IsVirtual |
| 196 | = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1), |
| 197 | "and"); |
| 198 | |
| 199 | IsVirtual = Builder.CreateTrunc(IsVirtual, |
| 200 | llvm::Type::getInt1Ty(VMContext)); |
| 201 | |
| 202 | llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual"); |
| 203 | llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual"); |
| 204 | llvm::BasicBlock *FnEnd = createBasicBlock("fn.end"); |
| 205 | |
| 206 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 207 | EmitBlock(FnVirtual); |
| 208 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 209 | const llvm::Type *VTableTy = |
Anders Carlsson | 51591be | 2010-02-04 17:08:48 +0000 | [diff] [blame] | 210 | FTy->getPointerTo()->getPointerTo(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 212 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
| 213 | VTable = Builder.CreateLoad(VTable); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 214 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 215 | VTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
| 216 | llvm::Value *VTableOffset = |
Anders Carlsson | 51591be | 2010-02-04 17:08:48 +0000 | [diff] [blame] | 217 | Builder.CreateSub(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 218 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 219 | VTable = Builder.CreateGEP(VTable, VTableOffset, "fn"); |
| 220 | VTable = Builder.CreateBitCast(VTable, VTableTy); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 221 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 222 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 223 | |
| 224 | EmitBranch(FnEnd); |
| 225 | EmitBlock(FnNonVirtual); |
| 226 | |
| 227 | // If the function is not virtual, just load the pointer. |
| 228 | llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn"); |
| 229 | NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo()); |
| 230 | |
| 231 | EmitBlock(FnEnd); |
| 232 | |
| 233 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 234 | Callee->reserveOperandSpace(2); |
| 235 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 236 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 237 | |
| 238 | CallArgList Args; |
| 239 | |
| 240 | QualType ThisType = |
| 241 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 242 | |
| 243 | // Push the this ptr. |
| 244 | Args.push_back(std::make_pair(RValue::get(This), ThisType)); |
| 245 | |
| 246 | // And the rest of the call args |
| 247 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 248 | const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>(); |
| 249 | return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 250 | ReturnValue, Args); |
| 251 | } |
| 252 | |
| 253 | RValue |
| 254 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 255 | const CXXMethodDecl *MD, |
| 256 | ReturnValueSlot ReturnValue) { |
| 257 | assert(MD->isInstance() && |
| 258 | "Trying to emit a member call expr on a static method!"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 259 | if (MD->isCopyAssignment()) { |
| 260 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); |
| 261 | if (ClassDecl->hasTrivialCopyAssignment()) { |
| 262 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 263 | "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); |
Fariborz Jahanian | b3ebe94 | 2010-05-10 22:57:35 +0000 | [diff] [blame] | 264 | LValue LV = EmitLValue(E->getArg(0)); |
| 265 | llvm::Value *This; |
| 266 | if (LV.isPropertyRef()) { |
Fariborz Jahanian | c9a8fa4 | 2010-05-16 00:10:46 +0000 | [diff] [blame] | 267 | llvm::Value *AggLoc = CreateMemTemp(E->getArg(1)->getType()); |
Fariborz Jahanian | 0ca0b1f | 2010-05-15 23:05:52 +0000 | [diff] [blame] | 268 | EmitAggExpr(E->getArg(1), AggLoc, false /*VolatileDest*/); |
| 269 | EmitObjCPropertySet(LV.getPropertyRefExpr(), |
| 270 | RValue::getAggregate(AggLoc, false /*VolatileDest*/)); |
| 271 | return RValue::getAggregate(0, false); |
Fariborz Jahanian | b3ebe94 | 2010-05-10 22:57:35 +0000 | [diff] [blame] | 272 | } |
| 273 | else |
| 274 | This = LV.getAddress(); |
| 275 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 276 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 277 | QualType Ty = E->getType(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 278 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 279 | return RValue::get(This); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 284 | const llvm::Type *Ty = |
| 285 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 286 | FPT->isVariadic()); |
Fariborz Jahanian | bbb5224 | 2010-05-07 18:56:13 +0000 | [diff] [blame] | 287 | LValue LV = EmitLValue(E->getArg(0)); |
| 288 | llvm::Value *This; |
| 289 | if (LV.isPropertyRef()) { |
Fariborz Jahanian | 1d49f21 | 2010-05-20 16:46:55 +0000 | [diff] [blame] | 290 | RValue RV = EmitLoadOfPropertyRefLValue(LV, E->getArg(0)->getType()); |
| 291 | assert (!RV.isScalar() && "EmitCXXOperatorMemberCallExpr"); |
| 292 | This = RV.getAggregateAddr(); |
Fariborz Jahanian | bbb5224 | 2010-05-07 18:56:13 +0000 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | This = LV.getAddress(); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 296 | |
| 297 | llvm::Value *Callee; |
| 298 | if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0))) |
| 299 | Callee = BuildVirtualCall(MD, This, Ty); |
| 300 | else |
| 301 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
| 302 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 303 | return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 304 | E->arg_begin() + 1, E->arg_end()); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 309 | const CXXConstructExpr *E) { |
| 310 | assert(Dest && "Must have a destination!"); |
| 311 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 312 | const ConstantArrayType *Array = |
| 313 | getContext().getAsConstantArrayType(E->getType()); |
| 314 | // For a copy constructor, even if it is trivial, must fall thru so |
| 315 | // its argument is code-gen'ed. |
| 316 | if (!CD->isCopyConstructor()) { |
| 317 | QualType InitType = E->getType(); |
| 318 | if (Array) |
| 319 | InitType = getContext().getBaseElementType(Array); |
| 320 | const CXXRecordDecl *RD = |
| 321 | cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl()); |
| 322 | if (RD->hasTrivialConstructor()) |
| 323 | return; |
| 324 | } |
| 325 | // Code gen optimization to eliminate copy constructor and return |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 326 | // its first argument instead, if in fact that argument is a temporary |
| 327 | // object. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 328 | if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 329 | if (const Expr *Arg = E->getArg(0)->getTemporaryObject()) { |
| 330 | EmitAggExpr(Arg, Dest, false); |
| 331 | return; |
| 332 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 333 | } |
| 334 | if (Array) { |
| 335 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 336 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 337 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 338 | llvm::Value *BaseAddrPtr = |
Anders Carlsson | 43db20e | 2010-05-01 17:02:18 +0000 | [diff] [blame] | 339 | Builder.CreateBitCast(Dest, BasePtr); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 340 | |
| 341 | EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr, |
| 342 | E->arg_begin(), E->arg_end()); |
| 343 | } |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 344 | else { |
| 345 | CXXCtorType Type = |
| 346 | (E->getConstructionKind() == CXXConstructExpr::CK_Complete) |
| 347 | ? Ctor_Complete : Ctor_Base; |
| 348 | bool ForVirtualBase = |
| 349 | E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase; |
| 350 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 351 | // Call the constructor. |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 352 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 353 | E->arg_begin(), E->arg_end()); |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 354 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 357 | static CharUnits CalculateCookiePadding(ASTContext &Ctx, QualType ElementType) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 358 | const RecordType *RT = ElementType->getAs<RecordType>(); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 359 | if (!RT) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 360 | return CharUnits::Zero(); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 361 | |
| 362 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 363 | if (!RD) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 364 | return CharUnits::Zero(); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 365 | |
| 366 | // Check if the class has a trivial destructor. |
| 367 | if (RD->hasTrivialDestructor()) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 368 | // Check if the usual deallocation function takes two arguments. |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 369 | const CXXMethodDecl *UsualDeallocationFunction = 0; |
| 370 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 371 | DeclarationName OpName = |
| 372 | Ctx.DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 373 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 374 | for (llvm::tie(Op, OpEnd) = RD->lookup(OpName); |
| 375 | Op != OpEnd; ++Op) { |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 376 | const CXXMethodDecl *Delete = cast<CXXMethodDecl>(*Op); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 377 | |
| 378 | if (Delete->isUsualDeallocationFunction()) { |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 379 | UsualDeallocationFunction = Delete; |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | } |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 383 | |
| 384 | // No usual deallocation function, we don't need a cookie. |
| 385 | if (!UsualDeallocationFunction) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 386 | return CharUnits::Zero(); |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 387 | |
| 388 | // The usual deallocation function doesn't take a size_t argument, so we |
| 389 | // don't need a cookie. |
| 390 | if (UsualDeallocationFunction->getNumParams() == 1) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 391 | return CharUnits::Zero(); |
Anders Carlsson | 710f705 | 2009-12-13 20:10:12 +0000 | [diff] [blame] | 392 | |
| 393 | assert(UsualDeallocationFunction->getNumParams() == 2 && |
| 394 | "Unexpected deallocation function type!"); |
| 395 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 396 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 397 | // Padding is the maximum of sizeof(size_t) and alignof(ElementType) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 398 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 399 | Ctx.getTypeAlignInChars(ElementType)); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 402 | static CharUnits CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 403 | if (!E->isArray()) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 404 | return CharUnits::Zero(); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 405 | |
Anders Carlsson | dd93755 | 2009-12-13 20:34:34 +0000 | [diff] [blame] | 406 | // No cookie is required if the new operator being used is |
| 407 | // ::operator new[](size_t, void*). |
| 408 | const FunctionDecl *OperatorNew = E->getOperatorNew(); |
| 409 | if (OperatorNew->getDeclContext()->getLookupContext()->isFileContext()) { |
| 410 | if (OperatorNew->getNumParams() == 2) { |
| 411 | CanQualType ParamType = |
| 412 | Ctx.getCanonicalType(OperatorNew->getParamDecl(1)->getType()); |
| 413 | |
| 414 | if (ParamType == Ctx.VoidPtrTy) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 415 | return CharUnits::Zero(); |
Anders Carlsson | dd93755 | 2009-12-13 20:34:34 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 419 | return CalculateCookiePadding(Ctx, E->getAllocatedType()); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 422 | static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context, |
| 423 | CodeGenFunction &CGF, |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 424 | const CXXNewExpr *E, |
| 425 | llvm::Value *& NumElements) { |
| 426 | QualType Type = E->getAllocatedType(); |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 427 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(Type); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 428 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 429 | |
| 430 | if (!E->isArray()) |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 431 | return llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity()); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 432 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 433 | CharUnits CookiePadding = CalculateCookiePadding(CGF.getContext(), E); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 434 | |
| 435 | Expr::EvalResult Result; |
| 436 | if (E->getArraySize()->Evaluate(Result, CGF.getContext()) && |
| 437 | !Result.HasSideEffects && Result.Val.isInt()) { |
| 438 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 439 | CharUnits AllocSize = |
| 440 | Result.Val.getInt().getZExtValue() * TypeSize + CookiePadding; |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 441 | |
| 442 | NumElements = |
| 443 | llvm::ConstantInt::get(SizeTy, Result.Val.getInt().getZExtValue()); |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 444 | while (const ArrayType *AType = Context.getAsArrayType(Type)) { |
| 445 | const llvm::ArrayType *llvmAType = |
| 446 | cast<llvm::ArrayType>(CGF.ConvertType(Type)); |
| 447 | NumElements = |
| 448 | CGF.Builder.CreateMul(NumElements, |
| 449 | llvm::ConstantInt::get( |
| 450 | SizeTy, llvmAType->getNumElements())); |
| 451 | Type = AType->getElementType(); |
| 452 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 453 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 454 | return llvm::ConstantInt::get(SizeTy, AllocSize.getQuantity()); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Emit the array size expression. |
| 458 | NumElements = CGF.EmitScalarExpr(E->getArraySize()); |
| 459 | |
| 460 | // Multiply with the type size. |
| 461 | llvm::Value *V = |
| 462 | CGF.Builder.CreateMul(NumElements, |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 463 | llvm::ConstantInt::get(SizeTy, |
| 464 | TypeSize.getQuantity())); |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 465 | |
| 466 | while (const ArrayType *AType = Context.getAsArrayType(Type)) { |
| 467 | const llvm::ArrayType *llvmAType = |
| 468 | cast<llvm::ArrayType>(CGF.ConvertType(Type)); |
| 469 | NumElements = |
| 470 | CGF.Builder.CreateMul(NumElements, |
| 471 | llvm::ConstantInt::get( |
| 472 | SizeTy, llvmAType->getNumElements())); |
| 473 | Type = AType->getElementType(); |
| 474 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 475 | |
| 476 | // And add the cookie padding if necessary. |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 477 | if (!CookiePadding.isZero()) |
| 478 | V = CGF.Builder.CreateAdd(V, |
| 479 | llvm::ConstantInt::get(SizeTy, CookiePadding.getQuantity())); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 480 | |
| 481 | return V; |
| 482 | } |
| 483 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame^] | 484 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 485 | llvm::Value *NewPtr) { |
| 486 | // We have a POD type. |
| 487 | if (E->getNumConstructorArgs() == 0) |
| 488 | return; |
| 489 | |
| 490 | assert(E->getNumConstructorArgs() == 1 && |
| 491 | "Can only have one argument to initializer of POD type."); |
| 492 | |
| 493 | const Expr *Init = E->getConstructorArg(0); |
| 494 | QualType AllocType = E->getAllocatedType(); |
| 495 | |
| 496 | if (!CGF.hasAggregateLLVMType(AllocType)) |
| 497 | CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr, |
| 498 | AllocType.isVolatileQualified(), AllocType); |
| 499 | else if (AllocType->isAnyComplexType()) |
| 500 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 501 | AllocType.isVolatileQualified()); |
| 502 | else |
| 503 | CGF.EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
| 508 | llvm::Value *NewPtr, |
| 509 | llvm::Value *NumElements) { |
| 510 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 511 | |
| 512 | // Create a temporary for the loop index and initialize it with 0. |
| 513 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 514 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 515 | Builder.CreateStore(Zero, IndexPtr); |
| 516 | |
| 517 | // Start the loop with a block that tests the condition. |
| 518 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 519 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 520 | |
| 521 | EmitBlock(CondBlock); |
| 522 | |
| 523 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 524 | |
| 525 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 526 | // otherwise, go to the block after the for-loop. |
| 527 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 528 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 529 | // If the condition is true, execute the body. |
| 530 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 531 | |
| 532 | EmitBlock(ForBody); |
| 533 | |
| 534 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 535 | // Inside the loop body, emit the constructor call on the array element. |
| 536 | Counter = Builder.CreateLoad(IndexPtr); |
| 537 | llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter, |
| 538 | "arrayidx"); |
| 539 | StoreAnyExprIntoOneUnit(*this, E, Address); |
| 540 | |
| 541 | EmitBlock(ContinueBlock); |
| 542 | |
| 543 | // Emit the increment of the loop counter. |
| 544 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 545 | Counter = Builder.CreateLoad(IndexPtr); |
| 546 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 547 | Builder.CreateStore(NextVal, IndexPtr); |
| 548 | |
| 549 | // Finally, branch back up to the condition for the next iteration. |
| 550 | EmitBranch(CondBlock); |
| 551 | |
| 552 | // Emit the fall-through block. |
| 553 | EmitBlock(AfterFor, true); |
| 554 | } |
| 555 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 556 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 557 | llvm::Value *NewPtr, |
| 558 | llvm::Value *NumElements) { |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 559 | if (E->isArray()) { |
Anders Carlsson | e99bdb6 | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 560 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
| 561 | if (!Ctor->getParent()->hasTrivialConstructor()) |
| 562 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 563 | E->constructor_arg_begin(), |
| 564 | E->constructor_arg_end()); |
| 565 | return; |
| 566 | } |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame^] | 567 | else { |
| 568 | CGF.EmitNewArrayInitializer(E, NewPtr, NumElements); |
| 569 | return; |
| 570 | } |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 571 | } |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 572 | |
| 573 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 574 | CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
| 575 | NewPtr, E->constructor_arg_begin(), |
Anders Carlsson | 5d4d946 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 576 | E->constructor_arg_end()); |
| 577 | |
| 578 | return; |
| 579 | } |
| 580 | |
Fariborz Jahanian | ef66872 | 2010-06-25 18:26:07 +0000 | [diff] [blame^] | 581 | StoreAnyExprIntoOneUnit(CGF, E, NewPtr); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 584 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 585 | QualType AllocType = E->getAllocatedType(); |
| 586 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 587 | const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>(); |
| 588 | |
| 589 | CallArgList NewArgs; |
| 590 | |
| 591 | // The allocation size is the first argument. |
| 592 | QualType SizeTy = getContext().getSizeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 593 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 594 | llvm::Value *NumElements = 0; |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 595 | llvm::Value *AllocSize = EmitCXXNewAllocSize(getContext(), |
| 596 | *this, E, NumElements); |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 597 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 598 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 599 | |
| 600 | // Emit the rest of the arguments. |
| 601 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 602 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 603 | |
| 604 | // First, use the types from the function type. |
| 605 | // We start at 1 here because the first argument (the allocation size) |
| 606 | // has already been emitted. |
| 607 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 608 | QualType ArgType = NewFTy->getArgType(i); |
| 609 | |
| 610 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 611 | getTypePtr() == |
| 612 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 613 | "type mismatch in call argument!"); |
| 614 | |
| 615 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 616 | ArgType)); |
| 617 | |
| 618 | } |
| 619 | |
| 620 | // Either we've emitted all the call args, or we have a call to a |
| 621 | // variadic function. |
| 622 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 623 | "Extra arguments in non-variadic function!"); |
| 624 | |
| 625 | // If we still have any arguments, emit them using the type of the argument. |
| 626 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 627 | NewArg != NewArgEnd; ++NewArg) { |
| 628 | QualType ArgType = NewArg->getType(); |
| 629 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 630 | ArgType)); |
| 631 | } |
| 632 | |
| 633 | // Emit the call to new. |
| 634 | RValue RV = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 635 | EmitCall(CGM.getTypes().getFunctionInfo(NewArgs, NewFTy), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 636 | CGM.GetAddrOfFunction(NewFD), ReturnValueSlot(), NewArgs, NewFD); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 637 | |
| 638 | // If an allocation function is declared with an empty exception specification |
| 639 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 640 | // (We don't need to check for null when there's no new initializer and |
| 641 | // we're allocating a POD type). |
| 642 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 643 | !(AllocType->isPODType() && !E->hasInitializer()); |
| 644 | |
| 645 | llvm::BasicBlock *NewNull = 0; |
| 646 | llvm::BasicBlock *NewNotNull = 0; |
| 647 | llvm::BasicBlock *NewEnd = 0; |
| 648 | |
| 649 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 650 | |
| 651 | if (NullCheckResult) { |
| 652 | NewNull = createBasicBlock("new.null"); |
| 653 | NewNotNull = createBasicBlock("new.notnull"); |
| 654 | NewEnd = createBasicBlock("new.end"); |
| 655 | |
| 656 | llvm::Value *IsNull = |
| 657 | Builder.CreateICmpEQ(NewPtr, |
| 658 | llvm::Constant::getNullValue(NewPtr->getType()), |
| 659 | "isnull"); |
| 660 | |
| 661 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 662 | EmitBlock(NewNotNull); |
| 663 | } |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 664 | |
| 665 | CharUnits CookiePadding = CalculateCookiePadding(getContext(), E); |
| 666 | if (!CookiePadding.isZero()) { |
| 667 | CharUnits CookieOffset = |
| 668 | CookiePadding - getContext().getTypeSizeInChars(SizeTy); |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 669 | |
| 670 | llvm::Value *NumElementsPtr = |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 671 | Builder.CreateConstInBoundsGEP1_64(NewPtr, CookieOffset.getQuantity()); |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 672 | |
| 673 | NumElementsPtr = Builder.CreateBitCast(NumElementsPtr, |
| 674 | ConvertType(SizeTy)->getPointerTo()); |
| 675 | Builder.CreateStore(NumElements, NumElementsPtr); |
| 676 | |
| 677 | // Now add the padding to the new ptr. |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 678 | NewPtr = Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 679 | CookiePadding.getQuantity()); |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Fariborz Jahanian | ceb43b6 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 682 | if (AllocType->isArrayType()) { |
| 683 | while (const ArrayType *AType = getContext().getAsArrayType(AllocType)) |
| 684 | AllocType = AType->getElementType(); |
| 685 | NewPtr = |
| 686 | Builder.CreateBitCast(NewPtr, |
| 687 | ConvertType(getContext().getPointerType(AllocType))); |
| 688 | EmitNewInitializer(*this, E, NewPtr, NumElements); |
| 689 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
| 690 | } |
| 691 | else { |
| 692 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
| 693 | EmitNewInitializer(*this, E, NewPtr, NumElements); |
| 694 | } |
| 695 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 696 | if (NullCheckResult) { |
| 697 | Builder.CreateBr(NewEnd); |
Eli Friedman | 7f1de45 | 2009-11-10 22:39:09 +0000 | [diff] [blame] | 698 | NewNotNull = Builder.GetInsertBlock(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 699 | EmitBlock(NewNull); |
| 700 | Builder.CreateBr(NewEnd); |
| 701 | EmitBlock(NewEnd); |
| 702 | |
| 703 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 704 | PHI->reserveOperandSpace(2); |
| 705 | PHI->addIncoming(NewPtr, NewNotNull); |
| 706 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); |
| 707 | |
| 708 | NewPtr = PHI; |
| 709 | } |
| 710 | |
| 711 | return NewPtr; |
| 712 | } |
| 713 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 714 | static std::pair<llvm::Value *, llvm::Value *> |
| 715 | GetAllocatedObjectPtrAndNumElements(CodeGenFunction &CGF, |
| 716 | llvm::Value *Ptr, QualType DeleteTy) { |
| 717 | QualType SizeTy = CGF.getContext().getSizeType(); |
| 718 | const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy); |
| 719 | |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 720 | CharUnits DeleteTypeAlign = CGF.getContext().getTypeAlignInChars(DeleteTy); |
| 721 | CharUnits CookiePadding = |
| 722 | std::max(CGF.getContext().getTypeSizeInChars(SizeTy), |
| 723 | DeleteTypeAlign); |
| 724 | assert(!CookiePadding.isZero() && "CookiePadding should not be 0."); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 725 | |
| 726 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 727 | CharUnits CookieOffset = |
| 728 | CookiePadding - CGF.getContext().getTypeSizeInChars(SizeTy); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 729 | |
| 730 | llvm::Value *AllocatedObjectPtr = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 731 | AllocatedObjectPtr = |
| 732 | CGF.Builder.CreateConstInBoundsGEP1_64(AllocatedObjectPtr, |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 733 | -CookiePadding.getQuantity()); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 734 | |
| 735 | llvm::Value *NumElementsPtr = |
| 736 | CGF.Builder.CreateConstInBoundsGEP1_64(AllocatedObjectPtr, |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 737 | CookieOffset.getQuantity()); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 738 | NumElementsPtr = |
| 739 | CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo()); |
| 740 | |
| 741 | llvm::Value *NumElements = CGF.Builder.CreateLoad(NumElementsPtr); |
| 742 | NumElements = |
| 743 | CGF.Builder.CreateIntCast(NumElements, SizeLTy, /*isSigned=*/false); |
| 744 | |
| 745 | return std::make_pair(AllocatedObjectPtr, NumElements); |
| 746 | } |
| 747 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 748 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 749 | llvm::Value *Ptr, |
| 750 | QualType DeleteTy) { |
| 751 | const FunctionProtoType *DeleteFTy = |
| 752 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 753 | |
| 754 | CallArgList DeleteArgs; |
| 755 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 756 | // Check if we need to pass the size to the delete operator. |
| 757 | llvm::Value *Size = 0; |
| 758 | QualType SizeTy; |
| 759 | if (DeleteFTy->getNumArgs() == 2) { |
| 760 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 4f122ef | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 761 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 762 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 763 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if (DeleteFD->getOverloadedOperator() == OO_Array_Delete && |
Ken Dyck | caf647c | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 767 | !CalculateCookiePadding(getContext(), DeleteTy).isZero()) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 768 | // We need to get the number of elements in the array from the cookie. |
| 769 | llvm::Value *AllocatedObjectPtr; |
| 770 | llvm::Value *NumElements; |
| 771 | llvm::tie(AllocatedObjectPtr, NumElements) = |
| 772 | GetAllocatedObjectPtrAndNumElements(*this, Ptr, DeleteTy); |
| 773 | |
| 774 | // Multiply the size with the number of elements. |
| 775 | if (Size) |
| 776 | Size = Builder.CreateMul(NumElements, Size); |
| 777 | |
| 778 | Ptr = AllocatedObjectPtr; |
| 779 | } |
| 780 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 781 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 782 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
| 783 | DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); |
| 784 | |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 785 | if (Size) |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 786 | DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy)); |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 787 | |
| 788 | // Emit the call to delete. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 789 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 790 | CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 791 | DeleteArgs, DeleteFD); |
| 792 | } |
| 793 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 794 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 796 | // Get at the argument before we performed the implicit conversion |
| 797 | // to void*. |
| 798 | const Expr *Arg = E->getArgument(); |
| 799 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
| 800 | if (ICE->getCastKind() != CastExpr::CK_UserDefinedConversion && |
| 801 | ICE->getType()->isVoidPointerType()) |
| 802 | Arg = ICE->getSubExpr(); |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 803 | else |
| 804 | break; |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 809 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 810 | |
| 811 | // Null check the pointer. |
| 812 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 813 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 814 | |
| 815 | llvm::Value *IsNull = |
| 816 | Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()), |
| 817 | "isnull"); |
| 818 | |
| 819 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 820 | EmitBlock(DeleteNotNull); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 821 | |
| 822 | bool ShouldCallDelete = true; |
| 823 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 824 | // Call the destructor if necessary. |
| 825 | if (const RecordType *RT = DeleteTy->getAs<RecordType>()) { |
| 826 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 827 | if (!RD->hasTrivialDestructor()) { |
| 828 | const CXXDestructorDecl *Dtor = RD->getDestructor(getContext()); |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 829 | if (E->isArrayForm()) { |
Anders Carlsson | 871d078 | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 830 | llvm::Value *AllocatedObjectPtr; |
| 831 | llvm::Value *NumElements; |
| 832 | llvm::tie(AllocatedObjectPtr, NumElements) = |
| 833 | GetAllocatedObjectPtrAndNumElements(*this, Ptr, DeleteTy); |
| 834 | |
Anders Carlsson | 6153710 | 2009-12-13 18:48:07 +0000 | [diff] [blame] | 835 | EmitCXXAggrDestructorCall(Dtor, NumElements, Ptr); |
Anders Carlsson | 6153710 | 2009-12-13 18:48:07 +0000 | [diff] [blame] | 836 | } else if (Dtor->isVirtual()) { |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 837 | const llvm::Type *Ty = |
| 838 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(Dtor), |
| 839 | /*isVariadic=*/false); |
| 840 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 841 | llvm::Value *Callee = BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 842 | EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0, |
| 843 | 0, 0); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 844 | |
| 845 | // The dtor took care of deleting the object. |
| 846 | ShouldCallDelete = false; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 847 | } else |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 848 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 849 | Ptr); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
Eli Friedman | 5fe0598 | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 854 | if (ShouldCallDelete) |
| 855 | EmitDeleteCall(E->getOperatorDelete(), Ptr, DeleteTy); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 856 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 857 | EmitBlock(DeleteEnd); |
| 858 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 859 | |
| 860 | llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 861 | QualType Ty = E->getType(); |
| 862 | const llvm::Type *LTy = ConvertType(Ty)->getPointerTo(); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 863 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 864 | if (E->isTypeOperand()) { |
| 865 | llvm::Constant *TypeInfo = |
| 866 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); |
| 867 | return Builder.CreateBitCast(TypeInfo, LTy); |
| 868 | } |
| 869 | |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 870 | Expr *subE = E->getExprOperand(); |
Mike Stump | 5fae856 | 2009-11-17 22:33:00 +0000 | [diff] [blame] | 871 | Ty = subE->getType(); |
| 872 | CanQualType CanTy = CGM.getContext().getCanonicalType(Ty); |
| 873 | Ty = CanTy.getUnqualifiedType().getNonReferenceType(); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 874 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 875 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 876 | if (RD->isPolymorphic()) { |
| 877 | // FIXME: if subE is an lvalue do |
| 878 | LValue Obj = EmitLValue(subE); |
| 879 | llvm::Value *This = Obj.getAddress(); |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 880 | LTy = LTy->getPointerTo()->getPointerTo(); |
| 881 | llvm::Value *V = Builder.CreateBitCast(This, LTy); |
| 882 | // We need to do a zero check for *p, unless it has NonNullAttr. |
| 883 | // FIXME: PointerType->hasAttr<NonNullAttr>() |
| 884 | bool CanBeZero = false; |
Mike Stump | db519a4 | 2009-11-17 00:45:21 +0000 | [diff] [blame] | 885 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens())) |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 886 | if (UO->getOpcode() == UnaryOperator::Deref) |
| 887 | CanBeZero = true; |
| 888 | if (CanBeZero) { |
| 889 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 890 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 891 | |
| 892 | llvm::Value *Zero = llvm::Constant::getNullValue(LTy); |
| 893 | Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero), |
| 894 | NonZeroBlock, ZeroBlock); |
| 895 | EmitBlock(ZeroBlock); |
| 896 | /// Call __cxa_bad_typeid |
| 897 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
| 898 | const llvm::FunctionType *FTy; |
| 899 | FTy = llvm::FunctionType::get(ResultType, false); |
| 900 | llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 901 | Builder.CreateCall(F)->setDoesNotReturn(); |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 902 | Builder.CreateUnreachable(); |
| 903 | EmitBlock(NonZeroBlock); |
| 904 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 905 | V = Builder.CreateLoad(V, "vtable"); |
| 906 | V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL); |
| 907 | V = Builder.CreateLoad(V); |
| 908 | return V; |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 909 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 910 | } |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 911 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 912 | } |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 913 | |
| 914 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V, |
| 915 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 916 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 917 | QualType DestTy = DCE->getTypeAsWritten(); |
| 918 | QualType InnerType = DestTy->getPointeeType(); |
| 919 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 920 | const llvm::Type *LTy = ConvertType(DCE->getType()); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 921 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 922 | bool CanBeZero = false; |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 923 | bool ToVoid = false; |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 924 | bool ThrowOnBad = false; |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 925 | if (DestTy->isPointerType()) { |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 926 | // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this |
| 927 | CanBeZero = true; |
| 928 | if (InnerType->isVoidType()) |
| 929 | ToVoid = true; |
| 930 | } else { |
| 931 | LTy = LTy->getPointerTo(); |
Douglas Gregor | 485ee32 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 932 | |
| 933 | // FIXME: What if exceptions are disabled? |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 934 | ThrowOnBad = true; |
| 935 | } |
| 936 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 937 | if (SrcTy->isPointerType() || SrcTy->isReferenceType()) |
| 938 | SrcTy = SrcTy->getPointeeType(); |
| 939 | SrcTy = SrcTy.getUnqualifiedType(); |
| 940 | |
Anders Carlsson | 6f0e485 | 2009-12-18 14:55:04 +0000 | [diff] [blame] | 941 | if (DestTy->isPointerType() || DestTy->isReferenceType()) |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 942 | DestTy = DestTy->getPointeeType(); |
| 943 | DestTy = DestTy.getUnqualifiedType(); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 944 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 945 | llvm::BasicBlock *ContBlock = createBasicBlock(); |
| 946 | llvm::BasicBlock *NullBlock = 0; |
| 947 | llvm::BasicBlock *NonZeroBlock = 0; |
| 948 | if (CanBeZero) { |
| 949 | NonZeroBlock = createBasicBlock(); |
| 950 | NullBlock = createBasicBlock(); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 951 | Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 952 | EmitBlock(NonZeroBlock); |
| 953 | } |
| 954 | |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 955 | llvm::BasicBlock *BadCastBlock = 0; |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 956 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 957 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 958 | |
| 959 | // See if this is a dynamic_cast(void*) |
| 960 | if (ToVoid) { |
| 961 | llvm::Value *This = V; |
| 962 | V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo()); |
| 963 | V = Builder.CreateLoad(V, "vtable"); |
| 964 | V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL); |
| 965 | V = Builder.CreateLoad(V, "offset to top"); |
| 966 | This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext)); |
| 967 | V = Builder.CreateInBoundsGEP(This, V); |
| 968 | V = Builder.CreateBitCast(V, LTy); |
| 969 | } else { |
| 970 | /// Call __dynamic_cast |
| 971 | const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext); |
| 972 | const llvm::FunctionType *FTy; |
| 973 | std::vector<const llvm::Type*> ArgTys; |
| 974 | const llvm::Type *PtrToInt8Ty |
| 975 | = llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 976 | ArgTys.push_back(PtrToInt8Ty); |
| 977 | ArgTys.push_back(PtrToInt8Ty); |
| 978 | ArgTys.push_back(PtrToInt8Ty); |
| 979 | ArgTys.push_back(PtrDiffTy); |
| 980 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 981 | |
| 982 | // FIXME: Calculate better hint. |
| 983 | llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 984 | |
| 985 | assert(SrcTy->isRecordType() && "Src type must be record type!"); |
| 986 | assert(DestTy->isRecordType() && "Dest type must be record type!"); |
| 987 | |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 988 | llvm::Value *SrcArg |
| 989 | = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType()); |
| 990 | llvm::Value *DestArg |
| 991 | = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType()); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 992 | |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 993 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 994 | V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"), |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 995 | V, SrcArg, DestArg, hint); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 996 | V = Builder.CreateBitCast(V, LTy); |
| 997 | |
| 998 | if (ThrowOnBad) { |
| 999 | BadCastBlock = createBasicBlock(); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1000 | Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1001 | EmitBlock(BadCastBlock); |
Douglas Gregor | 485ee32 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 1002 | /// Invoke __cxa_bad_cast |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1003 | ResultType = llvm::Type::getVoidTy(VMContext); |
| 1004 | const llvm::FunctionType *FBadTy; |
Mike Stump | fde17be | 2009-11-17 03:01:03 +0000 | [diff] [blame] | 1005 | FBadTy = llvm::FunctionType::get(ResultType, false); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1006 | llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast"); |
Douglas Gregor | 485ee32 | 2010-05-14 21:14:41 +0000 | [diff] [blame] | 1007 | if (llvm::BasicBlock *InvokeDest = getInvokeDest()) { |
| 1008 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 1009 | Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn(); |
| 1010 | EmitBlock(Cont); |
| 1011 | } else { |
| 1012 | // FIXME: Does this ever make sense? |
| 1013 | Builder.CreateCall(F)->setDoesNotReturn(); |
| 1014 | } |
Mike Stump | 8b152b8 | 2009-11-17 00:08:50 +0000 | [diff] [blame] | 1015 | Builder.CreateUnreachable(); |
Mike Stump | 2b35baf | 2009-11-16 22:52:20 +0000 | [diff] [blame] | 1016 | } |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | if (CanBeZero) { |
| 1020 | Builder.CreateBr(ContBlock); |
| 1021 | EmitBlock(NullBlock); |
| 1022 | Builder.CreateBr(ContBlock); |
| 1023 | } |
| 1024 | EmitBlock(ContBlock); |
| 1025 | if (CanBeZero) { |
| 1026 | llvm::PHINode *PHI = Builder.CreatePHI(LTy); |
Mike Stump | 14431c1 | 2009-11-17 00:10:05 +0000 | [diff] [blame] | 1027 | PHI->reserveOperandSpace(2); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1028 | PHI->addIncoming(V, NonZeroBlock); |
| 1029 | PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock); |
Mike Stump | c849c05 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1030 | V = PHI; |
| 1031 | } |
| 1032 | |
| 1033 | return V; |
| 1034 | } |