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