1 //===--- CGExpr.cpp - Emit LLVM Code from 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 to emit Expr nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGCall.h"
17 #include "CGCXXABI.h"
18 #include "CGDebugInfo.h"
19 #include "CGRecordLayout.h"
20 #include "CGObjCRuntime.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/Basic/ConvertUTF.h"
25 #include "clang/Frontend/CodeGenOptions.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/MDBuilder.h"
29 #include "llvm/DataLayout.h"
30 #include "llvm/ADT/Hashing.h"
31 using namespace clang;
32 using namespace CodeGen;
33 
34 //===--------------------------------------------------------------------===//
35 //                        Miscellaneous Helper Methods
36 //===--------------------------------------------------------------------===//
37 
38 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
39   unsigned addressSpace =
40     cast<llvm::PointerType>(value->getType())->getAddressSpace();
41 
42   llvm::PointerType *destType = Int8PtrTy;
43   if (addressSpace)
44     destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
45 
46   if (value->getType() == destType) return value;
47   return Builder.CreateBitCast(value, destType);
48 }
49 
50 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
51 /// block.
52 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
53                                                     const Twine &Name) {
54   if (!Builder.isNamePreserving())
55     return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
56   return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
57 }
58 
59 void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
60                                      llvm::Value *Init) {
61   llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
62   llvm::BasicBlock *Block = AllocaInsertPt->getParent();
63   Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
64 }
65 
66 llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
67                                                 const Twine &Name) {
68   llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
69   // FIXME: Should we prefer the preferred type alignment here?
70   CharUnits Align = getContext().getTypeAlignInChars(Ty);
71   Alloc->setAlignment(Align.getQuantity());
72   return Alloc;
73 }
74 
75 llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
76                                                  const Twine &Name) {
77   llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
78   // FIXME: Should we prefer the preferred type alignment here?
79   CharUnits Align = getContext().getTypeAlignInChars(Ty);
80   Alloc->setAlignment(Align.getQuantity());
81   return Alloc;
82 }
83 
84 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
85 /// expression and compare the result against zero, returning an Int1Ty value.
86 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
87   if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
88     llvm::Value *MemPtr = EmitScalarExpr(E);
89     return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
90   }
91 
92   QualType BoolTy = getContext().BoolTy;
93   if (!E->getType()->isAnyComplexType())
94     return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
95 
96   return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
97 }
98 
99 /// EmitIgnoredExpr - Emit code to compute the specified expression,
100 /// ignoring the result.
101 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
102   if (E->isRValue())
103     return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
104 
105   // Just emit it as an l-value and drop the result.
106   EmitLValue(E);
107 }
108 
109 /// EmitAnyExpr - Emit code to compute the specified expression which
110 /// can have any type.  The result is returned as an RValue struct.
111 /// If this is an aggregate expression, AggSlot indicates where the
112 /// result should be returned.
113 RValue CodeGenFunction::EmitAnyExpr(const Expr *E,
114                                     AggValueSlot aggSlot,
115                                     bool ignoreResult) {
116   if (!hasAggregateLLVMType(E->getType()))
117     return RValue::get(EmitScalarExpr(E, ignoreResult));
118   else if (E->getType()->isAnyComplexType())
119     return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
120 
121   if (!ignoreResult && aggSlot.isIgnored())
122     aggSlot = CreateAggTemp(E->getType(), "agg-temp");
123   EmitAggExpr(E, aggSlot);
124   return aggSlot.asRValue();
125 }
126 
127 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
128 /// always be accessible even if no aggregate location is provided.
129 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
130   AggValueSlot AggSlot = AggValueSlot::ignored();
131 
132   if (hasAggregateLLVMType(E->getType()) &&
133       !E->getType()->isAnyComplexType())
134     AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
135   return EmitAnyExpr(E, AggSlot);
136 }
137 
138 /// EmitAnyExprToMem - Evaluate an expression into a given memory
139 /// location.
140 void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
141                                        llvm::Value *Location,
142                                        Qualifiers Quals,
143                                        bool IsInit) {
144   // FIXME: This function should take an LValue as an argument.
145   if (E->getType()->isAnyComplexType()) {
146     EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile());
147   } else if (hasAggregateLLVMType(E->getType())) {
148     CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
149     EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals,
150                                          AggValueSlot::IsDestructed_t(IsInit),
151                                          AggValueSlot::DoesNotNeedGCBarriers,
152                                          AggValueSlot::IsAliased_t(!IsInit)));
153   } else {
154     RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
155     LValue LV = MakeAddrLValue(Location, E->getType());
156     EmitStoreThroughLValue(RV, LV);
157   }
158 }
159 
160 namespace {
161 /// \brief An adjustment to be made to the temporary created when emitting a
162 /// reference binding, which accesses a particular subobject of that temporary.
163   struct SubobjectAdjustment {
164     enum {
165       DerivedToBaseAdjustment,
166       FieldAdjustment,
167       MemberPointerAdjustment
168     } Kind;
169 
170     union {
171       struct {
172         const CastExpr *BasePath;
173         const CXXRecordDecl *DerivedClass;
174       } DerivedToBase;
175 
176       FieldDecl *Field;
177 
178       struct {
179         const MemberPointerType *MPT;
180         Expr *RHS;
181       } Ptr;
182     };
183 
184     SubobjectAdjustment(const CastExpr *BasePath,
185                         const CXXRecordDecl *DerivedClass)
186       : Kind(DerivedToBaseAdjustment) {
187       DerivedToBase.BasePath = BasePath;
188       DerivedToBase.DerivedClass = DerivedClass;
189     }
190 
191     SubobjectAdjustment(FieldDecl *Field)
192       : Kind(FieldAdjustment) {
193       this->Field = Field;
194     }
195 
196     SubobjectAdjustment(const MemberPointerType *MPT, Expr *RHS)
197       : Kind(MemberPointerAdjustment) {
198       this->Ptr.MPT = MPT;
199       this->Ptr.RHS = RHS;
200     }
201   };
202 }
203 
204 static llvm::Value *
205 CreateReferenceTemporary(CodeGenFunction &CGF, QualType Type,
206                          const NamedDecl *InitializedDecl) {
207   if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
208     if (VD->hasGlobalStorage()) {
209       SmallString<256> Name;
210       llvm::raw_svector_ostream Out(Name);
211       CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out);
212       Out.flush();
213 
214       llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type);
215 
216       // Create the reference temporary.
217       llvm::GlobalValue *RefTemp =
218         new llvm::GlobalVariable(CGF.CGM.getModule(),
219                                  RefTempTy, /*isConstant=*/false,
220                                  llvm::GlobalValue::InternalLinkage,
221                                  llvm::Constant::getNullValue(RefTempTy),
222                                  Name.str());
223       return RefTemp;
224     }
225   }
226 
227   return CGF.CreateMemTemp(Type, "ref.tmp");
228 }
229 
230 static const Expr *
231 findMaterializedTemporary(const Expr *E, const MaterializeTemporaryExpr *&MTE) {
232   // Look through single-element init lists that claim to be lvalues. They're
233   // just syntactic wrappers in this case.
234   if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
235     if (ILE->getNumInits() == 1 && ILE->isGLValue())
236       E = ILE->getInit(0);
237   }
238 
239   // Look through expressions for materialized temporaries (for now).
240   if (const MaterializeTemporaryExpr *M
241       = dyn_cast<MaterializeTemporaryExpr>(E)) {
242     MTE = M;
243     E = M->GetTemporaryExpr();
244   }
245 
246   if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
247     E = DAE->getExpr();
248   return E;
249 }
250 
251 static const Expr *
252 skipRValueSubobjectAdjustments(const Expr *E,
253                             SmallVectorImpl<SubobjectAdjustment> &Adjustments) {
254   while (true) {
255     E = E->IgnoreParens();
256 
257     if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
258       if ((CE->getCastKind() == CK_DerivedToBase ||
259            CE->getCastKind() == CK_UncheckedDerivedToBase) &&
260           E->getType()->isRecordType()) {
261         E = CE->getSubExpr();
262         CXXRecordDecl *Derived
263           = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
264         Adjustments.push_back(SubobjectAdjustment(CE, Derived));
265         continue;
266       }
267 
268       if (CE->getCastKind() == CK_NoOp) {
269         E = CE->getSubExpr();
270         continue;
271       }
272     } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
273       if (!ME->isArrow() && ME->getBase()->isRValue()) {
274         assert(ME->getBase()->getType()->isRecordType());
275         if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
276           E = ME->getBase();
277           Adjustments.push_back(SubobjectAdjustment(Field));
278           continue;
279         }
280       }
281     } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
282       if (BO->isPtrMemOp()) {
283         assert(BO->getLHS()->isRValue());
284         E = BO->getLHS();
285         const MemberPointerType *MPT =
286           BO->getRHS()->getType()->getAs<MemberPointerType>();
287         Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
288       }
289     }
290 
291     // Nothing changed.
292     break;
293   }
294   return E;
295 }
296 
297 static llvm::Value *
298 EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E,
299                             llvm::Value *&ReferenceTemporary,
300                             const CXXDestructorDecl *&ReferenceTemporaryDtor,
301                             QualType &ObjCARCReferenceLifetimeType,
302                             const NamedDecl *InitializedDecl) {
303   const MaterializeTemporaryExpr *M = NULL;
304   E = findMaterializedTemporary(E, M);
305   // Objective-C++ ARC:
306   //   If we are binding a reference to a temporary that has ownership, we
307   //   need to perform retain/release operations on the temporary.
308   if (M && CGF.getContext().getLangOpts().ObjCAutoRefCount &&
309       M->getType()->isObjCLifetimeType() &&
310       (M->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
311        M->getType().getObjCLifetime() == Qualifiers::OCL_Weak ||
312        M->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing))
313     ObjCARCReferenceLifetimeType = M->getType();
314 
315   if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(E)) {
316     CGF.enterFullExpression(EWC);
317     CodeGenFunction::RunCleanupsScope Scope(CGF);
318 
319     return EmitExprForReferenceBinding(CGF, EWC->getSubExpr(),
320                                        ReferenceTemporary,
321                                        ReferenceTemporaryDtor,
322                                        ObjCARCReferenceLifetimeType,
323                                        InitializedDecl);
324   }
325 
326   RValue RV;
327   if (E->isGLValue()) {
328     // Emit the expression as an lvalue.
329     LValue LV = CGF.EmitLValue(E);
330 
331     if (LV.isSimple())
332       return LV.getAddress();
333 
334     // We have to load the lvalue.
335     RV = CGF.EmitLoadOfLValue(LV);
336   } else {
337     if (!ObjCARCReferenceLifetimeType.isNull()) {
338       ReferenceTemporary = CreateReferenceTemporary(CGF,
339                                                   ObjCARCReferenceLifetimeType,
340                                                     InitializedDecl);
341 
342 
343       LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary,
344                                              ObjCARCReferenceLifetimeType);
345 
346       CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl),
347                          RefTempDst, false);
348 
349       bool ExtendsLifeOfTemporary = false;
350       if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
351         if (Var->extendsLifetimeOfTemporary())
352           ExtendsLifeOfTemporary = true;
353       } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) {
354         ExtendsLifeOfTemporary = true;
355       }
356 
357       if (!ExtendsLifeOfTemporary) {
358         // Since the lifetime of this temporary isn't going to be extended,
359         // we need to clean it up ourselves at the end of the full expression.
360         switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
361         case Qualifiers::OCL_None:
362         case Qualifiers::OCL_ExplicitNone:
363         case Qualifiers::OCL_Autoreleasing:
364           break;
365 
366         case Qualifiers::OCL_Strong: {
367           assert(!ObjCARCReferenceLifetimeType->isArrayType());
368           CleanupKind cleanupKind = CGF.getARCCleanupKind();
369           CGF.pushDestroy(cleanupKind,
370                           ReferenceTemporary,
371                           ObjCARCReferenceLifetimeType,
372                           CodeGenFunction::destroyARCStrongImprecise,
373                           cleanupKind & EHCleanup);
374           break;
375         }
376 
377         case Qualifiers::OCL_Weak:
378           assert(!ObjCARCReferenceLifetimeType->isArrayType());
379           CGF.pushDestroy(NormalAndEHCleanup,
380                           ReferenceTemporary,
381                           ObjCARCReferenceLifetimeType,
382                           CodeGenFunction::destroyARCWeak,
383                           /*useEHCleanupForArray*/ true);
384           break;
385         }
386 
387         ObjCARCReferenceLifetimeType = QualType();
388       }
389 
390       return ReferenceTemporary;
391     }
392 
393     SmallVector<SubobjectAdjustment, 2> Adjustments;
394     E = skipRValueSubobjectAdjustments(E, Adjustments);
395     if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E))
396       if (opaque->getType()->isRecordType())
397         return CGF.EmitOpaqueValueLValue(opaque).getAddress();
398 
399     // Create a reference temporary if necessary.
400     AggValueSlot AggSlot = AggValueSlot::ignored();
401     if (CGF.hasAggregateLLVMType(E->getType()) &&
402         !E->getType()->isAnyComplexType()) {
403       ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
404                                                     InitializedDecl);
405       CharUnits Alignment = CGF.getContext().getTypeAlignInChars(E->getType());
406       AggValueSlot::IsDestructed_t isDestructed
407         = AggValueSlot::IsDestructed_t(InitializedDecl != 0);
408       AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Alignment,
409                                       Qualifiers(), isDestructed,
410                                       AggValueSlot::DoesNotNeedGCBarriers,
411                                       AggValueSlot::IsNotAliased);
412     }
413 
414     if (InitializedDecl) {
415       // Get the destructor for the reference temporary.
416       if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
417         CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
418         if (!ClassDecl->hasTrivialDestructor())
419           ReferenceTemporaryDtor = ClassDecl->getDestructor();
420       }
421     }
422 
423     RV = CGF.EmitAnyExpr(E, AggSlot);
424 
425     // Check if need to perform derived-to-base casts and/or field accesses, to
426     // get from the temporary object we created (and, potentially, for which we
427     // extended the lifetime) to the subobject we're binding the reference to.
428     if (!Adjustments.empty()) {
429       llvm::Value *Object = RV.getAggregateAddr();
430       for (unsigned I = Adjustments.size(); I != 0; --I) {
431         SubobjectAdjustment &Adjustment = Adjustments[I-1];
432         switch (Adjustment.Kind) {
433         case SubobjectAdjustment::DerivedToBaseAdjustment:
434           Object =
435               CGF.GetAddressOfBaseClass(Object,
436                                         Adjustment.DerivedToBase.DerivedClass,
437                               Adjustment.DerivedToBase.BasePath->path_begin(),
438                               Adjustment.DerivedToBase.BasePath->path_end(),
439                                         /*NullCheckValue=*/false);
440           break;
441 
442         case SubobjectAdjustment::FieldAdjustment: {
443           LValue LV = CGF.MakeAddrLValue(Object, E->getType());
444           LV = CGF.EmitLValueForField(LV, Adjustment.Field);
445           if (LV.isSimple()) {
446             Object = LV.getAddress();
447             break;
448           }
449 
450           // For non-simple lvalues, we actually have to create a copy of
451           // the object we're binding to.
452           QualType T = Adjustment.Field->getType().getNonReferenceType()
453                                                   .getUnqualifiedType();
454           Object = CreateReferenceTemporary(CGF, T, InitializedDecl);
455           LValue TempLV = CGF.MakeAddrLValue(Object,
456                                              Adjustment.Field->getType());
457           CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV);
458           break;
459         }
460 
461         case SubobjectAdjustment::MemberPointerAdjustment: {
462           llvm::Value *Ptr = CGF.EmitScalarExpr(Adjustment.Ptr.RHS);
463           Object = CGF.CGM.getCXXABI().EmitMemberDataPointerAddress(
464                         CGF, Object, Ptr, Adjustment.Ptr.MPT);
465           break;
466         }
467         }
468       }
469 
470       return Object;
471     }
472   }
473 
474   if (RV.isAggregate())
475     return RV.getAggregateAddr();
476 
477   // Create a temporary variable that we can bind the reference to.
478   ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
479                                                 InitializedDecl);
480 
481 
482   unsigned Alignment =
483     CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity();
484   if (RV.isScalar())
485     CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary,
486                           /*Volatile=*/false, Alignment, E->getType());
487   else
488     CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary,
489                            /*Volatile=*/false);
490   return ReferenceTemporary;
491 }
492 
493 RValue
494 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E,
495                                             const NamedDecl *InitializedDecl) {
496   llvm::Value *ReferenceTemporary = 0;
497   const CXXDestructorDecl *ReferenceTemporaryDtor = 0;
498   QualType ObjCARCReferenceLifetimeType;
499   llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary,
500                                                    ReferenceTemporaryDtor,
501                                                    ObjCARCReferenceLifetimeType,
502                                                    InitializedDecl);
503   if (CatchUndefined && !E->getType()->isFunctionType()) {
504     // C++11 [dcl.ref]p5 (as amended by core issue 453):
505     //   If a glvalue to which a reference is directly bound designates neither
506     //   an existing object or function of an appropriate type nor a region of
507     //   storage of suitable size and alignment to contain an object of the
508     //   reference's type, the behavior is undefined.
509     QualType Ty = E->getType();
510     EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty);
511   }
512   if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull())
513     return RValue::get(Value);
514 
515   // Make sure to call the destructor for the reference temporary.
516   const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl);
517   if (VD && VD->hasGlobalStorage()) {
518     if (ReferenceTemporaryDtor) {
519       llvm::Constant *DtorFn =
520         CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
521       CGM.getCXXABI().registerGlobalDtor(*this, DtorFn,
522                                     cast<llvm::Constant>(ReferenceTemporary));
523     } else {
524       assert(!ObjCARCReferenceLifetimeType.isNull());
525       // Note: We intentionally do not register a global "destructor" to
526       // release the object.
527     }
528 
529     return RValue::get(Value);
530   }
531 
532   if (ReferenceTemporaryDtor)
533     PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary);
534   else {
535     switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
536     case Qualifiers::OCL_None:
537       llvm_unreachable(
538                       "Not a reference temporary that needs to be deallocated");
539     case Qualifiers::OCL_ExplicitNone:
540     case Qualifiers::OCL_Autoreleasing:
541       // Nothing to do.
542       break;
543 
544     case Qualifiers::OCL_Strong: {
545       bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>();
546       CleanupKind cleanupKind = getARCCleanupKind();
547       pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType,
548                   precise ? destroyARCStrongPrecise : destroyARCStrongImprecise,
549                   cleanupKind & EHCleanup);
550       break;
551     }
552 
553     case Qualifiers::OCL_Weak: {
554       // __weak objects always get EH cleanups; otherwise, exceptions
555       // could cause really nasty crashes instead of mere leaks.
556       pushDestroy(NormalAndEHCleanup, ReferenceTemporary,
557                   ObjCARCReferenceLifetimeType, destroyARCWeak, true);
558       break;
559     }
560     }
561   }
562 
563   return RValue::get(Value);
564 }
565 
566 
567 /// getAccessedFieldNo - Given an encoded value and a result number, return the
568 /// input field number being accessed.
569 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
570                                              const llvm::Constant *Elts) {
571   return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
572       ->getZExtValue();
573 }
574 
575 /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
576 static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
577                                     llvm::Value *High) {
578   llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
579   llvm::Value *K47 = Builder.getInt64(47);
580   llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
581   llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
582   llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
583   llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
584   return Builder.CreateMul(B1, KMul);
585 }
586 
587 void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
588                                     llvm::Value *Address,
589                                     QualType Ty, CharUnits Alignment) {
590   if (!CatchUndefined)
591     return;
592 
593   llvm::Value *Cond = 0;
594 
595   if (TCK != TCK_Load && TCK != TCK_Store) {
596     // The glvalue must not be an empty glvalue. Don't bother checking this for
597     // loads and stores, because we will get a segfault anyway (if the operation
598     // isn't optimized out).
599     Cond = Builder.CreateICmpNE(
600         Address, llvm::Constant::getNullValue(Address->getType()));
601   }
602 
603   uint64_t AlignVal = Alignment.getQuantity();
604 
605   if (!Ty->isIncompleteType()) {
606     uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity();
607     if (!AlignVal)
608       AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
609 
610     // This needs to be to the standard address space.
611     Address = Builder.CreateBitCast(Address, Int8PtrTy);
612 
613     // The glvalue must refer to a large enough storage region.
614     // FIXME: If -faddress-sanitizer is enabled, insert dynamic instrumentation
615     //        to check this.
616     llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy);
617     llvm::Value *Min = Builder.getFalse();
618     llvm::Value *LargeEnough =
619         Builder.CreateICmpUGE(Builder.CreateCall2(F, Address, Min),
620                               llvm::ConstantInt::get(IntPtrTy, Size));
621     Cond = Cond ? Builder.CreateAnd(Cond, LargeEnough) : LargeEnough;
622   }
623 
624   if (AlignVal) {
625     // The glvalue must be suitably aligned.
626     llvm::Value *Align =
627         Builder.CreateAnd(Builder.CreatePtrToInt(Address, IntPtrTy),
628                           llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
629     Cond = Builder.CreateAnd(Cond,
630         Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)));
631   }
632 
633   if (Cond) {
634     llvm::Constant *StaticData[] = {
635       EmitCheckSourceLocation(Loc),
636       EmitCheckTypeDescriptor(Ty),
637       llvm::ConstantInt::get(SizeTy, AlignVal),
638       llvm::ConstantInt::get(Int8Ty, TCK)
639     };
640     EmitCheck(Cond, "type_mismatch", StaticData, Address);
641   }
642 
643   CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
644   if (TCK != TCK_ConstructorCall &&
645       RD && RD->hasDefinition() && RD->isDynamicClass()) {
646     // Check that the vptr indicates that there is a subobject of type Ty at
647     // offset zero within this object.
648     // FIXME: Produce a diagnostic if the user tries to combine this check with
649     //        -fno-rtti.
650 
651     // Compute a hash of the mangled name of the type.
652     //
653     // FIXME: This is not guaranteed to be deterministic! Move to a
654     //        fingerprinting mechanism once LLVM provides one. For the time
655     //        being the implementation happens to be deterministic.
656     llvm::SmallString<64> MangledName;
657     llvm::raw_svector_ostream Out(MangledName);
658     CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
659                                                      Out);
660     llvm::hash_code TypeHash = hash_value(Out.str());
661 
662     // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
663     llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
664     llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
665     llvm::Value *VPtrAddr = Builder.CreateBitCast(Address, VPtrTy);
666     llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
667     llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
668 
669     llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
670     Hash = Builder.CreateTrunc(Hash, IntPtrTy);
671 
672     // Look the hash up in our cache.
673     const int CacheSize = 128;
674     llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
675     llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
676                                                    "__ubsan_vptr_type_cache");
677     llvm::Value *Slot = Builder.CreateAnd(Hash,
678                                           llvm::ConstantInt::get(IntPtrTy,
679                                                                  CacheSize-1));
680     llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
681     llvm::Value *CacheVal =
682       Builder.CreateLoad(Builder.CreateInBoundsGEP(Cache, Indices));
683 
684     // If the hash isn't in the cache, call a runtime handler to perform the
685     // hard work of checking whether the vptr is for an object of the right
686     // type. This will either fill in the cache and return, or produce a
687     // diagnostic.
688     llvm::Constant *StaticData[] = {
689       EmitCheckSourceLocation(Loc),
690       EmitCheckTypeDescriptor(Ty),
691       CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
692       llvm::ConstantInt::get(Int8Ty, TCK)
693     };
694     llvm::Value *DynamicData[] = { Address, Hash };
695     EmitCheck(Builder.CreateICmpEQ(CacheVal, Hash),
696               "dynamic_type_cache_miss", StaticData, DynamicData, true);
697   }
698 }
699 
700 
701 CodeGenFunction::ComplexPairTy CodeGenFunction::
702 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
703                          bool isInc, bool isPre) {
704   ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
705                                             LV.isVolatileQualified());
706 
707   llvm::Value *NextVal;
708   if (isa<llvm::IntegerType>(InVal.first->getType())) {
709     uint64_t AmountVal = isInc ? 1 : -1;
710     NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
711 
712     // Add the inc/dec to the real part.
713     NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
714   } else {
715     QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
716     llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
717     if (!isInc)
718       FVal.changeSign();
719     NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
720 
721     // Add the inc/dec to the real part.
722     NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
723   }
724 
725   ComplexPairTy IncVal(NextVal, InVal.second);
726 
727   // Store the updated result through the lvalue.
728   StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
729 
730   // If this is a postinc, return the value read from memory, otherwise use the
731   // updated value.
732   return isPre ? IncVal : InVal;
733 }
734 
735 
736 //===----------------------------------------------------------------------===//
737 //                         LValue Expression Emission
738 //===----------------------------------------------------------------------===//
739 
740 RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
741   if (Ty->isVoidType())
742     return RValue::get(0);
743 
744   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
745     llvm::Type *EltTy = ConvertType(CTy->getElementType());
746     llvm::Value *U = llvm::UndefValue::get(EltTy);
747     return RValue::getComplex(std::make_pair(U, U));
748   }
749 
750   // If this is a use of an undefined aggregate type, the aggregate must have an
751   // identifiable address.  Just because the contents of the value are undefined
752   // doesn't mean that the address can't be taken and compared.
753   if (hasAggregateLLVMType(Ty)) {
754     llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
755     return RValue::getAggregate(DestPtr);
756   }
757 
758   return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
759 }
760 
761 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
762                                               const char *Name) {
763   ErrorUnsupported(E, Name);
764   return GetUndefRValue(E->getType());
765 }
766 
767 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
768                                               const char *Name) {
769   ErrorUnsupported(E, Name);
770   llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
771   return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
772 }
773 
774 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
775   LValue LV = EmitLValue(E);
776   if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
777     EmitTypeCheck(TCK, E->getExprLoc(), LV.getAddress(),
778                   E->getType(), LV.getAlignment());
779   return LV;
780 }
781 
782 /// EmitLValue - Emit code to compute a designator that specifies the location
783 /// of the expression.
784 ///
785 /// This can return one of two things: a simple address or a bitfield reference.
786 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be
787 /// an LLVM pointer type.
788 ///
789 /// If this returns a bitfield reference, nothing about the pointee type of the
790 /// LLVM value is known: For example, it may not be a pointer to an integer.
791 ///
792 /// If this returns a normal address, and if the lvalue's C type is fixed size,
793 /// this method guarantees that the returned pointer type will point to an LLVM
794 /// type of the same size of the lvalue's type.  If the lvalue has a variable
795 /// length type, this is not possible.
796 ///
797 LValue CodeGenFunction::EmitLValue(const Expr *E) {
798   switch (E->getStmtClass()) {
799   default: return EmitUnsupportedLValue(E, "l-value expression");
800 
801   case Expr::ObjCPropertyRefExprClass:
802     llvm_unreachable("cannot emit a property reference directly");
803 
804   case Expr::ObjCSelectorExprClass:
805     return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
806   case Expr::ObjCIsaExprClass:
807     return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
808   case Expr::BinaryOperatorClass:
809     return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
810   case Expr::CompoundAssignOperatorClass:
811     if (!E->getType()->isAnyComplexType())
812       return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
813     return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
814   case Expr::CallExprClass:
815   case Expr::CXXMemberCallExprClass:
816   case Expr::CXXOperatorCallExprClass:
817   case Expr::UserDefinedLiteralClass:
818     return EmitCallExprLValue(cast<CallExpr>(E));
819   case Expr::VAArgExprClass:
820     return EmitVAArgExprLValue(cast<VAArgExpr>(E));
821   case Expr::DeclRefExprClass:
822     return EmitDeclRefLValue(cast<DeclRefExpr>(E));
823   case Expr::ParenExprClass:
824     return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
825   case Expr::GenericSelectionExprClass:
826     return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
827   case Expr::PredefinedExprClass:
828     return EmitPredefinedLValue(cast<PredefinedExpr>(E));
829   case Expr::StringLiteralClass:
830     return EmitStringLiteralLValue(cast<StringLiteral>(E));
831   case Expr::ObjCEncodeExprClass:
832     return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
833   case Expr::PseudoObjectExprClass:
834     return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
835   case Expr::InitListExprClass:
836     return EmitInitListLValue(cast<InitListExpr>(E));
837   case Expr::CXXTemporaryObjectExprClass:
838   case Expr::CXXConstructExprClass:
839     return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
840   case Expr::CXXBindTemporaryExprClass:
841     return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
842   case Expr::CXXUuidofExprClass:
843     return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
844   case Expr::LambdaExprClass:
845     return EmitLambdaLValue(cast<LambdaExpr>(E));
846 
847   case Expr::ExprWithCleanupsClass: {
848     const ExprWithCleanups *cleanups = cast<ExprWithCleanups>(E);
849     enterFullExpression(cleanups);
850     RunCleanupsScope Scope(*this);
851     return EmitLValue(cleanups->getSubExpr());
852   }
853 
854   case Expr::CXXScalarValueInitExprClass:
855     return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E));
856   case Expr::CXXDefaultArgExprClass:
857     return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
858   case Expr::CXXTypeidExprClass:
859     return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
860 
861   case Expr::ObjCMessageExprClass:
862     return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
863   case Expr::ObjCIvarRefExprClass:
864     return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
865   case Expr::StmtExprClass:
866     return EmitStmtExprLValue(cast<StmtExpr>(E));
867   case Expr::UnaryOperatorClass:
868     return EmitUnaryOpLValue(cast<UnaryOperator>(E));
869   case Expr::ArraySubscriptExprClass:
870     return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
871   case Expr::ExtVectorElementExprClass:
872     return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
873   case Expr::MemberExprClass:
874     return EmitMemberExpr(cast<MemberExpr>(E));
875   case Expr::CompoundLiteralExprClass:
876     return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
877   case Expr::ConditionalOperatorClass:
878     return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
879   case Expr::BinaryConditionalOperatorClass:
880     return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
881   case Expr::ChooseExprClass:
882     return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
883   case Expr::OpaqueValueExprClass:
884     return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
885   case Expr::SubstNonTypeTemplateParmExprClass:
886     return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
887   case Expr::ImplicitCastExprClass:
888   case Expr::CStyleCastExprClass:
889   case Expr::CXXFunctionalCastExprClass:
890   case Expr::CXXStaticCastExprClass:
891   case Expr::CXXDynamicCastExprClass:
892   case Expr::CXXReinterpretCastExprClass:
893   case Expr::CXXConstCastExprClass:
894   case Expr::ObjCBridgedCastExprClass:
895     return EmitCastLValue(cast<CastExpr>(E));
896 
897   case Expr::MaterializeTemporaryExprClass:
898     return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
899   }
900 }
901 
902 /// Given an object of the given canonical type, can we safely copy a
903 /// value out of it based on its initializer?
904 static bool isConstantEmittableObjectType(QualType type) {
905   assert(type.isCanonical());
906   assert(!type->isReferenceType());
907 
908   // Must be const-qualified but non-volatile.
909   Qualifiers qs = type.getLocalQualifiers();
910   if (!qs.hasConst() || qs.hasVolatile()) return false;
911 
912   // Otherwise, all object types satisfy this except C++ classes with
913   // mutable subobjects or non-trivial copy/destroy behavior.
914   if (const RecordType *RT = dyn_cast<RecordType>(type))
915     if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
916       if (RD->hasMutableFields() || !RD->isTrivial())
917         return false;
918 
919   return true;
920 }
921 
922 /// Can we constant-emit a load of a reference to a variable of the
923 /// given type?  This is different from predicates like
924 /// Decl::isUsableInConstantExpressions because we do want it to apply
925 /// in situations that don't necessarily satisfy the language's rules
926 /// for this (e.g. C++'s ODR-use rules).  For example, we want to able
927 /// to do this with const float variables even if those variables
928 /// aren't marked 'constexpr'.
929 enum ConstantEmissionKind {
930   CEK_None,
931   CEK_AsReferenceOnly,
932   CEK_AsValueOrReference,
933   CEK_AsValueOnly
934 };
935 static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) {
936   type = type.getCanonicalType();
937   if (const ReferenceType *ref = dyn_cast<ReferenceType>(type)) {
938     if (isConstantEmittableObjectType(ref->getPointeeType()))
939       return CEK_AsValueOrReference;
940     return CEK_AsReferenceOnly;
941   }
942   if (isConstantEmittableObjectType(type))
943     return CEK_AsValueOnly;
944   return CEK_None;
945 }
946 
947 /// Try to emit a reference to the given value without producing it as
948 /// an l-value.  This is actually more than an optimization: we can't
949 /// produce an l-value for variables that we never actually captured
950 /// in a block or lambda, which means const int variables or constexpr
951 /// literals or similar.
952 CodeGenFunction::ConstantEmission
953 CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) {
954   ValueDecl *value = refExpr->getDecl();
955 
956   // The value needs to be an enum constant or a constant variable.
957   ConstantEmissionKind CEK;
958   if (isa<ParmVarDecl>(value)) {
959     CEK = CEK_None;
960   } else if (VarDecl *var = dyn_cast<VarDecl>(value)) {
961     CEK = checkVarTypeForConstantEmission(var->getType());
962   } else if (isa<EnumConstantDecl>(value)) {
963     CEK = CEK_AsValueOnly;
964   } else {
965     CEK = CEK_None;
966   }
967   if (CEK == CEK_None) return ConstantEmission();
968 
969   Expr::EvalResult result;
970   bool resultIsReference;
971   QualType resultType;
972 
973   // It's best to evaluate all the way as an r-value if that's permitted.
974   if (CEK != CEK_AsReferenceOnly &&
975       refExpr->EvaluateAsRValue(result, getContext())) {
976     resultIsReference = false;
977     resultType = refExpr->getType();
978 
979   // Otherwise, try to evaluate as an l-value.
980   } else if (CEK != CEK_AsValueOnly &&
981              refExpr->EvaluateAsLValue(result, getContext())) {
982     resultIsReference = true;
983     resultType = value->getType();
984 
985   // Failure.
986   } else {
987     return ConstantEmission();
988   }
989 
990   // In any case, if the initializer has side-effects, abandon ship.
991   if (result.HasSideEffects)
992     return ConstantEmission();
993 
994   // Emit as a constant.
995   llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this);
996 
997   // Make sure we emit a debug reference to the global variable.
998   // This should probably fire even for
999   if (isa<VarDecl>(value)) {
1000     if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
1001       EmitDeclRefExprDbgValue(refExpr, C);
1002   } else {
1003     assert(isa<EnumConstantDecl>(value));
1004     EmitDeclRefExprDbgValue(refExpr, C);
1005   }
1006 
1007   // If we emitted a reference constant, we need to dereference that.
1008   if (resultIsReference)
1009     return ConstantEmission::forReference(C);
1010 
1011   return ConstantEmission::forValue(C);
1012 }
1013 
1014 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) {
1015   return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
1016                           lvalue.getAlignment().getQuantity(),
1017                           lvalue.getType(), lvalue.getTBAAInfo());
1018 }
1019 
1020 static bool hasBooleanRepresentation(QualType Ty) {
1021   if (Ty->isBooleanType())
1022     return true;
1023 
1024   if (const EnumType *ET = Ty->getAs<EnumType>())
1025     return ET->getDecl()->getIntegerType()->isBooleanType();
1026 
1027   if (const AtomicType *AT = Ty->getAs<AtomicType>())
1028     return hasBooleanRepresentation(AT->getValueType());
1029 
1030   return false;
1031 }
1032 
1033 llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
1034   const EnumType *ET = Ty->getAs<EnumType>();
1035   bool IsRegularCPlusPlusEnum = (getLangOpts().CPlusPlus && ET &&
1036                                  CGM.getCodeGenOpts().StrictEnums &&
1037                                  !ET->getDecl()->isFixed());
1038   bool IsBool = hasBooleanRepresentation(Ty);
1039   if (!IsBool && !IsRegularCPlusPlusEnum)
1040     return NULL;
1041 
1042   llvm::APInt Min;
1043   llvm::APInt End;
1044   if (IsBool) {
1045     Min = llvm::APInt(8, 0);
1046     End = llvm::APInt(8, 2);
1047   } else {
1048     const EnumDecl *ED = ET->getDecl();
1049     llvm::Type *LTy = ConvertTypeForMem(ED->getIntegerType());
1050     unsigned Bitwidth = LTy->getScalarSizeInBits();
1051     unsigned NumNegativeBits = ED->getNumNegativeBits();
1052     unsigned NumPositiveBits = ED->getNumPositiveBits();
1053 
1054     if (NumNegativeBits) {
1055       unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
1056       assert(NumBits <= Bitwidth);
1057       End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
1058       Min = -End;
1059     } else {
1060       assert(NumPositiveBits <= Bitwidth);
1061       End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
1062       Min = llvm::APInt(Bitwidth, 0);
1063     }
1064   }
1065 
1066   llvm::MDBuilder MDHelper(getLLVMContext());
1067   return MDHelper.createRange(Min, End);
1068 }
1069 
1070 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
1071                                               unsigned Alignment, QualType Ty,
1072                                               llvm::MDNode *TBAAInfo) {
1073 
1074   // For better performance, handle vector loads differently.
1075   if (Ty->isVectorType()) {
1076     llvm::Value *V;
1077     const llvm::Type *EltTy =
1078     cast<llvm::PointerType>(Addr->getType())->getElementType();
1079 
1080     const llvm::VectorType *VTy = cast<llvm::VectorType>(EltTy);
1081 
1082     // Handle vectors of size 3, like size 4 for better performance.
1083     if (VTy->getNumElements() == 3) {
1084 
1085       // Bitcast to vec4 type.
1086       llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(),
1087                                                          4);
1088       llvm::PointerType *ptVec4Ty =
1089       llvm::PointerType::get(vec4Ty,
1090                              (cast<llvm::PointerType>(
1091                                       Addr->getType()))->getAddressSpace());
1092       llvm::Value *Cast = Builder.CreateBitCast(Addr, ptVec4Ty,
1093                                                 "castToVec4");
1094       // Now load value.
1095       llvm::Value *LoadVal = Builder.CreateLoad(Cast, Volatile, "loadVec4");
1096 
1097       // Shuffle vector to get vec3.
1098       llvm::SmallVector<llvm::Constant*, 3> Mask;
1099       Mask.push_back(llvm::ConstantInt::get(
1100                                     llvm::Type::getInt32Ty(getLLVMContext()),
1101                                             0));
1102       Mask.push_back(llvm::ConstantInt::get(
1103                                     llvm::Type::getInt32Ty(getLLVMContext()),
1104                                             1));
1105       Mask.push_back(llvm::ConstantInt::get(
1106                                      llvm::Type::getInt32Ty(getLLVMContext()),
1107                                             2));
1108 
1109       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1110       V = Builder.CreateShuffleVector(LoadVal,
1111                                       llvm::UndefValue::get(vec4Ty),
1112                                       MaskV, "extractVec");
1113       return EmitFromMemory(V, Ty);
1114     }
1115   }
1116 
1117   llvm::LoadInst *Load = Builder.CreateLoad(Addr);
1118   if (Volatile)
1119     Load->setVolatile(true);
1120   if (Alignment)
1121     Load->setAlignment(Alignment);
1122   if (TBAAInfo)
1123     CGM.DecorateInstruction(Load, TBAAInfo);
1124   // If this is an atomic type, all normal reads must be atomic
1125   if (Ty->isAtomicType())
1126     Load->setAtomic(llvm::SequentiallyConsistent);
1127 
1128   if (CGM.getCodeGenOpts().OptimizationLevel > 0)
1129     if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
1130       Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
1131 
1132   return EmitFromMemory(Load, Ty);
1133 }
1134 
1135 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
1136   // Bool has a different representation in memory than in registers.
1137   if (hasBooleanRepresentation(Ty)) {
1138     // This should really always be an i1, but sometimes it's already
1139     // an i8, and it's awkward to track those cases down.
1140     if (Value->getType()->isIntegerTy(1))
1141       return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool");
1142     assert(Value->getType()->isIntegerTy(8) && "value rep of bool not i1/i8");
1143   }
1144 
1145   return Value;
1146 }
1147 
1148 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
1149   // Bool has a different representation in memory than in registers.
1150   if (hasBooleanRepresentation(Ty)) {
1151     assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8");
1152     return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
1153   }
1154 
1155   return Value;
1156 }
1157 
1158 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
1159                                         bool Volatile, unsigned Alignment,
1160                                         QualType Ty,
1161                                         llvm::MDNode *TBAAInfo,
1162                                         bool isInit) {
1163 
1164   // Handle vectors differently to get better performance.
1165   if (Ty->isVectorType()) {
1166     llvm::Type *SrcTy = Value->getType();
1167     llvm::VectorType *VecTy = cast<llvm::VectorType>(SrcTy);
1168     // Handle vec3 special.
1169     if (VecTy->getNumElements() == 3) {
1170       llvm::LLVMContext &VMContext = getLLVMContext();
1171 
1172       // Our source is a vec3, do a shuffle vector to make it a vec4.
1173       llvm::SmallVector<llvm::Constant*, 4> Mask;
1174       Mask.push_back(llvm::ConstantInt::get(
1175                                             llvm::Type::getInt32Ty(VMContext),
1176                                             0));
1177       Mask.push_back(llvm::ConstantInt::get(
1178                                             llvm::Type::getInt32Ty(VMContext),
1179                                             1));
1180       Mask.push_back(llvm::ConstantInt::get(
1181                                             llvm::Type::getInt32Ty(VMContext),
1182                                             2));
1183       Mask.push_back(llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)));
1184 
1185       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1186       Value = Builder.CreateShuffleVector(Value,
1187                                           llvm::UndefValue::get(VecTy),
1188                                           MaskV, "extractVec");
1189       SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4);
1190     }
1191     llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
1192     if (DstPtr->getElementType() != SrcTy) {
1193       llvm::Type *MemTy =
1194       llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
1195       Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
1196     }
1197   }
1198 
1199   Value = EmitToMemory(Value, Ty);
1200 
1201   llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
1202   if (Alignment)
1203     Store->setAlignment(Alignment);
1204   if (TBAAInfo)
1205     CGM.DecorateInstruction(Store, TBAAInfo);
1206   if (!isInit && Ty->isAtomicType())
1207     Store->setAtomic(llvm::SequentiallyConsistent);
1208 }
1209 
1210 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
1211     bool isInit) {
1212   EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
1213                     lvalue.getAlignment().getQuantity(), lvalue.getType(),
1214                     lvalue.getTBAAInfo(), isInit);
1215 }
1216 
1217 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
1218 /// method emits the address of the lvalue, then loads the result as an rvalue,
1219 /// returning the rvalue.
1220 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) {
1221   if (LV.isObjCWeak()) {
1222     // load of a __weak object.
1223     llvm::Value *AddrWeakObj = LV.getAddress();
1224     return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
1225                                                              AddrWeakObj));
1226   }
1227   if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak)
1228     return RValue::get(EmitARCLoadWeak(LV.getAddress()));
1229 
1230   if (LV.isSimple()) {
1231     assert(!LV.getType()->isFunctionType());
1232 
1233     // Everything needs a load.
1234     return RValue::get(EmitLoadOfScalar(LV));
1235   }
1236 
1237   if (LV.isVectorElt()) {
1238     llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(),
1239                                               LV.isVolatileQualified());
1240     Load->setAlignment(LV.getAlignment().getQuantity());
1241     return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
1242                                                     "vecext"));
1243   }
1244 
1245   // If this is a reference to a subset of the elements of a vector, either
1246   // shuffle the input or extract/insert them as appropriate.
1247   if (LV.isExtVectorElt())
1248     return EmitLoadOfExtVectorElementLValue(LV);
1249 
1250   assert(LV.isBitField() && "Unknown LValue type!");
1251   return EmitLoadOfBitfieldLValue(LV);
1252 }
1253 
1254 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
1255   const CGBitFieldInfo &Info = LV.getBitFieldInfo();
1256 
1257   // Get the output type.
1258   llvm::Type *ResLTy = ConvertType(LV.getType());
1259   unsigned ResSizeInBits = CGM.getDataLayout().getTypeSizeInBits(ResLTy);
1260 
1261   // Compute the result as an OR of all of the individual component accesses.
1262   llvm::Value *Res = 0;
1263   for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1264     const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1265     CharUnits AccessAlignment = AI.AccessAlignment;
1266     if (!LV.getAlignment().isZero())
1267       AccessAlignment = std::min(AccessAlignment, LV.getAlignment());
1268 
1269     // Get the field pointer.
1270     llvm::Value *Ptr = LV.getBitFieldBaseAddr();
1271 
1272     // Only offset by the field index if used, so that incoming values are not
1273     // required to be structures.
1274     if (AI.FieldIndex)
1275       Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
1276 
1277     // Offset by the byte offset, if used.
1278     if (!AI.FieldByteOffset.isZero()) {
1279       Ptr = EmitCastToVoidPtr(Ptr);
1280       Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
1281                                        "bf.field.offs");
1282     }
1283 
1284     // Cast to the access type.
1285     llvm::Type *PTy = llvm::Type::getIntNPtrTy(getLLVMContext(), AI.AccessWidth,
1286                        CGM.getContext().getTargetAddressSpace(LV.getType()));
1287     Ptr = Builder.CreateBitCast(Ptr, PTy);
1288 
1289     // Perform the load.
1290     llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
1291     Load->setAlignment(AccessAlignment.getQuantity());
1292 
1293     // Shift out unused low bits and mask out unused high bits.
1294     llvm::Value *Val = Load;
1295     if (AI.FieldBitStart)
1296       Val = Builder.CreateLShr(Load, AI.FieldBitStart);
1297     Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
1298                                                             AI.TargetBitWidth),
1299                             "bf.clear");
1300 
1301     // Extend or truncate to the target size.
1302     if (AI.AccessWidth < ResSizeInBits)
1303       Val = Builder.CreateZExt(Val, ResLTy);
1304     else if (AI.AccessWidth > ResSizeInBits)
1305       Val = Builder.CreateTrunc(Val, ResLTy);
1306 
1307     // Shift into place, and OR into the result.
1308     if (AI.TargetBitOffset)
1309       Val = Builder.CreateShl(Val, AI.TargetBitOffset);
1310     Res = Res ? Builder.CreateOr(Res, Val) : Val;
1311   }
1312 
1313   // If the bit-field is signed, perform the sign-extension.
1314   //
1315   // FIXME: This can easily be folded into the load of the high bits, which
1316   // could also eliminate the mask of high bits in some situations.
1317   if (Info.isSigned()) {
1318     unsigned ExtraBits = ResSizeInBits - Info.getSize();
1319     if (ExtraBits)
1320       Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
1321                                ExtraBits, "bf.val.sext");
1322   }
1323 
1324   return RValue::get(Res);
1325 }
1326 
1327 // If this is a reference to a subset of the elements of a vector, create an
1328 // appropriate shufflevector.
1329 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
1330   llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(),
1331                                             LV.isVolatileQualified());
1332   Load->setAlignment(LV.getAlignment().getQuantity());
1333   llvm::Value *Vec = Load;
1334 
1335   const llvm::Constant *Elts = LV.getExtVectorElts();
1336 
1337   // If the result of the expression is a non-vector type, we must be extracting
1338   // a single element.  Just codegen as an extractelement.
1339   const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
1340   if (!ExprVT) {
1341     unsigned InIdx = getAccessedFieldNo(0, Elts);
1342     llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1343     return RValue::get(Builder.CreateExtractElement(Vec, Elt));
1344   }
1345 
1346   // Always use shuffle vector to try to retain the original program structure
1347   unsigned NumResultElts = ExprVT->getNumElements();
1348 
1349   SmallVector<llvm::Constant*, 4> Mask;
1350   for (unsigned i = 0; i != NumResultElts; ++i)
1351     Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts)));
1352 
1353   llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1354   Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
1355                                     MaskV);
1356   return RValue::get(Vec);
1357 }
1358 
1359 
1360 
1361 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
1362 /// lvalue, where both are guaranteed to the have the same type, and that type
1363 /// is 'Ty'.
1364 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit) {
1365   if (!Dst.isSimple()) {
1366     if (Dst.isVectorElt()) {
1367       // Read/modify/write the vector, inserting the new element.
1368       llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(),
1369                                                 Dst.isVolatileQualified());
1370       Load->setAlignment(Dst.getAlignment().getQuantity());
1371       llvm::Value *Vec = Load;
1372       Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
1373                                         Dst.getVectorIdx(), "vecins");
1374       llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(),
1375                                                    Dst.isVolatileQualified());
1376       Store->setAlignment(Dst.getAlignment().getQuantity());
1377       return;
1378     }
1379 
1380     // If this is an update of extended vector elements, insert them as
1381     // appropriate.
1382     if (Dst.isExtVectorElt())
1383       return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
1384 
1385     assert(Dst.isBitField() && "Unknown LValue type");
1386     return EmitStoreThroughBitfieldLValue(Src, Dst);
1387   }
1388 
1389   // There's special magic for assigning into an ARC-qualified l-value.
1390   if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
1391     switch (Lifetime) {
1392     case Qualifiers::OCL_None:
1393       llvm_unreachable("present but none");
1394 
1395     case Qualifiers::OCL_ExplicitNone:
1396       // nothing special
1397       break;
1398 
1399     case Qualifiers::OCL_Strong:
1400       EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
1401       return;
1402 
1403     case Qualifiers::OCL_Weak:
1404       EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
1405       return;
1406 
1407     case Qualifiers::OCL_Autoreleasing:
1408       Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
1409                                                      Src.getScalarVal()));
1410       // fall into the normal path
1411       break;
1412     }
1413   }
1414 
1415   if (Dst.isObjCWeak() && !Dst.isNonGC()) {
1416     // load of a __weak object.
1417     llvm::Value *LvalueDst = Dst.getAddress();
1418     llvm::Value *src = Src.getScalarVal();
1419      CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
1420     return;
1421   }
1422 
1423   if (Dst.isObjCStrong() && !Dst.isNonGC()) {
1424     // load of a __strong object.
1425     llvm::Value *LvalueDst = Dst.getAddress();
1426     llvm::Value *src = Src.getScalarVal();
1427     if (Dst.isObjCIvar()) {
1428       assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1429       llvm::Type *ResultType = ConvertType(getContext().LongTy);
1430       llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1431       llvm::Value *dst = RHS;
1432       RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1433       llvm::Value *LHS =
1434         Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1435       llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1436       CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1437                                               BytesBetween);
1438     } else if (Dst.isGlobalObjCRef()) {
1439       CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1440                                                 Dst.isThreadLocalRef());
1441     }
1442     else
1443       CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1444     return;
1445   }
1446 
1447   assert(Src.isScalar() && "Can't emit an agg store with this method");
1448   EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
1449 }
1450 
1451 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1452                                                      llvm::Value **Result) {
1453   const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1454 
1455   // Get the output type.
1456   llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1457   unsigned ResSizeInBits = CGM.getDataLayout().getTypeSizeInBits(ResLTy);
1458 
1459   // Get the source value, truncated to the width of the bit-field.
1460   llvm::Value *SrcVal = Src.getScalarVal();
1461 
1462   if (hasBooleanRepresentation(Dst.getType()))
1463     SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
1464 
1465   SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
1466                                                                 Info.getSize()),
1467                              "bf.value");
1468 
1469   // Return the new value of the bit-field, if requested.
1470   if (Result) {
1471     // Cast back to the proper type for result.
1472     llvm::Type *SrcTy = Src.getScalarVal()->getType();
1473     llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
1474                                                    "bf.reload.val");
1475 
1476     // Sign extend if necessary.
1477     if (Info.isSigned()) {
1478       unsigned ExtraBits = ResSizeInBits - Info.getSize();
1479       if (ExtraBits)
1480         ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
1481                                        ExtraBits, "bf.reload.sext");
1482     }
1483 
1484     *Result = ReloadVal;
1485   }
1486 
1487   // Iterate over the components, writing each piece to memory.
1488   for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1489     const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1490     CharUnits AccessAlignment = AI.AccessAlignment;
1491     if (!Dst.getAlignment().isZero())
1492       AccessAlignment = std::min(AccessAlignment, Dst.getAlignment());
1493 
1494     // Get the field pointer.
1495     llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
1496     unsigned addressSpace =
1497       cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1498 
1499     // Only offset by the field index if used, so that incoming values are not
1500     // required to be structures.
1501     if (AI.FieldIndex)
1502       Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
1503 
1504     // Offset by the byte offset, if used.
1505     if (!AI.FieldByteOffset.isZero()) {
1506       Ptr = EmitCastToVoidPtr(Ptr);
1507       Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
1508                                        "bf.field.offs");
1509     }
1510 
1511     // Cast to the access type.
1512     llvm::Type *AccessLTy =
1513       llvm::Type::getIntNTy(getLLVMContext(), AI.AccessWidth);
1514 
1515     llvm::Type *PTy = AccessLTy->getPointerTo(addressSpace);
1516     Ptr = Builder.CreateBitCast(Ptr, PTy);
1517 
1518     // Extract the piece of the bit-field value to write in this access, limited
1519     // to the values that are part of this access.
1520     llvm::Value *Val = SrcVal;
1521     if (AI.TargetBitOffset)
1522       Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
1523     Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
1524                                                             AI.TargetBitWidth));
1525 
1526     // Extend or truncate to the access size.
1527     if (ResSizeInBits < AI.AccessWidth)
1528       Val = Builder.CreateZExt(Val, AccessLTy);
1529     else if (ResSizeInBits > AI.AccessWidth)
1530       Val = Builder.CreateTrunc(Val, AccessLTy);
1531 
1532     // Shift into the position in memory.
1533     if (AI.FieldBitStart)
1534       Val = Builder.CreateShl(Val, AI.FieldBitStart);
1535 
1536     // If necessary, load and OR in bits that are outside of the bit-field.
1537     if (AI.TargetBitWidth != AI.AccessWidth) {
1538       llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
1539       Load->setAlignment(AccessAlignment.getQuantity());
1540 
1541       // Compute the mask for zeroing the bits that are part of the bit-field.
1542       llvm::APInt InvMask =
1543         ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
1544                                  AI.FieldBitStart + AI.TargetBitWidth);
1545 
1546       // Apply the mask and OR in to the value to write.
1547       Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
1548     }
1549 
1550     // Write the value.
1551     llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
1552                                                  Dst.isVolatileQualified());
1553     Store->setAlignment(AccessAlignment.getQuantity());
1554   }
1555 }
1556 
1557 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1558                                                                LValue Dst) {
1559   // This access turns into a read/modify/write of the vector.  Load the input
1560   // value now.
1561   llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(),
1562                                             Dst.isVolatileQualified());
1563   Load->setAlignment(Dst.getAlignment().getQuantity());
1564   llvm::Value *Vec = Load;
1565   const llvm::Constant *Elts = Dst.getExtVectorElts();
1566 
1567   llvm::Value *SrcVal = Src.getScalarVal();
1568 
1569   if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1570     unsigned NumSrcElts = VTy->getNumElements();
1571     unsigned NumDstElts =
1572        cast<llvm::VectorType>(Vec->getType())->getNumElements();
1573     if (NumDstElts == NumSrcElts) {
1574       // Use shuffle vector is the src and destination are the same number of
1575       // elements and restore the vector mask since it is on the side it will be
1576       // stored.
1577       SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1578       for (unsigned i = 0; i != NumSrcElts; ++i)
1579         Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i);
1580 
1581       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1582       Vec = Builder.CreateShuffleVector(SrcVal,
1583                                         llvm::UndefValue::get(Vec->getType()),
1584                                         MaskV);
1585     } else if (NumDstElts > NumSrcElts) {
1586       // Extended the source vector to the same length and then shuffle it
1587       // into the destination.
1588       // FIXME: since we're shuffling with undef, can we just use the indices
1589       //        into that?  This could be simpler.
1590       SmallVector<llvm::Constant*, 4> ExtMask;
1591       for (unsigned i = 0; i != NumSrcElts; ++i)
1592         ExtMask.push_back(Builder.getInt32(i));
1593       ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty));
1594       llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1595       llvm::Value *ExtSrcVal =
1596         Builder.CreateShuffleVector(SrcVal,
1597                                     llvm::UndefValue::get(SrcVal->getType()),
1598                                     ExtMaskV);
1599       // build identity
1600       SmallVector<llvm::Constant*, 4> Mask;
1601       for (unsigned i = 0; i != NumDstElts; ++i)
1602         Mask.push_back(Builder.getInt32(i));
1603 
1604       // modify when what gets shuffled in
1605       for (unsigned i = 0; i != NumSrcElts; ++i)
1606         Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts);
1607       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1608       Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
1609     } else {
1610       // We should never shorten the vector
1611       llvm_unreachable("unexpected shorten vector length");
1612     }
1613   } else {
1614     // If the Src is a scalar (not a vector) it must be updating one element.
1615     unsigned InIdx = getAccessedFieldNo(0, Elts);
1616     llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1617     Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
1618   }
1619 
1620   llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(),
1621                                                Dst.isVolatileQualified());
1622   Store->setAlignment(Dst.getAlignment().getQuantity());
1623 }
1624 
1625 // setObjCGCLValueClass - sets class of he lvalue for the purpose of
1626 // generating write-barries API. It is currently a global, ivar,
1627 // or neither.
1628 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1629                                  LValue &LV,
1630                                  bool IsMemberAccess=false) {
1631   if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
1632     return;
1633 
1634   if (isa<ObjCIvarRefExpr>(E)) {
1635     QualType ExpTy = E->getType();
1636     if (IsMemberAccess && ExpTy->isPointerType()) {
1637       // If ivar is a structure pointer, assigning to field of
1638       // this struct follows gcc's behavior and makes it a non-ivar
1639       // writer-barrier conservatively.
1640       ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1641       if (ExpTy->isRecordType()) {
1642         LV.setObjCIvar(false);
1643         return;
1644       }
1645     }
1646     LV.setObjCIvar(true);
1647     ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1648     LV.setBaseIvarExp(Exp->getBase());
1649     LV.setObjCArray(E->getType()->isArrayType());
1650     return;
1651   }
1652 
1653   if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1654     if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1655       if (VD->hasGlobalStorage()) {
1656         LV.setGlobalObjCRef(true);
1657         LV.setThreadLocalRef(VD->isThreadSpecified());
1658       }
1659     }
1660     LV.setObjCArray(E->getType()->isArrayType());
1661     return;
1662   }
1663 
1664   if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1665     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1666     return;
1667   }
1668 
1669   if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1670     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1671     if (LV.isObjCIvar()) {
1672       // If cast is to a structure pointer, follow gcc's behavior and make it
1673       // a non-ivar write-barrier.
1674       QualType ExpTy = E->getType();
1675       if (ExpTy->isPointerType())
1676         ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1677       if (ExpTy->isRecordType())
1678         LV.setObjCIvar(false);
1679     }
1680     return;
1681   }
1682 
1683   if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1684     setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1685     return;
1686   }
1687 
1688   if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1689     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1690     return;
1691   }
1692 
1693   if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1694     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1695     return;
1696   }
1697 
1698   if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1699     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1700     return;
1701   }
1702 
1703   if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1704     setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1705     if (LV.isObjCIvar() && !LV.isObjCArray())
1706       // Using array syntax to assigning to what an ivar points to is not
1707       // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1708       LV.setObjCIvar(false);
1709     else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1710       // Using array syntax to assigning to what global points to is not
1711       // same as assigning to the global itself. {id *G;} G[i] = 0;
1712       LV.setGlobalObjCRef(false);
1713     return;
1714   }
1715 
1716   if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1717     setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
1718     // We don't know if member is an 'ivar', but this flag is looked at
1719     // only in the context of LV.isObjCIvar().
1720     LV.setObjCArray(E->getType()->isArrayType());
1721     return;
1722   }
1723 }
1724 
1725 static llvm::Value *
1726 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1727                                 llvm::Value *V, llvm::Type *IRType,
1728                                 StringRef Name = StringRef()) {
1729   unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1730   return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1731 }
1732 
1733 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1734                                       const Expr *E, const VarDecl *VD) {
1735   assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
1736          "Var decl must have external storage or be a file var decl!");
1737 
1738   llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1739   llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
1740   V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
1741   CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
1742   QualType T = E->getType();
1743   LValue LV;
1744   if (VD->getType()->isReferenceType()) {
1745     llvm::LoadInst *LI = CGF.Builder.CreateLoad(V);
1746     LI->setAlignment(Alignment.getQuantity());
1747     V = LI;
1748     LV = CGF.MakeNaturalAlignAddrLValue(V, T);
1749   } else {
1750     LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1751   }
1752   setObjCGCLValueClass(CGF.getContext(), E, LV);
1753   return LV;
1754 }
1755 
1756 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1757                                      const Expr *E, const FunctionDecl *FD) {
1758   llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1759   if (!FD->hasPrototype()) {
1760     if (const FunctionProtoType *Proto =
1761             FD->getType()->getAs<FunctionProtoType>()) {
1762       // Ugly case: for a K&R-style definition, the type of the definition
1763       // isn't the same as the type of a use.  Correct for this with a
1764       // bitcast.
1765       QualType NoProtoType =
1766           CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1767       NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1768       V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType));
1769     }
1770   }
1771   CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
1772   return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1773 }
1774 
1775 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1776   const NamedDecl *ND = E->getDecl();
1777   CharUnits Alignment = getContext().getDeclAlign(ND);
1778   QualType T = E->getType();
1779 
1780   // A DeclRefExpr for a reference initialized by a constant expression can
1781   // appear without being odr-used. Directly emit the constant initializer.
1782   if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1783     const Expr *Init = VD->getAnyInitializer(VD);
1784     if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() &&
1785         VD->isUsableInConstantExpressions(getContext()) &&
1786         VD->checkInitIsICE()) {
1787       llvm::Constant *Val =
1788         CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this);
1789       assert(Val && "failed to emit reference constant expression");
1790       // FIXME: Eventually we will want to emit vector element references.
1791       return MakeAddrLValue(Val, T, Alignment);
1792     }
1793   }
1794 
1795   // FIXME: We should be able to assert this for FunctionDecls as well!
1796   // FIXME: We should be able to assert this for all DeclRefExprs, not just
1797   // those with a valid source location.
1798   assert((ND->isUsed(false) || !isa<VarDecl>(ND) ||
1799           !E->getLocation().isValid()) &&
1800          "Should not use decl without marking it used!");
1801 
1802   if (ND->hasAttr<WeakRefAttr>()) {
1803     const ValueDecl *VD = cast<ValueDecl>(ND);
1804     llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1805     return MakeAddrLValue(Aliasee, T, Alignment);
1806   }
1807 
1808   if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1809     // Check if this is a global variable.
1810     if (VD->hasExternalStorage() || VD->isFileVarDecl())
1811       return EmitGlobalVarDeclLValue(*this, E, VD);
1812 
1813     bool isBlockVariable = VD->hasAttr<BlocksAttr>();
1814 
1815     bool NonGCable = VD->hasLocalStorage() &&
1816                      !VD->getType()->isReferenceType() &&
1817                      !isBlockVariable;
1818 
1819     llvm::Value *V = LocalDeclMap[VD];
1820     if (!V && VD->isStaticLocal())
1821       V = CGM.getStaticLocalDeclAddress(VD);
1822 
1823     // Use special handling for lambdas.
1824     if (!V) {
1825       if (FieldDecl *FD = LambdaCaptureFields.lookup(VD)) {
1826         QualType LambdaTagType = getContext().getTagDeclType(FD->getParent());
1827         LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue,
1828                                                      LambdaTagType);
1829         return EmitLValueForField(LambdaLV, FD);
1830       }
1831 
1832       assert(isa<BlockDecl>(CurCodeDecl) && E->refersToEnclosingLocal());
1833       return MakeAddrLValue(GetAddrOfBlockDecl(VD, isBlockVariable),
1834                             T, Alignment);
1835     }
1836 
1837     assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1838 
1839     if (isBlockVariable)
1840       V = BuildBlockByrefAddress(V, VD);
1841 
1842     LValue LV;
1843     if (VD->getType()->isReferenceType()) {
1844       llvm::LoadInst *LI = Builder.CreateLoad(V);
1845       LI->setAlignment(Alignment.getQuantity());
1846       V = LI;
1847       LV = MakeNaturalAlignAddrLValue(V, T);
1848     } else {
1849       LV = MakeAddrLValue(V, T, Alignment);
1850     }
1851 
1852     if (NonGCable) {
1853       LV.getQuals().removeObjCGCAttr();
1854       LV.setNonGC(true);
1855     }
1856     setObjCGCLValueClass(getContext(), E, LV);
1857     return LV;
1858   }
1859 
1860   if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND))
1861     return EmitFunctionDeclLValue(*this, E, fn);
1862 
1863   llvm_unreachable("Unhandled DeclRefExpr");
1864 }
1865 
1866 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1867   // __extension__ doesn't affect lvalue-ness.
1868   if (E->getOpcode() == UO_Extension)
1869     return EmitLValue(E->getSubExpr());
1870 
1871   QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1872   switch (E->getOpcode()) {
1873   default: llvm_unreachable("Unknown unary operator lvalue!");
1874   case UO_Deref: {
1875     QualType T = E->getSubExpr()->getType()->getPointeeType();
1876     assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1877 
1878     LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1879     LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1880 
1881     // We should not generate __weak write barrier on indirect reference
1882     // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1883     // But, we continue to generate __strong write barrier on indirect write
1884     // into a pointer to object.
1885     if (getContext().getLangOpts().ObjC1 &&
1886         getContext().getLangOpts().getGC() != LangOptions::NonGC &&
1887         LV.isObjCWeak())
1888       LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1889     return LV;
1890   }
1891   case UO_Real:
1892   case UO_Imag: {
1893     LValue LV = EmitLValue(E->getSubExpr());
1894     assert(LV.isSimple() && "real/imag on non-ordinary l-value");
1895     llvm::Value *Addr = LV.getAddress();
1896 
1897     // __real is valid on scalars.  This is a faster way of testing that.
1898     // __imag can only produce an rvalue on scalars.
1899     if (E->getOpcode() == UO_Real &&
1900         !cast<llvm::PointerType>(Addr->getType())
1901            ->getElementType()->isStructTy()) {
1902       assert(E->getSubExpr()->getType()->isArithmeticType());
1903       return LV;
1904     }
1905 
1906     assert(E->getSubExpr()->getType()->isAnyComplexType());
1907 
1908     unsigned Idx = E->getOpcode() == UO_Imag;
1909     return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1910                                                   Idx, "idx"),
1911                           ExprTy);
1912   }
1913   case UO_PreInc:
1914   case UO_PreDec: {
1915     LValue LV = EmitLValue(E->getSubExpr());
1916     bool isInc = E->getOpcode() == UO_PreInc;
1917 
1918     if (E->getType()->isAnyComplexType())
1919       EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1920     else
1921       EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1922     return LV;
1923   }
1924   }
1925 }
1926 
1927 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1928   return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1929                         E->getType());
1930 }
1931 
1932 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1933   return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1934                         E->getType());
1935 }
1936 
1937 static llvm::Constant*
1938 GetAddrOfConstantWideString(StringRef Str,
1939                             const char *GlobalName,
1940                             ASTContext &Context,
1941                             QualType Ty, SourceLocation Loc,
1942                             CodeGenModule &CGM) {
1943 
1944   StringLiteral *SL = StringLiteral::Create(Context,
1945                                             Str,
1946                                             StringLiteral::Wide,
1947                                             /*Pascal = */false,
1948                                             Ty, Loc);
1949   llvm::Constant *C = CGM.GetConstantArrayFromStringLiteral(SL);
1950   llvm::GlobalVariable *GV =
1951     new llvm::GlobalVariable(CGM.getModule(), C->getType(),
1952                              !CGM.getLangOpts().WritableStrings,
1953                              llvm::GlobalValue::PrivateLinkage,
1954                              C, GlobalName);
1955   const unsigned WideAlignment =
1956     Context.getTypeAlignInChars(Ty).getQuantity();
1957   GV->setAlignment(WideAlignment);
1958   return GV;
1959 }
1960 
1961 static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source,
1962                                     SmallString<32>& Target) {
1963   Target.resize(CharByteWidth * (Source.size() + 1));
1964   char *ResultPtr = &Target[0];
1965   const UTF8 *ErrorPtr;
1966   bool success = ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr);
1967   (void)success;
1968   assert(success);
1969   Target.resize(ResultPtr - &Target[0]);
1970 }
1971 
1972 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1973   switch (E->getIdentType()) {
1974   default:
1975     return EmitUnsupportedLValue(E, "predefined expression");
1976 
1977   case PredefinedExpr::Func:
1978   case PredefinedExpr::Function:
1979   case PredefinedExpr::LFunction:
1980   case PredefinedExpr::PrettyFunction: {
1981     unsigned IdentType = E->getIdentType();
1982     std::string GlobalVarName;
1983 
1984     switch (IdentType) {
1985     default: llvm_unreachable("Invalid type");
1986     case PredefinedExpr::Func:
1987       GlobalVarName = "__func__.";
1988       break;
1989     case PredefinedExpr::Function:
1990       GlobalVarName = "__FUNCTION__.";
1991       break;
1992     case PredefinedExpr::LFunction:
1993       GlobalVarName = "L__FUNCTION__.";
1994       break;
1995     case PredefinedExpr::PrettyFunction:
1996       GlobalVarName = "__PRETTY_FUNCTION__.";
1997       break;
1998     }
1999 
2000     StringRef FnName = CurFn->getName();
2001     if (FnName.startswith("\01"))
2002       FnName = FnName.substr(1);
2003     GlobalVarName += FnName;
2004 
2005     const Decl *CurDecl = CurCodeDecl;
2006     if (CurDecl == 0)
2007       CurDecl = getContext().getTranslationUnitDecl();
2008 
2009     std::string FunctionName =
2010         (isa<BlockDecl>(CurDecl)
2011          ? FnName.str()
2012          : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)IdentType,
2013                                        CurDecl));
2014 
2015     const Type* ElemType = E->getType()->getArrayElementTypeNoTypeQual();
2016     llvm::Constant *C;
2017     if (ElemType->isWideCharType()) {
2018       SmallString<32> RawChars;
2019       ConvertUTF8ToWideString(
2020           getContext().getTypeSizeInChars(ElemType).getQuantity(),
2021           FunctionName, RawChars);
2022       C = GetAddrOfConstantWideString(RawChars,
2023                                       GlobalVarName.c_str(),
2024                                       getContext(),
2025                                       E->getType(),
2026                                       E->getLocation(),
2027                                       CGM);
2028     } else {
2029       C = CGM.GetAddrOfConstantCString(FunctionName,
2030                                        GlobalVarName.c_str(),
2031                                        1);
2032     }
2033     return MakeAddrLValue(C, E->getType());
2034   }
2035   }
2036 }
2037 
2038 /// Emit a type description suitable for use by a runtime sanitizer library. The
2039 /// format of a type descriptor is
2040 ///
2041 /// \code
2042 ///   { i16 TypeKind, i16 TypeInfo }
2043 /// \endcode
2044 ///
2045 /// followed by an array of i8 containing the type name. TypeKind is 0 for an
2046 /// integer, 1 for a floating point value, and -1 for anything else.
2047 llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
2048   // FIXME: Only emit each type's descriptor once.
2049   uint16_t TypeKind = -1;
2050   uint16_t TypeInfo = 0;
2051 
2052   if (T->isIntegerType()) {
2053     TypeKind = 0;
2054     TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
2055                T->isSignedIntegerType();
2056   } else if (T->isFloatingType()) {
2057     TypeKind = 1;
2058     TypeInfo = getContext().getTypeSize(T);
2059   }
2060 
2061   // Format the type name as if for a diagnostic, including quotes and
2062   // optionally an 'aka'.
2063   llvm::SmallString<32> Buffer;
2064   CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
2065                                     (intptr_t)T.getAsOpaquePtr(),
2066                                     0, 0, 0, 0, 0, 0, Buffer,
2067                                     ArrayRef<intptr_t>());
2068 
2069   llvm::Constant *Components[] = {
2070     Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
2071     llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
2072   };
2073   llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
2074 
2075   llvm::GlobalVariable *GV =
2076     new llvm::GlobalVariable(CGM.getModule(), Descriptor->getType(),
2077                              /*isConstant=*/true,
2078                              llvm::GlobalVariable::PrivateLinkage,
2079                              Descriptor);
2080   GV->setUnnamedAddr(true);
2081   return GV;
2082 }
2083 
2084 llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
2085   llvm::Type *TargetTy = IntPtrTy;
2086 
2087   // Integers which fit in intptr_t are zero-extended and passed directly.
2088   if (V->getType()->isIntegerTy() &&
2089       V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
2090     return Builder.CreateZExt(V, TargetTy);
2091 
2092   // Pointers are passed directly, everything else is passed by address.
2093   if (!V->getType()->isPointerTy()) {
2094     llvm::Value *Ptr = Builder.CreateAlloca(V->getType());
2095     Builder.CreateStore(V, Ptr);
2096     V = Ptr;
2097   }
2098   return Builder.CreatePtrToInt(V, TargetTy);
2099 }
2100 
2101 /// \brief Emit a representation of a SourceLocation for passing to a handler
2102 /// in a sanitizer runtime library. The format for this data is:
2103 /// \code
2104 ///   struct SourceLocation {
2105 ///     const char *Filename;
2106 ///     int32_t Line, Column;
2107 ///   };
2108 /// \endcode
2109 /// For an invalid SourceLocation, the Filename pointer is null.
2110 llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) {
2111   PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
2112 
2113   llvm::Constant *Data[] = {
2114     // FIXME: Only emit each file name once.
2115     PLoc.isValid() ? cast<llvm::Constant>(
2116                        Builder.CreateGlobalStringPtr(PLoc.getFilename()))
2117                    : llvm::Constant::getNullValue(Int8PtrTy),
2118     Builder.getInt32(PLoc.getLine()),
2119     Builder.getInt32(PLoc.getColumn())
2120   };
2121 
2122   return llvm::ConstantStruct::getAnon(Data);
2123 }
2124 
2125 void CodeGenFunction::EmitCheck(llvm::Value *Checked, StringRef CheckName,
2126                                 llvm::ArrayRef<llvm::Constant *> StaticArgs,
2127                                 llvm::ArrayRef<llvm::Value *> DynamicArgs,
2128                                 bool Recoverable) {
2129   llvm::BasicBlock *Cont = createBasicBlock("cont");
2130 
2131   // If -fcatch-undefined-behavior is not enabled, just emit a trap. This
2132   // happens when using -ftrapv.
2133   // FIXME: Should -ftrapv require the ubsan runtime library?
2134   if (!CatchUndefined) {
2135     // If we're optimizing, collapse all calls to trap down to just one per
2136     // function to save on code size.
2137     if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
2138       TrapBB = createBasicBlock("trap");
2139       Builder.CreateCondBr(Checked, Cont, TrapBB);
2140       EmitBlock(TrapBB);
2141       llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
2142       llvm::CallInst *TrapCall = Builder.CreateCall(F);
2143       TrapCall->setDoesNotReturn();
2144       TrapCall->setDoesNotThrow();
2145       Builder.CreateUnreachable();
2146     } else {
2147       Builder.CreateCondBr(Checked, Cont, TrapBB);
2148     }
2149 
2150     EmitBlock(Cont);
2151     return;
2152   }
2153 
2154   llvm::BasicBlock *Handler = createBasicBlock("handler." + CheckName);
2155   Builder.CreateCondBr(Checked, Cont, Handler);
2156   EmitBlock(Handler);
2157 
2158   llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
2159   llvm::GlobalValue *InfoPtr =
2160       new llvm::GlobalVariable(CGM.getModule(), Info->getType(), true,
2161                                llvm::GlobalVariable::PrivateLinkage, Info);
2162   InfoPtr->setUnnamedAddr(true);
2163 
2164   llvm::SmallVector<llvm::Value *, 4> Args;
2165   llvm::SmallVector<llvm::Type *, 4> ArgTypes;
2166   Args.reserve(DynamicArgs.size() + 1);
2167   ArgTypes.reserve(DynamicArgs.size() + 1);
2168 
2169   // Handler functions take an i8* pointing to the (handler-specific) static
2170   // information block, followed by a sequence of intptr_t arguments
2171   // representing operand values.
2172   Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
2173   ArgTypes.push_back(Int8PtrTy);
2174   for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
2175     Args.push_back(EmitCheckValue(DynamicArgs[i]));
2176     ArgTypes.push_back(IntPtrTy);
2177   }
2178 
2179   llvm::FunctionType *FnType =
2180     llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
2181   llvm::AttrBuilder B;
2182   if (!Recoverable) {
2183     B.addAttribute(llvm::Attributes::NoReturn)
2184      .addAttribute(llvm::Attributes::NoUnwind);
2185   }
2186   B.addAttribute(llvm::Attributes::UWTable);
2187   llvm::Value *Fn = CGM.CreateRuntimeFunction(FnType,
2188                                           ("__ubsan_handle_" + CheckName).str(),
2189                                          llvm::Attributes::get(getLLVMContext(),
2190                                                                B));
2191   llvm::CallInst *HandlerCall = Builder.CreateCall(Fn, Args);
2192   if (Recoverable) {
2193     Builder.CreateBr(Cont);
2194   } else {
2195     HandlerCall->setDoesNotReturn();
2196     HandlerCall->setDoesNotThrow();
2197     Builder.CreateUnreachable();
2198   }
2199 
2200   EmitBlock(Cont);
2201 }
2202 
2203 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
2204 /// array to pointer, return the array subexpression.
2205 static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
2206   // If this isn't just an array->pointer decay, bail out.
2207   const CastExpr *CE = dyn_cast<CastExpr>(E);
2208   if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
2209     return 0;
2210 
2211   // If this is a decay from variable width array, bail out.
2212   const Expr *SubExpr = CE->getSubExpr();
2213   if (SubExpr->getType()->isVariableArrayType())
2214     return 0;
2215 
2216   return SubExpr;
2217 }
2218 
2219 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
2220   // The index must always be an integer, which is not an aggregate.  Emit it.
2221   llvm::Value *Idx = EmitScalarExpr(E->getIdx());
2222   QualType IdxTy  = E->getIdx()->getType();
2223   bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
2224 
2225   // If the base is a vector type, then we are forming a vector element lvalue
2226   // with this subscript.
2227   if (E->getBase()->getType()->isVectorType()) {
2228     // Emit the vector as an lvalue to get its address.
2229     LValue LHS = EmitLValue(E->getBase());
2230     assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
2231     Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx");
2232     return LValue::MakeVectorElt(LHS.getAddress(), Idx,
2233                                  E->getBase()->getType(), LHS.getAlignment());
2234   }
2235 
2236   // Extend or truncate the index type to 32 or 64-bits.
2237   if (Idx->getType() != IntPtrTy)
2238     Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
2239 
2240   // We know that the pointer points to a type of the correct size, unless the
2241   // size is a VLA or Objective-C interface.
2242   llvm::Value *Address = 0;
2243   CharUnits ArrayAlignment;
2244   if (const VariableArrayType *vla =
2245         getContext().getAsVariableArrayType(E->getType())) {
2246     // The base must be a pointer, which is not an aggregate.  Emit
2247     // it.  It needs to be emitted first in case it's what captures
2248     // the VLA bounds.
2249     Address = EmitScalarExpr(E->getBase());
2250 
2251     // The element count here is the total number of non-VLA elements.
2252     llvm::Value *numElements = getVLASize(vla).first;
2253 
2254     // Effectively, the multiply by the VLA size is part of the GEP.
2255     // GEP indexes are signed, and scaling an index isn't permitted to
2256     // signed-overflow, so we use the same semantics for our explicit
2257     // multiply.  We suppress this if overflow is not undefined behavior.
2258     if (getLangOpts().isSignedOverflowDefined()) {
2259       Idx = Builder.CreateMul(Idx, numElements);
2260       Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2261     } else {
2262       Idx = Builder.CreateNSWMul(Idx, numElements);
2263       Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
2264     }
2265   } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
2266     // Indexing over an interface, as in "NSString *P; P[4];"
2267     llvm::Value *InterfaceSize =
2268       llvm::ConstantInt::get(Idx->getType(),
2269           getContext().getTypeSizeInChars(OIT).getQuantity());
2270 
2271     Idx = Builder.CreateMul(Idx, InterfaceSize);
2272 
2273     // The base must be a pointer, which is not an aggregate.  Emit it.
2274     llvm::Value *Base = EmitScalarExpr(E->getBase());
2275     Address = EmitCastToVoidPtr(Base);
2276     Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2277     Address = Builder.CreateBitCast(Address, Base->getType());
2278   } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
2279     // If this is A[i] where A is an array, the frontend will have decayed the
2280     // base to be a ArrayToPointerDecay implicit cast.  While correct, it is
2281     // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
2282     // "gep x, i" here.  Emit one "gep A, 0, i".
2283     assert(Array->getType()->isArrayType() &&
2284            "Array to pointer decay must have array source type!");
2285     LValue ArrayLV = EmitLValue(Array);
2286     llvm::Value *ArrayPtr = ArrayLV.getAddress();
2287     llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
2288     llvm::Value *Args[] = { Zero, Idx };
2289 
2290     // Propagate the alignment from the array itself to the result.
2291     ArrayAlignment = ArrayLV.getAlignment();
2292 
2293     if (getContext().getLangOpts().isSignedOverflowDefined())
2294       Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx");
2295     else
2296       Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx");
2297   } else {
2298     // The base must be a pointer, which is not an aggregate.  Emit it.
2299     llvm::Value *Base = EmitScalarExpr(E->getBase());
2300     if (getContext().getLangOpts().isSignedOverflowDefined())
2301       Address = Builder.CreateGEP(Base, Idx, "arrayidx");
2302     else
2303       Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
2304   }
2305 
2306   QualType T = E->getBase()->getType()->getPointeeType();
2307   assert(!T.isNull() &&
2308          "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
2309 
2310 
2311   // Limit the alignment to that of the result type.
2312   LValue LV;
2313   if (!ArrayAlignment.isZero()) {
2314     CharUnits Align = getContext().getTypeAlignInChars(T);
2315     ArrayAlignment = std::min(Align, ArrayAlignment);
2316     LV = MakeAddrLValue(Address, T, ArrayAlignment);
2317   } else {
2318     LV = MakeNaturalAlignAddrLValue(Address, T);
2319   }
2320 
2321   LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
2322 
2323   if (getContext().getLangOpts().ObjC1 &&
2324       getContext().getLangOpts().getGC() != LangOptions::NonGC) {
2325     LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
2326     setObjCGCLValueClass(getContext(), E, LV);
2327   }
2328   return LV;
2329 }
2330 
2331 static
2332 llvm::Constant *GenerateConstantVector(CGBuilderTy &Builder,
2333                                        SmallVector<unsigned, 4> &Elts) {
2334   SmallVector<llvm::Constant*, 4> CElts;
2335   for (unsigned i = 0, e = Elts.size(); i != e; ++i)
2336     CElts.push_back(Builder.getInt32(Elts[i]));
2337 
2338   return llvm::ConstantVector::get(CElts);
2339 }
2340 
2341 LValue CodeGenFunction::
2342 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
2343   // Emit the base vector as an l-value.
2344   LValue Base;
2345 
2346   // ExtVectorElementExpr's base can either be a vector or pointer to vector.
2347   if (E->isArrow()) {
2348     // If it is a pointer to a vector, emit the address and form an lvalue with
2349     // it.
2350     llvm::Value *Ptr = EmitScalarExpr(E->getBase());
2351     const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
2352     Base = MakeAddrLValue(Ptr, PT->getPointeeType());
2353     Base.getQuals().removeObjCGCAttr();
2354   } else if (E->getBase()->isGLValue()) {
2355     // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
2356     // emit the base as an lvalue.
2357     assert(E->getBase()->getType()->isVectorType());
2358     Base = EmitLValue(E->getBase());
2359   } else {
2360     // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
2361     assert(E->getBase()->getType()->isVectorType() &&
2362            "Result must be a vector");
2363     llvm::Value *Vec = EmitScalarExpr(E->getBase());
2364 
2365     // Store the vector to memory (because LValue wants an address).
2366     llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
2367     Builder.CreateStore(Vec, VecMem);
2368     Base = MakeAddrLValue(VecMem, E->getBase()->getType());
2369   }
2370 
2371   QualType type =
2372     E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
2373 
2374   // Encode the element access list into a vector of unsigned indices.
2375   SmallVector<unsigned, 4> Indices;
2376   E->getEncodedElementAccess(Indices);
2377 
2378   if (Base.isSimple()) {
2379     llvm::Constant *CV = GenerateConstantVector(Builder, Indices);
2380     return LValue::MakeExtVectorElt(Base.getAddress(), CV, type,
2381                                     Base.getAlignment());
2382   }
2383   assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
2384 
2385   llvm::Constant *BaseElts = Base.getExtVectorElts();
2386   SmallVector<llvm::Constant *, 4> CElts;
2387 
2388   for (unsigned i = 0, e = Indices.size(); i != e; ++i)
2389     CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
2390   llvm::Constant *CV = llvm::ConstantVector::get(CElts);
2391   return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type,
2392                                   Base.getAlignment());
2393 }
2394 
2395 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
2396   Expr *BaseExpr = E->getBase();
2397 
2398   // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
2399   LValue BaseLV;
2400   if (E->isArrow()) {
2401     llvm::Value *Ptr = EmitScalarExpr(BaseExpr);
2402     QualType PtrTy = BaseExpr->getType()->getPointeeType();
2403     EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Ptr, PtrTy);
2404     BaseLV = MakeNaturalAlignAddrLValue(Ptr, PtrTy);
2405   } else
2406     BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
2407 
2408   NamedDecl *ND = E->getMemberDecl();
2409   if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
2410     LValue LV = EmitLValueForField(BaseLV, Field);
2411     setObjCGCLValueClass(getContext(), E, LV);
2412     return LV;
2413   }
2414 
2415   if (VarDecl *VD = dyn_cast<VarDecl>(ND))
2416     return EmitGlobalVarDeclLValue(*this, E, VD);
2417 
2418   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
2419     return EmitFunctionDeclLValue(*this, E, FD);
2420 
2421   llvm_unreachable("Unhandled member declaration!");
2422 }
2423 
2424 LValue CodeGenFunction::EmitLValueForField(LValue base,
2425                                            const FieldDecl *field) {
2426   if (field->isBitField()) {
2427     const CGRecordLayout &RL =
2428       CGM.getTypes().getCGRecordLayout(field->getParent());
2429     const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
2430     QualType fieldType =
2431       field->getType().withCVRQualifiers(base.getVRQualifiers());
2432     return LValue::MakeBitfield(base.getAddress(), Info, fieldType,
2433                                 base.getAlignment());
2434   }
2435 
2436   const RecordDecl *rec = field->getParent();
2437   QualType type = field->getType();
2438   CharUnits alignment = getContext().getDeclAlign(field);
2439 
2440   // FIXME: It should be impossible to have an LValue without alignment for a
2441   // complete type.
2442   if (!base.getAlignment().isZero())
2443     alignment = std::min(alignment, base.getAlignment());
2444 
2445   bool mayAlias = rec->hasAttr<MayAliasAttr>();
2446 
2447   llvm::Value *addr = base.getAddress();
2448   unsigned cvr = base.getVRQualifiers();
2449   if (rec->isUnion()) {
2450     // For unions, there is no pointer adjustment.
2451     assert(!type->isReferenceType() && "union has reference member");
2452   } else {
2453     // For structs, we GEP to the field that the record layout suggests.
2454     unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
2455     addr = Builder.CreateStructGEP(addr, idx, field->getName());
2456 
2457     // If this is a reference field, load the reference right now.
2458     if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
2459       llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
2460       if (cvr & Qualifiers::Volatile) load->setVolatile(true);
2461       load->setAlignment(alignment.getQuantity());
2462 
2463       if (CGM.shouldUseTBAA()) {
2464         llvm::MDNode *tbaa;
2465         if (mayAlias)
2466           tbaa = CGM.getTBAAInfo(getContext().CharTy);
2467         else
2468           tbaa = CGM.getTBAAInfo(type);
2469         CGM.DecorateInstruction(load, tbaa);
2470       }
2471 
2472       addr = load;
2473       mayAlias = false;
2474       type = refType->getPointeeType();
2475       if (type->isIncompleteType())
2476         alignment = CharUnits();
2477       else
2478         alignment = getContext().getTypeAlignInChars(type);
2479       cvr = 0; // qualifiers don't recursively apply to referencee
2480     }
2481   }
2482 
2483   // Make sure that the address is pointing to the right type.  This is critical
2484   // for both unions and structs.  A union needs a bitcast, a struct element
2485   // will need a bitcast if the LLVM type laid out doesn't match the desired
2486   // type.
2487   addr = EmitBitCastOfLValueToProperType(*this, addr,
2488                                          CGM.getTypes().ConvertTypeForMem(type),
2489                                          field->getName());
2490 
2491   if (field->hasAttr<AnnotateAttr>())
2492     addr = EmitFieldAnnotations(field, addr);
2493 
2494   LValue LV = MakeAddrLValue(addr, type, alignment);
2495   LV.getQuals().addCVRQualifiers(cvr);
2496 
2497   // __weak attribute on a field is ignored.
2498   if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
2499     LV.getQuals().removeObjCGCAttr();
2500 
2501   // Fields of may_alias structs act like 'char' for TBAA purposes.
2502   // FIXME: this should get propagated down through anonymous structs
2503   // and unions.
2504   if (mayAlias && LV.getTBAAInfo())
2505     LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
2506 
2507   return LV;
2508 }
2509 
2510 LValue
2511 CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
2512                                                   const FieldDecl *Field) {
2513   QualType FieldType = Field->getType();
2514 
2515   if (!FieldType->isReferenceType())
2516     return EmitLValueForField(Base, Field);
2517 
2518   const CGRecordLayout &RL =
2519     CGM.getTypes().getCGRecordLayout(Field->getParent());
2520   unsigned idx = RL.getLLVMFieldNo(Field);
2521   llvm::Value *V = Builder.CreateStructGEP(Base.getAddress(), idx);
2522   assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
2523 
2524   // Make sure that the address is pointing to the right type.  This is critical
2525   // for both unions and structs.  A union needs a bitcast, a struct element
2526   // will need a bitcast if the LLVM type laid out doesn't match the desired
2527   // type.
2528   llvm::Type *llvmType = ConvertTypeForMem(FieldType);
2529   V = EmitBitCastOfLValueToProperType(*this, V, llvmType, Field->getName());
2530 
2531   CharUnits Alignment = getContext().getDeclAlign(Field);
2532 
2533   // FIXME: It should be impossible to have an LValue without alignment for a
2534   // complete type.
2535   if (!Base.getAlignment().isZero())
2536     Alignment = std::min(Alignment, Base.getAlignment());
2537 
2538   return MakeAddrLValue(V, FieldType, Alignment);
2539 }
2540 
2541 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
2542   if (E->isFileScope()) {
2543     llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
2544     return MakeAddrLValue(GlobalPtr, E->getType());
2545   }
2546   if (E->getType()->isVariablyModifiedType())
2547     // make sure to emit the VLA size.
2548     EmitVariablyModifiedType(E->getType());
2549 
2550   llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
2551   const Expr *InitExpr = E->getInitializer();
2552   LValue Result = MakeAddrLValue(DeclPtr, E->getType());
2553 
2554   EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
2555                    /*Init*/ true);
2556 
2557   return Result;
2558 }
2559 
2560 LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) {
2561   if (!E->isGLValue())
2562     // Initializing an aggregate temporary in C++11: T{...}.
2563     return EmitAggExprToLValue(E);
2564 
2565   // An lvalue initializer list must be initializing a reference.
2566   assert(E->getNumInits() == 1 && "reference init with multiple values");
2567   return EmitLValue(E->getInit(0));
2568 }
2569 
2570 LValue CodeGenFunction::
2571 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
2572   if (!expr->isGLValue()) {
2573     // ?: here should be an aggregate.
2574     assert((hasAggregateLLVMType(expr->getType()) &&
2575             !expr->getType()->isAnyComplexType()) &&
2576            "Unexpected conditional operator!");
2577     return EmitAggExprToLValue(expr);
2578   }
2579 
2580   OpaqueValueMapping binding(*this, expr);
2581 
2582   const Expr *condExpr = expr->getCond();
2583   bool CondExprBool;
2584   if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
2585     const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
2586     if (!CondExprBool) std::swap(live, dead);
2587 
2588     if (!ContainsLabel(dead))
2589       return EmitLValue(live);
2590   }
2591 
2592   llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
2593   llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
2594   llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
2595 
2596   ConditionalEvaluation eval(*this);
2597   EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock);
2598 
2599   // Any temporaries created here are conditional.
2600   EmitBlock(lhsBlock);
2601   eval.begin(*this);
2602   LValue lhs = EmitLValue(expr->getTrueExpr());
2603   eval.end(*this);
2604 
2605   if (!lhs.isSimple())
2606     return EmitUnsupportedLValue(expr, "conditional operator");
2607 
2608   lhsBlock = Builder.GetInsertBlock();
2609   Builder.CreateBr(contBlock);
2610 
2611   // Any temporaries created here are conditional.
2612   EmitBlock(rhsBlock);
2613   eval.begin(*this);
2614   LValue rhs = EmitLValue(expr->getFalseExpr());
2615   eval.end(*this);
2616   if (!rhs.isSimple())
2617     return EmitUnsupportedLValue(expr, "conditional operator");
2618   rhsBlock = Builder.GetInsertBlock();
2619 
2620   EmitBlock(contBlock);
2621 
2622   llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2,
2623                                          "cond-lvalue");
2624   phi->addIncoming(lhs.getAddress(), lhsBlock);
2625   phi->addIncoming(rhs.getAddress(), rhsBlock);
2626   return MakeAddrLValue(phi, expr->getType());
2627 }
2628 
2629 /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
2630 /// type. If the cast is to a reference, we can have the usual lvalue result,
2631 /// otherwise if a cast is needed by the code generator in an lvalue context,
2632 /// then it must mean that we need the address of an aggregate in order to
2633 /// access one of its members.  This can happen for all the reasons that casts
2634 /// are permitted with aggregate result, including noop aggregate casts, and
2635 /// cast from scalar to union.
2636 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
2637   switch (E->getCastKind()) {
2638   case CK_ToVoid:
2639     return EmitUnsupportedLValue(E, "unexpected cast lvalue");
2640 
2641   case CK_Dependent:
2642     llvm_unreachable("dependent cast kind in IR gen!");
2643 
2644   case CK_BuiltinFnToFnPtr:
2645     llvm_unreachable("builtin functions are handled elsewhere");
2646 
2647   // These two casts are currently treated as no-ops, although they could
2648   // potentially be real operations depending on the target's ABI.
2649   case CK_NonAtomicToAtomic:
2650   case CK_AtomicToNonAtomic:
2651 
2652   case CK_NoOp:
2653   case CK_LValueToRValue:
2654     if (!E->getSubExpr()->Classify(getContext()).isPRValue()
2655         || E->getType()->isRecordType())
2656       return EmitLValue(E->getSubExpr());
2657     // Fall through to synthesize a temporary.
2658 
2659   case CK_BitCast:
2660   case CK_ArrayToPointerDecay:
2661   case CK_FunctionToPointerDecay:
2662   case CK_NullToMemberPointer:
2663   case CK_NullToPointer:
2664   case CK_IntegralToPointer:
2665   case CK_PointerToIntegral:
2666   case CK_PointerToBoolean:
2667   case CK_VectorSplat:
2668   case CK_IntegralCast:
2669   case CK_IntegralToBoolean:
2670   case CK_IntegralToFloating:
2671   case CK_FloatingToIntegral:
2672   case CK_FloatingToBoolean:
2673   case CK_FloatingCast:
2674   case CK_FloatingRealToComplex:
2675   case CK_FloatingComplexToReal:
2676   case CK_FloatingComplexToBoolean:
2677   case CK_FloatingComplexCast:
2678   case CK_FloatingComplexToIntegralComplex:
2679   case CK_IntegralRealToComplex:
2680   case CK_IntegralComplexToReal:
2681   case CK_IntegralComplexToBoolean:
2682   case CK_IntegralComplexCast:
2683   case CK_IntegralComplexToFloatingComplex:
2684   case CK_DerivedToBaseMemberPointer:
2685   case CK_BaseToDerivedMemberPointer:
2686   case CK_MemberPointerToBoolean:
2687   case CK_ReinterpretMemberPointer:
2688   case CK_AnyPointerToBlockPointerCast:
2689   case CK_ARCProduceObject:
2690   case CK_ARCConsumeObject:
2691   case CK_ARCReclaimReturnedObject:
2692   case CK_ARCExtendBlockObject:
2693   case CK_CopyAndAutoreleaseBlockObject: {
2694     // These casts only produce lvalues when we're binding a reference to a
2695     // temporary realized from a (converted) pure rvalue. Emit the expression
2696     // as a value, copy it into a temporary, and return an lvalue referring to
2697     // that temporary.
2698     llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp");
2699     EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false);
2700     return MakeAddrLValue(V, E->getType());
2701   }
2702 
2703   case CK_Dynamic: {
2704     LValue LV = EmitLValue(E->getSubExpr());
2705     llvm::Value *V = LV.getAddress();
2706     const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
2707     return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2708   }
2709 
2710   case CK_ConstructorConversion:
2711   case CK_UserDefinedConversion:
2712   case CK_CPointerToObjCPointerCast:
2713   case CK_BlockPointerToObjCPointerCast:
2714     return EmitLValue(E->getSubExpr());
2715 
2716   case CK_UncheckedDerivedToBase:
2717   case CK_DerivedToBase: {
2718     const RecordType *DerivedClassTy =
2719       E->getSubExpr()->getType()->getAs<RecordType>();
2720     CXXRecordDecl *DerivedClassDecl =
2721       cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2722 
2723     LValue LV = EmitLValue(E->getSubExpr());
2724     llvm::Value *This = LV.getAddress();
2725 
2726     // Perform the derived-to-base conversion
2727     llvm::Value *Base =
2728       GetAddressOfBaseClass(This, DerivedClassDecl,
2729                             E->path_begin(), E->path_end(),
2730                             /*NullCheckValue=*/false);
2731 
2732     return MakeAddrLValue(Base, E->getType());
2733   }
2734   case CK_ToUnion:
2735     return EmitAggExprToLValue(E);
2736   case CK_BaseToDerived: {
2737     const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2738     CXXRecordDecl *DerivedClassDecl =
2739       cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2740 
2741     LValue LV = EmitLValue(E->getSubExpr());
2742 
2743     // Perform the base-to-derived conversion
2744     llvm::Value *Derived =
2745       GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
2746                                E->path_begin(), E->path_end(),
2747                                /*NullCheckValue=*/false);
2748 
2749     return MakeAddrLValue(Derived, E->getType());
2750   }
2751   case CK_LValueBitCast: {
2752     // This must be a reinterpret_cast (or c-style equivalent).
2753     const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
2754 
2755     LValue LV = EmitLValue(E->getSubExpr());
2756     llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2757                                            ConvertType(CE->getTypeAsWritten()));
2758     return MakeAddrLValue(V, E->getType());
2759   }
2760   case CK_ObjCObjectLValueCast: {
2761     LValue LV = EmitLValue(E->getSubExpr());
2762     QualType ToType = getContext().getLValueReferenceType(E->getType());
2763     llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2764                                            ConvertType(ToType));
2765     return MakeAddrLValue(V, E->getType());
2766   }
2767   }
2768 
2769   llvm_unreachable("Unhandled lvalue cast kind?");
2770 }
2771 
2772 LValue CodeGenFunction::EmitNullInitializationLValue(
2773                                               const CXXScalarValueInitExpr *E) {
2774   QualType Ty = E->getType();
2775   LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty);
2776   EmitNullInitialization(LV.getAddress(), Ty);
2777   return LV;
2778 }
2779 
2780 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
2781   assert(OpaqueValueMappingData::shouldBindAsLValue(e));
2782   return getOpaqueLValueMapping(e);
2783 }
2784 
2785 LValue CodeGenFunction::EmitMaterializeTemporaryExpr(
2786                                            const MaterializeTemporaryExpr *E) {
2787   RValue RV = EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
2788   return MakeAddrLValue(RV.getScalarVal(), E->getType());
2789 }
2790 
2791 RValue CodeGenFunction::EmitRValueForField(LValue LV,
2792                                            const FieldDecl *FD) {
2793   QualType FT = FD->getType();
2794   LValue FieldLV = EmitLValueForField(LV, FD);
2795   if (FT->isAnyComplexType())
2796     return RValue::getComplex(
2797         LoadComplexFromAddr(FieldLV.getAddress(),
2798                             FieldLV.isVolatileQualified()));
2799   else if (CodeGenFunction::hasAggregateLLVMType(FT))
2800     return FieldLV.asAggregateRValue();
2801 
2802   return EmitLoadOfLValue(FieldLV);
2803 }
2804 
2805 //===--------------------------------------------------------------------===//
2806 //                             Expression Emission
2807 //===--------------------------------------------------------------------===//
2808 
2809 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
2810                                      ReturnValueSlot ReturnValue) {
2811   if (CGDebugInfo *DI = getDebugInfo())
2812     DI->EmitLocation(Builder, E->getLocStart());
2813 
2814   // Builtins never have block type.
2815   if (E->getCallee()->getType()->isBlockPointerType())
2816     return EmitBlockCallExpr(E, ReturnValue);
2817 
2818   if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
2819     return EmitCXXMemberCallExpr(CE, ReturnValue);
2820 
2821   if (const CUDAKernelCallExpr *CE = dyn_cast<CUDAKernelCallExpr>(E))
2822     return EmitCUDAKernelCallExpr(CE, ReturnValue);
2823 
2824   const Decl *TargetDecl = E->getCalleeDecl();
2825   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
2826     if (unsigned builtinID = FD->getBuiltinID())
2827       return EmitBuiltinExpr(FD, builtinID, E);
2828   }
2829 
2830   if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
2831     if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
2832       return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
2833 
2834   if (const CXXPseudoDestructorExpr *PseudoDtor
2835           = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
2836     QualType DestroyedType = PseudoDtor->getDestroyedType();
2837     if (getContext().getLangOpts().ObjCAutoRefCount &&
2838         DestroyedType->isObjCLifetimeType() &&
2839         (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
2840          DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
2841       // Automatic Reference Counting:
2842       //   If the pseudo-expression names a retainable object with weak or
2843       //   strong lifetime, the object shall be released.
2844       Expr *BaseExpr = PseudoDtor->getBase();
2845       llvm::Value *BaseValue = NULL;
2846       Qualifiers BaseQuals;
2847 
2848       // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2849       if (PseudoDtor->isArrow()) {
2850         BaseValue = EmitScalarExpr(BaseExpr);
2851         const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
2852         BaseQuals = PTy->getPointeeType().getQualifiers();
2853       } else {
2854         LValue BaseLV = EmitLValue(BaseExpr);
2855         BaseValue = BaseLV.getAddress();
2856         QualType BaseTy = BaseExpr->getType();
2857         BaseQuals = BaseTy.getQualifiers();
2858       }
2859 
2860       switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
2861       case Qualifiers::OCL_None:
2862       case Qualifiers::OCL_ExplicitNone:
2863       case Qualifiers::OCL_Autoreleasing:
2864         break;
2865 
2866       case Qualifiers::OCL_Strong:
2867         EmitARCRelease(Builder.CreateLoad(BaseValue,
2868                           PseudoDtor->getDestroyedType().isVolatileQualified()),
2869                        /*precise*/ true);
2870         break;
2871 
2872       case Qualifiers::OCL_Weak:
2873         EmitARCDestroyWeak(BaseValue);
2874         break;
2875       }
2876     } else {
2877       // C++ [expr.pseudo]p1:
2878       //   The result shall only be used as the operand for the function call
2879       //   operator (), and the result of such a call has type void. The only
2880       //   effect is the evaluation of the postfix-expression before the dot or
2881       //   arrow.
2882       EmitScalarExpr(E->getCallee());
2883     }
2884 
2885     return RValue::get(0);
2886   }
2887 
2888   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
2889   return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
2890                   E->arg_begin(), E->arg_end(), TargetDecl);
2891 }
2892 
2893 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
2894   // Comma expressions just emit their LHS then their RHS as an l-value.
2895   if (E->getOpcode() == BO_Comma) {
2896     EmitIgnoredExpr(E->getLHS());
2897     EnsureInsertPoint();
2898     return EmitLValue(E->getRHS());
2899   }
2900 
2901   if (E->getOpcode() == BO_PtrMemD ||
2902       E->getOpcode() == BO_PtrMemI)
2903     return EmitPointerToDataMemberBinaryExpr(E);
2904 
2905   assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
2906 
2907   // Note that in all of these cases, __block variables need the RHS
2908   // evaluated first just in case the variable gets moved by the RHS.
2909 
2910   if (!hasAggregateLLVMType(E->getType())) {
2911     switch (E->getLHS()->getType().getObjCLifetime()) {
2912     case Qualifiers::OCL_Strong:
2913       return EmitARCStoreStrong(E, /*ignored*/ false).first;
2914 
2915     case Qualifiers::OCL_Autoreleasing:
2916       return EmitARCStoreAutoreleasing(E).first;
2917 
2918     // No reason to do any of these differently.
2919     case Qualifiers::OCL_None:
2920     case Qualifiers::OCL_ExplicitNone:
2921     case Qualifiers::OCL_Weak:
2922       break;
2923     }
2924 
2925     RValue RV = EmitAnyExpr(E->getRHS());
2926     LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store);
2927     EmitStoreThroughLValue(RV, LV);
2928     return LV;
2929   }
2930 
2931   if (E->getType()->isAnyComplexType())
2932     return EmitComplexAssignmentLValue(E);
2933 
2934   return EmitAggExprToLValue(E);
2935 }
2936 
2937 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
2938   RValue RV = EmitCallExpr(E);
2939 
2940   if (!RV.isScalar())
2941     return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2942 
2943   assert(E->getCallReturnType()->isReferenceType() &&
2944          "Can't have a scalar return unless the return type is a "
2945          "reference type!");
2946 
2947   return MakeAddrLValue(RV.getScalarVal(), E->getType());
2948 }
2949 
2950 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
2951   // FIXME: This shouldn't require another copy.
2952   return EmitAggExprToLValue(E);
2953 }
2954 
2955 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
2956   assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
2957          && "binding l-value to type which needs a temporary");
2958   AggValueSlot Slot = CreateAggTemp(E->getType());
2959   EmitCXXConstructExpr(E, Slot);
2960   return MakeAddrLValue(Slot.getAddr(), E->getType());
2961 }
2962 
2963 LValue
2964 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
2965   return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
2966 }
2967 
2968 llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
2969   return CGM.GetAddrOfUuidDescriptor(E);
2970 }
2971 
2972 LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
2973   return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType());
2974 }
2975 
2976 LValue
2977 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
2978   AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2979   Slot.setExternallyDestructed();
2980   EmitAggExpr(E->getSubExpr(), Slot);
2981   EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr());
2982   return MakeAddrLValue(Slot.getAddr(), E->getType());
2983 }
2984 
2985 LValue
2986 CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) {
2987   AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2988   EmitLambdaExpr(E, Slot);
2989   return MakeAddrLValue(Slot.getAddr(), E->getType());
2990 }
2991 
2992 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
2993   RValue RV = EmitObjCMessageExpr(E);
2994 
2995   if (!RV.isScalar())
2996     return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2997 
2998   assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
2999          "Can't have a scalar return unless the return type is a "
3000          "reference type!");
3001 
3002   return MakeAddrLValue(RV.getScalarVal(), E->getType());
3003 }
3004 
3005 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
3006   llvm::Value *V =
3007     CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
3008   return MakeAddrLValue(V, E->getType());
3009 }
3010 
3011 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
3012                                              const ObjCIvarDecl *Ivar) {
3013   return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
3014 }
3015 
3016 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
3017                                           llvm::Value *BaseValue,
3018                                           const ObjCIvarDecl *Ivar,
3019                                           unsigned CVRQualifiers) {
3020   return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
3021                                                    Ivar, CVRQualifiers);
3022 }
3023 
3024 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
3025   // FIXME: A lot of the code below could be shared with EmitMemberExpr.
3026   llvm::Value *BaseValue = 0;
3027   const Expr *BaseExpr = E->getBase();
3028   Qualifiers BaseQuals;
3029   QualType ObjectTy;
3030   if (E->isArrow()) {
3031     BaseValue = EmitScalarExpr(BaseExpr);
3032     ObjectTy = BaseExpr->getType()->getPointeeType();
3033     BaseQuals = ObjectTy.getQualifiers();
3034   } else {
3035     LValue BaseLV = EmitLValue(BaseExpr);
3036     // FIXME: this isn't right for bitfields.
3037     BaseValue = BaseLV.getAddress();
3038     ObjectTy = BaseExpr->getType();
3039     BaseQuals = ObjectTy.getQualifiers();
3040   }
3041 
3042   LValue LV =
3043     EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
3044                       BaseQuals.getCVRQualifiers());
3045   setObjCGCLValueClass(getContext(), E, LV);
3046   return LV;
3047 }
3048 
3049 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
3050   // Can only get l-value for message expression returning aggregate type
3051   RValue RV = EmitAnyExprToTemp(E);
3052   return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3053 }
3054 
3055 RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
3056                                  ReturnValueSlot ReturnValue,
3057                                  CallExpr::const_arg_iterator ArgBeg,
3058                                  CallExpr::const_arg_iterator ArgEnd,
3059                                  const Decl *TargetDecl) {
3060   // Get the actual function type. The callee type will always be a pointer to
3061   // function type or a block pointer type.
3062   assert(CalleeType->isFunctionPointerType() &&
3063          "Call must have function pointer type!");
3064 
3065   CalleeType = getContext().getCanonicalType(CalleeType);
3066 
3067   const FunctionType *FnType
3068     = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
3069 
3070   CallArgList Args;
3071   EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
3072 
3073   const CGFunctionInfo &FnInfo =
3074     CGM.getTypes().arrangeFreeFunctionCall(Args, FnType);
3075 
3076   // C99 6.5.2.2p6:
3077   //   If the expression that denotes the called function has a type
3078   //   that does not include a prototype, [the default argument
3079   //   promotions are performed]. If the number of arguments does not
3080   //   equal the number of parameters, the behavior is undefined. If
3081   //   the function is defined with a type that includes a prototype,
3082   //   and either the prototype ends with an ellipsis (, ...) or the
3083   //   types of the arguments after promotion are not compatible with
3084   //   the types of the parameters, the behavior is undefined. If the
3085   //   function is defined with a type that does not include a
3086   //   prototype, and the types of the arguments after promotion are
3087   //   not compatible with those of the parameters after promotion,
3088   //   the behavior is undefined [except in some trivial cases].
3089   // That is, in the general case, we should assume that a call
3090   // through an unprototyped function type works like a *non-variadic*
3091   // call.  The way we make this work is to cast to the exact type
3092   // of the promoted arguments.
3093   if (isa<FunctionNoProtoType>(FnType) && !FnInfo.isVariadic()) {
3094     llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
3095     CalleeTy = CalleeTy->getPointerTo();
3096     Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast");
3097   }
3098 
3099   return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl);
3100 }
3101 
3102 LValue CodeGenFunction::
3103 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
3104   llvm::Value *BaseV;
3105   if (E->getOpcode() == BO_PtrMemI)
3106     BaseV = EmitScalarExpr(E->getLHS());
3107   else
3108     BaseV = EmitLValue(E->getLHS()).getAddress();
3109 
3110   llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
3111 
3112   const MemberPointerType *MPT
3113     = E->getRHS()->getType()->getAs<MemberPointerType>();
3114 
3115   llvm::Value *AddV =
3116     CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
3117 
3118   return MakeAddrLValue(AddV, MPT->getPointeeType());
3119 }
3120 
3121 static void
3122 EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest,
3123              llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2,
3124              uint64_t Size, unsigned Align, llvm::AtomicOrdering Order) {
3125   llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
3126   llvm::Instruction::BinaryOps PostOp = (llvm::Instruction::BinaryOps)0;
3127 
3128   switch (E->getOp()) {
3129   case AtomicExpr::AO__c11_atomic_init:
3130     llvm_unreachable("Already handled!");
3131 
3132   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3133   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
3134   case AtomicExpr::AO__atomic_compare_exchange:
3135   case AtomicExpr::AO__atomic_compare_exchange_n: {
3136     // Note that cmpxchg only supports specifying one ordering and
3137     // doesn't support weak cmpxchg, at least at the moment.
3138     llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
3139     LoadVal1->setAlignment(Align);
3140     llvm::LoadInst *LoadVal2 = CGF.Builder.CreateLoad(Val2);
3141     LoadVal2->setAlignment(Align);
3142     llvm::AtomicCmpXchgInst *CXI =
3143         CGF.Builder.CreateAtomicCmpXchg(Ptr, LoadVal1, LoadVal2, Order);
3144     CXI->setVolatile(E->isVolatile());
3145     llvm::StoreInst *StoreVal1 = CGF.Builder.CreateStore(CXI, Val1);
3146     StoreVal1->setAlignment(Align);
3147     llvm::Value *Cmp = CGF.Builder.CreateICmpEQ(CXI, LoadVal1);
3148     CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
3149     return;
3150   }
3151 
3152   case AtomicExpr::AO__c11_atomic_load:
3153   case AtomicExpr::AO__atomic_load_n:
3154   case AtomicExpr::AO__atomic_load: {
3155     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
3156     Load->setAtomic(Order);
3157     Load->setAlignment(Size);
3158     Load->setVolatile(E->isVolatile());
3159     llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest);
3160     StoreDest->setAlignment(Align);
3161     return;
3162   }
3163 
3164   case AtomicExpr::AO__c11_atomic_store:
3165   case AtomicExpr::AO__atomic_store:
3166   case AtomicExpr::AO__atomic_store_n: {
3167     assert(!Dest && "Store does not return a value");
3168     llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
3169     LoadVal1->setAlignment(Align);
3170     llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
3171     Store->setAtomic(Order);
3172     Store->setAlignment(Size);
3173     Store->setVolatile(E->isVolatile());
3174     return;
3175   }
3176 
3177   case AtomicExpr::AO__c11_atomic_exchange:
3178   case AtomicExpr::AO__atomic_exchange_n:
3179   case AtomicExpr::AO__atomic_exchange:
3180     Op = llvm::AtomicRMWInst::Xchg;
3181     break;
3182 
3183   case AtomicExpr::AO__atomic_add_fetch:
3184     PostOp = llvm::Instruction::Add;
3185     // Fall through.
3186   case AtomicExpr::AO__c11_atomic_fetch_add:
3187   case AtomicExpr::AO__atomic_fetch_add:
3188     Op = llvm::AtomicRMWInst::Add;
3189     break;
3190 
3191   case AtomicExpr::AO__atomic_sub_fetch:
3192     PostOp = llvm::Instruction::Sub;
3193     // Fall through.
3194   case AtomicExpr::AO__c11_atomic_fetch_sub:
3195   case AtomicExpr::AO__atomic_fetch_sub:
3196     Op = llvm::AtomicRMWInst::Sub;
3197     break;
3198 
3199   case AtomicExpr::AO__atomic_and_fetch:
3200     PostOp = llvm::Instruction::And;
3201     // Fall through.
3202   case AtomicExpr::AO__c11_atomic_fetch_and:
3203   case AtomicExpr::AO__atomic_fetch_and:
3204     Op = llvm::AtomicRMWInst::And;
3205     break;
3206 
3207   case AtomicExpr::AO__atomic_or_fetch:
3208     PostOp = llvm::Instruction::Or;
3209     // Fall through.
3210   case AtomicExpr::AO__c11_atomic_fetch_or:
3211   case AtomicExpr::AO__atomic_fetch_or:
3212     Op = llvm::AtomicRMWInst::Or;
3213     break;
3214 
3215   case AtomicExpr::AO__atomic_xor_fetch:
3216     PostOp = llvm::Instruction::Xor;
3217     // Fall through.
3218   case AtomicExpr::AO__c11_atomic_fetch_xor:
3219   case AtomicExpr::AO__atomic_fetch_xor:
3220     Op = llvm::AtomicRMWInst::Xor;
3221     break;
3222 
3223   case AtomicExpr::AO__atomic_nand_fetch:
3224     PostOp = llvm::Instruction::And;
3225     // Fall through.
3226   case AtomicExpr::AO__atomic_fetch_nand:
3227     Op = llvm::AtomicRMWInst::Nand;
3228     break;
3229   }
3230 
3231   llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
3232   LoadVal1->setAlignment(Align);
3233   llvm::AtomicRMWInst *RMWI =
3234       CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order);
3235   RMWI->setVolatile(E->isVolatile());
3236 
3237   // For __atomic_*_fetch operations, perform the operation again to
3238   // determine the value which was written.
3239   llvm::Value *Result = RMWI;
3240   if (PostOp)
3241     Result = CGF.Builder.CreateBinOp(PostOp, RMWI, LoadVal1);
3242   if (E->getOp() == AtomicExpr::AO__atomic_nand_fetch)
3243     Result = CGF.Builder.CreateNot(Result);
3244   llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Result, Dest);
3245   StoreDest->setAlignment(Align);
3246 }
3247 
3248 // This function emits any expression (scalar, complex, or aggregate)
3249 // into a temporary alloca.
3250 static llvm::Value *
3251 EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
3252   llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
3253   CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
3254                        /*Init*/ true);
3255   return DeclPtr;
3256 }
3257 
3258 static RValue ConvertTempToRValue(CodeGenFunction &CGF, QualType Ty,
3259                                   llvm::Value *Dest) {
3260   if (Ty->isAnyComplexType())
3261     return RValue::getComplex(CGF.LoadComplexFromAddr(Dest, false));
3262   if (CGF.hasAggregateLLVMType(Ty))
3263     return RValue::getAggregate(Dest);
3264   return RValue::get(CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(Dest, Ty)));
3265 }
3266 
3267 RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
3268   QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
3269   QualType MemTy = AtomicTy;
3270   if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
3271     MemTy = AT->getValueType();
3272   CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy);
3273   uint64_t Size = sizeChars.getQuantity();
3274   CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy);
3275   unsigned Align = alignChars.getQuantity();
3276   unsigned MaxInlineWidth =
3277       getContext().getTargetInfo().getMaxAtomicInlineWidth();
3278   bool UseLibcall = (Size != Align || Size > MaxInlineWidth);
3279 
3280 
3281 
3282   llvm::Value *Ptr, *Order, *OrderFail = 0, *Val1 = 0, *Val2 = 0;
3283   Ptr = EmitScalarExpr(E->getPtr());
3284 
3285   if (E->getOp() == AtomicExpr::AO__c11_atomic_init) {
3286     assert(!Dest && "Init does not return a value");
3287     if (!hasAggregateLLVMType(E->getVal1()->getType())) {
3288       QualType PointeeType
3289         = E->getPtr()->getType()->getAs<PointerType>()->getPointeeType();
3290       EmitScalarInit(EmitScalarExpr(E->getVal1()),
3291                      LValue::MakeAddr(Ptr, PointeeType, alignChars,
3292                                       getContext()));
3293     } else if (E->getType()->isAnyComplexType()) {
3294       EmitComplexExprIntoAddr(E->getVal1(), Ptr, E->isVolatile());
3295     } else {
3296       AggValueSlot Slot = AggValueSlot::forAddr(Ptr, alignChars,
3297                                         AtomicTy.getQualifiers(),
3298                                         AggValueSlot::IsNotDestructed,
3299                                         AggValueSlot::DoesNotNeedGCBarriers,
3300                                         AggValueSlot::IsNotAliased);
3301       EmitAggExpr(E->getVal1(), Slot);
3302     }
3303     return RValue::get(0);
3304   }
3305 
3306   Order = EmitScalarExpr(E->getOrder());
3307 
3308   switch (E->getOp()) {
3309   case AtomicExpr::AO__c11_atomic_init:
3310     llvm_unreachable("Already handled!");
3311 
3312   case AtomicExpr::AO__c11_atomic_load:
3313   case AtomicExpr::AO__atomic_load_n:
3314     break;
3315 
3316   case AtomicExpr::AO__atomic_load:
3317     Dest = EmitScalarExpr(E->getVal1());
3318     break;
3319 
3320   case AtomicExpr::AO__atomic_store:
3321     Val1 = EmitScalarExpr(E->getVal1());
3322     break;
3323 
3324   case AtomicExpr::AO__atomic_exchange:
3325     Val1 = EmitScalarExpr(E->getVal1());
3326     Dest = EmitScalarExpr(E->getVal2());
3327     break;
3328 
3329   case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3330   case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
3331   case AtomicExpr::AO__atomic_compare_exchange_n:
3332   case AtomicExpr::AO__atomic_compare_exchange:
3333     Val1 = EmitScalarExpr(E->getVal1());
3334     if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange)
3335       Val2 = EmitScalarExpr(E->getVal2());
3336     else
3337       Val2 = EmitValToTemp(*this, E->getVal2());
3338     OrderFail = EmitScalarExpr(E->getOrderFail());
3339     // Evaluate and discard the 'weak' argument.
3340     if (E->getNumSubExprs() == 6)
3341       EmitScalarExpr(E->getWeak());
3342     break;
3343 
3344   case AtomicExpr::AO__c11_atomic_fetch_add:
3345   case AtomicExpr::AO__c11_atomic_fetch_sub:
3346     if (MemTy->isPointerType()) {
3347       // For pointer arithmetic, we're required to do a bit of math:
3348       // adding 1 to an int* is not the same as adding 1 to a uintptr_t.
3349       // ... but only for the C11 builtins. The GNU builtins expect the
3350       // user to multiply by sizeof(T).
3351       QualType Val1Ty = E->getVal1()->getType();
3352       llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
3353       CharUnits PointeeIncAmt =
3354           getContext().getTypeSizeInChars(MemTy->getPointeeType());
3355       Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
3356       Val1 = CreateMemTemp(Val1Ty, ".atomictmp");
3357       EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty));
3358       break;
3359     }
3360     // Fall through.
3361   case AtomicExpr::AO__atomic_fetch_add:
3362   case AtomicExpr::AO__atomic_fetch_sub:
3363   case AtomicExpr::AO__atomic_add_fetch:
3364   case AtomicExpr::AO__atomic_sub_fetch:
3365   case AtomicExpr::AO__c11_atomic_store:
3366   case AtomicExpr::AO__c11_atomic_exchange:
3367   case AtomicExpr::AO__atomic_store_n:
3368   case AtomicExpr::AO__atomic_exchange_n:
3369   case AtomicExpr::AO__c11_atomic_fetch_and:
3370   case AtomicExpr::AO__c11_atomic_fetch_or:
3371   case AtomicExpr::AO__c11_atomic_fetch_xor:
3372   case AtomicExpr::AO__atomic_fetch_and:
3373   case AtomicExpr::AO__atomic_fetch_or:
3374   case AtomicExpr::AO__atomic_fetch_xor:
3375   case AtomicExpr::AO__atomic_fetch_nand:
3376   case AtomicExpr::AO__atomic_and_fetch:
3377   case AtomicExpr::AO__atomic_or_fetch:
3378   case AtomicExpr::AO__atomic_xor_fetch:
3379   case AtomicExpr::AO__atomic_nand_fetch:
3380     Val1 = EmitValToTemp(*this, E->getVal1());
3381     break;
3382   }
3383 
3384   if (!E->getType()->isVoidType() && !Dest)
3385     Dest = CreateMemTemp(E->getType(), ".atomicdst");
3386 
3387   // Use a library call.  See: http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
3388   if (UseLibcall) {
3389 
3390     llvm::SmallVector<QualType, 5> Params;
3391     CallArgList Args;
3392     // Size is always the first parameter
3393     Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
3394              getContext().getSizeType());
3395     // Atomic address is always the second parameter
3396     Args.add(RValue::get(EmitCastToVoidPtr(Ptr)),
3397              getContext().VoidPtrTy);
3398 
3399     const char* LibCallName;
3400     QualType RetTy = getContext().VoidTy;
3401     switch (E->getOp()) {
3402     // There is only one libcall for compare an exchange, because there is no
3403     // optimisation benefit possible from a libcall version of a weak compare
3404     // and exchange.
3405     // bool __atomic_compare_exchange(size_t size, void *obj, void *expected,
3406     //                                void *desired, int success, int failure)
3407     case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
3408     case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3409     case AtomicExpr::AO__atomic_compare_exchange:
3410     case AtomicExpr::AO__atomic_compare_exchange_n:
3411       LibCallName = "__atomic_compare_exchange";
3412       RetTy = getContext().BoolTy;
3413       Args.add(RValue::get(EmitCastToVoidPtr(Val1)),
3414                getContext().VoidPtrTy);
3415       Args.add(RValue::get(EmitCastToVoidPtr(Val2)),
3416                getContext().VoidPtrTy);
3417       Args.add(RValue::get(Order),
3418                getContext().IntTy);
3419       Order = OrderFail;
3420       break;
3421     // void __atomic_exchange(size_t size, void *mem, void *val, void *return,
3422     //                        int order)
3423     case AtomicExpr::AO__c11_atomic_exchange:
3424     case AtomicExpr::AO__atomic_exchange_n:
3425     case AtomicExpr::AO__atomic_exchange:
3426       LibCallName = "__atomic_exchange";
3427       Args.add(RValue::get(EmitCastToVoidPtr(Val1)),
3428                getContext().VoidPtrTy);
3429       Args.add(RValue::get(EmitCastToVoidPtr(Dest)),
3430                getContext().VoidPtrTy);
3431       break;
3432     // void __atomic_store(size_t size, void *mem, void *val, int order)
3433     case AtomicExpr::AO__c11_atomic_store:
3434     case AtomicExpr::AO__atomic_store:
3435     case AtomicExpr::AO__atomic_store_n:
3436       LibCallName = "__atomic_store";
3437       Args.add(RValue::get(EmitCastToVoidPtr(Val1)),
3438                getContext().VoidPtrTy);
3439       break;
3440     // void __atomic_load(size_t size, void *mem, void *return, int order)
3441     case AtomicExpr::AO__c11_atomic_load:
3442     case AtomicExpr::AO__atomic_load:
3443     case AtomicExpr::AO__atomic_load_n:
3444       LibCallName = "__atomic_load";
3445       Args.add(RValue::get(EmitCastToVoidPtr(Dest)),
3446                getContext().VoidPtrTy);
3447       break;
3448 #if 0
3449     // These are only defined for 1-16 byte integers.  It is not clear what
3450     // their semantics would be on anything else...
3451     case AtomicExpr::Add:   LibCallName = "__atomic_fetch_add_generic"; break;
3452     case AtomicExpr::Sub:   LibCallName = "__atomic_fetch_sub_generic"; break;
3453     case AtomicExpr::And:   LibCallName = "__atomic_fetch_and_generic"; break;
3454     case AtomicExpr::Or:    LibCallName = "__atomic_fetch_or_generic"; break;
3455     case AtomicExpr::Xor:   LibCallName = "__atomic_fetch_xor_generic"; break;
3456 #endif
3457     default: return EmitUnsupportedRValue(E, "atomic library call");
3458     }
3459     // order is always the last parameter
3460     Args.add(RValue::get(Order),
3461              getContext().IntTy);
3462 
3463     const CGFunctionInfo &FuncInfo =
3464         CGM.getTypes().arrangeFreeFunctionCall(RetTy, Args,
3465             FunctionType::ExtInfo(), RequiredArgs::All);
3466     llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
3467     llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
3468     RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args);
3469     if (E->isCmpXChg())
3470       return Res;
3471     if (E->getType()->isVoidType())
3472       return RValue::get(0);
3473     return ConvertTempToRValue(*this, E->getType(), Dest);
3474   }
3475 
3476   llvm::Type *IPtrTy =
3477       llvm::IntegerType::get(getLLVMContext(), Size * 8)->getPointerTo();
3478   llvm::Value *OrigDest = Dest;
3479   Ptr = Builder.CreateBitCast(Ptr, IPtrTy);
3480   if (Val1) Val1 = Builder.CreateBitCast(Val1, IPtrTy);
3481   if (Val2) Val2 = Builder.CreateBitCast(Val2, IPtrTy);
3482   if (Dest && !E->isCmpXChg()) Dest = Builder.CreateBitCast(Dest, IPtrTy);
3483 
3484   if (isa<llvm::ConstantInt>(Order)) {
3485     int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
3486     switch (ord) {
3487     case 0:  // memory_order_relaxed
3488       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3489                    llvm::Monotonic);
3490       break;
3491     case 1:  // memory_order_consume
3492     case 2:  // memory_order_acquire
3493       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3494                    llvm::Acquire);
3495       break;
3496     case 3:  // memory_order_release
3497       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3498                    llvm::Release);
3499       break;
3500     case 4:  // memory_order_acq_rel
3501       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3502                    llvm::AcquireRelease);
3503       break;
3504     case 5:  // memory_order_seq_cst
3505       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3506                    llvm::SequentiallyConsistent);
3507       break;
3508     default: // invalid order
3509       // We should not ever get here normally, but it's hard to
3510       // enforce that in general.
3511       break;
3512     }
3513     if (E->getType()->isVoidType())
3514       return RValue::get(0);
3515     return ConvertTempToRValue(*this, E->getType(), OrigDest);
3516   }
3517 
3518   // Long case, when Order isn't obviously constant.
3519 
3520   bool IsStore = E->getOp() == AtomicExpr::AO__c11_atomic_store ||
3521                  E->getOp() == AtomicExpr::AO__atomic_store ||
3522                  E->getOp() == AtomicExpr::AO__atomic_store_n;
3523   bool IsLoad = E->getOp() == AtomicExpr::AO__c11_atomic_load ||
3524                 E->getOp() == AtomicExpr::AO__atomic_load ||
3525                 E->getOp() == AtomicExpr::AO__atomic_load_n;
3526 
3527   // Create all the relevant BB's
3528   llvm::BasicBlock *MonotonicBB = 0, *AcquireBB = 0, *ReleaseBB = 0,
3529                    *AcqRelBB = 0, *SeqCstBB = 0;
3530   MonotonicBB = createBasicBlock("monotonic", CurFn);
3531   if (!IsStore)
3532     AcquireBB = createBasicBlock("acquire", CurFn);
3533   if (!IsLoad)
3534     ReleaseBB = createBasicBlock("release", CurFn);
3535   if (!IsLoad && !IsStore)
3536     AcqRelBB = createBasicBlock("acqrel", CurFn);
3537   SeqCstBB = createBasicBlock("seqcst", CurFn);
3538   llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
3539 
3540   // Create the switch for the split
3541   // MonotonicBB is arbitrarily chosen as the default case; in practice, this
3542   // doesn't matter unless someone is crazy enough to use something that
3543   // doesn't fold to a constant for the ordering.
3544   Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
3545   llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
3546 
3547   // Emit all the different atomics
3548   Builder.SetInsertPoint(MonotonicBB);
3549   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3550                llvm::Monotonic);
3551   Builder.CreateBr(ContBB);
3552   if (!IsStore) {
3553     Builder.SetInsertPoint(AcquireBB);
3554     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3555                  llvm::Acquire);
3556     Builder.CreateBr(ContBB);
3557     SI->addCase(Builder.getInt32(1), AcquireBB);
3558     SI->addCase(Builder.getInt32(2), AcquireBB);
3559   }
3560   if (!IsLoad) {
3561     Builder.SetInsertPoint(ReleaseBB);
3562     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3563                  llvm::Release);
3564     Builder.CreateBr(ContBB);
3565     SI->addCase(Builder.getInt32(3), ReleaseBB);
3566   }
3567   if (!IsLoad && !IsStore) {
3568     Builder.SetInsertPoint(AcqRelBB);
3569     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3570                  llvm::AcquireRelease);
3571     Builder.CreateBr(ContBB);
3572     SI->addCase(Builder.getInt32(4), AcqRelBB);
3573   }
3574   Builder.SetInsertPoint(SeqCstBB);
3575   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
3576                llvm::SequentiallyConsistent);
3577   Builder.CreateBr(ContBB);
3578   SI->addCase(Builder.getInt32(5), SeqCstBB);
3579 
3580   // Cleanup and return
3581   Builder.SetInsertPoint(ContBB);
3582   if (E->getType()->isVoidType())
3583     return RValue::get(0);
3584   return ConvertTempToRValue(*this, E->getType(), OrigDest);
3585 }
3586 
3587 void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
3588   assert(Val->getType()->isFPOrFPVectorTy());
3589   if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
3590     return;
3591 
3592   llvm::MDBuilder MDHelper(getLLVMContext());
3593   llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
3594 
3595   cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
3596 }
3597 
3598 namespace {
3599   struct LValueOrRValue {
3600     LValue LV;
3601     RValue RV;
3602   };
3603 }
3604 
3605 static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
3606                                            const PseudoObjectExpr *E,
3607                                            bool forLValue,
3608                                            AggValueSlot slot) {
3609   llvm::SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3610 
3611   // Find the result expression, if any.
3612   const Expr *resultExpr = E->getResultExpr();
3613   LValueOrRValue result;
3614 
3615   for (PseudoObjectExpr::const_semantics_iterator
3616          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
3617     const Expr *semantic = *i;
3618 
3619     // If this semantic expression is an opaque value, bind it
3620     // to the result of its source expression.
3621     if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3622 
3623       // If this is the result expression, we may need to evaluate
3624       // directly into the slot.
3625       typedef CodeGenFunction::OpaqueValueMappingData OVMA;
3626       OVMA opaqueData;
3627       if (ov == resultExpr && ov->isRValue() && !forLValue &&
3628           CodeGenFunction::hasAggregateLLVMType(ov->getType()) &&
3629           !ov->getType()->isAnyComplexType()) {
3630         CGF.EmitAggExpr(ov->getSourceExpr(), slot);
3631 
3632         LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType());
3633         opaqueData = OVMA::bind(CGF, ov, LV);
3634         result.RV = slot.asRValue();
3635 
3636       // Otherwise, emit as normal.
3637       } else {
3638         opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3639 
3640         // If this is the result, also evaluate the result now.
3641         if (ov == resultExpr) {
3642           if (forLValue)
3643             result.LV = CGF.EmitLValue(ov);
3644           else
3645             result.RV = CGF.EmitAnyExpr(ov, slot);
3646         }
3647       }
3648 
3649       opaques.push_back(opaqueData);
3650 
3651     // Otherwise, if the expression is the result, evaluate it
3652     // and remember the result.
3653     } else if (semantic == resultExpr) {
3654       if (forLValue)
3655         result.LV = CGF.EmitLValue(semantic);
3656       else
3657         result.RV = CGF.EmitAnyExpr(semantic, slot);
3658 
3659     // Otherwise, evaluate the expression in an ignored context.
3660     } else {
3661       CGF.EmitIgnoredExpr(semantic);
3662     }
3663   }
3664 
3665   // Unbind all the opaques now.
3666   for (unsigned i = 0, e = opaques.size(); i != e; ++i)
3667     opaques[i].unbind(CGF);
3668 
3669   return result;
3670 }
3671 
3672 RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
3673                                                AggValueSlot slot) {
3674   return emitPseudoObjectExpr(*this, E, false, slot).RV;
3675 }
3676 
3677 LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
3678   return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
3679 }
3680