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