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