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