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