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