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 | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Intrinsics.h" |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CallSite.h" |
| 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); |
| 89 | return EmitCall(getContext().getPointerType(MD->getType()), Callee, |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 90 | CE->getLocStart(), ReturnValue, CE->arg_begin(), |
| 91 | CE->arg_end()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 92 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 93 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 94 | // Compute the object pointer. |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 95 | const Expr *Base = ME->getBase(); |
| 96 | bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 97 | |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 98 | const CXXMethodDecl *DevirtualizedMethod = NULL; |
Benjamin Kramer | 7463ed7 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 99 | if (CanUseVirtualCall && CanDevirtualizeMemberFunctionCall(Base, MD)) { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 100 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
| 101 | DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); |
| 102 | assert(DevirtualizedMethod); |
| 103 | const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); |
| 104 | const Expr *Inner = Base->ignoreParenBaseCasts(); |
| 105 | if (getCXXRecord(Inner) == DevirtualizedClass) |
| 106 | // If the class of the Inner expression is where the dynamic method |
| 107 | // is defined, build the this pointer from it. |
| 108 | Base = Inner; |
| 109 | else if (getCXXRecord(Base) != DevirtualizedClass) { |
| 110 | // If the method is defined in a class that is not the best dynamic |
| 111 | // one or the one of the full expression, we would have to build |
| 112 | // a derived-to-base cast to compute the correct this pointer, but |
| 113 | // we don't have support for that yet, so do a virtual call. |
| 114 | DevirtualizedMethod = NULL; |
| 115 | } |
Rafael Espindola | b27564a | 2012-06-28 17:57:36 +0000 | [diff] [blame] | 116 | // If the return types are not the same, this might be a case where more |
| 117 | // code needs to run to compensate for it. For example, the derived |
| 118 | // method might return a type that inherits form from the return |
| 119 | // type of MD and has a prefix. |
| 120 | // For now we just avoid devirtualizing these covariant cases. |
| 121 | if (DevirtualizedMethod && |
| 122 | DevirtualizedMethod->getResultType().getCanonicalType() != |
| 123 | MD->getResultType().getCanonicalType()) |
Rafael Espindola | debc71c | 2012-06-28 15:11:39 +0000 | [diff] [blame] | 124 | DevirtualizedMethod = NULL; |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 125 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 126 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 127 | llvm::Value *This; |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 128 | if (ME->isArrow()) |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 129 | This = EmitScalarExpr(Base); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 130 | else |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 131 | This = EmitLValue(Base).getAddress(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 132 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 133 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 134 | if (MD->isTrivial()) { |
| 135 | if (isa<CXXDestructorDecl>(MD)) return RValue::get(0); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 136 | if (isa<CXXConstructorDecl>(MD) && |
| 137 | cast<CXXConstructorDecl>(MD)->isDefaultConstructor()) |
| 138 | return RValue::get(0); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 139 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 140 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) { |
| 141 | // We don't like to generate the trivial copy/move assignment operator |
| 142 | // when it isn't necessary; just produce the proper effect here. |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 143 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
Benjamin Kramer | 1ca6691 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 144 | EmitAggregateAssign(This, RHS, CE->getType()); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 145 | return RValue::get(This); |
| 146 | } |
| 147 | |
| 148 | if (isa<CXXConstructorDecl>(MD) && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 149 | cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) { |
| 150 | // Trivial move and copy ctor are the same. |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 151 | llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); |
| 152 | EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS, |
| 153 | CE->arg_begin(), CE->arg_end()); |
| 154 | return RValue::get(This); |
| 155 | } |
| 156 | llvm_unreachable("unknown trivial member function"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 157 | } |
| 158 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 159 | // Compute the function type we're calling. |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 160 | const CXXMethodDecl *CalleeDecl = DevirtualizedMethod ? DevirtualizedMethod : MD; |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 161 | const CGFunctionInfo *FInfo = 0; |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 162 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) |
| 163 | FInfo = &CGM.getTypes().arrangeCXXDestructor(Dtor, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 164 | Dtor_Complete); |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 165 | else if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl)) |
| 166 | FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, |
| 167 | Ctor_Complete); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 168 | else |
Eli Friedman | ade6097 | 2012-10-25 00:12:49 +0000 | [diff] [blame] | 169 | FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 170 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 171 | llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 172 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 173 | // C++ [class.virtual]p12: |
| 174 | // Explicit qualification with the scope operator (5.1) suppresses the |
| 175 | // virtual call mechanism. |
| 176 | // |
| 177 | // We also don't emit a virtual call if the base expression has a record type |
| 178 | // because then we know what the type is. |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 179 | bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; |
Stephen Lin | 19cee18 | 2013-06-19 23:23:19 +0000 | [diff] [blame] | 180 | llvm::Value *Callee; |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 181 | |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 182 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 183 | assert(CE->arg_begin() == CE->arg_end() && |
| 184 | "Destructor shouldn't have explicit parameters"); |
| 185 | assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 186 | if (UseVirtualCall) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 187 | CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, Dtor_Complete, |
| 188 | CE->getExprLoc(), This); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 189 | } else { |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 190 | if (getLangOpts().AppleKext && |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 191 | MD->isVirtual() && |
| 192 | ME->hasQualifier()) |
Fariborz Jahanian | 7f6f81b | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 193 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 194 | else if (!DevirtualizedMethod) |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 195 | Callee = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete, FInfo, Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 196 | else { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 197 | const CXXDestructorDecl *DDtor = |
| 198 | cast<CXXDestructorDecl>(DevirtualizedMethod); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 199 | Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty); |
| 200 | } |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 201 | EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This, |
| 202 | /*ImplicitParam=*/0, QualType(), 0, 0); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 203 | } |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 204 | return RValue::get(0); |
| 205 | } |
| 206 | |
| 207 | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 208 | Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 209 | } else if (UseVirtualCall) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 210 | Callee = CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 211 | } else { |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 212 | if (getLangOpts().AppleKext && |
Fariborz Jahanian | 9f9438b | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 213 | MD->isVirtual() && |
Fariborz Jahanian | 252a47f | 2011-01-21 01:04:41 +0000 | [diff] [blame] | 214 | ME->hasQualifier()) |
Fariborz Jahanian | 7f6f81b | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 215 | Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty); |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 216 | else if (!DevirtualizedMethod) |
Rafael Espindola | 727a771 | 2012-06-26 19:18:25 +0000 | [diff] [blame] | 217 | Callee = CGM.GetAddrOfFunction(MD, Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 218 | else { |
Rafael Espindola | 3b33c4e | 2012-06-28 14:28:57 +0000 | [diff] [blame] | 219 | Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty); |
Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 220 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 223 | if (MD->isVirtual()) |
| 224 | This = CGM.getCXXABI().adjustThisArgumentForVirtualCall(*this, MD, This); |
| 225 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 226 | return EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This, |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 227 | /*ImplicitParam=*/0, QualType(), |
| 228 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | RValue |
| 232 | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
| 233 | ReturnValueSlot ReturnValue) { |
| 234 | const BinaryOperator *BO = |
| 235 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
| 236 | const Expr *BaseExpr = BO->getLHS(); |
| 237 | const Expr *MemFnExpr = BO->getRHS(); |
| 238 | |
| 239 | const MemberPointerType *MPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 240 | MemFnExpr->getType()->castAs<MemberPointerType>(); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 242 | const FunctionProtoType *FPT = |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 243 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 244 | const CXXRecordDecl *RD = |
| 245 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 246 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 247 | // Get the member function pointer. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 248 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 249 | |
| 250 | // Emit the 'this' pointer. |
| 251 | llvm::Value *This; |
| 252 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 253 | if (BO->getOpcode() == BO_PtrMemI) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 254 | This = EmitScalarExpr(BaseExpr); |
| 255 | else |
| 256 | This = EmitLValue(BaseExpr).getAddress(); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 257 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 258 | EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This, |
| 259 | QualType(MPT->getClass(), 0)); |
Richard Smith | 69d0d26 | 2012-08-24 00:54:33 +0000 | [diff] [blame] | 260 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 261 | // Ask the ABI to load the callee. Note that This is modified. |
| 262 | llvm::Value *Callee = |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 263 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 264 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 265 | CallArgList Args; |
| 266 | |
| 267 | QualType ThisType = |
| 268 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
| 269 | |
| 270 | // Push the this ptr. |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 271 | Args.add(RValue::get(This), ThisType); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 272 | |
| 273 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 274 | |
| 275 | // And the rest of the call args |
| 276 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 277 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), |
| 278 | Callee, ReturnValue, Args); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | RValue |
| 282 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 283 | const CXXMethodDecl *MD, |
| 284 | ReturnValueSlot ReturnValue) { |
| 285 | assert(MD->isInstance() && |
| 286 | "Trying to emit a member call expr on a static method!"); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 287 | LValue LV = EmitLValue(E->getArg(0)); |
| 288 | llvm::Value *This = LV.getAddress(); |
| 289 | |
Douglas Gregor | 146b8e9 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 290 | if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && |
| 291 | MD->isTrivial()) { |
| 292 | llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); |
| 293 | QualType Ty = E->getType(); |
Benjamin Kramer | 1ca6691 | 2012-09-30 12:43:37 +0000 | [diff] [blame] | 294 | EmitAggregateAssign(This, Src, Ty); |
Douglas Gregor | 146b8e9 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 295 | return RValue::get(This); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Anders Carlsson | c36783e | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 298 | llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This); |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 299 | return EmitCXXMemberCall(MD, E->getExprLoc(), Callee, ReturnValue, This, |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 300 | /*ImplicitParam=*/0, QualType(), |
| 301 | E->arg_begin() + 1, E->arg_end()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 304 | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
| 305 | ReturnValueSlot ReturnValue) { |
| 306 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
| 307 | } |
| 308 | |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 309 | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
| 310 | llvm::Value *DestPtr, |
| 311 | const CXXRecordDecl *Base) { |
| 312 | if (Base->isEmpty()) |
| 313 | return; |
| 314 | |
| 315 | DestPtr = CGF.EmitCastToVoidPtr(DestPtr); |
| 316 | |
| 317 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
| 318 | CharUnits Size = Layout.getNonVirtualSize(); |
| 319 | CharUnits Align = Layout.getNonVirtualAlign(); |
| 320 | |
| 321 | llvm::Value *SizeVal = CGF.CGM.getSize(Size); |
| 322 | |
| 323 | // If the type contains a pointer to data member we can't memset it to zero. |
| 324 | // Instead, create a null constant and copy it to the destination. |
| 325 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 326 | // like -1, which happens to be the pattern used by member-pointers. |
| 327 | // TODO: isZeroInitializable can be over-conservative in the case where a |
| 328 | // virtual base contains a member pointer. |
| 329 | if (!CGF.CGM.getTypes().isZeroInitializable(Base)) { |
| 330 | llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base); |
| 331 | |
| 332 | llvm::GlobalVariable *NullVariable = |
| 333 | new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(), |
| 334 | /*isConstant=*/true, |
| 335 | llvm::GlobalVariable::PrivateLinkage, |
| 336 | NullConstant, Twine()); |
| 337 | NullVariable->setAlignment(Align.getQuantity()); |
| 338 | llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable); |
| 339 | |
| 340 | // Get and call the appropriate llvm.memcpy overload. |
| 341 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity()); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // Otherwise, just memset the whole thing to zero. This is legal |
| 346 | // because in LLVM, all default initializers (other than the ones we just |
| 347 | // handled above) are guaranteed to have a bit pattern of all zeros. |
| 348 | CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal, |
| 349 | Align.getQuantity()); |
| 350 | } |
| 351 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 352 | void |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 353 | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
| 354 | AggValueSlot Dest) { |
| 355 | assert(!Dest.isIgnored() && "Must have a destination!"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 356 | const CXXConstructorDecl *CD = E->getConstructor(); |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 357 | |
| 358 | // If we require zero initialization before (or instead of) calling the |
| 359 | // constructor, as can be the case with a non-user-provided default |
Argyrios Kyrtzidis | 0353526 | 2011-04-28 22:57:55 +0000 | [diff] [blame] | 360 | // constructor, emit the zero initialization now, unless destination is |
| 361 | // already zeroed. |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 362 | if (E->requiresZeroInitialization() && !Dest.isZeroed()) { |
| 363 | switch (E->getConstructionKind()) { |
| 364 | case CXXConstructExpr::CK_Delegating: |
Eli Friedman | fde961d | 2011-10-14 02:27:24 +0000 | [diff] [blame] | 365 | case CXXConstructExpr::CK_Complete: |
| 366 | EmitNullInitialization(Dest.getAddr(), E->getType()); |
| 367 | break; |
| 368 | case CXXConstructExpr::CK_VirtualBase: |
| 369 | case CXXConstructExpr::CK_NonVirtualBase: |
| 370 | EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent()); |
| 371 | break; |
| 372 | } |
| 373 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 374 | |
| 375 | // If this is a call to a trivial default constructor, do nothing. |
| 376 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
| 377 | return; |
| 378 | |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 379 | // Elide the constructor if we're constructing from a temporary. |
| 380 | // The temporary check is required because Sema sets this on NRVO |
| 381 | // returns. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 382 | if (getLangOpts().ElideConstructors && E->isElidable()) { |
John McCall | 8ea46b6 | 2010-09-18 00:58:34 +0000 | [diff] [blame] | 383 | assert(getContext().hasSameUnqualifiedType(E->getType(), |
| 384 | E->getArg(0)->getType())); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 385 | if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { |
| 386 | EmitAggExpr(E->getArg(0), Dest); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 389 | } |
Douglas Gregor | 630c76e | 2010-08-22 16:15:35 +0000 | [diff] [blame] | 390 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 391 | if (const ConstantArrayType *arrayType |
| 392 | = getContext().getAsConstantArrayType(E->getType())) { |
| 393 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 394 | E->arg_begin(), E->arg_end()); |
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(), |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 421 | E->arg_begin(), E->arg_end()); |
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 | |
| 587 | llvm::Value *hasOverflow = 0; |
| 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) { |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 723 | |
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: |
Eli Friedman | 38cd36d | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 727 | CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 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 |
| 750 | CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 751 | QualType elementType, |
| 752 | llvm::Value *beginPtr, |
| 753 | llvm::Value *numElements) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 754 | if (!E->hasInitializer()) |
| 755 | return; // We have a POD type. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 756 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 757 | llvm::Value *explicitPtr = beginPtr; |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 758 | // Find the end of the array, hoisted out of the loop. |
| 759 | llvm::Value *endPtr = |
| 760 | Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end"); |
| 761 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 762 | unsigned initializerElements = 0; |
| 763 | |
| 764 | const Expr *Init = E->getInitializer(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 765 | llvm::AllocaInst *endOfInit = 0; |
| 766 | QualType::DestructionKind dtorKind = elementType.isDestructedType(); |
| 767 | EHScopeStack::stable_iterator cleanup; |
| 768 | llvm::Instruction *cleanupDominator = 0; |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 769 | // If the initializer is an initializer list, first do the explicit elements. |
| 770 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 771 | initializerElements = ILE->getNumInits(); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 772 | |
| 773 | // Enter a partial-destruction cleanup if necessary. |
| 774 | if (needsEHCleanup(dtorKind)) { |
| 775 | // In principle we could tell the cleanup where we are more |
| 776 | // directly, but the control flow can get so varied here that it |
| 777 | // would actually be quite complex. Therefore we go through an |
| 778 | // alloca. |
| 779 | endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit"); |
| 780 | cleanupDominator = Builder.CreateStore(beginPtr, endOfInit); |
| 781 | pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType, |
| 782 | getDestroyer(dtorKind)); |
| 783 | cleanup = EHStack.stable_begin(); |
| 784 | } |
| 785 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 786 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 787 | // Tell the cleanup that it needs to destroy up to this |
| 788 | // element. TODO: some of these stores can be trivially |
| 789 | // observed to be unnecessary. |
| 790 | if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 791 | StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr); |
| 792 | explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next"); |
| 793 | } |
| 794 | |
| 795 | // The remaining elements are filled with the array filler expression. |
| 796 | Init = ILE->getArrayFiller(); |
| 797 | } |
| 798 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 799 | // Create the continuation block. |
| 800 | llvm::BasicBlock *contBB = createBasicBlock("new.loop.end"); |
| 801 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 802 | // If the number of elements isn't constant, we have to now check if there is |
| 803 | // anything left to initialize. |
| 804 | if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) { |
| 805 | // If all elements have already been initialized, skip the whole loop. |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 806 | if (constNum->getZExtValue() <= initializerElements) { |
| 807 | // If there was a cleanup, deactivate it. |
| 808 | if (cleanupDominator) |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 809 | DeactivateCleanupBlock(cleanup, cleanupDominator); |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 810 | return; |
| 811 | } |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 812 | } else { |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 813 | llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty"); |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 814 | llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 815 | "array.isempty"); |
| 816 | Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB); |
| 817 | EmitBlock(nonEmptyBB); |
| 818 | } |
| 819 | |
| 820 | // Enter the loop. |
| 821 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 822 | llvm::BasicBlock *loopBB = createBasicBlock("new.loop"); |
| 823 | |
| 824 | EmitBlock(loopBB); |
| 825 | |
| 826 | // Set up the current-element phi. |
| 827 | llvm::PHINode *curPtr = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 828 | Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur"); |
| 829 | curPtr->addIncoming(explicitPtr, entryBB); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 830 | |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 831 | // Store the new cleanup position for irregular cleanups. |
| 832 | if (endOfInit) Builder.CreateStore(curPtr, endOfInit); |
| 833 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 834 | // Enter a partial-destruction cleanup if necessary. |
Chad Rosier | f62290a | 2012-02-24 00:13:55 +0000 | [diff] [blame] | 835 | if (!cleanupDominator && needsEHCleanup(dtorKind)) { |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 836 | pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType, |
| 837 | getDestroyer(dtorKind)); |
| 838 | cleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 839 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // Emit the initializer into this element. |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 843 | StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr); |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 844 | |
| 845 | // Leave the cleanup if we entered one. |
Eli Friedman | de6a86b | 2011-12-09 23:05:37 +0000 | [diff] [blame] | 846 | if (cleanupDominator) { |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 847 | DeactivateCleanupBlock(cleanup, cleanupDominator); |
| 848 | cleanupDominator->eraseFromParent(); |
| 849 | } |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 850 | |
| 851 | // Advance to the next element. |
| 852 | llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next"); |
| 853 | |
| 854 | // Check whether we've gotten to the end of the array and, if so, |
| 855 | // exit the loop. |
| 856 | llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend"); |
| 857 | Builder.CreateCondBr(isEnd, contBB, loopBB); |
| 858 | curPtr->addIncoming(nextPtr, Builder.GetInsertBlock()); |
| 859 | |
| 860 | EmitBlock(contBB); |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 863 | static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T, |
| 864 | llvm::Value *NewPtr, llvm::Value *Size) { |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 865 | CGF.EmitCastToVoidPtr(NewPtr); |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 866 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T); |
Benjamin Kramer | acc6b4e | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 867 | CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size, |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 868 | Alignment.getQuantity(), false); |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 871 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 872 | QualType ElementType, |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 873 | llvm::Value *NewPtr, |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 874 | llvm::Value *NumElements, |
| 875 | llvm::Value *AllocSizeWithoutCookie) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 876 | const Expr *Init = E->getInitializer(); |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 877 | if (E->isArray()) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 878 | if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){ |
| 879 | CXXConstructorDecl *Ctor = CCE->getConstructor(); |
Douglas Gregor | d153103 | 2012-02-23 17:07:43 +0000 | [diff] [blame] | 880 | if (Ctor->isTrivial()) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 881 | // If new expression did not specify value-initialization, then there |
| 882 | // is no initialization. |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 883 | if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 884 | return; |
| 885 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 886 | if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 887 | // Optimization: since zero initialization will just set the memory |
| 888 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 889 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 890 | return; |
| 891 | } |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 892 | } |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 893 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 894 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr, |
| 895 | CCE->arg_begin(), CCE->arg_end(), |
Eli Friedman | 48ddcf2 | 2012-08-25 07:11:29 +0000 | [diff] [blame] | 896 | CCE->requiresZeroInitialization()); |
Anders Carlsson | d040e6b | 2010-05-03 15:09:17 +0000 | [diff] [blame] | 897 | return; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 898 | } else if (Init && isa<ImplicitValueInitExpr>(Init) && |
Eli Friedman | de6a86b | 2011-12-09 23:05:37 +0000 | [diff] [blame] | 899 | CGF.CGM.getTypes().isZeroInitializable(ElementType)) { |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 900 | // Optimization: since zero initialization will just set the memory |
| 901 | // to all zeroes, generate a single memset to do it in one shot. |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 902 | EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie); |
| 903 | return; |
Fariborz Jahanian | d5202e0 | 2010-06-25 18:26:07 +0000 | [diff] [blame] | 904 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 905 | CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements); |
| 906 | return; |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 907 | } |
Anders Carlsson | 3a202f6 | 2009-11-24 18:43:52 +0000 | [diff] [blame] | 908 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 909 | if (!Init) |
Fariborz Jahanian | b66b08e | 2010-06-25 20:01:13 +0000 | [diff] [blame] | 910 | return; |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 911 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 912 | StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 915 | /// Emit a call to an operator new or operator delete function, as implicitly |
| 916 | /// created by new-expressions and delete-expressions. |
| 917 | static RValue EmitNewDeleteCall(CodeGenFunction &CGF, |
| 918 | const FunctionDecl *Callee, |
| 919 | const FunctionProtoType *CalleeType, |
| 920 | const CallArgList &Args) { |
| 921 | llvm::Instruction *CallOrInvoke; |
Richard Smith | 1235a8d | 2013-07-29 20:14:16 +0000 | [diff] [blame] | 922 | llvm::Value *CalleeAddr = CGF.CGM.GetAddrOfFunction(Callee); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 923 | RValue RV = |
| 924 | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(Args, CalleeType), |
Richard Smith | 1235a8d | 2013-07-29 20:14:16 +0000 | [diff] [blame] | 925 | CalleeAddr, ReturnValueSlot(), Args, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 926 | Callee, &CallOrInvoke); |
| 927 | |
| 928 | /// C++1y [expr.new]p10: |
| 929 | /// [In a new-expression,] an implementation is allowed to omit a call |
| 930 | /// to a replaceable global allocation function. |
| 931 | /// |
| 932 | /// We model such elidable calls with the 'builtin' attribute. |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 933 | llvm::Function *Fn = dyn_cast<llvm::Function>(CalleeAddr); |
Richard Smith | 1235a8d | 2013-07-29 20:14:16 +0000 | [diff] [blame] | 934 | if (Callee->isReplaceableGlobalAllocationFunction() && |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 935 | Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) { |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 936 | // FIXME: Add addAttribute to CallSite. |
| 937 | if (llvm::CallInst *CI = dyn_cast<llvm::CallInst>(CallOrInvoke)) |
| 938 | CI->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 939 | llvm::Attribute::Builtin); |
| 940 | else if (llvm::InvokeInst *II = dyn_cast<llvm::InvokeInst>(CallOrInvoke)) |
| 941 | II->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 942 | llvm::Attribute::Builtin); |
| 943 | else |
| 944 | llvm_unreachable("unexpected kind of call instruction"); |
| 945 | } |
| 946 | |
| 947 | return RV; |
| 948 | } |
| 949 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 950 | namespace { |
| 951 | /// A cleanup to call the given 'operator delete' function upon |
| 952 | /// abnormal exit from a new expression. |
| 953 | class CallDeleteDuringNew : public EHScopeStack::Cleanup { |
| 954 | size_t NumPlacementArgs; |
| 955 | const FunctionDecl *OperatorDelete; |
| 956 | llvm::Value *Ptr; |
| 957 | llvm::Value *AllocSize; |
| 958 | |
| 959 | RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); } |
| 960 | |
| 961 | public: |
| 962 | static size_t getExtraSize(size_t NumPlacementArgs) { |
| 963 | return NumPlacementArgs * sizeof(RValue); |
| 964 | } |
| 965 | |
| 966 | CallDeleteDuringNew(size_t NumPlacementArgs, |
| 967 | const FunctionDecl *OperatorDelete, |
| 968 | llvm::Value *Ptr, |
| 969 | llvm::Value *AllocSize) |
| 970 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 971 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 972 | |
| 973 | void setPlacementArg(unsigned I, RValue Arg) { |
| 974 | assert(I < NumPlacementArgs && "index out of range"); |
| 975 | getPlacementArgs()[I] = Arg; |
| 976 | } |
| 977 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 978 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 979 | const FunctionProtoType *FPT |
| 980 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 981 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
John McCall | d441b1e | 2010-09-14 21:45:42 +0000 | [diff] [blame] | 982 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 983 | |
| 984 | CallArgList DeleteArgs; |
| 985 | |
| 986 | // The first argument is always a void*. |
| 987 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 988 | DeleteArgs.add(RValue::get(Ptr), *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 989 | |
| 990 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 991 | if (FPT->getNumArgs() == NumPlacementArgs + 2) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 992 | DeleteArgs.add(RValue::get(AllocSize), *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 993 | |
| 994 | // Pass the rest of the arguments, which must match exactly. |
| 995 | for (unsigned I = 0; I != NumPlacementArgs; ++I) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 996 | DeleteArgs.add(getPlacementArgs()[I], *AI++); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 997 | |
| 998 | // Call 'operator delete'. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 999 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1000 | } |
| 1001 | }; |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1002 | |
| 1003 | /// A cleanup to call the given 'operator delete' function upon |
| 1004 | /// abnormal exit from a new expression when the new expression is |
| 1005 | /// conditional. |
| 1006 | class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup { |
| 1007 | size_t NumPlacementArgs; |
| 1008 | const FunctionDecl *OperatorDelete; |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1009 | DominatingValue<RValue>::saved_type Ptr; |
| 1010 | DominatingValue<RValue>::saved_type AllocSize; |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1011 | |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1012 | DominatingValue<RValue>::saved_type *getPlacementArgs() { |
| 1013 | return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | public: |
| 1017 | static size_t getExtraSize(size_t NumPlacementArgs) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1018 | return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | CallDeleteDuringConditionalNew(size_t NumPlacementArgs, |
| 1022 | const FunctionDecl *OperatorDelete, |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1023 | DominatingValue<RValue>::saved_type Ptr, |
| 1024 | DominatingValue<RValue>::saved_type AllocSize) |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1025 | : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete), |
| 1026 | Ptr(Ptr), AllocSize(AllocSize) {} |
| 1027 | |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1028 | void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) { |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1029 | assert(I < NumPlacementArgs && "index out of range"); |
| 1030 | getPlacementArgs()[I] = Arg; |
| 1031 | } |
| 1032 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1033 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1034 | const FunctionProtoType *FPT |
| 1035 | = OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1036 | assert(FPT->getNumArgs() == NumPlacementArgs + 1 || |
| 1037 | (FPT->getNumArgs() == 2 && NumPlacementArgs == 0)); |
| 1038 | |
| 1039 | CallArgList DeleteArgs; |
| 1040 | |
| 1041 | // The first argument is always a void*. |
| 1042 | FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin(); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1043 | DeleteArgs.add(Ptr.restore(CGF), *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1044 | |
| 1045 | // A member 'operator delete' can take an extra 'size_t' argument. |
| 1046 | if (FPT->getNumArgs() == NumPlacementArgs + 2) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1047 | RValue RV = AllocSize.restore(CGF); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1048 | DeleteArgs.add(RV, *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | // Pass the rest of the arguments, which must match exactly. |
| 1052 | for (unsigned I = 0; I != NumPlacementArgs; ++I) { |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1053 | RValue RV = getPlacementArgs()[I].restore(CGF); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1054 | DeleteArgs.add(RV, *AI++); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Call 'operator delete'. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1058 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1059 | } |
| 1060 | }; |
| 1061 | } |
| 1062 | |
| 1063 | /// Enter a cleanup to call 'operator delete' if the initializer in a |
| 1064 | /// new-expression throws. |
| 1065 | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
| 1066 | const CXXNewExpr *E, |
| 1067 | llvm::Value *NewPtr, |
| 1068 | llvm::Value *AllocSize, |
| 1069 | const CallArgList &NewArgs) { |
| 1070 | // If we're not inside a conditional branch, then the cleanup will |
| 1071 | // dominate and we can do the easier (and more efficient) thing. |
| 1072 | if (!CGF.isInConditionalBranch()) { |
| 1073 | CallDeleteDuringNew *Cleanup = CGF.EHStack |
| 1074 | .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup, |
| 1075 | E->getNumPlacementArgs(), |
| 1076 | E->getOperatorDelete(), |
| 1077 | NewPtr, AllocSize); |
| 1078 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1079 | Cleanup->setPlacementArg(I, NewArgs[I+1].RV); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1080 | |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | // Otherwise, we need to save all this stuff. |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1085 | DominatingValue<RValue>::saved_type SavedNewPtr = |
| 1086 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr)); |
| 1087 | DominatingValue<RValue>::saved_type SavedAllocSize = |
| 1088 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1089 | |
| 1090 | CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1091 | .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup, |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1092 | E->getNumPlacementArgs(), |
| 1093 | E->getOperatorDelete(), |
| 1094 | SavedNewPtr, |
| 1095 | SavedAllocSize); |
| 1096 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) |
John McCall | cb5f77f | 2011-01-28 10:53:53 +0000 | [diff] [blame] | 1097 | Cleanup->setPlacementArg(I, |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1098 | DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV)); |
John McCall | 7f9c92a | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1099 | |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1100 | CGF.initFullExprCleanup(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1103 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1104 | // The element type being allocated. |
| 1105 | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1106 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1107 | // 1. Build a call to the allocation function. |
| 1108 | FunctionDecl *allocator = E->getOperatorNew(); |
| 1109 | const FunctionProtoType *allocatorType = |
| 1110 | allocator->getType()->castAs<FunctionProtoType>(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1111 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1112 | CallArgList allocatorArgs; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1113 | |
| 1114 | // The allocation size is the first argument. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1115 | QualType sizeType = getContext().getSizeType(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1116 | |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1117 | // If there is a brace-initializer, cannot allocate fewer elements than inits. |
| 1118 | unsigned minElements = 0; |
| 1119 | if (E->isArray() && E->hasInitializer()) { |
| 1120 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer())) |
| 1121 | minElements = ILE->getNumInits(); |
| 1122 | } |
| 1123 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1124 | llvm::Value *numElements = 0; |
| 1125 | llvm::Value *allocSizeWithoutCookie = 0; |
| 1126 | llvm::Value *allocSize = |
Sebastian Redl | f862eb6 | 2012-02-22 17:37:52 +0000 | [diff] [blame] | 1127 | EmitCXXNewAllocSize(*this, E, minElements, numElements, |
| 1128 | allocSizeWithoutCookie); |
Anders Carlsson | b4bd066 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 1129 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1130 | allocatorArgs.add(RValue::get(allocSize), sizeType); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1131 | |
| 1132 | // Emit the rest of the arguments. |
| 1133 | // FIXME: Ideally, this should just use EmitCallArgs. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1134 | CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1135 | |
| 1136 | // First, use the types from the function type. |
| 1137 | // We start at 1 here because the first argument (the allocation size) |
| 1138 | // has already been emitted. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1139 | for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e; |
| 1140 | ++i, ++placementArg) { |
| 1141 | QualType argType = allocatorType->getArgType(i); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1142 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1143 | assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(), |
| 1144 | placementArg->getType()) && |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1145 | "type mismatch in call argument!"); |
| 1146 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1147 | EmitCallArg(allocatorArgs, *placementArg, argType); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | // Either we've emitted all the call args, or we have a call to a |
| 1151 | // variadic function. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1152 | assert((placementArg == E->placement_arg_end() || |
| 1153 | allocatorType->isVariadic()) && |
| 1154 | "Extra arguments to non-variadic function!"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1155 | |
| 1156 | // If we still have any arguments, emit them using the type of the argument. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1157 | for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end(); |
| 1158 | placementArg != placementArgsEnd; ++placementArg) { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1159 | EmitCallArg(allocatorArgs, *placementArg, placementArg->getType()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1162 | // Emit the allocation call. If the allocator is a global placement |
| 1163 | // operator, just "inline" it directly. |
| 1164 | RValue RV; |
| 1165 | if (allocator->isReservedGlobalPlacementOperator()) { |
| 1166 | assert(allocatorArgs.size() == 2); |
| 1167 | RV = allocatorArgs[1].RV; |
| 1168 | // TODO: kill any unnecessary computations done for the size |
| 1169 | // argument. |
| 1170 | } else { |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1171 | RV = EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1172 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1173 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1174 | // Emit a null check on the allocation result if the allocation |
| 1175 | // function is allowed to return null (because it has a non-throwing |
| 1176 | // exception spec; for this part, we inline |
| 1177 | // CXXNewExpr::shouldNullCheckAllocation()) and we have an |
| 1178 | // interesting initializer. |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1179 | bool nullCheck = allocatorType->isNothrow(getContext()) && |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1180 | (!allocType.isPODType(getContext()) || E->hasInitializer()); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1181 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1182 | llvm::BasicBlock *nullCheckBB = 0; |
| 1183 | llvm::BasicBlock *contBB = 0; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1184 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1185 | llvm::Value *allocation = RV.getScalarVal(); |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1186 | unsigned AS = allocation->getType()->getPointerAddressSpace(); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1187 | |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1188 | // The null-check means that the initializer is conditionally |
| 1189 | // evaluated. |
| 1190 | ConditionalEvaluation conditional(*this); |
| 1191 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1192 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1193 | conditional.begin(*this); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1194 | |
| 1195 | nullCheckBB = Builder.GetInsertBlock(); |
| 1196 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
| 1197 | contBB = createBasicBlock("new.cont"); |
| 1198 | |
| 1199 | llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull"); |
| 1200 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
| 1201 | EmitBlock(notNullBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1202 | } |
Anders Carlsson | f771681 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 1203 | |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1204 | // If there's an operator delete, enter a cleanup to call it if an |
| 1205 | // exception is thrown. |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1206 | EHScopeStack::stable_iterator operatorDeleteCleanup; |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1207 | llvm::Instruction *cleanupDominator = 0; |
John McCall | 7ec4b43 | 2011-05-16 01:05:12 +0000 | [diff] [blame] | 1208 | if (E->getOperatorDelete() && |
| 1209 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1210 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs); |
| 1211 | operatorDeleteCleanup = EHStack.stable_begin(); |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1212 | cleanupDominator = Builder.CreateUnreachable(); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Eli Friedman | cf9b1f6 | 2011-09-06 18:53:03 +0000 | [diff] [blame] | 1215 | assert((allocSize == allocSizeWithoutCookie) == |
| 1216 | CalculateCookiePadding(*this, E).isZero()); |
| 1217 | if (allocSize != allocSizeWithoutCookie) { |
| 1218 | assert(E->isArray()); |
| 1219 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
| 1220 | numElements, |
| 1221 | E, allocType); |
| 1222 | } |
| 1223 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1224 | llvm::Type *elementPtrTy |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1225 | = ConvertTypeForMem(allocType)->getPointerTo(AS); |
| 1226 | llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy); |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1227 | |
John McCall | 99210dc | 2011-09-15 06:49:18 +0000 | [diff] [blame] | 1228 | EmitNewInitializer(*this, E, allocType, result, numElements, |
| 1229 | allocSizeWithoutCookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1230 | if (E->isArray()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1231 | // NewPtr is a pointer to the base element type. If we're |
| 1232 | // allocating an array of arrays, we'll need to cast back to the |
| 1233 | // array pointer type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1234 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1235 | if (result->getType() != resultType) |
| 1236 | result = Builder.CreateBitCast(result, resultType); |
Fariborz Jahanian | 47b4629 | 2010-03-24 16:57:01 +0000 | [diff] [blame] | 1237 | } |
John McCall | 824c2f5 | 2010-09-14 07:57:04 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Deactivate the 'operator delete' cleanup if we finished |
| 1240 | // initialization. |
John McCall | f4beacd | 2011-11-10 10:43:54 +0000 | [diff] [blame] | 1241 | if (operatorDeleteCleanup.isValid()) { |
| 1242 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
| 1243 | cleanupDominator->eraseFromParent(); |
| 1244 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1245 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1246 | if (nullCheck) { |
John McCall | f7dcf32 | 2011-03-07 01:52:56 +0000 | [diff] [blame] | 1247 | conditional.end(*this); |
| 1248 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1249 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 1250 | EmitBlock(contBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1251 | |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1252 | llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2); |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1253 | PHI->addIncoming(result, notNullBB); |
| 1254 | PHI->addIncoming(llvm::Constant::getNullValue(result->getType()), |
| 1255 | nullCheckBB); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1256 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1257 | result = PHI; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1258 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1259 | |
John McCall | 75f9498 | 2011-03-07 03:12:35 +0000 | [diff] [blame] | 1260 | return result; |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1263 | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
| 1264 | llvm::Value *Ptr, |
| 1265 | QualType DeleteTy) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1266 | assert(DeleteFD->getOverloadedOperator() == OO_Delete); |
| 1267 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1268 | const FunctionProtoType *DeleteFTy = |
| 1269 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 1270 | |
| 1271 | CallArgList DeleteArgs; |
| 1272 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1273 | // Check if we need to pass the size to the delete operator. |
| 1274 | llvm::Value *Size = 0; |
| 1275 | QualType SizeTy; |
| 1276 | if (DeleteFTy->getNumArgs() == 2) { |
| 1277 | SizeTy = DeleteFTy->getArgType(1); |
Ken Dyck | 7df3cbe | 2010-01-26 19:59:28 +0000 | [diff] [blame] | 1278 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
| 1279 | Size = llvm::ConstantInt::get(ConvertType(SizeTy), |
| 1280 | DeleteTypeSize.getQuantity()); |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1283 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 1284 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1285 | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1286 | |
Anders Carlsson | 21122cf | 2009-12-13 20:04:38 +0000 | [diff] [blame] | 1287 | if (Size) |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1288 | DeleteArgs.add(RValue::get(Size), SizeTy); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Emit the call to delete. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1291 | EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); |
Eli Friedman | fe81e3f | 2009-11-18 00:50:08 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1294 | namespace { |
| 1295 | /// Calls the given 'operator delete' on a single object. |
| 1296 | struct CallObjectDelete : EHScopeStack::Cleanup { |
| 1297 | llvm::Value *Ptr; |
| 1298 | const FunctionDecl *OperatorDelete; |
| 1299 | QualType ElementType; |
| 1300 | |
| 1301 | CallObjectDelete(llvm::Value *Ptr, |
| 1302 | const FunctionDecl *OperatorDelete, |
| 1303 | QualType ElementType) |
| 1304 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
| 1305 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1306 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1307 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
| 1308 | } |
| 1309 | }; |
| 1310 | } |
| 1311 | |
| 1312 | /// Emit the code for deleting a single object. |
| 1313 | static void EmitObjectDelete(CodeGenFunction &CGF, |
| 1314 | const FunctionDecl *OperatorDelete, |
| 1315 | llvm::Value *Ptr, |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1316 | QualType ElementType, |
| 1317 | bool UseGlobalDelete) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1318 | // Find the destructor for the type, if applicable. If the |
| 1319 | // destructor is virtual, we'll just emit the vcall and return. |
| 1320 | const CXXDestructorDecl *Dtor = 0; |
| 1321 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
| 1322 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | b23533d | 2011-08-02 18:05:30 +0000 | [diff] [blame] | 1323 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1324 | Dtor = RD->getDestructor(); |
| 1325 | |
| 1326 | if (Dtor->isVirtual()) { |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1327 | if (UseGlobalDelete) { |
| 1328 | // If we're supposed to call the global delete, make sure we do so |
| 1329 | // even if the destructor throws. |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Derive the complete-object pointer, which is what we need |
| 1332 | // to pass to the deallocation function. |
| 1333 | llvm::Value *completePtr = |
| 1334 | CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType); |
| 1335 | |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1336 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1337 | completePtr, OperatorDelete, |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1338 | ElementType); |
| 1339 | } |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1340 | |
Richard Smith | e30752c | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1341 | // FIXME: Provide a source location here. |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1342 | CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; |
| 1343 | CGF.CGM.getCXXABI().EmitVirtualDestructorCall(CGF, Dtor, DtorType, |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1344 | SourceLocation(), Ptr); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1346 | if (UseGlobalDelete) { |
| 1347 | CGF.PopCleanupBlock(); |
| 1348 | } |
| 1349 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1350 | return; |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | // Make sure that we call delete even if the dtor throws. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1356 | // This doesn't have to a conditional cleanup because we're going |
| 1357 | // to pop it off in a second. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1358 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
| 1359 | Ptr, OperatorDelete, ElementType); |
| 1360 | |
| 1361 | if (Dtor) |
| 1362 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1363 | /*ForVirtualBase=*/false, |
| 1364 | /*Delegating=*/false, |
| 1365 | Ptr); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1366 | else if (CGF.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1367 | ElementType->isObjCLifetimeType()) { |
| 1368 | switch (ElementType.getObjCLifetime()) { |
| 1369 | case Qualifiers::OCL_None: |
| 1370 | case Qualifiers::OCL_ExplicitNone: |
| 1371 | case Qualifiers::OCL_Autoreleasing: |
| 1372 | break; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1373 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1374 | case Qualifiers::OCL_Strong: { |
| 1375 | // Load the pointer value. |
| 1376 | llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr, |
| 1377 | ElementType.isVolatileQualified()); |
| 1378 | |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 1379 | CGF.EmitARCRelease(PtrValue, ARCPreciseLifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | case Qualifiers::OCL_Weak: |
| 1384 | CGF.EmitARCDestroyWeak(Ptr); |
| 1385 | break; |
| 1386 | } |
| 1387 | } |
| 1388 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1389 | CGF.PopCleanupBlock(); |
| 1390 | } |
| 1391 | |
| 1392 | namespace { |
| 1393 | /// Calls the given 'operator delete' on an array of objects. |
| 1394 | struct CallArrayDelete : EHScopeStack::Cleanup { |
| 1395 | llvm::Value *Ptr; |
| 1396 | const FunctionDecl *OperatorDelete; |
| 1397 | llvm::Value *NumElements; |
| 1398 | QualType ElementType; |
| 1399 | CharUnits CookieSize; |
| 1400 | |
| 1401 | CallArrayDelete(llvm::Value *Ptr, |
| 1402 | const FunctionDecl *OperatorDelete, |
| 1403 | llvm::Value *NumElements, |
| 1404 | QualType ElementType, |
| 1405 | CharUnits CookieSize) |
| 1406 | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
| 1407 | ElementType(ElementType), CookieSize(CookieSize) {} |
| 1408 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1409 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1410 | const FunctionProtoType *DeleteFTy = |
| 1411 | OperatorDelete->getType()->getAs<FunctionProtoType>(); |
| 1412 | assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2); |
| 1413 | |
| 1414 | CallArgList Args; |
| 1415 | |
| 1416 | // Pass the pointer as the first argument. |
| 1417 | QualType VoidPtrTy = DeleteFTy->getArgType(0); |
| 1418 | llvm::Value *DeletePtr |
| 1419 | = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy)); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1420 | Args.add(RValue::get(DeletePtr), VoidPtrTy); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Pass the original requested size as the second argument. |
| 1423 | if (DeleteFTy->getNumArgs() == 2) { |
| 1424 | QualType size_t = DeleteFTy->getArgType(1); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1425 | llvm::IntegerType *SizeTy |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1426 | = cast<llvm::IntegerType>(CGF.ConvertType(size_t)); |
| 1427 | |
| 1428 | CharUnits ElementTypeSize = |
| 1429 | CGF.CGM.getContext().getTypeSizeInChars(ElementType); |
| 1430 | |
| 1431 | // The size of an element, multiplied by the number of elements. |
| 1432 | llvm::Value *Size |
| 1433 | = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity()); |
| 1434 | Size = CGF.Builder.CreateMul(Size, NumElements); |
| 1435 | |
| 1436 | // Plus the size of the cookie if applicable. |
| 1437 | if (!CookieSize.isZero()) { |
| 1438 | llvm::Value *CookieSizeV |
| 1439 | = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()); |
| 1440 | Size = CGF.Builder.CreateAdd(Size, CookieSizeV); |
| 1441 | } |
| 1442 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1443 | Args.add(RValue::get(Size), size_t); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | // Emit the call to delete. |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1447 | EmitNewDeleteCall(CGF, OperatorDelete, DeleteFTy, Args); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1448 | } |
| 1449 | }; |
| 1450 | } |
| 1451 | |
| 1452 | /// Emit the code for deleting an array of objects. |
| 1453 | static void EmitArrayDelete(CodeGenFunction &CGF, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1454 | const CXXDeleteExpr *E, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1455 | llvm::Value *deletedPtr, |
| 1456 | QualType elementType) { |
| 1457 | llvm::Value *numElements = 0; |
| 1458 | llvm::Value *allocatedPtr = 0; |
| 1459 | CharUnits cookieSize; |
| 1460 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
| 1461 | numElements, allocatedPtr, cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1462 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1463 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1464 | |
| 1465 | // 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] | 1466 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1467 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1468 | allocatedPtr, operatorDelete, |
| 1469 | numElements, elementType, |
| 1470 | cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1471 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1472 | // Destroy the elements. |
| 1473 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
| 1474 | assert(numElements && "no element count for a type with a destructor!"); |
| 1475 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1476 | llvm::Value *arrayEnd = |
| 1477 | CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end"); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1478 | |
| 1479 | // Note that it is legal to allocate a zero-length array, and we |
| 1480 | // can never fold the check away because the length should always |
| 1481 | // come from a cookie. |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1482 | CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType, |
| 1483 | CGF.getDestroyer(dtorKind), |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 1484 | /*checkZeroLength*/ true, |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1485 | CGF.needsEHCleanup(dtorKind)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
John McCall | ca2c56f | 2011-07-13 01:41:37 +0000 | [diff] [blame] | 1488 | // Pop the cleanup block. |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1489 | CGF.PopCleanupBlock(); |
| 1490 | } |
| 1491 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1492 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1493 | const Expr *Arg = E->getArgument(); |
Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1494 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1495 | |
| 1496 | // Null check the pointer. |
| 1497 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 1498 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 1499 | |
Anders Carlsson | 98981b1 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 1500 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1501 | |
| 1502 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 1503 | EmitBlock(DeleteNotNull); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 1504 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1505 | // We might be deleting a pointer to array. If so, GEP down to the |
| 1506 | // first non-array element. |
| 1507 | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
| 1508 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
| 1509 | if (DeleteTy->isConstantArrayType()) { |
| 1510 | llvm::Value *Zero = Builder.getInt32(0); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1511 | SmallVector<llvm::Value*,8> GEP; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1512 | |
| 1513 | GEP.push_back(Zero); // point at the outermost array |
| 1514 | |
| 1515 | // For each layer of array type we're pointing at: |
| 1516 | while (const ConstantArrayType *Arr |
| 1517 | = getContext().getAsConstantArrayType(DeleteTy)) { |
| 1518 | // 1. Unpeel the array type. |
| 1519 | DeleteTy = Arr->getElementType(); |
| 1520 | |
| 1521 | // 2. GEP to the first element of the array. |
| 1522 | GEP.push_back(Zero); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1523 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1524 | |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 1525 | Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first"); |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Douglas Gregor | 04f3621 | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 1528 | assert(ConvertTypeForMem(DeleteTy) == |
| 1529 | cast<llvm::PointerType>(Ptr->getType())->getElementType()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1530 | |
| 1531 | if (E->isArrayForm()) { |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1532 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1533 | } else { |
Douglas Gregor | 1c2e20d | 2011-07-13 00:54:47 +0000 | [diff] [blame] | 1534 | EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy, |
| 1535 | E->isGlobalDelete()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1536 | } |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1537 | |
Anders Carlsson | cc52f65 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1538 | EmitBlock(DeleteEnd); |
| 1539 | } |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1541 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 1542 | // void __cxa_bad_typeid(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1543 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1544 | |
| 1545 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 1546 | } |
| 1547 | |
| 1548 | static void EmitBadTypeidCall(CodeGenFunction &CGF) { |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1549 | llvm::Value *Fn = getBadTypeidFn(CGF); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1550 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1551 | CGF.Builder.CreateUnreachable(); |
| 1552 | } |
| 1553 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1554 | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, |
| 1555 | const Expr *E, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1556 | llvm::Type *StdTypeInfoPtrTy) { |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1557 | // Get the vtable pointer. |
| 1558 | llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress(); |
| 1559 | |
| 1560 | // C++ [expr.typeid]p2: |
| 1561 | // If the glvalue expression is obtained by applying the unary * operator to |
| 1562 | // a pointer and the pointer is a null pointer value, the typeid expression |
| 1563 | // throws the std::bad_typeid exception. |
| 1564 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { |
| 1565 | if (UO->getOpcode() == UO_Deref) { |
| 1566 | llvm::BasicBlock *BadTypeidBlock = |
| 1567 | CGF.createBasicBlock("typeid.bad_typeid"); |
| 1568 | llvm::BasicBlock *EndBlock = |
| 1569 | CGF.createBasicBlock("typeid.end"); |
| 1570 | |
| 1571 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr); |
| 1572 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
| 1573 | |
| 1574 | CGF.EmitBlock(BadTypeidBlock); |
| 1575 | EmitBadTypeidCall(CGF); |
| 1576 | CGF.EmitBlock(EndBlock); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, |
| 1581 | StdTypeInfoPtrTy->getPointerTo()); |
| 1582 | |
| 1583 | // Load the type info. |
| 1584 | Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); |
| 1585 | return CGF.Builder.CreateLoad(Value); |
| 1586 | } |
| 1587 | |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1588 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1589 | llvm::Type *StdTypeInfoPtrTy = |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1590 | ConvertType(E->getType())->getPointerTo(); |
Anders Carlsson | fd7dfeb | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 1591 | |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1592 | if (E->isTypeOperand()) { |
David Majnemer | 143c55e | 2013-09-27 07:04:31 +0000 | [diff] [blame] | 1593 | llvm::Constant *TypeInfo = |
| 1594 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1595 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1596 | } |
Anders Carlsson | 0c63350 | 2011-04-11 14:13:40 +0000 | [diff] [blame] | 1597 | |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1598 | // C++ [expr.typeid]p2: |
| 1599 | // When typeid is applied to a glvalue expression whose type is a |
| 1600 | // polymorphic class type, the result refers to a std::type_info object |
| 1601 | // representing the type of the most derived object (that is, the dynamic |
| 1602 | // type) to which the glvalue refers. |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 1603 | if (E->isPotentiallyEvaluated()) |
| 1604 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
| 1605 | StdTypeInfoPtrTy); |
Anders Carlsson | 940f02d | 2011-04-18 00:57:03 +0000 | [diff] [blame] | 1606 | |
| 1607 | QualType OperandTy = E->getExprOperand()->getType(); |
| 1608 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
| 1609 | StdTypeInfoPtrTy); |
Mike Stump | c9b231c | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 1610 | } |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1611 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1612 | static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) { |
| 1613 | // void *__dynamic_cast(const void *sub, |
| 1614 | // const abi::__class_type_info *src, |
| 1615 | // const abi::__class_type_info *dst, |
| 1616 | // std::ptrdiff_t src2dst_offset); |
| 1617 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1618 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1619 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1620 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1621 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1622 | llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
Benjamin Kramer | b520633 | 2013-02-03 17:44:25 +0000 | [diff] [blame] | 1623 | |
| 1624 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 1625 | |
| 1626 | // Mark the function as nounwind readonly. |
| 1627 | llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, |
| 1628 | llvm::Attribute::ReadOnly }; |
| 1629 | llvm::AttributeSet Attrs = llvm::AttributeSet::get( |
| 1630 | CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); |
| 1631 | |
| 1632 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 1636 | // void __cxa_bad_cast(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1637 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1638 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 1639 | } |
| 1640 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1641 | static void EmitBadCastCall(CodeGenFunction &CGF) { |
Anders Carlsson | bbe277c | 2011-04-13 02:35:36 +0000 | [diff] [blame] | 1642 | llvm::Value *Fn = getBadCastFn(CGF); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1643 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1644 | CGF.Builder.CreateUnreachable(); |
| 1645 | } |
| 1646 | |
Benjamin Kramer | d9c8455 | 2013-02-03 19:59:25 +0000 | [diff] [blame] | 1647 | /// \brief Compute the src2dst_offset hint as described in the |
| 1648 | /// Itanium C++ ABI [2.9.7] |
| 1649 | static CharUnits computeOffsetHint(ASTContext &Context, |
| 1650 | const CXXRecordDecl *Src, |
| 1651 | const CXXRecordDecl *Dst) { |
| 1652 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1653 | /*DetectVirtual=*/false); |
| 1654 | |
| 1655 | // If Dst is not derived from Src we can skip the whole computation below and |
| 1656 | // return that Src is not a public base of Dst. Record all inheritance paths. |
| 1657 | if (!Dst->isDerivedFrom(Src, Paths)) |
| 1658 | return CharUnits::fromQuantity(-2ULL); |
| 1659 | |
| 1660 | unsigned NumPublicPaths = 0; |
| 1661 | CharUnits Offset; |
| 1662 | |
| 1663 | // Now walk all possible inheritance paths. |
| 1664 | for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); |
| 1665 | I != E; ++I) { |
| 1666 | if (I->Access != AS_public) // Ignore non-public inheritance. |
| 1667 | continue; |
| 1668 | |
| 1669 | ++NumPublicPaths; |
| 1670 | |
| 1671 | for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) { |
| 1672 | // If the path contains a virtual base class we can't give any hint. |
| 1673 | // -1: no hint. |
| 1674 | if (J->Base->isVirtual()) |
| 1675 | return CharUnits::fromQuantity(-1ULL); |
| 1676 | |
| 1677 | if (NumPublicPaths > 1) // Won't use offsets, skip computation. |
| 1678 | continue; |
| 1679 | |
| 1680 | // Accumulate the base class offsets. |
| 1681 | const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class); |
| 1682 | Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl()); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | // -2: Src is not a public base of Dst. |
| 1687 | if (NumPublicPaths == 0) |
| 1688 | return CharUnits::fromQuantity(-2ULL); |
| 1689 | |
| 1690 | // -3: Src is a multiple public base type but never a virtual base type. |
| 1691 | if (NumPublicPaths > 1) |
| 1692 | return CharUnits::fromQuantity(-3ULL); |
| 1693 | |
| 1694 | // Otherwise, the Src type is a unique public nonvirtual base type of Dst. |
| 1695 | // Return the offset of Src from the origin of Dst. |
| 1696 | return Offset; |
| 1697 | } |
| 1698 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1699 | static llvm::Value * |
| 1700 | EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 1701 | QualType SrcTy, QualType DestTy, |
| 1702 | llvm::BasicBlock *CastEnd) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1703 | llvm::Type *PtrDiffLTy = |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1704 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1705 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1706 | |
| 1707 | if (const PointerType *PTy = DestTy->getAs<PointerType>()) { |
| 1708 | if (PTy->getPointeeType()->isVoidType()) { |
| 1709 | // C++ [expr.dynamic.cast]p7: |
| 1710 | // If T is "pointer to cv void," then the result is a pointer to the |
| 1711 | // most derived object pointed to by v. |
| 1712 | |
| 1713 | // Get the vtable pointer. |
| 1714 | llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); |
| 1715 | |
| 1716 | // Get the offset-to-top from the vtable. |
| 1717 | llvm::Value *OffsetToTop = |
| 1718 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
| 1719 | OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); |
| 1720 | |
| 1721 | // Finally, add the offset to the pointer. |
| 1722 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1723 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1724 | |
| 1725 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | QualType SrcRecordTy; |
| 1730 | QualType DestRecordTy; |
| 1731 | |
| 1732 | if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) { |
| 1733 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
| 1734 | DestRecordTy = DestPTy->getPointeeType(); |
| 1735 | } else { |
| 1736 | SrcRecordTy = SrcTy; |
| 1737 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
| 1738 | } |
| 1739 | |
| 1740 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
| 1741 | assert(DestRecordTy->isRecordType() && "dest type must be a record type!"); |
| 1742 | |
| 1743 | llvm::Value *SrcRTTI = |
| 1744 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1745 | llvm::Value *DestRTTI = |
| 1746 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1747 | |
Benjamin Kramer | d9c8455 | 2013-02-03 19:59:25 +0000 | [diff] [blame] | 1748 | // Compute the offset hint. |
| 1749 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 1750 | const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); |
| 1751 | llvm::Value *OffsetHint = |
| 1752 | llvm::ConstantInt::get(PtrDiffLTy, |
| 1753 | computeOffsetHint(CGF.getContext(), SrcDecl, |
| 1754 | DestDecl).getQuantity()); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1755 | |
| 1756 | // Emit the call to __dynamic_cast. |
| 1757 | Value = CGF.EmitCastToVoidPtr(Value); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1758 | |
| 1759 | llvm::Value *args[] = { Value, SrcRTTI, DestRTTI, OffsetHint }; |
| 1760 | Value = CGF.EmitNounwindRuntimeCall(getDynamicCastFn(CGF), args); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1761 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1762 | |
| 1763 | /// C++ [expr.dynamic.cast]p9: |
| 1764 | /// A failed cast to reference type throws std::bad_cast |
| 1765 | if (DestTy->isReferenceType()) { |
| 1766 | llvm::BasicBlock *BadCastBlock = |
| 1767 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1768 | |
| 1769 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1770 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1771 | |
| 1772 | CGF.EmitBlock(BadCastBlock); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1773 | EmitBadCastCall(CGF); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | return Value; |
| 1777 | } |
| 1778 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1779 | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
| 1780 | QualType DestTy) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1781 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1782 | if (DestTy->isPointerType()) |
| 1783 | return llvm::Constant::getNullValue(DestLTy); |
| 1784 | |
| 1785 | /// C++ [expr.dynamic.cast]p9: |
| 1786 | /// A failed cast to reference type throws std::bad_cast |
| 1787 | EmitBadCastCall(CGF); |
| 1788 | |
| 1789 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
| 1790 | return llvm::UndefValue::get(DestLTy); |
| 1791 | } |
| 1792 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1793 | llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value, |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1794 | const CXXDynamicCastExpr *DCE) { |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1795 | QualType DestTy = DCE->getTypeAsWritten(); |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1796 | |
Anders Carlsson | c1c9971 | 2011-04-11 01:45:29 +0000 | [diff] [blame] | 1797 | if (DCE->isAlwaysNull()) |
| 1798 | return EmitDynamicCastToNull(*this, DestTy); |
| 1799 | |
| 1800 | QualType SrcTy = DCE->getSubExpr()->getType(); |
| 1801 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1802 | // C++ [expr.dynamic.cast]p4: |
| 1803 | // If the value of v is a null pointer value in the pointer case, the result |
| 1804 | // is the null pointer value of type T. |
| 1805 | bool ShouldNullCheckSrcValue = SrcTy->isPointerType(); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1806 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1807 | llvm::BasicBlock *CastNull = 0; |
| 1808 | llvm::BasicBlock *CastNotNull = 0; |
| 1809 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1810 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1811 | if (ShouldNullCheckSrcValue) { |
| 1812 | CastNull = createBasicBlock("dynamic_cast.null"); |
| 1813 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
| 1814 | |
| 1815 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
| 1816 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 1817 | EmitBlock(CastNotNull); |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Anders Carlsson | 882d790 | 2011-04-11 00:46:40 +0000 | [diff] [blame] | 1820 | Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd); |
| 1821 | |
| 1822 | if (ShouldNullCheckSrcValue) { |
| 1823 | EmitBranch(CastEnd); |
| 1824 | |
| 1825 | EmitBlock(CastNull); |
| 1826 | EmitBranch(CastEnd); |
| 1827 | } |
| 1828 | |
| 1829 | EmitBlock(CastEnd); |
| 1830 | |
| 1831 | if (ShouldNullCheckSrcValue) { |
| 1832 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
| 1833 | PHI->addIncoming(Value, CastNotNull); |
| 1834 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
| 1835 | |
| 1836 | Value = PHI; |
| 1837 | } |
| 1838 | |
| 1839 | return Value; |
Mike Stump | 6551170 | 2009-11-16 06:50:58 +0000 | [diff] [blame] | 1840 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1841 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1842 | void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) { |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 1843 | RunCleanupsScope Scope(*this); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1844 | LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(), |
| 1845 | Slot.getAlignment()); |
Eli Friedman | 8631f3e8 | 2012-02-09 03:47:20 +0000 | [diff] [blame] | 1846 | |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1847 | CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin(); |
| 1848 | for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(), |
| 1849 | e = E->capture_init_end(); |
Eric Christopher | d47e086 | 2012-02-29 03:25:18 +0000 | [diff] [blame] | 1850 | i != e; ++i, ++CurField) { |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1851 | // Emit initialization |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1852 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1853 | LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); |
Eli Friedman | 5f1a04f | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 1854 | ArrayRef<VarDecl *> ArrayIndexes; |
| 1855 | if (CurField->getType()->isArrayType()) |
| 1856 | ArrayIndexes = E->getCaptureInitIndexVars(i); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1857 | EmitInitializerForField(*CurField, LV, *i, ArrayIndexes); |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1858 | } |
Eli Friedman | c370a7e | 2012-02-09 03:32:31 +0000 | [diff] [blame] | 1859 | } |