blob: 79e3b220ef97d9fdaa384cc80a8250a413166ba3 [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
John McCall1e7fe752010-09-02 09:58:18 +0000757 const llvm::Type *ElementPtrTy = ConvertType(AllocType)->getPointerTo(AS);
758 NewPtr = Builder.CreateBitCast(NewPtr, ElementPtrTy);
759 if (E->isArray()) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000760 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
John McCall1e7fe752010-09-02 09:58:18 +0000761
762 // NewPtr is a pointer to the base element type. If we're
763 // allocating an array of arrays, we'll need to cast back to the
764 // array pointer type.
765 const llvm::Type *ResultTy = ConvertType(E->getType());
766 if (NewPtr->getType() != ResultTy)
767 NewPtr = Builder.CreateBitCast(NewPtr, ResultTy);
768 } else {
Douglas Gregor59174c02010-07-21 01:10:17 +0000769 EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
Fariborz Jahanianceb43b62010-03-24 16:57:01 +0000770 }
771
Anders Carlsson16d81b82009-09-22 22:53:17 +0000772 if (NullCheckResult) {
773 Builder.CreateBr(NewEnd);
John McCall1e7fe752010-09-02 09:58:18 +0000774 llvm::BasicBlock *NotNullSource = Builder.GetInsertBlock();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000775 EmitBlock(NewEnd);
776
777 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
778 PHI->reserveOperandSpace(2);
John McCall1e7fe752010-09-02 09:58:18 +0000779 PHI->addIncoming(NewPtr, NotNullSource);
780 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()),
781 NullCheckSource);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000782
783 NewPtr = PHI;
784 }
John McCall1e7fe752010-09-02 09:58:18 +0000785
Anders Carlsson16d81b82009-09-22 22:53:17 +0000786 return NewPtr;
787}
788
Eli Friedman5fe05982009-11-18 00:50:08 +0000789void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
790 llvm::Value *Ptr,
791 QualType DeleteTy) {
John McCall1e7fe752010-09-02 09:58:18 +0000792 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
793
Eli Friedman5fe05982009-11-18 00:50:08 +0000794 const FunctionProtoType *DeleteFTy =
795 DeleteFD->getType()->getAs<FunctionProtoType>();
796
797 CallArgList DeleteArgs;
798
Anders Carlsson871d0782009-12-13 20:04:38 +0000799 // Check if we need to pass the size to the delete operator.
800 llvm::Value *Size = 0;
801 QualType SizeTy;
802 if (DeleteFTy->getNumArgs() == 2) {
803 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck4f122ef2010-01-26 19:59:28 +0000804 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
805 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
806 DeleteTypeSize.getQuantity());
Anders Carlsson871d0782009-12-13 20:04:38 +0000807 }
808
Eli Friedman5fe05982009-11-18 00:50:08 +0000809 QualType ArgTy = DeleteFTy->getArgType(0);
810 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
811 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
812
Anders Carlsson871d0782009-12-13 20:04:38 +0000813 if (Size)
Eli Friedman5fe05982009-11-18 00:50:08 +0000814 DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
Eli Friedman5fe05982009-11-18 00:50:08 +0000815
816 // Emit the call to delete.
John McCall04a67a62010-02-05 21:31:56 +0000817 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000818 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedman5fe05982009-11-18 00:50:08 +0000819 DeleteArgs, DeleteFD);
820}
821
John McCall1e7fe752010-09-02 09:58:18 +0000822namespace {
823 /// Calls the given 'operator delete' on a single object.
824 struct CallObjectDelete : EHScopeStack::Cleanup {
825 llvm::Value *Ptr;
826 const FunctionDecl *OperatorDelete;
827 QualType ElementType;
828
829 CallObjectDelete(llvm::Value *Ptr,
830 const FunctionDecl *OperatorDelete,
831 QualType ElementType)
832 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
833
834 void Emit(CodeGenFunction &CGF, bool IsForEH) {
835 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
836 }
837 };
838}
839
840/// Emit the code for deleting a single object.
841static void EmitObjectDelete(CodeGenFunction &CGF,
842 const FunctionDecl *OperatorDelete,
843 llvm::Value *Ptr,
844 QualType ElementType) {
845 // Find the destructor for the type, if applicable. If the
846 // destructor is virtual, we'll just emit the vcall and return.
847 const CXXDestructorDecl *Dtor = 0;
848 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
849 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
850 if (!RD->hasTrivialDestructor()) {
851 Dtor = RD->getDestructor();
852
853 if (Dtor->isVirtual()) {
854 const llvm::Type *Ty =
855 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor),
856 /*isVariadic=*/false);
857
858 llvm::Value *Callee
859 = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
860 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
861 0, 0);
862
863 // The dtor took care of deleting the object.
864 return;
865 }
866 }
867 }
868
869 // Make sure that we call delete even if the dtor throws.
870 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
871 Ptr, OperatorDelete, ElementType);
872
873 if (Dtor)
874 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
875 /*ForVirtualBase=*/false, Ptr);
876
877 CGF.PopCleanupBlock();
878}
879
880namespace {
881 /// Calls the given 'operator delete' on an array of objects.
882 struct CallArrayDelete : EHScopeStack::Cleanup {
883 llvm::Value *Ptr;
884 const FunctionDecl *OperatorDelete;
885 llvm::Value *NumElements;
886 QualType ElementType;
887 CharUnits CookieSize;
888
889 CallArrayDelete(llvm::Value *Ptr,
890 const FunctionDecl *OperatorDelete,
891 llvm::Value *NumElements,
892 QualType ElementType,
893 CharUnits CookieSize)
894 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
895 ElementType(ElementType), CookieSize(CookieSize) {}
896
897 void Emit(CodeGenFunction &CGF, bool IsForEH) {
898 const FunctionProtoType *DeleteFTy =
899 OperatorDelete->getType()->getAs<FunctionProtoType>();
900 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
901
902 CallArgList Args;
903
904 // Pass the pointer as the first argument.
905 QualType VoidPtrTy = DeleteFTy->getArgType(0);
906 llvm::Value *DeletePtr
907 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
908 Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
909
910 // Pass the original requested size as the second argument.
911 if (DeleteFTy->getNumArgs() == 2) {
912 QualType size_t = DeleteFTy->getArgType(1);
913 const llvm::IntegerType *SizeTy
914 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
915
916 CharUnits ElementTypeSize =
917 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
918
919 // The size of an element, multiplied by the number of elements.
920 llvm::Value *Size
921 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
922 Size = CGF.Builder.CreateMul(Size, NumElements);
923
924 // Plus the size of the cookie if applicable.
925 if (!CookieSize.isZero()) {
926 llvm::Value *CookieSizeV
927 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
928 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
929 }
930
931 Args.push_back(std::make_pair(RValue::get(Size), size_t));
932 }
933
934 // Emit the call to delete.
935 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
936 CGF.CGM.GetAddrOfFunction(OperatorDelete),
937 ReturnValueSlot(), Args, OperatorDelete);
938 }
939 };
940}
941
942/// Emit the code for deleting an array of objects.
943static void EmitArrayDelete(CodeGenFunction &CGF,
944 const FunctionDecl *OperatorDelete,
945 llvm::Value *Ptr,
946 QualType ElementType) {
947 llvm::Value *NumElements = 0;
948 llvm::Value *AllocatedPtr = 0;
949 CharUnits CookieSize;
950 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, ElementType,
951 NumElements, AllocatedPtr, CookieSize);
952
953 assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
954
955 // Make sure that we call delete even if one of the dtors throws.
956 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
957 AllocatedPtr, OperatorDelete,
958 NumElements, ElementType,
959 CookieSize);
960
961 if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
962 if (!RD->hasTrivialDestructor()) {
963 assert(NumElements && "ReadArrayCookie didn't find element count"
964 " for a class with destructor");
965 CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
966 }
967 }
968
969 CGF.PopCleanupBlock();
970}
971
Anders Carlsson16d81b82009-09-22 22:53:17 +0000972void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000973
Douglas Gregor90916562009-09-29 18:16:17 +0000974 // Get at the argument before we performed the implicit conversion
975 // to void*.
976 const Expr *Arg = E->getArgument();
977 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +0000978 if (ICE->getCastKind() != CK_UserDefinedConversion &&
Douglas Gregor90916562009-09-29 18:16:17 +0000979 ICE->getType()->isVoidPointerType())
980 Arg = ICE->getSubExpr();
Douglas Gregord69dd782009-10-01 05:49:51 +0000981 else
982 break;
Douglas Gregor90916562009-09-29 18:16:17 +0000983 }
Anders Carlsson16d81b82009-09-22 22:53:17 +0000984
Douglas Gregor90916562009-09-29 18:16:17 +0000985 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000986
987 // Null check the pointer.
988 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
989 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
990
991 llvm::Value *IsNull =
992 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
993 "isnull");
994
995 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
996 EmitBlock(DeleteNotNull);
Anders Carlsson566abee2009-11-13 04:45:41 +0000997
John McCall1e7fe752010-09-02 09:58:18 +0000998 // We might be deleting a pointer to array. If so, GEP down to the
999 // first non-array element.
1000 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1001 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1002 if (DeleteTy->isConstantArrayType()) {
1003 llvm::Value *Zero = Builder.getInt32(0);
1004 llvm::SmallVector<llvm::Value*,8> GEP;
1005
1006 GEP.push_back(Zero); // point at the outermost array
1007
1008 // For each layer of array type we're pointing at:
1009 while (const ConstantArrayType *Arr
1010 = getContext().getAsConstantArrayType(DeleteTy)) {
1011 // 1. Unpeel the array type.
1012 DeleteTy = Arr->getElementType();
1013
1014 // 2. GEP to the first element of the array.
1015 GEP.push_back(Zero);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001016 }
John McCall1e7fe752010-09-02 09:58:18 +00001017
1018 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001019 }
1020
Douglas Gregoreede61a2010-09-02 17:38:50 +00001021 assert(ConvertTypeForMem(DeleteTy) ==
1022 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall1e7fe752010-09-02 09:58:18 +00001023
1024 if (E->isArrayForm()) {
1025 EmitArrayDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1026 } else {
1027 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1028 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001029
Anders Carlsson16d81b82009-09-22 22:53:17 +00001030 EmitBlock(DeleteEnd);
1031}
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001032
1033llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1034 QualType Ty = E->getType();
1035 const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
Anders Carlsson31b7f522009-12-11 02:46:30 +00001036
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001037 if (E->isTypeOperand()) {
1038 llvm::Constant *TypeInfo =
1039 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1040 return Builder.CreateBitCast(TypeInfo, LTy);
1041 }
1042
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001043 Expr *subE = E->getExprOperand();
Mike Stump5fae8562009-11-17 22:33:00 +00001044 Ty = subE->getType();
1045 CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
1046 Ty = CanTy.getUnqualifiedType().getNonReferenceType();
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001047 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1048 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1049 if (RD->isPolymorphic()) {
1050 // FIXME: if subE is an lvalue do
1051 LValue Obj = EmitLValue(subE);
1052 llvm::Value *This = Obj.getAddress();
Mike Stumpf549e892009-11-15 16:52:53 +00001053 LTy = LTy->getPointerTo()->getPointerTo();
1054 llvm::Value *V = Builder.CreateBitCast(This, LTy);
1055 // We need to do a zero check for *p, unless it has NonNullAttr.
1056 // FIXME: PointerType->hasAttr<NonNullAttr>()
1057 bool CanBeZero = false;
Mike Stumpdb519a42009-11-17 00:45:21 +00001058 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens()))
John McCall2de56d12010-08-25 11:45:40 +00001059 if (UO->getOpcode() == UO_Deref)
Mike Stumpf549e892009-11-15 16:52:53 +00001060 CanBeZero = true;
1061 if (CanBeZero) {
1062 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
1063 llvm::BasicBlock *ZeroBlock = createBasicBlock();
1064
1065 llvm::Value *Zero = llvm::Constant::getNullValue(LTy);
1066 Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero),
1067 NonZeroBlock, ZeroBlock);
1068 EmitBlock(ZeroBlock);
1069 /// Call __cxa_bad_typeid
1070 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1071 const llvm::FunctionType *FTy;
1072 FTy = llvm::FunctionType::get(ResultType, false);
1073 llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
Mike Stumpc849c052009-11-16 06:50:58 +00001074 Builder.CreateCall(F)->setDoesNotReturn();
Mike Stumpf549e892009-11-15 16:52:53 +00001075 Builder.CreateUnreachable();
1076 EmitBlock(NonZeroBlock);
1077 }
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001078 V = Builder.CreateLoad(V, "vtable");
1079 V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
1080 V = Builder.CreateLoad(V);
1081 return V;
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001082 }
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001083 }
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001084 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001085}
Mike Stumpc849c052009-11-16 06:50:58 +00001086
1087llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
1088 const CXXDynamicCastExpr *DCE) {
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001089 QualType SrcTy = DCE->getSubExpr()->getType();
1090 QualType DestTy = DCE->getTypeAsWritten();
1091 QualType InnerType = DestTy->getPointeeType();
1092
Mike Stumpc849c052009-11-16 06:50:58 +00001093 const llvm::Type *LTy = ConvertType(DCE->getType());
Mike Stump2b35baf2009-11-16 22:52:20 +00001094
Mike Stumpc849c052009-11-16 06:50:58 +00001095 bool CanBeZero = false;
Mike Stumpc849c052009-11-16 06:50:58 +00001096 bool ToVoid = false;
Mike Stump2b35baf2009-11-16 22:52:20 +00001097 bool ThrowOnBad = false;
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001098 if (DestTy->isPointerType()) {
Mike Stumpc849c052009-11-16 06:50:58 +00001099 // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this
1100 CanBeZero = true;
1101 if (InnerType->isVoidType())
1102 ToVoid = true;
1103 } else {
1104 LTy = LTy->getPointerTo();
Douglas Gregor485ee322010-05-14 21:14:41 +00001105
1106 // FIXME: What if exceptions are disabled?
Mike Stumpc849c052009-11-16 06:50:58 +00001107 ThrowOnBad = true;
1108 }
1109
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001110 if (SrcTy->isPointerType() || SrcTy->isReferenceType())
1111 SrcTy = SrcTy->getPointeeType();
1112 SrcTy = SrcTy.getUnqualifiedType();
1113
Anders Carlsson6f0e4852009-12-18 14:55:04 +00001114 if (DestTy->isPointerType() || DestTy->isReferenceType())
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001115 DestTy = DestTy->getPointeeType();
1116 DestTy = DestTy.getUnqualifiedType();
Mike Stumpc849c052009-11-16 06:50:58 +00001117
Mike Stumpc849c052009-11-16 06:50:58 +00001118 llvm::BasicBlock *ContBlock = createBasicBlock();
1119 llvm::BasicBlock *NullBlock = 0;
1120 llvm::BasicBlock *NonZeroBlock = 0;
1121 if (CanBeZero) {
1122 NonZeroBlock = createBasicBlock();
1123 NullBlock = createBasicBlock();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001124 Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock);
Mike Stumpc849c052009-11-16 06:50:58 +00001125 EmitBlock(NonZeroBlock);
1126 }
1127
Mike Stumpc849c052009-11-16 06:50:58 +00001128 llvm::BasicBlock *BadCastBlock = 0;
Mike Stumpc849c052009-11-16 06:50:58 +00001129
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001130 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
Mike Stump2b35baf2009-11-16 22:52:20 +00001131
1132 // See if this is a dynamic_cast(void*)
1133 if (ToVoid) {
1134 llvm::Value *This = V;
1135 V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo());
1136 V = Builder.CreateLoad(V, "vtable");
1137 V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL);
1138 V = Builder.CreateLoad(V, "offset to top");
1139 This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext));
1140 V = Builder.CreateInBoundsGEP(This, V);
1141 V = Builder.CreateBitCast(V, LTy);
1142 } else {
1143 /// Call __dynamic_cast
1144 const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext);
1145 const llvm::FunctionType *FTy;
1146 std::vector<const llvm::Type*> ArgTys;
1147 const llvm::Type *PtrToInt8Ty
1148 = llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1149 ArgTys.push_back(PtrToInt8Ty);
1150 ArgTys.push_back(PtrToInt8Ty);
1151 ArgTys.push_back(PtrToInt8Ty);
1152 ArgTys.push_back(PtrDiffTy);
1153 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump2b35baf2009-11-16 22:52:20 +00001154
1155 // FIXME: Calculate better hint.
1156 llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001157
1158 assert(SrcTy->isRecordType() && "Src type must be record type!");
1159 assert(DestTy->isRecordType() && "Dest type must be record type!");
1160
Douglas Gregor154fe982009-12-23 22:04:40 +00001161 llvm::Value *SrcArg
1162 = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType());
1163 llvm::Value *DestArg
1164 = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001165
Mike Stump2b35baf2009-11-16 22:52:20 +00001166 V = Builder.CreateBitCast(V, PtrToInt8Ty);
1167 V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"),
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001168 V, SrcArg, DestArg, hint);
Mike Stump2b35baf2009-11-16 22:52:20 +00001169 V = Builder.CreateBitCast(V, LTy);
1170
1171 if (ThrowOnBad) {
1172 BadCastBlock = createBasicBlock();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001173 Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
Mike Stump2b35baf2009-11-16 22:52:20 +00001174 EmitBlock(BadCastBlock);
Douglas Gregor485ee322010-05-14 21:14:41 +00001175 /// Invoke __cxa_bad_cast
Mike Stump2b35baf2009-11-16 22:52:20 +00001176 ResultType = llvm::Type::getVoidTy(VMContext);
1177 const llvm::FunctionType *FBadTy;
Mike Stumpfde17be2009-11-17 03:01:03 +00001178 FBadTy = llvm::FunctionType::get(ResultType, false);
Mike Stump2b35baf2009-11-16 22:52:20 +00001179 llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
Douglas Gregor485ee322010-05-14 21:14:41 +00001180 if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
1181 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1182 Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
1183 EmitBlock(Cont);
1184 } else {
1185 // FIXME: Does this ever make sense?
1186 Builder.CreateCall(F)->setDoesNotReturn();
1187 }
Mike Stump8b152b82009-11-17 00:08:50 +00001188 Builder.CreateUnreachable();
Mike Stump2b35baf2009-11-16 22:52:20 +00001189 }
Mike Stumpc849c052009-11-16 06:50:58 +00001190 }
1191
1192 if (CanBeZero) {
1193 Builder.CreateBr(ContBlock);
1194 EmitBlock(NullBlock);
1195 Builder.CreateBr(ContBlock);
1196 }
1197 EmitBlock(ContBlock);
1198 if (CanBeZero) {
1199 llvm::PHINode *PHI = Builder.CreatePHI(LTy);
Mike Stump14431c12009-11-17 00:10:05 +00001200 PHI->reserveOperandSpace(2);
Mike Stumpc849c052009-11-16 06:50:58 +00001201 PHI->addIncoming(V, NonZeroBlock);
1202 PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock);
Mike Stumpc849c052009-11-16 06:50:58 +00001203 V = PHI;
1204 }
1205
1206 return V;
1207}