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