blob: eb0e2068dbef0edebe8023ca170ba2319cde59a2 [file] [log] [blame]
Anders Carlsson59486a22009-11-24 05:51:11 +00001//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
Anders Carlssoncc52f652009-09-22 22:53:17 +00002//
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 Patel91bbb552010-09-30 19:05:55 +000014#include "clang/Frontend/CodeGenOptions.h"
Anders Carlssoncc52f652009-09-22 22:53:17 +000015#include "CodeGenFunction.h"
John McCall5d865c322010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Fariborz Jahanian60d215b2010-05-20 21:38:57 +000017#include "CGObjCRuntime.h"
Devang Patel91bbb552010-09-30 19:05:55 +000018#include "CGDebugInfo.h"
Chris Lattner26008e02010-07-20 20:19:24 +000019#include "llvm/Intrinsics.h"
Anders Carlssonbbe277c2011-04-13 02:35:36 +000020#include "llvm/Support/CallSite.h"
21
Anders Carlssoncc52f652009-09-22 22:53:17 +000022using namespace clang;
23using namespace CodeGen;
24
Anders Carlsson27da15b2010-01-01 20:29:01 +000025RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
26 llvm::Value *Callee,
27 ReturnValueSlot ReturnValue,
28 llvm::Value *This,
Anders Carlssone36a6b32010-01-02 01:01:18 +000029 llvm::Value *VTT,
Anders Carlsson27da15b2010-01-01 20:29:01 +000030 CallExpr::const_arg_iterator ArgBeg,
31 CallExpr::const_arg_iterator ArgEnd) {
32 assert(MD->isInstance() &&
33 "Trying to emit a member call expr on a static method!");
34
35 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
36
37 CallArgList Args;
38
39 // Push the this ptr.
Eli Friedman43dca6a2011-05-02 17:57:46 +000040 Args.add(RValue::get(This), MD->getThisType(getContext()));
Anders Carlsson27da15b2010-01-01 20:29:01 +000041
Anders Carlssone36a6b32010-01-02 01:01:18 +000042 // If there is a VTT parameter, emit it.
43 if (VTT) {
44 QualType T = getContext().getPointerType(getContext().VoidPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +000045 Args.add(RValue::get(VTT), T);
Anders Carlssone36a6b32010-01-02 01:01:18 +000046 }
47
Anders Carlsson27da15b2010-01-01 20:29:01 +000048 // And the rest of the call args
49 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
50
John McCallab26cfa2010-02-05 21:31:56 +000051 QualType ResultType = FPT->getResultType();
Tilmann Scheller99cc30c2011-03-02 21:36:49 +000052 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
53 FPT->getExtInfo()),
Rafael Espindolac50c27c2010-03-30 20:24:48 +000054 Callee, ReturnValue, Args, MD);
Anders Carlsson27da15b2010-01-01 20:29:01 +000055}
56
Anders Carlsson1ae64c52011-01-29 03:52:01 +000057static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
Anders Carlsson6b3afd72011-01-29 05:04:11 +000058 const Expr *E = Base;
59
60 while (true) {
61 E = E->IgnoreParens();
62 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
63 if (CE->getCastKind() == CK_DerivedToBase ||
64 CE->getCastKind() == CK_UncheckedDerivedToBase ||
65 CE->getCastKind() == CK_NoOp) {
66 E = CE->getSubExpr();
67 continue;
68 }
69 }
70
71 break;
72 }
73
74 QualType DerivedType = E->getType();
Anders Carlsson1ae64c52011-01-29 03:52:01 +000075 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
76 DerivedType = PTy->getPointeeType();
77
78 return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
79}
80
Anders Carlssonc53d9e82011-04-10 18:20:53 +000081// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
82// quite what we want.
83static const Expr *skipNoOpCastsAndParens(const Expr *E) {
84 while (true) {
85 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
86 E = PE->getSubExpr();
87 continue;
88 }
89
90 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
91 if (CE->getCastKind() == CK_NoOp) {
92 E = CE->getSubExpr();
93 continue;
94 }
95 }
96 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
97 if (UO->getOpcode() == UO_Extension) {
98 E = UO->getSubExpr();
99 continue;
100 }
101 }
102 return E;
103 }
104}
105
Anders Carlsson27da15b2010-01-01 20:29:01 +0000106/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
107/// expr can be devirtualized.
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000108static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
109 const Expr *Base,
Anders Carlssona7911fa2010-10-27 13:28:46 +0000110 const CXXMethodDecl *MD) {
111
Anders Carlsson1ae64c52011-01-29 03:52:01 +0000112 // When building with -fapple-kext, all calls must go through the vtable since
113 // the kernel linker can do runtime patching of vtables.
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000114 if (Context.getLangOptions().AppleKext)
115 return false;
116
Anders Carlsson1ae64c52011-01-29 03:52:01 +0000117 // If the most derived class is marked final, we know that no subclass can
118 // override this member function and so we can devirtualize it. For example:
119 //
120 // struct A { virtual void f(); }
121 // struct B final : A { };
122 //
123 // void f(B *b) {
124 // b->f();
125 // }
126 //
127 const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
128 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
129 return true;
130
Anders Carlsson19588aa2011-01-23 21:07:30 +0000131 // If the member function is marked 'final', we know that it can't be
Anders Carlssonb00c2142010-10-27 13:34:43 +0000132 // overridden and can therefore devirtualize it.
Anders Carlsson1eb95962011-01-24 16:26:15 +0000133 if (MD->hasAttr<FinalAttr>())
Anders Carlssona7911fa2010-10-27 13:28:46 +0000134 return true;
Anders Carlssonb00c2142010-10-27 13:34:43 +0000135
Anders Carlsson19588aa2011-01-23 21:07:30 +0000136 // Similarly, if the class itself is marked 'final' it can't be overridden
137 // and we can therefore devirtualize the member function call.
Anders Carlsson1eb95962011-01-24 16:26:15 +0000138 if (MD->getParent()->hasAttr<FinalAttr>())
Anders Carlssonb00c2142010-10-27 13:34:43 +0000139 return true;
140
Anders Carlssonc53d9e82011-04-10 18:20:53 +0000141 Base = skipNoOpCastsAndParens(Base);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000142 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
143 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
144 // This is a record decl. We know the type and can devirtualize it.
145 return VD->getType()->isRecordType();
146 }
147
148 return false;
149 }
150
151 // We can always devirtualize calls on temporary object expressions.
Eli Friedmana6824272010-01-31 20:58:15 +0000152 if (isa<CXXConstructExpr>(Base))
Anders Carlsson27da15b2010-01-01 20:29:01 +0000153 return true;
154
155 // And calls on bound temporaries.
156 if (isa<CXXBindTemporaryExpr>(Base))
157 return true;
158
159 // Check if this is a call expr that returns a record type.
160 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
161 return CE->getCallReturnType()->isRecordType();
Anders Carlssona7911fa2010-10-27 13:28:46 +0000162
Anders Carlsson27da15b2010-01-01 20:29:01 +0000163 // We can't devirtualize the call.
164 return false;
165}
166
Francois Pichet64225792011-01-18 05:04:39 +0000167// Note: This function also emit constructor calls to support a MSVC
168// extensions allowing explicit constructor function call.
Anders Carlsson27da15b2010-01-01 20:29:01 +0000169RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
170 ReturnValueSlot ReturnValue) {
John McCall2d2e8702011-04-11 07:02:50 +0000171 const Expr *callee = CE->getCallee()->IgnoreParens();
172
173 if (isa<BinaryOperator>(callee))
Anders Carlsson27da15b2010-01-01 20:29:01 +0000174 return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
John McCall2d2e8702011-04-11 07:02:50 +0000175
176 const MemberExpr *ME = cast<MemberExpr>(callee);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000177 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
178
Devang Patel91bbb552010-09-30 19:05:55 +0000179 CGDebugInfo *DI = getDebugInfo();
Devang Patel401c9162010-10-22 18:56:27 +0000180 if (DI && CGM.getCodeGenOpts().LimitDebugInfo
181 && !isa<CallExpr>(ME->getBase())) {
Devang Patel91bbb552010-09-30 19:05:55 +0000182 QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType();
183 if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) {
184 DI->getOrCreateRecordType(PTy->getPointeeType(),
185 MD->getParent()->getLocation());
186 }
187 }
188
Anders Carlsson27da15b2010-01-01 20:29:01 +0000189 if (MD->isStatic()) {
190 // The method is static, emit it as we would a regular call.
191 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
192 return EmitCall(getContext().getPointerType(MD->getType()), Callee,
193 ReturnValue, CE->arg_begin(), CE->arg_end());
194 }
Anders Carlsson27da15b2010-01-01 20:29:01 +0000195
John McCall0d635f52010-09-03 01:26:39 +0000196 // Compute the object pointer.
Anders Carlsson27da15b2010-01-01 20:29:01 +0000197 llvm::Value *This;
Anders Carlsson27da15b2010-01-01 20:29:01 +0000198 if (ME->isArrow())
199 This = EmitScalarExpr(ME->getBase());
John McCalle26a8722010-12-04 08:14:53 +0000200 else
201 This = EmitLValue(ME->getBase()).getAddress();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000202
John McCall0d635f52010-09-03 01:26:39 +0000203 if (MD->isTrivial()) {
204 if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
Francois Pichet64225792011-01-18 05:04:39 +0000205 if (isa<CXXConstructorDecl>(MD) &&
206 cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
207 return RValue::get(0);
John McCall0d635f52010-09-03 01:26:39 +0000208
Francois Pichet64225792011-01-18 05:04:39 +0000209 if (MD->isCopyAssignmentOperator()) {
210 // We don't like to generate the trivial copy assignment operator when
211 // it isn't necessary; just produce the proper effect here.
212 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
213 EmitAggregateCopy(This, RHS, CE->getType());
214 return RValue::get(This);
215 }
216
217 if (isa<CXXConstructorDecl>(MD) &&
218 cast<CXXConstructorDecl>(MD)->isCopyConstructor()) {
219 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
220 EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
221 CE->arg_begin(), CE->arg_end());
222 return RValue::get(This);
223 }
224 llvm_unreachable("unknown trivial member function");
Anders Carlsson27da15b2010-01-01 20:29:01 +0000225 }
226
John McCall0d635f52010-09-03 01:26:39 +0000227 // Compute the function type we're calling.
Francois Pichet64225792011-01-18 05:04:39 +0000228 const CGFunctionInfo *FInfo = 0;
229 if (isa<CXXDestructorDecl>(MD))
230 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
231 Dtor_Complete);
232 else if (isa<CXXConstructorDecl>(MD))
233 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD),
234 Ctor_Complete);
235 else
236 FInfo = &CGM.getTypes().getFunctionInfo(MD);
John McCall0d635f52010-09-03 01:26:39 +0000237
238 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
239 const llvm::Type *Ty
Francois Pichet64225792011-01-18 05:04:39 +0000240 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
John McCall0d635f52010-09-03 01:26:39 +0000241
Anders Carlsson27da15b2010-01-01 20:29:01 +0000242 // C++ [class.virtual]p12:
243 // Explicit qualification with the scope operator (5.1) suppresses the
244 // virtual call mechanism.
245 //
246 // We also don't emit a virtual call if the base expression has a record type
247 // because then we know what the type is.
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000248 bool UseVirtualCall;
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000249 UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
250 && !canDevirtualizeMemberFunctionCalls(getContext(),
251 ME->getBase(), MD);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000252 llvm::Value *Callee;
John McCall0d635f52010-09-03 01:26:39 +0000253 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
254 if (UseVirtualCall) {
255 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000256 } else {
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000257 if (getContext().getLangOptions().AppleKext &&
258 MD->isVirtual() &&
259 ME->hasQualifier())
Fariborz Jahanian7f6f81b2011-02-03 19:27:17 +0000260 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000261 else
262 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000263 }
Francois Pichet64225792011-01-18 05:04:39 +0000264 } else if (const CXXConstructorDecl *Ctor =
265 dyn_cast<CXXConstructorDecl>(MD)) {
266 Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
John McCall0d635f52010-09-03 01:26:39 +0000267 } else if (UseVirtualCall) {
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000268 Callee = BuildVirtualCall(MD, This, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000269 } else {
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000270 if (getContext().getLangOptions().AppleKext &&
Fariborz Jahanian9f9438b2011-01-28 23:42:29 +0000271 MD->isVirtual() &&
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000272 ME->hasQualifier())
Fariborz Jahanian7f6f81b2011-02-03 19:27:17 +0000273 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000274 else
275 Callee = CGM.GetAddrOfFunction(MD, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000276 }
277
Anders Carlssone36a6b32010-01-02 01:01:18 +0000278 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000279 CE->arg_begin(), CE->arg_end());
280}
281
282RValue
283CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
284 ReturnValueSlot ReturnValue) {
285 const BinaryOperator *BO =
286 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
287 const Expr *BaseExpr = BO->getLHS();
288 const Expr *MemFnExpr = BO->getRHS();
289
290 const MemberPointerType *MPT =
John McCall0009fcc2011-04-26 20:42:42 +0000291 MemFnExpr->getType()->castAs<MemberPointerType>();
John McCall475999d2010-08-22 00:05:51 +0000292
Anders Carlsson27da15b2010-01-01 20:29:01 +0000293 const FunctionProtoType *FPT =
John McCall0009fcc2011-04-26 20:42:42 +0000294 MPT->getPointeeType()->castAs<FunctionProtoType>();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000295 const CXXRecordDecl *RD =
296 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
297
Anders Carlsson27da15b2010-01-01 20:29:01 +0000298 // Get the member function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000299 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000300
301 // Emit the 'this' pointer.
302 llvm::Value *This;
303
John McCalle3027922010-08-25 11:45:40 +0000304 if (BO->getOpcode() == BO_PtrMemI)
Anders Carlsson27da15b2010-01-01 20:29:01 +0000305 This = EmitScalarExpr(BaseExpr);
306 else
307 This = EmitLValue(BaseExpr).getAddress();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000308
John McCall475999d2010-08-22 00:05:51 +0000309 // Ask the ABI to load the callee. Note that This is modified.
310 llvm::Value *Callee =
John McCallad7c5c12011-02-08 08:22:06 +0000311 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000312
Anders Carlsson27da15b2010-01-01 20:29:01 +0000313 CallArgList Args;
314
315 QualType ThisType =
316 getContext().getPointerType(getContext().getTagDeclType(RD));
317
318 // Push the this ptr.
Eli Friedman43dca6a2011-05-02 17:57:46 +0000319 Args.add(RValue::get(This), ThisType);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000320
321 // And the rest of the call args
322 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
John McCall0009fcc2011-04-26 20:42:42 +0000323 return EmitCall(CGM.getTypes().getFunctionInfo(Args, FPT), Callee,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000324 ReturnValue, Args);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000325}
326
327RValue
328CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
329 const CXXMethodDecl *MD,
330 ReturnValueSlot ReturnValue) {
331 assert(MD->isInstance() &&
332 "Trying to emit a member call expr on a static method!");
John McCalle26a8722010-12-04 08:14:53 +0000333 LValue LV = EmitLValue(E->getArg(0));
334 llvm::Value *This = LV.getAddress();
335
Douglas Gregorec3bec02010-09-27 22:37:28 +0000336 if (MD->isCopyAssignmentOperator()) {
Anders Carlsson27da15b2010-01-01 20:29:01 +0000337 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
338 if (ClassDecl->hasTrivialCopyAssignment()) {
339 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
340 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
Anders Carlsson27da15b2010-01-01 20:29:01 +0000341 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
342 QualType Ty = E->getType();
Fariborz Jahanian021510e2010-06-15 22:44:06 +0000343 EmitAggregateCopy(This, Src, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000344 return RValue::get(This);
345 }
346 }
347
348 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
349 const llvm::Type *Ty =
350 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
351 FPT->isVariadic());
Anders Carlsson27da15b2010-01-01 20:29:01 +0000352 llvm::Value *Callee;
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000353 if (MD->isVirtual() &&
Fariborz Jahanian252a47f2011-01-21 01:04:41 +0000354 !canDevirtualizeMemberFunctionCalls(getContext(),
355 E->getArg(0), MD))
Anders Carlsson27da15b2010-01-01 20:29:01 +0000356 Callee = BuildVirtualCall(MD, This, Ty);
357 else
358 Callee = CGM.GetAddrOfFunction(MD, Ty);
359
Anders Carlssone36a6b32010-01-02 01:01:18 +0000360 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000361 E->arg_begin() + 1, E->arg_end());
362}
363
364void
John McCall7a626f62010-09-15 10:14:12 +0000365CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
366 AggValueSlot Dest) {
367 assert(!Dest.isIgnored() && "Must have a destination!");
Anders Carlsson27da15b2010-01-01 20:29:01 +0000368 const CXXConstructorDecl *CD = E->getConstructor();
Douglas Gregor630c76e2010-08-22 16:15:35 +0000369
370 // If we require zero initialization before (or instead of) calling the
371 // constructor, as can be the case with a non-user-provided default
Argyrios Kyrtzidis03535262011-04-28 22:57:55 +0000372 // constructor, emit the zero initialization now, unless destination is
373 // already zeroed.
374 if (E->requiresZeroInitialization() && !Dest.isZeroed())
John McCall7a626f62010-09-15 10:14:12 +0000375 EmitNullInitialization(Dest.getAddr(), E->getType());
Douglas Gregor630c76e2010-08-22 16:15:35 +0000376
377 // If this is a call to a trivial default constructor, do nothing.
378 if (CD->isTrivial() && CD->isDefaultConstructor())
379 return;
380
John McCall8ea46b62010-09-18 00:58:34 +0000381 // Elide the constructor if we're constructing from a temporary.
382 // The temporary check is required because Sema sets this on NRVO
383 // returns.
Anders Carlsson27da15b2010-01-01 20:29:01 +0000384 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
John McCall8ea46b62010-09-18 00:58:34 +0000385 assert(getContext().hasSameUnqualifiedType(E->getType(),
386 E->getArg(0)->getType()));
John McCall7a626f62010-09-15 10:14:12 +0000387 if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
388 EmitAggExpr(E->getArg(0), Dest);
Douglas Gregor222cf0e2010-05-15 00:13:29 +0000389 return;
390 }
Anders Carlsson27da15b2010-01-01 20:29:01 +0000391 }
Douglas Gregor630c76e2010-08-22 16:15:35 +0000392
393 const ConstantArrayType *Array
394 = getContext().getAsConstantArrayType(E->getType());
Anders Carlsson27da15b2010-01-01 20:29:01 +0000395 if (Array) {
396 QualType BaseElementTy = getContext().getBaseElementType(Array);
397 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
398 BasePtr = llvm::PointerType::getUnqual(BasePtr);
399 llvm::Value *BaseAddrPtr =
John McCall7a626f62010-09-15 10:14:12 +0000400 Builder.CreateBitCast(Dest.getAddr(), BasePtr);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000401
402 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
403 E->arg_begin(), E->arg_end());
404 }
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000405 else {
Alexis Hunt61bc1732011-05-01 07:04:31 +0000406 CXXCtorType Type;
Alexis Hunt271c3682011-05-03 20:19:28 +0000407 bool ForVirtualBase = false;
408
409 switch (E->getConstructionKind()) {
410 case CXXConstructExpr::CK_Delegating:
Alexis Hunt61bc1732011-05-01 07:04:31 +0000411 // We should be emitting a constructor; GlobalDecl will assert this
412 Type = CurGD.getCtorType();
Alexis Hunt271c3682011-05-03 20:19:28 +0000413 break;
Alexis Hunt61bc1732011-05-01 07:04:31 +0000414
Alexis Hunt271c3682011-05-03 20:19:28 +0000415 case CXXConstructExpr::CK_Complete:
416 Type = Ctor_Complete;
417 break;
418
419 case CXXConstructExpr::CK_VirtualBase:
420 ForVirtualBase = true;
421 // fall-through
422
423 case CXXConstructExpr::CK_NonVirtualBase:
424 Type = Ctor_Base;
425 }
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000426
Anders Carlsson27da15b2010-01-01 20:29:01 +0000427 // Call the constructor.
John McCall7a626f62010-09-15 10:14:12 +0000428 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
Anders Carlsson27da15b2010-01-01 20:29:01 +0000429 E->arg_begin(), E->arg_end());
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000430 }
Anders Carlsson27da15b2010-01-01 20:29:01 +0000431}
432
Fariborz Jahaniane988bda2010-11-13 21:53:34 +0000433void
434CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
435 llvm::Value *Src,
Fariborz Jahanian50198092010-12-02 17:02:11 +0000436 const Expr *Exp) {
John McCall5d413782010-12-06 08:20:24 +0000437 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
Fariborz Jahaniane988bda2010-11-13 21:53:34 +0000438 Exp = E->getSubExpr();
439 assert(isa<CXXConstructExpr>(Exp) &&
440 "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
441 const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
442 const CXXConstructorDecl *CD = E->getConstructor();
443 RunCleanupsScope Scope(*this);
444
445 // If we require zero initialization before (or instead of) calling the
446 // constructor, as can be the case with a non-user-provided default
447 // constructor, emit the zero initialization now.
448 // FIXME. Do I still need this for a copy ctor synthesis?
449 if (E->requiresZeroInitialization())
450 EmitNullInitialization(Dest, E->getType());
451
Chandler Carruth99da11c2010-11-15 13:54:43 +0000452 assert(!getContext().getAsConstantArrayType(E->getType())
453 && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
Fariborz Jahaniane988bda2010-11-13 21:53:34 +0000454 EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
455 E->arg_begin(), E->arg_end());
456}
457
John McCallaa4149a2010-08-23 01:17:59 +0000458/// Check whether the given operator new[] is the global placement
459/// operator new[].
460static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
461 const FunctionDecl *Fn) {
462 // Must be in global scope. Note that allocation functions can't be
463 // declared in namespaces.
Sebastian Redl50c68252010-08-31 00:36:30 +0000464 if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
John McCallaa4149a2010-08-23 01:17:59 +0000465 return false;
466
467 // Signature must be void *operator new[](size_t, void*).
468 // The size_t is common to all operator new[]s.
469 if (Fn->getNumParams() != 2)
470 return false;
471
472 CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
473 return (ParamType == Ctx.VoidPtrTy);
474}
475
John McCall8ed55a52010-09-02 09:58:18 +0000476static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
477 const CXXNewExpr *E) {
Anders Carlsson21122cf2009-12-13 20:04:38 +0000478 if (!E->isArray())
Ken Dyck3eb55cf2010-01-26 19:44:24 +0000479 return CharUnits::Zero();
Anders Carlsson21122cf2009-12-13 20:04:38 +0000480
Anders Carlsson399f4992009-12-13 20:34:34 +0000481 // No cookie is required if the new operator being used is
482 // ::operator new[](size_t, void*).
483 const FunctionDecl *OperatorNew = E->getOperatorNew();
John McCall8ed55a52010-09-02 09:58:18 +0000484 if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
John McCallaa4149a2010-08-23 01:17:59 +0000485 return CharUnits::Zero();
486
John McCall284c48f2011-01-27 09:37:56 +0000487 return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000488}
489
Fariborz Jahanian47b46292010-03-24 16:57:01 +0000490static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
Chris Lattnercb46bdc2010-07-20 18:45:57 +0000491 CodeGenFunction &CGF,
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000492 const CXXNewExpr *E,
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000493 llvm::Value *&NumElements,
494 llvm::Value *&SizeWithoutCookie) {
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000495 QualType ElemType = E->getAllocatedType();
John McCall8ed55a52010-09-02 09:58:18 +0000496
497 const llvm::IntegerType *SizeTy =
498 cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000499
John McCall8ed55a52010-09-02 09:58:18 +0000500 CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
501
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000502 if (!E->isArray()) {
503 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
504 return SizeWithoutCookie;
505 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000506
John McCall8ed55a52010-09-02 09:58:18 +0000507 // Figure out the cookie size.
508 CharUnits CookieSize = CalculateCookiePadding(CGF, E);
509
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000510 // Emit the array size expression.
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000511 // We multiply the size of all dimensions for NumElements.
512 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000513 NumElements = CGF.EmitScalarExpr(E->getArraySize());
John McCall8ed55a52010-09-02 09:58:18 +0000514 assert(NumElements->getType() == SizeTy && "element count not a size_t");
515
516 uint64_t ArraySizeMultiplier = 1;
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000517 while (const ConstantArrayType *CAT
518 = CGF.getContext().getAsConstantArrayType(ElemType)) {
519 ElemType = CAT->getElementType();
John McCall8ed55a52010-09-02 09:58:18 +0000520 ArraySizeMultiplier *= CAT->getSize().getZExtValue();
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000521 }
522
John McCall8ed55a52010-09-02 09:58:18 +0000523 llvm::Value *Size;
Chris Lattnerf2f38702010-07-20 21:07:09 +0000524
Chris Lattner32ac5832010-07-20 21:55:52 +0000525 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
526 // Don't bloat the -O0 code.
527 if (llvm::ConstantInt *NumElementsC =
528 dyn_cast<llvm::ConstantInt>(NumElements)) {
Chris Lattner32ac5832010-07-20 21:55:52 +0000529 llvm::APInt NEC = NumElementsC->getValue();
John McCall8ed55a52010-09-02 09:58:18 +0000530 unsigned SizeWidth = NEC.getBitWidth();
531
532 // Determine if there is an overflow here by doing an extended multiply.
Jay Foad6d4db0c2010-12-07 08:25:34 +0000533 NEC = NEC.zext(SizeWidth*2);
John McCall8ed55a52010-09-02 09:58:18 +0000534 llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
Chris Lattner32ac5832010-07-20 21:55:52 +0000535 SC *= NEC;
John McCall8ed55a52010-09-02 09:58:18 +0000536
537 if (!CookieSize.isZero()) {
538 // Save the current size without a cookie. We don't care if an
539 // overflow's already happened because SizeWithoutCookie isn't
540 // used if the allocator returns null or throws, as it should
541 // always do on an overflow.
Jay Foad6d4db0c2010-12-07 08:25:34 +0000542 llvm::APInt SWC = SC.trunc(SizeWidth);
John McCall8ed55a52010-09-02 09:58:18 +0000543 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
544
545 // Add the cookie size.
546 SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
Chris Lattner32ac5832010-07-20 21:55:52 +0000547 }
548
John McCall8ed55a52010-09-02 09:58:18 +0000549 if (SC.countLeadingZeros() >= SizeWidth) {
Jay Foad6d4db0c2010-12-07 08:25:34 +0000550 SC = SC.trunc(SizeWidth);
John McCall8ed55a52010-09-02 09:58:18 +0000551 Size = llvm::ConstantInt::get(SizeTy, SC);
552 } else {
553 // On overflow, produce a -1 so operator new throws.
554 Size = llvm::Constant::getAllOnesValue(SizeTy);
555 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000556
John McCall8ed55a52010-09-02 09:58:18 +0000557 // Scale NumElements while we're at it.
558 uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
559 NumElements = llvm::ConstantInt::get(SizeTy, N);
560
561 // Otherwise, we don't need to do an overflow-checked multiplication if
562 // we're multiplying by one.
563 } else if (TypeSize.isOne()) {
564 assert(ArraySizeMultiplier == 1);
565
566 Size = NumElements;
567
568 // If we need a cookie, add its size in with an overflow check.
569 // This is maybe a little paranoid.
570 if (!CookieSize.isZero()) {
571 SizeWithoutCookie = Size;
572
573 llvm::Value *CookieSizeV
574 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
575
576 const llvm::Type *Types[] = { SizeTy };
577 llvm::Value *UAddF
578 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
579 llvm::Value *AddRes
580 = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
581
582 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
583 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
584 Size = CGF.Builder.CreateSelect(DidOverflow,
585 llvm::ConstantInt::get(SizeTy, -1),
586 Size);
587 }
588
589 // Otherwise use the int.umul.with.overflow intrinsic.
590 } else {
591 llvm::Value *OutermostElementSize
592 = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
593
594 llvm::Value *NumOutermostElements = NumElements;
595
596 // Scale NumElements by the array size multiplier. This might
597 // overflow, but only if the multiplication below also overflows,
598 // in which case this multiplication isn't used.
599 if (ArraySizeMultiplier != 1)
600 NumElements = CGF.Builder.CreateMul(NumElements,
601 llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
602
603 // The requested size of the outermost array is non-constant.
604 // Multiply that by the static size of the elements of that array;
605 // on unsigned overflow, set the size to -1 to trigger an
606 // exception from the allocation routine. This is sufficient to
607 // prevent buffer overruns from the allocator returning a
608 // seemingly valid pointer to insufficient space. This idea comes
609 // originally from MSVC, and GCC has an open bug requesting
610 // similar behavior:
611 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
612 //
613 // This will not be sufficient for C++0x, which requires a
614 // specific exception class (std::bad_array_new_length).
615 // That will require ABI support that has not yet been specified.
616 const llvm::Type *Types[] = { SizeTy };
617 llvm::Value *UMulF
618 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
619 llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
620 OutermostElementSize);
621
622 // The overflow bit.
623 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
624
625 // The result of the multiplication.
626 Size = CGF.Builder.CreateExtractValue(MulRes, 0);
627
628 // If we have a cookie, we need to add that size in, too.
629 if (!CookieSize.isZero()) {
630 SizeWithoutCookie = Size;
631
632 llvm::Value *CookieSizeV
633 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
634 llvm::Value *UAddF
635 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
636 llvm::Value *AddRes
637 = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
638
639 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
640
641 llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
Eli Friedmandb42a3e2011-04-09 19:54:33 +0000642 DidOverflow = CGF.Builder.CreateOr(DidOverflow, AddDidOverflow);
John McCall8ed55a52010-09-02 09:58:18 +0000643 }
644
645 Size = CGF.Builder.CreateSelect(DidOverflow,
646 llvm::ConstantInt::get(SizeTy, -1),
647 Size);
Chris Lattner32ac5832010-07-20 21:55:52 +0000648 }
John McCall8ed55a52010-09-02 09:58:18 +0000649
650 if (CookieSize.isZero())
651 SizeWithoutCookie = Size;
652 else
653 assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
654
Chris Lattner32ac5832010-07-20 21:55:52 +0000655 return Size;
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000656}
657
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000658static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
659 llvm::Value *NewPtr) {
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000660
661 assert(E->getNumConstructorArgs() == 1 &&
662 "Can only have one argument to initializer of POD type.");
663
664 const Expr *Init = E->getConstructorArg(0);
665 QualType AllocType = E->getAllocatedType();
Daniel Dunbar03816342010-08-21 02:24:36 +0000666
667 unsigned Alignment =
668 CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000669 if (!CGF.hasAggregateLLVMType(AllocType))
670 CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
Daniel Dunbar03816342010-08-21 02:24:36 +0000671 AllocType.isVolatileQualified(), Alignment,
672 AllocType);
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000673 else if (AllocType->isAnyComplexType())
674 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
675 AllocType.isVolatileQualified());
John McCall7a626f62010-09-15 10:14:12 +0000676 else {
677 AggValueSlot Slot
678 = AggValueSlot::forAddr(NewPtr, AllocType.isVolatileQualified(), true);
679 CGF.EmitAggExpr(Init, Slot);
680 }
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000681}
682
683void
684CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
685 llvm::Value *NewPtr,
686 llvm::Value *NumElements) {
Fariborz Jahanianb66b08e2010-06-25 20:01:13 +0000687 // We have a POD type.
688 if (E->getNumConstructorArgs() == 0)
689 return;
690
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000691 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
692
693 // Create a temporary for the loop index and initialize it with 0.
694 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
695 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
696 Builder.CreateStore(Zero, IndexPtr);
697
698 // Start the loop with a block that tests the condition.
699 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
700 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
701
702 EmitBlock(CondBlock);
703
704 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
705
706 // Generate: if (loop-index < number-of-elements fall to the loop body,
707 // otherwise, go to the block after the for-loop.
708 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
709 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
710 // If the condition is true, execute the body.
711 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
712
713 EmitBlock(ForBody);
714
715 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
716 // Inside the loop body, emit the constructor call on the array element.
717 Counter = Builder.CreateLoad(IndexPtr);
718 llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
719 "arrayidx");
720 StoreAnyExprIntoOneUnit(*this, E, Address);
721
722 EmitBlock(ContinueBlock);
723
724 // Emit the increment of the loop counter.
725 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
726 Counter = Builder.CreateLoad(IndexPtr);
727 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
728 Builder.CreateStore(NextVal, IndexPtr);
729
730 // Finally, branch back up to the condition for the next iteration.
731 EmitBranch(CondBlock);
732
733 // Emit the fall-through block.
734 EmitBlock(AfterFor, true);
735}
736
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000737static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
738 llvm::Value *NewPtr, llvm::Value *Size) {
John McCallad7c5c12011-02-08 08:22:06 +0000739 CGF.EmitCastToVoidPtr(NewPtr);
Ken Dyck705ba072011-01-19 01:58:38 +0000740 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
Benjamin Krameracc6b4e2010-12-30 00:13:21 +0000741 CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
Ken Dyck705ba072011-01-19 01:58:38 +0000742 Alignment.getQuantity(), false);
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000743}
744
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000745static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
746 llvm::Value *NewPtr,
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000747 llvm::Value *NumElements,
748 llvm::Value *AllocSizeWithoutCookie) {
Anders Carlsson3a202f62009-11-24 18:43:52 +0000749 if (E->isArray()) {
Anders Carlssond040e6b2010-05-03 15:09:17 +0000750 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000751 bool RequiresZeroInitialization = false;
752 if (Ctor->getParent()->hasTrivialConstructor()) {
753 // If new expression did not specify value-initialization, then there
754 // is no initialization.
755 if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
756 return;
757
John McCall614dbdc2010-08-22 21:01:12 +0000758 if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000759 // Optimization: since zero initialization will just set the memory
760 // to all zeroes, generate a single memset to do it in one shot.
761 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
762 AllocSizeWithoutCookie);
763 return;
764 }
765
766 RequiresZeroInitialization = true;
767 }
768
769 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
770 E->constructor_arg_begin(),
771 E->constructor_arg_end(),
772 RequiresZeroInitialization);
Anders Carlssond040e6b2010-05-03 15:09:17 +0000773 return;
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000774 } else if (E->getNumConstructorArgs() == 1 &&
775 isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
776 // Optimization: since zero initialization will just set the memory
777 // to all zeroes, generate a single memset to do it in one shot.
778 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
779 AllocSizeWithoutCookie);
780 return;
781 } else {
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000782 CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
783 return;
784 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000785 }
Anders Carlsson3a202f62009-11-24 18:43:52 +0000786
787 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor747eb782010-07-08 06:14:04 +0000788 // Per C++ [expr.new]p15, if we have an initializer, then we're performing
789 // direct initialization. C++ [dcl.init]p5 requires that we
790 // zero-initialize storage if there are no user-declared constructors.
791 if (E->hasInitializer() &&
792 !Ctor->getParent()->hasUserDeclaredConstructor() &&
793 !Ctor->getParent()->isEmpty())
794 CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
795
Douglas Gregore1823702010-07-07 23:37:33 +0000796 CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
797 NewPtr, E->constructor_arg_begin(),
798 E->constructor_arg_end());
Anders Carlsson3a202f62009-11-24 18:43:52 +0000799
800 return;
801 }
Fariborz Jahanianb66b08e2010-06-25 20:01:13 +0000802 // We have a POD type.
803 if (E->getNumConstructorArgs() == 0)
804 return;
805
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000806 StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000807}
808
John McCall824c2f52010-09-14 07:57:04 +0000809namespace {
810 /// A cleanup to call the given 'operator delete' function upon
811 /// abnormal exit from a new expression.
812 class CallDeleteDuringNew : public EHScopeStack::Cleanup {
813 size_t NumPlacementArgs;
814 const FunctionDecl *OperatorDelete;
815 llvm::Value *Ptr;
816 llvm::Value *AllocSize;
817
818 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
819
820 public:
821 static size_t getExtraSize(size_t NumPlacementArgs) {
822 return NumPlacementArgs * sizeof(RValue);
823 }
824
825 CallDeleteDuringNew(size_t NumPlacementArgs,
826 const FunctionDecl *OperatorDelete,
827 llvm::Value *Ptr,
828 llvm::Value *AllocSize)
829 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
830 Ptr(Ptr), AllocSize(AllocSize) {}
831
832 void setPlacementArg(unsigned I, RValue Arg) {
833 assert(I < NumPlacementArgs && "index out of range");
834 getPlacementArgs()[I] = Arg;
835 }
836
837 void Emit(CodeGenFunction &CGF, bool IsForEH) {
838 const FunctionProtoType *FPT
839 = OperatorDelete->getType()->getAs<FunctionProtoType>();
840 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
John McCalld441b1e2010-09-14 21:45:42 +0000841 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
John McCall824c2f52010-09-14 07:57:04 +0000842
843 CallArgList DeleteArgs;
844
845 // The first argument is always a void*.
846 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
Eli Friedman43dca6a2011-05-02 17:57:46 +0000847 DeleteArgs.add(RValue::get(Ptr), *AI++);
John McCall824c2f52010-09-14 07:57:04 +0000848
849 // A member 'operator delete' can take an extra 'size_t' argument.
850 if (FPT->getNumArgs() == NumPlacementArgs + 2)
Eli Friedman43dca6a2011-05-02 17:57:46 +0000851 DeleteArgs.add(RValue::get(AllocSize), *AI++);
John McCall824c2f52010-09-14 07:57:04 +0000852
853 // Pass the rest of the arguments, which must match exactly.
854 for (unsigned I = 0; I != NumPlacementArgs; ++I)
Eli Friedman43dca6a2011-05-02 17:57:46 +0000855 DeleteArgs.add(getPlacementArgs()[I], *AI++);
John McCall824c2f52010-09-14 07:57:04 +0000856
857 // Call 'operator delete'.
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000858 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
John McCall824c2f52010-09-14 07:57:04 +0000859 CGF.CGM.GetAddrOfFunction(OperatorDelete),
860 ReturnValueSlot(), DeleteArgs, OperatorDelete);
861 }
862 };
John McCall7f9c92a2010-09-17 00:50:28 +0000863
864 /// A cleanup to call the given 'operator delete' function upon
865 /// abnormal exit from a new expression when the new expression is
866 /// conditional.
867 class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
868 size_t NumPlacementArgs;
869 const FunctionDecl *OperatorDelete;
John McCallcb5f77f2011-01-28 10:53:53 +0000870 DominatingValue<RValue>::saved_type Ptr;
871 DominatingValue<RValue>::saved_type AllocSize;
John McCall7f9c92a2010-09-17 00:50:28 +0000872
John McCallcb5f77f2011-01-28 10:53:53 +0000873 DominatingValue<RValue>::saved_type *getPlacementArgs() {
874 return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
John McCall7f9c92a2010-09-17 00:50:28 +0000875 }
876
877 public:
878 static size_t getExtraSize(size_t NumPlacementArgs) {
John McCallcb5f77f2011-01-28 10:53:53 +0000879 return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
John McCall7f9c92a2010-09-17 00:50:28 +0000880 }
881
882 CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
883 const FunctionDecl *OperatorDelete,
John McCallcb5f77f2011-01-28 10:53:53 +0000884 DominatingValue<RValue>::saved_type Ptr,
885 DominatingValue<RValue>::saved_type AllocSize)
John McCall7f9c92a2010-09-17 00:50:28 +0000886 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
887 Ptr(Ptr), AllocSize(AllocSize) {}
888
John McCallcb5f77f2011-01-28 10:53:53 +0000889 void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
John McCall7f9c92a2010-09-17 00:50:28 +0000890 assert(I < NumPlacementArgs && "index out of range");
891 getPlacementArgs()[I] = Arg;
892 }
893
894 void Emit(CodeGenFunction &CGF, bool IsForEH) {
895 const FunctionProtoType *FPT
896 = OperatorDelete->getType()->getAs<FunctionProtoType>();
897 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
898 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
899
900 CallArgList DeleteArgs;
901
902 // The first argument is always a void*.
903 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
Eli Friedman43dca6a2011-05-02 17:57:46 +0000904 DeleteArgs.add(Ptr.restore(CGF), *AI++);
John McCall7f9c92a2010-09-17 00:50:28 +0000905
906 // A member 'operator delete' can take an extra 'size_t' argument.
907 if (FPT->getNumArgs() == NumPlacementArgs + 2) {
John McCallcb5f77f2011-01-28 10:53:53 +0000908 RValue RV = AllocSize.restore(CGF);
Eli Friedman43dca6a2011-05-02 17:57:46 +0000909 DeleteArgs.add(RV, *AI++);
John McCall7f9c92a2010-09-17 00:50:28 +0000910 }
911
912 // Pass the rest of the arguments, which must match exactly.
913 for (unsigned I = 0; I != NumPlacementArgs; ++I) {
John McCallcb5f77f2011-01-28 10:53:53 +0000914 RValue RV = getPlacementArgs()[I].restore(CGF);
Eli Friedman43dca6a2011-05-02 17:57:46 +0000915 DeleteArgs.add(RV, *AI++);
John McCall7f9c92a2010-09-17 00:50:28 +0000916 }
917
918 // Call 'operator delete'.
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000919 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
John McCall7f9c92a2010-09-17 00:50:28 +0000920 CGF.CGM.GetAddrOfFunction(OperatorDelete),
921 ReturnValueSlot(), DeleteArgs, OperatorDelete);
922 }
923 };
924}
925
926/// Enter a cleanup to call 'operator delete' if the initializer in a
927/// new-expression throws.
928static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
929 const CXXNewExpr *E,
930 llvm::Value *NewPtr,
931 llvm::Value *AllocSize,
932 const CallArgList &NewArgs) {
933 // If we're not inside a conditional branch, then the cleanup will
934 // dominate and we can do the easier (and more efficient) thing.
935 if (!CGF.isInConditionalBranch()) {
936 CallDeleteDuringNew *Cleanup = CGF.EHStack
937 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
938 E->getNumPlacementArgs(),
939 E->getOperatorDelete(),
940 NewPtr, AllocSize);
941 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000942 Cleanup->setPlacementArg(I, NewArgs[I+1].RV);
John McCall7f9c92a2010-09-17 00:50:28 +0000943
944 return;
945 }
946
947 // Otherwise, we need to save all this stuff.
John McCallcb5f77f2011-01-28 10:53:53 +0000948 DominatingValue<RValue>::saved_type SavedNewPtr =
949 DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
950 DominatingValue<RValue>::saved_type SavedAllocSize =
951 DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
John McCall7f9c92a2010-09-17 00:50:28 +0000952
953 CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
954 .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(InactiveEHCleanup,
955 E->getNumPlacementArgs(),
956 E->getOperatorDelete(),
957 SavedNewPtr,
958 SavedAllocSize);
959 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
John McCallcb5f77f2011-01-28 10:53:53 +0000960 Cleanup->setPlacementArg(I,
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000961 DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV));
John McCall7f9c92a2010-09-17 00:50:28 +0000962
963 CGF.ActivateCleanupBlock(CGF.EHStack.stable_begin());
John McCall824c2f52010-09-14 07:57:04 +0000964}
965
Anders Carlssoncc52f652009-09-22 22:53:17 +0000966llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
John McCall75f94982011-03-07 03:12:35 +0000967 // The element type being allocated.
968 QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
John McCall8ed55a52010-09-02 09:58:18 +0000969
John McCall75f94982011-03-07 03:12:35 +0000970 // 1. Build a call to the allocation function.
971 FunctionDecl *allocator = E->getOperatorNew();
972 const FunctionProtoType *allocatorType =
973 allocator->getType()->castAs<FunctionProtoType>();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000974
John McCall75f94982011-03-07 03:12:35 +0000975 CallArgList allocatorArgs;
Anders Carlssoncc52f652009-09-22 22:53:17 +0000976
977 // The allocation size is the first argument.
John McCall75f94982011-03-07 03:12:35 +0000978 QualType sizeType = getContext().getSizeType();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000979
John McCall75f94982011-03-07 03:12:35 +0000980 llvm::Value *numElements = 0;
981 llvm::Value *allocSizeWithoutCookie = 0;
982 llvm::Value *allocSize =
983 EmitCXXNewAllocSize(getContext(), *this, E, numElements,
984 allocSizeWithoutCookie);
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000985
Eli Friedman43dca6a2011-05-02 17:57:46 +0000986 allocatorArgs.add(RValue::get(allocSize), sizeType);
Anders Carlssoncc52f652009-09-22 22:53:17 +0000987
988 // Emit the rest of the arguments.
989 // FIXME: Ideally, this should just use EmitCallArgs.
John McCall75f94982011-03-07 03:12:35 +0000990 CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000991
992 // First, use the types from the function type.
993 // We start at 1 here because the first argument (the allocation size)
994 // has already been emitted.
John McCall75f94982011-03-07 03:12:35 +0000995 for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
996 ++i, ++placementArg) {
997 QualType argType = allocatorType->getArgType(i);
Anders Carlssoncc52f652009-09-22 22:53:17 +0000998
John McCall75f94982011-03-07 03:12:35 +0000999 assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
1000 placementArg->getType()) &&
Anders Carlssoncc52f652009-09-22 22:53:17 +00001001 "type mismatch in call argument!");
1002
John McCall32ea9692011-03-11 20:59:21 +00001003 EmitCallArg(allocatorArgs, *placementArg, argType);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001004 }
1005
1006 // Either we've emitted all the call args, or we have a call to a
1007 // variadic function.
John McCall75f94982011-03-07 03:12:35 +00001008 assert((placementArg == E->placement_arg_end() ||
1009 allocatorType->isVariadic()) &&
1010 "Extra arguments to non-variadic function!");
Anders Carlssoncc52f652009-09-22 22:53:17 +00001011
1012 // If we still have any arguments, emit them using the type of the argument.
John McCall75f94982011-03-07 03:12:35 +00001013 for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
1014 placementArg != placementArgsEnd; ++placementArg) {
John McCall32ea9692011-03-11 20:59:21 +00001015 EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
Anders Carlssoncc52f652009-09-22 22:53:17 +00001016 }
1017
John McCall75f94982011-03-07 03:12:35 +00001018 // Emit the allocation call.
Anders Carlssoncc52f652009-09-22 22:53:17 +00001019 RValue RV =
John McCall75f94982011-03-07 03:12:35 +00001020 EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType),
1021 CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
1022 allocatorArgs, allocator);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001023
John McCall75f94982011-03-07 03:12:35 +00001024 // Emit a null check on the allocation result if the allocation
1025 // function is allowed to return null (because it has a non-throwing
1026 // exception spec; for this part, we inline
1027 // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1028 // interesting initializer.
Sebastian Redl31ad7542011-03-13 17:09:40 +00001029 bool nullCheck = allocatorType->isNothrow(getContext()) &&
John McCall75f94982011-03-07 03:12:35 +00001030 !(allocType->isPODType() && !E->hasInitializer());
Anders Carlssoncc52f652009-09-22 22:53:17 +00001031
John McCall75f94982011-03-07 03:12:35 +00001032 llvm::BasicBlock *nullCheckBB = 0;
1033 llvm::BasicBlock *contBB = 0;
Anders Carlssoncc52f652009-09-22 22:53:17 +00001034
John McCall75f94982011-03-07 03:12:35 +00001035 llvm::Value *allocation = RV.getScalarVal();
1036 unsigned AS =
1037 cast<llvm::PointerType>(allocation->getType())->getAddressSpace();
Anders Carlssoncc52f652009-09-22 22:53:17 +00001038
John McCallf7dcf322011-03-07 01:52:56 +00001039 // The null-check means that the initializer is conditionally
1040 // evaluated.
1041 ConditionalEvaluation conditional(*this);
1042
John McCall75f94982011-03-07 03:12:35 +00001043 if (nullCheck) {
John McCallf7dcf322011-03-07 01:52:56 +00001044 conditional.begin(*this);
John McCall75f94982011-03-07 03:12:35 +00001045
1046 nullCheckBB = Builder.GetInsertBlock();
1047 llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1048 contBB = createBasicBlock("new.cont");
1049
1050 llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1051 Builder.CreateCondBr(isNull, contBB, notNullBB);
1052 EmitBlock(notNullBB);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001053 }
Ken Dyck3eb55cf2010-01-26 19:44:24 +00001054
John McCall75f94982011-03-07 03:12:35 +00001055 assert((allocSize == allocSizeWithoutCookie) ==
John McCall8ed55a52010-09-02 09:58:18 +00001056 CalculateCookiePadding(*this, E).isZero());
John McCall75f94982011-03-07 03:12:35 +00001057 if (allocSize != allocSizeWithoutCookie) {
John McCall8ed55a52010-09-02 09:58:18 +00001058 assert(E->isArray());
John McCall75f94982011-03-07 03:12:35 +00001059 allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1060 numElements,
1061 E, allocType);
John McCall8ed55a52010-09-02 09:58:18 +00001062 }
Anders Carlssonf7716812009-09-23 18:59:48 +00001063
John McCall824c2f52010-09-14 07:57:04 +00001064 // If there's an operator delete, enter a cleanup to call it if an
1065 // exception is thrown.
John McCall75f94982011-03-07 03:12:35 +00001066 EHScopeStack::stable_iterator operatorDeleteCleanup;
John McCall824c2f52010-09-14 07:57:04 +00001067 if (E->getOperatorDelete()) {
John McCall75f94982011-03-07 03:12:35 +00001068 EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1069 operatorDeleteCleanup = EHStack.stable_begin();
John McCall824c2f52010-09-14 07:57:04 +00001070 }
1071
John McCall75f94982011-03-07 03:12:35 +00001072 const llvm::Type *elementPtrTy
1073 = ConvertTypeForMem(allocType)->getPointerTo(AS);
1074 llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
John McCall824c2f52010-09-14 07:57:04 +00001075
John McCall8ed55a52010-09-02 09:58:18 +00001076 if (E->isArray()) {
John McCall75f94982011-03-07 03:12:35 +00001077 EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
John McCall8ed55a52010-09-02 09:58:18 +00001078
1079 // NewPtr is a pointer to the base element type. If we're
1080 // allocating an array of arrays, we'll need to cast back to the
1081 // array pointer type.
John McCall75f94982011-03-07 03:12:35 +00001082 const llvm::Type *resultType = ConvertTypeForMem(E->getType());
1083 if (result->getType() != resultType)
1084 result = Builder.CreateBitCast(result, resultType);
John McCall8ed55a52010-09-02 09:58:18 +00001085 } else {
John McCall75f94982011-03-07 03:12:35 +00001086 EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
Fariborz Jahanian47b46292010-03-24 16:57:01 +00001087 }
John McCall824c2f52010-09-14 07:57:04 +00001088
1089 // Deactivate the 'operator delete' cleanup if we finished
1090 // initialization.
John McCall75f94982011-03-07 03:12:35 +00001091 if (operatorDeleteCleanup.isValid())
1092 DeactivateCleanupBlock(operatorDeleteCleanup);
Fariborz Jahanian47b46292010-03-24 16:57:01 +00001093
John McCall75f94982011-03-07 03:12:35 +00001094 if (nullCheck) {
John McCallf7dcf322011-03-07 01:52:56 +00001095 conditional.end(*this);
1096
John McCall75f94982011-03-07 03:12:35 +00001097 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1098 EmitBlock(contBB);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001099
Jay Foad20c0f022011-03-30 11:28:58 +00001100 llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
John McCall75f94982011-03-07 03:12:35 +00001101 PHI->addIncoming(result, notNullBB);
1102 PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1103 nullCheckBB);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001104
John McCall75f94982011-03-07 03:12:35 +00001105 result = PHI;
Anders Carlssoncc52f652009-09-22 22:53:17 +00001106 }
John McCall8ed55a52010-09-02 09:58:18 +00001107
John McCall75f94982011-03-07 03:12:35 +00001108 return result;
Anders Carlssoncc52f652009-09-22 22:53:17 +00001109}
1110
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001111void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1112 llvm::Value *Ptr,
1113 QualType DeleteTy) {
John McCall8ed55a52010-09-02 09:58:18 +00001114 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1115
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001116 const FunctionProtoType *DeleteFTy =
1117 DeleteFD->getType()->getAs<FunctionProtoType>();
1118
1119 CallArgList DeleteArgs;
1120
Anders Carlsson21122cf2009-12-13 20:04:38 +00001121 // Check if we need to pass the size to the delete operator.
1122 llvm::Value *Size = 0;
1123 QualType SizeTy;
1124 if (DeleteFTy->getNumArgs() == 2) {
1125 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck7df3cbe2010-01-26 19:59:28 +00001126 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1127 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1128 DeleteTypeSize.getQuantity());
Anders Carlsson21122cf2009-12-13 20:04:38 +00001129 }
1130
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001131 QualType ArgTy = DeleteFTy->getArgType(0);
1132 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
Eli Friedman43dca6a2011-05-02 17:57:46 +00001133 DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001134
Anders Carlsson21122cf2009-12-13 20:04:38 +00001135 if (Size)
Eli Friedman43dca6a2011-05-02 17:57:46 +00001136 DeleteArgs.add(RValue::get(Size), SizeTy);
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001137
1138 // Emit the call to delete.
Tilmann Scheller99cc30c2011-03-02 21:36:49 +00001139 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
Anders Carlsson61a401c2009-12-24 19:25:24 +00001140 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001141 DeleteArgs, DeleteFD);
1142}
1143
John McCall8ed55a52010-09-02 09:58:18 +00001144namespace {
1145 /// Calls the given 'operator delete' on a single object.
1146 struct CallObjectDelete : EHScopeStack::Cleanup {
1147 llvm::Value *Ptr;
1148 const FunctionDecl *OperatorDelete;
1149 QualType ElementType;
1150
1151 CallObjectDelete(llvm::Value *Ptr,
1152 const FunctionDecl *OperatorDelete,
1153 QualType ElementType)
1154 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1155
1156 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1157 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1158 }
1159 };
1160}
1161
1162/// Emit the code for deleting a single object.
1163static void EmitObjectDelete(CodeGenFunction &CGF,
1164 const FunctionDecl *OperatorDelete,
1165 llvm::Value *Ptr,
1166 QualType ElementType) {
1167 // Find the destructor for the type, if applicable. If the
1168 // destructor is virtual, we'll just emit the vcall and return.
1169 const CXXDestructorDecl *Dtor = 0;
1170 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1171 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1172 if (!RD->hasTrivialDestructor()) {
1173 Dtor = RD->getDestructor();
1174
1175 if (Dtor->isVirtual()) {
1176 const llvm::Type *Ty =
John McCall0d635f52010-09-03 01:26:39 +00001177 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor,
1178 Dtor_Complete),
John McCall8ed55a52010-09-02 09:58:18 +00001179 /*isVariadic=*/false);
1180
1181 llvm::Value *Callee
1182 = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
1183 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
1184 0, 0);
1185
1186 // The dtor took care of deleting the object.
1187 return;
1188 }
1189 }
1190 }
1191
1192 // Make sure that we call delete even if the dtor throws.
John McCalle4df6c82011-01-28 08:37:24 +00001193 // This doesn't have to a conditional cleanup because we're going
1194 // to pop it off in a second.
John McCall8ed55a52010-09-02 09:58:18 +00001195 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1196 Ptr, OperatorDelete, ElementType);
1197
1198 if (Dtor)
1199 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1200 /*ForVirtualBase=*/false, Ptr);
1201
1202 CGF.PopCleanupBlock();
1203}
1204
1205namespace {
1206 /// Calls the given 'operator delete' on an array of objects.
1207 struct CallArrayDelete : EHScopeStack::Cleanup {
1208 llvm::Value *Ptr;
1209 const FunctionDecl *OperatorDelete;
1210 llvm::Value *NumElements;
1211 QualType ElementType;
1212 CharUnits CookieSize;
1213
1214 CallArrayDelete(llvm::Value *Ptr,
1215 const FunctionDecl *OperatorDelete,
1216 llvm::Value *NumElements,
1217 QualType ElementType,
1218 CharUnits CookieSize)
1219 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1220 ElementType(ElementType), CookieSize(CookieSize) {}
1221
1222 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1223 const FunctionProtoType *DeleteFTy =
1224 OperatorDelete->getType()->getAs<FunctionProtoType>();
1225 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1226
1227 CallArgList Args;
1228
1229 // Pass the pointer as the first argument.
1230 QualType VoidPtrTy = DeleteFTy->getArgType(0);
1231 llvm::Value *DeletePtr
1232 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
Eli Friedman43dca6a2011-05-02 17:57:46 +00001233 Args.add(RValue::get(DeletePtr), VoidPtrTy);
John McCall8ed55a52010-09-02 09:58:18 +00001234
1235 // Pass the original requested size as the second argument.
1236 if (DeleteFTy->getNumArgs() == 2) {
1237 QualType size_t = DeleteFTy->getArgType(1);
1238 const llvm::IntegerType *SizeTy
1239 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1240
1241 CharUnits ElementTypeSize =
1242 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1243
1244 // The size of an element, multiplied by the number of elements.
1245 llvm::Value *Size
1246 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1247 Size = CGF.Builder.CreateMul(Size, NumElements);
1248
1249 // Plus the size of the cookie if applicable.
1250 if (!CookieSize.isZero()) {
1251 llvm::Value *CookieSizeV
1252 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1253 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1254 }
1255
Eli Friedman43dca6a2011-05-02 17:57:46 +00001256 Args.add(RValue::get(Size), size_t);
John McCall8ed55a52010-09-02 09:58:18 +00001257 }
1258
1259 // Emit the call to delete.
Tilmann Scheller99cc30c2011-03-02 21:36:49 +00001260 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
John McCall8ed55a52010-09-02 09:58:18 +00001261 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1262 ReturnValueSlot(), Args, OperatorDelete);
1263 }
1264 };
1265}
1266
1267/// Emit the code for deleting an array of objects.
1268static void EmitArrayDelete(CodeGenFunction &CGF,
John McCall284c48f2011-01-27 09:37:56 +00001269 const CXXDeleteExpr *E,
John McCall8ed55a52010-09-02 09:58:18 +00001270 llvm::Value *Ptr,
1271 QualType ElementType) {
1272 llvm::Value *NumElements = 0;
1273 llvm::Value *AllocatedPtr = 0;
1274 CharUnits CookieSize;
John McCall284c48f2011-01-27 09:37:56 +00001275 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, E, ElementType,
John McCall8ed55a52010-09-02 09:58:18 +00001276 NumElements, AllocatedPtr, CookieSize);
1277
1278 assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
1279
1280 // Make sure that we call delete even if one of the dtors throws.
John McCall284c48f2011-01-27 09:37:56 +00001281 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
John McCall8ed55a52010-09-02 09:58:18 +00001282 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1283 AllocatedPtr, OperatorDelete,
1284 NumElements, ElementType,
1285 CookieSize);
1286
1287 if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
1288 if (!RD->hasTrivialDestructor()) {
1289 assert(NumElements && "ReadArrayCookie didn't find element count"
1290 " for a class with destructor");
1291 CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
1292 }
1293 }
1294
1295 CGF.PopCleanupBlock();
1296}
1297
Anders Carlssoncc52f652009-09-22 22:53:17 +00001298void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Fariborz Jahanian6814eaa2009-11-13 19:27:47 +00001299
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001300 // Get at the argument before we performed the implicit conversion
1301 // to void*.
1302 const Expr *Arg = E->getArgument();
1303 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
John McCalle3027922010-08-25 11:45:40 +00001304 if (ICE->getCastKind() != CK_UserDefinedConversion &&
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001305 ICE->getType()->isVoidPointerType())
1306 Arg = ICE->getSubExpr();
Douglas Gregore364e7b2009-10-01 05:49:51 +00001307 else
1308 break;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001309 }
Anders Carlssoncc52f652009-09-22 22:53:17 +00001310
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001311 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001312
1313 // Null check the pointer.
1314 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1315 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1316
Anders Carlsson98981b12011-04-11 00:30:07 +00001317 llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
Anders Carlssoncc52f652009-09-22 22:53:17 +00001318
1319 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1320 EmitBlock(DeleteNotNull);
Anders Carlssone828c362009-11-13 04:45:41 +00001321
John McCall8ed55a52010-09-02 09:58:18 +00001322 // We might be deleting a pointer to array. If so, GEP down to the
1323 // first non-array element.
1324 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1325 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1326 if (DeleteTy->isConstantArrayType()) {
1327 llvm::Value *Zero = Builder.getInt32(0);
1328 llvm::SmallVector<llvm::Value*,8> GEP;
1329
1330 GEP.push_back(Zero); // point at the outermost array
1331
1332 // For each layer of array type we're pointing at:
1333 while (const ConstantArrayType *Arr
1334 = getContext().getAsConstantArrayType(DeleteTy)) {
1335 // 1. Unpeel the array type.
1336 DeleteTy = Arr->getElementType();
1337
1338 // 2. GEP to the first element of the array.
1339 GEP.push_back(Zero);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001340 }
John McCall8ed55a52010-09-02 09:58:18 +00001341
1342 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
Anders Carlssoncc52f652009-09-22 22:53:17 +00001343 }
1344
Douglas Gregor04f36212010-09-02 17:38:50 +00001345 assert(ConvertTypeForMem(DeleteTy) ==
1346 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall8ed55a52010-09-02 09:58:18 +00001347
1348 if (E->isArrayForm()) {
John McCall284c48f2011-01-27 09:37:56 +00001349 EmitArrayDelete(*this, E, Ptr, DeleteTy);
John McCall8ed55a52010-09-02 09:58:18 +00001350 } else {
1351 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1352 }
Anders Carlssoncc52f652009-09-22 22:53:17 +00001353
Anders Carlssoncc52f652009-09-22 22:53:17 +00001354 EmitBlock(DeleteEnd);
1355}
Mike Stumpc9b231c2009-11-15 08:09:41 +00001356
Anders Carlsson0c633502011-04-11 14:13:40 +00001357static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1358 // void __cxa_bad_typeid();
1359
1360 const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext());
1361 const llvm::FunctionType *FTy =
1362 llvm::FunctionType::get(VoidTy, false);
1363
1364 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1365}
1366
1367static void EmitBadTypeidCall(CodeGenFunction &CGF) {
Anders Carlssonbbe277c2011-04-13 02:35:36 +00001368 llvm::Value *Fn = getBadTypeidFn(CGF);
1369 CGF.EmitCallOrInvoke(Fn, 0, 0).setDoesNotReturn();
Anders Carlsson0c633502011-04-11 14:13:40 +00001370 CGF.Builder.CreateUnreachable();
1371}
1372
Anders Carlsson940f02d2011-04-18 00:57:03 +00001373static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
1374 const Expr *E,
1375 const llvm::Type *StdTypeInfoPtrTy) {
1376 // Get the vtable pointer.
1377 llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1378
1379 // C++ [expr.typeid]p2:
1380 // If the glvalue expression is obtained by applying the unary * operator to
1381 // a pointer and the pointer is a null pointer value, the typeid expression
1382 // throws the std::bad_typeid exception.
1383 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
1384 if (UO->getOpcode() == UO_Deref) {
1385 llvm::BasicBlock *BadTypeidBlock =
1386 CGF.createBasicBlock("typeid.bad_typeid");
1387 llvm::BasicBlock *EndBlock =
1388 CGF.createBasicBlock("typeid.end");
1389
1390 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1391 CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1392
1393 CGF.EmitBlock(BadTypeidBlock);
1394 EmitBadTypeidCall(CGF);
1395 CGF.EmitBlock(EndBlock);
1396 }
1397 }
1398
1399 llvm::Value *Value = CGF.GetVTablePtr(ThisPtr,
1400 StdTypeInfoPtrTy->getPointerTo());
1401
1402 // Load the type info.
1403 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1404 return CGF.Builder.CreateLoad(Value);
1405}
1406
John McCalle4df6c82011-01-28 08:37:24 +00001407llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
Anders Carlsson940f02d2011-04-18 00:57:03 +00001408 const llvm::Type *StdTypeInfoPtrTy =
1409 ConvertType(E->getType())->getPointerTo();
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +00001410
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001411 if (E->isTypeOperand()) {
1412 llvm::Constant *TypeInfo =
1413 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
Anders Carlsson940f02d2011-04-18 00:57:03 +00001414 return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001415 }
Anders Carlsson0c633502011-04-11 14:13:40 +00001416
Anders Carlsson940f02d2011-04-18 00:57:03 +00001417 // C++ [expr.typeid]p2:
1418 // When typeid is applied to a glvalue expression whose type is a
1419 // polymorphic class type, the result refers to a std::type_info object
1420 // representing the type of the most derived object (that is, the dynamic
1421 // type) to which the glvalue refers.
1422 if (E->getExprOperand()->isGLValue()) {
1423 if (const RecordType *RT =
1424 E->getExprOperand()->getType()->getAs<RecordType>()) {
1425 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1426 if (RD->isPolymorphic())
1427 return EmitTypeidFromVTable(*this, E->getExprOperand(),
1428 StdTypeInfoPtrTy);
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001429 }
Mike Stumpc9b231c2009-11-15 08:09:41 +00001430 }
Anders Carlsson940f02d2011-04-18 00:57:03 +00001431
1432 QualType OperandTy = E->getExprOperand()->getType();
1433 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1434 StdTypeInfoPtrTy);
Mike Stumpc9b231c2009-11-15 08:09:41 +00001435}
Mike Stump65511702009-11-16 06:50:58 +00001436
Anders Carlsson882d7902011-04-11 00:46:40 +00001437static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) {
1438 // void *__dynamic_cast(const void *sub,
1439 // const abi::__class_type_info *src,
1440 // const abi::__class_type_info *dst,
1441 // std::ptrdiff_t src2dst_offset);
1442
1443 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
1444 const llvm::Type *PtrDiffTy =
1445 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1446
1447 const llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1448
1449 const llvm::FunctionType *FTy =
1450 llvm::FunctionType::get(Int8PtrTy, Args, false);
1451
1452 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast");
1453}
1454
1455static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1456 // void __cxa_bad_cast();
1457
1458 const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext());
1459 const llvm::FunctionType *FTy =
1460 llvm::FunctionType::get(VoidTy, false);
1461
1462 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1463}
1464
Anders Carlssonc1c99712011-04-11 01:45:29 +00001465static void EmitBadCastCall(CodeGenFunction &CGF) {
Anders Carlssonbbe277c2011-04-13 02:35:36 +00001466 llvm::Value *Fn = getBadCastFn(CGF);
1467 CGF.EmitCallOrInvoke(Fn, 0, 0).setDoesNotReturn();
Anders Carlssonc1c99712011-04-11 01:45:29 +00001468 CGF.Builder.CreateUnreachable();
1469}
1470
Anders Carlsson882d7902011-04-11 00:46:40 +00001471static llvm::Value *
1472EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
1473 QualType SrcTy, QualType DestTy,
1474 llvm::BasicBlock *CastEnd) {
1475 const llvm::Type *PtrDiffLTy =
1476 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1477 const llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1478
1479 if (const PointerType *PTy = DestTy->getAs<PointerType>()) {
1480 if (PTy->getPointeeType()->isVoidType()) {
1481 // C++ [expr.dynamic.cast]p7:
1482 // If T is "pointer to cv void," then the result is a pointer to the
1483 // most derived object pointed to by v.
1484
1485 // Get the vtable pointer.
1486 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1487
1488 // Get the offset-to-top from the vtable.
1489 llvm::Value *OffsetToTop =
1490 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1491 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1492
1493 // Finally, add the offset to the pointer.
1494 Value = CGF.EmitCastToVoidPtr(Value);
1495 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1496
1497 return CGF.Builder.CreateBitCast(Value, DestLTy);
1498 }
1499 }
1500
1501 QualType SrcRecordTy;
1502 QualType DestRecordTy;
1503
1504 if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) {
1505 SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1506 DestRecordTy = DestPTy->getPointeeType();
1507 } else {
1508 SrcRecordTy = SrcTy;
1509 DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1510 }
1511
1512 assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1513 assert(DestRecordTy->isRecordType() && "dest type must be a record type!");
1514
1515 llvm::Value *SrcRTTI =
1516 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1517 llvm::Value *DestRTTI =
1518 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1519
1520 // FIXME: Actually compute a hint here.
1521 llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL);
1522
1523 // Emit the call to __dynamic_cast.
1524 Value = CGF.EmitCastToVoidPtr(Value);
1525 Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value,
1526 SrcRTTI, DestRTTI, OffsetHint);
1527 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1528
1529 /// C++ [expr.dynamic.cast]p9:
1530 /// A failed cast to reference type throws std::bad_cast
1531 if (DestTy->isReferenceType()) {
1532 llvm::BasicBlock *BadCastBlock =
1533 CGF.createBasicBlock("dynamic_cast.bad_cast");
1534
1535 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1536 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1537
1538 CGF.EmitBlock(BadCastBlock);
Anders Carlssonc1c99712011-04-11 01:45:29 +00001539 EmitBadCastCall(CGF);
Anders Carlsson882d7902011-04-11 00:46:40 +00001540 }
1541
1542 return Value;
1543}
1544
Anders Carlssonc1c99712011-04-11 01:45:29 +00001545static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1546 QualType DestTy) {
1547 const llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1548 if (DestTy->isPointerType())
1549 return llvm::Constant::getNullValue(DestLTy);
1550
1551 /// C++ [expr.dynamic.cast]p9:
1552 /// A failed cast to reference type throws std::bad_cast
1553 EmitBadCastCall(CGF);
1554
1555 CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1556 return llvm::UndefValue::get(DestLTy);
1557}
1558
Anders Carlsson882d7902011-04-11 00:46:40 +00001559llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
Mike Stump65511702009-11-16 06:50:58 +00001560 const CXXDynamicCastExpr *DCE) {
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001561 QualType DestTy = DCE->getTypeAsWritten();
Anders Carlsson882d7902011-04-11 00:46:40 +00001562
Anders Carlssonc1c99712011-04-11 01:45:29 +00001563 if (DCE->isAlwaysNull())
1564 return EmitDynamicCastToNull(*this, DestTy);
1565
1566 QualType SrcTy = DCE->getSubExpr()->getType();
1567
Anders Carlsson882d7902011-04-11 00:46:40 +00001568 // C++ [expr.dynamic.cast]p4:
1569 // If the value of v is a null pointer value in the pointer case, the result
1570 // is the null pointer value of type T.
1571 bool ShouldNullCheckSrcValue = SrcTy->isPointerType();
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001572
Anders Carlsson882d7902011-04-11 00:46:40 +00001573 llvm::BasicBlock *CastNull = 0;
1574 llvm::BasicBlock *CastNotNull = 0;
1575 llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
Mike Stump65511702009-11-16 06:50:58 +00001576
Anders Carlsson882d7902011-04-11 00:46:40 +00001577 if (ShouldNullCheckSrcValue) {
1578 CastNull = createBasicBlock("dynamic_cast.null");
1579 CastNotNull = createBasicBlock("dynamic_cast.notnull");
1580
1581 llvm::Value *IsNull = Builder.CreateIsNull(Value);
1582 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1583 EmitBlock(CastNotNull);
Mike Stump65511702009-11-16 06:50:58 +00001584 }
1585
Anders Carlsson882d7902011-04-11 00:46:40 +00001586 Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd);
1587
1588 if (ShouldNullCheckSrcValue) {
1589 EmitBranch(CastEnd);
1590
1591 EmitBlock(CastNull);
1592 EmitBranch(CastEnd);
1593 }
1594
1595 EmitBlock(CastEnd);
1596
1597 if (ShouldNullCheckSrcValue) {
1598 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1599 PHI->addIncoming(Value, CastNotNull);
1600 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1601
1602 Value = PHI;
1603 }
1604
1605 return Value;
Mike Stump65511702009-11-16 06:50:58 +00001606}