159486a2dSAnders Carlsson //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// 259486a2dSAnders Carlsson // 359486a2dSAnders Carlsson // The LLVM Compiler Infrastructure 459486a2dSAnders Carlsson // 559486a2dSAnders Carlsson // This file is distributed under the University of Illinois Open Source 659486a2dSAnders Carlsson // License. See LICENSE.TXT for details. 759486a2dSAnders Carlsson // 859486a2dSAnders Carlsson //===----------------------------------------------------------------------===// 959486a2dSAnders Carlsson // 1059486a2dSAnders Carlsson // This contains code dealing with code generation of C++ expressions 1159486a2dSAnders Carlsson // 1259486a2dSAnders Carlsson //===----------------------------------------------------------------------===// 1359486a2dSAnders Carlsson 1459486a2dSAnders Carlsson #include "CodeGenFunction.h" 15fe883422SPeter Collingbourne #include "CGCUDARuntime.h" 165d865c32SJohn McCall #include "CGCXXABI.h" 1791bbb554SDevang Patel #include "CGDebugInfo.h" 183a02247dSChandler Carruth #include "CGObjCRuntime.h" 19a8e7df36SMark Lacey #include "clang/CodeGen/CGFunctionInfo.h" 2010a4972aSSaleem Abdulrasool #include "clang/Frontend/CodeGenOptions.h" 21c80ceea9SChandler Carruth #include "llvm/IR/CallSite.h" 22ffd5551bSChandler Carruth #include "llvm/IR/Intrinsics.h" 23bbe277c4SAnders Carlsson 2459486a2dSAnders Carlsson using namespace clang; 2559486a2dSAnders Carlsson using namespace CodeGen; 2659486a2dSAnders Carlsson 27efa956ceSAlexey Samsonov static RequiredArgs 28efa956ceSAlexey Samsonov commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, 29efa956ceSAlexey Samsonov llvm::Value *This, llvm::Value *ImplicitParam, 30efa956ceSAlexey Samsonov QualType ImplicitParamTy, const CallExpr *CE, 31efa956ceSAlexey Samsonov CallArgList &Args) { 32a5bf76bdSAlexey Samsonov assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) || 33a5bf76bdSAlexey Samsonov isa<CXXOperatorCallExpr>(CE)); 3427da15baSAnders Carlsson assert(MD->isInstance() && 35a5bf76bdSAlexey Samsonov "Trying to emit a member or operator call expr on a static method!"); 36*034e7270SReid Kleckner ASTContext &C = CGF.getContext(); 3727da15baSAnders Carlsson 3869d0d262SRichard Smith // C++11 [class.mfct.non-static]p2: 3969d0d262SRichard Smith // If a non-static member function of a class X is called for an object that 4069d0d262SRichard Smith // is not of type X, or of a type derived from X, the behavior is undefined. 41a5bf76bdSAlexey Samsonov SourceLocation CallLoc; 42a5bf76bdSAlexey Samsonov if (CE) 43a5bf76bdSAlexey Samsonov CallLoc = CE->getExprLoc(); 44*034e7270SReid Kleckner CGF.EmitTypeCheck(isa<CXXConstructorDecl>(MD) 45*034e7270SReid Kleckner ? CodeGenFunction::TCK_ConstructorCall 460c0b6d9aSDavid Majnemer : CodeGenFunction::TCK_MemberCall, 47*034e7270SReid Kleckner CallLoc, This, C.getRecordType(MD->getParent())); 4827da15baSAnders Carlsson 4927da15baSAnders Carlsson // Push the this ptr. 50*034e7270SReid Kleckner const CXXRecordDecl *RD = 51*034e7270SReid Kleckner CGF.CGM.getCXXABI().getThisArgumentTypeForMethod(MD); 52*034e7270SReid Kleckner Args.add(RValue::get(This), 53*034e7270SReid Kleckner RD ? C.getPointerType(C.getTypeDeclType(RD)) : C.VoidPtrTy); 5427da15baSAnders Carlsson 55ee6bc533STimur Iskhodzhanov // If there is an implicit parameter (e.g. VTT), emit it. 56ee6bc533STimur Iskhodzhanov if (ImplicitParam) { 57ee6bc533STimur Iskhodzhanov Args.add(RValue::get(ImplicitParam), ImplicitParamTy); 58e36a6b3eSAnders Carlsson } 59e36a6b3eSAnders Carlsson 60a729c62bSJohn McCall const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 61419996ccSGeorge Burgess IV RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size(), MD); 62a729c62bSJohn McCall 63a729c62bSJohn McCall // And the rest of the call args. 648e1162c7SAlexey Samsonov if (CE) { 65a5bf76bdSAlexey Samsonov // Special case: skip first argument of CXXOperatorCall (it is "this"). 668e1162c7SAlexey Samsonov unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0; 67f05779e2SDavid Blaikie CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip), 688e1162c7SAlexey Samsonov CE->getDirectCallee()); 69a5bf76bdSAlexey Samsonov } else { 708e1162c7SAlexey Samsonov assert( 718e1162c7SAlexey Samsonov FPT->getNumParams() == 0 && 728e1162c7SAlexey Samsonov "No CallExpr specified for function with non-zero number of arguments"); 73a5bf76bdSAlexey Samsonov } 740c0b6d9aSDavid Majnemer return required; 750c0b6d9aSDavid Majnemer } 7627da15baSAnders Carlsson 770c0b6d9aSDavid Majnemer RValue CodeGenFunction::EmitCXXMemberOrOperatorCall( 780c0b6d9aSDavid Majnemer const CXXMethodDecl *MD, llvm::Value *Callee, ReturnValueSlot ReturnValue, 790c0b6d9aSDavid Majnemer llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, 800c0b6d9aSDavid Majnemer const CallExpr *CE) { 810c0b6d9aSDavid Majnemer const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 820c0b6d9aSDavid Majnemer CallArgList Args; 830c0b6d9aSDavid Majnemer RequiredArgs required = commonEmitCXXMemberOrOperatorCall( 84efa956ceSAlexey Samsonov *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args); 858dda7b27SJohn McCall return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), 86c50c27ccSRafael Espindola Callee, ReturnValue, Args, MD); 8727da15baSAnders Carlsson } 8827da15baSAnders Carlsson 89ae81bbb4SAlexey Samsonov RValue CodeGenFunction::EmitCXXDestructorCall( 90ae81bbb4SAlexey Samsonov const CXXDestructorDecl *DD, llvm::Value *Callee, llvm::Value *This, 91ae81bbb4SAlexey Samsonov llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE, 92ae81bbb4SAlexey Samsonov StructorType Type) { 930c0b6d9aSDavid Majnemer CallArgList Args; 94ae81bbb4SAlexey Samsonov commonEmitCXXMemberOrOperatorCall(*this, DD, This, ImplicitParam, 95efa956ceSAlexey Samsonov ImplicitParamTy, CE, Args); 96ae81bbb4SAlexey Samsonov return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(DD, Type), 97ae81bbb4SAlexey Samsonov Callee, ReturnValueSlot(), Args, DD); 980c0b6d9aSDavid Majnemer } 990c0b6d9aSDavid Majnemer 1003b33c4ecSRafael Espindola static CXXRecordDecl *getCXXRecord(const Expr *E) { 1013b33c4ecSRafael Espindola QualType T = E->getType(); 1023b33c4ecSRafael Espindola if (const PointerType *PTy = T->getAs<PointerType>()) 1033b33c4ecSRafael Espindola T = PTy->getPointeeType(); 1043b33c4ecSRafael Espindola const RecordType *Ty = T->castAs<RecordType>(); 1053b33c4ecSRafael Espindola return cast<CXXRecordDecl>(Ty->getDecl()); 1063b33c4ecSRafael Espindola } 1073b33c4ecSRafael Espindola 10864225794SFrancois Pichet // Note: This function also emit constructor calls to support a MSVC 10964225794SFrancois Pichet // extensions allowing explicit constructor function call. 11027da15baSAnders Carlsson RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, 11127da15baSAnders Carlsson ReturnValueSlot ReturnValue) { 1122d2e8707SJohn McCall const Expr *callee = CE->getCallee()->IgnoreParens(); 1132d2e8707SJohn McCall 1142d2e8707SJohn McCall if (isa<BinaryOperator>(callee)) 11527da15baSAnders Carlsson return EmitCXXMemberPointerCallExpr(CE, ReturnValue); 11627da15baSAnders Carlsson 1172d2e8707SJohn McCall const MemberExpr *ME = cast<MemberExpr>(callee); 11827da15baSAnders Carlsson const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 11927da15baSAnders Carlsson 12027da15baSAnders Carlsson if (MD->isStatic()) { 12127da15baSAnders Carlsson // The method is static, emit it as we would a regular call. 12227da15baSAnders Carlsson llvm::Value *Callee = CGM.GetAddrOfFunction(MD); 12370b9c01bSAlexey Samsonov return EmitCall(getContext().getPointerType(MD->getType()), Callee, CE, 12470b9c01bSAlexey Samsonov ReturnValue); 12527da15baSAnders Carlsson } 12627da15baSAnders Carlsson 127aad4af6dSNico Weber bool HasQualifier = ME->hasQualifier(); 128aad4af6dSNico Weber NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr; 129aad4af6dSNico Weber bool IsArrow = ME->isArrow(); 130ecbe2e97SRafael Espindola const Expr *Base = ME->getBase(); 131aad4af6dSNico Weber 132aad4af6dSNico Weber return EmitCXXMemberOrOperatorMemberCallExpr( 133aad4af6dSNico Weber CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base); 134aad4af6dSNico Weber } 135aad4af6dSNico Weber 136aad4af6dSNico Weber RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr( 137aad4af6dSNico Weber const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, 138aad4af6dSNico Weber bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, 139aad4af6dSNico Weber const Expr *Base) { 140aad4af6dSNico Weber assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE)); 141aad4af6dSNico Weber 142aad4af6dSNico Weber // Compute the object pointer. 143aad4af6dSNico Weber bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier; 144ecbe2e97SRafael Espindola 1458a13c418SCraig Topper const CXXMethodDecl *DevirtualizedMethod = nullptr; 1467463ed7cSBenjamin Kramer if (CanUseVirtualCall && CanDevirtualizeMemberFunctionCall(Base, MD)) { 1473b33c4ecSRafael Espindola const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); 1483b33c4ecSRafael Espindola DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); 1493b33c4ecSRafael Espindola assert(DevirtualizedMethod); 1503b33c4ecSRafael Espindola const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); 1513b33c4ecSRafael Espindola const Expr *Inner = Base->ignoreParenBaseCasts(); 1525bd68794SAlexey Bataev if (DevirtualizedMethod->getReturnType().getCanonicalType() != 1535bd68794SAlexey Bataev MD->getReturnType().getCanonicalType()) 1545bd68794SAlexey Bataev // If the return types are not the same, this might be a case where more 1555bd68794SAlexey Bataev // code needs to run to compensate for it. For example, the derived 1565bd68794SAlexey Bataev // method might return a type that inherits form from the return 1575bd68794SAlexey Bataev // type of MD and has a prefix. 1585bd68794SAlexey Bataev // For now we just avoid devirtualizing these covariant cases. 1595bd68794SAlexey Bataev DevirtualizedMethod = nullptr; 1605bd68794SAlexey Bataev else if (getCXXRecord(Inner) == DevirtualizedClass) 1613b33c4ecSRafael Espindola // If the class of the Inner expression is where the dynamic method 1623b33c4ecSRafael Espindola // is defined, build the this pointer from it. 1633b33c4ecSRafael Espindola Base = Inner; 1643b33c4ecSRafael Espindola else if (getCXXRecord(Base) != DevirtualizedClass) { 1653b33c4ecSRafael Espindola // If the method is defined in a class that is not the best dynamic 1663b33c4ecSRafael Espindola // one or the one of the full expression, we would have to build 1673b33c4ecSRafael Espindola // a derived-to-base cast to compute the correct this pointer, but 1683b33c4ecSRafael Espindola // we don't have support for that yet, so do a virtual call. 1698a13c418SCraig Topper DevirtualizedMethod = nullptr; 1703b33c4ecSRafael Espindola } 1713b33c4ecSRafael Espindola } 172ecbe2e97SRafael Espindola 1737f416cc4SJohn McCall Address This = Address::invalid(); 174aad4af6dSNico Weber if (IsArrow) 1757f416cc4SJohn McCall This = EmitPointerWithAlignment(Base); 176f93ac894SFariborz Jahanian else 1773b33c4ecSRafael Espindola This = EmitLValue(Base).getAddress(); 178ecbe2e97SRafael Espindola 17927da15baSAnders Carlsson 180419bd094SRichard Smith if (MD->isTrivial() || (MD->isDefaulted() && MD->getParent()->isUnion())) { 1818a13c418SCraig Topper if (isa<CXXDestructorDecl>(MD)) return RValue::get(nullptr); 18264225794SFrancois Pichet if (isa<CXXConstructorDecl>(MD) && 18364225794SFrancois Pichet cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) 1848a13c418SCraig Topper return RValue::get(nullptr); 1850d635f53SJohn McCall 186aad4af6dSNico Weber if (!MD->getParent()->mayInsertExtraPadding()) { 18722653bacSSebastian Redl if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { 18822653bacSSebastian Redl // We don't like to generate the trivial copy/move assignment operator 18922653bacSSebastian Redl // when it isn't necessary; just produce the proper effect here. 190aad4af6dSNico Weber // Special case: skip first argument of CXXOperatorCall (it is "this"). 191aad4af6dSNico Weber unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0; 1927f416cc4SJohn McCall Address RHS = EmitLValue(*(CE->arg_begin() + ArgsToSkip)).getAddress(); 1931ca66919SBenjamin Kramer EmitAggregateAssign(This, RHS, CE->getType()); 1947f416cc4SJohn McCall return RValue::get(This.getPointer()); 19527da15baSAnders Carlsson } 19627da15baSAnders Carlsson 19764225794SFrancois Pichet if (isa<CXXConstructorDecl>(MD) && 19822653bacSSebastian Redl cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { 19922653bacSSebastian Redl // Trivial move and copy ctor are the same. 200525bf650SAlexey Samsonov assert(CE->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); 2017f416cc4SJohn McCall Address RHS = EmitLValue(*CE->arg_begin()).getAddress(); 202f48ee448SBenjamin Kramer EmitAggregateCopy(This, RHS, (*CE->arg_begin())->getType()); 2037f416cc4SJohn McCall return RValue::get(This.getPointer()); 20464225794SFrancois Pichet } 20564225794SFrancois Pichet llvm_unreachable("unknown trivial member function"); 20664225794SFrancois Pichet } 207aad4af6dSNico Weber } 20864225794SFrancois Pichet 2090d635f53SJohn McCall // Compute the function type we're calling. 2103abfe958SNico Weber const CXXMethodDecl *CalleeDecl = 2113abfe958SNico Weber DevirtualizedMethod ? DevirtualizedMethod : MD; 2128a13c418SCraig Topper const CGFunctionInfo *FInfo = nullptr; 2133abfe958SNico Weber if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) 2148d2a19b4SRafael Espindola FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 2158d2a19b4SRafael Espindola Dtor, StructorType::Complete); 2163abfe958SNico Weber else if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl)) 2178d2a19b4SRafael Espindola FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 2188d2a19b4SRafael Espindola Ctor, StructorType::Complete); 21964225794SFrancois Pichet else 220ade60977SEli Friedman FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); 2210d635f53SJohn McCall 222e7de47efSReid Kleckner llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); 2230d635f53SJohn McCall 22427da15baSAnders Carlsson // C++ [class.virtual]p12: 22527da15baSAnders Carlsson // Explicit qualification with the scope operator (5.1) suppresses the 22627da15baSAnders Carlsson // virtual call mechanism. 22727da15baSAnders Carlsson // 22827da15baSAnders Carlsson // We also don't emit a virtual call if the base expression has a record type 22927da15baSAnders Carlsson // because then we know what the type is. 2303b33c4ecSRafael Espindola bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; 23119cee187SStephen Lin llvm::Value *Callee; 2329dc6eef7SStephen Lin 2330d635f53SJohn McCall if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { 23419cee187SStephen Lin assert(CE->arg_begin() == CE->arg_end() && 2359dc6eef7SStephen Lin "Destructor shouldn't have explicit parameters"); 2369dc6eef7SStephen Lin assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); 2379dc6eef7SStephen Lin if (UseVirtualCall) { 238aad4af6dSNico Weber CGM.getCXXABI().EmitVirtualDestructorCall( 239aad4af6dSNico Weber *this, Dtor, Dtor_Complete, This, cast<CXXMemberCallExpr>(CE)); 24027da15baSAnders Carlsson } else { 241aad4af6dSNico Weber if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) 242aad4af6dSNico Weber Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); 2433b33c4ecSRafael Espindola else if (!DevirtualizedMethod) 2441ac0ec86SRafael Espindola Callee = 2451ac0ec86SRafael Espindola CGM.getAddrOfCXXStructor(Dtor, StructorType::Complete, FInfo, Ty); 24649e860b2SRafael Espindola else { 2473b33c4ecSRafael Espindola const CXXDestructorDecl *DDtor = 2483b33c4ecSRafael Espindola cast<CXXDestructorDecl>(DevirtualizedMethod); 24949e860b2SRafael Espindola Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty); 25049e860b2SRafael Espindola } 2517f416cc4SJohn McCall EmitCXXMemberOrOperatorCall(MD, Callee, ReturnValue, This.getPointer(), 252a5bf76bdSAlexey Samsonov /*ImplicitParam=*/nullptr, QualType(), CE); 25327da15baSAnders Carlsson } 2548a13c418SCraig Topper return RValue::get(nullptr); 2559dc6eef7SStephen Lin } 2569dc6eef7SStephen Lin 2579dc6eef7SStephen Lin if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { 25864225794SFrancois Pichet Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); 2590d635f53SJohn McCall } else if (UseVirtualCall) { 2606708c4a1SPeter Collingbourne Callee = CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, Ty, 2616708c4a1SPeter Collingbourne CE->getLocStart()); 26227da15baSAnders Carlsson } else { 2631a7488afSPeter Collingbourne if (SanOpts.has(SanitizerKind::CFINVCall) && 2641a7488afSPeter Collingbourne MD->getParent()->isDynamicClass()) { 2654b1ac72cSPiotr Padlewski llvm::Value *VTable = GetVTablePtr(This, Int8PtrTy, MD->getParent()); 266fb532b9aSPeter Collingbourne EmitVTablePtrCheckForCall(MD->getParent(), VTable, CFITCK_NVCall, 267fb532b9aSPeter Collingbourne CE->getLocStart()); 2681a7488afSPeter Collingbourne } 2691a7488afSPeter Collingbourne 270aad4af6dSNico Weber if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) 271aad4af6dSNico Weber Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); 2723b33c4ecSRafael Espindola else if (!DevirtualizedMethod) 273727a771aSRafael Espindola Callee = CGM.GetAddrOfFunction(MD, Ty); 27449e860b2SRafael Espindola else { 2753b33c4ecSRafael Espindola Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty); 27649e860b2SRafael Espindola } 27727da15baSAnders Carlsson } 27827da15baSAnders Carlsson 279f1749427STimur Iskhodzhanov if (MD->isVirtual()) { 280f1749427STimur Iskhodzhanov This = CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall( 2814b60f30aSReid Kleckner *this, CalleeDecl, This, UseVirtualCall); 282f1749427STimur Iskhodzhanov } 28388fd439aSTimur Iskhodzhanov 2847f416cc4SJohn McCall return EmitCXXMemberOrOperatorCall(MD, Callee, ReturnValue, This.getPointer(), 285a5bf76bdSAlexey Samsonov /*ImplicitParam=*/nullptr, QualType(), CE); 28627da15baSAnders Carlsson } 28727da15baSAnders Carlsson 28827da15baSAnders Carlsson RValue 28927da15baSAnders Carlsson CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, 29027da15baSAnders Carlsson ReturnValueSlot ReturnValue) { 29127da15baSAnders Carlsson const BinaryOperator *BO = 29227da15baSAnders Carlsson cast<BinaryOperator>(E->getCallee()->IgnoreParens()); 29327da15baSAnders Carlsson const Expr *BaseExpr = BO->getLHS(); 29427da15baSAnders Carlsson const Expr *MemFnExpr = BO->getRHS(); 29527da15baSAnders Carlsson 29627da15baSAnders Carlsson const MemberPointerType *MPT = 2970009fcc3SJohn McCall MemFnExpr->getType()->castAs<MemberPointerType>(); 298475999dcSJohn McCall 29927da15baSAnders Carlsson const FunctionProtoType *FPT = 3000009fcc3SJohn McCall MPT->getPointeeType()->castAs<FunctionProtoType>(); 30127da15baSAnders Carlsson const CXXRecordDecl *RD = 30227da15baSAnders Carlsson cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 30327da15baSAnders Carlsson 30427da15baSAnders Carlsson // Get the member function pointer. 305a1dee530SJohn McCall llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); 30627da15baSAnders Carlsson 30727da15baSAnders Carlsson // Emit the 'this' pointer. 3087f416cc4SJohn McCall Address This = Address::invalid(); 309e302792bSJohn McCall if (BO->getOpcode() == BO_PtrMemI) 3107f416cc4SJohn McCall This = EmitPointerWithAlignment(BaseExpr); 31127da15baSAnders Carlsson else 31227da15baSAnders Carlsson This = EmitLValue(BaseExpr).getAddress(); 31327da15baSAnders Carlsson 3147f416cc4SJohn McCall EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(), 315e30752c9SRichard Smith QualType(MPT->getClass(), 0)); 31669d0d262SRichard Smith 317475999dcSJohn McCall // Ask the ABI to load the callee. Note that This is modified. 3187f416cc4SJohn McCall llvm::Value *ThisPtrForCall = nullptr; 319475999dcSJohn McCall llvm::Value *Callee = 3207f416cc4SJohn McCall CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, 3217f416cc4SJohn McCall ThisPtrForCall, MemFnPtr, MPT); 32227da15baSAnders Carlsson 32327da15baSAnders Carlsson CallArgList Args; 32427da15baSAnders Carlsson 32527da15baSAnders Carlsson QualType ThisType = 32627da15baSAnders Carlsson getContext().getPointerType(getContext().getTagDeclType(RD)); 32727da15baSAnders Carlsson 32827da15baSAnders Carlsson // Push the this ptr. 3297f416cc4SJohn McCall Args.add(RValue::get(ThisPtrForCall), ThisType); 33027da15baSAnders Carlsson 331419996ccSGeorge Burgess IV RequiredArgs required = 332419996ccSGeorge Burgess IV RequiredArgs::forPrototypePlus(FPT, 1, /*FD=*/nullptr); 3338dda7b27SJohn McCall 33427da15baSAnders Carlsson // And the rest of the call args 335419996ccSGeorge Burgess IV EmitCallArgs(Args, FPT, E->arguments()); 3365fa40c3bSNick Lewycky return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), 3375fa40c3bSNick Lewycky Callee, ReturnValue, Args); 33827da15baSAnders Carlsson } 33927da15baSAnders Carlsson 34027da15baSAnders Carlsson RValue 34127da15baSAnders Carlsson CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 34227da15baSAnders Carlsson const CXXMethodDecl *MD, 34327da15baSAnders Carlsson ReturnValueSlot ReturnValue) { 34427da15baSAnders Carlsson assert(MD->isInstance() && 34527da15baSAnders Carlsson "Trying to emit a member call expr on a static method!"); 346aad4af6dSNico Weber return EmitCXXMemberOrOperatorMemberCallExpr( 347aad4af6dSNico Weber E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr, 348aad4af6dSNico Weber /*IsArrow=*/false, E->getArg(0)); 34927da15baSAnders Carlsson } 35027da15baSAnders Carlsson 351fe883422SPeter Collingbourne RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, 352fe883422SPeter Collingbourne ReturnValueSlot ReturnValue) { 353fe883422SPeter Collingbourne return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); 354fe883422SPeter Collingbourne } 355fe883422SPeter Collingbourne 356fde961dbSEli Friedman static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, 3577f416cc4SJohn McCall Address DestPtr, 358fde961dbSEli Friedman const CXXRecordDecl *Base) { 359fde961dbSEli Friedman if (Base->isEmpty()) 360fde961dbSEli Friedman return; 361fde961dbSEli Friedman 3627f416cc4SJohn McCall DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); 363fde961dbSEli Friedman 364fde961dbSEli Friedman const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); 3658671c6e0SDavid Majnemer CharUnits NVSize = Layout.getNonVirtualSize(); 3668671c6e0SDavid Majnemer 3678671c6e0SDavid Majnemer // We cannot simply zero-initialize the entire base sub-object if vbptrs are 3688671c6e0SDavid Majnemer // present, they are initialized by the most derived class before calling the 3698671c6e0SDavid Majnemer // constructor. 3708671c6e0SDavid Majnemer SmallVector<std::pair<CharUnits, CharUnits>, 1> Stores; 3718671c6e0SDavid Majnemer Stores.emplace_back(CharUnits::Zero(), NVSize); 3728671c6e0SDavid Majnemer 3738671c6e0SDavid Majnemer // Each store is split by the existence of a vbptr. 3748671c6e0SDavid Majnemer CharUnits VBPtrWidth = CGF.getPointerSize(); 3758671c6e0SDavid Majnemer std::vector<CharUnits> VBPtrOffsets = 3768671c6e0SDavid Majnemer CGF.CGM.getCXXABI().getVBPtrOffsets(Base); 3778671c6e0SDavid Majnemer for (CharUnits VBPtrOffset : VBPtrOffsets) { 3787f980d84SDavid Majnemer // Stop before we hit any virtual base pointers located in virtual bases. 3797f980d84SDavid Majnemer if (VBPtrOffset >= NVSize) 3807f980d84SDavid Majnemer break; 3818671c6e0SDavid Majnemer std::pair<CharUnits, CharUnits> LastStore = Stores.pop_back_val(); 3828671c6e0SDavid Majnemer CharUnits LastStoreOffset = LastStore.first; 3838671c6e0SDavid Majnemer CharUnits LastStoreSize = LastStore.second; 3848671c6e0SDavid Majnemer 3858671c6e0SDavid Majnemer CharUnits SplitBeforeOffset = LastStoreOffset; 3868671c6e0SDavid Majnemer CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset; 3878671c6e0SDavid Majnemer assert(!SplitBeforeSize.isNegative() && "negative store size!"); 3888671c6e0SDavid Majnemer if (!SplitBeforeSize.isZero()) 3898671c6e0SDavid Majnemer Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize); 3908671c6e0SDavid Majnemer 3918671c6e0SDavid Majnemer CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth; 3928671c6e0SDavid Majnemer CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset; 3938671c6e0SDavid Majnemer assert(!SplitAfterSize.isNegative() && "negative store size!"); 3948671c6e0SDavid Majnemer if (!SplitAfterSize.isZero()) 3958671c6e0SDavid Majnemer Stores.emplace_back(SplitAfterOffset, SplitAfterSize); 3968671c6e0SDavid Majnemer } 397fde961dbSEli Friedman 398fde961dbSEli Friedman // If the type contains a pointer to data member we can't memset it to zero. 399fde961dbSEli Friedman // Instead, create a null constant and copy it to the destination. 400fde961dbSEli Friedman // TODO: there are other patterns besides zero that we can usefully memset, 401fde961dbSEli Friedman // like -1, which happens to be the pattern used by member-pointers. 402fde961dbSEli Friedman // TODO: isZeroInitializable can be over-conservative in the case where a 403fde961dbSEli Friedman // virtual base contains a member pointer. 4048671c6e0SDavid Majnemer llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base); 4058671c6e0SDavid Majnemer if (!NullConstantForBase->isNullValue()) { 4068671c6e0SDavid Majnemer llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable( 4078671c6e0SDavid Majnemer CGF.CGM.getModule(), NullConstantForBase->getType(), 4088671c6e0SDavid Majnemer /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, 4098671c6e0SDavid Majnemer NullConstantForBase, Twine()); 4107f416cc4SJohn McCall 4117f416cc4SJohn McCall CharUnits Align = std::max(Layout.getNonVirtualAlignment(), 4127f416cc4SJohn McCall DestPtr.getAlignment()); 413fde961dbSEli Friedman NullVariable->setAlignment(Align.getQuantity()); 4147f416cc4SJohn McCall 4157f416cc4SJohn McCall Address SrcPtr = Address(CGF.EmitCastToVoidPtr(NullVariable), Align); 416fde961dbSEli Friedman 417fde961dbSEli Friedman // Get and call the appropriate llvm.memcpy overload. 4188671c6e0SDavid Majnemer for (std::pair<CharUnits, CharUnits> Store : Stores) { 4198671c6e0SDavid Majnemer CharUnits StoreOffset = Store.first; 4208671c6e0SDavid Majnemer CharUnits StoreSize = Store.second; 4218671c6e0SDavid Majnemer llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); 4228671c6e0SDavid Majnemer CGF.Builder.CreateMemCpy( 4238671c6e0SDavid Majnemer CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), 4248671c6e0SDavid Majnemer CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset), 4258671c6e0SDavid Majnemer StoreSizeVal); 426fde961dbSEli Friedman } 427fde961dbSEli Friedman 428fde961dbSEli Friedman // Otherwise, just memset the whole thing to zero. This is legal 429fde961dbSEli Friedman // because in LLVM, all default initializers (other than the ones we just 430fde961dbSEli Friedman // handled above) are guaranteed to have a bit pattern of all zeros. 4318671c6e0SDavid Majnemer } else { 4328671c6e0SDavid Majnemer for (std::pair<CharUnits, CharUnits> Store : Stores) { 4338671c6e0SDavid Majnemer CharUnits StoreOffset = Store.first; 4348671c6e0SDavid Majnemer CharUnits StoreSize = Store.second; 4358671c6e0SDavid Majnemer llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); 4368671c6e0SDavid Majnemer CGF.Builder.CreateMemSet( 4378671c6e0SDavid Majnemer CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), 4388671c6e0SDavid Majnemer CGF.Builder.getInt8(0), StoreSizeVal); 4398671c6e0SDavid Majnemer } 4408671c6e0SDavid Majnemer } 441fde961dbSEli Friedman } 442fde961dbSEli Friedman 44327da15baSAnders Carlsson void 4447a626f63SJohn McCall CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, 4457a626f63SJohn McCall AggValueSlot Dest) { 4467a626f63SJohn McCall assert(!Dest.isIgnored() && "Must have a destination!"); 44727da15baSAnders Carlsson const CXXConstructorDecl *CD = E->getConstructor(); 448630c76efSDouglas Gregor 449630c76efSDouglas Gregor // If we require zero initialization before (or instead of) calling the 450630c76efSDouglas Gregor // constructor, as can be the case with a non-user-provided default 45103535265SArgyrios Kyrtzidis // constructor, emit the zero initialization now, unless destination is 45203535265SArgyrios Kyrtzidis // already zeroed. 453fde961dbSEli Friedman if (E->requiresZeroInitialization() && !Dest.isZeroed()) { 454fde961dbSEli Friedman switch (E->getConstructionKind()) { 455fde961dbSEli Friedman case CXXConstructExpr::CK_Delegating: 456fde961dbSEli Friedman case CXXConstructExpr::CK_Complete: 4577f416cc4SJohn McCall EmitNullInitialization(Dest.getAddress(), E->getType()); 458fde961dbSEli Friedman break; 459fde961dbSEli Friedman case CXXConstructExpr::CK_VirtualBase: 460fde961dbSEli Friedman case CXXConstructExpr::CK_NonVirtualBase: 4617f416cc4SJohn McCall EmitNullBaseClassInitialization(*this, Dest.getAddress(), 4627f416cc4SJohn McCall CD->getParent()); 463fde961dbSEli Friedman break; 464fde961dbSEli Friedman } 465fde961dbSEli Friedman } 466630c76efSDouglas Gregor 467630c76efSDouglas Gregor // If this is a call to a trivial default constructor, do nothing. 468630c76efSDouglas Gregor if (CD->isTrivial() && CD->isDefaultConstructor()) 46927da15baSAnders Carlsson return; 470630c76efSDouglas Gregor 4718ea46b66SJohn McCall // Elide the constructor if we're constructing from a temporary. 4728ea46b66SJohn McCall // The temporary check is required because Sema sets this on NRVO 4738ea46b66SJohn McCall // returns. 4749c6890a7SRichard Smith if (getLangOpts().ElideConstructors && E->isElidable()) { 4758ea46b66SJohn McCall assert(getContext().hasSameUnqualifiedType(E->getType(), 4768ea46b66SJohn McCall E->getArg(0)->getType())); 4777a626f63SJohn McCall if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { 4787a626f63SJohn McCall EmitAggExpr(E->getArg(0), Dest); 47927da15baSAnders Carlsson return; 48027da15baSAnders Carlsson } 481222cf0efSDouglas Gregor } 482630c76efSDouglas Gregor 483e7545b33SAlexey Bataev if (const ArrayType *arrayType 484e7545b33SAlexey Bataev = getContext().getAsArrayType(E->getType())) { 4857f416cc4SJohn McCall EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E); 486f677a8e9SJohn McCall } else { 487bceca20aSCameron Esfahani CXXCtorType Type = Ctor_Complete; 488271c3681SAlexis Hunt bool ForVirtualBase = false; 48961535005SDouglas Gregor bool Delegating = false; 490271c3681SAlexis Hunt 491271c3681SAlexis Hunt switch (E->getConstructionKind()) { 492271c3681SAlexis Hunt case CXXConstructExpr::CK_Delegating: 49361bc1737SAlexis Hunt // We should be emitting a constructor; GlobalDecl will assert this 49461bc1737SAlexis Hunt Type = CurGD.getCtorType(); 49561535005SDouglas Gregor Delegating = true; 496271c3681SAlexis Hunt break; 49761bc1737SAlexis Hunt 498271c3681SAlexis Hunt case CXXConstructExpr::CK_Complete: 499271c3681SAlexis Hunt Type = Ctor_Complete; 500271c3681SAlexis Hunt break; 501271c3681SAlexis Hunt 502271c3681SAlexis Hunt case CXXConstructExpr::CK_VirtualBase: 503271c3681SAlexis Hunt ForVirtualBase = true; 504271c3681SAlexis Hunt // fall-through 505271c3681SAlexis Hunt 506271c3681SAlexis Hunt case CXXConstructExpr::CK_NonVirtualBase: 507271c3681SAlexis Hunt Type = Ctor_Base; 508271c3681SAlexis Hunt } 509e11f9ce9SAnders Carlsson 51027da15baSAnders Carlsson // Call the constructor. 5117f416cc4SJohn McCall EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, 5127f416cc4SJohn McCall Dest.getAddress(), E); 51327da15baSAnders Carlsson } 514e11f9ce9SAnders Carlsson } 51527da15baSAnders Carlsson 5167f416cc4SJohn McCall void CodeGenFunction::EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, 51750198098SFariborz Jahanian const Expr *Exp) { 5185d413781SJohn McCall if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) 519e988bdacSFariborz Jahanian Exp = E->getSubExpr(); 520e988bdacSFariborz Jahanian assert(isa<CXXConstructExpr>(Exp) && 521e988bdacSFariborz Jahanian "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); 522e988bdacSFariborz Jahanian const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); 523e988bdacSFariborz Jahanian const CXXConstructorDecl *CD = E->getConstructor(); 524e988bdacSFariborz Jahanian RunCleanupsScope Scope(*this); 525e988bdacSFariborz Jahanian 526e988bdacSFariborz Jahanian // If we require zero initialization before (or instead of) calling the 527e988bdacSFariborz Jahanian // constructor, as can be the case with a non-user-provided default 528e988bdacSFariborz Jahanian // constructor, emit the zero initialization now. 529e988bdacSFariborz Jahanian // FIXME. Do I still need this for a copy ctor synthesis? 530e988bdacSFariborz Jahanian if (E->requiresZeroInitialization()) 531e988bdacSFariborz Jahanian EmitNullInitialization(Dest, E->getType()); 532e988bdacSFariborz Jahanian 53399da11cfSChandler Carruth assert(!getContext().getAsConstantArrayType(E->getType()) 53499da11cfSChandler Carruth && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); 535525bf650SAlexey Samsonov EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E); 536e988bdacSFariborz Jahanian } 537e988bdacSFariborz Jahanian 5388ed55a54SJohn McCall static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, 5398ed55a54SJohn McCall const CXXNewExpr *E) { 54021122cf6SAnders Carlsson if (!E->isArray()) 5413eb55cfeSKen Dyck return CharUnits::Zero(); 54221122cf6SAnders Carlsson 5437ec4b434SJohn McCall // No cookie is required if the operator new[] being used is the 5447ec4b434SJohn McCall // reserved placement operator new[]. 5457ec4b434SJohn McCall if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) 5463eb55cfeSKen Dyck return CharUnits::Zero(); 547399f499fSAnders Carlsson 548284c48ffSJohn McCall return CGF.CGM.getCXXABI().GetArrayCookieSize(E); 54959486a2dSAnders Carlsson } 55059486a2dSAnders Carlsson 551036f2f6bSJohn McCall static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, 552036f2f6bSJohn McCall const CXXNewExpr *e, 553f862eb6aSSebastian Redl unsigned minElements, 554036f2f6bSJohn McCall llvm::Value *&numElements, 555036f2f6bSJohn McCall llvm::Value *&sizeWithoutCookie) { 556036f2f6bSJohn McCall QualType type = e->getAllocatedType(); 55759486a2dSAnders Carlsson 558036f2f6bSJohn McCall if (!e->isArray()) { 559036f2f6bSJohn McCall CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 560036f2f6bSJohn McCall sizeWithoutCookie 561036f2f6bSJohn McCall = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); 562036f2f6bSJohn McCall return sizeWithoutCookie; 56305fc5be3SDouglas Gregor } 56459486a2dSAnders Carlsson 565036f2f6bSJohn McCall // The width of size_t. 566036f2f6bSJohn McCall unsigned sizeWidth = CGF.SizeTy->getBitWidth(); 567036f2f6bSJohn McCall 5688ed55a54SJohn McCall // Figure out the cookie size. 569036f2f6bSJohn McCall llvm::APInt cookieSize(sizeWidth, 570036f2f6bSJohn McCall CalculateCookiePadding(CGF, e).getQuantity()); 5718ed55a54SJohn McCall 57259486a2dSAnders Carlsson // Emit the array size expression. 5737648fb46SArgyrios Kyrtzidis // We multiply the size of all dimensions for NumElements. 5747648fb46SArgyrios Kyrtzidis // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. 575036f2f6bSJohn McCall numElements = CGF.EmitScalarExpr(e->getArraySize()); 576036f2f6bSJohn McCall assert(isa<llvm::IntegerType>(numElements->getType())); 5778ed55a54SJohn McCall 578036f2f6bSJohn McCall // The number of elements can be have an arbitrary integer type; 579036f2f6bSJohn McCall // essentially, we need to multiply it by a constant factor, add a 580036f2f6bSJohn McCall // cookie size, and verify that the result is representable as a 581036f2f6bSJohn McCall // size_t. That's just a gloss, though, and it's wrong in one 582036f2f6bSJohn McCall // important way: if the count is negative, it's an error even if 583036f2f6bSJohn McCall // the cookie size would bring the total size >= 0. 5846ab2fa8fSDouglas Gregor bool isSigned 5856ab2fa8fSDouglas Gregor = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType(); 5862192fe50SChris Lattner llvm::IntegerType *numElementsType 587036f2f6bSJohn McCall = cast<llvm::IntegerType>(numElements->getType()); 588036f2f6bSJohn McCall unsigned numElementsWidth = numElementsType->getBitWidth(); 589036f2f6bSJohn McCall 590036f2f6bSJohn McCall // Compute the constant factor. 591036f2f6bSJohn McCall llvm::APInt arraySizeMultiplier(sizeWidth, 1); 5927648fb46SArgyrios Kyrtzidis while (const ConstantArrayType *CAT 593036f2f6bSJohn McCall = CGF.getContext().getAsConstantArrayType(type)) { 594036f2f6bSJohn McCall type = CAT->getElementType(); 595036f2f6bSJohn McCall arraySizeMultiplier *= CAT->getSize(); 5967648fb46SArgyrios Kyrtzidis } 59759486a2dSAnders Carlsson 598036f2f6bSJohn McCall CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); 599036f2f6bSJohn McCall llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); 600036f2f6bSJohn McCall typeSizeMultiplier *= arraySizeMultiplier; 601036f2f6bSJohn McCall 602036f2f6bSJohn McCall // This will be a size_t. 603036f2f6bSJohn McCall llvm::Value *size; 60432ac583dSChris Lattner 60532ac583dSChris Lattner // If someone is doing 'new int[42]' there is no need to do a dynamic check. 60632ac583dSChris Lattner // Don't bloat the -O0 code. 607036f2f6bSJohn McCall if (llvm::ConstantInt *numElementsC = 608036f2f6bSJohn McCall dyn_cast<llvm::ConstantInt>(numElements)) { 609036f2f6bSJohn McCall const llvm::APInt &count = numElementsC->getValue(); 61032ac583dSChris Lattner 611036f2f6bSJohn McCall bool hasAnyOverflow = false; 61232ac583dSChris Lattner 613036f2f6bSJohn McCall // If 'count' was a negative number, it's an overflow. 614036f2f6bSJohn McCall if (isSigned && count.isNegative()) 615036f2f6bSJohn McCall hasAnyOverflow = true; 6168ed55a54SJohn McCall 617036f2f6bSJohn McCall // We want to do all this arithmetic in size_t. If numElements is 618036f2f6bSJohn McCall // wider than that, check whether it's already too big, and if so, 619036f2f6bSJohn McCall // overflow. 620036f2f6bSJohn McCall else if (numElementsWidth > sizeWidth && 621036f2f6bSJohn McCall numElementsWidth - sizeWidth > count.countLeadingZeros()) 622036f2f6bSJohn McCall hasAnyOverflow = true; 623036f2f6bSJohn McCall 624036f2f6bSJohn McCall // Okay, compute a count at the right width. 625036f2f6bSJohn McCall llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); 626036f2f6bSJohn McCall 627f862eb6aSSebastian Redl // If there is a brace-initializer, we cannot allocate fewer elements than 628f862eb6aSSebastian Redl // there are initializers. If we do, that's treated like an overflow. 629f862eb6aSSebastian Redl if (adjustedCount.ult(minElements)) 630f862eb6aSSebastian Redl hasAnyOverflow = true; 631f862eb6aSSebastian Redl 632036f2f6bSJohn McCall // Scale numElements by that. This might overflow, but we don't 633036f2f6bSJohn McCall // care because it only overflows if allocationSize does, too, and 634036f2f6bSJohn McCall // if that overflows then we shouldn't use this. 635036f2f6bSJohn McCall numElements = llvm::ConstantInt::get(CGF.SizeTy, 636036f2f6bSJohn McCall adjustedCount * arraySizeMultiplier); 637036f2f6bSJohn McCall 638036f2f6bSJohn McCall // Compute the size before cookie, and track whether it overflowed. 639036f2f6bSJohn McCall bool overflow; 640036f2f6bSJohn McCall llvm::APInt allocationSize 641036f2f6bSJohn McCall = adjustedCount.umul_ov(typeSizeMultiplier, overflow); 642036f2f6bSJohn McCall hasAnyOverflow |= overflow; 643036f2f6bSJohn McCall 644036f2f6bSJohn McCall // Add in the cookie, and check whether it's overflowed. 645036f2f6bSJohn McCall if (cookieSize != 0) { 646036f2f6bSJohn McCall // Save the current size without a cookie. This shouldn't be 647036f2f6bSJohn McCall // used if there was overflow. 648036f2f6bSJohn McCall sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 649036f2f6bSJohn McCall 650036f2f6bSJohn McCall allocationSize = allocationSize.uadd_ov(cookieSize, overflow); 651036f2f6bSJohn McCall hasAnyOverflow |= overflow; 6528ed55a54SJohn McCall } 6538ed55a54SJohn McCall 654036f2f6bSJohn McCall // On overflow, produce a -1 so operator new will fail. 655455f42c9SAaron Ballman if (hasAnyOverflow) { 656455f42c9SAaron Ballman size = llvm::Constant::getAllOnesValue(CGF.SizeTy); 657455f42c9SAaron Ballman } else { 658036f2f6bSJohn McCall size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); 659455f42c9SAaron Ballman } 66032ac583dSChris Lattner 661036f2f6bSJohn McCall // Otherwise, we might need to use the overflow intrinsics. 6628ed55a54SJohn McCall } else { 663f862eb6aSSebastian Redl // There are up to five conditions we need to test for: 664036f2f6bSJohn McCall // 1) if isSigned, we need to check whether numElements is negative; 665036f2f6bSJohn McCall // 2) if numElementsWidth > sizeWidth, we need to check whether 666036f2f6bSJohn McCall // numElements is larger than something representable in size_t; 667f862eb6aSSebastian Redl // 3) if minElements > 0, we need to check whether numElements is smaller 668f862eb6aSSebastian Redl // than that. 669f862eb6aSSebastian Redl // 4) we need to compute 670036f2f6bSJohn McCall // sizeWithoutCookie := numElements * typeSizeMultiplier 671036f2f6bSJohn McCall // and check whether it overflows; and 672f862eb6aSSebastian Redl // 5) if we need a cookie, we need to compute 673036f2f6bSJohn McCall // size := sizeWithoutCookie + cookieSize 674036f2f6bSJohn McCall // and check whether it overflows. 6758ed55a54SJohn McCall 6768a13c418SCraig Topper llvm::Value *hasOverflow = nullptr; 6778ed55a54SJohn McCall 678036f2f6bSJohn McCall // If numElementsWidth > sizeWidth, then one way or another, we're 679036f2f6bSJohn McCall // going to have to do a comparison for (2), and this happens to 680036f2f6bSJohn McCall // take care of (1), too. 681036f2f6bSJohn McCall if (numElementsWidth > sizeWidth) { 682036f2f6bSJohn McCall llvm::APInt threshold(numElementsWidth, 1); 683036f2f6bSJohn McCall threshold <<= sizeWidth; 6848ed55a54SJohn McCall 685036f2f6bSJohn McCall llvm::Value *thresholdV 686036f2f6bSJohn McCall = llvm::ConstantInt::get(numElementsType, threshold); 687036f2f6bSJohn McCall 688036f2f6bSJohn McCall hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); 689036f2f6bSJohn McCall numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); 690036f2f6bSJohn McCall 691036f2f6bSJohn McCall // Otherwise, if we're signed, we want to sext up to size_t. 692036f2f6bSJohn McCall } else if (isSigned) { 693036f2f6bSJohn McCall if (numElementsWidth < sizeWidth) 694036f2f6bSJohn McCall numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); 695036f2f6bSJohn McCall 696036f2f6bSJohn McCall // If there's a non-1 type size multiplier, then we can do the 697036f2f6bSJohn McCall // signedness check at the same time as we do the multiply 698036f2f6bSJohn McCall // because a negative number times anything will cause an 699f862eb6aSSebastian Redl // unsigned overflow. Otherwise, we have to do it here. But at least 700f862eb6aSSebastian Redl // in this case, we can subsume the >= minElements check. 701036f2f6bSJohn McCall if (typeSizeMultiplier == 1) 702036f2f6bSJohn McCall hasOverflow = CGF.Builder.CreateICmpSLT(numElements, 703f862eb6aSSebastian Redl llvm::ConstantInt::get(CGF.SizeTy, minElements)); 704036f2f6bSJohn McCall 705036f2f6bSJohn McCall // Otherwise, zext up to size_t if necessary. 706036f2f6bSJohn McCall } else if (numElementsWidth < sizeWidth) { 707036f2f6bSJohn McCall numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); 708036f2f6bSJohn McCall } 709036f2f6bSJohn McCall 710036f2f6bSJohn McCall assert(numElements->getType() == CGF.SizeTy); 711036f2f6bSJohn McCall 712f862eb6aSSebastian Redl if (minElements) { 713f862eb6aSSebastian Redl // Don't allow allocation of fewer elements than we have initializers. 714f862eb6aSSebastian Redl if (!hasOverflow) { 715f862eb6aSSebastian Redl hasOverflow = CGF.Builder.CreateICmpULT(numElements, 716f862eb6aSSebastian Redl llvm::ConstantInt::get(CGF.SizeTy, minElements)); 717f862eb6aSSebastian Redl } else if (numElementsWidth > sizeWidth) { 718f862eb6aSSebastian Redl // The other existing overflow subsumes this check. 719f862eb6aSSebastian Redl // We do an unsigned comparison, since any signed value < -1 is 720f862eb6aSSebastian Redl // taken care of either above or below. 721f862eb6aSSebastian Redl hasOverflow = CGF.Builder.CreateOr(hasOverflow, 722f862eb6aSSebastian Redl CGF.Builder.CreateICmpULT(numElements, 723f862eb6aSSebastian Redl llvm::ConstantInt::get(CGF.SizeTy, minElements))); 724f862eb6aSSebastian Redl } 725f862eb6aSSebastian Redl } 726f862eb6aSSebastian Redl 727036f2f6bSJohn McCall size = numElements; 728036f2f6bSJohn McCall 729036f2f6bSJohn McCall // Multiply by the type size if necessary. This multiplier 730036f2f6bSJohn McCall // includes all the factors for nested arrays. 7318ed55a54SJohn McCall // 732036f2f6bSJohn McCall // This step also causes numElements to be scaled up by the 733036f2f6bSJohn McCall // nested-array factor if necessary. Overflow on this computation 734036f2f6bSJohn McCall // can be ignored because the result shouldn't be used if 735036f2f6bSJohn McCall // allocation fails. 736036f2f6bSJohn McCall if (typeSizeMultiplier != 1) { 737036f2f6bSJohn McCall llvm::Value *umul_with_overflow 7388d375cefSBenjamin Kramer = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); 7398ed55a54SJohn McCall 740036f2f6bSJohn McCall llvm::Value *tsmV = 741036f2f6bSJohn McCall llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); 742036f2f6bSJohn McCall llvm::Value *result = 74343f9bb73SDavid Blaikie CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV}); 7448ed55a54SJohn McCall 745036f2f6bSJohn McCall llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 746036f2f6bSJohn McCall if (hasOverflow) 747036f2f6bSJohn McCall hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 7488ed55a54SJohn McCall else 749036f2f6bSJohn McCall hasOverflow = overflowed; 75059486a2dSAnders Carlsson 751036f2f6bSJohn McCall size = CGF.Builder.CreateExtractValue(result, 0); 752036f2f6bSJohn McCall 753036f2f6bSJohn McCall // Also scale up numElements by the array size multiplier. 754036f2f6bSJohn McCall if (arraySizeMultiplier != 1) { 755036f2f6bSJohn McCall // If the base element type size is 1, then we can re-use the 756036f2f6bSJohn McCall // multiply we just did. 757036f2f6bSJohn McCall if (typeSize.isOne()) { 758036f2f6bSJohn McCall assert(arraySizeMultiplier == typeSizeMultiplier); 759036f2f6bSJohn McCall numElements = size; 760036f2f6bSJohn McCall 761036f2f6bSJohn McCall // Otherwise we need a separate multiply. 762036f2f6bSJohn McCall } else { 763036f2f6bSJohn McCall llvm::Value *asmV = 764036f2f6bSJohn McCall llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); 765036f2f6bSJohn McCall numElements = CGF.Builder.CreateMul(numElements, asmV); 766036f2f6bSJohn McCall } 767036f2f6bSJohn McCall } 768036f2f6bSJohn McCall } else { 769036f2f6bSJohn McCall // numElements doesn't need to be scaled. 770036f2f6bSJohn McCall assert(arraySizeMultiplier == 1); 771036f2f6bSJohn McCall } 772036f2f6bSJohn McCall 773036f2f6bSJohn McCall // Add in the cookie size if necessary. 774036f2f6bSJohn McCall if (cookieSize != 0) { 775036f2f6bSJohn McCall sizeWithoutCookie = size; 776036f2f6bSJohn McCall 777036f2f6bSJohn McCall llvm::Value *uadd_with_overflow 7788d375cefSBenjamin Kramer = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); 779036f2f6bSJohn McCall 780036f2f6bSJohn McCall llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); 781036f2f6bSJohn McCall llvm::Value *result = 78243f9bb73SDavid Blaikie CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV}); 783036f2f6bSJohn McCall 784036f2f6bSJohn McCall llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); 785036f2f6bSJohn McCall if (hasOverflow) 786036f2f6bSJohn McCall hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); 787036f2f6bSJohn McCall else 788036f2f6bSJohn McCall hasOverflow = overflowed; 789036f2f6bSJohn McCall 790036f2f6bSJohn McCall size = CGF.Builder.CreateExtractValue(result, 0); 791036f2f6bSJohn McCall } 792036f2f6bSJohn McCall 793036f2f6bSJohn McCall // If we had any possibility of dynamic overflow, make a select to 794036f2f6bSJohn McCall // overwrite 'size' with an all-ones value, which should cause 795036f2f6bSJohn McCall // operator new to throw. 796036f2f6bSJohn McCall if (hasOverflow) 797455f42c9SAaron Ballman size = CGF.Builder.CreateSelect(hasOverflow, 798455f42c9SAaron Ballman llvm::Constant::getAllOnesValue(CGF.SizeTy), 799036f2f6bSJohn McCall size); 800036f2f6bSJohn McCall } 801036f2f6bSJohn McCall 802036f2f6bSJohn McCall if (cookieSize == 0) 803036f2f6bSJohn McCall sizeWithoutCookie = size; 804036f2f6bSJohn McCall else 805036f2f6bSJohn McCall assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); 806036f2f6bSJohn McCall 807036f2f6bSJohn McCall return size; 80859486a2dSAnders Carlsson } 80959486a2dSAnders Carlsson 810f862eb6aSSebastian Redl static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, 8117f416cc4SJohn McCall QualType AllocType, Address NewPtr) { 8121c96bc5dSRichard Smith // FIXME: Refactor with EmitExprAsInit. 81347fb9508SJohn McCall switch (CGF.getEvaluationKind(AllocType)) { 81447fb9508SJohn McCall case TEK_Scalar: 815a2c1124fSDavid Blaikie CGF.EmitScalarInit(Init, nullptr, 8167f416cc4SJohn McCall CGF.MakeAddrLValue(NewPtr, AllocType), false); 81747fb9508SJohn McCall return; 81847fb9508SJohn McCall case TEK_Complex: 8197f416cc4SJohn McCall CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType), 82047fb9508SJohn McCall /*isInit*/ true); 82147fb9508SJohn McCall return; 82247fb9508SJohn McCall case TEK_Aggregate: { 8237a626f63SJohn McCall AggValueSlot Slot 8247f416cc4SJohn McCall = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(), 8258d6fc958SJohn McCall AggValueSlot::IsDestructed, 82646759f4fSJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 827615ed1a3SChad Rosier AggValueSlot::IsNotAliased); 8287a626f63SJohn McCall CGF.EmitAggExpr(Init, Slot); 82947fb9508SJohn McCall return; 8307a626f63SJohn McCall } 831d5202e09SFariborz Jahanian } 83247fb9508SJohn McCall llvm_unreachable("bad evaluation kind"); 83347fb9508SJohn McCall } 834d5202e09SFariborz Jahanian 835fb901c7aSDavid Blaikie void CodeGenFunction::EmitNewArrayInitializer( 836fb901c7aSDavid Blaikie const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, 8377f416cc4SJohn McCall Address BeginPtr, llvm::Value *NumElements, 83806a67e2cSRichard Smith llvm::Value *AllocSizeWithoutCookie) { 83906a67e2cSRichard Smith // If we have a type with trivial initialization and no initializer, 84006a67e2cSRichard Smith // there's nothing to do. 8416047f07eSSebastian Redl if (!E->hasInitializer()) 84206a67e2cSRichard Smith return; 843b66b08efSFariborz Jahanian 8447f416cc4SJohn McCall Address CurPtr = BeginPtr; 845d5202e09SFariborz Jahanian 84606a67e2cSRichard Smith unsigned InitListElements = 0; 847f862eb6aSSebastian Redl 848f862eb6aSSebastian Redl const Expr *Init = E->getInitializer(); 8497f416cc4SJohn McCall Address EndOfInit = Address::invalid(); 85006a67e2cSRichard Smith QualType::DestructionKind DtorKind = ElementType.isDestructedType(); 85106a67e2cSRichard Smith EHScopeStack::stable_iterator Cleanup; 85206a67e2cSRichard Smith llvm::Instruction *CleanupDominator = nullptr; 8531c96bc5dSRichard Smith 8547f416cc4SJohn McCall CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType); 8557f416cc4SJohn McCall CharUnits ElementAlign = 8567f416cc4SJohn McCall BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize); 8577f416cc4SJohn McCall 858f862eb6aSSebastian Redl // If the initializer is an initializer list, first do the explicit elements. 859f862eb6aSSebastian Redl if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { 86006a67e2cSRichard Smith InitListElements = ILE->getNumInits(); 861f62290a1SChad Rosier 8621c96bc5dSRichard Smith // If this is a multi-dimensional array new, we will initialize multiple 8631c96bc5dSRichard Smith // elements with each init list element. 8641c96bc5dSRichard Smith QualType AllocType = E->getAllocatedType(); 8651c96bc5dSRichard Smith if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>( 8661c96bc5dSRichard Smith AllocType->getAsArrayTypeUnsafe())) { 867fb901c7aSDavid Blaikie ElementTy = ConvertTypeForMem(AllocType); 8687f416cc4SJohn McCall CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy); 86906a67e2cSRichard Smith InitListElements *= getContext().getConstantArrayElementCount(CAT); 8701c96bc5dSRichard Smith } 8711c96bc5dSRichard Smith 87206a67e2cSRichard Smith // Enter a partial-destruction Cleanup if necessary. 87306a67e2cSRichard Smith if (needsEHCleanup(DtorKind)) { 87406a67e2cSRichard Smith // In principle we could tell the Cleanup where we are more 875f62290a1SChad Rosier // directly, but the control flow can get so varied here that it 876f62290a1SChad Rosier // would actually be quite complex. Therefore we go through an 877f62290a1SChad Rosier // alloca. 8787f416cc4SJohn McCall EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(), 8797f416cc4SJohn McCall "array.init.end"); 8807f416cc4SJohn McCall CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit); 8817f416cc4SJohn McCall pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit, 8827f416cc4SJohn McCall ElementType, ElementAlign, 88306a67e2cSRichard Smith getDestroyer(DtorKind)); 88406a67e2cSRichard Smith Cleanup = EHStack.stable_begin(); 885f62290a1SChad Rosier } 886f62290a1SChad Rosier 8877f416cc4SJohn McCall CharUnits StartAlign = CurPtr.getAlignment(); 888f862eb6aSSebastian Redl for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { 889f62290a1SChad Rosier // Tell the cleanup that it needs to destroy up to this 890f62290a1SChad Rosier // element. TODO: some of these stores can be trivially 891f62290a1SChad Rosier // observed to be unnecessary. 8927f416cc4SJohn McCall if (EndOfInit.isValid()) { 8937f416cc4SJohn McCall auto FinishedPtr = 8947f416cc4SJohn McCall Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType()); 8957f416cc4SJohn McCall Builder.CreateStore(FinishedPtr, EndOfInit); 8967f416cc4SJohn McCall } 89706a67e2cSRichard Smith // FIXME: If the last initializer is an incomplete initializer list for 89806a67e2cSRichard Smith // an array, and we have an array filler, we can fold together the two 89906a67e2cSRichard Smith // initialization loops. 9001c96bc5dSRichard Smith StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), 90106a67e2cSRichard Smith ILE->getInit(i)->getType(), CurPtr); 9027f416cc4SJohn McCall CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), 9037f416cc4SJohn McCall Builder.getSize(1), 9047f416cc4SJohn McCall "array.exp.next"), 9057f416cc4SJohn McCall StartAlign.alignmentAtOffset((i + 1) * ElementSize)); 906f862eb6aSSebastian Redl } 907f862eb6aSSebastian Redl 908f862eb6aSSebastian Redl // The remaining elements are filled with the array filler expression. 909f862eb6aSSebastian Redl Init = ILE->getArrayFiller(); 9101c96bc5dSRichard Smith 91106a67e2cSRichard Smith // Extract the initializer for the individual array elements by pulling 91206a67e2cSRichard Smith // out the array filler from all the nested initializer lists. This avoids 91306a67e2cSRichard Smith // generating a nested loop for the initialization. 91406a67e2cSRichard Smith while (Init && Init->getType()->isConstantArrayType()) { 91506a67e2cSRichard Smith auto *SubILE = dyn_cast<InitListExpr>(Init); 91606a67e2cSRichard Smith if (!SubILE) 91706a67e2cSRichard Smith break; 91806a67e2cSRichard Smith assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?"); 91906a67e2cSRichard Smith Init = SubILE->getArrayFiller(); 920f862eb6aSSebastian Redl } 921f862eb6aSSebastian Redl 92206a67e2cSRichard Smith // Switch back to initializing one base element at a time. 9237f416cc4SJohn McCall CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr.getType()); 924f62290a1SChad Rosier } 925e6c980c4SChandler Carruth 92606a67e2cSRichard Smith // Attempt to perform zero-initialization using memset. 92706a67e2cSRichard Smith auto TryMemsetInitialization = [&]() -> bool { 92806a67e2cSRichard Smith // FIXME: If the type is a pointer-to-data-member under the Itanium ABI, 92906a67e2cSRichard Smith // we can initialize with a memset to -1. 93006a67e2cSRichard Smith if (!CGM.getTypes().isZeroInitializable(ElementType)) 93106a67e2cSRichard Smith return false; 932e6c980c4SChandler Carruth 93306a67e2cSRichard Smith // Optimization: since zero initialization will just set the memory 93406a67e2cSRichard Smith // to all zeroes, generate a single memset to do it in one shot. 93506a67e2cSRichard Smith 93606a67e2cSRichard Smith // Subtract out the size of any elements we've already initialized. 93706a67e2cSRichard Smith auto *RemainingSize = AllocSizeWithoutCookie; 93806a67e2cSRichard Smith if (InitListElements) { 93906a67e2cSRichard Smith // We know this can't overflow; we check this when doing the allocation. 94006a67e2cSRichard Smith auto *InitializedSize = llvm::ConstantInt::get( 94106a67e2cSRichard Smith RemainingSize->getType(), 94206a67e2cSRichard Smith getContext().getTypeSizeInChars(ElementType).getQuantity() * 94306a67e2cSRichard Smith InitListElements); 94406a67e2cSRichard Smith RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize); 94599210dc9SJohn McCall } 946d5202e09SFariborz Jahanian 94706a67e2cSRichard Smith // Create the memset. 9487f416cc4SJohn McCall Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false); 94906a67e2cSRichard Smith return true; 95006a67e2cSRichard Smith }; 95105fc5be3SDouglas Gregor 952454a7cdfSRichard Smith // If all elements have already been initialized, skip any further 953454a7cdfSRichard Smith // initialization. 954454a7cdfSRichard Smith llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); 955454a7cdfSRichard Smith if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { 956454a7cdfSRichard Smith // If there was a Cleanup, deactivate it. 957454a7cdfSRichard Smith if (CleanupDominator) 958454a7cdfSRichard Smith DeactivateCleanupBlock(Cleanup, CleanupDominator); 959454a7cdfSRichard Smith return; 960454a7cdfSRichard Smith } 961454a7cdfSRichard Smith 962454a7cdfSRichard Smith assert(Init && "have trailing elements to initialize but no initializer"); 963454a7cdfSRichard Smith 96406a67e2cSRichard Smith // If this is a constructor call, try to optimize it out, and failing that 96506a67e2cSRichard Smith // emit a single loop to initialize all remaining elements. 966454a7cdfSRichard Smith if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { 9676047f07eSSebastian Redl CXXConstructorDecl *Ctor = CCE->getConstructor(); 968d153103cSDouglas Gregor if (Ctor->isTrivial()) { 96905fc5be3SDouglas Gregor // If new expression did not specify value-initialization, then there 97005fc5be3SDouglas Gregor // is no initialization. 9716047f07eSSebastian Redl if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) 97205fc5be3SDouglas Gregor return; 97305fc5be3SDouglas Gregor 97406a67e2cSRichard Smith if (TryMemsetInitialization()) 9753a202f60SAnders Carlsson return; 9763a202f60SAnders Carlsson } 97705fc5be3SDouglas Gregor 97806a67e2cSRichard Smith // Store the new Cleanup position for irregular Cleanups. 97906a67e2cSRichard Smith // 98006a67e2cSRichard Smith // FIXME: Share this cleanup with the constructor call emission rather than 98106a67e2cSRichard Smith // having it create a cleanup of its own. 9827f416cc4SJohn McCall if (EndOfInit.isValid()) 9837f416cc4SJohn McCall Builder.CreateStore(CurPtr.getPointer(), EndOfInit); 98406a67e2cSRichard Smith 98506a67e2cSRichard Smith // Emit a constructor call loop to initialize the remaining elements. 98606a67e2cSRichard Smith if (InitListElements) 98706a67e2cSRichard Smith NumElements = Builder.CreateSub( 98806a67e2cSRichard Smith NumElements, 98906a67e2cSRichard Smith llvm::ConstantInt::get(NumElements->getType(), InitListElements)); 99070b9c01bSAlexey Samsonov EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE, 99148ddcf2cSEli Friedman CCE->requiresZeroInitialization()); 99205fc5be3SDouglas Gregor return; 9936047f07eSSebastian Redl } 99406a67e2cSRichard Smith 99506a67e2cSRichard Smith // If this is value-initialization, we can usually use memset. 99606a67e2cSRichard Smith ImplicitValueInitExpr IVIE(ElementType); 997454a7cdfSRichard Smith if (isa<ImplicitValueInitExpr>(Init)) { 99806a67e2cSRichard Smith if (TryMemsetInitialization()) 99906a67e2cSRichard Smith return; 100006a67e2cSRichard Smith 100106a67e2cSRichard Smith // Switch to an ImplicitValueInitExpr for the element type. This handles 100206a67e2cSRichard Smith // only one case: multidimensional array new of pointers to members. In 100306a67e2cSRichard Smith // all other cases, we already have an initializer for the array element. 100406a67e2cSRichard Smith Init = &IVIE; 100506a67e2cSRichard Smith } 100606a67e2cSRichard Smith 100706a67e2cSRichard Smith // At this point we should have found an initializer for the individual 100806a67e2cSRichard Smith // elements of the array. 100906a67e2cSRichard Smith assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && 101006a67e2cSRichard Smith "got wrong type of element to initialize"); 101106a67e2cSRichard Smith 1012454a7cdfSRichard Smith // If we have an empty initializer list, we can usually use memset. 1013454a7cdfSRichard Smith if (auto *ILE = dyn_cast<InitListExpr>(Init)) 1014454a7cdfSRichard Smith if (ILE->getNumInits() == 0 && TryMemsetInitialization()) 1015d5202e09SFariborz Jahanian return; 101659486a2dSAnders Carlsson 1017cb77930dSYunzhong Gao // If we have a struct whose every field is value-initialized, we can 1018cb77930dSYunzhong Gao // usually use memset. 1019cb77930dSYunzhong Gao if (auto *ILE = dyn_cast<InitListExpr>(Init)) { 1020cb77930dSYunzhong Gao if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { 1021cb77930dSYunzhong Gao if (RType->getDecl()->isStruct()) { 1022872307e2SRichard Smith unsigned NumElements = 0; 1023872307e2SRichard Smith if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RType->getDecl())) 1024872307e2SRichard Smith NumElements = CXXRD->getNumBases(); 1025cb77930dSYunzhong Gao for (auto *Field : RType->getDecl()->fields()) 1026cb77930dSYunzhong Gao if (!Field->isUnnamedBitfield()) 1027872307e2SRichard Smith ++NumElements; 1028872307e2SRichard Smith // FIXME: Recurse into nested InitListExprs. 1029872307e2SRichard Smith if (ILE->getNumInits() == NumElements) 1030cb77930dSYunzhong Gao for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 1031cb77930dSYunzhong Gao if (!isa<ImplicitValueInitExpr>(ILE->getInit(i))) 1032872307e2SRichard Smith --NumElements; 1033872307e2SRichard Smith if (ILE->getNumInits() == NumElements && TryMemsetInitialization()) 1034cb77930dSYunzhong Gao return; 1035cb77930dSYunzhong Gao } 1036cb77930dSYunzhong Gao } 1037cb77930dSYunzhong Gao } 1038cb77930dSYunzhong Gao 103906a67e2cSRichard Smith // Create the loop blocks. 104006a67e2cSRichard Smith llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); 104106a67e2cSRichard Smith llvm::BasicBlock *LoopBB = createBasicBlock("new.loop"); 104206a67e2cSRichard Smith llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end"); 104359486a2dSAnders Carlsson 104406a67e2cSRichard Smith // Find the end of the array, hoisted out of the loop. 104506a67e2cSRichard Smith llvm::Value *EndPtr = 10467f416cc4SJohn McCall Builder.CreateInBoundsGEP(BeginPtr.getPointer(), NumElements, "array.end"); 104706a67e2cSRichard Smith 104806a67e2cSRichard Smith // If the number of elements isn't constant, we have to now check if there is 104906a67e2cSRichard Smith // anything left to initialize. 105006a67e2cSRichard Smith if (!ConstNum) { 10517f416cc4SJohn McCall llvm::Value *IsEmpty = 10527f416cc4SJohn McCall Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty"); 105306a67e2cSRichard Smith Builder.CreateCondBr(IsEmpty, ContBB, LoopBB); 105406a67e2cSRichard Smith } 105506a67e2cSRichard Smith 105606a67e2cSRichard Smith // Enter the loop. 105706a67e2cSRichard Smith EmitBlock(LoopBB); 105806a67e2cSRichard Smith 105906a67e2cSRichard Smith // Set up the current-element phi. 106006a67e2cSRichard Smith llvm::PHINode *CurPtrPhi = 10617f416cc4SJohn McCall Builder.CreatePHI(CurPtr.getType(), 2, "array.cur"); 10627f416cc4SJohn McCall CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB); 10637f416cc4SJohn McCall 10647f416cc4SJohn McCall CurPtr = Address(CurPtrPhi, ElementAlign); 106506a67e2cSRichard Smith 106606a67e2cSRichard Smith // Store the new Cleanup position for irregular Cleanups. 10677f416cc4SJohn McCall if (EndOfInit.isValid()) 10687f416cc4SJohn McCall Builder.CreateStore(CurPtr.getPointer(), EndOfInit); 106906a67e2cSRichard Smith 107006a67e2cSRichard Smith // Enter a partial-destruction Cleanup if necessary. 107106a67e2cSRichard Smith if (!CleanupDominator && needsEHCleanup(DtorKind)) { 10727f416cc4SJohn McCall pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(), 10737f416cc4SJohn McCall ElementType, ElementAlign, 107406a67e2cSRichard Smith getDestroyer(DtorKind)); 107506a67e2cSRichard Smith Cleanup = EHStack.stable_begin(); 107606a67e2cSRichard Smith CleanupDominator = Builder.CreateUnreachable(); 107706a67e2cSRichard Smith } 107806a67e2cSRichard Smith 107906a67e2cSRichard Smith // Emit the initializer into this element. 108006a67e2cSRichard Smith StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr); 108106a67e2cSRichard Smith 108206a67e2cSRichard Smith // Leave the Cleanup if we entered one. 108306a67e2cSRichard Smith if (CleanupDominator) { 108406a67e2cSRichard Smith DeactivateCleanupBlock(Cleanup, CleanupDominator); 108506a67e2cSRichard Smith CleanupDominator->eraseFromParent(); 108606a67e2cSRichard Smith } 108706a67e2cSRichard Smith 108806a67e2cSRichard Smith // Advance to the next element by adjusting the pointer type as necessary. 108906a67e2cSRichard Smith llvm::Value *NextPtr = 10907f416cc4SJohn McCall Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1, 10917f416cc4SJohn McCall "array.next"); 109206a67e2cSRichard Smith 109306a67e2cSRichard Smith // Check whether we've gotten to the end of the array and, if so, 109406a67e2cSRichard Smith // exit the loop. 109506a67e2cSRichard Smith llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend"); 109606a67e2cSRichard Smith Builder.CreateCondBr(IsEnd, ContBB, LoopBB); 109706a67e2cSRichard Smith CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock()); 109806a67e2cSRichard Smith 109906a67e2cSRichard Smith EmitBlock(ContBB); 110006a67e2cSRichard Smith } 110106a67e2cSRichard Smith 110206a67e2cSRichard Smith static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, 1103fb901c7aSDavid Blaikie QualType ElementType, llvm::Type *ElementTy, 11047f416cc4SJohn McCall Address NewPtr, llvm::Value *NumElements, 110506a67e2cSRichard Smith llvm::Value *AllocSizeWithoutCookie) { 11069b479666SDavid Blaikie ApplyDebugLocation DL(CGF, E); 110706a67e2cSRichard Smith if (E->isArray()) 1108fb901c7aSDavid Blaikie CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements, 110906a67e2cSRichard Smith AllocSizeWithoutCookie); 111006a67e2cSRichard Smith else if (const Expr *Init = E->getInitializer()) 111166e4197fSDavid Blaikie StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr); 111259486a2dSAnders Carlsson } 111359486a2dSAnders Carlsson 11148d0dc31dSRichard Smith /// Emit a call to an operator new or operator delete function, as implicitly 11158d0dc31dSRichard Smith /// created by new-expressions and delete-expressions. 11168d0dc31dSRichard Smith static RValue EmitNewDeleteCall(CodeGenFunction &CGF, 11178d0dc31dSRichard Smith const FunctionDecl *Callee, 11188d0dc31dSRichard Smith const FunctionProtoType *CalleeType, 11198d0dc31dSRichard Smith const CallArgList &Args) { 11208d0dc31dSRichard Smith llvm::Instruction *CallOrInvoke; 11211235a8daSRichard Smith llvm::Value *CalleeAddr = CGF.CGM.GetAddrOfFunction(Callee); 11228d0dc31dSRichard Smith RValue RV = 1123f770683fSPeter Collingbourne CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall( 1124f770683fSPeter Collingbourne Args, CalleeType, /*chainCall=*/false), 1125f770683fSPeter Collingbourne CalleeAddr, ReturnValueSlot(), Args, Callee, &CallOrInvoke); 11268d0dc31dSRichard Smith 11278d0dc31dSRichard Smith /// C++1y [expr.new]p10: 11288d0dc31dSRichard Smith /// [In a new-expression,] an implementation is allowed to omit a call 11298d0dc31dSRichard Smith /// to a replaceable global allocation function. 11308d0dc31dSRichard Smith /// 11318d0dc31dSRichard Smith /// We model such elidable calls with the 'builtin' attribute. 11326956d587SRafael Espindola llvm::Function *Fn = dyn_cast<llvm::Function>(CalleeAddr); 11331235a8daSRichard Smith if (Callee->isReplaceableGlobalAllocationFunction() && 11346956d587SRafael Espindola Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) { 11358d0dc31dSRichard Smith // FIXME: Add addAttribute to CallSite. 11368d0dc31dSRichard Smith if (llvm::CallInst *CI = dyn_cast<llvm::CallInst>(CallOrInvoke)) 11378d0dc31dSRichard Smith CI->addAttribute(llvm::AttributeSet::FunctionIndex, 11388d0dc31dSRichard Smith llvm::Attribute::Builtin); 11398d0dc31dSRichard Smith else if (llvm::InvokeInst *II = dyn_cast<llvm::InvokeInst>(CallOrInvoke)) 11408d0dc31dSRichard Smith II->addAttribute(llvm::AttributeSet::FunctionIndex, 11418d0dc31dSRichard Smith llvm::Attribute::Builtin); 11428d0dc31dSRichard Smith else 11438d0dc31dSRichard Smith llvm_unreachable("unexpected kind of call instruction"); 11448d0dc31dSRichard Smith } 11458d0dc31dSRichard Smith 11468d0dc31dSRichard Smith return RV; 11478d0dc31dSRichard Smith } 11488d0dc31dSRichard Smith 1149760520bcSRichard Smith RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, 1150760520bcSRichard Smith const Expr *Arg, 1151760520bcSRichard Smith bool IsDelete) { 1152760520bcSRichard Smith CallArgList Args; 1153760520bcSRichard Smith const Stmt *ArgS = Arg; 1154f05779e2SDavid Blaikie EmitCallArgs(Args, *Type->param_type_begin(), llvm::makeArrayRef(ArgS)); 1155760520bcSRichard Smith // Find the allocation or deallocation function that we're calling. 1156760520bcSRichard Smith ASTContext &Ctx = getContext(); 1157760520bcSRichard Smith DeclarationName Name = Ctx.DeclarationNames 1158760520bcSRichard Smith .getCXXOperatorName(IsDelete ? OO_Delete : OO_New); 1159760520bcSRichard Smith for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name)) 1160599bed75SRichard Smith if (auto *FD = dyn_cast<FunctionDecl>(Decl)) 1161599bed75SRichard Smith if (Ctx.hasSameType(FD->getType(), QualType(Type, 0))) 1162760520bcSRichard Smith return EmitNewDeleteCall(*this, cast<FunctionDecl>(Decl), Type, Args); 1163760520bcSRichard Smith llvm_unreachable("predeclared global operator new/delete is missing"); 1164760520bcSRichard Smith } 1165760520bcSRichard Smith 1166824c2f53SJohn McCall namespace { 1167824c2f53SJohn McCall /// A cleanup to call the given 'operator delete' function upon 1168824c2f53SJohn McCall /// abnormal exit from a new expression. 11697e70d680SDavid Blaikie class CallDeleteDuringNew final : public EHScopeStack::Cleanup { 1170824c2f53SJohn McCall size_t NumPlacementArgs; 1171824c2f53SJohn McCall const FunctionDecl *OperatorDelete; 1172824c2f53SJohn McCall llvm::Value *Ptr; 1173824c2f53SJohn McCall llvm::Value *AllocSize; 1174824c2f53SJohn McCall 1175824c2f53SJohn McCall RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } 1176824c2f53SJohn McCall 1177824c2f53SJohn McCall public: 1178824c2f53SJohn McCall static size_t getExtraSize(size_t NumPlacementArgs) { 1179824c2f53SJohn McCall return NumPlacementArgs * sizeof(RValue); 1180824c2f53SJohn McCall } 1181824c2f53SJohn McCall 1182824c2f53SJohn McCall CallDeleteDuringNew(size_t NumPlacementArgs, 1183824c2f53SJohn McCall const FunctionDecl *OperatorDelete, 1184824c2f53SJohn McCall llvm::Value *Ptr, 1185824c2f53SJohn McCall llvm::Value *AllocSize) 1186824c2f53SJohn McCall : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 1187824c2f53SJohn McCall Ptr(Ptr), AllocSize(AllocSize) {} 1188824c2f53SJohn McCall 1189824c2f53SJohn McCall void setPlacementArg(unsigned I, RValue Arg) { 1190824c2f53SJohn McCall assert(I < NumPlacementArgs && "index out of range"); 1191824c2f53SJohn McCall getPlacementArgs()[I] = Arg; 1192824c2f53SJohn McCall } 1193824c2f53SJohn McCall 11944f12f10dSCraig Topper void Emit(CodeGenFunction &CGF, Flags flags) override { 1195824c2f53SJohn McCall const FunctionProtoType *FPT 1196824c2f53SJohn McCall = OperatorDelete->getType()->getAs<FunctionProtoType>(); 11979cacbabdSAlp Toker assert(FPT->getNumParams() == NumPlacementArgs + 1 || 11989cacbabdSAlp Toker (FPT->getNumParams() == 2 && NumPlacementArgs == 0)); 1199824c2f53SJohn McCall 1200824c2f53SJohn McCall CallArgList DeleteArgs; 1201824c2f53SJohn McCall 1202824c2f53SJohn McCall // The first argument is always a void*. 12039cacbabdSAlp Toker FunctionProtoType::param_type_iterator AI = FPT->param_type_begin(); 120443dca6a8SEli Friedman DeleteArgs.add(RValue::get(Ptr), *AI++); 1205824c2f53SJohn McCall 1206824c2f53SJohn McCall // A member 'operator delete' can take an extra 'size_t' argument. 12079cacbabdSAlp Toker if (FPT->getNumParams() == NumPlacementArgs + 2) 120843dca6a8SEli Friedman DeleteArgs.add(RValue::get(AllocSize), *AI++); 1209824c2f53SJohn McCall 1210824c2f53SJohn McCall // Pass the rest of the arguments, which must match exactly. 1211824c2f53SJohn McCall for (unsigned I = 0; I != NumPlacementArgs; ++I) 121243dca6a8SEli Friedman DeleteArgs.add(getPlacementArgs()[I], *AI++); 1213824c2f53SJohn McCall 1214824c2f53SJohn McCall // Call 'operator delete'. 12158d0dc31dSRichard Smith EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); 1216824c2f53SJohn McCall } 1217824c2f53SJohn McCall }; 12187f9c92a9SJohn McCall 12197f9c92a9SJohn McCall /// A cleanup to call the given 'operator delete' function upon 12207f9c92a9SJohn McCall /// abnormal exit from a new expression when the new expression is 12217f9c92a9SJohn McCall /// conditional. 12227e70d680SDavid Blaikie class CallDeleteDuringConditionalNew final : public EHScopeStack::Cleanup { 12237f9c92a9SJohn McCall size_t NumPlacementArgs; 12247f9c92a9SJohn McCall const FunctionDecl *OperatorDelete; 1225cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type Ptr; 1226cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type AllocSize; 12277f9c92a9SJohn McCall 1228cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type *getPlacementArgs() { 1229cb5f77f0SJohn McCall return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); 12307f9c92a9SJohn McCall } 12317f9c92a9SJohn McCall 12327f9c92a9SJohn McCall public: 12337f9c92a9SJohn McCall static size_t getExtraSize(size_t NumPlacementArgs) { 1234cb5f77f0SJohn McCall return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); 12357f9c92a9SJohn McCall } 12367f9c92a9SJohn McCall 12377f9c92a9SJohn McCall CallDeleteDuringConditionalNew(size_t NumPlacementArgs, 12387f9c92a9SJohn McCall const FunctionDecl *OperatorDelete, 1239cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type Ptr, 1240cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type AllocSize) 12417f9c92a9SJohn McCall : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), 12427f9c92a9SJohn McCall Ptr(Ptr), AllocSize(AllocSize) {} 12437f9c92a9SJohn McCall 1244cb5f77f0SJohn McCall void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { 12457f9c92a9SJohn McCall assert(I < NumPlacementArgs && "index out of range"); 12467f9c92a9SJohn McCall getPlacementArgs()[I] = Arg; 12477f9c92a9SJohn McCall } 12487f9c92a9SJohn McCall 12494f12f10dSCraig Topper void Emit(CodeGenFunction &CGF, Flags flags) override { 12507f9c92a9SJohn McCall const FunctionProtoType *FPT 12517f9c92a9SJohn McCall = OperatorDelete->getType()->getAs<FunctionProtoType>(); 12529cacbabdSAlp Toker assert(FPT->getNumParams() == NumPlacementArgs + 1 || 12539cacbabdSAlp Toker (FPT->getNumParams() == 2 && NumPlacementArgs == 0)); 12547f9c92a9SJohn McCall 12557f9c92a9SJohn McCall CallArgList DeleteArgs; 12567f9c92a9SJohn McCall 12577f9c92a9SJohn McCall // The first argument is always a void*. 12589cacbabdSAlp Toker FunctionProtoType::param_type_iterator AI = FPT->param_type_begin(); 125943dca6a8SEli Friedman DeleteArgs.add(Ptr.restore(CGF), *AI++); 12607f9c92a9SJohn McCall 12617f9c92a9SJohn McCall // A member 'operator delete' can take an extra 'size_t' argument. 12629cacbabdSAlp Toker if (FPT->getNumParams() == NumPlacementArgs + 2) { 1263cb5f77f0SJohn McCall RValue RV = AllocSize.restore(CGF); 126443dca6a8SEli Friedman DeleteArgs.add(RV, *AI++); 12657f9c92a9SJohn McCall } 12667f9c92a9SJohn McCall 12677f9c92a9SJohn McCall // Pass the rest of the arguments, which must match exactly. 12687f9c92a9SJohn McCall for (unsigned I = 0; I != NumPlacementArgs; ++I) { 1269cb5f77f0SJohn McCall RValue RV = getPlacementArgs()[I].restore(CGF); 127043dca6a8SEli Friedman DeleteArgs.add(RV, *AI++); 12717f9c92a9SJohn McCall } 12727f9c92a9SJohn McCall 12737f9c92a9SJohn McCall // Call 'operator delete'. 12748d0dc31dSRichard Smith EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); 12757f9c92a9SJohn McCall } 12767f9c92a9SJohn McCall }; 1277ab9db510SAlexander Kornienko } 12787f9c92a9SJohn McCall 12797f9c92a9SJohn McCall /// Enter a cleanup to call 'operator delete' if the initializer in a 12807f9c92a9SJohn McCall /// new-expression throws. 12817f9c92a9SJohn McCall static void EnterNewDeleteCleanup(CodeGenFunction &CGF, 12827f9c92a9SJohn McCall const CXXNewExpr *E, 12837f416cc4SJohn McCall Address NewPtr, 12847f9c92a9SJohn McCall llvm::Value *AllocSize, 12857f9c92a9SJohn McCall const CallArgList &NewArgs) { 12867f9c92a9SJohn McCall // If we're not inside a conditional branch, then the cleanup will 12877f9c92a9SJohn McCall // dominate and we can do the easier (and more efficient) thing. 12887f9c92a9SJohn McCall if (!CGF.isInConditionalBranch()) { 12897f9c92a9SJohn McCall CallDeleteDuringNew *Cleanup = CGF.EHStack 12907f9c92a9SJohn McCall .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, 12917f9c92a9SJohn McCall E->getNumPlacementArgs(), 12927f9c92a9SJohn McCall E->getOperatorDelete(), 12937f416cc4SJohn McCall NewPtr.getPointer(), 12947f416cc4SJohn McCall AllocSize); 12957f9c92a9SJohn McCall for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1296f4258eb4SEli Friedman Cleanup->setPlacementArg(I, NewArgs[I+1].RV); 12977f9c92a9SJohn McCall 12987f9c92a9SJohn McCall return; 12997f9c92a9SJohn McCall } 13007f9c92a9SJohn McCall 13017f9c92a9SJohn McCall // Otherwise, we need to save all this stuff. 1302cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type SavedNewPtr = 13037f416cc4SJohn McCall DominatingValue<RValue>::save(CGF, RValue::get(NewPtr.getPointer())); 1304cb5f77f0SJohn McCall DominatingValue<RValue>::saved_type SavedAllocSize = 1305cb5f77f0SJohn McCall DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); 13067f9c92a9SJohn McCall 13077f9c92a9SJohn McCall CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack 1308f4beacd0SJohn McCall .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, 13097f9c92a9SJohn McCall E->getNumPlacementArgs(), 13107f9c92a9SJohn McCall E->getOperatorDelete(), 13117f9c92a9SJohn McCall SavedNewPtr, 13127f9c92a9SJohn McCall SavedAllocSize); 13137f9c92a9SJohn McCall for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) 1314cb5f77f0SJohn McCall Cleanup->setPlacementArg(I, 1315f4258eb4SEli Friedman DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); 13167f9c92a9SJohn McCall 1317f4beacd0SJohn McCall CGF.initFullExprCleanup(); 1318824c2f53SJohn McCall } 1319824c2f53SJohn McCall 132059486a2dSAnders Carlsson llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { 132175f9498aSJohn McCall // The element type being allocated. 132275f9498aSJohn McCall QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); 13238ed55a54SJohn McCall 132475f9498aSJohn McCall // 1. Build a call to the allocation function. 132575f9498aSJohn McCall FunctionDecl *allocator = E->getOperatorNew(); 132659486a2dSAnders Carlsson 1327f862eb6aSSebastian Redl // If there is a brace-initializer, cannot allocate fewer elements than inits. 1328f862eb6aSSebastian Redl unsigned minElements = 0; 1329f862eb6aSSebastian Redl if (E->isArray() && E->hasInitializer()) { 1330f862eb6aSSebastian Redl if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer())) 1331f862eb6aSSebastian Redl minElements = ILE->getNumInits(); 1332f862eb6aSSebastian Redl } 1333f862eb6aSSebastian Redl 13348a13c418SCraig Topper llvm::Value *numElements = nullptr; 13358a13c418SCraig Topper llvm::Value *allocSizeWithoutCookie = nullptr; 133675f9498aSJohn McCall llvm::Value *allocSize = 1337f862eb6aSSebastian Redl EmitCXXNewAllocSize(*this, E, minElements, numElements, 1338f862eb6aSSebastian Redl allocSizeWithoutCookie); 133959486a2dSAnders Carlsson 13407f416cc4SJohn McCall // Emit the allocation call. If the allocator is a global placement 13417f416cc4SJohn McCall // operator, just "inline" it directly. 13427f416cc4SJohn McCall Address allocation = Address::invalid(); 13437f416cc4SJohn McCall CallArgList allocatorArgs; 13447f416cc4SJohn McCall if (allocator->isReservedGlobalPlacementOperator()) { 134553dcf94dSJohn McCall assert(E->getNumPlacementArgs() == 1); 134653dcf94dSJohn McCall const Expr *arg = *E->placement_arguments().begin(); 134753dcf94dSJohn McCall 13487f416cc4SJohn McCall AlignmentSource alignSource; 134953dcf94dSJohn McCall allocation = EmitPointerWithAlignment(arg, &alignSource); 13507f416cc4SJohn McCall 13517f416cc4SJohn McCall // The pointer expression will, in many cases, be an opaque void*. 13527f416cc4SJohn McCall // In these cases, discard the computed alignment and use the 13537f416cc4SJohn McCall // formal alignment of the allocated type. 13547f416cc4SJohn McCall if (alignSource != AlignmentSource::Decl) { 13557f416cc4SJohn McCall allocation = Address(allocation.getPointer(), 13567f416cc4SJohn McCall getContext().getTypeAlignInChars(allocType)); 13577f416cc4SJohn McCall } 13587f416cc4SJohn McCall 135953dcf94dSJohn McCall // Set up allocatorArgs for the call to operator delete if it's not 136053dcf94dSJohn McCall // the reserved global operator. 136153dcf94dSJohn McCall if (E->getOperatorDelete() && 136253dcf94dSJohn McCall !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { 136353dcf94dSJohn McCall allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType()); 136453dcf94dSJohn McCall allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType()); 136553dcf94dSJohn McCall } 136653dcf94dSJohn McCall 13677f416cc4SJohn McCall } else { 13687f416cc4SJohn McCall const FunctionProtoType *allocatorType = 13697f416cc4SJohn McCall allocator->getType()->castAs<FunctionProtoType>(); 13707f416cc4SJohn McCall 13717f416cc4SJohn McCall // The allocation size is the first argument. 13727f416cc4SJohn McCall QualType sizeType = getContext().getSizeType(); 137343dca6a8SEli Friedman allocatorArgs.add(RValue::get(allocSize), sizeType); 137459486a2dSAnders Carlsson 137559486a2dSAnders Carlsson // We start at 1 here because the first argument (the allocation size) 137659486a2dSAnders Carlsson // has already been emitted. 1377f05779e2SDavid Blaikie EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(), 1378f05779e2SDavid Blaikie /* CalleeDecl */ nullptr, 13798e1162c7SAlexey Samsonov /*ParamsToSkip*/ 1); 138059486a2dSAnders Carlsson 13817f416cc4SJohn McCall RValue RV = 13827f416cc4SJohn McCall EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); 13837f416cc4SJohn McCall 13847f416cc4SJohn McCall // For now, only assume that the allocation function returns 13857f416cc4SJohn McCall // something satisfactorily aligned for the element type, plus 13867f416cc4SJohn McCall // the cookie if we have one. 13877f416cc4SJohn McCall CharUnits allocationAlign = 13887f416cc4SJohn McCall getContext().getTypeAlignInChars(allocType); 13897f416cc4SJohn McCall if (allocSize != allocSizeWithoutCookie) { 13907f416cc4SJohn McCall CharUnits cookieAlign = getSizeAlign(); // FIXME? 13917f416cc4SJohn McCall allocationAlign = std::max(allocationAlign, cookieAlign); 13927f416cc4SJohn McCall } 13937f416cc4SJohn McCall 13947f416cc4SJohn McCall allocation = Address(RV.getScalarVal(), allocationAlign); 13957ec4b434SJohn McCall } 139659486a2dSAnders Carlsson 139775f9498aSJohn McCall // Emit a null check on the allocation result if the allocation 139875f9498aSJohn McCall // function is allowed to return null (because it has a non-throwing 1399902a0238SRichard Smith // exception spec or is the reserved placement new) and we have an 140075f9498aSJohn McCall // interesting initializer. 1401902a0238SRichard Smith bool nullCheck = E->shouldNullCheckAllocation(getContext()) && 14026047f07eSSebastian Redl (!allocType.isPODType(getContext()) || E->hasInitializer()); 140359486a2dSAnders Carlsson 14048a13c418SCraig Topper llvm::BasicBlock *nullCheckBB = nullptr; 14058a13c418SCraig Topper llvm::BasicBlock *contBB = nullptr; 140659486a2dSAnders Carlsson 1407f7dcf320SJohn McCall // The null-check means that the initializer is conditionally 1408f7dcf320SJohn McCall // evaluated. 1409f7dcf320SJohn McCall ConditionalEvaluation conditional(*this); 1410f7dcf320SJohn McCall 141175f9498aSJohn McCall if (nullCheck) { 1412f7dcf320SJohn McCall conditional.begin(*this); 141375f9498aSJohn McCall 141475f9498aSJohn McCall nullCheckBB = Builder.GetInsertBlock(); 141575f9498aSJohn McCall llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); 141675f9498aSJohn McCall contBB = createBasicBlock("new.cont"); 141775f9498aSJohn McCall 14187f416cc4SJohn McCall llvm::Value *isNull = 14197f416cc4SJohn McCall Builder.CreateIsNull(allocation.getPointer(), "new.isnull"); 142075f9498aSJohn McCall Builder.CreateCondBr(isNull, contBB, notNullBB); 142175f9498aSJohn McCall EmitBlock(notNullBB); 142259486a2dSAnders Carlsson } 142359486a2dSAnders Carlsson 1424824c2f53SJohn McCall // If there's an operator delete, enter a cleanup to call it if an 1425824c2f53SJohn McCall // exception is thrown. 142675f9498aSJohn McCall EHScopeStack::stable_iterator operatorDeleteCleanup; 14278a13c418SCraig Topper llvm::Instruction *cleanupDominator = nullptr; 14287ec4b434SJohn McCall if (E->getOperatorDelete() && 14297ec4b434SJohn McCall !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { 143075f9498aSJohn McCall EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); 143175f9498aSJohn McCall operatorDeleteCleanup = EHStack.stable_begin(); 1432f4beacd0SJohn McCall cleanupDominator = Builder.CreateUnreachable(); 1433824c2f53SJohn McCall } 1434824c2f53SJohn McCall 1435cf9b1f65SEli Friedman assert((allocSize == allocSizeWithoutCookie) == 1436cf9b1f65SEli Friedman CalculateCookiePadding(*this, E).isZero()); 1437cf9b1f65SEli Friedman if (allocSize != allocSizeWithoutCookie) { 1438cf9b1f65SEli Friedman assert(E->isArray()); 1439cf9b1f65SEli Friedman allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, 1440cf9b1f65SEli Friedman numElements, 1441cf9b1f65SEli Friedman E, allocType); 1442cf9b1f65SEli Friedman } 1443cf9b1f65SEli Friedman 1444fb901c7aSDavid Blaikie llvm::Type *elementTy = ConvertTypeForMem(allocType); 14457f416cc4SJohn McCall Address result = Builder.CreateElementBitCast(allocation, elementTy); 1446824c2f53SJohn McCall 1447338c9d0aSPiotr Padlewski // Passing pointer through invariant.group.barrier to avoid propagation of 1448338c9d0aSPiotr Padlewski // vptrs information which may be included in previous type. 1449338c9d0aSPiotr Padlewski if (CGM.getCodeGenOpts().StrictVTablePointers && 1450338c9d0aSPiotr Padlewski CGM.getCodeGenOpts().OptimizationLevel > 0 && 1451338c9d0aSPiotr Padlewski allocator->isReservedGlobalPlacementOperator()) 1452338c9d0aSPiotr Padlewski result = Address(Builder.CreateInvariantGroupBarrier(result.getPointer()), 1453338c9d0aSPiotr Padlewski result.getAlignment()); 1454338c9d0aSPiotr Padlewski 1455fb901c7aSDavid Blaikie EmitNewInitializer(*this, E, allocType, elementTy, result, numElements, 145699210dc9SJohn McCall allocSizeWithoutCookie); 14578ed55a54SJohn McCall if (E->isArray()) { 14588ed55a54SJohn McCall // NewPtr is a pointer to the base element type. If we're 14598ed55a54SJohn McCall // allocating an array of arrays, we'll need to cast back to the 14608ed55a54SJohn McCall // array pointer type. 14612192fe50SChris Lattner llvm::Type *resultType = ConvertTypeForMem(E->getType()); 14627f416cc4SJohn McCall if (result.getType() != resultType) 146375f9498aSJohn McCall result = Builder.CreateBitCast(result, resultType); 146447b4629bSFariborz Jahanian } 146559486a2dSAnders Carlsson 1466824c2f53SJohn McCall // Deactivate the 'operator delete' cleanup if we finished 1467824c2f53SJohn McCall // initialization. 1468f4beacd0SJohn McCall if (operatorDeleteCleanup.isValid()) { 1469f4beacd0SJohn McCall DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); 1470f4beacd0SJohn McCall cleanupDominator->eraseFromParent(); 1471f4beacd0SJohn McCall } 1472824c2f53SJohn McCall 14737f416cc4SJohn McCall llvm::Value *resultPtr = result.getPointer(); 147475f9498aSJohn McCall if (nullCheck) { 1475f7dcf320SJohn McCall conditional.end(*this); 1476f7dcf320SJohn McCall 147775f9498aSJohn McCall llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); 147875f9498aSJohn McCall EmitBlock(contBB); 147959486a2dSAnders Carlsson 14807f416cc4SJohn McCall llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2); 14817f416cc4SJohn McCall PHI->addIncoming(resultPtr, notNullBB); 14827f416cc4SJohn McCall PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()), 148375f9498aSJohn McCall nullCheckBB); 148459486a2dSAnders Carlsson 14857f416cc4SJohn McCall resultPtr = PHI; 148659486a2dSAnders Carlsson } 148759486a2dSAnders Carlsson 14887f416cc4SJohn McCall return resultPtr; 148959486a2dSAnders Carlsson } 149059486a2dSAnders Carlsson 149159486a2dSAnders Carlsson void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, 149259486a2dSAnders Carlsson llvm::Value *Ptr, 149359486a2dSAnders Carlsson QualType DeleteTy) { 14948ed55a54SJohn McCall assert(DeleteFD->getOverloadedOperator() == OO_Delete); 14958ed55a54SJohn McCall 149659486a2dSAnders Carlsson const FunctionProtoType *DeleteFTy = 149759486a2dSAnders Carlsson DeleteFD->getType()->getAs<FunctionProtoType>(); 149859486a2dSAnders Carlsson 149959486a2dSAnders Carlsson CallArgList DeleteArgs; 150059486a2dSAnders Carlsson 150121122cf6SAnders Carlsson // Check if we need to pass the size to the delete operator. 15028a13c418SCraig Topper llvm::Value *Size = nullptr; 150321122cf6SAnders Carlsson QualType SizeTy; 15049cacbabdSAlp Toker if (DeleteFTy->getNumParams() == 2) { 15059cacbabdSAlp Toker SizeTy = DeleteFTy->getParamType(1); 15067df3cbebSKen Dyck CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); 15077df3cbebSKen Dyck Size = llvm::ConstantInt::get(ConvertType(SizeTy), 15087df3cbebSKen Dyck DeleteTypeSize.getQuantity()); 150921122cf6SAnders Carlsson } 151021122cf6SAnders Carlsson 15119cacbabdSAlp Toker QualType ArgTy = DeleteFTy->getParamType(0); 151259486a2dSAnders Carlsson llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); 151343dca6a8SEli Friedman DeleteArgs.add(RValue::get(DeletePtr), ArgTy); 151459486a2dSAnders Carlsson 151521122cf6SAnders Carlsson if (Size) 151643dca6a8SEli Friedman DeleteArgs.add(RValue::get(Size), SizeTy); 151759486a2dSAnders Carlsson 151859486a2dSAnders Carlsson // Emit the call to delete. 15198d0dc31dSRichard Smith EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); 152059486a2dSAnders Carlsson } 152159486a2dSAnders Carlsson 15228ed55a54SJohn McCall namespace { 15238ed55a54SJohn McCall /// Calls the given 'operator delete' on a single object. 15247e70d680SDavid Blaikie struct CallObjectDelete final : EHScopeStack::Cleanup { 15258ed55a54SJohn McCall llvm::Value *Ptr; 15268ed55a54SJohn McCall const FunctionDecl *OperatorDelete; 15278ed55a54SJohn McCall QualType ElementType; 15288ed55a54SJohn McCall 15298ed55a54SJohn McCall CallObjectDelete(llvm::Value *Ptr, 15308ed55a54SJohn McCall const FunctionDecl *OperatorDelete, 15318ed55a54SJohn McCall QualType ElementType) 15328ed55a54SJohn McCall : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} 15338ed55a54SJohn McCall 15344f12f10dSCraig Topper void Emit(CodeGenFunction &CGF, Flags flags) override { 15358ed55a54SJohn McCall CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); 15368ed55a54SJohn McCall } 15378ed55a54SJohn McCall }; 1538ab9db510SAlexander Kornienko } 15398ed55a54SJohn McCall 15400c0b6d9aSDavid Majnemer void 15410c0b6d9aSDavid Majnemer CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, 15420c0b6d9aSDavid Majnemer llvm::Value *CompletePtr, 15430c0b6d9aSDavid Majnemer QualType ElementType) { 15440c0b6d9aSDavid Majnemer EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr, 15450c0b6d9aSDavid Majnemer OperatorDelete, ElementType); 15460c0b6d9aSDavid Majnemer } 15470c0b6d9aSDavid Majnemer 15488ed55a54SJohn McCall /// Emit the code for deleting a single object. 15498ed55a54SJohn McCall static void EmitObjectDelete(CodeGenFunction &CGF, 15500868137aSDavid Majnemer const CXXDeleteExpr *DE, 15517f416cc4SJohn McCall Address Ptr, 15520868137aSDavid Majnemer QualType ElementType) { 15538ed55a54SJohn McCall // Find the destructor for the type, if applicable. If the 15548ed55a54SJohn McCall // destructor is virtual, we'll just emit the vcall and return. 15558a13c418SCraig Topper const CXXDestructorDecl *Dtor = nullptr; 15568ed55a54SJohn McCall if (const RecordType *RT = ElementType->getAs<RecordType>()) { 15578ed55a54SJohn McCall CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1558b23533dbSEli Friedman if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { 15598ed55a54SJohn McCall Dtor = RD->getDestructor(); 15608ed55a54SJohn McCall 15618ed55a54SJohn McCall if (Dtor->isVirtual()) { 15620868137aSDavid Majnemer CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, 15630868137aSDavid Majnemer Dtor); 15648ed55a54SJohn McCall return; 15658ed55a54SJohn McCall } 15668ed55a54SJohn McCall } 15678ed55a54SJohn McCall } 15688ed55a54SJohn McCall 15698ed55a54SJohn McCall // Make sure that we call delete even if the dtor throws. 1570e4df6c8dSJohn McCall // This doesn't have to a conditional cleanup because we're going 1571e4df6c8dSJohn McCall // to pop it off in a second. 15720868137aSDavid Majnemer const FunctionDecl *OperatorDelete = DE->getOperatorDelete(); 15738ed55a54SJohn McCall CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, 15747f416cc4SJohn McCall Ptr.getPointer(), 15757f416cc4SJohn McCall OperatorDelete, ElementType); 15768ed55a54SJohn McCall 15778ed55a54SJohn McCall if (Dtor) 15788ed55a54SJohn McCall CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, 157961535005SDouglas Gregor /*ForVirtualBase=*/false, 158061535005SDouglas Gregor /*Delegating=*/false, 158161535005SDouglas Gregor Ptr); 1582460ce58fSJohn McCall else if (auto Lifetime = ElementType.getObjCLifetime()) { 1583460ce58fSJohn McCall switch (Lifetime) { 158431168b07SJohn McCall case Qualifiers::OCL_None: 158531168b07SJohn McCall case Qualifiers::OCL_ExplicitNone: 158631168b07SJohn McCall case Qualifiers::OCL_Autoreleasing: 158731168b07SJohn McCall break; 158831168b07SJohn McCall 15897f416cc4SJohn McCall case Qualifiers::OCL_Strong: 15907f416cc4SJohn McCall CGF.EmitARCDestroyStrong(Ptr, ARCPreciseLifetime); 159131168b07SJohn McCall break; 159231168b07SJohn McCall 159331168b07SJohn McCall case Qualifiers::OCL_Weak: 159431168b07SJohn McCall CGF.EmitARCDestroyWeak(Ptr); 159531168b07SJohn McCall break; 159631168b07SJohn McCall } 159731168b07SJohn McCall } 15988ed55a54SJohn McCall 15998ed55a54SJohn McCall CGF.PopCleanupBlock(); 16008ed55a54SJohn McCall } 16018ed55a54SJohn McCall 16028ed55a54SJohn McCall namespace { 16038ed55a54SJohn McCall /// Calls the given 'operator delete' on an array of objects. 16047e70d680SDavid Blaikie struct CallArrayDelete final : EHScopeStack::Cleanup { 16058ed55a54SJohn McCall llvm::Value *Ptr; 16068ed55a54SJohn McCall const FunctionDecl *OperatorDelete; 16078ed55a54SJohn McCall llvm::Value *NumElements; 16088ed55a54SJohn McCall QualType ElementType; 16098ed55a54SJohn McCall CharUnits CookieSize; 16108ed55a54SJohn McCall 16118ed55a54SJohn McCall CallArrayDelete(llvm::Value *Ptr, 16128ed55a54SJohn McCall const FunctionDecl *OperatorDelete, 16138ed55a54SJohn McCall llvm::Value *NumElements, 16148ed55a54SJohn McCall QualType ElementType, 16158ed55a54SJohn McCall CharUnits CookieSize) 16168ed55a54SJohn McCall : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), 16178ed55a54SJohn McCall ElementType(ElementType), CookieSize(CookieSize) {} 16188ed55a54SJohn McCall 16194f12f10dSCraig Topper void Emit(CodeGenFunction &CGF, Flags flags) override { 16208ed55a54SJohn McCall const FunctionProtoType *DeleteFTy = 16218ed55a54SJohn McCall OperatorDelete->getType()->getAs<FunctionProtoType>(); 16229cacbabdSAlp Toker assert(DeleteFTy->getNumParams() == 1 || DeleteFTy->getNumParams() == 2); 16238ed55a54SJohn McCall 16248ed55a54SJohn McCall CallArgList Args; 16258ed55a54SJohn McCall 16268ed55a54SJohn McCall // Pass the pointer as the first argument. 16279cacbabdSAlp Toker QualType VoidPtrTy = DeleteFTy->getParamType(0); 16288ed55a54SJohn McCall llvm::Value *DeletePtr 16298ed55a54SJohn McCall = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); 163043dca6a8SEli Friedman Args.add(RValue::get(DeletePtr), VoidPtrTy); 16318ed55a54SJohn McCall 16328ed55a54SJohn McCall // Pass the original requested size as the second argument. 16339cacbabdSAlp Toker if (DeleteFTy->getNumParams() == 2) { 16349cacbabdSAlp Toker QualType size_t = DeleteFTy->getParamType(1); 16352192fe50SChris Lattner llvm::IntegerType *SizeTy 16368ed55a54SJohn McCall = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); 16378ed55a54SJohn McCall 16388ed55a54SJohn McCall CharUnits ElementTypeSize = 16398ed55a54SJohn McCall CGF.CGM.getContext().getTypeSizeInChars(ElementType); 16408ed55a54SJohn McCall 16418ed55a54SJohn McCall // The size of an element, multiplied by the number of elements. 16428ed55a54SJohn McCall llvm::Value *Size 16438ed55a54SJohn McCall = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); 1644149e6031SDavid Majnemer if (NumElements) 16458ed55a54SJohn McCall Size = CGF.Builder.CreateMul(Size, NumElements); 16468ed55a54SJohn McCall 16478ed55a54SJohn McCall // Plus the size of the cookie if applicable. 16488ed55a54SJohn McCall if (!CookieSize.isZero()) { 16498ed55a54SJohn McCall llvm::Value *CookieSizeV 16508ed55a54SJohn McCall = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); 16518ed55a54SJohn McCall Size = CGF.Builder.CreateAdd(Size, CookieSizeV); 16528ed55a54SJohn McCall } 16538ed55a54SJohn McCall 165443dca6a8SEli Friedman Args.add(RValue::get(Size), size_t); 16558ed55a54SJohn McCall } 16568ed55a54SJohn McCall 16578ed55a54SJohn McCall // Emit the call to delete. 16588d0dc31dSRichard Smith EmitNewDeleteCall(CGF, OperatorDelete, DeleteFTy, Args); 16598ed55a54SJohn McCall } 16608ed55a54SJohn McCall }; 1661ab9db510SAlexander Kornienko } 16628ed55a54SJohn McCall 16638ed55a54SJohn McCall /// Emit the code for deleting an array of objects. 16648ed55a54SJohn McCall static void EmitArrayDelete(CodeGenFunction &CGF, 1665284c48ffSJohn McCall const CXXDeleteExpr *E, 16667f416cc4SJohn McCall Address deletedPtr, 1667ca2c56f2SJohn McCall QualType elementType) { 16688a13c418SCraig Topper llvm::Value *numElements = nullptr; 16698a13c418SCraig Topper llvm::Value *allocatedPtr = nullptr; 1670ca2c56f2SJohn McCall CharUnits cookieSize; 1671ca2c56f2SJohn McCall CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, 1672ca2c56f2SJohn McCall numElements, allocatedPtr, cookieSize); 16738ed55a54SJohn McCall 1674ca2c56f2SJohn McCall assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); 16758ed55a54SJohn McCall 16768ed55a54SJohn McCall // Make sure that we call delete even if one of the dtors throws. 1677ca2c56f2SJohn McCall const FunctionDecl *operatorDelete = E->getOperatorDelete(); 16788ed55a54SJohn McCall CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, 1679ca2c56f2SJohn McCall allocatedPtr, operatorDelete, 1680ca2c56f2SJohn McCall numElements, elementType, 1681ca2c56f2SJohn McCall cookieSize); 16828ed55a54SJohn McCall 1683ca2c56f2SJohn McCall // Destroy the elements. 1684ca2c56f2SJohn McCall if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { 1685ca2c56f2SJohn McCall assert(numElements && "no element count for a type with a destructor!"); 168631168b07SJohn McCall 16877f416cc4SJohn McCall CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); 16887f416cc4SJohn McCall CharUnits elementAlign = 16897f416cc4SJohn McCall deletedPtr.getAlignment().alignmentOfArrayElement(elementSize); 16907f416cc4SJohn McCall 16917f416cc4SJohn McCall llvm::Value *arrayBegin = deletedPtr.getPointer(); 1692ca2c56f2SJohn McCall llvm::Value *arrayEnd = 16937f416cc4SJohn McCall CGF.Builder.CreateInBoundsGEP(arrayBegin, numElements, "delete.end"); 169497eab0a2SJohn McCall 169597eab0a2SJohn McCall // Note that it is legal to allocate a zero-length array, and we 169697eab0a2SJohn McCall // can never fold the check away because the length should always 169797eab0a2SJohn McCall // come from a cookie. 16987f416cc4SJohn McCall CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign, 1699ca2c56f2SJohn McCall CGF.getDestroyer(dtorKind), 170097eab0a2SJohn McCall /*checkZeroLength*/ true, 1701ca2c56f2SJohn McCall CGF.needsEHCleanup(dtorKind)); 17028ed55a54SJohn McCall } 17038ed55a54SJohn McCall 1704ca2c56f2SJohn McCall // Pop the cleanup block. 17058ed55a54SJohn McCall CGF.PopCleanupBlock(); 17068ed55a54SJohn McCall } 17078ed55a54SJohn McCall 170859486a2dSAnders Carlsson void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { 170959486a2dSAnders Carlsson const Expr *Arg = E->getArgument(); 17107f416cc4SJohn McCall Address Ptr = EmitPointerWithAlignment(Arg); 171159486a2dSAnders Carlsson 171259486a2dSAnders Carlsson // Null check the pointer. 171359486a2dSAnders Carlsson llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); 171459486a2dSAnders Carlsson llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); 171559486a2dSAnders Carlsson 17167f416cc4SJohn McCall llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull"); 171759486a2dSAnders Carlsson 171859486a2dSAnders Carlsson Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); 171959486a2dSAnders Carlsson EmitBlock(DeleteNotNull); 172059486a2dSAnders Carlsson 17218ed55a54SJohn McCall // We might be deleting a pointer to array. If so, GEP down to the 17228ed55a54SJohn McCall // first non-array element. 17238ed55a54SJohn McCall // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) 17248ed55a54SJohn McCall QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); 17258ed55a54SJohn McCall if (DeleteTy->isConstantArrayType()) { 17268ed55a54SJohn McCall llvm::Value *Zero = Builder.getInt32(0); 17270e62c1ccSChris Lattner SmallVector<llvm::Value*,8> GEP; 172859486a2dSAnders Carlsson 17298ed55a54SJohn McCall GEP.push_back(Zero); // point at the outermost array 17308ed55a54SJohn McCall 17318ed55a54SJohn McCall // For each layer of array type we're pointing at: 17328ed55a54SJohn McCall while (const ConstantArrayType *Arr 17338ed55a54SJohn McCall = getContext().getAsConstantArrayType(DeleteTy)) { 17348ed55a54SJohn McCall // 1. Unpeel the array type. 17358ed55a54SJohn McCall DeleteTy = Arr->getElementType(); 17368ed55a54SJohn McCall 17378ed55a54SJohn McCall // 2. GEP to the first element of the array. 17388ed55a54SJohn McCall GEP.push_back(Zero); 17398ed55a54SJohn McCall } 17408ed55a54SJohn McCall 17417f416cc4SJohn McCall Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getPointer(), GEP, "del.first"), 17427f416cc4SJohn McCall Ptr.getAlignment()); 17438ed55a54SJohn McCall } 17448ed55a54SJohn McCall 17457f416cc4SJohn McCall assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType()); 17468ed55a54SJohn McCall 17477270ef57SReid Kleckner if (E->isArrayForm()) { 17487270ef57SReid Kleckner EmitArrayDelete(*this, E, Ptr, DeleteTy); 17497270ef57SReid Kleckner } else { 17507270ef57SReid Kleckner EmitObjectDelete(*this, E, Ptr, DeleteTy); 17517270ef57SReid Kleckner } 175259486a2dSAnders Carlsson 175359486a2dSAnders Carlsson EmitBlock(DeleteEnd); 175459486a2dSAnders Carlsson } 175559486a2dSAnders Carlsson 17561c3d95ebSDavid Majnemer static bool isGLValueFromPointerDeref(const Expr *E) { 17571c3d95ebSDavid Majnemer E = E->IgnoreParens(); 17581c3d95ebSDavid Majnemer 17591c3d95ebSDavid Majnemer if (const auto *CE = dyn_cast<CastExpr>(E)) { 17601c3d95ebSDavid Majnemer if (!CE->getSubExpr()->isGLValue()) 17611c3d95ebSDavid Majnemer return false; 17621c3d95ebSDavid Majnemer return isGLValueFromPointerDeref(CE->getSubExpr()); 17631c3d95ebSDavid Majnemer } 17641c3d95ebSDavid Majnemer 17651c3d95ebSDavid Majnemer if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 17661c3d95ebSDavid Majnemer return isGLValueFromPointerDeref(OVE->getSourceExpr()); 17671c3d95ebSDavid Majnemer 17681c3d95ebSDavid Majnemer if (const auto *BO = dyn_cast<BinaryOperator>(E)) 17691c3d95ebSDavid Majnemer if (BO->getOpcode() == BO_Comma) 17701c3d95ebSDavid Majnemer return isGLValueFromPointerDeref(BO->getRHS()); 17711c3d95ebSDavid Majnemer 17721c3d95ebSDavid Majnemer if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E)) 17731c3d95ebSDavid Majnemer return isGLValueFromPointerDeref(ACO->getTrueExpr()) || 17741c3d95ebSDavid Majnemer isGLValueFromPointerDeref(ACO->getFalseExpr()); 17751c3d95ebSDavid Majnemer 17761c3d95ebSDavid Majnemer // C++11 [expr.sub]p1: 17771c3d95ebSDavid Majnemer // The expression E1[E2] is identical (by definition) to *((E1)+(E2)) 17781c3d95ebSDavid Majnemer if (isa<ArraySubscriptExpr>(E)) 17791c3d95ebSDavid Majnemer return true; 17801c3d95ebSDavid Majnemer 17811c3d95ebSDavid Majnemer if (const auto *UO = dyn_cast<UnaryOperator>(E)) 17821c3d95ebSDavid Majnemer if (UO->getOpcode() == UO_Deref) 17831c3d95ebSDavid Majnemer return true; 17841c3d95ebSDavid Majnemer 17851c3d95ebSDavid Majnemer return false; 17861c3d95ebSDavid Majnemer } 17871c3d95ebSDavid Majnemer 1788747e301eSWarren Hunt static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, 17892192fe50SChris Lattner llvm::Type *StdTypeInfoPtrTy) { 1790940f02d2SAnders Carlsson // Get the vtable pointer. 17917f416cc4SJohn McCall Address ThisPtr = CGF.EmitLValue(E).getAddress(); 1792940f02d2SAnders Carlsson 1793940f02d2SAnders Carlsson // C++ [expr.typeid]p2: 1794940f02d2SAnders Carlsson // If the glvalue expression is obtained by applying the unary * operator to 1795940f02d2SAnders Carlsson // a pointer and the pointer is a null pointer value, the typeid expression 1796940f02d2SAnders Carlsson // throws the std::bad_typeid exception. 17971c3d95ebSDavid Majnemer // 17981c3d95ebSDavid Majnemer // However, this paragraph's intent is not clear. We choose a very generous 17991c3d95ebSDavid Majnemer // interpretation which implores us to consider comma operators, conditional 18001c3d95ebSDavid Majnemer // operators, parentheses and other such constructs. 18011162d25cSDavid Majnemer QualType SrcRecordTy = E->getType(); 18021c3d95ebSDavid Majnemer if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked( 18031c3d95ebSDavid Majnemer isGLValueFromPointerDeref(E), SrcRecordTy)) { 1804940f02d2SAnders Carlsson llvm::BasicBlock *BadTypeidBlock = 1805940f02d2SAnders Carlsson CGF.createBasicBlock("typeid.bad_typeid"); 18061162d25cSDavid Majnemer llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end"); 1807940f02d2SAnders Carlsson 18087f416cc4SJohn McCall llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer()); 1809940f02d2SAnders Carlsson CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); 1810940f02d2SAnders Carlsson 1811940f02d2SAnders Carlsson CGF.EmitBlock(BadTypeidBlock); 18121162d25cSDavid Majnemer CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF); 1813940f02d2SAnders Carlsson CGF.EmitBlock(EndBlock); 1814940f02d2SAnders Carlsson } 1815940f02d2SAnders Carlsson 18161162d25cSDavid Majnemer return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr, 18171162d25cSDavid Majnemer StdTypeInfoPtrTy); 1818940f02d2SAnders Carlsson } 1819940f02d2SAnders Carlsson 182059486a2dSAnders Carlsson llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { 18212192fe50SChris Lattner llvm::Type *StdTypeInfoPtrTy = 1822940f02d2SAnders Carlsson ConvertType(E->getType())->getPointerTo(); 1823fd7dfeb7SAnders Carlsson 18243f4336cbSAnders Carlsson if (E->isTypeOperand()) { 18253f4336cbSAnders Carlsson llvm::Constant *TypeInfo = 1826143c55eaSDavid Majnemer CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); 1827940f02d2SAnders Carlsson return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); 18283f4336cbSAnders Carlsson } 1829fd7dfeb7SAnders Carlsson 1830940f02d2SAnders Carlsson // C++ [expr.typeid]p2: 1831940f02d2SAnders Carlsson // When typeid is applied to a glvalue expression whose type is a 1832940f02d2SAnders Carlsson // polymorphic class type, the result refers to a std::type_info object 1833940f02d2SAnders Carlsson // representing the type of the most derived object (that is, the dynamic 1834940f02d2SAnders Carlsson // type) to which the glvalue refers. 1835ef8bf436SRichard Smith if (E->isPotentiallyEvaluated()) 1836940f02d2SAnders Carlsson return EmitTypeidFromVTable(*this, E->getExprOperand(), 1837940f02d2SAnders Carlsson StdTypeInfoPtrTy); 1838940f02d2SAnders Carlsson 1839940f02d2SAnders Carlsson QualType OperandTy = E->getExprOperand()->getType(); 1840940f02d2SAnders Carlsson return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), 1841940f02d2SAnders Carlsson StdTypeInfoPtrTy); 184259486a2dSAnders Carlsson } 184359486a2dSAnders Carlsson 1844c1c9971cSAnders Carlsson static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, 1845c1c9971cSAnders Carlsson QualType DestTy) { 18462192fe50SChris Lattner llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1847c1c9971cSAnders Carlsson if (DestTy->isPointerType()) 1848c1c9971cSAnders Carlsson return llvm::Constant::getNullValue(DestLTy); 1849c1c9971cSAnders Carlsson 1850c1c9971cSAnders Carlsson /// C++ [expr.dynamic.cast]p9: 1851c1c9971cSAnders Carlsson /// A failed cast to reference type throws std::bad_cast 18521162d25cSDavid Majnemer if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF)) 18531162d25cSDavid Majnemer return nullptr; 1854c1c9971cSAnders Carlsson 1855c1c9971cSAnders Carlsson CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); 1856c1c9971cSAnders Carlsson return llvm::UndefValue::get(DestLTy); 1857c1c9971cSAnders Carlsson } 1858c1c9971cSAnders Carlsson 18597f416cc4SJohn McCall llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr, 186059486a2dSAnders Carlsson const CXXDynamicCastExpr *DCE) { 18612bf9b4c0SAlexey Bataev CGM.EmitExplicitCastExprType(DCE, this); 18623f4336cbSAnders Carlsson QualType DestTy = DCE->getTypeAsWritten(); 18633f4336cbSAnders Carlsson 1864c1c9971cSAnders Carlsson if (DCE->isAlwaysNull()) 18651162d25cSDavid Majnemer if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy)) 18661162d25cSDavid Majnemer return T; 1867c1c9971cSAnders Carlsson 1868c1c9971cSAnders Carlsson QualType SrcTy = DCE->getSubExpr()->getType(); 1869c1c9971cSAnders Carlsson 18701162d25cSDavid Majnemer // C++ [expr.dynamic.cast]p7: 18711162d25cSDavid Majnemer // If T is "pointer to cv void," then the result is a pointer to the most 18721162d25cSDavid Majnemer // derived object pointed to by v. 18731162d25cSDavid Majnemer const PointerType *DestPTy = DestTy->getAs<PointerType>(); 18741162d25cSDavid Majnemer 18751162d25cSDavid Majnemer bool isDynamicCastToVoid; 18761162d25cSDavid Majnemer QualType SrcRecordTy; 18771162d25cSDavid Majnemer QualType DestRecordTy; 18781162d25cSDavid Majnemer if (DestPTy) { 18791162d25cSDavid Majnemer isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType(); 18801162d25cSDavid Majnemer SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); 18811162d25cSDavid Majnemer DestRecordTy = DestPTy->getPointeeType(); 18821162d25cSDavid Majnemer } else { 18831162d25cSDavid Majnemer isDynamicCastToVoid = false; 18841162d25cSDavid Majnemer SrcRecordTy = SrcTy; 18851162d25cSDavid Majnemer DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); 18861162d25cSDavid Majnemer } 18871162d25cSDavid Majnemer 18881162d25cSDavid Majnemer assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); 18891162d25cSDavid Majnemer 1890882d790fSAnders Carlsson // C++ [expr.dynamic.cast]p4: 1891882d790fSAnders Carlsson // If the value of v is a null pointer value in the pointer case, the result 1892882d790fSAnders Carlsson // is the null pointer value of type T. 18931162d25cSDavid Majnemer bool ShouldNullCheckSrcValue = 18941162d25cSDavid Majnemer CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(), 18951162d25cSDavid Majnemer SrcRecordTy); 189659486a2dSAnders Carlsson 18978a13c418SCraig Topper llvm::BasicBlock *CastNull = nullptr; 18988a13c418SCraig Topper llvm::BasicBlock *CastNotNull = nullptr; 1899882d790fSAnders Carlsson llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); 1900fa8b4955SDouglas Gregor 1901882d790fSAnders Carlsson if (ShouldNullCheckSrcValue) { 1902882d790fSAnders Carlsson CastNull = createBasicBlock("dynamic_cast.null"); 1903882d790fSAnders Carlsson CastNotNull = createBasicBlock("dynamic_cast.notnull"); 1904882d790fSAnders Carlsson 19057f416cc4SJohn McCall llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer()); 1906882d790fSAnders Carlsson Builder.CreateCondBr(IsNull, CastNull, CastNotNull); 1907882d790fSAnders Carlsson EmitBlock(CastNotNull); 190859486a2dSAnders Carlsson } 190959486a2dSAnders Carlsson 19107f416cc4SJohn McCall llvm::Value *Value; 19111162d25cSDavid Majnemer if (isDynamicCastToVoid) { 19127f416cc4SJohn McCall Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy, 19131162d25cSDavid Majnemer DestTy); 19141162d25cSDavid Majnemer } else { 19151162d25cSDavid Majnemer assert(DestRecordTy->isRecordType() && 19161162d25cSDavid Majnemer "destination type must be a record type!"); 19177f416cc4SJohn McCall Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy, 19181162d25cSDavid Majnemer DestTy, DestRecordTy, CastEnd); 191967528eaaSDavid Majnemer CastNotNull = Builder.GetInsertBlock(); 19201162d25cSDavid Majnemer } 19213f4336cbSAnders Carlsson 1922882d790fSAnders Carlsson if (ShouldNullCheckSrcValue) { 1923882d790fSAnders Carlsson EmitBranch(CastEnd); 192459486a2dSAnders Carlsson 1925882d790fSAnders Carlsson EmitBlock(CastNull); 1926882d790fSAnders Carlsson EmitBranch(CastEnd); 192759486a2dSAnders Carlsson } 192859486a2dSAnders Carlsson 1929882d790fSAnders Carlsson EmitBlock(CastEnd); 193059486a2dSAnders Carlsson 1931882d790fSAnders Carlsson if (ShouldNullCheckSrcValue) { 1932882d790fSAnders Carlsson llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); 1933882d790fSAnders Carlsson PHI->addIncoming(Value, CastNotNull); 1934882d790fSAnders Carlsson PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); 193559486a2dSAnders Carlsson 1936882d790fSAnders Carlsson Value = PHI; 193759486a2dSAnders Carlsson } 193859486a2dSAnders Carlsson 1939882d790fSAnders Carlsson return Value; 194059486a2dSAnders Carlsson } 1941c370a7eeSEli Friedman 1942c370a7eeSEli Friedman void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) { 19438631f3e8SEli Friedman RunCleanupsScope Scope(*this); 19447f416cc4SJohn McCall LValue SlotLV = MakeAddrLValue(Slot.getAddress(), E->getType()); 19458631f3e8SEli Friedman 1946c370a7eeSEli Friedman CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin(); 194753c7616eSJames Y Knight for (LambdaExpr::const_capture_init_iterator i = E->capture_init_begin(), 1948c370a7eeSEli Friedman e = E->capture_init_end(); 1949c370a7eeSEli Friedman i != e; ++i, ++CurField) { 1950c370a7eeSEli Friedman // Emit initialization 195140ed2973SDavid Blaikie LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); 195239c81e28SAlexey Bataev if (CurField->hasCapturedVLAType()) { 195339c81e28SAlexey Bataev auto VAT = CurField->getCapturedVLAType(); 195439c81e28SAlexey Bataev EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); 195539c81e28SAlexey Bataev } else { 19565f1a04ffSEli Friedman ArrayRef<VarDecl *> ArrayIndexes; 19575f1a04ffSEli Friedman if (CurField->getType()->isArrayType()) 19585f1a04ffSEli Friedman ArrayIndexes = E->getCaptureInitIndexVars(i); 195940ed2973SDavid Blaikie EmitInitializerForField(*CurField, LV, *i, ArrayIndexes); 1960c370a7eeSEli Friedman } 1961c370a7eeSEli Friedman } 196239c81e28SAlexey Bataev } 1963