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