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