1199990Srdivacky//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// 2199990Srdivacky// 3199990Srdivacky// The LLVM Compiler Infrastructure 4199990Srdivacky// 5199990Srdivacky// This file is distributed under the University of Illinois Open Source 6199990Srdivacky// License. See LICENSE.TXT for details. 7199990Srdivacky// 8199990Srdivacky//===----------------------------------------------------------------------===// 9199990Srdivacky// 10199990Srdivacky// This contains code dealing with code generation of C++ expressions 11199990Srdivacky// 12199990Srdivacky//===----------------------------------------------------------------------===// 13199990Srdivacky 14199990Srdivacky#include "CodeGenFunction.h" 15226633Sdim#include "CGCUDARuntime.h" 16212904Sdim#include "CGCXXABI.h" 17249423Sdim#include "CGDebugInfo.h" 18208600Srdivacky#include "CGObjCRuntime.h" 19249423Sdim#include "clang/Frontend/CodeGenOptions.h" 20249423Sdim#include "llvm/IR/Intrinsics.h" 21221345Sdim#include "llvm/Support/CallSite.h" 22221345Sdim 23199990Srdivackyusing namespace clang; 24199990Srdivackyusing namespace CodeGen; 25199990Srdivacky 26202379SrdivackyRValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, 27243830Sdim SourceLocation CallLoc, 28202379Srdivacky llvm::Value *Callee, 29202379Srdivacky ReturnValueSlot ReturnValue, 30202379Srdivacky llvm::Value *This, 31249423Sdim llvm::Value *ImplicitParam, 32249423Sdim QualType ImplicitParamTy, 33202379Srdivacky CallExpr::const_arg_iterator ArgBeg, 34202379Srdivacky CallExpr::const_arg_iterator ArgEnd) { 35202379Srdivacky assert(MD->isInstance() && 36202379Srdivacky "Trying to emit a member call expr on a static method!"); 37202379Srdivacky 38243830Sdim // C++11 [class.mfct.non-static]p2: 39243830Sdim // If a non-static member function of a class X is called for an object that 40243830Sdim // is not of type X, or of a type derived from X, the behavior is undefined. 41243830Sdim EmitTypeCheck(isa<CXXConstructorDecl>(MD) ? TCK_ConstructorCall 42243830Sdim : TCK_MemberCall, 43243830Sdim CallLoc, This, getContext().getRecordType(MD->getParent())); 44243830Sdim 45202379Srdivacky CallArgList Args; 46202379Srdivacky 47202379Srdivacky // Push the this ptr. 48221345Sdim Args.add(RValue::get(This), MD->getThisType(getContext())); 49202379Srdivacky 50249423Sdim // If there is an implicit parameter (e.g. VTT), emit it. 51249423Sdim if (ImplicitParam) { 52249423Sdim Args.add(RValue::get(ImplicitParam), ImplicitParamTy); 53202379Srdivacky } 54234353Sdim 55234353Sdim const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 56234353Sdim RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size()); 57202379Srdivacky 58234353Sdim // And the rest of the call args. 59202379Srdivacky EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); 60202379Srdivacky 61239462Sdim return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), 62206084Srdivacky Callee, ReturnValue, Args, MD); 63202379Srdivacky} 64202379Srdivacky 65221345Sdim// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do 66221345Sdim// quite what we want. 67221345Sdimstatic const Expr *skipNoOpCastsAndParens(const Expr *E) { 68221345Sdim while (true) { 69221345Sdim if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { 70221345Sdim E = PE->getSubExpr(); 71221345Sdim continue; 72221345Sdim } 73221345Sdim 74221345Sdim if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 75221345Sdim if (CE->getCastKind() == CK_NoOp) { 76221345Sdim E = CE->getSubExpr(); 77221345Sdim continue; 78221345Sdim } 79221345Sdim } 80221345Sdim if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 81221345Sdim if (UO->getOpcode() == UO_Extension) { 82221345Sdim E = UO->getSubExpr(); 83221345Sdim continue; 84221345Sdim } 85221345Sdim } 86221345Sdim return E; 87221345Sdim } 88221345Sdim} 89221345Sdim 90202379Srdivacky/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given 91202379Srdivacky/// expr can be devirtualized. 92218893Sdimstatic bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, 93218893Sdim const Expr *Base, 94218893Sdim const CXXMethodDecl *MD) { 95218893Sdim 96218893Sdim // When building with -fapple-kext, all calls must go through the vtable since 97218893Sdim // the kernel linker can do runtime patching of vtables. 98234353Sdim if (Context.getLangOpts().AppleKext) 99218893Sdim return false; 100218893Sdim 101218893Sdim // If the most derived class is marked final, we know that no subclass can 102218893Sdim // override this member function and so we can devirtualize it. For example: 103218893Sdim // 104218893Sdim // struct A { virtual void f(); } 105218893Sdim // struct B final : A { }; 106218893Sdim // 107218893Sdim // void f(B *b) { 108218893Sdim // b->f(); 109218893Sdim // } 110218893Sdim // 111239462Sdim const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); 112218893Sdim if (MostDerivedClassDecl->hasAttr<FinalAttr>()) 113218893Sdim return true; 114218893Sdim 115218893Sdim // If the member function is marked 'final', we know that it can't be 116218893Sdim // overridden and can therefore devirtualize it. 117218893Sdim if (MD->hasAttr<FinalAttr>()) 118218893Sdim return true; 119218893Sdim 120218893Sdim // Similarly, if the class itself is marked 'final' it can't be overridden 121218893Sdim // and we can therefore devirtualize the member function call. 122218893Sdim if (MD->getParent()->hasAttr<FinalAttr>()) 123218893Sdim return true; 124218893Sdim 125221345Sdim Base = skipNoOpCastsAndParens(Base); 126202379Srdivacky if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { 127202379Srdivacky if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 128202379Srdivacky // This is a record decl. We know the type and can devirtualize it. 129202379Srdivacky return VD->getType()->isRecordType(); 130202379Srdivacky } 131202379Srdivacky 132202379Srdivacky return false; 133202379Srdivacky } 134239462Sdim 135239462Sdim // We can devirtualize calls on an object accessed by a class member access 136239462Sdim // expression, since by C++11 [basic.life]p6 we know that it can't refer to 137239462Sdim // a derived class object constructed in the same location. 138239462Sdim if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base)) 139239462Sdim if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl())) 140239462Sdim return VD->getType()->isRecordType(); 141239462Sdim 142202379Srdivacky // We can always devirtualize calls on temporary object expressions. 143203955Srdivacky if (isa<CXXConstructExpr>(Base)) 144202379Srdivacky return true; 145202379Srdivacky 146202379Srdivacky // And calls on bound temporaries. 147202379Srdivacky if (isa<CXXBindTemporaryExpr>(Base)) 148202379Srdivacky return true; 149202379Srdivacky 150202379Srdivacky // Check if this is a call expr that returns a record type. 151202379Srdivacky if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) 152202379Srdivacky return CE->getCallReturnType()->isRecordType(); 153218893Sdim 154202379Srdivacky // We can't devirtualize the call. 155202379Srdivacky return false; 156202379Srdivacky} 157202379Srdivacky 158239462Sdimstatic CXXRecordDecl *getCXXRecord(const Expr *E) { 159239462Sdim QualType T = E->getType(); 160239462Sdim if (const PointerType *PTy = T->getAs<PointerType>()) 161239462Sdim T = PTy->getPointeeType(); 162239462Sdim const RecordType *Ty = T->castAs<RecordType>(); 163239462Sdim return cast<CXXRecordDecl>(Ty->getDecl()); 164239462Sdim} 165239462Sdim 166218893Sdim// Note: This function also emit constructor calls to support a MSVC 167218893Sdim// extensions allowing explicit constructor function call. 168202379SrdivackyRValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, 169202379Srdivacky ReturnValueSlot ReturnValue) { 170221345Sdim const Expr *callee = CE->getCallee()->IgnoreParens(); 171221345Sdim 172221345Sdim if (isa<BinaryOperator>(callee)) 173202379Srdivacky return EmitCXXMemberPointerCallExpr(CE, ReturnValue); 174221345Sdim 175221345Sdim const MemberExpr *ME = cast<MemberExpr>(callee); 176202379Srdivacky const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 177202379Srdivacky 178218893Sdim CGDebugInfo *DI = getDebugInfo(); 179243830Sdim if (DI && 180243830Sdim CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo && 181243830Sdim !isa<CallExpr>(ME->getBase())) { 182218893Sdim QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType(); 183218893Sdim if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) { 184218893Sdim DI->getOrCreateRecordType(PTy->getPointeeType(), 185218893Sdim MD->getParent()->getLocation()); 186218893Sdim } 187218893Sdim } 188218893Sdim 189202379Srdivacky if (MD->isStatic()) { 190202379Srdivacky // The method is static, emit it as we would a regular call. 191202379Srdivacky llvm::Value *Callee = CGM.GetAddrOfFunction(MD); 192202379Srdivacky return EmitCall(getContext().getPointerType(MD->getType()), Callee, 193202379Srdivacky ReturnValue, CE->arg_begin(), CE->arg_end()); 194202379Srdivacky } 195202379Srdivacky 196212904Sdim // Compute the object pointer. 197239462Sdim const Expr *Base = ME->getBase(); 198239462Sdim bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier(); 199239462Sdim 200239462Sdim const CXXMethodDecl *DevirtualizedMethod = NULL; 201239462Sdim if (CanUseVirtualCall && 202239462Sdim canDevirtualizeMemberFunctionCalls(getContext(), Base, MD)) { 203239462Sdim const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); 204239462Sdim DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); 205239462Sdim assert(DevirtualizedMethod); 206239462Sdim const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); 207239462Sdim const Expr *Inner = Base->ignoreParenBaseCasts(); 208239462Sdim if (getCXXRecord(Inner) == DevirtualizedClass) 209239462Sdim // If the class of the Inner expression is where the dynamic method 210239462Sdim // is defined, build the this pointer from it. 211239462Sdim Base = Inner; 212239462Sdim else if (getCXXRecord(Base) != DevirtualizedClass) { 213239462Sdim // If the method is defined in a class that is not the best dynamic 214239462Sdim // one or the one of the full expression, we would have to build 215239462Sdim // a derived-to-base cast to compute the correct this pointer, but 216239462Sdim // we don't have support for that yet, so do a virtual call. 217239462Sdim DevirtualizedMethod = NULL; 218239462Sdim } 219239462Sdim // If the return types are not the same, this might be a case where more 220239462Sdim // code needs to run to compensate for it. For example, the derived 221239462Sdim // method might return a type that inherits form from the return 222239462Sdim // type of MD and has a prefix. 223239462Sdim // For now we just avoid devirtualizing these covariant cases. 224239462Sdim if (DevirtualizedMethod && 225239462Sdim DevirtualizedMethod->getResultType().getCanonicalType() != 226239462Sdim MD->getResultType().getCanonicalType()) 227239462Sdim DevirtualizedMethod = NULL; 228239462Sdim } 229239462Sdim 230202379Srdivacky llvm::Value *This; 231202379Srdivacky if (ME->isArrow()) 232239462Sdim This = EmitScalarExpr(Base); 233218893Sdim else 234239462Sdim This = EmitLValue(Base).getAddress(); 235202379Srdivacky 236239462Sdim 237212904Sdim if (MD->isTrivial()) { 238212904Sdim if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); 239218893Sdim if (isa<CXXConstructorDecl>(MD) && 240218893Sdim cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) 241218893Sdim return RValue::get(0); 242212904Sdim 243226633Sdim if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { 244226633Sdim // We don't like to generate the trivial copy/move assignment operator 245226633Sdim // when it isn't necessary; just produce the proper effect here. 246218893Sdim llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); 247243830Sdim EmitAggregateAssign(This, RHS, CE->getType()); 248218893Sdim return RValue::get(This); 249218893Sdim } 250218893Sdim 251218893Sdim if (isa<CXXConstructorDecl>(MD) && 252226633Sdim cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { 253226633Sdim // Trivial move and copy ctor are the same. 254218893Sdim llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); 255218893Sdim EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, 256218893Sdim CE->arg_begin(), CE->arg_end()); 257218893Sdim return RValue::get(This); 258218893Sdim } 259218893Sdim llvm_unreachable("unknown trivial member function"); 260202379Srdivacky } 261202379Srdivacky 262212904Sdim // Compute the function type we're calling. 263243830Sdim const CXXMethodDecl *CalleeDecl = DevirtualizedMethod ? DevirtualizedMethod : MD; 264218893Sdim const CGFunctionInfo *FInfo = 0; 265243830Sdim if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) 266243830Sdim FInfo = &CGM.getTypes().arrangeCXXDestructor(Dtor, 267234353Sdim Dtor_Complete); 268243830Sdim else if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl)) 269243830Sdim FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, 270243830Sdim Ctor_Complete); 271218893Sdim else 272243830Sdim FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); 273212904Sdim 274234353Sdim llvm::Type *Ty = CGM.getTypes().GetFunctionType(*FInfo); 275212904Sdim 276202379Srdivacky // C++ [class.virtual]p12: 277202379Srdivacky // Explicit qualification with the scope operator (5.1) suppresses the 278202379Srdivacky // virtual call mechanism. 279202379Srdivacky // 280202379Srdivacky // We also don't emit a virtual call if the base expression has a record type 281202379Srdivacky // because then we know what the type is. 282239462Sdim bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; 283239462Sdim 284202379Srdivacky llvm::Value *Callee; 285212904Sdim if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { 286212904Sdim if (UseVirtualCall) { 287249423Sdim assert(CE->arg_begin() == CE->arg_end() && 288249423Sdim "Virtual destructor shouldn't have explicit parameters"); 289249423Sdim return CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, 290249423Sdim Dtor_Complete, 291249423Sdim CE->getExprLoc(), 292249423Sdim ReturnValue, This); 293202379Srdivacky } else { 294243830Sdim if (getLangOpts().AppleKext && 295218893Sdim MD->isVirtual() && 296218893Sdim ME->hasQualifier()) 297218893Sdim Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); 298239462Sdim else if (!DevirtualizedMethod) 299218893Sdim Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty); 300239462Sdim else { 301239462Sdim const CXXDestructorDecl *DDtor = 302239462Sdim cast<CXXDestructorDecl>(DevirtualizedMethod); 303239462Sdim Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty); 304239462Sdim } 305202379Srdivacky } 306218893Sdim } else if (const CXXConstructorDecl *Ctor = 307218893Sdim dyn_cast<CXXConstructorDecl>(MD)) { 308218893Sdim Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); 309212904Sdim } else if (UseVirtualCall) { 310218893Sdim Callee = BuildVirtualCall(MD, This, Ty); 311202379Srdivacky } else { 312243830Sdim if (getLangOpts().AppleKext && 313218893Sdim MD->isVirtual() && 314218893Sdim ME->hasQualifier()) 315218893Sdim Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); 316239462Sdim else if (!DevirtualizedMethod) 317218893Sdim Callee = CGM.GetAddrOfFunction(MD, Ty); 318239462Sdim else { 319239462Sdim Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty); 320239462Sdim } 321202379Srdivacky } 322202379Srdivacky 323243830Sdim return EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This, 324249423Sdim /*ImplicitParam=*/0, QualType(), 325249423Sdim CE->arg_begin(), CE->arg_end()); 326202379Srdivacky} 327202379Srdivacky 328202379SrdivackyRValue 329202379SrdivackyCodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, 330202379Srdivacky ReturnValueSlot ReturnValue) { 331202379Srdivacky const BinaryOperator *BO = 332202379Srdivacky cast<BinaryOperator>(E->getCallee()->IgnoreParens()); 333202379Srdivacky const Expr *BaseExpr = BO->getLHS(); 334202379Srdivacky const Expr *MemFnExpr = BO->getRHS(); 335202379Srdivacky 336202379Srdivacky const MemberPointerType *MPT = 337221345Sdim MemFnExpr->getType()->castAs<MemberPointerType>(); 338212904Sdim 339202379Srdivacky const FunctionProtoType *FPT = 340221345Sdim MPT->getPointeeType()->castAs<FunctionProtoType>(); 341202379Srdivacky const CXXRecordDecl *RD = 342202379Srdivacky cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 343202379Srdivacky 344202379Srdivacky // Get the member function pointer. 345212904Sdim llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); 346202379Srdivacky 347202379Srdivacky // Emit the 'this' pointer. 348202379Srdivacky llvm::Value *This; 349202379Srdivacky 350212904Sdim if (BO->getOpcode() == BO_PtrMemI) 351202379Srdivacky This = EmitScalarExpr(BaseExpr); 352202379Srdivacky else 353202379Srdivacky This = EmitLValue(BaseExpr).getAddress(); 354202379Srdivacky 355243830Sdim EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This, 356243830Sdim QualType(MPT->getClass(), 0)); 357243830Sdim 358212904Sdim // Ask the ABI to load the callee. Note that This is modified. 359212904Sdim llvm::Value *Callee = 360218893Sdim CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); 361202379Srdivacky 362202379Srdivacky CallArgList Args; 363202379Srdivacky 364202379Srdivacky QualType ThisType = 365202379Srdivacky getContext().getPointerType(getContext().getTagDeclType(RD)); 366202379Srdivacky 367202379Srdivacky // Push the this ptr. 368221345Sdim Args.add(RValue::get(This), ThisType); 369239462Sdim 370239462Sdim RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); 371202379Srdivacky 372202379Srdivacky // And the rest of the call args 373202379Srdivacky EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); 374239462Sdim return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), Callee, 375202379Srdivacky ReturnValue, Args); 376202379Srdivacky} 377202379Srdivacky 378202379SrdivackyRValue 379202379SrdivackyCodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 380202379Srdivacky const CXXMethodDecl *MD, 381202379Srdivacky ReturnValueSlot ReturnValue) { 382202379Srdivacky assert(MD->isInstance() && 383202379Srdivacky "Trying to emit a member call expr on a static method!"); 384218893Sdim LValue LV = EmitLValue(E->getArg(0)); 385218893Sdim llvm::Value *This = LV.getAddress(); 386218893Sdim 387226633Sdim if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && 388226633Sdim MD->isTrivial()) { 389226633Sdim llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); 390226633Sdim QualType Ty = E->getType(); 391243830Sdim EmitAggregateAssign(This, Src, Ty); 392226633Sdim return RValue::get(This); 393202379Srdivacky } 394202379Srdivacky 395223017Sdim llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This); 396243830Sdim return EmitCXXMemberCall(MD, E->getExprLoc(), Callee, ReturnValue, This, 397249423Sdim /*ImplicitParam=*/0, QualType(), 398249423Sdim E->arg_begin() + 1, E->arg_end()); 399202379Srdivacky} 400202379Srdivacky 401226633SdimRValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, 402226633Sdim ReturnValueSlot ReturnValue) { 403226633Sdim return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); 404226633Sdim} 405226633Sdim 406226633Sdimstatic void EmitNullBaseClassInitialization(CodeGenFunction &CGF, 407226633Sdim llvm::Value *DestPtr, 408226633Sdim const CXXRecordDecl *Base) { 409226633Sdim if (Base->isEmpty()) 410226633Sdim return; 411226633Sdim 412226633Sdim DestPtr = CGF.EmitCastToVoidPtr(DestPtr); 413226633Sdim 414226633Sdim const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); 415226633Sdim CharUnits Size = Layout.getNonVirtualSize(); 416226633Sdim CharUnits Align = Layout.getNonVirtualAlign(); 417226633Sdim 418226633Sdim llvm::Value *SizeVal = CGF.CGM.getSize(Size); 419226633Sdim 420226633Sdim // If the type contains a pointer to data member we can't memset it to zero. 421226633Sdim // Instead, create a null constant and copy it to the destination. 422226633Sdim // TODO: there are other patterns besides zero that we can usefully memset, 423226633Sdim // like -1, which happens to be the pattern used by member-pointers. 424226633Sdim // TODO: isZeroInitializable can be over-conservative in the case where a 425226633Sdim // virtual base contains a member pointer. 426226633Sdim if (!CGF.CGM.getTypes().isZeroInitializable(Base)) { 427226633Sdim llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base); 428226633Sdim 429226633Sdim llvm::GlobalVariable *NullVariable = 430226633Sdim new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(), 431226633Sdim /*isConstant=*/true, 432226633Sdim llvm::GlobalVariable::PrivateLinkage, 433226633Sdim NullConstant, Twine()); 434226633Sdim NullVariable->setAlignment(Align.getQuantity()); 435226633Sdim llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable); 436226633Sdim 437226633Sdim // Get and call the appropriate llvm.memcpy overload. 438226633Sdim CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity()); 439226633Sdim return; 440226633Sdim } 441226633Sdim 442226633Sdim // Otherwise, just memset the whole thing to zero. This is legal 443226633Sdim // because in LLVM, all default initializers (other than the ones we just 444226633Sdim // handled above) are guaranteed to have a bit pattern of all zeros. 445226633Sdim CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal, 446226633Sdim Align.getQuantity()); 447226633Sdim} 448226633Sdim 449202379Srdivackyvoid 450218893SdimCodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, 451218893Sdim AggValueSlot Dest) { 452218893Sdim assert(!Dest.isIgnored() && "Must have a destination!"); 453202379Srdivacky const CXXConstructorDecl *CD = E->getConstructor(); 454212904Sdim 455212904Sdim // If we require zero initialization before (or instead of) calling the 456212904Sdim // constructor, as can be the case with a non-user-provided default 457221345Sdim // constructor, emit the zero initialization now, unless destination is 458221345Sdim // already zeroed. 459226633Sdim if (E->requiresZeroInitialization() && !Dest.isZeroed()) { 460226633Sdim switch (E->getConstructionKind()) { 461226633Sdim case CXXConstructExpr::CK_Delegating: 462226633Sdim case CXXConstructExpr::CK_Complete: 463226633Sdim EmitNullInitialization(Dest.getAddr(), E->getType()); 464226633Sdim break; 465226633Sdim case CXXConstructExpr::CK_VirtualBase: 466226633Sdim case CXXConstructExpr::CK_NonVirtualBase: 467226633Sdim EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent()); 468226633Sdim break; 469226633Sdim } 470226633Sdim } 471212904Sdim 472212904Sdim // If this is a call to a trivial default constructor, do nothing. 473212904Sdim if (CD->isTrivial() && CD->isDefaultConstructor()) 474212904Sdim return; 475212904Sdim 476218893Sdim // Elide the constructor if we're constructing from a temporary. 477218893Sdim // The temporary check is required because Sema sets this on NRVO 478218893Sdim // returns. 479243830Sdim if (getLangOpts().ElideConstructors && E->isElidable()) { 480218893Sdim assert(getContext().hasSameUnqualifiedType(E->getType(), 481218893Sdim E->getArg(0)->getType())); 482218893Sdim if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { 483218893Sdim EmitAggExpr(E->getArg(0), Dest); 484208600Srdivacky return; 485208600Srdivacky } 486202379Srdivacky } 487212904Sdim 488224145Sdim if (const ConstantArrayType *arrayType 489224145Sdim = getContext().getAsConstantArrayType(E->getType())) { 490224145Sdim EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), 491202379Srdivacky E->arg_begin(), E->arg_end()); 492224145Sdim } else { 493223017Sdim CXXCtorType Type = Ctor_Complete; 494223017Sdim bool ForVirtualBase = false; 495249423Sdim bool Delegating = false; 496249423Sdim 497223017Sdim switch (E->getConstructionKind()) { 498223017Sdim case CXXConstructExpr::CK_Delegating: 499221345Sdim // We should be emitting a constructor; GlobalDecl will assert this 500221345Sdim Type = CurGD.getCtorType(); 501249423Sdim Delegating = true; 502223017Sdim break; 503223017Sdim 504223017Sdim case CXXConstructExpr::CK_Complete: 505223017Sdim Type = Ctor_Complete; 506223017Sdim break; 507223017Sdim 508223017Sdim case CXXConstructExpr::CK_VirtualBase: 509223017Sdim ForVirtualBase = true; 510223017Sdim // fall-through 511223017Sdim 512223017Sdim case CXXConstructExpr::CK_NonVirtualBase: 513223017Sdim Type = Ctor_Base; 514221345Sdim } 515207619Srdivacky 516202379Srdivacky // Call the constructor. 517249423Sdim EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest.getAddr(), 518202379Srdivacky E->arg_begin(), E->arg_end()); 519207619Srdivacky } 520202379Srdivacky} 521202379Srdivacky 522218893Sdimvoid 523218893SdimCodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, 524218893Sdim llvm::Value *Src, 525218893Sdim const Expr *Exp) { 526218893Sdim if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) 527218893Sdim Exp = E->getSubExpr(); 528218893Sdim assert(isa<CXXConstructExpr>(Exp) && 529218893Sdim "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); 530218893Sdim const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); 531218893Sdim const CXXConstructorDecl *CD = E->getConstructor(); 532218893Sdim RunCleanupsScope Scope(*this); 533218893Sdim 534218893Sdim // If we require zero initialization before (or instead of) calling the 535218893Sdim // constructor, as can be the case with a non-user-provided default 536218893Sdim // constructor, emit the zero initialization now. 537218893Sdim // FIXME. Do I still need this for a copy ctor synthesis? 538218893Sdim if (E->requiresZeroInitialization()) 539218893Sdim EmitNullInitialization(Dest, E->getType()); 540218893Sdim 541218893Sdim assert(!getContext().getAsConstantArrayType(E->getType()) 542218893Sdim && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); 543218893Sdim EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, 544218893Sdim E->arg_begin(), E->arg_end()); 545218893Sdim} 546218893Sdim 547212904Sdimstatic CharUnits CalculateCookiePadding(CodeGenFunction &CGF, 548212904Sdim const CXXNewExpr *E) { 549200583Srdivacky if (!E->isArray()) 550203955Srdivacky return CharUnits::Zero(); 551200583Srdivacky 552223017Sdim // No cookie is required if the operator new[] being used is the 553223017Sdim // reserved placement operator new[]. 554223017Sdim if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) 555212904Sdim return CharUnits::Zero(); 556212904Sdim 557218893Sdim return CGF.CGM.getCXXABI().GetArrayCookieSize(E); 558200583Srdivacky} 559200583Srdivacky 560223017Sdimstatic llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, 561223017Sdim const CXXNewExpr *e, 562234353Sdim unsigned minElements, 563223017Sdim llvm::Value *&numElements, 564223017Sdim llvm::Value *&sizeWithoutCookie) { 565223017Sdim QualType type = e->getAllocatedType(); 566199990Srdivacky 567223017Sdim if (!e->isArray()) { 568223017Sdim CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 569223017Sdim sizeWithoutCookie 570223017Sdim = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); 571223017Sdim return sizeWithoutCookie; 572199990Srdivacky } 573212904Sdim 574223017Sdim // The width of size_t. 575223017Sdim unsigned sizeWidth = CGF.SizeTy->getBitWidth(); 576223017Sdim 577212904Sdim // Figure out the cookie size. 578223017Sdim llvm::APInt cookieSize(sizeWidth, 579223017Sdim CalculateCookiePadding(CGF, e).getQuantity()); 580212904Sdim 581199990Srdivacky // Emit the array size expression. 582212904Sdim // We multiply the size of all dimensions for NumElements. 583212904Sdim // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. 584223017Sdim numElements = CGF.EmitScalarExpr(e->getArraySize()); 585223017Sdim assert(isa<llvm::IntegerType>(numElements->getType())); 586212904Sdim 587223017Sdim // The number of elements can be have an arbitrary integer type; 588223017Sdim // essentially, we need to multiply it by a constant factor, add a 589223017Sdim // cookie size, and verify that the result is representable as a 590223017Sdim // size_t. That's just a gloss, though, and it's wrong in one 591223017Sdim // important way: if the count is negative, it's an error even if 592223017Sdim // the cookie size would bring the total size >= 0. 593223017Sdim bool isSigned 594223017Sdim = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); 595226633Sdim llvm::IntegerType *numElementsType 596223017Sdim = cast<llvm::IntegerType>(numElements->getType()); 597223017Sdim unsigned numElementsWidth = numElementsType->getBitWidth(); 598223017Sdim 599223017Sdim // Compute the constant factor. 600223017Sdim llvm::APInt arraySizeMultiplier(sizeWidth, 1); 601212904Sdim while (const ConstantArrayType *CAT 602223017Sdim = CGF.getContext().getAsConstantArrayType(type)) { 603223017Sdim type = CAT->getElementType(); 604223017Sdim arraySizeMultiplier *= CAT->getSize(); 605212904Sdim } 606212904Sdim 607223017Sdim CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 608223017Sdim llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); 609223017Sdim typeSizeMultiplier *= arraySizeMultiplier; 610223017Sdim 611223017Sdim // This will be a size_t. 612223017Sdim llvm::Value *size; 613199990Srdivacky 614212904Sdim // If someone is doing 'new int[42]' there is no need to do a dynamic check. 615212904Sdim // Don't bloat the -O0 code. 616223017Sdim if (llvm::ConstantInt *numElementsC = 617223017Sdim dyn_cast<llvm::ConstantInt>(numElements)) { 618223017Sdim const llvm::APInt &count = numElementsC->getValue(); 619212904Sdim 620223017Sdim bool hasAnyOverflow = false; 621212904Sdim 622223017Sdim // If 'count' was a negative number, it's an overflow. 623223017Sdim if (isSigned && count.isNegative()) 624223017Sdim hasAnyOverflow = true; 625212904Sdim 626223017Sdim // We want to do all this arithmetic in size_t. If numElements is 627223017Sdim // wider than that, check whether it's already too big, and if so, 628223017Sdim // overflow. 629223017Sdim else if (numElementsWidth > sizeWidth && 630223017Sdim numElementsWidth - sizeWidth > count.countLeadingZeros()) 631223017Sdim hasAnyOverflow = true; 632223017Sdim 633223017Sdim // Okay, compute a count at the right width. 634223017Sdim llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); 635223017Sdim 636234353Sdim // If there is a brace-initializer, we cannot allocate fewer elements than 637234353Sdim // there are initializers. If we do, that's treated like an overflow. 638234353Sdim if (adjustedCount.ult(minElements)) 639234353Sdim hasAnyOverflow = true; 640234353Sdim 641223017Sdim // Scale numElements by that. This might overflow, but we don't 642223017Sdim // care because it only overflows if allocationSize does, too, and 643223017Sdim // if that overflows then we shouldn't use this. 644223017Sdim numElements = llvm::ConstantInt::get(CGF.SizeTy, 645223017Sdim adjustedCount * arraySizeMultiplier); 646223017Sdim 647223017Sdim // Compute the size before cookie, and track whether it overflowed. 648223017Sdim bool overflow; 649223017Sdim llvm::APInt allocationSize 650223017Sdim = adjustedCount.umul_ov(typeSizeMultiplier, overflow); 651223017Sdim hasAnyOverflow |= overflow; 652223017Sdim 653223017Sdim // Add in the cookie, and check whether it's overflowed. 654223017Sdim if (cookieSize != 0) { 655223017Sdim // Save the current size without a cookie. This shouldn't be 656223017Sdim // used if there was overflow. 657223017Sdim sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 658223017Sdim 659223017Sdim allocationSize = allocationSize.uadd_ov(cookieSize, overflow); 660223017Sdim hasAnyOverflow |= overflow; 661212904Sdim } 662223017Sdim 663223017Sdim // On overflow, produce a -1 so operator new will fail. 664223017Sdim if (hasAnyOverflow) { 665223017Sdim size = llvm::Constant::getAllOnesValue(CGF.SizeTy); 666212904Sdim } else { 667223017Sdim size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 668212904Sdim } 669212904Sdim 670223017Sdim // Otherwise, we might need to use the overflow intrinsics. 671223017Sdim } else { 672234353Sdim // There are up to five conditions we need to test for: 673223017Sdim // 1) if isSigned, we need to check whether numElements is negative; 674223017Sdim // 2) if numElementsWidth > sizeWidth, we need to check whether 675223017Sdim // numElements is larger than something representable in size_t; 676234353Sdim // 3) if minElements > 0, we need to check whether numElements is smaller 677234353Sdim // than that. 678234353Sdim // 4) we need to compute 679223017Sdim // sizeWithoutCookie := numElements * typeSizeMultiplier 680223017Sdim // and check whether it overflows; and 681234353Sdim // 5) if we need a cookie, we need to compute 682223017Sdim // size := sizeWithoutCookie + cookieSize 683223017Sdim // and check whether it overflows. 684212904Sdim 685223017Sdim llvm::Value *hasOverflow = 0; 686212904Sdim 687223017Sdim // If numElementsWidth > sizeWidth, then one way or another, we're 688223017Sdim // going to have to do a comparison for (2), and this happens to 689223017Sdim // take care of (1), too. 690223017Sdim if (numElementsWidth > sizeWidth) { 691223017Sdim llvm::APInt threshold(numElementsWidth, 1); 692223017Sdim threshold <<= sizeWidth; 693212904Sdim 694223017Sdim llvm::Value *thresholdV 695223017Sdim = llvm::ConstantInt::get(numElementsType, threshold); 696212904Sdim 697223017Sdim hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); 698223017Sdim numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); 699212904Sdim 700223017Sdim // Otherwise, if we're signed, we want to sext up to size_t. 701223017Sdim } else if (isSigned) { 702223017Sdim if (numElementsWidth < sizeWidth) 703223017Sdim numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); 704223017Sdim 705223017Sdim // If there's a non-1 type size multiplier, then we can do the 706223017Sdim // signedness check at the same time as we do the multiply 707223017Sdim // because a negative number times anything will cause an 708234353Sdim // unsigned overflow. Otherwise, we have to do it here. But at least 709234353Sdim // in this case, we can subsume the >= minElements check. 710223017Sdim if (typeSizeMultiplier == 1) 711223017Sdim hasOverflow = CGF.Builder.CreateICmpSLT(numElements, 712234353Sdim llvm::ConstantInt::get(CGF.SizeTy, minElements)); 713212904Sdim 714223017Sdim // Otherwise, zext up to size_t if necessary. 715223017Sdim } else if (numElementsWidth < sizeWidth) { 716223017Sdim numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); 717212904Sdim } 718212904Sdim 719223017Sdim assert(numElements->getType() == CGF.SizeTy); 720212904Sdim 721234353Sdim if (minElements) { 722234353Sdim // Don't allow allocation of fewer elements than we have initializers. 723234353Sdim if (!hasOverflow) { 724234353Sdim hasOverflow = CGF.Builder.CreateICmpULT(numElements, 725234353Sdim llvm::ConstantInt::get(CGF.SizeTy, minElements)); 726234353Sdim } else if (numElementsWidth > sizeWidth) { 727234353Sdim // The other existing overflow subsumes this check. 728234353Sdim // We do an unsigned comparison, since any signed value < -1 is 729234353Sdim // taken care of either above or below. 730234353Sdim hasOverflow = CGF.Builder.CreateOr(hasOverflow, 731234353Sdim CGF.Builder.CreateICmpULT(numElements, 732234353Sdim llvm::ConstantInt::get(CGF.SizeTy, minElements))); 733234353Sdim } 734234353Sdim } 735234353Sdim 736223017Sdim size = numElements; 737212904Sdim 738223017Sdim // Multiply by the type size if necessary. This multiplier 739223017Sdim // includes all the factors for nested arrays. 740212904Sdim // 741223017Sdim // This step also causes numElements to be scaled up by the 742223017Sdim // nested-array factor if necessary. Overflow on this computation 743223017Sdim // can be ignored because the result shouldn't be used if 744223017Sdim // allocation fails. 745223017Sdim if (typeSizeMultiplier != 1) { 746223017Sdim llvm::Value *umul_with_overflow 747224145Sdim = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); 748212904Sdim 749223017Sdim llvm::Value *tsmV = 750223017Sdim llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); 751223017Sdim llvm::Value *result = 752223017Sdim CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV); 753212904Sdim 754223017Sdim llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 755223017Sdim if (hasOverflow) 756223017Sdim hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 757223017Sdim else 758223017Sdim hasOverflow = overflowed; 759212904Sdim 760223017Sdim size = CGF.Builder.CreateExtractValue(result, 0); 761212904Sdim 762223017Sdim // Also scale up numElements by the array size multiplier. 763223017Sdim if (arraySizeMultiplier != 1) { 764223017Sdim // If the base element type size is 1, then we can re-use the 765223017Sdim // multiply we just did. 766223017Sdim if (typeSize.isOne()) { 767223017Sdim assert(arraySizeMultiplier == typeSizeMultiplier); 768223017Sdim numElements = size; 769212904Sdim 770223017Sdim // Otherwise we need a separate multiply. 771223017Sdim } else { 772223017Sdim llvm::Value *asmV = 773223017Sdim llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); 774223017Sdim numElements = CGF.Builder.CreateMul(numElements, asmV); 775223017Sdim } 776223017Sdim } 777223017Sdim } else { 778223017Sdim // numElements doesn't need to be scaled. 779223017Sdim assert(arraySizeMultiplier == 1); 780223017Sdim } 781223017Sdim 782223017Sdim // Add in the cookie size if necessary. 783223017Sdim if (cookieSize != 0) { 784223017Sdim sizeWithoutCookie = size; 785212904Sdim 786223017Sdim llvm::Value *uadd_with_overflow 787224145Sdim = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); 788223017Sdim 789223017Sdim llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); 790223017Sdim llvm::Value *result = 791223017Sdim CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV); 792223017Sdim 793223017Sdim llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 794223017Sdim if (hasOverflow) 795223017Sdim hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 796223017Sdim else 797223017Sdim hasOverflow = overflowed; 798223017Sdim 799223017Sdim size = CGF.Builder.CreateExtractValue(result, 0); 800212904Sdim } 801212904Sdim 802223017Sdim // If we had any possibility of dynamic overflow, make a select to 803223017Sdim // overwrite 'size' with an all-ones value, which should cause 804223017Sdim // operator new to throw. 805223017Sdim if (hasOverflow) 806223017Sdim size = CGF.Builder.CreateSelect(hasOverflow, 807223017Sdim llvm::Constant::getAllOnesValue(CGF.SizeTy), 808223017Sdim size); 809206084Srdivacky } 810199990Srdivacky 811223017Sdim if (cookieSize == 0) 812223017Sdim sizeWithoutCookie = size; 813212904Sdim else 814223017Sdim assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); 815212904Sdim 816223017Sdim return size; 817199990Srdivacky} 818199990Srdivacky 819234353Sdimstatic void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, 820234353Sdim QualType AllocType, llvm::Value *NewPtr) { 821212904Sdim 822234353Sdim CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType); 823249423Sdim switch (CGF.getEvaluationKind(AllocType)) { 824249423Sdim case TEK_Scalar: 825234353Sdim CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, 826234353Sdim Alignment), 827224145Sdim false); 828249423Sdim return; 829249423Sdim case TEK_Complex: 830249423Sdim CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType, 831249423Sdim Alignment), 832249423Sdim /*isInit*/ true); 833249423Sdim return; 834249423Sdim case TEK_Aggregate: { 835218893Sdim AggValueSlot Slot 836234353Sdim = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(), 837226633Sdim AggValueSlot::IsDestructed, 838226633Sdim AggValueSlot::DoesNotNeedGCBarriers, 839226633Sdim AggValueSlot::IsNotAliased); 840218893Sdim CGF.EmitAggExpr(Init, Slot); 841234353Sdim 842234353Sdim CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init); 843249423Sdim return; 844218893Sdim } 845249423Sdim } 846249423Sdim llvm_unreachable("bad evaluation kind"); 847210299Sed} 848210299Sed 849210299Sedvoid 850210299SedCodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, 851226633Sdim QualType elementType, 852226633Sdim llvm::Value *beginPtr, 853226633Sdim llvm::Value *numElements) { 854234353Sdim if (!E->hasInitializer()) 855234353Sdim return; // We have a POD type. 856226633Sdim 857234353Sdim llvm::Value *explicitPtr = beginPtr; 858226633Sdim // Find the end of the array, hoisted out of the loop. 859226633Sdim llvm::Value *endPtr = 860226633Sdim Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end"); 861226633Sdim 862234353Sdim unsigned initializerElements = 0; 863234353Sdim 864234353Sdim const Expr *Init = E->getInitializer(); 865234353Sdim llvm::AllocaInst *endOfInit = 0; 866234353Sdim QualType::DestructionKind dtorKind = elementType.isDestructedType(); 867234353Sdim EHScopeStack::stable_iterator cleanup; 868234353Sdim llvm::Instruction *cleanupDominator = 0; 869234353Sdim // If the initializer is an initializer list, first do the explicit elements. 870234353Sdim if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { 871234353Sdim initializerElements = ILE->getNumInits(); 872234353Sdim 873234353Sdim // Enter a partial-destruction cleanup if necessary. 874234353Sdim if (needsEHCleanup(dtorKind)) { 875234353Sdim // In principle we could tell the cleanup where we are more 876234353Sdim // directly, but the control flow can get so varied here that it 877234353Sdim // would actually be quite complex. Therefore we go through an 878234353Sdim // alloca. 879234353Sdim endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit"); 880234353Sdim cleanupDominator = Builder.CreateStore(beginPtr, endOfInit); 881234353Sdim pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType, 882234353Sdim getDestroyer(dtorKind)); 883234353Sdim cleanup = EHStack.stable_begin(); 884234353Sdim } 885234353Sdim 886234353Sdim for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { 887234353Sdim // Tell the cleanup that it needs to destroy up to this 888234353Sdim // element. TODO: some of these stores can be trivially 889234353Sdim // observed to be unnecessary. 890234353Sdim if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit); 891234353Sdim StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr); 892234353Sdim explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next"); 893234353Sdim } 894234353Sdim 895234353Sdim // The remaining elements are filled with the array filler expression. 896234353Sdim Init = ILE->getArrayFiller(); 897234353Sdim } 898234353Sdim 899226633Sdim // Create the continuation block. 900226633Sdim llvm::BasicBlock *contBB = createBasicBlock("new.loop.end"); 901226633Sdim 902234353Sdim // If the number of elements isn't constant, we have to now check if there is 903234353Sdim // anything left to initialize. 904234353Sdim if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) { 905234353Sdim // If all elements have already been initialized, skip the whole loop. 906234353Sdim if (constNum->getZExtValue() <= initializerElements) { 907234353Sdim // If there was a cleanup, deactivate it. 908234353Sdim if (cleanupDominator) 909243830Sdim DeactivateCleanupBlock(cleanup, cleanupDominator); 910234353Sdim return; 911234353Sdim } 912234353Sdim } else { 913226633Sdim llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty"); 914234353Sdim llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr, 915226633Sdim "array.isempty"); 916226633Sdim Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB); 917226633Sdim EmitBlock(nonEmptyBB); 918226633Sdim } 919226633Sdim 920226633Sdim // Enter the loop. 921226633Sdim llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 922226633Sdim llvm::BasicBlock *loopBB = createBasicBlock("new.loop"); 923226633Sdim 924226633Sdim EmitBlock(loopBB); 925226633Sdim 926226633Sdim // Set up the current-element phi. 927226633Sdim llvm::PHINode *curPtr = 928234353Sdim Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur"); 929234353Sdim curPtr->addIncoming(explicitPtr, entryBB); 930226633Sdim 931234353Sdim // Store the new cleanup position for irregular cleanups. 932234353Sdim if (endOfInit) Builder.CreateStore(curPtr, endOfInit); 933234353Sdim 934226633Sdim // Enter a partial-destruction cleanup if necessary. 935234353Sdim if (!cleanupDominator && needsEHCleanup(dtorKind)) { 936226633Sdim pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType, 937226633Sdim getDestroyer(dtorKind)); 938226633Sdim cleanup = EHStack.stable_begin(); 939234353Sdim cleanupDominator = Builder.CreateUnreachable(); 940226633Sdim } 941226633Sdim 942226633Sdim // Emit the initializer into this element. 943234353Sdim StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr); 944226633Sdim 945226633Sdim // Leave the cleanup if we entered one. 946234353Sdim if (cleanupDominator) { 947234353Sdim DeactivateCleanupBlock(cleanup, cleanupDominator); 948234353Sdim cleanupDominator->eraseFromParent(); 949234353Sdim } 950226633Sdim 951226633Sdim // Advance to the next element. 952226633Sdim llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next"); 953226633Sdim 954226633Sdim // Check whether we've gotten to the end of the array and, if so, 955226633Sdim // exit the loop. 956226633Sdim llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend"); 957226633Sdim Builder.CreateCondBr(isEnd, contBB, loopBB); 958226633Sdim curPtr->addIncoming(nextPtr, Builder.GetInsertBlock()); 959226633Sdim 960226633Sdim EmitBlock(contBB); 961210299Sed} 962210299Sed 963212904Sdimstatic void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, 964212904Sdim llvm::Value *NewPtr, llvm::Value *Size) { 965218893Sdim CGF.EmitCastToVoidPtr(NewPtr); 966218893Sdim CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); 967218893Sdim CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, 968218893Sdim Alignment.getQuantity(), false); 969212904Sdim} 970212904Sdim 971199990Srdivackystatic void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, 972226633Sdim QualType ElementType, 973199990Srdivacky llvm::Value *NewPtr, 974212904Sdim llvm::Value *NumElements, 975212904Sdim llvm::Value *AllocSizeWithoutCookie) { 976234353Sdim const Expr *Init = E->getInitializer(); 977199990Srdivacky if (E->isArray()) { 978234353Sdim if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){ 979234353Sdim CXXConstructorDecl *Ctor = CCE->getConstructor(); 980234353Sdim if (Ctor->isTrivial()) { 981212904Sdim // If new expression did not specify value-initialization, then there 982212904Sdim // is no initialization. 983234353Sdim if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) 984212904Sdim return; 985212904Sdim 986226633Sdim if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) { 987212904Sdim // Optimization: since zero initialization will just set the memory 988212904Sdim // to all zeroes, generate a single memset to do it in one shot. 989226633Sdim EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); 990212904Sdim return; 991212904Sdim } 992212904Sdim } 993224145Sdim 994234353Sdim CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, 995234353Sdim CCE->arg_begin(), CCE->arg_end(), 996243830Sdim CCE->requiresZeroInitialization()); 997207619Srdivacky return; 998234353Sdim } else if (Init && isa<ImplicitValueInitExpr>(Init) && 999234353Sdim CGF.CGM.getTypes().isZeroInitializable(ElementType)) { 1000212904Sdim // Optimization: since zero initialization will just set the memory 1001212904Sdim // to all zeroes, generate a single memset to do it in one shot. 1002226633Sdim EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); 1003226633Sdim return; 1004210299Sed } 1005234353Sdim CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements); 1006234353Sdim return; 1007199990Srdivacky } 1008199990Srdivacky 1009234353Sdim if (!Init) 1010234353Sdim return; 1011199990Srdivacky 1012234353Sdim StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr); 1013199990Srdivacky} 1014199990Srdivacky 1015218893Sdimnamespace { 1016218893Sdim /// A cleanup to call the given 'operator delete' function upon 1017218893Sdim /// abnormal exit from a new expression. 1018218893Sdim class CallDeleteDuringNew : public EHScopeStack::Cleanup { 1019218893Sdim size_t NumPlacementArgs; 1020218893Sdim const FunctionDecl *OperatorDelete; 1021218893Sdim llvm::Value *Ptr; 1022218893Sdim llvm::Value *AllocSize; 1023218893Sdim 1024218893Sdim RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } 1025218893Sdim 1026218893Sdim public: 1027218893Sdim static size_t getExtraSize(size_t NumPlacementArgs) { 1028218893Sdim return NumPlacementArgs * sizeof(RValue); 1029218893Sdim } 1030218893Sdim 1031218893Sdim CallDeleteDuringNew(size_t NumPlacementArgs, 1032218893Sdim const FunctionDecl *OperatorDelete, 1033218893Sdim llvm::Value *Ptr, 1034218893Sdim llvm::Value *AllocSize) 1035218893Sdim : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 1036218893Sdim Ptr(Ptr), AllocSize(AllocSize) {} 1037218893Sdim 1038218893Sdim void setPlacementArg(unsigned I, RValue Arg) { 1039218893Sdim assert(I < NumPlacementArgs && "index out of range"); 1040218893Sdim getPlacementArgs()[I] = Arg; 1041218893Sdim } 1042218893Sdim 1043224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1044218893Sdim const FunctionProtoType *FPT 1045218893Sdim = OperatorDelete->getType()->getAs<FunctionProtoType>(); 1046218893Sdim assert(FPT->getNumArgs() == NumPlacementArgs + 1 || 1047218893Sdim (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); 1048218893Sdim 1049218893Sdim CallArgList DeleteArgs; 1050218893Sdim 1051218893Sdim // The first argument is always a void*. 1052218893Sdim FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); 1053221345Sdim DeleteArgs.add(RValue::get(Ptr), *AI++); 1054218893Sdim 1055218893Sdim // A member 'operator delete' can take an extra 'size_t' argument. 1056218893Sdim if (FPT->getNumArgs() == NumPlacementArgs + 2) 1057221345Sdim DeleteArgs.add(RValue::get(AllocSize), *AI++); 1058218893Sdim 1059218893Sdim // Pass the rest of the arguments, which must match exactly. 1060218893Sdim for (unsigned I = 0; I != NumPlacementArgs; ++I) 1061221345Sdim DeleteArgs.add(getPlacementArgs()[I], *AI++); 1062218893Sdim 1063218893Sdim // Call 'operator delete'. 1064239462Sdim CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT), 1065218893Sdim CGF.CGM.GetAddrOfFunction(OperatorDelete), 1066218893Sdim ReturnValueSlot(), DeleteArgs, OperatorDelete); 1067218893Sdim } 1068218893Sdim }; 1069218893Sdim 1070218893Sdim /// A cleanup to call the given 'operator delete' function upon 1071218893Sdim /// abnormal exit from a new expression when the new expression is 1072218893Sdim /// conditional. 1073218893Sdim class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { 1074218893Sdim size_t NumPlacementArgs; 1075218893Sdim const FunctionDecl *OperatorDelete; 1076218893Sdim DominatingValue<RValue>::saved_type Ptr; 1077218893Sdim DominatingValue<RValue>::saved_type AllocSize; 1078218893Sdim 1079218893Sdim DominatingValue<RValue>::saved_type *getPlacementArgs() { 1080218893Sdim return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); 1081218893Sdim } 1082218893Sdim 1083218893Sdim public: 1084218893Sdim static size_t getExtraSize(size_t NumPlacementArgs) { 1085218893Sdim return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); 1086218893Sdim } 1087218893Sdim 1088218893Sdim CallDeleteDuringConditionalNew(size_t NumPlacementArgs, 1089218893Sdim const FunctionDecl *OperatorDelete, 1090218893Sdim DominatingValue<RValue>::saved_type Ptr, 1091218893Sdim DominatingValue<RValue>::saved_type AllocSize) 1092218893Sdim : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 1093218893Sdim Ptr(Ptr), AllocSize(AllocSize) {} 1094218893Sdim 1095218893Sdim void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { 1096218893Sdim assert(I < NumPlacementArgs && "index out of range"); 1097218893Sdim getPlacementArgs()[I] = Arg; 1098218893Sdim } 1099218893Sdim 1100224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1101218893Sdim const FunctionProtoType *FPT 1102218893Sdim = OperatorDelete->getType()->getAs<FunctionProtoType>(); 1103218893Sdim assert(FPT->getNumArgs() == NumPlacementArgs + 1 || 1104218893Sdim (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); 1105218893Sdim 1106218893Sdim CallArgList DeleteArgs; 1107218893Sdim 1108218893Sdim // The first argument is always a void*. 1109218893Sdim FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); 1110221345Sdim DeleteArgs.add(Ptr.restore(CGF), *AI++); 1111218893Sdim 1112218893Sdim // A member 'operator delete' can take an extra 'size_t' argument. 1113218893Sdim if (FPT->getNumArgs() == NumPlacementArgs + 2) { 1114218893Sdim RValue RV = AllocSize.restore(CGF); 1115221345Sdim DeleteArgs.add(RV, *AI++); 1116218893Sdim } 1117218893Sdim 1118218893Sdim // Pass the rest of the arguments, which must match exactly. 1119218893Sdim for (unsigned I = 0; I != NumPlacementArgs; ++I) { 1120218893Sdim RValue RV = getPlacementArgs()[I].restore(CGF); 1121221345Sdim DeleteArgs.add(RV, *AI++); 1122218893Sdim } 1123218893Sdim 1124218893Sdim // Call 'operator delete'. 1125239462Sdim CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT), 1126218893Sdim CGF.CGM.GetAddrOfFunction(OperatorDelete), 1127218893Sdim ReturnValueSlot(), DeleteArgs, OperatorDelete); 1128218893Sdim } 1129218893Sdim }; 1130218893Sdim} 1131218893Sdim 1132218893Sdim/// Enter a cleanup to call 'operator delete' if the initializer in a 1133218893Sdim/// new-expression throws. 1134218893Sdimstatic void EnterNewDeleteCleanup(CodeGenFunction &CGF, 1135218893Sdim const CXXNewExpr *E, 1136218893Sdim llvm::Value *NewPtr, 1137218893Sdim llvm::Value *AllocSize, 1138218893Sdim const CallArgList &NewArgs) { 1139218893Sdim // If we're not inside a conditional branch, then the cleanup will 1140218893Sdim // dominate and we can do the easier (and more efficient) thing. 1141218893Sdim if (!CGF.isInConditionalBranch()) { 1142218893Sdim CallDeleteDuringNew *Cleanup = CGF.EHStack 1143218893Sdim .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, 1144218893Sdim E->getNumPlacementArgs(), 1145218893Sdim E->getOperatorDelete(), 1146218893Sdim NewPtr, AllocSize); 1147218893Sdim for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1148221345Sdim Cleanup->setPlacementArg(I, NewArgs[I+1].RV); 1149218893Sdim 1150218893Sdim return; 1151218893Sdim } 1152218893Sdim 1153218893Sdim // Otherwise, we need to save all this stuff. 1154218893Sdim DominatingValue<RValue>::saved_type SavedNewPtr = 1155218893Sdim DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); 1156218893Sdim DominatingValue<RValue>::saved_type SavedAllocSize = 1157218893Sdim DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); 1158218893Sdim 1159218893Sdim CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack 1160234353Sdim .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, 1161218893Sdim E->getNumPlacementArgs(), 1162218893Sdim E->getOperatorDelete(), 1163218893Sdim SavedNewPtr, 1164218893Sdim SavedAllocSize); 1165218893Sdim for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1166218893Sdim Cleanup->setPlacementArg(I, 1167221345Sdim DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); 1168218893Sdim 1169234353Sdim CGF.initFullExprCleanup(); 1170218893Sdim} 1171218893Sdim 1172199990Srdivackyllvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { 1173221345Sdim // The element type being allocated. 1174221345Sdim QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); 1175212904Sdim 1176221345Sdim // 1. Build a call to the allocation function. 1177221345Sdim FunctionDecl *allocator = E->getOperatorNew(); 1178221345Sdim const FunctionProtoType *allocatorType = 1179221345Sdim allocator->getType()->castAs<FunctionProtoType>(); 1180199990Srdivacky 1181221345Sdim CallArgList allocatorArgs; 1182199990Srdivacky 1183199990Srdivacky // The allocation size is the first argument. 1184221345Sdim QualType sizeType = getContext().getSizeType(); 1185199990Srdivacky 1186234353Sdim // If there is a brace-initializer, cannot allocate fewer elements than inits. 1187234353Sdim unsigned minElements = 0; 1188234353Sdim if (E->isArray() && E->hasInitializer()) { 1189234353Sdim if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer())) 1190234353Sdim minElements = ILE->getNumInits(); 1191234353Sdim } 1192234353Sdim 1193221345Sdim llvm::Value *numElements = 0; 1194221345Sdim llvm::Value *allocSizeWithoutCookie = 0; 1195221345Sdim llvm::Value *allocSize = 1196234353Sdim EmitCXXNewAllocSize(*this, E, minElements, numElements, 1197234353Sdim allocSizeWithoutCookie); 1198199990Srdivacky 1199221345Sdim allocatorArgs.add(RValue::get(allocSize), sizeType); 1200199990Srdivacky 1201199990Srdivacky // Emit the rest of the arguments. 1202199990Srdivacky // FIXME: Ideally, this should just use EmitCallArgs. 1203221345Sdim CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); 1204199990Srdivacky 1205199990Srdivacky // First, use the types from the function type. 1206199990Srdivacky // We start at 1 here because the first argument (the allocation size) 1207199990Srdivacky // has already been emitted. 1208221345Sdim for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; 1209221345Sdim ++i, ++placementArg) { 1210221345Sdim QualType argType = allocatorType->getArgType(i); 1211199990Srdivacky 1212221345Sdim assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), 1213221345Sdim placementArg->getType()) && 1214199990Srdivacky "type mismatch in call argument!"); 1215199990Srdivacky 1216221345Sdim EmitCallArg(allocatorArgs, *placementArg, argType); 1217199990Srdivacky } 1218199990Srdivacky 1219199990Srdivacky // Either we've emitted all the call args, or we have a call to a 1220199990Srdivacky // variadic function. 1221221345Sdim assert((placementArg == E->placement_arg_end() || 1222221345Sdim allocatorType->isVariadic()) && 1223221345Sdim "Extra arguments to non-variadic function!"); 1224199990Srdivacky 1225199990Srdivacky // If we still have any arguments, emit them using the type of the argument. 1226221345Sdim for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); 1227221345Sdim placementArg != placementArgsEnd; ++placementArg) { 1228221345Sdim EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); 1229199990Srdivacky } 1230199990Srdivacky 1231223017Sdim // Emit the allocation call. If the allocator is a global placement 1232223017Sdim // operator, just "inline" it directly. 1233223017Sdim RValue RV; 1234223017Sdim if (allocator->isReservedGlobalPlacementOperator()) { 1235223017Sdim assert(allocatorArgs.size() == 2); 1236223017Sdim RV = allocatorArgs[1].RV; 1237223017Sdim // TODO: kill any unnecessary computations done for the size 1238223017Sdim // argument. 1239223017Sdim } else { 1240239462Sdim RV = EmitCall(CGM.getTypes().arrangeFreeFunctionCall(allocatorArgs, 1241239462Sdim allocatorType), 1242223017Sdim CGM.GetAddrOfFunction(allocator), ReturnValueSlot(), 1243223017Sdim allocatorArgs, allocator); 1244223017Sdim } 1245199990Srdivacky 1246221345Sdim // Emit a null check on the allocation result if the allocation 1247221345Sdim // function is allowed to return null (because it has a non-throwing 1248221345Sdim // exception spec; for this part, we inline 1249221345Sdim // CXXNewExpr::shouldNullCheckAllocation()) and we have an 1250221345Sdim // interesting initializer. 1251221345Sdim bool nullCheck = allocatorType->isNothrow(getContext()) && 1252234353Sdim (!allocType.isPODType(getContext()) || E->hasInitializer()); 1253199990Srdivacky 1254221345Sdim llvm::BasicBlock *nullCheckBB = 0; 1255221345Sdim llvm::BasicBlock *contBB = 0; 1256199990Srdivacky 1257221345Sdim llvm::Value *allocation = RV.getScalarVal(); 1258243830Sdim unsigned AS = allocation->getType()->getPointerAddressSpace(); 1259199990Srdivacky 1260221345Sdim // The null-check means that the initializer is conditionally 1261221345Sdim // evaluated. 1262221345Sdim ConditionalEvaluation conditional(*this); 1263199990Srdivacky 1264221345Sdim if (nullCheck) { 1265221345Sdim conditional.begin(*this); 1266221345Sdim 1267221345Sdim nullCheckBB = Builder.GetInsertBlock(); 1268221345Sdim llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); 1269221345Sdim contBB = createBasicBlock("new.cont"); 1270221345Sdim 1271221345Sdim llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); 1272221345Sdim Builder.CreateCondBr(isNull, contBB, notNullBB); 1273221345Sdim EmitBlock(notNullBB); 1274199990Srdivacky } 1275199990Srdivacky 1276218893Sdim // If there's an operator delete, enter a cleanup to call it if an 1277218893Sdim // exception is thrown. 1278221345Sdim EHScopeStack::stable_iterator operatorDeleteCleanup; 1279234353Sdim llvm::Instruction *cleanupDominator = 0; 1280223017Sdim if (E->getOperatorDelete() && 1281223017Sdim !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { 1282221345Sdim EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); 1283221345Sdim operatorDeleteCleanup = EHStack.stable_begin(); 1284234353Sdim cleanupDominator = Builder.CreateUnreachable(); 1285218893Sdim } 1286218893Sdim 1287226633Sdim assert((allocSize == allocSizeWithoutCookie) == 1288226633Sdim CalculateCookiePadding(*this, E).isZero()); 1289226633Sdim if (allocSize != allocSizeWithoutCookie) { 1290226633Sdim assert(E->isArray()); 1291226633Sdim allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, 1292226633Sdim numElements, 1293226633Sdim E, allocType); 1294226633Sdim } 1295226633Sdim 1296226633Sdim llvm::Type *elementPtrTy 1297221345Sdim = ConvertTypeForMem(allocType)->getPointerTo(AS); 1298221345Sdim llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); 1299218893Sdim 1300226633Sdim EmitNewInitializer(*this, E, allocType, result, numElements, 1301226633Sdim allocSizeWithoutCookie); 1302212904Sdim if (E->isArray()) { 1303212904Sdim // NewPtr is a pointer to the base element type. If we're 1304212904Sdim // allocating an array of arrays, we'll need to cast back to the 1305212904Sdim // array pointer type. 1306226633Sdim llvm::Type *resultType = ConvertTypeForMem(E->getType()); 1307221345Sdim if (result->getType() != resultType) 1308221345Sdim result = Builder.CreateBitCast(result, resultType); 1309199990Srdivacky } 1310218893Sdim 1311218893Sdim // Deactivate the 'operator delete' cleanup if we finished 1312218893Sdim // initialization. 1313234353Sdim if (operatorDeleteCleanup.isValid()) { 1314234353Sdim DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); 1315234353Sdim cleanupDominator->eraseFromParent(); 1316234353Sdim } 1317234353Sdim 1318221345Sdim if (nullCheck) { 1319221345Sdim conditional.end(*this); 1320199990Srdivacky 1321221345Sdim llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); 1322221345Sdim EmitBlock(contBB); 1323199990Srdivacky 1324221345Sdim llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); 1325221345Sdim PHI->addIncoming(result, notNullBB); 1326221345Sdim PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), 1327221345Sdim nullCheckBB); 1328221345Sdim 1329221345Sdim result = PHI; 1330199990Srdivacky } 1331212904Sdim 1332221345Sdim return result; 1333199990Srdivacky} 1334199990Srdivacky 1335199990Srdivackyvoid CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, 1336199990Srdivacky llvm::Value *Ptr, 1337199990Srdivacky QualType DeleteTy) { 1338212904Sdim assert(DeleteFD->getOverloadedOperator() == OO_Delete); 1339212904Sdim 1340199990Srdivacky const FunctionProtoType *DeleteFTy = 1341199990Srdivacky DeleteFD->getType()->getAs<FunctionProtoType>(); 1342199990Srdivacky 1343199990Srdivacky CallArgList DeleteArgs; 1344199990Srdivacky 1345200583Srdivacky // Check if we need to pass the size to the delete operator. 1346200583Srdivacky llvm::Value *Size = 0; 1347200583Srdivacky QualType SizeTy; 1348200583Srdivacky if (DeleteFTy->getNumArgs() == 2) { 1349200583Srdivacky SizeTy = DeleteFTy->getArgType(1); 1350203955Srdivacky CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); 1351203955Srdivacky Size = llvm::ConstantInt::get(ConvertType(SizeTy), 1352203955Srdivacky DeleteTypeSize.getQuantity()); 1353200583Srdivacky } 1354200583Srdivacky 1355199990Srdivacky QualType ArgTy = DeleteFTy->getArgType(0); 1356199990Srdivacky llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); 1357221345Sdim DeleteArgs.add(RValue::get(DeletePtr), ArgTy); 1358199990Srdivacky 1359200583Srdivacky if (Size) 1360221345Sdim DeleteArgs.add(RValue::get(Size), SizeTy); 1361199990Srdivacky 1362199990Srdivacky // Emit the call to delete. 1363239462Sdim EmitCall(CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, DeleteFTy), 1364201361Srdivacky CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(), 1365199990Srdivacky DeleteArgs, DeleteFD); 1366199990Srdivacky} 1367199990Srdivacky 1368212904Sdimnamespace { 1369212904Sdim /// Calls the given 'operator delete' on a single object. 1370212904Sdim struct CallObjectDelete : EHScopeStack::Cleanup { 1371212904Sdim llvm::Value *Ptr; 1372212904Sdim const FunctionDecl *OperatorDelete; 1373212904Sdim QualType ElementType; 1374212904Sdim 1375212904Sdim CallObjectDelete(llvm::Value *Ptr, 1376212904Sdim const FunctionDecl *OperatorDelete, 1377212904Sdim QualType ElementType) 1378212904Sdim : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} 1379212904Sdim 1380224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1381212904Sdim CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); 1382212904Sdim } 1383212904Sdim }; 1384212904Sdim} 1385212904Sdim 1386212904Sdim/// Emit the code for deleting a single object. 1387212904Sdimstatic void EmitObjectDelete(CodeGenFunction &CGF, 1388212904Sdim const FunctionDecl *OperatorDelete, 1389212904Sdim llvm::Value *Ptr, 1390224145Sdim QualType ElementType, 1391224145Sdim bool UseGlobalDelete) { 1392212904Sdim // Find the destructor for the type, if applicable. If the 1393212904Sdim // destructor is virtual, we'll just emit the vcall and return. 1394212904Sdim const CXXDestructorDecl *Dtor = 0; 1395212904Sdim if (const RecordType *RT = ElementType->getAs<RecordType>()) { 1396212904Sdim CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1397226633Sdim if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { 1398212904Sdim Dtor = RD->getDestructor(); 1399212904Sdim 1400212904Sdim if (Dtor->isVirtual()) { 1401224145Sdim if (UseGlobalDelete) { 1402224145Sdim // If we're supposed to call the global delete, make sure we do so 1403224145Sdim // even if the destructor throws. 1404243830Sdim 1405243830Sdim // Derive the complete-object pointer, which is what we need 1406243830Sdim // to pass to the deallocation function. 1407243830Sdim llvm::Value *completePtr = 1408243830Sdim CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType); 1409243830Sdim 1410224145Sdim CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, 1411243830Sdim completePtr, OperatorDelete, 1412224145Sdim ElementType); 1413224145Sdim } 1414249423Sdim 1415243830Sdim // FIXME: Provide a source location here. 1416249423Sdim CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 1417249423Sdim CGF.CGM.getCXXABI().EmitVirtualDestructorCall(CGF, Dtor, DtorType, 1418249423Sdim SourceLocation(), 1419249423Sdim ReturnValueSlot(), Ptr); 1420212904Sdim 1421224145Sdim if (UseGlobalDelete) { 1422224145Sdim CGF.PopCleanupBlock(); 1423224145Sdim } 1424224145Sdim 1425212904Sdim return; 1426212904Sdim } 1427212904Sdim } 1428212904Sdim } 1429212904Sdim 1430212904Sdim // Make sure that we call delete even if the dtor throws. 1431218893Sdim // This doesn't have to a conditional cleanup because we're going 1432218893Sdim // to pop it off in a second. 1433212904Sdim CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, 1434212904Sdim Ptr, OperatorDelete, ElementType); 1435212904Sdim 1436212904Sdim if (Dtor) 1437212904Sdim CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, 1438249423Sdim /*ForVirtualBase=*/false, 1439249423Sdim /*Delegating=*/false, 1440249423Sdim Ptr); 1441234353Sdim else if (CGF.getLangOpts().ObjCAutoRefCount && 1442224145Sdim ElementType->isObjCLifetimeType()) { 1443224145Sdim switch (ElementType.getObjCLifetime()) { 1444224145Sdim case Qualifiers::OCL_None: 1445224145Sdim case Qualifiers::OCL_ExplicitNone: 1446224145Sdim case Qualifiers::OCL_Autoreleasing: 1447224145Sdim break; 1448212904Sdim 1449224145Sdim case Qualifiers::OCL_Strong: { 1450224145Sdim // Load the pointer value. 1451224145Sdim llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr, 1452224145Sdim ElementType.isVolatileQualified()); 1453224145Sdim 1454249423Sdim CGF.EmitARCRelease(PtrValue, ARCPreciseLifetime); 1455224145Sdim break; 1456224145Sdim } 1457224145Sdim 1458224145Sdim case Qualifiers::OCL_Weak: 1459224145Sdim CGF.EmitARCDestroyWeak(Ptr); 1460224145Sdim break; 1461224145Sdim } 1462224145Sdim } 1463224145Sdim 1464212904Sdim CGF.PopCleanupBlock(); 1465212904Sdim} 1466212904Sdim 1467212904Sdimnamespace { 1468212904Sdim /// Calls the given 'operator delete' on an array of objects. 1469212904Sdim struct CallArrayDelete : EHScopeStack::Cleanup { 1470212904Sdim llvm::Value *Ptr; 1471212904Sdim const FunctionDecl *OperatorDelete; 1472212904Sdim llvm::Value *NumElements; 1473212904Sdim QualType ElementType; 1474212904Sdim CharUnits CookieSize; 1475212904Sdim 1476212904Sdim CallArrayDelete(llvm::Value *Ptr, 1477212904Sdim const FunctionDecl *OperatorDelete, 1478212904Sdim llvm::Value *NumElements, 1479212904Sdim QualType ElementType, 1480212904Sdim CharUnits CookieSize) 1481212904Sdim : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), 1482212904Sdim ElementType(ElementType), CookieSize(CookieSize) {} 1483212904Sdim 1484224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1485212904Sdim const FunctionProtoType *DeleteFTy = 1486212904Sdim OperatorDelete->getType()->getAs<FunctionProtoType>(); 1487212904Sdim assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); 1488212904Sdim 1489212904Sdim CallArgList Args; 1490212904Sdim 1491212904Sdim // Pass the pointer as the first argument. 1492212904Sdim QualType VoidPtrTy = DeleteFTy->getArgType(0); 1493212904Sdim llvm::Value *DeletePtr 1494212904Sdim = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); 1495221345Sdim Args.add(RValue::get(DeletePtr), VoidPtrTy); 1496212904Sdim 1497212904Sdim // Pass the original requested size as the second argument. 1498212904Sdim if (DeleteFTy->getNumArgs() == 2) { 1499212904Sdim QualType size_t = DeleteFTy->getArgType(1); 1500226633Sdim llvm::IntegerType *SizeTy 1501212904Sdim = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); 1502212904Sdim 1503212904Sdim CharUnits ElementTypeSize = 1504212904Sdim CGF.CGM.getContext().getTypeSizeInChars(ElementType); 1505212904Sdim 1506212904Sdim // The size of an element, multiplied by the number of elements. 1507212904Sdim llvm::Value *Size 1508212904Sdim = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); 1509212904Sdim Size = CGF.Builder.CreateMul(Size, NumElements); 1510212904Sdim 1511212904Sdim // Plus the size of the cookie if applicable. 1512212904Sdim if (!CookieSize.isZero()) { 1513212904Sdim llvm::Value *CookieSizeV 1514212904Sdim = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); 1515212904Sdim Size = CGF.Builder.CreateAdd(Size, CookieSizeV); 1516212904Sdim } 1517212904Sdim 1518221345Sdim Args.add(RValue::get(Size), size_t); 1519212904Sdim } 1520212904Sdim 1521212904Sdim // Emit the call to delete. 1522239462Sdim CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Args, DeleteFTy), 1523212904Sdim CGF.CGM.GetAddrOfFunction(OperatorDelete), 1524212904Sdim ReturnValueSlot(), Args, OperatorDelete); 1525212904Sdim } 1526212904Sdim }; 1527212904Sdim} 1528212904Sdim 1529212904Sdim/// Emit the code for deleting an array of objects. 1530212904Sdimstatic void EmitArrayDelete(CodeGenFunction &CGF, 1531218893Sdim const CXXDeleteExpr *E, 1532224145Sdim llvm::Value *deletedPtr, 1533224145Sdim QualType elementType) { 1534224145Sdim llvm::Value *numElements = 0; 1535224145Sdim llvm::Value *allocatedPtr = 0; 1536224145Sdim CharUnits cookieSize; 1537224145Sdim CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, 1538224145Sdim numElements, allocatedPtr, cookieSize); 1539212904Sdim 1540224145Sdim assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); 1541212904Sdim 1542212904Sdim // Make sure that we call delete even if one of the dtors throws. 1543224145Sdim const FunctionDecl *operatorDelete = E->getOperatorDelete(); 1544212904Sdim CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, 1545224145Sdim allocatedPtr, operatorDelete, 1546224145Sdim numElements, elementType, 1547224145Sdim cookieSize); 1548212904Sdim 1549224145Sdim // Destroy the elements. 1550224145Sdim if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { 1551224145Sdim assert(numElements && "no element count for a type with a destructor!"); 1552224145Sdim 1553224145Sdim llvm::Value *arrayEnd = 1554224145Sdim CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end"); 1555224145Sdim 1556224145Sdim // Note that it is legal to allocate a zero-length array, and we 1557224145Sdim // can never fold the check away because the length should always 1558224145Sdim // come from a cookie. 1559224145Sdim CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType, 1560224145Sdim CGF.getDestroyer(dtorKind), 1561224145Sdim /*checkZeroLength*/ true, 1562224145Sdim CGF.needsEHCleanup(dtorKind)); 1563212904Sdim } 1564212904Sdim 1565224145Sdim // Pop the cleanup block. 1566212904Sdim CGF.PopCleanupBlock(); 1567212904Sdim} 1568212904Sdim 1569199990Srdivackyvoid CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { 1570199990Srdivacky const Expr *Arg = E->getArgument(); 1571199990Srdivacky llvm::Value *Ptr = EmitScalarExpr(Arg); 1572199990Srdivacky 1573199990Srdivacky // Null check the pointer. 1574199990Srdivacky llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); 1575199990Srdivacky llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); 1576199990Srdivacky 1577221345Sdim llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); 1578199990Srdivacky 1579199990Srdivacky Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); 1580199990Srdivacky EmitBlock(DeleteNotNull); 1581199990Srdivacky 1582212904Sdim // We might be deleting a pointer to array. If so, GEP down to the 1583212904Sdim // first non-array element. 1584212904Sdim // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) 1585212904Sdim QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); 1586212904Sdim if (DeleteTy->isConstantArrayType()) { 1587212904Sdim llvm::Value *Zero = Builder.getInt32(0); 1588226633Sdim SmallVector<llvm::Value*,8> GEP; 1589212904Sdim 1590212904Sdim GEP.push_back(Zero); // point at the outermost array 1591212904Sdim 1592212904Sdim // For each layer of array type we're pointing at: 1593212904Sdim while (const ConstantArrayType *Arr 1594212904Sdim = getContext().getAsConstantArrayType(DeleteTy)) { 1595212904Sdim // 1. Unpeel the array type. 1596212904Sdim DeleteTy = Arr->getElementType(); 1597212904Sdim 1598212904Sdim // 2. GEP to the first element of the array. 1599212904Sdim GEP.push_back(Zero); 1600199990Srdivacky } 1601212904Sdim 1602226633Sdim Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first"); 1603199990Srdivacky } 1604199990Srdivacky 1605212904Sdim assert(ConvertTypeForMem(DeleteTy) == 1606212904Sdim cast<llvm::PointerType>(Ptr->getType())->getElementType()); 1607199990Srdivacky 1608212904Sdim if (E->isArrayForm()) { 1609218893Sdim EmitArrayDelete(*this, E, Ptr, DeleteTy); 1610212904Sdim } else { 1611224145Sdim EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy, 1612224145Sdim E->isGlobalDelete()); 1613212904Sdim } 1614212904Sdim 1615199990Srdivacky EmitBlock(DeleteEnd); 1616199990Srdivacky} 1617199990Srdivacky 1618221345Sdimstatic llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { 1619221345Sdim // void __cxa_bad_typeid(); 1620234353Sdim llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 1621221345Sdim 1622221345Sdim return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); 1623221345Sdim} 1624221345Sdim 1625221345Sdimstatic void EmitBadTypeidCall(CodeGenFunction &CGF) { 1626221345Sdim llvm::Value *Fn = getBadTypeidFn(CGF); 1627249423Sdim CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1628221345Sdim CGF.Builder.CreateUnreachable(); 1629221345Sdim} 1630221345Sdim 1631221345Sdimstatic llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, 1632221345Sdim const Expr *E, 1633226633Sdim llvm::Type *StdTypeInfoPtrTy) { 1634221345Sdim // Get the vtable pointer. 1635221345Sdim llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); 1636221345Sdim 1637221345Sdim // C++ [expr.typeid]p2: 1638221345Sdim // If the glvalue expression is obtained by applying the unary * operator to 1639221345Sdim // a pointer and the pointer is a null pointer value, the typeid expression 1640221345Sdim // throws the std::bad_typeid exception. 1641221345Sdim if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { 1642221345Sdim if (UO->getOpcode() == UO_Deref) { 1643221345Sdim llvm::BasicBlock *BadTypeidBlock = 1644221345Sdim CGF.createBasicBlock("typeid.bad_typeid"); 1645221345Sdim llvm::BasicBlock *EndBlock = 1646221345Sdim CGF.createBasicBlock("typeid.end"); 1647221345Sdim 1648221345Sdim llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr); 1649221345Sdim CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); 1650221345Sdim 1651221345Sdim CGF.EmitBlock(BadTypeidBlock); 1652221345Sdim EmitBadTypeidCall(CGF); 1653221345Sdim CGF.EmitBlock(EndBlock); 1654221345Sdim } 1655221345Sdim } 1656221345Sdim 1657221345Sdim llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, 1658221345Sdim StdTypeInfoPtrTy->getPointerTo()); 1659221345Sdim 1660221345Sdim // Load the type info. 1661221345Sdim Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); 1662221345Sdim return CGF.Builder.CreateLoad(Value); 1663221345Sdim} 1664221345Sdim 1665218893Sdimllvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { 1666226633Sdim llvm::Type *StdTypeInfoPtrTy = 1667221345Sdim ConvertType(E->getType())->getPointerTo(); 1668200583Srdivacky 1669201361Srdivacky if (E->isTypeOperand()) { 1670201361Srdivacky llvm::Constant *TypeInfo = 1671201361Srdivacky CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand()); 1672221345Sdim return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); 1673201361Srdivacky } 1674221345Sdim 1675221345Sdim // C++ [expr.typeid]p2: 1676221345Sdim // When typeid is applied to a glvalue expression whose type is a 1677221345Sdim // polymorphic class type, the result refers to a std::type_info object 1678221345Sdim // representing the type of the most derived object (that is, the dynamic 1679221345Sdim // type) to which the glvalue refers. 1680239462Sdim if (E->isPotentiallyEvaluated()) 1681239462Sdim return EmitTypeidFromVTable(*this, E->getExprOperand(), 1682239462Sdim StdTypeInfoPtrTy); 1683221345Sdim 1684221345Sdim QualType OperandTy = E->getExprOperand()->getType(); 1685221345Sdim return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), 1686221345Sdim StdTypeInfoPtrTy); 1687199990Srdivacky} 1688199990Srdivacky 1689221345Sdimstatic llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { 1690221345Sdim // void *__dynamic_cast(const void *sub, 1691221345Sdim // const abi::__class_type_info *src, 1692221345Sdim // const abi::__class_type_info *dst, 1693221345Sdim // std::ptrdiff_t src2dst_offset); 1694201361Srdivacky 1695234353Sdim llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 1696224145Sdim llvm::Type *PtrDiffTy = 1697221345Sdim CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1698199990Srdivacky 1699224145Sdim llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; 1700249423Sdim 1701249423Sdim llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); 1702249423Sdim 1703249423Sdim // Mark the function as nounwind readonly. 1704249423Sdim llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, 1705249423Sdim llvm::Attribute::ReadOnly }; 1706249423Sdim llvm::AttributeSet Attrs = llvm::AttributeSet::get( 1707249423Sdim CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); 1708249423Sdim 1709249423Sdim return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); 1710221345Sdim} 1711221345Sdim 1712221345Sdimstatic llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { 1713221345Sdim // void __cxa_bad_cast(); 1714234353Sdim llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 1715221345Sdim return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); 1716221345Sdim} 1717221345Sdim 1718221345Sdimstatic void EmitBadCastCall(CodeGenFunction &CGF) { 1719221345Sdim llvm::Value *Fn = getBadCastFn(CGF); 1720249423Sdim CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1721221345Sdim CGF.Builder.CreateUnreachable(); 1722221345Sdim} 1723221345Sdim 1724249423Sdim/// \brief Compute the src2dst_offset hint as described in the 1725249423Sdim/// Itanium C++ ABI [2.9.7] 1726249423Sdimstatic CharUnits computeOffsetHint(ASTContext &Context, 1727249423Sdim const CXXRecordDecl *Src, 1728249423Sdim const CXXRecordDecl *Dst) { 1729249423Sdim CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 1730249423Sdim /*DetectVirtual=*/false); 1731249423Sdim 1732249423Sdim // If Dst is not derived from Src we can skip the whole computation below and 1733249423Sdim // return that Src is not a public base of Dst. Record all inheritance paths. 1734249423Sdim if (!Dst->isDerivedFrom(Src, Paths)) 1735249423Sdim return CharUnits::fromQuantity(-2ULL); 1736249423Sdim 1737249423Sdim unsigned NumPublicPaths = 0; 1738249423Sdim CharUnits Offset; 1739249423Sdim 1740249423Sdim // Now walk all possible inheritance paths. 1741249423Sdim for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); 1742249423Sdim I != E; ++I) { 1743249423Sdim if (I->Access != AS_public) // Ignore non-public inheritance. 1744249423Sdim continue; 1745249423Sdim 1746249423Sdim ++NumPublicPaths; 1747249423Sdim 1748249423Sdim for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) { 1749249423Sdim // If the path contains a virtual base class we can't give any hint. 1750249423Sdim // -1: no hint. 1751249423Sdim if (J->Base->isVirtual()) 1752249423Sdim return CharUnits::fromQuantity(-1ULL); 1753249423Sdim 1754249423Sdim if (NumPublicPaths > 1) // Won't use offsets, skip computation. 1755249423Sdim continue; 1756249423Sdim 1757249423Sdim // Accumulate the base class offsets. 1758249423Sdim const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class); 1759249423Sdim Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl()); 1760249423Sdim } 1761249423Sdim } 1762249423Sdim 1763249423Sdim // -2: Src is not a public base of Dst. 1764249423Sdim if (NumPublicPaths == 0) 1765249423Sdim return CharUnits::fromQuantity(-2ULL); 1766249423Sdim 1767249423Sdim // -3: Src is a multiple public base type but never a virtual base type. 1768249423Sdim if (NumPublicPaths > 1) 1769249423Sdim return CharUnits::fromQuantity(-3ULL); 1770249423Sdim 1771249423Sdim // Otherwise, the Src type is a unique public nonvirtual base type of Dst. 1772249423Sdim // Return the offset of Src from the origin of Dst. 1773249423Sdim return Offset; 1774249423Sdim} 1775249423Sdim 1776221345Sdimstatic llvm::Value * 1777221345SdimEmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, 1778221345Sdim QualType SrcTy, QualType DestTy, 1779221345Sdim llvm::BasicBlock *CastEnd) { 1780226633Sdim llvm::Type *PtrDiffLTy = 1781221345Sdim CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1782226633Sdim llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1783221345Sdim 1784221345Sdim if (const PointerType *PTy = DestTy->getAs<PointerType>()) { 1785221345Sdim if (PTy->getPointeeType()->isVoidType()) { 1786221345Sdim // C++ [expr.dynamic.cast]p7: 1787221345Sdim // If T is "pointer to cv void," then the result is a pointer to the 1788221345Sdim // most derived object pointed to by v. 1789221345Sdim 1790221345Sdim // Get the vtable pointer. 1791221345Sdim llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); 1792221345Sdim 1793221345Sdim // Get the offset-to-top from the vtable. 1794221345Sdim llvm::Value *OffsetToTop = 1795221345Sdim CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); 1796221345Sdim OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); 1797221345Sdim 1798221345Sdim // Finally, add the offset to the pointer. 1799221345Sdim Value = CGF.EmitCastToVoidPtr(Value); 1800221345Sdim Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); 1801221345Sdim 1802221345Sdim return CGF.Builder.CreateBitCast(Value, DestLTy); 1803221345Sdim } 1804221345Sdim } 1805221345Sdim 1806221345Sdim QualType SrcRecordTy; 1807221345Sdim QualType DestRecordTy; 1808221345Sdim 1809221345Sdim if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { 1810221345Sdim SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); 1811221345Sdim DestRecordTy = DestPTy->getPointeeType(); 1812199990Srdivacky } else { 1813221345Sdim SrcRecordTy = SrcTy; 1814221345Sdim DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); 1815199990Srdivacky } 1816199990Srdivacky 1817221345Sdim assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); 1818221345Sdim assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); 1819199990Srdivacky 1820221345Sdim llvm::Value *SrcRTTI = 1821221345Sdim CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 1822221345Sdim llvm::Value *DestRTTI = 1823221345Sdim CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 1824201361Srdivacky 1825249423Sdim // Compute the offset hint. 1826249423Sdim const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 1827249423Sdim const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); 1828249423Sdim llvm::Value *OffsetHint = 1829249423Sdim llvm::ConstantInt::get(PtrDiffLTy, 1830249423Sdim computeOffsetHint(CGF.getContext(), SrcDecl, 1831249423Sdim DestDecl).getQuantity()); 1832221345Sdim 1833221345Sdim // Emit the call to __dynamic_cast. 1834221345Sdim Value = CGF.EmitCastToVoidPtr(Value); 1835249423Sdim 1836249423Sdim llvm::Value *args[] = { Value, SrcRTTI, DestRTTI, OffsetHint }; 1837249423Sdim Value = CGF.EmitNounwindRuntimeCall(getDynamicCastFn(CGF), args); 1838221345Sdim Value = CGF.Builder.CreateBitCast(Value, DestLTy); 1839221345Sdim 1840221345Sdim /// C++ [expr.dynamic.cast]p9: 1841221345Sdim /// A failed cast to reference type throws std::bad_cast 1842221345Sdim if (DestTy->isReferenceType()) { 1843221345Sdim llvm::BasicBlock *BadCastBlock = 1844221345Sdim CGF.createBasicBlock("dynamic_cast.bad_cast"); 1845221345Sdim 1846221345Sdim llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); 1847221345Sdim CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); 1848221345Sdim 1849221345Sdim CGF.EmitBlock(BadCastBlock); 1850221345Sdim EmitBadCastCall(CGF); 1851199990Srdivacky } 1852199990Srdivacky 1853221345Sdim return Value; 1854221345Sdim} 1855199990Srdivacky 1856221345Sdimstatic llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, 1857221345Sdim QualType DestTy) { 1858226633Sdim llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1859221345Sdim if (DestTy->isPointerType()) 1860221345Sdim return llvm::Constant::getNullValue(DestLTy); 1861199990Srdivacky 1862221345Sdim /// C++ [expr.dynamic.cast]p9: 1863221345Sdim /// A failed cast to reference type throws std::bad_cast 1864221345Sdim EmitBadCastCall(CGF); 1865199990Srdivacky 1866221345Sdim CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); 1867221345Sdim return llvm::UndefValue::get(DestLTy); 1868221345Sdim} 1869199990Srdivacky 1870221345Sdimllvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, 1871221345Sdim const CXXDynamicCastExpr *DCE) { 1872221345Sdim QualType DestTy = DCE->getTypeAsWritten(); 1873221345Sdim 1874221345Sdim if (DCE->isAlwaysNull()) 1875221345Sdim return EmitDynamicCastToNull(*this, DestTy); 1876221345Sdim 1877221345Sdim QualType SrcTy = DCE->getSubExpr()->getType(); 1878221345Sdim 1879221345Sdim // C++ [expr.dynamic.cast]p4: 1880221345Sdim // If the value of v is a null pointer value in the pointer case, the result 1881221345Sdim // is the null pointer value of type T. 1882221345Sdim bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); 1883199990Srdivacky 1884221345Sdim llvm::BasicBlock *CastNull = 0; 1885221345Sdim llvm::BasicBlock *CastNotNull = 0; 1886221345Sdim llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); 1887221345Sdim 1888221345Sdim if (ShouldNullCheckSrcValue) { 1889221345Sdim CastNull = createBasicBlock("dynamic_cast.null"); 1890221345Sdim CastNotNull = createBasicBlock("dynamic_cast.notnull"); 1891221345Sdim 1892221345Sdim llvm::Value *IsNull = Builder.CreateIsNull(Value); 1893221345Sdim Builder.CreateCondBr(IsNull, CastNull, CastNotNull); 1894221345Sdim EmitBlock(CastNotNull); 1895199990Srdivacky } 1896221345Sdim 1897221345Sdim Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); 1898221345Sdim 1899221345Sdim if (ShouldNullCheckSrcValue) { 1900221345Sdim EmitBranch(CastEnd); 1901221345Sdim 1902221345Sdim EmitBlock(CastNull); 1903221345Sdim EmitBranch(CastEnd); 1904199990Srdivacky } 1905199990Srdivacky 1906221345Sdim EmitBlock(CastEnd); 1907221345Sdim 1908221345Sdim if (ShouldNullCheckSrcValue) { 1909221345Sdim llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); 1910221345Sdim PHI->addIncoming(Value, CastNotNull); 1911221345Sdim PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); 1912221345Sdim 1913221345Sdim Value = PHI; 1914221345Sdim } 1915221345Sdim 1916221345Sdim return Value; 1917199990Srdivacky} 1918234353Sdim 1919234353Sdimvoid CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) { 1920234353Sdim RunCleanupsScope Scope(*this); 1921234982Sdim LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(), 1922234982Sdim Slot.getAlignment()); 1923234353Sdim 1924234353Sdim CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin(); 1925234353Sdim for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(), 1926234353Sdim e = E->capture_init_end(); 1927234353Sdim i != e; ++i, ++CurField) { 1928234353Sdim // Emit initialization 1929234982Sdim 1930234982Sdim LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); 1931234353Sdim ArrayRef<VarDecl *> ArrayIndexes; 1932234353Sdim if (CurField->getType()->isArrayType()) 1933234353Sdim ArrayIndexes = E->getCaptureInitIndexVars(i); 1934234353Sdim EmitInitializerForField(*CurField, LV, *i, ArrayIndexes); 1935234353Sdim } 1936234353Sdim} 1937