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