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