1193326Sed//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This contains code to emit Expr nodes as LLVM code.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#include "CodeGenFunction.h"
15249423Sdim#include "CGCXXABI.h"
16193326Sed#include "CGCall.h"
17221345Sdim#include "CGDebugInfo.h"
18249423Sdim#include "CGObjCRuntime.h"
19206084Srdivacky#include "CGRecordLayout.h"
20249423Sdim#include "CodeGenModule.h"
21226633Sdim#include "TargetInfo.h"
22193326Sed#include "clang/AST/ASTContext.h"
23193326Sed#include "clang/AST/DeclObjC.h"
24224145Sdim#include "clang/Frontend/CodeGenOptions.h"
25243830Sdim#include "llvm/ADT/Hashing.h"
26249423Sdim#include "llvm/IR/DataLayout.h"
27249423Sdim#include "llvm/IR/Intrinsics.h"
28249423Sdim#include "llvm/IR/LLVMContext.h"
29249423Sdim#include "llvm/IR/MDBuilder.h"
30249423Sdim#include "llvm/Support/ConvertUTF.h"
31249423Sdim
32193326Sedusing namespace clang;
33193326Sedusing namespace CodeGen;
34193326Sed
35193326Sed//===--------------------------------------------------------------------===//
36193326Sed//                        Miscellaneous Helper Methods
37193326Sed//===--------------------------------------------------------------------===//
38193326Sed
39218893Sdimllvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
40218893Sdim  unsigned addressSpace =
41218893Sdim    cast<llvm::PointerType>(value->getType())->getAddressSpace();
42218893Sdim
43226633Sdim  llvm::PointerType *destType = Int8PtrTy;
44218893Sdim  if (addressSpace)
45218893Sdim    destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
46218893Sdim
47218893Sdim  if (value->getType() == destType) return value;
48218893Sdim  return Builder.CreateBitCast(value, destType);
49218893Sdim}
50218893Sdim
51193326Sed/// CreateTempAlloca - This creates a alloca and inserts it into the entry
52193326Sed/// block.
53226633Sdimllvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
54226633Sdim                                                    const Twine &Name) {
55193326Sed  if (!Builder.isNamePreserving())
56198398Srdivacky    return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
57193326Sed  return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
58193326Sed}
59193326Sed
60207619Srdivackyvoid CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
61207619Srdivacky                                     llvm::Value *Init) {
62207619Srdivacky  llvm::StoreInst *Store = new llvm::StoreInst(Init, Var);
63207619Srdivacky  llvm::BasicBlock *Block = AllocaInsertPt->getParent();
64207619Srdivacky  Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
65207619Srdivacky}
66207619Srdivacky
67210299Sedllvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
68226633Sdim                                                const Twine &Name) {
69204643Srdivacky  llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
70204643Srdivacky  // FIXME: Should we prefer the preferred type alignment here?
71204643Srdivacky  CharUnits Align = getContext().getTypeAlignInChars(Ty);
72204643Srdivacky  Alloc->setAlignment(Align.getQuantity());
73204643Srdivacky  return Alloc;
74204643Srdivacky}
75204643Srdivacky
76210299Sedllvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
77226633Sdim                                                 const Twine &Name) {
78203955Srdivacky  llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
79203955Srdivacky  // FIXME: Should we prefer the preferred type alignment here?
80203955Srdivacky  CharUnits Align = getContext().getTypeAlignInChars(Ty);
81203955Srdivacky  Alloc->setAlignment(Align.getQuantity());
82203955Srdivacky  return Alloc;
83203955Srdivacky}
84203955Srdivacky
85193326Sed/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
86193326Sed/// expression and compare the result against zero, returning an Int1Ty value.
87193326Sedllvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
88212904Sdim  if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
89212904Sdim    llvm::Value *MemPtr = EmitScalarExpr(E);
90218893Sdim    return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
91212904Sdim  }
92212904Sdim
93193326Sed  QualType BoolTy = getContext().BoolTy;
94193326Sed  if (!E->getType()->isAnyComplexType())
95193326Sed    return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
96193326Sed
97193326Sed  return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
98193326Sed}
99193326Sed
100218893Sdim/// EmitIgnoredExpr - Emit code to compute the specified expression,
101218893Sdim/// ignoring the result.
102218893Sdimvoid CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
103218893Sdim  if (E->isRValue())
104218893Sdim    return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
105218893Sdim
106218893Sdim  // Just emit it as an l-value and drop the result.
107218893Sdim  EmitLValue(E);
108218893Sdim}
109218893Sdim
110218893Sdim/// EmitAnyExpr - Emit code to compute the specified expression which
111218893Sdim/// can have any type.  The result is returned as an RValue struct.
112218893Sdim/// If this is an aggregate expression, AggSlot indicates where the
113198092Srdivacky/// result should be returned.
114239462SdimRValue CodeGenFunction::EmitAnyExpr(const Expr *E,
115239462Sdim                                    AggValueSlot aggSlot,
116239462Sdim                                    bool ignoreResult) {
117249423Sdim  switch (getEvaluationKind(E->getType())) {
118249423Sdim  case TEK_Scalar:
119239462Sdim    return RValue::get(EmitScalarExpr(E, ignoreResult));
120249423Sdim  case TEK_Complex:
121239462Sdim    return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
122249423Sdim  case TEK_Aggregate:
123249423Sdim    if (!ignoreResult && aggSlot.isIgnored())
124249423Sdim      aggSlot = CreateAggTemp(E->getType(), "agg-temp");
125249423Sdim    EmitAggExpr(E, aggSlot);
126249423Sdim    return aggSlot.asRValue();
127249423Sdim  }
128249423Sdim  llvm_unreachable("bad evaluation kind");
129193326Sed}
130193326Sed
131198092Srdivacky/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
132198092Srdivacky/// always be accessible even if no aggregate location is provided.
133218893SdimRValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
134218893Sdim  AggValueSlot AggSlot = AggValueSlot::ignored();
135198092Srdivacky
136249423Sdim  if (hasAggregateEvaluationKind(E->getType()))
137218893Sdim    AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
138218893Sdim  return EmitAnyExpr(E, AggSlot);
139193326Sed}
140193326Sed
141207619Srdivacky/// EmitAnyExprToMem - Evaluate an expression into a given memory
142207619Srdivacky/// location.
143207619Srdivackyvoid CodeGenFunction::EmitAnyExprToMem(const Expr *E,
144207619Srdivacky                                       llvm::Value *Location,
145224145Sdim                                       Qualifiers Quals,
146207619Srdivacky                                       bool IsInit) {
147234353Sdim  // FIXME: This function should take an LValue as an argument.
148249423Sdim  switch (getEvaluationKind(E->getType())) {
149249423Sdim  case TEK_Complex:
150249423Sdim    EmitComplexExprIntoLValue(E,
151249423Sdim                         MakeNaturalAlignAddrLValue(Location, E->getType()),
152249423Sdim                              /*isInit*/ false);
153249423Sdim    return;
154249423Sdim
155249423Sdim  case TEK_Aggregate: {
156234353Sdim    CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
157234353Sdim    EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals,
158226633Sdim                                         AggValueSlot::IsDestructed_t(IsInit),
159226633Sdim                                         AggValueSlot::DoesNotNeedGCBarriers,
160226633Sdim                                         AggValueSlot::IsAliased_t(!IsInit)));
161249423Sdim    return;
162249423Sdim  }
163249423Sdim
164249423Sdim  case TEK_Scalar: {
165207619Srdivacky    RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
166212904Sdim    LValue LV = MakeAddrLValue(Location, E->getType());
167224145Sdim    EmitStoreThroughLValue(RV, LV);
168249423Sdim    return;
169207619Srdivacky  }
170249423Sdim  }
171249423Sdim  llvm_unreachable("bad evaluation kind");
172207619Srdivacky}
173207619Srdivacky
174263508Sdimstatic void
175263508SdimpushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
176263508Sdim                     const Expr *E, llvm::Value *ReferenceTemporary) {
177263508Sdim  // Objective-C++ ARC:
178263508Sdim  //   If we are binding a reference to a temporary that has ownership, we
179263508Sdim  //   need to perform retain/release operations on the temporary.
180263508Sdim  //
181263508Sdim  // FIXME: This should be looking at E, not M.
182263508Sdim  if (CGF.getLangOpts().ObjCAutoRefCount &&
183263508Sdim      M->getType()->isObjCLifetimeType()) {
184263508Sdim    QualType ObjCARCReferenceLifetimeType = M->getType();
185263508Sdim    switch (Qualifiers::ObjCLifetime Lifetime =
186263508Sdim                ObjCARCReferenceLifetimeType.getObjCLifetime()) {
187263508Sdim    case Qualifiers::OCL_None:
188263508Sdim    case Qualifiers::OCL_ExplicitNone:
189263508Sdim      // Carry on to normal cleanup handling.
190263508Sdim      break;
191218893Sdim
192263508Sdim    case Qualifiers::OCL_Autoreleasing:
193263508Sdim      // Nothing to do; cleaned up by an autorelease pool.
194263508Sdim      return;
195263508Sdim
196263508Sdim    case Qualifiers::OCL_Strong:
197263508Sdim    case Qualifiers::OCL_Weak:
198263508Sdim      switch (StorageDuration Duration = M->getStorageDuration()) {
199263508Sdim      case SD_Static:
200263508Sdim        // Note: we intentionally do not register a cleanup to release
201263508Sdim        // the object on program termination.
202263508Sdim        return;
203263508Sdim
204263508Sdim      case SD_Thread:
205263508Sdim        // FIXME: We should probably register a cleanup in this case.
206263508Sdim        return;
207263508Sdim
208263508Sdim      case SD_Automatic:
209263508Sdim      case SD_FullExpression:
210263508Sdim        assert(!ObjCARCReferenceLifetimeType->isArrayType());
211263508Sdim        CodeGenFunction::Destroyer *Destroy;
212263508Sdim        CleanupKind CleanupKind;
213263508Sdim        if (Lifetime == Qualifiers::OCL_Strong) {
214263508Sdim          const ValueDecl *VD = M->getExtendingDecl();
215263508Sdim          bool Precise =
216263508Sdim              VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
217263508Sdim          CleanupKind = CGF.getARCCleanupKind();
218263508Sdim          Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
219263508Sdim                            : &CodeGenFunction::destroyARCStrongImprecise;
220263508Sdim        } else {
221263508Sdim          // __weak objects always get EH cleanups; otherwise, exceptions
222263508Sdim          // could cause really nasty crashes instead of mere leaks.
223263508Sdim          CleanupKind = NormalAndEHCleanup;
224263508Sdim          Destroy = &CodeGenFunction::destroyARCWeak;
225263508Sdim        }
226263508Sdim        if (Duration == SD_FullExpression)
227263508Sdim          CGF.pushDestroy(CleanupKind, ReferenceTemporary,
228263508Sdim                          ObjCARCReferenceLifetimeType, *Destroy,
229263508Sdim                          CleanupKind & EHCleanup);
230263508Sdim        else
231263508Sdim          CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
232263508Sdim                                          ObjCARCReferenceLifetimeType,
233263508Sdim                                          *Destroy, CleanupKind & EHCleanup);
234263508Sdim        return;
235263508Sdim
236263508Sdim      case SD_Dynamic:
237263508Sdim        llvm_unreachable("temporary cannot have dynamic storage duration");
238263508Sdim      }
239263508Sdim      llvm_unreachable("unknown storage duration");
240210299Sed    }
241210299Sed  }
242201361Srdivacky
243263508Sdim  CXXDestructorDecl *ReferenceTemporaryDtor = 0;
244263508Sdim  if (const RecordType *RT =
245263508Sdim          E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
246263508Sdim    // Get the destructor for the reference temporary.
247263508Sdim    CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
248263508Sdim    if (!ClassDecl->hasTrivialDestructor())
249263508Sdim      ReferenceTemporaryDtor = ClassDecl->getDestructor();
250263508Sdim  }
251263508Sdim
252263508Sdim  if (!ReferenceTemporaryDtor)
253263508Sdim    return;
254263508Sdim
255263508Sdim  // Call the destructor for the temporary.
256263508Sdim  switch (M->getStorageDuration()) {
257263508Sdim  case SD_Static:
258263508Sdim  case SD_Thread: {
259263508Sdim    llvm::Constant *CleanupFn;
260263508Sdim    llvm::Constant *CleanupArg;
261263508Sdim    if (E->getType()->isArrayType()) {
262263508Sdim      CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
263263508Sdim          cast<llvm::Constant>(ReferenceTemporary), E->getType(),
264263508Sdim          CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions,
265263508Sdim          dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
266263508Sdim      CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
267263508Sdim    } else {
268263508Sdim      CleanupFn =
269263508Sdim        CGF.CGM.GetAddrOfCXXDestructor(ReferenceTemporaryDtor, Dtor_Complete);
270263508Sdim      CleanupArg = cast<llvm::Constant>(ReferenceTemporary);
271263508Sdim    }
272263508Sdim    CGF.CGM.getCXXABI().registerGlobalDtor(
273263508Sdim        CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
274263508Sdim    break;
275263508Sdim  }
276263508Sdim
277263508Sdim  case SD_FullExpression:
278263508Sdim    CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
279263508Sdim                    CodeGenFunction::destroyCXXObject,
280263508Sdim                    CGF.getLangOpts().Exceptions);
281263508Sdim    break;
282263508Sdim
283263508Sdim  case SD_Automatic:
284263508Sdim    CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup,
285263508Sdim                                    ReferenceTemporary, E->getType(),
286263508Sdim                                    CodeGenFunction::destroyCXXObject,
287263508Sdim                                    CGF.getLangOpts().Exceptions);
288263508Sdim    break;
289263508Sdim
290263508Sdim  case SD_Dynamic:
291263508Sdim    llvm_unreachable("temporary cannot have dynamic storage duration");
292263508Sdim  }
293210299Sed}
294210299Sed
295210299Sedstatic llvm::Value *
296263508SdimcreateReferenceTemporary(CodeGenFunction &CGF,
297263508Sdim                         const MaterializeTemporaryExpr *M, const Expr *Inner) {
298263508Sdim  switch (M->getStorageDuration()) {
299263508Sdim  case SD_FullExpression:
300263508Sdim  case SD_Automatic:
301263508Sdim    return CGF.CreateMemTemp(Inner->getType(), "ref.tmp");
302234353Sdim
303263508Sdim  case SD_Thread:
304263508Sdim  case SD_Static:
305263508Sdim    return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
306201361Srdivacky
307263508Sdim  case SD_Dynamic:
308263508Sdim    llvm_unreachable("temporary can't have dynamic storage duration");
309198112Srdivacky  }
310263508Sdim  llvm_unreachable("unknown storage duration");
311263508Sdim}
312210299Sed
313263508SdimLValue CodeGenFunction::EmitMaterializeTemporaryExpr(
314263508Sdim                                           const MaterializeTemporaryExpr *M) {
315263508Sdim  const Expr *E = M->GetTemporaryExpr();
316224145Sdim
317263508Sdim  if (getLangOpts().ObjCAutoRefCount &&
318263508Sdim      M->getType()->isObjCLifetimeType() &&
319263508Sdim      M->getType().getObjCLifetime() != Qualifiers::OCL_None &&
320263508Sdim      M->getType().getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
321263508Sdim    // FIXME: Fold this into the general case below.
322263508Sdim    llvm::Value *Object = createReferenceTemporary(*this, M, E);
323263508Sdim    LValue RefTempDst = MakeAddrLValue(Object, M->getType());
324263508Sdim
325263508Sdim    if (llvm::GlobalVariable *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
326263508Sdim      // We should not have emitted the initializer for this temporary as a
327263508Sdim      // constant.
328263508Sdim      assert(!Var->hasInitializer());
329263508Sdim      Var->setInitializer(CGM.EmitNullConstant(E->getType()));
330251662Sdim    }
331263508Sdim
332263508Sdim    EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
333263508Sdim
334263508Sdim    pushTemporaryCleanup(*this, M, E, Object);
335263508Sdim    return RefTempDst;
336251662Sdim  }
337243830Sdim
338263508Sdim  SmallVector<const Expr *, 2> CommaLHSs;
339251662Sdim  SmallVector<SubobjectAdjustment, 2> Adjustments;
340263508Sdim  E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
341207619Srdivacky
342263508Sdim  for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I)
343263508Sdim    EmitIgnoredExpr(CommaLHSs[I]);
344263508Sdim
345263508Sdim  if (const OpaqueValueExpr *opaque = dyn_cast<OpaqueValueExpr>(E)) {
346263508Sdim    if (opaque->getType()->isRecordType()) {
347263508Sdim      assert(Adjustments.empty());
348263508Sdim      return EmitOpaqueValueLValue(opaque);
349263508Sdim    }
350251662Sdim  }
351263508Sdim
352263508Sdim  // Create and initialize the reference temporary.
353263508Sdim  llvm::Value *Object = createReferenceTemporary(*this, M, E);
354263508Sdim  if (llvm::GlobalVariable *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
355263508Sdim    // If the temporary is a global and has a constant initializer, we may
356263508Sdim    // have already initialized it.
357263508Sdim    if (!Var->hasInitializer()) {
358263508Sdim      Var->setInitializer(CGM.EmitNullConstant(E->getType()));
359263508Sdim      EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
360218893Sdim    }
361263508Sdim  } else {
362263508Sdim    EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
363251662Sdim  }
364263508Sdim  pushTemporaryCleanup(*this, M, E, Object);
365210299Sed
366263508Sdim  // Perform derived-to-base casts and/or field accesses, to get from the
367263508Sdim  // temporary object we created (and, potentially, for which we extended
368263508Sdim  // the lifetime) to the subobject we're binding the reference to.
369263508Sdim  for (unsigned I = Adjustments.size(); I != 0; --I) {
370263508Sdim    SubobjectAdjustment &Adjustment = Adjustments[I-1];
371263508Sdim    switch (Adjustment.Kind) {
372263508Sdim    case SubobjectAdjustment::DerivedToBaseAdjustment:
373263508Sdim      Object =
374263508Sdim          GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
375263508Sdim                                Adjustment.DerivedToBase.BasePath->path_begin(),
376263508Sdim                                Adjustment.DerivedToBase.BasePath->path_end(),
377263508Sdim                                /*NullCheckValue=*/ false);
378263508Sdim      break;
379224145Sdim
380263508Sdim    case SubobjectAdjustment::FieldAdjustment: {
381263508Sdim      LValue LV = MakeAddrLValue(Object, E->getType());
382263508Sdim      LV = EmitLValueForField(LV, Adjustment.Field);
383263508Sdim      assert(LV.isSimple() &&
384263508Sdim             "materialized temporary field is not a simple lvalue");
385263508Sdim      Object = LV.getAddress();
386263508Sdim      break;
387263508Sdim    }
388210299Sed
389263508Sdim    case SubobjectAdjustment::MemberPointerAdjustment: {
390263508Sdim      llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
391263508Sdim      Object = CGM.getCXXABI().EmitMemberDataPointerAddress(
392263508Sdim                    *this, Object, Ptr, Adjustment.Ptr.MPT);
393263508Sdim      break;
394251662Sdim    }
395263508Sdim    }
396193326Sed  }
397193326Sed
398263508Sdim  return MakeAddrLValue(Object, M->getType());
399210299Sed}
400210299Sed
401210299SedRValue
402263508SdimCodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) {
403263508Sdim  // Emit the expression as an lvalue.
404263508Sdim  LValue LV = EmitLValue(E);
405263508Sdim  assert(LV.isSimple());
406263508Sdim  llvm::Value *Value = LV.getAddress();
407263508Sdim
408243830Sdim  if (SanitizePerformTypeCheck && !E->getType()->isFunctionType()) {
409243830Sdim    // C++11 [dcl.ref]p5 (as amended by core issue 453):
410243830Sdim    //   If a glvalue to which a reference is directly bound designates neither
411243830Sdim    //   an existing object or function of an appropriate type nor a region of
412243830Sdim    //   storage of suitable size and alignment to contain an object of the
413243830Sdim    //   reference's type, the behavior is undefined.
414243830Sdim    QualType Ty = E->getType();
415243830Sdim    EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty);
416243830Sdim  }
417193326Sed
418210299Sed  return RValue::get(Value);
419193326Sed}
420193326Sed
421193326Sed
422198092Srdivacky/// getAccessedFieldNo - Given an encoded value and a result number, return the
423198092Srdivacky/// input field number being accessed.
424198092Srdivackyunsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
425193326Sed                                             const llvm::Constant *Elts) {
426234353Sdim  return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
427234353Sdim      ->getZExtValue();
428193326Sed}
429193326Sed
430243830Sdim/// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
431243830Sdimstatic llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
432243830Sdim                                    llvm::Value *High) {
433243830Sdim  llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
434243830Sdim  llvm::Value *K47 = Builder.getInt64(47);
435243830Sdim  llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
436243830Sdim  llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
437243830Sdim  llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
438243830Sdim  llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
439243830Sdim  return Builder.CreateMul(B1, KMul);
440243830Sdim}
441243830Sdim
442243830Sdimvoid CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
443243830Sdim                                    llvm::Value *Address,
444243830Sdim                                    QualType Ty, CharUnits Alignment) {
445243830Sdim  if (!SanitizePerformTypeCheck)
446201361Srdivacky    return;
447193326Sed
448243830Sdim  // Don't check pointers outside the default address space. The null check
449243830Sdim  // isn't correct, the object-size check isn't supported by LLVM, and we can't
450243830Sdim  // communicate the addresses to the runtime handler for the vptr check.
451243830Sdim  if (Address->getType()->getPointerAddressSpace())
452243830Sdim    return;
453201361Srdivacky
454243830Sdim  llvm::Value *Cond = 0;
455249423Sdim  llvm::BasicBlock *Done = 0;
456207619Srdivacky
457249423Sdim  if (SanOpts->Null) {
458243830Sdim    // The glvalue must not be an empty glvalue.
459243830Sdim    Cond = Builder.CreateICmpNE(
460243830Sdim        Address, llvm::Constant::getNullValue(Address->getType()));
461249423Sdim
462249423Sdim    if (TCK == TCK_DowncastPointer) {
463249423Sdim      // When performing a pointer downcast, it's OK if the value is null.
464249423Sdim      // Skip the remaining checks in that case.
465249423Sdim      Done = createBasicBlock("null");
466249423Sdim      llvm::BasicBlock *Rest = createBasicBlock("not.null");
467249423Sdim      Builder.CreateCondBr(Cond, Rest, Done);
468249423Sdim      EmitBlock(Rest);
469249423Sdim      Cond = 0;
470249423Sdim    }
471243830Sdim  }
472243830Sdim
473249423Sdim  if (SanOpts->ObjectSize && !Ty->isIncompleteType()) {
474243830Sdim    uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity();
475243830Sdim
476243830Sdim    // The glvalue must refer to a large enough storage region.
477243830Sdim    // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation
478243830Sdim    //        to check this.
479263508Sdim    // FIXME: Get object address space
480263508Sdim    llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy };
481263508Sdim    llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
482243830Sdim    llvm::Value *Min = Builder.getFalse();
483243830Sdim    llvm::Value *CastAddr = Builder.CreateBitCast(Address, Int8PtrTy);
484243830Sdim    llvm::Value *LargeEnough =
485243830Sdim        Builder.CreateICmpUGE(Builder.CreateCall2(F, CastAddr, Min),
486243830Sdim                              llvm::ConstantInt::get(IntPtrTy, Size));
487243830Sdim    Cond = Cond ? Builder.CreateAnd(Cond, LargeEnough) : LargeEnough;
488243830Sdim  }
489243830Sdim
490243830Sdim  uint64_t AlignVal = 0;
491243830Sdim
492249423Sdim  if (SanOpts->Alignment) {
493243830Sdim    AlignVal = Alignment.getQuantity();
494243830Sdim    if (!Ty->isIncompleteType() && !AlignVal)
495243830Sdim      AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
496243830Sdim
497243830Sdim    // The glvalue must be suitably aligned.
498243830Sdim    if (AlignVal) {
499243830Sdim      llvm::Value *Align =
500243830Sdim          Builder.CreateAnd(Builder.CreatePtrToInt(Address, IntPtrTy),
501243830Sdim                            llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
502243830Sdim      llvm::Value *Aligned =
503243830Sdim        Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
504243830Sdim      Cond = Cond ? Builder.CreateAnd(Cond, Aligned) : Aligned;
505243830Sdim    }
506243830Sdim  }
507243830Sdim
508243830Sdim  if (Cond) {
509243830Sdim    llvm::Constant *StaticData[] = {
510243830Sdim      EmitCheckSourceLocation(Loc),
511243830Sdim      EmitCheckTypeDescriptor(Ty),
512243830Sdim      llvm::ConstantInt::get(SizeTy, AlignVal),
513243830Sdim      llvm::ConstantInt::get(Int8Ty, TCK)
514243830Sdim    };
515249423Sdim    EmitCheck(Cond, "type_mismatch", StaticData, Address, CRK_Recoverable);
516243830Sdim  }
517243830Sdim
518243830Sdim  // If possible, check that the vptr indicates that there is a subobject of
519243830Sdim  // type Ty at offset zero within this object.
520249423Sdim  //
521249423Sdim  // C++11 [basic.life]p5,6:
522249423Sdim  //   [For storage which does not refer to an object within its lifetime]
523249423Sdim  //   The program has undefined behavior if:
524249423Sdim  //    -- the [pointer or glvalue] is used to access a non-static data member
525249423Sdim  //       or call a non-static member function
526243830Sdim  CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
527249423Sdim  if (SanOpts->Vptr &&
528249423Sdim      (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
529249423Sdim       TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference) &&
530243830Sdim      RD && RD->hasDefinition() && RD->isDynamicClass()) {
531243830Sdim    // Compute a hash of the mangled name of the type.
532243830Sdim    //
533243830Sdim    // FIXME: This is not guaranteed to be deterministic! Move to a
534243830Sdim    //        fingerprinting mechanism once LLVM provides one. For the time
535243830Sdim    //        being the implementation happens to be deterministic.
536249423Sdim    SmallString<64> MangledName;
537243830Sdim    llvm::raw_svector_ostream Out(MangledName);
538243830Sdim    CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
539243830Sdim                                                     Out);
540243830Sdim    llvm::hash_code TypeHash = hash_value(Out.str());
541243830Sdim
542243830Sdim    // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
543243830Sdim    llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
544243830Sdim    llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
545243830Sdim    llvm::Value *VPtrAddr = Builder.CreateBitCast(Address, VPtrTy);
546243830Sdim    llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
547243830Sdim    llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
548243830Sdim
549243830Sdim    llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
550243830Sdim    Hash = Builder.CreateTrunc(Hash, IntPtrTy);
551243830Sdim
552243830Sdim    // Look the hash up in our cache.
553243830Sdim    const int CacheSize = 128;
554243830Sdim    llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
555243830Sdim    llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
556243830Sdim                                                   "__ubsan_vptr_type_cache");
557243830Sdim    llvm::Value *Slot = Builder.CreateAnd(Hash,
558243830Sdim                                          llvm::ConstantInt::get(IntPtrTy,
559243830Sdim                                                                 CacheSize-1));
560243830Sdim    llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
561243830Sdim    llvm::Value *CacheVal =
562243830Sdim      Builder.CreateLoad(Builder.CreateInBoundsGEP(Cache, Indices));
563243830Sdim
564243830Sdim    // If the hash isn't in the cache, call a runtime handler to perform the
565243830Sdim    // hard work of checking whether the vptr is for an object of the right
566243830Sdim    // type. This will either fill in the cache and return, or produce a
567243830Sdim    // diagnostic.
568243830Sdim    llvm::Constant *StaticData[] = {
569243830Sdim      EmitCheckSourceLocation(Loc),
570243830Sdim      EmitCheckTypeDescriptor(Ty),
571243830Sdim      CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
572243830Sdim      llvm::ConstantInt::get(Int8Ty, TCK)
573243830Sdim    };
574243830Sdim    llvm::Value *DynamicData[] = { Address, Hash };
575243830Sdim    EmitCheck(Builder.CreateICmpEQ(CacheVal, Hash),
576249423Sdim              "dynamic_type_cache_miss", StaticData, DynamicData,
577249423Sdim              CRK_AlwaysRecoverable);
578243830Sdim  }
579249423Sdim
580249423Sdim  if (Done) {
581249423Sdim    Builder.CreateBr(Done);
582249423Sdim    EmitBlock(Done);
583249423Sdim  }
584201361Srdivacky}
585201361Srdivacky
586249423Sdim/// Determine whether this expression refers to a flexible array member in a
587249423Sdim/// struct. We disable array bounds checks for such members.
588249423Sdimstatic bool isFlexibleArrayMemberExpr(const Expr *E) {
589249423Sdim  // For compatibility with existing code, we treat arrays of length 0 or
590249423Sdim  // 1 as flexible array members.
591249423Sdim  const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe();
592249423Sdim  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
593249423Sdim    if (CAT->getSize().ugt(1))
594249423Sdim      return false;
595249423Sdim  } else if (!isa<IncompleteArrayType>(AT))
596249423Sdim    return false;
597202379Srdivacky
598249423Sdim  E = E->IgnoreParens();
599249423Sdim
600249423Sdim  // A flexible array member must be the last member in the class.
601249423Sdim  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
602249423Sdim    // FIXME: If the base type of the member expr is not FD->getParent(),
603249423Sdim    // this should not be treated as a flexible array member access.
604249423Sdim    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
605249423Sdim      RecordDecl::field_iterator FI(
606249423Sdim          DeclContext::decl_iterator(const_cast<FieldDecl *>(FD)));
607249423Sdim      return ++FI == FD->getParent()->field_end();
608249423Sdim    }
609249423Sdim  }
610249423Sdim
611249423Sdim  return false;
612249423Sdim}
613249423Sdim
614249423Sdim/// If Base is known to point to the start of an array, return the length of
615249423Sdim/// that array. Return 0 if the length cannot be determined.
616249423Sdimstatic llvm::Value *getArrayIndexingBound(
617249423Sdim    CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) {
618249423Sdim  // For the vector indexing extension, the bound is the number of elements.
619249423Sdim  if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
620249423Sdim    IndexedType = Base->getType();
621249423Sdim    return CGF.Builder.getInt32(VT->getNumElements());
622249423Sdim  }
623249423Sdim
624249423Sdim  Base = Base->IgnoreParens();
625249423Sdim
626249423Sdim  if (const CastExpr *CE = dyn_cast<CastExpr>(Base)) {
627249423Sdim    if (CE->getCastKind() == CK_ArrayToPointerDecay &&
628249423Sdim        !isFlexibleArrayMemberExpr(CE->getSubExpr())) {
629249423Sdim      IndexedType = CE->getSubExpr()->getType();
630249423Sdim      const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
631249423Sdim      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
632249423Sdim        return CGF.Builder.getInt(CAT->getSize());
633249423Sdim      else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT))
634249423Sdim        return CGF.getVLASize(VAT).first;
635249423Sdim    }
636249423Sdim  }
637249423Sdim
638249423Sdim  return 0;
639249423Sdim}
640249423Sdim
641249423Sdimvoid CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
642249423Sdim                                      llvm::Value *Index, QualType IndexType,
643249423Sdim                                      bool Accessed) {
644263508Sdim  assert(SanOpts->ArrayBounds &&
645263508Sdim         "should not be called unless adding bounds checks");
646249423Sdim
647249423Sdim  QualType IndexedType;
648249423Sdim  llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType);
649249423Sdim  if (!Bound)
650249423Sdim    return;
651249423Sdim
652249423Sdim  bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
653249423Sdim  llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
654249423Sdim  llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
655249423Sdim
656249423Sdim  llvm::Constant *StaticData[] = {
657249423Sdim    EmitCheckSourceLocation(E->getExprLoc()),
658249423Sdim    EmitCheckTypeDescriptor(IndexedType),
659249423Sdim    EmitCheckTypeDescriptor(IndexType)
660249423Sdim  };
661249423Sdim  llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
662249423Sdim                                : Builder.CreateICmpULE(IndexVal, BoundVal);
663249423Sdim  EmitCheck(Check, "out_of_bounds", StaticData, Index, CRK_Recoverable);
664249423Sdim}
665249423Sdim
666249423Sdim
667202379SrdivackyCodeGenFunction::ComplexPairTy CodeGenFunction::
668202379SrdivackyEmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
669202379Srdivacky                         bool isInc, bool isPre) {
670263508Sdim  ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
671263508Sdim
672202379Srdivacky  llvm::Value *NextVal;
673202379Srdivacky  if (isa<llvm::IntegerType>(InVal.first->getType())) {
674202379Srdivacky    uint64_t AmountVal = isInc ? 1 : -1;
675202379Srdivacky    NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
676263508Sdim
677202379Srdivacky    // Add the inc/dec to the real part.
678202379Srdivacky    NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
679202379Srdivacky  } else {
680202379Srdivacky    QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
681202379Srdivacky    llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
682202379Srdivacky    if (!isInc)
683202379Srdivacky      FVal.changeSign();
684202379Srdivacky    NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
685263508Sdim
686202379Srdivacky    // Add the inc/dec to the real part.
687202379Srdivacky    NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
688202379Srdivacky  }
689263508Sdim
690202379Srdivacky  ComplexPairTy IncVal(NextVal, InVal.second);
691263508Sdim
692202379Srdivacky  // Store the updated result through the lvalue.
693249423Sdim  EmitStoreOfComplex(IncVal, LV, /*init*/ false);
694263508Sdim
695202379Srdivacky  // If this is a postinc, return the value read from memory, otherwise use the
696202379Srdivacky  // updated value.
697202379Srdivacky  return isPre ? IncVal : InVal;
698202379Srdivacky}
699202379Srdivacky
700202379Srdivacky
701193326Sed//===----------------------------------------------------------------------===//
702193326Sed//                         LValue Expression Emission
703193326Sed//===----------------------------------------------------------------------===//
704193326Sed
705193326SedRValue CodeGenFunction::GetUndefRValue(QualType Ty) {
706198893Srdivacky  if (Ty->isVoidType())
707193326Sed    return RValue::get(0);
708249423Sdim
709249423Sdim  switch (getEvaluationKind(Ty)) {
710249423Sdim  case TEK_Complex: {
711249423Sdim    llvm::Type *EltTy =
712249423Sdim      ConvertType(Ty->castAs<ComplexType>()->getElementType());
713193326Sed    llvm::Value *U = llvm::UndefValue::get(EltTy);
714193326Sed    return RValue::getComplex(std::make_pair(U, U));
715198893Srdivacky  }
716263508Sdim
717212904Sdim  // If this is a use of an undefined aggregate type, the aggregate must have an
718212904Sdim  // identifiable address.  Just because the contents of the value are undefined
719212904Sdim  // doesn't mean that the address can't be taken and compared.
720249423Sdim  case TEK_Aggregate: {
721212904Sdim    llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
722212904Sdim    return RValue::getAggregate(DestPtr);
723193326Sed  }
724249423Sdim
725249423Sdim  case TEK_Scalar:
726249423Sdim    return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
727249423Sdim  }
728249423Sdim  llvm_unreachable("bad evaluation kind");
729193326Sed}
730193326Sed
731193326SedRValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
732193326Sed                                              const char *Name) {
733193326Sed  ErrorUnsupported(E, Name);
734193326Sed  return GetUndefRValue(E->getType());
735193326Sed}
736193326Sed
737193326SedLValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
738193326Sed                                              const char *Name) {
739193326Sed  ErrorUnsupported(E, Name);
740193326Sed  llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
741212904Sdim  return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
742193326Sed}
743193326Sed
744243830SdimLValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
745249423Sdim  LValue LV;
746263508Sdim  if (SanOpts->ArrayBounds && isa<ArraySubscriptExpr>(E))
747249423Sdim    LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
748249423Sdim  else
749249423Sdim    LV = EmitLValue(E);
750206275Srdivacky  if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
751243830Sdim    EmitTypeCheck(TCK, E->getExprLoc(), LV.getAddress(),
752243830Sdim                  E->getType(), LV.getAlignment());
753201361Srdivacky  return LV;
754201361Srdivacky}
755201361Srdivacky
756193326Sed/// EmitLValue - Emit code to compute a designator that specifies the location
757193326Sed/// of the expression.
758193326Sed///
759198092Srdivacky/// This can return one of two things: a simple address or a bitfield reference.
760198092Srdivacky/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
761198092Srdivacky/// an LLVM pointer type.
762193326Sed///
763198092Srdivacky/// If this returns a bitfield reference, nothing about the pointee type of the
764198092Srdivacky/// LLVM value is known: For example, it may not be a pointer to an integer.
765193326Sed///
766198092Srdivacky/// If this returns a normal address, and if the lvalue's C type is fixed size,
767198092Srdivacky/// this method guarantees that the returned pointer type will point to an LLVM
768198092Srdivacky/// type of the same size of the lvalue's type.  If the lvalue has a variable
769198092Srdivacky/// length type, this is not possible.
770193326Sed///
771193326SedLValue CodeGenFunction::EmitLValue(const Expr *E) {
772193326Sed  switch (E->getStmtClass()) {
773193326Sed  default: return EmitUnsupportedLValue(E, "l-value expression");
774193326Sed
775234353Sdim  case Expr::ObjCPropertyRefExprClass:
776234353Sdim    llvm_unreachable("cannot emit a property reference directly");
777234353Sdim
778210299Sed  case Expr::ObjCSelectorExprClass:
779243830Sdim    return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
780200583Srdivacky  case Expr::ObjCIsaExprClass:
781200583Srdivacky    return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
782198092Srdivacky  case Expr::BinaryOperatorClass:
783193326Sed    return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
784207619Srdivacky  case Expr::CompoundAssignOperatorClass:
785218893Sdim    if (!E->getType()->isAnyComplexType())
786218893Sdim      return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
787218893Sdim    return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
788198092Srdivacky  case Expr::CallExprClass:
789198092Srdivacky  case Expr::CXXMemberCallExprClass:
790193326Sed  case Expr::CXXOperatorCallExprClass:
791234353Sdim  case Expr::UserDefinedLiteralClass:
792193326Sed    return EmitCallExprLValue(cast<CallExpr>(E));
793193326Sed  case Expr::VAArgExprClass:
794193326Sed    return EmitVAArgExprLValue(cast<VAArgExpr>(E));
795198092Srdivacky  case Expr::DeclRefExprClass:
796193326Sed    return EmitDeclRefLValue(cast<DeclRefExpr>(E));
797226633Sdim  case Expr::ParenExprClass:
798226633Sdim    return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
799221345Sdim  case Expr::GenericSelectionExprClass:
800221345Sdim    return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
801193326Sed  case Expr::PredefinedExprClass:
802193326Sed    return EmitPredefinedLValue(cast<PredefinedExpr>(E));
803193326Sed  case Expr::StringLiteralClass:
804193326Sed    return EmitStringLiteralLValue(cast<StringLiteral>(E));
805193326Sed  case Expr::ObjCEncodeExprClass:
806193326Sed    return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
807234353Sdim  case Expr::PseudoObjectExprClass:
808234353Sdim    return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
809234353Sdim  case Expr::InitListExprClass:
810239462Sdim    return EmitInitListLValue(cast<InitListExpr>(E));
811193326Sed  case Expr::CXXTemporaryObjectExprClass:
812193326Sed  case Expr::CXXConstructExprClass:
813193326Sed    return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
814193326Sed  case Expr::CXXBindTemporaryExprClass:
815193326Sed    return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
816243830Sdim  case Expr::CXXUuidofExprClass:
817243830Sdim    return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
818234353Sdim  case Expr::LambdaExprClass:
819234353Sdim    return EmitLambdaLValue(cast<LambdaExpr>(E));
820234353Sdim
821234353Sdim  case Expr::ExprWithCleanupsClass: {
822234353Sdim    const ExprWithCleanups *cleanups = cast<ExprWithCleanups>(E);
823234353Sdim    enterFullExpression(cleanups);
824234353Sdim    RunCleanupsScope Scope(*this);
825234353Sdim    return EmitLValue(cleanups->getSubExpr());
826234353Sdim  }
827234353Sdim
828199482Srdivacky  case Expr::CXXDefaultArgExprClass:
829199482Srdivacky    return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
830251662Sdim  case Expr::CXXDefaultInitExprClass: {
831251662Sdim    CXXDefaultInitExprScope Scope(*this);
832251662Sdim    return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr());
833251662Sdim  }
834199482Srdivacky  case Expr::CXXTypeidExprClass:
835199482Srdivacky    return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
836193326Sed
837193326Sed  case Expr::ObjCMessageExprClass:
838193326Sed    return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
839198092Srdivacky  case Expr::ObjCIvarRefExprClass:
840193326Sed    return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
841193326Sed  case Expr::StmtExprClass:
842193326Sed    return EmitStmtExprLValue(cast<StmtExpr>(E));
843198092Srdivacky  case Expr::UnaryOperatorClass:
844193326Sed    return EmitUnaryOpLValue(cast<UnaryOperator>(E));
845193326Sed  case Expr::ArraySubscriptExprClass:
846193326Sed    return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
847193326Sed  case Expr::ExtVectorElementExprClass:
848193326Sed    return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
849198092Srdivacky  case Expr::MemberExprClass:
850198092Srdivacky    return EmitMemberExpr(cast<MemberExpr>(E));
851193326Sed  case Expr::CompoundLiteralExprClass:
852193326Sed    return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
853193326Sed  case Expr::ConditionalOperatorClass:
854198092Srdivacky    return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
855218893Sdim  case Expr::BinaryConditionalOperatorClass:
856218893Sdim    return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
857193326Sed  case Expr::ChooseExprClass:
858263508Sdim    return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr());
859218893Sdim  case Expr::OpaqueValueExprClass:
860218893Sdim    return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
861224145Sdim  case Expr::SubstNonTypeTemplateParmExprClass:
862224145Sdim    return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
863193326Sed  case Expr::ImplicitCastExprClass:
864193326Sed  case Expr::CStyleCastExprClass:
865193326Sed  case Expr::CXXFunctionalCastExprClass:
866193326Sed  case Expr::CXXStaticCastExprClass:
867193326Sed  case Expr::CXXDynamicCastExprClass:
868193326Sed  case Expr::CXXReinterpretCastExprClass:
869193326Sed  case Expr::CXXConstCastExprClass:
870224145Sdim  case Expr::ObjCBridgedCastExprClass:
871193326Sed    return EmitCastLValue(cast<CastExpr>(E));
872234353Sdim
873224145Sdim  case Expr::MaterializeTemporaryExprClass:
874224145Sdim    return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
875193326Sed  }
876193326Sed}
877193326Sed
878234353Sdim/// Given an object of the given canonical type, can we safely copy a
879234353Sdim/// value out of it based on its initializer?
880234353Sdimstatic bool isConstantEmittableObjectType(QualType type) {
881234353Sdim  assert(type.isCanonical());
882234353Sdim  assert(!type->isReferenceType());
883234353Sdim
884234353Sdim  // Must be const-qualified but non-volatile.
885234353Sdim  Qualifiers qs = type.getLocalQualifiers();
886234353Sdim  if (!qs.hasConst() || qs.hasVolatile()) return false;
887234353Sdim
888234353Sdim  // Otherwise, all object types satisfy this except C++ classes with
889234353Sdim  // mutable subobjects or non-trivial copy/destroy behavior.
890234353Sdim  if (const RecordType *RT = dyn_cast<RecordType>(type))
891234353Sdim    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
892234353Sdim      if (RD->hasMutableFields() || !RD->isTrivial())
893234353Sdim        return false;
894234353Sdim
895234353Sdim  return true;
896234353Sdim}
897234353Sdim
898234353Sdim/// Can we constant-emit a load of a reference to a variable of the
899234353Sdim/// given type?  This is different from predicates like
900234353Sdim/// Decl::isUsableInConstantExpressions because we do want it to apply
901234353Sdim/// in situations that don't necessarily satisfy the language's rules
902234353Sdim/// for this (e.g. C++'s ODR-use rules).  For example, we want to able
903234353Sdim/// to do this with const float variables even if those variables
904234353Sdim/// aren't marked 'constexpr'.
905234353Sdimenum ConstantEmissionKind {
906234353Sdim  CEK_None,
907234353Sdim  CEK_AsReferenceOnly,
908234353Sdim  CEK_AsValueOrReference,
909234353Sdim  CEK_AsValueOnly
910234353Sdim};
911234353Sdimstatic ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) {
912234353Sdim  type = type.getCanonicalType();
913234353Sdim  if (const ReferenceType *ref = dyn_cast<ReferenceType>(type)) {
914234353Sdim    if (isConstantEmittableObjectType(ref->getPointeeType()))
915234353Sdim      return CEK_AsValueOrReference;
916234353Sdim    return CEK_AsReferenceOnly;
917234353Sdim  }
918234353Sdim  if (isConstantEmittableObjectType(type))
919234353Sdim    return CEK_AsValueOnly;
920234353Sdim  return CEK_None;
921234353Sdim}
922234353Sdim
923234353Sdim/// Try to emit a reference to the given value without producing it as
924234353Sdim/// an l-value.  This is actually more than an optimization: we can't
925234353Sdim/// produce an l-value for variables that we never actually captured
926234353Sdim/// in a block or lambda, which means const int variables or constexpr
927234353Sdim/// literals or similar.
928234353SdimCodeGenFunction::ConstantEmission
929234353SdimCodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) {
930234353Sdim  ValueDecl *value = refExpr->getDecl();
931234353Sdim
932234353Sdim  // The value needs to be an enum constant or a constant variable.
933234353Sdim  ConstantEmissionKind CEK;
934234353Sdim  if (isa<ParmVarDecl>(value)) {
935234353Sdim    CEK = CEK_None;
936234353Sdim  } else if (VarDecl *var = dyn_cast<VarDecl>(value)) {
937234353Sdim    CEK = checkVarTypeForConstantEmission(var->getType());
938234353Sdim  } else if (isa<EnumConstantDecl>(value)) {
939234353Sdim    CEK = CEK_AsValueOnly;
940234353Sdim  } else {
941234353Sdim    CEK = CEK_None;
942234353Sdim  }
943234353Sdim  if (CEK == CEK_None) return ConstantEmission();
944234353Sdim
945234353Sdim  Expr::EvalResult result;
946234353Sdim  bool resultIsReference;
947234353Sdim  QualType resultType;
948234353Sdim
949234353Sdim  // It's best to evaluate all the way as an r-value if that's permitted.
950234353Sdim  if (CEK != CEK_AsReferenceOnly &&
951234353Sdim      refExpr->EvaluateAsRValue(result, getContext())) {
952234353Sdim    resultIsReference = false;
953234353Sdim    resultType = refExpr->getType();
954234353Sdim
955234353Sdim  // Otherwise, try to evaluate as an l-value.
956234353Sdim  } else if (CEK != CEK_AsValueOnly &&
957234353Sdim             refExpr->EvaluateAsLValue(result, getContext())) {
958234353Sdim    resultIsReference = true;
959234353Sdim    resultType = value->getType();
960234353Sdim
961234353Sdim  // Failure.
962234353Sdim  } else {
963234353Sdim    return ConstantEmission();
964234353Sdim  }
965234353Sdim
966234353Sdim  // In any case, if the initializer has side-effects, abandon ship.
967234353Sdim  if (result.HasSideEffects)
968234353Sdim    return ConstantEmission();
969234353Sdim
970234353Sdim  // Emit as a constant.
971234353Sdim  llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this);
972234353Sdim
973234353Sdim  // Make sure we emit a debug reference to the global variable.
974263508Sdim  // This should probably fire even for
975234353Sdim  if (isa<VarDecl>(value)) {
976234353Sdim    if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
977234353Sdim      EmitDeclRefExprDbgValue(refExpr, C);
978234353Sdim  } else {
979234353Sdim    assert(isa<EnumConstantDecl>(value));
980234353Sdim    EmitDeclRefExprDbgValue(refExpr, C);
981234353Sdim  }
982234353Sdim
983234353Sdim  // If we emitted a reference constant, we need to dereference that.
984234353Sdim  if (resultIsReference)
985234353Sdim    return ConstantEmission::forReference(C);
986234353Sdim
987234353Sdim  return ConstantEmission::forValue(C);
988234353Sdim}
989234353Sdim
990263508Sdimllvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
991263508Sdim                                               SourceLocation Loc) {
992224145Sdim  return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
993234353Sdim                          lvalue.getAlignment().getQuantity(),
994263508Sdim                          lvalue.getType(), Loc, lvalue.getTBAAInfo(),
995249423Sdim                          lvalue.getTBAABaseType(), lvalue.getTBAAOffset());
996224145Sdim}
997224145Sdim
998234353Sdimstatic bool hasBooleanRepresentation(QualType Ty) {
999234353Sdim  if (Ty->isBooleanType())
1000234353Sdim    return true;
1001234353Sdim
1002234353Sdim  if (const EnumType *ET = Ty->getAs<EnumType>())
1003234353Sdim    return ET->getDecl()->getIntegerType()->isBooleanType();
1004234353Sdim
1005234353Sdim  if (const AtomicType *AT = Ty->getAs<AtomicType>())
1006234353Sdim    return hasBooleanRepresentation(AT->getValueType());
1007234353Sdim
1008234353Sdim  return false;
1009234353Sdim}
1010234353Sdim
1011249423Sdimstatic bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
1012249423Sdim                            llvm::APInt &Min, llvm::APInt &End,
1013249423Sdim                            bool StrictEnums) {
1014234353Sdim  const EnumType *ET = Ty->getAs<EnumType>();
1015249423Sdim  bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums &&
1016249423Sdim                                ET && !ET->getDecl()->isFixed();
1017234353Sdim  bool IsBool = hasBooleanRepresentation(Ty);
1018234353Sdim  if (!IsBool && !IsRegularCPlusPlusEnum)
1019249423Sdim    return false;
1020234353Sdim
1021234353Sdim  if (IsBool) {
1022249423Sdim    Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
1023249423Sdim    End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
1024234353Sdim  } else {
1025234353Sdim    const EnumDecl *ED = ET->getDecl();
1026249423Sdim    llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType());
1027234353Sdim    unsigned Bitwidth = LTy->getScalarSizeInBits();
1028234353Sdim    unsigned NumNegativeBits = ED->getNumNegativeBits();
1029234353Sdim    unsigned NumPositiveBits = ED->getNumPositiveBits();
1030234353Sdim
1031234353Sdim    if (NumNegativeBits) {
1032234353Sdim      unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
1033234353Sdim      assert(NumBits <= Bitwidth);
1034234353Sdim      End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
1035234353Sdim      Min = -End;
1036234353Sdim    } else {
1037234353Sdim      assert(NumPositiveBits <= Bitwidth);
1038234353Sdim      End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
1039234353Sdim      Min = llvm::APInt(Bitwidth, 0);
1040234353Sdim    }
1041234353Sdim  }
1042249423Sdim  return true;
1043249423Sdim}
1044234353Sdim
1045249423Sdimllvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
1046249423Sdim  llvm::APInt Min, End;
1047249423Sdim  if (!getRangeForType(*this, Ty, Min, End,
1048249423Sdim                       CGM.getCodeGenOpts().StrictEnums))
1049249423Sdim    return 0;
1050249423Sdim
1051234982Sdim  llvm::MDBuilder MDHelper(getLLVMContext());
1052234982Sdim  return MDHelper.createRange(Min, End);
1053234353Sdim}
1054234353Sdim
1055193326Sedllvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
1056263508Sdim                                               unsigned Alignment, QualType Ty,
1057263508Sdim                                               SourceLocation Loc,
1058263508Sdim                                               llvm::MDNode *TBAAInfo,
1059263508Sdim                                               QualType TBAABaseType,
1060263508Sdim                                               uint64_t TBAAOffset) {
1061239462Sdim  // For better performance, handle vector loads differently.
1062239462Sdim  if (Ty->isVectorType()) {
1063239462Sdim    llvm::Value *V;
1064239462Sdim    const llvm::Type *EltTy =
1065239462Sdim    cast<llvm::PointerType>(Addr->getType())->getElementType();
1066263508Sdim
1067239462Sdim    const llvm::VectorType *VTy = cast<llvm::VectorType>(EltTy);
1068263508Sdim
1069239462Sdim    // Handle vectors of size 3, like size 4 for better performance.
1070239462Sdim    if (VTy->getNumElements() == 3) {
1071263508Sdim
1072239462Sdim      // Bitcast to vec4 type.
1073239462Sdim      llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(),
1074239462Sdim                                                         4);
1075239462Sdim      llvm::PointerType *ptVec4Ty =
1076239462Sdim      llvm::PointerType::get(vec4Ty,
1077239462Sdim                             (cast<llvm::PointerType>(
1078239462Sdim                                      Addr->getType()))->getAddressSpace());
1079239462Sdim      llvm::Value *Cast = Builder.CreateBitCast(Addr, ptVec4Ty,
1080239462Sdim                                                "castToVec4");
1081239462Sdim      // Now load value.
1082239462Sdim      llvm::Value *LoadVal = Builder.CreateLoad(Cast, Volatile, "loadVec4");
1083249423Sdim
1084239462Sdim      // Shuffle vector to get vec3.
1085249423Sdim      llvm::Constant *Mask[] = {
1086249423Sdim        llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 0),
1087249423Sdim        llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 1),
1088249423Sdim        llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 2)
1089249423Sdim      };
1090249423Sdim
1091239462Sdim      llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1092239462Sdim      V = Builder.CreateShuffleVector(LoadVal,
1093239462Sdim                                      llvm::UndefValue::get(vec4Ty),
1094239462Sdim                                      MaskV, "extractVec");
1095239462Sdim      return EmitFromMemory(V, Ty);
1096239462Sdim    }
1097239462Sdim  }
1098249423Sdim
1099249423Sdim  // Atomic operations have to be done on integral types.
1100249423Sdim  if (Ty->isAtomicType()) {
1101249423Sdim    LValue lvalue = LValue::MakeAddr(Addr, Ty,
1102249423Sdim                                     CharUnits::fromQuantity(Alignment),
1103249423Sdim                                     getContext(), TBAAInfo);
1104263508Sdim    return EmitAtomicLoad(lvalue, Loc).getScalarVal();
1105249423Sdim  }
1106263508Sdim
1107226633Sdim  llvm::LoadInst *Load = Builder.CreateLoad(Addr);
1108199990Srdivacky  if (Volatile)
1109199990Srdivacky    Load->setVolatile(true);
1110212904Sdim  if (Alignment)
1111212904Sdim    Load->setAlignment(Alignment);
1112249423Sdim  if (TBAAInfo) {
1113249423Sdim    llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1114249423Sdim                                                      TBAAOffset);
1115263508Sdim    if (TBAAPath)
1116263508Sdim      CGM.DecorateInstruction(Load, TBAAPath, false/*ConvertTypeToTag*/);
1117249423Sdim  }
1118193326Sed
1119249423Sdim  if ((SanOpts->Bool && hasBooleanRepresentation(Ty)) ||
1120249423Sdim      (SanOpts->Enum && Ty->getAs<EnumType>())) {
1121249423Sdim    llvm::APInt Min, End;
1122249423Sdim    if (getRangeForType(*this, Ty, Min, End, true)) {
1123249423Sdim      --End;
1124249423Sdim      llvm::Value *Check;
1125249423Sdim      if (!Min)
1126249423Sdim        Check = Builder.CreateICmpULE(
1127249423Sdim          Load, llvm::ConstantInt::get(getLLVMContext(), End));
1128249423Sdim      else {
1129249423Sdim        llvm::Value *Upper = Builder.CreateICmpSLE(
1130249423Sdim          Load, llvm::ConstantInt::get(getLLVMContext(), End));
1131249423Sdim        llvm::Value *Lower = Builder.CreateICmpSGE(
1132249423Sdim          Load, llvm::ConstantInt::get(getLLVMContext(), Min));
1133249423Sdim        Check = Builder.CreateAnd(Upper, Lower);
1134249423Sdim      }
1135263508Sdim      llvm::Constant *StaticArgs[] = {
1136263508Sdim        EmitCheckSourceLocation(Loc),
1137263508Sdim        EmitCheckTypeDescriptor(Ty)
1138263508Sdim      };
1139263508Sdim      EmitCheck(Check, "load_invalid_value", StaticArgs, EmitCheckValue(Load),
1140263508Sdim                CRK_Recoverable);
1141249423Sdim    }
1142249423Sdim  } else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
1143234353Sdim    if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
1144234353Sdim      Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
1145234353Sdim
1146218893Sdim  return EmitFromMemory(Load, Ty);
1147218893Sdim}
1148198092Srdivacky
1149218893Sdimllvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
1150218893Sdim  // Bool has a different representation in memory than in registers.
1151234353Sdim  if (hasBooleanRepresentation(Ty)) {
1152218893Sdim    // This should really always be an i1, but sometimes it's already
1153218893Sdim    // an i8, and it's awkward to track those cases down.
1154218893Sdim    if (Value->getType()->isIntegerTy(1))
1155249423Sdim      return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool");
1156249423Sdim    assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1157249423Sdim           "wrong value rep of bool");
1158218893Sdim  }
1159198092Srdivacky
1160218893Sdim  return Value;
1161218893Sdim}
1162218893Sdim
1163218893Sdimllvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
1164218893Sdim  // Bool has a different representation in memory than in registers.
1165234353Sdim  if (hasBooleanRepresentation(Ty)) {
1166249423Sdim    assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1167249423Sdim           "wrong value rep of bool");
1168218893Sdim    return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
1169193326Sed  }
1170212904Sdim
1171218893Sdim  return Value;
1172218893Sdim}
1173218893Sdim
1174218893Sdimvoid CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
1175218893Sdim                                        bool Volatile, unsigned Alignment,
1176263508Sdim                                        QualType Ty, llvm::MDNode *TBAAInfo,
1177249423Sdim                                        bool isInit, QualType TBAABaseType,
1178249423Sdim                                        uint64_t TBAAOffset) {
1179263508Sdim
1180239462Sdim  // Handle vectors differently to get better performance.
1181239462Sdim  if (Ty->isVectorType()) {
1182239462Sdim    llvm::Type *SrcTy = Value->getType();
1183239462Sdim    llvm::VectorType *VecTy = cast<llvm::VectorType>(SrcTy);
1184239462Sdim    // Handle vec3 special.
1185239462Sdim    if (VecTy->getNumElements() == 3) {
1186239462Sdim      llvm::LLVMContext &VMContext = getLLVMContext();
1187263508Sdim
1188239462Sdim      // Our source is a vec3, do a shuffle vector to make it a vec4.
1189249423Sdim      SmallVector<llvm::Constant*, 4> Mask;
1190263508Sdim      Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1191239462Sdim                                            0));
1192263508Sdim      Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1193239462Sdim                                            1));
1194263508Sdim      Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1195239462Sdim                                            2));
1196239462Sdim      Mask.push_back(llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)));
1197263508Sdim
1198239462Sdim      llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1199239462Sdim      Value = Builder.CreateShuffleVector(Value,
1200239462Sdim                                          llvm::UndefValue::get(VecTy),
1201239462Sdim                                          MaskV, "extractVec");
1202239462Sdim      SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4);
1203239462Sdim    }
1204239462Sdim    llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
1205239462Sdim    if (DstPtr->getElementType() != SrcTy) {
1206239462Sdim      llvm::Type *MemTy =
1207239462Sdim      llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
1208239462Sdim      Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
1209239462Sdim    }
1210239462Sdim  }
1211263508Sdim
1212218893Sdim  Value = EmitToMemory(Value, Ty);
1213249423Sdim
1214249423Sdim  if (Ty->isAtomicType()) {
1215249423Sdim    EmitAtomicStore(RValue::get(Value),
1216249423Sdim                    LValue::MakeAddr(Addr, Ty,
1217249423Sdim                                     CharUnits::fromQuantity(Alignment),
1218249423Sdim                                     getContext(), TBAAInfo),
1219249423Sdim                    isInit);
1220249423Sdim    return;
1221249423Sdim  }
1222249423Sdim
1223212904Sdim  llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
1224212904Sdim  if (Alignment)
1225212904Sdim    Store->setAlignment(Alignment);
1226249423Sdim  if (TBAAInfo) {
1227249423Sdim    llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1228249423Sdim                                                      TBAAOffset);
1229263508Sdim    if (TBAAPath)
1230263508Sdim      CGM.DecorateInstruction(Store, TBAAPath, false/*ConvertTypeToTag*/);
1231249423Sdim  }
1232193326Sed}
1233193326Sed
1234234353Sdimvoid CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
1235249423Sdim                                        bool isInit) {
1236224145Sdim  EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
1237234353Sdim                    lvalue.getAlignment().getQuantity(), lvalue.getType(),
1238249423Sdim                    lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(),
1239249423Sdim                    lvalue.getTBAAOffset());
1240224145Sdim}
1241224145Sdim
1242198092Srdivacky/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
1243198092Srdivacky/// method emits the address of the lvalue, then loads the result as an rvalue,
1244198092Srdivacky/// returning the rvalue.
1245263508SdimRValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
1246193326Sed  if (LV.isObjCWeak()) {
1247198092Srdivacky    // load of a __weak object.
1248193326Sed    llvm::Value *AddrWeakObj = LV.getAddress();
1249198893Srdivacky    return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
1250198893Srdivacky                                                             AddrWeakObj));
1251193326Sed  }
1252249423Sdim  if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
1253249423Sdim    llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress());
1254249423Sdim    Object = EmitObjCConsumeObject(LV.getType(), Object);
1255249423Sdim    return RValue::get(Object);
1256249423Sdim  }
1257198092Srdivacky
1258193326Sed  if (LV.isSimple()) {
1259224145Sdim    assert(!LV.getType()->isFunctionType());
1260198092Srdivacky
1261212904Sdim    // Everything needs a load.
1262263508Sdim    return RValue::get(EmitLoadOfScalar(LV, Loc));
1263193326Sed  }
1264198092Srdivacky
1265193326Sed  if (LV.isVectorElt()) {
1266234353Sdim    llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(),
1267234353Sdim                                              LV.isVolatileQualified());
1268234353Sdim    Load->setAlignment(LV.getAlignment().getQuantity());
1269234353Sdim    return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
1270193326Sed                                                    "vecext"));
1271193326Sed  }
1272193326Sed
1273193326Sed  // If this is a reference to a subset of the elements of a vector, either
1274193326Sed  // shuffle the input or extract/insert them as appropriate.
1275193326Sed  if (LV.isExtVectorElt())
1276224145Sdim    return EmitLoadOfExtVectorElementLValue(LV);
1277193326Sed
1278234353Sdim  assert(LV.isBitField() && "Unknown LValue type!");
1279234353Sdim  return EmitLoadOfBitfieldLValue(LV);
1280193326Sed}
1281193326Sed
1282224145SdimRValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
1283206275Srdivacky  const CGBitFieldInfo &Info = LV.getBitFieldInfo();
1284193326Sed
1285207619Srdivacky  // Get the output type.
1286226633Sdim  llvm::Type *ResLTy = ConvertType(LV.getType());
1287193326Sed
1288249423Sdim  llvm::Value *Ptr = LV.getBitFieldAddr();
1289249423Sdim  llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),
1290249423Sdim                                        "bf.load");
1291249423Sdim  cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
1292198092Srdivacky
1293249423Sdim  if (Info.IsSigned) {
1294249423Sdim    assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize);
1295249423Sdim    unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size;
1296249423Sdim    if (HighBits)
1297249423Sdim      Val = Builder.CreateShl(Val, HighBits, "bf.shl");
1298249423Sdim    if (Info.Offset + HighBits)
1299249423Sdim      Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr");
1300249423Sdim  } else {
1301249423Sdim    if (Info.Offset)
1302249423Sdim      Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr");
1303249423Sdim    if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize)
1304249423Sdim      Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize,
1305249423Sdim                                                              Info.Size),
1306249423Sdim                              "bf.clear");
1307193326Sed  }
1308249423Sdim  Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
1309193326Sed
1310249423Sdim  return RValue::get(Val);
1311193326Sed}
1312193326Sed
1313193326Sed// If this is a reference to a subset of the elements of a vector, create an
1314193326Sed// appropriate shufflevector.
1315224145SdimRValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
1316234353Sdim  llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(),
1317234353Sdim                                            LV.isVolatileQualified());
1318234353Sdim  Load->setAlignment(LV.getAlignment().getQuantity());
1319234353Sdim  llvm::Value *Vec = Load;
1320198092Srdivacky
1321193326Sed  const llvm::Constant *Elts = LV.getExtVectorElts();
1322198092Srdivacky
1323198092Srdivacky  // If the result of the expression is a non-vector type, we must be extracting
1324198092Srdivacky  // a single element.  Just codegen as an extractelement.
1325224145Sdim  const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
1326193326Sed  if (!ExprVT) {
1327193326Sed    unsigned InIdx = getAccessedFieldNo(0, Elts);
1328210299Sed    llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1329226633Sdim    return RValue::get(Builder.CreateExtractElement(Vec, Elt));
1330193326Sed  }
1331193326Sed
1332193326Sed  // Always use shuffle vector to try to retain the original program structure
1333193326Sed  unsigned NumResultElts = ExprVT->getNumElements();
1334198092Srdivacky
1335226633Sdim  SmallVector<llvm::Constant*, 4> Mask;
1336234353Sdim  for (unsigned i = 0; i != NumResultElts; ++i)
1337234353Sdim    Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts)));
1338198092Srdivacky
1339218893Sdim  llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1340218893Sdim  Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
1341226633Sdim                                    MaskV);
1342193326Sed  return RValue::get(Vec);
1343193326Sed}
1344193326Sed
1345193326Sed
1346193326Sed
1347193326Sed/// EmitStoreThroughLValue - Store the specified rvalue into the specified
1348193326Sed/// lvalue, where both are guaranteed to the have the same type, and that type
1349193326Sed/// is 'Ty'.
1350263508Sdimvoid CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
1351263508Sdim                                             bool isInit) {
1352193326Sed  if (!Dst.isSimple()) {
1353193326Sed    if (Dst.isVectorElt()) {
1354193326Sed      // Read/modify/write the vector, inserting the new element.
1355234353Sdim      llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(),
1356234353Sdim                                                Dst.isVolatileQualified());
1357234353Sdim      Load->setAlignment(Dst.getAlignment().getQuantity());
1358234353Sdim      llvm::Value *Vec = Load;
1359193326Sed      Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
1360193326Sed                                        Dst.getVectorIdx(), "vecins");
1361234353Sdim      llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(),
1362234353Sdim                                                   Dst.isVolatileQualified());
1363234353Sdim      Store->setAlignment(Dst.getAlignment().getQuantity());
1364193326Sed      return;
1365193326Sed    }
1366198092Srdivacky
1367193326Sed    // If this is an update of extended vector elements, insert them as
1368193326Sed    // appropriate.
1369193326Sed    if (Dst.isExtVectorElt())
1370224145Sdim      return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
1371193326Sed
1372234353Sdim    assert(Dst.isBitField() && "Unknown LValue type");
1373234353Sdim    return EmitStoreThroughBitfieldLValue(Src, Dst);
1374193326Sed  }
1375198092Srdivacky
1376224145Sdim  // There's special magic for assigning into an ARC-qualified l-value.
1377224145Sdim  if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
1378224145Sdim    switch (Lifetime) {
1379224145Sdim    case Qualifiers::OCL_None:
1380224145Sdim      llvm_unreachable("present but none");
1381224145Sdim
1382224145Sdim    case Qualifiers::OCL_ExplicitNone:
1383224145Sdim      // nothing special
1384224145Sdim      break;
1385224145Sdim
1386224145Sdim    case Qualifiers::OCL_Strong:
1387224145Sdim      EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
1388224145Sdim      return;
1389224145Sdim
1390224145Sdim    case Qualifiers::OCL_Weak:
1391224145Sdim      EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
1392224145Sdim      return;
1393224145Sdim
1394224145Sdim    case Qualifiers::OCL_Autoreleasing:
1395224145Sdim      Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
1396224145Sdim                                                     Src.getScalarVal()));
1397224145Sdim      // fall into the normal path
1398224145Sdim      break;
1399224145Sdim    }
1400224145Sdim  }
1401224145Sdim
1402193326Sed  if (Dst.isObjCWeak() && !Dst.isNonGC()) {
1403198092Srdivacky    // load of a __weak object.
1404193326Sed    llvm::Value *LvalueDst = Dst.getAddress();
1405193326Sed    llvm::Value *src = Src.getScalarVal();
1406193326Sed     CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
1407193326Sed    return;
1408193326Sed  }
1409198092Srdivacky
1410193326Sed  if (Dst.isObjCStrong() && !Dst.isNonGC()) {
1411198092Srdivacky    // load of a __strong object.
1412193326Sed    llvm::Value *LvalueDst = Dst.getAddress();
1413193326Sed    llvm::Value *src = Src.getScalarVal();
1414198092Srdivacky    if (Dst.isObjCIvar()) {
1415198092Srdivacky      assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
1416226633Sdim      llvm::Type *ResultType = ConvertType(getContext().LongTy);
1417198092Srdivacky      llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
1418198092Srdivacky      llvm::Value *dst = RHS;
1419198092Srdivacky      RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
1420263508Sdim      llvm::Value *LHS =
1421198092Srdivacky        Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
1422198092Srdivacky      llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
1423198092Srdivacky      CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
1424198092Srdivacky                                              BytesBetween);
1425212904Sdim    } else if (Dst.isGlobalObjCRef()) {
1426212904Sdim      CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
1427212904Sdim                                                Dst.isThreadLocalRef());
1428212904Sdim    }
1429193326Sed    else
1430193326Sed      CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
1431193326Sed    return;
1432193326Sed  }
1433198092Srdivacky
1434193326Sed  assert(Src.isScalar() && "Can't emit an agg store with this method");
1435234353Sdim  EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
1436193326Sed}
1437193326Sed
1438193326Sedvoid CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1439193326Sed                                                     llvm::Value **Result) {
1440206275Srdivacky  const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
1441226633Sdim  llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
1442249423Sdim  llvm::Value *Ptr = Dst.getBitFieldAddr();
1443193326Sed
1444207619Srdivacky  // Get the source value, truncated to the width of the bit-field.
1445193326Sed  llvm::Value *SrcVal = Src.getScalarVal();
1446193326Sed
1447249423Sdim  // Cast the source to the storage type and shift it into place.
1448249423Sdim  SrcVal = Builder.CreateIntCast(SrcVal,
1449249423Sdim                                 Ptr->getType()->getPointerElementType(),
1450249423Sdim                                 /*IsSigned=*/false);
1451249423Sdim  llvm::Value *MaskedVal = SrcVal;
1452207619Srdivacky
1453249423Sdim  // See if there are other bits in the bitfield's storage we'll need to load
1454249423Sdim  // and mask together with source before storing.
1455249423Sdim  if (Info.StorageSize != Info.Size) {
1456249423Sdim    assert(Info.StorageSize > Info.Size && "Invalid bitfield size.");
1457249423Sdim    llvm::Value *Val = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
1458249423Sdim                                          "bf.load");
1459249423Sdim    cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
1460207619Srdivacky
1461249423Sdim    // Mask the source value as needed.
1462249423Sdim    if (!hasBooleanRepresentation(Dst.getType()))
1463249423Sdim      SrcVal = Builder.CreateAnd(SrcVal,
1464249423Sdim                                 llvm::APInt::getLowBitsSet(Info.StorageSize,
1465249423Sdim                                                            Info.Size),
1466249423Sdim                                 "bf.value");
1467249423Sdim    MaskedVal = SrcVal;
1468249423Sdim    if (Info.Offset)
1469249423Sdim      SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl");
1470193326Sed
1471249423Sdim    // Mask out the original value.
1472249423Sdim    Val = Builder.CreateAnd(Val,
1473249423Sdim                            ~llvm::APInt::getBitsSet(Info.StorageSize,
1474249423Sdim                                                     Info.Offset,
1475249423Sdim                                                     Info.Offset + Info.Size),
1476249423Sdim                            "bf.clear");
1477193326Sed
1478249423Sdim    // Or together the unchanged values and the source value.
1479249423Sdim    SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
1480249423Sdim  } else {
1481249423Sdim    assert(Info.Offset == 0);
1482193326Sed  }
1483193326Sed
1484249423Sdim  // Write the new value back out.
1485249423Sdim  llvm::StoreInst *Store = Builder.CreateStore(SrcVal, Ptr,
1486249423Sdim                                               Dst.isVolatileQualified());
1487249423Sdim  Store->setAlignment(Info.StorageAlignment);
1488193326Sed
1489249423Sdim  // Return the new value of the bit-field, if requested.
1490249423Sdim  if (Result) {
1491249423Sdim    llvm::Value *ResultVal = MaskedVal;
1492198092Srdivacky
1493249423Sdim    // Sign extend the value if needed.
1494249423Sdim    if (Info.IsSigned) {
1495249423Sdim      assert(Info.Size <= Info.StorageSize);
1496249423Sdim      unsigned HighBits = Info.StorageSize - Info.Size;
1497249423Sdim      if (HighBits) {
1498249423Sdim        ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
1499249423Sdim        ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
1500249423Sdim      }
1501207619Srdivacky    }
1502193326Sed
1503249423Sdim    ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
1504249423Sdim                                      "bf.result.cast");
1505249423Sdim    *Result = EmitFromMemory(ResultVal, Dst.getType());
1506193326Sed  }
1507193326Sed}
1508193326Sed
1509193326Sedvoid CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
1510224145Sdim                                                               LValue Dst) {
1511193326Sed  // This access turns into a read/modify/write of the vector.  Load the input
1512193326Sed  // value now.
1513234353Sdim  llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(),
1514234353Sdim                                            Dst.isVolatileQualified());
1515234353Sdim  Load->setAlignment(Dst.getAlignment().getQuantity());
1516234353Sdim  llvm::Value *Vec = Load;
1517193326Sed  const llvm::Constant *Elts = Dst.getExtVectorElts();
1518198092Srdivacky
1519193326Sed  llvm::Value *SrcVal = Src.getScalarVal();
1520198092Srdivacky
1521224145Sdim  if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
1522193326Sed    unsigned NumSrcElts = VTy->getNumElements();
1523193326Sed    unsigned NumDstElts =
1524193326Sed       cast<llvm::VectorType>(Vec->getType())->getNumElements();
1525193326Sed    if (NumDstElts == NumSrcElts) {
1526198092Srdivacky      // Use shuffle vector is the src and destination are the same number of
1527198092Srdivacky      // elements and restore the vector mask since it is on the side it will be
1528198092Srdivacky      // stored.
1529226633Sdim      SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
1530234353Sdim      for (unsigned i = 0; i != NumSrcElts; ++i)
1531234353Sdim        Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i);
1532198092Srdivacky
1533218893Sdim      llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1534193326Sed      Vec = Builder.CreateShuffleVector(SrcVal,
1535193326Sed                                        llvm::UndefValue::get(Vec->getType()),
1536226633Sdim                                        MaskV);
1537198092Srdivacky    } else if (NumDstElts > NumSrcElts) {
1538193326Sed      // Extended the source vector to the same length and then shuffle it
1539193326Sed      // into the destination.
1540193326Sed      // FIXME: since we're shuffling with undef, can we just use the indices
1541193326Sed      //        into that?  This could be simpler.
1542226633Sdim      SmallVector<llvm::Constant*, 4> ExtMask;
1543234353Sdim      for (unsigned i = 0; i != NumSrcElts; ++i)
1544234353Sdim        ExtMask.push_back(Builder.getInt32(i));
1545234353Sdim      ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty));
1546218893Sdim      llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1547198092Srdivacky      llvm::Value *ExtSrcVal =
1548193326Sed        Builder.CreateShuffleVector(SrcVal,
1549193326Sed                                    llvm::UndefValue::get(SrcVal->getType()),
1550226633Sdim                                    ExtMaskV);
1551193326Sed      // build identity
1552226633Sdim      SmallVector<llvm::Constant*, 4> Mask;
1553198893Srdivacky      for (unsigned i = 0; i != NumDstElts; ++i)
1554234353Sdim        Mask.push_back(Builder.getInt32(i));
1555198893Srdivacky
1556263508Sdim      // When the vector size is odd and .odd or .hi is used, the last element
1557263508Sdim      // of the Elts constant array will be one past the size of the vector.
1558263508Sdim      // Ignore the last element here, if it is greater than the mask size.
1559263508Sdim      if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
1560263508Sdim        NumSrcElts--;
1561263508Sdim
1562193326Sed      // modify when what gets shuffled in
1563234353Sdim      for (unsigned i = 0; i != NumSrcElts; ++i)
1564234353Sdim        Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts);
1565218893Sdim      llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1566226633Sdim      Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
1567198092Srdivacky    } else {
1568193326Sed      // We should never shorten the vector
1569226633Sdim      llvm_unreachable("unexpected shorten vector length");
1570193326Sed    }
1571193326Sed  } else {
1572193326Sed    // If the Src is a scalar (not a vector) it must be updating one element.
1573193326Sed    unsigned InIdx = getAccessedFieldNo(0, Elts);
1574198893Srdivacky    llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
1575226633Sdim    Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
1576193326Sed  }
1577198092Srdivacky
1578234353Sdim  llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(),
1579234353Sdim                                               Dst.isVolatileQualified());
1580234353Sdim  Store->setAlignment(Dst.getAlignment().getQuantity());
1581193326Sed}
1582193326Sed
1583198092Srdivacky// setObjCGCLValueClass - sets class of he lvalue for the purpose of
1584198092Srdivacky// generating write-barries API. It is currently a global, ivar,
1585198092Srdivacky// or neither.
1586198893Srdivackystatic void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
1587226633Sdim                                 LValue &LV,
1588226633Sdim                                 bool IsMemberAccess=false) {
1589234353Sdim  if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
1590198092Srdivacky    return;
1591263508Sdim
1592198092Srdivacky  if (isa<ObjCIvarRefExpr>(E)) {
1593226633Sdim    QualType ExpTy = E->getType();
1594226633Sdim    if (IsMemberAccess && ExpTy->isPointerType()) {
1595226633Sdim      // If ivar is a structure pointer, assigning to field of
1596263508Sdim      // this struct follows gcc's behavior and makes it a non-ivar
1597226633Sdim      // writer-barrier conservatively.
1598226633Sdim      ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1599226633Sdim      if (ExpTy->isRecordType()) {
1600226633Sdim        LV.setObjCIvar(false);
1601226633Sdim        return;
1602226633Sdim      }
1603226633Sdim    }
1604212904Sdim    LV.setObjCIvar(true);
1605198092Srdivacky    ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
1606198092Srdivacky    LV.setBaseIvarExp(Exp->getBase());
1607212904Sdim    LV.setObjCArray(E->getType()->isArrayType());
1608198092Srdivacky    return;
1609198092Srdivacky  }
1610263508Sdim
1611198092Srdivacky  if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
1612198092Srdivacky    if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1613218893Sdim      if (VD->hasGlobalStorage()) {
1614212904Sdim        LV.setGlobalObjCRef(true);
1615251662Sdim        LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
1616212904Sdim      }
1617198092Srdivacky    }
1618212904Sdim    LV.setObjCArray(E->getType()->isArrayType());
1619198893Srdivacky    return;
1620198092Srdivacky  }
1621263508Sdim
1622198893Srdivacky  if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
1623226633Sdim    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1624198893Srdivacky    return;
1625198893Srdivacky  }
1626263508Sdim
1627198893Srdivacky  if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
1628226633Sdim    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1629198092Srdivacky    if (LV.isObjCIvar()) {
1630198092Srdivacky      // If cast is to a structure pointer, follow gcc's behavior and make it
1631198092Srdivacky      // a non-ivar write-barrier.
1632198092Srdivacky      QualType ExpTy = E->getType();
1633198092Srdivacky      if (ExpTy->isPointerType())
1634198092Srdivacky        ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
1635198092Srdivacky      if (ExpTy->isRecordType())
1636263508Sdim        LV.setObjCIvar(false);
1637198893Srdivacky    }
1638198893Srdivacky    return;
1639198092Srdivacky  }
1640221345Sdim
1641221345Sdim  if (const GenericSelectionExpr *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1642221345Sdim    setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
1643221345Sdim    return;
1644221345Sdim  }
1645221345Sdim
1646198893Srdivacky  if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1647226633Sdim    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1648198893Srdivacky    return;
1649198893Srdivacky  }
1650263508Sdim
1651198893Srdivacky  if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
1652226633Sdim    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1653198893Srdivacky    return;
1654198893Srdivacky  }
1655224145Sdim
1656224145Sdim  if (const ObjCBridgedCastExpr *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1657226633Sdim    setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
1658224145Sdim    return;
1659224145Sdim  }
1660224145Sdim
1661198893Srdivacky  if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1662198092Srdivacky    setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
1663263508Sdim    if (LV.isObjCIvar() && !LV.isObjCArray())
1664263508Sdim      // Using array syntax to assigning to what an ivar points to is not
1665198092Srdivacky      // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1666263508Sdim      LV.setObjCIvar(false);
1667198092Srdivacky    else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1668263508Sdim      // Using array syntax to assigning to what global points to is not
1669198092Srdivacky      // same as assigning to the global itself. {id *G;} G[i] = 0;
1670212904Sdim      LV.setGlobalObjCRef(false);
1671198893Srdivacky    return;
1672198092Srdivacky  }
1673226633Sdim
1674198893Srdivacky  if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
1675226633Sdim    setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
1676198092Srdivacky    // We don't know if member is an 'ivar', but this flag is looked at
1677198092Srdivacky    // only in the context of LV.isObjCIvar().
1678212904Sdim    LV.setObjCArray(E->getType()->isArrayType());
1679198893Srdivacky    return;
1680198092Srdivacky  }
1681198092Srdivacky}
1682198092Srdivacky
1683224145Sdimstatic llvm::Value *
1684224145SdimEmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
1685224145Sdim                                llvm::Value *V, llvm::Type *IRType,
1686226633Sdim                                StringRef Name = StringRef()) {
1687224145Sdim  unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1688224145Sdim  return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
1689224145Sdim}
1690224145Sdim
1691199482Srdivackystatic LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1692199482Srdivacky                                      const Expr *E, const VarDecl *VD) {
1693199482Srdivacky  llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1694234353Sdim  llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
1695234353Sdim  V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
1696234353Sdim  CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
1697234353Sdim  QualType T = E->getType();
1698234353Sdim  LValue LV;
1699234353Sdim  if (VD->getType()->isReferenceType()) {
1700234353Sdim    llvm::LoadInst *LI = CGF.Builder.CreateLoad(V);
1701234353Sdim    LI->setAlignment(Alignment.getQuantity());
1702234353Sdim    V = LI;
1703234353Sdim    LV = CGF.MakeNaturalAlignAddrLValue(V, T);
1704234353Sdim  } else {
1705234353Sdim    LV = CGF.MakeAddrLValue(V, E->getType(), Alignment);
1706234353Sdim  }
1707199482Srdivacky  setObjCGCLValueClass(CGF.getContext(), E, LV);
1708199482Srdivacky  return LV;
1709199482Srdivacky}
1710199482Srdivacky
1711199990Srdivackystatic LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1712224145Sdim                                     const Expr *E, const FunctionDecl *FD) {
1713218893Sdim  llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
1714199990Srdivacky  if (!FD->hasPrototype()) {
1715199990Srdivacky    if (const FunctionProtoType *Proto =
1716199990Srdivacky            FD->getType()->getAs<FunctionProtoType>()) {
1717199990Srdivacky      // Ugly case: for a K&R-style definition, the type of the definition
1718199990Srdivacky      // isn't the same as the type of a use.  Correct for this with a
1719199990Srdivacky      // bitcast.
1720199990Srdivacky      QualType NoProtoType =
1721199990Srdivacky          CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1722199990Srdivacky      NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1723226633Sdim      V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType));
1724199990Srdivacky    }
1725199990Srdivacky  }
1726234353Sdim  CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
1727212904Sdim  return CGF.MakeAddrLValue(V, E->getType(), Alignment);
1728199990Srdivacky}
1729199990Srdivacky
1730263508Sdimstatic LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD,
1731263508Sdim                                      llvm::Value *ThisValue) {
1732263508Sdim  QualType TagType = CGF.getContext().getTagDeclType(FD->getParent());
1733263508Sdim  LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType);
1734263508Sdim  return CGF.EmitLValueForField(LV, FD);
1735263508Sdim}
1736263508Sdim
1737193326SedLValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
1738199482Srdivacky  const NamedDecl *ND = E->getDecl();
1739234353Sdim  CharUnits Alignment = getContext().getDeclAlign(ND);
1740234353Sdim  QualType T = E->getType();
1741198092Srdivacky
1742243830Sdim  // A DeclRefExpr for a reference initialized by a constant expression can
1743243830Sdim  // appear without being odr-used. Directly emit the constant initializer.
1744243830Sdim  if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1745243830Sdim    const Expr *Init = VD->getAnyInitializer(VD);
1746243830Sdim    if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() &&
1747243830Sdim        VD->isUsableInConstantExpressions(getContext()) &&
1748243830Sdim        VD->checkInitIsICE()) {
1749243830Sdim      llvm::Constant *Val =
1750243830Sdim        CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this);
1751243830Sdim      assert(Val && "failed to emit reference constant expression");
1752243830Sdim      // FIXME: Eventually we will want to emit vector element references.
1753243830Sdim      return MakeAddrLValue(Val, T, Alignment);
1754243830Sdim    }
1755243830Sdim  }
1756243830Sdim
1757234353Sdim  // FIXME: We should be able to assert this for FunctionDecls as well!
1758234353Sdim  // FIXME: We should be able to assert this for all DeclRefExprs, not just
1759234353Sdim  // those with a valid source location.
1760234353Sdim  assert((ND->isUsed(false) || !isa<VarDecl>(ND) ||
1761234353Sdim          !E->getLocation().isValid()) &&
1762234353Sdim         "Should not use decl without marking it used!");
1763234353Sdim
1764204793Srdivacky  if (ND->hasAttr<WeakRefAttr>()) {
1765218893Sdim    const ValueDecl *VD = cast<ValueDecl>(ND);
1766204793Srdivacky    llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
1767243830Sdim    return MakeAddrLValue(Aliasee, T, Alignment);
1768204793Srdivacky  }
1769204793Srdivacky
1770199482Srdivacky  if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1771199482Srdivacky    // Check if this is a global variable.
1772251662Sdim    if (VD->hasLinkage() || VD->isStaticDataMember()) {
1773251662Sdim      // If it's thread_local, emit a call to its wrapper function instead.
1774251662Sdim      if (VD->getTLSKind() == VarDecl::TLS_Dynamic)
1775251662Sdim        return CGM.getCXXABI().EmitThreadLocalDeclRefExpr(*this, E);
1776199482Srdivacky      return EmitGlobalVarDeclLValue(*this, E, VD);
1777251662Sdim    }
1778198092Srdivacky
1779234353Sdim    bool isBlockVariable = VD->hasAttr<BlocksAttr>();
1780234353Sdim
1781249423Sdim    llvm::Value *V = LocalDeclMap.lookup(VD);
1782263508Sdim    if (!V && VD->isStaticLocal())
1783207619Srdivacky      V = CGM.getStaticLocalDeclAddress(VD);
1784234353Sdim
1785234353Sdim    // Use special handling for lambdas.
1786234353Sdim    if (!V) {
1787234982Sdim      if (FieldDecl *FD = LambdaCaptureFields.lookup(VD)) {
1788263508Sdim        return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
1789263508Sdim      } else if (CapturedStmtInfo) {
1790263508Sdim        if (const FieldDecl *FD = CapturedStmtInfo->lookup(VD))
1791263508Sdim          return EmitCapturedFieldLValue(*this, FD,
1792263508Sdim                                         CapturedStmtInfo->getContextValue());
1793234982Sdim      }
1794234353Sdim
1795234353Sdim      assert(isa<BlockDecl>(CurCodeDecl) && E->refersToEnclosingLocal());
1796234353Sdim      return MakeAddrLValue(GetAddrOfBlockDecl(VD, isBlockVariable),
1797243830Sdim                            T, Alignment);
1798234353Sdim    }
1799234353Sdim
1800199482Srdivacky    assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1801199482Srdivacky
1802234353Sdim    if (isBlockVariable)
1803218893Sdim      V = BuildBlockByrefAddress(V, VD);
1804212904Sdim
1805234353Sdim    LValue LV;
1806234353Sdim    if (VD->getType()->isReferenceType()) {
1807234353Sdim      llvm::LoadInst *LI = Builder.CreateLoad(V);
1808234353Sdim      LI->setAlignment(Alignment.getQuantity());
1809234353Sdim      V = LI;
1810234353Sdim      LV = MakeNaturalAlignAddrLValue(V, T);
1811234353Sdim    } else {
1812234353Sdim      LV = MakeAddrLValue(V, T, Alignment);
1813234353Sdim    }
1814224145Sdim
1815249423Sdim    bool isLocalStorage = VD->hasLocalStorage();
1816249423Sdim
1817249423Sdim    bool NonGCable = isLocalStorage &&
1818249423Sdim                     !VD->getType()->isReferenceType() &&
1819249423Sdim                     !isBlockVariable;
1820212904Sdim    if (NonGCable) {
1821212904Sdim      LV.getQuals().removeObjCGCAttr();
1822212904Sdim      LV.setNonGC(true);
1823212904Sdim    }
1824249423Sdim
1825249423Sdim    bool isImpreciseLifetime =
1826249423Sdim      (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
1827249423Sdim    if (isImpreciseLifetime)
1828249423Sdim      LV.setARCPreciseLifetime(ARCImpreciseLifetime);
1829198092Srdivacky    setObjCGCLValueClass(getContext(), E, LV);
1830193326Sed    return LV;
1831198893Srdivacky  }
1832218893Sdim
1833263508Sdim  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1834263508Sdim    return EmitFunctionDeclLValue(*this, E, FD);
1835218893Sdim
1836226633Sdim  llvm_unreachable("Unhandled DeclRefExpr");
1837193326Sed}
1838193326Sed
1839193326SedLValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1840193326Sed  // __extension__ doesn't affect lvalue-ness.
1841212904Sdim  if (E->getOpcode() == UO_Extension)
1842193326Sed    return EmitLValue(E->getSubExpr());
1843198092Srdivacky
1844193326Sed  QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
1845193326Sed  switch (E->getOpcode()) {
1846226633Sdim  default: llvm_unreachable("Unknown unary operator lvalue!");
1847212904Sdim  case UO_Deref: {
1848198893Srdivacky    QualType T = E->getSubExpr()->getType()->getPointeeType();
1849198893Srdivacky    assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
1850198092Srdivacky
1851234353Sdim    LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
1852212904Sdim    LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
1853198092Srdivacky
1854198893Srdivacky    // We should not generate __weak write barrier on indirect reference
1855198893Srdivacky    // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1856198893Srdivacky    // But, we continue to generate __strong write barrier on indirect write
1857198893Srdivacky    // into a pointer to object.
1858243830Sdim    if (getLangOpts().ObjC1 &&
1859243830Sdim        getLangOpts().getGC() != LangOptions::NonGC &&
1860198893Srdivacky        LV.isObjCWeak())
1861212904Sdim      LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
1862198893Srdivacky    return LV;
1863198893Srdivacky  }
1864212904Sdim  case UO_Real:
1865212904Sdim  case UO_Imag: {
1866193326Sed    LValue LV = EmitLValue(E->getSubExpr());
1867218893Sdim    assert(LV.isSimple() && "real/imag on non-ordinary l-value");
1868218893Sdim    llvm::Value *Addr = LV.getAddress();
1869218893Sdim
1870234353Sdim    // __real is valid on scalars.  This is a faster way of testing that.
1871234353Sdim    // __imag can only produce an rvalue on scalars.
1872234353Sdim    if (E->getOpcode() == UO_Real &&
1873234353Sdim        !cast<llvm::PointerType>(Addr->getType())
1874218893Sdim           ->getElementType()->isStructTy()) {
1875218893Sdim      assert(E->getSubExpr()->getType()->isArithmeticType());
1876218893Sdim      return LV;
1877218893Sdim    }
1878218893Sdim
1879218893Sdim    assert(E->getSubExpr()->getType()->isAnyComplexType());
1880218893Sdim
1881212904Sdim    unsigned Idx = E->getOpcode() == UO_Imag;
1882212904Sdim    return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
1883218893Sdim                                                  Idx, "idx"),
1884212904Sdim                          ExprTy);
1885193326Sed  }
1886212904Sdim  case UO_PreInc:
1887212904Sdim  case UO_PreDec: {
1888202379Srdivacky    LValue LV = EmitLValue(E->getSubExpr());
1889212904Sdim    bool isInc = E->getOpcode() == UO_PreInc;
1890263508Sdim
1891202379Srdivacky    if (E->getType()->isAnyComplexType())
1892202379Srdivacky      EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1893202379Srdivacky    else
1894202379Srdivacky      EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1895202379Srdivacky    return LV;
1896199482Srdivacky  }
1897202379Srdivacky  }
1898193326Sed}
1899193326Sed
1900193326SedLValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
1901212904Sdim  return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
1902212904Sdim                        E->getType());
1903193326Sed}
1904193326Sed
1905193326SedLValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
1906212904Sdim  return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1907212904Sdim                        E->getType());
1908193326Sed}
1909193326Sed
1910239462Sdimstatic llvm::Constant*
1911239462SdimGetAddrOfConstantWideString(StringRef Str,
1912239462Sdim                            const char *GlobalName,
1913239462Sdim                            ASTContext &Context,
1914239462Sdim                            QualType Ty, SourceLocation Loc,
1915239462Sdim                            CodeGenModule &CGM) {
1916193326Sed
1917239462Sdim  StringLiteral *SL = StringLiteral::Create(Context,
1918239462Sdim                                            Str,
1919239462Sdim                                            StringLiteral::Wide,
1920239462Sdim                                            /*Pascal = */false,
1921239462Sdim                                            Ty, Loc);
1922239462Sdim  llvm::Constant *C = CGM.GetConstantArrayFromStringLiteral(SL);
1923239462Sdim  llvm::GlobalVariable *GV =
1924239462Sdim    new llvm::GlobalVariable(CGM.getModule(), C->getType(),
1925239462Sdim                             !CGM.getLangOpts().WritableStrings,
1926239462Sdim                             llvm::GlobalValue::PrivateLinkage,
1927239462Sdim                             C, GlobalName);
1928239462Sdim  const unsigned WideAlignment =
1929239462Sdim    Context.getTypeAlignInChars(Ty).getQuantity();
1930239462Sdim  GV->setAlignment(WideAlignment);
1931239462Sdim  return GV;
1932239462Sdim}
1933239462Sdim
1934239462Sdimstatic void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source,
1935239462Sdim                                    SmallString<32>& Target) {
1936239462Sdim  Target.resize(CharByteWidth * (Source.size() + 1));
1937243830Sdim  char *ResultPtr = &Target[0];
1938243830Sdim  const UTF8 *ErrorPtr;
1939243830Sdim  bool success = ConvertUTF8toWide(CharByteWidth, Source, ResultPtr, ErrorPtr);
1940239462Sdim  (void)success;
1941239462Sdim  assert(success);
1942239462Sdim  Target.resize(ResultPtr - &Target[0]);
1943239462Sdim}
1944239462Sdim
1945212904SdimLValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
1946212904Sdim  switch (E->getIdentType()) {
1947212904Sdim  default:
1948212904Sdim    return EmitUnsupportedLValue(E, "predefined expression");
1949193326Sed
1950193326Sed  case PredefinedExpr::Func:
1951193326Sed  case PredefinedExpr::Function:
1952239462Sdim  case PredefinedExpr::LFunction:
1953263508Sdim  case PredefinedExpr::FuncDName:
1954212904Sdim  case PredefinedExpr::PrettyFunction: {
1955263508Sdim    PredefinedExpr::IdentType IdentType = E->getIdentType();
1956212904Sdim    std::string GlobalVarName;
1957193326Sed
1958239462Sdim    switch (IdentType) {
1959226633Sdim    default: llvm_unreachable("Invalid type");
1960212904Sdim    case PredefinedExpr::Func:
1961212904Sdim      GlobalVarName = "__func__.";
1962212904Sdim      break;
1963212904Sdim    case PredefinedExpr::Function:
1964212904Sdim      GlobalVarName = "__FUNCTION__.";
1965212904Sdim      break;
1966263508Sdim    case PredefinedExpr::FuncDName:
1967263508Sdim      GlobalVarName = "__FUNCDNAME__.";
1968263508Sdim      break;
1969239462Sdim    case PredefinedExpr::LFunction:
1970239462Sdim      GlobalVarName = "L__FUNCTION__.";
1971239462Sdim      break;
1972212904Sdim    case PredefinedExpr::PrettyFunction:
1973212904Sdim      GlobalVarName = "__PRETTY_FUNCTION__.";
1974212904Sdim      break;
1975212904Sdim    }
1976193326Sed
1977226633Sdim    StringRef FnName = CurFn->getName();
1978212904Sdim    if (FnName.startswith("\01"))
1979212904Sdim      FnName = FnName.substr(1);
1980212904Sdim    GlobalVarName += FnName;
1981198092Srdivacky
1982263508Sdim    // If this is outside of a function use the top level decl.
1983212904Sdim    const Decl *CurDecl = CurCodeDecl;
1984263508Sdim    if (CurDecl == 0 || isa<VarDecl>(CurDecl))
1985212904Sdim      CurDecl = getContext().getTranslationUnitDecl();
1986193326Sed
1987263508Sdim    const Type *ElemType = E->getType()->getArrayElementTypeNoTypeQual();
1988263508Sdim    std::string FunctionName;
1989263508Sdim    if (isa<BlockDecl>(CurDecl)) {
1990263508Sdim      // Blocks use the mangled function name.
1991263508Sdim      // FIXME: ComputeName should handle blocks.
1992263508Sdim      FunctionName = FnName.str();
1993263508Sdim    } else if (isa<CapturedDecl>(CurDecl)) {
1994263508Sdim      // For a captured statement, the function name is its enclosing
1995263508Sdim      // function name not the one compiler generated.
1996263508Sdim      FunctionName = PredefinedExpr::ComputeName(IdentType, CurDecl);
1997263508Sdim    } else {
1998263508Sdim      FunctionName = PredefinedExpr::ComputeName(IdentType, CurDecl);
1999263508Sdim      assert(cast<ConstantArrayType>(E->getType())->getSize() - 1 ==
2000263508Sdim                 FunctionName.size() &&
2001263508Sdim             "Computed __func__ length differs from type!");
2002263508Sdim    }
2003212904Sdim
2004239462Sdim    llvm::Constant *C;
2005239462Sdim    if (ElemType->isWideCharType()) {
2006239462Sdim      SmallString<32> RawChars;
2007239462Sdim      ConvertUTF8ToWideString(
2008239462Sdim          getContext().getTypeSizeInChars(ElemType).getQuantity(),
2009239462Sdim          FunctionName, RawChars);
2010239462Sdim      C = GetAddrOfConstantWideString(RawChars,
2011239462Sdim                                      GlobalVarName.c_str(),
2012239462Sdim                                      getContext(),
2013239462Sdim                                      E->getType(),
2014239462Sdim                                      E->getLocation(),
2015239462Sdim                                      CGM);
2016239462Sdim    } else {
2017239462Sdim      C = CGM.GetAddrOfConstantCString(FunctionName,
2018239462Sdim                                       GlobalVarName.c_str(),
2019239462Sdim                                       1);
2020239462Sdim    }
2021212904Sdim    return MakeAddrLValue(C, E->getType());
2022193326Sed  }
2023212904Sdim  }
2024193326Sed}
2025193326Sed
2026243830Sdim/// Emit a type description suitable for use by a runtime sanitizer library. The
2027243830Sdim/// format of a type descriptor is
2028243830Sdim///
2029243830Sdim/// \code
2030243830Sdim///   { i16 TypeKind, i16 TypeInfo }
2031243830Sdim/// \endcode
2032243830Sdim///
2033243830Sdim/// followed by an array of i8 containing the type name. TypeKind is 0 for an
2034243830Sdim/// integer, 1 for a floating point value, and -1 for anything else.
2035243830Sdimllvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
2036263508Sdim  // Only emit each type's descriptor once.
2037263508Sdim  if (llvm::Constant *C = CGM.getTypeDescriptor(T))
2038263508Sdim    return C;
2039263508Sdim
2040243830Sdim  uint16_t TypeKind = -1;
2041243830Sdim  uint16_t TypeInfo = 0;
2042200583Srdivacky
2043243830Sdim  if (T->isIntegerType()) {
2044243830Sdim    TypeKind = 0;
2045243830Sdim    TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
2046249423Sdim               (T->isSignedIntegerType() ? 1 : 0);
2047243830Sdim  } else if (T->isFloatingType()) {
2048243830Sdim    TypeKind = 1;
2049243830Sdim    TypeInfo = getContext().getTypeSize(T);
2050243830Sdim  }
2051200583Srdivacky
2052243830Sdim  // Format the type name as if for a diagnostic, including quotes and
2053243830Sdim  // optionally an 'aka'.
2054249423Sdim  SmallString<32> Buffer;
2055243830Sdim  CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
2056243830Sdim                                    (intptr_t)T.getAsOpaquePtr(),
2057243830Sdim                                    0, 0, 0, 0, 0, 0, Buffer,
2058243830Sdim                                    ArrayRef<intptr_t>());
2059243830Sdim
2060243830Sdim  llvm::Constant *Components[] = {
2061243830Sdim    Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
2062243830Sdim    llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
2063243830Sdim  };
2064243830Sdim  llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
2065243830Sdim
2066243830Sdim  llvm::GlobalVariable *GV =
2067243830Sdim    new llvm::GlobalVariable(CGM.getModule(), Descriptor->getType(),
2068243830Sdim                             /*isConstant=*/true,
2069243830Sdim                             llvm::GlobalVariable::PrivateLinkage,
2070243830Sdim                             Descriptor);
2071243830Sdim  GV->setUnnamedAddr(true);
2072263508Sdim
2073263508Sdim  // Remember the descriptor for this type.
2074263508Sdim  CGM.setTypeDescriptor(T, GV);
2075263508Sdim
2076243830Sdim  return GV;
2077243830Sdim}
2078243830Sdim
2079243830Sdimllvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
2080243830Sdim  llvm::Type *TargetTy = IntPtrTy;
2081243830Sdim
2082249423Sdim  // Floating-point types which fit into intptr_t are bitcast to integers
2083249423Sdim  // and then passed directly (after zero-extension, if necessary).
2084249423Sdim  if (V->getType()->isFloatingPointTy()) {
2085249423Sdim    unsigned Bits = V->getType()->getPrimitiveSizeInBits();
2086249423Sdim    if (Bits <= TargetTy->getIntegerBitWidth())
2087249423Sdim      V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
2088249423Sdim                                                         Bits));
2089249423Sdim  }
2090249423Sdim
2091243830Sdim  // Integers which fit in intptr_t are zero-extended and passed directly.
2092243830Sdim  if (V->getType()->isIntegerTy() &&
2093243830Sdim      V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
2094243830Sdim    return Builder.CreateZExt(V, TargetTy);
2095243830Sdim
2096243830Sdim  // Pointers are passed directly, everything else is passed by address.
2097243830Sdim  if (!V->getType()->isPointerTy()) {
2098249423Sdim    llvm::Value *Ptr = CreateTempAlloca(V->getType());
2099243830Sdim    Builder.CreateStore(V, Ptr);
2100243830Sdim    V = Ptr;
2101200583Srdivacky  }
2102243830Sdim  return Builder.CreatePtrToInt(V, TargetTy);
2103243830Sdim}
2104200583Srdivacky
2105243830Sdim/// \brief Emit a representation of a SourceLocation for passing to a handler
2106243830Sdim/// in a sanitizer runtime library. The format for this data is:
2107243830Sdim/// \code
2108243830Sdim///   struct SourceLocation {
2109243830Sdim///     const char *Filename;
2110243830Sdim///     int32_t Line, Column;
2111243830Sdim///   };
2112243830Sdim/// \endcode
2113243830Sdim/// For an invalid SourceLocation, the Filename pointer is null.
2114243830Sdimllvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) {
2115243830Sdim  PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
2116200583Srdivacky
2117243830Sdim  llvm::Constant *Data[] = {
2118263508Sdim    PLoc.isValid() ? CGM.GetAddrOfConstantCString(PLoc.getFilename(), ".src")
2119243830Sdim                   : llvm::Constant::getNullValue(Int8PtrTy),
2120263508Sdim    Builder.getInt32(PLoc.isValid() ? PLoc.getLine() : 0),
2121263508Sdim    Builder.getInt32(PLoc.isValid() ? PLoc.getColumn() : 0)
2122243830Sdim  };
2123243830Sdim
2124243830Sdim  return llvm::ConstantStruct::getAnon(Data);
2125200583Srdivacky}
2126200583Srdivacky
2127243830Sdimvoid CodeGenFunction::EmitCheck(llvm::Value *Checked, StringRef CheckName,
2128249423Sdim                                ArrayRef<llvm::Constant *> StaticArgs,
2129249423Sdim                                ArrayRef<llvm::Value *> DynamicArgs,
2130249423Sdim                                CheckRecoverableKind RecoverKind) {
2131249423Sdim  assert(SanOpts != &SanitizerOptions::Disabled);
2132249423Sdim
2133249423Sdim  if (CGM.getCodeGenOpts().SanitizeUndefinedTrapOnError) {
2134249423Sdim    assert (RecoverKind != CRK_AlwaysRecoverable &&
2135249423Sdim            "Runtime call required for AlwaysRecoverable kind!");
2136249423Sdim    return EmitTrapCheck(Checked);
2137249423Sdim  }
2138249423Sdim
2139243830Sdim  llvm::BasicBlock *Cont = createBasicBlock("cont");
2140243830Sdim
2141243830Sdim  llvm::BasicBlock *Handler = createBasicBlock("handler." + CheckName);
2142249423Sdim
2143249423Sdim  llvm::Instruction *Branch = Builder.CreateCondBr(Checked, Cont, Handler);
2144249423Sdim
2145249423Sdim  // Give hint that we very much don't expect to execute the handler
2146249423Sdim  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
2147249423Sdim  llvm::MDBuilder MDHelper(getLLVMContext());
2148249423Sdim  llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
2149249423Sdim  Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
2150249423Sdim
2151243830Sdim  EmitBlock(Handler);
2152243830Sdim
2153243830Sdim  llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
2154243830Sdim  llvm::GlobalValue *InfoPtr =
2155249423Sdim      new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
2156243830Sdim                               llvm::GlobalVariable::PrivateLinkage, Info);
2157243830Sdim  InfoPtr->setUnnamedAddr(true);
2158243830Sdim
2159249423Sdim  SmallVector<llvm::Value *, 4> Args;
2160249423Sdim  SmallVector<llvm::Type *, 4> ArgTypes;
2161243830Sdim  Args.reserve(DynamicArgs.size() + 1);
2162243830Sdim  ArgTypes.reserve(DynamicArgs.size() + 1);
2163243830Sdim
2164243830Sdim  // Handler functions take an i8* pointing to the (handler-specific) static
2165243830Sdim  // information block, followed by a sequence of intptr_t arguments
2166243830Sdim  // representing operand values.
2167243830Sdim  Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
2168243830Sdim  ArgTypes.push_back(Int8PtrTy);
2169243830Sdim  for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
2170243830Sdim    Args.push_back(EmitCheckValue(DynamicArgs[i]));
2171243830Sdim    ArgTypes.push_back(IntPtrTy);
2172243830Sdim  }
2173243830Sdim
2174249423Sdim  bool Recover = (RecoverKind == CRK_AlwaysRecoverable) ||
2175249423Sdim                 ((RecoverKind == CRK_Recoverable) &&
2176249423Sdim                   CGM.getCodeGenOpts().SanitizeRecover);
2177249423Sdim
2178243830Sdim  llvm::FunctionType *FnType =
2179243830Sdim    llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
2180243830Sdim  llvm::AttrBuilder B;
2181249423Sdim  if (!Recover) {
2182249423Sdim    B.addAttribute(llvm::Attribute::NoReturn)
2183249423Sdim     .addAttribute(llvm::Attribute::NoUnwind);
2184243830Sdim  }
2185249423Sdim  B.addAttribute(llvm::Attribute::UWTable);
2186249423Sdim
2187249423Sdim  // Checks that have two variants use a suffix to differentiate them
2188249423Sdim  bool NeedsAbortSuffix = (RecoverKind != CRK_Unrecoverable) &&
2189249423Sdim                           !CGM.getCodeGenOpts().SanitizeRecover;
2190249423Sdim  std::string FunctionName = ("__ubsan_handle_" + CheckName +
2191249423Sdim                              (NeedsAbortSuffix? "_abort" : "")).str();
2192249423Sdim  llvm::Value *Fn =
2193249423Sdim    CGM.CreateRuntimeFunction(FnType, FunctionName,
2194249423Sdim                              llvm::AttributeSet::get(getLLVMContext(),
2195249423Sdim                                              llvm::AttributeSet::FunctionIndex,
2196249423Sdim                                                      B));
2197249423Sdim  llvm::CallInst *HandlerCall = EmitNounwindRuntimeCall(Fn, Args);
2198249423Sdim  if (Recover) {
2199243830Sdim    Builder.CreateBr(Cont);
2200243830Sdim  } else {
2201243830Sdim    HandlerCall->setDoesNotReturn();
2202243830Sdim    Builder.CreateUnreachable();
2203243830Sdim  }
2204243830Sdim
2205243830Sdim  EmitBlock(Cont);
2206243830Sdim}
2207243830Sdim
2208249423Sdimvoid CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) {
2209243830Sdim  llvm::BasicBlock *Cont = createBasicBlock("cont");
2210243830Sdim
2211243830Sdim  // If we're optimizing, collapse all calls to trap down to just one per
2212243830Sdim  // function to save on code size.
2213243830Sdim  if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
2214243830Sdim    TrapBB = createBasicBlock("trap");
2215243830Sdim    Builder.CreateCondBr(Checked, Cont, TrapBB);
2216243830Sdim    EmitBlock(TrapBB);
2217243830Sdim    llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
2218243830Sdim    llvm::CallInst *TrapCall = Builder.CreateCall(F);
2219243830Sdim    TrapCall->setDoesNotReturn();
2220243830Sdim    TrapCall->setDoesNotThrow();
2221243830Sdim    Builder.CreateUnreachable();
2222243830Sdim  } else {
2223243830Sdim    Builder.CreateCondBr(Checked, Cont, TrapBB);
2224243830Sdim  }
2225243830Sdim
2226243830Sdim  EmitBlock(Cont);
2227243830Sdim}
2228243830Sdim
2229210299Sed/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
2230210299Sed/// array to pointer, return the array subexpression.
2231210299Sedstatic const Expr *isSimpleArrayDecayOperand(const Expr *E) {
2232210299Sed  // If this isn't just an array->pointer decay, bail out.
2233210299Sed  const CastExpr *CE = dyn_cast<CastExpr>(E);
2234212904Sdim  if (CE == 0 || CE->getCastKind() != CK_ArrayToPointerDecay)
2235210299Sed    return 0;
2236263508Sdim
2237210299Sed  // If this is a decay from variable width array, bail out.
2238210299Sed  const Expr *SubExpr = CE->getSubExpr();
2239210299Sed  if (SubExpr->getType()->isVariableArrayType())
2240210299Sed    return 0;
2241263508Sdim
2242210299Sed  return SubExpr;
2243210299Sed}
2244210299Sed
2245249423SdimLValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2246249423Sdim                                               bool Accessed) {
2247193326Sed  // The index must always be an integer, which is not an aggregate.  Emit it.
2248193326Sed  llvm::Value *Idx = EmitScalarExpr(E->getIdx());
2249193631Sed  QualType IdxTy  = E->getIdx()->getType();
2250223017Sdim  bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
2251193631Sed
2252263508Sdim  if (SanOpts->ArrayBounds)
2253249423Sdim    EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
2254249423Sdim
2255193326Sed  // If the base is a vector type, then we are forming a vector element lvalue
2256193326Sed  // with this subscript.
2257193326Sed  if (E->getBase()->getType()->isVectorType()) {
2258193326Sed    // Emit the vector as an lvalue to get its address.
2259193326Sed    LValue LHS = EmitLValue(E->getBase());
2260193326Sed    assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
2261218893Sdim    Idx = Builder.CreateIntCast(Idx, Int32Ty, IdxSigned, "vidx");
2262193326Sed    return LValue::MakeVectorElt(LHS.getAddress(), Idx,
2263234353Sdim                                 E->getBase()->getType(), LHS.getAlignment());
2264193326Sed  }
2265198092Srdivacky
2266193326Sed  // Extend or truncate the index type to 32 or 64-bits.
2267218893Sdim  if (Idx->getType() != IntPtrTy)
2268218893Sdim    Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
2269200583Srdivacky
2270198092Srdivacky  // We know that the pointer points to a type of the correct size, unless the
2271198092Srdivacky  // size is a VLA or Objective-C interface.
2272193326Sed  llvm::Value *Address = 0;
2273234353Sdim  CharUnits ArrayAlignment;
2274224145Sdim  if (const VariableArrayType *vla =
2275193326Sed        getContext().getAsVariableArrayType(E->getType())) {
2276224145Sdim    // The base must be a pointer, which is not an aggregate.  Emit
2277224145Sdim    // it.  It needs to be emitted first in case it's what captures
2278224145Sdim    // the VLA bounds.
2279224145Sdim    Address = EmitScalarExpr(E->getBase());
2280198092Srdivacky
2281224145Sdim    // The element count here is the total number of non-VLA elements.
2282224145Sdim    llvm::Value *numElements = getVLASize(vla).first;
2283198092Srdivacky
2284224145Sdim    // Effectively, the multiply by the VLA size is part of the GEP.
2285224145Sdim    // GEP indexes are signed, and scaling an index isn't permitted to
2286224145Sdim    // signed-overflow, so we use the same semantics for our explicit
2287224145Sdim    // multiply.  We suppress this if overflow is not undefined behavior.
2288234353Sdim    if (getLangOpts().isSignedOverflowDefined()) {
2289224145Sdim      Idx = Builder.CreateMul(Idx, numElements);
2290221345Sdim      Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2291224145Sdim    } else {
2292224145Sdim      Idx = Builder.CreateNSWMul(Idx, numElements);
2293221345Sdim      Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
2294224145Sdim    }
2295210299Sed  } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
2296210299Sed    // Indexing over an interface, as in "NSString *P; P[4];"
2297198092Srdivacky    llvm::Value *InterfaceSize =
2298193326Sed      llvm::ConstantInt::get(Idx->getType(),
2299202379Srdivacky          getContext().getTypeSizeInChars(OIT).getQuantity());
2300198092Srdivacky
2301193326Sed    Idx = Builder.CreateMul(Idx, InterfaceSize);
2302193326Sed
2303210299Sed    // The base must be a pointer, which is not an aggregate.  Emit it.
2304210299Sed    llvm::Value *Base = EmitScalarExpr(E->getBase());
2305218893Sdim    Address = EmitCastToVoidPtr(Base);
2306218893Sdim    Address = Builder.CreateGEP(Address, Idx, "arrayidx");
2307193326Sed    Address = Builder.CreateBitCast(Address, Base->getType());
2308210299Sed  } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
2309210299Sed    // If this is A[i] where A is an array, the frontend will have decayed the
2310210299Sed    // base to be a ArrayToPointerDecay implicit cast.  While correct, it is
2311210299Sed    // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
2312210299Sed    // "gep x, i" here.  Emit one "gep A, 0, i".
2313210299Sed    assert(Array->getType()->isArrayType() &&
2314210299Sed           "Array to pointer decay must have array source type!");
2315249423Sdim    LValue ArrayLV;
2316249423Sdim    // For simple multidimensional array indexing, set the 'accessed' flag for
2317249423Sdim    // better bounds-checking of the base expression.
2318249423Sdim    if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(Array))
2319249423Sdim      ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
2320249423Sdim    else
2321249423Sdim      ArrayLV = EmitLValue(Array);
2322221345Sdim    llvm::Value *ArrayPtr = ArrayLV.getAddress();
2323210299Sed    llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
2324210299Sed    llvm::Value *Args[] = { Zero, Idx };
2325263508Sdim
2326221345Sdim    // Propagate the alignment from the array itself to the result.
2327221345Sdim    ArrayAlignment = ArrayLV.getAlignment();
2328221345Sdim
2329243830Sdim    if (getLangOpts().isSignedOverflowDefined())
2330226633Sdim      Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx");
2331221345Sdim    else
2332226633Sdim      Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx");
2333193326Sed  } else {
2334210299Sed    // The base must be a pointer, which is not an aggregate.  Emit it.
2335210299Sed    llvm::Value *Base = EmitScalarExpr(E->getBase());
2336243830Sdim    if (getLangOpts().isSignedOverflowDefined())
2337221345Sdim      Address = Builder.CreateGEP(Base, Idx, "arrayidx");
2338221345Sdim    else
2339221345Sdim      Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
2340193326Sed  }
2341198092Srdivacky
2342198092Srdivacky  QualType T = E->getBase()->getType()->getPointeeType();
2343198092Srdivacky  assert(!T.isNull() &&
2344198092Srdivacky         "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
2345198092Srdivacky
2346263508Sdim
2347221345Sdim  // Limit the alignment to that of the result type.
2348234353Sdim  LValue LV;
2349234353Sdim  if (!ArrayAlignment.isZero()) {
2350234353Sdim    CharUnits Align = getContext().getTypeAlignInChars(T);
2351221345Sdim    ArrayAlignment = std::min(Align, ArrayAlignment);
2352234353Sdim    LV = MakeAddrLValue(Address, T, ArrayAlignment);
2353234353Sdim  } else {
2354234353Sdim    LV = MakeNaturalAlignAddrLValue(Address, T);
2355221345Sdim  }
2356221345Sdim
2357212904Sdim  LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
2358198092Srdivacky
2359243830Sdim  if (getLangOpts().ObjC1 &&
2360243830Sdim      getLangOpts().getGC() != LangOptions::NonGC) {
2361212904Sdim    LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
2362198092Srdivacky    setObjCGCLValueClass(getContext(), E, LV);
2363198092Srdivacky  }
2364193326Sed  return LV;
2365193326Sed}
2366193326Sed
2367198092Srdivackystatic
2368234353Sdimllvm::Constant *GenerateConstantVector(CGBuilderTy &Builder,
2369263508Sdim                                       SmallVectorImpl<unsigned> &Elts) {
2370226633Sdim  SmallVector<llvm::Constant*, 4> CElts;
2371193326Sed  for (unsigned i = 0, e = Elts.size(); i != e; ++i)
2372234353Sdim    CElts.push_back(Builder.getInt32(Elts[i]));
2373193326Sed
2374218893Sdim  return llvm::ConstantVector::get(CElts);
2375193326Sed}
2376193326Sed
2377193326SedLValue CodeGenFunction::
2378193326SedEmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
2379193326Sed  // Emit the base vector as an l-value.
2380193326Sed  LValue Base;
2381193326Sed
2382193326Sed  // ExtVectorElementExpr's base can either be a vector or pointer to vector.
2383201361Srdivacky  if (E->isArrow()) {
2384201361Srdivacky    // If it is a pointer to a vector, emit the address and form an lvalue with
2385201361Srdivacky    // it.
2386201361Srdivacky    llvm::Value *Ptr = EmitScalarExpr(E->getBase());
2387198092Srdivacky    const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
2388212904Sdim    Base = MakeAddrLValue(Ptr, PT->getPointeeType());
2389212904Sdim    Base.getQuals().removeObjCGCAttr();
2390218893Sdim  } else if (E->getBase()->isGLValue()) {
2391201361Srdivacky    // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
2392201361Srdivacky    // emit the base as an lvalue.
2393201361Srdivacky    assert(E->getBase()->getType()->isVectorType());
2394201361Srdivacky    Base = EmitLValue(E->getBase());
2395201361Srdivacky  } else {
2396201361Srdivacky    // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
2397224145Sdim    assert(E->getBase()->getType()->isVectorType() &&
2398202379Srdivacky           "Result must be a vector");
2399201361Srdivacky    llvm::Value *Vec = EmitScalarExpr(E->getBase());
2400263508Sdim
2401201361Srdivacky    // Store the vector to memory (because LValue wants an address).
2402203955Srdivacky    llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
2403201361Srdivacky    Builder.CreateStore(Vec, VecMem);
2404212904Sdim    Base = MakeAddrLValue(VecMem, E->getBase()->getType());
2405193326Sed  }
2406224145Sdim
2407224145Sdim  QualType type =
2408224145Sdim    E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
2409263508Sdim
2410193326Sed  // Encode the element access list into a vector of unsigned indices.
2411226633Sdim  SmallVector<unsigned, 4> Indices;
2412193326Sed  E->getEncodedElementAccess(Indices);
2413193326Sed
2414193326Sed  if (Base.isSimple()) {
2415234353Sdim    llvm::Constant *CV = GenerateConstantVector(Builder, Indices);
2416234353Sdim    return LValue::MakeExtVectorElt(Base.getAddress(), CV, type,
2417234353Sdim                                    Base.getAlignment());
2418193326Sed  }
2419193326Sed  assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
2420193326Sed
2421193326Sed  llvm::Constant *BaseElts = Base.getExtVectorElts();
2422226633Sdim  SmallVector<llvm::Constant *, 4> CElts;
2423193326Sed
2424234353Sdim  for (unsigned i = 0, e = Indices.size(); i != e; ++i)
2425234353Sdim    CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
2426218893Sdim  llvm::Constant *CV = llvm::ConstantVector::get(CElts);
2427234353Sdim  return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type,
2428234353Sdim                                  Base.getAlignment());
2429193326Sed}
2430193326Sed
2431193326SedLValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
2432193326Sed  Expr *BaseExpr = E->getBase();
2433193326Sed
2434193326Sed  // If this is s.x, emit s as an lvalue.  If it is s->x, emit s as a scalar.
2435234982Sdim  LValue BaseLV;
2436243830Sdim  if (E->isArrow()) {
2437243830Sdim    llvm::Value *Ptr = EmitScalarExpr(BaseExpr);
2438243830Sdim    QualType PtrTy = BaseExpr->getType()->getPointeeType();
2439243830Sdim    EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Ptr, PtrTy);
2440243830Sdim    BaseLV = MakeNaturalAlignAddrLValue(Ptr, PtrTy);
2441243830Sdim  } else
2442243830Sdim    BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
2443193326Sed
2444199482Srdivacky  NamedDecl *ND = E->getMemberDecl();
2445199482Srdivacky  if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
2446234982Sdim    LValue LV = EmitLValueForField(BaseLV, Field);
2447199482Srdivacky    setObjCGCLValueClass(getContext(), E, LV);
2448199482Srdivacky    return LV;
2449199482Srdivacky  }
2450263508Sdim
2451199482Srdivacky  if (VarDecl *VD = dyn_cast<VarDecl>(ND))
2452199482Srdivacky    return EmitGlobalVarDeclLValue(*this, E, VD);
2453199990Srdivacky
2454199990Srdivacky  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
2455199990Srdivacky    return EmitFunctionDeclLValue(*this, E, FD);
2456199990Srdivacky
2457226633Sdim  llvm_unreachable("Unhandled member declaration!");
2458193326Sed}
2459193326Sed
2460251662Sdim/// Given that we are currently emitting a lambda, emit an l-value for
2461251662Sdim/// one of its members.
2462251662SdimLValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) {
2463251662Sdim  assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda());
2464251662Sdim  assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent());
2465251662Sdim  QualType LambdaTagType =
2466251662Sdim    getContext().getTagDeclType(Field->getParent());
2467251662Sdim  LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType);
2468251662Sdim  return EmitLValueForField(LambdaLV, Field);
2469251662Sdim}
2470251662Sdim
2471234982SdimLValue CodeGenFunction::EmitLValueForField(LValue base,
2472234982Sdim                                           const FieldDecl *field) {
2473239462Sdim  if (field->isBitField()) {
2474239462Sdim    const CGRecordLayout &RL =
2475239462Sdim      CGM.getTypes().getCGRecordLayout(field->getParent());
2476239462Sdim    const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
2477249423Sdim    llvm::Value *Addr = base.getAddress();
2478249423Sdim    unsigned Idx = RL.getLLVMFieldNo(field);
2479249423Sdim    if (Idx != 0)
2480249423Sdim      // For structs, we GEP to the field that the record layout suggests.
2481249423Sdim      Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
2482249423Sdim    // Get the access type.
2483249423Sdim    llvm::Type *PtrTy = llvm::Type::getIntNPtrTy(
2484249423Sdim      getLLVMContext(), Info.StorageSize,
2485249423Sdim      CGM.getContext().getTargetAddressSpace(base.getType()));
2486249423Sdim    if (Addr->getType() != PtrTy)
2487249423Sdim      Addr = Builder.CreateBitCast(Addr, PtrTy);
2488249423Sdim
2489239462Sdim    QualType fieldType =
2490239462Sdim      field->getType().withCVRQualifiers(base.getVRQualifiers());
2491249423Sdim    return LValue::MakeBitfield(Addr, Info, fieldType, base.getAlignment());
2492239462Sdim  }
2493198092Srdivacky
2494219077Sdim  const RecordDecl *rec = field->getParent();
2495219077Sdim  QualType type = field->getType();
2496234353Sdim  CharUnits alignment = getContext().getDeclAlign(field);
2497193326Sed
2498234982Sdim  // FIXME: It should be impossible to have an LValue without alignment for a
2499234982Sdim  // complete type.
2500234982Sdim  if (!base.getAlignment().isZero())
2501234982Sdim    alignment = std::min(alignment, base.getAlignment());
2502234982Sdim
2503219077Sdim  bool mayAlias = rec->hasAttr<MayAliasAttr>();
2504219077Sdim
2505234982Sdim  llvm::Value *addr = base.getAddress();
2506234982Sdim  unsigned cvr = base.getVRQualifiers();
2507249423Sdim  bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA;
2508219077Sdim  if (rec->isUnion()) {
2509224145Sdim    // For unions, there is no pointer adjustment.
2510219077Sdim    assert(!type->isReferenceType() && "union has reference member");
2511249423Sdim    // TODO: handle path-aware TBAA for union.
2512249423Sdim    TBAAPath = false;
2513219077Sdim  } else {
2514219077Sdim    // For structs, we GEP to the field that the record layout suggests.
2515219077Sdim    unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
2516224145Sdim    addr = Builder.CreateStructGEP(addr, idx, field->getName());
2517219077Sdim
2518219077Sdim    // If this is a reference field, load the reference right now.
2519219077Sdim    if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
2520219077Sdim      llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
2521219077Sdim      if (cvr & Qualifiers::Volatile) load->setVolatile(true);
2522234353Sdim      load->setAlignment(alignment.getQuantity());
2523219077Sdim
2524249423Sdim      // Loading the reference will disable path-aware TBAA.
2525249423Sdim      TBAAPath = false;
2526219077Sdim      if (CGM.shouldUseTBAA()) {
2527219077Sdim        llvm::MDNode *tbaa;
2528219077Sdim        if (mayAlias)
2529219077Sdim          tbaa = CGM.getTBAAInfo(getContext().CharTy);
2530219077Sdim        else
2531219077Sdim          tbaa = CGM.getTBAAInfo(type);
2532263508Sdim        if (tbaa)
2533263508Sdim          CGM.DecorateInstruction(load, tbaa);
2534219077Sdim      }
2535219077Sdim
2536219077Sdim      addr = load;
2537219077Sdim      mayAlias = false;
2538219077Sdim      type = refType->getPointeeType();
2539234353Sdim      if (type->isIncompleteType())
2540234353Sdim        alignment = CharUnits();
2541234353Sdim      else
2542234353Sdim        alignment = getContext().getTypeAlignInChars(type);
2543219077Sdim      cvr = 0; // qualifiers don't recursively apply to referencee
2544219077Sdim    }
2545193326Sed  }
2546263508Sdim
2547224145Sdim  // Make sure that the address is pointing to the right type.  This is critical
2548224145Sdim  // for both unions and structs.  A union needs a bitcast, a struct element
2549224145Sdim  // will need a bitcast if the LLVM type laid out doesn't match the desired
2550224145Sdim  // type.
2551224145Sdim  addr = EmitBitCastOfLValueToProperType(*this, addr,
2552224145Sdim                                         CGM.getTypes().ConvertTypeForMem(type),
2553224145Sdim                                         field->getName());
2554193326Sed
2555226633Sdim  if (field->hasAttr<AnnotateAttr>())
2556226633Sdim    addr = EmitFieldAnnotations(field, addr);
2557226633Sdim
2558219077Sdim  LValue LV = MakeAddrLValue(addr, type, alignment);
2559219077Sdim  LV.getQuals().addCVRQualifiers(cvr);
2560249423Sdim  if (TBAAPath) {
2561249423Sdim    const ASTRecordLayout &Layout =
2562249423Sdim        getContext().getASTRecordLayout(field->getParent());
2563249423Sdim    // Set the base type to be the base type of the base LValue and
2564249423Sdim    // update offset to be relative to the base type.
2565251662Sdim    LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType());
2566251662Sdim    LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() +
2567249423Sdim                     Layout.getFieldOffset(field->getFieldIndex()) /
2568249423Sdim                                           getContext().getCharWidth());
2569249423Sdim  }
2570212904Sdim
2571198092Srdivacky  // __weak attribute on a field is ignored.
2572212904Sdim  if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
2573212904Sdim    LV.getQuals().removeObjCGCAttr();
2574219077Sdim
2575219077Sdim  // Fields of may_alias structs act like 'char' for TBAA purposes.
2576219077Sdim  // FIXME: this should get propagated down through anonymous structs
2577219077Sdim  // and unions.
2578219077Sdim  if (mayAlias && LV.getTBAAInfo())
2579219077Sdim    LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
2580219077Sdim
2581212904Sdim  return LV;
2582193326Sed}
2583193326Sed
2584263508SdimLValue
2585263508SdimCodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
2586234982Sdim                                                  const FieldDecl *Field) {
2587203955Srdivacky  QualType FieldType = Field->getType();
2588263508Sdim
2589203955Srdivacky  if (!FieldType->isReferenceType())
2590234982Sdim    return EmitLValueForField(Base, Field);
2591203955Srdivacky
2592206084Srdivacky  const CGRecordLayout &RL =
2593206084Srdivacky    CGM.getTypes().getCGRecordLayout(Field->getParent());
2594206084Srdivacky  unsigned idx = RL.getLLVMFieldNo(Field);
2595234982Sdim  llvm::Value *V = Builder.CreateStructGEP(Base.getAddress(), idx);
2596203955Srdivacky  assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
2597203955Srdivacky
2598224145Sdim  // Make sure that the address is pointing to the right type.  This is critical
2599224145Sdim  // for both unions and structs.  A union needs a bitcast, a struct element
2600224145Sdim  // will need a bitcast if the LLVM type laid out doesn't match the desired
2601224145Sdim  // type.
2602226633Sdim  llvm::Type *llvmType = ConvertTypeForMem(FieldType);
2603234982Sdim  V = EmitBitCastOfLValueToProperType(*this, V, llvmType, Field->getName());
2604234982Sdim
2605234353Sdim  CharUnits Alignment = getContext().getDeclAlign(Field);
2606234982Sdim
2607234982Sdim  // FIXME: It should be impossible to have an LValue without alignment for a
2608234982Sdim  // complete type.
2609234982Sdim  if (!Base.getAlignment().isZero())
2610234982Sdim    Alignment = std::min(Alignment, Base.getAlignment());
2611234982Sdim
2612212904Sdim  return MakeAddrLValue(V, FieldType, Alignment);
2613203955Srdivacky}
2614203955Srdivacky
2615218893SdimLValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
2616234353Sdim  if (E->isFileScope()) {
2617234353Sdim    llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
2618234353Sdim    return MakeAddrLValue(GlobalPtr, E->getType());
2619234353Sdim  }
2620239462Sdim  if (E->getType()->isVariablyModifiedType())
2621239462Sdim    // make sure to emit the VLA size.
2622239462Sdim    EmitVariablyModifiedType(E->getType());
2623263508Sdim
2624204643Srdivacky  llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
2625218893Sdim  const Expr *InitExpr = E->getInitializer();
2626212904Sdim  LValue Result = MakeAddrLValue(DeclPtr, E->getType());
2627193326Sed
2628224145Sdim  EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
2629224145Sdim                   /*Init*/ true);
2630193326Sed
2631193326Sed  return Result;
2632193326Sed}
2633193326Sed
2634239462SdimLValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) {
2635239462Sdim  if (!E->isGLValue())
2636239462Sdim    // Initializing an aggregate temporary in C++11: T{...}.
2637239462Sdim    return EmitAggExprToLValue(E);
2638239462Sdim
2639239462Sdim  // An lvalue initializer list must be initializing a reference.
2640239462Sdim  assert(E->getNumInits() == 1 && "reference init with multiple values");
2641239462Sdim  return EmitLValue(E->getInit(0));
2642239462Sdim}
2643239462Sdim
2644218893SdimLValue CodeGenFunction::
2645218893SdimEmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
2646218893Sdim  if (!expr->isGLValue()) {
2647218893Sdim    // ?: here should be an aggregate.
2648249423Sdim    assert(hasAggregateEvaluationKind(expr->getType()) &&
2649218893Sdim           "Unexpected conditional operator!");
2650218893Sdim    return EmitAggExprToLValue(expr);
2651218893Sdim  }
2652201361Srdivacky
2653234353Sdim  OpaqueValueMapping binding(*this, expr);
2654234353Sdim
2655218893Sdim  const Expr *condExpr = expr->getCond();
2656221345Sdim  bool CondExprBool;
2657221345Sdim  if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
2658218893Sdim    const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
2659221345Sdim    if (!CondExprBool) std::swap(live, dead);
2660218893Sdim
2661218893Sdim    if (!ContainsLabel(dead))
2662218893Sdim      return EmitLValue(live);
2663218893Sdim  }
2664218893Sdim
2665218893Sdim  llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
2666218893Sdim  llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
2667218893Sdim  llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
2668218893Sdim
2669218893Sdim  ConditionalEvaluation eval(*this);
2670218893Sdim  EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock);
2671263508Sdim
2672218893Sdim  // Any temporaries created here are conditional.
2673218893Sdim  EmitBlock(lhsBlock);
2674218893Sdim  eval.begin(*this);
2675218893Sdim  LValue lhs = EmitLValue(expr->getTrueExpr());
2676218893Sdim  eval.end(*this);
2677263508Sdim
2678218893Sdim  if (!lhs.isSimple())
2679218893Sdim    return EmitUnsupportedLValue(expr, "conditional operator");
2680198092Srdivacky
2681218893Sdim  lhsBlock = Builder.GetInsertBlock();
2682218893Sdim  Builder.CreateBr(contBlock);
2683263508Sdim
2684218893Sdim  // Any temporaries created here are conditional.
2685218893Sdim  EmitBlock(rhsBlock);
2686218893Sdim  eval.begin(*this);
2687218893Sdim  LValue rhs = EmitLValue(expr->getFalseExpr());
2688218893Sdim  eval.end(*this);
2689218893Sdim  if (!rhs.isSimple())
2690218893Sdim    return EmitUnsupportedLValue(expr, "conditional operator");
2691218893Sdim  rhsBlock = Builder.GetInsertBlock();
2692198092Srdivacky
2693218893Sdim  EmitBlock(contBlock);
2694198092Srdivacky
2695221345Sdim  llvm::PHINode *phi = Builder.CreatePHI(lhs.getAddress()->getType(), 2,
2696218893Sdim                                         "cond-lvalue");
2697218893Sdim  phi->addIncoming(lhs.getAddress(), lhsBlock);
2698218893Sdim  phi->addIncoming(rhs.getAddress(), rhsBlock);
2699218893Sdim  return MakeAddrLValue(phi, expr->getType());
2700193326Sed}
2701193326Sed
2702239462Sdim/// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
2703239462Sdim/// type. If the cast is to a reference, we can have the usual lvalue result,
2704199482Srdivacky/// otherwise if a cast is needed by the code generator in an lvalue context,
2705199482Srdivacky/// then it must mean that we need the address of an aggregate in order to
2706239462Sdim/// access one of its members.  This can happen for all the reasons that casts
2707199482Srdivacky/// are permitted with aggregate result, including noop aggregate casts, and
2708199482Srdivacky/// cast from scalar to union.
2709193326SedLValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
2710198092Srdivacky  switch (E->getCastKind()) {
2711212904Sdim  case CK_ToVoid:
2712212904Sdim  case CK_BitCast:
2713212904Sdim  case CK_ArrayToPointerDecay:
2714212904Sdim  case CK_FunctionToPointerDecay:
2715212904Sdim  case CK_NullToMemberPointer:
2716218893Sdim  case CK_NullToPointer:
2717212904Sdim  case CK_IntegralToPointer:
2718212904Sdim  case CK_PointerToIntegral:
2719218893Sdim  case CK_PointerToBoolean:
2720212904Sdim  case CK_VectorSplat:
2721212904Sdim  case CK_IntegralCast:
2722218893Sdim  case CK_IntegralToBoolean:
2723212904Sdim  case CK_IntegralToFloating:
2724212904Sdim  case CK_FloatingToIntegral:
2725218893Sdim  case CK_FloatingToBoolean:
2726212904Sdim  case CK_FloatingCast:
2727218893Sdim  case CK_FloatingRealToComplex:
2728218893Sdim  case CK_FloatingComplexToReal:
2729218893Sdim  case CK_FloatingComplexToBoolean:
2730218893Sdim  case CK_FloatingComplexCast:
2731218893Sdim  case CK_FloatingComplexToIntegralComplex:
2732218893Sdim  case CK_IntegralRealToComplex:
2733218893Sdim  case CK_IntegralComplexToReal:
2734218893Sdim  case CK_IntegralComplexToBoolean:
2735218893Sdim  case CK_IntegralComplexCast:
2736218893Sdim  case CK_IntegralComplexToFloatingComplex:
2737212904Sdim  case CK_DerivedToBaseMemberPointer:
2738212904Sdim  case CK_BaseToDerivedMemberPointer:
2739212904Sdim  case CK_MemberPointerToBoolean:
2740234353Sdim  case CK_ReinterpretMemberPointer:
2741224145Sdim  case CK_AnyPointerToBlockPointerCast:
2742226633Sdim  case CK_ARCProduceObject:
2743226633Sdim  case CK_ARCConsumeObject:
2744226633Sdim  case CK_ARCReclaimReturnedObject:
2745263508Sdim  case CK_ARCExtendBlockObject:
2746263508Sdim  case CK_CopyAndAutoreleaseBlockObject:
2747263508Sdim    return EmitUnsupportedLValue(E, "unexpected cast lvalue");
2748199482Srdivacky
2749263508Sdim  case CK_Dependent:
2750263508Sdim    llvm_unreachable("dependent cast kind in IR gen!");
2751263508Sdim
2752263508Sdim  case CK_BuiltinFnToFnPtr:
2753263508Sdim    llvm_unreachable("builtin functions are handled elsewhere");
2754263508Sdim
2755263508Sdim  // These are never l-values; just use the aggregate emission code.
2756263508Sdim  case CK_NonAtomicToAtomic:
2757263508Sdim  case CK_AtomicToNonAtomic:
2758263508Sdim    return EmitAggExprToLValue(E);
2759263508Sdim
2760212904Sdim  case CK_Dynamic: {
2761199482Srdivacky    LValue LV = EmitLValue(E->getSubExpr());
2762199482Srdivacky    llvm::Value *V = LV.getAddress();
2763199482Srdivacky    const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
2764212904Sdim    return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
2765199482Srdivacky  }
2766199482Srdivacky
2767212904Sdim  case CK_ConstructorConversion:
2768212904Sdim  case CK_UserDefinedConversion:
2769226633Sdim  case CK_CPointerToObjCPointerCast:
2770226633Sdim  case CK_BlockPointerToObjCPointerCast:
2771263508Sdim  case CK_NoOp:
2772263508Sdim  case CK_LValueToRValue:
2773193326Sed    return EmitLValue(E->getSubExpr());
2774263508Sdim
2775212904Sdim  case CK_UncheckedDerivedToBase:
2776212904Sdim  case CK_DerivedToBase: {
2777263508Sdim    const RecordType *DerivedClassTy =
2778198092Srdivacky      E->getSubExpr()->getType()->getAs<RecordType>();
2779263508Sdim    CXXRecordDecl *DerivedClassDecl =
2780198092Srdivacky      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2781263508Sdim
2782198092Srdivacky    LValue LV = EmitLValue(E->getSubExpr());
2783218893Sdim    llvm::Value *This = LV.getAddress();
2784263508Sdim
2785198092Srdivacky    // Perform the derived-to-base conversion
2786263508Sdim    llvm::Value *Base =
2787263508Sdim      GetAddressOfBaseClass(This, DerivedClassDecl,
2788212904Sdim                            E->path_begin(), E->path_end(),
2789212904Sdim                            /*NullCheckValue=*/false);
2790263508Sdim
2791212904Sdim    return MakeAddrLValue(Base, E->getType());
2792198092Srdivacky  }
2793212904Sdim  case CK_ToUnion:
2794203955Srdivacky    return EmitAggExprToLValue(E);
2795212904Sdim  case CK_BaseToDerived: {
2796199990Srdivacky    const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
2797263508Sdim    CXXRecordDecl *DerivedClassDecl =
2798199990Srdivacky      cast<CXXRecordDecl>(DerivedClassTy->getDecl());
2799263508Sdim
2800199990Srdivacky    LValue LV = EmitLValue(E->getSubExpr());
2801249423Sdim
2802263508Sdim    // Perform the base-to-derived conversion
2803263508Sdim    llvm::Value *Derived =
2804263508Sdim      GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
2805263508Sdim                               E->path_begin(), E->path_end(),
2806263508Sdim                               /*NullCheckValue=*/false);
2807263508Sdim
2808249423Sdim    // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
2809249423Sdim    // performed and the object is not of the derived type.
2810249423Sdim    if (SanitizePerformTypeCheck)
2811249423Sdim      EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(),
2812263508Sdim                    Derived, E->getType());
2813249423Sdim
2814212904Sdim    return MakeAddrLValue(Derived, E->getType());
2815199482Srdivacky  }
2816212904Sdim  case CK_LValueBitCast: {
2817199482Srdivacky    // This must be a reinterpret_cast (or c-style equivalent).
2818199482Srdivacky    const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
2819263508Sdim
2820199482Srdivacky    LValue LV = EmitLValue(E->getSubExpr());
2821199482Srdivacky    llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2822199482Srdivacky                                           ConvertType(CE->getTypeAsWritten()));
2823212904Sdim    return MakeAddrLValue(V, E->getType());
2824199482Srdivacky  }
2825212904Sdim  case CK_ObjCObjectLValueCast: {
2826212904Sdim    LValue LV = EmitLValue(E->getSubExpr());
2827212904Sdim    QualType ToType = getContext().getLValueReferenceType(E->getType());
2828263508Sdim    llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
2829212904Sdim                                           ConvertType(ToType));
2830212904Sdim    return MakeAddrLValue(V, E->getType());
2831199482Srdivacky  }
2832249423Sdim  case CK_ZeroToOCLEvent:
2833249423Sdim    llvm_unreachable("NULL to OpenCL event lvalue cast is not valid");
2834212904Sdim  }
2835263508Sdim
2836212904Sdim  llvm_unreachable("Unhandled lvalue cast kind?");
2837193326Sed}
2838193326Sed
2839218893SdimLValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
2840234353Sdim  assert(OpaqueValueMappingData::shouldBindAsLValue(e));
2841218893Sdim  return getOpaqueLValueMapping(e);
2842218893Sdim}
2843218893Sdim
2844234982SdimRValue CodeGenFunction::EmitRValueForField(LValue LV,
2845263508Sdim                                           const FieldDecl *FD,
2846263508Sdim                                           SourceLocation Loc) {
2847234982Sdim  QualType FT = FD->getType();
2848234982Sdim  LValue FieldLV = EmitLValueForField(LV, FD);
2849249423Sdim  switch (getEvaluationKind(FT)) {
2850249423Sdim  case TEK_Complex:
2851263508Sdim    return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
2852249423Sdim  case TEK_Aggregate:
2853234982Sdim    return FieldLV.asAggregateRValue();
2854249423Sdim  case TEK_Scalar:
2855263508Sdim    return EmitLoadOfLValue(FieldLV, Loc);
2856249423Sdim  }
2857249423Sdim  llvm_unreachable("bad evaluation kind");
2858234982Sdim}
2859234982Sdim
2860193326Sed//===--------------------------------------------------------------------===//
2861193326Sed//                             Expression Emission
2862193326Sed//===--------------------------------------------------------------------===//
2863193326Sed
2864263508SdimRValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
2865201361Srdivacky                                     ReturnValueSlot ReturnValue) {
2866249423Sdim  if (CGDebugInfo *DI = getDebugInfo()) {
2867249423Sdim    SourceLocation Loc = E->getLocStart();
2868249423Sdim    // Force column info to be generated so we can differentiate
2869249423Sdim    // multiple call sites on the same line in the debug info.
2870249423Sdim    const FunctionDecl* Callee = E->getDirectCallee();
2871249423Sdim    bool ForceColumnInfo = Callee && Callee->isInlineSpecified();
2872249423Sdim    DI->EmitLocation(Builder, Loc, ForceColumnInfo);
2873249423Sdim  }
2874221345Sdim
2875193326Sed  // Builtins never have block type.
2876193326Sed  if (E->getCallee()->getType()->isBlockPointerType())
2877201361Srdivacky    return EmitBlockCallExpr(E, ReturnValue);
2878193326Sed
2879193326Sed  if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
2880201361Srdivacky    return EmitCXXMemberCallExpr(CE, ReturnValue);
2881198092Srdivacky
2882226633Sdim  if (const CUDAKernelCallExpr *CE = dyn_cast<CUDAKernelCallExpr>(E))
2883226633Sdim    return EmitCUDAKernelCallExpr(CE, ReturnValue);
2884226633Sdim
2885226633Sdim  const Decl *TargetDecl = E->getCalleeDecl();
2886226633Sdim  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
2887226633Sdim    if (unsigned builtinID = FD->getBuiltinID())
2888226633Sdim      return EmitBuiltinExpr(FD, builtinID, E);
2889193326Sed  }
2890193326Sed
2891194179Sed  if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
2892193326Sed    if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
2893201361Srdivacky      return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
2894198092Srdivacky
2895263508Sdim  if (const CXXPseudoDestructorExpr *PseudoDtor
2896224145Sdim          = dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
2897224145Sdim    QualType DestroyedType = PseudoDtor->getDestroyedType();
2898243830Sdim    if (getLangOpts().ObjCAutoRefCount &&
2899224145Sdim        DestroyedType->isObjCLifetimeType() &&
2900224145Sdim        (DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
2901224145Sdim         DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
2902224145Sdim      // Automatic Reference Counting:
2903224145Sdim      //   If the pseudo-expression names a retainable object with weak or
2904224145Sdim      //   strong lifetime, the object shall be released.
2905224145Sdim      Expr *BaseExpr = PseudoDtor->getBase();
2906224145Sdim      llvm::Value *BaseValue = NULL;
2907224145Sdim      Qualifiers BaseQuals;
2908263508Sdim
2909224145Sdim      // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
2910224145Sdim      if (PseudoDtor->isArrow()) {
2911224145Sdim        BaseValue = EmitScalarExpr(BaseExpr);
2912224145Sdim        const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
2913224145Sdim        BaseQuals = PTy->getPointeeType().getQualifiers();
2914224145Sdim      } else {
2915224145Sdim        LValue BaseLV = EmitLValue(BaseExpr);
2916224145Sdim        BaseValue = BaseLV.getAddress();
2917224145Sdim        QualType BaseTy = BaseExpr->getType();
2918224145Sdim        BaseQuals = BaseTy.getQualifiers();
2919224145Sdim      }
2920263508Sdim
2921224145Sdim      switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
2922224145Sdim      case Qualifiers::OCL_None:
2923224145Sdim      case Qualifiers::OCL_ExplicitNone:
2924224145Sdim      case Qualifiers::OCL_Autoreleasing:
2925224145Sdim        break;
2926263508Sdim
2927224145Sdim      case Qualifiers::OCL_Strong:
2928263508Sdim        EmitARCRelease(Builder.CreateLoad(BaseValue,
2929224145Sdim                          PseudoDtor->getDestroyedType().isVolatileQualified()),
2930249423Sdim                       ARCPreciseLifetime);
2931224145Sdim        break;
2932224145Sdim
2933224145Sdim      case Qualifiers::OCL_Weak:
2934224145Sdim        EmitARCDestroyWeak(BaseValue);
2935224145Sdim        break;
2936224145Sdim      }
2937224145Sdim    } else {
2938224145Sdim      // C++ [expr.pseudo]p1:
2939224145Sdim      //   The result shall only be used as the operand for the function call
2940224145Sdim      //   operator (), and the result of such a call has type void. The only
2941224145Sdim      //   effect is the evaluation of the postfix-expression before the dot or
2942263508Sdim      //   arrow.
2943224145Sdim      EmitScalarExpr(E->getCallee());
2944224145Sdim    }
2945263508Sdim
2946198092Srdivacky    return RValue::get(0);
2947198092Srdivacky  }
2948198092Srdivacky
2949193326Sed  llvm::Value *Callee = EmitScalarExpr(E->getCallee());
2950263508Sdim  return EmitCall(E->getCallee()->getType(), Callee, E->getLocStart(),
2951263508Sdim                  ReturnValue, E->arg_begin(), E->arg_end(), TargetDecl);
2952193326Sed}
2953193326Sed
2954193326SedLValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
2955193326Sed  // Comma expressions just emit their LHS then their RHS as an l-value.
2956212904Sdim  if (E->getOpcode() == BO_Comma) {
2957218893Sdim    EmitIgnoredExpr(E->getLHS());
2958200583Srdivacky    EnsureInsertPoint();
2959193326Sed    return EmitLValue(E->getRHS());
2960193326Sed  }
2961198092Srdivacky
2962212904Sdim  if (E->getOpcode() == BO_PtrMemD ||
2963212904Sdim      E->getOpcode() == BO_PtrMemI)
2964198398Srdivacky    return EmitPointerToDataMemberBinaryExpr(E);
2965218893Sdim
2966218893Sdim  assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
2967224145Sdim
2968224145Sdim  // Note that in all of these cases, __block variables need the RHS
2969224145Sdim  // evaluated first just in case the variable gets moved by the RHS.
2970249423Sdim
2971249423Sdim  switch (getEvaluationKind(E->getType())) {
2972249423Sdim  case TEK_Scalar: {
2973224145Sdim    switch (E->getLHS()->getType().getObjCLifetime()) {
2974224145Sdim    case Qualifiers::OCL_Strong:
2975224145Sdim      return EmitARCStoreStrong(E, /*ignored*/ false).first;
2976224145Sdim
2977224145Sdim    case Qualifiers::OCL_Autoreleasing:
2978224145Sdim      return EmitARCStoreAutoreleasing(E).first;
2979224145Sdim
2980224145Sdim    // No reason to do any of these differently.
2981224145Sdim    case Qualifiers::OCL_None:
2982224145Sdim    case Qualifiers::OCL_ExplicitNone:
2983224145Sdim    case Qualifiers::OCL_Weak:
2984224145Sdim      break;
2985224145Sdim    }
2986224145Sdim
2987218893Sdim    RValue RV = EmitAnyExpr(E->getRHS());
2988243830Sdim    LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store);
2989224145Sdim    EmitStoreThroughLValue(RV, LV);
2990198398Srdivacky    return LV;
2991198398Srdivacky  }
2992218893Sdim
2993249423Sdim  case TEK_Complex:
2994218893Sdim    return EmitComplexAssignmentLValue(E);
2995218893Sdim
2996249423Sdim  case TEK_Aggregate:
2997249423Sdim    return EmitAggExprToLValue(E);
2998249423Sdim  }
2999249423Sdim  llvm_unreachable("bad evaluation kind");
3000193326Sed}
3001193326Sed
3002193326SedLValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
3003193326Sed  RValue RV = EmitCallExpr(E);
3004193326Sed
3005198893Srdivacky  if (!RV.isScalar())
3006212904Sdim    return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3007263508Sdim
3008198893Srdivacky  assert(E->getCallReturnType()->isReferenceType() &&
3009198893Srdivacky         "Can't have a scalar return unless the return type is a "
3010198893Srdivacky         "reference type!");
3011198092Srdivacky
3012212904Sdim  return MakeAddrLValue(RV.getScalarVal(), E->getType());
3013193326Sed}
3014193326Sed
3015193326SedLValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
3016193326Sed  // FIXME: This shouldn't require another copy.
3017203955Srdivacky  return EmitAggExprToLValue(E);
3018193326Sed}
3019193326Sed
3020193326SedLValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
3021218893Sdim  assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
3022218893Sdim         && "binding l-value to type which needs a temporary");
3023226633Sdim  AggValueSlot Slot = CreateAggTemp(E->getType());
3024218893Sdim  EmitCXXConstructExpr(E, Slot);
3025218893Sdim  return MakeAddrLValue(Slot.getAddr(), E->getType());
3026193326Sed}
3027193326Sed
3028193326SedLValue
3029199482SrdivackyCodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
3030212904Sdim  return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
3031199482Srdivacky}
3032199482Srdivacky
3033243830Sdimllvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
3034263508Sdim  return Builder.CreateBitCast(CGM.GetAddrOfUuidDescriptor(E),
3035263508Sdim                               ConvertType(E->getType())->getPointerTo());
3036243830Sdim}
3037243830Sdim
3038243830SdimLValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
3039243830Sdim  return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType());
3040243830Sdim}
3041243830Sdim
3042199482SrdivackyLValue
3043193326SedCodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
3044218893Sdim  AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
3045226633Sdim  Slot.setExternallyDestructed();
3046218893Sdim  EmitAggExpr(E->getSubExpr(), Slot);
3047234353Sdim  EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr());
3048218893Sdim  return MakeAddrLValue(Slot.getAddr(), E->getType());
3049193326Sed}
3050193326Sed
3051234353SdimLValue
3052234353SdimCodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) {
3053234353Sdim  AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
3054234353Sdim  EmitLambdaExpr(E, Slot);
3055234353Sdim  return MakeAddrLValue(Slot.getAddr(), E->getType());
3056234353Sdim}
3057234353Sdim
3058193326SedLValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
3059193326Sed  RValue RV = EmitObjCMessageExpr(E);
3060263508Sdim
3061210299Sed  if (!RV.isScalar())
3062212904Sdim    return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3063263508Sdim
3064210299Sed  assert(E->getMethodDecl()->getResultType()->isReferenceType() &&
3065210299Sed         "Can't have a scalar return unless the return type is a "
3066210299Sed         "reference type!");
3067263508Sdim
3068212904Sdim  return MakeAddrLValue(RV.getScalarVal(), E->getType());
3069193326Sed}
3070193326Sed
3071210299SedLValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
3072263508Sdim  llvm::Value *V =
3073249423Sdim    CGM.getObjCRuntime().GetSelector(*this, E->getSelector(), true);
3074212904Sdim  return MakeAddrLValue(V, E->getType());
3075210299Sed}
3076210299Sed
3077193326Sedllvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
3078193326Sed                                             const ObjCIvarDecl *Ivar) {
3079193326Sed  return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
3080193326Sed}
3081193326Sed
3082193326SedLValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
3083193326Sed                                          llvm::Value *BaseValue,
3084193326Sed                                          const ObjCIvarDecl *Ivar,
3085193326Sed                                          unsigned CVRQualifiers) {
3086193326Sed  return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
3087193326Sed                                                   Ivar, CVRQualifiers);
3088193326Sed}
3089193326Sed
3090193326SedLValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
3091193326Sed  // FIXME: A lot of the code below could be shared with EmitMemberExpr.
3092193326Sed  llvm::Value *BaseValue = 0;
3093193326Sed  const Expr *BaseExpr = E->getBase();
3094198092Srdivacky  Qualifiers BaseQuals;
3095193326Sed  QualType ObjectTy;
3096193326Sed  if (E->isArrow()) {
3097193326Sed    BaseValue = EmitScalarExpr(BaseExpr);
3098198092Srdivacky    ObjectTy = BaseExpr->getType()->getPointeeType();
3099198092Srdivacky    BaseQuals = ObjectTy.getQualifiers();
3100193326Sed  } else {
3101193326Sed    LValue BaseLV = EmitLValue(BaseExpr);
3102193326Sed    // FIXME: this isn't right for bitfields.
3103193326Sed    BaseValue = BaseLV.getAddress();
3104193326Sed    ObjectTy = BaseExpr->getType();
3105198092Srdivacky    BaseQuals = ObjectTy.getQualifiers();
3106193326Sed  }
3107193326Sed
3108263508Sdim  LValue LV =
3109198092Srdivacky    EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
3110198092Srdivacky                      BaseQuals.getCVRQualifiers());
3111198092Srdivacky  setObjCGCLValueClass(getContext(), E, LV);
3112198092Srdivacky  return LV;
3113193326Sed}
3114193326Sed
3115193326SedLValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
3116193326Sed  // Can only get l-value for message expression returning aggregate type
3117193326Sed  RValue RV = EmitAnyExprToTemp(E);
3118212904Sdim  return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
3119193326Sed}
3120193326Sed
3121201361SrdivackyRValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
3122263508Sdim                                 SourceLocation CallLoc,
3123201361Srdivacky                                 ReturnValueSlot ReturnValue,
3124193326Sed                                 CallExpr::const_arg_iterator ArgBeg,
3125193326Sed                                 CallExpr::const_arg_iterator ArgEnd,
3126193326Sed                                 const Decl *TargetDecl) {
3127198092Srdivacky  // Get the actual function type. The callee type will always be a pointer to
3128198092Srdivacky  // function type or a block pointer type.
3129198092Srdivacky  assert(CalleeType->isFunctionPointerType() &&
3130193326Sed         "Call must have function pointer type!");
3131193326Sed
3132198398Srdivacky  CalleeType = getContext().getCanonicalType(CalleeType);
3133193326Sed
3134203955Srdivacky  const FunctionType *FnType
3135203955Srdivacky    = cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
3136198398Srdivacky
3137263508Sdim  // Force column info to differentiate multiple inlined call sites on
3138263508Sdim  // the same line, analoguous to EmitCallExpr.
3139263508Sdim  bool ForceColumnInfo = false;
3140263508Sdim  if (const FunctionDecl* FD = dyn_cast_or_null<const FunctionDecl>(TargetDecl))
3141263508Sdim    ForceColumnInfo = FD->isInlineSpecified();
3142263508Sdim
3143263508Sdim  if (getLangOpts().CPlusPlus && SanOpts->Function &&
3144263508Sdim      (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
3145263508Sdim    if (llvm::Constant *PrefixSig =
3146263508Sdim            CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
3147263508Sdim      llvm::Constant *FTRTTIConst =
3148263508Sdim          CGM.GetAddrOfRTTIDescriptor(QualType(FnType, 0), /*ForEH=*/true);
3149263508Sdim      llvm::Type *PrefixStructTyElems[] = {
3150263508Sdim        PrefixSig->getType(),
3151263508Sdim        FTRTTIConst->getType()
3152263508Sdim      };
3153263508Sdim      llvm::StructType *PrefixStructTy = llvm::StructType::get(
3154263508Sdim          CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true);
3155263508Sdim
3156263508Sdim      llvm::Value *CalleePrefixStruct = Builder.CreateBitCast(
3157263508Sdim          Callee, llvm::PointerType::getUnqual(PrefixStructTy));
3158263508Sdim      llvm::Value *CalleeSigPtr =
3159263508Sdim          Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 0);
3160263508Sdim      llvm::Value *CalleeSig = Builder.CreateLoad(CalleeSigPtr);
3161263508Sdim      llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
3162263508Sdim
3163263508Sdim      llvm::BasicBlock *Cont = createBasicBlock("cont");
3164263508Sdim      llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
3165263508Sdim      Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
3166263508Sdim
3167263508Sdim      EmitBlock(TypeCheck);
3168263508Sdim      llvm::Value *CalleeRTTIPtr =
3169263508Sdim          Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 1);
3170263508Sdim      llvm::Value *CalleeRTTI = Builder.CreateLoad(CalleeRTTIPtr);
3171263508Sdim      llvm::Value *CalleeRTTIMatch =
3172263508Sdim          Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
3173263508Sdim      llvm::Constant *StaticData[] = {
3174263508Sdim        EmitCheckSourceLocation(CallLoc),
3175263508Sdim        EmitCheckTypeDescriptor(CalleeType)
3176263508Sdim      };
3177263508Sdim      EmitCheck(CalleeRTTIMatch,
3178263508Sdim                "function_type_mismatch",
3179263508Sdim                StaticData,
3180263508Sdim                Callee,
3181263508Sdim                CRK_Recoverable);
3182263508Sdim
3183263508Sdim      Builder.CreateBr(Cont);
3184263508Sdim      EmitBlock(Cont);
3185263508Sdim    }
3186263508Sdim  }
3187263508Sdim
3188193326Sed  CallArgList Args;
3189263508Sdim  EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd,
3190263508Sdim               ForceColumnInfo);
3191193326Sed
3192234353Sdim  const CGFunctionInfo &FnInfo =
3193239462Sdim    CGM.getTypes().arrangeFreeFunctionCall(Args, FnType);
3194226633Sdim
3195226633Sdim  // C99 6.5.2.2p6:
3196226633Sdim  //   If the expression that denotes the called function has a type
3197226633Sdim  //   that does not include a prototype, [the default argument
3198226633Sdim  //   promotions are performed]. If the number of arguments does not
3199226633Sdim  //   equal the number of parameters, the behavior is undefined. If
3200226633Sdim  //   the function is defined with a type that includes a prototype,
3201226633Sdim  //   and either the prototype ends with an ellipsis (, ...) or the
3202226633Sdim  //   types of the arguments after promotion are not compatible with
3203226633Sdim  //   the types of the parameters, the behavior is undefined. If the
3204226633Sdim  //   function is defined with a type that does not include a
3205226633Sdim  //   prototype, and the types of the arguments after promotion are
3206226633Sdim  //   not compatible with those of the parameters after promotion,
3207226633Sdim  //   the behavior is undefined [except in some trivial cases].
3208226633Sdim  // That is, in the general case, we should assume that a call
3209226633Sdim  // through an unprototyped function type works like a *non-variadic*
3210226633Sdim  // call.  The way we make this work is to cast to the exact type
3211226633Sdim  // of the promoted arguments.
3212249423Sdim  if (isa<FunctionNoProtoType>(FnType)) {
3213234353Sdim    llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
3214226633Sdim    CalleeTy = CalleeTy->getPointerTo();
3215226633Sdim    Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast");
3216226633Sdim  }
3217226633Sdim
3218226633Sdim  return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl);
3219193326Sed}
3220198398Srdivacky
3221198893SrdivackyLValue CodeGenFunction::
3222198893SrdivackyEmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
3223199482Srdivacky  llvm::Value *BaseV;
3224212904Sdim  if (E->getOpcode() == BO_PtrMemI)
3225199482Srdivacky    BaseV = EmitScalarExpr(E->getLHS());
3226199482Srdivacky  else
3227199482Srdivacky    BaseV = EmitLValue(E->getLHS()).getAddress();
3228212904Sdim
3229199482Srdivacky  llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
3230198893Srdivacky
3231212904Sdim  const MemberPointerType *MPT
3232212904Sdim    = E->getRHS()->getType()->getAs<MemberPointerType>();
3233212904Sdim
3234212904Sdim  llvm::Value *AddV =
3235212904Sdim    CGM.getCXXABI().EmitMemberDataPointerAddress(*this, BaseV, OffsetV, MPT);
3236212904Sdim
3237212904Sdim  return MakeAddrLValue(AddV, MPT->getPointeeType());
3238198398Srdivacky}
3239226633Sdim
3240249423Sdim/// Given the address of a temporary variable, produce an r-value of
3241249423Sdim/// its type.
3242249423SdimRValue CodeGenFunction::convertTempToRValue(llvm::Value *addr,
3243263508Sdim                                            QualType type,
3244263508Sdim                                            SourceLocation loc) {
3245249423Sdim  LValue lvalue = MakeNaturalAlignAddrLValue(addr, type);
3246249423Sdim  switch (getEvaluationKind(type)) {
3247249423Sdim  case TEK_Complex:
3248263508Sdim    return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
3249249423Sdim  case TEK_Aggregate:
3250249423Sdim    return lvalue.asAggregateRValue();
3251249423Sdim  case TEK_Scalar:
3252263508Sdim    return RValue::get(EmitLoadOfScalar(lvalue, loc));
3253226633Sdim  }
3254249423Sdim  llvm_unreachable("bad evaluation kind");
3255226633Sdim}
3256226633Sdim
3257234353Sdimvoid CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
3258234353Sdim  assert(Val->getType()->isFPOrFPVectorTy());
3259234353Sdim  if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
3260234353Sdim    return;
3261234353Sdim
3262234982Sdim  llvm::MDBuilder MDHelper(getLLVMContext());
3263234982Sdim  llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
3264234353Sdim
3265234982Sdim  cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
3266234353Sdim}
3267234353Sdim
3268234353Sdimnamespace {
3269234353Sdim  struct LValueOrRValue {
3270234353Sdim    LValue LV;
3271234353Sdim    RValue RV;
3272234353Sdim  };
3273234353Sdim}
3274234353Sdim
3275234353Sdimstatic LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
3276234353Sdim                                           const PseudoObjectExpr *E,
3277234353Sdim                                           bool forLValue,
3278234353Sdim                                           AggValueSlot slot) {
3279249423Sdim  SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
3280234353Sdim
3281234353Sdim  // Find the result expression, if any.
3282234353Sdim  const Expr *resultExpr = E->getResultExpr();
3283234353Sdim  LValueOrRValue result;
3284234353Sdim
3285234353Sdim  for (PseudoObjectExpr::const_semantics_iterator
3286234353Sdim         i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
3287234353Sdim    const Expr *semantic = *i;
3288234353Sdim
3289234353Sdim    // If this semantic expression is an opaque value, bind it
3290234353Sdim    // to the result of its source expression.
3291234353Sdim    if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
3292234353Sdim
3293234353Sdim      // If this is the result expression, we may need to evaluate
3294234353Sdim      // directly into the slot.
3295234353Sdim      typedef CodeGenFunction::OpaqueValueMappingData OVMA;
3296234353Sdim      OVMA opaqueData;
3297234353Sdim      if (ov == resultExpr && ov->isRValue() && !forLValue &&
3298249423Sdim          CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) {
3299234353Sdim        CGF.EmitAggExpr(ov->getSourceExpr(), slot);
3300234353Sdim
3301234353Sdim        LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType());
3302234353Sdim        opaqueData = OVMA::bind(CGF, ov, LV);
3303234353Sdim        result.RV = slot.asRValue();
3304234353Sdim
3305234353Sdim      // Otherwise, emit as normal.
3306234353Sdim      } else {
3307234353Sdim        opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3308234353Sdim
3309234353Sdim        // If this is the result, also evaluate the result now.
3310234353Sdim        if (ov == resultExpr) {
3311234353Sdim          if (forLValue)
3312234353Sdim            result.LV = CGF.EmitLValue(ov);
3313234353Sdim          else
3314234353Sdim            result.RV = CGF.EmitAnyExpr(ov, slot);
3315234353Sdim        }
3316234353Sdim      }
3317234353Sdim
3318234353Sdim      opaques.push_back(opaqueData);
3319234353Sdim
3320234353Sdim    // Otherwise, if the expression is the result, evaluate it
3321234353Sdim    // and remember the result.
3322234353Sdim    } else if (semantic == resultExpr) {
3323234353Sdim      if (forLValue)
3324234353Sdim        result.LV = CGF.EmitLValue(semantic);
3325234353Sdim      else
3326234353Sdim        result.RV = CGF.EmitAnyExpr(semantic, slot);
3327234353Sdim
3328234353Sdim    // Otherwise, evaluate the expression in an ignored context.
3329234353Sdim    } else {
3330234353Sdim      CGF.EmitIgnoredExpr(semantic);
3331234353Sdim    }
3332234353Sdim  }
3333234353Sdim
3334234353Sdim  // Unbind all the opaques now.
3335234353Sdim  for (unsigned i = 0, e = opaques.size(); i != e; ++i)
3336234353Sdim    opaques[i].unbind(CGF);
3337234353Sdim
3338234353Sdim  return result;
3339234353Sdim}
3340234353Sdim
3341234353SdimRValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
3342234353Sdim                                               AggValueSlot slot) {
3343234353Sdim  return emitPseudoObjectExpr(*this, E, false, slot).RV;
3344234353Sdim}
3345234353Sdim
3346234353SdimLValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
3347234353Sdim  return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
3348234353Sdim}
3349