CGExprCXX.cpp revision ae3f7608756eb90d8dd5d014238437fcbf1c7de7
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 "CGCUDARuntime.h"
16#include "CGCXXABI.h"
17#include "CGDebugInfo.h"
18#include "CGObjCRuntime.h"
19#include "clang/Frontend/CodeGenOptions.h"
20#include "llvm/IR/Intrinsics.h"
21#include "llvm/Support/CallSite.h"
22
23using namespace clang;
24using namespace CodeGen;
25
26RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
27                                          SourceLocation CallLoc,
28                                          llvm::Value *Callee,
29                                          ReturnValueSlot ReturnValue,
30                                          llvm::Value *This,
31                                          llvm::Value *VTT,
32                                          CallExpr::const_arg_iterator ArgBeg,
33                                          CallExpr::const_arg_iterator ArgEnd) {
34  assert(MD->isInstance() &&
35         "Trying to emit a member call expr on a static method!");
36
37  // C++11 [class.mfct.non-static]p2:
38  //   If a non-static member function of a class X is called for an object that
39  //   is not of type X, or of a type derived from X, the behavior is undefined.
40  EmitTypeCheck(isa<CXXConstructorDecl>(MD) ? TCK_ConstructorCall
41                                            : TCK_MemberCall,
42                CallLoc, This, getContext().getRecordType(MD->getParent()));
43
44  CallArgList Args;
45
46  // Push the this ptr.
47  Args.add(RValue::get(This), MD->getThisType(getContext()));
48
49  // If there is a VTT parameter, emit it.
50  if (VTT) {
51    QualType T = getContext().getPointerType(getContext().VoidPtrTy);
52    Args.add(RValue::get(VTT), T);
53  }
54
55  const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
56  RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size());
57
58  // And the rest of the call args.
59  EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
60
61  return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required),
62                  Callee, ReturnValue, Args, MD);
63}
64
65// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
66// quite what we want.
67static const Expr *skipNoOpCastsAndParens(const Expr *E) {
68  while (true) {
69    if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
70      E = PE->getSubExpr();
71      continue;
72    }
73
74    if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
75      if (CE->getCastKind() == CK_NoOp) {
76        E = CE->getSubExpr();
77        continue;
78      }
79    }
80    if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
81      if (UO->getOpcode() == UO_Extension) {
82        E = UO->getSubExpr();
83        continue;
84      }
85    }
86    return E;
87  }
88}
89
90/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
91/// expr can be devirtualized.
92static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
93                                               const Expr *Base,
94                                               const CXXMethodDecl *MD) {
95
96  // When building with -fapple-kext, all calls must go through the vtable since
97  // the kernel linker can do runtime patching of vtables.
98  if (Context.getLangOpts().AppleKext)
99    return false;
100
101  // If the most derived class is marked final, we know that no subclass can
102  // override this member function and so we can devirtualize it. For example:
103  //
104  // struct A { virtual void f(); }
105  // struct B final : A { };
106  //
107  // void f(B *b) {
108  //   b->f();
109  // }
110  //
111  const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
112  if (MostDerivedClassDecl->hasAttr<FinalAttr>())
113    return true;
114
115  // If the member function is marked 'final', we know that it can't be
116  // overridden and can therefore devirtualize it.
117  if (MD->hasAttr<FinalAttr>())
118    return true;
119
120  // Similarly, if the class itself is marked 'final' it can't be overridden
121  // and we can therefore devirtualize the member function call.
122  if (MD->getParent()->hasAttr<FinalAttr>())
123    return true;
124
125  Base = skipNoOpCastsAndParens(Base);
126  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
127    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
128      // This is a record decl. We know the type and can devirtualize it.
129      return VD->getType()->isRecordType();
130    }
131
132    return false;
133  }
134
135  // We can devirtualize calls on an object accessed by a class member access
136  // expression, since by C++11 [basic.life]p6 we know that it can't refer to
137  // a derived class object constructed in the same location.
138  if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base))
139    if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl()))
140      return VD->getType()->isRecordType();
141
142  // We can always devirtualize calls on temporary object expressions.
143  if (isa<CXXConstructExpr>(Base))
144    return true;
145
146  // And calls on bound temporaries.
147  if (isa<CXXBindTemporaryExpr>(Base))
148    return true;
149
150  // Check if this is a call expr that returns a record type.
151  if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
152    return CE->getCallReturnType()->isRecordType();
153
154  // We can't devirtualize the call.
155  return false;
156}
157
158static CXXRecordDecl *getCXXRecord(const Expr *E) {
159  QualType T = E->getType();
160  if (const PointerType *PTy = T->getAs<PointerType>())
161    T = PTy->getPointeeType();
162  const RecordType *Ty = T->castAs<RecordType>();
163  return cast<CXXRecordDecl>(Ty->getDecl());
164}
165
166// Note: This function also emit constructor calls to support a MSVC
167// extensions allowing explicit constructor function call.
168RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
169                                              ReturnValueSlot ReturnValue) {
170  const Expr *callee = CE->getCallee()->IgnoreParens();
171
172  if (isa<BinaryOperator>(callee))
173    return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
174
175  const MemberExpr *ME = cast<MemberExpr>(callee);
176  const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
177
178  CGDebugInfo *DI = getDebugInfo();
179  if (DI &&
180      CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo &&
181      !isa<CallExpr>(ME->getBase())) {
182    QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType();
183    if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) {
184      DI->getOrCreateRecordType(PTy->getPointeeType(),
185                                MD->getParent()->getLocation());
186    }
187  }
188
189  if (MD->isStatic()) {
190    // The method is static, emit it as we would a regular call.
191    llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
192    return EmitCall(getContext().getPointerType(MD->getType()), Callee,
193                    ReturnValue, CE->arg_begin(), CE->arg_end());
194  }
195
196  // Compute the object pointer.
197  const Expr *Base = ME->getBase();
198  bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier();
199
200  const CXXMethodDecl *DevirtualizedMethod = NULL;
201  if (CanUseVirtualCall &&
202      canDevirtualizeMemberFunctionCalls(getContext(), Base, MD)) {
203    const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
204    DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl);
205    assert(DevirtualizedMethod);
206    const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent();
207    const Expr *Inner = Base->ignoreParenBaseCasts();
208    if (getCXXRecord(Inner) == DevirtualizedClass)
209      // If the class of the Inner expression is where the dynamic method
210      // is defined, build the this pointer from it.
211      Base = Inner;
212    else if (getCXXRecord(Base) != DevirtualizedClass) {
213      // If the method is defined in a class that is not the best dynamic
214      // one or the one of the full expression, we would have to build
215      // a derived-to-base cast to compute the correct this pointer, but
216      // we don't have support for that yet, so do a virtual call.
217      DevirtualizedMethod = NULL;
218    }
219    // If the return types are not the same, this might be a case where more
220    // code needs to run to compensate for it. For example, the derived
221    // method might return a type that inherits form from the return
222    // type of MD and has a prefix.
223    // For now we just avoid devirtualizing these covariant cases.
224    if (DevirtualizedMethod &&
225        DevirtualizedMethod->getResultType().getCanonicalType() !=
226        MD->getResultType().getCanonicalType())
227      DevirtualizedMethod = NULL;
228  }
229
230  llvm::Value *This;
231  if (ME->isArrow())
232    This = EmitScalarExpr(Base);
233  else
234    This = EmitLValue(Base).getAddress();
235
236
237  if (MD->isTrivial()) {
238    if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
239    if (isa<CXXConstructorDecl>(MD) &&
240        cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
241      return RValue::get(0);
242
243    if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) {
244      // We don't like to generate the trivial copy/move assignment operator
245      // when it isn't necessary; just produce the proper effect here.
246      llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
247      EmitAggregateAssign(This, RHS, CE->getType());
248      return RValue::get(This);
249    }
250
251    if (isa<CXXConstructorDecl>(MD) &&
252        cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) {
253      // Trivial move and copy ctor are the same.
254      llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
255      EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
256                                     CE->arg_begin(), CE->arg_end());
257      return RValue::get(This);
258    }
259    llvm_unreachable("unknown trivial member function");
260  }
261
262  // Compute the function type we're calling.
263  const CXXMethodDecl *CalleeDecl = DevirtualizedMethod ? DevirtualizedMethod : MD;
264  const CGFunctionInfo *FInfo = 0;
265  if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl))
266    FInfo = &CGM.getTypes().arrangeCXXDestructor(Dtor,
267                                                 Dtor_Complete);
268  else if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl))
269    FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor,
270                                                             Ctor_Complete);
271  else
272    FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl);
273
274  llvm::Type *Ty = CGM.getTypes().GetFunctionType(*FInfo);
275
276  // C++ [class.virtual]p12:
277  //   Explicit qualification with the scope operator (5.1) suppresses the
278  //   virtual call mechanism.
279  //
280  // We also don't emit a virtual call if the base expression has a record type
281  // because then we know what the type is.
282  bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod;
283
284  llvm::Value *Callee;
285  if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
286    if (UseVirtualCall) {
287      Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
288    } else {
289      if (getLangOpts().AppleKext &&
290          MD->isVirtual() &&
291          ME->hasQualifier())
292        Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
293      else if (!DevirtualizedMethod)
294        Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
295      else {
296        const CXXDestructorDecl *DDtor =
297          cast<CXXDestructorDecl>(DevirtualizedMethod);
298        Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty);
299      }
300    }
301  } else if (const CXXConstructorDecl *Ctor =
302               dyn_cast<CXXConstructorDecl>(MD)) {
303    Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
304  } else if (UseVirtualCall) {
305      Callee = BuildVirtualCall(MD, This, Ty);
306  } else {
307    if (getLangOpts().AppleKext &&
308        MD->isVirtual() &&
309        ME->hasQualifier())
310      Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
311    else if (!DevirtualizedMethod)
312      Callee = CGM.GetAddrOfFunction(MD, Ty);
313    else {
314      Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty);
315    }
316  }
317
318  return EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This,
319                           /*VTT=*/0, CE->arg_begin(), CE->arg_end());
320}
321
322RValue
323CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
324                                              ReturnValueSlot ReturnValue) {
325  const BinaryOperator *BO =
326      cast<BinaryOperator>(E->getCallee()->IgnoreParens());
327  const Expr *BaseExpr = BO->getLHS();
328  const Expr *MemFnExpr = BO->getRHS();
329
330  const MemberPointerType *MPT =
331    MemFnExpr->getType()->castAs<MemberPointerType>();
332
333  const FunctionProtoType *FPT =
334    MPT->getPointeeType()->castAs<FunctionProtoType>();
335  const CXXRecordDecl *RD =
336    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
337
338  // Get the member function pointer.
339  llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
340
341  // Emit the 'this' pointer.
342  llvm::Value *This;
343
344  if (BO->getOpcode() == BO_PtrMemI)
345    This = EmitScalarExpr(BaseExpr);
346  else
347    This = EmitLValue(BaseExpr).getAddress();
348
349  EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This,
350                QualType(MPT->getClass(), 0));
351
352  // Ask the ABI to load the callee.  Note that This is modified.
353  llvm::Value *Callee =
354    CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
355
356  CallArgList Args;
357
358  QualType ThisType =
359    getContext().getPointerType(getContext().getTagDeclType(RD));
360
361  // Push the this ptr.
362  Args.add(RValue::get(This), ThisType);
363
364  RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1);
365
366  // And the rest of the call args
367  EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
368  return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), Callee,
369                  ReturnValue, Args);
370}
371
372RValue
373CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
374                                               const CXXMethodDecl *MD,
375                                               ReturnValueSlot ReturnValue) {
376  assert(MD->isInstance() &&
377         "Trying to emit a member call expr on a static method!");
378  LValue LV = EmitLValue(E->getArg(0));
379  llvm::Value *This = LV.getAddress();
380
381  if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) &&
382      MD->isTrivial()) {
383    llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
384    QualType Ty = E->getType();
385    EmitAggregateAssign(This, Src, Ty);
386    return RValue::get(This);
387  }
388
389  llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This);
390  return EmitCXXMemberCall(MD, E->getExprLoc(), Callee, ReturnValue, This,
391                           /*VTT=*/0, E->arg_begin() + 1, E->arg_end());
392}
393
394RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
395                                               ReturnValueSlot ReturnValue) {
396  return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue);
397}
398
399static void EmitNullBaseClassInitialization(CodeGenFunction &CGF,
400                                            llvm::Value *DestPtr,
401                                            const CXXRecordDecl *Base) {
402  if (Base->isEmpty())
403    return;
404
405  DestPtr = CGF.EmitCastToVoidPtr(DestPtr);
406
407  const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base);
408  CharUnits Size = Layout.getNonVirtualSize();
409  CharUnits Align = Layout.getNonVirtualAlign();
410
411  llvm::Value *SizeVal = CGF.CGM.getSize(Size);
412
413  // If the type contains a pointer to data member we can't memset it to zero.
414  // Instead, create a null constant and copy it to the destination.
415  // TODO: there are other patterns besides zero that we can usefully memset,
416  // like -1, which happens to be the pattern used by member-pointers.
417  // TODO: isZeroInitializable can be over-conservative in the case where a
418  // virtual base contains a member pointer.
419  if (!CGF.CGM.getTypes().isZeroInitializable(Base)) {
420    llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base);
421
422    llvm::GlobalVariable *NullVariable =
423      new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(),
424                               /*isConstant=*/true,
425                               llvm::GlobalVariable::PrivateLinkage,
426                               NullConstant, Twine());
427    NullVariable->setAlignment(Align.getQuantity());
428    llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable);
429
430    // Get and call the appropriate llvm.memcpy overload.
431    CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity());
432    return;
433  }
434
435  // Otherwise, just memset the whole thing to zero.  This is legal
436  // because in LLVM, all default initializers (other than the ones we just
437  // handled above) are guaranteed to have a bit pattern of all zeros.
438  CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal,
439                           Align.getQuantity());
440}
441
442void
443CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
444                                      AggValueSlot Dest) {
445  assert(!Dest.isIgnored() && "Must have a destination!");
446  const CXXConstructorDecl *CD = E->getConstructor();
447
448  // If we require zero initialization before (or instead of) calling the
449  // constructor, as can be the case with a non-user-provided default
450  // constructor, emit the zero initialization now, unless destination is
451  // already zeroed.
452  if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
453    switch (E->getConstructionKind()) {
454    case CXXConstructExpr::CK_Delegating:
455    case CXXConstructExpr::CK_Complete:
456      EmitNullInitialization(Dest.getAddr(), E->getType());
457      break;
458    case CXXConstructExpr::CK_VirtualBase:
459    case CXXConstructExpr::CK_NonVirtualBase:
460      EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent());
461      break;
462    }
463  }
464
465  // If this is a call to a trivial default constructor, do nothing.
466  if (CD->isTrivial() && CD->isDefaultConstructor())
467    return;
468
469  // Elide the constructor if we're constructing from a temporary.
470  // The temporary check is required because Sema sets this on NRVO
471  // returns.
472  if (getLangOpts().ElideConstructors && E->isElidable()) {
473    assert(getContext().hasSameUnqualifiedType(E->getType(),
474                                               E->getArg(0)->getType()));
475    if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
476      EmitAggExpr(E->getArg(0), Dest);
477      return;
478    }
479  }
480
481  if (const ConstantArrayType *arrayType
482        = getContext().getAsConstantArrayType(E->getType())) {
483    EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(),
484                               E->arg_begin(), E->arg_end());
485  } else {
486    CXXCtorType Type = Ctor_Complete;
487    bool ForVirtualBase = false;
488    bool Delegating = false;
489
490    switch (E->getConstructionKind()) {
491     case CXXConstructExpr::CK_Delegating:
492      // We should be emitting a constructor; GlobalDecl will assert this
493      Type = CurGD.getCtorType();
494      Delegating = true;
495      break;
496
497     case CXXConstructExpr::CK_Complete:
498      Type = Ctor_Complete;
499      break;
500
501     case CXXConstructExpr::CK_VirtualBase:
502      ForVirtualBase = true;
503      // fall-through
504
505     case CXXConstructExpr::CK_NonVirtualBase:
506      Type = Ctor_Base;
507    }
508
509    // Call the constructor.
510    EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest.getAddr(),
511                           E->arg_begin(), E->arg_end());
512  }
513}
514
515void
516CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
517                                            llvm::Value *Src,
518                                            const Expr *Exp) {
519  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
520    Exp = E->getSubExpr();
521  assert(isa<CXXConstructExpr>(Exp) &&
522         "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
523  const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
524  const CXXConstructorDecl *CD = E->getConstructor();
525  RunCleanupsScope Scope(*this);
526
527  // If we require zero initialization before (or instead of) calling the
528  // constructor, as can be the case with a non-user-provided default
529  // constructor, emit the zero initialization now.
530  // FIXME. Do I still need this for a copy ctor synthesis?
531  if (E->requiresZeroInitialization())
532    EmitNullInitialization(Dest, E->getType());
533
534  assert(!getContext().getAsConstantArrayType(E->getType())
535         && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
536  EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
537                                 E->arg_begin(), E->arg_end());
538}
539
540static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
541                                        const CXXNewExpr *E) {
542  if (!E->isArray())
543    return CharUnits::Zero();
544
545  // No cookie is required if the operator new[] being used is the
546  // reserved placement operator new[].
547  if (E->getOperatorNew()->isReservedGlobalPlacementOperator())
548    return CharUnits::Zero();
549
550  return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
551}
552
553static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF,
554                                        const CXXNewExpr *e,
555                                        unsigned minElements,
556                                        llvm::Value *&numElements,
557                                        llvm::Value *&sizeWithoutCookie) {
558  QualType type = e->getAllocatedType();
559
560  if (!e->isArray()) {
561    CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
562    sizeWithoutCookie
563      = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity());
564    return sizeWithoutCookie;
565  }
566
567  // The width of size_t.
568  unsigned sizeWidth = CGF.SizeTy->getBitWidth();
569
570  // Figure out the cookie size.
571  llvm::APInt cookieSize(sizeWidth,
572                         CalculateCookiePadding(CGF, e).getQuantity());
573
574  // Emit the array size expression.
575  // We multiply the size of all dimensions for NumElements.
576  // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
577  numElements = CGF.EmitScalarExpr(e->getArraySize());
578  assert(isa<llvm::IntegerType>(numElements->getType()));
579
580  // The number of elements can be have an arbitrary integer type;
581  // essentially, we need to multiply it by a constant factor, add a
582  // cookie size, and verify that the result is representable as a
583  // size_t.  That's just a gloss, though, and it's wrong in one
584  // important way: if the count is negative, it's an error even if
585  // the cookie size would bring the total size >= 0.
586  bool isSigned
587    = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType();
588  llvm::IntegerType *numElementsType
589    = cast<llvm::IntegerType>(numElements->getType());
590  unsigned numElementsWidth = numElementsType->getBitWidth();
591
592  // Compute the constant factor.
593  llvm::APInt arraySizeMultiplier(sizeWidth, 1);
594  while (const ConstantArrayType *CAT
595             = CGF.getContext().getAsConstantArrayType(type)) {
596    type = CAT->getElementType();
597    arraySizeMultiplier *= CAT->getSize();
598  }
599
600  CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
601  llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
602  typeSizeMultiplier *= arraySizeMultiplier;
603
604  // This will be a size_t.
605  llvm::Value *size;
606
607  // If someone is doing 'new int[42]' there is no need to do a dynamic check.
608  // Don't bloat the -O0 code.
609  if (llvm::ConstantInt *numElementsC =
610        dyn_cast<llvm::ConstantInt>(numElements)) {
611    const llvm::APInt &count = numElementsC->getValue();
612
613    bool hasAnyOverflow = false;
614
615    // If 'count' was a negative number, it's an overflow.
616    if (isSigned && count.isNegative())
617      hasAnyOverflow = true;
618
619    // We want to do all this arithmetic in size_t.  If numElements is
620    // wider than that, check whether it's already too big, and if so,
621    // overflow.
622    else if (numElementsWidth > sizeWidth &&
623             numElementsWidth - sizeWidth > count.countLeadingZeros())
624      hasAnyOverflow = true;
625
626    // Okay, compute a count at the right width.
627    llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
628
629    // If there is a brace-initializer, we cannot allocate fewer elements than
630    // there are initializers. If we do, that's treated like an overflow.
631    if (adjustedCount.ult(minElements))
632      hasAnyOverflow = true;
633
634    // Scale numElements by that.  This might overflow, but we don't
635    // care because it only overflows if allocationSize does, too, and
636    // if that overflows then we shouldn't use this.
637    numElements = llvm::ConstantInt::get(CGF.SizeTy,
638                                         adjustedCount * arraySizeMultiplier);
639
640    // Compute the size before cookie, and track whether it overflowed.
641    bool overflow;
642    llvm::APInt allocationSize
643      = adjustedCount.umul_ov(typeSizeMultiplier, overflow);
644    hasAnyOverflow |= overflow;
645
646    // Add in the cookie, and check whether it's overflowed.
647    if (cookieSize != 0) {
648      // Save the current size without a cookie.  This shouldn't be
649      // used if there was overflow.
650      sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
651
652      allocationSize = allocationSize.uadd_ov(cookieSize, overflow);
653      hasAnyOverflow |= overflow;
654    }
655
656    // On overflow, produce a -1 so operator new will fail.
657    if (hasAnyOverflow) {
658      size = llvm::Constant::getAllOnesValue(CGF.SizeTy);
659    } else {
660      size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
661    }
662
663  // Otherwise, we might need to use the overflow intrinsics.
664  } else {
665    // There are up to five conditions we need to test for:
666    // 1) if isSigned, we need to check whether numElements is negative;
667    // 2) if numElementsWidth > sizeWidth, we need to check whether
668    //   numElements is larger than something representable in size_t;
669    // 3) if minElements > 0, we need to check whether numElements is smaller
670    //    than that.
671    // 4) we need to compute
672    //      sizeWithoutCookie := numElements * typeSizeMultiplier
673    //    and check whether it overflows; and
674    // 5) if we need a cookie, we need to compute
675    //      size := sizeWithoutCookie + cookieSize
676    //    and check whether it overflows.
677
678    llvm::Value *hasOverflow = 0;
679
680    // If numElementsWidth > sizeWidth, then one way or another, we're
681    // going to have to do a comparison for (2), and this happens to
682    // take care of (1), too.
683    if (numElementsWidth > sizeWidth) {
684      llvm::APInt threshold(numElementsWidth, 1);
685      threshold <<= sizeWidth;
686
687      llvm::Value *thresholdV
688        = llvm::ConstantInt::get(numElementsType, threshold);
689
690      hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV);
691      numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy);
692
693    // Otherwise, if we're signed, we want to sext up to size_t.
694    } else if (isSigned) {
695      if (numElementsWidth < sizeWidth)
696        numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy);
697
698      // If there's a non-1 type size multiplier, then we can do the
699      // signedness check at the same time as we do the multiply
700      // because a negative number times anything will cause an
701      // unsigned overflow.  Otherwise, we have to do it here. But at least
702      // in this case, we can subsume the >= minElements check.
703      if (typeSizeMultiplier == 1)
704        hasOverflow = CGF.Builder.CreateICmpSLT(numElements,
705                              llvm::ConstantInt::get(CGF.SizeTy, minElements));
706
707    // Otherwise, zext up to size_t if necessary.
708    } else if (numElementsWidth < sizeWidth) {
709      numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy);
710    }
711
712    assert(numElements->getType() == CGF.SizeTy);
713
714    if (minElements) {
715      // Don't allow allocation of fewer elements than we have initializers.
716      if (!hasOverflow) {
717        hasOverflow = CGF.Builder.CreateICmpULT(numElements,
718                              llvm::ConstantInt::get(CGF.SizeTy, minElements));
719      } else if (numElementsWidth > sizeWidth) {
720        // The other existing overflow subsumes this check.
721        // We do an unsigned comparison, since any signed value < -1 is
722        // taken care of either above or below.
723        hasOverflow = CGF.Builder.CreateOr(hasOverflow,
724                          CGF.Builder.CreateICmpULT(numElements,
725                              llvm::ConstantInt::get(CGF.SizeTy, minElements)));
726      }
727    }
728
729    size = numElements;
730
731    // Multiply by the type size if necessary.  This multiplier
732    // includes all the factors for nested arrays.
733    //
734    // This step also causes numElements to be scaled up by the
735    // nested-array factor if necessary.  Overflow on this computation
736    // can be ignored because the result shouldn't be used if
737    // allocation fails.
738    if (typeSizeMultiplier != 1) {
739      llvm::Value *umul_with_overflow
740        = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy);
741
742      llvm::Value *tsmV =
743        llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier);
744      llvm::Value *result =
745        CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV);
746
747      llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
748      if (hasOverflow)
749        hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
750      else
751        hasOverflow = overflowed;
752
753      size = CGF.Builder.CreateExtractValue(result, 0);
754
755      // Also scale up numElements by the array size multiplier.
756      if (arraySizeMultiplier != 1) {
757        // If the base element type size is 1, then we can re-use the
758        // multiply we just did.
759        if (typeSize.isOne()) {
760          assert(arraySizeMultiplier == typeSizeMultiplier);
761          numElements = size;
762
763        // Otherwise we need a separate multiply.
764        } else {
765          llvm::Value *asmV =
766            llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier);
767          numElements = CGF.Builder.CreateMul(numElements, asmV);
768        }
769      }
770    } else {
771      // numElements doesn't need to be scaled.
772      assert(arraySizeMultiplier == 1);
773    }
774
775    // Add in the cookie size if necessary.
776    if (cookieSize != 0) {
777      sizeWithoutCookie = size;
778
779      llvm::Value *uadd_with_overflow
780        = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy);
781
782      llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize);
783      llvm::Value *result =
784        CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV);
785
786      llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
787      if (hasOverflow)
788        hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
789      else
790        hasOverflow = overflowed;
791
792      size = CGF.Builder.CreateExtractValue(result, 0);
793    }
794
795    // If we had any possibility of dynamic overflow, make a select to
796    // overwrite 'size' with an all-ones value, which should cause
797    // operator new to throw.
798    if (hasOverflow)
799      size = CGF.Builder.CreateSelect(hasOverflow,
800                                 llvm::Constant::getAllOnesValue(CGF.SizeTy),
801                                      size);
802  }
803
804  if (cookieSize == 0)
805    sizeWithoutCookie = size;
806  else
807    assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
808
809  return size;
810}
811
812static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init,
813                                    QualType AllocType, llvm::Value *NewPtr) {
814
815  CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType);
816  if (!CGF.hasAggregateLLVMType(AllocType))
817    CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType,
818                                                   Alignment),
819                       false);
820  else if (AllocType->isAnyComplexType())
821    CGF.EmitComplexExprIntoAddr(Init, NewPtr,
822                                AllocType.isVolatileQualified());
823  else {
824    AggValueSlot Slot
825      = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(),
826                              AggValueSlot::IsDestructed,
827                              AggValueSlot::DoesNotNeedGCBarriers,
828                              AggValueSlot::IsNotAliased);
829    CGF.EmitAggExpr(Init, Slot);
830
831    CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init);
832  }
833}
834
835void
836CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
837                                         QualType elementType,
838                                         llvm::Value *beginPtr,
839                                         llvm::Value *numElements) {
840  if (!E->hasInitializer())
841    return; // We have a POD type.
842
843  llvm::Value *explicitPtr = beginPtr;
844  // Find the end of the array, hoisted out of the loop.
845  llvm::Value *endPtr =
846    Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end");
847
848  unsigned initializerElements = 0;
849
850  const Expr *Init = E->getInitializer();
851  llvm::AllocaInst *endOfInit = 0;
852  QualType::DestructionKind dtorKind = elementType.isDestructedType();
853  EHScopeStack::stable_iterator cleanup;
854  llvm::Instruction *cleanupDominator = 0;
855  // If the initializer is an initializer list, first do the explicit elements.
856  if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
857    initializerElements = ILE->getNumInits();
858
859    // Enter a partial-destruction cleanup if necessary.
860    if (needsEHCleanup(dtorKind)) {
861      // In principle we could tell the cleanup where we are more
862      // directly, but the control flow can get so varied here that it
863      // would actually be quite complex.  Therefore we go through an
864      // alloca.
865      endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit");
866      cleanupDominator = Builder.CreateStore(beginPtr, endOfInit);
867      pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType,
868                                       getDestroyer(dtorKind));
869      cleanup = EHStack.stable_begin();
870    }
871
872    for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) {
873      // Tell the cleanup that it needs to destroy up to this
874      // element.  TODO: some of these stores can be trivially
875      // observed to be unnecessary.
876      if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit);
877      StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr);
878      explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next");
879    }
880
881    // The remaining elements are filled with the array filler expression.
882    Init = ILE->getArrayFiller();
883  }
884
885  // Create the continuation block.
886  llvm::BasicBlock *contBB = createBasicBlock("new.loop.end");
887
888  // If the number of elements isn't constant, we have to now check if there is
889  // anything left to initialize.
890  if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) {
891    // If all elements have already been initialized, skip the whole loop.
892    if (constNum->getZExtValue() <= initializerElements) {
893      // If there was a cleanup, deactivate it.
894      if (cleanupDominator)
895        DeactivateCleanupBlock(cleanup, cleanupDominator);
896      return;
897    }
898  } else {
899    llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty");
900    llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr,
901                                                "array.isempty");
902    Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB);
903    EmitBlock(nonEmptyBB);
904  }
905
906  // Enter the loop.
907  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
908  llvm::BasicBlock *loopBB = createBasicBlock("new.loop");
909
910  EmitBlock(loopBB);
911
912  // Set up the current-element phi.
913  llvm::PHINode *curPtr =
914    Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur");
915  curPtr->addIncoming(explicitPtr, entryBB);
916
917  // Store the new cleanup position for irregular cleanups.
918  if (endOfInit) Builder.CreateStore(curPtr, endOfInit);
919
920  // Enter a partial-destruction cleanup if necessary.
921  if (!cleanupDominator && needsEHCleanup(dtorKind)) {
922    pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType,
923                                   getDestroyer(dtorKind));
924    cleanup = EHStack.stable_begin();
925    cleanupDominator = Builder.CreateUnreachable();
926  }
927
928  // Emit the initializer into this element.
929  StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr);
930
931  // Leave the cleanup if we entered one.
932  if (cleanupDominator) {
933    DeactivateCleanupBlock(cleanup, cleanupDominator);
934    cleanupDominator->eraseFromParent();
935  }
936
937  // Advance to the next element.
938  llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next");
939
940  // Check whether we've gotten to the end of the array and, if so,
941  // exit the loop.
942  llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend");
943  Builder.CreateCondBr(isEnd, contBB, loopBB);
944  curPtr->addIncoming(nextPtr, Builder.GetInsertBlock());
945
946  EmitBlock(contBB);
947}
948
949static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
950                           llvm::Value *NewPtr, llvm::Value *Size) {
951  CGF.EmitCastToVoidPtr(NewPtr);
952  CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
953  CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
954                           Alignment.getQuantity(), false);
955}
956
957static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
958                               QualType ElementType,
959                               llvm::Value *NewPtr,
960                               llvm::Value *NumElements,
961                               llvm::Value *AllocSizeWithoutCookie) {
962  const Expr *Init = E->getInitializer();
963  if (E->isArray()) {
964    if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){
965      CXXConstructorDecl *Ctor = CCE->getConstructor();
966      if (Ctor->isTrivial()) {
967        // If new expression did not specify value-initialization, then there
968        // is no initialization.
969        if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty())
970          return;
971
972        if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
973          // Optimization: since zero initialization will just set the memory
974          // to all zeroes, generate a single memset to do it in one shot.
975          EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
976          return;
977        }
978      }
979
980      CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
981                                     CCE->arg_begin(),  CCE->arg_end(),
982                                     CCE->requiresZeroInitialization());
983      return;
984    } else if (Init && isa<ImplicitValueInitExpr>(Init) &&
985               CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
986      // Optimization: since zero initialization will just set the memory
987      // to all zeroes, generate a single memset to do it in one shot.
988      EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
989      return;
990    }
991    CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements);
992    return;
993  }
994
995  if (!Init)
996    return;
997
998  StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr);
999}
1000
1001namespace {
1002  /// A cleanup to call the given 'operator delete' function upon
1003  /// abnormal exit from a new expression.
1004  class CallDeleteDuringNew : public EHScopeStack::Cleanup {
1005    size_t NumPlacementArgs;
1006    const FunctionDecl *OperatorDelete;
1007    llvm::Value *Ptr;
1008    llvm::Value *AllocSize;
1009
1010    RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
1011
1012  public:
1013    static size_t getExtraSize(size_t NumPlacementArgs) {
1014      return NumPlacementArgs * sizeof(RValue);
1015    }
1016
1017    CallDeleteDuringNew(size_t NumPlacementArgs,
1018                        const FunctionDecl *OperatorDelete,
1019                        llvm::Value *Ptr,
1020                        llvm::Value *AllocSize)
1021      : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1022        Ptr(Ptr), AllocSize(AllocSize) {}
1023
1024    void setPlacementArg(unsigned I, RValue Arg) {
1025      assert(I < NumPlacementArgs && "index out of range");
1026      getPlacementArgs()[I] = Arg;
1027    }
1028
1029    void Emit(CodeGenFunction &CGF, Flags flags) {
1030      const FunctionProtoType *FPT
1031        = OperatorDelete->getType()->getAs<FunctionProtoType>();
1032      assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
1033             (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
1034
1035      CallArgList DeleteArgs;
1036
1037      // The first argument is always a void*.
1038      FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
1039      DeleteArgs.add(RValue::get(Ptr), *AI++);
1040
1041      // A member 'operator delete' can take an extra 'size_t' argument.
1042      if (FPT->getNumArgs() == NumPlacementArgs + 2)
1043        DeleteArgs.add(RValue::get(AllocSize), *AI++);
1044
1045      // Pass the rest of the arguments, which must match exactly.
1046      for (unsigned I = 0; I != NumPlacementArgs; ++I)
1047        DeleteArgs.add(getPlacementArgs()[I], *AI++);
1048
1049      // Call 'operator delete'.
1050      CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
1051                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
1052                   ReturnValueSlot(), DeleteArgs, OperatorDelete);
1053    }
1054  };
1055
1056  /// A cleanup to call the given 'operator delete' function upon
1057  /// abnormal exit from a new expression when the new expression is
1058  /// conditional.
1059  class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
1060    size_t NumPlacementArgs;
1061    const FunctionDecl *OperatorDelete;
1062    DominatingValue<RValue>::saved_type Ptr;
1063    DominatingValue<RValue>::saved_type AllocSize;
1064
1065    DominatingValue<RValue>::saved_type *getPlacementArgs() {
1066      return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
1067    }
1068
1069  public:
1070    static size_t getExtraSize(size_t NumPlacementArgs) {
1071      return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
1072    }
1073
1074    CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
1075                                   const FunctionDecl *OperatorDelete,
1076                                   DominatingValue<RValue>::saved_type Ptr,
1077                              DominatingValue<RValue>::saved_type AllocSize)
1078      : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1079        Ptr(Ptr), AllocSize(AllocSize) {}
1080
1081    void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
1082      assert(I < NumPlacementArgs && "index out of range");
1083      getPlacementArgs()[I] = Arg;
1084    }
1085
1086    void Emit(CodeGenFunction &CGF, Flags flags) {
1087      const FunctionProtoType *FPT
1088        = OperatorDelete->getType()->getAs<FunctionProtoType>();
1089      assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
1090             (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
1091
1092      CallArgList DeleteArgs;
1093
1094      // The first argument is always a void*.
1095      FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
1096      DeleteArgs.add(Ptr.restore(CGF), *AI++);
1097
1098      // A member 'operator delete' can take an extra 'size_t' argument.
1099      if (FPT->getNumArgs() == NumPlacementArgs + 2) {
1100        RValue RV = AllocSize.restore(CGF);
1101        DeleteArgs.add(RV, *AI++);
1102      }
1103
1104      // Pass the rest of the arguments, which must match exactly.
1105      for (unsigned I = 0; I != NumPlacementArgs; ++I) {
1106        RValue RV = getPlacementArgs()[I].restore(CGF);
1107        DeleteArgs.add(RV, *AI++);
1108      }
1109
1110      // Call 'operator delete'.
1111      CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
1112                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
1113                   ReturnValueSlot(), DeleteArgs, OperatorDelete);
1114    }
1115  };
1116}
1117
1118/// Enter a cleanup to call 'operator delete' if the initializer in a
1119/// new-expression throws.
1120static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
1121                                  const CXXNewExpr *E,
1122                                  llvm::Value *NewPtr,
1123                                  llvm::Value *AllocSize,
1124                                  const CallArgList &NewArgs) {
1125  // If we're not inside a conditional branch, then the cleanup will
1126  // dominate and we can do the easier (and more efficient) thing.
1127  if (!CGF.isInConditionalBranch()) {
1128    CallDeleteDuringNew *Cleanup = CGF.EHStack
1129      .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
1130                                                 E->getNumPlacementArgs(),
1131                                                 E->getOperatorDelete(),
1132                                                 NewPtr, AllocSize);
1133    for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1134      Cleanup->setPlacementArg(I, NewArgs[I+1].RV);
1135
1136    return;
1137  }
1138
1139  // Otherwise, we need to save all this stuff.
1140  DominatingValue<RValue>::saved_type SavedNewPtr =
1141    DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
1142  DominatingValue<RValue>::saved_type SavedAllocSize =
1143    DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
1144
1145  CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
1146    .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup,
1147                                                 E->getNumPlacementArgs(),
1148                                                 E->getOperatorDelete(),
1149                                                 SavedNewPtr,
1150                                                 SavedAllocSize);
1151  for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1152    Cleanup->setPlacementArg(I,
1153                     DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV));
1154
1155  CGF.initFullExprCleanup();
1156}
1157
1158llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
1159  // The element type being allocated.
1160  QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
1161
1162  // 1. Build a call to the allocation function.
1163  FunctionDecl *allocator = E->getOperatorNew();
1164  const FunctionProtoType *allocatorType =
1165    allocator->getType()->castAs<FunctionProtoType>();
1166
1167  CallArgList allocatorArgs;
1168
1169  // The allocation size is the first argument.
1170  QualType sizeType = getContext().getSizeType();
1171
1172  // If there is a brace-initializer, cannot allocate fewer elements than inits.
1173  unsigned minElements = 0;
1174  if (E->isArray() && E->hasInitializer()) {
1175    if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()))
1176      minElements = ILE->getNumInits();
1177  }
1178
1179  llvm::Value *numElements = 0;
1180  llvm::Value *allocSizeWithoutCookie = 0;
1181  llvm::Value *allocSize =
1182    EmitCXXNewAllocSize(*this, E, minElements, numElements,
1183                        allocSizeWithoutCookie);
1184
1185  allocatorArgs.add(RValue::get(allocSize), sizeType);
1186
1187  // Emit the rest of the arguments.
1188  // FIXME: Ideally, this should just use EmitCallArgs.
1189  CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
1190
1191  // First, use the types from the function type.
1192  // We start at 1 here because the first argument (the allocation size)
1193  // has already been emitted.
1194  for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
1195       ++i, ++placementArg) {
1196    QualType argType = allocatorType->getArgType(i);
1197
1198    assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
1199                                               placementArg->getType()) &&
1200           "type mismatch in call argument!");
1201
1202    EmitCallArg(allocatorArgs, *placementArg, argType);
1203  }
1204
1205  // Either we've emitted all the call args, or we have a call to a
1206  // variadic function.
1207  assert((placementArg == E->placement_arg_end() ||
1208          allocatorType->isVariadic()) &&
1209         "Extra arguments to non-variadic function!");
1210
1211  // If we still have any arguments, emit them using the type of the argument.
1212  for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
1213       placementArg != placementArgsEnd; ++placementArg) {
1214    EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
1215  }
1216
1217  // Emit the allocation call.  If the allocator is a global placement
1218  // operator, just "inline" it directly.
1219  RValue RV;
1220  if (allocator->isReservedGlobalPlacementOperator()) {
1221    assert(allocatorArgs.size() == 2);
1222    RV = allocatorArgs[1].RV;
1223    // TODO: kill any unnecessary computations done for the size
1224    // argument.
1225  } else {
1226    RV = EmitCall(CGM.getTypes().arrangeFreeFunctionCall(allocatorArgs,
1227                                                         allocatorType),
1228                  CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
1229                  allocatorArgs, allocator);
1230  }
1231
1232  // Emit a null check on the allocation result if the allocation
1233  // function is allowed to return null (because it has a non-throwing
1234  // exception spec; for this part, we inline
1235  // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1236  // interesting initializer.
1237  bool nullCheck = allocatorType->isNothrow(getContext()) &&
1238    (!allocType.isPODType(getContext()) || E->hasInitializer());
1239
1240  llvm::BasicBlock *nullCheckBB = 0;
1241  llvm::BasicBlock *contBB = 0;
1242
1243  llvm::Value *allocation = RV.getScalarVal();
1244  unsigned AS = allocation->getType()->getPointerAddressSpace();
1245
1246  // The null-check means that the initializer is conditionally
1247  // evaluated.
1248  ConditionalEvaluation conditional(*this);
1249
1250  if (nullCheck) {
1251    conditional.begin(*this);
1252
1253    nullCheckBB = Builder.GetInsertBlock();
1254    llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1255    contBB = createBasicBlock("new.cont");
1256
1257    llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1258    Builder.CreateCondBr(isNull, contBB, notNullBB);
1259    EmitBlock(notNullBB);
1260  }
1261
1262  // If there's an operator delete, enter a cleanup to call it if an
1263  // exception is thrown.
1264  EHScopeStack::stable_iterator operatorDeleteCleanup;
1265  llvm::Instruction *cleanupDominator = 0;
1266  if (E->getOperatorDelete() &&
1267      !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) {
1268    EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1269    operatorDeleteCleanup = EHStack.stable_begin();
1270    cleanupDominator = Builder.CreateUnreachable();
1271  }
1272
1273  assert((allocSize == allocSizeWithoutCookie) ==
1274         CalculateCookiePadding(*this, E).isZero());
1275  if (allocSize != allocSizeWithoutCookie) {
1276    assert(E->isArray());
1277    allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1278                                                       numElements,
1279                                                       E, allocType);
1280  }
1281
1282  llvm::Type *elementPtrTy
1283    = ConvertTypeForMem(allocType)->getPointerTo(AS);
1284  llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
1285
1286  EmitNewInitializer(*this, E, allocType, result, numElements,
1287                     allocSizeWithoutCookie);
1288  if (E->isArray()) {
1289    // NewPtr is a pointer to the base element type.  If we're
1290    // allocating an array of arrays, we'll need to cast back to the
1291    // array pointer type.
1292    llvm::Type *resultType = ConvertTypeForMem(E->getType());
1293    if (result->getType() != resultType)
1294      result = Builder.CreateBitCast(result, resultType);
1295  }
1296
1297  // Deactivate the 'operator delete' cleanup if we finished
1298  // initialization.
1299  if (operatorDeleteCleanup.isValid()) {
1300    DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator);
1301    cleanupDominator->eraseFromParent();
1302  }
1303
1304  if (nullCheck) {
1305    conditional.end(*this);
1306
1307    llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1308    EmitBlock(contBB);
1309
1310    llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
1311    PHI->addIncoming(result, notNullBB);
1312    PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1313                     nullCheckBB);
1314
1315    result = PHI;
1316  }
1317
1318  return result;
1319}
1320
1321void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1322                                     llvm::Value *Ptr,
1323                                     QualType DeleteTy) {
1324  assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1325
1326  const FunctionProtoType *DeleteFTy =
1327    DeleteFD->getType()->getAs<FunctionProtoType>();
1328
1329  CallArgList DeleteArgs;
1330
1331  // Check if we need to pass the size to the delete operator.
1332  llvm::Value *Size = 0;
1333  QualType SizeTy;
1334  if (DeleteFTy->getNumArgs() == 2) {
1335    SizeTy = DeleteFTy->getArgType(1);
1336    CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1337    Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1338                                  DeleteTypeSize.getQuantity());
1339  }
1340
1341  QualType ArgTy = DeleteFTy->getArgType(0);
1342  llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1343  DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
1344
1345  if (Size)
1346    DeleteArgs.add(RValue::get(Size), SizeTy);
1347
1348  // Emit the call to delete.
1349  EmitCall(CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, DeleteFTy),
1350           CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
1351           DeleteArgs, DeleteFD);
1352}
1353
1354namespace {
1355  /// Calls the given 'operator delete' on a single object.
1356  struct CallObjectDelete : EHScopeStack::Cleanup {
1357    llvm::Value *Ptr;
1358    const FunctionDecl *OperatorDelete;
1359    QualType ElementType;
1360
1361    CallObjectDelete(llvm::Value *Ptr,
1362                     const FunctionDecl *OperatorDelete,
1363                     QualType ElementType)
1364      : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1365
1366    void Emit(CodeGenFunction &CGF, Flags flags) {
1367      CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1368    }
1369  };
1370}
1371
1372/// Emit the code for deleting a single object.
1373static void EmitObjectDelete(CodeGenFunction &CGF,
1374                             const FunctionDecl *OperatorDelete,
1375                             llvm::Value *Ptr,
1376                             QualType ElementType,
1377                             bool UseGlobalDelete) {
1378  // Find the destructor for the type, if applicable.  If the
1379  // destructor is virtual, we'll just emit the vcall and return.
1380  const CXXDestructorDecl *Dtor = 0;
1381  if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1382    CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1383    if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
1384      Dtor = RD->getDestructor();
1385
1386      if (Dtor->isVirtual()) {
1387        if (UseGlobalDelete) {
1388          // If we're supposed to call the global delete, make sure we do so
1389          // even if the destructor throws.
1390
1391          // Derive the complete-object pointer, which is what we need
1392          // to pass to the deallocation function.
1393          llvm::Value *completePtr =
1394            CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType);
1395
1396          CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1397                                                    completePtr, OperatorDelete,
1398                                                    ElementType);
1399        }
1400
1401        llvm::Type *Ty =
1402          CGF.getTypes().GetFunctionType(
1403                         CGF.getTypes().arrangeCXXDestructor(Dtor, Dtor_Complete));
1404
1405        llvm::Value *Callee
1406          = CGF.BuildVirtualCall(Dtor,
1407                                 UseGlobalDelete? Dtor_Complete : Dtor_Deleting,
1408                                 Ptr, Ty);
1409        // FIXME: Provide a source location here.
1410        CGF.EmitCXXMemberCall(Dtor, SourceLocation(), Callee, ReturnValueSlot(),
1411                              Ptr, /*VTT=*/0, 0, 0);
1412
1413        if (UseGlobalDelete) {
1414          CGF.PopCleanupBlock();
1415        }
1416
1417        return;
1418      }
1419    }
1420  }
1421
1422  // Make sure that we call delete even if the dtor throws.
1423  // This doesn't have to a conditional cleanup because we're going
1424  // to pop it off in a second.
1425  CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1426                                            Ptr, OperatorDelete, ElementType);
1427
1428  if (Dtor)
1429    CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1430                              /*ForVirtualBase=*/false,
1431                              /*Delegating=*/false,
1432                              Ptr);
1433  else if (CGF.getLangOpts().ObjCAutoRefCount &&
1434           ElementType->isObjCLifetimeType()) {
1435    switch (ElementType.getObjCLifetime()) {
1436    case Qualifiers::OCL_None:
1437    case Qualifiers::OCL_ExplicitNone:
1438    case Qualifiers::OCL_Autoreleasing:
1439      break;
1440
1441    case Qualifiers::OCL_Strong: {
1442      // Load the pointer value.
1443      llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr,
1444                                             ElementType.isVolatileQualified());
1445
1446      CGF.EmitARCRelease(PtrValue, /*precise*/ true);
1447      break;
1448    }
1449
1450    case Qualifiers::OCL_Weak:
1451      CGF.EmitARCDestroyWeak(Ptr);
1452      break;
1453    }
1454  }
1455
1456  CGF.PopCleanupBlock();
1457}
1458
1459namespace {
1460  /// Calls the given 'operator delete' on an array of objects.
1461  struct CallArrayDelete : EHScopeStack::Cleanup {
1462    llvm::Value *Ptr;
1463    const FunctionDecl *OperatorDelete;
1464    llvm::Value *NumElements;
1465    QualType ElementType;
1466    CharUnits CookieSize;
1467
1468    CallArrayDelete(llvm::Value *Ptr,
1469                    const FunctionDecl *OperatorDelete,
1470                    llvm::Value *NumElements,
1471                    QualType ElementType,
1472                    CharUnits CookieSize)
1473      : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1474        ElementType(ElementType), CookieSize(CookieSize) {}
1475
1476    void Emit(CodeGenFunction &CGF, Flags flags) {
1477      const FunctionProtoType *DeleteFTy =
1478        OperatorDelete->getType()->getAs<FunctionProtoType>();
1479      assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1480
1481      CallArgList Args;
1482
1483      // Pass the pointer as the first argument.
1484      QualType VoidPtrTy = DeleteFTy->getArgType(0);
1485      llvm::Value *DeletePtr
1486        = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1487      Args.add(RValue::get(DeletePtr), VoidPtrTy);
1488
1489      // Pass the original requested size as the second argument.
1490      if (DeleteFTy->getNumArgs() == 2) {
1491        QualType size_t = DeleteFTy->getArgType(1);
1492        llvm::IntegerType *SizeTy
1493          = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1494
1495        CharUnits ElementTypeSize =
1496          CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1497
1498        // The size of an element, multiplied by the number of elements.
1499        llvm::Value *Size
1500          = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1501        Size = CGF.Builder.CreateMul(Size, NumElements);
1502
1503        // Plus the size of the cookie if applicable.
1504        if (!CookieSize.isZero()) {
1505          llvm::Value *CookieSizeV
1506            = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1507          Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1508        }
1509
1510        Args.add(RValue::get(Size), size_t);
1511      }
1512
1513      // Emit the call to delete.
1514      CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Args, DeleteFTy),
1515                   CGF.CGM.GetAddrOfFunction(OperatorDelete),
1516                   ReturnValueSlot(), Args, OperatorDelete);
1517    }
1518  };
1519}
1520
1521/// Emit the code for deleting an array of objects.
1522static void EmitArrayDelete(CodeGenFunction &CGF,
1523                            const CXXDeleteExpr *E,
1524                            llvm::Value *deletedPtr,
1525                            QualType elementType) {
1526  llvm::Value *numElements = 0;
1527  llvm::Value *allocatedPtr = 0;
1528  CharUnits cookieSize;
1529  CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType,
1530                                      numElements, allocatedPtr, cookieSize);
1531
1532  assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer");
1533
1534  // Make sure that we call delete even if one of the dtors throws.
1535  const FunctionDecl *operatorDelete = E->getOperatorDelete();
1536  CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1537                                           allocatedPtr, operatorDelete,
1538                                           numElements, elementType,
1539                                           cookieSize);
1540
1541  // Destroy the elements.
1542  if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) {
1543    assert(numElements && "no element count for a type with a destructor!");
1544
1545    llvm::Value *arrayEnd =
1546      CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end");
1547
1548    // Note that it is legal to allocate a zero-length array, and we
1549    // can never fold the check away because the length should always
1550    // come from a cookie.
1551    CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType,
1552                         CGF.getDestroyer(dtorKind),
1553                         /*checkZeroLength*/ true,
1554                         CGF.needsEHCleanup(dtorKind));
1555  }
1556
1557  // Pop the cleanup block.
1558  CGF.PopCleanupBlock();
1559}
1560
1561void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
1562  const Expr *Arg = E->getArgument();
1563  llvm::Value *Ptr = EmitScalarExpr(Arg);
1564
1565  // Null check the pointer.
1566  llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1567  llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1568
1569  llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
1570
1571  Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1572  EmitBlock(DeleteNotNull);
1573
1574  // We might be deleting a pointer to array.  If so, GEP down to the
1575  // first non-array element.
1576  // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1577  QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1578  if (DeleteTy->isConstantArrayType()) {
1579    llvm::Value *Zero = Builder.getInt32(0);
1580    SmallVector<llvm::Value*,8> GEP;
1581
1582    GEP.push_back(Zero); // point at the outermost array
1583
1584    // For each layer of array type we're pointing at:
1585    while (const ConstantArrayType *Arr
1586             = getContext().getAsConstantArrayType(DeleteTy)) {
1587      // 1. Unpeel the array type.
1588      DeleteTy = Arr->getElementType();
1589
1590      // 2. GEP to the first element of the array.
1591      GEP.push_back(Zero);
1592    }
1593
1594    Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first");
1595  }
1596
1597  assert(ConvertTypeForMem(DeleteTy) ==
1598         cast<llvm::PointerType>(Ptr->getType())->getElementType());
1599
1600  if (E->isArrayForm()) {
1601    EmitArrayDelete(*this, E, Ptr, DeleteTy);
1602  } else {
1603    EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy,
1604                     E->isGlobalDelete());
1605  }
1606
1607  EmitBlock(DeleteEnd);
1608}
1609
1610static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1611  // void __cxa_bad_typeid();
1612  llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1613
1614  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1615}
1616
1617static void EmitBadTypeidCall(CodeGenFunction &CGF) {
1618  llvm::Value *Fn = getBadTypeidFn(CGF);
1619  CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
1620  CGF.Builder.CreateUnreachable();
1621}
1622
1623static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
1624                                         const Expr *E,
1625                                         llvm::Type *StdTypeInfoPtrTy) {
1626  // Get the vtable pointer.
1627  llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1628
1629  // C++ [expr.typeid]p2:
1630  //   If the glvalue expression is obtained by applying the unary * operator to
1631  //   a pointer and the pointer is a null pointer value, the typeid expression
1632  //   throws the std::bad_typeid exception.
1633  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
1634    if (UO->getOpcode() == UO_Deref) {
1635      llvm::BasicBlock *BadTypeidBlock =
1636        CGF.createBasicBlock("typeid.bad_typeid");
1637      llvm::BasicBlock *EndBlock =
1638        CGF.createBasicBlock("typeid.end");
1639
1640      llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1641      CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1642
1643      CGF.EmitBlock(BadTypeidBlock);
1644      EmitBadTypeidCall(CGF);
1645      CGF.EmitBlock(EndBlock);
1646    }
1647  }
1648
1649  llvm::Value *Value = CGF.GetVTablePtr(ThisPtr,
1650                                        StdTypeInfoPtrTy->getPointerTo());
1651
1652  // Load the type info.
1653  Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1654  return CGF.Builder.CreateLoad(Value);
1655}
1656
1657llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1658  llvm::Type *StdTypeInfoPtrTy =
1659    ConvertType(E->getType())->getPointerTo();
1660
1661  if (E->isTypeOperand()) {
1662    llvm::Constant *TypeInfo =
1663      CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1664    return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
1665  }
1666
1667  // C++ [expr.typeid]p2:
1668  //   When typeid is applied to a glvalue expression whose type is a
1669  //   polymorphic class type, the result refers to a std::type_info object
1670  //   representing the type of the most derived object (that is, the dynamic
1671  //   type) to which the glvalue refers.
1672  if (E->isPotentiallyEvaluated())
1673    return EmitTypeidFromVTable(*this, E->getExprOperand(),
1674                                StdTypeInfoPtrTy);
1675
1676  QualType OperandTy = E->getExprOperand()->getType();
1677  return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1678                               StdTypeInfoPtrTy);
1679}
1680
1681static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) {
1682  // void *__dynamic_cast(const void *sub,
1683  //                      const abi::__class_type_info *src,
1684  //                      const abi::__class_type_info *dst,
1685  //                      std::ptrdiff_t src2dst_offset);
1686
1687  llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1688  llvm::Type *PtrDiffTy =
1689    CGF.ConvertType(CGF.getContext().getPointerDiffType());
1690
1691  llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1692
1693  llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1694
1695  // Mark the function as nounwind readonly.
1696  llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1697                                            llvm::Attribute::ReadOnly };
1698  llvm::AttributeSet Attrs = llvm::AttributeSet::get(
1699      CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs);
1700
1701  return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1702}
1703
1704static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1705  // void __cxa_bad_cast();
1706  llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1707  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1708}
1709
1710static void EmitBadCastCall(CodeGenFunction &CGF) {
1711  llvm::Value *Fn = getBadCastFn(CGF);
1712  CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
1713  CGF.Builder.CreateUnreachable();
1714}
1715
1716/// \brief Compute the src2dst_offset hint as described in the
1717/// Itanium C++ ABI [2.9.7]
1718static CharUnits computeOffsetHint(ASTContext &Context,
1719                                   const CXXRecordDecl *Src,
1720                                   const CXXRecordDecl *Dst) {
1721  CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1722                     /*DetectVirtual=*/false);
1723
1724  // If Dst is not derived from Src we can skip the whole computation below and
1725  // return that Src is not a public base of Dst.  Record all inheritance paths.
1726  if (!Dst->isDerivedFrom(Src, Paths))
1727    return CharUnits::fromQuantity(-2ULL);
1728
1729  unsigned NumPublicPaths = 0;
1730  CharUnits Offset;
1731
1732  // Now walk all possible inheritance paths.
1733  for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end();
1734       I != E; ++I) {
1735    if (I->Access != AS_public) // Ignore non-public inheritance.
1736      continue;
1737
1738    ++NumPublicPaths;
1739
1740    for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) {
1741      // If the path contains a virtual base class we can't give any hint.
1742      // -1: no hint.
1743      if (J->Base->isVirtual())
1744        return CharUnits::fromQuantity(-1ULL);
1745
1746      if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1747        continue;
1748
1749      // Accumulate the base class offsets.
1750      const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class);
1751      Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
1752    }
1753  }
1754
1755  // -2: Src is not a public base of Dst.
1756  if (NumPublicPaths == 0)
1757    return CharUnits::fromQuantity(-2ULL);
1758
1759  // -3: Src is a multiple public base type but never a virtual base type.
1760  if (NumPublicPaths > 1)
1761    return CharUnits::fromQuantity(-3ULL);
1762
1763  // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1764  // Return the offset of Src from the origin of Dst.
1765  return Offset;
1766}
1767
1768static llvm::Value *
1769EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
1770                    QualType SrcTy, QualType DestTy,
1771                    llvm::BasicBlock *CastEnd) {
1772  llvm::Type *PtrDiffLTy =
1773    CGF.ConvertType(CGF.getContext().getPointerDiffType());
1774  llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1775
1776  if (const PointerType *PTy = DestTy->getAs<PointerType>()) {
1777    if (PTy->getPointeeType()->isVoidType()) {
1778      // C++ [expr.dynamic.cast]p7:
1779      //   If T is "pointer to cv void," then the result is a pointer to the
1780      //   most derived object pointed to by v.
1781
1782      // Get the vtable pointer.
1783      llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1784
1785      // Get the offset-to-top from the vtable.
1786      llvm::Value *OffsetToTop =
1787        CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1788      OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1789
1790      // Finally, add the offset to the pointer.
1791      Value = CGF.EmitCastToVoidPtr(Value);
1792      Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1793
1794      return CGF.Builder.CreateBitCast(Value, DestLTy);
1795    }
1796  }
1797
1798  QualType SrcRecordTy;
1799  QualType DestRecordTy;
1800
1801  if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) {
1802    SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1803    DestRecordTy = DestPTy->getPointeeType();
1804  } else {
1805    SrcRecordTy = SrcTy;
1806    DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1807  }
1808
1809  assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1810  assert(DestRecordTy->isRecordType() && "dest type must be a record type!");
1811
1812  llvm::Value *SrcRTTI =
1813    CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1814  llvm::Value *DestRTTI =
1815    CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1816
1817  // Compute the offset hint.
1818  const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1819  const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1820  llvm::Value *OffsetHint =
1821    llvm::ConstantInt::get(PtrDiffLTy,
1822                           computeOffsetHint(CGF.getContext(), SrcDecl,
1823                                             DestDecl).getQuantity());
1824
1825  // Emit the call to __dynamic_cast.
1826  Value = CGF.EmitCastToVoidPtr(Value);
1827  Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value,
1828                                  SrcRTTI, DestRTTI, OffsetHint);
1829  Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1830
1831  /// C++ [expr.dynamic.cast]p9:
1832  ///   A failed cast to reference type throws std::bad_cast
1833  if (DestTy->isReferenceType()) {
1834    llvm::BasicBlock *BadCastBlock =
1835      CGF.createBasicBlock("dynamic_cast.bad_cast");
1836
1837    llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1838    CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1839
1840    CGF.EmitBlock(BadCastBlock);
1841    EmitBadCastCall(CGF);
1842  }
1843
1844  return Value;
1845}
1846
1847static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1848                                          QualType DestTy) {
1849  llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1850  if (DestTy->isPointerType())
1851    return llvm::Constant::getNullValue(DestLTy);
1852
1853  /// C++ [expr.dynamic.cast]p9:
1854  ///   A failed cast to reference type throws std::bad_cast
1855  EmitBadCastCall(CGF);
1856
1857  CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1858  return llvm::UndefValue::get(DestLTy);
1859}
1860
1861llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
1862                                              const CXXDynamicCastExpr *DCE) {
1863  QualType DestTy = DCE->getTypeAsWritten();
1864
1865  if (DCE->isAlwaysNull())
1866    return EmitDynamicCastToNull(*this, DestTy);
1867
1868  QualType SrcTy = DCE->getSubExpr()->getType();
1869
1870  // C++ [expr.dynamic.cast]p4:
1871  //   If the value of v is a null pointer value in the pointer case, the result
1872  //   is the null pointer value of type T.
1873  bool ShouldNullCheckSrcValue = SrcTy->isPointerType();
1874
1875  llvm::BasicBlock *CastNull = 0;
1876  llvm::BasicBlock *CastNotNull = 0;
1877  llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
1878
1879  if (ShouldNullCheckSrcValue) {
1880    CastNull = createBasicBlock("dynamic_cast.null");
1881    CastNotNull = createBasicBlock("dynamic_cast.notnull");
1882
1883    llvm::Value *IsNull = Builder.CreateIsNull(Value);
1884    Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1885    EmitBlock(CastNotNull);
1886  }
1887
1888  Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd);
1889
1890  if (ShouldNullCheckSrcValue) {
1891    EmitBranch(CastEnd);
1892
1893    EmitBlock(CastNull);
1894    EmitBranch(CastEnd);
1895  }
1896
1897  EmitBlock(CastEnd);
1898
1899  if (ShouldNullCheckSrcValue) {
1900    llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1901    PHI->addIncoming(Value, CastNotNull);
1902    PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1903
1904    Value = PHI;
1905  }
1906
1907  return Value;
1908}
1909
1910void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) {
1911  RunCleanupsScope Scope(*this);
1912  LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(),
1913                                 Slot.getAlignment());
1914
1915  CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
1916  for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(),
1917                                         e = E->capture_init_end();
1918       i != e; ++i, ++CurField) {
1919    // Emit initialization
1920
1921    LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
1922    ArrayRef<VarDecl *> ArrayIndexes;
1923    if (CurField->getType()->isArrayType())
1924      ArrayIndexes = E->getCaptureInitIndexVars(i);
1925    EmitInitializerForField(*CurField, LV, *i, ArrayIndexes);
1926  }
1927}
1928