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