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