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