blob: 10e29d199c3029187f32fea59abe880443a48bc7 [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
14#include "CodeGenFunction.h"
John McCall5d865c322010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Fariborz Jahanian60d215b2010-05-20 21:38:57 +000016#include "CGObjCRuntime.h"
Chris Lattner26008e02010-07-20 20:19:24 +000017#include "llvm/Intrinsics.h"
Anders Carlssoncc52f652009-09-22 22:53:17 +000018using namespace clang;
19using namespace CodeGen;
20
Anders Carlsson27da15b2010-01-01 20:29:01 +000021RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
22 llvm::Value *Callee,
23 ReturnValueSlot ReturnValue,
24 llvm::Value *This,
Anders Carlssone36a6b32010-01-02 01:01:18 +000025 llvm::Value *VTT,
Anders Carlsson27da15b2010-01-01 20:29:01 +000026 CallExpr::const_arg_iterator ArgBeg,
27 CallExpr::const_arg_iterator ArgEnd) {
28 assert(MD->isInstance() &&
29 "Trying to emit a member call expr on a static method!");
30
31 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
32
33 CallArgList Args;
34
35 // Push the this ptr.
36 Args.push_back(std::make_pair(RValue::get(This),
37 MD->getThisType(getContext())));
38
Anders Carlssone36a6b32010-01-02 01:01:18 +000039 // If there is a VTT parameter, emit it.
40 if (VTT) {
41 QualType T = getContext().getPointerType(getContext().VoidPtrTy);
42 Args.push_back(std::make_pair(RValue::get(VTT), T));
43 }
44
Anders Carlsson27da15b2010-01-01 20:29:01 +000045 // And the rest of the call args
46 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
47
John McCallab26cfa2010-02-05 21:31:56 +000048 QualType ResultType = FPT->getResultType();
49 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +000050 FPT->getExtInfo()),
51 Callee, ReturnValue, Args, MD);
Anders Carlsson27da15b2010-01-01 20:29:01 +000052}
53
54/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
55/// expr can be devirtualized.
56static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
57 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
58 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
59 // This is a record decl. We know the type and can devirtualize it.
60 return VD->getType()->isRecordType();
61 }
62
63 return false;
64 }
65
66 // We can always devirtualize calls on temporary object expressions.
Eli Friedmana6824272010-01-31 20:58:15 +000067 if (isa<CXXConstructExpr>(Base))
Anders Carlsson27da15b2010-01-01 20:29:01 +000068 return true;
69
70 // And calls on bound temporaries.
71 if (isa<CXXBindTemporaryExpr>(Base))
72 return true;
73
74 // Check if this is a call expr that returns a record type.
75 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
76 return CE->getCallReturnType()->isRecordType();
77
78 // We can't devirtualize the call.
79 return false;
80}
81
82RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
83 ReturnValueSlot ReturnValue) {
84 if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens()))
85 return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
86
87 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens());
88 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
89
90 if (MD->isStatic()) {
91 // The method is static, emit it as we would a regular call.
92 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
93 return EmitCall(getContext().getPointerType(MD->getType()), Callee,
94 ReturnValue, CE->arg_begin(), CE->arg_end());
95 }
Anders Carlsson27da15b2010-01-01 20:29:01 +000096
John McCall0d635f52010-09-03 01:26:39 +000097 // Compute the object pointer.
Anders Carlsson27da15b2010-01-01 20:29:01 +000098 llvm::Value *This;
Anders Carlsson27da15b2010-01-01 20:29:01 +000099 if (ME->isArrow())
100 This = EmitScalarExpr(ME->getBase());
101 else {
102 LValue BaseLV = EmitLValue(ME->getBase());
Fariborz Jahanianf93ac892010-09-10 18:56:35 +0000103 if (BaseLV.isPropertyRef() || BaseLV.isKVCRef()) {
104 QualType QT = ME->getBase()->getType();
105 RValue RV =
106 BaseLV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(BaseLV, QT)
107 : EmitLoadOfKVCRefLValue(BaseLV, QT);
108 This = RV.isScalar() ? RV.getScalarVal() : RV.getAggregateAddr();
109 }
110 else
111 This = BaseLV.getAddress();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000112 }
113
John McCall0d635f52010-09-03 01:26:39 +0000114 if (MD->isTrivial()) {
115 if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
116
117 assert(MD->isCopyAssignment() && "unknown trivial member function");
Anders Carlsson27da15b2010-01-01 20:29:01 +0000118 // We don't like to generate the trivial copy assignment operator when
119 // it isn't necessary; just produce the proper effect here.
120 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
121 EmitAggregateCopy(This, RHS, CE->getType());
122 return RValue::get(This);
123 }
124
John McCall0d635f52010-09-03 01:26:39 +0000125 // Compute the function type we're calling.
126 const CGFunctionInfo &FInfo =
127 (isa<CXXDestructorDecl>(MD)
128 ? CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
129 Dtor_Complete)
130 : CGM.getTypes().getFunctionInfo(MD));
131
132 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
133 const llvm::Type *Ty
134 = CGM.getTypes().GetFunctionType(FInfo, FPT->isVariadic());
135
Anders Carlsson27da15b2010-01-01 20:29:01 +0000136 // C++ [class.virtual]p12:
137 // Explicit qualification with the scope operator (5.1) suppresses the
138 // virtual call mechanism.
139 //
140 // We also don't emit a virtual call if the base expression has a record type
141 // because then we know what the type is.
John McCall0d635f52010-09-03 01:26:39 +0000142 bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
143 && !canDevirtualizeMemberFunctionCalls(ME->getBase());
144
Anders Carlsson27da15b2010-01-01 20:29:01 +0000145 llvm::Value *Callee;
John McCall0d635f52010-09-03 01:26:39 +0000146 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
147 if (UseVirtualCall) {
148 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000149 } else {
John McCall0d635f52010-09-03 01:26:39 +0000150 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000151 }
John McCall0d635f52010-09-03 01:26:39 +0000152 } else if (UseVirtualCall) {
Anders Carlsson27da15b2010-01-01 20:29:01 +0000153 Callee = BuildVirtualCall(MD, This, Ty);
154 } else {
155 Callee = CGM.GetAddrOfFunction(MD, Ty);
156 }
157
Anders Carlssone36a6b32010-01-02 01:01:18 +0000158 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000159 CE->arg_begin(), CE->arg_end());
160}
161
162RValue
163CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
164 ReturnValueSlot ReturnValue) {
165 const BinaryOperator *BO =
166 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
167 const Expr *BaseExpr = BO->getLHS();
168 const Expr *MemFnExpr = BO->getRHS();
169
170 const MemberPointerType *MPT =
171 MemFnExpr->getType()->getAs<MemberPointerType>();
John McCall475999d2010-08-22 00:05:51 +0000172
Anders Carlsson27da15b2010-01-01 20:29:01 +0000173 const FunctionProtoType *FPT =
174 MPT->getPointeeType()->getAs<FunctionProtoType>();
175 const CXXRecordDecl *RD =
176 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
177
Anders Carlsson27da15b2010-01-01 20:29:01 +0000178 // Get the member function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000179 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000180
181 // Emit the 'this' pointer.
182 llvm::Value *This;
183
John McCalle3027922010-08-25 11:45:40 +0000184 if (BO->getOpcode() == BO_PtrMemI)
Anders Carlsson27da15b2010-01-01 20:29:01 +0000185 This = EmitScalarExpr(BaseExpr);
186 else
187 This = EmitLValue(BaseExpr).getAddress();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000188
John McCall475999d2010-08-22 00:05:51 +0000189 // Ask the ABI to load the callee. Note that This is modified.
190 llvm::Value *Callee =
191 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(CGF, This, MemFnPtr, MPT);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000192
Anders Carlsson27da15b2010-01-01 20:29:01 +0000193 CallArgList Args;
194
195 QualType ThisType =
196 getContext().getPointerType(getContext().getTagDeclType(RD));
197
198 // Push the this ptr.
199 Args.push_back(std::make_pair(RValue::get(This), ThisType));
200
201 // And the rest of the call args
202 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
John McCallab26cfa2010-02-05 21:31:56 +0000203 const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>();
204 return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000205 ReturnValue, Args);
206}
207
208RValue
209CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
210 const CXXMethodDecl *MD,
211 ReturnValueSlot ReturnValue) {
212 assert(MD->isInstance() &&
213 "Trying to emit a member call expr on a static method!");
Anders Carlsson27da15b2010-01-01 20:29:01 +0000214 if (MD->isCopyAssignment()) {
215 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
216 if (ClassDecl->hasTrivialCopyAssignment()) {
217 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
218 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
Fariborz Jahanian43a40f92010-05-10 22:57:35 +0000219 LValue LV = EmitLValue(E->getArg(0));
220 llvm::Value *This;
Fariborz Jahanian61a31242010-09-01 19:36:41 +0000221 if (LV.isPropertyRef() || LV.isKVCRef()) {
Fariborz Jahanian1b8b8bf2010-05-16 00:10:46 +0000222 llvm::Value *AggLoc = CreateMemTemp(E->getArg(1)->getType());
Fariborz Jahaniane1b45a52010-05-15 23:05:52 +0000223 EmitAggExpr(E->getArg(1), AggLoc, false /*VolatileDest*/);
Fariborz Jahanian61a31242010-09-01 19:36:41 +0000224 if (LV.isPropertyRef())
225 EmitObjCPropertySet(LV.getPropertyRefExpr(),
226 RValue::getAggregate(AggLoc,
227 false /*VolatileDest*/));
228 else
229 EmitObjCPropertySet(LV.getKVCRefExpr(),
230 RValue::getAggregate(AggLoc,
231 false /*VolatileDest*/));
Fariborz Jahaniane1b45a52010-05-15 23:05:52 +0000232 return RValue::getAggregate(0, false);
Fariborz Jahanian43a40f92010-05-10 22:57:35 +0000233 }
234 else
235 This = LV.getAddress();
236
Anders Carlsson27da15b2010-01-01 20:29:01 +0000237 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
238 QualType Ty = E->getType();
Fariborz Jahanian021510e2010-06-15 22:44:06 +0000239 EmitAggregateCopy(This, Src, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000240 return RValue::get(This);
241 }
242 }
243
244 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
245 const llvm::Type *Ty =
246 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
247 FPT->isVariadic());
Fariborz Jahanianfdf474b2010-05-07 18:56:13 +0000248 LValue LV = EmitLValue(E->getArg(0));
249 llvm::Value *This;
Fariborz Jahanian61a31242010-09-01 19:36:41 +0000250 if (LV.isPropertyRef() || LV.isKVCRef()) {
251 QualType QT = E->getArg(0)->getType();
252 RValue RV =
253 LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT)
254 : EmitLoadOfKVCRefLValue(LV, QT);
Fariborz Jahanian6855ba22010-05-20 16:46:55 +0000255 assert (!RV.isScalar() && "EmitCXXOperatorMemberCallExpr");
256 This = RV.getAggregateAddr();
Fariborz Jahanianfdf474b2010-05-07 18:56:13 +0000257 }
258 else
259 This = LV.getAddress();
Anders Carlsson27da15b2010-01-01 20:29:01 +0000260
261 llvm::Value *Callee;
262 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
263 Callee = BuildVirtualCall(MD, This, Ty);
264 else
265 Callee = CGM.GetAddrOfFunction(MD, Ty);
266
Anders Carlssone36a6b32010-01-02 01:01:18 +0000267 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000268 E->arg_begin() + 1, E->arg_end());
269}
270
271void
272CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
273 const CXXConstructExpr *E) {
274 assert(Dest && "Must have a destination!");
275 const CXXConstructorDecl *CD = E->getConstructor();
Douglas Gregor630c76e2010-08-22 16:15:35 +0000276
277 // If we require zero initialization before (or instead of) calling the
278 // constructor, as can be the case with a non-user-provided default
279 // constructor, emit the zero initialization now.
280 if (E->requiresZeroInitialization())
281 EmitNullInitialization(Dest, E->getType());
282
283
284 // If this is a call to a trivial default constructor, do nothing.
285 if (CD->isTrivial() && CD->isDefaultConstructor())
286 return;
287
Anders Carlsson27da15b2010-01-01 20:29:01 +0000288 // Code gen optimization to eliminate copy constructor and return
Douglas Gregor222cf0e2010-05-15 00:13:29 +0000289 // its first argument instead, if in fact that argument is a temporary
290 // object.
Anders Carlsson27da15b2010-01-01 20:29:01 +0000291 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Douglas Gregor222cf0e2010-05-15 00:13:29 +0000292 if (const Expr *Arg = E->getArg(0)->getTemporaryObject()) {
293 EmitAggExpr(Arg, Dest, false);
294 return;
295 }
Anders Carlsson27da15b2010-01-01 20:29:01 +0000296 }
Douglas Gregor630c76e2010-08-22 16:15:35 +0000297
298 const ConstantArrayType *Array
299 = getContext().getAsConstantArrayType(E->getType());
Anders Carlsson27da15b2010-01-01 20:29:01 +0000300 if (Array) {
301 QualType BaseElementTy = getContext().getBaseElementType(Array);
302 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
303 BasePtr = llvm::PointerType::getUnqual(BasePtr);
304 llvm::Value *BaseAddrPtr =
Anders Carlssonaab3b572010-05-01 17:02:18 +0000305 Builder.CreateBitCast(Dest, BasePtr);
Anders Carlsson27da15b2010-01-01 20:29:01 +0000306
307 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
308 E->arg_begin(), E->arg_end());
309 }
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000310 else {
311 CXXCtorType Type =
312 (E->getConstructionKind() == CXXConstructExpr::CK_Complete)
313 ? Ctor_Complete : Ctor_Base;
314 bool ForVirtualBase =
315 E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
316
Anders Carlsson27da15b2010-01-01 20:29:01 +0000317 // Call the constructor.
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000318 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest,
Anders Carlsson27da15b2010-01-01 20:29:01 +0000319 E->arg_begin(), E->arg_end());
Anders Carlssone11f9ce2010-05-02 23:20:53 +0000320 }
Anders Carlsson27da15b2010-01-01 20:29:01 +0000321}
322
John McCallaa4149a2010-08-23 01:17:59 +0000323/// Check whether the given operator new[] is the global placement
324/// operator new[].
325static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
326 const FunctionDecl *Fn) {
327 // Must be in global scope. Note that allocation functions can't be
328 // declared in namespaces.
Sebastian Redl50c68252010-08-31 00:36:30 +0000329 if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
John McCallaa4149a2010-08-23 01:17:59 +0000330 return false;
331
332 // Signature must be void *operator new[](size_t, void*).
333 // The size_t is common to all operator new[]s.
334 if (Fn->getNumParams() != 2)
335 return false;
336
337 CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
338 return (ParamType == Ctx.VoidPtrTy);
339}
340
John McCall8ed55a52010-09-02 09:58:18 +0000341static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
342 const CXXNewExpr *E) {
Anders Carlsson21122cf2009-12-13 20:04:38 +0000343 if (!E->isArray())
Ken Dyck3eb55cf2010-01-26 19:44:24 +0000344 return CharUnits::Zero();
Anders Carlsson21122cf2009-12-13 20:04:38 +0000345
Anders Carlsson399f4992009-12-13 20:34:34 +0000346 // No cookie is required if the new operator being used is
347 // ::operator new[](size_t, void*).
348 const FunctionDecl *OperatorNew = E->getOperatorNew();
John McCall8ed55a52010-09-02 09:58:18 +0000349 if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
John McCallaa4149a2010-08-23 01:17:59 +0000350 return CharUnits::Zero();
351
John McCall8ed55a52010-09-02 09:58:18 +0000352 return CGF.CGM.getCXXABI().GetArrayCookieSize(E->getAllocatedType());
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000353}
354
Fariborz Jahanian47b46292010-03-24 16:57:01 +0000355static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
Chris Lattnercb46bdc2010-07-20 18:45:57 +0000356 CodeGenFunction &CGF,
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000357 const CXXNewExpr *E,
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000358 llvm::Value *&NumElements,
359 llvm::Value *&SizeWithoutCookie) {
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000360 QualType ElemType = E->getAllocatedType();
John McCall8ed55a52010-09-02 09:58:18 +0000361
362 const llvm::IntegerType *SizeTy =
363 cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000364
John McCall8ed55a52010-09-02 09:58:18 +0000365 CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
366
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000367 if (!E->isArray()) {
368 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
369 return SizeWithoutCookie;
370 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000371
John McCall8ed55a52010-09-02 09:58:18 +0000372 // Figure out the cookie size.
373 CharUnits CookieSize = CalculateCookiePadding(CGF, E);
374
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000375 // Emit the array size expression.
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000376 // We multiply the size of all dimensions for NumElements.
377 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000378 NumElements = CGF.EmitScalarExpr(E->getArraySize());
John McCall8ed55a52010-09-02 09:58:18 +0000379 assert(NumElements->getType() == SizeTy && "element count not a size_t");
380
381 uint64_t ArraySizeMultiplier = 1;
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000382 while (const ConstantArrayType *CAT
383 = CGF.getContext().getAsConstantArrayType(ElemType)) {
384 ElemType = CAT->getElementType();
John McCall8ed55a52010-09-02 09:58:18 +0000385 ArraySizeMultiplier *= CAT->getSize().getZExtValue();
Argyrios Kyrtzidis7648fb42010-08-26 15:23:38 +0000386 }
387
John McCall8ed55a52010-09-02 09:58:18 +0000388 llvm::Value *Size;
Chris Lattnerf2f38702010-07-20 21:07:09 +0000389
Chris Lattner32ac5832010-07-20 21:55:52 +0000390 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
391 // Don't bloat the -O0 code.
392 if (llvm::ConstantInt *NumElementsC =
393 dyn_cast<llvm::ConstantInt>(NumElements)) {
Chris Lattner32ac5832010-07-20 21:55:52 +0000394 llvm::APInt NEC = NumElementsC->getValue();
John McCall8ed55a52010-09-02 09:58:18 +0000395 unsigned SizeWidth = NEC.getBitWidth();
396
397 // Determine if there is an overflow here by doing an extended multiply.
398 NEC.zext(SizeWidth*2);
399 llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
Chris Lattner32ac5832010-07-20 21:55:52 +0000400 SC *= NEC;
John McCall8ed55a52010-09-02 09:58:18 +0000401
402 if (!CookieSize.isZero()) {
403 // Save the current size without a cookie. We don't care if an
404 // overflow's already happened because SizeWithoutCookie isn't
405 // used if the allocator returns null or throws, as it should
406 // always do on an overflow.
407 llvm::APInt SWC = SC;
408 SWC.trunc(SizeWidth);
409 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
410
411 // Add the cookie size.
412 SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
Chris Lattner32ac5832010-07-20 21:55:52 +0000413 }
414
John McCall8ed55a52010-09-02 09:58:18 +0000415 if (SC.countLeadingZeros() >= SizeWidth) {
416 SC.trunc(SizeWidth);
417 Size = llvm::ConstantInt::get(SizeTy, SC);
418 } else {
419 // On overflow, produce a -1 so operator new throws.
420 Size = llvm::Constant::getAllOnesValue(SizeTy);
421 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000422
John McCall8ed55a52010-09-02 09:58:18 +0000423 // Scale NumElements while we're at it.
424 uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
425 NumElements = llvm::ConstantInt::get(SizeTy, N);
426
427 // Otherwise, we don't need to do an overflow-checked multiplication if
428 // we're multiplying by one.
429 } else if (TypeSize.isOne()) {
430 assert(ArraySizeMultiplier == 1);
431
432 Size = NumElements;
433
434 // If we need a cookie, add its size in with an overflow check.
435 // This is maybe a little paranoid.
436 if (!CookieSize.isZero()) {
437 SizeWithoutCookie = Size;
438
439 llvm::Value *CookieSizeV
440 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
441
442 const llvm::Type *Types[] = { SizeTy };
443 llvm::Value *UAddF
444 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
445 llvm::Value *AddRes
446 = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
447
448 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
449 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
450 Size = CGF.Builder.CreateSelect(DidOverflow,
451 llvm::ConstantInt::get(SizeTy, -1),
452 Size);
453 }
454
455 // Otherwise use the int.umul.with.overflow intrinsic.
456 } else {
457 llvm::Value *OutermostElementSize
458 = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
459
460 llvm::Value *NumOutermostElements = NumElements;
461
462 // Scale NumElements by the array size multiplier. This might
463 // overflow, but only if the multiplication below also overflows,
464 // in which case this multiplication isn't used.
465 if (ArraySizeMultiplier != 1)
466 NumElements = CGF.Builder.CreateMul(NumElements,
467 llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
468
469 // The requested size of the outermost array is non-constant.
470 // Multiply that by the static size of the elements of that array;
471 // on unsigned overflow, set the size to -1 to trigger an
472 // exception from the allocation routine. This is sufficient to
473 // prevent buffer overruns from the allocator returning a
474 // seemingly valid pointer to insufficient space. This idea comes
475 // originally from MSVC, and GCC has an open bug requesting
476 // similar behavior:
477 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
478 //
479 // This will not be sufficient for C++0x, which requires a
480 // specific exception class (std::bad_array_new_length).
481 // That will require ABI support that has not yet been specified.
482 const llvm::Type *Types[] = { SizeTy };
483 llvm::Value *UMulF
484 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
485 llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
486 OutermostElementSize);
487
488 // The overflow bit.
489 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
490
491 // The result of the multiplication.
492 Size = CGF.Builder.CreateExtractValue(MulRes, 0);
493
494 // If we have a cookie, we need to add that size in, too.
495 if (!CookieSize.isZero()) {
496 SizeWithoutCookie = Size;
497
498 llvm::Value *CookieSizeV
499 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
500 llvm::Value *UAddF
501 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
502 llvm::Value *AddRes
503 = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
504
505 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
506
507 llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
508 DidOverflow = CGF.Builder.CreateAnd(DidOverflow, AddDidOverflow);
509 }
510
511 Size = CGF.Builder.CreateSelect(DidOverflow,
512 llvm::ConstantInt::get(SizeTy, -1),
513 Size);
Chris Lattner32ac5832010-07-20 21:55:52 +0000514 }
John McCall8ed55a52010-09-02 09:58:18 +0000515
516 if (CookieSize.isZero())
517 SizeWithoutCookie = Size;
518 else
519 assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
520
Chris Lattner32ac5832010-07-20 21:55:52 +0000521 return Size;
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000522}
523
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000524static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
525 llvm::Value *NewPtr) {
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000526
527 assert(E->getNumConstructorArgs() == 1 &&
528 "Can only have one argument to initializer of POD type.");
529
530 const Expr *Init = E->getConstructorArg(0);
531 QualType AllocType = E->getAllocatedType();
Daniel Dunbar03816342010-08-21 02:24:36 +0000532
533 unsigned Alignment =
534 CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000535 if (!CGF.hasAggregateLLVMType(AllocType))
536 CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
Daniel Dunbar03816342010-08-21 02:24:36 +0000537 AllocType.isVolatileQualified(), Alignment,
538 AllocType);
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000539 else if (AllocType->isAnyComplexType())
540 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
541 AllocType.isVolatileQualified());
542 else
543 CGF.EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
544}
545
546void
547CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
548 llvm::Value *NewPtr,
549 llvm::Value *NumElements) {
Fariborz Jahanianb66b08e2010-06-25 20:01:13 +0000550 // We have a POD type.
551 if (E->getNumConstructorArgs() == 0)
552 return;
553
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000554 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
555
556 // Create a temporary for the loop index and initialize it with 0.
557 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
558 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
559 Builder.CreateStore(Zero, IndexPtr);
560
561 // Start the loop with a block that tests the condition.
562 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
563 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
564
565 EmitBlock(CondBlock);
566
567 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
568
569 // Generate: if (loop-index < number-of-elements fall to the loop body,
570 // otherwise, go to the block after the for-loop.
571 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
572 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
573 // If the condition is true, execute the body.
574 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
575
576 EmitBlock(ForBody);
577
578 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
579 // Inside the loop body, emit the constructor call on the array element.
580 Counter = Builder.CreateLoad(IndexPtr);
581 llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
582 "arrayidx");
583 StoreAnyExprIntoOneUnit(*this, E, Address);
584
585 EmitBlock(ContinueBlock);
586
587 // Emit the increment of the loop counter.
588 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
589 Counter = Builder.CreateLoad(IndexPtr);
590 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
591 Builder.CreateStore(NextVal, IndexPtr);
592
593 // Finally, branch back up to the condition for the next iteration.
594 EmitBranch(CondBlock);
595
596 // Emit the fall-through block.
597 EmitBlock(AfterFor, true);
598}
599
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000600static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
601 llvm::Value *NewPtr, llvm::Value *Size) {
602 llvm::LLVMContext &VMContext = CGF.CGM.getLLVMContext();
603 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
604 if (NewPtr->getType() != BP)
605 NewPtr = CGF.Builder.CreateBitCast(NewPtr, BP, "tmp");
606
607 CGF.Builder.CreateCall5(CGF.CGM.getMemSetFn(BP, CGF.IntPtrTy), NewPtr,
608 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
609 Size,
610 llvm::ConstantInt::get(CGF.Int32Ty,
611 CGF.getContext().getTypeAlign(T)/8),
612 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
613 0));
614}
615
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000616static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
617 llvm::Value *NewPtr,
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000618 llvm::Value *NumElements,
619 llvm::Value *AllocSizeWithoutCookie) {
Anders Carlsson3a202f62009-11-24 18:43:52 +0000620 if (E->isArray()) {
Anders Carlssond040e6b2010-05-03 15:09:17 +0000621 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000622 bool RequiresZeroInitialization = false;
623 if (Ctor->getParent()->hasTrivialConstructor()) {
624 // If new expression did not specify value-initialization, then there
625 // is no initialization.
626 if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
627 return;
628
John McCall614dbdc2010-08-22 21:01:12 +0000629 if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000630 // Optimization: since zero initialization will just set the memory
631 // to all zeroes, generate a single memset to do it in one shot.
632 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
633 AllocSizeWithoutCookie);
634 return;
635 }
636
637 RequiresZeroInitialization = true;
638 }
639
640 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
641 E->constructor_arg_begin(),
642 E->constructor_arg_end(),
643 RequiresZeroInitialization);
Anders Carlssond040e6b2010-05-03 15:09:17 +0000644 return;
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000645 } else if (E->getNumConstructorArgs() == 1 &&
646 isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
647 // Optimization: since zero initialization will just set the memory
648 // to all zeroes, generate a single memset to do it in one shot.
649 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
650 AllocSizeWithoutCookie);
651 return;
652 } else {
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000653 CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
654 return;
655 }
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000656 }
Anders Carlsson3a202f62009-11-24 18:43:52 +0000657
658 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor747eb782010-07-08 06:14:04 +0000659 // Per C++ [expr.new]p15, if we have an initializer, then we're performing
660 // direct initialization. C++ [dcl.init]p5 requires that we
661 // zero-initialize storage if there are no user-declared constructors.
662 if (E->hasInitializer() &&
663 !Ctor->getParent()->hasUserDeclaredConstructor() &&
664 !Ctor->getParent()->isEmpty())
665 CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
666
Douglas Gregore1823702010-07-07 23:37:33 +0000667 CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
668 NewPtr, E->constructor_arg_begin(),
669 E->constructor_arg_end());
Anders Carlsson3a202f62009-11-24 18:43:52 +0000670
671 return;
672 }
Fariborz Jahanianb66b08e2010-06-25 20:01:13 +0000673 // We have a POD type.
674 if (E->getNumConstructorArgs() == 0)
675 return;
676
Fariborz Jahaniand5202e02010-06-25 18:26:07 +0000677 StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000678}
679
John McCall824c2f52010-09-14 07:57:04 +0000680namespace {
681 /// A cleanup to call the given 'operator delete' function upon
682 /// abnormal exit from a new expression.
683 class CallDeleteDuringNew : public EHScopeStack::Cleanup {
684 size_t NumPlacementArgs;
685 const FunctionDecl *OperatorDelete;
686 llvm::Value *Ptr;
687 llvm::Value *AllocSize;
688
689 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
690
691 public:
692 static size_t getExtraSize(size_t NumPlacementArgs) {
693 return NumPlacementArgs * sizeof(RValue);
694 }
695
696 CallDeleteDuringNew(size_t NumPlacementArgs,
697 const FunctionDecl *OperatorDelete,
698 llvm::Value *Ptr,
699 llvm::Value *AllocSize)
700 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
701 Ptr(Ptr), AllocSize(AllocSize) {}
702
703 void setPlacementArg(unsigned I, RValue Arg) {
704 assert(I < NumPlacementArgs && "index out of range");
705 getPlacementArgs()[I] = Arg;
706 }
707
708 void Emit(CodeGenFunction &CGF, bool IsForEH) {
709 const FunctionProtoType *FPT
710 = OperatorDelete->getType()->getAs<FunctionProtoType>();
711 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
John McCalld441b1e2010-09-14 21:45:42 +0000712 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
John McCall824c2f52010-09-14 07:57:04 +0000713
714 CallArgList DeleteArgs;
715
716 // The first argument is always a void*.
717 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
718 DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++));
719
720 // A member 'operator delete' can take an extra 'size_t' argument.
721 if (FPT->getNumArgs() == NumPlacementArgs + 2)
722 DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++));
723
724 // Pass the rest of the arguments, which must match exactly.
725 for (unsigned I = 0; I != NumPlacementArgs; ++I)
726 DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++));
727
728 // Call 'operator delete'.
729 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
730 CGF.CGM.GetAddrOfFunction(OperatorDelete),
731 ReturnValueSlot(), DeleteArgs, OperatorDelete);
732 }
733 };
734}
735
Anders Carlssoncc52f652009-09-22 22:53:17 +0000736llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
Anders Carlssoncc52f652009-09-22 22:53:17 +0000737 QualType AllocType = E->getAllocatedType();
John McCall8ed55a52010-09-02 09:58:18 +0000738 if (AllocType->isArrayType())
739 while (const ArrayType *AType = getContext().getAsArrayType(AllocType))
740 AllocType = AType->getElementType();
741
Anders Carlssoncc52f652009-09-22 22:53:17 +0000742 FunctionDecl *NewFD = E->getOperatorNew();
743 const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>();
744
745 CallArgList NewArgs;
746
747 // The allocation size is the first argument.
748 QualType SizeTy = getContext().getSizeType();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000749
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000750 llvm::Value *NumElements = 0;
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000751 llvm::Value *AllocSizeWithoutCookie = 0;
Fariborz Jahanian47b46292010-03-24 16:57:01 +0000752 llvm::Value *AllocSize = EmitCXXNewAllocSize(getContext(),
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000753 *this, E, NumElements,
754 AllocSizeWithoutCookie);
Anders Carlssonb4bd0662009-09-23 16:07:23 +0000755
Anders Carlssoncc52f652009-09-22 22:53:17 +0000756 NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
757
758 // Emit the rest of the arguments.
759 // FIXME: Ideally, this should just use EmitCallArgs.
760 CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
761
762 // First, use the types from the function type.
763 // We start at 1 here because the first argument (the allocation size)
764 // has already been emitted.
765 for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
766 QualType ArgType = NewFTy->getArgType(i);
767
768 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
769 getTypePtr() ==
770 getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
771 "type mismatch in call argument!");
772
773 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
774 ArgType));
775
776 }
777
778 // Either we've emitted all the call args, or we have a call to a
779 // variadic function.
780 assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
781 "Extra arguments in non-variadic function!");
782
783 // If we still have any arguments, emit them using the type of the argument.
784 for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
785 NewArg != NewArgEnd; ++NewArg) {
786 QualType ArgType = NewArg->getType();
787 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
788 ArgType));
789 }
790
791 // Emit the call to new.
792 RValue RV =
John McCallab26cfa2010-02-05 21:31:56 +0000793 EmitCall(CGM.getTypes().getFunctionInfo(NewArgs, NewFTy),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000794 CGM.GetAddrOfFunction(NewFD), ReturnValueSlot(), NewArgs, NewFD);
Anders Carlssoncc52f652009-09-22 22:53:17 +0000795
796 // If an allocation function is declared with an empty exception specification
797 // it returns null to indicate failure to allocate storage. [expr.new]p13.
798 // (We don't need to check for null when there's no new initializer and
799 // we're allocating a POD type).
800 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
801 !(AllocType->isPODType() && !E->hasInitializer());
802
John McCall8ed55a52010-09-02 09:58:18 +0000803 llvm::BasicBlock *NullCheckSource = 0;
Anders Carlssoncc52f652009-09-22 22:53:17 +0000804 llvm::BasicBlock *NewNotNull = 0;
805 llvm::BasicBlock *NewEnd = 0;
806
807 llvm::Value *NewPtr = RV.getScalarVal();
John McCall8ed55a52010-09-02 09:58:18 +0000808 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000809
810 if (NullCheckResult) {
John McCall8ed55a52010-09-02 09:58:18 +0000811 NullCheckSource = Builder.GetInsertBlock();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000812 NewNotNull = createBasicBlock("new.notnull");
813 NewEnd = createBasicBlock("new.end");
814
John McCall8ed55a52010-09-02 09:58:18 +0000815 llvm::Value *IsNull = Builder.CreateIsNull(NewPtr, "new.isnull");
816 Builder.CreateCondBr(IsNull, NewEnd, NewNotNull);
Anders Carlssoncc52f652009-09-22 22:53:17 +0000817 EmitBlock(NewNotNull);
818 }
Ken Dyck3eb55cf2010-01-26 19:44:24 +0000819
John McCall8ed55a52010-09-02 09:58:18 +0000820 assert((AllocSize == AllocSizeWithoutCookie) ==
821 CalculateCookiePadding(*this, E).isZero());
822 if (AllocSize != AllocSizeWithoutCookie) {
823 assert(E->isArray());
824 NewPtr = CGM.getCXXABI().InitializeArrayCookie(CGF, NewPtr, NumElements,
825 AllocType);
826 }
Anders Carlssonf7716812009-09-23 18:59:48 +0000827
John McCall824c2f52010-09-14 07:57:04 +0000828 // If there's an operator delete, enter a cleanup to call it if an
829 // exception is thrown.
830 EHScopeStack::stable_iterator CallOperatorDelete;
831 if (E->getOperatorDelete()) {
832 CallDeleteDuringNew *Cleanup = CGF.EHStack
833 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
834 E->getNumPlacementArgs(),
835 E->getOperatorDelete(),
836 NewPtr, AllocSize);
837 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
838 Cleanup->setPlacementArg(I, NewArgs[I+1].first);
839 CallOperatorDelete = EHStack.stable_begin();
840 }
841
Douglas Gregor040ad502010-09-02 23:24:14 +0000842 const llvm::Type *ElementPtrTy
843 = ConvertTypeForMem(AllocType)->getPointerTo(AS);
John McCall8ed55a52010-09-02 09:58:18 +0000844 NewPtr = Builder.CreateBitCast(NewPtr, ElementPtrTy);
John McCall824c2f52010-09-14 07:57:04 +0000845
John McCall8ed55a52010-09-02 09:58:18 +0000846 if (E->isArray()) {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000847 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
John McCall8ed55a52010-09-02 09:58:18 +0000848
849 // NewPtr is a pointer to the base element type. If we're
850 // allocating an array of arrays, we'll need to cast back to the
851 // array pointer type.
Douglas Gregor040ad502010-09-02 23:24:14 +0000852 const llvm::Type *ResultTy = ConvertTypeForMem(E->getType());
John McCall8ed55a52010-09-02 09:58:18 +0000853 if (NewPtr->getType() != ResultTy)
854 NewPtr = Builder.CreateBitCast(NewPtr, ResultTy);
855 } else {
Douglas Gregor05fc5be2010-07-21 01:10:17 +0000856 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
Fariborz Jahanian47b46292010-03-24 16:57:01 +0000857 }
John McCall824c2f52010-09-14 07:57:04 +0000858
859 // Deactivate the 'operator delete' cleanup if we finished
860 // initialization.
861 if (CallOperatorDelete.isValid())
862 DeactivateCleanupBlock(CallOperatorDelete);
Fariborz Jahanian47b46292010-03-24 16:57:01 +0000863
Anders Carlssoncc52f652009-09-22 22:53:17 +0000864 if (NullCheckResult) {
865 Builder.CreateBr(NewEnd);
John McCall8ed55a52010-09-02 09:58:18 +0000866 llvm::BasicBlock *NotNullSource = Builder.GetInsertBlock();
Anders Carlssoncc52f652009-09-22 22:53:17 +0000867 EmitBlock(NewEnd);
868
869 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
870 PHI->reserveOperandSpace(2);
John McCall8ed55a52010-09-02 09:58:18 +0000871 PHI->addIncoming(NewPtr, NotNullSource);
872 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()),
873 NullCheckSource);
Anders Carlssoncc52f652009-09-22 22:53:17 +0000874
875 NewPtr = PHI;
876 }
John McCall8ed55a52010-09-02 09:58:18 +0000877
Anders Carlssoncc52f652009-09-22 22:53:17 +0000878 return NewPtr;
879}
880
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000881void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
882 llvm::Value *Ptr,
883 QualType DeleteTy) {
John McCall8ed55a52010-09-02 09:58:18 +0000884 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
885
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000886 const FunctionProtoType *DeleteFTy =
887 DeleteFD->getType()->getAs<FunctionProtoType>();
888
889 CallArgList DeleteArgs;
890
Anders Carlsson21122cf2009-12-13 20:04:38 +0000891 // Check if we need to pass the size to the delete operator.
892 llvm::Value *Size = 0;
893 QualType SizeTy;
894 if (DeleteFTy->getNumArgs() == 2) {
895 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck7df3cbe2010-01-26 19:59:28 +0000896 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
897 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
898 DeleteTypeSize.getQuantity());
Anders Carlsson21122cf2009-12-13 20:04:38 +0000899 }
900
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000901 QualType ArgTy = DeleteFTy->getArgType(0);
902 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
903 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
904
Anders Carlsson21122cf2009-12-13 20:04:38 +0000905 if (Size)
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000906 DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000907
908 // Emit the call to delete.
John McCallab26cfa2010-02-05 21:31:56 +0000909 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
Anders Carlsson61a401c2009-12-24 19:25:24 +0000910 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedmanfe81e3f2009-11-18 00:50:08 +0000911 DeleteArgs, DeleteFD);
912}
913
John McCall8ed55a52010-09-02 09:58:18 +0000914namespace {
915 /// Calls the given 'operator delete' on a single object.
916 struct CallObjectDelete : EHScopeStack::Cleanup {
917 llvm::Value *Ptr;
918 const FunctionDecl *OperatorDelete;
919 QualType ElementType;
920
921 CallObjectDelete(llvm::Value *Ptr,
922 const FunctionDecl *OperatorDelete,
923 QualType ElementType)
924 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
925
926 void Emit(CodeGenFunction &CGF, bool IsForEH) {
927 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
928 }
929 };
930}
931
932/// Emit the code for deleting a single object.
933static void EmitObjectDelete(CodeGenFunction &CGF,
934 const FunctionDecl *OperatorDelete,
935 llvm::Value *Ptr,
936 QualType ElementType) {
937 // Find the destructor for the type, if applicable. If the
938 // destructor is virtual, we'll just emit the vcall and return.
939 const CXXDestructorDecl *Dtor = 0;
940 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
941 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
942 if (!RD->hasTrivialDestructor()) {
943 Dtor = RD->getDestructor();
944
945 if (Dtor->isVirtual()) {
946 const llvm::Type *Ty =
John McCall0d635f52010-09-03 01:26:39 +0000947 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor,
948 Dtor_Complete),
John McCall8ed55a52010-09-02 09:58:18 +0000949 /*isVariadic=*/false);
950
951 llvm::Value *Callee
952 = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
953 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
954 0, 0);
955
956 // The dtor took care of deleting the object.
957 return;
958 }
959 }
960 }
961
962 // Make sure that we call delete even if the dtor throws.
963 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
964 Ptr, OperatorDelete, ElementType);
965
966 if (Dtor)
967 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
968 /*ForVirtualBase=*/false, Ptr);
969
970 CGF.PopCleanupBlock();
971}
972
973namespace {
974 /// Calls the given 'operator delete' on an array of objects.
975 struct CallArrayDelete : EHScopeStack::Cleanup {
976 llvm::Value *Ptr;
977 const FunctionDecl *OperatorDelete;
978 llvm::Value *NumElements;
979 QualType ElementType;
980 CharUnits CookieSize;
981
982 CallArrayDelete(llvm::Value *Ptr,
983 const FunctionDecl *OperatorDelete,
984 llvm::Value *NumElements,
985 QualType ElementType,
986 CharUnits CookieSize)
987 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
988 ElementType(ElementType), CookieSize(CookieSize) {}
989
990 void Emit(CodeGenFunction &CGF, bool IsForEH) {
991 const FunctionProtoType *DeleteFTy =
992 OperatorDelete->getType()->getAs<FunctionProtoType>();
993 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
994
995 CallArgList Args;
996
997 // Pass the pointer as the first argument.
998 QualType VoidPtrTy = DeleteFTy->getArgType(0);
999 llvm::Value *DeletePtr
1000 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1001 Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
1002
1003 // Pass the original requested size as the second argument.
1004 if (DeleteFTy->getNumArgs() == 2) {
1005 QualType size_t = DeleteFTy->getArgType(1);
1006 const llvm::IntegerType *SizeTy
1007 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1008
1009 CharUnits ElementTypeSize =
1010 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1011
1012 // The size of an element, multiplied by the number of elements.
1013 llvm::Value *Size
1014 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1015 Size = CGF.Builder.CreateMul(Size, NumElements);
1016
1017 // Plus the size of the cookie if applicable.
1018 if (!CookieSize.isZero()) {
1019 llvm::Value *CookieSizeV
1020 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1021 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1022 }
1023
1024 Args.push_back(std::make_pair(RValue::get(Size), size_t));
1025 }
1026
1027 // Emit the call to delete.
1028 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
1029 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1030 ReturnValueSlot(), Args, OperatorDelete);
1031 }
1032 };
1033}
1034
1035/// Emit the code for deleting an array of objects.
1036static void EmitArrayDelete(CodeGenFunction &CGF,
1037 const FunctionDecl *OperatorDelete,
1038 llvm::Value *Ptr,
1039 QualType ElementType) {
1040 llvm::Value *NumElements = 0;
1041 llvm::Value *AllocatedPtr = 0;
1042 CharUnits CookieSize;
1043 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, ElementType,
1044 NumElements, AllocatedPtr, CookieSize);
1045
1046 assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
1047
1048 // Make sure that we call delete even if one of the dtors throws.
1049 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1050 AllocatedPtr, OperatorDelete,
1051 NumElements, ElementType,
1052 CookieSize);
1053
1054 if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
1055 if (!RD->hasTrivialDestructor()) {
1056 assert(NumElements && "ReadArrayCookie didn't find element count"
1057 " for a class with destructor");
1058 CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
1059 }
1060 }
1061
1062 CGF.PopCleanupBlock();
1063}
1064
Anders Carlssoncc52f652009-09-22 22:53:17 +00001065void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Fariborz Jahanian6814eaa2009-11-13 19:27:47 +00001066
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001067 // Get at the argument before we performed the implicit conversion
1068 // to void*.
1069 const Expr *Arg = E->getArgument();
1070 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
John McCalle3027922010-08-25 11:45:40 +00001071 if (ICE->getCastKind() != CK_UserDefinedConversion &&
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001072 ICE->getType()->isVoidPointerType())
1073 Arg = ICE->getSubExpr();
Douglas Gregore364e7b2009-10-01 05:49:51 +00001074 else
1075 break;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001076 }
Anders Carlssoncc52f652009-09-22 22:53:17 +00001077
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001078 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001079
1080 // Null check the pointer.
1081 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1082 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1083
1084 llvm::Value *IsNull =
1085 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
1086 "isnull");
1087
1088 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1089 EmitBlock(DeleteNotNull);
Anders Carlssone828c362009-11-13 04:45:41 +00001090
John McCall8ed55a52010-09-02 09:58:18 +00001091 // We might be deleting a pointer to array. If so, GEP down to the
1092 // first non-array element.
1093 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1094 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1095 if (DeleteTy->isConstantArrayType()) {
1096 llvm::Value *Zero = Builder.getInt32(0);
1097 llvm::SmallVector<llvm::Value*,8> GEP;
1098
1099 GEP.push_back(Zero); // point at the outermost array
1100
1101 // For each layer of array type we're pointing at:
1102 while (const ConstantArrayType *Arr
1103 = getContext().getAsConstantArrayType(DeleteTy)) {
1104 // 1. Unpeel the array type.
1105 DeleteTy = Arr->getElementType();
1106
1107 // 2. GEP to the first element of the array.
1108 GEP.push_back(Zero);
Anders Carlssoncc52f652009-09-22 22:53:17 +00001109 }
John McCall8ed55a52010-09-02 09:58:18 +00001110
1111 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
Anders Carlssoncc52f652009-09-22 22:53:17 +00001112 }
1113
Douglas Gregor04f36212010-09-02 17:38:50 +00001114 assert(ConvertTypeForMem(DeleteTy) ==
1115 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall8ed55a52010-09-02 09:58:18 +00001116
1117 if (E->isArrayForm()) {
1118 EmitArrayDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1119 } else {
1120 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1121 }
Anders Carlssoncc52f652009-09-22 22:53:17 +00001122
Anders Carlssoncc52f652009-09-22 22:53:17 +00001123 EmitBlock(DeleteEnd);
1124}
Mike Stumpc9b231c2009-11-15 08:09:41 +00001125
1126llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1127 QualType Ty = E->getType();
1128 const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +00001129
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001130 if (E->isTypeOperand()) {
1131 llvm::Constant *TypeInfo =
1132 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1133 return Builder.CreateBitCast(TypeInfo, LTy);
1134 }
1135
Mike Stumpc9b231c2009-11-15 08:09:41 +00001136 Expr *subE = E->getExprOperand();
Mike Stump6fdfea62009-11-17 22:33:00 +00001137 Ty = subE->getType();
1138 CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
1139 Ty = CanTy.getUnqualifiedType().getNonReferenceType();
Mike Stumpc9b231c2009-11-15 08:09:41 +00001140 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1141 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1142 if (RD->isPolymorphic()) {
1143 // FIXME: if subE is an lvalue do
1144 LValue Obj = EmitLValue(subE);
1145 llvm::Value *This = Obj.getAddress();
Mike Stump1bf924b2009-11-15 16:52:53 +00001146 LTy = LTy->getPointerTo()->getPointerTo();
1147 llvm::Value *V = Builder.CreateBitCast(This, LTy);
1148 // We need to do a zero check for *p, unless it has NonNullAttr.
1149 // FIXME: PointerType->hasAttr<NonNullAttr>()
1150 bool CanBeZero = false;
Mike Stumpc2c03342009-11-17 00:45:21 +00001151 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens()))
John McCalle3027922010-08-25 11:45:40 +00001152 if (UO->getOpcode() == UO_Deref)
Mike Stump1bf924b2009-11-15 16:52:53 +00001153 CanBeZero = true;
1154 if (CanBeZero) {
1155 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
1156 llvm::BasicBlock *ZeroBlock = createBasicBlock();
1157
1158 llvm::Value *Zero = llvm::Constant::getNullValue(LTy);
1159 Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero),
1160 NonZeroBlock, ZeroBlock);
1161 EmitBlock(ZeroBlock);
1162 /// Call __cxa_bad_typeid
1163 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1164 const llvm::FunctionType *FTy;
1165 FTy = llvm::FunctionType::get(ResultType, false);
1166 llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
Mike Stump65511702009-11-16 06:50:58 +00001167 Builder.CreateCall(F)->setDoesNotReturn();
Mike Stump1bf924b2009-11-15 16:52:53 +00001168 Builder.CreateUnreachable();
1169 EmitBlock(NonZeroBlock);
1170 }
Mike Stumpc9b231c2009-11-15 08:09:41 +00001171 V = Builder.CreateLoad(V, "vtable");
1172 V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
1173 V = Builder.CreateLoad(V);
1174 return V;
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001175 }
Mike Stumpc9b231c2009-11-15 08:09:41 +00001176 }
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001177 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy);
Mike Stumpc9b231c2009-11-15 08:09:41 +00001178}
Mike Stump65511702009-11-16 06:50:58 +00001179
1180llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
1181 const CXXDynamicCastExpr *DCE) {
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001182 QualType SrcTy = DCE->getSubExpr()->getType();
1183 QualType DestTy = DCE->getTypeAsWritten();
1184 QualType InnerType = DestTy->getPointeeType();
1185
Mike Stump65511702009-11-16 06:50:58 +00001186 const llvm::Type *LTy = ConvertType(DCE->getType());
Mike Stump6ca0e212009-11-16 22:52:20 +00001187
Mike Stump65511702009-11-16 06:50:58 +00001188 bool CanBeZero = false;
Mike Stump65511702009-11-16 06:50:58 +00001189 bool ToVoid = false;
Mike Stump6ca0e212009-11-16 22:52:20 +00001190 bool ThrowOnBad = false;
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001191 if (DestTy->isPointerType()) {
Mike Stump65511702009-11-16 06:50:58 +00001192 // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this
1193 CanBeZero = true;
1194 if (InnerType->isVoidType())
1195 ToVoid = true;
1196 } else {
1197 LTy = LTy->getPointerTo();
Douglas Gregorfa8b4952010-05-14 21:14:41 +00001198
1199 // FIXME: What if exceptions are disabled?
Mike Stump65511702009-11-16 06:50:58 +00001200 ThrowOnBad = true;
1201 }
1202
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001203 if (SrcTy->isPointerType() || SrcTy->isReferenceType())
1204 SrcTy = SrcTy->getPointeeType();
1205 SrcTy = SrcTy.getUnqualifiedType();
1206
Anders Carlsson0087bc82009-12-18 14:55:04 +00001207 if (DestTy->isPointerType() || DestTy->isReferenceType())
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001208 DestTy = DestTy->getPointeeType();
1209 DestTy = DestTy.getUnqualifiedType();
Mike Stump65511702009-11-16 06:50:58 +00001210
Mike Stump65511702009-11-16 06:50:58 +00001211 llvm::BasicBlock *ContBlock = createBasicBlock();
1212 llvm::BasicBlock *NullBlock = 0;
1213 llvm::BasicBlock *NonZeroBlock = 0;
1214 if (CanBeZero) {
1215 NonZeroBlock = createBasicBlock();
1216 NullBlock = createBasicBlock();
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001217 Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock);
Mike Stump65511702009-11-16 06:50:58 +00001218 EmitBlock(NonZeroBlock);
1219 }
1220
Mike Stump65511702009-11-16 06:50:58 +00001221 llvm::BasicBlock *BadCastBlock = 0;
Mike Stump65511702009-11-16 06:50:58 +00001222
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001223 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
Mike Stump6ca0e212009-11-16 22:52:20 +00001224
1225 // See if this is a dynamic_cast(void*)
1226 if (ToVoid) {
1227 llvm::Value *This = V;
1228 V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo());
1229 V = Builder.CreateLoad(V, "vtable");
1230 V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL);
1231 V = Builder.CreateLoad(V, "offset to top");
1232 This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext));
1233 V = Builder.CreateInBoundsGEP(This, V);
1234 V = Builder.CreateBitCast(V, LTy);
1235 } else {
1236 /// Call __dynamic_cast
1237 const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext);
1238 const llvm::FunctionType *FTy;
1239 std::vector<const llvm::Type*> ArgTys;
1240 const llvm::Type *PtrToInt8Ty
1241 = llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1242 ArgTys.push_back(PtrToInt8Ty);
1243 ArgTys.push_back(PtrToInt8Ty);
1244 ArgTys.push_back(PtrToInt8Ty);
1245 ArgTys.push_back(PtrDiffTy);
1246 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump6ca0e212009-11-16 22:52:20 +00001247
1248 // FIXME: Calculate better hint.
1249 llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL);
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001250
1251 assert(SrcTy->isRecordType() && "Src type must be record type!");
1252 assert(DestTy->isRecordType() && "Dest type must be record type!");
1253
Douglas Gregor247894b2009-12-23 22:04:40 +00001254 llvm::Value *SrcArg
1255 = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType());
1256 llvm::Value *DestArg
1257 = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType());
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001258
Mike Stump6ca0e212009-11-16 22:52:20 +00001259 V = Builder.CreateBitCast(V, PtrToInt8Ty);
1260 V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"),
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001261 V, SrcArg, DestArg, hint);
Mike Stump6ca0e212009-11-16 22:52:20 +00001262 V = Builder.CreateBitCast(V, LTy);
1263
1264 if (ThrowOnBad) {
1265 BadCastBlock = createBasicBlock();
Anders Carlsson3f4336c2009-12-17 07:09:17 +00001266 Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
Mike Stump6ca0e212009-11-16 22:52:20 +00001267 EmitBlock(BadCastBlock);
Douglas Gregorfa8b4952010-05-14 21:14:41 +00001268 /// Invoke __cxa_bad_cast
Mike Stump6ca0e212009-11-16 22:52:20 +00001269 ResultType = llvm::Type::getVoidTy(VMContext);
1270 const llvm::FunctionType *FBadTy;
Mike Stump3afea1d2009-11-17 03:01:03 +00001271 FBadTy = llvm::FunctionType::get(ResultType, false);
Mike Stump6ca0e212009-11-16 22:52:20 +00001272 llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
Douglas Gregorfa8b4952010-05-14 21:14:41 +00001273 if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
1274 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1275 Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
1276 EmitBlock(Cont);
1277 } else {
1278 // FIXME: Does this ever make sense?
1279 Builder.CreateCall(F)->setDoesNotReturn();
1280 }
Mike Stumpe8cdcc92009-11-17 00:08:50 +00001281 Builder.CreateUnreachable();
Mike Stump6ca0e212009-11-16 22:52:20 +00001282 }
Mike Stump65511702009-11-16 06:50:58 +00001283 }
1284
1285 if (CanBeZero) {
1286 Builder.CreateBr(ContBlock);
1287 EmitBlock(NullBlock);
1288 Builder.CreateBr(ContBlock);
1289 }
1290 EmitBlock(ContBlock);
1291 if (CanBeZero) {
1292 llvm::PHINode *PHI = Builder.CreatePHI(LTy);
Mike Stump4d0e9092009-11-17 00:10:05 +00001293 PHI->reserveOperandSpace(2);
Mike Stump65511702009-11-16 06:50:58 +00001294 PHI->addIncoming(V, NonZeroBlock);
1295 PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock);
Mike Stump65511702009-11-16 06:50:58 +00001296 V = PHI;
1297 }
1298
1299 return V;
1300}