1 //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// 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 "clang/Frontend/CodeGenOptions.h" 15 #include "CodeGenFunction.h" 16 #include "CGCUDARuntime.h" 17 #include "CGCXXABI.h" 18 #include "CGObjCRuntime.h" 19 #include "CGDebugInfo.h" 20 #include "llvm/Intrinsics.h" 21 #include "llvm/Support/CallSite.h" 22 23 using namespace clang; 24 using namespace CodeGen; 25 26 RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, 27 llvm::Value *Callee, 28 ReturnValueSlot ReturnValue, 29 llvm::Value *This, 30 llvm::Value *VTT, 31 CallExpr::const_arg_iterator ArgBeg, 32 CallExpr::const_arg_iterator ArgEnd) { 33 assert(MD->isInstance() && 34 "Trying to emit a member call expr on a static method!"); 35 36 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 37 38 CallArgList Args; 39 40 // Push the this ptr. 41 Args.add(RValue::get(This), MD->getThisType(getContext())); 42 43 // If there is a VTT parameter, emit it. 44 if (VTT) { 45 QualType T = getContext().getPointerType(getContext().VoidPtrTy); 46 Args.add(RValue::get(VTT), T); 47 } 48 49 // And the rest of the call args 50 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); 51 52 QualType ResultType = FPT->getResultType(); 53 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, 54 FPT->getExtInfo()), 55 Callee, ReturnValue, Args, MD); 56 } 57 58 static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { 59 const Expr *E = Base; 60 61 while (true) { 62 E = E->IgnoreParens(); 63 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 64 if (CE->getCastKind() == CK_DerivedToBase || 65 CE->getCastKind() == CK_UncheckedDerivedToBase || 66 CE->getCastKind() == CK_NoOp) { 67 E = CE->getSubExpr(); 68 continue; 69 } 70 } 71 72 break; 73 } 74 75 QualType DerivedType = E->getType(); 76 if (const PointerType *PTy = DerivedType->getAs<PointerType>()) 77 DerivedType = PTy->getPointeeType(); 78 79 return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl()); 80 } 81 82 // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do 83 // quite what we want. 84 static const Expr *skipNoOpCastsAndParens(const Expr *E) { 85 while (true) { 86 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { 87 E = PE->getSubExpr(); 88 continue; 89 } 90 91 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 92 if (CE->getCastKind() == CK_NoOp) { 93 E = CE->getSubExpr(); 94 continue; 95 } 96 } 97 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 98 if (UO->getOpcode() == UO_Extension) { 99 E = UO->getSubExpr(); 100 continue; 101 } 102 } 103 return E; 104 } 105 } 106 107 /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given 108 /// expr can be devirtualized. 109 static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, 110 const Expr *Base, 111 const CXXMethodDecl *MD) { 112 113 // When building with -fapple-kext, all calls must go through the vtable since 114 // the kernel linker can do runtime patching of vtables. 115 if (Context.getLangOptions().AppleKext) 116 return false; 117 118 // If the most derived class is marked final, we know that no subclass can 119 // override this member function and so we can devirtualize it. For example: 120 // 121 // struct A { virtual void f(); } 122 // struct B final : A { }; 123 // 124 // void f(B *b) { 125 // b->f(); 126 // } 127 // 128 const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base); 129 if (MostDerivedClassDecl->hasAttr<FinalAttr>()) 130 return true; 131 132 // If the member function is marked 'final', we know that it can't be 133 // overridden and can therefore devirtualize it. 134 if (MD->hasAttr<FinalAttr>()) 135 return true; 136 137 // Similarly, if the class itself is marked 'final' it can't be overridden 138 // and we can therefore devirtualize the member function call. 139 if (MD->getParent()->hasAttr<FinalAttr>()) 140 return true; 141 142 Base = skipNoOpCastsAndParens(Base); 143 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { 144 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 145 // This is a record decl. We know the type and can devirtualize it. 146 return VD->getType()->isRecordType(); 147 } 148 149 return false; 150 } 151 152 // We can always devirtualize calls on temporary object expressions. 153 if (isa<CXXConstructExpr>(Base)) 154 return true; 155 156 // And calls on bound temporaries. 157 if (isa<CXXBindTemporaryExpr>(Base)) 158 return true; 159 160 // Check if this is a call expr that returns a record type. 161 if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) 162 return CE->getCallReturnType()->isRecordType(); 163 164 // We can't devirtualize the call. 165 return false; 166 } 167 168 // Note: This function also emit constructor calls to support a MSVC 169 // extensions allowing explicit constructor function call. 170 RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, 171 ReturnValueSlot ReturnValue) { 172 const Expr *callee = CE->getCallee()->IgnoreParens(); 173 174 if (isa<BinaryOperator>(callee)) 175 return EmitCXXMemberPointerCallExpr(CE, ReturnValue); 176 177 const MemberExpr *ME = cast<MemberExpr>(callee); 178 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 179 180 CGDebugInfo *DI = getDebugInfo(); 181 if (DI && CGM.getCodeGenOpts().LimitDebugInfo 182 && !isa<CallExpr>(ME->getBase())) { 183 QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType(); 184 if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) { 185 DI->getOrCreateRecordType(PTy->getPointeeType(), 186 MD->getParent()->getLocation()); 187 } 188 } 189 190 if (MD->isStatic()) { 191 // The method is static, emit it as we would a regular call. 192 llvm::Value *Callee = CGM.GetAddrOfFunction(MD); 193 return EmitCall(getContext().getPointerType(MD->getType()), Callee, 194 ReturnValue, CE->arg_begin(), CE->arg_end()); 195 } 196 197 // Compute the object pointer. 198 llvm::Value *This; 199 if (ME->isArrow()) 200 This = EmitScalarExpr(ME->getBase()); 201 else 202 This = EmitLValue(ME->getBase()).getAddress(); 203 204 if (MD->isTrivial()) { 205 if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); 206 if (isa<CXXConstructorDecl>(MD) && 207 cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) 208 return RValue::get(0); 209 210 if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { 211 // We don't like to generate the trivial copy/move assignment operator 212 // when it isn't necessary; just produce the proper effect here. 213 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); 214 EmitAggregateCopy(This, RHS, CE->getType()); 215 return RValue::get(This); 216 } 217 218 if (isa<CXXConstructorDecl>(MD) && 219 cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { 220 // Trivial move and copy ctor are the same. 221 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); 222 EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, 223 CE->arg_begin(), CE->arg_end()); 224 return RValue::get(This); 225 } 226 llvm_unreachable("unknown trivial member function"); 227 } 228 229 // Compute the function type we're calling. 230 const CGFunctionInfo *FInfo = 0; 231 if (isa<CXXDestructorDecl>(MD)) 232 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), 233 Dtor_Complete); 234 else if (isa<CXXConstructorDecl>(MD)) 235 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD), 236 Ctor_Complete); 237 else 238 FInfo = &CGM.getTypes().getFunctionInfo(MD); 239 240 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 241 llvm::Type *Ty 242 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic()); 243 244 // C++ [class.virtual]p12: 245 // Explicit qualification with the scope operator (5.1) suppresses the 246 // virtual call mechanism. 247 // 248 // We also don't emit a virtual call if the base expression has a record type 249 // because then we know what the type is. 250 bool UseVirtualCall; 251 UseVirtualCall = MD->isVirtual() && !ME->hasQualifier() 252 && !canDevirtualizeMemberFunctionCalls(getContext(), 253 ME->getBase(), MD); 254 llvm::Value *Callee; 255 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { 256 if (UseVirtualCall) { 257 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty); 258 } else { 259 if (getContext().getLangOptions().AppleKext && 260 MD->isVirtual() && 261 ME->hasQualifier()) 262 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); 263 else 264 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); 265 } 266 } else if (const CXXConstructorDecl *Ctor = 267 dyn_cast<CXXConstructorDecl>(MD)) { 268 Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); 269 } else if (UseVirtualCall) { 270 Callee = BuildVirtualCall(MD, This, Ty); 271 } else { 272 if (getContext().getLangOptions().AppleKext && 273 MD->isVirtual() && 274 ME->hasQualifier()) 275 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); 276 else 277 Callee = CGM.GetAddrOfFunction(MD, Ty); 278 } 279 280 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, 281 CE->arg_begin(), CE->arg_end()); 282 } 283 284 RValue 285 CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, 286 ReturnValueSlot ReturnValue) { 287 const BinaryOperator *BO = 288 cast<BinaryOperator>(E->getCallee()->IgnoreParens()); 289 const Expr *BaseExpr = BO->getLHS(); 290 const Expr *MemFnExpr = BO->getRHS(); 291 292 const MemberPointerType *MPT = 293 MemFnExpr->getType()->castAs<MemberPointerType>(); 294 295 const FunctionProtoType *FPT = 296 MPT->getPointeeType()->castAs<FunctionProtoType>(); 297 const CXXRecordDecl *RD = 298 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 299 300 // Get the member function pointer. 301 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); 302 303 // Emit the 'this' pointer. 304 llvm::Value *This; 305 306 if (BO->getOpcode() == BO_PtrMemI) 307 This = EmitScalarExpr(BaseExpr); 308 else 309 This = EmitLValue(BaseExpr).getAddress(); 310 311 // Ask the ABI to load the callee. Note that This is modified. 312 llvm::Value *Callee = 313 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); 314 315 CallArgList Args; 316 317 QualType ThisType = 318 getContext().getPointerType(getContext().getTagDeclType(RD)); 319 320 // Push the this ptr. 321 Args.add(RValue::get(This), ThisType); 322 323 // And the rest of the call args 324 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); 325 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FPT), Callee, 326 ReturnValue, Args); 327 } 328 329 RValue 330 CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 331 const CXXMethodDecl *MD, 332 ReturnValueSlot ReturnValue) { 333 assert(MD->isInstance() && 334 "Trying to emit a member call expr on a static method!"); 335 LValue LV = EmitLValue(E->getArg(0)); 336 llvm::Value *This = LV.getAddress(); 337 338 if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && 339 MD->isTrivial()) { 340 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); 341 QualType Ty = E->getType(); 342 EmitAggregateCopy(This, Src, Ty); 343 return RValue::get(This); 344 } 345 346 llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This); 347 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0, 348 E->arg_begin() + 1, E->arg_end()); 349 } 350 351 RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, 352 ReturnValueSlot ReturnValue) { 353 return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); 354 } 355 356 static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, 357 llvm::Value *DestPtr, 358 const CXXRecordDecl *Base) { 359 if (Base->isEmpty()) 360 return; 361 362 DestPtr = CGF.EmitCastToVoidPtr(DestPtr); 363 364 const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); 365 CharUnits Size = Layout.getNonVirtualSize(); 366 CharUnits Align = Layout.getNonVirtualAlign(); 367 368 llvm::Value *SizeVal = CGF.CGM.getSize(Size); 369 370 // If the type contains a pointer to data member we can't memset it to zero. 371 // Instead, create a null constant and copy it to the destination. 372 // TODO: there are other patterns besides zero that we can usefully memset, 373 // like -1, which happens to be the pattern used by member-pointers. 374 // TODO: isZeroInitializable can be over-conservative in the case where a 375 // virtual base contains a member pointer. 376 if (!CGF.CGM.getTypes().isZeroInitializable(Base)) { 377 llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base); 378 379 llvm::GlobalVariable *NullVariable = 380 new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(), 381 /*isConstant=*/true, 382 llvm::GlobalVariable::PrivateLinkage, 383 NullConstant, Twine()); 384 NullVariable->setAlignment(Align.getQuantity()); 385 llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable); 386 387 // Get and call the appropriate llvm.memcpy overload. 388 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity()); 389 return; 390 } 391 392 // Otherwise, just memset the whole thing to zero. This is legal 393 // because in LLVM, all default initializers (other than the ones we just 394 // handled above) are guaranteed to have a bit pattern of all zeros. 395 CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal, 396 Align.getQuantity()); 397 } 398 399 void 400 CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, 401 AggValueSlot Dest) { 402 assert(!Dest.isIgnored() && "Must have a destination!"); 403 const CXXConstructorDecl *CD = E->getConstructor(); 404 405 // If we require zero initialization before (or instead of) calling the 406 // constructor, as can be the case with a non-user-provided default 407 // constructor, emit the zero initialization now, unless destination is 408 // already zeroed. 409 if (E->requiresZeroInitialization() && !Dest.isZeroed()) { 410 switch (E->getConstructionKind()) { 411 case CXXConstructExpr::CK_Delegating: 412 assert(0 && "Delegating constructor should not need zeroing"); 413 case CXXConstructExpr::CK_Complete: 414 EmitNullInitialization(Dest.getAddr(), E->getType()); 415 break; 416 case CXXConstructExpr::CK_VirtualBase: 417 case CXXConstructExpr::CK_NonVirtualBase: 418 EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent()); 419 break; 420 } 421 } 422 423 // If this is a call to a trivial default constructor, do nothing. 424 if (CD->isTrivial() && CD->isDefaultConstructor()) 425 return; 426 427 // Elide the constructor if we're constructing from a temporary. 428 // The temporary check is required because Sema sets this on NRVO 429 // returns. 430 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { 431 assert(getContext().hasSameUnqualifiedType(E->getType(), 432 E->getArg(0)->getType())); 433 if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { 434 EmitAggExpr(E->getArg(0), Dest); 435 return; 436 } 437 } 438 439 if (const ConstantArrayType *arrayType 440 = getContext().getAsConstantArrayType(E->getType())) { 441 EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), 442 E->arg_begin(), E->arg_end()); 443 } else { 444 CXXCtorType Type = Ctor_Complete; 445 bool ForVirtualBase = false; 446 447 switch (E->getConstructionKind()) { 448 case CXXConstructExpr::CK_Delegating: 449 // We should be emitting a constructor; GlobalDecl will assert this 450 Type = CurGD.getCtorType(); 451 break; 452 453 case CXXConstructExpr::CK_Complete: 454 Type = Ctor_Complete; 455 break; 456 457 case CXXConstructExpr::CK_VirtualBase: 458 ForVirtualBase = true; 459 // fall-through 460 461 case CXXConstructExpr::CK_NonVirtualBase: 462 Type = Ctor_Base; 463 } 464 465 // Call the constructor. 466 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(), 467 E->arg_begin(), E->arg_end()); 468 } 469 } 470 471 void 472 CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, 473 llvm::Value *Src, 474 const Expr *Exp) { 475 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) 476 Exp = E->getSubExpr(); 477 assert(isa<CXXConstructExpr>(Exp) && 478 "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); 479 const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); 480 const CXXConstructorDecl *CD = E->getConstructor(); 481 RunCleanupsScope Scope(*this); 482 483 // If we require zero initialization before (or instead of) calling the 484 // constructor, as can be the case with a non-user-provided default 485 // constructor, emit the zero initialization now. 486 // FIXME. Do I still need this for a copy ctor synthesis? 487 if (E->requiresZeroInitialization()) 488 EmitNullInitialization(Dest, E->getType()); 489 490 assert(!getContext().getAsConstantArrayType(E->getType()) 491 && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); 492 EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, 493 E->arg_begin(), E->arg_end()); 494 } 495 496 static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, 497 const CXXNewExpr *E) { 498 if (!E->isArray()) 499 return CharUnits::Zero(); 500 501 // No cookie is required if the operator new[] being used is the 502 // reserved placement operator new[]. 503 if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) 504 return CharUnits::Zero(); 505 506 return CGF.CGM.getCXXABI().GetArrayCookieSize(E); 507 } 508 509 static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, 510 const CXXNewExpr *e, 511 llvm::Value *&numElements, 512 llvm::Value *&sizeWithoutCookie) { 513 QualType type = e->getAllocatedType(); 514 515 if (!e->isArray()) { 516 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 517 sizeWithoutCookie 518 = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); 519 return sizeWithoutCookie; 520 } 521 522 // The width of size_t. 523 unsigned sizeWidth = CGF.SizeTy->getBitWidth(); 524 525 // Figure out the cookie size. 526 llvm::APInt cookieSize(sizeWidth, 527 CalculateCookiePadding(CGF, e).getQuantity()); 528 529 // Emit the array size expression. 530 // We multiply the size of all dimensions for NumElements. 531 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. 532 numElements = CGF.EmitScalarExpr(e->getArraySize()); 533 assert(isa<llvm::IntegerType>(numElements->getType())); 534 535 // The number of elements can be have an arbitrary integer type; 536 // essentially, we need to multiply it by a constant factor, add a 537 // cookie size, and verify that the result is representable as a 538 // size_t. That's just a gloss, though, and it's wrong in one 539 // important way: if the count is negative, it's an error even if 540 // the cookie size would bring the total size >= 0. 541 bool isSigned 542 = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); 543 llvm::IntegerType *numElementsType 544 = cast<llvm::IntegerType>(numElements->getType()); 545 unsigned numElementsWidth = numElementsType->getBitWidth(); 546 547 // Compute the constant factor. 548 llvm::APInt arraySizeMultiplier(sizeWidth, 1); 549 while (const ConstantArrayType *CAT 550 = CGF.getContext().getAsConstantArrayType(type)) { 551 type = CAT->getElementType(); 552 arraySizeMultiplier *= CAT->getSize(); 553 } 554 555 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 556 llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); 557 typeSizeMultiplier *= arraySizeMultiplier; 558 559 // This will be a size_t. 560 llvm::Value *size; 561 562 // If someone is doing 'new int[42]' there is no need to do a dynamic check. 563 // Don't bloat the -O0 code. 564 if (llvm::ConstantInt *numElementsC = 565 dyn_cast<llvm::ConstantInt>(numElements)) { 566 const llvm::APInt &count = numElementsC->getValue(); 567 568 bool hasAnyOverflow = false; 569 570 // If 'count' was a negative number, it's an overflow. 571 if (isSigned && count.isNegative()) 572 hasAnyOverflow = true; 573 574 // We want to do all this arithmetic in size_t. If numElements is 575 // wider than that, check whether it's already too big, and if so, 576 // overflow. 577 else if (numElementsWidth > sizeWidth && 578 numElementsWidth - sizeWidth > count.countLeadingZeros()) 579 hasAnyOverflow = true; 580 581 // Okay, compute a count at the right width. 582 llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); 583 584 // Scale numElements by that. This might overflow, but we don't 585 // care because it only overflows if allocationSize does, too, and 586 // if that overflows then we shouldn't use this. 587 numElements = llvm::ConstantInt::get(CGF.SizeTy, 588 adjustedCount * arraySizeMultiplier); 589 590 // Compute the size before cookie, and track whether it overflowed. 591 bool overflow; 592 llvm::APInt allocationSize 593 = adjustedCount.umul_ov(typeSizeMultiplier, overflow); 594 hasAnyOverflow |= overflow; 595 596 // Add in the cookie, and check whether it's overflowed. 597 if (cookieSize != 0) { 598 // Save the current size without a cookie. This shouldn't be 599 // used if there was overflow. 600 sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 601 602 allocationSize = allocationSize.uadd_ov(cookieSize, overflow); 603 hasAnyOverflow |= overflow; 604 } 605 606 // On overflow, produce a -1 so operator new will fail. 607 if (hasAnyOverflow) { 608 size = llvm::Constant::getAllOnesValue(CGF.SizeTy); 609 } else { 610 size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 611 } 612 613 // Otherwise, we might need to use the overflow intrinsics. 614 } else { 615 // There are up to four conditions we need to test for: 616 // 1) if isSigned, we need to check whether numElements is negative; 617 // 2) if numElementsWidth > sizeWidth, we need to check whether 618 // numElements is larger than something representable in size_t; 619 // 3) we need to compute 620 // sizeWithoutCookie := numElements * typeSizeMultiplier 621 // and check whether it overflows; and 622 // 4) if we need a cookie, we need to compute 623 // size := sizeWithoutCookie + cookieSize 624 // and check whether it overflows. 625 626 llvm::Value *hasOverflow = 0; 627 628 // If numElementsWidth > sizeWidth, then one way or another, we're 629 // going to have to do a comparison for (2), and this happens to 630 // take care of (1), too. 631 if (numElementsWidth > sizeWidth) { 632 llvm::APInt threshold(numElementsWidth, 1); 633 threshold <<= sizeWidth; 634 635 llvm::Value *thresholdV 636 = llvm::ConstantInt::get(numElementsType, threshold); 637 638 hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); 639 numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); 640 641 // Otherwise, if we're signed, we want to sext up to size_t. 642 } else if (isSigned) { 643 if (numElementsWidth < sizeWidth) 644 numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); 645 646 // If there's a non-1 type size multiplier, then we can do the 647 // signedness check at the same time as we do the multiply 648 // because a negative number times anything will cause an 649 // unsigned overflow. Otherwise, we have to do it here. 650 if (typeSizeMultiplier == 1) 651 hasOverflow = CGF.Builder.CreateICmpSLT(numElements, 652 llvm::ConstantInt::get(CGF.SizeTy, 0)); 653 654 // Otherwise, zext up to size_t if necessary. 655 } else if (numElementsWidth < sizeWidth) { 656 numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); 657 } 658 659 assert(numElements->getType() == CGF.SizeTy); 660 661 size = numElements; 662 663 // Multiply by the type size if necessary. This multiplier 664 // includes all the factors for nested arrays. 665 // 666 // This step also causes numElements to be scaled up by the 667 // nested-array factor if necessary. Overflow on this computation 668 // can be ignored because the result shouldn't be used if 669 // allocation fails. 670 if (typeSizeMultiplier != 1) { 671 llvm::Value *umul_with_overflow 672 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); 673 674 llvm::Value *tsmV = 675 llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); 676 llvm::Value *result = 677 CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV); 678 679 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 680 if (hasOverflow) 681 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 682 else 683 hasOverflow = overflowed; 684 685 size = CGF.Builder.CreateExtractValue(result, 0); 686 687 // Also scale up numElements by the array size multiplier. 688 if (arraySizeMultiplier != 1) { 689 // If the base element type size is 1, then we can re-use the 690 // multiply we just did. 691 if (typeSize.isOne()) { 692 assert(arraySizeMultiplier == typeSizeMultiplier); 693 numElements = size; 694 695 // Otherwise we need a separate multiply. 696 } else { 697 llvm::Value *asmV = 698 llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); 699 numElements = CGF.Builder.CreateMul(numElements, asmV); 700 } 701 } 702 } else { 703 // numElements doesn't need to be scaled. 704 assert(arraySizeMultiplier == 1); 705 } 706 707 // Add in the cookie size if necessary. 708 if (cookieSize != 0) { 709 sizeWithoutCookie = size; 710 711 llvm::Value *uadd_with_overflow 712 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); 713 714 llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); 715 llvm::Value *result = 716 CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV); 717 718 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 719 if (hasOverflow) 720 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 721 else 722 hasOverflow = overflowed; 723 724 size = CGF.Builder.CreateExtractValue(result, 0); 725 } 726 727 // If we had any possibility of dynamic overflow, make a select to 728 // overwrite 'size' with an all-ones value, which should cause 729 // operator new to throw. 730 if (hasOverflow) 731 size = CGF.Builder.CreateSelect(hasOverflow, 732 llvm::Constant::getAllOnesValue(CGF.SizeTy), 733 size); 734 } 735 736 if (cookieSize == 0) 737 sizeWithoutCookie = size; 738 else 739 assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); 740 741 return size; 742 } 743 744 static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, 745 llvm::Value *NewPtr) { 746 747 assert(E->getNumConstructorArgs() == 1 && 748 "Can only have one argument to initializer of POD type."); 749 750 const Expr *Init = E->getConstructorArg(0); 751 QualType AllocType = E->getAllocatedType(); 752 753 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType); 754 if (!CGF.hasAggregateLLVMType(AllocType)) 755 CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, 756 Alignment), 757 false); 758 else if (AllocType->isAnyComplexType()) 759 CGF.EmitComplexExprIntoAddr(Init, NewPtr, 760 AllocType.isVolatileQualified()); 761 else { 762 AggValueSlot Slot 763 = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(), 764 AggValueSlot::IsDestructed, 765 AggValueSlot::DoesNotNeedGCBarriers, 766 AggValueSlot::IsNotAliased); 767 CGF.EmitAggExpr(Init, Slot); 768 } 769 } 770 771 void 772 CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, 773 QualType elementType, 774 llvm::Value *beginPtr, 775 llvm::Value *numElements) { 776 // We have a POD type. 777 if (E->getNumConstructorArgs() == 0) 778 return; 779 780 // Check if the number of elements is constant. 781 bool checkZero = true; 782 if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) { 783 // If it's constant zero, skip the whole loop. 784 if (constNum->isZero()) return; 785 786 checkZero = false; 787 } 788 789 // Find the end of the array, hoisted out of the loop. 790 llvm::Value *endPtr = 791 Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end"); 792 793 // Create the continuation block. 794 llvm::BasicBlock *contBB = createBasicBlock("new.loop.end"); 795 796 // If we need to check for zero, do so now. 797 if (checkZero) { 798 llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty"); 799 llvm::Value *isEmpty = Builder.CreateICmpEQ(beginPtr, endPtr, 800 "array.isempty"); 801 Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB); 802 EmitBlock(nonEmptyBB); 803 } 804 805 // Enter the loop. 806 llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 807 llvm::BasicBlock *loopBB = createBasicBlock("new.loop"); 808 809 EmitBlock(loopBB); 810 811 // Set up the current-element phi. 812 llvm::PHINode *curPtr = 813 Builder.CreatePHI(beginPtr->getType(), 2, "array.cur"); 814 curPtr->addIncoming(beginPtr, entryBB); 815 816 // Enter a partial-destruction cleanup if necessary. 817 QualType::DestructionKind dtorKind = elementType.isDestructedType(); 818 EHScopeStack::stable_iterator cleanup; 819 llvm::Instruction *cleanupDominator = 0; 820 if (needsEHCleanup(dtorKind)) { 821 pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType, 822 getDestroyer(dtorKind)); 823 cleanup = EHStack.stable_begin(); 824 cleanupDominator = Builder.CreateUnreachable(); 825 } 826 827 // Emit the initializer into this element. 828 StoreAnyExprIntoOneUnit(*this, E, curPtr); 829 830 // Leave the cleanup if we entered one. 831 if (cleanupDominator) { 832 DeactivateCleanupBlock(cleanup, cleanupDominator); 833 cleanupDominator->eraseFromParent(); 834 } 835 836 // Advance to the next element. 837 llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next"); 838 839 // Check whether we've gotten to the end of the array and, if so, 840 // exit the loop. 841 llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend"); 842 Builder.CreateCondBr(isEnd, contBB, loopBB); 843 curPtr->addIncoming(nextPtr, Builder.GetInsertBlock()); 844 845 EmitBlock(contBB); 846 } 847 848 static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, 849 llvm::Value *NewPtr, llvm::Value *Size) { 850 CGF.EmitCastToVoidPtr(NewPtr); 851 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); 852 CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, 853 Alignment.getQuantity(), false); 854 } 855 856 static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, 857 QualType ElementType, 858 llvm::Value *NewPtr, 859 llvm::Value *NumElements, 860 llvm::Value *AllocSizeWithoutCookie) { 861 if (E->isArray()) { 862 if (CXXConstructorDecl *Ctor = E->getConstructor()) { 863 bool RequiresZeroInitialization = false; 864 if (Ctor->getParent()->hasTrivialDefaultConstructor()) { 865 // If new expression did not specify value-initialization, then there 866 // is no initialization. 867 if (!E->hasInitializer() || Ctor->getParent()->isEmpty()) 868 return; 869 870 if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) { 871 // Optimization: since zero initialization will just set the memory 872 // to all zeroes, generate a single memset to do it in one shot. 873 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); 874 return; 875 } 876 877 RequiresZeroInitialization = true; 878 } 879 880 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, 881 E->constructor_arg_begin(), 882 E->constructor_arg_end(), 883 RequiresZeroInitialization); 884 return; 885 } else if (E->getNumConstructorArgs() == 1 && 886 isa<ImplicitValueInitExpr>(E->getConstructorArg(0)) && 887 CGF.CGM.getTypes().isZeroInitializable(ElementType)) { 888 // Optimization: since zero initialization will just set the memory 889 // to all zeroes, generate a single memset to do it in one shot. 890 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); 891 return; 892 } else { 893 CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements); 894 return; 895 } 896 } 897 898 if (CXXConstructorDecl *Ctor = E->getConstructor()) { 899 // Per C++ [expr.new]p15, if we have an initializer, then we're performing 900 // direct initialization. C++ [dcl.init]p5 requires that we 901 // zero-initialize storage if there are no user-declared constructors. 902 if (E->hasInitializer() && 903 !Ctor->getParent()->hasUserDeclaredConstructor() && 904 !Ctor->getParent()->isEmpty()) 905 CGF.EmitNullInitialization(NewPtr, ElementType); 906 907 CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, 908 NewPtr, E->constructor_arg_begin(), 909 E->constructor_arg_end()); 910 911 return; 912 } 913 // We have a POD type. 914 if (E->getNumConstructorArgs() == 0) 915 return; 916 917 StoreAnyExprIntoOneUnit(CGF, E, NewPtr); 918 } 919 920 namespace { 921 /// A cleanup to call the given 'operator delete' function upon 922 /// abnormal exit from a new expression. 923 class CallDeleteDuringNew : public EHScopeStack::Cleanup { 924 size_t NumPlacementArgs; 925 const FunctionDecl *OperatorDelete; 926 llvm::Value *Ptr; 927 llvm::Value *AllocSize; 928 929 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } 930 931 public: 932 static size_t getExtraSize(size_t NumPlacementArgs) { 933 return NumPlacementArgs * sizeof(RValue); 934 } 935 936 CallDeleteDuringNew(size_t NumPlacementArgs, 937 const FunctionDecl *OperatorDelete, 938 llvm::Value *Ptr, 939 llvm::Value *AllocSize) 940 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 941 Ptr(Ptr), AllocSize(AllocSize) {} 942 943 void setPlacementArg(unsigned I, RValue Arg) { 944 assert(I < NumPlacementArgs && "index out of range"); 945 getPlacementArgs()[I] = Arg; 946 } 947 948 void Emit(CodeGenFunction &CGF, Flags flags) { 949 const FunctionProtoType *FPT 950 = OperatorDelete->getType()->getAs<FunctionProtoType>(); 951 assert(FPT->getNumArgs() == NumPlacementArgs + 1 || 952 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); 953 954 CallArgList DeleteArgs; 955 956 // The first argument is always a void*. 957 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); 958 DeleteArgs.add(RValue::get(Ptr), *AI++); 959 960 // A member 'operator delete' can take an extra 'size_t' argument. 961 if (FPT->getNumArgs() == NumPlacementArgs + 2) 962 DeleteArgs.add(RValue::get(AllocSize), *AI++); 963 964 // Pass the rest of the arguments, which must match exactly. 965 for (unsigned I = 0; I != NumPlacementArgs; ++I) 966 DeleteArgs.add(getPlacementArgs()[I], *AI++); 967 968 // Call 'operator delete'. 969 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), 970 CGF.CGM.GetAddrOfFunction(OperatorDelete), 971 ReturnValueSlot(), DeleteArgs, OperatorDelete); 972 } 973 }; 974 975 /// A cleanup to call the given 'operator delete' function upon 976 /// abnormal exit from a new expression when the new expression is 977 /// conditional. 978 class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { 979 size_t NumPlacementArgs; 980 const FunctionDecl *OperatorDelete; 981 DominatingValue<RValue>::saved_type Ptr; 982 DominatingValue<RValue>::saved_type AllocSize; 983 984 DominatingValue<RValue>::saved_type *getPlacementArgs() { 985 return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); 986 } 987 988 public: 989 static size_t getExtraSize(size_t NumPlacementArgs) { 990 return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); 991 } 992 993 CallDeleteDuringConditionalNew(size_t NumPlacementArgs, 994 const FunctionDecl *OperatorDelete, 995 DominatingValue<RValue>::saved_type Ptr, 996 DominatingValue<RValue>::saved_type AllocSize) 997 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 998 Ptr(Ptr), AllocSize(AllocSize) {} 999 1000 void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { 1001 assert(I < NumPlacementArgs && "index out of range"); 1002 getPlacementArgs()[I] = Arg; 1003 } 1004 1005 void Emit(CodeGenFunction &CGF, Flags flags) { 1006 const FunctionProtoType *FPT 1007 = OperatorDelete->getType()->getAs<FunctionProtoType>(); 1008 assert(FPT->getNumArgs() == NumPlacementArgs + 1 || 1009 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); 1010 1011 CallArgList DeleteArgs; 1012 1013 // The first argument is always a void*. 1014 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); 1015 DeleteArgs.add(Ptr.restore(CGF), *AI++); 1016 1017 // A member 'operator delete' can take an extra 'size_t' argument. 1018 if (FPT->getNumArgs() == NumPlacementArgs + 2) { 1019 RValue RV = AllocSize.restore(CGF); 1020 DeleteArgs.add(RV, *AI++); 1021 } 1022 1023 // Pass the rest of the arguments, which must match exactly. 1024 for (unsigned I = 0; I != NumPlacementArgs; ++I) { 1025 RValue RV = getPlacementArgs()[I].restore(CGF); 1026 DeleteArgs.add(RV, *AI++); 1027 } 1028 1029 // Call 'operator delete'. 1030 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT), 1031 CGF.CGM.GetAddrOfFunction(OperatorDelete), 1032 ReturnValueSlot(), DeleteArgs, OperatorDelete); 1033 } 1034 }; 1035 } 1036 1037 /// Enter a cleanup to call 'operator delete' if the initializer in a 1038 /// new-expression throws. 1039 static void EnterNewDeleteCleanup(CodeGenFunction &CGF, 1040 const CXXNewExpr *E, 1041 llvm::Value *NewPtr, 1042 llvm::Value *AllocSize, 1043 const CallArgList &NewArgs) { 1044 // If we're not inside a conditional branch, then the cleanup will 1045 // dominate and we can do the easier (and more efficient) thing. 1046 if (!CGF.isInConditionalBranch()) { 1047 CallDeleteDuringNew *Cleanup = CGF.EHStack 1048 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, 1049 E->getNumPlacementArgs(), 1050 E->getOperatorDelete(), 1051 NewPtr, AllocSize); 1052 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1053 Cleanup->setPlacementArg(I, NewArgs[I+1].RV); 1054 1055 return; 1056 } 1057 1058 // Otherwise, we need to save all this stuff. 1059 DominatingValue<RValue>::saved_type SavedNewPtr = 1060 DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); 1061 DominatingValue<RValue>::saved_type SavedAllocSize = 1062 DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); 1063 1064 CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack 1065 .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, 1066 E->getNumPlacementArgs(), 1067 E->getOperatorDelete(), 1068 SavedNewPtr, 1069 SavedAllocSize); 1070 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1071 Cleanup->setPlacementArg(I, 1072 DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); 1073 1074 CGF.initFullExprCleanup(); 1075 } 1076 1077 llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { 1078 // The element type being allocated. 1079 QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); 1080 1081 // 1. Build a call to the allocation function. 1082 FunctionDecl *allocator = E->getOperatorNew(); 1083 const FunctionProtoType *allocatorType = 1084 allocator->getType()->castAs<FunctionProtoType>(); 1085 1086 CallArgList allocatorArgs; 1087 1088 // The allocation size is the first argument. 1089 QualType sizeType = getContext().getSizeType(); 1090 1091 llvm::Value *numElements = 0; 1092 llvm::Value *allocSizeWithoutCookie = 0; 1093 llvm::Value *allocSize = 1094 EmitCXXNewAllocSize(*this, E, numElements, allocSizeWithoutCookie); 1095 1096 allocatorArgs.add(RValue::get(allocSize), sizeType); 1097 1098 // Emit the rest of the arguments. 1099 // FIXME: Ideally, this should just use EmitCallArgs. 1100 CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); 1101 1102 // First, use the types from the function type. 1103 // We start at 1 here because the first argument (the allocation size) 1104 // has already been emitted. 1105 for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; 1106 ++i, ++placementArg) { 1107 QualType argType = allocatorType->getArgType(i); 1108 1109 assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), 1110 placementArg->getType()) && 1111 "type mismatch in call argument!"); 1112 1113 EmitCallArg(allocatorArgs, *placementArg, argType); 1114 } 1115 1116 // Either we've emitted all the call args, or we have a call to a 1117 // variadic function. 1118 assert((placementArg == E->placement_arg_end() || 1119 allocatorType->isVariadic()) && 1120 "Extra arguments to non-variadic function!"); 1121 1122 // If we still have any arguments, emit them using the type of the argument. 1123 for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); 1124 placementArg != placementArgsEnd; ++placementArg) { 1125 EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); 1126 } 1127 1128 // Emit the allocation call. If the allocator is a global placement 1129 // operator, just "inline" it directly. 1130 RValue RV; 1131 if (allocator->isReservedGlobalPlacementOperator()) { 1132 assert(allocatorArgs.size() == 2); 1133 RV = allocatorArgs[1].RV; 1134 // TODO: kill any unnecessary computations done for the size 1135 // argument. 1136 } else { 1137 RV = EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType), 1138 CGM.GetAddrOfFunction(allocator), ReturnValueSlot(), 1139 allocatorArgs, allocator); 1140 } 1141 1142 // Emit a null check on the allocation result if the allocation 1143 // function is allowed to return null (because it has a non-throwing 1144 // exception spec; for this part, we inline 1145 // CXXNewExpr::shouldNullCheckAllocation()) and we have an 1146 // interesting initializer. 1147 bool nullCheck = allocatorType->isNothrow(getContext()) && 1148 !(allocType.isPODType(getContext()) && !E->hasInitializer()); 1149 1150 llvm::BasicBlock *nullCheckBB = 0; 1151 llvm::BasicBlock *contBB = 0; 1152 1153 llvm::Value *allocation = RV.getScalarVal(); 1154 unsigned AS = 1155 cast<llvm::PointerType>(allocation->getType())->getAddressSpace(); 1156 1157 // The null-check means that the initializer is conditionally 1158 // evaluated. 1159 ConditionalEvaluation conditional(*this); 1160 1161 if (nullCheck) { 1162 conditional.begin(*this); 1163 1164 nullCheckBB = Builder.GetInsertBlock(); 1165 llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); 1166 contBB = createBasicBlock("new.cont"); 1167 1168 llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); 1169 Builder.CreateCondBr(isNull, contBB, notNullBB); 1170 EmitBlock(notNullBB); 1171 } 1172 1173 // If there's an operator delete, enter a cleanup to call it if an 1174 // exception is thrown. 1175 EHScopeStack::stable_iterator operatorDeleteCleanup; 1176 llvm::Instruction *cleanupDominator = 0; 1177 if (E->getOperatorDelete() && 1178 !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { 1179 EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); 1180 operatorDeleteCleanup = EHStack.stable_begin(); 1181 cleanupDominator = Builder.CreateUnreachable(); 1182 } 1183 1184 assert((allocSize == allocSizeWithoutCookie) == 1185 CalculateCookiePadding(*this, E).isZero()); 1186 if (allocSize != allocSizeWithoutCookie) { 1187 assert(E->isArray()); 1188 allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, 1189 numElements, 1190 E, allocType); 1191 } 1192 1193 llvm::Type *elementPtrTy 1194 = ConvertTypeForMem(allocType)->getPointerTo(AS); 1195 llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); 1196 1197 EmitNewInitializer(*this, E, allocType, result, numElements, 1198 allocSizeWithoutCookie); 1199 if (E->isArray()) { 1200 // NewPtr is a pointer to the base element type. If we're 1201 // allocating an array of arrays, we'll need to cast back to the 1202 // array pointer type. 1203 llvm::Type *resultType = ConvertTypeForMem(E->getType()); 1204 if (result->getType() != resultType) 1205 result = Builder.CreateBitCast(result, resultType); 1206 } 1207 1208 // Deactivate the 'operator delete' cleanup if we finished 1209 // initialization. 1210 if (operatorDeleteCleanup.isValid()) { 1211 DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); 1212 cleanupDominator->eraseFromParent(); 1213 } 1214 1215 if (nullCheck) { 1216 conditional.end(*this); 1217 1218 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); 1219 EmitBlock(contBB); 1220 1221 llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); 1222 PHI->addIncoming(result, notNullBB); 1223 PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), 1224 nullCheckBB); 1225 1226 result = PHI; 1227 } 1228 1229 return result; 1230 } 1231 1232 void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, 1233 llvm::Value *Ptr, 1234 QualType DeleteTy) { 1235 assert(DeleteFD->getOverloadedOperator() == OO_Delete); 1236 1237 const FunctionProtoType *DeleteFTy = 1238 DeleteFD->getType()->getAs<FunctionProtoType>(); 1239 1240 CallArgList DeleteArgs; 1241 1242 // Check if we need to pass the size to the delete operator. 1243 llvm::Value *Size = 0; 1244 QualType SizeTy; 1245 if (DeleteFTy->getNumArgs() == 2) { 1246 SizeTy = DeleteFTy->getArgType(1); 1247 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); 1248 Size = llvm::ConstantInt::get(ConvertType(SizeTy), 1249 DeleteTypeSize.getQuantity()); 1250 } 1251 1252 QualType ArgTy = DeleteFTy->getArgType(0); 1253 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); 1254 DeleteArgs.add(RValue::get(DeletePtr), ArgTy); 1255 1256 if (Size) 1257 DeleteArgs.add(RValue::get(Size), SizeTy); 1258 1259 // Emit the call to delete. 1260 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy), 1261 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), 1262 DeleteArgs, DeleteFD); 1263 } 1264 1265 namespace { 1266 /// Calls the given 'operator delete' on a single object. 1267 struct CallObjectDelete : EHScopeStack::Cleanup { 1268 llvm::Value *Ptr; 1269 const FunctionDecl *OperatorDelete; 1270 QualType ElementType; 1271 1272 CallObjectDelete(llvm::Value *Ptr, 1273 const FunctionDecl *OperatorDelete, 1274 QualType ElementType) 1275 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} 1276 1277 void Emit(CodeGenFunction &CGF, Flags flags) { 1278 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); 1279 } 1280 }; 1281 } 1282 1283 /// Emit the code for deleting a single object. 1284 static void EmitObjectDelete(CodeGenFunction &CGF, 1285 const FunctionDecl *OperatorDelete, 1286 llvm::Value *Ptr, 1287 QualType ElementType, 1288 bool UseGlobalDelete) { 1289 // Find the destructor for the type, if applicable. If the 1290 // destructor is virtual, we'll just emit the vcall and return. 1291 const CXXDestructorDecl *Dtor = 0; 1292 if (const RecordType *RT = ElementType->getAs<RecordType>()) { 1293 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1294 if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { 1295 Dtor = RD->getDestructor(); 1296 1297 if (Dtor->isVirtual()) { 1298 if (UseGlobalDelete) { 1299 // If we're supposed to call the global delete, make sure we do so 1300 // even if the destructor throws. 1301 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, 1302 Ptr, OperatorDelete, 1303 ElementType); 1304 } 1305 1306 llvm::Type *Ty = 1307 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor, 1308 Dtor_Complete), 1309 /*isVariadic=*/false); 1310 1311 llvm::Value *Callee 1312 = CGF.BuildVirtualCall(Dtor, 1313 UseGlobalDelete? Dtor_Complete : Dtor_Deleting, 1314 Ptr, Ty); 1315 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0, 1316 0, 0); 1317 1318 if (UseGlobalDelete) { 1319 CGF.PopCleanupBlock(); 1320 } 1321 1322 return; 1323 } 1324 } 1325 } 1326 1327 // Make sure that we call delete even if the dtor throws. 1328 // This doesn't have to a conditional cleanup because we're going 1329 // to pop it off in a second. 1330 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, 1331 Ptr, OperatorDelete, ElementType); 1332 1333 if (Dtor) 1334 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, 1335 /*ForVirtualBase=*/false, Ptr); 1336 else if (CGF.getLangOptions().ObjCAutoRefCount && 1337 ElementType->isObjCLifetimeType()) { 1338 switch (ElementType.getObjCLifetime()) { 1339 case Qualifiers::OCL_None: 1340 case Qualifiers::OCL_ExplicitNone: 1341 case Qualifiers::OCL_Autoreleasing: 1342 break; 1343 1344 case Qualifiers::OCL_Strong: { 1345 // Load the pointer value. 1346 llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr, 1347 ElementType.isVolatileQualified()); 1348 1349 CGF.EmitARCRelease(PtrValue, /*precise*/ true); 1350 break; 1351 } 1352 1353 case Qualifiers::OCL_Weak: 1354 CGF.EmitARCDestroyWeak(Ptr); 1355 break; 1356 } 1357 } 1358 1359 CGF.PopCleanupBlock(); 1360 } 1361 1362 namespace { 1363 /// Calls the given 'operator delete' on an array of objects. 1364 struct CallArrayDelete : EHScopeStack::Cleanup { 1365 llvm::Value *Ptr; 1366 const FunctionDecl *OperatorDelete; 1367 llvm::Value *NumElements; 1368 QualType ElementType; 1369 CharUnits CookieSize; 1370 1371 CallArrayDelete(llvm::Value *Ptr, 1372 const FunctionDecl *OperatorDelete, 1373 llvm::Value *NumElements, 1374 QualType ElementType, 1375 CharUnits CookieSize) 1376 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), 1377 ElementType(ElementType), CookieSize(CookieSize) {} 1378 1379 void Emit(CodeGenFunction &CGF, Flags flags) { 1380 const FunctionProtoType *DeleteFTy = 1381 OperatorDelete->getType()->getAs<FunctionProtoType>(); 1382 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); 1383 1384 CallArgList Args; 1385 1386 // Pass the pointer as the first argument. 1387 QualType VoidPtrTy = DeleteFTy->getArgType(0); 1388 llvm::Value *DeletePtr 1389 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); 1390 Args.add(RValue::get(DeletePtr), VoidPtrTy); 1391 1392 // Pass the original requested size as the second argument. 1393 if (DeleteFTy->getNumArgs() == 2) { 1394 QualType size_t = DeleteFTy->getArgType(1); 1395 llvm::IntegerType *SizeTy 1396 = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); 1397 1398 CharUnits ElementTypeSize = 1399 CGF.CGM.getContext().getTypeSizeInChars(ElementType); 1400 1401 // The size of an element, multiplied by the number of elements. 1402 llvm::Value *Size 1403 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); 1404 Size = CGF.Builder.CreateMul(Size, NumElements); 1405 1406 // Plus the size of the cookie if applicable. 1407 if (!CookieSize.isZero()) { 1408 llvm::Value *CookieSizeV 1409 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); 1410 Size = CGF.Builder.CreateAdd(Size, CookieSizeV); 1411 } 1412 1413 Args.add(RValue::get(Size), size_t); 1414 } 1415 1416 // Emit the call to delete. 1417 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy), 1418 CGF.CGM.GetAddrOfFunction(OperatorDelete), 1419 ReturnValueSlot(), Args, OperatorDelete); 1420 } 1421 }; 1422 } 1423 1424 /// Emit the code for deleting an array of objects. 1425 static void EmitArrayDelete(CodeGenFunction &CGF, 1426 const CXXDeleteExpr *E, 1427 llvm::Value *deletedPtr, 1428 QualType elementType) { 1429 llvm::Value *numElements = 0; 1430 llvm::Value *allocatedPtr = 0; 1431 CharUnits cookieSize; 1432 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, 1433 numElements, allocatedPtr, cookieSize); 1434 1435 assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); 1436 1437 // Make sure that we call delete even if one of the dtors throws. 1438 const FunctionDecl *operatorDelete = E->getOperatorDelete(); 1439 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, 1440 allocatedPtr, operatorDelete, 1441 numElements, elementType, 1442 cookieSize); 1443 1444 // Destroy the elements. 1445 if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { 1446 assert(numElements && "no element count for a type with a destructor!"); 1447 1448 llvm::Value *arrayEnd = 1449 CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end"); 1450 1451 // Note that it is legal to allocate a zero-length array, and we 1452 // can never fold the check away because the length should always 1453 // come from a cookie. 1454 CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType, 1455 CGF.getDestroyer(dtorKind), 1456 /*checkZeroLength*/ true, 1457 CGF.needsEHCleanup(dtorKind)); 1458 } 1459 1460 // Pop the cleanup block. 1461 CGF.PopCleanupBlock(); 1462 } 1463 1464 void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { 1465 1466 // Get at the argument before we performed the implicit conversion 1467 // to void*. 1468 const Expr *Arg = E->getArgument(); 1469 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { 1470 if (ICE->getCastKind() != CK_UserDefinedConversion && 1471 ICE->getType()->isVoidPointerType()) 1472 Arg = ICE->getSubExpr(); 1473 else 1474 break; 1475 } 1476 1477 llvm::Value *Ptr = EmitScalarExpr(Arg); 1478 1479 // Null check the pointer. 1480 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); 1481 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); 1482 1483 llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); 1484 1485 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); 1486 EmitBlock(DeleteNotNull); 1487 1488 // We might be deleting a pointer to array. If so, GEP down to the 1489 // first non-array element. 1490 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) 1491 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); 1492 if (DeleteTy->isConstantArrayType()) { 1493 llvm::Value *Zero = Builder.getInt32(0); 1494 SmallVector<llvm::Value*,8> GEP; 1495 1496 GEP.push_back(Zero); // point at the outermost array 1497 1498 // For each layer of array type we're pointing at: 1499 while (const ConstantArrayType *Arr 1500 = getContext().getAsConstantArrayType(DeleteTy)) { 1501 // 1. Unpeel the array type. 1502 DeleteTy = Arr->getElementType(); 1503 1504 // 2. GEP to the first element of the array. 1505 GEP.push_back(Zero); 1506 } 1507 1508 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first"); 1509 } 1510 1511 assert(ConvertTypeForMem(DeleteTy) == 1512 cast<llvm::PointerType>(Ptr->getType())->getElementType()); 1513 1514 if (E->isArrayForm()) { 1515 EmitArrayDelete(*this, E, Ptr, DeleteTy); 1516 } else { 1517 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy, 1518 E->isGlobalDelete()); 1519 } 1520 1521 EmitBlock(DeleteEnd); 1522 } 1523 1524 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { 1525 // void __cxa_bad_typeid(); 1526 1527 llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); 1528 llvm::FunctionType *FTy = 1529 llvm::FunctionType::get(VoidTy, false); 1530 1531 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); 1532 } 1533 1534 static void EmitBadTypeidCall(CodeGenFunction &CGF) { 1535 llvm::Value *Fn = getBadTypeidFn(CGF); 1536 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); 1537 CGF.Builder.CreateUnreachable(); 1538 } 1539 1540 static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, 1541 const Expr *E, 1542 llvm::Type *StdTypeInfoPtrTy) { 1543 // Get the vtable pointer. 1544 llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); 1545 1546 // C++ [expr.typeid]p2: 1547 // If the glvalue expression is obtained by applying the unary * operator to 1548 // a pointer and the pointer is a null pointer value, the typeid expression 1549 // throws the std::bad_typeid exception. 1550 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { 1551 if (UO->getOpcode() == UO_Deref) { 1552 llvm::BasicBlock *BadTypeidBlock = 1553 CGF.createBasicBlock("typeid.bad_typeid"); 1554 llvm::BasicBlock *EndBlock = 1555 CGF.createBasicBlock("typeid.end"); 1556 1557 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr); 1558 CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); 1559 1560 CGF.EmitBlock(BadTypeidBlock); 1561 EmitBadTypeidCall(CGF); 1562 CGF.EmitBlock(EndBlock); 1563 } 1564 } 1565 1566 llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, 1567 StdTypeInfoPtrTy->getPointerTo()); 1568 1569 // Load the type info. 1570 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); 1571 return CGF.Builder.CreateLoad(Value); 1572 } 1573 1574 llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { 1575 llvm::Type *StdTypeInfoPtrTy = 1576 ConvertType(E->getType())->getPointerTo(); 1577 1578 if (E->isTypeOperand()) { 1579 llvm::Constant *TypeInfo = 1580 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); 1581 return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); 1582 } 1583 1584 // C++ [expr.typeid]p2: 1585 // When typeid is applied to a glvalue expression whose type is a 1586 // polymorphic class type, the result refers to a std::type_info object 1587 // representing the type of the most derived object (that is, the dynamic 1588 // type) to which the glvalue refers. 1589 if (E->getExprOperand()->isGLValue()) { 1590 if (const RecordType *RT = 1591 E->getExprOperand()->getType()->getAs<RecordType>()) { 1592 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1593 if (RD->isPolymorphic()) 1594 return EmitTypeidFromVTable(*this, E->getExprOperand(), 1595 StdTypeInfoPtrTy); 1596 } 1597 } 1598 1599 QualType OperandTy = E->getExprOperand()->getType(); 1600 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), 1601 StdTypeInfoPtrTy); 1602 } 1603 1604 static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { 1605 // void *__dynamic_cast(const void *sub, 1606 // const abi::__class_type_info *src, 1607 // const abi::__class_type_info *dst, 1608 // std::ptrdiff_t src2dst_offset); 1609 1610 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 1611 llvm::Type *PtrDiffTy = 1612 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1613 1614 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; 1615 1616 llvm::FunctionType *FTy = 1617 llvm::FunctionType::get(Int8PtrTy, Args, false); 1618 1619 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"); 1620 } 1621 1622 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { 1623 // void __cxa_bad_cast(); 1624 1625 llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext()); 1626 llvm::FunctionType *FTy = 1627 llvm::FunctionType::get(VoidTy, false); 1628 1629 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); 1630 } 1631 1632 static void EmitBadCastCall(CodeGenFunction &CGF) { 1633 llvm::Value *Fn = getBadCastFn(CGF); 1634 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn(); 1635 CGF.Builder.CreateUnreachable(); 1636 } 1637 1638 static llvm::Value * 1639 EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, 1640 QualType SrcTy, QualType DestTy, 1641 llvm::BasicBlock *CastEnd) { 1642 llvm::Type *PtrDiffLTy = 1643 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1644 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1645 1646 if (const PointerType *PTy = DestTy->getAs<PointerType>()) { 1647 if (PTy->getPointeeType()->isVoidType()) { 1648 // C++ [expr.dynamic.cast]p7: 1649 // If T is "pointer to cv void," then the result is a pointer to the 1650 // most derived object pointed to by v. 1651 1652 // Get the vtable pointer. 1653 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); 1654 1655 // Get the offset-to-top from the vtable. 1656 llvm::Value *OffsetToTop = 1657 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); 1658 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); 1659 1660 // Finally, add the offset to the pointer. 1661 Value = CGF.EmitCastToVoidPtr(Value); 1662 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); 1663 1664 return CGF.Builder.CreateBitCast(Value, DestLTy); 1665 } 1666 } 1667 1668 QualType SrcRecordTy; 1669 QualType DestRecordTy; 1670 1671 if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { 1672 SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); 1673 DestRecordTy = DestPTy->getPointeeType(); 1674 } else { 1675 SrcRecordTy = SrcTy; 1676 DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); 1677 } 1678 1679 assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); 1680 assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); 1681 1682 llvm::Value *SrcRTTI = 1683 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 1684 llvm::Value *DestRTTI = 1685 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 1686 1687 // FIXME: Actually compute a hint here. 1688 llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL); 1689 1690 // Emit the call to __dynamic_cast. 1691 Value = CGF.EmitCastToVoidPtr(Value); 1692 Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value, 1693 SrcRTTI, DestRTTI, OffsetHint); 1694 Value = CGF.Builder.CreateBitCast(Value, DestLTy); 1695 1696 /// C++ [expr.dynamic.cast]p9: 1697 /// A failed cast to reference type throws std::bad_cast 1698 if (DestTy->isReferenceType()) { 1699 llvm::BasicBlock *BadCastBlock = 1700 CGF.createBasicBlock("dynamic_cast.bad_cast"); 1701 1702 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); 1703 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); 1704 1705 CGF.EmitBlock(BadCastBlock); 1706 EmitBadCastCall(CGF); 1707 } 1708 1709 return Value; 1710 } 1711 1712 static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, 1713 QualType DestTy) { 1714 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1715 if (DestTy->isPointerType()) 1716 return llvm::Constant::getNullValue(DestLTy); 1717 1718 /// C++ [expr.dynamic.cast]p9: 1719 /// A failed cast to reference type throws std::bad_cast 1720 EmitBadCastCall(CGF); 1721 1722 CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); 1723 return llvm::UndefValue::get(DestLTy); 1724 } 1725 1726 llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, 1727 const CXXDynamicCastExpr *DCE) { 1728 QualType DestTy = DCE->getTypeAsWritten(); 1729 1730 if (DCE->isAlwaysNull()) 1731 return EmitDynamicCastToNull(*this, DestTy); 1732 1733 QualType SrcTy = DCE->getSubExpr()->getType(); 1734 1735 // C++ [expr.dynamic.cast]p4: 1736 // If the value of v is a null pointer value in the pointer case, the result 1737 // is the null pointer value of type T. 1738 bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); 1739 1740 llvm::BasicBlock *CastNull = 0; 1741 llvm::BasicBlock *CastNotNull = 0; 1742 llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); 1743 1744 if (ShouldNullCheckSrcValue) { 1745 CastNull = createBasicBlock("dynamic_cast.null"); 1746 CastNotNull = createBasicBlock("dynamic_cast.notnull"); 1747 1748 llvm::Value *IsNull = Builder.CreateIsNull(Value); 1749 Builder.CreateCondBr(IsNull, CastNull, CastNotNull); 1750 EmitBlock(CastNotNull); 1751 } 1752 1753 Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); 1754 1755 if (ShouldNullCheckSrcValue) { 1756 EmitBranch(CastEnd); 1757 1758 EmitBlock(CastNull); 1759 EmitBranch(CastEnd); 1760 } 1761 1762 EmitBlock(CastEnd); 1763 1764 if (ShouldNullCheckSrcValue) { 1765 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); 1766 PHI->addIncoming(Value, CastNotNull); 1767 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); 1768 1769 Value = PHI; 1770 } 1771 1772 return Value; 1773 } 1774