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