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