Anders Carlsson | 59486a2 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code dealing with code generation of C++ expressions |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 13 | #include "CGCUDARuntime.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
Devang Patel | 91bbb55 | 2010-09-30 19:05:55 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 18 | #include "ConstantEmitter.h" |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 19 | #include "TargetInfo.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 20 | #include "clang/Basic/CodeGenOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Intrinsics.h" |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | struct MemberCallInfo { |
| 29 | RequiredArgs ReqArgs; |
| 30 | // Number of prefix arguments for the call. Ignores the `this` pointer. |
| 31 | unsigned PrefixSize; |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | static MemberCallInfo |
Alexey Samsonov | efa956c | 2016-03-10 00:20:33 +0000 | [diff] [blame] | 36 | commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, |
| 37 | llvm::Value *This, llvm::Value *ImplicitParam, |
| 38 | QualType ImplicitParamTy, const CallExpr *CE, |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 39 | CallArgList &Args, CallArgList *RtlArgs) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 40 | assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) || |
| 41 | isa<CXXOperatorCallExpr>(CE)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 42 | assert(MD->isInstance() && |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 43 | "Trying to emit a member or operator call expr on a static method!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 44 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 45 | // Push the this ptr. |
Reid Kleckner | 034e727 | 2016-09-07 15:15:51 +0000 | [diff] [blame] | 46 | const CXXRecordDecl *RD = |
| 47 | CGF.CGM.getCXXABI().getThisArgumentTypeForMethod(MD); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 48 | Args.add(RValue::get(This), CGF.getTypes().DeriveThisType(RD, MD)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 49 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 50 | // If there is an implicit parameter (e.g. VTT), emit it. |
| 51 | if (ImplicitParam) { |
| 52 | Args.add(RValue::get(ImplicitParam), ImplicitParamTy); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 53 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 54 | |
| 55 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 56 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size()); |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 57 | unsigned PrefixSize = Args.size() - 1; |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 58 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 59 | // And the rest of the call args. |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 60 | if (RtlArgs) { |
| 61 | // Special case: if the caller emitted the arguments right-to-left already |
| 62 | // (prior to emitting the *this argument), we're done. This happens for |
| 63 | // assignment operators. |
| 64 | Args.addFrom(*RtlArgs); |
| 65 | } else if (CE) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 66 | // Special case: skip first argument of CXXOperatorCall (it is "this"). |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 67 | unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0; |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 68 | CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip), |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 69 | CE->getDirectCallee()); |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 70 | } else { |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 71 | assert( |
| 72 | FPT->getNumParams() == 0 && |
| 73 | "No CallExpr specified for function with non-zero number of arguments"); |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 74 | } |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 75 | return {required, PrefixSize}; |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 76 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 77 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 78 | RValue CodeGenFunction::EmitCXXMemberOrOperatorCall( |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 79 | const CXXMethodDecl *MD, const CGCallee &Callee, |
| 80 | ReturnValueSlot ReturnValue, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 81 | llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 82 | const CallExpr *CE, CallArgList *RtlArgs) { |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 83 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 84 | CallArgList Args; |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 85 | MemberCallInfo CallInfo = commonEmitCXXMemberOrOperatorCall( |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 86 | *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args, RtlArgs); |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 87 | auto &FnInfo = CGM.getTypes().arrangeCXXMethodCall( |
| 88 | Args, FPT, CallInfo.ReqArgs, CallInfo.PrefixSize); |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 89 | return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr, |
| 90 | CE ? CE->getExprLoc() : SourceLocation()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Alexey Samsonov | ae81bbb | 2016-03-10 00:20:37 +0000 | [diff] [blame] | 93 | RValue CodeGenFunction::EmitCXXDestructorCall( |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 94 | GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This, QualType ThisTy, |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 95 | llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE) { |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 96 | const CXXMethodDecl *DtorDecl = cast<CXXMethodDecl>(Dtor.getDecl()); |
| 97 | |
| 98 | assert(!ThisTy.isNull()); |
| 99 | assert(ThisTy->getAsCXXRecordDecl() == DtorDecl->getParent() && |
| 100 | "Pointer/Object mixup"); |
| 101 | |
| 102 | LangAS SrcAS = ThisTy.getAddressSpace(); |
| 103 | LangAS DstAS = DtorDecl->getMethodQualifiers().getAddressSpace(); |
| 104 | if (SrcAS != DstAS) { |
| 105 | QualType DstTy = DtorDecl->getThisType(); |
| 106 | llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy); |
| 107 | This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, DstAS, |
| 108 | NewType); |
| 109 | } |
| 110 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 111 | CallArgList Args; |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 112 | commonEmitCXXMemberOrOperatorCall(*this, DtorDecl, This, ImplicitParam, |
| 113 | ImplicitParamTy, CE, Args, nullptr); |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 114 | return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee, |
| 115 | ReturnValueSlot(), Args); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | RValue CodeGenFunction::EmitCXXPseudoDestructorExpr( |
| 119 | const CXXPseudoDestructorExpr *E) { |
| 120 | QualType DestroyedType = E->getDestroyedType(); |
| 121 | if (DestroyedType.hasStrongOrWeakObjCLifetime()) { |
| 122 | // Automatic Reference Counting: |
| 123 | // If the pseudo-expression names a retainable object with weak or |
| 124 | // strong lifetime, the object shall be released. |
| 125 | Expr *BaseExpr = E->getBase(); |
| 126 | Address BaseValue = Address::invalid(); |
| 127 | Qualifiers BaseQuals; |
| 128 | |
| 129 | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
| 130 | if (E->isArrow()) { |
| 131 | BaseValue = EmitPointerWithAlignment(BaseExpr); |
| 132 | const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>(); |
| 133 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
| 134 | } else { |
| 135 | LValue BaseLV = EmitLValue(BaseExpr); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 136 | BaseValue = BaseLV.getAddress(*this); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 137 | QualType BaseTy = BaseExpr->getType(); |
| 138 | BaseQuals = BaseTy.getQualifiers(); |
| 139 | } |
| 140 | |
| 141 | switch (DestroyedType.getObjCLifetime()) { |
| 142 | case Qualifiers::OCL_None: |
| 143 | case Qualifiers::OCL_ExplicitNone: |
| 144 | case Qualifiers::OCL_Autoreleasing: |
| 145 | break; |
| 146 | |
| 147 | case Qualifiers::OCL_Strong: |
| 148 | EmitARCRelease(Builder.CreateLoad(BaseValue, |
| 149 | DestroyedType.isVolatileQualified()), |
| 150 | ARCPreciseLifetime); |
| 151 | break; |
| 152 | |
| 153 | case Qualifiers::OCL_Weak: |
| 154 | EmitARCDestroyWeak(BaseValue); |
| 155 | break; |
| 156 | } |
| 157 | } else { |
| 158 | // C++ [expr.pseudo]p1: |
| 159 | // The result shall only be used as the operand for the function call |
| 160 | // operator (), and the result of such a call has type void. The only |
| 161 | // effect is the evaluation of the postfix-expression before the dot or |
| 162 | // arrow. |
| 163 | EmitIgnoredExpr(E->getBase()); |
| 164 | } |
| 165 | |
| 166 | return RValue::get(nullptr); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 169 | static CXXRecordDecl *getCXXRecord(const Expr *E) { |
| 170 | QualType T = E->getType(); |
| 171 | if (const PointerType *PTy = T->getAs<PointerType>()) |
| 172 | T = PTy->getPointeeType(); |
| 173 | const RecordType *Ty = T->castAs<RecordType>(); |
| 174 | return cast<CXXRecordDecl>(Ty->getDecl()); |
| 175 | } |
| 176 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 177 | // Note: This function also emit constructor calls to support a MSVC |
| 178 | // extensions allowing explicit constructor function call. |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 179 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
| 180 | ReturnValueSlot ReturnValue) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 181 | const Expr *callee = CE->getCallee()->IgnoreParens(); |
| 182 | |
| 183 | if (isa<BinaryOperator>(callee)) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 184 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 185 | |
| 186 | const MemberExpr *ME = cast<MemberExpr>(callee); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 187 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 188 | |
| 189 | if (MD->isStatic()) { |
| 190 | // The method is static, emit it as we would a regular call. |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 191 | CGCallee callee = |
| 192 | CGCallee::forDirect(CGM.GetAddrOfFunction(MD), GlobalDecl(MD)); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 193 | return EmitCall(getContext().getPointerType(MD->getType()), callee, CE, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 194 | ReturnValue); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 195 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 196 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 197 | bool HasQualifier = ME->hasQualifier(); |
| 198 | NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr; |
| 199 | bool IsArrow = ME->isArrow(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 200 | const Expr *Base = ME->getBase(); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 201 | |
| 202 | return EmitCXXMemberOrOperatorMemberCallExpr( |
| 203 | CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base); |
| 204 | } |
| 205 | |
| 206 | RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr( |
| 207 | const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, |
| 208 | bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, |
| 209 | const Expr *Base) { |
| 210 | assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE)); |
| 211 | |
| 212 | // Compute the object pointer. |
| 213 | bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier; |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 214 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 215 | const CXXMethodDecl *DevirtualizedMethod = nullptr; |
Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 216 | if (CanUseVirtualCall && |
| 217 | MD->getDevirtualizedMethod(Base, getLangOpts().AppleKext)) { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 218 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
| 219 | DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); |
| 220 | assert(DevirtualizedMethod); |
| 221 | const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); |
| 222 | const Expr *Inner = Base->ignoreParenBaseCasts(); |
Alexey Bataev | 5bd6879 | 2014-09-29 10:32:21 +0000 | [diff] [blame] | 223 | if (DevirtualizedMethod->getReturnType().getCanonicalType() != |
| 224 | MD->getReturnType().getCanonicalType()) |
| 225 | // If the return types are not the same, this might be a case where more |
| 226 | // code needs to run to compensate for it. For example, the derived |
| 227 | // method might return a type that inherits form from the return |
| 228 | // type of MD and has a prefix. |
| 229 | // For now we just avoid devirtualizing these covariant cases. |
| 230 | DevirtualizedMethod = nullptr; |
| 231 | else if (getCXXRecord(Inner) == DevirtualizedClass) |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 232 | // If the class of the Inner expression is where the dynamic method |
| 233 | // is defined, build the this pointer from it. |
| 234 | Base = Inner; |
| 235 | else if (getCXXRecord(Base) != DevirtualizedClass) { |
| 236 | // If the method is defined in a class that is not the best dynamic |
| 237 | // one or the one of the full expression, we would have to build |
| 238 | // a derived-to-base cast to compute the correct this pointer, but |
| 239 | // we don't have support for that yet, so do a virtual call. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 240 | DevirtualizedMethod = nullptr; |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 243 | |
Richard Smith | 3ced239 | 2019-12-18 14:01:40 -0800 | [diff] [blame] | 244 | bool TrivialForCodegen = |
| 245 | MD->isTrivial() || (MD->isDefaulted() && MD->getParent()->isUnion()); |
| 246 | bool TrivialAssignment = |
| 247 | TrivialForCodegen && |
| 248 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && |
| 249 | !MD->getParent()->mayInsertExtraPadding(); |
| 250 | |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 251 | // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment |
| 252 | // operator before the LHS. |
| 253 | CallArgList RtlArgStorage; |
| 254 | CallArgList *RtlArgs = nullptr; |
Richard Smith | 3ced239 | 2019-12-18 14:01:40 -0800 | [diff] [blame] | 255 | LValue TrivialAssignmentRHS; |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 256 | if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(CE)) { |
| 257 | if (OCE->isAssignmentOp()) { |
Richard Smith | 3ced239 | 2019-12-18 14:01:40 -0800 | [diff] [blame] | 258 | if (TrivialAssignment) { |
| 259 | TrivialAssignmentRHS = EmitLValue(CE->getArg(1)); |
| 260 | } else { |
| 261 | RtlArgs = &RtlArgStorage; |
| 262 | EmitCallArgs(*RtlArgs, MD->getType()->castAs<FunctionProtoType>(), |
| 263 | drop_begin(CE->arguments(), 1), CE->getDirectCallee(), |
| 264 | /*ParamsToSkip*/0, EvaluationOrder::ForceRightToLeft); |
| 265 | } |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 269 | LValue This; |
| 270 | if (IsArrow) { |
| 271 | LValueBaseInfo BaseInfo; |
| 272 | TBAAAccessInfo TBAAInfo; |
| 273 | Address ThisValue = EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo); |
| 274 | This = MakeAddrLValue(ThisValue, Base->getType(), BaseInfo, TBAAInfo); |
| 275 | } else { |
| 276 | This = EmitLValue(Base); |
| 277 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 278 | |
James Y Knight | ab4f7f1 | 2019-02-06 00:06:03 +0000 | [diff] [blame] | 279 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
| 280 | // This is the MSVC p->Ctor::Ctor(...) extension. We assume that's |
| 281 | // constructing a new complete object of type Ctor. |
| 282 | assert(!RtlArgs); |
| 283 | assert(ReturnValue.isNull() && "Constructor shouldn't have return value"); |
| 284 | CallArgList Args; |
| 285 | commonEmitCXXMemberOrOperatorCall( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 286 | *this, Ctor, This.getPointer(*this), /*ImplicitParam=*/nullptr, |
James Y Knight | ab4f7f1 | 2019-02-06 00:06:03 +0000 | [diff] [blame] | 287 | /*ImplicitParamTy=*/QualType(), CE, Args, nullptr); |
| 288 | |
| 289 | EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 290 | /*Delegating=*/false, This.getAddress(*this), Args, |
James Y Knight | ab4f7f1 | 2019-02-06 00:06:03 +0000 | [diff] [blame] | 291 | AggValueSlot::DoesNotOverlap, CE->getExprLoc(), |
| 292 | /*NewPointerIsChecked=*/false); |
| 293 | return RValue::get(nullptr); |
| 294 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 295 | |
Richard Smith | 3ced239 | 2019-12-18 14:01:40 -0800 | [diff] [blame] | 296 | if (TrivialForCodegen) { |
| 297 | if (isa<CXXDestructorDecl>(MD)) |
| 298 | return RValue::get(nullptr); |
| 299 | |
| 300 | if (TrivialAssignment) { |
| 301 | // We don't like to generate the trivial copy/move assignment operator |
| 302 | // when it isn't necessary; just produce the proper effect here. |
| 303 | // It's important that we use the result of EmitLValue here rather than |
| 304 | // emitting call arguments, in order to preserve TBAA information from |
| 305 | // the RHS. |
| 306 | LValue RHS = isa<CXXOperatorCallExpr>(CE) |
| 307 | ? TrivialAssignmentRHS |
| 308 | : EmitLValue(*CE->arg_begin()); |
| 309 | EmitAggregateAssign(This, RHS, CE->getType()); |
| 310 | return RValue::get(This.getPointer(*this)); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 311 | } |
Richard Smith | 3ced239 | 2019-12-18 14:01:40 -0800 | [diff] [blame] | 312 | |
| 313 | assert(MD->getParent()->mayInsertExtraPadding() && |
| 314 | "unknown trivial member function"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 317 | // Compute the function type we're calling. |
Nico Weber | 3abfe95 | 2014-12-02 20:41:18 +0000 | [diff] [blame] | 318 | const CXXMethodDecl *CalleeDecl = |
| 319 | DevirtualizedMethod ? DevirtualizedMethod : MD; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 320 | const CGFunctionInfo *FInfo = nullptr; |
Nico Weber | 3abfe95 | 2014-12-02 20:41:18 +0000 | [diff] [blame] | 321 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 322 | FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 323 | GlobalDecl(Dtor, Dtor_Complete)); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 324 | else |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 325 | FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 326 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 327 | llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 328 | |
Ivan Krasin | d98f5d7 | 2016-11-17 00:39:48 +0000 | [diff] [blame] | 329 | // C++11 [class.mfct.non-static]p2: |
| 330 | // If a non-static member function of a class X is called for an object that |
| 331 | // is not of type X, or of a type derived from X, the behavior is undefined. |
| 332 | SourceLocation CallLoc; |
| 333 | ASTContext &C = getContext(); |
| 334 | if (CE) |
| 335 | CallLoc = CE->getExprLoc(); |
| 336 | |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 337 | SanitizerSet SkippedChecks; |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 338 | if (const auto *CMCE = dyn_cast<CXXMemberCallExpr>(CE)) { |
| 339 | auto *IOA = CMCE->getImplicitObjectArgument(); |
| 340 | bool IsImplicitObjectCXXThis = IsWrappedCXXThis(IOA); |
| 341 | if (IsImplicitObjectCXXThis) |
| 342 | SkippedChecks.set(SanitizerKind::Alignment, true); |
| 343 | if (IsImplicitObjectCXXThis || isa<DeclRefExpr>(IOA)) |
Vedant Kumar | 34b1fd6 | 2017-02-17 23:22:59 +0000 | [diff] [blame] | 344 | SkippedChecks.set(SanitizerKind::Null, true); |
Vedant Kumar | ffd7c88 | 2017-04-14 22:03:34 +0000 | [diff] [blame] | 345 | } |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 346 | EmitTypeCheck(CodeGenFunction::TCK_MemberCall, CallLoc, |
| 347 | This.getPointer(*this), |
James Y Knight | ab4f7f1 | 2019-02-06 00:06:03 +0000 | [diff] [blame] | 348 | C.getRecordType(CalleeDecl->getParent()), |
| 349 | /*Alignment=*/CharUnits::Zero(), SkippedChecks); |
Ivan Krasin | d98f5d7 | 2016-11-17 00:39:48 +0000 | [diff] [blame] | 350 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 351 | // C++ [class.virtual]p12: |
| 352 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 353 | // virtual call mechanism. |
| 354 | // |
| 355 | // We also don't emit a virtual call if the base expression has a record type |
| 356 | // because then we know what the type is. |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 357 | bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 358 | |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 359 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 360 | assert(CE->arg_begin() == CE->arg_end() && |
| 361 | "Destructor shouldn't have explicit parameters"); |
| 362 | assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 363 | if (UseVirtualCall) { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 364 | CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, Dtor_Complete, |
| 365 | This.getAddress(*this), |
| 366 | cast<CXXMemberCallExpr>(CE)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 367 | } else { |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 368 | GlobalDecl GD(Dtor, Dtor_Complete); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 369 | CGCallee Callee; |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 370 | if (getLangOpts().AppleKext && Dtor->isVirtual() && HasQualifier) |
| 371 | Callee = BuildAppleKextVirtualCall(Dtor, Qualifier, Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 372 | else if (!DevirtualizedMethod) |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 373 | Callee = |
| 374 | CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD, FInfo, Ty), GD); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 375 | else { |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 376 | Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(GD, Ty), GD); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 377 | } |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 378 | |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 379 | QualType ThisTy = |
| 380 | IsArrow ? Base->getType()->getPointeeType() : Base->getType(); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 381 | EmitCXXDestructorCall(GD, Callee, This.getPointer(*this), ThisTy, |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 382 | /*ImplicitParam=*/nullptr, |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 383 | /*ImplicitParamTy=*/QualType(), nullptr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 384 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 385 | return RValue::get(nullptr); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 386 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 387 | |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 388 | // FIXME: Uses of 'MD' past this point need to be audited. We may need to use |
| 389 | // 'CalleeDecl' instead. |
| 390 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 391 | CGCallee Callee; |
James Y Knight | ab4f7f1 | 2019-02-06 00:06:03 +0000 | [diff] [blame] | 392 | if (UseVirtualCall) { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 393 | Callee = CGCallee::forVirtual(CE, MD, This.getAddress(*this), Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 394 | } else { |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 395 | if (SanOpts.has(SanitizerKind::CFINVCall) && |
| 396 | MD->getParent()->isDynamicClass()) { |
Peter Collingbourne | 6010880 | 2017-12-13 21:53:04 +0000 | [diff] [blame] | 397 | llvm::Value *VTable; |
| 398 | const CXXRecordDecl *RD; |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 399 | std::tie(VTable, RD) = CGM.getCXXABI().LoadVTablePtr( |
| 400 | *this, This.getAddress(*this), CalleeDecl->getParent()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 401 | EmitVTablePtrCheckForCall(RD, VTable, CFITCK_NVCall, CE->getBeginLoc()); |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 404 | if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) |
| 405 | Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 406 | else if (!DevirtualizedMethod) |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 407 | Callee = |
| 408 | CGCallee::forDirect(CGM.GetAddrOfFunction(MD, Ty), GlobalDecl(MD)); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 409 | else { |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 410 | Callee = |
| 411 | CGCallee::forDirect(CGM.GetAddrOfFunction(DevirtualizedMethod, Ty), |
| 412 | GlobalDecl(DevirtualizedMethod)); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 413 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 416 | if (MD->isVirtual()) { |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 417 | Address NewThisAddr = |
| 418 | CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 419 | *this, CalleeDecl, This.getAddress(*this), UseVirtualCall); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 420 | This.setAddress(NewThisAddr); |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 421 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 422 | |
Vedant Kumar | 018f266 | 2016-10-19 20:21:16 +0000 | [diff] [blame] | 423 | return EmitCXXMemberOrOperatorCall( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 424 | CalleeDecl, Callee, ReturnValue, This.getPointer(*this), |
Vedant Kumar | 018f266 | 2016-10-19 20:21:16 +0000 | [diff] [blame] | 425 | /*ImplicitParam=*/nullptr, QualType(), CE, RtlArgs); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | RValue |
| 429 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 430 | ReturnValueSlot ReturnValue) { |
| 431 | const BinaryOperator *BO = |
| 432 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 433 | const Expr *BaseExpr = BO->getLHS(); |
| 434 | const Expr *MemFnExpr = BO->getRHS(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 435 | |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 436 | const auto *MPT = MemFnExpr->getType()->castAs<MemberPointerType>(); |
| 437 | const auto *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>(); |
| 438 | const auto *RD = |
| 439 | cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 440 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 441 | // Emit the 'this' pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 442 | Address This = Address::invalid(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 443 | if (BO->getOpcode() == BO_PtrMemI) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 444 | This = EmitPointerWithAlignment(BaseExpr); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 445 | else |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 446 | This = EmitLValue(BaseExpr).getAddress(*this); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 447 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 448 | EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(), |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 449 | QualType(MPT->getClass(), 0)); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 450 | |
Richard Smith | bde62d7 | 2016-09-26 23:56:57 +0000 | [diff] [blame] | 451 | // Get the member function pointer. |
| 452 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
| 453 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 454 | // Ask the ABI to load the callee. Note that This is modified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 455 | llvm::Value *ThisPtrForCall = nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 456 | CGCallee Callee = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 457 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, |
| 458 | ThisPtrForCall, MemFnPtr, MPT); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 460 | CallArgList Args; |
| 461 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 462 | QualType ThisType = |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 463 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 464 | |
| 465 | // Push the this ptr. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 466 | Args.add(RValue::get(ThisPtrForCall), ThisType); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 467 | |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 468 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 469 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 470 | // And the rest of the call args |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 471 | EmitCallArgs(Args, FPT, E->arguments()); |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 472 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required, |
| 473 | /*PrefixSize=*/0), |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 474 | Callee, ReturnValue, Args, nullptr, E->getExprLoc()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | RValue |
| 478 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 479 | const CXXMethodDecl *MD, |
| 480 | ReturnValueSlot ReturnValue) { |
| 481 | assert(MD->isInstance() && |
| 482 | "Trying to emit a member call expr on a static method!"); |
Nico Weber | aad4af6 | 2014-12-03 01:21:41 +0000 | [diff] [blame] | 483 | return EmitCXXMemberOrOperatorMemberCallExpr( |
| 484 | E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr, |
| 485 | /*IsArrow=*/false, E->getArg(0)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 488 | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 489 | ReturnValueSlot ReturnValue) { |
| 490 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
| 491 | } |
| 492 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 493 | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 494 | Address DestPtr, |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 495 | const CXXRecordDecl *Base) { |
| 496 | if (Base->isEmpty()) |
| 497 | return; |
| 498 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 499 | DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 500 | |
| 501 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 502 | CharUnits NVSize = Layout.getNonVirtualSize(); |
| 503 | |
| 504 | // We cannot simply zero-initialize the entire base sub-object if vbptrs are |
| 505 | // present, they are initialized by the most derived class before calling the |
| 506 | // constructor. |
| 507 | SmallVector<std::pair<CharUnits, CharUnits>, 1> Stores; |
| 508 | Stores.emplace_back(CharUnits::Zero(), NVSize); |
| 509 | |
| 510 | // Each store is split by the existence of a vbptr. |
| 511 | CharUnits VBPtrWidth = CGF.getPointerSize(); |
| 512 | std::vector<CharUnits> VBPtrOffsets = |
| 513 | CGF.CGM.getCXXABI().getVBPtrOffsets(Base); |
| 514 | for (CharUnits VBPtrOffset : VBPtrOffsets) { |
David Majnemer | 7f980d8 | 2016-05-12 03:51:52 +0000 | [diff] [blame] | 515 | // Stop before we hit any virtual base pointers located in virtual bases. |
| 516 | if (VBPtrOffset >= NVSize) |
| 517 | break; |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 518 | std::pair<CharUnits, CharUnits> LastStore = Stores.pop_back_val(); |
| 519 | CharUnits LastStoreOffset = LastStore.first; |
| 520 | CharUnits LastStoreSize = LastStore.second; |
| 521 | |
| 522 | CharUnits SplitBeforeOffset = LastStoreOffset; |
| 523 | CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset; |
| 524 | assert(!SplitBeforeSize.isNegative() && "negative store size!"); |
| 525 | if (!SplitBeforeSize.isZero()) |
| 526 | Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize); |
| 527 | |
| 528 | CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth; |
| 529 | CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset; |
| 530 | assert(!SplitAfterSize.isNegative() && "negative store size!"); |
| 531 | if (!SplitAfterSize.isZero()) |
| 532 | Stores.emplace_back(SplitAfterOffset, SplitAfterSize); |
| 533 | } |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 534 | |
| 535 | // If the type contains a pointer to data member we can't memset it to zero. |
| 536 | // Instead, create a null constant and copy it to the destination. |
| 537 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 538 | // like -1, which happens to be the pattern used by member-pointers. |
| 539 | // TODO: isZeroInitializable can be over-conservative in the case where a |
| 540 | // virtual base contains a member pointer. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 541 | llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base); |
| 542 | if (!NullConstantForBase->isNullValue()) { |
| 543 | llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable( |
| 544 | CGF.CGM.getModule(), NullConstantForBase->getType(), |
| 545 | /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, |
| 546 | NullConstantForBase, Twine()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 547 | |
| 548 | CharUnits Align = std::max(Layout.getNonVirtualAlignment(), |
| 549 | DestPtr.getAlignment()); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame] | 550 | NullVariable->setAlignment(Align.getAsAlign()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 551 | |
| 552 | Address SrcPtr = Address(CGF.EmitCastToVoidPtr(NullVariable), Align); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 553 | |
| 554 | // Get and call the appropriate llvm.memcpy overload. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 555 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
| 556 | CharUnits StoreOffset = Store.first; |
| 557 | CharUnits StoreSize = Store.second; |
| 558 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
| 559 | CGF.Builder.CreateMemCpy( |
| 560 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
| 561 | CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset), |
| 562 | StoreSizeVal); |
| 563 | } |
| 564 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 565 | // Otherwise, just memset the whole thing to zero. This is legal |
| 566 | // because in LLVM, all default initializers (other than the ones we just |
| 567 | // handled above) are guaranteed to have a bit pattern of all zeros. |
David Majnemer | 8671c6e | 2015-11-02 09:01:44 +0000 | [diff] [blame] | 568 | } else { |
| 569 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
| 570 | CharUnits StoreOffset = Store.first; |
| 571 | CharUnits StoreSize = Store.second; |
| 572 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
| 573 | CGF.Builder.CreateMemSet( |
| 574 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
| 575 | CGF.Builder.getInt8(0), StoreSizeVal); |
| 576 | } |
| 577 | } |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 580 | void |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 581 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 582 | AggValueSlot Dest) { |
| 583 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 584 | const CXXConstructorDecl *CD = E->getConstructor(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 586 | // If we require zero initialization before (or instead of) calling the |
| 587 | // constructor, as can be the case with a non-user-provided default |
Argyrios Kyrtzidis | 0353526 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 588 | // constructor, emit the zero initialization now, unless destination is |
| 589 | // already zeroed. |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 590 | if (E->requiresZeroInitialization() && !Dest.isZeroed()) { |
| 591 | switch (E->getConstructionKind()) { |
| 592 | case CXXConstructExpr::CK_Delegating: |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 593 | case CXXConstructExpr::CK_Complete: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 594 | EmitNullInitialization(Dest.getAddress(), E->getType()); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 595 | break; |
| 596 | case CXXConstructExpr::CK_VirtualBase: |
| 597 | case CXXConstructExpr::CK_NonVirtualBase: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 598 | EmitNullBaseClassInitialization(*this, Dest.getAddress(), |
| 599 | CD->getParent()); |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 604 | // If this is a call to a trivial default constructor, do nothing. |
| 605 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 606 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 607 | |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 608 | // Elide the constructor if we're constructing from a temporary. |
| 609 | // The temporary check is required because Sema sets this on NRVO |
| 610 | // returns. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 611 | if (getLangOpts().ElideConstructors && E->isElidable()) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 612 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 613 | E->getArg(0)->getType())); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 614 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 615 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 616 | return; |
| 617 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 618 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 619 | |
Alexey Bataev | e7545b3 | 2016-04-29 09:39:50 +0000 | [diff] [blame] | 620 | if (const ArrayType *arrayType |
| 621 | = getContext().getAsArrayType(E->getType())) { |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 622 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E, |
| 623 | Dest.isSanitizerChecked()); |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 624 | } else { |
Cameron Esfahani | bceca20 | 2011-05-06 21:28:42 +0000 | [diff] [blame] | 625 | CXXCtorType Type = Ctor_Complete; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 626 | bool ForVirtualBase = false; |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 627 | bool Delegating = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 628 | |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 629 | switch (E->getConstructionKind()) { |
| 630 | case CXXConstructExpr::CK_Delegating: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 631 | // We should be emitting a constructor; GlobalDecl will assert this |
| 632 | Type = CurGD.getCtorType(); |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 633 | Delegating = true; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 634 | break; |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 635 | |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 636 | case CXXConstructExpr::CK_Complete: |
| 637 | Type = Ctor_Complete; |
| 638 | break; |
| 639 | |
| 640 | case CXXConstructExpr::CK_VirtualBase: |
| 641 | ForVirtualBase = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 642 | LLVM_FALLTHROUGH; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 643 | |
| 644 | case CXXConstructExpr::CK_NonVirtualBase: |
| 645 | Type = Ctor_Base; |
Anastasia Stulova | 094c726 | 2019-04-04 10:48:36 +0000 | [diff] [blame] | 646 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 647 | |
Anastasia Stulova | 094c726 | 2019-04-04 10:48:36 +0000 | [diff] [blame] | 648 | // Call the constructor. |
| 649 | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest, E); |
Anders Carlsson | e11f9ce | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 650 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 651 | } |
| 652 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 653 | void CodeGenFunction::EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, |
| 654 | const Expr *Exp) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 655 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 656 | Exp = E->getSubExpr(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 657 | assert(isa<CXXConstructExpr>(Exp) && |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 658 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
| 659 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
| 660 | const CXXConstructorDecl *CD = E->getConstructor(); |
| 661 | RunCleanupsScope Scope(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 662 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 663 | // If we require zero initialization before (or instead of) calling the |
| 664 | // constructor, as can be the case with a non-user-provided default |
| 665 | // constructor, emit the zero initialization now. |
| 666 | // FIXME. Do I still need this for a copy ctor synthesis? |
| 667 | if (E->requiresZeroInitialization()) |
| 668 | EmitNullInitialization(Dest, E->getType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 669 | |
Chandler Carruth | 99da11c | 2010-11-15 13:54:43 +0000 | [diff] [blame] | 670 | assert(!getContext().getAsConstantArrayType(E->getType()) |
| 671 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
Alexey Samsonov | 525bf65 | 2014-08-25 21:58:56 +0000 | [diff] [blame] | 672 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E); |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 673 | } |
| 674 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 675 | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
| 676 | const CXXNewExpr *E) { |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 677 | if (!E->isArray()) |
Ken Dyck | 3eb55cf | 2010-01-26 19:44:24 +0000 | [diff] [blame] | 678 | return CharUnits::Zero(); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 679 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 680 | // No cookie is required if the operator new[] being used is the |
| 681 | // reserved placement operator new[]. |
| 682 | if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) |
John McCall | aa4149a | 2010-08-23 01:17:59 +0000 | [diff] [blame] | 683 | return CharUnits::Zero(); |
| 684 | |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 685 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 686 | } |
| 687 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 688 | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
| 689 | const CXXNewExpr *e, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 690 | unsigned minElements, |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 691 | llvm::Value *&numElements, |
| 692 | llvm::Value *&sizeWithoutCookie) { |
| 693 | QualType type = e->getAllocatedType(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 694 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 695 | if (!e->isArray()) { |
| 696 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 697 | sizeWithoutCookie |
| 698 | = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); |
| 699 | return sizeWithoutCookie; |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 700 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 701 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 702 | // The width of size_t. |
| 703 | unsigned sizeWidth = CGF.SizeTy->getBitWidth(); |
| 704 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 705 | // Figure out the cookie size. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 706 | llvm::APInt cookieSize(sizeWidth, |
| 707 | CalculateCookiePadding(CGF, e).getQuantity()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 708 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 709 | // Emit the array size expression. |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 710 | // We multiply the size of all dimensions for NumElements. |
| 711 | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 712 | numElements = |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 713 | ConstantEmitter(CGF).tryEmitAbstract(*e->getArraySize(), e->getType()); |
Nick Lewycky | 0752762 | 2017-02-13 23:49:55 +0000 | [diff] [blame] | 714 | if (!numElements) |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 715 | numElements = CGF.EmitScalarExpr(*e->getArraySize()); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 716 | assert(isa<llvm::IntegerType>(numElements->getType())); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 717 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 718 | // The number of elements can be have an arbitrary integer type; |
| 719 | // essentially, we need to multiply it by a constant factor, add a |
| 720 | // cookie size, and verify that the result is representable as a |
| 721 | // size_t. That's just a gloss, though, and it's wrong in one |
| 722 | // important way: if the count is negative, it's an error even if |
| 723 | // the cookie size would bring the total size >= 0. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 724 | bool isSigned |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 725 | = (*e->getArraySize())->getType()->isSignedIntegerOrEnumerationType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 726 | llvm::IntegerType *numElementsType |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 727 | = cast<llvm::IntegerType>(numElements->getType()); |
| 728 | unsigned numElementsWidth = numElementsType->getBitWidth(); |
| 729 | |
| 730 | // Compute the constant factor. |
| 731 | llvm::APInt arraySizeMultiplier(sizeWidth, 1); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 732 | while (const ConstantArrayType *CAT |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 733 | = CGF.getContext().getAsConstantArrayType(type)) { |
| 734 | type = CAT->getElementType(); |
| 735 | arraySizeMultiplier *= CAT->getSize(); |
Argyrios Kyrtzidis | 7648fb4 | 2010-08-26 15:23:38 +0000 | [diff] [blame] | 736 | } |
| 737 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 738 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
| 739 | llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); |
| 740 | typeSizeMultiplier *= arraySizeMultiplier; |
| 741 | |
| 742 | // This will be a size_t. |
| 743 | llvm::Value *size; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 745 | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
| 746 | // Don't bloat the -O0 code. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 747 | if (llvm::ConstantInt *numElementsC = |
| 748 | dyn_cast<llvm::ConstantInt>(numElements)) { |
| 749 | const llvm::APInt &count = numElementsC->getValue(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 750 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 751 | bool hasAnyOverflow = false; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 752 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 753 | // If 'count' was a negative number, it's an overflow. |
| 754 | if (isSigned && count.isNegative()) |
| 755 | hasAnyOverflow = true; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 756 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 757 | // We want to do all this arithmetic in size_t. If numElements is |
| 758 | // wider than that, check whether it's already too big, and if so, |
| 759 | // overflow. |
| 760 | else if (numElementsWidth > sizeWidth && |
| 761 | numElementsWidth - sizeWidth > count.countLeadingZeros()) |
| 762 | hasAnyOverflow = true; |
| 763 | |
| 764 | // Okay, compute a count at the right width. |
| 765 | llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); |
| 766 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 767 | // If there is a brace-initializer, we cannot allocate fewer elements than |
| 768 | // there are initializers. If we do, that's treated like an overflow. |
| 769 | if (adjustedCount.ult(minElements)) |
| 770 | hasAnyOverflow = true; |
| 771 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 772 | // Scale numElements by that. This might overflow, but we don't |
| 773 | // care because it only overflows if allocationSize does, too, and |
| 774 | // if that overflows then we shouldn't use this. |
| 775 | numElements = llvm::ConstantInt::get(CGF.SizeTy, |
| 776 | adjustedCount * arraySizeMultiplier); |
| 777 | |
| 778 | // Compute the size before cookie, and track whether it overflowed. |
| 779 | bool overflow; |
| 780 | llvm::APInt allocationSize |
| 781 | = adjustedCount.umul_ov(typeSizeMultiplier, overflow); |
| 782 | hasAnyOverflow |= overflow; |
| 783 | |
| 784 | // Add in the cookie, and check whether it's overflowed. |
| 785 | if (cookieSize != 0) { |
| 786 | // Save the current size without a cookie. This shouldn't be |
| 787 | // used if there was overflow. |
| 788 | sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
| 789 | |
| 790 | allocationSize = allocationSize.uadd_ov(cookieSize, overflow); |
| 791 | hasAnyOverflow |= overflow; |
| 792 | } |
| 793 | |
| 794 | // On overflow, produce a -1 so operator new will fail. |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 795 | if (hasAnyOverflow) { |
| 796 | size = llvm::Constant::getAllOnesValue(CGF.SizeTy); |
| 797 | } else { |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 798 | size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 799 | } |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 800 | |
| 801 | // Otherwise, we might need to use the overflow intrinsics. |
| 802 | } else { |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 803 | // There are up to five conditions we need to test for: |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 804 | // 1) if isSigned, we need to check whether numElements is negative; |
| 805 | // 2) if numElementsWidth > sizeWidth, we need to check whether |
| 806 | // numElements is larger than something representable in size_t; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 807 | // 3) if minElements > 0, we need to check whether numElements is smaller |
| 808 | // than that. |
| 809 | // 4) we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 810 | // sizeWithoutCookie := numElements * typeSizeMultiplier |
| 811 | // and check whether it overflows; and |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 812 | // 5) if we need a cookie, we need to compute |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 813 | // size := sizeWithoutCookie + cookieSize |
| 814 | // and check whether it overflows. |
| 815 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 816 | llvm::Value *hasOverflow = nullptr; |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 817 | |
| 818 | // If numElementsWidth > sizeWidth, then one way or another, we're |
| 819 | // going to have to do a comparison for (2), and this happens to |
| 820 | // take care of (1), too. |
| 821 | if (numElementsWidth > sizeWidth) { |
| 822 | llvm::APInt threshold(numElementsWidth, 1); |
| 823 | threshold <<= sizeWidth; |
| 824 | |
| 825 | llvm::Value *thresholdV |
| 826 | = llvm::ConstantInt::get(numElementsType, threshold); |
| 827 | |
| 828 | hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); |
| 829 | numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); |
| 830 | |
| 831 | // Otherwise, if we're signed, we want to sext up to size_t. |
| 832 | } else if (isSigned) { |
| 833 | if (numElementsWidth < sizeWidth) |
| 834 | numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 835 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 836 | // If there's a non-1 type size multiplier, then we can do the |
| 837 | // signedness check at the same time as we do the multiply |
| 838 | // because a negative number times anything will cause an |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 839 | // unsigned overflow. Otherwise, we have to do it here. But at least |
| 840 | // in this case, we can subsume the >= minElements check. |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 841 | if (typeSizeMultiplier == 1) |
| 842 | hasOverflow = CGF.Builder.CreateICmpSLT(numElements, |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 843 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 844 | |
| 845 | // Otherwise, zext up to size_t if necessary. |
| 846 | } else if (numElementsWidth < sizeWidth) { |
| 847 | numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); |
| 848 | } |
| 849 | |
| 850 | assert(numElements->getType() == CGF.SizeTy); |
| 851 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 852 | if (minElements) { |
| 853 | // Don't allow allocation of fewer elements than we have initializers. |
| 854 | if (!hasOverflow) { |
| 855 | hasOverflow = CGF.Builder.CreateICmpULT(numElements, |
| 856 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
| 857 | } else if (numElementsWidth > sizeWidth) { |
| 858 | // The other existing overflow subsumes this check. |
| 859 | // We do an unsigned comparison, since any signed value < -1 is |
| 860 | // taken care of either above or below. |
| 861 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, |
| 862 | CGF.Builder.CreateICmpULT(numElements, |
| 863 | llvm::ConstantInt::get(CGF.SizeTy, minElements))); |
| 864 | } |
| 865 | } |
| 866 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 867 | size = numElements; |
| 868 | |
| 869 | // Multiply by the type size if necessary. This multiplier |
| 870 | // includes all the factors for nested arrays. |
| 871 | // |
| 872 | // This step also causes numElements to be scaled up by the |
| 873 | // nested-array factor if necessary. Overflow on this computation |
| 874 | // can be ignored because the result shouldn't be used if |
| 875 | // allocation fails. |
| 876 | if (typeSizeMultiplier != 1) { |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 877 | llvm::Function *umul_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 878 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 879 | |
| 880 | llvm::Value *tsmV = |
| 881 | llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); |
| 882 | llvm::Value *result = |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 883 | CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV}); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 884 | |
| 885 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 886 | if (hasOverflow) |
| 887 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 888 | else |
| 889 | hasOverflow = overflowed; |
| 890 | |
| 891 | size = CGF.Builder.CreateExtractValue(result, 0); |
| 892 | |
| 893 | // Also scale up numElements by the array size multiplier. |
| 894 | if (arraySizeMultiplier != 1) { |
| 895 | // If the base element type size is 1, then we can re-use the |
| 896 | // multiply we just did. |
| 897 | if (typeSize.isOne()) { |
| 898 | assert(arraySizeMultiplier == typeSizeMultiplier); |
| 899 | numElements = size; |
| 900 | |
| 901 | // Otherwise we need a separate multiply. |
| 902 | } else { |
| 903 | llvm::Value *asmV = |
| 904 | llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); |
| 905 | numElements = CGF.Builder.CreateMul(numElements, asmV); |
| 906 | } |
| 907 | } |
| 908 | } else { |
| 909 | // numElements doesn't need to be scaled. |
| 910 | assert(arraySizeMultiplier == 1); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 911 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 912 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 913 | // Add in the cookie size if necessary. |
| 914 | if (cookieSize != 0) { |
| 915 | sizeWithoutCookie = size; |
| 916 | |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 917 | llvm::Function *uadd_with_overflow |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 918 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 919 | |
| 920 | llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); |
| 921 | llvm::Value *result = |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 922 | CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV}); |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 923 | |
| 924 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
| 925 | if (hasOverflow) |
| 926 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
| 927 | else |
| 928 | hasOverflow = overflowed; |
| 929 | |
| 930 | size = CGF.Builder.CreateExtractValue(result, 0); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 931 | } |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 932 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 933 | // If we had any possibility of dynamic overflow, make a select to |
| 934 | // overwrite 'size' with an all-ones value, which should cause |
| 935 | // operator new to throw. |
| 936 | if (hasOverflow) |
Aaron Ballman | 455f42c | 2014-08-28 17:24:14 +0000 | [diff] [blame] | 937 | size = CGF.Builder.CreateSelect(hasOverflow, |
| 938 | llvm::Constant::getAllOnesValue(CGF.SizeTy), |
| 939 | size); |
Chris Lattner | 32ac583 | 2010-07-20 21:55:52 +0000 | [diff] [blame] | 940 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 941 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 942 | if (cookieSize == 0) |
| 943 | sizeWithoutCookie = size; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 944 | else |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 945 | assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 946 | |
John McCall | 036f2f6 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 947 | return size; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 950 | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 951 | QualType AllocType, Address NewPtr, |
| 952 | AggValueSlot::Overlap_t MayOverlap) { |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 953 | // FIXME: Refactor with EmitExprAsInit. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 954 | switch (CGF.getEvaluationKind(AllocType)) { |
| 955 | case TEK_Scalar: |
David Blaikie | a2c1124 | 2014-12-10 19:04:09 +0000 | [diff] [blame] | 956 | CGF.EmitScalarInit(Init, nullptr, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 957 | CGF.MakeAddrLValue(NewPtr, AllocType), false); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 958 | return; |
| 959 | case TEK_Complex: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 960 | CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 961 | /*isInit*/ true); |
| 962 | return; |
| 963 | case TEK_Aggregate: { |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 964 | AggValueSlot Slot |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 965 | = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(), |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 966 | AggValueSlot::IsDestructed, |
John McCall | 46759f4 | 2011-08-26 07:31:35 +0000 | [diff] [blame] | 967 | AggValueSlot::DoesNotNeedGCBarriers, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 968 | AggValueSlot::IsNotAliased, |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 969 | MayOverlap, AggValueSlot::IsNotZeroed, |
| 970 | AggValueSlot::IsSanitizerChecked); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 971 | CGF.EmitAggExpr(Init, Slot); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 972 | return; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 973 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 974 | } |
| 975 | llvm_unreachable("bad evaluation kind"); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 976 | } |
| 977 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 978 | void CodeGenFunction::EmitNewArrayInitializer( |
| 979 | const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 980 | Address BeginPtr, llvm::Value *NumElements, |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 981 | llvm::Value *AllocSizeWithoutCookie) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 982 | // If we have a type with trivial initialization and no initializer, |
| 983 | // there's nothing to do. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 984 | if (!E->hasInitializer()) |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 985 | return; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 986 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 987 | Address CurPtr = BeginPtr; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 988 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 989 | unsigned InitListElements = 0; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 990 | |
| 991 | const Expr *Init = E->getInitializer(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 992 | Address EndOfInit = Address::invalid(); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 993 | QualType::DestructionKind DtorKind = ElementType.isDestructedType(); |
| 994 | EHScopeStack::stable_iterator Cleanup; |
| 995 | llvm::Instruction *CleanupDominator = nullptr; |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 996 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 997 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType); |
| 998 | CharUnits ElementAlign = |
| 999 | BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize); |
| 1000 | |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1001 | // Attempt to perform zero-initialization using memset. |
| 1002 | auto TryMemsetInitialization = [&]() -> bool { |
| 1003 | // FIXME: If the type is a pointer-to-data-member under the Itanium ABI, |
| 1004 | // we can initialize with a memset to -1. |
| 1005 | if (!CGM.getTypes().isZeroInitializable(ElementType)) |
| 1006 | return false; |
| 1007 | |
| 1008 | // Optimization: since zero initialization will just set the memory |
| 1009 | // to all zeroes, generate a single memset to do it in one shot. |
| 1010 | |
| 1011 | // Subtract out the size of any elements we've already initialized. |
| 1012 | auto *RemainingSize = AllocSizeWithoutCookie; |
| 1013 | if (InitListElements) { |
| 1014 | // We know this can't overflow; we check this when doing the allocation. |
| 1015 | auto *InitializedSize = llvm::ConstantInt::get( |
| 1016 | RemainingSize->getType(), |
| 1017 | getContext().getTypeSizeInChars(ElementType).getQuantity() * |
| 1018 | InitListElements); |
| 1019 | RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize); |
| 1020 | } |
| 1021 | |
| 1022 | // Create the memset. |
| 1023 | Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false); |
| 1024 | return true; |
| 1025 | }; |
| 1026 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1027 | // If the initializer is an initializer list, first do the explicit elements. |
| 1028 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1029 | // Initializing from a (braced) string literal is a special case; the init |
| 1030 | // list element does not initialize a (single) array element. |
| 1031 | if (ILE->isStringLiteralInit()) { |
| 1032 | // Initialize the initial portion of length equal to that of the string |
| 1033 | // literal. The allocation must be for at least this much; we emitted a |
| 1034 | // check for that earlier. |
| 1035 | AggValueSlot Slot = |
| 1036 | AggValueSlot::forAddr(CurPtr, ElementType.getQualifiers(), |
| 1037 | AggValueSlot::IsDestructed, |
| 1038 | AggValueSlot::DoesNotNeedGCBarriers, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1039 | AggValueSlot::IsNotAliased, |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 1040 | AggValueSlot::DoesNotOverlap, |
| 1041 | AggValueSlot::IsNotZeroed, |
| 1042 | AggValueSlot::IsSanitizerChecked); |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1043 | EmitAggExpr(ILE->getInit(0), Slot); |
| 1044 | |
| 1045 | // Move past these elements. |
| 1046 | InitListElements = |
| 1047 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
| 1048 | ->getSize().getZExtValue(); |
| 1049 | CurPtr = |
| 1050 | Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), |
| 1051 | Builder.getSize(InitListElements), |
| 1052 | "string.init.end"), |
| 1053 | CurPtr.getAlignment().alignmentAtOffset(InitListElements * |
| 1054 | ElementSize)); |
| 1055 | |
| 1056 | // Zero out the rest, if any remain. |
| 1057 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
| 1058 | if (!ConstNum || !ConstNum->equalsInt(InitListElements)) { |
| 1059 | bool OK = TryMemsetInitialization(); |
| 1060 | (void)OK; |
| 1061 | assert(OK && "couldn't memset character type?"); |
| 1062 | } |
| 1063 | return; |
| 1064 | } |
| 1065 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1066 | InitListElements = ILE->getNumInits(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1067 | |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1068 | // If this is a multi-dimensional array new, we will initialize multiple |
| 1069 | // elements with each init list element. |
| 1070 | QualType AllocType = E->getAllocatedType(); |
| 1071 | if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>( |
| 1072 | AllocType->getAsArrayTypeUnsafe())) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1073 | ElementTy = ConvertTypeForMem(AllocType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1074 | CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1075 | InitListElements *= getContext().getConstantArrayElementCount(CAT); |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1078 | // Enter a partial-destruction Cleanup if necessary. |
| 1079 | if (needsEHCleanup(DtorKind)) { |
| 1080 | // In principle we could tell the Cleanup where we are more |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1081 | // directly, but the control flow can get so varied here that it |
| 1082 | // would actually be quite complex. Therefore we go through an |
| 1083 | // alloca. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1084 | EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(), |
| 1085 | "array.init.end"); |
| 1086 | CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit); |
| 1087 | pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit, |
| 1088 | ElementType, ElementAlign, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1089 | getDestroyer(DtorKind)); |
| 1090 | Cleanup = EHStack.stable_begin(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1093 | CharUnits StartAlign = CurPtr.getAlignment(); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1094 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1095 | // Tell the cleanup that it needs to destroy up to this |
| 1096 | // element. TODO: some of these stores can be trivially |
| 1097 | // observed to be unnecessary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1098 | if (EndOfInit.isValid()) { |
| 1099 | auto FinishedPtr = |
| 1100 | Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType()); |
| 1101 | Builder.CreateStore(FinishedPtr, EndOfInit); |
| 1102 | } |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1103 | // FIXME: If the last initializer is an incomplete initializer list for |
| 1104 | // an array, and we have an array filler, we can fold together the two |
| 1105 | // initialization loops. |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1106 | StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1107 | ILE->getInit(i)->getType(), CurPtr, |
| 1108 | AggValueSlot::DoesNotOverlap); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1109 | CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), |
| 1110 | Builder.getSize(1), |
| 1111 | "array.exp.next"), |
| 1112 | StartAlign.alignmentAtOffset((i + 1) * ElementSize)); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // The remaining elements are filled with the array filler expression. |
| 1116 | Init = ILE->getArrayFiller(); |
Richard Smith | 1c96bc5 | 2013-12-11 01:40:16 +0000 | [diff] [blame] | 1117 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1118 | // Extract the initializer for the individual array elements by pulling |
| 1119 | // out the array filler from all the nested initializer lists. This avoids |
| 1120 | // generating a nested loop for the initialization. |
| 1121 | while (Init && Init->getType()->isConstantArrayType()) { |
| 1122 | auto *SubILE = dyn_cast<InitListExpr>(Init); |
| 1123 | if (!SubILE) |
| 1124 | break; |
| 1125 | assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?"); |
| 1126 | Init = SubILE->getArrayFiller(); |
| 1127 | } |
| 1128 | |
| 1129 | // Switch back to initializing one base element at a time. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1130 | CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr.getType()); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1133 | // If all elements have already been initialized, skip any further |
| 1134 | // initialization. |
| 1135 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
| 1136 | if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { |
| 1137 | // If there was a Cleanup, deactivate it. |
| 1138 | if (CleanupDominator) |
| 1139 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | assert(Init && "have trailing elements to initialize but no initializer"); |
| 1144 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1145 | // If this is a constructor call, try to optimize it out, and failing that |
| 1146 | // emit a single loop to initialize all remaining elements. |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1147 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1148 | CXXConstructorDecl *Ctor = CCE->getConstructor(); |
| 1149 | if (Ctor->isTrivial()) { |
| 1150 | // If new expression did not specify value-initialization, then there |
| 1151 | // is no initialization. |
| 1152 | if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) |
| 1153 | return; |
| 1154 | |
| 1155 | if (TryMemsetInitialization()) |
| 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | // Store the new Cleanup position for irregular Cleanups. |
| 1160 | // |
| 1161 | // FIXME: Share this cleanup with the constructor call emission rather than |
| 1162 | // having it create a cleanup of its own. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1163 | if (EndOfInit.isValid()) |
| 1164 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1165 | |
| 1166 | // Emit a constructor call loop to initialize the remaining elements. |
| 1167 | if (InitListElements) |
| 1168 | NumElements = Builder.CreateSub( |
| 1169 | NumElements, |
| 1170 | llvm::ConstantInt::get(NumElements->getType(), InitListElements)); |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1171 | EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE, |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 1172 | /*NewPointerIsChecked*/true, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1173 | CCE->requiresZeroInitialization()); |
Chandler Carruth | e6c980c | 2014-05-03 09:16:57 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
| 1176 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1177 | // If this is value-initialization, we can usually use memset. |
| 1178 | ImplicitValueInitExpr IVIE(ElementType); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1179 | if (isa<ImplicitValueInitExpr>(Init)) { |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1180 | if (TryMemsetInitialization()) |
| 1181 | return; |
| 1182 | |
| 1183 | // Switch to an ImplicitValueInitExpr for the element type. This handles |
| 1184 | // only one case: multidimensional array new of pointers to members. In |
| 1185 | // all other cases, we already have an initializer for the array element. |
| 1186 | Init = &IVIE; |
| 1187 | } |
| 1188 | |
| 1189 | // At this point we should have found an initializer for the individual |
| 1190 | // elements of the array. |
| 1191 | assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && |
| 1192 | "got wrong type of element to initialize"); |
| 1193 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1194 | // If we have an empty initializer list, we can usually use memset. |
| 1195 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) |
| 1196 | if (ILE->getNumInits() == 0 && TryMemsetInitialization()) |
| 1197 | return; |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1198 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1199 | // If we have a struct whose every field is value-initialized, we can |
| 1200 | // usually use memset. |
| 1201 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) { |
| 1202 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
| 1203 | if (RType->getDecl()->isStruct()) { |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1204 | unsigned NumElements = 0; |
| 1205 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RType->getDecl())) |
| 1206 | NumElements = CXXRD->getNumBases(); |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1207 | for (auto *Field : RType->getDecl()->fields()) |
| 1208 | if (!Field->isUnnamedBitfield()) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1209 | ++NumElements; |
| 1210 | // FIXME: Recurse into nested InitListExprs. |
| 1211 | if (ILE->getNumInits() == NumElements) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1212 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) |
| 1213 | if (!isa<ImplicitValueInitExpr>(ILE->getInit(i))) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1214 | --NumElements; |
| 1215 | if (ILE->getNumInits() == NumElements && TryMemsetInitialization()) |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1216 | return; |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1221 | // Create the loop blocks. |
| 1222 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
| 1223 | llvm::BasicBlock *LoopBB = createBasicBlock("new.loop"); |
| 1224 | llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end"); |
| 1225 | |
| 1226 | // Find the end of the array, hoisted out of the loop. |
| 1227 | llvm::Value *EndPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1228 | Builder.CreateInBoundsGEP(BeginPtr.getPointer(), NumElements, "array.end"); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1229 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1230 | // If the number of elements isn't constant, we have to now check if there is |
| 1231 | // anything left to initialize. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1232 | if (!ConstNum) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1233 | llvm::Value *IsEmpty = |
| 1234 | Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty"); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1235 | Builder.CreateCondBr(IsEmpty, ContBB, LoopBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | // Enter the loop. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1239 | EmitBlock(LoopBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Set up the current-element phi. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1242 | llvm::PHINode *CurPtrPhi = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1243 | Builder.CreatePHI(CurPtr.getType(), 2, "array.cur"); |
| 1244 | CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB); |
| 1245 | |
| 1246 | CurPtr = Address(CurPtrPhi, ElementAlign); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1247 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1248 | // Store the new Cleanup position for irregular Cleanups. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1249 | if (EndOfInit.isValid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1250 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 1251 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1252 | // Enter a partial-destruction Cleanup if necessary. |
| 1253 | if (!CleanupDominator && needsEHCleanup(DtorKind)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1254 | pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(), |
| 1255 | ElementType, ElementAlign, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1256 | getDestroyer(DtorKind)); |
| 1257 | Cleanup = EHStack.stable_begin(); |
| 1258 | CleanupDominator = Builder.CreateUnreachable(); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | // Emit the initializer into this element. |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1262 | StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr, |
| 1263 | AggValueSlot::DoesNotOverlap); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1264 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1265 | // Leave the Cleanup if we entered one. |
| 1266 | if (CleanupDominator) { |
| 1267 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
| 1268 | CleanupDominator->eraseFromParent(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1269 | } |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1270 | |
Faisal Vali | 57ae056 | 2013-12-14 00:40:05 +0000 | [diff] [blame] | 1271 | // Advance to the next element by adjusting the pointer type as necessary. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1272 | llvm::Value *NextPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1273 | Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1, |
| 1274 | "array.next"); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1275 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1276 | // Check whether we've gotten to the end of the array and, if so, |
| 1277 | // exit the loop. |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1278 | llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend"); |
| 1279 | Builder.CreateCondBr(IsEnd, ContBB, LoopBB); |
| 1280 | CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock()); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1281 | |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1282 | EmitBlock(ContBB); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1285 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1286 | QualType ElementType, llvm::Type *ElementTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1287 | Address NewPtr, llvm::Value *NumElements, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1288 | llvm::Value *AllocSizeWithoutCookie) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 1289 | ApplyDebugLocation DL(CGF, E); |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1290 | if (E->isArray()) |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1291 | CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements, |
Richard Smith | 06a67e2 | 2014-06-03 06:58:52 +0000 | [diff] [blame] | 1292 | AllocSizeWithoutCookie); |
| 1293 | else if (const Expr *Init = E->getInitializer()) |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1294 | StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr, |
| 1295 | AggValueSlot::DoesNotOverlap); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1298 | /// Emit a call to an operator new or operator delete function, as implicitly |
| 1299 | /// created by new-expressions and delete-expressions. |
| 1300 | static RValue EmitNewDeleteCall(CodeGenFunction &CGF, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1301 | const FunctionDecl *CalleeDecl, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1302 | const FunctionProtoType *CalleeType, |
| 1303 | const CallArgList &Args) { |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 1304 | llvm::CallBase *CallOrInvoke; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1305 | llvm::Constant *CalleePtr = CGF.CGM.GetAddrOfFunction(CalleeDecl); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1306 | CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(CalleeDecl)); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1307 | RValue RV = |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1308 | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall( |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1309 | Args, CalleeType, /*ChainCall=*/false), |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1310 | Callee, ReturnValueSlot(), Args, &CallOrInvoke); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1311 | |
| 1312 | /// C++1y [expr.new]p10: |
| 1313 | /// [In a new-expression,] an implementation is allowed to omit a call |
| 1314 | /// to a replaceable global allocation function. |
| 1315 | /// |
| 1316 | /// We model such elidable calls with the 'builtin' attribute. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 1317 | llvm::Function *Fn = dyn_cast<llvm::Function>(CalleePtr); |
| 1318 | if (CalleeDecl->isReplaceableGlobalAllocationFunction() && |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 1319 | Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) { |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 1320 | CallOrInvoke->addAttribute(llvm::AttributeList::FunctionIndex, |
| 1321 | llvm::Attribute::Builtin); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | return RV; |
| 1325 | } |
| 1326 | |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1327 | RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 1328 | const CallExpr *TheCall, |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1329 | bool IsDelete) { |
| 1330 | CallArgList Args; |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 1331 | EmitCallArgs(Args, Type->getParamTypes(), TheCall->arguments()); |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1332 | // Find the allocation or deallocation function that we're calling. |
| 1333 | ASTContext &Ctx = getContext(); |
| 1334 | DeclarationName Name = Ctx.DeclarationNames |
| 1335 | .getCXXOperatorName(IsDelete ? OO_Delete : OO_New); |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 1336 | |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1337 | for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name)) |
Richard Smith | 599bed7 | 2014-06-05 00:43:02 +0000 | [diff] [blame] | 1338 | if (auto *FD = dyn_cast<FunctionDecl>(Decl)) |
| 1339 | if (Ctx.hasSameType(FD->getType(), QualType(Type, 0))) |
Eric Fiselier | fa752f2 | 2018-03-21 19:19:48 +0000 | [diff] [blame] | 1340 | return EmitNewDeleteCall(*this, FD, Type, Args); |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 1341 | llvm_unreachable("predeclared global operator new/delete is missing"); |
| 1342 | } |
| 1343 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1344 | namespace { |
| 1345 | /// The parameters to pass to a usual operator delete. |
| 1346 | struct UsualDeleteParams { |
| 1347 | bool DestroyingDelete = false; |
| 1348 | bool Size = false; |
| 1349 | bool Alignment = false; |
| 1350 | }; |
| 1351 | } |
| 1352 | |
| 1353 | static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD) { |
| 1354 | UsualDeleteParams Params; |
| 1355 | |
| 1356 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1357 | auto AI = FPT->param_type_begin(), AE = FPT->param_type_end(); |
Richard Smith | 189e52f | 2016-10-10 06:42:31 +0000 | [diff] [blame] | 1358 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1359 | // The first argument is always a void*. |
| 1360 | ++AI; |
| 1361 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1362 | // The next parameter may be a std::destroying_delete_t. |
| 1363 | if (FD->isDestroyingOperatorDelete()) { |
| 1364 | Params.DestroyingDelete = true; |
| 1365 | assert(AI != AE); |
| 1366 | ++AI; |
| 1367 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1368 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1369 | // Figure out what other parameters we should be implicitly passing. |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1370 | if (AI != AE && (*AI)->isIntegerType()) { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1371 | Params.Size = true; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1372 | ++AI; |
| 1373 | } |
| 1374 | |
| 1375 | if (AI != AE && (*AI)->isAlignValT()) { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1376 | Params.Alignment = true; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1377 | ++AI; |
| 1378 | } |
| 1379 | |
| 1380 | assert(AI == AE && "unexpected usual deallocation function parameter"); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1381 | return Params; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | namespace { |
| 1385 | /// A cleanup to call the given 'operator delete' function upon abnormal |
| 1386 | /// exit from a new expression. Templated on a traits type that deals with |
| 1387 | /// ensuring that the arguments dominate the cleanup if necessary. |
| 1388 | template<typename Traits> |
| 1389 | class CallDeleteDuringNew final : public EHScopeStack::Cleanup { |
| 1390 | /// Type used to hold llvm::Value*s. |
| 1391 | typedef typename Traits::ValueTy ValueTy; |
| 1392 | /// Type used to hold RValues. |
| 1393 | typedef typename Traits::RValueTy RValueTy; |
| 1394 | struct PlacementArg { |
| 1395 | RValueTy ArgValue; |
| 1396 | QualType ArgType; |
| 1397 | }; |
| 1398 | |
| 1399 | unsigned NumPlacementArgs : 31; |
| 1400 | unsigned PassAlignmentToPlacementDelete : 1; |
| 1401 | const FunctionDecl *OperatorDelete; |
| 1402 | ValueTy Ptr; |
| 1403 | ValueTy AllocSize; |
| 1404 | CharUnits AllocAlign; |
| 1405 | |
| 1406 | PlacementArg *getPlacementArgs() { |
| 1407 | return reinterpret_cast<PlacementArg *>(this + 1); |
| 1408 | } |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1409 | |
| 1410 | public: |
| 1411 | static size_t getExtraSize(size_t NumPlacementArgs) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1412 | return NumPlacementArgs * sizeof(PlacementArg); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | CallDeleteDuringNew(size_t NumPlacementArgs, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1416 | const FunctionDecl *OperatorDelete, ValueTy Ptr, |
| 1417 | ValueTy AllocSize, bool PassAlignmentToPlacementDelete, |
| 1418 | CharUnits AllocAlign) |
| 1419 | : NumPlacementArgs(NumPlacementArgs), |
| 1420 | PassAlignmentToPlacementDelete(PassAlignmentToPlacementDelete), |
| 1421 | OperatorDelete(OperatorDelete), Ptr(Ptr), AllocSize(AllocSize), |
| 1422 | AllocAlign(AllocAlign) {} |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1423 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1424 | void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1425 | assert(I < NumPlacementArgs && "index out of range"); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1426 | getPlacementArgs()[I] = {Arg, Type}; |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1430 | const FunctionProtoType *FPT = |
| 1431 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1432 | CallArgList DeleteArgs; |
| 1433 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1434 | // The first argument is always a void* (or C* for a destroying operator |
| 1435 | // delete for class type C). |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1436 | DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); |
Daniel Jasper | e9abe64 | 2016-10-10 14:13:55 +0000 | [diff] [blame] | 1437 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1438 | // Figure out what other parameters we should be implicitly passing. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1439 | UsualDeleteParams Params; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1440 | if (NumPlacementArgs) { |
| 1441 | // A placement deallocation function is implicitly passed an alignment |
| 1442 | // if the placement allocation function was, but is never passed a size. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1443 | Params.Alignment = PassAlignmentToPlacementDelete; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1444 | } else { |
| 1445 | // For a non-placement new-expression, 'operator delete' can take a |
| 1446 | // size and/or an alignment if it has the right parameters. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1447 | Params = getUsualDeleteParams(OperatorDelete); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1450 | assert(!Params.DestroyingDelete && |
| 1451 | "should not call destroying delete in a new-expression"); |
| 1452 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1453 | // The second argument can be a std::size_t (for non-placement delete). |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1454 | if (Params.Size) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1455 | DeleteArgs.add(Traits::get(CGF, AllocSize), |
| 1456 | CGF.getContext().getSizeType()); |
| 1457 | |
| 1458 | // The next (second or third) argument can be a std::align_val_t, which |
| 1459 | // is an enum whose underlying type is std::size_t. |
| 1460 | // FIXME: Use the right type as the parameter type. Note that in a call |
| 1461 | // to operator delete(size_t, ...), we may not have it available. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1462 | if (Params.Alignment) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1463 | DeleteArgs.add(RValue::get(llvm::ConstantInt::get( |
| 1464 | CGF.SizeTy, AllocAlign.getQuantity())), |
| 1465 | CGF.getContext().getSizeType()); |
| 1466 | |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1467 | // Pass the rest of the arguments, which must match exactly. |
| 1468 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1469 | auto Arg = getPlacementArgs()[I]; |
| 1470 | DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | // Call 'operator delete'. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1474 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1475 | } |
| 1476 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1477 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1478 | |
| 1479 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 1480 | /// new-expression throws. |
| 1481 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 1482 | const CXXNewExpr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1483 | Address NewPtr, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1484 | llvm::Value *AllocSize, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1485 | CharUnits AllocAlign, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1486 | const CallArgList &NewArgs) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1487 | unsigned NumNonPlacementArgs = E->passAlignment() ? 2 : 1; |
| 1488 | |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1489 | // If we're not inside a conditional branch, then the cleanup will |
| 1490 | // dominate and we can do the easier (and more efficient) thing. |
| 1491 | if (!CGF.isInConditionalBranch()) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1492 | struct DirectCleanupTraits { |
| 1493 | typedef llvm::Value *ValueTy; |
| 1494 | typedef RValue RValueTy; |
| 1495 | static RValue get(CodeGenFunction &, ValueTy V) { return RValue::get(V); } |
| 1496 | static RValue get(CodeGenFunction &, RValueTy V) { return V; } |
| 1497 | }; |
| 1498 | |
| 1499 | typedef CallDeleteDuringNew<DirectCleanupTraits> DirectCleanup; |
| 1500 | |
| 1501 | DirectCleanup *Cleanup = CGF.EHStack |
| 1502 | .pushCleanupWithExtra<DirectCleanup>(EHCleanup, |
| 1503 | E->getNumPlacementArgs(), |
| 1504 | E->getOperatorDelete(), |
| 1505 | NewPtr.getPointer(), |
| 1506 | AllocSize, |
| 1507 | E->passAlignment(), |
| 1508 | AllocAlign); |
| 1509 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { |
| 1510 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1511 | Cleanup->setPlacementArg(I, Arg.getRValue(CGF), Arg.Ty); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1512 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1513 | |
| 1514 | return; |
| 1515 | } |
| 1516 | |
| 1517 | // Otherwise, we need to save all this stuff. |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1518 | DominatingValue<RValue>::saved_type SavedNewPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1519 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr.getPointer())); |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1520 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 1521 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1522 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1523 | struct ConditionalCleanupTraits { |
| 1524 | typedef DominatingValue<RValue>::saved_type ValueTy; |
| 1525 | typedef DominatingValue<RValue>::saved_type RValueTy; |
| 1526 | static RValue get(CodeGenFunction &CGF, ValueTy V) { |
| 1527 | return V.restore(CGF); |
| 1528 | } |
| 1529 | }; |
| 1530 | typedef CallDeleteDuringNew<ConditionalCleanupTraits> ConditionalCleanup; |
| 1531 | |
| 1532 | ConditionalCleanup *Cleanup = CGF.EHStack |
| 1533 | .pushCleanupWithExtra<ConditionalCleanup>(EHCleanup, |
| 1534 | E->getNumPlacementArgs(), |
| 1535 | E->getOperatorDelete(), |
| 1536 | SavedNewPtr, |
| 1537 | SavedAllocSize, |
| 1538 | E->passAlignment(), |
| 1539 | AllocAlign); |
| 1540 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { |
| 1541 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1542 | Cleanup->setPlacementArg( |
| 1543 | I, DominatingValue<RValue>::save(CGF, Arg.getRValue(CGF)), Arg.Ty); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1544 | } |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1545 | |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1546 | CGF.initFullExprCleanup(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1549 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1550 | // The element type being allocated. |
| 1551 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1552 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1553 | // 1. Build a call to the allocation function. |
| 1554 | FunctionDecl *allocator = E->getOperatorNew(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1555 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1556 | // If there is a brace-initializer, cannot allocate fewer elements than inits. |
| 1557 | unsigned minElements = 0; |
| 1558 | if (E->isArray() && E->hasInitializer()) { |
Richard Smith | 0511d23 | 2016-10-05 22:41:02 +0000 | [diff] [blame] | 1559 | const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()); |
| 1560 | if (ILE && ILE->isStringLiteralInit()) |
| 1561 | minElements = |
| 1562 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
| 1563 | ->getSize().getZExtValue(); |
| 1564 | else if (ILE) |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1565 | minElements = ILE->getNumInits(); |
| 1566 | } |
| 1567 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1568 | llvm::Value *numElements = nullptr; |
| 1569 | llvm::Value *allocSizeWithoutCookie = nullptr; |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1570 | llvm::Value *allocSize = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1571 | EmitCXXNewAllocSize(*this, E, minElements, numElements, |
| 1572 | allocSizeWithoutCookie); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1573 | CharUnits allocAlign = getContext().getTypeAlignInChars(allocType); |
Alexey Samsonov | cbe875a | 2014-08-28 00:22:11 +0000 | [diff] [blame] | 1574 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1575 | // Emit the allocation call. If the allocator is a global placement |
| 1576 | // operator, just "inline" it directly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1577 | Address allocation = Address::invalid(); |
| 1578 | CallArgList allocatorArgs; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1579 | if (allocator->isReservedGlobalPlacementOperator()) { |
John McCall | 53dcf94 | 2015-09-29 23:55:17 +0000 | [diff] [blame] | 1580 | assert(E->getNumPlacementArgs() == 1); |
| 1581 | const Expr *arg = *E->placement_arguments().begin(); |
| 1582 | |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1583 | LValueBaseInfo BaseInfo; |
| 1584 | allocation = EmitPointerWithAlignment(arg, &BaseInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1585 | |
| 1586 | // The pointer expression will, in many cases, be an opaque void*. |
| 1587 | // In these cases, discard the computed alignment and use the |
| 1588 | // formal alignment of the allocated type. |
Krzysztof Parzyszek | 8f24823 | 2017-05-18 17:07:11 +0000 | [diff] [blame] | 1589 | if (BaseInfo.getAlignmentSource() != AlignmentSource::Decl) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1590 | allocation = Address(allocation.getPointer(), allocAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1591 | |
John McCall | 53dcf94 | 2015-09-29 23:55:17 +0000 | [diff] [blame] | 1592 | // Set up allocatorArgs for the call to operator delete if it's not |
| 1593 | // the reserved global operator. |
| 1594 | if (E->getOperatorDelete() && |
| 1595 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
| 1596 | allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType()); |
| 1597 | allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType()); |
| 1598 | } |
| 1599 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1600 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1601 | const FunctionProtoType *allocatorType = |
| 1602 | allocator->getType()->castAs<FunctionProtoType>(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1603 | unsigned ParamsToSkip = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1604 | |
| 1605 | // The allocation size is the first argument. |
| 1606 | QualType sizeType = getContext().getSizeType(); |
| 1607 | allocatorArgs.add(RValue::get(allocSize), sizeType); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1608 | ++ParamsToSkip; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1609 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1610 | if (allocSize != allocSizeWithoutCookie) { |
| 1611 | CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI. |
| 1612 | allocAlign = std::max(allocAlign, cookieAlign); |
| 1613 | } |
| 1614 | |
| 1615 | // The allocation alignment may be passed as the second argument. |
| 1616 | if (E->passAlignment()) { |
| 1617 | QualType AlignValT = sizeType; |
| 1618 | if (allocatorType->getNumParams() > 1) { |
| 1619 | AlignValT = allocatorType->getParamType(1); |
| 1620 | assert(getContext().hasSameUnqualifiedType( |
| 1621 | AlignValT->castAs<EnumType>()->getDecl()->getIntegerType(), |
| 1622 | sizeType) && |
| 1623 | "wrong type for alignment parameter"); |
| 1624 | ++ParamsToSkip; |
| 1625 | } else { |
| 1626 | // Corner case, passing alignment to 'operator new(size_t, ...)'. |
| 1627 | assert(allocator->isVariadic() && "can't pass alignment to allocator"); |
| 1628 | } |
| 1629 | allocatorArgs.add( |
| 1630 | RValue::get(llvm::ConstantInt::get(SizeTy, allocAlign.getQuantity())), |
| 1631 | AlignValT); |
| 1632 | } |
| 1633 | |
| 1634 | // FIXME: Why do we not pass a CalleeDecl here? |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1635 | EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(), |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 1636 | /*AC*/AbstractCallee(), /*ParamsToSkip*/ParamsToSkip); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1637 | |
| 1638 | RValue RV = |
| 1639 | EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); |
| 1640 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1641 | // If this was a call to a global replaceable allocation function that does |
| 1642 | // not take an alignment argument, the allocator is known to produce |
| 1643 | // storage that's suitably aligned for any object that fits, up to a known |
| 1644 | // threshold. Otherwise assume it's suitably aligned for the allocated type. |
| 1645 | CharUnits allocationAlign = allocAlign; |
| 1646 | if (!E->passAlignment() && |
| 1647 | allocator->isReplaceableGlobalAllocationFunction()) { |
| 1648 | unsigned AllocatorAlign = llvm::PowerOf2Floor(std::min<uint64_t>( |
| 1649 | Target.getNewAlign(), getContext().getTypeSize(allocType))); |
| 1650 | allocationAlign = std::max( |
| 1651 | allocationAlign, getContext().toCharUnitsFromBits(AllocatorAlign)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | allocation = Address(RV.getScalarVal(), allocationAlign); |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1655 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1656 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1657 | // Emit a null check on the allocation result if the allocation |
| 1658 | // function is allowed to return null (because it has a non-throwing |
Richard Smith | 902a023 | 2015-02-14 01:52:20 +0000 | [diff] [blame] | 1659 | // exception spec or is the reserved placement new) and we have an |
Richard Smith | 2f72a75 | 2019-01-10 00:03:29 +0000 | [diff] [blame] | 1660 | // interesting initializer will be running sanitizers on the initialization. |
Bruno Ricci | 9b6dfac | 2019-01-07 15:04:45 +0000 | [diff] [blame] | 1661 | bool nullCheck = E->shouldNullCheckAllocation() && |
Richard Smith | 2f72a75 | 2019-01-10 00:03:29 +0000 | [diff] [blame] | 1662 | (!allocType.isPODType(getContext()) || E->hasInitializer() || |
| 1663 | sanitizePerformTypeCheck()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1664 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1665 | llvm::BasicBlock *nullCheckBB = nullptr; |
| 1666 | llvm::BasicBlock *contBB = nullptr; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1667 | |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1668 | // The null-check means that the initializer is conditionally |
| 1669 | // evaluated. |
| 1670 | ConditionalEvaluation conditional(*this); |
| 1671 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1672 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1673 | conditional.begin(*this); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1674 | |
| 1675 | nullCheckBB = Builder.GetInsertBlock(); |
| 1676 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1677 | contBB = createBasicBlock("new.cont"); |
| 1678 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1679 | llvm::Value *isNull = |
| 1680 | Builder.CreateIsNull(allocation.getPointer(), "new.isnull"); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1681 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1682 | EmitBlock(notNullBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1683 | } |
Anders Carlsson | f771681 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1684 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1685 | // If there's an operator delete, enter a cleanup to call it if an |
| 1686 | // exception is thrown. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1687 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1688 | llvm::Instruction *cleanupDominator = nullptr; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1689 | if (E->getOperatorDelete() && |
| 1690 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1691 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocAlign, |
| 1692 | allocatorArgs); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1693 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1694 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Eli Friedman | cf9b1f6 | 2011-09-06 18:53:03 +0000 | [diff] [blame] | 1697 | assert((allocSize == allocSizeWithoutCookie) == |
| 1698 | CalculateCookiePadding(*this, E).isZero()); |
| 1699 | if (allocSize != allocSizeWithoutCookie) { |
| 1700 | assert(E->isArray()); |
| 1701 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1702 | numElements, |
| 1703 | E, allocType); |
| 1704 | } |
| 1705 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1706 | llvm::Type *elementTy = ConvertTypeForMem(allocType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1707 | Address result = Builder.CreateElementBitCast(allocation, elementTy); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1708 | |
Piotr Padlewski | 5dde809 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 1709 | // Passing pointer through launder.invariant.group to avoid propagation of |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1710 | // vptrs information which may be included in previous type. |
Piotr Padlewski | 31fd99c | 2017-05-20 08:56:18 +0000 | [diff] [blame] | 1711 | // To not break LTO with different optimizations levels, we do it regardless |
| 1712 | // of optimization level. |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1713 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1714 | allocator->isReservedGlobalPlacementOperator()) |
Piotr Padlewski | 5dde809 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 1715 | result = Address(Builder.CreateLaunderInvariantGroup(result.getPointer()), |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1716 | result.getAlignment()); |
| 1717 | |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 1718 | // Emit sanitizer checks for pointer value now, so that in the case of an |
Richard Smith | cfa79b2 | 2019-01-23 03:37:29 +0000 | [diff] [blame] | 1719 | // array it was checked only once and not at each constructor call. We may |
| 1720 | // have already checked that the pointer is non-null. |
| 1721 | // FIXME: If we have an array cookie and a potentially-throwing allocator, |
| 1722 | // we'll null check the wrong pointer here. |
| 1723 | SanitizerSet SkippedChecks; |
| 1724 | SkippedChecks.set(SanitizerKind::Null, nullCheck); |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 1725 | EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, |
Richard Smith | cfa79b2 | 2019-01-23 03:37:29 +0000 | [diff] [blame] | 1726 | E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
| 1727 | result.getPointer(), allocType, result.getAlignment(), |
| 1728 | SkippedChecks, numElements); |
Serge Pavlov | 3760518 | 2018-07-28 15:33:03 +0000 | [diff] [blame] | 1729 | |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1730 | EmitNewInitializer(*this, E, allocType, elementTy, result, numElements, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1731 | allocSizeWithoutCookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1732 | if (E->isArray()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1733 | // NewPtr is a pointer to the base element type. If we're |
| 1734 | // allocating an array of arrays, we'll need to cast back to the |
| 1735 | // array pointer type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1736 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1737 | if (result.getType() != resultType) |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1738 | result = Builder.CreateBitCast(result, resultType); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1739 | } |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1740 | |
| 1741 | // Deactivate the 'operator delete' cleanup if we finished |
| 1742 | // initialization. |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1743 | if (operatorDeleteCleanup.isValid()) { |
| 1744 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
| 1745 | cleanupDominator->eraseFromParent(); |
| 1746 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1747 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1748 | llvm::Value *resultPtr = result.getPointer(); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1749 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1750 | conditional.end(*this); |
| 1751 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1752 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1753 | EmitBlock(contBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1754 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1755 | llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2); |
| 1756 | PHI->addIncoming(resultPtr, notNullBB); |
| 1757 | PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()), |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1758 | nullCheckBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1759 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1760 | resultPtr = PHI; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1761 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1762 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1763 | return resultPtr; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1766 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1767 | llvm::Value *Ptr, QualType DeleteTy, |
| 1768 | llvm::Value *NumElements, |
| 1769 | CharUnits CookieSize) { |
| 1770 | assert((!NumElements && CookieSize.isZero()) || |
| 1771 | DeleteFD->getOverloadedOperator() == OO_Array_Delete); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1772 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1773 | const FunctionProtoType *DeleteFTy = |
| 1774 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1775 | |
| 1776 | CallArgList DeleteArgs; |
| 1777 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1778 | auto Params = getUsualDeleteParams(DeleteFD); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1779 | auto ParamTypeIt = DeleteFTy->param_type_begin(); |
| 1780 | |
| 1781 | // Pass the pointer itself. |
| 1782 | QualType ArgTy = *ParamTypeIt++; |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1783 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1784 | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1785 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1786 | // Pass the std::destroying_delete tag if present. |
| 1787 | if (Params.DestroyingDelete) { |
| 1788 | QualType DDTag = *ParamTypeIt++; |
| 1789 | // Just pass an 'undef'. We expect the tag type to be an empty struct. |
| 1790 | auto *V = llvm::UndefValue::get(getTypes().ConvertType(DDTag)); |
| 1791 | DeleteArgs.add(RValue::get(V), DDTag); |
| 1792 | } |
| 1793 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1794 | // Pass the size if the delete function has a size_t parameter. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1795 | if (Params.Size) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1796 | QualType SizeType = *ParamTypeIt++; |
| 1797 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1798 | llvm::Value *Size = llvm::ConstantInt::get(ConvertType(SizeType), |
| 1799 | DeleteTypeSize.getQuantity()); |
| 1800 | |
| 1801 | // For array new, multiply by the number of elements. |
| 1802 | if (NumElements) |
| 1803 | Size = Builder.CreateMul(Size, NumElements); |
| 1804 | |
| 1805 | // If there is a cookie, add the cookie size. |
| 1806 | if (!CookieSize.isZero()) |
| 1807 | Size = Builder.CreateAdd( |
| 1808 | Size, llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity())); |
| 1809 | |
| 1810 | DeleteArgs.add(RValue::get(Size), SizeType); |
| 1811 | } |
| 1812 | |
| 1813 | // Pass the alignment if the delete function has an align_val_t parameter. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1814 | if (Params.Alignment) { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1815 | QualType AlignValType = *ParamTypeIt++; |
| 1816 | CharUnits DeleteTypeAlign = getContext().toCharUnitsFromBits( |
| 1817 | getContext().getTypeAlignIfKnown(DeleteTy)); |
| 1818 | llvm::Value *Align = llvm::ConstantInt::get(ConvertType(AlignValType), |
| 1819 | DeleteTypeAlign.getQuantity()); |
| 1820 | DeleteArgs.add(RValue::get(Align), AlignValType); |
| 1821 | } |
| 1822 | |
| 1823 | assert(ParamTypeIt == DeleteFTy->param_type_end() && |
| 1824 | "unknown parameter to usual delete function"); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1825 | |
| 1826 | // Emit the call to delete. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1827 | EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1830 | namespace { |
| 1831 | /// Calls the given 'operator delete' on a single object. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1832 | struct CallObjectDelete final : EHScopeStack::Cleanup { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1833 | llvm::Value *Ptr; |
| 1834 | const FunctionDecl *OperatorDelete; |
| 1835 | QualType ElementType; |
| 1836 | |
| 1837 | CallObjectDelete(llvm::Value *Ptr, |
| 1838 | const FunctionDecl *OperatorDelete, |
| 1839 | QualType ElementType) |
| 1840 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1841 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1842 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1843 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1844 | } |
| 1845 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1846 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1847 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1848 | void |
| 1849 | CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, |
| 1850 | llvm::Value *CompletePtr, |
| 1851 | QualType ElementType) { |
| 1852 | EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr, |
| 1853 | OperatorDelete, ElementType); |
| 1854 | } |
| 1855 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1856 | /// Emit the code for deleting a single object with a destroying operator |
| 1857 | /// delete. If the element type has a non-virtual destructor, Ptr has already |
| 1858 | /// been converted to the type of the parameter of 'operator delete'. Otherwise |
| 1859 | /// Ptr points to an object of the static type. |
| 1860 | static void EmitDestroyingObjectDelete(CodeGenFunction &CGF, |
| 1861 | const CXXDeleteExpr *DE, Address Ptr, |
| 1862 | QualType ElementType) { |
| 1863 | auto *Dtor = ElementType->getAsCXXRecordDecl()->getDestructor(); |
| 1864 | if (Dtor && Dtor->isVirtual()) |
| 1865 | CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, |
| 1866 | Dtor); |
| 1867 | else |
| 1868 | CGF.EmitDeleteCall(DE->getOperatorDelete(), Ptr.getPointer(), ElementType); |
| 1869 | } |
| 1870 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1871 | /// Emit the code for deleting a single object. |
| 1872 | static void EmitObjectDelete(CodeGenFunction &CGF, |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1873 | const CXXDeleteExpr *DE, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1874 | Address Ptr, |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1875 | QualType ElementType) { |
Ivan Krasin | d98f5d7 | 2016-11-17 00:39:48 +0000 | [diff] [blame] | 1876 | // C++11 [expr.delete]p3: |
| 1877 | // If the static type of the object to be deleted is different from its |
| 1878 | // dynamic type, the static type shall be a base class of the dynamic type |
| 1879 | // of the object to be deleted and the static type shall have a virtual |
| 1880 | // destructor or the behavior is undefined. |
| 1881 | CGF.EmitTypeCheck(CodeGenFunction::TCK_MemberCall, |
| 1882 | DE->getExprLoc(), Ptr.getPointer(), |
| 1883 | ElementType); |
| 1884 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1885 | const FunctionDecl *OperatorDelete = DE->getOperatorDelete(); |
| 1886 | assert(!OperatorDelete->isDestroyingOperatorDelete()); |
| 1887 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1888 | // Find the destructor for the type, if applicable. If the |
| 1889 | // destructor is virtual, we'll just emit the vcall and return. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1890 | const CXXDestructorDecl *Dtor = nullptr; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1891 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1892 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | b23533d | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 1893 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1894 | Dtor = RD->getDestructor(); |
| 1895 | |
| 1896 | if (Dtor->isVirtual()) { |
Hiroshi Yamauchi | cb30590 | 2019-08-08 18:00:49 +0000 | [diff] [blame] | 1897 | bool UseVirtualCall = true; |
| 1898 | const Expr *Base = DE->getArgument(); |
| 1899 | if (auto *DevirtualizedDtor = |
| 1900 | dyn_cast_or_null<const CXXDestructorDecl>( |
| 1901 | Dtor->getDevirtualizedMethod( |
| 1902 | Base, CGF.CGM.getLangOpts().AppleKext))) { |
| 1903 | UseVirtualCall = false; |
| 1904 | const CXXRecordDecl *DevirtualizedClass = |
| 1905 | DevirtualizedDtor->getParent(); |
| 1906 | if (declaresSameEntity(getCXXRecord(Base), DevirtualizedClass)) { |
| 1907 | // Devirtualized to the class of the base type (the type of the |
| 1908 | // whole expression). |
| 1909 | Dtor = DevirtualizedDtor; |
| 1910 | } else { |
| 1911 | // Devirtualized to some other type. Would need to cast the this |
| 1912 | // pointer to that type but we don't have support for that yet, so |
| 1913 | // do a virtual call. FIXME: handle the case where it is |
| 1914 | // devirtualized to the derived type (the type of the inner |
| 1915 | // expression) as in EmitCXXMemberOrOperatorMemberCallExpr. |
| 1916 | UseVirtualCall = true; |
| 1917 | } |
| 1918 | } |
| 1919 | if (UseVirtualCall) { |
| 1920 | CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, |
| 1921 | Dtor); |
| 1922 | return; |
| 1923 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | // Make sure that we call delete even if the dtor throws. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1929 | // This doesn't have to a conditional cleanup because we're going |
| 1930 | // to pop it off in a second. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1931 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1932 | Ptr.getPointer(), |
| 1933 | OperatorDelete, ElementType); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1934 | |
| 1935 | if (Dtor) |
| 1936 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1937 | /*ForVirtualBase=*/false, |
| 1938 | /*Delegating=*/false, |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 1939 | Ptr, ElementType); |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1940 | else if (auto Lifetime = ElementType.getObjCLifetime()) { |
| 1941 | switch (Lifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1942 | case Qualifiers::OCL_None: |
| 1943 | case Qualifiers::OCL_ExplicitNone: |
| 1944 | case Qualifiers::OCL_Autoreleasing: |
| 1945 | break; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1946 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1947 | case Qualifiers::OCL_Strong: |
| 1948 | CGF.EmitARCDestroyStrong(Ptr, ARCPreciseLifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1949 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1950 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1951 | case Qualifiers::OCL_Weak: |
| 1952 | CGF.EmitARCDestroyWeak(Ptr); |
| 1953 | break; |
| 1954 | } |
| 1955 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1956 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1957 | CGF.PopCleanupBlock(); |
| 1958 | } |
| 1959 | |
| 1960 | namespace { |
| 1961 | /// Calls the given 'operator delete' on an array of objects. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1962 | struct CallArrayDelete final : EHScopeStack::Cleanup { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1963 | llvm::Value *Ptr; |
| 1964 | const FunctionDecl *OperatorDelete; |
| 1965 | llvm::Value *NumElements; |
| 1966 | QualType ElementType; |
| 1967 | CharUnits CookieSize; |
| 1968 | |
| 1969 | CallArrayDelete(llvm::Value *Ptr, |
| 1970 | const FunctionDecl *OperatorDelete, |
| 1971 | llvm::Value *NumElements, |
| 1972 | QualType ElementType, |
| 1973 | CharUnits CookieSize) |
| 1974 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1975 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1976 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1977 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1978 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType, NumElements, |
| 1979 | CookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1980 | } |
| 1981 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1982 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1983 | |
| 1984 | /// Emit the code for deleting an array of objects. |
| 1985 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1986 | const CXXDeleteExpr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1987 | Address deletedPtr, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1988 | QualType elementType) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1989 | llvm::Value *numElements = nullptr; |
| 1990 | llvm::Value *allocatedPtr = nullptr; |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1991 | CharUnits cookieSize; |
| 1992 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
| 1993 | numElements, allocatedPtr, cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1994 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1995 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Make sure that we call delete even if one of the dtors throws. |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1998 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1999 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2000 | allocatedPtr, operatorDelete, |
| 2001 | numElements, elementType, |
| 2002 | cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2003 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2004 | // Destroy the elements. |
| 2005 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
| 2006 | assert(numElements && "no element count for a type with a destructor!"); |
| 2007 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2008 | CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); |
| 2009 | CharUnits elementAlign = |
| 2010 | deletedPtr.getAlignment().alignmentOfArrayElement(elementSize); |
| 2011 | |
| 2012 | llvm::Value *arrayBegin = deletedPtr.getPointer(); |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2013 | llvm::Value *arrayEnd = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2014 | CGF.Builder.CreateInBoundsGEP(arrayBegin, numElements, "delete.end"); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2015 | |
| 2016 | // Note that it is legal to allocate a zero-length array, and we |
| 2017 | // can never fold the check away because the length should always |
| 2018 | // come from a cookie. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2019 | CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2020 | CGF.getDestroyer(dtorKind), |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2021 | /*checkZeroLength*/ true, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2022 | CGF.needsEHCleanup(dtorKind)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 2025 | // Pop the cleanup block. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2026 | CGF.PopCleanupBlock(); |
| 2027 | } |
| 2028 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2029 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2030 | const Expr *Arg = E->getArgument(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2031 | Address Ptr = EmitPointerWithAlignment(Arg); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2032 | |
| 2033 | // Null check the pointer. |
| 2034 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 2035 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 2036 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2037 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2038 | |
| 2039 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 2040 | EmitBlock(DeleteNotNull); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 2041 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2042 | QualType DeleteTy = E->getDestroyedType(); |
| 2043 | |
| 2044 | // A destroying operator delete overrides the entire operation of the |
| 2045 | // delete expression. |
| 2046 | if (E->getOperatorDelete()->isDestroyingOperatorDelete()) { |
| 2047 | EmitDestroyingObjectDelete(*this, E, Ptr, DeleteTy); |
| 2048 | EmitBlock(DeleteEnd); |
| 2049 | return; |
| 2050 | } |
| 2051 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2052 | // We might be deleting a pointer to array. If so, GEP down to the |
| 2053 | // first non-array element. |
| 2054 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2055 | if (DeleteTy->isConstantArrayType()) { |
| 2056 | llvm::Value *Zero = Builder.getInt32(0); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2057 | SmallVector<llvm::Value*,8> GEP; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2058 | |
| 2059 | GEP.push_back(Zero); // point at the outermost array |
| 2060 | |
| 2061 | // For each layer of array type we're pointing at: |
| 2062 | while (const ConstantArrayType *Arr |
| 2063 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 2064 | // 1. Unpeel the array type. |
| 2065 | DeleteTy = Arr->getElementType(); |
| 2066 | |
| 2067 | // 2. GEP to the first element of the array. |
| 2068 | GEP.push_back(Zero); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2069 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2070 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2071 | Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getPointer(), GEP, "del.first"), |
| 2072 | Ptr.getAlignment()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2075 | assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 2076 | |
Reid Kleckner | 7270ef5 | 2015-03-19 17:03:58 +0000 | [diff] [blame] | 2077 | if (E->isArrayForm()) { |
| 2078 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
| 2079 | } else { |
| 2080 | EmitObjectDelete(*this, E, Ptr, DeleteTy); |
| 2081 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2082 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 2083 | EmitBlock(DeleteEnd); |
| 2084 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2085 | |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 2086 | static bool isGLValueFromPointerDeref(const Expr *E) { |
| 2087 | E = E->IgnoreParens(); |
| 2088 | |
| 2089 | if (const auto *CE = dyn_cast<CastExpr>(E)) { |
| 2090 | if (!CE->getSubExpr()->isGLValue()) |
| 2091 | return false; |
| 2092 | return isGLValueFromPointerDeref(CE->getSubExpr()); |
| 2093 | } |
| 2094 | |
| 2095 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 2096 | return isGLValueFromPointerDeref(OVE->getSourceExpr()); |
| 2097 | |
| 2098 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) |
| 2099 | if (BO->getOpcode() == BO_Comma) |
| 2100 | return isGLValueFromPointerDeref(BO->getRHS()); |
| 2101 | |
| 2102 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E)) |
| 2103 | return isGLValueFromPointerDeref(ACO->getTrueExpr()) || |
| 2104 | isGLValueFromPointerDeref(ACO->getFalseExpr()); |
| 2105 | |
| 2106 | // C++11 [expr.sub]p1: |
| 2107 | // The expression E1[E2] is identical (by definition) to *((E1)+(E2)) |
| 2108 | if (isa<ArraySubscriptExpr>(E)) |
| 2109 | return true; |
| 2110 | |
| 2111 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) |
| 2112 | if (UO->getOpcode() == UO_Deref) |
| 2113 | return true; |
| 2114 | |
| 2115 | return false; |
| 2116 | } |
| 2117 | |
Warren Hunt | 747e301 | 2014-06-18 21:15:55 +0000 | [diff] [blame] | 2118 | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2119 | llvm::Type *StdTypeInfoPtrTy) { |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2120 | // Get the vtable pointer. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2121 | Address ThisPtr = CGF.EmitLValue(E).getAddress(CGF); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2122 | |
Stephan Bergmann | d71ad17 | 2017-12-28 12:45:41 +0000 | [diff] [blame] | 2123 | QualType SrcRecordTy = E->getType(); |
| 2124 | |
| 2125 | // C++ [class.cdtor]p4: |
| 2126 | // If the operand of typeid refers to the object under construction or |
| 2127 | // destruction and the static type of the operand is neither the constructor |
| 2128 | // or destructor’s class nor one of its bases, the behavior is undefined. |
| 2129 | CGF.EmitTypeCheck(CodeGenFunction::TCK_DynamicOperation, E->getExprLoc(), |
| 2130 | ThisPtr.getPointer(), SrcRecordTy); |
| 2131 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2132 | // C++ [expr.typeid]p2: |
| 2133 | // If the glvalue expression is obtained by applying the unary * operator to |
| 2134 | // a pointer and the pointer is a null pointer value, the typeid expression |
| 2135 | // throws the std::bad_typeid exception. |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 2136 | // |
| 2137 | // However, this paragraph's intent is not clear. We choose a very generous |
| 2138 | // interpretation which implores us to consider comma operators, conditional |
| 2139 | // operators, parentheses and other such constructs. |
David Majnemer | 1c3d95e | 2014-07-19 00:17:06 +0000 | [diff] [blame] | 2140 | if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked( |
| 2141 | isGLValueFromPointerDeref(E), SrcRecordTy)) { |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2142 | llvm::BasicBlock *BadTypeidBlock = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2143 | CGF.createBasicBlock("typeid.bad_typeid"); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2144 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end"); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2145 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2146 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2147 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2148 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2149 | CGF.EmitBlock(BadTypeidBlock); |
| 2150 | CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF); |
| 2151 | CGF.EmitBlock(EndBlock); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2154 | return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr, |
| 2155 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 2158 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2159 | llvm::Type *StdTypeInfoPtrTy = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2160 | ConvertType(E->getType())->getPointerTo(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2161 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2162 | if (E->isTypeOperand()) { |
David Majnemer | 143c55e | 2013-09-27 07:04:31 +0000 | [diff] [blame] | 2163 | llvm::Constant *TypeInfo = |
| 2164 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2165 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2166 | } |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 2167 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2168 | // C++ [expr.typeid]p2: |
| 2169 | // When typeid is applied to a glvalue expression whose type is a |
| 2170 | // polymorphic class type, the result refers to a std::type_info object |
| 2171 | // representing the type of the most derived object (that is, the dynamic |
| 2172 | // type) to which the glvalue refers. |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 2173 | if (E->isPotentiallyEvaluated()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2174 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 2175 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 2176 | |
| 2177 | QualType OperandTy = E->getExprOperand()->getType(); |
| 2178 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
| 2179 | StdTypeInfoPtrTy); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 2180 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2181 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2182 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 2183 | QualType DestTy) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2184 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2185 | if (DestTy->isPointerType()) |
| 2186 | return llvm::Constant::getNullValue(DestLTy); |
| 2187 | |
| 2188 | /// C++ [expr.dynamic.cast]p9: |
| 2189 | /// A failed cast to reference type throws std::bad_cast |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2190 | if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF)) |
| 2191 | return nullptr; |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2192 | |
| 2193 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 2194 | return llvm::UndefValue::get(DestLTy); |
| 2195 | } |
| 2196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2197 | llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2198 | const CXXDynamicCastExpr *DCE) { |
Alexey Bataev | 2bf9b4c | 2015-10-20 04:24:12 +0000 | [diff] [blame] | 2199 | CGM.EmitExplicitCastExprType(DCE, this); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2200 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2201 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 2202 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 2203 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2204 | // C++ [expr.dynamic.cast]p7: |
| 2205 | // If T is "pointer to cv void," then the result is a pointer to the most |
| 2206 | // derived object pointed to by v. |
| 2207 | const PointerType *DestPTy = DestTy->getAs<PointerType>(); |
| 2208 | |
| 2209 | bool isDynamicCastToVoid; |
| 2210 | QualType SrcRecordTy; |
| 2211 | QualType DestRecordTy; |
| 2212 | if (DestPTy) { |
| 2213 | isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType(); |
| 2214 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 2215 | DestRecordTy = DestPTy->getPointeeType(); |
| 2216 | } else { |
| 2217 | isDynamicCastToVoid = false; |
| 2218 | SrcRecordTy = SrcTy; |
| 2219 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 2220 | } |
| 2221 | |
Stephan Bergmann | d71ad17 | 2017-12-28 12:45:41 +0000 | [diff] [blame] | 2222 | // C++ [class.cdtor]p5: |
| 2223 | // If the operand of the dynamic_cast refers to the object under |
| 2224 | // construction or destruction and the static type of the operand is not a |
| 2225 | // pointer to or object of the constructor or destructor’s own class or one |
| 2226 | // of its bases, the dynamic_cast results in undefined behavior. |
| 2227 | EmitTypeCheck(TCK_DynamicOperation, DCE->getExprLoc(), ThisAddr.getPointer(), |
| 2228 | SrcRecordTy); |
| 2229 | |
| 2230 | if (DCE->isAlwaysNull()) |
| 2231 | if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy)) |
| 2232 | return T; |
| 2233 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2234 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 2235 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2236 | // C++ [expr.dynamic.cast]p4: |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2237 | // If the value of v is a null pointer value in the pointer case, the result |
| 2238 | // is the null pointer value of type T. |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2239 | bool ShouldNullCheckSrcValue = |
| 2240 | CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(), |
| 2241 | SrcRecordTy); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2242 | |
| 2243 | llvm::BasicBlock *CastNull = nullptr; |
| 2244 | llvm::BasicBlock *CastNotNull = nullptr; |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2245 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2246 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2247 | if (ShouldNullCheckSrcValue) { |
| 2248 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 2249 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 2250 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2251 | llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer()); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2252 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 2253 | EmitBlock(CastNotNull); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2256 | llvm::Value *Value; |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2257 | if (isDynamicCastToVoid) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2258 | Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2259 | DestTy); |
| 2260 | } else { |
| 2261 | assert(DestRecordTy->isRecordType() && |
| 2262 | "destination type must be a record type!"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2263 | Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2264 | DestTy, DestRecordTy, CastEnd); |
David Majnemer | 67528ea | 2015-11-23 03:01:14 +0000 | [diff] [blame] | 2265 | CastNotNull = Builder.GetInsertBlock(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 2266 | } |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 2267 | |
| 2268 | if (ShouldNullCheckSrcValue) { |
| 2269 | EmitBranch(CastEnd); |
| 2270 | |
| 2271 | EmitBlock(CastNull); |
| 2272 | EmitBranch(CastEnd); |
| 2273 | } |
| 2274 | |
| 2275 | EmitBlock(CastEnd); |
| 2276 | |
| 2277 | if (ShouldNullCheckSrcValue) { |
| 2278 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 2279 | PHI->addIncoming(Value, CastNotNull); |
| 2280 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 2281 | |
| 2282 | Value = PHI; |
| 2283 | } |
| 2284 | |
| 2285 | return Value; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 2286 | } |