1 //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with code generation of C++ expressions
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/CodeGenOptions.h"
15 #include "CodeGenFunction.h"
16 #include "CGCUDARuntime.h"
18 #include "CGObjCRuntime.h"
19 #include "CGDebugInfo.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Support/CallSite.h"
23 using namespace clang;
24 using namespace CodeGen;
26 RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
27 SourceLocation CallLoc,
29 ReturnValueSlot ReturnValue,
32 CallExpr::const_arg_iterator ArgBeg,
33 CallExpr::const_arg_iterator ArgEnd) {
34 assert(MD->isInstance() &&
35 "Trying to emit a member call expr on a static method!");
37 // C++11 [class.mfct.non-static]p2:
38 // If a non-static member function of a class X is called for an object that
39 // is not of type X, or of a type derived from X, the behavior is undefined.
40 EmitTypeCheck(isa<CXXConstructorDecl>(MD) ? TCK_ConstructorCall
42 CallLoc, This, getContext().getRecordType(MD->getParent()));
47 Args.add(RValue::get(This), MD->getThisType(getContext()));
49 // If there is a VTT parameter, emit it.
51 QualType T = getContext().getPointerType(getContext().VoidPtrTy);
52 Args.add(RValue::get(VTT), T);
55 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
56 RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size());
58 // And the rest of the call args.
59 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
61 return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required),
62 Callee, ReturnValue, Args, MD);
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) {
69 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
74 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
75 if (CE->getCastKind() == CK_NoOp) {
80 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
81 if (UO->getOpcode() == UO_Extension) {
90 /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
91 /// expr can be devirtualized.
92 static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
94 const CXXMethodDecl *MD) {
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)
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:
104 // struct A { virtual void f(); }
105 // struct B final : A { };
111 const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
112 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
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>())
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>())
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();
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();
142 // We can always devirtualize calls on temporary object expressions.
143 if (isa<CXXConstructExpr>(Base))
146 // And calls on bound temporaries.
147 if (isa<CXXBindTemporaryExpr>(Base))
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();
154 // We can't devirtualize the call.
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());
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();
172 if (isa<BinaryOperator>(callee))
173 return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
175 const MemberExpr *ME = cast<MemberExpr>(callee);
176 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
178 CGDebugInfo *DI = getDebugInfo();
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());
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());
196 // Compute the object pointer.
197 const Expr *Base = ME->getBase();
198 bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier();
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.
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;
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;
232 This = EmitScalarExpr(Base);
234 This = EmitLValue(Base).getAddress();
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);
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);
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);
259 llvm_unreachable("unknown trivial member function");
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,
268 else if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl))
269 FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor,
272 FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl);
274 llvm::Type *Ty = CGM.getTypes().GetFunctionType(*FInfo);
276 // C++ [class.virtual]p12:
277 // Explicit qualification with the scope operator (5.1) suppresses the
278 // virtual call mechanism.
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;
285 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
286 if (UseVirtualCall) {
287 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
289 if (getLangOpts().AppleKext &&
292 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
293 else if (!DevirtualizedMethod)
294 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
296 const CXXDestructorDecl *DDtor =
297 cast<CXXDestructorDecl>(DevirtualizedMethod);
298 Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty);
301 } else if (const CXXConstructorDecl *Ctor =
302 dyn_cast<CXXConstructorDecl>(MD)) {
303 Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
304 } else if (UseVirtualCall) {
305 Callee = BuildVirtualCall(MD, This, Ty);
307 if (getLangOpts().AppleKext &&
310 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
311 else if (!DevirtualizedMethod)
312 Callee = CGM.GetAddrOfFunction(MD, Ty);
314 Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty);
318 return EmitCXXMemberCall(MD, CE->getExprLoc(), Callee, ReturnValue, This,
319 /*VTT=*/0, CE->arg_begin(), CE->arg_end());
323 CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
324 ReturnValueSlot ReturnValue) {
325 const BinaryOperator *BO =
326 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
327 const Expr *BaseExpr = BO->getLHS();
328 const Expr *MemFnExpr = BO->getRHS();
330 const MemberPointerType *MPT =
331 MemFnExpr->getType()->castAs<MemberPointerType>();
333 const FunctionProtoType *FPT =
334 MPT->getPointeeType()->castAs<FunctionProtoType>();
335 const CXXRecordDecl *RD =
336 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
338 // Get the member function pointer.
339 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
341 // Emit the 'this' pointer.
344 if (BO->getOpcode() == BO_PtrMemI)
345 This = EmitScalarExpr(BaseExpr);
347 This = EmitLValue(BaseExpr).getAddress();
349 EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This,
350 QualType(MPT->getClass(), 0));
352 // Ask the ABI to load the callee. Note that This is modified.
353 llvm::Value *Callee =
354 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
359 getContext().getPointerType(getContext().getTagDeclType(RD));
361 // Push the this ptr.
362 Args.add(RValue::get(This), ThisType);
364 RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1);
366 // And the rest of the call args
367 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
368 return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), Callee,
373 CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
374 const CXXMethodDecl *MD,
375 ReturnValueSlot ReturnValue) {
376 assert(MD->isInstance() &&
377 "Trying to emit a member call expr on a static method!");
378 LValue LV = EmitLValue(E->getArg(0));
379 llvm::Value *This = LV.getAddress();
381 if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) &&
383 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
384 QualType Ty = E->getType();
385 EmitAggregateAssign(This, Src, Ty);
386 return RValue::get(This);
389 llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This);
390 return EmitCXXMemberCall(MD, E->getExprLoc(), Callee, ReturnValue, This,
391 /*VTT=*/0, E->arg_begin() + 1, E->arg_end());
394 RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
395 ReturnValueSlot ReturnValue) {
396 return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue);
399 static void EmitNullBaseClassInitialization(CodeGenFunction &CGF,
400 llvm::Value *DestPtr,
401 const CXXRecordDecl *Base) {
405 DestPtr = CGF.EmitCastToVoidPtr(DestPtr);
407 const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base);
408 CharUnits Size = Layout.getNonVirtualSize();
409 CharUnits Align = Layout.getNonVirtualAlign();
411 llvm::Value *SizeVal = CGF.CGM.getSize(Size);
413 // If the type contains a pointer to data member we can't memset it to zero.
414 // Instead, create a null constant and copy it to the destination.
415 // TODO: there are other patterns besides zero that we can usefully memset,
416 // like -1, which happens to be the pattern used by member-pointers.
417 // TODO: isZeroInitializable can be over-conservative in the case where a
418 // virtual base contains a member pointer.
419 if (!CGF.CGM.getTypes().isZeroInitializable(Base)) {
420 llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base);
422 llvm::GlobalVariable *NullVariable =
423 new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(),
425 llvm::GlobalVariable::PrivateLinkage,
426 NullConstant, Twine());
427 NullVariable->setAlignment(Align.getQuantity());
428 llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable);
430 // Get and call the appropriate llvm.memcpy overload.
431 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity());
435 // Otherwise, just memset the whole thing to zero. This is legal
436 // because in LLVM, all default initializers (other than the ones we just
437 // handled above) are guaranteed to have a bit pattern of all zeros.
438 CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal,
439 Align.getQuantity());
443 CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
445 assert(!Dest.isIgnored() && "Must have a destination!");
446 const CXXConstructorDecl *CD = E->getConstructor();
448 // If we require zero initialization before (or instead of) calling the
449 // constructor, as can be the case with a non-user-provided default
450 // constructor, emit the zero initialization now, unless destination is
452 if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
453 switch (E->getConstructionKind()) {
454 case CXXConstructExpr::CK_Delegating:
455 case CXXConstructExpr::CK_Complete:
456 EmitNullInitialization(Dest.getAddr(), E->getType());
458 case CXXConstructExpr::CK_VirtualBase:
459 case CXXConstructExpr::CK_NonVirtualBase:
460 EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent());
465 // If this is a call to a trivial default constructor, do nothing.
466 if (CD->isTrivial() && CD->isDefaultConstructor())
469 // Elide the constructor if we're constructing from a temporary.
470 // The temporary check is required because Sema sets this on NRVO
472 if (getLangOpts().ElideConstructors && E->isElidable()) {
473 assert(getContext().hasSameUnqualifiedType(E->getType(),
474 E->getArg(0)->getType()));
475 if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
476 EmitAggExpr(E->getArg(0), Dest);
481 if (const ConstantArrayType *arrayType
482 = getContext().getAsConstantArrayType(E->getType())) {
483 EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(),
484 E->arg_begin(), E->arg_end());
486 CXXCtorType Type = Ctor_Complete;
487 bool ForVirtualBase = false;
489 switch (E->getConstructionKind()) {
490 case CXXConstructExpr::CK_Delegating:
491 // We should be emitting a constructor; GlobalDecl will assert this
492 Type = CurGD.getCtorType();
495 case CXXConstructExpr::CK_Complete:
496 Type = Ctor_Complete;
499 case CXXConstructExpr::CK_VirtualBase:
500 ForVirtualBase = true;
503 case CXXConstructExpr::CK_NonVirtualBase:
507 // Call the constructor.
508 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
509 E->arg_begin(), E->arg_end());
514 CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
517 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
518 Exp = E->getSubExpr();
519 assert(isa<CXXConstructExpr>(Exp) &&
520 "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
521 const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
522 const CXXConstructorDecl *CD = E->getConstructor();
523 RunCleanupsScope Scope(*this);
525 // If we require zero initialization before (or instead of) calling the
526 // constructor, as can be the case with a non-user-provided default
527 // constructor, emit the zero initialization now.
528 // FIXME. Do I still need this for a copy ctor synthesis?
529 if (E->requiresZeroInitialization())
530 EmitNullInitialization(Dest, E->getType());
532 assert(!getContext().getAsConstantArrayType(E->getType())
533 && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
534 EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
535 E->arg_begin(), E->arg_end());
538 static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
539 const CXXNewExpr *E) {
541 return CharUnits::Zero();
543 // No cookie is required if the operator new[] being used is the
544 // reserved placement operator new[].
545 if (E->getOperatorNew()->isReservedGlobalPlacementOperator())
546 return CharUnits::Zero();
548 return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
551 static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF,
553 unsigned minElements,
554 llvm::Value *&numElements,
555 llvm::Value *&sizeWithoutCookie) {
556 QualType type = e->getAllocatedType();
559 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
561 = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity());
562 return sizeWithoutCookie;
565 // The width of size_t.
566 unsigned sizeWidth = CGF.SizeTy->getBitWidth();
568 // Figure out the cookie size.
569 llvm::APInt cookieSize(sizeWidth,
570 CalculateCookiePadding(CGF, e).getQuantity());
572 // Emit the array size expression.
573 // We multiply the size of all dimensions for NumElements.
574 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
575 numElements = CGF.EmitScalarExpr(e->getArraySize());
576 assert(isa<llvm::IntegerType>(numElements->getType()));
578 // The number of elements can be have an arbitrary integer type;
579 // essentially, we need to multiply it by a constant factor, add a
580 // cookie size, and verify that the result is representable as a
581 // size_t. That's just a gloss, though, and it's wrong in one
582 // important way: if the count is negative, it's an error even if
583 // the cookie size would bring the total size >= 0.
585 = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType();
586 llvm::IntegerType *numElementsType
587 = cast<llvm::IntegerType>(numElements->getType());
588 unsigned numElementsWidth = numElementsType->getBitWidth();
590 // Compute the constant factor.
591 llvm::APInt arraySizeMultiplier(sizeWidth, 1);
592 while (const ConstantArrayType *CAT
593 = CGF.getContext().getAsConstantArrayType(type)) {
594 type = CAT->getElementType();
595 arraySizeMultiplier *= CAT->getSize();
598 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
599 llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
600 typeSizeMultiplier *= arraySizeMultiplier;
602 // This will be a size_t.
605 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
606 // Don't bloat the -O0 code.
607 if (llvm::ConstantInt *numElementsC =
608 dyn_cast<llvm::ConstantInt>(numElements)) {
609 const llvm::APInt &count = numElementsC->getValue();
611 bool hasAnyOverflow = false;
613 // If 'count' was a negative number, it's an overflow.
614 if (isSigned && count.isNegative())
615 hasAnyOverflow = true;
617 // We want to do all this arithmetic in size_t. If numElements is
618 // wider than that, check whether it's already too big, and if so,
620 else if (numElementsWidth > sizeWidth &&
621 numElementsWidth - sizeWidth > count.countLeadingZeros())
622 hasAnyOverflow = true;
624 // Okay, compute a count at the right width.
625 llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
627 // If there is a brace-initializer, we cannot allocate fewer elements than
628 // there are initializers. If we do, that's treated like an overflow.
629 if (adjustedCount.ult(minElements))
630 hasAnyOverflow = true;
632 // Scale numElements by that. This might overflow, but we don't
633 // care because it only overflows if allocationSize does, too, and
634 // if that overflows then we shouldn't use this.
635 numElements = llvm::ConstantInt::get(CGF.SizeTy,
636 adjustedCount * arraySizeMultiplier);
638 // Compute the size before cookie, and track whether it overflowed.
640 llvm::APInt allocationSize
641 = adjustedCount.umul_ov(typeSizeMultiplier, overflow);
642 hasAnyOverflow |= overflow;
644 // Add in the cookie, and check whether it's overflowed.
645 if (cookieSize != 0) {
646 // Save the current size without a cookie. This shouldn't be
647 // used if there was overflow.
648 sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
650 allocationSize = allocationSize.uadd_ov(cookieSize, overflow);
651 hasAnyOverflow |= overflow;
654 // On overflow, produce a -1 so operator new will fail.
655 if (hasAnyOverflow) {
656 size = llvm::Constant::getAllOnesValue(CGF.SizeTy);
658 size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
661 // Otherwise, we might need to use the overflow intrinsics.
663 // There are up to five conditions we need to test for:
664 // 1) if isSigned, we need to check whether numElements is negative;
665 // 2) if numElementsWidth > sizeWidth, we need to check whether
666 // numElements is larger than something representable in size_t;
667 // 3) if minElements > 0, we need to check whether numElements is smaller
669 // 4) we need to compute
670 // sizeWithoutCookie := numElements * typeSizeMultiplier
671 // and check whether it overflows; and
672 // 5) if we need a cookie, we need to compute
673 // size := sizeWithoutCookie + cookieSize
674 // and check whether it overflows.
676 llvm::Value *hasOverflow = 0;
678 // If numElementsWidth > sizeWidth, then one way or another, we're
679 // going to have to do a comparison for (2), and this happens to
680 // take care of (1), too.
681 if (numElementsWidth > sizeWidth) {
682 llvm::APInt threshold(numElementsWidth, 1);
683 threshold <<= sizeWidth;
685 llvm::Value *thresholdV
686 = llvm::ConstantInt::get(numElementsType, threshold);
688 hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV);
689 numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy);
691 // Otherwise, if we're signed, we want to sext up to size_t.
692 } else if (isSigned) {
693 if (numElementsWidth < sizeWidth)
694 numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy);
696 // If there's a non-1 type size multiplier, then we can do the
697 // signedness check at the same time as we do the multiply
698 // because a negative number times anything will cause an
699 // unsigned overflow. Otherwise, we have to do it here. But at least
700 // in this case, we can subsume the >= minElements check.
701 if (typeSizeMultiplier == 1)
702 hasOverflow = CGF.Builder.CreateICmpSLT(numElements,
703 llvm::ConstantInt::get(CGF.SizeTy, minElements));
705 // Otherwise, zext up to size_t if necessary.
706 } else if (numElementsWidth < sizeWidth) {
707 numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy);
710 assert(numElements->getType() == CGF.SizeTy);
713 // Don't allow allocation of fewer elements than we have initializers.
715 hasOverflow = CGF.Builder.CreateICmpULT(numElements,
716 llvm::ConstantInt::get(CGF.SizeTy, minElements));
717 } else if (numElementsWidth > sizeWidth) {
718 // The other existing overflow subsumes this check.
719 // We do an unsigned comparison, since any signed value < -1 is
720 // taken care of either above or below.
721 hasOverflow = CGF.Builder.CreateOr(hasOverflow,
722 CGF.Builder.CreateICmpULT(numElements,
723 llvm::ConstantInt::get(CGF.SizeTy, minElements)));
729 // Multiply by the type size if necessary. This multiplier
730 // includes all the factors for nested arrays.
732 // This step also causes numElements to be scaled up by the
733 // nested-array factor if necessary. Overflow on this computation
734 // can be ignored because the result shouldn't be used if
736 if (typeSizeMultiplier != 1) {
737 llvm::Value *umul_with_overflow
738 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy);
741 llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier);
742 llvm::Value *result =
743 CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV);
745 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
747 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
749 hasOverflow = overflowed;
751 size = CGF.Builder.CreateExtractValue(result, 0);
753 // Also scale up numElements by the array size multiplier.
754 if (arraySizeMultiplier != 1) {
755 // If the base element type size is 1, then we can re-use the
756 // multiply we just did.
757 if (typeSize.isOne()) {
758 assert(arraySizeMultiplier == typeSizeMultiplier);
761 // Otherwise we need a separate multiply.
764 llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier);
765 numElements = CGF.Builder.CreateMul(numElements, asmV);
769 // numElements doesn't need to be scaled.
770 assert(arraySizeMultiplier == 1);
773 // Add in the cookie size if necessary.
774 if (cookieSize != 0) {
775 sizeWithoutCookie = size;
777 llvm::Value *uadd_with_overflow
778 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy);
780 llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize);
781 llvm::Value *result =
782 CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV);
784 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
786 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
788 hasOverflow = overflowed;
790 size = CGF.Builder.CreateExtractValue(result, 0);
793 // If we had any possibility of dynamic overflow, make a select to
794 // overwrite 'size' with an all-ones value, which should cause
795 // operator new to throw.
797 size = CGF.Builder.CreateSelect(hasOverflow,
798 llvm::Constant::getAllOnesValue(CGF.SizeTy),
803 sizeWithoutCookie = size;
805 assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
810 static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init,
811 QualType AllocType, llvm::Value *NewPtr) {
813 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType);
814 if (!CGF.hasAggregateLLVMType(AllocType))
815 CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType,
818 else if (AllocType->isAnyComplexType())
819 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
820 AllocType.isVolatileQualified());
823 = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(),
824 AggValueSlot::IsDestructed,
825 AggValueSlot::DoesNotNeedGCBarriers,
826 AggValueSlot::IsNotAliased);
827 CGF.EmitAggExpr(Init, Slot);
829 CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init);
834 CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
835 QualType elementType,
836 llvm::Value *beginPtr,
837 llvm::Value *numElements) {
838 if (!E->hasInitializer())
839 return; // We have a POD type.
841 llvm::Value *explicitPtr = beginPtr;
842 // Find the end of the array, hoisted out of the loop.
843 llvm::Value *endPtr =
844 Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end");
846 unsigned initializerElements = 0;
848 const Expr *Init = E->getInitializer();
849 llvm::AllocaInst *endOfInit = 0;
850 QualType::DestructionKind dtorKind = elementType.isDestructedType();
851 EHScopeStack::stable_iterator cleanup;
852 llvm::Instruction *cleanupDominator = 0;
853 // If the initializer is an initializer list, first do the explicit elements.
854 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
855 initializerElements = ILE->getNumInits();
857 // Enter a partial-destruction cleanup if necessary.
858 if (needsEHCleanup(dtorKind)) {
859 // In principle we could tell the cleanup where we are more
860 // directly, but the control flow can get so varied here that it
861 // would actually be quite complex. Therefore we go through an
863 endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit");
864 cleanupDominator = Builder.CreateStore(beginPtr, endOfInit);
865 pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType,
866 getDestroyer(dtorKind));
867 cleanup = EHStack.stable_begin();
870 for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) {
871 // Tell the cleanup that it needs to destroy up to this
872 // element. TODO: some of these stores can be trivially
873 // observed to be unnecessary.
874 if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit);
875 StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr);
876 explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next");
879 // The remaining elements are filled with the array filler expression.
880 Init = ILE->getArrayFiller();
883 // Create the continuation block.
884 llvm::BasicBlock *contBB = createBasicBlock("new.loop.end");
886 // If the number of elements isn't constant, we have to now check if there is
887 // anything left to initialize.
888 if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) {
889 // If all elements have already been initialized, skip the whole loop.
890 if (constNum->getZExtValue() <= initializerElements) {
891 // If there was a cleanup, deactivate it.
892 if (cleanupDominator)
893 DeactivateCleanupBlock(cleanup, cleanupDominator);
897 llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty");
898 llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr,
900 Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB);
901 EmitBlock(nonEmptyBB);
905 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
906 llvm::BasicBlock *loopBB = createBasicBlock("new.loop");
910 // Set up the current-element phi.
911 llvm::PHINode *curPtr =
912 Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur");
913 curPtr->addIncoming(explicitPtr, entryBB);
915 // Store the new cleanup position for irregular cleanups.
916 if (endOfInit) Builder.CreateStore(curPtr, endOfInit);
918 // Enter a partial-destruction cleanup if necessary.
919 if (!cleanupDominator && needsEHCleanup(dtorKind)) {
920 pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType,
921 getDestroyer(dtorKind));
922 cleanup = EHStack.stable_begin();
923 cleanupDominator = Builder.CreateUnreachable();
926 // Emit the initializer into this element.
927 StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr);
929 // Leave the cleanup if we entered one.
930 if (cleanupDominator) {
931 DeactivateCleanupBlock(cleanup, cleanupDominator);
932 cleanupDominator->eraseFromParent();
935 // Advance to the next element.
936 llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next");
938 // Check whether we've gotten to the end of the array and, if so,
940 llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend");
941 Builder.CreateCondBr(isEnd, contBB, loopBB);
942 curPtr->addIncoming(nextPtr, Builder.GetInsertBlock());
947 static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
948 llvm::Value *NewPtr, llvm::Value *Size) {
949 CGF.EmitCastToVoidPtr(NewPtr);
950 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
951 CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
952 Alignment.getQuantity(), false);
955 static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
956 QualType ElementType,
958 llvm::Value *NumElements,
959 llvm::Value *AllocSizeWithoutCookie) {
960 const Expr *Init = E->getInitializer();
962 if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){
963 CXXConstructorDecl *Ctor = CCE->getConstructor();
964 if (Ctor->isTrivial()) {
965 // If new expression did not specify value-initialization, then there
966 // is no initialization.
967 if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty())
970 if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
971 // Optimization: since zero initialization will just set the memory
972 // to all zeroes, generate a single memset to do it in one shot.
973 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
978 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
979 CCE->arg_begin(), CCE->arg_end(),
980 CCE->requiresZeroInitialization());
982 } else if (Init && isa<ImplicitValueInitExpr>(Init) &&
983 CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
984 // Optimization: since zero initialization will just set the memory
985 // to all zeroes, generate a single memset to do it in one shot.
986 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
989 CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements);
996 StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr);
1000 /// A cleanup to call the given 'operator delete' function upon
1001 /// abnormal exit from a new expression.
1002 class CallDeleteDuringNew : public EHScopeStack::Cleanup {
1003 size_t NumPlacementArgs;
1004 const FunctionDecl *OperatorDelete;
1006 llvm::Value *AllocSize;
1008 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
1011 static size_t getExtraSize(size_t NumPlacementArgs) {
1012 return NumPlacementArgs * sizeof(RValue);
1015 CallDeleteDuringNew(size_t NumPlacementArgs,
1016 const FunctionDecl *OperatorDelete,
1018 llvm::Value *AllocSize)
1019 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1020 Ptr(Ptr), AllocSize(AllocSize) {}
1022 void setPlacementArg(unsigned I, RValue Arg) {
1023 assert(I < NumPlacementArgs && "index out of range");
1024 getPlacementArgs()[I] = Arg;
1027 void Emit(CodeGenFunction &CGF, Flags flags) {
1028 const FunctionProtoType *FPT
1029 = OperatorDelete->getType()->getAs<FunctionProtoType>();
1030 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
1031 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
1033 CallArgList DeleteArgs;
1035 // The first argument is always a void*.
1036 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
1037 DeleteArgs.add(RValue::get(Ptr), *AI++);
1039 // A member 'operator delete' can take an extra 'size_t' argument.
1040 if (FPT->getNumArgs() == NumPlacementArgs + 2)
1041 DeleteArgs.add(RValue::get(AllocSize), *AI++);
1043 // Pass the rest of the arguments, which must match exactly.
1044 for (unsigned I = 0; I != NumPlacementArgs; ++I)
1045 DeleteArgs.add(getPlacementArgs()[I], *AI++);
1047 // Call 'operator delete'.
1048 CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
1049 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1050 ReturnValueSlot(), DeleteArgs, OperatorDelete);
1054 /// A cleanup to call the given 'operator delete' function upon
1055 /// abnormal exit from a new expression when the new expression is
1057 class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
1058 size_t NumPlacementArgs;
1059 const FunctionDecl *OperatorDelete;
1060 DominatingValue<RValue>::saved_type Ptr;
1061 DominatingValue<RValue>::saved_type AllocSize;
1063 DominatingValue<RValue>::saved_type *getPlacementArgs() {
1064 return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
1068 static size_t getExtraSize(size_t NumPlacementArgs) {
1069 return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
1072 CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
1073 const FunctionDecl *OperatorDelete,
1074 DominatingValue<RValue>::saved_type Ptr,
1075 DominatingValue<RValue>::saved_type AllocSize)
1076 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1077 Ptr(Ptr), AllocSize(AllocSize) {}
1079 void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
1080 assert(I < NumPlacementArgs && "index out of range");
1081 getPlacementArgs()[I] = Arg;
1084 void Emit(CodeGenFunction &CGF, Flags flags) {
1085 const FunctionProtoType *FPT
1086 = OperatorDelete->getType()->getAs<FunctionProtoType>();
1087 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
1088 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
1090 CallArgList DeleteArgs;
1092 // The first argument is always a void*.
1093 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
1094 DeleteArgs.add(Ptr.restore(CGF), *AI++);
1096 // A member 'operator delete' can take an extra 'size_t' argument.
1097 if (FPT->getNumArgs() == NumPlacementArgs + 2) {
1098 RValue RV = AllocSize.restore(CGF);
1099 DeleteArgs.add(RV, *AI++);
1102 // Pass the rest of the arguments, which must match exactly.
1103 for (unsigned I = 0; I != NumPlacementArgs; ++I) {
1104 RValue RV = getPlacementArgs()[I].restore(CGF);
1105 DeleteArgs.add(RV, *AI++);
1108 // Call 'operator delete'.
1109 CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
1110 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1111 ReturnValueSlot(), DeleteArgs, OperatorDelete);
1116 /// Enter a cleanup to call 'operator delete' if the initializer in a
1117 /// new-expression throws.
1118 static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
1119 const CXXNewExpr *E,
1120 llvm::Value *NewPtr,
1121 llvm::Value *AllocSize,
1122 const CallArgList &NewArgs) {
1123 // If we're not inside a conditional branch, then the cleanup will
1124 // dominate and we can do the easier (and more efficient) thing.
1125 if (!CGF.isInConditionalBranch()) {
1126 CallDeleteDuringNew *Cleanup = CGF.EHStack
1127 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
1128 E->getNumPlacementArgs(),
1129 E->getOperatorDelete(),
1131 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1132 Cleanup->setPlacementArg(I, NewArgs[I+1].RV);
1137 // Otherwise, we need to save all this stuff.
1138 DominatingValue<RValue>::saved_type SavedNewPtr =
1139 DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
1140 DominatingValue<RValue>::saved_type SavedAllocSize =
1141 DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
1143 CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
1144 .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup,
1145 E->getNumPlacementArgs(),
1146 E->getOperatorDelete(),
1149 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1150 Cleanup->setPlacementArg(I,
1151 DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV));
1153 CGF.initFullExprCleanup();
1156 llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
1157 // The element type being allocated.
1158 QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
1160 // 1. Build a call to the allocation function.
1161 FunctionDecl *allocator = E->getOperatorNew();
1162 const FunctionProtoType *allocatorType =
1163 allocator->getType()->castAs<FunctionProtoType>();
1165 CallArgList allocatorArgs;
1167 // The allocation size is the first argument.
1168 QualType sizeType = getContext().getSizeType();
1170 // If there is a brace-initializer, cannot allocate fewer elements than inits.
1171 unsigned minElements = 0;
1172 if (E->isArray() && E->hasInitializer()) {
1173 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()))
1174 minElements = ILE->getNumInits();
1177 llvm::Value *numElements = 0;
1178 llvm::Value *allocSizeWithoutCookie = 0;
1179 llvm::Value *allocSize =
1180 EmitCXXNewAllocSize(*this, E, minElements, numElements,
1181 allocSizeWithoutCookie);
1183 allocatorArgs.add(RValue::get(allocSize), sizeType);
1185 // Emit the rest of the arguments.
1186 // FIXME: Ideally, this should just use EmitCallArgs.
1187 CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
1189 // First, use the types from the function type.
1190 // We start at 1 here because the first argument (the allocation size)
1191 // has already been emitted.
1192 for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
1193 ++i, ++placementArg) {
1194 QualType argType = allocatorType->getArgType(i);
1196 assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
1197 placementArg->getType()) &&
1198 "type mismatch in call argument!");
1200 EmitCallArg(allocatorArgs, *placementArg, argType);
1203 // Either we've emitted all the call args, or we have a call to a
1204 // variadic function.
1205 assert((placementArg == E->placement_arg_end() ||
1206 allocatorType->isVariadic()) &&
1207 "Extra arguments to non-variadic function!");
1209 // If we still have any arguments, emit them using the type of the argument.
1210 for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
1211 placementArg != placementArgsEnd; ++placementArg) {
1212 EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
1215 // Emit the allocation call. If the allocator is a global placement
1216 // operator, just "inline" it directly.
1218 if (allocator->isReservedGlobalPlacementOperator()) {
1219 assert(allocatorArgs.size() == 2);
1220 RV = allocatorArgs[1].RV;
1221 // TODO: kill any unnecessary computations done for the size
1224 RV = EmitCall(CGM.getTypes().arrangeFreeFunctionCall(allocatorArgs,
1226 CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
1227 allocatorArgs, allocator);
1230 // Emit a null check on the allocation result if the allocation
1231 // function is allowed to return null (because it has a non-throwing
1232 // exception spec; for this part, we inline
1233 // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1234 // interesting initializer.
1235 bool nullCheck = allocatorType->isNothrow(getContext()) &&
1236 (!allocType.isPODType(getContext()) || E->hasInitializer());
1238 llvm::BasicBlock *nullCheckBB = 0;
1239 llvm::BasicBlock *contBB = 0;
1241 llvm::Value *allocation = RV.getScalarVal();
1242 unsigned AS = allocation->getType()->getPointerAddressSpace();
1244 // The null-check means that the initializer is conditionally
1246 ConditionalEvaluation conditional(*this);
1249 conditional.begin(*this);
1251 nullCheckBB = Builder.GetInsertBlock();
1252 llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1253 contBB = createBasicBlock("new.cont");
1255 llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1256 Builder.CreateCondBr(isNull, contBB, notNullBB);
1257 EmitBlock(notNullBB);
1260 // If there's an operator delete, enter a cleanup to call it if an
1261 // exception is thrown.
1262 EHScopeStack::stable_iterator operatorDeleteCleanup;
1263 llvm::Instruction *cleanupDominator = 0;
1264 if (E->getOperatorDelete() &&
1265 !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) {
1266 EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1267 operatorDeleteCleanup = EHStack.stable_begin();
1268 cleanupDominator = Builder.CreateUnreachable();
1271 assert((allocSize == allocSizeWithoutCookie) ==
1272 CalculateCookiePadding(*this, E).isZero());
1273 if (allocSize != allocSizeWithoutCookie) {
1274 assert(E->isArray());
1275 allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1280 llvm::Type *elementPtrTy
1281 = ConvertTypeForMem(allocType)->getPointerTo(AS);
1282 llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
1284 EmitNewInitializer(*this, E, allocType, result, numElements,
1285 allocSizeWithoutCookie);
1287 // NewPtr is a pointer to the base element type. If we're
1288 // allocating an array of arrays, we'll need to cast back to the
1289 // array pointer type.
1290 llvm::Type *resultType = ConvertTypeForMem(E->getType());
1291 if (result->getType() != resultType)
1292 result = Builder.CreateBitCast(result, resultType);
1295 // Deactivate the 'operator delete' cleanup if we finished
1297 if (operatorDeleteCleanup.isValid()) {
1298 DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator);
1299 cleanupDominator->eraseFromParent();
1303 conditional.end(*this);
1305 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1308 llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
1309 PHI->addIncoming(result, notNullBB);
1310 PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1319 void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1321 QualType DeleteTy) {
1322 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1324 const FunctionProtoType *DeleteFTy =
1325 DeleteFD->getType()->getAs<FunctionProtoType>();
1327 CallArgList DeleteArgs;
1329 // Check if we need to pass the size to the delete operator.
1330 llvm::Value *Size = 0;
1332 if (DeleteFTy->getNumArgs() == 2) {
1333 SizeTy = DeleteFTy->getArgType(1);
1334 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1335 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1336 DeleteTypeSize.getQuantity());
1339 QualType ArgTy = DeleteFTy->getArgType(0);
1340 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1341 DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
1344 DeleteArgs.add(RValue::get(Size), SizeTy);
1346 // Emit the call to delete.
1347 EmitCall(CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, DeleteFTy),
1348 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
1349 DeleteArgs, DeleteFD);
1353 /// Calls the given 'operator delete' on a single object.
1354 struct CallObjectDelete : EHScopeStack::Cleanup {
1356 const FunctionDecl *OperatorDelete;
1357 QualType ElementType;
1359 CallObjectDelete(llvm::Value *Ptr,
1360 const FunctionDecl *OperatorDelete,
1361 QualType ElementType)
1362 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1364 void Emit(CodeGenFunction &CGF, Flags flags) {
1365 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1370 /// Emit the code for deleting a single object.
1371 static void EmitObjectDelete(CodeGenFunction &CGF,
1372 const FunctionDecl *OperatorDelete,
1374 QualType ElementType,
1375 bool UseGlobalDelete) {
1376 // Find the destructor for the type, if applicable. If the
1377 // destructor is virtual, we'll just emit the vcall and return.
1378 const CXXDestructorDecl *Dtor = 0;
1379 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1380 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1381 if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
1382 Dtor = RD->getDestructor();
1384 if (Dtor->isVirtual()) {
1385 if (UseGlobalDelete) {
1386 // If we're supposed to call the global delete, make sure we do so
1387 // even if the destructor throws.
1389 // Derive the complete-object pointer, which is what we need
1390 // to pass to the deallocation function.
1391 llvm::Value *completePtr =
1392 CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType);
1394 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1395 completePtr, OperatorDelete,
1400 CGF.getTypes().GetFunctionType(
1401 CGF.getTypes().arrangeCXXDestructor(Dtor, Dtor_Complete));
1404 = CGF.BuildVirtualCall(Dtor,
1405 UseGlobalDelete? Dtor_Complete : Dtor_Deleting,
1407 // FIXME: Provide a source location here.
1408 CGF.EmitCXXMemberCall(Dtor, SourceLocation(), Callee, ReturnValueSlot(),
1409 Ptr, /*VTT=*/0, 0, 0);
1411 if (UseGlobalDelete) {
1412 CGF.PopCleanupBlock();
1420 // Make sure that we call delete even if the dtor throws.
1421 // This doesn't have to a conditional cleanup because we're going
1422 // to pop it off in a second.
1423 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1424 Ptr, OperatorDelete, ElementType);
1427 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1428 /*ForVirtualBase=*/false, Ptr);
1429 else if (CGF.getLangOpts().ObjCAutoRefCount &&
1430 ElementType->isObjCLifetimeType()) {
1431 switch (ElementType.getObjCLifetime()) {
1432 case Qualifiers::OCL_None:
1433 case Qualifiers::OCL_ExplicitNone:
1434 case Qualifiers::OCL_Autoreleasing:
1437 case Qualifiers::OCL_Strong: {
1438 // Load the pointer value.
1439 llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr,
1440 ElementType.isVolatileQualified());
1442 CGF.EmitARCRelease(PtrValue, /*precise*/ true);
1446 case Qualifiers::OCL_Weak:
1447 CGF.EmitARCDestroyWeak(Ptr);
1452 CGF.PopCleanupBlock();
1456 /// Calls the given 'operator delete' on an array of objects.
1457 struct CallArrayDelete : EHScopeStack::Cleanup {
1459 const FunctionDecl *OperatorDelete;
1460 llvm::Value *NumElements;
1461 QualType ElementType;
1462 CharUnits CookieSize;
1464 CallArrayDelete(llvm::Value *Ptr,
1465 const FunctionDecl *OperatorDelete,
1466 llvm::Value *NumElements,
1467 QualType ElementType,
1468 CharUnits CookieSize)
1469 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1470 ElementType(ElementType), CookieSize(CookieSize) {}
1472 void Emit(CodeGenFunction &CGF, Flags flags) {
1473 const FunctionProtoType *DeleteFTy =
1474 OperatorDelete->getType()->getAs<FunctionProtoType>();
1475 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1479 // Pass the pointer as the first argument.
1480 QualType VoidPtrTy = DeleteFTy->getArgType(0);
1481 llvm::Value *DeletePtr
1482 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1483 Args.add(RValue::get(DeletePtr), VoidPtrTy);
1485 // Pass the original requested size as the second argument.
1486 if (DeleteFTy->getNumArgs() == 2) {
1487 QualType size_t = DeleteFTy->getArgType(1);
1488 llvm::IntegerType *SizeTy
1489 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1491 CharUnits ElementTypeSize =
1492 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1494 // The size of an element, multiplied by the number of elements.
1496 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1497 Size = CGF.Builder.CreateMul(Size, NumElements);
1499 // Plus the size of the cookie if applicable.
1500 if (!CookieSize.isZero()) {
1501 llvm::Value *CookieSizeV
1502 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1503 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1506 Args.add(RValue::get(Size), size_t);
1509 // Emit the call to delete.
1510 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Args, DeleteFTy),
1511 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1512 ReturnValueSlot(), Args, OperatorDelete);
1517 /// Emit the code for deleting an array of objects.
1518 static void EmitArrayDelete(CodeGenFunction &CGF,
1519 const CXXDeleteExpr *E,
1520 llvm::Value *deletedPtr,
1521 QualType elementType) {
1522 llvm::Value *numElements = 0;
1523 llvm::Value *allocatedPtr = 0;
1524 CharUnits cookieSize;
1525 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType,
1526 numElements, allocatedPtr, cookieSize);
1528 assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer");
1530 // Make sure that we call delete even if one of the dtors throws.
1531 const FunctionDecl *operatorDelete = E->getOperatorDelete();
1532 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1533 allocatedPtr, operatorDelete,
1534 numElements, elementType,
1537 // Destroy the elements.
1538 if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) {
1539 assert(numElements && "no element count for a type with a destructor!");
1541 llvm::Value *arrayEnd =
1542 CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end");
1544 // Note that it is legal to allocate a zero-length array, and we
1545 // can never fold the check away because the length should always
1546 // come from a cookie.
1547 CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType,
1548 CGF.getDestroyer(dtorKind),
1549 /*checkZeroLength*/ true,
1550 CGF.needsEHCleanup(dtorKind));
1553 // Pop the cleanup block.
1554 CGF.PopCleanupBlock();
1557 void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
1558 const Expr *Arg = E->getArgument();
1559 llvm::Value *Ptr = EmitScalarExpr(Arg);
1561 // Null check the pointer.
1562 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1563 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1565 llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
1567 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1568 EmitBlock(DeleteNotNull);
1570 // We might be deleting a pointer to array. If so, GEP down to the
1571 // first non-array element.
1572 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1573 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1574 if (DeleteTy->isConstantArrayType()) {
1575 llvm::Value *Zero = Builder.getInt32(0);
1576 SmallVector<llvm::Value*,8> GEP;
1578 GEP.push_back(Zero); // point at the outermost array
1580 // For each layer of array type we're pointing at:
1581 while (const ConstantArrayType *Arr
1582 = getContext().getAsConstantArrayType(DeleteTy)) {
1583 // 1. Unpeel the array type.
1584 DeleteTy = Arr->getElementType();
1586 // 2. GEP to the first element of the array.
1587 GEP.push_back(Zero);
1590 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first");
1593 assert(ConvertTypeForMem(DeleteTy) ==
1594 cast<llvm::PointerType>(Ptr->getType())->getElementType());
1596 if (E->isArrayForm()) {
1597 EmitArrayDelete(*this, E, Ptr, DeleteTy);
1599 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy,
1600 E->isGlobalDelete());
1603 EmitBlock(DeleteEnd);
1606 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1607 // void __cxa_bad_typeid();
1608 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1610 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1613 static void EmitBadTypeidCall(CodeGenFunction &CGF) {
1614 llvm::Value *Fn = getBadTypeidFn(CGF);
1615 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
1616 CGF.Builder.CreateUnreachable();
1619 static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
1621 llvm::Type *StdTypeInfoPtrTy) {
1622 // Get the vtable pointer.
1623 llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1625 // C++ [expr.typeid]p2:
1626 // If the glvalue expression is obtained by applying the unary * operator to
1627 // a pointer and the pointer is a null pointer value, the typeid expression
1628 // throws the std::bad_typeid exception.
1629 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
1630 if (UO->getOpcode() == UO_Deref) {
1631 llvm::BasicBlock *BadTypeidBlock =
1632 CGF.createBasicBlock("typeid.bad_typeid");
1633 llvm::BasicBlock *EndBlock =
1634 CGF.createBasicBlock("typeid.end");
1636 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1637 CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1639 CGF.EmitBlock(BadTypeidBlock);
1640 EmitBadTypeidCall(CGF);
1641 CGF.EmitBlock(EndBlock);
1645 llvm::Value *Value = CGF.GetVTablePtr(ThisPtr,
1646 StdTypeInfoPtrTy->getPointerTo());
1648 // Load the type info.
1649 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1650 return CGF.Builder.CreateLoad(Value);
1653 llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1654 llvm::Type *StdTypeInfoPtrTy =
1655 ConvertType(E->getType())->getPointerTo();
1657 if (E->isTypeOperand()) {
1658 llvm::Constant *TypeInfo =
1659 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
1660 return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
1663 // C++ [expr.typeid]p2:
1664 // When typeid is applied to a glvalue expression whose type is a
1665 // polymorphic class type, the result refers to a std::type_info object
1666 // representing the type of the most derived object (that is, the dynamic
1667 // type) to which the glvalue refers.
1668 if (E->isPotentiallyEvaluated())
1669 return EmitTypeidFromVTable(*this, E->getExprOperand(),
1672 QualType OperandTy = E->getExprOperand()->getType();
1673 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1677 static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) {
1678 // void *__dynamic_cast(const void *sub,
1679 // const abi::__class_type_info *src,
1680 // const abi::__class_type_info *dst,
1681 // std::ptrdiff_t src2dst_offset);
1683 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1684 llvm::Type *PtrDiffTy =
1685 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1687 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1689 llvm::FunctionType *FTy =
1690 llvm::FunctionType::get(Int8PtrTy, Args, false);
1692 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast");
1695 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1696 // void __cxa_bad_cast();
1697 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1698 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1701 static void EmitBadCastCall(CodeGenFunction &CGF) {
1702 llvm::Value *Fn = getBadCastFn(CGF);
1703 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
1704 CGF.Builder.CreateUnreachable();
1707 static llvm::Value *
1708 EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
1709 QualType SrcTy, QualType DestTy,
1710 llvm::BasicBlock *CastEnd) {
1711 llvm::Type *PtrDiffLTy =
1712 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1713 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1715 if (const PointerType *PTy = DestTy->getAs<PointerType>()) {
1716 if (PTy->getPointeeType()->isVoidType()) {
1717 // C++ [expr.dynamic.cast]p7:
1718 // If T is "pointer to cv void," then the result is a pointer to the
1719 // most derived object pointed to by v.
1721 // Get the vtable pointer.
1722 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1724 // Get the offset-to-top from the vtable.
1725 llvm::Value *OffsetToTop =
1726 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1727 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1729 // Finally, add the offset to the pointer.
1730 Value = CGF.EmitCastToVoidPtr(Value);
1731 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1733 return CGF.Builder.CreateBitCast(Value, DestLTy);
1737 QualType SrcRecordTy;
1738 QualType DestRecordTy;
1740 if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) {
1741 SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1742 DestRecordTy = DestPTy->getPointeeType();
1744 SrcRecordTy = SrcTy;
1745 DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1748 assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1749 assert(DestRecordTy->isRecordType() && "dest type must be a record type!");
1751 llvm::Value *SrcRTTI =
1752 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1753 llvm::Value *DestRTTI =
1754 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1756 // FIXME: Actually compute a hint here.
1757 llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL);
1759 // Emit the call to __dynamic_cast.
1760 Value = CGF.EmitCastToVoidPtr(Value);
1761 Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value,
1762 SrcRTTI, DestRTTI, OffsetHint);
1763 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1765 /// C++ [expr.dynamic.cast]p9:
1766 /// A failed cast to reference type throws std::bad_cast
1767 if (DestTy->isReferenceType()) {
1768 llvm::BasicBlock *BadCastBlock =
1769 CGF.createBasicBlock("dynamic_cast.bad_cast");
1771 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1772 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1774 CGF.EmitBlock(BadCastBlock);
1775 EmitBadCastCall(CGF);
1781 static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1783 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1784 if (DestTy->isPointerType())
1785 return llvm::Constant::getNullValue(DestLTy);
1787 /// C++ [expr.dynamic.cast]p9:
1788 /// A failed cast to reference type throws std::bad_cast
1789 EmitBadCastCall(CGF);
1791 CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1792 return llvm::UndefValue::get(DestLTy);
1795 llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
1796 const CXXDynamicCastExpr *DCE) {
1797 QualType DestTy = DCE->getTypeAsWritten();
1799 if (DCE->isAlwaysNull())
1800 return EmitDynamicCastToNull(*this, DestTy);
1802 QualType SrcTy = DCE->getSubExpr()->getType();
1804 // C++ [expr.dynamic.cast]p4:
1805 // If the value of v is a null pointer value in the pointer case, the result
1806 // is the null pointer value of type T.
1807 bool ShouldNullCheckSrcValue = SrcTy->isPointerType();
1809 llvm::BasicBlock *CastNull = 0;
1810 llvm::BasicBlock *CastNotNull = 0;
1811 llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
1813 if (ShouldNullCheckSrcValue) {
1814 CastNull = createBasicBlock("dynamic_cast.null");
1815 CastNotNull = createBasicBlock("dynamic_cast.notnull");
1817 llvm::Value *IsNull = Builder.CreateIsNull(Value);
1818 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1819 EmitBlock(CastNotNull);
1822 Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd);
1824 if (ShouldNullCheckSrcValue) {
1825 EmitBranch(CastEnd);
1827 EmitBlock(CastNull);
1828 EmitBranch(CastEnd);
1833 if (ShouldNullCheckSrcValue) {
1834 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1835 PHI->addIncoming(Value, CastNotNull);
1836 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1844 void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) {
1845 RunCleanupsScope Scope(*this);
1846 LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(),
1847 Slot.getAlignment());
1849 CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
1850 for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(),
1851 e = E->capture_init_end();
1852 i != e; ++i, ++CurField) {
1853 // Emit initialization
1855 LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
1856 ArrayRef<VarDecl *> ArrayIndexes;
1857 if (CurField->getType()->isArrayType())
1858 ArrayIndexes = E->getCaptureInitIndexVars(i);
1859 EmitInitializerForField(*CurField, LV, *i, ArrayIndexes);