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/Frontend/CodeGenOptions.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/LLVMContext.h"
27 #include "llvm/Target/TargetData.h"
28 using namespace clang;
29 using namespace CodeGen;
30 
31 //===--------------------------------------------------------------------===//
32 //                        Miscellaneous Helper Methods
33 //===--------------------------------------------------------------------===//
34 
35 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
36   unsigned addressSpace =
37     cast<llvm::PointerType>(value->getType())->getAddressSpace();
38 
39   llvm::PointerType *destType = Int8PtrTy;
40   if (addressSpace)
41     destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
42 
43   if (value->getType() == destType) return value;
44   return Builder.CreateBitCast(value, destType);
45 }
46 
47 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
48 /// block.
49 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
50                                                     const Twine &Name) {
51   if (!Builder.isNamePreserving())
52     return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
53   return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
54 }
55 
56 void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
57                                      llvm::Value *Init) {
58   llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
59   llvm::BasicBlock *Block = AllocaInsertPt->getParent();
60   Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
61 }
62 
63 llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
64                                                 const Twine &Name) {
65   llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
66   // FIXME: Should we prefer the preferred type alignment here?
67   CharUnits Align = getContext().getTypeAlignInChars(Ty);
68   Alloc->setAlignment(Align.getQuantity());
69   return Alloc;
70 }
71 
72 llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
73                                                  const Twine &Name) {
74   llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
75   // FIXME: Should we prefer the preferred type alignment here?
76   CharUnits Align = getContext().getTypeAlignInChars(Ty);
77   Alloc->setAlignment(Align.getQuantity());
78   return Alloc;
79 }
80 
81 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
82 /// expression and compare the result against zero, returning an Int1Ty value.
83 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
84   if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
85     llvm::Value *MemPtr = EmitScalarExpr(E);
86     return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
87   }
88 
89   QualType BoolTy = getContext().BoolTy;
90   if (!E->getType()->isAnyComplexType())
91     return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
92 
93   return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
94 }
95 
96 /// EmitIgnoredExpr - Emit code to compute the specified expression,
97 /// ignoring the result.
98 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
99   if (E->isRValue())
100     return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
101 
102   // Just emit it as an l-value and drop the result.
103   EmitLValue(E);
104 }
105 
106 /// EmitAnyExpr - Emit code to compute the specified expression which
107 /// can have any type.  The result is returned as an RValue struct.
108 /// If this is an aggregate expression, AggSlot indicates where the
109 /// result should be returned.
110 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, AggValueSlot AggSlot,
111                                     bool IgnoreResult) {
112   if (!hasAggregateLLVMType(E->getType()))
113     return RValue::get(EmitScalarExpr(E, IgnoreResult));
114   else if (E->getType()->isAnyComplexType())
115     return RValue::getComplex(EmitComplexExpr(E, IgnoreResult, IgnoreResult));
116 
117   EmitAggExpr(E, AggSlot, IgnoreResult);
118   return AggSlot.asRValue();
119 }
120 
121 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
122 /// always be accessible even if no aggregate location is provided.
123 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
124   AggValueSlot AggSlot = AggValueSlot::ignored();
125 
126   if (hasAggregateLLVMType(E->getType()) &&
127       !E->getType()->isAnyComplexType())
128     AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
129   return EmitAnyExpr(E, AggSlot);
130 }
131 
132 /// EmitAnyExprToMem - Evaluate an expression into a given memory
133 /// location.
134 void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
135                                        llvm::Value *Location,
136                                        Qualifiers Quals,
137                                        bool IsInit) {
138   if (E->getType()->isAnyComplexType())
139     EmitComplexExprIntoAddr(E, Location, Quals.hasVolatile());
140   else if (hasAggregateLLVMType(E->getType()))
141     EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals,
142                                          AggValueSlot::IsDestructed_t(IsInit),
143                                          AggValueSlot::DoesNotNeedGCBarriers,
144                                          AggValueSlot::IsAliased_t(!IsInit)));
145   else {
146     RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
147     LValue LV = MakeAddrLValue(Location, E->getType());
148     EmitStoreThroughLValue(RV, LV);
149   }
150 }
151 
152 namespace {
153 /// \brief An adjustment to be made to the temporary created when emitting a
154 /// reference binding, which accesses a particular subobject of that temporary.
155   struct SubobjectAdjustment {
156     enum { DerivedToBaseAdjustment, FieldAdjustment } Kind;
157 
158     union {
159       struct {
160         const CastExpr *BasePath;
161         const CXXRecordDecl *DerivedClass;
162       } DerivedToBase;
163 
164       FieldDecl *Field;
165     };
166 
167     SubobjectAdjustment(const CastExpr *BasePath,
168                         const CXXRecordDecl *DerivedClass)
169       : Kind(DerivedToBaseAdjustment) {
170       DerivedToBase.BasePath = BasePath;
171       DerivedToBase.DerivedClass = DerivedClass;
172     }
173 
174     SubobjectAdjustment(FieldDecl *Field)
175       : Kind(FieldAdjustment) {
176       this->Field = Field;
177     }
178   };
179 }
180 
181 static llvm::Value *
182 CreateReferenceTemporary(CodeGenFunction &CGF, QualType Type,
183                          const NamedDecl *InitializedDecl) {
184   if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
185     if (VD->hasGlobalStorage()) {
186       llvm::SmallString<256> Name;
187       llvm::raw_svector_ostream Out(Name);
188       CGF.CGM.getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out);
189       Out.flush();
190 
191       llvm::Type *RefTempTy = CGF.ConvertTypeForMem(Type);
192 
193       // Create the reference temporary.
194       llvm::GlobalValue *RefTemp =
195         new llvm::GlobalVariable(CGF.CGM.getModule(),
196                                  RefTempTy, /*isConstant=*/false,
197                                  llvm::GlobalValue::InternalLinkage,
198                                  llvm::Constant::getNullValue(RefTempTy),
199                                  Name.str());
200       return RefTemp;
201     }
202   }
203 
204   return CGF.CreateMemTemp(Type, "ref.tmp");
205 }
206 
207 static llvm::Value *
208 EmitExprForReferenceBinding(CodeGenFunction &CGF, const Expr *E,
209                             llvm::Value *&ReferenceTemporary,
210                             const CXXDestructorDecl *&ReferenceTemporaryDtor,
211                             QualType &ObjCARCReferenceLifetimeType,
212                             const NamedDecl *InitializedDecl) {
213   // Look through expressions for materialized temporaries (for now).
214   if (const MaterializeTemporaryExpr *M
215                                       = dyn_cast<MaterializeTemporaryExpr>(E)) {
216     // Objective-C++ ARC:
217     //   If we are binding a reference to a temporary that has ownership, we
218     //   need to perform retain/release operations on the temporary.
219     if (CGF.getContext().getLangOptions().ObjCAutoRefCount &&
220         E->getType()->isObjCLifetimeType() &&
221         (E->getType().getObjCLifetime() == Qualifiers::OCL_Strong ||
222          E->getType().getObjCLifetime() == Qualifiers::OCL_Weak ||
223          E->getType().getObjCLifetime() == Qualifiers::OCL_Autoreleasing))
224       ObjCARCReferenceLifetimeType = E->getType();
225 
226     E = M->GetTemporaryExpr();
227   }
228 
229   if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
230     E = DAE->getExpr();
231 
232   if (const ExprWithCleanups *TE = dyn_cast<ExprWithCleanups>(E)) {
233     CodeGenFunction::RunCleanupsScope Scope(CGF);
234 
235     return EmitExprForReferenceBinding(CGF, TE->getSubExpr(),
236                                        ReferenceTemporary,
237                                        ReferenceTemporaryDtor,
238                                        ObjCARCReferenceLifetimeType,
239                                        InitializedDecl);
240   }
241 
242   RValue RV;
243   if (E->isGLValue()) {
244     // Emit the expression as an lvalue.
245     LValue LV = CGF.EmitLValue(E);
246 
247     if (LV.isSimple())
248       return LV.getAddress();
249 
250     // We have to load the lvalue.
251     RV = CGF.EmitLoadOfLValue(LV);
252   } else {
253     if (!ObjCARCReferenceLifetimeType.isNull()) {
254       ReferenceTemporary = CreateReferenceTemporary(CGF,
255                                                   ObjCARCReferenceLifetimeType,
256                                                     InitializedDecl);
257 
258 
259       LValue RefTempDst = CGF.MakeAddrLValue(ReferenceTemporary,
260                                              ObjCARCReferenceLifetimeType);
261 
262       CGF.EmitScalarInit(E, dyn_cast_or_null<ValueDecl>(InitializedDecl),
263                          RefTempDst, false);
264 
265       bool ExtendsLifeOfTemporary = false;
266       if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(InitializedDecl)) {
267         if (Var->extendsLifetimeOfTemporary())
268           ExtendsLifeOfTemporary = true;
269       } else if (InitializedDecl && isa<FieldDecl>(InitializedDecl)) {
270         ExtendsLifeOfTemporary = true;
271       }
272 
273       if (!ExtendsLifeOfTemporary) {
274         // Since the lifetime of this temporary isn't going to be extended,
275         // we need to clean it up ourselves at the end of the full expression.
276         switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
277         case Qualifiers::OCL_None:
278         case Qualifiers::OCL_ExplicitNone:
279         case Qualifiers::OCL_Autoreleasing:
280           break;
281 
282         case Qualifiers::OCL_Strong: {
283           assert(!ObjCARCReferenceLifetimeType->isArrayType());
284           CleanupKind cleanupKind = CGF.getARCCleanupKind();
285           CGF.pushDestroy(cleanupKind,
286                           ReferenceTemporary,
287                           ObjCARCReferenceLifetimeType,
288                           CodeGenFunction::destroyARCStrongImprecise,
289                           cleanupKind & EHCleanup);
290           break;
291         }
292 
293         case Qualifiers::OCL_Weak:
294           assert(!ObjCARCReferenceLifetimeType->isArrayType());
295           CGF.pushDestroy(NormalAndEHCleanup,
296                           ReferenceTemporary,
297                           ObjCARCReferenceLifetimeType,
298                           CodeGenFunction::destroyARCWeak,
299                           /*useEHCleanupForArray*/ true);
300           break;
301         }
302 
303         ObjCARCReferenceLifetimeType = QualType();
304       }
305 
306       return ReferenceTemporary;
307     }
308 
309     SmallVector<SubobjectAdjustment, 2> Adjustments;
310     while (true) {
311       E = E->IgnoreParens();
312 
313       if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
314         if ((CE->getCastKind() == CK_DerivedToBase ||
315              CE->getCastKind() == CK_UncheckedDerivedToBase) &&
316             E->getType()->isRecordType()) {
317           E = CE->getSubExpr();
318           CXXRecordDecl *Derived
319             = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
320           Adjustments.push_back(SubobjectAdjustment(CE, Derived));
321           continue;
322         }
323 
324         if (CE->getCastKind() == CK_NoOp) {
325           E = CE->getSubExpr();
326           continue;
327         }
328       } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
329         if (!ME->isArrow() && ME->getBase()->isRValue()) {
330           assert(ME->getBase()->getType()->isRecordType());
331           if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
332             E = ME->getBase();
333             Adjustments.push_back(SubobjectAdjustment(Field));
334             continue;
335           }
336         }
337       }
338 
339       if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E))
340         if (opaque->getType()->isRecordType())
341           return CGF.EmitOpaqueValueLValue(opaque).getAddress();
342 
343       // Nothing changed.
344       break;
345     }
346 
347     // Create a reference temporary if necessary.
348     AggValueSlot AggSlot = AggValueSlot::ignored();
349     if (CGF.hasAggregateLLVMType(E->getType()) &&
350         !E->getType()->isAnyComplexType()) {
351       ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
352                                                     InitializedDecl);
353       AggValueSlot::IsDestructed_t isDestructed
354         = AggValueSlot::IsDestructed_t(InitializedDecl != 0);
355       AggSlot = AggValueSlot::forAddr(ReferenceTemporary, Qualifiers(),
356                                       isDestructed,
357                                       AggValueSlot::DoesNotNeedGCBarriers,
358                                       AggValueSlot::IsNotAliased);
359     }
360 
361     if (InitializedDecl) {
362       // Get the destructor for the reference temporary.
363       if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
364         CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
365         if (!ClassDecl->hasTrivialDestructor())
366           ReferenceTemporaryDtor = ClassDecl->getDestructor();
367       }
368     }
369 
370     RV = CGF.EmitAnyExpr(E, AggSlot);
371 
372     // Check if need to perform derived-to-base casts and/or field accesses, to
373     // get from the temporary object we created (and, potentially, for which we
374     // extended the lifetime) to the subobject we're binding the reference to.
375     if (!Adjustments.empty()) {
376       llvm::Value *Object = RV.getAggregateAddr();
377       for (unsigned I = Adjustments.size(); I != 0; --I) {
378         SubobjectAdjustment &Adjustment = Adjustments[I-1];
379         switch (Adjustment.Kind) {
380         case SubobjectAdjustment::DerivedToBaseAdjustment:
381           Object =
382               CGF.GetAddressOfBaseClass(Object,
383                                         Adjustment.DerivedToBase.DerivedClass,
384                               Adjustment.DerivedToBase.BasePath->path_begin(),
385                               Adjustment.DerivedToBase.BasePath->path_end(),
386                                         /*NullCheckValue=*/false);
387           break;
388 
389         case SubobjectAdjustment::FieldAdjustment: {
390           LValue LV =
391             CGF.EmitLValueForField(Object, Adjustment.Field, 0);
392           if (LV.isSimple()) {
393             Object = LV.getAddress();
394             break;
395           }
396 
397           // For non-simple lvalues, we actually have to create a copy of
398           // the object we're binding to.
399           QualType T = Adjustment.Field->getType().getNonReferenceType()
400                                                   .getUnqualifiedType();
401           Object = CreateReferenceTemporary(CGF, T, InitializedDecl);
402           LValue TempLV = CGF.MakeAddrLValue(Object,
403                                              Adjustment.Field->getType());
404           CGF.EmitStoreThroughLValue(CGF.EmitLoadOfLValue(LV), TempLV);
405           break;
406         }
407 
408         }
409       }
410 
411       return Object;
412     }
413   }
414 
415   if (RV.isAggregate())
416     return RV.getAggregateAddr();
417 
418   // Create a temporary variable that we can bind the reference to.
419   ReferenceTemporary = CreateReferenceTemporary(CGF, E->getType(),
420                                                 InitializedDecl);
421 
422 
423   unsigned Alignment =
424     CGF.getContext().getTypeAlignInChars(E->getType()).getQuantity();
425   if (RV.isScalar())
426     CGF.EmitStoreOfScalar(RV.getScalarVal(), ReferenceTemporary,
427                           /*Volatile=*/false, Alignment, E->getType());
428   else
429     CGF.StoreComplexToAddr(RV.getComplexVal(), ReferenceTemporary,
430                            /*Volatile=*/false);
431   return ReferenceTemporary;
432 }
433 
434 RValue
435 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E,
436                                             const NamedDecl *InitializedDecl) {
437   llvm::Value *ReferenceTemporary = 0;
438   const CXXDestructorDecl *ReferenceTemporaryDtor = 0;
439   QualType ObjCARCReferenceLifetimeType;
440   llvm::Value *Value = EmitExprForReferenceBinding(*this, E, ReferenceTemporary,
441                                                    ReferenceTemporaryDtor,
442                                                    ObjCARCReferenceLifetimeType,
443                                                    InitializedDecl);
444   if (!ReferenceTemporaryDtor && ObjCARCReferenceLifetimeType.isNull())
445     return RValue::get(Value);
446 
447   // Make sure to call the destructor for the reference temporary.
448   const VarDecl *VD = dyn_cast_or_null<VarDecl>(InitializedDecl);
449   if (VD && VD->hasGlobalStorage()) {
450     if (ReferenceTemporaryDtor) {
451       llvm::Constant *DtorFn =
452         CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
453       EmitCXXGlobalDtorRegistration(DtorFn,
454                                     cast<llvm::Constant>(ReferenceTemporary));
455     } else {
456       assert(!ObjCARCReferenceLifetimeType.isNull());
457       // Note: We intentionally do not register a global "destructor" to
458       // release the object.
459     }
460 
461     return RValue::get(Value);
462   }
463 
464   if (ReferenceTemporaryDtor)
465     PushDestructorCleanup(ReferenceTemporaryDtor, ReferenceTemporary);
466   else {
467     switch (ObjCARCReferenceLifetimeType.getObjCLifetime()) {
468     case Qualifiers::OCL_None:
469       llvm_unreachable(
470                       "Not a reference temporary that needs to be deallocated");
471     case Qualifiers::OCL_ExplicitNone:
472     case Qualifiers::OCL_Autoreleasing:
473       // Nothing to do.
474       break;
475 
476     case Qualifiers::OCL_Strong: {
477       bool precise = VD && VD->hasAttr<ObjCPreciseLifetimeAttr>();
478       CleanupKind cleanupKind = getARCCleanupKind();
479       // This local is a GCC and MSVC compiler workaround.
480       Destroyer *destroyer = precise ? &destroyARCStrongPrecise :
481                                        &destroyARCStrongImprecise;
482       pushDestroy(cleanupKind, ReferenceTemporary, ObjCARCReferenceLifetimeType,
483                   *destroyer, cleanupKind & EHCleanup);
484       break;
485     }
486 
487     case Qualifiers::OCL_Weak: {
488       // This local is a GCC and MSVC compiler workaround.
489       Destroyer *destroyer = &destroyARCWeak;
490       // __weak objects always get EH cleanups; otherwise, exceptions
491       // could cause really nasty crashes instead of mere leaks.
492       pushDestroy(NormalAndEHCleanup, ReferenceTemporary,
493                   ObjCARCReferenceLifetimeType, *destroyer, true);
494       break;
495     }
496     }
497   }
498 
499   return RValue::get(Value);
500 }
501 
502 
503 /// getAccessedFieldNo - Given an encoded value and a result number, return the
504 /// input field number being accessed.
505 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
506                                              const llvm::Constant *Elts) {
507   if (isa<llvm::ConstantAggregateZero>(Elts))
508     return 0;
509 
510   return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
511 }
512 
513 void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
514   if (!CatchUndefined)
515     return;
516 
517   // This needs to be to the standard address space.
518   Address = Builder.CreateBitCast(Address, Int8PtrTy);
519 
520   llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy);
521 
522   // In time, people may want to control this and use a 1 here.
523   llvm::Value *Arg = Builder.getFalse();
524   llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
525   llvm::BasicBlock *Cont = createBasicBlock();
526   llvm::BasicBlock *Check = createBasicBlock();
527   llvm::Value *NegativeOne = llvm::ConstantInt::get(IntPtrTy, -1ULL);
528   Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
529 
530   EmitBlock(Check);
531   Builder.CreateCondBr(Builder.CreateICmpUGE(C,
532                                         llvm::ConstantInt::get(IntPtrTy, Size)),
533                        Cont, getTrapBB());
534   EmitBlock(Cont);
535 }
536 
537 
538 CodeGenFunction::ComplexPairTy CodeGenFunction::
539 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
540                          bool isInc, bool isPre) {
541   ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
542                                             LV.isVolatileQualified());
543 
544   llvm::Value *NextVal;
545   if (isa<llvm::IntegerType>(InVal.first->getType())) {
546     uint64_t AmountVal = isInc ? 1 : -1;
547     NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
548 
549     // Add the inc/dec to the real part.
550     NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
551   } else {
552     QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
553     llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
554     if (!isInc)
555       FVal.changeSign();
556     NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
557 
558     // Add the inc/dec to the real part.
559     NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
560   }
561 
562   ComplexPairTy IncVal(NextVal, InVal.second);
563 
564   // Store the updated result through the lvalue.
565   StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
566 
567   // If this is a postinc, return the value read from memory, otherwise use the
568   // updated value.
569   return isPre ? IncVal : InVal;
570 }
571 
572 
573 //===----------------------------------------------------------------------===//
574 //                         LValue Expression Emission
575 //===----------------------------------------------------------------------===//
576 
577 RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
578   if (Ty->isVoidType())
579     return RValue::get(0);
580 
581   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
582     llvm::Type *EltTy = ConvertType(CTy->getElementType());
583     llvm::Value *U = llvm::UndefValue::get(EltTy);
584     return RValue::getComplex(std::make_pair(U, U));
585   }
586 
587   // If this is a use of an undefined aggregate type, the aggregate must have an
588   // identifiable address.  Just because the contents of the value are undefined
589   // doesn't mean that the address can't be taken and compared.
590   if (hasAggregateLLVMType(Ty)) {
591     llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
592     return RValue::getAggregate(DestPtr);
593   }
594 
595   return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
596 }
597 
598 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
599                                               const char *Name) {
600   ErrorUnsupported(E, Name);
601   return GetUndefRValue(E->getType());
602 }
603 
604 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
605                                               const char *Name) {
606   ErrorUnsupported(E, Name);
607   llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
608   return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
609 }
610 
611 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
612   LValue LV = EmitLValue(E);
613   if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
614     EmitCheck(LV.getAddress(),
615               getContext().getTypeSizeInChars(E->getType()).getQuantity());
616   return LV;
617 }
618 
619 /// EmitLValue - Emit code to compute a designator that specifies the location
620 /// of the expression.
621 ///
622 /// This can return one of two things: a simple address or a bitfield reference.
623 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be
624 /// an LLVM pointer type.
625 ///
626 /// If this returns a bitfield reference, nothing about the pointee type of the
627 /// LLVM value is known: For example, it may not be a pointer to an integer.
628 ///
629 /// If this returns a normal address, and if the lvalue's C type is fixed size,
630 /// this method guarantees that the returned pointer type will point to an LLVM
631 /// type of the same size of the lvalue's type.  If the lvalue has a variable
632 /// length type, this is not possible.
633 ///
634 LValue CodeGenFunction::EmitLValue(const Expr *E) {
635   switch (E->getStmtClass()) {
636   default: return EmitUnsupportedLValue(E, "l-value expression");
637 
638   case Expr::ObjCPropertyRefExprClass:
639     llvm_unreachable("cannot emit a property reference directly");
640 
641   case Expr::ObjCSelectorExprClass:
642   return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
643   case Expr::ObjCIsaExprClass:
644     return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
645   case Expr::BinaryOperatorClass:
646     return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
647   case Expr::CompoundAssignOperatorClass:
648     if (!E->getType()->isAnyComplexType())
649       return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
650     return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
651   case Expr::CallExprClass:
652   case Expr::CXXMemberCallExprClass:
653   case Expr::CXXOperatorCallExprClass:
654     return EmitCallExprLValue(cast<CallExpr>(E));
655   case Expr::VAArgExprClass:
656     return EmitVAArgExprLValue(cast<VAArgExpr>(E));
657   case Expr::DeclRefExprClass:
658     return EmitDeclRefLValue(cast<DeclRefExpr>(E));
659   case Expr::ParenExprClass:
660     return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
661   case Expr::GenericSelectionExprClass:
662     return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
663   case Expr::PredefinedExprClass:
664     return EmitPredefinedLValue(cast<PredefinedExpr>(E));
665   case Expr::StringLiteralClass:
666     return EmitStringLiteralLValue(cast<StringLiteral>(E));
667   case Expr::ObjCEncodeExprClass:
668     return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
669   case Expr::PseudoObjectExprClass:
670     return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
671 
672   case Expr::BlockDeclRefExprClass:
673     return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
674 
675   case Expr::CXXTemporaryObjectExprClass:
676   case Expr::CXXConstructExprClass:
677     return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
678   case Expr::CXXBindTemporaryExprClass:
679     return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
680   case Expr::ExprWithCleanupsClass:
681     return EmitExprWithCleanupsLValue(cast<ExprWithCleanups>(E));
682   case Expr::CXXScalarValueInitExprClass:
683     return EmitNullInitializationLValue(cast<CXXScalarValueInitExpr>(E));
684   case Expr::CXXDefaultArgExprClass:
685     return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
686   case Expr::CXXTypeidExprClass:
687     return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
688 
689   case Expr::ObjCMessageExprClass:
690     return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
691   case Expr::ObjCIvarRefExprClass:
692     return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
693   case Expr::StmtExprClass:
694     return EmitStmtExprLValue(cast<StmtExpr>(E));
695   case Expr::UnaryOperatorClass:
696     return EmitUnaryOpLValue(cast<UnaryOperator>(E));
697   case Expr::ArraySubscriptExprClass:
698     return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
699   case Expr::ExtVectorElementExprClass:
700     return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
701   case Expr::MemberExprClass:
702     return EmitMemberExpr(cast<MemberExpr>(E));
703   case Expr::CompoundLiteralExprClass:
704     return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
705   case Expr::ConditionalOperatorClass:
706     return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
707   case Expr::BinaryConditionalOperatorClass:
708     return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
709   case Expr::ChooseExprClass:
710     return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
711   case Expr::OpaqueValueExprClass:
712     return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
713   case Expr::SubstNonTypeTemplateParmExprClass:
714     return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
715   case Expr::ImplicitCastExprClass:
716   case Expr::CStyleCastExprClass:
717   case Expr::CXXFunctionalCastExprClass:
718   case Expr::CXXStaticCastExprClass:
719   case Expr::CXXDynamicCastExprClass:
720   case Expr::CXXReinterpretCastExprClass:
721   case Expr::CXXConstCastExprClass:
722   case Expr::ObjCBridgedCastExprClass:
723     return EmitCastLValue(cast<CastExpr>(E));
724 
725   case Expr::MaterializeTemporaryExprClass:
726     return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
727   }
728 }
729 
730 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) {
731   return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
732                           lvalue.getAlignment(), lvalue.getType(),
733                           lvalue.getTBAAInfo());
734 }
735 
736 llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
737                                               unsigned Alignment, QualType Ty,
738                                               llvm::MDNode *TBAAInfo) {
739   llvm::LoadInst *Load = Builder.CreateLoad(Addr);
740   if (Volatile)
741     Load->setVolatile(true);
742   if (Alignment)
743     Load->setAlignment(Alignment);
744   if (TBAAInfo)
745     CGM.DecorateInstruction(Load, TBAAInfo);
746 
747   return EmitFromMemory(Load, Ty);
748 }
749 
750 static bool isBooleanUnderlyingType(QualType Ty) {
751   if (const EnumType *ET = dyn_cast<EnumType>(Ty))
752     return ET->getDecl()->getIntegerType()->isBooleanType();
753   return false;
754 }
755 
756 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
757   // Bool has a different representation in memory than in registers.
758   if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
759     // This should really always be an i1, but sometimes it's already
760     // an i8, and it's awkward to track those cases down.
761     if (Value->getType()->isIntegerTy(1))
762       return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool");
763     assert(Value->getType()->isIntegerTy(8) && "value rep of bool not i1/i8");
764   }
765 
766   return Value;
767 }
768 
769 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
770   // Bool has a different representation in memory than in registers.
771   if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
772     assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8");
773     return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
774   }
775 
776   return Value;
777 }
778 
779 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
780                                         bool Volatile, unsigned Alignment,
781                                         QualType Ty,
782                                         llvm::MDNode *TBAAInfo) {
783   Value = EmitToMemory(Value, Ty);
784 
785   llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
786   if (Alignment)
787     Store->setAlignment(Alignment);
788   if (TBAAInfo)
789     CGM.DecorateInstruction(Store, TBAAInfo);
790 }
791 
792 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue) {
793   EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
794                     lvalue.getAlignment(), lvalue.getType(),
795                     lvalue.getTBAAInfo());
796 }
797 
798 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
799 /// method emits the address of the lvalue, then loads the result as an rvalue,
800 /// returning the rvalue.
801 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV) {
802   if (LV.isObjCWeak()) {
803     // load of a __weak object.
804     llvm::Value *AddrWeakObj = LV.getAddress();
805     return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
806                                                              AddrWeakObj));
807   }
808   if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak)
809     return RValue::get(EmitARCLoadWeak(LV.getAddress()));
810 
811   if (LV.isSimple()) {
812     assert(!LV.getType()->isFunctionType());
813 
814     // Everything needs a load.
815     return RValue::get(EmitLoadOfScalar(LV));
816   }
817 
818   if (LV.isVectorElt()) {
819     llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
820                                           LV.isVolatileQualified());
821     return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
822                                                     "vecext"));
823   }
824 
825   // If this is a reference to a subset of the elements of a vector, either
826   // shuffle the input or extract/insert them as appropriate.
827   if (LV.isExtVectorElt())
828     return EmitLoadOfExtVectorElementLValue(LV);
829 
830   assert(LV.isBitField() && "Unknown LValue type!");
831   return EmitLoadOfBitfieldLValue(LV);
832 }
833 
834 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
835   const CGBitFieldInfo &Info = LV.getBitFieldInfo();
836 
837   // Get the output type.
838   llvm::Type *ResLTy = ConvertType(LV.getType());
839   unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
840 
841   // Compute the result as an OR of all of the individual component accesses.
842   llvm::Value *Res = 0;
843   for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
844     const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
845 
846     // Get the field pointer.
847     llvm::Value *Ptr = LV.getBitFieldBaseAddr();
848 
849     // Only offset by the field index if used, so that incoming values are not
850     // required to be structures.
851     if (AI.FieldIndex)
852       Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
853 
854     // Offset by the byte offset, if used.
855     if (!AI.FieldByteOffset.isZero()) {
856       Ptr = EmitCastToVoidPtr(Ptr);
857       Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
858                                        "bf.field.offs");
859     }
860 
861     // Cast to the access type.
862     llvm::Type *PTy = llvm::Type::getIntNPtrTy(getLLVMContext(),
863                                                      AI.AccessWidth,
864                        CGM.getContext().getTargetAddressSpace(LV.getType()));
865     Ptr = Builder.CreateBitCast(Ptr, PTy);
866 
867     // Perform the load.
868     llvm::LoadInst *Load = Builder.CreateLoad(Ptr, LV.isVolatileQualified());
869     if (!AI.AccessAlignment.isZero())
870       Load->setAlignment(AI.AccessAlignment.getQuantity());
871 
872     // Shift out unused low bits and mask out unused high bits.
873     llvm::Value *Val = Load;
874     if (AI.FieldBitStart)
875       Val = Builder.CreateLShr(Load, AI.FieldBitStart);
876     Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(AI.AccessWidth,
877                                                             AI.TargetBitWidth),
878                             "bf.clear");
879 
880     // Extend or truncate to the target size.
881     if (AI.AccessWidth < ResSizeInBits)
882       Val = Builder.CreateZExt(Val, ResLTy);
883     else if (AI.AccessWidth > ResSizeInBits)
884       Val = Builder.CreateTrunc(Val, ResLTy);
885 
886     // Shift into place, and OR into the result.
887     if (AI.TargetBitOffset)
888       Val = Builder.CreateShl(Val, AI.TargetBitOffset);
889     Res = Res ? Builder.CreateOr(Res, Val) : Val;
890   }
891 
892   // If the bit-field is signed, perform the sign-extension.
893   //
894   // FIXME: This can easily be folded into the load of the high bits, which
895   // could also eliminate the mask of high bits in some situations.
896   if (Info.isSigned()) {
897     unsigned ExtraBits = ResSizeInBits - Info.getSize();
898     if (ExtraBits)
899       Res = Builder.CreateAShr(Builder.CreateShl(Res, ExtraBits),
900                                ExtraBits, "bf.val.sext");
901   }
902 
903   return RValue::get(Res);
904 }
905 
906 // If this is a reference to a subset of the elements of a vector, create an
907 // appropriate shufflevector.
908 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
909   llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
910                                         LV.isVolatileQualified());
911 
912   const llvm::Constant *Elts = LV.getExtVectorElts();
913 
914   // If the result of the expression is a non-vector type, we must be extracting
915   // a single element.  Just codegen as an extractelement.
916   const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
917   if (!ExprVT) {
918     unsigned InIdx = getAccessedFieldNo(0, Elts);
919     llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
920     return RValue::get(Builder.CreateExtractElement(Vec, Elt));
921   }
922 
923   // Always use shuffle vector to try to retain the original program structure
924   unsigned NumResultElts = ExprVT->getNumElements();
925 
926   SmallVector<llvm::Constant*, 4> Mask;
927   for (unsigned i = 0; i != NumResultElts; ++i) {
928     unsigned InIdx = getAccessedFieldNo(i, Elts);
929     Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx));
930   }
931 
932   llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
933   Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
934                                     MaskV);
935   return RValue::get(Vec);
936 }
937 
938 
939 
940 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
941 /// lvalue, where both are guaranteed to the have the same type, and that type
942 /// is 'Ty'.
943 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst) {
944   if (!Dst.isSimple()) {
945     if (Dst.isVectorElt()) {
946       // Read/modify/write the vector, inserting the new element.
947       llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
948                                             Dst.isVolatileQualified());
949       Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
950                                         Dst.getVectorIdx(), "vecins");
951       Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
952       return;
953     }
954 
955     // If this is an update of extended vector elements, insert them as
956     // appropriate.
957     if (Dst.isExtVectorElt())
958       return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
959 
960     assert(Dst.isBitField() && "Unknown LValue type");
961     return EmitStoreThroughBitfieldLValue(Src, Dst);
962   }
963 
964   // There's special magic for assigning into an ARC-qualified l-value.
965   if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
966     switch (Lifetime) {
967     case Qualifiers::OCL_None:
968       llvm_unreachable("present but none");
969 
970     case Qualifiers::OCL_ExplicitNone:
971       // nothing special
972       break;
973 
974     case Qualifiers::OCL_Strong:
975       EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
976       return;
977 
978     case Qualifiers::OCL_Weak:
979       EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
980       return;
981 
982     case Qualifiers::OCL_Autoreleasing:
983       Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
984                                                      Src.getScalarVal()));
985       // fall into the normal path
986       break;
987     }
988   }
989 
990   if (Dst.isObjCWeak() && !Dst.isNonGC()) {
991     // load of a __weak object.
992     llvm::Value *LvalueDst = Dst.getAddress();
993     llvm::Value *src = Src.getScalarVal();
994      CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
995     return;
996   }
997 
998   if (Dst.isObjCStrong() && !Dst.isNonGC()) {
999     // load of a __strong object.
1000     llvm::Value *LvalueDst = Dst.getAddress();
1001     llvm::Value *src = Src.getScalarVal();
1002     if (Dst.isObjCIvar()) {
1003       assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1004       llvm::Type *ResultType = ConvertType(getContext().LongTy);
1005       llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1006       llvm::Value *dst = RHS;
1007       RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1008       llvm::Value *LHS =
1009         Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1010       llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1011       CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1012                                               BytesBetween);
1013     } else if (Dst.isGlobalObjCRef()) {
1014       CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1015                                                 Dst.isThreadLocalRef());
1016     }
1017     else
1018       CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1019     return;
1020   }
1021 
1022   assert(Src.isScalar() && "Can't emit an agg store with this method");
1023   EmitStoreOfScalar(Src.getScalarVal(), Dst);
1024 }
1025 
1026 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1027                                                      llvm::Value **Result) {
1028   const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1029 
1030   // Get the output type.
1031   llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1032   unsigned ResSizeInBits = CGM.getTargetData().getTypeSizeInBits(ResLTy);
1033 
1034   // Get the source value, truncated to the width of the bit-field.
1035   llvm::Value *SrcVal = Src.getScalarVal();
1036 
1037   if (Dst.getType()->isBooleanType())
1038     SrcVal = Builder.CreateIntCast(SrcVal, ResLTy, /*IsSigned=*/false);
1039 
1040   SrcVal = Builder.CreateAnd(SrcVal, llvm::APInt::getLowBitsSet(ResSizeInBits,
1041                                                                 Info.getSize()),
1042                              "bf.value");
1043 
1044   // Return the new value of the bit-field, if requested.
1045   if (Result) {
1046     // Cast back to the proper type for result.
1047     llvm::Type *SrcTy = Src.getScalarVal()->getType();
1048     llvm::Value *ReloadVal = Builder.CreateIntCast(SrcVal, SrcTy, false,
1049                                                    "bf.reload.val");
1050 
1051     // Sign extend if necessary.
1052     if (Info.isSigned()) {
1053       unsigned ExtraBits = ResSizeInBits - Info.getSize();
1054       if (ExtraBits)
1055         ReloadVal = Builder.CreateAShr(Builder.CreateShl(ReloadVal, ExtraBits),
1056                                        ExtraBits, "bf.reload.sext");
1057     }
1058 
1059     *Result = ReloadVal;
1060   }
1061 
1062   // Iterate over the components, writing each piece to memory.
1063   for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1064     const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1065 
1066     // Get the field pointer.
1067     llvm::Value *Ptr = Dst.getBitFieldBaseAddr();
1068     unsigned addressSpace =
1069       cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1070 
1071     // Only offset by the field index if used, so that incoming values are not
1072     // required to be structures.
1073     if (AI.FieldIndex)
1074       Ptr = Builder.CreateStructGEP(Ptr, AI.FieldIndex, "bf.field");
1075 
1076     // Offset by the byte offset, if used.
1077     if (!AI.FieldByteOffset.isZero()) {
1078       Ptr = EmitCastToVoidPtr(Ptr);
1079       Ptr = Builder.CreateConstGEP1_32(Ptr, AI.FieldByteOffset.getQuantity(),
1080                                        "bf.field.offs");
1081     }
1082 
1083     // Cast to the access type.
1084     llvm::Type *AccessLTy =
1085       llvm::Type::getIntNTy(getLLVMContext(), AI.AccessWidth);
1086 
1087     llvm::Type *PTy = AccessLTy->getPointerTo(addressSpace);
1088     Ptr = Builder.CreateBitCast(Ptr, PTy);
1089 
1090     // Extract the piece of the bit-field value to write in this access, limited
1091     // to the values that are part of this access.
1092     llvm::Value *Val = SrcVal;
1093     if (AI.TargetBitOffset)
1094       Val = Builder.CreateLShr(Val, AI.TargetBitOffset);
1095     Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(ResSizeInBits,
1096                                                             AI.TargetBitWidth));
1097 
1098     // Extend or truncate to the access size.
1099     if (ResSizeInBits < AI.AccessWidth)
1100       Val = Builder.CreateZExt(Val, AccessLTy);
1101     else if (ResSizeInBits > AI.AccessWidth)
1102       Val = Builder.CreateTrunc(Val, AccessLTy);
1103 
1104     // Shift into the position in memory.
1105     if (AI.FieldBitStart)
1106       Val = Builder.CreateShl(Val, AI.FieldBitStart);
1107 
1108     // If necessary, load and OR in bits that are outside of the bit-field.
1109     if (AI.TargetBitWidth != AI.AccessWidth) {
1110       llvm::LoadInst *Load = Builder.CreateLoad(Ptr, Dst.isVolatileQualified());
1111       if (!AI.AccessAlignment.isZero())
1112         Load->setAlignment(AI.AccessAlignment.getQuantity());
1113 
1114       // Compute the mask for zeroing the bits that are part of the bit-field.
1115       llvm::APInt InvMask =
1116         ~llvm::APInt::getBitsSet(AI.AccessWidth, AI.FieldBitStart,
1117                                  AI.FieldBitStart + AI.TargetBitWidth);
1118 
1119       // Apply the mask and OR in to the value to write.
1120       Val = Builder.CreateOr(Builder.CreateAnd(Load, InvMask), Val);
1121     }
1122 
1123     // Write the value.
1124     llvm::StoreInst *Store = Builder.CreateStore(Val, Ptr,
1125                                                  Dst.isVolatileQualified());
1126     if (!AI.AccessAlignment.isZero())
1127       Store->setAlignment(AI.AccessAlignment.getQuantity());
1128   }
1129 }
1130 
1131 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1132                                                                LValue Dst) {
1133   // This access turns into a read/modify/write of the vector.  Load the input
1134   // value now.
1135   llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
1136                                         Dst.isVolatileQualified());
1137   const llvm::Constant *Elts = Dst.getExtVectorElts();
1138 
1139   llvm::Value *SrcVal = Src.getScalarVal();
1140 
1141   if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1142     unsigned NumSrcElts = VTy->getNumElements();
1143     unsigned NumDstElts =
1144        cast<llvm::VectorType>(Vec->getType())->getNumElements();
1145     if (NumDstElts == NumSrcElts) {
1146       // Use shuffle vector is the src and destination are the same number of
1147       // elements and restore the vector mask since it is on the side it will be
1148       // stored.
1149       SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1150       for (unsigned i = 0; i != NumSrcElts; ++i) {
1151         unsigned InIdx = getAccessedFieldNo(i, Elts);
1152         Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i);
1153       }
1154 
1155       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1156       Vec = Builder.CreateShuffleVector(SrcVal,
1157                                         llvm::UndefValue::get(Vec->getType()),
1158                                         MaskV);
1159     } else if (NumDstElts > NumSrcElts) {
1160       // Extended the source vector to the same length and then shuffle it
1161       // into the destination.
1162       // FIXME: since we're shuffling with undef, can we just use the indices
1163       //        into that?  This could be simpler.
1164       SmallVector<llvm::Constant*, 4> ExtMask;
1165       unsigned i;
1166       for (i = 0; i != NumSrcElts; ++i)
1167         ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1168       for (; i != NumDstElts; ++i)
1169         ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
1170       llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1171       llvm::Value *ExtSrcVal =
1172         Builder.CreateShuffleVector(SrcVal,
1173                                     llvm::UndefValue::get(SrcVal->getType()),
1174                                     ExtMaskV);
1175       // build identity
1176       SmallVector<llvm::Constant*, 4> Mask;
1177       for (unsigned i = 0; i != NumDstElts; ++i)
1178         Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
1179 
1180       // modify when what gets shuffled in
1181       for (unsigned i = 0; i != NumSrcElts; ++i) {
1182         unsigned Idx = getAccessedFieldNo(i, Elts);
1183         Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
1184       }
1185       llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1186       Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
1187     } else {
1188       // We should never shorten the vector
1189       llvm_unreachable("unexpected shorten vector length");
1190     }
1191   } else {
1192     // If the Src is a scalar (not a vector) it must be updating one element.
1193     unsigned InIdx = getAccessedFieldNo(0, Elts);
1194     llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1195     Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
1196   }
1197 
1198   Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
1199 }
1200 
1201 // setObjCGCLValueClass - sets class of he lvalue for the purpose of
1202 // generating write-barries API. It is currently a global, ivar,
1203 // or neither.
1204 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1205                                  LValue &LV,
1206                                  bool IsMemberAccess=false) {
1207   if (Ctx.getLangOptions().getGC() == LangOptions::NonGC)
1208     return;
1209 
1210   if (isa<ObjCIvarRefExpr>(E)) {
1211     QualType ExpTy = E->getType();
1212     if (IsMemberAccess && ExpTy->isPointerType()) {
1213       // If ivar is a structure pointer, assigning to field of
1214       // this struct follows gcc's behavior and makes it a non-ivar
1215       // writer-barrier conservatively.
1216       ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1217       if (ExpTy->isRecordType()) {
1218         LV.setObjCIvar(false);
1219         return;
1220       }
1221     }
1222     LV.setObjCIvar(true);
1223     ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1224     LV.setBaseIvarExp(Exp->getBase());
1225     LV.setObjCArray(E->getType()->isArrayType());
1226     return;
1227   }
1228 
1229   if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1230     if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1231       if (VD->hasGlobalStorage()) {
1232         LV.setGlobalObjCRef(true);
1233         LV.setThreadLocalRef(VD->isThreadSpecified());
1234       }
1235     }
1236     LV.setObjCArray(E->getType()->isArrayType());
1237     return;
1238   }
1239 
1240   if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1241     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1242     return;
1243   }
1244 
1245   if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1246     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1247     if (LV.isObjCIvar()) {
1248       // If cast is to a structure pointer, follow gcc's behavior and make it
1249       // a non-ivar write-barrier.
1250       QualType ExpTy = E->getType();
1251       if (ExpTy->isPointerType())
1252         ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1253       if (ExpTy->isRecordType())
1254         LV.setObjCIvar(false);
1255     }
1256     return;
1257   }
1258 
1259   if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1260     setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1261     return;
1262   }
1263 
1264   if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1265     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1266     return;
1267   }
1268 
1269   if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1270     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1271     return;
1272   }
1273 
1274   if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1275     setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1276     return;
1277   }
1278 
1279   if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1280     setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1281     if (LV.isObjCIvar() && !LV.isObjCArray())
1282       // Using array syntax to assigning to what an ivar points to is not
1283       // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1284       LV.setObjCIvar(false);
1285     else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1286       // Using array syntax to assigning to what global points to is not
1287       // same as assigning to the global itself. {id *G;} G[i] = 0;
1288       LV.setGlobalObjCRef(false);
1289     return;
1290   }
1291 
1292   if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1293     setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
1294     // We don't know if member is an 'ivar', but this flag is looked at
1295     // only in the context of LV.isObjCIvar().
1296     LV.setObjCArray(E->getType()->isArrayType());
1297     return;
1298   }
1299 }
1300 
1301 static llvm::Value *
1302 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1303                                 llvm::Value *V, llvm::Type *IRType,
1304                                 StringRef Name = StringRef()) {
1305   unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1306   return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1307 }
1308 
1309 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1310                                       const Expr *E, const VarDecl *VD) {
1311   assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
1312          "Var decl must have external storage or be a file var decl!");
1313 
1314   llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1315   if (VD->getType()->isReferenceType())
1316     V = CGF.Builder.CreateLoad(V);
1317 
1318   V = EmitBitCastOfLValueToProperType(CGF, V,
1319                                 CGF.getTypes().ConvertTypeForMem(E->getType()));
1320 
1321   unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity();
1322   LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1323   setObjCGCLValueClass(CGF.getContext(), E, LV);
1324   return LV;
1325 }
1326 
1327 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1328                                      const Expr *E, const FunctionDecl *FD) {
1329   llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1330   if (!FD->hasPrototype()) {
1331     if (const FunctionProtoType *Proto =
1332             FD->getType()->getAs<FunctionProtoType>()) {
1333       // Ugly case: for a K&R-style definition, the type of the definition
1334       // isn't the same as the type of a use.  Correct for this with a
1335       // bitcast.
1336       QualType NoProtoType =
1337           CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1338       NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1339       V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType));
1340     }
1341   }
1342   unsigned Alignment = CGF.getContext().getDeclAlign(FD).getQuantity();
1343   return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1344 }
1345 
1346 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1347   const NamedDecl *ND = E->getDecl();
1348   unsigned Alignment = getContext().getDeclAlign(ND).getQuantity();
1349 
1350   if (ND->hasAttr<WeakRefAttr>()) {
1351     const ValueDecl *VD = cast<ValueDecl>(ND);
1352     llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1353     return MakeAddrLValue(Aliasee, E->getType(), Alignment);
1354   }
1355 
1356   if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1357 
1358     // Check if this is a global variable.
1359     if (VD->hasExternalStorage() || VD->isFileVarDecl())
1360       return EmitGlobalVarDeclLValue(*this, E, VD);
1361 
1362     bool NonGCable = VD->hasLocalStorage() &&
1363                      !VD->getType()->isReferenceType() &&
1364                      !VD->hasAttr<BlocksAttr>();
1365 
1366     llvm::Value *V = LocalDeclMap[VD];
1367     if (!V && VD->isStaticLocal())
1368       V = CGM.getStaticLocalDeclAddress(VD);
1369     assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1370 
1371     if (VD->hasAttr<BlocksAttr>())
1372       V = BuildBlockByrefAddress(V, VD);
1373 
1374     if (VD->getType()->isReferenceType())
1375       V = Builder.CreateLoad(V);
1376 
1377     V = EmitBitCastOfLValueToProperType(*this, V,
1378                                     getTypes().ConvertTypeForMem(E->getType()));
1379 
1380     LValue LV = MakeAddrLValue(V, E->getType(), Alignment);
1381     if (NonGCable) {
1382       LV.getQuals().removeObjCGCAttr();
1383       LV.setNonGC(true);
1384     }
1385     setObjCGCLValueClass(getContext(), E, LV);
1386     return LV;
1387   }
1388 
1389   if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND))
1390     return EmitFunctionDeclLValue(*this, E, fn);
1391 
1392   llvm_unreachable("Unhandled DeclRefExpr");
1393 
1394   // an invalid LValue, but the assert will
1395   // ensure that this point is never reached.
1396   return LValue();
1397 }
1398 
1399 LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
1400   unsigned Alignment =
1401     getContext().getDeclAlign(E->getDecl()).getQuantity();
1402   return MakeAddrLValue(GetAddrOfBlockDecl(E), E->getType(), Alignment);
1403 }
1404 
1405 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1406   // __extension__ doesn't affect lvalue-ness.
1407   if (E->getOpcode() == UO_Extension)
1408     return EmitLValue(E->getSubExpr());
1409 
1410   QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1411   switch (E->getOpcode()) {
1412   default: llvm_unreachable("Unknown unary operator lvalue!");
1413   case UO_Deref: {
1414     QualType T = E->getSubExpr()->getType()->getPointeeType();
1415     assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1416 
1417     LValue LV = MakeAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1418     LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1419 
1420     // We should not generate __weak write barrier on indirect reference
1421     // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1422     // But, we continue to generate __strong write barrier on indirect write
1423     // into a pointer to object.
1424     if (getContext().getLangOptions().ObjC1 &&
1425         getContext().getLangOptions().getGC() != LangOptions::NonGC &&
1426         LV.isObjCWeak())
1427       LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1428     return LV;
1429   }
1430   case UO_Real:
1431   case UO_Imag: {
1432     LValue LV = EmitLValue(E->getSubExpr());
1433     assert(LV.isSimple() && "real/imag on non-ordinary l-value");
1434     llvm::Value *Addr = LV.getAddress();
1435 
1436     // real and imag are valid on scalars.  This is a faster way of
1437     // testing that.
1438     if (!cast<llvm::PointerType>(Addr->getType())
1439            ->getElementType()->isStructTy()) {
1440       assert(E->getSubExpr()->getType()->isArithmeticType());
1441       return LV;
1442     }
1443 
1444     assert(E->getSubExpr()->getType()->isAnyComplexType());
1445 
1446     unsigned Idx = E->getOpcode() == UO_Imag;
1447     return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1448                                                   Idx, "idx"),
1449                           ExprTy);
1450   }
1451   case UO_PreInc:
1452   case UO_PreDec: {
1453     LValue LV = EmitLValue(E->getSubExpr());
1454     bool isInc = E->getOpcode() == UO_PreInc;
1455 
1456     if (E->getType()->isAnyComplexType())
1457       EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1458     else
1459       EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1460     return LV;
1461   }
1462   }
1463 }
1464 
1465 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1466   return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1467                         E->getType());
1468 }
1469 
1470 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1471   return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1472                         E->getType());
1473 }
1474 
1475 
1476 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1477   switch (E->getIdentType()) {
1478   default:
1479     return EmitUnsupportedLValue(E, "predefined expression");
1480 
1481   case PredefinedExpr::Func:
1482   case PredefinedExpr::Function:
1483   case PredefinedExpr::PrettyFunction: {
1484     unsigned Type = E->getIdentType();
1485     std::string GlobalVarName;
1486 
1487     switch (Type) {
1488     default: llvm_unreachable("Invalid type");
1489     case PredefinedExpr::Func:
1490       GlobalVarName = "__func__.";
1491       break;
1492     case PredefinedExpr::Function:
1493       GlobalVarName = "__FUNCTION__.";
1494       break;
1495     case PredefinedExpr::PrettyFunction:
1496       GlobalVarName = "__PRETTY_FUNCTION__.";
1497       break;
1498     }
1499 
1500     StringRef FnName = CurFn->getName();
1501     if (FnName.startswith("\01"))
1502       FnName = FnName.substr(1);
1503     GlobalVarName += FnName;
1504 
1505     const Decl *CurDecl = CurCodeDecl;
1506     if (CurDecl == 0)
1507       CurDecl = getContext().getTranslationUnitDecl();
1508 
1509     std::string FunctionName =
1510         (isa<BlockDecl>(CurDecl)
1511          ? FnName.str()
1512          : PredefinedExpr::ComputeName((PredefinedExpr::IdentType)Type, CurDecl));
1513 
1514     llvm::Constant *C =
1515       CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
1516     return MakeAddrLValue(C, E->getType());
1517   }
1518   }
1519 }
1520 
1521 llvm::BasicBlock *CodeGenFunction::getTrapBB() {
1522   const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1523 
1524   // If we are not optimzing, don't collapse all calls to trap in the function
1525   // to the same call, that way, in the debugger they can see which operation
1526   // did in fact fail.  If we are optimizing, we collapse all calls to trap down
1527   // to just one per function to save on codesize.
1528   if (GCO.OptimizationLevel && TrapBB)
1529     return TrapBB;
1530 
1531   llvm::BasicBlock *Cont = 0;
1532   if (HaveInsertPoint()) {
1533     Cont = createBasicBlock("cont");
1534     EmitBranch(Cont);
1535   }
1536   TrapBB = createBasicBlock("trap");
1537   EmitBlock(TrapBB);
1538 
1539   llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
1540   llvm::CallInst *TrapCall = Builder.CreateCall(F);
1541   TrapCall->setDoesNotReturn();
1542   TrapCall->setDoesNotThrow();
1543   Builder.CreateUnreachable();
1544 
1545   if (Cont)
1546     EmitBlock(Cont);
1547   return TrapBB;
1548 }
1549 
1550 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
1551 /// array to pointer, return the array subexpression.
1552 static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
1553   // If this isn't just an array->pointer decay, bail out.
1554   const CastExpr *CE = dyn_cast<CastExpr>(E);
1555   if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
1556     return 0;
1557 
1558   // If this is a decay from variable width array, bail out.
1559   const Expr *SubExpr = CE->getSubExpr();
1560   if (SubExpr->getType()->isVariableArrayType())
1561     return 0;
1562 
1563   return SubExpr;
1564 }
1565 
1566 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1567   // The index must always be an integer, which is not an aggregate.  Emit it.
1568   llvm::Value *Idx = EmitScalarExpr(E->getIdx());
1569   QualType IdxTy  = E->getIdx()->getType();
1570   bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
1571 
1572   // If the base is a vector type, then we are forming a vector element lvalue
1573   // with this subscript.
1574   if (E->getBase()->getType()->isVectorType()) {
1575     // Emit the vector as an lvalue to get its address.
1576     LValue LHS = EmitLValue(E->getBase());
1577     assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
1578     Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx");
1579     return LValue::MakeVectorElt(LHS.getAddress(), Idx,
1580                                  E->getBase()->getType());
1581   }
1582 
1583   // Extend or truncate the index type to 32 or 64-bits.
1584   if (Idx->getType() != IntPtrTy)
1585     Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
1586 
1587   // FIXME: As llvm implements the object size checking, this can come out.
1588   if (CatchUndefined) {
1589     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E->getBase())){
1590       if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1591         if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
1592           if (const ConstantArrayType *CAT
1593               = getContext().getAsConstantArrayType(DRE->getType())) {
1594             llvm::APInt Size = CAT->getSize();
1595             llvm::BasicBlock *Cont = createBasicBlock("cont");
1596             Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
1597                                   llvm::ConstantInt::get(Idx->getType(), Size)),
1598                                  Cont, getTrapBB());
1599             EmitBlock(Cont);
1600           }
1601         }
1602       }
1603     }
1604   }
1605 
1606   // We know that the pointer points to a type of the correct size, unless the
1607   // size is a VLA or Objective-C interface.
1608   llvm::Value *Address = 0;
1609   unsigned ArrayAlignment = 0;
1610   if (const VariableArrayType *vla =
1611         getContext().getAsVariableArrayType(E->getType())) {
1612     // The base must be a pointer, which is not an aggregate.  Emit
1613     // it.  It needs to be emitted first in case it's what captures
1614     // the VLA bounds.
1615     Address = EmitScalarExpr(E->getBase());
1616 
1617     // The element count here is the total number of non-VLA elements.
1618     llvm::Value *numElements = getVLASize(vla).first;
1619 
1620     // Effectively, the multiply by the VLA size is part of the GEP.
1621     // GEP indexes are signed, and scaling an index isn't permitted to
1622     // signed-overflow, so we use the same semantics for our explicit
1623     // multiply.  We suppress this if overflow is not undefined behavior.
1624     if (getLangOptions().isSignedOverflowDefined()) {
1625       Idx = Builder.CreateMul(Idx, numElements);
1626       Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1627     } else {
1628       Idx = Builder.CreateNSWMul(Idx, numElements);
1629       Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
1630     }
1631   } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
1632     // Indexing over an interface, as in "NSString *P; P[4];"
1633     llvm::Value *InterfaceSize =
1634       llvm::ConstantInt::get(Idx->getType(),
1635           getContext().getTypeSizeInChars(OIT).getQuantity());
1636 
1637     Idx = Builder.CreateMul(Idx, InterfaceSize);
1638 
1639     // The base must be a pointer, which is not an aggregate.  Emit it.
1640     llvm::Value *Base = EmitScalarExpr(E->getBase());
1641     Address = EmitCastToVoidPtr(Base);
1642     Address = Builder.CreateGEP(Address, Idx, "arrayidx");
1643     Address = Builder.CreateBitCast(Address, Base->getType());
1644   } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
1645     // If this is A[i] where A is an array, the frontend will have decayed the
1646     // base to be a ArrayToPointerDecay implicit cast.  While correct, it is
1647     // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
1648     // "gep x, i" here.  Emit one "gep A, 0, i".
1649     assert(Array->getType()->isArrayType() &&
1650            "Array to pointer decay must have array source type!");
1651     LValue ArrayLV = EmitLValue(Array);
1652     llvm::Value *ArrayPtr = ArrayLV.getAddress();
1653     llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
1654     llvm::Value *Args[] = { Zero, Idx };
1655 
1656     // Propagate the alignment from the array itself to the result.
1657     ArrayAlignment = ArrayLV.getAlignment();
1658 
1659     if (getContext().getLangOptions().isSignedOverflowDefined())
1660       Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx");
1661     else
1662       Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx");
1663   } else {
1664     // The base must be a pointer, which is not an aggregate.  Emit it.
1665     llvm::Value *Base = EmitScalarExpr(E->getBase());
1666     if (getContext().getLangOptions().isSignedOverflowDefined())
1667       Address = Builder.CreateGEP(Base, Idx, "arrayidx");
1668     else
1669       Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
1670   }
1671 
1672   QualType T = E->getBase()->getType()->getPointeeType();
1673   assert(!T.isNull() &&
1674          "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
1675 
1676   // Limit the alignment to that of the result type.
1677   if (ArrayAlignment) {
1678     unsigned Align = getContext().getTypeAlignInChars(T).getQuantity();
1679     ArrayAlignment = std::min(Align, ArrayAlignment);
1680   }
1681 
1682   LValue LV = MakeAddrLValue(Address, T, ArrayAlignment);
1683   LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
1684 
1685   if (getContext().getLangOptions().ObjC1 &&
1686       getContext().getLangOptions().getGC() != LangOptions::NonGC) {
1687     LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1688     setObjCGCLValueClass(getContext(), E, LV);
1689   }
1690   return LV;
1691 }
1692 
1693 static
1694 llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1695                                        SmallVector<unsigned, 4> &Elts) {
1696   SmallVector<llvm::Constant*, 4> CElts;
1697 
1698   llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1699   for (unsigned i = 0, e = Elts.size(); i != e; ++i)
1700     CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i]));
1701 
1702   return llvm::ConstantVector::get(CElts);
1703 }
1704 
1705 LValue CodeGenFunction::
1706 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
1707   // Emit the base vector as an l-value.
1708   LValue Base;
1709 
1710   // ExtVectorElementExpr's base can either be a vector or pointer to vector.
1711   if (E->isArrow()) {
1712     // If it is a pointer to a vector, emit the address and form an lvalue with
1713     // it.
1714     llvm::Value *Ptr = EmitScalarExpr(E->getBase());
1715     const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
1716     Base = MakeAddrLValue(Ptr, PT->getPointeeType());
1717     Base.getQuals().removeObjCGCAttr();
1718   } else if (E->getBase()->isGLValue()) {
1719     // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1720     // emit the base as an lvalue.
1721     assert(E->getBase()->getType()->isVectorType());
1722     Base = EmitLValue(E->getBase());
1723   } else {
1724     // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
1725     assert(E->getBase()->getType()->isVectorType() &&
1726            "Result must be a vector");
1727     llvm::Value *Vec = EmitScalarExpr(E->getBase());
1728 
1729     // Store the vector to memory (because LValue wants an address).
1730     llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
1731     Builder.CreateStore(Vec, VecMem);
1732     Base = MakeAddrLValue(VecMem, E->getBase()->getType());
1733   }
1734 
1735   QualType type =
1736     E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
1737 
1738   // Encode the element access list into a vector of unsigned indices.
1739   SmallVector<unsigned, 4> Indices;
1740   E->getEncodedElementAccess(Indices);
1741 
1742   if (Base.isSimple()) {
1743     llvm::Constant *CV = GenerateConstantVector(getLLVMContext(), Indices);
1744     return LValue::MakeExtVectorElt(Base.getAddress(), CV, type);
1745   }
1746   assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1747 
1748   llvm::Constant *BaseElts = Base.getExtVectorElts();
1749   SmallVector<llvm::Constant *, 4> CElts;
1750 
1751   for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1752     if (isa<llvm::ConstantAggregateZero>(BaseElts))
1753       CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
1754     else
1755       CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
1756   }
1757   llvm::Constant *CV = llvm::ConstantVector::get(CElts);
1758   return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type);
1759 }
1760 
1761 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
1762   bool isNonGC = false;
1763   Expr *BaseExpr = E->getBase();
1764   llvm::Value *BaseValue = NULL;
1765   Qualifiers BaseQuals;
1766 
1767   // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
1768   if (E->isArrow()) {
1769     BaseValue = EmitScalarExpr(BaseExpr);
1770     const PointerType *PTy =
1771       BaseExpr->getType()->getAs<PointerType>();
1772     BaseQuals = PTy->getPointeeType().getQualifiers();
1773   } else {
1774     LValue BaseLV = EmitLValue(BaseExpr);
1775     if (BaseLV.isNonGC())
1776       isNonGC = true;
1777     // FIXME: this isn't right for bitfields.
1778     BaseValue = BaseLV.getAddress();
1779     QualType BaseTy = BaseExpr->getType();
1780     BaseQuals = BaseTy.getQualifiers();
1781   }
1782 
1783   NamedDecl *ND = E->getMemberDecl();
1784   if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1785     LValue LV = EmitLValueForField(BaseValue, Field,
1786                                    BaseQuals.getCVRQualifiers());
1787     LV.setNonGC(isNonGC);
1788     setObjCGCLValueClass(getContext(), E, LV);
1789     return LV;
1790   }
1791 
1792   if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1793     return EmitGlobalVarDeclLValue(*this, E, VD);
1794 
1795   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1796     return EmitFunctionDeclLValue(*this, E, FD);
1797 
1798   llvm_unreachable("Unhandled member declaration!");
1799 }
1800 
1801 LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value *BaseValue,
1802                                               const FieldDecl *Field,
1803                                               unsigned CVRQualifiers) {
1804   const CGRecordLayout &RL =
1805     CGM.getTypes().getCGRecordLayout(Field->getParent());
1806   const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
1807   return LValue::MakeBitfield(BaseValue, Info,
1808                           Field->getType().withCVRQualifiers(CVRQualifiers));
1809 }
1810 
1811 /// EmitLValueForAnonRecordField - Given that the field is a member of
1812 /// an anonymous struct or union buried inside a record, and given
1813 /// that the base value is a pointer to the enclosing record, derive
1814 /// an lvalue for the ultimate field.
1815 LValue CodeGenFunction::EmitLValueForAnonRecordField(llvm::Value *BaseValue,
1816                                              const IndirectFieldDecl *Field,
1817                                                      unsigned CVRQualifiers) {
1818   IndirectFieldDecl::chain_iterator I = Field->chain_begin(),
1819     IEnd = Field->chain_end();
1820   while (true) {
1821     LValue LV = EmitLValueForField(BaseValue, cast<FieldDecl>(*I),
1822                                    CVRQualifiers);
1823     if (++I == IEnd) return LV;
1824 
1825     assert(LV.isSimple());
1826     BaseValue = LV.getAddress();
1827     CVRQualifiers |= LV.getVRQualifiers();
1828   }
1829 }
1830 
1831 LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr,
1832                                            const FieldDecl *field,
1833                                            unsigned cvr) {
1834   if (field->isBitField())
1835     return EmitLValueForBitfield(baseAddr, field, cvr);
1836 
1837   const RecordDecl *rec = field->getParent();
1838   QualType type = field->getType();
1839 
1840   bool mayAlias = rec->hasAttr<MayAliasAttr>();
1841 
1842   llvm::Value *addr = baseAddr;
1843   if (rec->isUnion()) {
1844     // For unions, there is no pointer adjustment.
1845     assert(!type->isReferenceType() && "union has reference member");
1846   } else {
1847     // For structs, we GEP to the field that the record layout suggests.
1848     unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
1849     addr = Builder.CreateStructGEP(addr, idx, field->getName());
1850 
1851     // If this is a reference field, load the reference right now.
1852     if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
1853       llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
1854       if (cvr & Qualifiers::Volatile) load->setVolatile(true);
1855 
1856       if (CGM.shouldUseTBAA()) {
1857         llvm::MDNode *tbaa;
1858         if (mayAlias)
1859           tbaa = CGM.getTBAAInfo(getContext().CharTy);
1860         else
1861           tbaa = CGM.getTBAAInfo(type);
1862         CGM.DecorateInstruction(load, tbaa);
1863       }
1864 
1865       addr = load;
1866       mayAlias = false;
1867       type = refType->getPointeeType();
1868       cvr = 0; // qualifiers don't recursively apply to referencee
1869     }
1870   }
1871 
1872   // Make sure that the address is pointing to the right type.  This is critical
1873   // for both unions and structs.  A union needs a bitcast, a struct element
1874   // will need a bitcast if the LLVM type laid out doesn't match the desired
1875   // type.
1876   addr = EmitBitCastOfLValueToProperType(*this, addr,
1877                                          CGM.getTypes().ConvertTypeForMem(type),
1878                                          field->getName());
1879 
1880   if (field->hasAttr<AnnotateAttr>())
1881     addr = EmitFieldAnnotations(field, addr);
1882 
1883   unsigned alignment = getContext().getDeclAlign(field).getQuantity();
1884   LValue LV = MakeAddrLValue(addr, type, alignment);
1885   LV.getQuals().addCVRQualifiers(cvr);
1886 
1887   // __weak attribute on a field is ignored.
1888   if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
1889     LV.getQuals().removeObjCGCAttr();
1890 
1891   // Fields of may_alias structs act like 'char' for TBAA purposes.
1892   // FIXME: this should get propagated down through anonymous structs
1893   // and unions.
1894   if (mayAlias && LV.getTBAAInfo())
1895     LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
1896 
1897   return LV;
1898 }
1899 
1900 LValue
1901 CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue,
1902                                                   const FieldDecl *Field,
1903                                                   unsigned CVRQualifiers) {
1904   QualType FieldType = Field->getType();
1905 
1906   if (!FieldType->isReferenceType())
1907     return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1908 
1909   const CGRecordLayout &RL =
1910     CGM.getTypes().getCGRecordLayout(Field->getParent());
1911   unsigned idx = RL.getLLVMFieldNo(Field);
1912   llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx);
1913   assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1914 
1915 
1916   // Make sure that the address is pointing to the right type.  This is critical
1917   // for both unions and structs.  A union needs a bitcast, a struct element
1918   // will need a bitcast if the LLVM type laid out doesn't match the desired
1919   // type.
1920   llvm::Type *llvmType = ConvertTypeForMem(FieldType);
1921   unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1922   V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS));
1923 
1924   unsigned Alignment = getContext().getDeclAlign(Field).getQuantity();
1925   return MakeAddrLValue(V, FieldType, Alignment);
1926 }
1927 
1928 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
1929   llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
1930   const Expr *InitExpr = E->getInitializer();
1931   LValue Result = MakeAddrLValue(DeclPtr, E->getType());
1932 
1933   EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
1934                    /*Init*/ true);
1935 
1936   return Result;
1937 }
1938 
1939 LValue CodeGenFunction::
1940 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
1941   if (!expr->isGLValue()) {
1942     // ?: here should be an aggregate.
1943     assert((hasAggregateLLVMType(expr->getType()) &&
1944             !expr->getType()->isAnyComplexType()) &&
1945            "Unexpected conditional operator!");
1946     return EmitAggExprToLValue(expr);
1947   }
1948 
1949   const Expr *condExpr = expr->getCond();
1950   bool CondExprBool;
1951   if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
1952     const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
1953     if (!CondExprBool) std::swap(live, dead);
1954 
1955     if (!ContainsLabel(dead))
1956       return EmitLValue(live);
1957   }
1958 
1959   OpaqueValueMapping binding(*this, expr);
1960 
1961   llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
1962   llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
1963   llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
1964 
1965   ConditionalEvaluation eval(*this);
1966   EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock);
1967 
1968   // Any temporaries created here are conditional.
1969   EmitBlock(lhsBlock);
1970   eval.begin(*this);
1971   LValue lhs = EmitLValue(expr->getTrueExpr());
1972   eval.end(*this);
1973 
1974   if (!lhs.isSimple())
1975     return EmitUnsupportedLValue(expr, "conditional operator");
1976 
1977   lhsBlock = Builder.GetInsertBlock();
1978   Builder.CreateBr(contBlock);
1979 
1980   // Any temporaries created here are conditional.
1981   EmitBlock(rhsBlock);
1982   eval.begin(*this);
1983   LValue rhs = EmitLValue(expr->getFalseExpr());
1984   eval.end(*this);
1985   if (!rhs.isSimple())
1986     return EmitUnsupportedLValue(expr, "conditional operator");
1987   rhsBlock = Builder.GetInsertBlock();
1988 
1989   EmitBlock(contBlock);
1990 
1991   llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2,
1992                                          "cond-lvalue");
1993   phi->addIncoming(lhs.getAddress(), lhsBlock);
1994   phi->addIncoming(rhs.getAddress(), rhsBlock);
1995   return MakeAddrLValue(phi, expr->getType());
1996 }
1997 
1998 /// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1999 /// If the cast is a dynamic_cast, we can have the usual lvalue result,
2000 /// otherwise if a cast is needed by the code generator in an lvalue context,
2001 /// then it must mean that we need the address of an aggregate in order to
2002 /// access one of its fields.  This can happen for all the reasons that casts
2003 /// are permitted with aggregate result, including noop aggregate casts, and
2004 /// cast from scalar to union.
2005 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
2006   switch (E->getCastKind()) {
2007   case CK_ToVoid:
2008     return EmitUnsupportedLValue(E, "unexpected cast lvalue");
2009 
2010   case CK_Dependent:
2011     llvm_unreachable("dependent cast kind in IR gen!");
2012 
2013   case CK_NoOp:
2014   case CK_LValueToRValue:
2015     if (!E->getSubExpr()->Classify(getContext()).isPRValue()
2016         || E->getType()->isRecordType())
2017       return EmitLValue(E->getSubExpr());
2018     // Fall through to synthesize a temporary.
2019 
2020   case CK_BitCast:
2021   case CK_ArrayToPointerDecay:
2022   case CK_FunctionToPointerDecay:
2023   case CK_NullToMemberPointer:
2024   case CK_NullToPointer:
2025   case CK_IntegralToPointer:
2026   case CK_PointerToIntegral:
2027   case CK_PointerToBoolean:
2028   case CK_VectorSplat:
2029   case CK_IntegralCast:
2030   case CK_IntegralToBoolean:
2031   case CK_IntegralToFloating:
2032   case CK_FloatingToIntegral:
2033   case CK_FloatingToBoolean:
2034   case CK_FloatingCast:
2035   case CK_FloatingRealToComplex:
2036   case CK_FloatingComplexToReal:
2037   case CK_FloatingComplexToBoolean:
2038   case CK_FloatingComplexCast:
2039   case CK_FloatingComplexToIntegralComplex:
2040   case CK_IntegralRealToComplex:
2041   case CK_IntegralComplexToReal:
2042   case CK_IntegralComplexToBoolean:
2043   case CK_IntegralComplexCast:
2044   case CK_IntegralComplexToFloatingComplex:
2045   case CK_DerivedToBaseMemberPointer:
2046   case CK_BaseToDerivedMemberPointer:
2047   case CK_MemberPointerToBoolean:
2048   case CK_AnyPointerToBlockPointerCast:
2049   case CK_ARCProduceObject:
2050   case CK_ARCConsumeObject:
2051   case CK_ARCReclaimReturnedObject:
2052   case CK_ARCExtendBlockObject: {
2053     // These casts only produce lvalues when we're binding a reference to a
2054     // temporary realized from a (converted) pure rvalue. Emit the expression
2055     // as a value, copy it into a temporary, and return an lvalue referring to
2056     // that temporary.
2057     llvm::Value *V = CreateMemTemp(E->getType(), "ref.temp");
2058     EmitAnyExprToMem(E, V, E->getType().getQualifiers(), false);
2059     return MakeAddrLValue(V, E->getType());
2060   }
2061 
2062   case CK_Dynamic: {
2063     LValue LV = EmitLValue(E->getSubExpr());
2064     llvm::Value *V = LV.getAddress();
2065     const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
2066     return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2067   }
2068 
2069   case CK_ConstructorConversion:
2070   case CK_UserDefinedConversion:
2071   case CK_CPointerToObjCPointerCast:
2072   case CK_BlockPointerToObjCPointerCast:
2073     return EmitLValue(E->getSubExpr());
2074 
2075   case CK_UncheckedDerivedToBase:
2076   case CK_DerivedToBase: {
2077     const RecordType *DerivedClassTy =
2078       E->getSubExpr()->getType()->getAs<RecordType>();
2079     CXXRecordDecl *DerivedClassDecl =
2080       cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2081 
2082     LValue LV = EmitLValue(E->getSubExpr());
2083     llvm::Value *This = LV.getAddress();
2084 
2085     // Perform the derived-to-base conversion
2086     llvm::Value *Base =
2087       GetAddressOfBaseClass(This, DerivedClassDecl,
2088                             E->path_begin(), E->path_end(),
2089                             /*NullCheckValue=*/false);
2090 
2091     return MakeAddrLValue(Base, E->getType());
2092   }
2093   case CK_ToUnion:
2094     return EmitAggExprToLValue(E);
2095   case CK_BaseToDerived: {
2096     const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2097     CXXRecordDecl *DerivedClassDecl =
2098       cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2099 
2100     LValue LV = EmitLValue(E->getSubExpr());
2101 
2102     // Perform the base-to-derived conversion
2103     llvm::Value *Derived =
2104       GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
2105                                E->path_begin(), E->path_end(),
2106                                /*NullCheckValue=*/false);
2107 
2108     return MakeAddrLValue(Derived, E->getType());
2109   }
2110   case CK_LValueBitCast: {
2111     // This must be a reinterpret_cast (or c-style equivalent).
2112     const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
2113 
2114     LValue LV = EmitLValue(E->getSubExpr());
2115     llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2116                                            ConvertType(CE->getTypeAsWritten()));
2117     return MakeAddrLValue(V, E->getType());
2118   }
2119   case CK_ObjCObjectLValueCast: {
2120     LValue LV = EmitLValue(E->getSubExpr());
2121     QualType ToType = getContext().getLValueReferenceType(E->getType());
2122     llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2123                                            ConvertType(ToType));
2124     return MakeAddrLValue(V, E->getType());
2125   }
2126   }
2127 
2128   llvm_unreachable("Unhandled lvalue cast kind?");
2129 }
2130 
2131 LValue CodeGenFunction::EmitNullInitializationLValue(
2132                                               const CXXScalarValueInitExpr *E) {
2133   QualType Ty = E->getType();
2134   LValue LV = MakeAddrLValue(CreateMemTemp(Ty), Ty);
2135   EmitNullInitialization(LV.getAddress(), Ty);
2136   return LV;
2137 }
2138 
2139 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
2140   assert(OpaqueValueMappingData::shouldBindAsLValue(e));
2141   return getOpaqueLValueMapping(e);
2142 }
2143 
2144 LValue CodeGenFunction::EmitMaterializeTemporaryExpr(
2145                                            const MaterializeTemporaryExpr *E) {
2146   RValue RV = EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
2147   return MakeAddrLValue(RV.getScalarVal(), E->getType());
2148 }
2149 
2150 
2151 //===--------------------------------------------------------------------===//
2152 //                             Expression Emission
2153 //===--------------------------------------------------------------------===//
2154 
2155 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
2156                                      ReturnValueSlot ReturnValue) {
2157   if (CGDebugInfo *DI = getDebugInfo())
2158     DI->EmitLocation(Builder, E->getLocStart());
2159 
2160   // Builtins never have block type.
2161   if (E->getCallee()->getType()->isBlockPointerType())
2162     return EmitBlockCallExpr(E, ReturnValue);
2163 
2164   if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
2165     return EmitCXXMemberCallExpr(CE, ReturnValue);
2166 
2167   if (const CUDAKernelCallExpr *CE = dyn_cast<CUDAKernelCallExpr>(E))
2168     return EmitCUDAKernelCallExpr(CE, ReturnValue);
2169 
2170   const Decl *TargetDecl = E->getCalleeDecl();
2171   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
2172     if (unsigned builtinID = FD->getBuiltinID())
2173       return EmitBuiltinExpr(FD, builtinID, E);
2174   }
2175 
2176   if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
2177     if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
2178       return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
2179 
2180   if (const CXXPseudoDestructorExpr *PseudoDtor
2181           = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
2182     QualType DestroyedType = PseudoDtor->getDestroyedType();
2183     if (getContext().getLangOptions().ObjCAutoRefCount &&
2184         DestroyedType->isObjCLifetimeType() &&
2185         (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
2186          DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
2187       // Automatic Reference Counting:
2188       //   If the pseudo-expression names a retainable object with weak or
2189       //   strong lifetime, the object shall be released.
2190       Expr *BaseExpr = PseudoDtor->getBase();
2191       llvm::Value *BaseValue = NULL;
2192       Qualifiers BaseQuals;
2193 
2194       // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2195       if (PseudoDtor->isArrow()) {
2196         BaseValue = EmitScalarExpr(BaseExpr);
2197         const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
2198         BaseQuals = PTy->getPointeeType().getQualifiers();
2199       } else {
2200         LValue BaseLV = EmitLValue(BaseExpr);
2201         BaseValue = BaseLV.getAddress();
2202         QualType BaseTy = BaseExpr->getType();
2203         BaseQuals = BaseTy.getQualifiers();
2204       }
2205 
2206       switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
2207       case Qualifiers::OCL_None:
2208       case Qualifiers::OCL_ExplicitNone:
2209       case Qualifiers::OCL_Autoreleasing:
2210         break;
2211 
2212       case Qualifiers::OCL_Strong:
2213         EmitARCRelease(Builder.CreateLoad(BaseValue,
2214                           PseudoDtor->getDestroyedType().isVolatileQualified()),
2215                        /*precise*/ true);
2216         break;
2217 
2218       case Qualifiers::OCL_Weak:
2219         EmitARCDestroyWeak(BaseValue);
2220         break;
2221       }
2222     } else {
2223       // C++ [expr.pseudo]p1:
2224       //   The result shall only be used as the operand for the function call
2225       //   operator (), and the result of such a call has type void. The only
2226       //   effect is the evaluation of the postfix-expression before the dot or
2227       //   arrow.
2228       EmitScalarExpr(E->getCallee());
2229     }
2230 
2231     return RValue::get(0);
2232   }
2233 
2234   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
2235   return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
2236                   E->arg_begin(), E->arg_end(), TargetDecl);
2237 }
2238 
2239 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
2240   // Comma expressions just emit their LHS then their RHS as an l-value.
2241   if (E->getOpcode() == BO_Comma) {
2242     EmitIgnoredExpr(E->getLHS());
2243     EnsureInsertPoint();
2244     return EmitLValue(E->getRHS());
2245   }
2246 
2247   if (E->getOpcode() == BO_PtrMemD ||
2248       E->getOpcode() == BO_PtrMemI)
2249     return EmitPointerToDataMemberBinaryExpr(E);
2250 
2251   assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
2252 
2253   // Note that in all of these cases, __block variables need the RHS
2254   // evaluated first just in case the variable gets moved by the RHS.
2255 
2256   if (!hasAggregateLLVMType(E->getType())) {
2257     switch (E->getLHS()->getType().getObjCLifetime()) {
2258     case Qualifiers::OCL_Strong:
2259       return EmitARCStoreStrong(E, /*ignored*/ false).first;
2260 
2261     case Qualifiers::OCL_Autoreleasing:
2262       return EmitARCStoreAutoreleasing(E).first;
2263 
2264     // No reason to do any of these differently.
2265     case Qualifiers::OCL_None:
2266     case Qualifiers::OCL_ExplicitNone:
2267     case Qualifiers::OCL_Weak:
2268       break;
2269     }
2270 
2271     RValue RV = EmitAnyExpr(E->getRHS());
2272     LValue LV = EmitLValue(E->getLHS());
2273     EmitStoreThroughLValue(RV, LV);
2274     return LV;
2275   }
2276 
2277   if (E->getType()->isAnyComplexType())
2278     return EmitComplexAssignmentLValue(E);
2279 
2280   return EmitAggExprToLValue(E);
2281 }
2282 
2283 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
2284   RValue RV = EmitCallExpr(E);
2285 
2286   if (!RV.isScalar())
2287     return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2288 
2289   assert(E->getCallReturnType()->isReferenceType() &&
2290          "Can't have a scalar return unless the return type is a "
2291          "reference type!");
2292 
2293   return MakeAddrLValue(RV.getScalarVal(), E->getType());
2294 }
2295 
2296 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
2297   // FIXME: This shouldn't require another copy.
2298   return EmitAggExprToLValue(E);
2299 }
2300 
2301 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
2302   assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
2303          && "binding l-value to type which needs a temporary");
2304   AggValueSlot Slot = CreateAggTemp(E->getType());
2305   EmitCXXConstructExpr(E, Slot);
2306   return MakeAddrLValue(Slot.getAddr(), E->getType());
2307 }
2308 
2309 LValue
2310 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
2311   return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
2312 }
2313 
2314 LValue
2315 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
2316   AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
2317   Slot.setExternallyDestructed();
2318   EmitAggExpr(E->getSubExpr(), Slot);
2319   EmitCXXTemporary(E->getTemporary(), Slot.getAddr());
2320   return MakeAddrLValue(Slot.getAddr(), E->getType());
2321 }
2322 
2323 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
2324   RValue RV = EmitObjCMessageExpr(E);
2325 
2326   if (!RV.isScalar())
2327     return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2328 
2329   assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
2330          "Can't have a scalar return unless the return type is a "
2331          "reference type!");
2332 
2333   return MakeAddrLValue(RV.getScalarVal(), E->getType());
2334 }
2335 
2336 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
2337   llvm::Value *V =
2338     CGM.getObjCRuntime().GetSelector(Builder, E->getSelector(), true);
2339   return MakeAddrLValue(V, E->getType());
2340 }
2341 
2342 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2343                                              const ObjCIvarDecl *Ivar) {
2344   return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
2345 }
2346 
2347 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
2348                                           llvm::Value *BaseValue,
2349                                           const ObjCIvarDecl *Ivar,
2350                                           unsigned CVRQualifiers) {
2351   return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
2352                                                    Ivar, CVRQualifiers);
2353 }
2354 
2355 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
2356   // FIXME: A lot of the code below could be shared with EmitMemberExpr.
2357   llvm::Value *BaseValue = 0;
2358   const Expr *BaseExpr = E->getBase();
2359   Qualifiers BaseQuals;
2360   QualType ObjectTy;
2361   if (E->isArrow()) {
2362     BaseValue = EmitScalarExpr(BaseExpr);
2363     ObjectTy = BaseExpr->getType()->getPointeeType();
2364     BaseQuals = ObjectTy.getQualifiers();
2365   } else {
2366     LValue BaseLV = EmitLValue(BaseExpr);
2367     // FIXME: this isn't right for bitfields.
2368     BaseValue = BaseLV.getAddress();
2369     ObjectTy = BaseExpr->getType();
2370     BaseQuals = ObjectTy.getQualifiers();
2371   }
2372 
2373   LValue LV =
2374     EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
2375                       BaseQuals.getCVRQualifiers());
2376   setObjCGCLValueClass(getContext(), E, LV);
2377   return LV;
2378 }
2379 
2380 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
2381   // Can only get l-value for message expression returning aggregate type
2382   RValue RV = EmitAnyExprToTemp(E);
2383   return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
2384 }
2385 
2386 RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
2387                                  ReturnValueSlot ReturnValue,
2388                                  CallExpr::const_arg_iterator ArgBeg,
2389                                  CallExpr::const_arg_iterator ArgEnd,
2390                                  const Decl *TargetDecl) {
2391   // Get the actual function type. The callee type will always be a pointer to
2392   // function type or a block pointer type.
2393   assert(CalleeType->isFunctionPointerType() &&
2394          "Call must have function pointer type!");
2395 
2396   CalleeType = getContext().getCanonicalType(CalleeType);
2397 
2398   const FunctionType *FnType
2399     = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
2400 
2401   CallArgList Args;
2402   EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
2403 
2404   const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(Args, FnType);
2405 
2406   // C99 6.5.2.2p6:
2407   //   If the expression that denotes the called function has a type
2408   //   that does not include a prototype, [the default argument
2409   //   promotions are performed]. If the number of arguments does not
2410   //   equal the number of parameters, the behavior is undefined. If
2411   //   the function is defined with a type that includes a prototype,
2412   //   and either the prototype ends with an ellipsis (, ...) or the
2413   //   types of the arguments after promotion are not compatible with
2414   //   the types of the parameters, the behavior is undefined. If the
2415   //   function is defined with a type that does not include a
2416   //   prototype, and the types of the arguments after promotion are
2417   //   not compatible with those of the parameters after promotion,
2418   //   the behavior is undefined [except in some trivial cases].
2419   // That is, in the general case, we should assume that a call
2420   // through an unprototyped function type works like a *non-variadic*
2421   // call.  The way we make this work is to cast to the exact type
2422   // of the promoted arguments.
2423   if (isa<FunctionNoProtoType>(FnType) &&
2424       !getTargetHooks().isNoProtoCallVariadic(FnType->getCallConv())) {
2425     assert(cast<llvm::FunctionType>(Callee->getType()->getContainedType(0))
2426              ->isVarArg());
2427     llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo, false);
2428     CalleeTy = CalleeTy->getPointerTo();
2429     Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast");
2430   }
2431 
2432   return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl);
2433 }
2434 
2435 LValue CodeGenFunction::
2436 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
2437   llvm::Value *BaseV;
2438   if (E->getOpcode() == BO_PtrMemI)
2439     BaseV = EmitScalarExpr(E->getLHS());
2440   else
2441     BaseV = EmitLValue(E->getLHS()).getAddress();
2442 
2443   llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
2444 
2445   const MemberPointerType *MPT
2446     = E->getRHS()->getType()->getAs<MemberPointerType>();
2447 
2448   llvm::Value *AddV =
2449     CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
2450 
2451   return MakeAddrLValue(AddV, MPT->getPointeeType());
2452 }
2453 
2454 static void
2455 EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, llvm::Value *Dest,
2456              llvm::Value *Ptr, llvm::Value *Val1, llvm::Value *Val2,
2457              uint64_t Size, unsigned Align, llvm::AtomicOrdering Order) {
2458   if (E->isCmpXChg()) {
2459     // Note that cmpxchg only supports specifying one ordering and
2460     // doesn't support weak cmpxchg, at least at the moment.
2461     llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
2462     LoadVal1->setAlignment(Align);
2463     llvm::LoadInst *LoadVal2 = CGF.Builder.CreateLoad(Val2);
2464     LoadVal2->setAlignment(Align);
2465     llvm::AtomicCmpXchgInst *CXI =
2466         CGF.Builder.CreateAtomicCmpXchg(Ptr, LoadVal1, LoadVal2, Order);
2467     CXI->setVolatile(E->isVolatile());
2468     llvm::StoreInst *StoreVal1 = CGF.Builder.CreateStore(CXI, Val1);
2469     StoreVal1->setAlignment(Align);
2470     llvm::Value *Cmp = CGF.Builder.CreateICmpEQ(CXI, LoadVal1);
2471     CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
2472     return;
2473   }
2474 
2475   if (E->getOp() == AtomicExpr::Load) {
2476     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Ptr);
2477     Load->setAtomic(Order);
2478     Load->setAlignment(Size);
2479     Load->setVolatile(E->isVolatile());
2480     llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(Load, Dest);
2481     StoreDest->setAlignment(Align);
2482     return;
2483   }
2484 
2485   if (E->getOp() == AtomicExpr::Store) {
2486     assert(!Dest && "Store does not return a value");
2487     llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
2488     LoadVal1->setAlignment(Align);
2489     llvm::StoreInst *Store = CGF.Builder.CreateStore(LoadVal1, Ptr);
2490     Store->setAtomic(Order);
2491     Store->setAlignment(Size);
2492     Store->setVolatile(E->isVolatile());
2493     return;
2494   }
2495 
2496   llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
2497   switch (E->getOp()) {
2498     case AtomicExpr::CmpXchgWeak:
2499     case AtomicExpr::CmpXchgStrong:
2500     case AtomicExpr::Store:
2501     case AtomicExpr::Load:  assert(0 && "Already handled!");
2502     case AtomicExpr::Add:   Op = llvm::AtomicRMWInst::Add;  break;
2503     case AtomicExpr::Sub:   Op = llvm::AtomicRMWInst::Sub;  break;
2504     case AtomicExpr::And:   Op = llvm::AtomicRMWInst::And;  break;
2505     case AtomicExpr::Or:    Op = llvm::AtomicRMWInst::Or;   break;
2506     case AtomicExpr::Xor:   Op = llvm::AtomicRMWInst::Xor;  break;
2507     case AtomicExpr::Xchg:  Op = llvm::AtomicRMWInst::Xchg; break;
2508   }
2509   llvm::LoadInst *LoadVal1 = CGF.Builder.CreateLoad(Val1);
2510   LoadVal1->setAlignment(Align);
2511   llvm::AtomicRMWInst *RMWI =
2512       CGF.Builder.CreateAtomicRMW(Op, Ptr, LoadVal1, Order);
2513   RMWI->setVolatile(E->isVolatile());
2514   llvm::StoreInst *StoreDest = CGF.Builder.CreateStore(RMWI, Dest);
2515   StoreDest->setAlignment(Align);
2516 }
2517 
2518 // This function emits any expression (scalar, complex, or aggregate)
2519 // into a temporary alloca.
2520 static llvm::Value *
2521 EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
2522   llvm::Value *DeclPtr = CGF.CreateMemTemp(E->getType(), ".atomictmp");
2523   CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
2524                        /*Init*/ true);
2525   return DeclPtr;
2526 }
2527 
2528 static RValue ConvertTempToRValue(CodeGenFunction &CGF, QualType Ty,
2529                                   llvm::Value *Dest) {
2530   if (Ty->isAnyComplexType())
2531     return RValue::getComplex(CGF.LoadComplexFromAddr(Dest, false));
2532   if (CGF.hasAggregateLLVMType(Ty))
2533     return RValue::getAggregate(Dest);
2534   return RValue::get(CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(Dest, Ty)));
2535 }
2536 
2537 RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest) {
2538   QualType AtomicTy = E->getPtr()->getType()->getPointeeType();
2539   QualType MemTy = AtomicTy->getAs<AtomicType>()->getValueType();
2540   CharUnits sizeChars = getContext().getTypeSizeInChars(AtomicTy);
2541   uint64_t Size = sizeChars.getQuantity();
2542   CharUnits alignChars = getContext().getTypeAlignInChars(AtomicTy);
2543   unsigned Align = alignChars.getQuantity();
2544   unsigned MaxInlineWidth =
2545       getContext().getTargetInfo().getMaxAtomicInlineWidth();
2546   bool UseLibcall = (Size != Align || Size > MaxInlineWidth);
2547 
2548   llvm::Value *Ptr, *Order, *OrderFail = 0, *Val1 = 0, *Val2 = 0;
2549   Ptr = EmitScalarExpr(E->getPtr());
2550   Order = EmitScalarExpr(E->getOrder());
2551   if (E->isCmpXChg()) {
2552     Val1 = EmitScalarExpr(E->getVal1());
2553     Val2 = EmitValToTemp(*this, E->getVal2());
2554     OrderFail = EmitScalarExpr(E->getOrderFail());
2555     (void)OrderFail; // OrderFail is unused at the moment
2556   } else if ((E->getOp() == AtomicExpr::Add || E->getOp() == AtomicExpr::Sub) &&
2557              MemTy->isPointerType()) {
2558     // For pointers, we're required to do a bit of math: adding 1 to an int*
2559     // is not the same as adding 1 to a uintptr_t.
2560     QualType Val1Ty = E->getVal1()->getType();
2561     llvm::Value *Val1Scalar = EmitScalarExpr(E->getVal1());
2562     CharUnits PointeeIncAmt =
2563         getContext().getTypeSizeInChars(MemTy->getPointeeType());
2564     Val1Scalar = Builder.CreateMul(Val1Scalar, CGM.getSize(PointeeIncAmt));
2565     Val1 = CreateMemTemp(Val1Ty, ".atomictmp");
2566     EmitStoreOfScalar(Val1Scalar, MakeAddrLValue(Val1, Val1Ty));
2567   } else if (E->getOp() != AtomicExpr::Load) {
2568     Val1 = EmitValToTemp(*this, E->getVal1());
2569   }
2570 
2571   if (E->getOp() != AtomicExpr::Store && !Dest)
2572     Dest = CreateMemTemp(E->getType(), ".atomicdst");
2573 
2574   if (UseLibcall) {
2575     // FIXME: Finalize what the libcalls are actually supposed to look like.
2576     // See also http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary .
2577     return EmitUnsupportedRValue(E, "atomic library call");
2578   }
2579 #if 0
2580   if (UseLibcall) {
2581     const char* LibCallName;
2582     switch (E->getOp()) {
2583     case AtomicExpr::CmpXchgWeak:
2584       LibCallName = "__atomic_compare_exchange_generic"; break;
2585     case AtomicExpr::CmpXchgStrong:
2586       LibCallName = "__atomic_compare_exchange_generic"; break;
2587     case AtomicExpr::Add:   LibCallName = "__atomic_fetch_add_generic"; break;
2588     case AtomicExpr::Sub:   LibCallName = "__atomic_fetch_sub_generic"; break;
2589     case AtomicExpr::And:   LibCallName = "__atomic_fetch_and_generic"; break;
2590     case AtomicExpr::Or:    LibCallName = "__atomic_fetch_or_generic"; break;
2591     case AtomicExpr::Xor:   LibCallName = "__atomic_fetch_xor_generic"; break;
2592     case AtomicExpr::Xchg:  LibCallName = "__atomic_exchange_generic"; break;
2593     case AtomicExpr::Store: LibCallName = "__atomic_store_generic"; break;
2594     case AtomicExpr::Load:  LibCallName = "__atomic_load_generic"; break;
2595     }
2596     llvm::SmallVector<QualType, 4> Params;
2597     CallArgList Args;
2598     QualType RetTy = getContext().VoidTy;
2599     if (E->getOp() != AtomicExpr::Store && !E->isCmpXChg())
2600       Args.add(RValue::get(EmitCastToVoidPtr(Dest)),
2601                getContext().VoidPtrTy);
2602     Args.add(RValue::get(EmitCastToVoidPtr(Ptr)),
2603              getContext().VoidPtrTy);
2604     if (E->getOp() != AtomicExpr::Load)
2605       Args.add(RValue::get(EmitCastToVoidPtr(Val1)),
2606                getContext().VoidPtrTy);
2607     if (E->isCmpXChg()) {
2608       Args.add(RValue::get(EmitCastToVoidPtr(Val2)),
2609                getContext().VoidPtrTy);
2610       RetTy = getContext().IntTy;
2611     }
2612     Args.add(RValue::get(llvm::ConstantInt::get(SizeTy, Size)),
2613              getContext().getSizeType());
2614     const CGFunctionInfo &FuncInfo =
2615         CGM.getTypes().getFunctionInfo(RetTy, Args, FunctionType::ExtInfo());
2616     llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo, false);
2617     llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
2618     RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args);
2619     if (E->isCmpXChg())
2620       return Res;
2621     if (E->getOp() == AtomicExpr::Store)
2622       return RValue::get(0);
2623     return ConvertTempToRValue(*this, E->getType(), Dest);
2624   }
2625 #endif
2626   llvm::Type *IPtrTy =
2627       llvm::IntegerType::get(getLLVMContext(), Size * 8)->getPointerTo();
2628   llvm::Value *OrigDest = Dest;
2629   Ptr = Builder.CreateBitCast(Ptr, IPtrTy);
2630   if (Val1) Val1 = Builder.CreateBitCast(Val1, IPtrTy);
2631   if (Val2) Val2 = Builder.CreateBitCast(Val2, IPtrTy);
2632   if (Dest && !E->isCmpXChg()) Dest = Builder.CreateBitCast(Dest, IPtrTy);
2633 
2634   if (isa<llvm::ConstantInt>(Order)) {
2635     int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
2636     switch (ord) {
2637     case 0:  // memory_order_relaxed
2638       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2639                    llvm::Monotonic);
2640       break;
2641     case 1:  // memory_order_consume
2642     case 2:  // memory_order_acquire
2643       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2644                    llvm::Acquire);
2645       break;
2646     case 3:  // memory_order_release
2647       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2648                    llvm::Release);
2649       break;
2650     case 4:  // memory_order_acq_rel
2651       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2652                    llvm::AcquireRelease);
2653       break;
2654     case 5:  // memory_order_seq_cst
2655       EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2656                    llvm::SequentiallyConsistent);
2657       break;
2658     default: // invalid order
2659       // We should not ever get here normally, but it's hard to
2660       // enforce that in general.
2661       break;
2662     }
2663     if (E->getOp() == AtomicExpr::Store)
2664       return RValue::get(0);
2665     return ConvertTempToRValue(*this, E->getType(), OrigDest);
2666   }
2667 
2668   // Long case, when Order isn't obviously constant.
2669 
2670   // Create all the relevant BB's
2671   llvm::BasicBlock *MonotonicBB = 0, *AcquireBB = 0, *ReleaseBB = 0,
2672                    *AcqRelBB = 0, *SeqCstBB = 0;
2673   MonotonicBB = createBasicBlock("monotonic", CurFn);
2674   if (E->getOp() != AtomicExpr::Store)
2675     AcquireBB = createBasicBlock("acquire", CurFn);
2676   if (E->getOp() != AtomicExpr::Load)
2677     ReleaseBB = createBasicBlock("release", CurFn);
2678   if (E->getOp() != AtomicExpr::Load && E->getOp() != AtomicExpr::Store)
2679     AcqRelBB = createBasicBlock("acqrel", CurFn);
2680   SeqCstBB = createBasicBlock("seqcst", CurFn);
2681   llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
2682 
2683   // Create the switch for the split
2684   // MonotonicBB is arbitrarily chosen as the default case; in practice, this
2685   // doesn't matter unless someone is crazy enough to use something that
2686   // doesn't fold to a constant for the ordering.
2687   Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
2688   llvm::SwitchInst *SI = Builder.CreateSwitch(Order, MonotonicBB);
2689 
2690   // Emit all the different atomics
2691   Builder.SetInsertPoint(MonotonicBB);
2692   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2693                llvm::Monotonic);
2694   Builder.CreateBr(ContBB);
2695   if (E->getOp() != AtomicExpr::Store) {
2696     Builder.SetInsertPoint(AcquireBB);
2697     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2698                  llvm::Acquire);
2699     Builder.CreateBr(ContBB);
2700     SI->addCase(Builder.getInt32(1), AcquireBB);
2701     SI->addCase(Builder.getInt32(2), AcquireBB);
2702   }
2703   if (E->getOp() != AtomicExpr::Load) {
2704     Builder.SetInsertPoint(ReleaseBB);
2705     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2706                  llvm::Release);
2707     Builder.CreateBr(ContBB);
2708     SI->addCase(Builder.getInt32(3), ReleaseBB);
2709   }
2710   if (E->getOp() != AtomicExpr::Load && E->getOp() != AtomicExpr::Store) {
2711     Builder.SetInsertPoint(AcqRelBB);
2712     EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2713                  llvm::AcquireRelease);
2714     Builder.CreateBr(ContBB);
2715     SI->addCase(Builder.getInt32(4), AcqRelBB);
2716   }
2717   Builder.SetInsertPoint(SeqCstBB);
2718   EmitAtomicOp(*this, E, Dest, Ptr, Val1, Val2, Size, Align,
2719                llvm::SequentiallyConsistent);
2720   Builder.CreateBr(ContBB);
2721   SI->addCase(Builder.getInt32(5), SeqCstBB);
2722 
2723   // Cleanup and return
2724   Builder.SetInsertPoint(ContBB);
2725   if (E->getOp() == AtomicExpr::Store)
2726     return RValue::get(0);
2727   return ConvertTempToRValue(*this, E->getType(), OrigDest);
2728 }
2729 
2730 void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, unsigned AccuracyN,
2731                                     unsigned AccuracyD) {
2732   assert(Val->getType()->isFPOrFPVectorTy());
2733   if (!AccuracyN || !isa<llvm::Instruction>(Val))
2734     return;
2735 
2736   llvm::Value *Vals[2];
2737   Vals[0] = llvm::ConstantInt::get(Int32Ty, AccuracyN);
2738   Vals[1] = llvm::ConstantInt::get(Int32Ty, AccuracyD);
2739   llvm::MDNode *Node = llvm::MDNode::get(getLLVMContext(), Vals);
2740 
2741   cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpaccuracy,
2742                                             Node);
2743 }
2744 
2745 namespace {
2746   struct LValueOrRValue {
2747     LValue LV;
2748     RValue RV;
2749   };
2750 }
2751 
2752 static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
2753                                            const PseudoObjectExpr *E,
2754                                            bool forLValue,
2755                                            AggValueSlot slot) {
2756   llvm::SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
2757 
2758   // Find the result expression, if any.
2759   const Expr *resultExpr = E->getResultExpr();
2760   LValueOrRValue result;
2761 
2762   for (PseudoObjectExpr::const_semantics_iterator
2763          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
2764     const Expr *semantic = *i;
2765 
2766     // If this semantic expression is an opaque value, bind it
2767     // to the result of its source expression.
2768     if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
2769 
2770       // If this is the result expression, we may need to evaluate
2771       // directly into the slot.
2772       typedef CodeGenFunction::OpaqueValueMappingData OVMA;
2773       OVMA opaqueData;
2774       if (ov == resultExpr && ov->isRValue() && !forLValue &&
2775           CodeGenFunction::hasAggregateLLVMType(ov->getType()) &&
2776           !ov->getType()->isAnyComplexType()) {
2777         CGF.EmitAggExpr(ov->getSourceExpr(), slot);
2778 
2779         LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType());
2780         opaqueData = OVMA::bind(CGF, ov, LV);
2781         result.RV = slot.asRValue();
2782 
2783       // Otherwise, emit as normal.
2784       } else {
2785         opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
2786 
2787         // If this is the result, also evaluate the result now.
2788         if (ov == resultExpr) {
2789           if (forLValue)
2790             result.LV = CGF.EmitLValue(ov);
2791           else
2792             result.RV = CGF.EmitAnyExpr(ov, slot);
2793         }
2794       }
2795 
2796       opaques.push_back(opaqueData);
2797 
2798     // Otherwise, if the expression is the result, evaluate it
2799     // and remember the result.
2800     } else if (semantic == resultExpr) {
2801       if (forLValue)
2802         result.LV = CGF.EmitLValue(semantic);
2803       else
2804         result.RV = CGF.EmitAnyExpr(semantic, slot);
2805 
2806     // Otherwise, evaluate the expression in an ignored context.
2807     } else {
2808       CGF.EmitIgnoredExpr(semantic);
2809     }
2810   }
2811 
2812   // Unbind all the opaques now.
2813   for (unsigned i = 0, e = opaques.size(); i != e; ++i)
2814     opaques[i].unbind(CGF);
2815 
2816   return result;
2817 }
2818 
2819 RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
2820                                                AggValueSlot slot) {
2821   return emitPseudoObjectExpr(*this, E, false, slot).RV;
2822 }
2823 
2824 LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
2825   return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
2826 }
2827