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