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