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