blob: a5c16913078d9d94f839a5fb9d4ff71a8ea1a358 [file] [log] [blame]
Anders Carlsson5b955922009-11-24 05:51:11 +00001//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
Anders Carlsson16d81b82009-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 McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Fariborz Jahanian842ddd02010-05-20 21:38:57 +000016#include "CGObjCRuntime.h"
Chris Lattner6c552c12010-07-20 20:19:24 +000017#include "llvm/Intrinsics.h"
Anders Carlsson16d81b82009-09-22 22:53:17 +000018using namespace clang;
19using namespace CodeGen;
20
Anders Carlsson3b5ad222010-01-01 20:29:01 +000021RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
22 llvm::Value *Callee,
23 ReturnValueSlot ReturnValue,
24 llvm::Value *This,
Anders Carlssonc997d422010-01-02 01:01:18 +000025 llvm::Value *VTT,
Anders Carlsson3b5ad222010-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 Carlssonc997d422010-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 Carlsson3b5ad222010-01-01 20:29:01 +000045 // And the rest of the call args
46 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
47
John McCall04a67a62010-02-05 21:31:56 +000048 QualType ResultType = FPT->getResultType();
49 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +000050 FPT->getExtInfo()),
51 Callee, ReturnValue, Args, MD);
Anders Carlsson3b5ad222010-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 Friedman6997aae2010-01-31 20:58:15 +000067 if (isa<CXXConstructExpr>(Base))
Anders Carlsson3b5ad222010-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 }
96
97 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
98
99 const llvm::Type *Ty =
100 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
101 FPT->isVariadic());
102 llvm::Value *This;
103
104 if (ME->isArrow())
105 This = EmitScalarExpr(ME->getBase());
106 else {
107 LValue BaseLV = EmitLValue(ME->getBase());
108 This = BaseLV.getAddress();
109 }
110
111 if (MD->isCopyAssignment() && MD->isTrivial()) {
112 // We don't like to generate the trivial copy assignment operator when
113 // it isn't necessary; just produce the proper effect here.
114 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
115 EmitAggregateCopy(This, RHS, CE->getType());
116 return RValue::get(This);
117 }
118
119 // C++ [class.virtual]p12:
120 // Explicit qualification with the scope operator (5.1) suppresses the
121 // virtual call mechanism.
122 //
123 // We also don't emit a virtual call if the base expression has a record type
124 // because then we know what the type is.
125 llvm::Value *Callee;
126 if (const CXXDestructorDecl *Destructor
127 = dyn_cast<CXXDestructorDecl>(MD)) {
128 if (Destructor->isTrivial())
129 return RValue::get(0);
130 if (MD->isVirtual() && !ME->hasQualifier() &&
131 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
132 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
133 } else {
134 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
135 }
136 } else if (MD->isVirtual() && !ME->hasQualifier() &&
137 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
138 Callee = BuildVirtualCall(MD, This, Ty);
139 } else {
140 Callee = CGM.GetAddrOfFunction(MD, Ty);
141 }
142
Anders Carlssonc997d422010-01-02 01:01:18 +0000143 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000144 CE->arg_begin(), CE->arg_end());
145}
146
147RValue
148CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
149 ReturnValueSlot ReturnValue) {
150 const BinaryOperator *BO =
151 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
152 const Expr *BaseExpr = BO->getLHS();
153 const Expr *MemFnExpr = BO->getRHS();
154
155 const MemberPointerType *MPT =
156 MemFnExpr->getType()->getAs<MemberPointerType>();
John McCall93d557b2010-08-22 00:05:51 +0000157
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000158 const FunctionProtoType *FPT =
159 MPT->getPointeeType()->getAs<FunctionProtoType>();
160 const CXXRecordDecl *RD =
161 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
162
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000163 // Get the member function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000164 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000165
166 // Emit the 'this' pointer.
167 llvm::Value *This;
168
John McCall2de56d12010-08-25 11:45:40 +0000169 if (BO->getOpcode() == BO_PtrMemI)
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000170 This = EmitScalarExpr(BaseExpr);
171 else
172 This = EmitLValue(BaseExpr).getAddress();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000173
John McCall93d557b2010-08-22 00:05:51 +0000174 // Ask the ABI to load the callee. Note that This is modified.
175 llvm::Value *Callee =
176 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(CGF, This, MemFnPtr, MPT);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000177
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000178 CallArgList Args;
179
180 QualType ThisType =
181 getContext().getPointerType(getContext().getTagDeclType(RD));
182
183 // Push the this ptr.
184 Args.push_back(std::make_pair(RValue::get(This), ThisType));
185
186 // And the rest of the call args
187 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
John McCall04a67a62010-02-05 21:31:56 +0000188 const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>();
189 return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000190 ReturnValue, Args);
191}
192
193RValue
194CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
195 const CXXMethodDecl *MD,
196 ReturnValueSlot ReturnValue) {
197 assert(MD->isInstance() &&
198 "Trying to emit a member call expr on a static method!");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000199 if (MD->isCopyAssignment()) {
200 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
201 if (ClassDecl->hasTrivialCopyAssignment()) {
202 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
203 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
Fariborz Jahanianb3ebe942010-05-10 22:57:35 +0000204 LValue LV = EmitLValue(E->getArg(0));
205 llvm::Value *This;
Fariborz Jahanian98c9d1f2010-09-01 19:36:41 +0000206 if (LV.isPropertyRef() || LV.isKVCRef()) {
Fariborz Jahanianc9a8fa42010-05-16 00:10:46 +0000207 llvm::Value *AggLoc = CreateMemTemp(E->getArg(1)->getType());
Fariborz Jahanian0ca0b1f2010-05-15 23:05:52 +0000208 EmitAggExpr(E->getArg(1), AggLoc, false /*VolatileDest*/);
Fariborz Jahanian98c9d1f2010-09-01 19:36:41 +0000209 if (LV.isPropertyRef())
210 EmitObjCPropertySet(LV.getPropertyRefExpr(),
211 RValue::getAggregate(AggLoc,
212 false /*VolatileDest*/));
213 else
214 EmitObjCPropertySet(LV.getKVCRefExpr(),
215 RValue::getAggregate(AggLoc,
216 false /*VolatileDest*/));
Fariborz Jahanian0ca0b1f2010-05-15 23:05:52 +0000217 return RValue::getAggregate(0, false);
Fariborz Jahanianb3ebe942010-05-10 22:57:35 +0000218 }
219 else
220 This = LV.getAddress();
221
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000222 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
223 QualType Ty = E->getType();
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000224 EmitAggregateCopy(This, Src, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000225 return RValue::get(This);
226 }
227 }
228
229 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
230 const llvm::Type *Ty =
231 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
232 FPT->isVariadic());
Fariborz Jahanianbbb52242010-05-07 18:56:13 +0000233 LValue LV = EmitLValue(E->getArg(0));
234 llvm::Value *This;
Fariborz Jahanian98c9d1f2010-09-01 19:36:41 +0000235 if (LV.isPropertyRef() || LV.isKVCRef()) {
236 QualType QT = E->getArg(0)->getType();
237 RValue RV =
238 LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT)
239 : EmitLoadOfKVCRefLValue(LV, QT);
Fariborz Jahanian1d49f212010-05-20 16:46:55 +0000240 assert (!RV.isScalar() && "EmitCXXOperatorMemberCallExpr");
241 This = RV.getAggregateAddr();
Fariborz Jahanianbbb52242010-05-07 18:56:13 +0000242 }
243 else
244 This = LV.getAddress();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000245
246 llvm::Value *Callee;
247 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
248 Callee = BuildVirtualCall(MD, This, Ty);
249 else
250 Callee = CGM.GetAddrOfFunction(MD, Ty);
251
Anders Carlssonc997d422010-01-02 01:01:18 +0000252 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000253 E->arg_begin() + 1, E->arg_end());
254}
255
256void
257CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
258 const CXXConstructExpr *E) {
259 assert(Dest && "Must have a destination!");
260 const CXXConstructorDecl *CD = E->getConstructor();
Douglas Gregor759e41b2010-08-22 16:15:35 +0000261
262 // If we require zero initialization before (or instead of) calling the
263 // constructor, as can be the case with a non-user-provided default
264 // constructor, emit the zero initialization now.
265 if (E->requiresZeroInitialization())
266 EmitNullInitialization(Dest, E->getType());
267
268
269 // If this is a call to a trivial default constructor, do nothing.
270 if (CD->isTrivial() && CD->isDefaultConstructor())
271 return;
272
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000273 // Code gen optimization to eliminate copy constructor and return
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000274 // its first argument instead, if in fact that argument is a temporary
275 // object.
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000276 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000277 if (const Expr *Arg = E->getArg(0)->getTemporaryObject()) {
278 EmitAggExpr(Arg, Dest, false);
279 return;
280 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000281 }
Douglas Gregor759e41b2010-08-22 16:15:35 +0000282
283 const ConstantArrayType *Array
284 = getContext().getAsConstantArrayType(E->getType());
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000285 if (Array) {
286 QualType BaseElementTy = getContext().getBaseElementType(Array);
287 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
288 BasePtr = llvm::PointerType::getUnqual(BasePtr);
289 llvm::Value *BaseAddrPtr =
Anders Carlsson43db20e2010-05-01 17:02:18 +0000290 Builder.CreateBitCast(Dest, BasePtr);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000291
292 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
293 E->arg_begin(), E->arg_end());
294 }
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000295 else {
296 CXXCtorType Type =
297 (E->getConstructionKind() == CXXConstructExpr::CK_Complete)
298 ? Ctor_Complete : Ctor_Base;
299 bool ForVirtualBase =
300 E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
301
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000302 // Call the constructor.
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000303 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000304 E->arg_begin(), E->arg_end());
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000305 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000306}
307
John McCall5172ed92010-08-23 01:17:59 +0000308/// Check whether the given operator new[] is the global placement
309/// operator new[].
310static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
311 const FunctionDecl *Fn) {
312 // Must be in global scope. Note that allocation functions can't be
313 // declared in namespaces.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000314 if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
John McCall5172ed92010-08-23 01:17:59 +0000315 return false;
316
317 // Signature must be void *operator new[](size_t, void*).
318 // The size_t is common to all operator new[]s.
319 if (Fn->getNumParams() != 2)
320 return false;
321
322 CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
323 return (ParamType == Ctx.VoidPtrTy);
324}
325
John McCall1e7fe752010-09-02 09:58:18 +0000326static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
327 const CXXNewExpr *E) {
Anders Carlsson871d0782009-12-13 20:04:38 +0000328 if (!E->isArray())
Ken Dyckcaf647c2010-01-26 19:44:24 +0000329 return CharUnits::Zero();
Anders Carlsson871d0782009-12-13 20:04:38 +0000330
Anders Carlssondd937552009-12-13 20:34:34 +0000331 // No cookie is required if the new operator being used is
332 // ::operator new[](size_t, void*).
333 const FunctionDecl *OperatorNew = E->getOperatorNew();
John McCall1e7fe752010-09-02 09:58:18 +0000334 if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
John McCall5172ed92010-08-23 01:17:59 +0000335 return CharUnits::Zero();
336
John McCall1e7fe752010-09-02 09:58:18 +0000337 return CGF.CGM.getCXXABI().GetArrayCookieSize(E->getAllocatedType());
Anders Carlssona4d4c012009-09-23 16:07:23 +0000338}
339
Fariborz Jahanianceb43b62010-03-24 16:57:01 +0000340static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
Chris Lattnerdefe8b22010-07-20 18:45:57 +0000341 CodeGenFunction &CGF,
Anders Carlssona4d4c012009-09-23 16:07:23 +0000342 const CXXNewExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +0000343 llvm::Value *&NumElements,
344 llvm::Value *&SizeWithoutCookie) {
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000345 QualType ElemType = E->getAllocatedType();
John McCall1e7fe752010-09-02 09:58:18 +0000346
347 const llvm::IntegerType *SizeTy =
348 cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
Anders Carlssona4d4c012009-09-23 16:07:23 +0000349
John McCall1e7fe752010-09-02 09:58:18 +0000350 CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
351
Douglas Gregor59174c02010-07-21 01:10:17 +0000352 if (!E->isArray()) {
353 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
354 return SizeWithoutCookie;
355 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000356
John McCall1e7fe752010-09-02 09:58:18 +0000357 // Figure out the cookie size.
358 CharUnits CookieSize = CalculateCookiePadding(CGF, E);
359
Anders Carlssona4d4c012009-09-23 16:07:23 +0000360 // Emit the array size expression.
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000361 // We multiply the size of all dimensions for NumElements.
362 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
Anders Carlssona4d4c012009-09-23 16:07:23 +0000363 NumElements = CGF.EmitScalarExpr(E->getArraySize());
John McCall1e7fe752010-09-02 09:58:18 +0000364 assert(NumElements->getType() == SizeTy && "element count not a size_t");
365
366 uint64_t ArraySizeMultiplier = 1;
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000367 while (const ConstantArrayType *CAT
368 = CGF.getContext().getAsConstantArrayType(ElemType)) {
369 ElemType = CAT->getElementType();
John McCall1e7fe752010-09-02 09:58:18 +0000370 ArraySizeMultiplier *= CAT->getSize().getZExtValue();
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000371 }
372
John McCall1e7fe752010-09-02 09:58:18 +0000373 llvm::Value *Size;
Chris Lattner83252dc2010-07-20 21:07:09 +0000374
Chris Lattner806941e2010-07-20 21:55:52 +0000375 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
376 // Don't bloat the -O0 code.
377 if (llvm::ConstantInt *NumElementsC =
378 dyn_cast<llvm::ConstantInt>(NumElements)) {
Chris Lattner806941e2010-07-20 21:55:52 +0000379 llvm::APInt NEC = NumElementsC->getValue();
John McCall1e7fe752010-09-02 09:58:18 +0000380 unsigned SizeWidth = NEC.getBitWidth();
381
382 // Determine if there is an overflow here by doing an extended multiply.
383 NEC.zext(SizeWidth*2);
384 llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
Chris Lattner806941e2010-07-20 21:55:52 +0000385 SC *= NEC;
John McCall1e7fe752010-09-02 09:58:18 +0000386
387 if (!CookieSize.isZero()) {
388 // Save the current size without a cookie. We don't care if an
389 // overflow's already happened because SizeWithoutCookie isn't
390 // used if the allocator returns null or throws, as it should
391 // always do on an overflow.
392 llvm::APInt SWC = SC;
393 SWC.trunc(SizeWidth);
394 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
395
396 // Add the cookie size.
397 SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
Chris Lattner806941e2010-07-20 21:55:52 +0000398 }
399
John McCall1e7fe752010-09-02 09:58:18 +0000400 if (SC.countLeadingZeros() >= SizeWidth) {
401 SC.trunc(SizeWidth);
402 Size = llvm::ConstantInt::get(SizeTy, SC);
403 } else {
404 // On overflow, produce a -1 so operator new throws.
405 Size = llvm::Constant::getAllOnesValue(SizeTy);
406 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000407
John McCall1e7fe752010-09-02 09:58:18 +0000408 // Scale NumElements while we're at it.
409 uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
410 NumElements = llvm::ConstantInt::get(SizeTy, N);
411
412 // Otherwise, we don't need to do an overflow-checked multiplication if
413 // we're multiplying by one.
414 } else if (TypeSize.isOne()) {
415 assert(ArraySizeMultiplier == 1);
416
417 Size = NumElements;
418
419 // If we need a cookie, add its size in with an overflow check.
420 // This is maybe a little paranoid.
421 if (!CookieSize.isZero()) {
422 SizeWithoutCookie = Size;
423
424 llvm::Value *CookieSizeV
425 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
426
427 const llvm::Type *Types[] = { SizeTy };
428 llvm::Value *UAddF
429 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
430 llvm::Value *AddRes
431 = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
432
433 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
434 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
435 Size = CGF.Builder.CreateSelect(DidOverflow,
436 llvm::ConstantInt::get(SizeTy, -1),
437 Size);
438 }
439
440 // Otherwise use the int.umul.with.overflow intrinsic.
441 } else {
442 llvm::Value *OutermostElementSize
443 = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
444
445 llvm::Value *NumOutermostElements = NumElements;
446
447 // Scale NumElements by the array size multiplier. This might
448 // overflow, but only if the multiplication below also overflows,
449 // in which case this multiplication isn't used.
450 if (ArraySizeMultiplier != 1)
451 NumElements = CGF.Builder.CreateMul(NumElements,
452 llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
453
454 // The requested size of the outermost array is non-constant.
455 // Multiply that by the static size of the elements of that array;
456 // on unsigned overflow, set the size to -1 to trigger an
457 // exception from the allocation routine. This is sufficient to
458 // prevent buffer overruns from the allocator returning a
459 // seemingly valid pointer to insufficient space. This idea comes
460 // originally from MSVC, and GCC has an open bug requesting
461 // similar behavior:
462 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
463 //
464 // This will not be sufficient for C++0x, which requires a
465 // specific exception class (std::bad_array_new_length).
466 // That will require ABI support that has not yet been specified.
467 const llvm::Type *Types[] = { SizeTy };
468 llvm::Value *UMulF
469 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
470 llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
471 OutermostElementSize);
472
473 // The overflow bit.
474 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
475
476 // The result of the multiplication.
477 Size = CGF.Builder.CreateExtractValue(MulRes, 0);
478
479 // If we have a cookie, we need to add that size in, too.
480 if (!CookieSize.isZero()) {
481 SizeWithoutCookie = Size;
482
483 llvm::Value *CookieSizeV
484 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
485 llvm::Value *UAddF
486 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
487 llvm::Value *AddRes
488 = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
489
490 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
491
492 llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
493 DidOverflow = CGF.Builder.CreateAnd(DidOverflow, AddDidOverflow);
494 }
495
496 Size = CGF.Builder.CreateSelect(DidOverflow,
497 llvm::ConstantInt::get(SizeTy, -1),
498 Size);
Chris Lattner806941e2010-07-20 21:55:52 +0000499 }
John McCall1e7fe752010-09-02 09:58:18 +0000500
501 if (CookieSize.isZero())
502 SizeWithoutCookie = Size;
503 else
504 assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
505
Chris Lattner806941e2010-07-20 21:55:52 +0000506 return Size;
Anders Carlssona4d4c012009-09-23 16:07:23 +0000507}
508
Fariborz Jahanianef668722010-06-25 18:26:07 +0000509static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
510 llvm::Value *NewPtr) {
Fariborz Jahanianef668722010-06-25 18:26:07 +0000511
512 assert(E->getNumConstructorArgs() == 1 &&
513 "Can only have one argument to initializer of POD type.");
514
515 const Expr *Init = E->getConstructorArg(0);
516 QualType AllocType = E->getAllocatedType();
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000517
518 unsigned Alignment =
519 CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
Fariborz Jahanianef668722010-06-25 18:26:07 +0000520 if (!CGF.hasAggregateLLVMType(AllocType))
521 CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000522 AllocType.isVolatileQualified(), Alignment,
523 AllocType);
Fariborz Jahanianef668722010-06-25 18:26:07 +0000524 else if (AllocType->isAnyComplexType())
525 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
526 AllocType.isVolatileQualified());
527 else
528 CGF.EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
529}
530
531void
532CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
533 llvm::Value *NewPtr,
534 llvm::Value *NumElements) {
Fariborz Jahanian5304c952010-06-25 20:01:13 +0000535 // We have a POD type.
536 if (E->getNumConstructorArgs() == 0)
537 return;
538
Fariborz Jahanianef668722010-06-25 18:26:07 +0000539 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
540
541 // Create a temporary for the loop index and initialize it with 0.
542 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
543 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
544 Builder.CreateStore(Zero, IndexPtr);
545
546 // Start the loop with a block that tests the condition.
547 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
548 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
549
550 EmitBlock(CondBlock);
551
552 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
553
554 // Generate: if (loop-index < number-of-elements fall to the loop body,
555 // otherwise, go to the block after the for-loop.
556 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
557 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
558 // If the condition is true, execute the body.
559 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
560
561 EmitBlock(ForBody);
562
563 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
564 // Inside the loop body, emit the constructor call on the array element.
565 Counter = Builder.CreateLoad(IndexPtr);
566 llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
567 "arrayidx");
568 StoreAnyExprIntoOneUnit(*this, E, Address);
569
570 EmitBlock(ContinueBlock);
571
572 // Emit the increment of the loop counter.
573 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
574 Counter = Builder.CreateLoad(IndexPtr);
575 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
576 Builder.CreateStore(NextVal, IndexPtr);
577
578 // Finally, branch back up to the condition for the next iteration.
579 EmitBranch(CondBlock);
580
581 // Emit the fall-through block.
582 EmitBlock(AfterFor, true);
583}
584
Douglas Gregor59174c02010-07-21 01:10:17 +0000585static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
586 llvm::Value *NewPtr, llvm::Value *Size) {
587 llvm::LLVMContext &VMContext = CGF.CGM.getLLVMContext();
588 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
589 if (NewPtr->getType() != BP)
590 NewPtr = CGF.Builder.CreateBitCast(NewPtr, BP, "tmp");
591
592 CGF.Builder.CreateCall5(CGF.CGM.getMemSetFn(BP, CGF.IntPtrTy), NewPtr,
593 llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
594 Size,
595 llvm::ConstantInt::get(CGF.Int32Ty,
596 CGF.getContext().getTypeAlign(T)/8),
597 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
598 0));
599}
600
Anders Carlssona4d4c012009-09-23 16:07:23 +0000601static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
602 llvm::Value *NewPtr,
Douglas Gregor59174c02010-07-21 01:10:17 +0000603 llvm::Value *NumElements,
604 llvm::Value *AllocSizeWithoutCookie) {
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000605 if (E->isArray()) {
Anders Carlssone99bdb62010-05-03 15:09:17 +0000606 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000607 bool RequiresZeroInitialization = false;
608 if (Ctor->getParent()->hasTrivialConstructor()) {
609 // If new expression did not specify value-initialization, then there
610 // is no initialization.
611 if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
612 return;
613
John McCallf16aa102010-08-22 21:01:12 +0000614 if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000615 // Optimization: since zero initialization will just set the memory
616 // to all zeroes, generate a single memset to do it in one shot.
617 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
618 AllocSizeWithoutCookie);
619 return;
620 }
621
622 RequiresZeroInitialization = true;
623 }
624
625 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
626 E->constructor_arg_begin(),
627 E->constructor_arg_end(),
628 RequiresZeroInitialization);
Anders Carlssone99bdb62010-05-03 15:09:17 +0000629 return;
Douglas Gregor59174c02010-07-21 01:10:17 +0000630 } else if (E->getNumConstructorArgs() == 1 &&
631 isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
632 // Optimization: since zero initialization will just set the memory
633 // to all zeroes, generate a single memset to do it in one shot.
634 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
635 AllocSizeWithoutCookie);
636 return;
637 } else {
Fariborz Jahanianef668722010-06-25 18:26:07 +0000638 CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
639 return;
640 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000641 }
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000642
643 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregored8abf12010-07-08 06:14:04 +0000644 // Per C++ [expr.new]p15, if we have an initializer, then we're performing
645 // direct initialization. C++ [dcl.init]p5 requires that we
646 // zero-initialize storage if there are no user-declared constructors.
647 if (E->hasInitializer() &&
648 !Ctor->getParent()->hasUserDeclaredConstructor() &&
649 !Ctor->getParent()->isEmpty())
650 CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
651
Douglas Gregor84745672010-07-07 23:37:33 +0000652 CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
653 NewPtr, E->constructor_arg_begin(),
654 E->constructor_arg_end());
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000655
656 return;
657 }
Fariborz Jahanian5304c952010-06-25 20:01:13 +0000658 // We have a POD type.
659 if (E->getNumConstructorArgs() == 0)
660 return;
661
Fariborz Jahanianef668722010-06-25 18:26:07 +0000662 StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000663}
664
Anders Carlsson16d81b82009-09-22 22:53:17 +0000665llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
Anders Carlsson16d81b82009-09-22 22:53:17 +0000666 QualType AllocType = E->getAllocatedType();
John McCall1e7fe752010-09-02 09:58:18 +0000667 if (AllocType->isArrayType())
668 while (const ArrayType *AType = getContext().getAsArrayType(AllocType))
669 AllocType = AType->getElementType();
670
Anders Carlsson16d81b82009-09-22 22:53:17 +0000671 FunctionDecl *NewFD = E->getOperatorNew();
672 const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>();
673
674 CallArgList NewArgs;
675
676 // The allocation size is the first argument.
677 QualType SizeTy = getContext().getSizeType();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000678
Anders Carlssona4d4c012009-09-23 16:07:23 +0000679 llvm::Value *NumElements = 0;
Douglas Gregor59174c02010-07-21 01:10:17 +0000680 llvm::Value *AllocSizeWithoutCookie = 0;
Fariborz Jahanianceb43b62010-03-24 16:57:01 +0000681 llvm::Value *AllocSize = EmitCXXNewAllocSize(getContext(),
Douglas Gregor59174c02010-07-21 01:10:17 +0000682 *this, E, NumElements,
683 AllocSizeWithoutCookie);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000684
Anders Carlsson16d81b82009-09-22 22:53:17 +0000685 NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
686
687 // Emit the rest of the arguments.
688 // FIXME: Ideally, this should just use EmitCallArgs.
689 CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
690
691 // First, use the types from the function type.
692 // We start at 1 here because the first argument (the allocation size)
693 // has already been emitted.
694 for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
695 QualType ArgType = NewFTy->getArgType(i);
696
697 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
698 getTypePtr() ==
699 getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
700 "type mismatch in call argument!");
701
702 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
703 ArgType));
704
705 }
706
707 // Either we've emitted all the call args, or we have a call to a
708 // variadic function.
709 assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
710 "Extra arguments in non-variadic function!");
711
712 // If we still have any arguments, emit them using the type of the argument.
713 for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
714 NewArg != NewArgEnd; ++NewArg) {
715 QualType ArgType = NewArg->getType();
716 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
717 ArgType));
718 }
719
720 // Emit the call to new.
721 RValue RV =
John McCall04a67a62010-02-05 21:31:56 +0000722 EmitCall(CGM.getTypes().getFunctionInfo(NewArgs, NewFTy),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000723 CGM.GetAddrOfFunction(NewFD), ReturnValueSlot(), NewArgs, NewFD);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000724
725 // If an allocation function is declared with an empty exception specification
726 // it returns null to indicate failure to allocate storage. [expr.new]p13.
727 // (We don't need to check for null when there's no new initializer and
728 // we're allocating a POD type).
729 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
730 !(AllocType->isPODType() && !E->hasInitializer());
731
John McCall1e7fe752010-09-02 09:58:18 +0000732 llvm::BasicBlock *NullCheckSource = 0;
Anders Carlsson16d81b82009-09-22 22:53:17 +0000733 llvm::BasicBlock *NewNotNull = 0;
734 llvm::BasicBlock *NewEnd = 0;
735
736 llvm::Value *NewPtr = RV.getScalarVal();
John McCall1e7fe752010-09-02 09:58:18 +0000737 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000738
739 if (NullCheckResult) {
John McCall1e7fe752010-09-02 09:58:18 +0000740 NullCheckSource = Builder.GetInsertBlock();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000741 NewNotNull = createBasicBlock("new.notnull");
742 NewEnd = createBasicBlock("new.end");
743
John McCall1e7fe752010-09-02 09:58:18 +0000744 llvm::Value *IsNull = Builder.CreateIsNull(NewPtr, "new.isnull");
745 Builder.CreateCondBr(IsNull, NewEnd, NewNotNull);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000746 EmitBlock(NewNotNull);
747 }
Ken Dyckcaf647c2010-01-26 19:44:24 +0000748
John McCall1e7fe752010-09-02 09:58:18 +0000749 assert((AllocSize == AllocSizeWithoutCookie) ==
750 CalculateCookiePadding(*this, E).isZero());
751 if (AllocSize != AllocSizeWithoutCookie) {
752 assert(E->isArray());
753 NewPtr = CGM.getCXXABI().InitializeArrayCookie(CGF, NewPtr, NumElements,
754 AllocType);
755 }
Anders Carlsson6ac5fc42009-09-23 18:59:48 +0000756
Douglas Gregorcc09c022010-09-02 23:24:14 +0000757 const llvm::Type *ElementPtrTy
758 = ConvertTypeForMem(AllocType)->getPointerTo(AS);
John McCall1e7fe752010-09-02 09:58:18 +0000759 NewPtr = Builder.CreateBitCast(NewPtr, ElementPtrTy);
760 if (E->isArray()) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000761 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
John McCall1e7fe752010-09-02 09:58:18 +0000762
763 // NewPtr is a pointer to the base element type. If we're
764 // allocating an array of arrays, we'll need to cast back to the
765 // array pointer type.
Douglas Gregorcc09c022010-09-02 23:24:14 +0000766 const llvm::Type *ResultTy = ConvertTypeForMem(E->getType());
John McCall1e7fe752010-09-02 09:58:18 +0000767 if (NewPtr->getType() != ResultTy)
768 NewPtr = Builder.CreateBitCast(NewPtr, ResultTy);
769 } else {
Douglas Gregor59174c02010-07-21 01:10:17 +0000770 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
Fariborz Jahanianceb43b62010-03-24 16:57:01 +0000771 }
772
Anders Carlsson16d81b82009-09-22 22:53:17 +0000773 if (NullCheckResult) {
774 Builder.CreateBr(NewEnd);
John McCall1e7fe752010-09-02 09:58:18 +0000775 llvm::BasicBlock *NotNullSource = Builder.GetInsertBlock();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000776 EmitBlock(NewEnd);
777
778 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
779 PHI->reserveOperandSpace(2);
John McCall1e7fe752010-09-02 09:58:18 +0000780 PHI->addIncoming(NewPtr, NotNullSource);
781 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()),
782 NullCheckSource);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000783
784 NewPtr = PHI;
785 }
John McCall1e7fe752010-09-02 09:58:18 +0000786
Anders Carlsson16d81b82009-09-22 22:53:17 +0000787 return NewPtr;
788}
789
Eli Friedman5fe05982009-11-18 00:50:08 +0000790void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
791 llvm::Value *Ptr,
792 QualType DeleteTy) {
John McCall1e7fe752010-09-02 09:58:18 +0000793 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
794
Eli Friedman5fe05982009-11-18 00:50:08 +0000795 const FunctionProtoType *DeleteFTy =
796 DeleteFD->getType()->getAs<FunctionProtoType>();
797
798 CallArgList DeleteArgs;
799
Anders Carlsson871d0782009-12-13 20:04:38 +0000800 // Check if we need to pass the size to the delete operator.
801 llvm::Value *Size = 0;
802 QualType SizeTy;
803 if (DeleteFTy->getNumArgs() == 2) {
804 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck4f122ef2010-01-26 19:59:28 +0000805 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
806 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
807 DeleteTypeSize.getQuantity());
Anders Carlsson871d0782009-12-13 20:04:38 +0000808 }
809
Eli Friedman5fe05982009-11-18 00:50:08 +0000810 QualType ArgTy = DeleteFTy->getArgType(0);
811 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
812 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
813
Anders Carlsson871d0782009-12-13 20:04:38 +0000814 if (Size)
Eli Friedman5fe05982009-11-18 00:50:08 +0000815 DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
Eli Friedman5fe05982009-11-18 00:50:08 +0000816
817 // Emit the call to delete.
John McCall04a67a62010-02-05 21:31:56 +0000818 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000819 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedman5fe05982009-11-18 00:50:08 +0000820 DeleteArgs, DeleteFD);
821}
822
John McCall1e7fe752010-09-02 09:58:18 +0000823namespace {
824 /// Calls the given 'operator delete' on a single object.
825 struct CallObjectDelete : EHScopeStack::Cleanup {
826 llvm::Value *Ptr;
827 const FunctionDecl *OperatorDelete;
828 QualType ElementType;
829
830 CallObjectDelete(llvm::Value *Ptr,
831 const FunctionDecl *OperatorDelete,
832 QualType ElementType)
833 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
834
835 void Emit(CodeGenFunction &CGF, bool IsForEH) {
836 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
837 }
838 };
839}
840
841/// Emit the code for deleting a single object.
842static void EmitObjectDelete(CodeGenFunction &CGF,
843 const FunctionDecl *OperatorDelete,
844 llvm::Value *Ptr,
845 QualType ElementType) {
846 // Find the destructor for the type, if applicable. If the
847 // destructor is virtual, we'll just emit the vcall and return.
848 const CXXDestructorDecl *Dtor = 0;
849 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
850 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
851 if (!RD->hasTrivialDestructor()) {
852 Dtor = RD->getDestructor();
853
854 if (Dtor->isVirtual()) {
855 const llvm::Type *Ty =
856 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor),
857 /*isVariadic=*/false);
858
859 llvm::Value *Callee
860 = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
861 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
862 0, 0);
863
864 // The dtor took care of deleting the object.
865 return;
866 }
867 }
868 }
869
870 // Make sure that we call delete even if the dtor throws.
871 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
872 Ptr, OperatorDelete, ElementType);
873
874 if (Dtor)
875 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
876 /*ForVirtualBase=*/false, Ptr);
877
878 CGF.PopCleanupBlock();
879}
880
881namespace {
882 /// Calls the given 'operator delete' on an array of objects.
883 struct CallArrayDelete : EHScopeStack::Cleanup {
884 llvm::Value *Ptr;
885 const FunctionDecl *OperatorDelete;
886 llvm::Value *NumElements;
887 QualType ElementType;
888 CharUnits CookieSize;
889
890 CallArrayDelete(llvm::Value *Ptr,
891 const FunctionDecl *OperatorDelete,
892 llvm::Value *NumElements,
893 QualType ElementType,
894 CharUnits CookieSize)
895 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
896 ElementType(ElementType), CookieSize(CookieSize) {}
897
898 void Emit(CodeGenFunction &CGF, bool IsForEH) {
899 const FunctionProtoType *DeleteFTy =
900 OperatorDelete->getType()->getAs<FunctionProtoType>();
901 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
902
903 CallArgList Args;
904
905 // Pass the pointer as the first argument.
906 QualType VoidPtrTy = DeleteFTy->getArgType(0);
907 llvm::Value *DeletePtr
908 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
909 Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
910
911 // Pass the original requested size as the second argument.
912 if (DeleteFTy->getNumArgs() == 2) {
913 QualType size_t = DeleteFTy->getArgType(1);
914 const llvm::IntegerType *SizeTy
915 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
916
917 CharUnits ElementTypeSize =
918 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
919
920 // The size of an element, multiplied by the number of elements.
921 llvm::Value *Size
922 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
923 Size = CGF.Builder.CreateMul(Size, NumElements);
924
925 // Plus the size of the cookie if applicable.
926 if (!CookieSize.isZero()) {
927 llvm::Value *CookieSizeV
928 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
929 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
930 }
931
932 Args.push_back(std::make_pair(RValue::get(Size), size_t));
933 }
934
935 // Emit the call to delete.
936 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
937 CGF.CGM.GetAddrOfFunction(OperatorDelete),
938 ReturnValueSlot(), Args, OperatorDelete);
939 }
940 };
941}
942
943/// Emit the code for deleting an array of objects.
944static void EmitArrayDelete(CodeGenFunction &CGF,
945 const FunctionDecl *OperatorDelete,
946 llvm::Value *Ptr,
947 QualType ElementType) {
948 llvm::Value *NumElements = 0;
949 llvm::Value *AllocatedPtr = 0;
950 CharUnits CookieSize;
951 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, ElementType,
952 NumElements, AllocatedPtr, CookieSize);
953
954 assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
955
956 // Make sure that we call delete even if one of the dtors throws.
957 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
958 AllocatedPtr, OperatorDelete,
959 NumElements, ElementType,
960 CookieSize);
961
962 if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
963 if (!RD->hasTrivialDestructor()) {
964 assert(NumElements && "ReadArrayCookie didn't find element count"
965 " for a class with destructor");
966 CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
967 }
968 }
969
970 CGF.PopCleanupBlock();
971}
972
Anders Carlsson16d81b82009-09-22 22:53:17 +0000973void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000974
Douglas Gregor90916562009-09-29 18:16:17 +0000975 // Get at the argument before we performed the implicit conversion
976 // to void*.
977 const Expr *Arg = E->getArgument();
978 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +0000979 if (ICE->getCastKind() != CK_UserDefinedConversion &&
Douglas Gregor90916562009-09-29 18:16:17 +0000980 ICE->getType()->isVoidPointerType())
981 Arg = ICE->getSubExpr();
Douglas Gregord69dd782009-10-01 05:49:51 +0000982 else
983 break;
Douglas Gregor90916562009-09-29 18:16:17 +0000984 }
Anders Carlsson16d81b82009-09-22 22:53:17 +0000985
Douglas Gregor90916562009-09-29 18:16:17 +0000986 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000987
988 // Null check the pointer.
989 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
990 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
991
992 llvm::Value *IsNull =
993 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
994 "isnull");
995
996 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
997 EmitBlock(DeleteNotNull);
Anders Carlsson566abee2009-11-13 04:45:41 +0000998
John McCall1e7fe752010-09-02 09:58:18 +0000999 // We might be deleting a pointer to array. If so, GEP down to the
1000 // first non-array element.
1001 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1002 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1003 if (DeleteTy->isConstantArrayType()) {
1004 llvm::Value *Zero = Builder.getInt32(0);
1005 llvm::SmallVector<llvm::Value*,8> GEP;
1006
1007 GEP.push_back(Zero); // point at the outermost array
1008
1009 // For each layer of array type we're pointing at:
1010 while (const ConstantArrayType *Arr
1011 = getContext().getAsConstantArrayType(DeleteTy)) {
1012 // 1. Unpeel the array type.
1013 DeleteTy = Arr->getElementType();
1014
1015 // 2. GEP to the first element of the array.
1016 GEP.push_back(Zero);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001017 }
John McCall1e7fe752010-09-02 09:58:18 +00001018
1019 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001020 }
1021
Douglas Gregoreede61a2010-09-02 17:38:50 +00001022 assert(ConvertTypeForMem(DeleteTy) ==
1023 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall1e7fe752010-09-02 09:58:18 +00001024
1025 if (E->isArrayForm()) {
1026 EmitArrayDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1027 } else {
1028 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1029 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001030
Anders Carlsson16d81b82009-09-22 22:53:17 +00001031 EmitBlock(DeleteEnd);
1032}
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001033
1034llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1035 QualType Ty = E->getType();
1036 const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
Anders Carlsson31b7f522009-12-11 02:46:30 +00001037
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001038 if (E->isTypeOperand()) {
1039 llvm::Constant *TypeInfo =
1040 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1041 return Builder.CreateBitCast(TypeInfo, LTy);
1042 }
1043
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001044 Expr *subE = E->getExprOperand();
Mike Stump5fae8562009-11-17 22:33:00 +00001045 Ty = subE->getType();
1046 CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
1047 Ty = CanTy.getUnqualifiedType().getNonReferenceType();
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001048 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1049 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1050 if (RD->isPolymorphic()) {
1051 // FIXME: if subE is an lvalue do
1052 LValue Obj = EmitLValue(subE);
1053 llvm::Value *This = Obj.getAddress();
Mike Stumpf549e892009-11-15 16:52:53 +00001054 LTy = LTy->getPointerTo()->getPointerTo();
1055 llvm::Value *V = Builder.CreateBitCast(This, LTy);
1056 // We need to do a zero check for *p, unless it has NonNullAttr.
1057 // FIXME: PointerType->hasAttr<NonNullAttr>()
1058 bool CanBeZero = false;
Mike Stumpdb519a42009-11-17 00:45:21 +00001059 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens()))
John McCall2de56d12010-08-25 11:45:40 +00001060 if (UO->getOpcode() == UO_Deref)
Mike Stumpf549e892009-11-15 16:52:53 +00001061 CanBeZero = true;
1062 if (CanBeZero) {
1063 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
1064 llvm::BasicBlock *ZeroBlock = createBasicBlock();
1065
1066 llvm::Value *Zero = llvm::Constant::getNullValue(LTy);
1067 Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero),
1068 NonZeroBlock, ZeroBlock);
1069 EmitBlock(ZeroBlock);
1070 /// Call __cxa_bad_typeid
1071 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1072 const llvm::FunctionType *FTy;
1073 FTy = llvm::FunctionType::get(ResultType, false);
1074 llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
Mike Stumpc849c052009-11-16 06:50:58 +00001075 Builder.CreateCall(F)->setDoesNotReturn();
Mike Stumpf549e892009-11-15 16:52:53 +00001076 Builder.CreateUnreachable();
1077 EmitBlock(NonZeroBlock);
1078 }
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001079 V = Builder.CreateLoad(V, "vtable");
1080 V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
1081 V = Builder.CreateLoad(V);
1082 return V;
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001083 }
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001084 }
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001085 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001086}
Mike Stumpc849c052009-11-16 06:50:58 +00001087
1088llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
1089 const CXXDynamicCastExpr *DCE) {
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001090 QualType SrcTy = DCE->getSubExpr()->getType();
1091 QualType DestTy = DCE->getTypeAsWritten();
1092 QualType InnerType = DestTy->getPointeeType();
1093
Mike Stumpc849c052009-11-16 06:50:58 +00001094 const llvm::Type *LTy = ConvertType(DCE->getType());
Mike Stump2b35baf2009-11-16 22:52:20 +00001095
Mike Stumpc849c052009-11-16 06:50:58 +00001096 bool CanBeZero = false;
Mike Stumpc849c052009-11-16 06:50:58 +00001097 bool ToVoid = false;
Mike Stump2b35baf2009-11-16 22:52:20 +00001098 bool ThrowOnBad = false;
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001099 if (DestTy->isPointerType()) {
Mike Stumpc849c052009-11-16 06:50:58 +00001100 // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this
1101 CanBeZero = true;
1102 if (InnerType->isVoidType())
1103 ToVoid = true;
1104 } else {
1105 LTy = LTy->getPointerTo();
Douglas Gregor485ee322010-05-14 21:14:41 +00001106
1107 // FIXME: What if exceptions are disabled?
Mike Stumpc849c052009-11-16 06:50:58 +00001108 ThrowOnBad = true;
1109 }
1110
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001111 if (SrcTy->isPointerType() || SrcTy->isReferenceType())
1112 SrcTy = SrcTy->getPointeeType();
1113 SrcTy = SrcTy.getUnqualifiedType();
1114
Anders Carlsson6f0e4852009-12-18 14:55:04 +00001115 if (DestTy->isPointerType() || DestTy->isReferenceType())
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001116 DestTy = DestTy->getPointeeType();
1117 DestTy = DestTy.getUnqualifiedType();
Mike Stumpc849c052009-11-16 06:50:58 +00001118
Mike Stumpc849c052009-11-16 06:50:58 +00001119 llvm::BasicBlock *ContBlock = createBasicBlock();
1120 llvm::BasicBlock *NullBlock = 0;
1121 llvm::BasicBlock *NonZeroBlock = 0;
1122 if (CanBeZero) {
1123 NonZeroBlock = createBasicBlock();
1124 NullBlock = createBasicBlock();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001125 Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock);
Mike Stumpc849c052009-11-16 06:50:58 +00001126 EmitBlock(NonZeroBlock);
1127 }
1128
Mike Stumpc849c052009-11-16 06:50:58 +00001129 llvm::BasicBlock *BadCastBlock = 0;
Mike Stumpc849c052009-11-16 06:50:58 +00001130
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001131 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
Mike Stump2b35baf2009-11-16 22:52:20 +00001132
1133 // See if this is a dynamic_cast(void*)
1134 if (ToVoid) {
1135 llvm::Value *This = V;
1136 V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo());
1137 V = Builder.CreateLoad(V, "vtable");
1138 V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL);
1139 V = Builder.CreateLoad(V, "offset to top");
1140 This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext));
1141 V = Builder.CreateInBoundsGEP(This, V);
1142 V = Builder.CreateBitCast(V, LTy);
1143 } else {
1144 /// Call __dynamic_cast
1145 const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext);
1146 const llvm::FunctionType *FTy;
1147 std::vector<const llvm::Type*> ArgTys;
1148 const llvm::Type *PtrToInt8Ty
1149 = llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1150 ArgTys.push_back(PtrToInt8Ty);
1151 ArgTys.push_back(PtrToInt8Ty);
1152 ArgTys.push_back(PtrToInt8Ty);
1153 ArgTys.push_back(PtrDiffTy);
1154 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump2b35baf2009-11-16 22:52:20 +00001155
1156 // FIXME: Calculate better hint.
1157 llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001158
1159 assert(SrcTy->isRecordType() && "Src type must be record type!");
1160 assert(DestTy->isRecordType() && "Dest type must be record type!");
1161
Douglas Gregor154fe982009-12-23 22:04:40 +00001162 llvm::Value *SrcArg
1163 = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType());
1164 llvm::Value *DestArg
1165 = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001166
Mike Stump2b35baf2009-11-16 22:52:20 +00001167 V = Builder.CreateBitCast(V, PtrToInt8Ty);
1168 V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"),
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001169 V, SrcArg, DestArg, hint);
Mike Stump2b35baf2009-11-16 22:52:20 +00001170 V = Builder.CreateBitCast(V, LTy);
1171
1172 if (ThrowOnBad) {
1173 BadCastBlock = createBasicBlock();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001174 Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
Mike Stump2b35baf2009-11-16 22:52:20 +00001175 EmitBlock(BadCastBlock);
Douglas Gregor485ee322010-05-14 21:14:41 +00001176 /// Invoke __cxa_bad_cast
Mike Stump2b35baf2009-11-16 22:52:20 +00001177 ResultType = llvm::Type::getVoidTy(VMContext);
1178 const llvm::FunctionType *FBadTy;
Mike Stumpfde17be2009-11-17 03:01:03 +00001179 FBadTy = llvm::FunctionType::get(ResultType, false);
Mike Stump2b35baf2009-11-16 22:52:20 +00001180 llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
Douglas Gregor485ee322010-05-14 21:14:41 +00001181 if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
1182 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1183 Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
1184 EmitBlock(Cont);
1185 } else {
1186 // FIXME: Does this ever make sense?
1187 Builder.CreateCall(F)->setDoesNotReturn();
1188 }
Mike Stump8b152b82009-11-17 00:08:50 +00001189 Builder.CreateUnreachable();
Mike Stump2b35baf2009-11-16 22:52:20 +00001190 }
Mike Stumpc849c052009-11-16 06:50:58 +00001191 }
1192
1193 if (CanBeZero) {
1194 Builder.CreateBr(ContBlock);
1195 EmitBlock(NullBlock);
1196 Builder.CreateBr(ContBlock);
1197 }
1198 EmitBlock(ContBlock);
1199 if (CanBeZero) {
1200 llvm::PHINode *PHI = Builder.CreatePHI(LTy);
Mike Stump14431c12009-11-17 00:10:05 +00001201 PHI->reserveOperandSpace(2);
Mike Stumpc849c052009-11-16 06:50:58 +00001202 PHI->addIncoming(V, NonZeroBlock);
1203 PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock);
Mike Stumpc849c052009-11-16 06:50:58 +00001204 V = PHI;
1205 }
1206
1207 return V;
1208}