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