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