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