CGExprCXX.cpp revision 3e9438b5251a547253d64169863c2909b9b2772a
1//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with code generation of C++ expressions
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CGCXXABI.h"
16#include "CGObjCRuntime.h"
17#include "llvm/Intrinsics.h"
18using namespace clang;
19using namespace CodeGen;
20
21RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
22                                          llvm::Value *Callee,
23                                          ReturnValueSlot ReturnValue,
24                                          llvm::Value *This,
25                                          llvm::Value *VTT,
26                                          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
39  // 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
45  // And the rest of the call args
46  EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
47
48  QualType ResultType = FPT->getResultType();
49  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
50                                                 FPT->getExtInfo()),
51                  Callee, ReturnValue, Args, MD);
52}
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.
67  if (isa<CXXConstructExpr>(Base))
68    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  // Compute the object pointer.
98  llvm::Value *This;
99  if (ME->isArrow())
100    This = EmitScalarExpr(ME->getBase());
101  else {
102    LValue BaseLV = EmitLValue(ME->getBase());
103    if (BaseLV.isPropertyRef() || BaseLV.isKVCRef()) {
104      QualType QT = ME->getBase()->getType();
105      RValue RV =
106        BaseLV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(BaseLV, QT)
107          : EmitLoadOfKVCRefLValue(BaseLV, QT);
108      This = RV.isScalar() ? RV.getScalarVal() : RV.getAggregateAddr();
109    }
110    else
111      This = BaseLV.getAddress();
112  }
113
114  if (MD->isTrivial()) {
115    if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
116
117    assert(MD->isCopyAssignmentOperator() && "unknown trivial member function");
118    // We don't like to generate the trivial copy assignment operator when
119    // it isn't necessary; just produce the proper effect here.
120    llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
121    EmitAggregateCopy(This, RHS, CE->getType());
122    return RValue::get(This);
123  }
124
125  // Compute the function type we're calling.
126  const CGFunctionInfo &FInfo =
127    (isa<CXXDestructorDecl>(MD)
128     ? CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
129                                      Dtor_Complete)
130     : CGM.getTypes().getFunctionInfo(MD));
131
132  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
133  const llvm::Type *Ty
134    = CGM.getTypes().GetFunctionType(FInfo, FPT->isVariadic());
135
136  // C++ [class.virtual]p12:
137  //   Explicit qualification with the scope operator (5.1) suppresses the
138  //   virtual call mechanism.
139  //
140  // We also don't emit a virtual call if the base expression has a record type
141  // because then we know what the type is.
142  bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
143                     && !canDevirtualizeMemberFunctionCalls(ME->getBase());
144
145  llvm::Value *Callee;
146  if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
147    if (UseVirtualCall) {
148      Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
149    } else {
150      Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
151    }
152  } else if (UseVirtualCall) {
153    Callee = BuildVirtualCall(MD, This, Ty);
154  } else {
155    Callee = CGM.GetAddrOfFunction(MD, Ty);
156  }
157
158  return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
159                           CE->arg_begin(), CE->arg_end());
160}
161
162RValue
163CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
164                                              ReturnValueSlot ReturnValue) {
165  const BinaryOperator *BO =
166      cast<BinaryOperator>(E->getCallee()->IgnoreParens());
167  const Expr *BaseExpr = BO->getLHS();
168  const Expr *MemFnExpr = BO->getRHS();
169
170  const MemberPointerType *MPT =
171    MemFnExpr->getType()->getAs<MemberPointerType>();
172
173  const FunctionProtoType *FPT =
174    MPT->getPointeeType()->getAs<FunctionProtoType>();
175  const CXXRecordDecl *RD =
176    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
177
178  // Get the member function pointer.
179  llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
180
181  // Emit the 'this' pointer.
182  llvm::Value *This;
183
184  if (BO->getOpcode() == BO_PtrMemI)
185    This = EmitScalarExpr(BaseExpr);
186  else
187    This = EmitLValue(BaseExpr).getAddress();
188
189  // Ask the ABI to load the callee.  Note that This is modified.
190  llvm::Value *Callee =
191    CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(CGF, This, MemFnPtr, MPT);
192
193  CallArgList Args;
194
195  QualType ThisType =
196    getContext().getPointerType(getContext().getTagDeclType(RD));
197
198  // Push the this ptr.
199  Args.push_back(std::make_pair(RValue::get(This), ThisType));
200
201  // And the rest of the call args
202  EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
203  const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>();
204  return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee,
205                  ReturnValue, Args);
206}
207
208RValue
209CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
210                                               const CXXMethodDecl *MD,
211                                               ReturnValueSlot ReturnValue) {
212  assert(MD->isInstance() &&
213         "Trying to emit a member call expr on a static method!");
214  if (MD->isCopyAssignmentOperator()) {
215    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
216    if (ClassDecl->hasTrivialCopyAssignment()) {
217      assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
218             "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
219      LValue LV = EmitLValue(E->getArg(0));
220      llvm::Value *This;
221      if (LV.isPropertyRef() || LV.isKVCRef()) {
222        AggValueSlot Slot = CreateAggTemp(E->getArg(1)->getType());
223        EmitAggExpr(E->getArg(1), Slot);
224        if (LV.isPropertyRef())
225          EmitObjCPropertySet(LV.getPropertyRefExpr(), Slot.asRValue());
226        else
227          EmitObjCPropertySet(LV.getKVCRefExpr(), Slot.asRValue());
228        return RValue::getAggregate(0, false);
229      }
230      else
231        This = LV.getAddress();
232
233      llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
234      QualType Ty = E->getType();
235      EmitAggregateCopy(This, Src, Ty);
236      return RValue::get(This);
237    }
238  }
239
240  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
241  const llvm::Type *Ty =
242    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
243                                   FPT->isVariadic());
244  LValue LV = EmitLValue(E->getArg(0));
245  llvm::Value *This;
246  if (LV.isPropertyRef() || LV.isKVCRef()) {
247    QualType QT = E->getArg(0)->getType();
248    RValue RV =
249      LV.isPropertyRef() ? EmitLoadOfPropertyRefLValue(LV, QT)
250                         : EmitLoadOfKVCRefLValue(LV, QT);
251    assert (!RV.isScalar() && "EmitCXXOperatorMemberCallExpr");
252    This = RV.getAggregateAddr();
253  }
254  else
255    This = LV.getAddress();
256
257  llvm::Value *Callee;
258  if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
259    Callee = BuildVirtualCall(MD, This, Ty);
260  else
261    Callee = CGM.GetAddrOfFunction(MD, Ty);
262
263  return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
264                           E->arg_begin() + 1, E->arg_end());
265}
266
267void
268CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
269                                      AggValueSlot Dest) {
270  assert(!Dest.isIgnored() && "Must have a destination!");
271  const CXXConstructorDecl *CD = E->getConstructor();
272
273  // If we require zero initialization before (or instead of) calling the
274  // constructor, as can be the case with a non-user-provided default
275  // constructor, emit the zero initialization now.
276  if (E->requiresZeroInitialization())
277    EmitNullInitialization(Dest.getAddr(), E->getType());
278
279  // If this is a call to a trivial default constructor, do nothing.
280  if (CD->isTrivial() && CD->isDefaultConstructor())
281    return;
282
283  // Elide the constructor if we're constructing from a temporary.
284  // The temporary check is required because Sema sets this on NRVO
285  // returns.
286  if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
287    assert(getContext().hasSameUnqualifiedType(E->getType(),
288                                               E->getArg(0)->getType()));
289    if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
290      EmitAggExpr(E->getArg(0), Dest);
291      return;
292    }
293  }
294
295  const ConstantArrayType *Array
296    = getContext().getAsConstantArrayType(E->getType());
297  if (Array) {
298    QualType BaseElementTy = getContext().getBaseElementType(Array);
299    const llvm::Type *BasePtr = ConvertType(BaseElementTy);
300    BasePtr = llvm::PointerType::getUnqual(BasePtr);
301    llvm::Value *BaseAddrPtr =
302      Builder.CreateBitCast(Dest.getAddr(), BasePtr);
303
304    EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
305                               E->arg_begin(), E->arg_end());
306  }
307  else {
308    CXXCtorType Type =
309      (E->getConstructionKind() == CXXConstructExpr::CK_Complete)
310      ? Ctor_Complete : Ctor_Base;
311    bool ForVirtualBase =
312      E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
313
314    // Call the constructor.
315    EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
316                           E->arg_begin(), E->arg_end());
317  }
318}
319
320/// Check whether the given operator new[] is the global placement
321/// operator new[].
322static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
323                                        const FunctionDecl *Fn) {
324  // Must be in global scope.  Note that allocation functions can't be
325  // declared in namespaces.
326  if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
327    return false;
328
329  // Signature must be void *operator new[](size_t, void*).
330  // The size_t is common to all operator new[]s.
331  if (Fn->getNumParams() != 2)
332    return false;
333
334  CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
335  return (ParamType == Ctx.VoidPtrTy);
336}
337
338static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
339                                        const CXXNewExpr *E) {
340  if (!E->isArray())
341    return CharUnits::Zero();
342
343  // No cookie is required if the new operator being used is
344  // ::operator new[](size_t, void*).
345  const FunctionDecl *OperatorNew = E->getOperatorNew();
346  if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
347    return CharUnits::Zero();
348
349  return CGF.CGM.getCXXABI().GetArrayCookieSize(E->getAllocatedType());
350}
351
352static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
353                                        CodeGenFunction &CGF,
354                                        const CXXNewExpr *E,
355                                        llvm::Value *&NumElements,
356                                        llvm::Value *&SizeWithoutCookie) {
357  QualType ElemType = E->getAllocatedType();
358
359  const llvm::IntegerType *SizeTy =
360    cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
361
362  CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
363
364  if (!E->isArray()) {
365    SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
366    return SizeWithoutCookie;
367  }
368
369  // Figure out the cookie size.
370  CharUnits CookieSize = CalculateCookiePadding(CGF, E);
371
372  // Emit the array size expression.
373  // We multiply the size of all dimensions for NumElements.
374  // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
375  NumElements = CGF.EmitScalarExpr(E->getArraySize());
376  assert(NumElements->getType() == SizeTy && "element count not a size_t");
377
378  uint64_t ArraySizeMultiplier = 1;
379  while (const ConstantArrayType *CAT
380             = CGF.getContext().getAsConstantArrayType(ElemType)) {
381    ElemType = CAT->getElementType();
382    ArraySizeMultiplier *= CAT->getSize().getZExtValue();
383  }
384
385  llvm::Value *Size;
386
387  // If someone is doing 'new int[42]' there is no need to do a dynamic check.
388  // Don't bloat the -O0 code.
389  if (llvm::ConstantInt *NumElementsC =
390        dyn_cast<llvm::ConstantInt>(NumElements)) {
391    llvm::APInt NEC = NumElementsC->getValue();
392    unsigned SizeWidth = NEC.getBitWidth();
393
394    // Determine if there is an overflow here by doing an extended multiply.
395    NEC.zext(SizeWidth*2);
396    llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
397    SC *= NEC;
398
399    if (!CookieSize.isZero()) {
400      // Save the current size without a cookie.  We don't care if an
401      // overflow's already happened because SizeWithoutCookie isn't
402      // used if the allocator returns null or throws, as it should
403      // always do on an overflow.
404      llvm::APInt SWC = SC;
405      SWC.trunc(SizeWidth);
406      SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
407
408      // Add the cookie size.
409      SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
410    }
411
412    if (SC.countLeadingZeros() >= SizeWidth) {
413      SC.trunc(SizeWidth);
414      Size = llvm::ConstantInt::get(SizeTy, SC);
415    } else {
416      // On overflow, produce a -1 so operator new throws.
417      Size = llvm::Constant::getAllOnesValue(SizeTy);
418    }
419
420    // Scale NumElements while we're at it.
421    uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
422    NumElements = llvm::ConstantInt::get(SizeTy, N);
423
424  // Otherwise, we don't need to do an overflow-checked multiplication if
425  // we're multiplying by one.
426  } else if (TypeSize.isOne()) {
427    assert(ArraySizeMultiplier == 1);
428
429    Size = NumElements;
430
431    // If we need a cookie, add its size in with an overflow check.
432    // This is maybe a little paranoid.
433    if (!CookieSize.isZero()) {
434      SizeWithoutCookie = Size;
435
436      llvm::Value *CookieSizeV
437        = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
438
439      const llvm::Type *Types[] = { SizeTy };
440      llvm::Value *UAddF
441        = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
442      llvm::Value *AddRes
443        = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
444
445      Size = CGF.Builder.CreateExtractValue(AddRes, 0);
446      llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
447      Size = CGF.Builder.CreateSelect(DidOverflow,
448                                      llvm::ConstantInt::get(SizeTy, -1),
449                                      Size);
450    }
451
452  // Otherwise use the int.umul.with.overflow intrinsic.
453  } else {
454    llvm::Value *OutermostElementSize
455      = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
456
457    llvm::Value *NumOutermostElements = NumElements;
458
459    // Scale NumElements by the array size multiplier.  This might
460    // overflow, but only if the multiplication below also overflows,
461    // in which case this multiplication isn't used.
462    if (ArraySizeMultiplier != 1)
463      NumElements = CGF.Builder.CreateMul(NumElements,
464                         llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
465
466    // The requested size of the outermost array is non-constant.
467    // Multiply that by the static size of the elements of that array;
468    // on unsigned overflow, set the size to -1 to trigger an
469    // exception from the allocation routine.  This is sufficient to
470    // prevent buffer overruns from the allocator returning a
471    // seemingly valid pointer to insufficient space.  This idea comes
472    // originally from MSVC, and GCC has an open bug requesting
473    // similar behavior:
474    //   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
475    //
476    // This will not be sufficient for C++0x, which requires a
477    // specific exception class (std::bad_array_new_length).
478    // That will require ABI support that has not yet been specified.
479    const llvm::Type *Types[] = { SizeTy };
480    llvm::Value *UMulF
481      = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
482    llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
483                                                  OutermostElementSize);
484
485    // The overflow bit.
486    llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
487
488    // The result of the multiplication.
489    Size = CGF.Builder.CreateExtractValue(MulRes, 0);
490
491    // If we have a cookie, we need to add that size in, too.
492    if (!CookieSize.isZero()) {
493      SizeWithoutCookie = Size;
494
495      llvm::Value *CookieSizeV
496        = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
497      llvm::Value *UAddF
498        = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
499      llvm::Value *AddRes
500        = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
501
502      Size = CGF.Builder.CreateExtractValue(AddRes, 0);
503
504      llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
505      DidOverflow = CGF.Builder.CreateAnd(DidOverflow, AddDidOverflow);
506    }
507
508    Size = CGF.Builder.CreateSelect(DidOverflow,
509                                    llvm::ConstantInt::get(SizeTy, -1),
510                                    Size);
511  }
512
513  if (CookieSize.isZero())
514    SizeWithoutCookie = Size;
515  else
516    assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
517
518  return Size;
519}
520
521static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
522                                    llvm::Value *NewPtr) {
523
524  assert(E->getNumConstructorArgs() == 1 &&
525         "Can only have one argument to initializer of POD type.");
526
527  const Expr *Init = E->getConstructorArg(0);
528  QualType AllocType = E->getAllocatedType();
529
530  unsigned Alignment =
531    CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
532  if (!CGF.hasAggregateLLVMType(AllocType))
533    CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
534                          AllocType.isVolatileQualified(), Alignment,
535                          AllocType);
536  else if (AllocType->isAnyComplexType())
537    CGF.EmitComplexExprIntoAddr(Init, NewPtr,
538                                AllocType.isVolatileQualified());
539  else {
540    AggValueSlot Slot
541      = AggValueSlot::forAddr(NewPtr, AllocType.isVolatileQualified(), true);
542    CGF.EmitAggExpr(Init, Slot);
543  }
544}
545
546void
547CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
548                                         llvm::Value *NewPtr,
549                                         llvm::Value *NumElements) {
550  // We have a POD type.
551  if (E->getNumConstructorArgs() == 0)
552    return;
553
554  const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
555
556  // Create a temporary for the loop index and initialize it with 0.
557  llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
558  llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
559  Builder.CreateStore(Zero, IndexPtr);
560
561  // Start the loop with a block that tests the condition.
562  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
563  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
564
565  EmitBlock(CondBlock);
566
567  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
568
569  // Generate: if (loop-index < number-of-elements fall to the loop body,
570  // otherwise, go to the block after the for-loop.
571  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
572  llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
573  // If the condition is true, execute the body.
574  Builder.CreateCondBr(IsLess, ForBody, AfterFor);
575
576  EmitBlock(ForBody);
577
578  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
579  // Inside the loop body, emit the constructor call on the array element.
580  Counter = Builder.CreateLoad(IndexPtr);
581  llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
582                                                   "arrayidx");
583  StoreAnyExprIntoOneUnit(*this, E, Address);
584
585  EmitBlock(ContinueBlock);
586
587  // Emit the increment of the loop counter.
588  llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
589  Counter = Builder.CreateLoad(IndexPtr);
590  NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
591  Builder.CreateStore(NextVal, IndexPtr);
592
593  // Finally, branch back up to the condition for the next iteration.
594  EmitBranch(CondBlock);
595
596  // Emit the fall-through block.
597  EmitBlock(AfterFor, true);
598}
599
600static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
601                           llvm::Value *NewPtr, llvm::Value *Size) {
602  llvm::LLVMContext &VMContext = CGF.CGM.getLLVMContext();
603  const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
604  if (NewPtr->getType() != BP)
605    NewPtr = CGF.Builder.CreateBitCast(NewPtr, BP, "tmp");
606
607  CGF.Builder.CreateCall5(CGF.CGM.getMemSetFn(BP, CGF.IntPtrTy), NewPtr,
608                llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
609                          Size,
610                    llvm::ConstantInt::get(CGF.Int32Ty,
611                                           CGF.getContext().getTypeAlign(T)/8),
612                          llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
613                                                 0));
614}
615
616static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
617                               llvm::Value *NewPtr,
618                               llvm::Value *NumElements,
619                               llvm::Value *AllocSizeWithoutCookie) {
620  if (E->isArray()) {
621    if (CXXConstructorDecl *Ctor = E->getConstructor()) {
622      bool RequiresZeroInitialization = false;
623      if (Ctor->getParent()->hasTrivialConstructor()) {
624        // If new expression did not specify value-initialization, then there
625        // is no initialization.
626        if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
627          return;
628
629        if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
630          // Optimization: since zero initialization will just set the memory
631          // to all zeroes, generate a single memset to do it in one shot.
632          EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
633                         AllocSizeWithoutCookie);
634          return;
635        }
636
637        RequiresZeroInitialization = true;
638      }
639
640      CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
641                                     E->constructor_arg_begin(),
642                                     E->constructor_arg_end(),
643                                     RequiresZeroInitialization);
644      return;
645    } else if (E->getNumConstructorArgs() == 1 &&
646               isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
647      // Optimization: since zero initialization will just set the memory
648      // to all zeroes, generate a single memset to do it in one shot.
649      EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
650                     AllocSizeWithoutCookie);
651      return;
652    } else {
653      CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
654      return;
655    }
656  }
657
658  if (CXXConstructorDecl *Ctor = E->getConstructor()) {
659    // Per C++ [expr.new]p15, if we have an initializer, then we're performing
660    // direct initialization. C++ [dcl.init]p5 requires that we
661    // zero-initialize storage if there are no user-declared constructors.
662    if (E->hasInitializer() &&
663        !Ctor->getParent()->hasUserDeclaredConstructor() &&
664        !Ctor->getParent()->isEmpty())
665      CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
666
667    CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
668                               NewPtr, E->constructor_arg_begin(),
669                               E->constructor_arg_end());
670
671    return;
672  }
673  // We have a POD type.
674  if (E->getNumConstructorArgs() == 0)
675    return;
676
677  StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
678}
679
680/// A utility class for saving an rvalue.
681class SavedRValue {
682public:
683  enum Kind { ScalarLiteral, ScalarAddress,
684              AggregateLiteral, AggregateAddress,
685              Complex };
686
687private:
688  llvm::Value *Value;
689  Kind K;
690
691  SavedRValue(llvm::Value *V, Kind K) : Value(V), K(K) {}
692
693public:
694  SavedRValue() {}
695
696  static SavedRValue forScalarLiteral(llvm::Value *V) {
697    return SavedRValue(V, ScalarLiteral);
698  }
699
700  static SavedRValue forScalarAddress(llvm::Value *Addr) {
701    return SavedRValue(Addr, ScalarAddress);
702  }
703
704  static SavedRValue forAggregateLiteral(llvm::Value *V) {
705    return SavedRValue(V, AggregateLiteral);
706  }
707
708  static SavedRValue forAggregateAddress(llvm::Value *Addr) {
709    return SavedRValue(Addr, AggregateAddress);
710  }
711
712  static SavedRValue forComplexAddress(llvm::Value *Addr) {
713    return SavedRValue(Addr, Complex);
714  }
715
716  Kind getKind() const { return K; }
717  llvm::Value *getValue() const { return Value; }
718};
719
720/// Given an r-value, perform the code necessary to make sure that a
721/// future RestoreRValue will be able to load the value without
722/// domination concerns.
723static SavedRValue SaveRValue(CodeGenFunction &CGF, RValue RV) {
724  if (RV.isScalar()) {
725    llvm::Value *V = RV.getScalarVal();
726
727    // These automatically dominate and don't need to be saved.
728    if (isa<llvm::Constant>(V) || isa<llvm::AllocaInst>(V))
729      return SavedRValue::forScalarLiteral(V);
730
731    // Everything else needs an alloca.
732    llvm::Value *Addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
733    CGF.Builder.CreateStore(V, Addr);
734    return SavedRValue::forScalarAddress(Addr);
735  }
736
737  if (RV.isComplex()) {
738    CodeGenFunction::ComplexPairTy V = RV.getComplexVal();
739    const llvm::Type *ComplexTy =
740      llvm::StructType::get(CGF.getLLVMContext(),
741                            V.first->getType(), V.second->getType(),
742                            (void*) 0);
743    llvm::Value *Addr = CGF.CreateTempAlloca(ComplexTy, "saved-complex");
744    CGF.StoreComplexToAddr(V, Addr, /*volatile*/ false);
745    return SavedRValue::forComplexAddress(Addr);
746  }
747
748  assert(RV.isAggregate());
749  llvm::Value *V = RV.getAggregateAddr(); // TODO: volatile?
750  if (isa<llvm::Constant>(V) || isa<llvm::AllocaInst>(V))
751    return SavedRValue::forAggregateLiteral(V);
752
753  llvm::Value *Addr = CGF.CreateTempAlloca(V->getType(), "saved-rvalue");
754  CGF.Builder.CreateStore(V, Addr);
755  return SavedRValue::forAggregateAddress(Addr);
756}
757
758/// Given a saved r-value produced by SaveRValue, perform the code
759/// necessary to restore it to usability at the current insertion
760/// point.
761static RValue RestoreRValue(CodeGenFunction &CGF, SavedRValue RV) {
762  switch (RV.getKind()) {
763  case SavedRValue::ScalarLiteral:
764    return RValue::get(RV.getValue());
765  case SavedRValue::ScalarAddress:
766    return RValue::get(CGF.Builder.CreateLoad(RV.getValue()));
767  case SavedRValue::AggregateLiteral:
768    return RValue::getAggregate(RV.getValue());
769  case SavedRValue::AggregateAddress:
770    return RValue::getAggregate(CGF.Builder.CreateLoad(RV.getValue()));
771  case SavedRValue::Complex:
772    return RValue::getComplex(CGF.LoadComplexFromAddr(RV.getValue(), false));
773  }
774
775  llvm_unreachable("bad saved r-value kind");
776  return RValue();
777}
778
779namespace {
780  /// A cleanup to call the given 'operator delete' function upon
781  /// abnormal exit from a new expression.
782  class CallDeleteDuringNew : public EHScopeStack::Cleanup {
783    size_t NumPlacementArgs;
784    const FunctionDecl *OperatorDelete;
785    llvm::Value *Ptr;
786    llvm::Value *AllocSize;
787
788    RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
789
790  public:
791    static size_t getExtraSize(size_t NumPlacementArgs) {
792      return NumPlacementArgs * sizeof(RValue);
793    }
794
795    CallDeleteDuringNew(size_t NumPlacementArgs,
796                        const FunctionDecl *OperatorDelete,
797                        llvm::Value *Ptr,
798                        llvm::Value *AllocSize)
799      : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
800        Ptr(Ptr), AllocSize(AllocSize) {}
801
802    void setPlacementArg(unsigned I, RValue Arg) {
803      assert(I < NumPlacementArgs && "index out of range");
804      getPlacementArgs()[I] = Arg;
805    }
806
807    void Emit(CodeGenFunction &CGF, bool IsForEH) {
808      const FunctionProtoType *FPT
809        = OperatorDelete->getType()->getAs<FunctionProtoType>();
810      assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
811             (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
812
813      CallArgList DeleteArgs;
814
815      // The first argument is always a void*.
816      FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
817      DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++));
818
819      // A member 'operator delete' can take an extra 'size_t' argument.
820      if (FPT->getNumArgs() == NumPlacementArgs + 2)
821        DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++));
822
823      // Pass the rest of the arguments, which must match exactly.
824      for (unsigned I = 0; I != NumPlacementArgs; ++I)
825        DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++));
826
827      // Call 'operator delete'.
828      CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
829                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
830                   ReturnValueSlot(), DeleteArgs, OperatorDelete);
831    }
832  };
833
834  /// A cleanup to call the given 'operator delete' function upon
835  /// abnormal exit from a new expression when the new expression is
836  /// conditional.
837  class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
838    size_t NumPlacementArgs;
839    const FunctionDecl *OperatorDelete;
840    SavedRValue Ptr;
841    SavedRValue AllocSize;
842
843    SavedRValue *getPlacementArgs() {
844      return reinterpret_cast<SavedRValue*>(this+1);
845    }
846
847  public:
848    static size_t getExtraSize(size_t NumPlacementArgs) {
849      return NumPlacementArgs * sizeof(SavedRValue);
850    }
851
852    CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
853                                   const FunctionDecl *OperatorDelete,
854                                   SavedRValue Ptr,
855                                   SavedRValue AllocSize)
856      : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
857        Ptr(Ptr), AllocSize(AllocSize) {}
858
859    void setPlacementArg(unsigned I, SavedRValue Arg) {
860      assert(I < NumPlacementArgs && "index out of range");
861      getPlacementArgs()[I] = Arg;
862    }
863
864    void Emit(CodeGenFunction &CGF, bool IsForEH) {
865      const FunctionProtoType *FPT
866        = OperatorDelete->getType()->getAs<FunctionProtoType>();
867      assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
868             (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
869
870      CallArgList DeleteArgs;
871
872      // The first argument is always a void*.
873      FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
874      DeleteArgs.push_back(std::make_pair(RestoreRValue(CGF, Ptr), *AI++));
875
876      // A member 'operator delete' can take an extra 'size_t' argument.
877      if (FPT->getNumArgs() == NumPlacementArgs + 2) {
878        RValue RV = RestoreRValue(CGF, AllocSize);
879        DeleteArgs.push_back(std::make_pair(RV, *AI++));
880      }
881
882      // Pass the rest of the arguments, which must match exactly.
883      for (unsigned I = 0; I != NumPlacementArgs; ++I) {
884        RValue RV = RestoreRValue(CGF, getPlacementArgs()[I]);
885        DeleteArgs.push_back(std::make_pair(RV, *AI++));
886      }
887
888      // Call 'operator delete'.
889      CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
890                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
891                   ReturnValueSlot(), DeleteArgs, OperatorDelete);
892    }
893  };
894}
895
896/// Enter a cleanup to call 'operator delete' if the initializer in a
897/// new-expression throws.
898static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
899                                  const CXXNewExpr *E,
900                                  llvm::Value *NewPtr,
901                                  llvm::Value *AllocSize,
902                                  const CallArgList &NewArgs) {
903  // If we're not inside a conditional branch, then the cleanup will
904  // dominate and we can do the easier (and more efficient) thing.
905  if (!CGF.isInConditionalBranch()) {
906    CallDeleteDuringNew *Cleanup = CGF.EHStack
907      .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
908                                                 E->getNumPlacementArgs(),
909                                                 E->getOperatorDelete(),
910                                                 NewPtr, AllocSize);
911    for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
912      Cleanup->setPlacementArg(I, NewArgs[I+1].first);
913
914    return;
915  }
916
917  // Otherwise, we need to save all this stuff.
918  SavedRValue SavedNewPtr = SaveRValue(CGF, RValue::get(NewPtr));
919  SavedRValue SavedAllocSize = SaveRValue(CGF, RValue::get(AllocSize));
920
921  CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
922    .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(InactiveEHCleanup,
923                                                 E->getNumPlacementArgs(),
924                                                 E->getOperatorDelete(),
925                                                 SavedNewPtr,
926                                                 SavedAllocSize);
927  for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
928    Cleanup->setPlacementArg(I, SaveRValue(CGF, NewArgs[I+1].first));
929
930  CGF.ActivateCleanupBlock(CGF.EHStack.stable_begin());
931}
932
933llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
934  QualType AllocType = E->getAllocatedType();
935  if (AllocType->isArrayType())
936    while (const ArrayType *AType = getContext().getAsArrayType(AllocType))
937      AllocType = AType->getElementType();
938
939  FunctionDecl *NewFD = E->getOperatorNew();
940  const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>();
941
942  CallArgList NewArgs;
943
944  // The allocation size is the first argument.
945  QualType SizeTy = getContext().getSizeType();
946
947  llvm::Value *NumElements = 0;
948  llvm::Value *AllocSizeWithoutCookie = 0;
949  llvm::Value *AllocSize = EmitCXXNewAllocSize(getContext(),
950                                               *this, E, NumElements,
951                                               AllocSizeWithoutCookie);
952
953  NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
954
955  // Emit the rest of the arguments.
956  // FIXME: Ideally, this should just use EmitCallArgs.
957  CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
958
959  // First, use the types from the function type.
960  // We start at 1 here because the first argument (the allocation size)
961  // has already been emitted.
962  for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
963    QualType ArgType = NewFTy->getArgType(i);
964
965    assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
966           getTypePtr() ==
967           getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
968           "type mismatch in call argument!");
969
970    NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
971                                     ArgType));
972
973  }
974
975  // Either we've emitted all the call args, or we have a call to a
976  // variadic function.
977  assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
978         "Extra arguments in non-variadic function!");
979
980  // If we still have any arguments, emit them using the type of the argument.
981  for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
982       NewArg != NewArgEnd; ++NewArg) {
983    QualType ArgType = NewArg->getType();
984    NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
985                                     ArgType));
986  }
987
988  // Emit the call to new.
989  RValue RV =
990    EmitCall(CGM.getTypes().getFunctionInfo(NewArgs, NewFTy),
991             CGM.GetAddrOfFunction(NewFD), ReturnValueSlot(), NewArgs, NewFD);
992
993  // If an allocation function is declared with an empty exception specification
994  // it returns null to indicate failure to allocate storage. [expr.new]p13.
995  // (We don't need to check for null when there's no new initializer and
996  // we're allocating a POD type).
997  bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
998    !(AllocType->isPODType() && !E->hasInitializer());
999
1000  llvm::BasicBlock *NullCheckSource = 0;
1001  llvm::BasicBlock *NewNotNull = 0;
1002  llvm::BasicBlock *NewEnd = 0;
1003
1004  llvm::Value *NewPtr = RV.getScalarVal();
1005  unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
1006
1007  if (NullCheckResult) {
1008    NullCheckSource = Builder.GetInsertBlock();
1009    NewNotNull = createBasicBlock("new.notnull");
1010    NewEnd = createBasicBlock("new.end");
1011
1012    llvm::Value *IsNull = Builder.CreateIsNull(NewPtr, "new.isnull");
1013    Builder.CreateCondBr(IsNull, NewEnd, NewNotNull);
1014    EmitBlock(NewNotNull);
1015  }
1016
1017  assert((AllocSize == AllocSizeWithoutCookie) ==
1018         CalculateCookiePadding(*this, E).isZero());
1019  if (AllocSize != AllocSizeWithoutCookie) {
1020    assert(E->isArray());
1021    NewPtr = CGM.getCXXABI().InitializeArrayCookie(CGF, NewPtr, NumElements,
1022                                                   AllocType);
1023  }
1024
1025  // If there's an operator delete, enter a cleanup to call it if an
1026  // exception is thrown.
1027  EHScopeStack::stable_iterator CallOperatorDelete;
1028  if (E->getOperatorDelete()) {
1029    EnterNewDeleteCleanup(*this, E, NewPtr, AllocSize, NewArgs);
1030    CallOperatorDelete = EHStack.stable_begin();
1031  }
1032
1033  const llvm::Type *ElementPtrTy
1034    = ConvertTypeForMem(AllocType)->getPointerTo(AS);
1035  NewPtr = Builder.CreateBitCast(NewPtr, ElementPtrTy);
1036
1037  if (E->isArray()) {
1038    EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
1039
1040    // NewPtr is a pointer to the base element type.  If we're
1041    // allocating an array of arrays, we'll need to cast back to the
1042    // array pointer type.
1043    const llvm::Type *ResultTy = ConvertTypeForMem(E->getType());
1044    if (NewPtr->getType() != ResultTy)
1045      NewPtr = Builder.CreateBitCast(NewPtr, ResultTy);
1046  } else {
1047    EmitNewInitializer(*this, E, NewPtr, NumElements, AllocSizeWithoutCookie);
1048  }
1049
1050  // Deactivate the 'operator delete' cleanup if we finished
1051  // initialization.
1052  if (CallOperatorDelete.isValid())
1053    DeactivateCleanupBlock(CallOperatorDelete);
1054
1055  if (NullCheckResult) {
1056    Builder.CreateBr(NewEnd);
1057    llvm::BasicBlock *NotNullSource = Builder.GetInsertBlock();
1058    EmitBlock(NewEnd);
1059
1060    llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
1061    PHI->reserveOperandSpace(2);
1062    PHI->addIncoming(NewPtr, NotNullSource);
1063    PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()),
1064                     NullCheckSource);
1065
1066    NewPtr = PHI;
1067  }
1068
1069  return NewPtr;
1070}
1071
1072void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1073                                     llvm::Value *Ptr,
1074                                     QualType DeleteTy) {
1075  assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1076
1077  const FunctionProtoType *DeleteFTy =
1078    DeleteFD->getType()->getAs<FunctionProtoType>();
1079
1080  CallArgList DeleteArgs;
1081
1082  // Check if we need to pass the size to the delete operator.
1083  llvm::Value *Size = 0;
1084  QualType SizeTy;
1085  if (DeleteFTy->getNumArgs() == 2) {
1086    SizeTy = DeleteFTy->getArgType(1);
1087    CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1088    Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1089                                  DeleteTypeSize.getQuantity());
1090  }
1091
1092  QualType ArgTy = DeleteFTy->getArgType(0);
1093  llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1094  DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
1095
1096  if (Size)
1097    DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
1098
1099  // Emit the call to delete.
1100  EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
1101           CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
1102           DeleteArgs, DeleteFD);
1103}
1104
1105namespace {
1106  /// Calls the given 'operator delete' on a single object.
1107  struct CallObjectDelete : EHScopeStack::Cleanup {
1108    llvm::Value *Ptr;
1109    const FunctionDecl *OperatorDelete;
1110    QualType ElementType;
1111
1112    CallObjectDelete(llvm::Value *Ptr,
1113                     const FunctionDecl *OperatorDelete,
1114                     QualType ElementType)
1115      : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1116
1117    void Emit(CodeGenFunction &CGF, bool IsForEH) {
1118      CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1119    }
1120  };
1121}
1122
1123/// Emit the code for deleting a single object.
1124static void EmitObjectDelete(CodeGenFunction &CGF,
1125                             const FunctionDecl *OperatorDelete,
1126                             llvm::Value *Ptr,
1127                             QualType ElementType) {
1128  // Find the destructor for the type, if applicable.  If the
1129  // destructor is virtual, we'll just emit the vcall and return.
1130  const CXXDestructorDecl *Dtor = 0;
1131  if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1132    CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1133    if (!RD->hasTrivialDestructor()) {
1134      Dtor = RD->getDestructor();
1135
1136      if (Dtor->isVirtual()) {
1137        const llvm::Type *Ty =
1138          CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor,
1139                                                               Dtor_Complete),
1140                                         /*isVariadic=*/false);
1141
1142        llvm::Value *Callee
1143          = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
1144        CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
1145                              0, 0);
1146
1147        // The dtor took care of deleting the object.
1148        return;
1149      }
1150    }
1151  }
1152
1153  // Make sure that we call delete even if the dtor throws.
1154  CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1155                                            Ptr, OperatorDelete, ElementType);
1156
1157  if (Dtor)
1158    CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1159                              /*ForVirtualBase=*/false, Ptr);
1160
1161  CGF.PopCleanupBlock();
1162}
1163
1164namespace {
1165  /// Calls the given 'operator delete' on an array of objects.
1166  struct CallArrayDelete : EHScopeStack::Cleanup {
1167    llvm::Value *Ptr;
1168    const FunctionDecl *OperatorDelete;
1169    llvm::Value *NumElements;
1170    QualType ElementType;
1171    CharUnits CookieSize;
1172
1173    CallArrayDelete(llvm::Value *Ptr,
1174                    const FunctionDecl *OperatorDelete,
1175                    llvm::Value *NumElements,
1176                    QualType ElementType,
1177                    CharUnits CookieSize)
1178      : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1179        ElementType(ElementType), CookieSize(CookieSize) {}
1180
1181    void Emit(CodeGenFunction &CGF, bool IsForEH) {
1182      const FunctionProtoType *DeleteFTy =
1183        OperatorDelete->getType()->getAs<FunctionProtoType>();
1184      assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1185
1186      CallArgList Args;
1187
1188      // Pass the pointer as the first argument.
1189      QualType VoidPtrTy = DeleteFTy->getArgType(0);
1190      llvm::Value *DeletePtr
1191        = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1192      Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
1193
1194      // Pass the original requested size as the second argument.
1195      if (DeleteFTy->getNumArgs() == 2) {
1196        QualType size_t = DeleteFTy->getArgType(1);
1197        const llvm::IntegerType *SizeTy
1198          = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1199
1200        CharUnits ElementTypeSize =
1201          CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1202
1203        // The size of an element, multiplied by the number of elements.
1204        llvm::Value *Size
1205          = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1206        Size = CGF.Builder.CreateMul(Size, NumElements);
1207
1208        // Plus the size of the cookie if applicable.
1209        if (!CookieSize.isZero()) {
1210          llvm::Value *CookieSizeV
1211            = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1212          Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1213        }
1214
1215        Args.push_back(std::make_pair(RValue::get(Size), size_t));
1216      }
1217
1218      // Emit the call to delete.
1219      CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
1220                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
1221                   ReturnValueSlot(), Args, OperatorDelete);
1222    }
1223  };
1224}
1225
1226/// Emit the code for deleting an array of objects.
1227static void EmitArrayDelete(CodeGenFunction &CGF,
1228                            const FunctionDecl *OperatorDelete,
1229                            llvm::Value *Ptr,
1230                            QualType ElementType) {
1231  llvm::Value *NumElements = 0;
1232  llvm::Value *AllocatedPtr = 0;
1233  CharUnits CookieSize;
1234  CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, ElementType,
1235                                      NumElements, AllocatedPtr, CookieSize);
1236
1237  assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
1238
1239  // Make sure that we call delete even if one of the dtors throws.
1240  CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1241                                           AllocatedPtr, OperatorDelete,
1242                                           NumElements, ElementType,
1243                                           CookieSize);
1244
1245  if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
1246    if (!RD->hasTrivialDestructor()) {
1247      assert(NumElements && "ReadArrayCookie didn't find element count"
1248                            " for a class with destructor");
1249      CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
1250    }
1251  }
1252
1253  CGF.PopCleanupBlock();
1254}
1255
1256void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
1257
1258  // Get at the argument before we performed the implicit conversion
1259  // to void*.
1260  const Expr *Arg = E->getArgument();
1261  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
1262    if (ICE->getCastKind() != CK_UserDefinedConversion &&
1263        ICE->getType()->isVoidPointerType())
1264      Arg = ICE->getSubExpr();
1265    else
1266      break;
1267  }
1268
1269  llvm::Value *Ptr = EmitScalarExpr(Arg);
1270
1271  // Null check the pointer.
1272  llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1273  llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1274
1275  llvm::Value *IsNull =
1276    Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
1277                         "isnull");
1278
1279  Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1280  EmitBlock(DeleteNotNull);
1281
1282  // We might be deleting a pointer to array.  If so, GEP down to the
1283  // first non-array element.
1284  // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1285  QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1286  if (DeleteTy->isConstantArrayType()) {
1287    llvm::Value *Zero = Builder.getInt32(0);
1288    llvm::SmallVector<llvm::Value*,8> GEP;
1289
1290    GEP.push_back(Zero); // point at the outermost array
1291
1292    // For each layer of array type we're pointing at:
1293    while (const ConstantArrayType *Arr
1294             = getContext().getAsConstantArrayType(DeleteTy)) {
1295      // 1. Unpeel the array type.
1296      DeleteTy = Arr->getElementType();
1297
1298      // 2. GEP to the first element of the array.
1299      GEP.push_back(Zero);
1300    }
1301
1302    Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
1303  }
1304
1305  assert(ConvertTypeForMem(DeleteTy) ==
1306         cast<llvm::PointerType>(Ptr->getType())->getElementType());
1307
1308  if (E->isArrayForm()) {
1309    EmitArrayDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1310  } else {
1311    EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1312  }
1313
1314  EmitBlock(DeleteEnd);
1315}
1316
1317llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1318  QualType Ty = E->getType();
1319  const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
1320
1321  if (E->isTypeOperand()) {
1322    llvm::Constant *TypeInfo =
1323      CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1324    return Builder.CreateBitCast(TypeInfo, LTy);
1325  }
1326
1327  Expr *subE = E->getExprOperand();
1328  Ty = subE->getType();
1329  CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
1330  Ty = CanTy.getUnqualifiedType().getNonReferenceType();
1331  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1332    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1333    if (RD->isPolymorphic()) {
1334      // FIXME: if subE is an lvalue do
1335      LValue Obj = EmitLValue(subE);
1336      llvm::Value *This = Obj.getAddress();
1337      LTy = LTy->getPointerTo()->getPointerTo();
1338      llvm::Value *V = Builder.CreateBitCast(This, LTy);
1339      // We need to do a zero check for *p, unless it has NonNullAttr.
1340      // FIXME: PointerType->hasAttr<NonNullAttr>()
1341      bool CanBeZero = false;
1342      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens()))
1343        if (UO->getOpcode() == UO_Deref)
1344          CanBeZero = true;
1345      if (CanBeZero) {
1346        llvm::BasicBlock *NonZeroBlock = createBasicBlock();
1347        llvm::BasicBlock *ZeroBlock = createBasicBlock();
1348
1349        llvm::Value *Zero = llvm::Constant::getNullValue(LTy);
1350        Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero),
1351                             NonZeroBlock, ZeroBlock);
1352        EmitBlock(ZeroBlock);
1353        /// Call __cxa_bad_typeid
1354        const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1355        const llvm::FunctionType *FTy;
1356        FTy = llvm::FunctionType::get(ResultType, false);
1357        llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1358        Builder.CreateCall(F)->setDoesNotReturn();
1359        Builder.CreateUnreachable();
1360        EmitBlock(NonZeroBlock);
1361      }
1362      V = Builder.CreateLoad(V, "vtable");
1363      V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
1364      V = Builder.CreateLoad(V);
1365      return V;
1366    }
1367  }
1368  return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy);
1369}
1370
1371llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
1372                                              const CXXDynamicCastExpr *DCE) {
1373  QualType SrcTy = DCE->getSubExpr()->getType();
1374  QualType DestTy = DCE->getTypeAsWritten();
1375  QualType InnerType = DestTy->getPointeeType();
1376
1377  const llvm::Type *LTy = ConvertType(DCE->getType());
1378
1379  bool CanBeZero = false;
1380  bool ToVoid = false;
1381  bool ThrowOnBad = false;
1382  if (DestTy->isPointerType()) {
1383    // FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this
1384    CanBeZero = true;
1385    if (InnerType->isVoidType())
1386      ToVoid = true;
1387  } else {
1388    LTy = LTy->getPointerTo();
1389
1390    // FIXME: What if exceptions are disabled?
1391    ThrowOnBad = true;
1392  }
1393
1394  if (SrcTy->isPointerType() || SrcTy->isReferenceType())
1395    SrcTy = SrcTy->getPointeeType();
1396  SrcTy = SrcTy.getUnqualifiedType();
1397
1398  if (DestTy->isPointerType() || DestTy->isReferenceType())
1399    DestTy = DestTy->getPointeeType();
1400  DestTy = DestTy.getUnqualifiedType();
1401
1402  llvm::BasicBlock *ContBlock = createBasicBlock();
1403  llvm::BasicBlock *NullBlock = 0;
1404  llvm::BasicBlock *NonZeroBlock = 0;
1405  if (CanBeZero) {
1406    NonZeroBlock = createBasicBlock();
1407    NullBlock = createBasicBlock();
1408    Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock);
1409    EmitBlock(NonZeroBlock);
1410  }
1411
1412  llvm::BasicBlock *BadCastBlock = 0;
1413
1414  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
1415
1416  // See if this is a dynamic_cast(void*)
1417  if (ToVoid) {
1418    llvm::Value *This = V;
1419    V = Builder.CreateBitCast(This, PtrDiffTy->getPointerTo()->getPointerTo());
1420    V = Builder.CreateLoad(V, "vtable");
1421    V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL);
1422    V = Builder.CreateLoad(V, "offset to top");
1423    This = Builder.CreateBitCast(This, llvm::Type::getInt8PtrTy(VMContext));
1424    V = Builder.CreateInBoundsGEP(This, V);
1425    V = Builder.CreateBitCast(V, LTy);
1426  } else {
1427    /// Call __dynamic_cast
1428    const llvm::Type *ResultType = llvm::Type::getInt8PtrTy(VMContext);
1429    const llvm::FunctionType *FTy;
1430    std::vector<const llvm::Type*> ArgTys;
1431    const llvm::Type *PtrToInt8Ty
1432      = llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1433    ArgTys.push_back(PtrToInt8Ty);
1434    ArgTys.push_back(PtrToInt8Ty);
1435    ArgTys.push_back(PtrToInt8Ty);
1436    ArgTys.push_back(PtrDiffTy);
1437    FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1438
1439    // FIXME: Calculate better hint.
1440    llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL);
1441
1442    assert(SrcTy->isRecordType() && "Src type must be record type!");
1443    assert(DestTy->isRecordType() && "Dest type must be record type!");
1444
1445    llvm::Value *SrcArg
1446      = CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType());
1447    llvm::Value *DestArg
1448      = CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType());
1449
1450    V = Builder.CreateBitCast(V, PtrToInt8Ty);
1451    V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"),
1452                            V, SrcArg, DestArg, hint);
1453    V = Builder.CreateBitCast(V, LTy);
1454
1455    if (ThrowOnBad) {
1456      BadCastBlock = createBasicBlock();
1457      Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
1458      EmitBlock(BadCastBlock);
1459      /// Invoke __cxa_bad_cast
1460      ResultType = llvm::Type::getVoidTy(VMContext);
1461      const llvm::FunctionType *FBadTy;
1462      FBadTy = llvm::FunctionType::get(ResultType, false);
1463      llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
1464      if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
1465        llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1466        Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
1467        EmitBlock(Cont);
1468      } else {
1469        // FIXME: Does this ever make sense?
1470        Builder.CreateCall(F)->setDoesNotReturn();
1471      }
1472      Builder.CreateUnreachable();
1473    }
1474  }
1475
1476  if (CanBeZero) {
1477    Builder.CreateBr(ContBlock);
1478    EmitBlock(NullBlock);
1479    Builder.CreateBr(ContBlock);
1480  }
1481  EmitBlock(ContBlock);
1482  if (CanBeZero) {
1483    llvm::PHINode *PHI = Builder.CreatePHI(LTy);
1484    PHI->reserveOperandSpace(2);
1485    PHI->addIncoming(V, NonZeroBlock);
1486    PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock);
1487    V = PHI;
1488  }
1489
1490  return V;
1491}
1492