clang 22.0.0git
CGExpr.cpp
Go to the documentation of this file.
1//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit Expr nodes as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ABIInfoImpl.h"
14#include "CGCUDARuntime.h"
15#include "CGCXXABI.h"
16#include "CGCall.h"
17#include "CGCleanup.h"
18#include "CGDebugInfo.h"
19#include "CGHLSLRuntime.h"
20#include "CGObjCRuntime.h"
21#include "CGOpenMPRuntime.h"
22#include "CGRecordLayout.h"
23#include "CodeGenFunction.h"
24#include "CodeGenModule.h"
25#include "CodeGenPGO.h"
26#include "ConstantEmitter.h"
27#include "TargetInfo.h"
29#include "clang/AST/ASTLambda.h"
30#include "clang/AST/Attr.h"
31#include "clang/AST/DeclObjC.h"
33#include "clang/AST/NSAPI.h"
38#include "clang/Basic/Module.h"
40#include "llvm/ADT/STLExtras.h"
41#include "llvm/ADT/ScopeExit.h"
42#include "llvm/ADT/StringExtras.h"
43#include "llvm/IR/DataLayout.h"
44#include "llvm/IR/Intrinsics.h"
45#include "llvm/IR/LLVMContext.h"
46#include "llvm/IR/MDBuilder.h"
47#include "llvm/IR/MatrixBuilder.h"
48#include "llvm/Support/ConvertUTF.h"
49#include "llvm/Support/Endian.h"
50#include "llvm/Support/MathExtras.h"
51#include "llvm/Support/Path.h"
52#include "llvm/Support/xxhash.h"
53#include "llvm/Transforms/Utils/SanitizerStats.h"
54
55#include <numeric>
56#include <optional>
57#include <string>
58
59using namespace clang;
60using namespace CodeGen;
61
62namespace clang {
63// TODO: consider deprecating ClSanitizeGuardChecks; functionality is subsumed
64// by -fsanitize-skip-hot-cutoff
65llvm::cl::opt<bool> ClSanitizeGuardChecks(
66 "ubsan-guard-checks", llvm::cl::Optional,
67 llvm::cl::desc("Guard UBSAN checks with `llvm.allow.ubsan.check()`."));
68
69} // namespace clang
70
71//===--------------------------------------------------------------------===//
72// Defines for metadata
73//===--------------------------------------------------------------------===//
74
75// Those values are crucial to be the SAME as in ubsan runtime library.
77 /// An integer type.
78 TK_Integer = 0x0000,
79 /// A floating-point type.
80 TK_Float = 0x0001,
81 /// An _BitInt(N) type.
82 TK_BitInt = 0x0002,
83 /// Any other type. The value representation is unspecified.
84 TK_Unknown = 0xffff
85};
86
87//===--------------------------------------------------------------------===//
88// Miscellaneous Helper Methods
89//===--------------------------------------------------------------------===//
90
91static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID) {
92 switch (ID) {
93#define SANITIZER_CHECK(Enum, Name, Version, Msg) \
94 case SanitizerHandler::Enum: \
95 return Msg;
97#undef SANITIZER_CHECK
98 }
99 llvm_unreachable("unhandled switch case");
100}
101
102/// CreateTempAlloca - This creates a alloca and inserts it into the entry
103/// block.
106 const Twine &Name,
107 llvm::Value *ArraySize) {
108 auto Alloca = CreateTempAlloca(Ty, Name, ArraySize);
109 Alloca->setAlignment(Align.getAsAlign());
110 return RawAddress(Alloca, Ty, Align, KnownNonNull);
111}
112
113RawAddress CodeGenFunction::MaybeCastStackAddressSpace(RawAddress Alloca,
114 LangAS DestLangAS,
115 llvm::Value *ArraySize) {
116
117 llvm::Value *V = Alloca.getPointer();
118 // Alloca always returns a pointer in alloca address space, which may
119 // be different from the type defined by the language. For example,
120 // in C++ the auto variables are in the default address space. Therefore
121 // cast alloca to the default address space when necessary.
122
123 unsigned DestAddrSpace = getContext().getTargetAddressSpace(DestLangAS);
124 if (DestAddrSpace != Alloca.getAddressSpace()) {
125 llvm::IRBuilderBase::InsertPointGuard IPG(Builder);
126 // When ArraySize is nullptr, alloca is inserted at AllocaInsertPt,
127 // otherwise alloca is inserted at the current insertion point of the
128 // builder.
129 if (!ArraySize)
130 Builder.SetInsertPoint(getPostAllocaInsertPoint());
132 *this, V, getASTAllocaAddressSpace(), Builder.getPtrTy(DestAddrSpace),
133 /*IsNonNull=*/true);
134 }
135
136 return RawAddress(V, Alloca.getElementType(), Alloca.getAlignment(),
138}
139
141 CharUnits Align, const Twine &Name,
142 llvm::Value *ArraySize,
143 RawAddress *AllocaAddr) {
144 RawAddress Alloca = CreateTempAllocaWithoutCast(Ty, Align, Name, ArraySize);
145 if (AllocaAddr)
146 *AllocaAddr = Alloca;
147 return MaybeCastStackAddressSpace(Alloca, DestLangAS, ArraySize);
148}
149
150/// CreateTempAlloca - This creates an alloca and inserts it into the entry
151/// block if \p ArraySize is nullptr, otherwise inserts it at the current
152/// insertion point of the builder.
153llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
154 const Twine &Name,
155 llvm::Value *ArraySize) {
156 llvm::AllocaInst *Alloca;
157 if (ArraySize)
158 Alloca = Builder.CreateAlloca(Ty, ArraySize, Name);
159 else
160 Alloca =
161 new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
162 ArraySize, Name, AllocaInsertPt->getIterator());
163 if (SanOpts.Mask & SanitizerKind::Address) {
164 Alloca->addAnnotationMetadata({"alloca_name_altered", Name.str()});
165 }
166 if (Allocas) {
167 Allocas->Add(Alloca);
168 }
169 return Alloca;
170}
171
172/// CreateDefaultAlignTempAlloca - This creates an alloca with the
173/// default alignment of the corresponding LLVM type, which is *not*
174/// guaranteed to be related in any way to the expected alignment of
175/// an AST type that might have been lowered to Ty.
177 const Twine &Name) {
178 CharUnits Align =
179 CharUnits::fromQuantity(CGM.getDataLayout().getPrefTypeAlign(Ty));
180 return CreateTempAlloca(Ty, Align, Name);
181}
182
185 return CreateTempAlloca(ConvertType(Ty), Align, Name);
186}
187
189 RawAddress *Alloca) {
190 // FIXME: Should we prefer the preferred type alignment here?
191 return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name, Alloca);
192}
193
195 const Twine &Name,
196 RawAddress *Alloca) {
198 /*ArraySize=*/nullptr, Alloca);
199
200 if (Ty->isConstantMatrixType()) {
201 auto *ArrayTy = cast<llvm::ArrayType>(Result.getElementType());
202 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
203 ArrayTy->getNumElements());
204
205 Result = Address(Result.getPointer(), VectorTy, Result.getAlignment(),
207 }
208 return Result;
209}
210
212 CharUnits Align,
213 const Twine &Name) {
214 return CreateTempAllocaWithoutCast(ConvertTypeForMem(Ty), Align, Name);
215}
216
218 const Twine &Name) {
219 return CreateMemTempWithoutCast(Ty, getContext().getTypeAlignInChars(Ty),
220 Name);
221}
222
223/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
224/// expression and compare the result against zero, returning an Int1Ty value.
226 PGO->setCurrentStmt(E);
227 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
228 llvm::Value *MemPtr = EmitScalarExpr(E);
229 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
230 }
231
232 QualType BoolTy = getContext().BoolTy;
233 SourceLocation Loc = E->getExprLoc();
234 CGFPOptionsRAII FPOptsRAII(*this, E);
235 if (!E->getType()->isAnyComplexType())
236 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc);
237
239 Loc);
240}
241
242/// EmitIgnoredExpr - Emit code to compute the specified expression,
243/// ignoring the result.
245 if (E->isPRValue())
246 return (void)EmitAnyExpr(E, AggValueSlot::ignored(), true);
247
248 // if this is a bitfield-resulting conditional operator, we can special case
249 // emit this. The normal 'EmitLValue' version of this is particularly
250 // difficult to codegen for, since creating a single "LValue" for two
251 // different sized arguments here is not particularly doable.
252 if (const auto *CondOp = dyn_cast<AbstractConditionalOperator>(
254 if (CondOp->getObjectKind() == OK_BitField)
255 return EmitIgnoredConditionalOperator(CondOp);
256 }
257
258 // Just emit it as an l-value and drop the result.
259 EmitLValue(E);
260}
261
262/// EmitAnyExpr - Emit code to compute the specified expression which
263/// can have any type. The result is returned as an RValue struct.
264/// If this is an aggregate expression, AggSlot indicates where the
265/// result should be returned.
267 AggValueSlot aggSlot,
268 bool ignoreResult) {
269 switch (getEvaluationKind(E->getType())) {
270 case TEK_Scalar:
271 return RValue::get(EmitScalarExpr(E, ignoreResult));
272 case TEK_Complex:
273 return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
274 case TEK_Aggregate:
275 if (!ignoreResult && aggSlot.isIgnored())
276 aggSlot = CreateAggTemp(E->getType(), "agg-temp");
277 EmitAggExpr(E, aggSlot);
278 return aggSlot.asRValue();
279 }
280 llvm_unreachable("bad evaluation kind");
281}
282
283/// EmitAnyExprToTemp - Similar to EmitAnyExpr(), however, the result will
284/// always be accessible even if no aggregate location is provided.
287
289 AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
290 return EmitAnyExpr(E, AggSlot);
291}
292
293/// EmitAnyExprToMem - Evaluate an expression into a given memory
294/// location.
296 Address Location,
297 Qualifiers Quals,
298 bool IsInit) {
299 // FIXME: This function should take an LValue as an argument.
300 switch (getEvaluationKind(E->getType())) {
301 case TEK_Complex:
303 /*isInit*/ false);
304 return;
305
306 case TEK_Aggregate: {
307 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals,
312 return;
313 }
314
315 case TEK_Scalar: {
316 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
317 LValue LV = MakeAddrLValue(Location, E->getType());
319 return;
320 }
321 }
322 llvm_unreachable("bad evaluation kind");
323}
324
326 const Expr *E, LValue LV, AggValueSlot::IsZeroed_t IsZeroed) {
327 QualType Type = LV.getType();
328 switch (getEvaluationKind(Type)) {
329 case TEK_Complex:
330 EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
331 return;
332 case TEK_Aggregate:
336 AggValueSlot::MayOverlap, IsZeroed));
337 return;
338 case TEK_Scalar:
339 if (LV.isSimple())
340 EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false);
341 else
343 return;
344 }
345 llvm_unreachable("bad evaluation kind");
346}
347
348static void
350 const Expr *E, Address ReferenceTemporary) {
351 // Objective-C++ ARC:
352 // If we are binding a reference to a temporary that has ownership, we
353 // need to perform retain/release operations on the temporary.
354 //
355 // FIXME: This should be looking at E, not M.
356 if (auto Lifetime = M->getType().getObjCLifetime()) {
357 switch (Lifetime) {
360 // Carry on to normal cleanup handling.
361 break;
362
364 // Nothing to do; cleaned up by an autorelease pool.
365 return;
366
369 switch (StorageDuration Duration = M->getStorageDuration()) {
370 case SD_Static:
371 // Note: we intentionally do not register a cleanup to release
372 // the object on program termination.
373 return;
374
375 case SD_Thread:
376 // FIXME: We should probably register a cleanup in this case.
377 return;
378
379 case SD_Automatic:
383 if (Lifetime == Qualifiers::OCL_Strong) {
384 const ValueDecl *VD = M->getExtendingDecl();
385 bool Precise = isa_and_nonnull<VarDecl>(VD) &&
386 VD->hasAttr<ObjCPreciseLifetimeAttr>();
390 } else {
391 // __weak objects always get EH cleanups; otherwise, exceptions
392 // could cause really nasty crashes instead of mere leaks.
395 }
396 if (Duration == SD_FullExpression)
397 CGF.pushDestroy(CleanupKind, ReferenceTemporary,
398 M->getType(), *Destroy,
400 else
401 CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
402 M->getType(),
403 *Destroy, CleanupKind & EHCleanup);
404 return;
405
406 case SD_Dynamic:
407 llvm_unreachable("temporary cannot have dynamic storage duration");
408 }
409 llvm_unreachable("unknown storage duration");
410 }
411 }
412
414 if (DK != QualType::DK_none) {
415 switch (M->getStorageDuration()) {
416 case SD_Static:
417 case SD_Thread: {
418 CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
419 if (const auto *ClassDecl =
421 ClassDecl && !ClassDecl->hasTrivialDestructor())
422 // Get the destructor for the reference temporary.
423 ReferenceTemporaryDtor = ClassDecl->getDestructor();
424
425 if (!ReferenceTemporaryDtor)
426 return;
427
428 llvm::FunctionCallee CleanupFn;
429 llvm::Constant *CleanupArg;
430 if (E->getType()->isArrayType()) {
432 ReferenceTemporary, E->getType(), CodeGenFunction::destroyCXXObject,
433 CGF.getLangOpts().Exceptions,
434 dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
435 CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
436 } else {
437 CleanupFn = CGF.CGM.getAddrAndTypeOfCXXStructor(
438 GlobalDecl(ReferenceTemporaryDtor, Dtor_Complete));
439 CleanupArg =
440 cast<llvm::Constant>(ReferenceTemporary.emitRawPointer(CGF));
441 }
443 CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
444 } break;
446 CGF.pushDestroy(DK, ReferenceTemporary, E->getType());
447 break;
448 case SD_Automatic:
449 CGF.pushLifetimeExtendedDestroy(DK, ReferenceTemporary, E->getType());
450 break;
451 case SD_Dynamic:
452 llvm_unreachable("temporary cannot have dynamic storage duration");
453 }
454 }
455}
456
459 const Expr *Inner,
460 RawAddress *Alloca = nullptr) {
461 auto &TCG = CGF.getTargetHooks();
462 switch (M->getStorageDuration()) {
464 case SD_Automatic: {
465 // If we have a constant temporary array or record try to promote it into a
466 // constant global under the same rules a normal constant would've been
467 // promoted. This is easier on the optimizer and generally emits fewer
468 // instructions.
469 QualType Ty = Inner->getType();
470 if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
471 (Ty->isArrayType() || Ty->isRecordType()) &&
472 Ty.isConstantStorage(CGF.getContext(), true, false))
473 if (auto Init = ConstantEmitter(CGF).tryEmitAbstract(Inner, Ty)) {
474 auto AS = CGF.CGM.GetGlobalConstantAddressSpace();
475 auto *GV = new llvm::GlobalVariable(
476 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
477 llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp", nullptr,
478 llvm::GlobalValue::NotThreadLocal,
480 CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty);
481 GV->setAlignment(alignment.getAsAlign());
482 llvm::Constant *C = GV;
483 if (AS != LangAS::Default)
484 C = TCG.performAddrSpaceCast(
485 CGF.CGM, GV, AS,
486 llvm::PointerType::get(
487 CGF.getLLVMContext(),
489 // FIXME: Should we put the new global into a COMDAT?
490 return RawAddress(C, GV->getValueType(), alignment);
491 }
492 return CGF.CreateMemTemp(Ty, "ref.tmp", Alloca);
493 }
494 case SD_Thread:
495 case SD_Static:
496 return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
497
498 case SD_Dynamic:
499 llvm_unreachable("temporary can't have dynamic storage duration");
500 }
501 llvm_unreachable("unknown storage duration");
502}
503
504/// Helper method to check if the underlying ABI is AAPCS
505static bool isAAPCS(const TargetInfo &TargetInfo) {
506 return TargetInfo.getABI().starts_with("aapcs");
507}
508
511 const Expr *E = M->getSubExpr();
512
513 assert((!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) ||
514 !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) &&
515 "Reference should never be pseudo-strong!");
516
517 // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so
518 // as that will cause the lifetime adjustment to be lost for ARC
519 auto ownership = M->getType().getObjCLifetime();
520 if (ownership != Qualifiers::OCL_None &&
521 ownership != Qualifiers::OCL_ExplicitNone) {
522 RawAddress Object = createReferenceTemporary(*this, M, E);
523 if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) {
524 llvm::Type *Ty = ConvertTypeForMem(E->getType());
525 Object = Object.withElementType(Ty);
526
527 // createReferenceTemporary will promote the temporary to a global with a
528 // constant initializer if it can. It can only do this to a value of
529 // ARC-manageable type if the value is global and therefore "immune" to
530 // ref-counting operations. Therefore we have no need to emit either a
531 // dynamic initialization or a cleanup and we can just return the address
532 // of the temporary.
533 if (Var->hasInitializer())
534 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
535
536 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
537 }
538 LValue RefTempDst = MakeAddrLValue(Object, M->getType(),
540
541 switch (getEvaluationKind(E->getType())) {
542 default: llvm_unreachable("expected scalar or aggregate expression");
543 case TEK_Scalar:
544 EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
545 break;
546 case TEK_Aggregate: {
548 E->getType().getQualifiers(),
553 break;
554 }
555 }
556
557 pushTemporaryCleanup(*this, M, E, Object);
558 return RefTempDst;
559 }
560
563 E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
564
565 for (const auto &Ignored : CommaLHSs)
566 EmitIgnoredExpr(Ignored);
567
568 if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
569 if (opaque->getType()->isRecordType()) {
570 assert(Adjustments.empty());
571 return EmitOpaqueValueLValue(opaque);
572 }
573 }
574
575 // Create and initialize the reference temporary.
576 RawAddress Alloca = Address::invalid();
577 RawAddress Object = createReferenceTemporary(*this, M, E, &Alloca);
578 if (auto *Var = dyn_cast<llvm::GlobalVariable>(
579 Object.getPointer()->stripPointerCasts())) {
580 llvm::Type *TemporaryType = ConvertTypeForMem(E->getType());
581 Object = Object.withElementType(TemporaryType);
582 // If the temporary is a global and has a constant initializer or is a
583 // constant temporary that we promoted to a global, we may have already
584 // initialized it.
585 if (!Var->hasInitializer()) {
586 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
588 if (RefType.getPointerAuth()) {
589 // Use the qualifier of the reference temporary to sign the pointer.
590 LValue LV = MakeRawAddrLValue(Object.getPointer(), RefType,
591 Object.getAlignment());
592 EmitScalarInit(E, M->getExtendingDecl(), LV, false);
593 } else {
594 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/ true);
595 }
596 }
597 } else {
598 switch (M->getStorageDuration()) {
599 case SD_Automatic:
600 if (EmitLifetimeStart(Alloca.getPointer())) {
602 Alloca);
603 }
604 break;
605
606 case SD_FullExpression: {
607 if (!ShouldEmitLifetimeMarkers)
608 break;
609
610 // Avoid creating a conditional cleanup just to hold an llvm.lifetime.end
611 // marker. Instead, start the lifetime of a conditional temporary earlier
612 // so that it's unconditional. Don't do this with sanitizers which need
613 // more precise lifetime marks. However when inside an "await.suspend"
614 // block, we should always avoid conditional cleanup because it creates
615 // boolean marker that lives across await_suspend, which can destroy coro
616 // frame.
617 ConditionalEvaluation *OldConditional = nullptr;
618 CGBuilderTy::InsertPoint OldIP;
620 ((!SanOpts.has(SanitizerKind::HWAddress) &&
621 !SanOpts.has(SanitizerKind::Memory) &&
622 !CGM.getCodeGenOpts().SanitizeAddressUseAfterScope) ||
623 inSuspendBlock())) {
624 OldConditional = OutermostConditional;
625 OutermostConditional = nullptr;
626
627 OldIP = Builder.saveIP();
628 llvm::BasicBlock *Block = OldConditional->getStartingBlock();
629 Builder.restoreIP(CGBuilderTy::InsertPoint(
630 Block, llvm::BasicBlock::iterator(Block->back())));
631 }
632
633 if (EmitLifetimeStart(Alloca.getPointer())) {
635 }
636
637 if (OldConditional) {
638 OutermostConditional = OldConditional;
639 Builder.restoreIP(OldIP);
640 }
641 break;
642 }
643
644 default:
645 break;
646 }
647 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
648 }
649 pushTemporaryCleanup(*this, M, E, Object);
650
651 // Perform derived-to-base casts and/or field accesses, to get from the
652 // temporary object we created (and, potentially, for which we extended
653 // the lifetime) to the subobject we're binding the reference to.
654 for (SubobjectAdjustment &Adjustment : llvm::reverse(Adjustments)) {
655 switch (Adjustment.Kind) {
657 Object =
658 GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
659 Adjustment.DerivedToBase.BasePath->path_begin(),
660 Adjustment.DerivedToBase.BasePath->path_end(),
661 /*NullCheckValue=*/ false, E->getExprLoc());
662 break;
663
666 LV = EmitLValueForField(LV, Adjustment.Field);
667 assert(LV.isSimple() &&
668 "materialized temporary field is not a simple lvalue");
669 Object = LV.getAddress();
670 break;
671 }
672
674 llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
676 E, Object, Ptr, Adjustment.Ptr.MPT, /*IsInBounds=*/true);
677 break;
678 }
679 }
680 }
681
682 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
683}
684
685RValue
687 // Emit the expression as an lvalue.
688 LValue LV = EmitLValue(E);
689 assert(LV.isSimple());
690 llvm::Value *Value = LV.getPointer(*this);
691
693 // C++11 [dcl.ref]p5 (as amended by core issue 453):
694 // If a glvalue to which a reference is directly bound designates neither
695 // an existing object or function of an appropriate type nor a region of
696 // storage of suitable size and alignment to contain an object of the
697 // reference's type, the behavior is undefined.
698 QualType Ty = E->getType();
700 }
701
702 return RValue::get(Value);
703}
704
705
706/// getAccessedFieldNo - Given an encoded value and a result number, return the
707/// input field number being accessed.
709 const llvm::Constant *Elts) {
710 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
711 ->getZExtValue();
712}
713
714static llvm::Value *emitHashMix(CGBuilderTy &Builder, llvm::Value *Acc,
715 llvm::Value *Ptr) {
716 llvm::Value *A0 =
717 Builder.CreateMul(Ptr, Builder.getInt64(0xbf58476d1ce4e5b9u));
718 llvm::Value *A1 =
719 Builder.CreateXor(A0, Builder.CreateLShr(A0, Builder.getInt64(31)));
720 return Builder.CreateXor(Acc, A1);
721}
722
727
730 return (RD && RD->hasDefinition() && RD->isDynamicClass()) &&
731 (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
734}
735
737 return SanOpts.has(SanitizerKind::Null) ||
738 SanOpts.has(SanitizerKind::Alignment) ||
739 SanOpts.has(SanitizerKind::ObjectSize) ||
740 SanOpts.has(SanitizerKind::Vptr);
741}
742
744 llvm::Value *Ptr, QualType Ty,
745 CharUnits Alignment,
746 SanitizerSet SkippedChecks,
747 llvm::Value *ArraySize) {
749 return;
750
751 // Don't check pointers outside the default address space. The null check
752 // isn't correct, the object-size check isn't supported by LLVM, and we can't
753 // communicate the addresses to the runtime handler for the vptr check.
754 if (Ptr->getType()->getPointerAddressSpace())
755 return;
756
757 // Don't check pointers to volatile data. The behavior here is implementation-
758 // defined.
759 if (Ty.isVolatileQualified())
760 return;
761
762 // Quickly determine whether we have a pointer to an alloca. It's possible
763 // to skip null checks, and some alignment checks, for these pointers. This
764 // can reduce compile-time significantly.
765 auto PtrToAlloca = dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCasts());
766
767 llvm::Value *IsNonNull = nullptr;
768 bool IsGuaranteedNonNull =
769 SkippedChecks.has(SanitizerKind::Null) || PtrToAlloca;
770
771 llvm::BasicBlock *Done = nullptr;
772 bool DoneViaNullSanitize = false;
773
774 {
775 auto CheckHandler = SanitizerHandler::TypeMismatch;
776 SanitizerDebugLocation SanScope(this,
777 {SanitizerKind::SO_Null,
778 SanitizerKind::SO_ObjectSize,
779 SanitizerKind::SO_Alignment},
780 CheckHandler);
781
783 Checks;
784
785 llvm::Value *True = llvm::ConstantInt::getTrue(getLLVMContext());
786 bool AllowNullPointers = isNullPointerAllowed(TCK);
787 if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) &&
788 !IsGuaranteedNonNull) {
789 // The glvalue must not be an empty glvalue.
790 IsNonNull = Builder.CreateIsNotNull(Ptr);
791
792 // The IR builder can constant-fold the null check if the pointer points
793 // to a constant.
794 IsGuaranteedNonNull = IsNonNull == True;
795
796 // Skip the null check if the pointer is known to be non-null.
797 if (!IsGuaranteedNonNull) {
798 if (AllowNullPointers) {
799 // When performing pointer casts, it's OK if the value is null.
800 // Skip the remaining checks in that case.
801 Done = createBasicBlock("null");
802 DoneViaNullSanitize = true;
803 llvm::BasicBlock *Rest = createBasicBlock("not.null");
804 Builder.CreateCondBr(IsNonNull, Rest, Done);
805 EmitBlock(Rest);
806 } else {
807 Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::SO_Null));
808 }
809 }
810 }
811
812 if (SanOpts.has(SanitizerKind::ObjectSize) &&
813 !SkippedChecks.has(SanitizerKind::ObjectSize) &&
814 !Ty->isIncompleteType()) {
815 uint64_t TySize = CGM.getMinimumObjectSize(Ty).getQuantity();
816 llvm::Value *Size = llvm::ConstantInt::get(IntPtrTy, TySize);
817 if (ArraySize)
818 Size = Builder.CreateMul(Size, ArraySize);
819
820 // Degenerate case: new X[0] does not need an objectsize check.
821 llvm::Constant *ConstantSize = dyn_cast<llvm::Constant>(Size);
822 if (!ConstantSize || !ConstantSize->isNullValue()) {
823 // The glvalue must refer to a large enough storage region.
824 // FIXME: If Address Sanitizer is enabled, insert dynamic
825 // instrumentation
826 // to check this.
827 // FIXME: Get object address space
828 llvm::Type *Tys[2] = {IntPtrTy, Int8PtrTy};
829 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
830 llvm::Value *Min = Builder.getFalse();
831 llvm::Value *NullIsUnknown = Builder.getFalse();
832 llvm::Value *Dynamic = Builder.getFalse();
833 llvm::Value *LargeEnough = Builder.CreateICmpUGE(
834 Builder.CreateCall(F, {Ptr, Min, NullIsUnknown, Dynamic}), Size);
835 Checks.push_back(
836 std::make_pair(LargeEnough, SanitizerKind::SO_ObjectSize));
837 }
838 }
839
840 llvm::MaybeAlign AlignVal;
841 llvm::Value *PtrAsInt = nullptr;
842
843 if (SanOpts.has(SanitizerKind::Alignment) &&
844 !SkippedChecks.has(SanitizerKind::Alignment)) {
845 AlignVal = Alignment.getAsMaybeAlign();
846 if (!Ty->isIncompleteType() && !AlignVal)
847 AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr,
848 /*ForPointeeType=*/true)
849 .getAsMaybeAlign();
850
851 // The glvalue must be suitably aligned.
852 if (AlignVal && *AlignVal > llvm::Align(1) &&
853 (!PtrToAlloca || PtrToAlloca->getAlign() < *AlignVal)) {
854 PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
855 llvm::Value *Align = Builder.CreateAnd(
856 PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal->value() - 1));
857 llvm::Value *Aligned =
858 Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
859 if (Aligned != True)
860 Checks.push_back(
861 std::make_pair(Aligned, SanitizerKind::SO_Alignment));
862 }
863 }
864
865 if (Checks.size() > 0) {
866 llvm::Constant *StaticData[] = {
868 llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2(*AlignVal) : 1),
869 llvm::ConstantInt::get(Int8Ty, TCK)};
870 EmitCheck(Checks, CheckHandler, StaticData, PtrAsInt ? PtrAsInt : Ptr);
871 }
872 }
873
874 // If possible, check that the vptr indicates that there is a subobject of
875 // type Ty at offset zero within this object.
876 //
877 // C++11 [basic.life]p5,6:
878 // [For storage which does not refer to an object within its lifetime]
879 // The program has undefined behavior if:
880 // -- the [pointer or glvalue] is used to access a non-static data member
881 // or call a non-static member function
882 if (SanOpts.has(SanitizerKind::Vptr) &&
883 !SkippedChecks.has(SanitizerKind::Vptr) && isVptrCheckRequired(TCK, Ty)) {
884 SanitizerDebugLocation SanScope(this, {SanitizerKind::SO_Vptr},
885 SanitizerHandler::DynamicTypeCacheMiss);
886
887 // Ensure that the pointer is non-null before loading it. If there is no
888 // compile-time guarantee, reuse the run-time null check or emit a new one.
889 if (!IsGuaranteedNonNull) {
890 if (!IsNonNull)
891 IsNonNull = Builder.CreateIsNotNull(Ptr);
892 if (!Done)
893 Done = createBasicBlock("vptr.null");
894 llvm::BasicBlock *VptrNotNull = createBasicBlock("vptr.not.null");
895 Builder.CreateCondBr(IsNonNull, VptrNotNull, Done);
896 EmitBlock(VptrNotNull);
897 }
898
899 // Compute a deterministic hash of the mangled name of the type.
900 SmallString<64> MangledName;
901 llvm::raw_svector_ostream Out(MangledName);
902 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
903 Out);
904
905 // Contained in NoSanitizeList based on the mangled type.
906 if (!CGM.getContext().getNoSanitizeList().containsType(SanitizerKind::Vptr,
907 Out.str())) {
908 // Load the vptr, and mix it with TypeHash.
909 llvm::Value *TypeHash =
910 llvm::ConstantInt::get(Int64Ty, xxh3_64bits(Out.str()));
911
912 llvm::Type *VPtrTy = llvm::PointerType::get(getLLVMContext(), 0);
913 Address VPtrAddr(Ptr, IntPtrTy, getPointerAlign());
914 llvm::Value *VPtrVal = GetVTablePtr(VPtrAddr, VPtrTy,
915 Ty->getAsCXXRecordDecl(),
917 VPtrVal = Builder.CreateBitOrPointerCast(VPtrVal, IntPtrTy);
918
919 llvm::Value *Hash =
920 emitHashMix(Builder, TypeHash, Builder.CreateZExt(VPtrVal, Int64Ty));
921 Hash = Builder.CreateTrunc(Hash, IntPtrTy);
922
923 // Look the hash up in our cache.
924 const int CacheSize = 128;
925 llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
926 llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
927 "__ubsan_vptr_type_cache");
928 llvm::Value *Slot = Builder.CreateAnd(Hash,
929 llvm::ConstantInt::get(IntPtrTy,
930 CacheSize-1));
931 llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
932 llvm::Value *CacheVal = Builder.CreateAlignedLoad(
933 IntPtrTy, Builder.CreateInBoundsGEP(HashTable, Cache, Indices),
935
936 // If the hash isn't in the cache, call a runtime handler to perform the
937 // hard work of checking whether the vptr is for an object of the right
938 // type. This will either fill in the cache and return, or produce a
939 // diagnostic.
940 llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash);
941 llvm::Constant *StaticData[] = {
944 CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
945 llvm::ConstantInt::get(Int8Ty, TCK)
946 };
947 llvm::Value *DynamicData[] = { Ptr, Hash };
948 EmitCheck(std::make_pair(EqualHash, SanitizerKind::SO_Vptr),
949 SanitizerHandler::DynamicTypeCacheMiss, StaticData,
950 DynamicData);
951 }
952 }
953
954 if (Done) {
955 SanitizerDebugLocation SanScope(
956 this,
957 {DoneViaNullSanitize ? SanitizerKind::SO_Null : SanitizerKind::SO_Vptr},
958 DoneViaNullSanitize ? SanitizerHandler::TypeMismatch
959 : SanitizerHandler::DynamicTypeCacheMiss);
960 Builder.CreateBr(Done);
961 EmitBlock(Done);
962 }
963}
964
966 QualType EltTy) {
968 uint64_t EltSize = C.getTypeSizeInChars(EltTy).getQuantity();
969 if (!EltSize)
970 return nullptr;
971
972 auto *ArrayDeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
973 if (!ArrayDeclRef)
974 return nullptr;
975
976 auto *ParamDecl = dyn_cast<ParmVarDecl>(ArrayDeclRef->getDecl());
977 if (!ParamDecl)
978 return nullptr;
979
980 auto *POSAttr = ParamDecl->getAttr<PassObjectSizeAttr>();
981 if (!POSAttr)
982 return nullptr;
983
984 // Don't load the size if it's a lower bound.
985 int POSType = POSAttr->getType();
986 if (POSType != 0 && POSType != 1)
987 return nullptr;
988
989 // Find the implicit size parameter.
990 auto PassedSizeIt = SizeArguments.find(ParamDecl);
991 if (PassedSizeIt == SizeArguments.end())
992 return nullptr;
993
994 const ImplicitParamDecl *PassedSizeDecl = PassedSizeIt->second;
995 assert(LocalDeclMap.count(PassedSizeDecl) && "Passed size not loadable");
996 Address AddrOfSize = LocalDeclMap.find(PassedSizeDecl)->second;
997 llvm::Value *SizeInBytes = EmitLoadOfScalar(AddrOfSize, /*Volatile=*/false,
998 C.getSizeType(), E->getExprLoc());
999 llvm::Value *SizeOfElement =
1000 llvm::ConstantInt::get(SizeInBytes->getType(), EltSize);
1001 return Builder.CreateUDiv(SizeInBytes, SizeOfElement);
1002}
1003
1004/// If Base is known to point to the start of an array, return the length of
1005/// that array. Return 0 if the length cannot be determined.
1007 const Expr *Base,
1008 QualType &IndexedType,
1010 StrictFlexArraysLevel) {
1011 // For the vector indexing extension, the bound is the number of elements.
1012 if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
1013 IndexedType = Base->getType();
1014 return CGF.Builder.getInt32(VT->getNumElements());
1015 }
1016
1017 Base = Base->IgnoreParens();
1018
1019 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
1020 if (CE->getCastKind() == CK_ArrayToPointerDecay &&
1021 !CE->getSubExpr()->isFlexibleArrayMemberLike(CGF.getContext(),
1022 StrictFlexArraysLevel)) {
1023 CodeGenFunction::SanitizerScope SanScope(&CGF);
1024
1025 IndexedType = CE->getSubExpr()->getType();
1026 const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
1027 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
1028 return CGF.Builder.getInt(CAT->getSize());
1029
1030 if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
1031 return CGF.getVLASize(VAT).NumElts;
1032 // Ignore pass_object_size here. It's not applicable on decayed pointers.
1033 }
1034 }
1035
1036 CodeGenFunction::SanitizerScope SanScope(&CGF);
1037
1038 QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0};
1039 if (llvm::Value *POS = CGF.LoadPassedObjectSize(Base, EltTy)) {
1040 IndexedType = Base->getType();
1041 return POS;
1042 }
1043
1044 return nullptr;
1045}
1046
1047namespace {
1048
1049/// \p StructAccessBase returns the base \p Expr of a field access. It returns
1050/// either a \p DeclRefExpr, representing the base pointer to the struct, i.e.:
1051///
1052/// p in p-> a.b.c
1053///
1054/// or a \p MemberExpr, if the \p MemberExpr has the \p RecordDecl we're
1055/// looking for:
1056///
1057/// struct s {
1058/// struct s *ptr;
1059/// int count;
1060/// char array[] __attribute__((counted_by(count)));
1061/// };
1062///
1063/// If we have an expression like \p p->ptr->array[index], we want the
1064/// \p MemberExpr for \p p->ptr instead of \p p.
1065class StructAccessBase
1066 : public ConstStmtVisitor<StructAccessBase, const Expr *> {
1067 const RecordDecl *ExpectedRD;
1068
1069 bool IsExpectedRecordDecl(const Expr *E) const {
1070 QualType Ty = E->getType();
1071 if (Ty->isPointerType())
1072 Ty = Ty->getPointeeType();
1073 return ExpectedRD == Ty->getAsRecordDecl();
1074 }
1075
1076public:
1077 StructAccessBase(const RecordDecl *ExpectedRD) : ExpectedRD(ExpectedRD) {}
1078
1079 //===--------------------------------------------------------------------===//
1080 // Visitor Methods
1081 //===--------------------------------------------------------------------===//
1082
1083 // NOTE: If we build C++ support for counted_by, then we'll have to handle
1084 // horrors like this:
1085 //
1086 // struct S {
1087 // int x, y;
1088 // int blah[] __attribute__((counted_by(x)));
1089 // } s;
1090 //
1091 // int foo(int index, int val) {
1092 // int (S::*IHatePMDs)[] = &S::blah;
1093 // (s.*IHatePMDs)[index] = val;
1094 // }
1095
1096 const Expr *Visit(const Expr *E) {
1097 return ConstStmtVisitor<StructAccessBase, const Expr *>::Visit(E);
1098 }
1099
1100 const Expr *VisitStmt(const Stmt *S) { return nullptr; }
1101
1102 // These are the types we expect to return (in order of most to least
1103 // likely):
1104 //
1105 // 1. DeclRefExpr - This is the expression for the base of the structure.
1106 // It's exactly what we want to build an access to the \p counted_by
1107 // field.
1108 // 2. MemberExpr - This is the expression that has the same \p RecordDecl
1109 // as the flexble array member's lexical enclosing \p RecordDecl. This
1110 // allows us to catch things like: "p->p->array"
1111 // 3. CompoundLiteralExpr - This is for people who create something
1112 // heretical like (struct foo has a flexible array member):
1113 //
1114 // (struct foo){ 1, 2 }.blah[idx];
1115 const Expr *VisitDeclRefExpr(const DeclRefExpr *E) {
1116 return IsExpectedRecordDecl(E) ? E : nullptr;
1117 }
1118 const Expr *VisitMemberExpr(const MemberExpr *E) {
1119 if (IsExpectedRecordDecl(E) && E->isArrow())
1120 return E;
1121 const Expr *Res = Visit(E->getBase());
1122 return !Res && IsExpectedRecordDecl(E) ? E : Res;
1123 }
1124 const Expr *VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1125 return IsExpectedRecordDecl(E) ? E : nullptr;
1126 }
1127 const Expr *VisitCallExpr(const CallExpr *E) {
1128 return IsExpectedRecordDecl(E) ? E : nullptr;
1129 }
1130
1131 const Expr *VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1132 if (IsExpectedRecordDecl(E))
1133 return E;
1134 return Visit(E->getBase());
1135 }
1136 const Expr *VisitCastExpr(const CastExpr *E) {
1137 if (E->getCastKind() == CK_LValueToRValue)
1138 return IsExpectedRecordDecl(E) ? E : nullptr;
1139 return Visit(E->getSubExpr());
1140 }
1141 const Expr *VisitParenExpr(const ParenExpr *E) {
1142 return Visit(E->getSubExpr());
1143 }
1144 const Expr *VisitUnaryAddrOf(const UnaryOperator *E) {
1145 return Visit(E->getSubExpr());
1146 }
1147 const Expr *VisitUnaryDeref(const UnaryOperator *E) {
1148 return Visit(E->getSubExpr());
1149 }
1150};
1151
1152} // end anonymous namespace
1153
1155
1157 const FieldDecl *Field,
1158 RecIndicesTy &Indices) {
1159 const CGRecordLayout &Layout = CGF.CGM.getTypes().getCGRecordLayout(RD);
1160 int64_t FieldNo = -1;
1161 for (const FieldDecl *FD : RD->fields()) {
1162 if (!Layout.containsFieldDecl(FD))
1163 // This could happen if the field has a struct type that's empty. I don't
1164 // know why either.
1165 continue;
1166
1167 FieldNo = Layout.getLLVMFieldNo(FD);
1168 if (FD == Field) {
1169 Indices.emplace_back(CGF.Builder.getInt32(FieldNo));
1170 return true;
1171 }
1172
1173 QualType Ty = FD->getType();
1174 if (Ty->isRecordType()) {
1175 if (getGEPIndicesToField(CGF, Ty->getAsRecordDecl(), Field, Indices)) {
1176 if (RD->isUnion())
1177 FieldNo = 0;
1178 Indices.emplace_back(CGF.Builder.getInt32(FieldNo));
1179 return true;
1180 }
1181 }
1182 }
1183
1184 return false;
1185}
1186
1188 const Expr *Base, const FieldDecl *FAMDecl, const FieldDecl *CountDecl) {
1189 const RecordDecl *RD = CountDecl->getParent()->getOuterLexicalRecordContext();
1190
1191 // Find the base struct expr (i.e. p in p->a.b.c.d).
1192 const Expr *StructBase = StructAccessBase(RD).Visit(Base);
1193 if (!StructBase || StructBase->HasSideEffects(getContext()))
1194 return nullptr;
1195
1196 llvm::Value *Res = nullptr;
1197 if (StructBase->getType()->isPointerType()) {
1198 LValueBaseInfo BaseInfo;
1199 TBAAAccessInfo TBAAInfo;
1200 Address Addr = EmitPointerWithAlignment(StructBase, &BaseInfo, &TBAAInfo);
1201 Res = Addr.emitRawPointer(*this);
1202 } else if (StructBase->isLValue()) {
1203 LValue LV = EmitLValue(StructBase);
1204 Address Addr = LV.getAddress();
1205 Res = Addr.emitRawPointer(*this);
1206 } else {
1207 return nullptr;
1208 }
1209
1210 RecIndicesTy Indices;
1211 getGEPIndicesToField(*this, RD, CountDecl, Indices);
1212 if (Indices.empty())
1213 return nullptr;
1214
1215 Indices.push_back(Builder.getInt32(0));
1216 CanQualType T = CGM.getContext().getCanonicalTagType(RD);
1217 return Builder.CreateInBoundsGEP(ConvertType(T), Res,
1218 RecIndicesTy(llvm::reverse(Indices)),
1219 "counted_by.gep");
1220}
1221
1222/// This method is typically called in contexts where we can't generate
1223/// side-effects, like in __builtin_dynamic_object_size. When finding
1224/// expressions, only choose those that have either already been emitted or can
1225/// be loaded without side-effects.
1226///
1227/// - \p FAMDecl: the \p Decl for the flexible array member. It may not be
1228/// within the top-level struct.
1229/// - \p CountDecl: must be within the same non-anonymous struct as \p FAMDecl.
1231 const Expr *Base, const FieldDecl *FAMDecl, const FieldDecl *CountDecl) {
1232 if (llvm::Value *GEP = GetCountedByFieldExprGEP(Base, FAMDecl, CountDecl))
1233 return Builder.CreateAlignedLoad(ConvertType(CountDecl->getType()), GEP,
1234 getIntAlign(), "counted_by.load");
1235 return nullptr;
1236}
1237
1239 llvm::Value *Index, QualType IndexType,
1240 bool Accessed) {
1241 assert(SanOpts.has(SanitizerKind::ArrayBounds) &&
1242 "should not be called unless adding bounds checks");
1243 const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
1244 getLangOpts().getStrictFlexArraysLevel();
1245 QualType IndexedType;
1246 llvm::Value *Bound =
1247 getArrayIndexingBound(*this, Base, IndexedType, StrictFlexArraysLevel);
1248
1249 EmitBoundsCheckImpl(E, Bound, Index, IndexType, IndexedType, Accessed);
1250}
1251
1252void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
1253 llvm::Value *Index,
1254 QualType IndexType,
1255 QualType IndexedType, bool Accessed) {
1256 if (!Bound)
1257 return;
1258
1259 auto CheckKind = SanitizerKind::SO_ArrayBounds;
1260 auto CheckHandler = SanitizerHandler::OutOfBounds;
1261 SanitizerDebugLocation SanScope(this, {CheckKind}, CheckHandler);
1262
1263 bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
1264 llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
1265 llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
1266
1267 llvm::Constant *StaticData[] = {
1269 EmitCheckTypeDescriptor(IndexedType),
1270 EmitCheckTypeDescriptor(IndexType)
1271 };
1272 llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
1273 : Builder.CreateICmpULE(IndexVal, BoundVal);
1274 EmitCheck(std::make_pair(Check, CheckKind), CheckHandler, StaticData, Index);
1275}
1276
1278 auto ATMD = infer_alloc::getAllocTokenMetadata(AllocType, getContext());
1279 if (!ATMD)
1280 return nullptr;
1281
1282 llvm::MDBuilder MDB(getLLVMContext());
1283 auto *TypeNameMD = MDB.createString(ATMD->TypeName);
1284 auto *ContainsPtrC = Builder.getInt1(ATMD->ContainsPointer);
1285 auto *ContainsPtrMD = MDB.createConstant(ContainsPtrC);
1286
1287 // Format: !{<type-name>, <contains-pointer>}
1288 return llvm::MDNode::get(CGM.getLLVMContext(), {TypeNameMD, ContainsPtrMD});
1289}
1290
1291void CodeGenFunction::EmitAllocToken(llvm::CallBase *CB, QualType AllocType) {
1292 assert(SanOpts.has(SanitizerKind::AllocToken) &&
1293 "Only needed with -fsanitize=alloc-token");
1294 CB->setMetadata(llvm::LLVMContext::MD_alloc_token,
1295 buildAllocToken(AllocType));
1296}
1297
1300 if (!AllocType.isNull())
1301 return buildAllocToken(AllocType);
1302 return nullptr;
1303}
1304
1305void CodeGenFunction::EmitAllocToken(llvm::CallBase *CB, const CallExpr *E) {
1306 assert(SanOpts.has(SanitizerKind::AllocToken) &&
1307 "Only needed with -fsanitize=alloc-token");
1308 if (llvm::MDNode *MDN = buildAllocToken(E))
1309 CB->setMetadata(llvm::LLVMContext::MD_alloc_token, MDN);
1310}
1311
1314 bool isInc, bool isPre) {
1315 ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
1316
1317 llvm::Value *NextVal;
1318 if (isa<llvm::IntegerType>(InVal.first->getType())) {
1319 uint64_t AmountVal = isInc ? 1 : -1;
1320 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
1321
1322 // Add the inc/dec to the real part.
1323 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1324 } else {
1325 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
1326 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
1327 if (!isInc)
1328 FVal.changeSign();
1329 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
1330
1331 // Add the inc/dec to the real part.
1332 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1333 }
1334
1335 ComplexPairTy IncVal(NextVal, InVal.second);
1336
1337 // Store the updated result through the lvalue.
1338 EmitStoreOfComplex(IncVal, LV, /*init*/ false);
1339 if (getLangOpts().OpenMP)
1340 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
1341 E->getSubExpr());
1342
1343 // If this is a postinc, return the value read from memory, otherwise use the
1344 // updated value.
1345 return isPre ? IncVal : InVal;
1346}
1347
1349 CodeGenFunction *CGF) {
1350 // Bind VLAs in the cast type.
1351 if (CGF && E->getType()->isVariablyModifiedType())
1353
1354 if (CGDebugInfo *DI = getModuleDebugInfo())
1355 DI->EmitExplicitCastType(E->getType());
1356}
1357
1358//===----------------------------------------------------------------------===//
1359// LValue Expression Emission
1360//===----------------------------------------------------------------------===//
1361
1362static CharUnits getArrayElementAlign(CharUnits arrayAlign, llvm::Value *idx,
1363 CharUnits eltSize) {
1364 // If we have a constant index, we can use the exact offset of the
1365 // element we're accessing.
1366 if (auto *constantIdx = dyn_cast<llvm::ConstantInt>(idx)) {
1367 CharUnits offset = constantIdx->getZExtValue() * eltSize;
1368 return arrayAlign.alignmentAtOffset(offset);
1369 }
1370
1371 // Otherwise, use the worst-case alignment for any element.
1372 return arrayAlign.alignmentOfArrayElement(eltSize);
1373}
1374
1375/// Emit pointer + index arithmetic.
1377 const BinaryOperator *BO,
1378 LValueBaseInfo *BaseInfo,
1379 TBAAAccessInfo *TBAAInfo,
1380 KnownNonNull_t IsKnownNonNull) {
1381 assert(BO->isAdditiveOp() && "Expect an addition or subtraction.");
1382 Expr *pointerOperand = BO->getLHS();
1383 Expr *indexOperand = BO->getRHS();
1384 bool isSubtraction = BO->getOpcode() == BO_Sub;
1385
1386 Address BaseAddr = Address::invalid();
1387 llvm::Value *index = nullptr;
1388 // In a subtraction, the LHS is always the pointer.
1389 // Note: do not change the evaluation order.
1390 if (!isSubtraction && !pointerOperand->getType()->isAnyPointerType()) {
1391 std::swap(pointerOperand, indexOperand);
1392 index = CGF.EmitScalarExpr(indexOperand);
1393 BaseAddr = CGF.EmitPointerWithAlignment(pointerOperand, BaseInfo, TBAAInfo,
1395 } else {
1396 BaseAddr = CGF.EmitPointerWithAlignment(pointerOperand, BaseInfo, TBAAInfo,
1398 index = CGF.EmitScalarExpr(indexOperand);
1399 }
1400
1401 llvm::Value *pointer = BaseAddr.getBasePointer();
1402 llvm::Value *Res = CGF.EmitPointerArithmetic(
1403 BO, pointerOperand, pointer, indexOperand, index, isSubtraction);
1404 QualType PointeeTy = BO->getType()->getPointeeType();
1405 CharUnits Align =
1407 CGF.getContext().getTypeSizeInChars(PointeeTy));
1408 return Address(Res, CGF.ConvertTypeForMem(PointeeTy), Align,
1410 /*Offset=*/nullptr, IsKnownNonNull);
1411}
1412
1414 TBAAAccessInfo *TBAAInfo,
1415 KnownNonNull_t IsKnownNonNull,
1416 CodeGenFunction &CGF) {
1417 // We allow this with ObjC object pointers because of fragile ABIs.
1418 assert(E->getType()->isPointerType() ||
1420 E = E->IgnoreParens();
1421
1422 // Casts:
1423 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1424 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE))
1425 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
1426
1427 switch (CE->getCastKind()) {
1428 // Non-converting casts (but not C's implicit conversion from void*).
1429 case CK_BitCast:
1430 case CK_NoOp:
1431 case CK_AddressSpaceConversion:
1432 if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) {
1433 if (PtrTy->getPointeeType()->isVoidType())
1434 break;
1435
1436 LValueBaseInfo InnerBaseInfo;
1437 TBAAAccessInfo InnerTBAAInfo;
1439 CE->getSubExpr(), &InnerBaseInfo, &InnerTBAAInfo, IsKnownNonNull);
1440 if (BaseInfo) *BaseInfo = InnerBaseInfo;
1441 if (TBAAInfo) *TBAAInfo = InnerTBAAInfo;
1442
1443 if (isa<ExplicitCastExpr>(CE)) {
1444 LValueBaseInfo TargetTypeBaseInfo;
1445 TBAAAccessInfo TargetTypeTBAAInfo;
1447 E->getType(), &TargetTypeBaseInfo, &TargetTypeTBAAInfo);
1448 if (TBAAInfo)
1449 *TBAAInfo =
1450 CGF.CGM.mergeTBAAInfoForCast(*TBAAInfo, TargetTypeTBAAInfo);
1451 // If the source l-value is opaque, honor the alignment of the
1452 // casted-to type.
1453 if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
1454 if (BaseInfo)
1455 BaseInfo->mergeForCast(TargetTypeBaseInfo);
1456 Addr.setAlignment(Align);
1457 }
1458 }
1459
1460 if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
1461 CE->getCastKind() == CK_BitCast) {
1462 if (auto PT = E->getType()->getAs<PointerType>())
1463 CGF.EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr,
1464 /*MayBeNull=*/true,
1466 CE->getBeginLoc());
1467 }
1468
1469 llvm::Type *ElemTy =
1471 Addr = Addr.withElementType(ElemTy);
1472 if (CE->getCastKind() == CK_AddressSpaceConversion)
1474 Addr, CGF.ConvertType(E->getType()), ElemTy);
1475
1476 return CGF.authPointerToPointerCast(Addr, CE->getSubExpr()->getType(),
1477 CE->getType());
1478 }
1479 break;
1480
1481 // Array-to-pointer decay.
1482 case CK_ArrayToPointerDecay:
1483 return CGF.EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo, TBAAInfo);
1484
1485 // Derived-to-base conversions.
1486 case CK_UncheckedDerivedToBase:
1487 case CK_DerivedToBase: {
1488 // TODO: Support accesses to members of base classes in TBAA. For now, we
1489 // conservatively pretend that the complete object is of the base class
1490 // type.
1491 if (TBAAInfo)
1492 *TBAAInfo = CGF.CGM.getTBAAAccessInfo(E->getType());
1494 CE->getSubExpr(), BaseInfo, nullptr,
1495 (KnownNonNull_t)(IsKnownNonNull ||
1496 CE->getCastKind() == CK_UncheckedDerivedToBase));
1497 auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl();
1498 return CGF.GetAddressOfBaseClass(
1499 Addr, Derived, CE->path_begin(), CE->path_end(),
1500 CGF.ShouldNullCheckClassCastValue(CE), CE->getExprLoc());
1501 }
1502
1503 // TODO: Is there any reason to treat base-to-derived conversions
1504 // specially?
1505 default:
1506 break;
1507 }
1508 }
1509
1510 // Unary &.
1511 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1512 if (UO->getOpcode() == UO_AddrOf) {
1513 LValue LV = CGF.EmitLValue(UO->getSubExpr(), IsKnownNonNull);
1514 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
1515 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
1516 return LV.getAddress();
1517 }
1518 }
1519
1520 // std::addressof and variants.
1521 if (auto *Call = dyn_cast<CallExpr>(E)) {
1522 switch (Call->getBuiltinCallee()) {
1523 default:
1524 break;
1525 case Builtin::BIaddressof:
1526 case Builtin::BI__addressof:
1527 case Builtin::BI__builtin_addressof: {
1528 LValue LV = CGF.EmitLValue(Call->getArg(0), IsKnownNonNull);
1529 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
1530 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
1531 return LV.getAddress();
1532 }
1533 }
1534 }
1535
1536 // Pointer arithmetic: pointer +/- index.
1537 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
1538 if (BO->isAdditiveOp())
1539 return emitPointerArithmetic(CGF, BO, BaseInfo, TBAAInfo, IsKnownNonNull);
1540 }
1541
1542 // TODO: conditional operators, comma.
1543
1544 // Otherwise, use the alignment of the type.
1547 /*ForPointeeType=*/true, BaseInfo, TBAAInfo, IsKnownNonNull);
1548}
1549
1550/// EmitPointerWithAlignment - Given an expression of pointer type, try to
1551/// derive a more accurate bound on the alignment of the pointer.
1553 const Expr *E, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo,
1554 KnownNonNull_t IsKnownNonNull) {
1555 Address Addr =
1556 ::EmitPointerWithAlignment(E, BaseInfo, TBAAInfo, IsKnownNonNull, *this);
1557 if (IsKnownNonNull && !Addr.isKnownNonNull())
1558 Addr.setKnownNonNull();
1559 return Addr;
1560}
1561
1563 llvm::Value *V = RV.getScalarVal();
1564 if (auto MPT = T->getAs<MemberPointerType>())
1565 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, V, MPT);
1566 return Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
1567}
1568
1570 if (Ty->isVoidType())
1571 return RValue::get(nullptr);
1572
1573 switch (getEvaluationKind(Ty)) {
1574 case TEK_Complex: {
1575 llvm::Type *EltTy =
1577 llvm::Value *U = llvm::UndefValue::get(EltTy);
1578 return RValue::getComplex(std::make_pair(U, U));
1579 }
1580
1581 // If this is a use of an undefined aggregate type, the aggregate must have an
1582 // identifiable address. Just because the contents of the value are undefined
1583 // doesn't mean that the address can't be taken and compared.
1584 case TEK_Aggregate: {
1585 Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
1586 return RValue::getAggregate(DestPtr);
1587 }
1588
1589 case TEK_Scalar:
1590 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
1591 }
1592 llvm_unreachable("bad evaluation kind");
1593}
1594
1596 const char *Name) {
1597 ErrorUnsupported(E, Name);
1598 return GetUndefRValue(E->getType());
1599}
1600
1602 const char *Name) {
1603 ErrorUnsupported(E, Name);
1604 llvm::Type *ElTy = ConvertType(E->getType());
1605 llvm::Type *Ty = DefaultPtrTy;
1606 return MakeAddrLValue(
1607 Address(llvm::UndefValue::get(Ty), ElTy, CharUnits::One()), E->getType());
1608}
1609
1611 const Expr *Base = Obj;
1612 while (!isa<CXXThisExpr>(Base)) {
1613 // The result of a dynamic_cast can be null.
1615 return false;
1616
1617 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
1618 Base = CE->getSubExpr();
1619 } else if (const auto *PE = dyn_cast<ParenExpr>(Base)) {
1620 Base = PE->getSubExpr();
1621 } else if (const auto *UO = dyn_cast<UnaryOperator>(Base)) {
1622 if (UO->getOpcode() == UO_Extension)
1623 Base = UO->getSubExpr();
1624 else
1625 return false;
1626 } else {
1627 return false;
1628 }
1629 }
1630 return true;
1631}
1632
1634 LValue LV;
1635 if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
1636 LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
1637 else
1638 LV = EmitLValue(E);
1639 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) {
1640 SanitizerSet SkippedChecks;
1641 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
1642 bool IsBaseCXXThis = IsWrappedCXXThis(ME->getBase());
1643 if (IsBaseCXXThis)
1644 SkippedChecks.set(SanitizerKind::Alignment, true);
1645 if (IsBaseCXXThis || isa<DeclRefExpr>(ME->getBase()))
1646 SkippedChecks.set(SanitizerKind::Null, true);
1647 }
1648 EmitTypeCheck(TCK, E->getExprLoc(), LV, E->getType(), SkippedChecks);
1649 }
1650 return LV;
1651}
1652
1653/// EmitLValue - Emit code to compute a designator that specifies the location
1654/// of the expression.
1655///
1656/// This can return one of two things: a simple address or a bitfield reference.
1657/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
1658/// an LLVM pointer type.
1659///
1660/// If this returns a bitfield reference, nothing about the pointee type of the
1661/// LLVM value is known: For example, it may not be a pointer to an integer.
1662///
1663/// If this returns a normal address, and if the lvalue's C type is fixed size,
1664/// this method guarantees that the returned pointer type will point to an LLVM
1665/// type of the same size of the lvalue's type. If the lvalue has a variable
1666/// length type, this is not possible.
1667///
1669 KnownNonNull_t IsKnownNonNull) {
1670 // Running with sufficient stack space to avoid deeply nested expressions
1671 // cause a stack overflow.
1672 LValue LV;
1673 CGM.runWithSufficientStackSpace(
1674 E->getExprLoc(), [&] { LV = EmitLValueHelper(E, IsKnownNonNull); });
1675
1676 if (IsKnownNonNull && !LV.isKnownNonNull())
1677 LV.setKnownNonNull();
1678 return LV;
1679}
1680
1682 const ASTContext &Ctx) {
1683 const Expr *SE = E->getSubExpr()->IgnoreImplicit();
1684 if (isa<OpaqueValueExpr>(SE))
1685 return SE->getType();
1686 return cast<CallExpr>(SE)->getCallReturnType(Ctx)->getPointeeType();
1687}
1688
1689LValue CodeGenFunction::EmitLValueHelper(const Expr *E,
1690 KnownNonNull_t IsKnownNonNull) {
1691 ApplyDebugLocation DL(*this, E);
1692 switch (E->getStmtClass()) {
1693 default: return EmitUnsupportedLValue(E, "l-value expression");
1694
1695 case Expr::ObjCPropertyRefExprClass:
1696 llvm_unreachable("cannot emit a property reference directly");
1697
1698 case Expr::ObjCSelectorExprClass:
1700 case Expr::ObjCIsaExprClass:
1702 case Expr::BinaryOperatorClass:
1704 case Expr::CompoundAssignOperatorClass: {
1705 QualType Ty = E->getType();
1706 if (const AtomicType *AT = Ty->getAs<AtomicType>())
1707 Ty = AT->getValueType();
1708 if (!Ty->isAnyComplexType())
1711 }
1712 case Expr::CallExprClass:
1713 case Expr::CXXMemberCallExprClass:
1714 case Expr::CXXOperatorCallExprClass:
1715 case Expr::UserDefinedLiteralClass:
1717 case Expr::CXXRewrittenBinaryOperatorClass:
1718 return EmitLValue(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(),
1719 IsKnownNonNull);
1720 case Expr::VAArgExprClass:
1722 case Expr::DeclRefExprClass:
1724 case Expr::ConstantExprClass: {
1725 const ConstantExpr *CE = cast<ConstantExpr>(E);
1726 if (llvm::Value *Result = ConstantEmitter(*this).tryEmitConstantExpr(CE)) {
1727 QualType RetType = getConstantExprReferredType(CE, getContext());
1728 return MakeNaturalAlignAddrLValue(Result, RetType);
1729 }
1730 return EmitLValue(cast<ConstantExpr>(E)->getSubExpr(), IsKnownNonNull);
1731 }
1732 case Expr::ParenExprClass:
1733 return EmitLValue(cast<ParenExpr>(E)->getSubExpr(), IsKnownNonNull);
1734 case Expr::GenericSelectionExprClass:
1735 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr(),
1736 IsKnownNonNull);
1737 case Expr::PredefinedExprClass:
1739 case Expr::StringLiteralClass:
1741 case Expr::ObjCEncodeExprClass:
1743 case Expr::PseudoObjectExprClass:
1745 case Expr::InitListExprClass:
1747 case Expr::CXXTemporaryObjectExprClass:
1748 case Expr::CXXConstructExprClass:
1750 case Expr::CXXBindTemporaryExprClass:
1752 case Expr::CXXUuidofExprClass:
1754 case Expr::LambdaExprClass:
1755 return EmitAggExprToLValue(E);
1756
1757 case Expr::ExprWithCleanupsClass: {
1758 const auto *cleanups = cast<ExprWithCleanups>(E);
1759 RunCleanupsScope Scope(*this);
1760 LValue LV = EmitLValue(cleanups->getSubExpr(), IsKnownNonNull);
1761 if (LV.isSimple()) {
1762 // Defend against branches out of gnu statement expressions surrounded by
1763 // cleanups.
1764 Address Addr = LV.getAddress();
1765 llvm::Value *V = Addr.getBasePointer();
1766 Scope.ForceCleanup({&V});
1767 Addr.replaceBasePointer(V);
1768 return LValue::MakeAddr(Addr, LV.getType(), getContext(),
1769 LV.getBaseInfo(), LV.getTBAAInfo());
1770 }
1771 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1772 // bitfield lvalue or some other non-simple lvalue?
1773 return LV;
1774 }
1775
1776 case Expr::CXXDefaultArgExprClass: {
1777 auto *DAE = cast<CXXDefaultArgExpr>(E);
1778 CXXDefaultArgExprScope Scope(*this, DAE);
1779 return EmitLValue(DAE->getExpr(), IsKnownNonNull);
1780 }
1781 case Expr::CXXDefaultInitExprClass: {
1782 auto *DIE = cast<CXXDefaultInitExpr>(E);
1783 CXXDefaultInitExprScope Scope(*this, DIE);
1784 return EmitLValue(DIE->getExpr(), IsKnownNonNull);
1785 }
1786 case Expr::CXXTypeidExprClass:
1788
1789 case Expr::ObjCMessageExprClass:
1791 case Expr::ObjCIvarRefExprClass:
1793 case Expr::StmtExprClass:
1795 case Expr::UnaryOperatorClass:
1797 case Expr::ArraySubscriptExprClass:
1799 case Expr::MatrixSubscriptExprClass:
1801 case Expr::ArraySectionExprClass:
1803 case Expr::ExtVectorElementExprClass:
1805 case Expr::CXXThisExprClass:
1807 case Expr::MemberExprClass:
1809 case Expr::CompoundLiteralExprClass:
1811 case Expr::ConditionalOperatorClass:
1813 case Expr::BinaryConditionalOperatorClass:
1815 case Expr::ChooseExprClass:
1816 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(), IsKnownNonNull);
1817 case Expr::OpaqueValueExprClass:
1819 case Expr::SubstNonTypeTemplateParmExprClass:
1820 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
1821 IsKnownNonNull);
1822 case Expr::ImplicitCastExprClass:
1823 case Expr::CStyleCastExprClass:
1824 case Expr::CXXFunctionalCastExprClass:
1825 case Expr::CXXStaticCastExprClass:
1826 case Expr::CXXDynamicCastExprClass:
1827 case Expr::CXXReinterpretCastExprClass:
1828 case Expr::CXXConstCastExprClass:
1829 case Expr::CXXAddrspaceCastExprClass:
1830 case Expr::ObjCBridgedCastExprClass:
1831 return EmitCastLValue(cast<CastExpr>(E));
1832
1833 case Expr::MaterializeTemporaryExprClass:
1835
1836 case Expr::CoawaitExprClass:
1838 case Expr::CoyieldExprClass:
1840 case Expr::PackIndexingExprClass:
1841 return EmitLValue(cast<PackIndexingExpr>(E)->getSelectedExpr());
1842 case Expr::HLSLOutArgExprClass:
1843 llvm_unreachable("cannot emit a HLSL out argument directly");
1844 }
1845}
1846
1847/// Given an object of the given canonical type, can we safely copy a
1848/// value out of it based on its initializer?
1850 assert(type.isCanonical());
1851 assert(!type->isReferenceType());
1852
1853 // Must be const-qualified but non-volatile.
1854 Qualifiers qs = type.getLocalQualifiers();
1855 if (!qs.hasConst() || qs.hasVolatile()) return false;
1856
1857 // Otherwise, all object types satisfy this except C++ classes with
1858 // mutable subobjects or non-trivial copy/destroy behavior.
1859 if (const auto *RT = dyn_cast<RecordType>(type))
1860 if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1861 RD = RD->getDefinitionOrSelf();
1862 if (RD->hasMutableFields() || !RD->isTrivial())
1863 return false;
1864 }
1865
1866 return true;
1867}
1868
1869/// Can we constant-emit a load of a reference to a variable of the
1870/// given type? This is different from predicates like
1871/// Decl::mightBeUsableInConstantExpressions because we do want it to apply
1872/// in situations that don't necessarily satisfy the language's rules
1873/// for this (e.g. C++'s ODR-use rules). For example, we want to able
1874/// to do this with const float variables even if those variables
1875/// aren't marked 'constexpr'.
1883 type = type.getCanonicalType();
1884 if (const auto *ref = dyn_cast<ReferenceType>(type)) {
1885 if (isConstantEmittableObjectType(ref->getPointeeType()))
1887 return CEK_AsReferenceOnly;
1888 }
1890 return CEK_AsValueOnly;
1891 return CEK_None;
1892}
1893
1894/// Try to emit a reference to the given value without producing it as
1895/// an l-value. This is just an optimization, but it avoids us needing
1896/// to emit global copies of variables if they're named without triggering
1897/// a formal use in a context where we can't emit a direct reference to them,
1898/// for instance if a block or lambda or a member of a local class uses a
1899/// const int variable or constexpr variable from an enclosing function.
1902 const ValueDecl *Value = RefExpr->getDecl();
1903
1904 // The value needs to be an enum constant or a constant variable.
1906 if (isa<ParmVarDecl>(Value)) {
1907 CEK = CEK_None;
1908 } else if (const auto *var = dyn_cast<VarDecl>(Value)) {
1909 CEK = checkVarTypeForConstantEmission(var->getType());
1910 } else if (isa<EnumConstantDecl>(Value)) {
1911 CEK = CEK_AsValueOnly;
1912 } else {
1913 CEK = CEK_None;
1914 }
1915 if (CEK == CEK_None) return ConstantEmission();
1916
1917 Expr::EvalResult result;
1918 bool resultIsReference;
1919 QualType resultType;
1920
1921 // It's best to evaluate all the way as an r-value if that's permitted.
1922 if (CEK != CEK_AsReferenceOnly &&
1923 RefExpr->EvaluateAsRValue(result, getContext())) {
1924 resultIsReference = false;
1925 resultType = RefExpr->getType().getUnqualifiedType();
1926
1927 // Otherwise, try to evaluate as an l-value.
1928 } else if (CEK != CEK_AsValueOnly &&
1929 RefExpr->EvaluateAsLValue(result, getContext())) {
1930 resultIsReference = true;
1931 resultType = Value->getType();
1932
1933 // Failure.
1934 } else {
1935 return ConstantEmission();
1936 }
1937
1938 // In any case, if the initializer has side-effects, abandon ship.
1939 if (result.HasSideEffects)
1940 return ConstantEmission();
1941
1942 // In CUDA/HIP device compilation, a lambda may capture a reference variable
1943 // referencing a global host variable by copy. In this case the lambda should
1944 // make a copy of the value of the global host variable. The DRE of the
1945 // captured reference variable cannot be emitted as load from the host
1946 // global variable as compile time constant, since the host variable is not
1947 // accessible on device. The DRE of the captured reference variable has to be
1948 // loaded from captures.
1949 if (CGM.getLangOpts().CUDAIsDevice && result.Val.isLValue() &&
1951 auto *MD = dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl);
1952 if (isLambdaMethod(MD) && MD->getOverloadedOperator() == OO_Call) {
1953 const APValue::LValueBase &base = result.Val.getLValueBase();
1954 if (const ValueDecl *D = base.dyn_cast<const ValueDecl *>()) {
1955 if (const VarDecl *VD = dyn_cast<const VarDecl>(D)) {
1956 if (!VD->hasAttr<CUDADeviceAttr>()) {
1957 return ConstantEmission();
1958 }
1959 }
1960 }
1961 }
1962 }
1963
1964 // Emit as a constant.
1965 llvm::Constant *C = ConstantEmitter(*this).emitAbstract(
1966 RefExpr->getLocation(), result.Val, resultType);
1967
1968 // Make sure we emit a debug reference to the global variable.
1969 // This should probably fire even for
1970 if (isa<VarDecl>(Value)) {
1971 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(Value)))
1972 EmitDeclRefExprDbgValue(RefExpr, result.Val);
1973 } else {
1975 EmitDeclRefExprDbgValue(RefExpr, result.Val);
1976 }
1977
1978 // If we emitted a reference constant, we need to dereference that.
1979 if (resultIsReference)
1981
1983}
1984
1986 const MemberExpr *ME) {
1987 if (auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
1988 // Try to emit static variable member expressions as DREs.
1989 return DeclRefExpr::Create(
1991 /*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(),
1992 ME->getType(), ME->getValueKind(), nullptr, nullptr, ME->isNonOdrUse());
1993 }
1994 return nullptr;
1995}
1996
2000 return tryEmitAsConstant(DRE);
2001 return ConstantEmission();
2002}
2003
2005 const CodeGenFunction::ConstantEmission &Constant, Expr *E) {
2006 assert(Constant && "not a constant");
2007 if (Constant.isReference())
2008 return EmitLoadOfLValue(Constant.getReferenceLValue(*this, E),
2009 E->getExprLoc())
2010 .getScalarVal();
2011 return Constant.getValue();
2012}
2013
2015 SourceLocation Loc) {
2016 return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
2017 lvalue.getType(), Loc, lvalue.getBaseInfo(),
2018 lvalue.getTBAAInfo(), lvalue.isNontemporal());
2019}
2020
2022 llvm::APInt &Min, llvm::APInt &End,
2023 bool StrictEnums, bool IsBool) {
2024 const auto *ED = Ty->getAsEnumDecl();
2025 bool IsRegularCPlusPlusEnum =
2026 CGF.getLangOpts().CPlusPlus && StrictEnums && ED && !ED->isFixed();
2027 if (!IsBool && !IsRegularCPlusPlusEnum)
2028 return false;
2029
2030 if (IsBool) {
2031 Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
2032 End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
2033 } else {
2034 ED->getValueRange(End, Min);
2035 }
2036 return true;
2037}
2038
2039llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
2040 llvm::APInt Min, End;
2041 if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
2042 Ty->hasBooleanRepresentation() && !Ty->isVectorType()))
2043 return nullptr;
2044
2045 llvm::MDBuilder MDHelper(getLLVMContext());
2046 return MDHelper.createRange(Min, End);
2047}
2048
2050 SourceLocation Loc) {
2051 if (EmitScalarRangeCheck(Load, Ty, Loc)) {
2052 // In order to prevent the optimizer from throwing away the check, don't
2053 // attach range metadata to the load.
2054 } else if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2055 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) {
2056 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
2057 Load->setMetadata(llvm::LLVMContext::MD_noundef,
2058 llvm::MDNode::get(CGM.getLLVMContext(), {}));
2059 }
2060 }
2061}
2062
2064 SourceLocation Loc) {
2065 bool HasBoolCheck = SanOpts.has(SanitizerKind::Bool);
2066 bool HasEnumCheck = SanOpts.has(SanitizerKind::Enum);
2067 if (!HasBoolCheck && !HasEnumCheck)
2068 return false;
2069
2070 bool IsBool = (Ty->hasBooleanRepresentation() && !Ty->isVectorType()) ||
2071 NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
2072 bool NeedsBoolCheck = HasBoolCheck && IsBool;
2073 bool NeedsEnumCheck = HasEnumCheck && Ty->isEnumeralType();
2074 if (!NeedsBoolCheck && !NeedsEnumCheck)
2075 return false;
2076
2077 // Single-bit booleans don't need to be checked. Special-case this to avoid
2078 // a bit width mismatch when handling bitfield values. This is handled by
2079 // EmitFromMemory for the non-bitfield case.
2080 if (IsBool &&
2081 cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
2082 return false;
2083
2084 if (NeedsEnumCheck &&
2085 getContext().isTypeIgnoredBySanitizer(SanitizerKind::Enum, Ty))
2086 return false;
2087
2088 llvm::APInt Min, End;
2089 if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
2090 return true;
2091
2093 NeedsEnumCheck ? SanitizerKind::SO_Enum : SanitizerKind::SO_Bool;
2094
2095 auto &Ctx = getLLVMContext();
2096 auto CheckHandler = SanitizerHandler::LoadInvalidValue;
2097 SanitizerDebugLocation SanScope(this, {Kind}, CheckHandler);
2098 llvm::Value *Check;
2099 --End;
2100 if (!Min) {
2101 Check = Builder.CreateICmpULE(Value, llvm::ConstantInt::get(Ctx, End));
2102 } else {
2103 llvm::Value *Upper =
2104 Builder.CreateICmpSLE(Value, llvm::ConstantInt::get(Ctx, End));
2105 llvm::Value *Lower =
2106 Builder.CreateICmpSGE(Value, llvm::ConstantInt::get(Ctx, Min));
2107 Check = Builder.CreateAnd(Upper, Lower);
2108 }
2109 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc),
2111 EmitCheck(std::make_pair(Check, Kind), CheckHandler, StaticArgs, Value);
2112 return true;
2113}
2114
2116 QualType Ty,
2117 SourceLocation Loc,
2118 LValueBaseInfo BaseInfo,
2119 TBAAAccessInfo TBAAInfo,
2120 bool isNontemporal) {
2121 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getBasePointer()))
2122 if (GV->isThreadLocal())
2123 Addr = Addr.withPointer(Builder.CreateThreadLocalAddress(GV),
2125
2126 if (const auto *ClangVecTy = Ty->getAs<VectorType>()) {
2127 // Boolean vectors use `iN` as storage type.
2128 if (ClangVecTy->isPackedVectorBoolType(getContext())) {
2129 llvm::Type *ValTy = ConvertType(Ty);
2130 unsigned ValNumElems =
2131 cast<llvm::FixedVectorType>(ValTy)->getNumElements();
2132 // Load the `iP` storage object (P is the padded vector size).
2133 auto *RawIntV = Builder.CreateLoad(Addr, Volatile, "load_bits");
2134 const auto *RawIntTy = RawIntV->getType();
2135 assert(RawIntTy->isIntegerTy() && "compressed iN storage for bitvectors");
2136 // Bitcast iP --> <P x i1>.
2137 auto *PaddedVecTy = llvm::FixedVectorType::get(
2138 Builder.getInt1Ty(), RawIntTy->getPrimitiveSizeInBits());
2139 llvm::Value *V = Builder.CreateBitCast(RawIntV, PaddedVecTy);
2140 // Shuffle <P x i1> --> <N x i1> (N is the actual bit size).
2141 V = emitBoolVecConversion(V, ValNumElems, "extractvec");
2142
2143 return EmitFromMemory(V, Ty);
2144 }
2145
2146 // Handles vectors of sizes that are likely to be expanded to a larger size
2147 // to optimize performance.
2148 auto *VTy = cast<llvm::FixedVectorType>(Addr.getElementType());
2149 auto *NewVecTy =
2150 CGM.getABIInfo().getOptimalVectorMemoryType(VTy, getLangOpts());
2151
2152 if (VTy != NewVecTy) {
2153 Address Cast = Addr.withElementType(NewVecTy);
2154 llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVecN");
2155 unsigned OldNumElements = VTy->getNumElements();
2156 SmallVector<int, 16> Mask(OldNumElements);
2157 std::iota(Mask.begin(), Mask.end(), 0);
2158 V = Builder.CreateShuffleVector(V, Mask, "extractVec");
2159 return EmitFromMemory(V, Ty);
2160 }
2161 }
2162
2163 // Atomic operations have to be done on integral types.
2164 LValue AtomicLValue =
2165 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
2166 if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) {
2167 return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal();
2168 }
2169
2170 Addr =
2171 Addr.withElementType(convertTypeForLoadStore(Ty, Addr.getElementType()));
2172
2173 llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile);
2174 if (isNontemporal) {
2175 llvm::MDNode *Node = llvm::MDNode::get(
2176 Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
2177 Load->setMetadata(llvm::LLVMContext::MD_nontemporal, Node);
2178 }
2179
2180 CGM.DecorateInstructionWithTBAA(Load, TBAAInfo);
2181
2182 maybeAttachRangeForLoad(Load, Ty, Loc);
2183
2184 return EmitFromMemory(Load, Ty);
2185}
2186
2187/// Converts a scalar value from its primary IR type (as returned
2188/// by ConvertType) to its load/store type (as returned by
2189/// convertTypeForLoadStore).
2190llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
2191 if (auto *AtomicTy = Ty->getAs<AtomicType>())
2192 Ty = AtomicTy->getValueType();
2193
2194 if (Ty->isExtVectorBoolType()) {
2195 llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
2196 if (StoreTy->isVectorTy() && StoreTy->getScalarSizeInBits() >
2197 Value->getType()->getScalarSizeInBits())
2198 return Builder.CreateZExt(Value, StoreTy);
2199
2200 // Expand to the memory bit width.
2201 unsigned MemNumElems = StoreTy->getPrimitiveSizeInBits();
2202 // <N x i1> --> <P x i1>.
2203 Value = emitBoolVecConversion(Value, MemNumElems, "insertvec");
2204 // <P x i1> --> iP.
2205 Value = Builder.CreateBitCast(Value, StoreTy);
2206 }
2207
2208 if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
2209 llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
2211 return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
2212 }
2213
2214 return Value;
2215}
2216
2217/// Converts a scalar value from its load/store type (as returned
2218/// by convertTypeForLoadStore) to its primary IR type (as returned
2219/// by ConvertType).
2220llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
2221 if (auto *AtomicTy = Ty->getAs<AtomicType>())
2222 Ty = AtomicTy->getValueType();
2223
2225 const auto *RawIntTy = Value->getType();
2226
2227 // Bitcast iP --> <P x i1>.
2228 auto *PaddedVecTy = llvm::FixedVectorType::get(
2229 Builder.getInt1Ty(), RawIntTy->getPrimitiveSizeInBits());
2230 auto *V = Builder.CreateBitCast(Value, PaddedVecTy);
2231 // Shuffle <P x i1> --> <N x i1> (N is the actual bit size).
2232 llvm::Type *ValTy = ConvertType(Ty);
2233 unsigned ValNumElems = cast<llvm::FixedVectorType>(ValTy)->getNumElements();
2234 return emitBoolVecConversion(V, ValNumElems, "extractvec");
2235 }
2236
2237 llvm::Type *ResTy = ConvertType(Ty);
2238 if (Ty->hasBooleanRepresentation() || Ty->isBitIntType() ||
2239 Ty->isExtVectorBoolType())
2240 return Builder.CreateTrunc(Value, ResTy, "loadedv");
2241
2242 return Value;
2243}
2244
2245// Convert the pointer of \p Addr to a pointer to a vector (the value type of
2246// MatrixType), if it points to a array (the memory type of MatrixType).
2248 CodeGenFunction &CGF,
2249 bool IsVector = true) {
2250 auto *ArrayTy = dyn_cast<llvm::ArrayType>(Addr.getElementType());
2251 if (ArrayTy && IsVector) {
2252 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
2253 ArrayTy->getNumElements());
2254
2255 return Addr.withElementType(VectorTy);
2256 }
2257 auto *VectorTy = dyn_cast<llvm::VectorType>(Addr.getElementType());
2258 if (VectorTy && !IsVector) {
2259 auto *ArrayTy = llvm::ArrayType::get(
2260 VectorTy->getElementType(),
2261 cast<llvm::FixedVectorType>(VectorTy)->getNumElements());
2262
2263 return Addr.withElementType(ArrayTy);
2264 }
2265
2266 return Addr;
2267}
2268
2269// Emit a store of a matrix LValue. This may require casting the original
2270// pointer to memory address (ArrayType) to a pointer to the value type
2271// (VectorType).
2272static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue,
2273 bool isInit, CodeGenFunction &CGF) {
2274 Address Addr = MaybeConvertMatrixAddress(lvalue.getAddress(), CGF,
2275 value->getType()->isVectorTy());
2276 CGF.EmitStoreOfScalar(value, Addr, lvalue.isVolatile(), lvalue.getType(),
2277 lvalue.getBaseInfo(), lvalue.getTBAAInfo(), isInit,
2278 lvalue.isNontemporal());
2279}
2280
2282 bool Volatile, QualType Ty,
2283 LValueBaseInfo BaseInfo,
2284 TBAAAccessInfo TBAAInfo,
2285 bool isInit, bool isNontemporal) {
2286 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getBasePointer()))
2287 if (GV->isThreadLocal())
2288 Addr = Addr.withPointer(Builder.CreateThreadLocalAddress(GV),
2290
2291 // Handles vectors of sizes that are likely to be expanded to a larger size
2292 // to optimize performance.
2293 llvm::Type *SrcTy = Value->getType();
2294 if (const auto *ClangVecTy = Ty->getAs<VectorType>()) {
2295 if (auto *VecTy = dyn_cast<llvm::FixedVectorType>(SrcTy)) {
2296 auto *NewVecTy =
2297 CGM.getABIInfo().getOptimalVectorMemoryType(VecTy, getLangOpts());
2298 if (!ClangVecTy->isPackedVectorBoolType(getContext()) &&
2299 VecTy != NewVecTy) {
2300 SmallVector<int, 16> Mask(NewVecTy->getNumElements(),
2301 VecTy->getNumElements());
2302 std::iota(Mask.begin(), Mask.begin() + VecTy->getNumElements(), 0);
2303 // Use undef instead of poison for the padding lanes, to make sure no
2304 // padding bits are poisoned, which may break coercion.
2305 Value = Builder.CreateShuffleVector(Value, llvm::UndefValue::get(VecTy),
2306 Mask, "extractVec");
2307 SrcTy = NewVecTy;
2308 }
2309 if (Addr.getElementType() != SrcTy)
2310 Addr = Addr.withElementType(SrcTy);
2311 }
2312 }
2313
2314 Value = EmitToMemory(Value, Ty);
2315
2316 LValue AtomicLValue =
2317 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
2318 if (Ty->isAtomicType() ||
2319 (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) {
2320 EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit);
2321 return;
2322 }
2323
2324 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
2326
2327 if (isNontemporal) {
2328 llvm::MDNode *Node =
2329 llvm::MDNode::get(Store->getContext(),
2330 llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
2331 Store->setMetadata(llvm::LLVMContext::MD_nontemporal, Node);
2332 }
2333
2334 CGM.DecorateInstructionWithTBAA(Store, TBAAInfo);
2335}
2336
2337void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
2338 bool isInit) {
2339 if (lvalue.getType()->isConstantMatrixType()) {
2340 EmitStoreOfMatrixScalar(value, lvalue, isInit, *this);
2341 return;
2342 }
2343
2344 EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
2345 lvalue.getType(), lvalue.getBaseInfo(),
2346 lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal());
2347}
2348
2349// Emit a load of a LValue of matrix type. This may require casting the pointer
2350// to memory address (ArrayType) to a pointer to the value type (VectorType).
2352 CodeGenFunction &CGF) {
2353 assert(LV.getType()->isConstantMatrixType());
2354 Address Addr = MaybeConvertMatrixAddress(LV.getAddress(), CGF);
2355 LV.setAddress(Addr);
2356 return RValue::get(CGF.EmitLoadOfScalar(LV, Loc));
2357}
2358
2360 SourceLocation Loc) {
2361 QualType Ty = LV.getType();
2362 switch (getEvaluationKind(Ty)) {
2363 case TEK_Scalar:
2364 return EmitLoadOfLValue(LV, Loc);
2365 case TEK_Complex:
2366 return RValue::getComplex(EmitLoadOfComplex(LV, Loc));
2367 case TEK_Aggregate:
2368 EmitAggFinalDestCopy(Ty, Slot, LV, EVK_NonRValue);
2369 return Slot.asRValue();
2370 }
2371 llvm_unreachable("bad evaluation kind");
2372}
2373
2374/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
2375/// method emits the address of the lvalue, then loads the result as an rvalue,
2376/// returning the rvalue.
2378 // Load from __ptrauth.
2379 if (PointerAuthQualifier PtrAuth = LV.getQuals().getPointerAuth()) {
2381 llvm::Value *Value = EmitLoadOfLValue(LV, Loc).getScalarVal();
2382 return RValue::get(EmitPointerAuthUnqualify(PtrAuth, Value, LV.getType(),
2383 LV.getAddress(),
2384 /*known nonnull*/ false));
2385 }
2386
2387 if (LV.isObjCWeak()) {
2388 // load of a __weak object.
2389 Address AddrWeakObj = LV.getAddress();
2390 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
2391 AddrWeakObj));
2392 }
2394 // In MRC mode, we do a load+autorelease.
2395 if (!getLangOpts().ObjCAutoRefCount) {
2397 }
2398
2399 // In ARC mode, we load retained and then consume the value.
2400 llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress());
2401 Object = EmitObjCConsumeObject(LV.getType(), Object);
2402 return RValue::get(Object);
2403 }
2404
2405 if (LV.isSimple()) {
2406 assert(!LV.getType()->isFunctionType());
2407
2408 if (LV.getType()->isConstantMatrixType())
2409 return EmitLoadOfMatrixLValue(LV, Loc, *this);
2410
2411 // Everything needs a load.
2412 return RValue::get(EmitLoadOfScalar(LV, Loc));
2413 }
2414
2415 if (LV.isVectorElt()) {
2416 llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(),
2417 LV.isVolatileQualified());
2418 return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
2419 "vecext"));
2420 }
2421
2422 // If this is a reference to a subset of the elements of a vector, either
2423 // shuffle the input or extract/insert them as appropriate.
2424 if (LV.isExtVectorElt()) {
2426 }
2427
2428 // Global Register variables always invoke intrinsics
2429 if (LV.isGlobalReg())
2430 return EmitLoadOfGlobalRegLValue(LV);
2431
2432 if (LV.isMatrixElt()) {
2433 llvm::Value *Idx = LV.getMatrixIdx();
2434 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2435 const auto *const MatTy = LV.getType()->castAs<ConstantMatrixType>();
2436 llvm::MatrixBuilder MB(Builder);
2437 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
2438 }
2439 llvm::LoadInst *Load =
2440 Builder.CreateLoad(LV.getMatrixAddress(), LV.isVolatileQualified());
2441 return RValue::get(Builder.CreateExtractElement(Load, Idx, "matrixext"));
2442 }
2443
2444 assert(LV.isBitField() && "Unknown LValue type!");
2445 return EmitLoadOfBitfieldLValue(LV, Loc);
2446}
2447
2449 SourceLocation Loc) {
2450 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
2451
2452 // Get the output type.
2453 llvm::Type *ResLTy = ConvertType(LV.getType());
2454
2455 Address Ptr = LV.getBitFieldAddress();
2456 llvm::Value *Val =
2457 Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load");
2458
2459 bool UseVolatile = LV.isVolatileQualified() &&
2460 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
2461 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
2462 const unsigned StorageSize =
2463 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
2464 if (Info.IsSigned) {
2465 assert(static_cast<unsigned>(Offset + Info.Size) <= StorageSize);
2466 unsigned HighBits = StorageSize - Offset - Info.Size;
2467 if (HighBits)
2468 Val = Builder.CreateShl(Val, HighBits, "bf.shl");
2469 if (Offset + HighBits)
2470 Val = Builder.CreateAShr(Val, Offset + HighBits, "bf.ashr");
2471 } else {
2472 if (Offset)
2473 Val = Builder.CreateLShr(Val, Offset, "bf.lshr");
2474 if (static_cast<unsigned>(Offset) + Info.Size < StorageSize)
2475 Val = Builder.CreateAnd(
2476 Val, llvm::APInt::getLowBitsSet(StorageSize, Info.Size), "bf.clear");
2477 }
2478 Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
2479 EmitScalarRangeCheck(Val, LV.getType(), Loc);
2480 return RValue::get(Val);
2481}
2482
2483// If this is a reference to a subset of the elements of a vector, create an
2484// appropriate shufflevector.
2486 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(),
2487 LV.isVolatileQualified());
2488
2489 // HLSL allows treating scalars as one-element vectors. Converting the scalar
2490 // IR value to a vector here allows the rest of codegen to behave as normal.
2491 if (getLangOpts().HLSL && !Vec->getType()->isVectorTy()) {
2492 llvm::Type *DstTy = llvm::FixedVectorType::get(Vec->getType(), 1);
2493 llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
2494 Vec = Builder.CreateInsertElement(DstTy, Vec, Zero, "cast.splat");
2495 }
2496
2497 const llvm::Constant *Elts = LV.getExtVectorElts();
2498
2499 // If the result of the expression is a non-vector type, we must be extracting
2500 // a single element. Just codegen as an extractelement.
2501 const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
2502 if (!ExprVT) {
2503 unsigned InIdx = getAccessedFieldNo(0, Elts);
2504 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
2505
2506 llvm::Value *Element = Builder.CreateExtractElement(Vec, Elt);
2507
2508 llvm::Type *LVTy = ConvertType(LV.getType());
2509 if (Element->getType()->getPrimitiveSizeInBits() >
2510 LVTy->getPrimitiveSizeInBits())
2511 Element = Builder.CreateTrunc(Element, LVTy);
2512
2513 return RValue::get(Element);
2514 }
2515
2516 // Always use shuffle vector to try to retain the original program structure
2517 unsigned NumResultElts = ExprVT->getNumElements();
2518
2520 for (unsigned i = 0; i != NumResultElts; ++i)
2521 Mask.push_back(getAccessedFieldNo(i, Elts));
2522
2523 Vec = Builder.CreateShuffleVector(Vec, Mask);
2524
2525 if (LV.getType()->isExtVectorBoolType())
2526 Vec = Builder.CreateTrunc(Vec, ConvertType(LV.getType()), "truncv");
2527
2528 return RValue::get(Vec);
2529}
2530
2531/// Generates lvalue for partial ext_vector access.
2533 Address VectorAddress = LV.getExtVectorAddress();
2534 QualType EQT = LV.getType()->castAs<VectorType>()->getElementType();
2535 llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
2536
2537 Address CastToPointerElement = VectorAddress.withElementType(VectorElementTy);
2538
2539 const llvm::Constant *Elts = LV.getExtVectorElts();
2540 unsigned ix = getAccessedFieldNo(0, Elts);
2541
2542 Address VectorBasePtrPlusIx =
2543 Builder.CreateConstInBoundsGEP(CastToPointerElement, ix,
2544 "vector.elt");
2545
2546 return VectorBasePtrPlusIx;
2547}
2548
2549/// Load of global named registers are always calls to intrinsics.
2551 assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) &&
2552 "Bad type for register variable");
2553 llvm::MDNode *RegName = cast<llvm::MDNode>(
2554 cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata());
2555
2556 // We accept integer and pointer types only
2557 llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType());
2558 llvm::Type *Ty = OrigTy;
2559 if (OrigTy->isPointerTy())
2560 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2561 llvm::Type *Types[] = { Ty };
2562
2563 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
2564 llvm::Value *Call = Builder.CreateCall(
2565 F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
2566 if (OrigTy->isPointerTy())
2567 Call = Builder.CreateIntToPtr(Call, OrigTy);
2568 return RValue::get(Call);
2569}
2570
2571/// EmitStoreThroughLValue - Store the specified rvalue into the specified
2572/// lvalue, where both are guaranteed to the have the same type, and that type
2573/// is 'Ty'.
2575 bool isInit) {
2576 if (!Dst.isSimple()) {
2577 if (Dst.isVectorElt()) {
2578 // Read/modify/write the vector, inserting the new element.
2579 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(),
2580 Dst.isVolatileQualified());
2581 llvm::Type *VecTy = Vec->getType();
2582 llvm::Value *SrcVal = Src.getScalarVal();
2583
2584 if (SrcVal->getType()->getPrimitiveSizeInBits() <
2585 VecTy->getScalarSizeInBits())
2586 SrcVal = Builder.CreateZExt(SrcVal, VecTy->getScalarType());
2587
2588 auto *IRStoreTy = dyn_cast<llvm::IntegerType>(Vec->getType());
2589 if (IRStoreTy) {
2590 auto *IRVecTy = llvm::FixedVectorType::get(
2591 Builder.getInt1Ty(), IRStoreTy->getPrimitiveSizeInBits());
2592 Vec = Builder.CreateBitCast(Vec, IRVecTy);
2593 // iN --> <N x i1>.
2594 }
2595
2596 // Allow inserting `<1 x T>` into an `<N x T>`. It can happen with scalar
2597 // types which are mapped to vector LLVM IR types (e.g. for implementing
2598 // an ABI).
2599 if (auto *EltTy = dyn_cast<llvm::FixedVectorType>(SrcVal->getType());
2600 EltTy && EltTy->getNumElements() == 1)
2601 SrcVal = Builder.CreateBitCast(SrcVal, EltTy->getElementType());
2602
2603 Vec = Builder.CreateInsertElement(Vec, SrcVal, Dst.getVectorIdx(),
2604 "vecins");
2605 if (IRStoreTy) {
2606 // <N x i1> --> <iN>.
2607 Vec = Builder.CreateBitCast(Vec, IRStoreTy);
2608 }
2609
2610 auto *I = Builder.CreateStore(Vec, Dst.getVectorAddress(),
2611 Dst.isVolatileQualified());
2613 return;
2614 }
2615
2616 // If this is an update of extended vector elements, insert them as
2617 // appropriate.
2618 if (Dst.isExtVectorElt())
2620
2621 if (Dst.isGlobalReg())
2622 return EmitStoreThroughGlobalRegLValue(Src, Dst);
2623
2624 if (Dst.isMatrixElt()) {
2625 llvm::Value *Idx = Dst.getMatrixIdx();
2626 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2627 const auto *const MatTy = Dst.getType()->castAs<ConstantMatrixType>();
2628 llvm::MatrixBuilder MB(Builder);
2629 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
2630 }
2631 llvm::Instruction *Load = Builder.CreateLoad(Dst.getMatrixAddress());
2632 llvm::Value *Vec =
2633 Builder.CreateInsertElement(Load, Src.getScalarVal(), Idx, "matins");
2634 auto *I = Builder.CreateStore(Vec, Dst.getMatrixAddress(),
2635 Dst.isVolatileQualified());
2637 return;
2638 }
2639
2640 assert(Dst.isBitField() && "Unknown LValue type");
2641 return EmitStoreThroughBitfieldLValue(Src, Dst);
2642 }
2643
2644 // Handle __ptrauth qualification by re-signing the value.
2645 if (PointerAuthQualifier PointerAuth = Dst.getQuals().getPointerAuth()) {
2646 Src = RValue::get(EmitPointerAuthQualify(PointerAuth, Src.getScalarVal(),
2647 Dst.getType(), Dst.getAddress(),
2648 /*known nonnull*/ false));
2649 }
2650
2651 // There's special magic for assigning into an ARC-qualified l-value.
2652 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
2653 switch (Lifetime) {
2655 llvm_unreachable("present but none");
2656
2658 // nothing special
2659 break;
2660
2662 if (isInit) {
2663 Src = RValue::get(EmitARCRetain(Dst.getType(), Src.getScalarVal()));
2664 break;
2665 }
2666 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
2667 return;
2668
2670 if (isInit)
2671 // Initialize and then skip the primitive store.
2673 else
2675 /*ignore*/ true);
2676 return;
2677
2680 Src.getScalarVal()));
2681 // fall into the normal path
2682 break;
2683 }
2684 }
2685
2686 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
2687 // load of a __weak object.
2688 Address LvalueDst = Dst.getAddress();
2689 llvm::Value *src = Src.getScalarVal();
2690 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
2691 return;
2692 }
2693
2694 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
2695 // load of a __strong object.
2696 Address LvalueDst = Dst.getAddress();
2697 llvm::Value *src = Src.getScalarVal();
2698 if (Dst.isObjCIvar()) {
2699 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
2700 llvm::Type *ResultType = IntPtrTy;
2702 llvm::Value *RHS = dst.emitRawPointer(*this);
2703 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
2704 llvm::Value *LHS = Builder.CreatePtrToInt(LvalueDst.emitRawPointer(*this),
2705 ResultType, "sub.ptr.lhs.cast");
2706 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
2707 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, BytesBetween);
2708 } else if (Dst.isGlobalObjCRef()) {
2709 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
2710 Dst.isThreadLocalRef());
2711 }
2712 else
2713 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
2714 return;
2715 }
2716
2717 assert(Src.isScalar() && "Can't emit an agg store with this method");
2718 EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
2719}
2720
2722 llvm::Value **Result) {
2723 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
2724 llvm::Type *ResLTy = convertTypeForLoadStore(Dst.getType());
2725 Address Ptr = Dst.getBitFieldAddress();
2726
2727 // Get the source value, truncated to the width of the bit-field.
2728 llvm::Value *SrcVal = Src.getScalarVal();
2729
2730 // Cast the source to the storage type and shift it into place.
2731 SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(),
2732 /*isSigned=*/false);
2733 llvm::Value *MaskedVal = SrcVal;
2734
2735 const bool UseVolatile =
2736 CGM.getCodeGenOpts().AAPCSBitfieldWidth && Dst.isVolatileQualified() &&
2737 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
2738 const unsigned StorageSize =
2739 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
2740 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
2741 // See if there are other bits in the bitfield's storage we'll need to load
2742 // and mask together with source before storing.
2743 if (StorageSize != Info.Size) {
2744 assert(StorageSize > Info.Size && "Invalid bitfield size.");
2745 llvm::Value *Val =
2746 Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
2747
2748 // Mask the source value as needed.
2749 if (!Dst.getType()->hasBooleanRepresentation())
2750 SrcVal = Builder.CreateAnd(
2751 SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
2752 "bf.value");
2753 MaskedVal = SrcVal;
2754 if (Offset)
2755 SrcVal = Builder.CreateShl(SrcVal, Offset, "bf.shl");
2756
2757 // Mask out the original value.
2758 Val = Builder.CreateAnd(
2759 Val, ~llvm::APInt::getBitsSet(StorageSize, Offset, Offset + Info.Size),
2760 "bf.clear");
2761
2762 // Or together the unchanged values and the source value.
2763 SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
2764 } else {
2765 assert(Offset == 0);
2766 // According to the AACPS:
2767 // When a volatile bit-field is written, and its container does not overlap
2768 // with any non-bit-field member, its container must be read exactly once
2769 // and written exactly once using the access width appropriate to the type
2770 // of the container. The two accesses are not atomic.
2771 if (Dst.isVolatileQualified() && isAAPCS(CGM.getTarget()) &&
2772 CGM.getCodeGenOpts().ForceAAPCSBitfieldLoad)
2773 Builder.CreateLoad(Ptr, true, "bf.load");
2774 }
2775
2776 // Write the new value back out.
2777 auto *I = Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified());
2778 addInstToCurrentSourceAtom(I, SrcVal);
2779
2780 // Return the new value of the bit-field, if requested.
2781 if (Result) {
2782 llvm::Value *ResultVal = MaskedVal;
2783
2784 // Sign extend the value if needed.
2785 if (Info.IsSigned) {
2786 assert(Info.Size <= StorageSize);
2787 unsigned HighBits = StorageSize - Info.Size;
2788 if (HighBits) {
2789 ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
2790 ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
2791 }
2792 }
2793
2794 ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
2795 "bf.result.cast");
2796 *Result = EmitFromMemory(ResultVal, Dst.getType());
2797 }
2798}
2799
2801 LValue Dst) {
2802 llvm::Value *SrcVal = Src.getScalarVal();
2803 Address DstAddr = Dst.getExtVectorAddress();
2804 if (DstAddr.getElementType()->getScalarSizeInBits() >
2805 SrcVal->getType()->getScalarSizeInBits())
2806 SrcVal = Builder.CreateZExt(
2807 SrcVal, convertTypeForLoadStore(Dst.getType(), SrcVal->getType()));
2808
2809 // HLSL allows storing to scalar values through ExtVector component LValues.
2810 // To support this we need to handle the case where the destination address is
2811 // a scalar.
2812 if (!DstAddr.getElementType()->isVectorTy()) {
2813 assert(!Dst.getType()->isVectorType() &&
2814 "this should only occur for non-vector l-values");
2815 Builder.CreateStore(SrcVal, DstAddr, Dst.isVolatileQualified());
2816 return;
2817 }
2818
2819 // This access turns into a read/modify/write of the vector. Load the input
2820 // value now.
2821 llvm::Value *Vec = Builder.CreateLoad(DstAddr, Dst.isVolatileQualified());
2822 llvm::Type *VecTy = Vec->getType();
2823 const llvm::Constant *Elts = Dst.getExtVectorElts();
2824
2825 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
2826 unsigned NumSrcElts = VTy->getNumElements();
2827 unsigned NumDstElts = cast<llvm::FixedVectorType>(VecTy)->getNumElements();
2828 if (NumDstElts == NumSrcElts) {
2829 // Use shuffle vector is the src and destination are the same number of
2830 // elements and restore the vector mask since it is on the side it will be
2831 // stored.
2832 SmallVector<int, 4> Mask(NumDstElts);
2833 for (unsigned i = 0; i != NumSrcElts; ++i)
2834 Mask[getAccessedFieldNo(i, Elts)] = i;
2835
2836 Vec = Builder.CreateShuffleVector(SrcVal, Mask);
2837 } else if (NumDstElts > NumSrcElts) {
2838 // Extended the source vector to the same length and then shuffle it
2839 // into the destination.
2840 // FIXME: since we're shuffling with undef, can we just use the indices
2841 // into that? This could be simpler.
2842 SmallVector<int, 4> ExtMask;
2843 for (unsigned i = 0; i != NumSrcElts; ++i)
2844 ExtMask.push_back(i);
2845 ExtMask.resize(NumDstElts, -1);
2846 llvm::Value *ExtSrcVal = Builder.CreateShuffleVector(SrcVal, ExtMask);
2847 // build identity
2849 for (unsigned i = 0; i != NumDstElts; ++i)
2850 Mask.push_back(i);
2851
2852 // When the vector size is odd and .odd or .hi is used, the last element
2853 // of the Elts constant array will be one past the size of the vector.
2854 // Ignore the last element here, if it is greater than the mask size.
2855 if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
2856 NumSrcElts--;
2857
2858 // modify when what gets shuffled in
2859 for (unsigned i = 0; i != NumSrcElts; ++i)
2860 Mask[getAccessedFieldNo(i, Elts)] = i + NumDstElts;
2861 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, Mask);
2862 } else {
2863 // We should never shorten the vector
2864 llvm_unreachable("unexpected shorten vector length");
2865 }
2866 } else {
2867 // If the Src is a scalar (not a vector), and the target is a vector it must
2868 // be updating one element.
2869 unsigned InIdx = getAccessedFieldNo(0, Elts);
2870 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
2871
2872 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
2873 }
2874
2875 Builder.CreateStore(Vec, Dst.getExtVectorAddress(),
2876 Dst.isVolatileQualified());
2877}
2878
2879/// Store of global named registers are always calls to intrinsics.
2881 assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) &&
2882 "Bad type for register variable");
2883 llvm::MDNode *RegName = cast<llvm::MDNode>(
2884 cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata());
2885 assert(RegName && "Register LValue is not metadata");
2886
2887 // We accept integer and pointer types only
2888 llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType());
2889 llvm::Type *Ty = OrigTy;
2890 if (OrigTy->isPointerTy())
2891 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2892 llvm::Type *Types[] = { Ty };
2893
2894 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
2895 llvm::Value *Value = Src.getScalarVal();
2896 if (OrigTy->isPointerTy())
2897 Value = Builder.CreatePtrToInt(Value, Ty);
2898 Builder.CreateCall(
2899 F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value});
2900}
2901
2902// setObjCGCLValueClass - sets class of the lvalue for the purpose of
2903// generating write-barries API. It is currently a global, ivar,
2904// or neither.
2905static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
2906 LValue &LV,
2907 bool IsMemberAccess=false) {
2908 if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
2909 return;
2910
2911 if (isa<ObjCIvarRefExpr>(E)) {
2912 QualType ExpTy = E->getType();
2913 if (IsMemberAccess && ExpTy->isPointerType()) {
2914 // If ivar is a structure pointer, assigning to field of
2915 // this struct follows gcc's behavior and makes it a non-ivar
2916 // writer-barrier conservatively.
2917 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2918 if (ExpTy->isRecordType()) {
2919 LV.setObjCIvar(false);
2920 return;
2921 }
2922 }
2923 LV.setObjCIvar(true);
2924 auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E));
2925 LV.setBaseIvarExp(Exp->getBase());
2926 LV.setObjCArray(E->getType()->isArrayType());
2927 return;
2928 }
2929
2930 if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
2931 if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
2932 if (VD->hasGlobalStorage()) {
2933 LV.setGlobalObjCRef(true);
2934 LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
2935 }
2936 }
2937 LV.setObjCArray(E->getType()->isArrayType());
2938 return;
2939 }
2940
2941 if (const auto *Exp = dyn_cast<UnaryOperator>(E)) {
2942 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2943 return;
2944 }
2945
2946 if (const auto *Exp = dyn_cast<ParenExpr>(E)) {
2947 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2948 if (LV.isObjCIvar()) {
2949 // If cast is to a structure pointer, follow gcc's behavior and make it
2950 // a non-ivar write-barrier.
2951 QualType ExpTy = E->getType();
2952 if (ExpTy->isPointerType())
2953 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2954 if (ExpTy->isRecordType())
2955 LV.setObjCIvar(false);
2956 }
2957 return;
2958 }
2959
2960 if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
2961 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
2962 return;
2963 }
2964
2965 if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
2966 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2967 return;
2968 }
2969
2970 if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
2971 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2972 return;
2973 }
2974
2975 if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
2976 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2977 return;
2978 }
2979
2980 if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
2981 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
2982 if (LV.isObjCIvar() && !LV.isObjCArray())
2983 // Using array syntax to assigning to what an ivar points to is not
2984 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
2985 LV.setObjCIvar(false);
2986 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
2987 // Using array syntax to assigning to what global points to is not
2988 // same as assigning to the global itself. {id *G;} G[i] = 0;
2989 LV.setGlobalObjCRef(false);
2990 return;
2991 }
2992
2993 if (const auto *Exp = dyn_cast<MemberExpr>(E)) {
2994 setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
2995 // We don't know if member is an 'ivar', but this flag is looked at
2996 // only in the context of LV.isObjCIvar().
2997 LV.setObjCArray(E->getType()->isArrayType());
2998 return;
2999 }
3000}
3001
3003 CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr,
3004 llvm::Type *RealVarTy, SourceLocation Loc) {
3005 if (CGF.CGM.getLangOpts().OpenMPIRBuilder)
3007 CGF, VD, Addr, Loc);
3008 else
3009 Addr =
3010 CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc);
3011
3012 Addr = Addr.withElementType(RealVarTy);
3014}
3015
3017 const VarDecl *VD, QualType T) {
3018 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3019 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3020 // Return an invalid address if variable is MT_To (or MT_Enter starting with
3021 // OpenMP 5.2) and unified memory is not enabled. For all other cases: MT_Link
3022 // and MT_To (or MT_Enter) with unified memory, return a valid address.
3023 if (!Res || ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3024 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3026 return Address::invalid();
3027 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
3028 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3029 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3031 "Expected link clause OR to clause with unified memory enabled.");
3032 QualType PtrTy = CGF.getContext().getPointerType(VD->getType());
3034 return CGF.EmitLoadOfPointer(Addr, PtrTy->castAs<PointerType>());
3035}
3036
3037Address
3039 LValueBaseInfo *PointeeBaseInfo,
3040 TBAAAccessInfo *PointeeTBAAInfo) {
3041 llvm::LoadInst *Load =
3042 Builder.CreateLoad(RefLVal.getAddress(), RefLVal.isVolatile());
3043 CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo());
3044 QualType PTy = RefLVal.getType()->getPointeeType();
3045 CharUnits Align = CGM.getNaturalTypeAlignment(
3046 PTy, PointeeBaseInfo, PointeeTBAAInfo, /*ForPointeeType=*/true);
3047 if (!PTy->isIncompleteType()) {
3048 llvm::LLVMContext &Ctx = getLLVMContext();
3049 llvm::MDBuilder MDB(Ctx);
3050 // Emit !nonnull metadata
3051 if (CGM.getTypes().getTargetAddressSpace(PTy) == 0 &&
3052 !CGM.getCodeGenOpts().NullPointerIsValid)
3053 Load->setMetadata(llvm::LLVMContext::MD_nonnull,
3054 llvm::MDNode::get(Ctx, {}));
3055 // Emit !align metadata
3056 if (PTy->isObjectType()) {
3057 auto AlignVal = Align.getQuantity();
3058 if (AlignVal > 1) {
3059 Load->setMetadata(
3060 llvm::LLVMContext::MD_align,
3061 llvm::MDNode::get(Ctx, MDB.createConstant(llvm::ConstantInt::get(
3062 Builder.getInt64Ty(), AlignVal))));
3063 }
3064 }
3065 }
3066 return makeNaturalAddressForPointer(Load, PTy, Align,
3067 /*ForPointeeType=*/true, PointeeBaseInfo,
3068 PointeeTBAAInfo);
3069}
3070
3072 LValueBaseInfo PointeeBaseInfo;
3073 TBAAAccessInfo PointeeTBAAInfo;
3074 Address PointeeAddr = EmitLoadOfReference(RefLVal, &PointeeBaseInfo,
3075 &PointeeTBAAInfo);
3076 return MakeAddrLValue(PointeeAddr, RefLVal.getType()->getPointeeType(),
3077 PointeeBaseInfo, PointeeTBAAInfo);
3078}
3079
3081 const PointerType *PtrTy,
3082 LValueBaseInfo *BaseInfo,
3083 TBAAAccessInfo *TBAAInfo) {
3084 llvm::Value *Addr = Builder.CreateLoad(Ptr);
3085 return makeNaturalAddressForPointer(Addr, PtrTy->getPointeeType(),
3086 CharUnits(), /*ForPointeeType=*/true,
3087 BaseInfo, TBAAInfo);
3088}
3089
3091 const PointerType *PtrTy) {
3092 LValueBaseInfo BaseInfo;
3093 TBAAAccessInfo TBAAInfo;
3094 Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &BaseInfo, &TBAAInfo);
3095 return MakeAddrLValue(Addr, PtrTy->getPointeeType(), BaseInfo, TBAAInfo);
3096}
3097
3099 const Expr *E, const VarDecl *VD) {
3100 QualType T = E->getType();
3101
3102 // If it's thread_local, emit a call to its wrapper function instead.
3103 if (VD->getTLSKind() == VarDecl::TLS_Dynamic &&
3105 return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T);
3106 // Check if the variable is marked as declare target with link clause in
3107 // device codegen.
3108 if (CGF.getLangOpts().OpenMPIsTargetDevice) {
3110 if (Addr.isValid())
3112 }
3113
3114 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
3115
3116 if (VD->getTLSKind() != VarDecl::TLS_None)
3117 V = CGF.Builder.CreateThreadLocalAddress(V);
3118
3119 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
3120 CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
3121 Address Addr(V, RealVarTy, Alignment);
3122 // Emit reference to the private copy of the variable if it is an OpenMP
3123 // threadprivate variable.
3124 if (CGF.getLangOpts().OpenMP && !CGF.getLangOpts().OpenMPSimd &&
3125 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
3126 return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy,
3127 E->getExprLoc());
3128 }
3129 LValue LV = VD->getType()->isReferenceType() ?
3133 setObjCGCLValueClass(CGF.getContext(), E, LV);
3134 return LV;
3135}
3136
3138 llvm::Type *Ty) {
3139 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3140 if (FD->hasAttr<WeakRefAttr>()) {
3142 return aliasee.getPointer();
3143 }
3144
3145 llvm::Constant *V = GetAddrOfFunction(GD, Ty);
3146 return V;
3147}
3148
3149static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E,
3150 GlobalDecl GD) {
3151 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3152 llvm::Constant *V = CGF.CGM.getFunctionPointer(GD);
3153 QualType ETy = E->getType();
3155 if (auto *GV = dyn_cast<llvm::GlobalValue>(V))
3156 V = llvm::NoCFIValue::get(GV);
3157 }
3158 CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
3159 return CGF.MakeAddrLValue(V, ETy, Alignment, AlignmentSource::Decl);
3160}
3161
3163 llvm::Value *ThisValue) {
3164
3165 return CGF.EmitLValueForLambdaField(FD, ThisValue);
3166}
3167
3168/// Named Registers are named metadata pointing to the register name
3169/// which will be read from/written to as an argument to the intrinsic
3170/// @llvm.read/write_register.
3171/// So far, only the name is being passed down, but other options such as
3172/// register type, allocation type or even optimization options could be
3173/// passed down via the metadata node.
3174static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM) {
3175 SmallString<64> Name("llvm.named.register.");
3176 AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
3177 assert(Asm->getLabel().size() < 64-Name.size() &&
3178 "Register name too big");
3179 Name.append(Asm->getLabel());
3180 llvm::NamedMDNode *M =
3181 CGM.getModule().getOrInsertNamedMetadata(Name);
3182 if (M->getNumOperands() == 0) {
3183 llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(),
3184 Asm->getLabel());
3185 llvm::Metadata *Ops[] = {Str};
3186 M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
3187 }
3188
3189 CharUnits Alignment = CGM.getContext().getDeclAlign(VD);
3190
3191 llvm::Value *Ptr =
3192 llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0));
3193 return LValue::MakeGlobalReg(Ptr, Alignment, VD->getType());
3194}
3195
3196/// Determine whether we can emit a reference to \p VD from the current
3197/// context, despite not necessarily having seen an odr-use of the variable in
3198/// this context.
3200 const DeclRefExpr *E,
3201 const VarDecl *VD) {
3202 // For a variable declared in an enclosing scope, do not emit a spurious
3203 // reference even if we have a capture, as that will emit an unwarranted
3204 // reference to our capture state, and will likely generate worse code than
3205 // emitting a local copy.
3207 return false;
3208
3209 // For a local declaration declared in this function, we can always reference
3210 // it even if we don't have an odr-use.
3211 if (VD->hasLocalStorage()) {
3212 return VD->getDeclContext() ==
3213 dyn_cast_or_null<DeclContext>(CGF.CurCodeDecl);
3214 }
3215
3216 // For a global declaration, we can emit a reference to it if we know
3217 // for sure that we are able to emit a definition of it.
3218 VD = VD->getDefinition(CGF.getContext());
3219 if (!VD)
3220 return false;
3221
3222 // Don't emit a spurious reference if it might be to a variable that only
3223 // exists on a different device / target.
3224 // FIXME: This is unnecessarily broad. Check whether this would actually be a
3225 // cross-target reference.
3226 if (CGF.getLangOpts().OpenMP || CGF.getLangOpts().CUDA ||
3227 CGF.getLangOpts().OpenCL) {
3228 return false;
3229 }
3230
3231 // We can emit a spurious reference only if the linkage implies that we'll
3232 // be emitting a non-interposable symbol that will be retained until link
3233 // time.
3234 switch (CGF.CGM.getLLVMLinkageVarDefinition(VD)) {
3235 case llvm::GlobalValue::ExternalLinkage:
3236 case llvm::GlobalValue::LinkOnceODRLinkage:
3237 case llvm::GlobalValue::WeakODRLinkage:
3238 case llvm::GlobalValue::InternalLinkage:
3239 case llvm::GlobalValue::PrivateLinkage:
3240 return true;
3241 default:
3242 return false;
3243 }
3244}
3245
3247 const NamedDecl *ND = E->getDecl();
3248 QualType T = E->getType();
3249
3250 assert(E->isNonOdrUse() != NOUR_Unevaluated &&
3251 "should not emit an unevaluated operand");
3252
3253 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
3254 // Global Named registers access via intrinsics only
3255 if (VD->getStorageClass() == SC_Register &&
3256 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
3257 return EmitGlobalNamedRegister(VD, CGM);
3258
3259 // If this DeclRefExpr does not constitute an odr-use of the variable,
3260 // we're not permitted to emit a reference to it in general, and it might
3261 // not be captured if capture would be necessary for a use. Emit the
3262 // constant value directly instead.
3263 if (E->isNonOdrUse() == NOUR_Constant &&
3264 (VD->getType()->isReferenceType() ||
3265 !canEmitSpuriousReferenceToVariable(*this, E, VD))) {
3266 VD->getAnyInitializer(VD);
3267 llvm::Constant *Val = ConstantEmitter(*this).emitAbstract(
3268 E->getLocation(), *VD->evaluateValue(), VD->getType());
3269 assert(Val && "failed to emit constant expression");
3270
3272 if (!VD->getType()->isReferenceType()) {
3273 // Spill the constant value to a global.
3274 Addr = CGM.createUnnamedGlobalFrom(*VD, Val,
3275 getContext().getDeclAlign(VD));
3276 llvm::Type *VarTy = getTypes().ConvertTypeForMem(VD->getType());
3277 auto *PTy = llvm::PointerType::get(
3278 getLLVMContext(), getTypes().getTargetAddressSpace(VD->getType()));
3279 Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PTy, VarTy);
3280 } else {
3281 // Should we be using the alignment of the constant pointer we emitted?
3282 CharUnits Alignment =
3283 CGM.getNaturalTypeAlignment(E->getType(),
3284 /* BaseInfo= */ nullptr,
3285 /* TBAAInfo= */ nullptr,
3286 /* forPointeeType= */ true);
3287 Addr = makeNaturalAddressForPointer(Val, T, Alignment);
3288 }
3290 }
3291
3292 // FIXME: Handle other kinds of non-odr-use DeclRefExprs.
3293
3294 // Check for captured variables.
3296 VD = VD->getCanonicalDecl();
3297 if (auto *FD = LambdaCaptureFields.lookup(VD))
3298 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
3299 if (CapturedStmtInfo) {
3300 auto I = LocalDeclMap.find(VD);
3301 if (I != LocalDeclMap.end()) {
3302 LValue CapLVal;
3303 if (VD->getType()->isReferenceType())
3304 CapLVal = EmitLoadOfReferenceLValue(I->second, VD->getType(),
3306 else
3307 CapLVal = MakeAddrLValue(I->second, T);
3308 // Mark lvalue as nontemporal if the variable is marked as nontemporal
3309 // in simd context.
3310 if (getLangOpts().OpenMP &&
3311 CGM.getOpenMPRuntime().isNontemporalDecl(VD))
3312 CapLVal.setNontemporal(/*Value=*/true);
3313 return CapLVal;
3314 }
3315 LValue CapLVal =
3316 EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD),
3317 CapturedStmtInfo->getContextValue());
3318 Address LValueAddress = CapLVal.getAddress();
3319 CapLVal = MakeAddrLValue(Address(LValueAddress.emitRawPointer(*this),
3320 LValueAddress.getElementType(),
3321 getContext().getDeclAlign(VD)),
3322 CapLVal.getType(),
3324 CapLVal.getTBAAInfo());
3325 // Mark lvalue as nontemporal if the variable is marked as nontemporal
3326 // in simd context.
3327 if (getLangOpts().OpenMP &&
3328 CGM.getOpenMPRuntime().isNontemporalDecl(VD))
3329 CapLVal.setNontemporal(/*Value=*/true);
3330 return CapLVal;
3331 }
3332
3333 assert(isa<BlockDecl>(CurCodeDecl));
3334 Address addr = GetAddrOfBlockDecl(VD);
3335 return MakeAddrLValue(addr, T, AlignmentSource::Decl);
3336 }
3337 }
3338
3339 // FIXME: We should be able to assert this for FunctionDecls as well!
3340 // FIXME: We should be able to assert this for all DeclRefExprs, not just
3341 // those with a valid source location.
3342 assert((ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() ||
3343 !E->getLocation().isValid()) &&
3344 "Should not use decl without marking it used!");
3345
3346 if (ND->hasAttr<WeakRefAttr>()) {
3347 const auto *VD = cast<ValueDecl>(ND);
3348 ConstantAddress Aliasee = CGM.GetWeakRefReference(VD);
3349 return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl);
3350 }
3351
3352 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
3353 // Check if this is a global variable.
3354 if (VD->hasLinkage() || VD->isStaticDataMember())
3355 return EmitGlobalVarDeclLValue(*this, E, VD);
3356
3357 Address addr = Address::invalid();
3358
3359 // The variable should generally be present in the local decl map.
3360 auto iter = LocalDeclMap.find(VD);
3361 if (iter != LocalDeclMap.end()) {
3362 addr = iter->second;
3363
3364 // Otherwise, it might be static local we haven't emitted yet for
3365 // some reason; most likely, because it's in an outer function.
3366 } else if (VD->isStaticLocal()) {
3367 llvm::Constant *var = CGM.getOrCreateStaticVarDecl(
3368 *VD, CGM.getLLVMLinkageVarDefinition(VD));
3369 addr = Address(
3370 var, ConvertTypeForMem(VD->getType()), getContext().getDeclAlign(VD));
3371
3372 // No other cases for now.
3373 } else {
3374 llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?");
3375 }
3376
3377 // Handle threadlocal function locals.
3378 if (VD->getTLSKind() != VarDecl::TLS_None)
3379 addr = addr.withPointer(
3380 Builder.CreateThreadLocalAddress(addr.getBasePointer()),
3382
3383 // Check for OpenMP threadprivate variables.
3384 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
3385 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
3387 *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()),
3388 E->getExprLoc());
3389 }
3390
3391 // Drill into block byref variables.
3392 bool isBlockByref = VD->isEscapingByref();
3393 if (isBlockByref) {
3394 addr = emitBlockByrefAddress(addr, VD);
3395 }
3396
3397 // Drill into reference types.
3398 LValue LV = VD->getType()->isReferenceType() ?
3401
3402 bool isLocalStorage = VD->hasLocalStorage();
3403
3404 bool NonGCable = isLocalStorage &&
3405 !VD->getType()->isReferenceType() &&
3406 !isBlockByref;
3407 if (NonGCable) {
3409 LV.setNonGC(true);
3410 }
3411
3412 bool isImpreciseLifetime =
3413 (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
3414 if (isImpreciseLifetime)
3417 return LV;
3418 }
3419
3420 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
3421 return EmitFunctionDeclLValue(*this, E, FD);
3422
3423 // FIXME: While we're emitting a binding from an enclosing scope, all other
3424 // DeclRefExprs we see should be implicitly treated as if they also refer to
3425 // an enclosing scope.
3426 if (const auto *BD = dyn_cast<BindingDecl>(ND)) {
3428 auto *FD = LambdaCaptureFields.lookup(BD);
3429 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
3430 }
3431 // Suppress debug location updates when visiting the binding, since the
3432 // binding may emit instructions that would otherwise be associated with the
3433 // binding itself, rather than the expression referencing the binding. (this
3434 // leads to jumpy debug stepping behavior where the location/debugger jump
3435 // back to the binding declaration, then back to the expression referencing
3436 // the binding)
3438 return EmitLValue(BD->getBinding(), NotKnownNonNull);
3439 }
3440
3441 // We can form DeclRefExprs naming GUID declarations when reconstituting
3442 // non-type template parameters into expressions.
3443 if (const auto *GD = dyn_cast<MSGuidDecl>(ND))
3444 return MakeAddrLValue(CGM.GetAddrOfMSGuidDecl(GD), T,
3446
3447 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
3448 auto ATPO = CGM.GetAddrOfTemplateParamObject(TPO);
3449 auto AS = getLangASFromTargetAS(ATPO.getAddressSpace());
3450
3451 if (AS != T.getAddressSpace()) {
3452 auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace());
3453 auto PtrTy = llvm::PointerType::get(CGM.getLLVMContext(), TargetAS);
3454 auto ASC = getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(),
3455 AS, PtrTy);
3456 ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment());
3457 }
3458
3459 return MakeAddrLValue(ATPO, T, AlignmentSource::Decl);
3460 }
3461
3462 llvm_unreachable("Unhandled DeclRefExpr");
3463}
3464
3466 // __extension__ doesn't affect lvalue-ness.
3467 if (E->getOpcode() == UO_Extension)
3468 return EmitLValue(E->getSubExpr());
3469
3471 switch (E->getOpcode()) {
3472 default: llvm_unreachable("Unknown unary operator lvalue!");
3473 case UO_Deref: {
3475 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
3476
3477 LValueBaseInfo BaseInfo;
3478 TBAAAccessInfo TBAAInfo;
3480 &TBAAInfo);
3481 LValue LV = MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo);
3483
3484 // We should not generate __weak write barrier on indirect reference
3485 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
3486 // But, we continue to generate __strong write barrier on indirect write
3487 // into a pointer to object.
3488 if (getLangOpts().ObjC &&
3489 getLangOpts().getGC() != LangOptions::NonGC &&
3490 LV.isObjCWeak())
3492 return LV;
3493 }
3494 case UO_Real:
3495 case UO_Imag: {
3496 LValue LV = EmitLValue(E->getSubExpr());
3497 assert(LV.isSimple() && "real/imag on non-ordinary l-value");
3498
3499 // __real is valid on scalars. This is a faster way of testing that.
3500 // __imag can only produce an rvalue on scalars.
3501 if (E->getOpcode() == UO_Real &&
3502 !LV.getAddress().getElementType()->isStructTy()) {
3503 assert(E->getSubExpr()->getType()->isArithmeticType());
3504 return LV;
3505 }
3506
3507 QualType T = ExprTy->castAs<ComplexType>()->getElementType();
3508
3509 Address Component =
3510 (E->getOpcode() == UO_Real
3513 LValue ElemLV = MakeAddrLValue(Component, T, LV.getBaseInfo(),
3514 CGM.getTBAAInfoForSubobject(LV, T));
3515 ElemLV.getQuals().addQualifiers(LV.getQuals());
3516 return ElemLV;
3517 }
3518 case UO_PreInc:
3519 case UO_PreDec: {
3520 LValue LV = EmitLValue(E->getSubExpr());
3521 bool isInc = E->getOpcode() == UO_PreInc;
3522
3523 if (E->getType()->isAnyComplexType())
3524 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
3525 else
3526 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
3527 return LV;
3528 }
3529 }
3530}
3531
3533 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
3535}
3536
3538 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
3540}
3541
3543 auto SL = E->getFunctionName();
3544 assert(SL != nullptr && "No StringLiteral name in PredefinedExpr");
3545 StringRef FnName = CurFn->getName();
3546 FnName.consume_front("\01");
3547 StringRef NameItems[] = {
3549 std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
3550 if (auto *BD = dyn_cast_or_null<BlockDecl>(CurCodeDecl)) {
3551 std::string Name = std::string(SL->getString());
3552 if (!Name.empty()) {
3553 unsigned Discriminator =
3554 CGM.getCXXABI().getMangleContext().getBlockId(BD, true);
3555 if (Discriminator)
3556 Name += "_" + Twine(Discriminator + 1).str();
3557 auto C = CGM.GetAddrOfConstantCString(Name, GVName);
3559 } else {
3560 auto C = CGM.GetAddrOfConstantCString(std::string(FnName), GVName);
3562 }
3563 }
3564 auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
3566}
3567
3568/// Emit a type description suitable for use by a runtime sanitizer library. The
3569/// format of a type descriptor is
3570///
3571/// \code
3572/// { i16 TypeKind, i16 TypeInfo }
3573/// \endcode
3574///
3575/// followed by an array of i8 containing the type name with extra information
3576/// for BitInt. TypeKind is TK_Integer(0) for an integer, TK_Float(1) for a
3577/// floating point value, TK_BitInt(2) for BitInt and TK_Unknown(0xFFFF) for
3578/// anything else.
3580 // Only emit each type's descriptor once.
3581 if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
3582 return C;
3583
3584 uint16_t TypeKind = TK_Unknown;
3585 uint16_t TypeInfo = 0;
3586 bool IsBitInt = false;
3587
3588 if (T->isIntegerType()) {
3589 TypeKind = TK_Integer;
3590 TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
3591 (T->isSignedIntegerType() ? 1 : 0);
3592 // Follow suggestion from discussion of issue 64100.
3593 // So we can write the exact amount of bits in TypeName after '\0'
3594 // making it <diagnostic-like type name>.'\0'.<32-bit width>.
3595 if (T->isSignedIntegerType() && T->getAs<BitIntType>()) {
3596 // Do a sanity checks as we are using 32-bit type to store bit length.
3597 assert(getContext().getTypeSize(T) > 0 &&
3598 " non positive amount of bits in __BitInt type");
3599 assert(getContext().getTypeSize(T) <= 0xFFFFFFFF &&
3600 " too many bits in __BitInt type");
3601
3602 // Redefine TypeKind with the actual __BitInt type if we have signed
3603 // BitInt.
3604 TypeKind = TK_BitInt;
3605 IsBitInt = true;
3606 }
3607 } else if (T->isFloatingType()) {
3608 TypeKind = TK_Float;
3610 }
3611
3612 // Format the type name as if for a diagnostic, including quotes and
3613 // optionally an 'aka'.
3614 SmallString<32> Buffer;
3615 CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
3616 (intptr_t)T.getAsOpaquePtr(), StringRef(),
3617 StringRef(), {}, Buffer, {});
3618
3619 if (IsBitInt) {
3620 // The Structure is: 0 to end the string, 32 bit unsigned integer in target
3621 // endianness, zero.
3622 char S[6] = {'\0', '\0', '\0', '\0', '\0', '\0'};
3623 const auto *EIT = T->castAs<BitIntType>();
3624 uint32_t Bits = EIT->getNumBits();
3625 llvm::support::endian::write32(S + 1, Bits,
3626 getTarget().isBigEndian()
3627 ? llvm::endianness::big
3628 : llvm::endianness::little);
3629 StringRef Str = StringRef(S, sizeof(S) / sizeof(decltype(S[0])));
3630 Buffer.append(Str);
3631 }
3632
3633 llvm::Constant *Components[] = {
3634 Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
3635 llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
3636 };
3637 llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
3638
3639 auto *GV = new llvm::GlobalVariable(
3640 CGM.getModule(), Descriptor->getType(),
3641 /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
3642 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3643 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
3644
3645 // Remember the descriptor for this type.
3646 CGM.setTypeDescriptorInMap(T, GV);
3647
3648 return GV;
3649}
3650
3651llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
3652 llvm::Type *TargetTy = IntPtrTy;
3653
3654 if (V->getType() == TargetTy)
3655 return V;
3656
3657 // Floating-point types which fit into intptr_t are bitcast to integers
3658 // and then passed directly (after zero-extension, if necessary).
3659 if (V->getType()->isFloatingPointTy()) {
3660 unsigned Bits = V->getType()->getPrimitiveSizeInBits().getFixedValue();
3661 if (Bits <= TargetTy->getIntegerBitWidth())
3662 V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
3663 Bits));
3664 }
3665
3666 // Integers which fit in intptr_t are zero-extended and passed directly.
3667 if (V->getType()->isIntegerTy() &&
3668 V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
3669 return Builder.CreateZExt(V, TargetTy);
3670
3671 // Pointers are passed directly, everything else is passed by address.
3672 if (!V->getType()->isPointerTy()) {
3673 RawAddress Ptr = CreateDefaultAlignTempAlloca(V->getType());
3674 Builder.CreateStore(V, Ptr);
3675 V = Ptr.getPointer();
3676 }
3677 return Builder.CreatePtrToInt(V, TargetTy);
3678}
3679
3680/// Emit a representation of a SourceLocation for passing to a handler
3681/// in a sanitizer runtime library. The format for this data is:
3682/// \code
3683/// struct SourceLocation {
3684/// const char *Filename;
3685/// int32_t Line, Column;
3686/// };
3687/// \endcode
3688/// For an invalid SourceLocation, the Filename pointer is null.
3690 llvm::Constant *Filename;
3691 int Line, Column;
3692
3694 if (PLoc.isValid()) {
3695 StringRef FilenameString = PLoc.getFilename();
3696
3697 int PathComponentsToStrip =
3698 CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip;
3699 if (PathComponentsToStrip < 0) {
3700 assert(PathComponentsToStrip != INT_MIN);
3701 int PathComponentsToKeep = -PathComponentsToStrip;
3702 auto I = llvm::sys::path::rbegin(FilenameString);
3703 auto E = llvm::sys::path::rend(FilenameString);
3704 while (I != E && --PathComponentsToKeep)
3705 ++I;
3706
3707 FilenameString = FilenameString.substr(I - E);
3708 } else if (PathComponentsToStrip > 0) {
3709 auto I = llvm::sys::path::begin(FilenameString);
3710 auto E = llvm::sys::path::end(FilenameString);
3711 while (I != E && PathComponentsToStrip--)
3712 ++I;
3713
3714 if (I != E)
3715 FilenameString =
3716 FilenameString.substr(I - llvm::sys::path::begin(FilenameString));
3717 else
3718 FilenameString = llvm::sys::path::filename(FilenameString);
3719 }
3720
3721 auto FilenameGV =
3722 CGM.GetAddrOfConstantCString(std::string(FilenameString), ".src");
3723 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(
3725 FilenameGV.getPointer()->stripPointerCasts()));
3726 Filename = FilenameGV.getPointer();
3727 Line = PLoc.getLine();
3728 Column = PLoc.getColumn();
3729 } else {
3730 Filename = llvm::Constant::getNullValue(Int8PtrTy);
3731 Line = Column = 0;
3732 }
3733
3734 llvm::Constant *Data[] = {Filename, Builder.getInt32(Line),
3735 Builder.getInt32(Column)};
3736
3737 return llvm::ConstantStruct::getAnon(Data);
3738}
3739
3740namespace {
3741/// Specify under what conditions this check can be recovered
3742enum class CheckRecoverableKind {
3743 /// Always terminate program execution if this check fails.
3745 /// Check supports recovering, runtime has both fatal (noreturn) and
3746 /// non-fatal handlers for this check.
3747 Recoverable,
3748 /// Runtime conditionally aborts, always need to support recovery.
3750};
3751}
3752
3753static CheckRecoverableKind
3755 if (Ordinal == SanitizerKind::SO_Vptr)
3756 return CheckRecoverableKind::AlwaysRecoverable;
3757 else if (Ordinal == SanitizerKind::SO_Return ||
3758 Ordinal == SanitizerKind::SO_Unreachable)
3759 return CheckRecoverableKind::Unrecoverable;
3760 else
3761 return CheckRecoverableKind::Recoverable;
3762}
3763
3764namespace {
3765struct SanitizerHandlerInfo {
3766 char const *const Name;
3767 unsigned Version;
3768};
3769}
3770
3771const SanitizerHandlerInfo SanitizerHandlers[] = {
3772#define SANITIZER_CHECK(Enum, Name, Version, Msg) {#Name, Version},
3774#undef SANITIZER_CHECK
3775};
3776
3778 llvm::FunctionType *FnType,
3780 SanitizerHandler CheckHandler,
3781 CheckRecoverableKind RecoverKind, bool IsFatal,
3782 llvm::BasicBlock *ContBB, bool NoMerge) {
3783 assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable);
3784 std::optional<ApplyDebugLocation> DL;
3785 if (!CGF.Builder.getCurrentDebugLocation()) {
3786 // Ensure that the call has at least an artificial debug location.
3787 DL.emplace(CGF, SourceLocation());
3788 }
3789 bool NeedsAbortSuffix =
3790 IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
3791 bool MinimalRuntime = CGF.CGM.getCodeGenOpts().SanitizeMinimalRuntime;
3792 const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler];
3793 const StringRef CheckName = CheckInfo.Name;
3794 std::string FnName = "__ubsan_handle_" + CheckName.str();
3795 if (CheckInfo.Version && !MinimalRuntime)
3796 FnName += "_v" + llvm::utostr(CheckInfo.Version);
3797 if (MinimalRuntime)
3798 FnName += "_minimal";
3799 if (NeedsAbortSuffix)
3800 FnName += "_abort";
3801 bool MayReturn =
3802 !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
3803
3804 llvm::AttrBuilder B(CGF.getLLVMContext());
3805 if (!MayReturn) {
3806 B.addAttribute(llvm::Attribute::NoReturn)
3807 .addAttribute(llvm::Attribute::NoUnwind);
3808 }
3809 B.addUWTableAttr(llvm::UWTableKind::Default);
3810
3811 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(
3812 FnType, FnName,
3813 llvm::AttributeList::get(CGF.getLLVMContext(),
3814 llvm::AttributeList::FunctionIndex, B),
3815 /*Local=*/true);
3816 llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
3817 NoMerge = NoMerge || !CGF.CGM.getCodeGenOpts().OptimizationLevel ||
3818 (CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
3819 if (NoMerge)
3820 HandlerCall->addFnAttr(llvm::Attribute::NoMerge);
3821 if (!MayReturn) {
3822 HandlerCall->setDoesNotReturn();
3823 CGF.Builder.CreateUnreachable();
3824 } else {
3825 CGF.Builder.CreateBr(ContBB);
3826 }
3827}
3828
3830 ArrayRef<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>> Checked,
3831 SanitizerHandler CheckHandler, ArrayRef<llvm::Constant *> StaticArgs,
3832 ArrayRef<llvm::Value *> DynamicArgs, const TrapReason *TR) {
3833 assert(IsSanitizerScope);
3834 assert(Checked.size() > 0);
3835 assert(CheckHandler >= 0 &&
3836 size_t(CheckHandler) < std::size(SanitizerHandlers));
3837 const StringRef CheckName = SanitizerHandlers[CheckHandler].Name;
3838
3839 llvm::Value *FatalCond = nullptr;
3840 llvm::Value *RecoverableCond = nullptr;
3841 llvm::Value *TrapCond = nullptr;
3842 bool NoMerge = false;
3843 // Expand checks into:
3844 // (Check1 || !allow_ubsan_check) && (Check2 || !allow_ubsan_check) ...
3845 // We need separate allow_ubsan_check intrinsics because they have separately
3846 // specified cutoffs.
3847 // This expression looks expensive but will be simplified after
3848 // LowerAllowCheckPass.
3849 for (auto &[Check, Ord] : Checked) {
3850 llvm::Value *GuardedCheck = Check;
3852 (CGM.getCodeGenOpts().SanitizeSkipHotCutoffs[Ord] > 0)) {
3853 llvm::Value *Allow = Builder.CreateCall(
3854 CGM.getIntrinsic(llvm::Intrinsic::allow_ubsan_check),
3855 llvm::ConstantInt::get(CGM.Int8Ty, Ord));
3856 GuardedCheck = Builder.CreateOr(Check, Builder.CreateNot(Allow));
3857 }
3858
3859 // -fsanitize-trap= overrides -fsanitize-recover=.
3860 llvm::Value *&Cond = CGM.getCodeGenOpts().SanitizeTrap.has(Ord) ? TrapCond
3861 : CGM.getCodeGenOpts().SanitizeRecover.has(Ord)
3862 ? RecoverableCond
3863 : FatalCond;
3864 Cond = Cond ? Builder.CreateAnd(Cond, GuardedCheck) : GuardedCheck;
3865
3866 if (!CGM.getCodeGenOpts().SanitizeMergeHandlers.has(Ord))
3867 NoMerge = true;
3868 }
3869
3870 if (TrapCond)
3871 EmitTrapCheck(TrapCond, CheckHandler, NoMerge, TR);
3872 if (!FatalCond && !RecoverableCond)
3873 return;
3874
3875 llvm::Value *JointCond;
3876 if (FatalCond && RecoverableCond)
3877 JointCond = Builder.CreateAnd(FatalCond, RecoverableCond);
3878 else
3879 JointCond = FatalCond ? FatalCond : RecoverableCond;
3880 assert(JointCond);
3881
3882 CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second);
3883 assert(SanOpts.has(Checked[0].second));
3884#ifndef NDEBUG
3885 for (int i = 1, n = Checked.size(); i < n; ++i) {
3886 assert(RecoverKind == getRecoverableKind(Checked[i].second) &&
3887 "All recoverable kinds in a single check must be same!");
3888 assert(SanOpts.has(Checked[i].second));
3889 }
3890#endif
3891
3892 llvm::BasicBlock *Cont = createBasicBlock("cont");
3893 llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName);
3894 llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers);
3895 // Give hint that we very much don't expect to execute the handler
3896 llvm::MDBuilder MDHelper(getLLVMContext());
3897 llvm::MDNode *Node = MDHelper.createLikelyBranchWeights();
3898 Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
3899 EmitBlock(Handlers);
3900
3901 // Clear arguments for the MinimalRuntime handler.
3902 if (CGM.getCodeGenOpts().SanitizeMinimalRuntime) {
3903 switch (CheckHandler) {
3904 case SanitizerHandler::TypeMismatch:
3905 // Pass value pointer only. It adds minimal overhead.
3906 StaticArgs = {};
3907 assert(DynamicArgs.size() == 1);
3908 break;
3909 default:
3910 // No arguments for other checks.
3911 StaticArgs = {};
3912 DynamicArgs = {};
3913 break;
3914 }
3915 }
3916
3917 // Handler functions take an i8* pointing to the (handler-specific) static
3918 // information block, followed by a sequence of intptr_t arguments
3919 // representing operand values.
3922
3923 Args.reserve(DynamicArgs.size() + 1);
3924 ArgTypes.reserve(DynamicArgs.size() + 1);
3925
3926 // Emit handler arguments and create handler function type.
3927 if (!StaticArgs.empty()) {
3928 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3929 auto *InfoPtr = new llvm::GlobalVariable(
3930 CGM.getModule(), Info->getType(),
3931 // Non-constant global is used in a handler to deduplicate reports.
3932 // TODO: change deduplication logic and make it constant.
3933 /*isConstant=*/false, llvm::GlobalVariable::PrivateLinkage, Info, "",
3934 nullptr, llvm::GlobalVariable::NotThreadLocal,
3935 CGM.getDataLayout().getDefaultGlobalsAddressSpace());
3936 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3937 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
3938 Args.push_back(InfoPtr);
3939 ArgTypes.push_back(Args.back()->getType());
3940 }
3941
3942 for (llvm::Value *DynamicArg : DynamicArgs) {
3943 Args.push_back(EmitCheckValue(DynamicArg));
3944 ArgTypes.push_back(IntPtrTy);
3945 }
3946
3947 llvm::FunctionType *FnType =
3948 llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
3949
3950 if (!FatalCond || !RecoverableCond) {
3951 // Simple case: we need to generate a single handler call, either
3952 // fatal, or non-fatal.
3953 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind,
3954 (FatalCond != nullptr), Cont, NoMerge);
3955 } else {
3956 // Emit two handler calls: first one for set of unrecoverable checks,
3957 // another one for recoverable.
3958 llvm::BasicBlock *NonFatalHandlerBB =
3959 createBasicBlock("non_fatal." + CheckName);
3960 llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName);
3961 Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
3962 EmitBlock(FatalHandlerBB);
3963 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, true,
3964 NonFatalHandlerBB, NoMerge);
3965 EmitBlock(NonFatalHandlerBB);
3966 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, false,
3967 Cont, NoMerge);
3968 }
3969
3970 EmitBlock(Cont);
3971}
3972
3974 SanitizerKind::SanitizerOrdinal Ordinal, llvm::Value *Cond,
3975 llvm::ConstantInt *TypeId, llvm::Value *Ptr,
3976 ArrayRef<llvm::Constant *> StaticArgs) {
3977 llvm::BasicBlock *Cont = createBasicBlock("cfi.cont");
3978
3979 llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath");
3980 llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB);
3981
3982 llvm::MDBuilder MDHelper(getLLVMContext());
3983 llvm::MDNode *Node = MDHelper.createLikelyBranchWeights();
3984 BI->setMetadata(llvm::LLVMContext::MD_prof, Node);
3985
3986 EmitBlock(CheckBB);
3987
3988 bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Ordinal);
3989
3990 llvm::CallInst *CheckCall;
3991 llvm::FunctionCallee SlowPathFn;
3992 if (WithDiag) {
3993 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3994 auto *InfoPtr =
3995 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
3996 llvm::GlobalVariable::PrivateLinkage, Info);
3997 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3998 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
3999
4000 SlowPathFn = CGM.getModule().getOrInsertFunction(
4001 "__cfi_slowpath_diag",
4002 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy},
4003 false));
4004 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr, InfoPtr});
4005 } else {
4006 SlowPathFn = CGM.getModule().getOrInsertFunction(
4007 "__cfi_slowpath",
4008 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false));
4009 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr});
4010 }
4011
4012 CGM.setDSOLocal(
4013 cast<llvm::GlobalValue>(SlowPathFn.getCallee()->stripPointerCasts()));
4014 CheckCall->setDoesNotThrow();
4015
4016 EmitBlock(Cont);
4017}
4018
4019// Emit a stub for __cfi_check function so that the linker knows about this
4020// symbol in LTO mode.
4022 llvm::Module *M = &CGM.getModule();
4023 ASTContext &C = getContext();
4024 QualType QInt64Ty = C.getIntTypeForBitwidth(64, false);
4025
4027 ImplicitParamDecl ArgCallsiteTypeId(C, QInt64Ty, ImplicitParamKind::Other);
4028 ImplicitParamDecl ArgAddr(C, C.VoidPtrTy, ImplicitParamKind::Other);
4029 ImplicitParamDecl ArgCFICheckFailData(C, C.VoidPtrTy,
4031 FnArgs.push_back(&ArgCallsiteTypeId);
4032 FnArgs.push_back(&ArgAddr);
4033 FnArgs.push_back(&ArgCFICheckFailData);
4034 const CGFunctionInfo &FI =
4035 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, FnArgs);
4036
4037 llvm::Function *F = llvm::Function::Create(
4038 llvm::FunctionType::get(VoidTy, {Int64Ty, VoidPtrTy, VoidPtrTy}, false),
4039 llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
4040 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
4041 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
4042 F->setAlignment(llvm::Align(4096));
4043 CGM.setDSOLocal(F);
4044
4045 llvm::LLVMContext &Ctx = M->getContext();
4046 llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
4047 // CrossDSOCFI pass is not executed if there is no executable code.
4048 SmallVector<llvm::Value*> Args{F->getArg(2), F->getArg(1)};
4049 llvm::CallInst::Create(M->getFunction("__cfi_check_fail"), Args, "", BB);
4050 llvm::ReturnInst::Create(Ctx, nullptr, BB);
4051}
4052
4053// This function is basically a switch over the CFI failure kind, which is
4054// extracted from CFICheckFailData (1st function argument). Each case is either
4055// llvm.trap or a call to one of the two runtime handlers, based on
4056// -fsanitize-trap and -fsanitize-recover settings. Default case (invalid
4057// failure kind) traps, but this should really never happen. CFICheckFailData
4058// can be nullptr if the calling module has -fsanitize-trap behavior for this
4059// check kind; in this case __cfi_check_fail traps as well.
4061 auto CheckHandler = SanitizerHandler::CFICheckFail;
4062 // TODO: the SanitizerKind is not yet determined for this check (and might
4063 // not even be available, if Data == nullptr). However, we still want to
4064 // annotate the instrumentation. We approximate this by using all the CFI
4065 // kinds.
4066 SanitizerDebugLocation SanScope(
4067 this,
4068 {SanitizerKind::SO_CFIVCall, SanitizerKind::SO_CFINVCall,
4069 SanitizerKind::SO_CFIDerivedCast, SanitizerKind::SO_CFIUnrelatedCast,
4070 SanitizerKind::SO_CFIICall},
4071 CheckHandler);
4072 FunctionArgList Args;
4077 Args.push_back(&ArgData);
4078 Args.push_back(&ArgAddr);
4079
4080 const CGFunctionInfo &FI =
4081 CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, Args);
4082
4083 llvm::Function *F = llvm::Function::Create(
4084 llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false),
4085 llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule());
4086
4087 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
4088 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
4089 F->setVisibility(llvm::GlobalValue::HiddenVisibility);
4090
4091 StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args,
4092 SourceLocation());
4093
4095
4096 // This function is not affected by NoSanitizeList. This function does
4097 // not have a source location, but "src:*" would still apply. Revert any
4098 // changes to SanOpts made in StartFunction.
4099 SanOpts = CGM.getLangOpts().Sanitize;
4100
4101 llvm::Value *Data =
4102 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false,
4103 CGM.getContext().VoidPtrTy, ArgData.getLocation());
4104 llvm::Value *Addr =
4105 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false,
4106 CGM.getContext().VoidPtrTy, ArgAddr.getLocation());
4107
4108 // Data == nullptr means the calling module has trap behaviour for this check.
4109 llvm::Value *DataIsNotNullPtr =
4110 Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy));
4111 // TODO: since there is no data, we don't know the CheckKind, and therefore
4112 // cannot inspect CGM.getCodeGenOpts().SanitizeMergeHandlers. We default to
4113 // NoMerge = false. Users can disable merging by disabling optimization.
4114 EmitTrapCheck(DataIsNotNullPtr, SanitizerHandler::CFICheckFail,
4115 /*NoMerge=*/false);
4116
4117 llvm::StructType *SourceLocationTy =
4118 llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty);
4119 llvm::StructType *CfiCheckFailDataTy =
4120 llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy);
4121
4122 llvm::Value *V = Builder.CreateConstGEP2_32(
4123 CfiCheckFailDataTy, Builder.CreatePointerCast(Data, DefaultPtrTy), 0, 0);
4124
4125 Address CheckKindAddr(V, Int8Ty, getIntAlign());
4126 llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr);
4127
4128 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
4129 CGM.getLLVMContext(),
4130 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
4131 llvm::Value *ValidVtable = Builder.CreateZExt(
4132 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
4133 {Addr, AllVtables}),
4134 IntPtrTy);
4135
4136 const std::pair<int, SanitizerKind::SanitizerOrdinal> CheckKinds[] = {
4137 {CFITCK_VCall, SanitizerKind::SO_CFIVCall},
4138 {CFITCK_NVCall, SanitizerKind::SO_CFINVCall},
4139 {CFITCK_DerivedCast, SanitizerKind::SO_CFIDerivedCast},
4140 {CFITCK_UnrelatedCast, SanitizerKind::SO_CFIUnrelatedCast},
4141 {CFITCK_ICall, SanitizerKind::SO_CFIICall}};
4142
4143 for (auto CheckKindOrdinalPair : CheckKinds) {
4144 int Kind = CheckKindOrdinalPair.first;
4145 SanitizerKind::SanitizerOrdinal Ordinal = CheckKindOrdinalPair.second;
4146
4147 // TODO: we could apply SanitizerAnnotateDebugInfo(Ordinal) instead of
4148 // relying on the SanitizerScope with all CFI ordinals
4149
4150 llvm::Value *Cond =
4151 Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
4152 if (CGM.getLangOpts().Sanitize.has(Ordinal))
4153 EmitCheck(std::make_pair(Cond, Ordinal), SanitizerHandler::CFICheckFail,
4154 {}, {Data, Addr, ValidVtable});
4155 else
4156 // TODO: we can't rely on CGM.getCodeGenOpts().SanitizeMergeHandlers.
4157 // Although the compiler allows SanitizeMergeHandlers to be set
4158 // independently of CGM.getLangOpts().Sanitize, Driver/SanitizerArgs.cpp
4159 // requires that SanitizeMergeHandlers is a subset of Sanitize.
4160 EmitTrapCheck(Cond, CheckHandler, /*NoMerge=*/false);
4161 }
4162
4164 // The only reference to this function will be created during LTO link.
4165 // Make sure it survives until then.
4166 CGM.addUsedGlobal(F);
4167}
4168
4170 if (SanOpts.has(SanitizerKind::Unreachable)) {
4171 auto CheckOrdinal = SanitizerKind::SO_Unreachable;
4172 auto CheckHandler = SanitizerHandler::BuiltinUnreachable;
4173 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
4174 EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()),
4175 CheckOrdinal),
4176 CheckHandler, EmitCheckSourceLocation(Loc), {});
4177 }
4178 Builder.CreateUnreachable();
4179}
4180
4181void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
4182 SanitizerHandler CheckHandlerID,
4183 bool NoMerge, const TrapReason *TR) {
4184 llvm::BasicBlock *Cont = createBasicBlock("cont");
4185
4186 // If we're optimizing, collapse all calls to trap down to just one per
4187 // check-type per function to save on code size.
4188 if ((int)TrapBBs.size() <= CheckHandlerID)
4189 TrapBBs.resize(CheckHandlerID + 1);
4190
4191 llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
4192
4193 llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
4194 llvm::StringRef TrapMessage;
4195 llvm::StringRef TrapCategory;
4196 auto DebugTrapReasonKind = CGM.getCodeGenOpts().getSanitizeDebugTrapReasons();
4197 if (TR && !TR->isEmpty() &&
4198 DebugTrapReasonKind ==
4200 TrapMessage = TR->getMessage();
4201 TrapCategory = TR->getCategory();
4202 } else {
4203 TrapMessage = GetUBSanTrapForHandler(CheckHandlerID);
4204 TrapCategory = "Undefined Behavior Sanitizer";
4205 }
4206
4207 if (getDebugInfo() && !TrapMessage.empty() &&
4208 DebugTrapReasonKind !=
4210 TrapLocation) {
4211 TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
4212 TrapLocation, TrapCategory, TrapMessage);
4213 }
4214
4215 NoMerge = NoMerge || !CGM.getCodeGenOpts().OptimizationLevel ||
4216 (CurCodeDecl && CurCodeDecl->hasAttr<OptimizeNoneAttr>());
4217
4218 llvm::MDBuilder MDHelper(getLLVMContext());
4219 if (TrapBB && !NoMerge) {
4220 auto Call = TrapBB->begin();
4221 assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB");
4222
4223 Call->applyMergedLocation(Call->getDebugLoc(), TrapLocation);
4224
4225 Builder.CreateCondBr(Checked, Cont, TrapBB,
4226 MDHelper.createLikelyBranchWeights());
4227 } else {
4228 TrapBB = createBasicBlock("trap");
4229 Builder.CreateCondBr(Checked, Cont, TrapBB,
4230 MDHelper.createLikelyBranchWeights());
4231 EmitBlock(TrapBB);
4232
4233 ApplyDebugLocation applyTrapDI(*this, TrapLocation);
4234
4235 llvm::CallInst *TrapCall =
4236 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::ubsantrap),
4237 llvm::ConstantInt::get(CGM.Int8Ty, CheckHandlerID));
4238
4239 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
4240 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
4241 CGM.getCodeGenOpts().TrapFuncName);
4242 TrapCall->addFnAttr(A);
4243 }
4244 if (NoMerge)
4245 TrapCall->addFnAttr(llvm::Attribute::NoMerge);
4246 TrapCall->setDoesNotReturn();
4247 TrapCall->setDoesNotThrow();
4248 Builder.CreateUnreachable();
4249 }
4250
4251 EmitBlock(Cont);
4252}
4253
4254llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) {
4255 llvm::CallInst *TrapCall =
4256 Builder.CreateCall(CGM.getIntrinsic(IntrID));
4257
4258 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
4259 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
4260 CGM.getCodeGenOpts().TrapFuncName);
4261 TrapCall->addFnAttr(A);
4262 }
4263
4265 TrapCall->addFnAttr(llvm::Attribute::NoMerge);
4266 return TrapCall;
4267}
4268
4270 LValueBaseInfo *BaseInfo,
4271 TBAAAccessInfo *TBAAInfo) {
4272 assert(E->getType()->isArrayType() &&
4273 "Array to pointer decay must have array source type!");
4274
4275 // Expressions of array type can't be bitfields or vector elements.
4276 LValue LV = EmitLValue(E);
4277 Address Addr = LV.getAddress();
4278
4279 // If the array type was an incomplete type, we need to make sure
4280 // the decay ends up being the right type.
4281 llvm::Type *NewTy = ConvertType(E->getType());
4282 Addr = Addr.withElementType(NewTy);
4283
4284 // Note that VLA pointers are always decayed, so we don't need to do
4285 // anything here.
4286 if (!E->getType()->isVariableArrayType()) {
4287 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
4288 "Expected pointer to array");
4289 Addr = Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
4290 }
4291
4292 // The result of this decay conversion points to an array element within the
4293 // base lvalue. However, since TBAA currently does not support representing
4294 // accesses to elements of member arrays, we conservatively represent accesses
4295 // to the pointee object as if it had no any base lvalue specified.
4296 // TODO: Support TBAA for member arrays.
4298 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
4299 if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(EltType);
4300
4301 return Addr.withElementType(ConvertTypeForMem(EltType));
4302}
4303
4304/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
4305/// array to pointer, return the array subexpression.
4306static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
4307 // If this isn't just an array->pointer decay, bail out.
4308 const auto *CE = dyn_cast<CastExpr>(E);
4309 if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
4310 return nullptr;
4311
4312 // If this is a decay from variable width array, bail out.
4313 const Expr *SubExpr = CE->getSubExpr();
4314 if (SubExpr->getType()->isVariableArrayType())
4315 return nullptr;
4316
4317 return SubExpr;
4318}
4319
4321 llvm::Type *elemType,
4322 llvm::Value *ptr,
4323 ArrayRef<llvm::Value*> indices,
4324 bool inbounds,
4325 bool signedIndices,
4326 SourceLocation loc,
4327 const llvm::Twine &name = "arrayidx") {
4328 if (inbounds) {
4329 return CGF.EmitCheckedInBoundsGEP(elemType, ptr, indices, signedIndices,
4331 name);
4332 } else {
4333 return CGF.Builder.CreateGEP(elemType, ptr, indices, name);
4334 }
4335}
4336
4339 llvm::Type *elementType, bool inbounds,
4340 bool signedIndices, SourceLocation loc,
4341 CharUnits align,
4342 const llvm::Twine &name = "arrayidx") {
4343 if (inbounds) {
4344 return CGF.EmitCheckedInBoundsGEP(addr, indices, elementType, signedIndices,
4346 align, name);
4347 } else {
4348 return CGF.Builder.CreateGEP(addr, indices, elementType, align, name);
4349 }
4350}
4351
4353 const VariableArrayType *vla) {
4354 QualType eltType;
4355 do {
4356 eltType = vla->getElementType();
4357 } while ((vla = ctx.getAsVariableArrayType(eltType)));
4358 return eltType;
4359}
4360
4362 return D && D->hasAttr<BPFPreserveStaticOffsetAttr>();
4363}
4364
4365static bool hasBPFPreserveStaticOffset(const Expr *E) {
4366 if (!E)
4367 return false;
4368 QualType PointeeType = E->getType()->getPointeeType();
4369 if (PointeeType.isNull())
4370 return false;
4371 if (const auto *BaseDecl = PointeeType->getAsRecordDecl())
4372 return hasBPFPreserveStaticOffset(BaseDecl);
4373 return false;
4374}
4375
4376// Wraps Addr with a call to llvm.preserve.static.offset intrinsic.
4378 Address &Addr) {
4379 if (!CGF.getTarget().getTriple().isBPF())
4380 return Addr;
4381
4382 llvm::Function *Fn =
4383 CGF.CGM.getIntrinsic(llvm::Intrinsic::preserve_static_offset);
4384 llvm::CallInst *Call = CGF.Builder.CreateCall(Fn, {Addr.emitRawPointer(CGF)});
4385 return Address(Call, Addr.getElementType(), Addr.getAlignment());
4386}
4387
4388/// Given an array base, check whether its member access belongs to a record
4389/// with preserve_access_index attribute or not.
4390static bool IsPreserveAIArrayBase(CodeGenFunction &CGF, const Expr *ArrayBase) {
4391 if (!ArrayBase || !CGF.getDebugInfo())
4392 return false;
4393
4394 // Only support base as either a MemberExpr or DeclRefExpr.
4395 // DeclRefExpr to cover cases like:
4396 // struct s { int a; int b[10]; };
4397 // struct s *p;
4398 // p[1].a
4399 // p[1] will generate a DeclRefExpr and p[1].a is a MemberExpr.
4400 // p->b[5] is a MemberExpr example.
4401 const Expr *E = ArrayBase->IgnoreImpCasts();
4402 if (const auto *ME = dyn_cast<MemberExpr>(E))
4403 return ME->getMemberDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
4404
4405 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
4406 const auto *VarDef = dyn_cast<VarDecl>(DRE->getDecl());
4407 if (!VarDef)
4408 return false;
4409
4410 const auto *PtrT = VarDef->getType()->getAs<PointerType>();
4411 if (!PtrT)
4412 return false;
4413
4414 const auto *PointeeT = PtrT->getPointeeType()
4416 if (const auto *RecT = dyn_cast<RecordType>(PointeeT))
4417 return RecT->getDecl()
4418 ->getMostRecentDecl()
4419 ->hasAttr<BPFPreserveAccessIndexAttr>();
4420 return false;
4421 }
4422
4423 return false;
4424}
4425
4428 QualType eltType, bool inbounds,
4429 bool signedIndices, SourceLocation loc,
4430 QualType *arrayType = nullptr,
4431 const Expr *Base = nullptr,
4432 const llvm::Twine &name = "arrayidx") {
4433 // All the indices except that last must be zero.
4434#ifndef NDEBUG
4435 for (auto *idx : indices.drop_back())
4436 assert(isa<llvm::ConstantInt>(idx) &&
4437 cast<llvm::ConstantInt>(idx)->isZero());
4438#endif
4439
4440 // Determine the element size of the statically-sized base. This is
4441 // the thing that the indices are expressed in terms of.
4442 if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) {
4443 eltType = getFixedSizeElementType(CGF.getContext(), vla);
4444 }
4445
4446 // We can use that to compute the best alignment of the element.
4447 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
4448 CharUnits eltAlign =
4449 getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize);
4450
4452 addr = wrapWithBPFPreserveStaticOffset(CGF, addr);
4453
4454 llvm::Value *eltPtr;
4455 auto LastIndex = dyn_cast<llvm::ConstantInt>(indices.back());
4456 if (!LastIndex ||
4458 addr = emitArraySubscriptGEP(CGF, addr, indices,
4459 CGF.ConvertTypeForMem(eltType), inbounds,
4460 signedIndices, loc, eltAlign, name);
4461 return addr;
4462 } else {
4463 // Remember the original array subscript for bpf target
4464 unsigned idx = LastIndex->getZExtValue();
4465 llvm::DIType *DbgInfo = nullptr;
4466 if (arrayType)
4467 DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(*arrayType, loc);
4468 eltPtr = CGF.Builder.CreatePreserveArrayAccessIndex(
4469 addr.getElementType(), addr.emitRawPointer(CGF), indices.size() - 1,
4470 idx, DbgInfo);
4471 }
4472
4473 return Address(eltPtr, CGF.ConvertTypeForMem(eltType), eltAlign);
4474}
4475
4476namespace {
4477
4478/// StructFieldAccess is a simple visitor class to grab the first l-value to
4479/// r-value cast Expr.
4480struct StructFieldAccess
4481 : public ConstStmtVisitor<StructFieldAccess, const Expr *> {
4482 const Expr *VisitCastExpr(const CastExpr *E) {
4483 if (E->getCastKind() == CK_LValueToRValue)
4484 return E;
4485 return Visit(E->getSubExpr());
4486 }
4487 const Expr *VisitParenExpr(const ParenExpr *E) {
4488 return Visit(E->getSubExpr());
4489 }
4490};
4491
4492} // end anonymous namespace
4493
4494/// The offset of a field from the beginning of the record.
4496 const FieldDecl *Field, int64_t &Offset) {
4497 ASTContext &Ctx = CGF.getContext();
4498 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
4499 unsigned FieldNo = 0;
4500
4501 for (const FieldDecl *FD : RD->fields()) {
4502 if (FD == Field) {
4503 Offset += Layout.getFieldOffset(FieldNo);
4504 return true;
4505 }
4506
4507 QualType Ty = FD->getType();
4508 if (Ty->isRecordType())
4509 if (getFieldOffsetInBits(CGF, Ty->getAsRecordDecl(), Field, Offset)) {
4510 Offset += Layout.getFieldOffset(FieldNo);
4511 return true;
4512 }
4513
4514 if (!RD->isUnion())
4515 ++FieldNo;
4516 }
4517
4518 return false;
4519}
4520
4521/// Returns the relative offset difference between \p FD1 and \p FD2.
4522/// \code
4523/// offsetof(struct foo, FD1) - offsetof(struct foo, FD2)
4524/// \endcode
4525/// Both fields must be within the same struct.
4526static std::optional<int64_t> getOffsetDifferenceInBits(CodeGenFunction &CGF,
4527 const FieldDecl *FD1,
4528 const FieldDecl *FD2) {
4529 const RecordDecl *FD1OuterRec =
4531 const RecordDecl *FD2OuterRec =
4533
4534 if (FD1OuterRec != FD2OuterRec)
4535 // Fields must be within the same RecordDecl.
4536 return std::optional<int64_t>();
4537
4538 int64_t FD1Offset = 0;
4539 if (!getFieldOffsetInBits(CGF, FD1OuterRec, FD1, FD1Offset))
4540 return std::optional<int64_t>();
4541
4542 int64_t FD2Offset = 0;
4543 if (!getFieldOffsetInBits(CGF, FD2OuterRec, FD2, FD2Offset))
4544 return std::optional<int64_t>();
4545
4546 return std::make_optional<int64_t>(FD1Offset - FD2Offset);
4547}
4548
4549/// EmitCountedByBoundsChecking - If the array being accessed has a "counted_by"
4550/// attribute, generate bounds checking code. The "count" field is at the top
4551/// level of the struct or in an anonymous struct, that's also at the top level.
4552/// Future expansions may allow the "count" to reside at any place in the
4553/// struct, but the value of "counted_by" will be a "simple" path to the count,
4554/// i.e. "a.b.count", so we shouldn't need the full force of EmitLValue or
4555/// similar to emit the correct GEP.
4557 const Expr *E, llvm::Value *Idx, Address Addr, QualType IdxTy,
4558 QualType ArrayTy, bool Accessed, bool FlexibleArray) {
4559 const auto *ME = dyn_cast<MemberExpr>(E->IgnoreImpCasts());
4560 if (!ME || !ME->getMemberDecl()->getType()->isCountAttributedType())
4561 return;
4562
4563 const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
4564 getLangOpts().getStrictFlexArraysLevel();
4565 if (FlexibleArray &&
4566 !ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel))
4567 return;
4568
4569 const FieldDecl *FD = cast<FieldDecl>(ME->getMemberDecl());
4570 const FieldDecl *CountFD = FD->findCountedByField();
4571 if (!CountFD)
4572 return;
4573
4574 if (std::optional<int64_t> Diff =
4575 getOffsetDifferenceInBits(*this, CountFD, FD)) {
4576 if (!Addr.isValid()) {
4577 // An invalid Address indicates we're checking a pointer array access.
4578 // Emit the checked L-Value here.
4580 Addr = LV.getAddress();
4581 }
4582
4583 // FIXME: The 'static_cast' is necessary, otherwise the result turns into a
4584 // uint64_t, which messes things up if we have a negative offset difference.
4585 Diff = *Diff / static_cast<int64_t>(CGM.getContext().getCharWidth());
4586
4587 // Create a GEP with the byte offset between the counted object and the
4588 // count and use that to load the count value.
4589 Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Int8PtrTy, Int8Ty);
4590
4591 llvm::Type *CountTy = ConvertType(CountFD->getType());
4592 llvm::Value *Res =
4593 Builder.CreateInBoundsGEP(Int8Ty, Addr.emitRawPointer(*this),
4594 Builder.getInt32(*Diff), ".counted_by.gep");
4595 Res = Builder.CreateAlignedLoad(CountTy, Res, getIntAlign(),
4596 ".counted_by.load");
4597
4598 // Now emit the bounds checking.
4599 EmitBoundsCheckImpl(E, Res, Idx, IdxTy, ArrayTy, Accessed);
4600 }
4601}
4602
4604 bool Accessed) {
4605 // The index must always be an integer, which is not an aggregate. Emit it
4606 // in lexical order (this complexity is, sadly, required by C++17).
4607 llvm::Value *IdxPre =
4608 (E->getLHS() == E->getIdx()) ? EmitScalarExpr(E->getIdx()) : nullptr;
4609 bool SignedIndices = false;
4610 auto EmitIdxAfterBase = [&, IdxPre](bool Promote) -> llvm::Value * {
4611 auto *Idx = IdxPre;
4612 if (E->getLHS() != E->getIdx()) {
4613 assert(E->getRHS() == E->getIdx() && "index was neither LHS nor RHS");
4614 Idx = EmitScalarExpr(E->getIdx());
4615 }
4616
4617 QualType IdxTy = E->getIdx()->getType();
4618 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
4619 SignedIndices |= IdxSigned;
4620
4621 if (SanOpts.has(SanitizerKind::ArrayBounds))
4622 EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
4623
4624 // Extend or truncate the index type to 32 or 64-bits.
4625 if (Promote && Idx->getType() != IntPtrTy)
4626 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
4627
4628 return Idx;
4629 };
4630 IdxPre = nullptr;
4631
4632 // If the base is a vector type, then we are forming a vector element lvalue
4633 // with this subscript.
4634 if (E->getBase()->getType()->isSubscriptableVectorType() &&
4636 // Emit the vector as an lvalue to get its address.
4637 LValue LHS = EmitLValue(E->getBase());
4638 auto *Idx = EmitIdxAfterBase(/*Promote*/false);
4639 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
4640 return LValue::MakeVectorElt(LHS.getAddress(), Idx, E->getBase()->getType(),
4641 LHS.getBaseInfo(), TBAAAccessInfo());
4642 }
4643
4644 // The HLSL runtime handle the subscript expression on global resource arrays.
4645 if (getLangOpts().HLSL && (E->getType()->isHLSLResourceRecord() ||
4647 std::optional<LValue> LV =
4648 CGM.getHLSLRuntime().emitResourceArraySubscriptExpr(E, *this);
4649 if (LV.has_value())
4650 return *LV;
4651 }
4652
4653 // All the other cases basically behave like simple offsetting.
4654
4655 // Handle the extvector case we ignored above.
4657 LValue LV = EmitLValue(E->getBase());
4658 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4660
4661 QualType EltType = LV.getType()->castAs<VectorType>()->getElementType();
4662 Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true,
4663 SignedIndices, E->getExprLoc());
4664 return MakeAddrLValue(Addr, EltType, LV.getBaseInfo(),
4665 CGM.getTBAAInfoForSubobject(LV, EltType));
4666 }
4667
4668 LValueBaseInfo EltBaseInfo;
4669 TBAAAccessInfo EltTBAAInfo;
4671 if (const VariableArrayType *vla =
4672 getContext().getAsVariableArrayType(E->getType())) {
4673 // The base must be a pointer, which is not an aggregate. Emit
4674 // it. It needs to be emitted first in case it's what captures
4675 // the VLA bounds.
4676 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4677 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4678
4679 // The element count here is the total number of non-VLA elements.
4680 llvm::Value *numElements = getVLASize(vla).NumElts;
4681
4682 // Effectively, the multiply by the VLA size is part of the GEP.
4683 // GEP indexes are signed, and scaling an index isn't permitted to
4684 // signed-overflow, so we use the same semantics for our explicit
4685 // multiply. We suppress this if overflow is not undefined behavior.
4686 if (getLangOpts().PointerOverflowDefined) {
4687 Idx = Builder.CreateMul(Idx, numElements);
4688 } else {
4689 Idx = Builder.CreateNSWMul(Idx, numElements);
4690 }
4691
4692 Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(),
4693 !getLangOpts().PointerOverflowDefined,
4694 SignedIndices, E->getExprLoc());
4695
4696 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
4697 // Indexing over an interface, as in "NSString *P; P[4];"
4698
4699 // Emit the base pointer.
4700 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4701 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4702
4703 CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT);
4704 llvm::Value *InterfaceSizeVal =
4705 llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity());
4706
4707 llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal);
4708
4709 // We don't necessarily build correct LLVM struct types for ObjC
4710 // interfaces, so we can't rely on GEP to do this scaling
4711 // correctly, so we need to cast to i8*. FIXME: is this actually
4712 // true? A lot of other things in the fragile ABI would break...
4713 llvm::Type *OrigBaseElemTy = Addr.getElementType();
4714
4715 // Do the GEP.
4716 CharUnits EltAlign =
4717 getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize);
4718 llvm::Value *EltPtr =
4719 emitArraySubscriptGEP(*this, Int8Ty, Addr.emitRawPointer(*this),
4720 ScaledIdx, false, SignedIndices, E->getExprLoc());
4721 Addr = Address(EltPtr, OrigBaseElemTy, EltAlign);
4722 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
4723 // If this is A[i] where A is an array, the frontend will have decayed the
4724 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
4725 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
4726 // "gep x, i" here. Emit one "gep A, 0, i".
4727 assert(Array->getType()->isArrayType() &&
4728 "Array to pointer decay must have array source type!");
4729 LValue ArrayLV;
4730 // For simple multidimensional array indexing, set the 'accessed' flag for
4731 // better bounds-checking of the base expression.
4732 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
4733 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
4734 else
4735 ArrayLV = EmitLValue(Array);
4736 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4737
4738 if (SanOpts.has(SanitizerKind::ArrayBounds))
4739 EmitCountedByBoundsChecking(Array, Idx, ArrayLV.getAddress(),
4740 E->getIdx()->getType(), Array->getType(),
4741 Accessed, /*FlexibleArray=*/true);
4742
4743 // Propagate the alignment from the array itself to the result.
4744 QualType arrayType = Array->getType();
4746 *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx},
4747 E->getType(), !getLangOpts().PointerOverflowDefined, SignedIndices,
4748 E->getExprLoc(), &arrayType, E->getBase());
4749 EltBaseInfo = ArrayLV.getBaseInfo();
4750 if (!CGM.getCodeGenOpts().NewStructPathTBAA) {
4751 // Since CodeGenTBAA::getTypeInfoHelper only handles array types for
4752 // new struct path TBAA, we must a use a plain access.
4753 EltTBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, E->getType());
4754 } else if (ArrayLV.getTBAAInfo().isMayAlias()) {
4755 EltTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
4756 } else if (ArrayLV.getTBAAInfo().isIncomplete()) {
4757 // The array element is complete, even if the array is not.
4758 EltTBAAInfo = CGM.getTBAAAccessInfo(E->getType());
4759 } else {
4760 // The TBAA access info from the array (base) lvalue is ordinary. We will
4761 // adapt it to create access info for the element.
4762 EltTBAAInfo = ArrayLV.getTBAAInfo();
4763
4764 // We retain the TBAA struct path (BaseType and Offset members) from the
4765 // array. In the TBAA representation, we map any array access to the
4766 // element at index 0, as the index is generally a runtime value. This
4767 // element has the same offset in the base type as the array itself.
4768 // If the array lvalue had no base type, there is no point trying to
4769 // generate one, since an array itself is not a valid base type.
4770
4771 // We also retain the access type from the base lvalue, but the access
4772 // size must be updated to the size of an individual element.
4773 EltTBAAInfo.Size =
4775 }
4776 } else {
4777 // The base must be a pointer; emit it with an estimate of its alignment.
4778 Address BaseAddr =
4779 EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4780 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4781 QualType ptrType = E->getBase()->getType();
4782 Addr = emitArraySubscriptGEP(*this, BaseAddr, Idx, E->getType(),
4783 !getLangOpts().PointerOverflowDefined,
4784 SignedIndices, E->getExprLoc(), &ptrType,
4785 E->getBase());
4786
4787 if (SanOpts.has(SanitizerKind::ArrayBounds)) {
4788 StructFieldAccess Visitor;
4789 const Expr *Base = Visitor.Visit(E->getBase());
4790
4791 if (const auto *CE = dyn_cast_if_present<CastExpr>(Base);
4792 CE && CE->getCastKind() == CK_LValueToRValue)
4794 E->getIdx()->getType(), ptrType, Accessed,
4795 /*FlexibleArray=*/false);
4796 }
4797 }
4798
4799 LValue LV = MakeAddrLValue(Addr, E->getType(), EltBaseInfo, EltTBAAInfo);
4800
4801 if (getLangOpts().ObjC &&
4802 getLangOpts().getGC() != LangOptions::NonGC) {
4805 }
4806 return LV;
4807}
4808
4810 llvm::Value *Idx = EmitScalarExpr(E);
4811 if (Idx->getType() == IntPtrTy)
4812 return Idx;
4813 bool IsSigned = E->getType()->isSignedIntegerOrEnumerationType();
4814 return Builder.CreateIntCast(Idx, IntPtrTy, IsSigned);
4815}
4816
4818 assert(
4819 !E->isIncomplete() &&
4820 "incomplete matrix subscript expressions should be rejected during Sema");
4821 LValue Base = EmitLValue(E->getBase());
4822
4823 // Extend or truncate the index type to 32 or 64-bits if needed.
4824 llvm::Value *RowIdx = EmitMatrixIndexExpr(E->getRowIdx());
4825 llvm::Value *ColIdx = EmitMatrixIndexExpr(E->getColumnIdx());
4826
4827 llvm::Value *NumRows = Builder.getIntN(
4828 RowIdx->getType()->getScalarSizeInBits(),
4830 llvm::Value *FinalIdx =
4831 Builder.CreateAdd(Builder.CreateMul(ColIdx, NumRows), RowIdx);
4832 return LValue::MakeMatrixElt(
4833 MaybeConvertMatrixAddress(Base.getAddress(), *this), FinalIdx,
4834 E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo());
4835}
4836
4838 LValueBaseInfo &BaseInfo,
4839 TBAAAccessInfo &TBAAInfo,
4840 QualType BaseTy, QualType ElTy,
4841 bool IsLowerBound) {
4842 LValue BaseLVal;
4843 if (auto *ASE = dyn_cast<ArraySectionExpr>(Base->IgnoreParenImpCasts())) {
4844 BaseLVal = CGF.EmitArraySectionExpr(ASE, IsLowerBound);
4845 if (BaseTy->isArrayType()) {
4846 Address Addr = BaseLVal.getAddress();
4847 BaseInfo = BaseLVal.getBaseInfo();
4848
4849 // If the array type was an incomplete type, we need to make sure
4850 // the decay ends up being the right type.
4851 llvm::Type *NewTy = CGF.ConvertType(BaseTy);
4852 Addr = Addr.withElementType(NewTy);
4853
4854 // Note that VLA pointers are always decayed, so we don't need to do
4855 // anything here.
4856 if (!BaseTy->isVariableArrayType()) {
4857 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
4858 "Expected pointer to array");
4859 Addr = CGF.Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
4860 }
4861
4862 return Addr.withElementType(CGF.ConvertTypeForMem(ElTy));
4863 }
4864 LValueBaseInfo TypeBaseInfo;
4865 TBAAAccessInfo TypeTBAAInfo;
4866 CharUnits Align =
4867 CGF.CGM.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, &TypeTBAAInfo);
4868 BaseInfo.mergeForCast(TypeBaseInfo);
4869 TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo);
4870 return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()),
4871 CGF.ConvertTypeForMem(ElTy), Align);
4872 }
4873 return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
4874}
4875
4877 bool IsLowerBound) {
4878
4879 assert(!E->isOpenACCArraySection() &&
4880 "OpenACC Array section codegen not implemented");
4881
4883 QualType ResultExprTy;
4884 if (auto *AT = getContext().getAsArrayType(BaseTy))
4885 ResultExprTy = AT->getElementType();
4886 else
4887 ResultExprTy = BaseTy->getPointeeType();
4888 llvm::Value *Idx = nullptr;
4889 if (IsLowerBound || E->getColonLocFirst().isInvalid()) {
4890 // Requesting lower bound or upper bound, but without provided length and
4891 // without ':' symbol for the default length -> length = 1.
4892 // Idx = LowerBound ?: 0;
4893 if (auto *LowerBound = E->getLowerBound()) {
4894 Idx = Builder.CreateIntCast(
4895 EmitScalarExpr(LowerBound), IntPtrTy,
4896 LowerBound->getType()->hasSignedIntegerRepresentation());
4897 } else
4898 Idx = llvm::ConstantInt::getNullValue(IntPtrTy);
4899 } else {
4900 // Try to emit length or lower bound as constant. If this is possible, 1
4901 // is subtracted from constant length or lower bound. Otherwise, emit LLVM
4902 // IR (LB + Len) - 1.
4903 auto &C = CGM.getContext();
4904 auto *Length = E->getLength();
4905 llvm::APSInt ConstLength;
4906 if (Length) {
4907 // Idx = LowerBound + Length - 1;
4908 if (std::optional<llvm::APSInt> CL = Length->getIntegerConstantExpr(C)) {
4909 ConstLength = CL->zextOrTrunc(PointerWidthInBits);
4910 Length = nullptr;
4911 }
4912 auto *LowerBound = E->getLowerBound();
4913 llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false);
4914 if (LowerBound) {
4915 if (std::optional<llvm::APSInt> LB =
4916 LowerBound->getIntegerConstantExpr(C)) {
4917 ConstLowerBound = LB->zextOrTrunc(PointerWidthInBits);
4918 LowerBound = nullptr;
4919 }
4920 }
4921 if (!Length)
4922 --ConstLength;
4923 else if (!LowerBound)
4924 --ConstLowerBound;
4925
4926 if (Length || LowerBound) {
4927 auto *LowerBoundVal =
4928 LowerBound
4929 ? Builder.CreateIntCast(
4930 EmitScalarExpr(LowerBound), IntPtrTy,
4931 LowerBound->getType()->hasSignedIntegerRepresentation())
4932 : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound);
4933 auto *LengthVal =
4934 Length
4935 ? Builder.CreateIntCast(
4936 EmitScalarExpr(Length), IntPtrTy,
4937 Length->getType()->hasSignedIntegerRepresentation())
4938 : llvm::ConstantInt::get(IntPtrTy, ConstLength);
4939 Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len",
4940 /*HasNUW=*/false,
4941 !getLangOpts().PointerOverflowDefined);
4942 if (Length && LowerBound) {
4943 Idx = Builder.CreateSub(
4944 Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1",
4945 /*HasNUW=*/false, !getLangOpts().PointerOverflowDefined);
4946 }
4947 } else
4948 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound);
4949 } else {
4950 // Idx = ArraySize - 1;
4951 QualType ArrayTy = BaseTy->isPointerType()
4953 : BaseTy;
4954 if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) {
4955 Length = VAT->getSizeExpr();
4956 if (std::optional<llvm::APSInt> L = Length->getIntegerConstantExpr(C)) {
4957 ConstLength = *L;
4958 Length = nullptr;
4959 }
4960 } else {
4961 auto *CAT = C.getAsConstantArrayType(ArrayTy);
4962 assert(CAT && "unexpected type for array initializer");
4963 ConstLength = CAT->getSize();
4964 }
4965 if (Length) {
4966 auto *LengthVal = Builder.CreateIntCast(
4967 EmitScalarExpr(Length), IntPtrTy,
4968 Length->getType()->hasSignedIntegerRepresentation());
4969 Idx = Builder.CreateSub(
4970 LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1",
4971 /*HasNUW=*/false, !getLangOpts().PointerOverflowDefined);
4972 } else {
4973 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits);
4974 --ConstLength;
4975 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength);
4976 }
4977 }
4978 }
4979 assert(Idx);
4980
4981 Address EltPtr = Address::invalid();
4982 LValueBaseInfo BaseInfo;
4983 TBAAAccessInfo TBAAInfo;
4984 if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) {
4985 // The base must be a pointer, which is not an aggregate. Emit
4986 // it. It needs to be emitted first in case it's what captures
4987 // the VLA bounds.
4988 Address Base =
4989 emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, TBAAInfo,
4990 BaseTy, VLA->getElementType(), IsLowerBound);
4991 // The element count here is the total number of non-VLA elements.
4992 llvm::Value *NumElements = getVLASize(VLA).NumElts;
4993
4994 // Effectively, the multiply by the VLA size is part of the GEP.
4995 // GEP indexes are signed, and scaling an index isn't permitted to
4996 // signed-overflow, so we use the same semantics for our explicit
4997 // multiply. We suppress this if overflow is not undefined behavior.
4998 if (getLangOpts().PointerOverflowDefined)
4999 Idx = Builder.CreateMul(Idx, NumElements);
5000 else
5001 Idx = Builder.CreateNSWMul(Idx, NumElements);
5002 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(),
5003 !getLangOpts().PointerOverflowDefined,
5004 /*signedIndices=*/false, E->getExprLoc());
5005 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
5006 // If this is A[i] where A is an array, the frontend will have decayed the
5007 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
5008 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
5009 // "gep x, i" here. Emit one "gep A, 0, i".
5010 assert(Array->getType()->isArrayType() &&
5011 "Array to pointer decay must have array source type!");
5012 LValue ArrayLV;
5013 // For simple multidimensional array indexing, set the 'accessed' flag for
5014 // better bounds-checking of the base expression.
5015 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
5016 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
5017 else
5018 ArrayLV = EmitLValue(Array);
5019
5020 // Propagate the alignment from the array itself to the result.
5021 EltPtr = emitArraySubscriptGEP(
5022 *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx},
5023 ResultExprTy, !getLangOpts().PointerOverflowDefined,
5024 /*signedIndices=*/false, E->getExprLoc());
5025 BaseInfo = ArrayLV.getBaseInfo();
5026 TBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, ResultExprTy);
5027 } else {
5028 Address Base =
5029 emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, TBAAInfo, BaseTy,
5030 ResultExprTy, IsLowerBound);
5031 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy,
5032 !getLangOpts().PointerOverflowDefined,
5033 /*signedIndices=*/false, E->getExprLoc());
5034 }
5035
5036 return MakeAddrLValue(EltPtr, ResultExprTy, BaseInfo, TBAAInfo);
5037}
5038
5041 // Emit the base vector as an l-value.
5042 LValue Base;
5043
5044 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
5045 if (E->isArrow()) {
5046 // If it is a pointer to a vector, emit the address and form an lvalue with
5047 // it.
5048 LValueBaseInfo BaseInfo;
5049 TBAAAccessInfo TBAAInfo;
5050 Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo, &TBAAInfo);
5051 const auto *PT = E->getBase()->getType()->castAs<PointerType>();
5052 Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo, TBAAInfo);
5053 Base.getQuals().removeObjCGCAttr();
5054 } else if (E->getBase()->isGLValue()) {
5055 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
5056 // emit the base as an lvalue.
5057 assert(E->getBase()->getType()->isVectorType());
5058 Base = EmitLValue(E->getBase());
5059 } else {
5060 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
5061 assert(E->getBase()->getType()->isVectorType() &&
5062 "Result must be a vector");
5063 llvm::Value *Vec = EmitScalarExpr(E->getBase());
5064
5065 // Store the vector to memory (because LValue wants an address).
5066 Address VecMem = CreateMemTemp(E->getBase()->getType());
5067 // need to zero extend an hlsl boolean vector to store it back to memory
5068 QualType Ty = E->getBase()->getType();
5069 llvm::Type *LTy = convertTypeForLoadStore(Ty, Vec->getType());
5070 if (LTy->getScalarSizeInBits() > Vec->getType()->getScalarSizeInBits())
5071 Vec = Builder.CreateZExt(Vec, LTy);
5072 Builder.CreateStore(Vec, VecMem);
5074 }
5075
5076 QualType type =
5077 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
5078
5079 // Encode the element access list into a vector of unsigned indices.
5081 E->getEncodedElementAccess(Indices);
5082
5083 if (Base.isSimple()) {
5084 llvm::Constant *CV =
5085 llvm::ConstantDataVector::get(getLLVMContext(), Indices);
5086 return LValue::MakeExtVectorElt(Base.getAddress(), CV, type,
5087 Base.getBaseInfo(), TBAAAccessInfo());
5088 }
5089 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
5090
5091 llvm::Constant *BaseElts = Base.getExtVectorElts();
5093
5094 for (unsigned Index : Indices)
5095 CElts.push_back(BaseElts->getAggregateElement(Index));
5096 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
5097 return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type,
5098 Base.getBaseInfo(), TBAAAccessInfo());
5099}
5100
5102 const Expr *UnderlyingBaseExpr = E->IgnoreParens();
5103 while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
5104 UnderlyingBaseExpr = BaseMemberExpr->getBase()->IgnoreParens();
5105 return getContext().isSentinelNullExpr(UnderlyingBaseExpr);
5106}
5107
5109 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) {
5111 return EmitDeclRefLValue(DRE);
5112 }
5113
5114 Expr *BaseExpr = E->getBase();
5115 // Check whether the underlying base pointer is a constant null.
5116 // If so, we do not set inbounds flag for GEP to avoid breaking some
5117 // old-style offsetof idioms.
5118 bool IsInBounds = !getLangOpts().PointerOverflowDefined &&
5120 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
5121 LValue BaseLV;
5122 if (E->isArrow()) {
5123 LValueBaseInfo BaseInfo;
5124 TBAAAccessInfo TBAAInfo;
5125 Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo, &TBAAInfo);
5126 QualType PtrTy = BaseExpr->getType()->getPointeeType();
5127 SanitizerSet SkippedChecks;
5128 bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr);
5129 if (IsBaseCXXThis)
5130 SkippedChecks.set(SanitizerKind::Alignment, true);
5131 if (IsBaseCXXThis || isa<DeclRefExpr>(BaseExpr))
5132 SkippedChecks.set(SanitizerKind::Null, true);
5134 /*Alignment=*/CharUnits::Zero(), SkippedChecks);
5135 BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo, TBAAInfo);
5136 } else
5137 BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
5138
5139 NamedDecl *ND = E->getMemberDecl();
5140 if (auto *Field = dyn_cast<FieldDecl>(ND)) {
5141 LValue LV = EmitLValueForField(BaseLV, Field, IsInBounds);
5143 if (getLangOpts().OpenMP) {
5144 // If the member was explicitly marked as nontemporal, mark it as
5145 // nontemporal. If the base lvalue is marked as nontemporal, mark access
5146 // to children as nontemporal too.
5147 if ((IsWrappedCXXThis(BaseExpr) &&
5148 CGM.getOpenMPRuntime().isNontemporalDecl(Field)) ||
5149 BaseLV.isNontemporal())
5150 LV.setNontemporal(/*Value=*/true);
5151 }
5152 return LV;
5153 }
5154
5155 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
5156 return EmitFunctionDeclLValue(*this, E, FD);
5157
5158 llvm_unreachable("Unhandled member declaration!");
5159}
5160
5161/// Given that we are currently emitting a lambda, emit an l-value for
5162/// one of its members.
5163///
5165 llvm::Value *ThisValue) {
5166 bool HasExplicitObjectParameter = false;
5167 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(CurCodeDecl);
5168 if (MD) {
5169 HasExplicitObjectParameter = MD->isExplicitObjectMemberFunction();
5170 assert(MD->getParent()->isLambda());
5171 assert(MD->getParent() == Field->getParent());
5172 }
5173 LValue LambdaLV;
5174 if (HasExplicitObjectParameter) {
5175 const VarDecl *D = cast<CXXMethodDecl>(CurCodeDecl)->getParamDecl(0);
5176 auto It = LocalDeclMap.find(D);
5177 assert(It != LocalDeclMap.end() && "explicit parameter not loaded?");
5178 Address AddrOfExplicitObject = It->getSecond();
5179 if (D->getType()->isReferenceType())
5180 LambdaLV = EmitLoadOfReferenceLValue(AddrOfExplicitObject, D->getType(),
5182 else
5183 LambdaLV = MakeAddrLValue(AddrOfExplicitObject,
5185
5186 // Make sure we have an lvalue to the lambda itself and not a derived class.
5187 auto *ThisTy = D->getType().getNonReferenceType()->getAsCXXRecordDecl();
5188 auto *LambdaTy = cast<CXXRecordDecl>(Field->getParent());
5189 if (ThisTy != LambdaTy) {
5190 const CXXCastPath &BasePathArray = getContext().LambdaCastPaths.at(MD);
5192 LambdaLV.getAddress(), ThisTy, BasePathArray.begin(),
5193 BasePathArray.end(), /*NullCheckValue=*/false, SourceLocation());
5195 LambdaLV = MakeAddrLValue(Base, T);
5196 }
5197 } else {
5198 CanQualType LambdaTagType =
5199 getContext().getCanonicalTagType(Field->getParent());
5200 LambdaLV = MakeNaturalAlignAddrLValue(ThisValue, LambdaTagType);
5201 }
5202 return EmitLValueForField(LambdaLV, Field);
5203}
5204
5206 return EmitLValueForLambdaField(Field, CXXABIThisValue);
5207}
5208
5209/// Get the field index in the debug info. The debug info structure/union
5210/// will ignore the unnamed bitfields.
5212 unsigned FieldIndex) {
5213 unsigned I = 0, Skipped = 0;
5214
5215 for (auto *F : Rec->getDefinition()->fields()) {
5216 if (I == FieldIndex)
5217 break;
5218 if (F->isUnnamedBitField())
5219 Skipped++;
5220 I++;
5221 }
5222
5223 return FieldIndex - Skipped;
5224}
5225
5226/// Get the address of a zero-sized field within a record. The resulting
5227/// address doesn't necessarily have the right type.
5229 const FieldDecl *Field,
5230 bool IsInBounds) {
5232 CGF.getContext().getFieldOffset(Field));
5233 if (Offset.isZero())
5234 return Base;
5235 Base = Base.withElementType(CGF.Int8Ty);
5236 if (!IsInBounds)
5237 return CGF.Builder.CreateConstByteGEP(Base, Offset);
5238 return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
5239}
5240
5241/// Drill down to the storage of a field without walking into
5242/// reference types.
5243///
5244/// The resulting address doesn't necessarily have the right type.
5246 const FieldDecl *field, bool IsInBounds) {
5247 if (isEmptyFieldForLayout(CGF.getContext(), field))
5248 return emitAddrOfZeroSizeField(CGF, base, field, IsInBounds);
5249
5250 const RecordDecl *rec = field->getParent();
5251
5252 unsigned idx =
5253 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
5254
5255 if (!IsInBounds)
5256 return CGF.Builder.CreateConstGEP2_32(base, 0, idx, field->getName());
5257
5258 return CGF.Builder.CreateStructGEP(base, idx, field->getName());
5259}
5260
5262 Address addr, const FieldDecl *field) {
5263 const RecordDecl *rec = field->getParent();
5264 llvm::DIType *DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(
5265 base.getType(), rec->getLocation());
5266
5267 unsigned idx =
5268 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
5269
5271 addr, idx, CGF.getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo);
5272}
5273
5274static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
5275 const auto *RD = Type.getTypePtr()->getAsCXXRecordDecl();
5276 if (!RD)
5277 return false;
5278
5279 if (RD->isDynamicClass())
5280 return true;
5281
5282 for (const auto &Base : RD->bases())
5283 if (hasAnyVptr(Base.getType(), Context))
5284 return true;
5285
5286 for (const FieldDecl *Field : RD->fields())
5287 if (hasAnyVptr(Field->getType(), Context))
5288 return true;
5289
5290 return false;
5291}
5292
5294 bool IsInBounds) {
5295 LValueBaseInfo BaseInfo = base.getBaseInfo();
5296
5297 if (field->isBitField()) {
5298 const CGRecordLayout &RL =
5299 CGM.getTypes().getCGRecordLayout(field->getParent());
5300 const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
5301 const bool UseVolatile = isAAPCS(CGM.getTarget()) &&
5302 CGM.getCodeGenOpts().AAPCSBitfieldWidth &&
5303 Info.VolatileStorageSize != 0 &&
5304 field->getType()
5307 Address Addr = base.getAddress();
5308 unsigned Idx = RL.getLLVMFieldNo(field);
5309 const RecordDecl *rec = field->getParent();
5312 if (!UseVolatile) {
5313 if (!IsInPreservedAIRegion &&
5314 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
5315 if (Idx != 0) {
5316 // For structs, we GEP to the field that the record layout suggests.
5317 if (!IsInBounds)
5318 Addr = Builder.CreateConstGEP2_32(Addr, 0, Idx, field->getName());
5319 else
5320 Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
5321 }
5322 } else {
5323 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
5324 getContext().getCanonicalTagType(rec), rec->getLocation());
5325 Addr = Builder.CreatePreserveStructAccessIndex(
5326 Addr, Idx, getDebugInfoFIndex(rec, field->getFieldIndex()),
5327 DbgInfo);
5328 }
5329 }
5330 const unsigned SS =
5331 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
5332 // Get the access type.
5333 llvm::Type *FieldIntTy = llvm::Type::getIntNTy(getLLVMContext(), SS);
5334 Addr = Addr.withElementType(FieldIntTy);
5335 if (UseVolatile) {
5336 const unsigned VolatileOffset = Info.VolatileStorageOffset.getQuantity();
5337 if (VolatileOffset)
5338 Addr = Builder.CreateConstInBoundsGEP(Addr, VolatileOffset);
5339 }
5340
5341 QualType fieldType =
5342 field->getType().withCVRQualifiers(base.getVRQualifiers());
5343 // TODO: Support TBAA for bit fields.
5344 LValueBaseInfo FieldBaseInfo(BaseInfo.getAlignmentSource());
5345 return LValue::MakeBitfield(Addr, Info, fieldType, FieldBaseInfo,
5346 TBAAAccessInfo());
5347 }
5348
5349 // Fields of may-alias structures are may-alias themselves.
5350 // FIXME: this should get propagated down through anonymous structs
5351 // and unions.
5352 QualType FieldType = field->getType();
5353 const RecordDecl *rec = field->getParent();
5354 AlignmentSource BaseAlignSource = BaseInfo.getAlignmentSource();
5355 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(BaseAlignSource));
5356 TBAAAccessInfo FieldTBAAInfo;
5357 if (base.getTBAAInfo().isMayAlias() ||
5358 rec->hasAttr<MayAliasAttr>() || FieldType->isVectorType()) {
5359 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
5360 } else if (rec->isUnion()) {
5361 // TODO: Support TBAA for unions.
5362 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
5363 } else {
5364 // If no base type been assigned for the base access, then try to generate
5365 // one for this base lvalue.
5366 FieldTBAAInfo = base.getTBAAInfo();
5367 if (!FieldTBAAInfo.BaseType) {
5368 FieldTBAAInfo.BaseType = CGM.getTBAABaseTypeInfo(base.getType());
5369 assert(!FieldTBAAInfo.Offset &&
5370 "Nonzero offset for an access with no base type!");
5371 }
5372
5373 // Adjust offset to be relative to the base type.
5374 const ASTRecordLayout &Layout =
5376 unsigned CharWidth = getContext().getCharWidth();
5377 if (FieldTBAAInfo.BaseType)
5378 FieldTBAAInfo.Offset +=
5379 Layout.getFieldOffset(field->getFieldIndex()) / CharWidth;
5380
5381 // Update the final access type and size.
5382 FieldTBAAInfo.AccessType = CGM.getTBAATypeInfo(FieldType);
5383 FieldTBAAInfo.Size =
5385 }
5386
5387 Address addr = base.getAddress();
5389 addr = wrapWithBPFPreserveStaticOffset(*this, addr);
5390 if (auto *ClassDef = dyn_cast<CXXRecordDecl>(rec)) {
5391 if (CGM.getCodeGenOpts().StrictVTablePointers &&
5392 ClassDef->isDynamicClass()) {
5393 // Getting to any field of dynamic object requires stripping dynamic
5394 // information provided by invariant.group. This is because accessing
5395 // fields may leak the real address of dynamic object, which could result
5396 // in miscompilation when leaked pointer would be compared.
5397 auto *stripped =
5398 Builder.CreateStripInvariantGroup(addr.emitRawPointer(*this));
5399 addr = Address(stripped, addr.getElementType(), addr.getAlignment());
5400 }
5401 }
5402
5403 unsigned RecordCVR = base.getVRQualifiers();
5404 if (rec->isUnion()) {
5405 // For unions, there is no pointer adjustment.
5406 if (CGM.getCodeGenOpts().StrictVTablePointers &&
5407 hasAnyVptr(FieldType, getContext()))
5408 // Because unions can easily skip invariant.barriers, we need to add
5409 // a barrier every time CXXRecord field with vptr is referenced.
5410 addr = Builder.CreateLaunderInvariantGroup(addr);
5411
5413 (getDebugInfo() && rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
5414 // Remember the original union field index
5415 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateStandaloneType(base.getType(),
5416 rec->getLocation());
5417 addr =
5418 Address(Builder.CreatePreserveUnionAccessIndex(
5419 addr.emitRawPointer(*this),
5420 getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo),
5421 addr.getElementType(), addr.getAlignment());
5422 }
5423
5424 if (FieldType->isReferenceType())
5425 addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType));
5426 } else {
5427 if (!IsInPreservedAIRegion &&
5428 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
5429 // For structs, we GEP to the field that the record layout suggests.
5430 addr = emitAddrOfFieldStorage(*this, addr, field, IsInBounds);
5431 else
5432 // Remember the original struct field index
5433 addr = emitPreserveStructAccess(*this, base, addr, field);
5434 }
5435
5436 // If this is a reference field, load the reference right now.
5437 if (FieldType->isReferenceType()) {
5438 LValue RefLVal =
5439 MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
5440 if (RecordCVR & Qualifiers::Volatile)
5441 RefLVal.getQuals().addVolatile();
5442 addr = EmitLoadOfReference(RefLVal, &FieldBaseInfo, &FieldTBAAInfo);
5443
5444 // Qualifiers on the struct don't apply to the referencee.
5445 RecordCVR = 0;
5446 FieldType = FieldType->getPointeeType();
5447 }
5448
5449 // Make sure that the address is pointing to the right type. This is critical
5450 // for both unions and structs.
5451 addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType));
5452
5453 if (field->hasAttr<AnnotateAttr>())
5454 addr = EmitFieldAnnotations(field, addr);
5455
5456 LValue LV = MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
5457 LV.getQuals().addCVRQualifiers(RecordCVR);
5458
5459 // __weak attribute on a field is ignored.
5462
5463 return LV;
5464}
5465
5466LValue
5468 const FieldDecl *Field) {
5469 QualType FieldType = Field->getType();
5470
5471 if (!FieldType->isReferenceType())
5472 return EmitLValueForField(Base, Field);
5473
5475 *this, Base.getAddress(), Field,
5476 /*IsInBounds=*/!getLangOpts().PointerOverflowDefined);
5477
5478 // Make sure that the address is pointing to the right type.
5479 llvm::Type *llvmType = ConvertTypeForMem(FieldType);
5480 V = V.withElementType(llvmType);
5481
5482 // TODO: Generate TBAA information that describes this access as a structure
5483 // member access and not just an access to an object of the field's type. This
5484 // should be similar to what we do in EmitLValueForField().
5485 LValueBaseInfo BaseInfo = Base.getBaseInfo();
5486 AlignmentSource FieldAlignSource = BaseInfo.getAlignmentSource();
5487 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(FieldAlignSource));
5488 return MakeAddrLValue(V, FieldType, FieldBaseInfo,
5489 CGM.getTBAAInfoForSubobject(Base, FieldType));
5490}
5491
5493 if (E->isFileScope()) {
5494 ConstantAddress GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
5495 return MakeAddrLValue(GlobalPtr, E->getType(), AlignmentSource::Decl);
5496 }
5497 if (E->getType()->isVariablyModifiedType())
5498 // make sure to emit the VLA size.
5500
5501 Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
5502 const Expr *InitExpr = E->getInitializer();
5504
5505 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
5506 /*Init*/ true);
5507
5508 // Block-scope compound literals are destroyed at the end of the enclosing
5509 // scope in C.
5510 if (!getLangOpts().CPlusPlus)
5513 E->getType(), getDestroyer(DtorKind),
5514 DtorKind & EHCleanup);
5515
5516 return Result;
5517}
5518
5520 if (!E->isGLValue())
5521 // Initializing an aggregate temporary in C++11: T{...}.
5522 return EmitAggExprToLValue(E);
5523
5524 // An lvalue initializer list must be initializing a reference.
5525 assert(E->isTransparent() && "non-transparent glvalue init list");
5526 return EmitLValue(E->getInit(0));
5527}
5528
5529/// Emit the operand of a glvalue conditional operator. This is either a glvalue
5530/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
5531/// LValue is returned and the current block has been terminated.
5532static std::optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF,
5533 const Expr *Operand) {
5534 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) {
5535 CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false);
5536 return std::nullopt;
5537 }
5538
5539 return CGF.EmitLValue(Operand);
5540}
5541
5542namespace {
5543// Handle the case where the condition is a constant evaluatable simple integer,
5544// which means we don't have to separately handle the true/false blocks.
5545std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
5546 CodeGenFunction &CGF, const AbstractConditionalOperator *E) {
5547 const Expr *condExpr = E->getCond();
5548 bool CondExprBool;
5549 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
5550 const Expr *Live = E->getTrueExpr(), *Dead = E->getFalseExpr();
5551 if (!CondExprBool)
5552 std::swap(Live, Dead);
5553
5554 if (!CGF.ContainsLabel(Dead)) {
5555 // If the true case is live, we need to track its region.
5556 if (CondExprBool)
5558 CGF.markStmtMaybeUsed(Dead);
5559 // If a throw expression we emit it and return an undefined lvalue
5560 // because it can't be used.
5561 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Live->IgnoreParens())) {
5562 CGF.EmitCXXThrowExpr(ThrowExpr);
5563 llvm::Type *ElemTy = CGF.ConvertType(Dead->getType());
5564 llvm::Type *Ty = CGF.DefaultPtrTy;
5565 return CGF.MakeAddrLValue(
5566 Address(llvm::UndefValue::get(Ty), ElemTy, CharUnits::One()),
5567 Dead->getType());
5568 }
5569 return CGF.EmitLValue(Live);
5570 }
5571 }
5572 return std::nullopt;
5573}
5574struct ConditionalInfo {
5575 llvm::BasicBlock *lhsBlock, *rhsBlock;
5576 std::optional<LValue> LHS, RHS;
5577};
5578
5579// Create and generate the 3 blocks for a conditional operator.
5580// Leaves the 'current block' in the continuation basic block.
5581template<typename FuncTy>
5582ConditionalInfo EmitConditionalBlocks(CodeGenFunction &CGF,
5583 const AbstractConditionalOperator *E,
5584 const FuncTy &BranchGenFunc) {
5585 ConditionalInfo Info{CGF.createBasicBlock("cond.true"),
5586 CGF.createBasicBlock("cond.false"), std::nullopt,
5587 std::nullopt};
5588 llvm::BasicBlock *endBlock = CGF.createBasicBlock("cond.end");
5589
5591 CGF.EmitBranchOnBoolExpr(E->getCond(), Info.lhsBlock, Info.rhsBlock,
5592 CGF.getProfileCount(E));
5593
5594 // Any temporaries created here are conditional.
5595 CGF.EmitBlock(Info.lhsBlock);
5597 eval.begin(CGF);
5598 Info.LHS = BranchGenFunc(CGF, E->getTrueExpr());
5599 eval.end(CGF);
5600 Info.lhsBlock = CGF.Builder.GetInsertBlock();
5601
5602 if (Info.LHS)
5603 CGF.Builder.CreateBr(endBlock);
5604
5605 // Any temporaries created here are conditional.
5606 CGF.EmitBlock(Info.rhsBlock);
5607 eval.begin(CGF);
5608 Info.RHS = BranchGenFunc(CGF, E->getFalseExpr());
5609 eval.end(CGF);
5610 Info.rhsBlock = CGF.Builder.GetInsertBlock();
5611 CGF.EmitBlock(endBlock);
5612
5613 return Info;
5614}
5615} // namespace
5616
5618 const AbstractConditionalOperator *E) {
5619 if (!E->isGLValue()) {
5620 // ?: here should be an aggregate.
5621 assert(hasAggregateEvaluationKind(E->getType()) &&
5622 "Unexpected conditional operator!");
5623 return (void)EmitAggExprToLValue(E);
5624 }
5625
5626 OpaqueValueMapping binding(*this, E);
5627 if (HandleConditionalOperatorLValueSimpleCase(*this, E))
5628 return;
5629
5630 EmitConditionalBlocks(*this, E, [](CodeGenFunction &CGF, const Expr *E) {
5631 CGF.EmitIgnoredExpr(E);
5632 return LValue{};
5633 });
5634}
5637 if (!expr->isGLValue()) {
5638 // ?: here should be an aggregate.
5639 assert(hasAggregateEvaluationKind(expr->getType()) &&
5640 "Unexpected conditional operator!");
5641 return EmitAggExprToLValue(expr);
5642 }
5643
5644 OpaqueValueMapping binding(*this, expr);
5645 if (std::optional<LValue> Res =
5646 HandleConditionalOperatorLValueSimpleCase(*this, expr))
5647 return *Res;
5648
5649 ConditionalInfo Info = EmitConditionalBlocks(
5650 *this, expr, [](CodeGenFunction &CGF, const Expr *E) {
5651 return EmitLValueOrThrowExpression(CGF, E);
5652 });
5653
5654 if ((Info.LHS && !Info.LHS->isSimple()) ||
5655 (Info.RHS && !Info.RHS->isSimple()))
5656 return EmitUnsupportedLValue(expr, "conditional operator");
5657
5658 if (Info.LHS && Info.RHS) {
5659 Address lhsAddr = Info.LHS->getAddress();
5660 Address rhsAddr = Info.RHS->getAddress();
5662 lhsAddr, rhsAddr, Info.lhsBlock, Info.rhsBlock,
5663 Builder.GetInsertBlock(), expr->getType());
5664 AlignmentSource alignSource =
5665 std::max(Info.LHS->getBaseInfo().getAlignmentSource(),
5666 Info.RHS->getBaseInfo().getAlignmentSource());
5667 TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForConditionalOperator(
5668 Info.LHS->getTBAAInfo(), Info.RHS->getTBAAInfo());
5669 return MakeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource),
5670 TBAAInfo);
5671 } else {
5672 assert((Info.LHS || Info.RHS) &&
5673 "both operands of glvalue conditional are throw-expressions?");
5674 return Info.LHS ? *Info.LHS : *Info.RHS;
5675 }
5676}
5677
5678/// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
5679/// type. If the cast is to a reference, we can have the usual lvalue result,
5680/// otherwise if a cast is needed by the code generator in an lvalue context,
5681/// then it must mean that we need the address of an aggregate in order to
5682/// access one of its members. This can happen for all the reasons that casts
5683/// are permitted with aggregate result, including noop aggregate casts, and
5684/// cast from scalar to union.
5686 auto RestoreCurCast =
5687 llvm::make_scope_exit([this, Prev = CurCast] { CurCast = Prev; });
5688 CurCast = E;
5689 switch (E->getCastKind()) {
5690 case CK_ToVoid:
5691 case CK_BitCast:
5692 case CK_LValueToRValueBitCast:
5693 case CK_ArrayToPointerDecay:
5694 case CK_FunctionToPointerDecay:
5695 case CK_NullToMemberPointer:
5696 case CK_NullToPointer:
5697 case CK_IntegralToPointer:
5698 case CK_PointerToIntegral:
5699 case CK_PointerToBoolean:
5700 case CK_IntegralCast:
5701 case CK_BooleanToSignedIntegral:
5702 case CK_IntegralToBoolean:
5703 case CK_IntegralToFloating:
5704 case CK_FloatingToIntegral:
5705 case CK_FloatingToBoolean:
5706 case CK_FloatingCast:
5707 case CK_FloatingRealToComplex:
5708 case CK_FloatingComplexToReal:
5709 case CK_FloatingComplexToBoolean:
5710 case CK_FloatingComplexCast:
5711 case CK_FloatingComplexToIntegralComplex:
5712 case CK_IntegralRealToComplex:
5713 case CK_IntegralComplexToReal:
5714 case CK_IntegralComplexToBoolean:
5715 case CK_IntegralComplexCast:
5716 case CK_IntegralComplexToFloatingComplex:
5717 case CK_DerivedToBaseMemberPointer:
5718 case CK_BaseToDerivedMemberPointer:
5719 case CK_MemberPointerToBoolean:
5720 case CK_ReinterpretMemberPointer:
5721 case CK_AnyPointerToBlockPointerCast:
5722 case CK_ARCProduceObject:
5723 case CK_ARCConsumeObject:
5724 case CK_ARCReclaimReturnedObject:
5725 case CK_ARCExtendBlockObject:
5726 case CK_CopyAndAutoreleaseBlockObject:
5727 case CK_IntToOCLSampler:
5728 case CK_FloatingToFixedPoint:
5729 case CK_FixedPointToFloating:
5730 case CK_FixedPointCast:
5731 case CK_FixedPointToBoolean:
5732 case CK_FixedPointToIntegral:
5733 case CK_IntegralToFixedPoint:
5734 case CK_MatrixCast:
5735 case CK_HLSLVectorTruncation:
5736 case CK_HLSLArrayRValue:
5737 case CK_HLSLElementwiseCast:
5738 case CK_HLSLAggregateSplatCast:
5739 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
5740
5741 case CK_Dependent:
5742 llvm_unreachable("dependent cast kind in IR gen!");
5743
5744 case CK_BuiltinFnToFnPtr:
5745 llvm_unreachable("builtin functions are handled elsewhere");
5746
5747 // These are never l-values; just use the aggregate emission code.
5748 case CK_NonAtomicToAtomic:
5749 case CK_AtomicToNonAtomic:
5750 return EmitAggExprToLValue(E);
5751
5752 case CK_Dynamic: {
5753 LValue LV = EmitLValue(E->getSubExpr());
5754 Address V = LV.getAddress();
5755 const auto *DCE = cast<CXXDynamicCastExpr>(E);
5757 }
5758
5759 case CK_ConstructorConversion:
5760 case CK_UserDefinedConversion:
5761 case CK_CPointerToObjCPointerCast:
5762 case CK_BlockPointerToObjCPointerCast:
5763 case CK_LValueToRValue:
5764 return EmitLValue(E->getSubExpr());
5765
5766 case CK_NoOp: {
5767 // CK_NoOp can model a qualification conversion, which can remove an array
5768 // bound and change the IR type.
5769 // FIXME: Once pointee types are removed from IR, remove this.
5770 LValue LV = EmitLValue(E->getSubExpr());
5771 // Propagate the volatile qualifer to LValue, if exist in E.
5773 LV.getQuals() = E->getType().getQualifiers();
5774 if (LV.isSimple()) {
5775 Address V = LV.getAddress();
5776 if (V.isValid()) {
5777 llvm::Type *T = ConvertTypeForMem(E->getType());
5778 if (V.getElementType() != T)
5779 LV.setAddress(V.withElementType(T));
5780 }
5781 }
5782 return LV;
5783 }
5784
5785 case CK_UncheckedDerivedToBase:
5786 case CK_DerivedToBase: {
5787 auto *DerivedClassDecl = E->getSubExpr()->getType()->castAsCXXRecordDecl();
5788 LValue LV = EmitLValue(E->getSubExpr());
5789 Address This = LV.getAddress();
5790
5791 // Perform the derived-to-base conversion
5793 This, DerivedClassDecl, E->path_begin(), E->path_end(),
5794 /*NullCheckValue=*/false, E->getExprLoc());
5795
5796 // TODO: Support accesses to members of base classes in TBAA. For now, we
5797 // conservatively pretend that the complete object is of the base class
5798 // type.
5799 return MakeAddrLValue(Base, E->getType(), LV.getBaseInfo(),
5800 CGM.getTBAAInfoForSubobject(LV, E->getType()));
5801 }
5802 case CK_ToUnion:
5803 return EmitAggExprToLValue(E);
5804 case CK_BaseToDerived: {
5805 auto *DerivedClassDecl = E->getType()->castAsCXXRecordDecl();
5806 LValue LV = EmitLValue(E->getSubExpr());
5807
5808 // Perform the base-to-derived conversion
5810 LV.getAddress(), DerivedClassDecl, E->path_begin(), E->path_end(),
5811 /*NullCheckValue=*/false);
5812
5813 // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
5814 // performed and the object is not of the derived type.
5817 E->getType());
5818
5819 if (SanOpts.has(SanitizerKind::CFIDerivedCast))
5820 EmitVTablePtrCheckForCast(E->getType(), Derived,
5821 /*MayBeNull=*/false, CFITCK_DerivedCast,
5822 E->getBeginLoc());
5823
5824 return MakeAddrLValue(Derived, E->getType(), LV.getBaseInfo(),
5825 CGM.getTBAAInfoForSubobject(LV, E->getType()));
5826 }
5827 case CK_LValueBitCast: {
5828 // This must be a reinterpret_cast (or c-style equivalent).
5829 const auto *CE = cast<ExplicitCastExpr>(E);
5830
5831 CGM.EmitExplicitCastExprType(CE, this);
5832 LValue LV = EmitLValue(E->getSubExpr());
5834 ConvertTypeForMem(CE->getTypeAsWritten()->getPointeeType()));
5835
5836 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast))
5838 /*MayBeNull=*/false, CFITCK_UnrelatedCast,
5839 E->getBeginLoc());
5840
5841 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
5842 CGM.getTBAAInfoForSubobject(LV, E->getType()));
5843 }
5844 case CK_AddressSpaceConversion: {
5845 LValue LV = EmitLValue(E->getSubExpr());
5846 QualType DestTy = getContext().getPointerType(E->getType());
5847 llvm::Value *V = getTargetHooks().performAddrSpaceCast(
5848 *this, LV.getPointer(*this),
5849 E->getSubExpr()->getType().getAddressSpace(), ConvertType(DestTy));
5851 LV.getAddress().getAlignment()),
5852 E->getType(), LV.getBaseInfo(), LV.getTBAAInfo());
5853 }
5854 case CK_ObjCObjectLValueCast: {
5855 LValue LV = EmitLValue(E->getSubExpr());
5857 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
5858 CGM.getTBAAInfoForSubobject(LV, E->getType()));
5859 }
5860 case CK_ZeroToOCLOpaqueType:
5861 llvm_unreachable("NULL to OpenCL opaque type lvalue cast is not valid");
5862
5863 case CK_VectorSplat: {
5864 // LValue results of vector splats are only supported in HLSL.
5865 if (!getLangOpts().HLSL)
5866 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
5867 return EmitLValue(E->getSubExpr());
5868 }
5869 }
5870
5871 llvm_unreachable("Unhandled lvalue cast kind?");
5872}
5873
5878
5879std::pair<LValue, LValue>
5881 // Emitting the casted temporary through an opaque value.
5882 LValue BaseLV = EmitLValue(E->getArgLValue());
5884
5885 QualType ExprTy = E->getType();
5886 Address OutTemp = CreateIRTemp(ExprTy);
5887 LValue TempLV = MakeAddrLValue(OutTemp, ExprTy);
5888
5889 if (E->isInOut())
5891 TempLV);
5892
5894 return std::make_pair(BaseLV, TempLV);
5895}
5896
5898 CallArgList &Args, QualType Ty) {
5899
5900 auto [BaseLV, TempLV] = EmitHLSLOutArgLValues(E, Ty);
5901
5902 llvm::Value *Addr = TempLV.getAddress().getBasePointer();
5903 llvm::Type *ElTy = ConvertTypeForMem(TempLV.getType());
5904
5906
5907 Address TmpAddr(Addr, ElTy, TempLV.getAlignment());
5908 Args.addWriteback(BaseLV, TmpAddr, nullptr, E->getWritebackCast());
5909 Args.add(RValue::get(TmpAddr, *this), Ty);
5910 return TempLV;
5911}
5912
5913LValue
5916
5917 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
5918 it = OpaqueLValues.find(e);
5919
5920 if (it != OpaqueLValues.end())
5921 return it->second;
5922
5923 assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted");
5924 return EmitLValue(e->getSourceExpr());
5925}
5926
5927RValue
5930
5931 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
5932 it = OpaqueRValues.find(e);
5933
5934 if (it != OpaqueRValues.end())
5935 return it->second;
5936
5937 assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted");
5938 return EmitAnyExpr(e->getSourceExpr());
5939}
5940
5943 return OpaqueLValues.contains(E);
5944 return OpaqueRValues.contains(E);
5945}
5946
5948 const FieldDecl *FD,
5949 SourceLocation Loc) {
5950 QualType FT = FD->getType();
5951 LValue FieldLV = EmitLValueForField(LV, FD);
5952 switch (getEvaluationKind(FT)) {
5953 case TEK_Complex:
5954 return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
5955 case TEK_Aggregate:
5956 return FieldLV.asAggregateRValue();
5957 case TEK_Scalar:
5958 // This routine is used to load fields one-by-one to perform a copy, so
5959 // don't load reference fields.
5960 if (FD->getType()->isReferenceType())
5961 return RValue::get(FieldLV.getPointer(*this));
5962 // Call EmitLoadOfScalar except when the lvalue is a bitfield to emit a
5963 // primitive load.
5964 if (FieldLV.isBitField())
5965 return EmitLoadOfLValue(FieldLV, Loc);
5966 return RValue::get(EmitLoadOfScalar(FieldLV, Loc));
5967 }
5968 llvm_unreachable("bad evaluation kind");
5969}
5970
5971//===--------------------------------------------------------------------===//
5972// Expression Emission
5973//===--------------------------------------------------------------------===//
5974
5977 llvm::CallBase **CallOrInvoke) {
5978 llvm::CallBase *CallOrInvokeStorage;
5979 if (!CallOrInvoke) {
5980 CallOrInvoke = &CallOrInvokeStorage;
5981 }
5982
5983 auto AddCoroElideSafeOnExit = llvm::make_scope_exit([&] {
5984 if (E->isCoroElideSafe()) {
5985 auto *I = *CallOrInvoke;
5986 if (I)
5987 I->addFnAttr(llvm::Attribute::CoroElideSafe);
5988 }
5989 });
5990
5991 // Builtins never have block type.
5992 if (E->getCallee()->getType()->isBlockPointerType())
5993 return EmitBlockCallExpr(E, ReturnValue, CallOrInvoke);
5994
5995 if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
5996 return EmitCXXMemberCallExpr(CE, ReturnValue, CallOrInvoke);
5997
5998 if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
5999 return EmitCUDAKernelCallExpr(CE, ReturnValue, CallOrInvoke);
6000
6001 // A CXXOperatorCallExpr is created even for explicit object methods, but
6002 // these should be treated like static function call.
6003 if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
6004 if (const auto *MD =
6005 dyn_cast_if_present<CXXMethodDecl>(CE->getCalleeDecl());
6006 MD && MD->isImplicitObjectMemberFunction())
6007 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue, CallOrInvoke);
6008
6009 CGCallee callee = EmitCallee(E->getCallee());
6010
6011 if (callee.isBuiltin()) {
6012 return EmitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(),
6013 E, ReturnValue);
6014 }
6015
6016 if (callee.isPseudoDestructor()) {
6018 }
6019
6020 return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue,
6021 /*Chain=*/nullptr, CallOrInvoke);
6022}
6023
6024/// Emit a CallExpr without considering whether it might be a subclass.
6027 llvm::CallBase **CallOrInvoke) {
6028 CGCallee Callee = EmitCallee(E->getCallee());
6029 return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue,
6030 /*Chain=*/nullptr, CallOrInvoke);
6031}
6032
6033// Detect the unusual situation where an inline version is shadowed by a
6034// non-inline version. In that case we should pick the external one
6035// everywhere. That's GCC behavior too.
6037 for (const FunctionDecl *PD = FD; PD; PD = PD->getPreviousDecl())
6038 if (!PD->isInlineBuiltinDeclaration())
6039 return false;
6040 return true;
6041}
6042
6044 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
6045
6046 if (auto builtinID = FD->getBuiltinID()) {
6047 std::string NoBuiltinFD = ("no-builtin-" + FD->getName()).str();
6048 std::string NoBuiltins = "no-builtins";
6049
6050 StringRef Ident = CGF.CGM.getMangledName(GD);
6051 std::string FDInlineName = (Ident + ".inline").str();
6052
6053 bool IsPredefinedLibFunction =
6055 bool HasAttributeNoBuiltin =
6056 CGF.CurFn->getAttributes().hasFnAttr(NoBuiltinFD) ||
6057 CGF.CurFn->getAttributes().hasFnAttr(NoBuiltins);
6058
6059 // When directing calling an inline builtin, call it through it's mangled
6060 // name to make it clear it's not the actual builtin.
6061 if (CGF.CurFn->getName() != FDInlineName &&
6063 llvm::Constant *CalleePtr = CGF.CGM.getRawFunctionPointer(GD);
6064 llvm::Function *Fn = llvm::cast<llvm::Function>(CalleePtr);
6065 llvm::Module *M = Fn->getParent();
6066 llvm::Function *Clone = M->getFunction(FDInlineName);
6067 if (!Clone) {
6068 Clone = llvm::Function::Create(Fn->getFunctionType(),
6069 llvm::GlobalValue::InternalLinkage,
6070 Fn->getAddressSpace(), FDInlineName, M);
6071 Clone->addFnAttr(llvm::Attribute::AlwaysInline);
6072 }
6073 return CGCallee::forDirect(Clone, GD);
6074 }
6075
6076 // Replaceable builtins provide their own implementation of a builtin. If we
6077 // are in an inline builtin implementation, avoid trivial infinite
6078 // recursion. Honor __attribute__((no_builtin("foo"))) or
6079 // __attribute__((no_builtin)) on the current function unless foo is
6080 // not a predefined library function which means we must generate the
6081 // builtin no matter what.
6082 else if (!IsPredefinedLibFunction || !HasAttributeNoBuiltin)
6083 return CGCallee::forBuiltin(builtinID, FD);
6084 }
6085
6086 llvm::Constant *CalleePtr = CGF.CGM.getRawFunctionPointer(GD);
6087 if (CGF.CGM.getLangOpts().CUDA && !CGF.CGM.getLangOpts().CUDAIsDevice &&
6088 FD->hasAttr<CUDAGlobalAttr>())
6089 CalleePtr = CGF.CGM.getCUDARuntime().getKernelStub(
6090 cast<llvm::GlobalValue>(CalleePtr->stripPointerCasts()));
6091
6092 return CGCallee::forDirect(CalleePtr, GD);
6093}
6094
6096 if (DeviceKernelAttr::isOpenCLSpelling(FD->getAttr<DeviceKernelAttr>()))
6098 return GlobalDecl(FD);
6099}
6100
6102 E = E->IgnoreParens();
6103
6104 // Look through function-to-pointer decay.
6105 if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) {
6106 if (ICE->getCastKind() == CK_FunctionToPointerDecay ||
6107 ICE->getCastKind() == CK_BuiltinFnToFnPtr) {
6108 return EmitCallee(ICE->getSubExpr());
6109 }
6110
6111 // Try to remember the original __ptrauth qualifier for loads of
6112 // function pointers.
6113 if (ICE->getCastKind() == CK_LValueToRValue) {
6114 const Expr *SubExpr = ICE->getSubExpr();
6115 if (const auto *PtrType = SubExpr->getType()->getAs<PointerType>()) {
6116 std::pair<llvm::Value *, CGPointerAuthInfo> Result =
6118
6120 assert(FunctionType->isFunctionType());
6121
6122 GlobalDecl GD;
6123 if (const auto *VD =
6124 dyn_cast_or_null<VarDecl>(E->getReferencedDeclOfCallee())) {
6125 GD = GlobalDecl(VD);
6126 }
6128 CGCallee Callee(CalleeInfo, Result.first, Result.second);
6129 return Callee;
6130 }
6131 }
6132
6133 // Resolve direct calls.
6134 } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) {
6135 if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
6137 }
6138 } else if (auto ME = dyn_cast<MemberExpr>(E)) {
6139 if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) {
6140 EmitIgnoredExpr(ME->getBase());
6141 return EmitDirectCallee(*this, FD);
6142 }
6143
6144 // Look through template substitutions.
6145 } else if (auto NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
6146 return EmitCallee(NTTP->getReplacement());
6147
6148 // Treat pseudo-destructor calls differently.
6149 } else if (auto PDE = dyn_cast<CXXPseudoDestructorExpr>(E)) {
6151 }
6152
6153 // Otherwise, we have an indirect reference.
6154 llvm::Value *calleePtr;
6156 if (auto ptrType = E->getType()->getAs<PointerType>()) {
6157 calleePtr = EmitScalarExpr(E);
6158 functionType = ptrType->getPointeeType();
6159 } else {
6160 functionType = E->getType();
6161 calleePtr = EmitLValue(E, KnownNonNull).getPointer(*this);
6162 }
6163 assert(functionType->isFunctionType());
6164
6165 GlobalDecl GD;
6166 if (const auto *VD =
6167 dyn_cast_or_null<VarDecl>(E->getReferencedDeclOfCallee()))
6168 GD = GlobalDecl(VD);
6169
6170 CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), GD);
6171 CGPointerAuthInfo pointerAuth = CGM.getFunctionPointerAuthInfo(functionType);
6172 CGCallee callee(calleeInfo, calleePtr, pointerAuth);
6173 return callee;
6174}
6175
6177 // Comma expressions just emit their LHS then their RHS as an l-value.
6178 if (E->getOpcode() == BO_Comma) {
6179 EmitIgnoredExpr(E->getLHS());
6181 return EmitLValue(E->getRHS());
6182 }
6183
6184 if (E->getOpcode() == BO_PtrMemD ||
6185 E->getOpcode() == BO_PtrMemI)
6187
6188 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
6189
6190 // Create a Key Instructions source location atom group that covers both
6191 // LHS and RHS expressions. Nested RHS expressions may get subsequently
6192 // separately grouped (1 below):
6193 //
6194 // 1. `a = b = c` -> Two atoms.
6195 // 2. `x = new(1)` -> One atom (for both addr store and value store).
6196 // 3. Complex and agg assignment -> One atom.
6198
6199 // Note that in all of these cases, __block variables need the RHS
6200 // evaluated first just in case the variable gets moved by the RHS.
6201
6202 switch (getEvaluationKind(E->getType())) {
6203 case TEK_Scalar: {
6204 if (PointerAuthQualifier PtrAuth =
6205 E->getLHS()->getType().getPointerAuth()) {
6207 LValue CopiedLV = LV;
6208 CopiedLV.getQuals().removePointerAuth();
6209 llvm::Value *RV =
6210 EmitPointerAuthQualify(PtrAuth, E->getRHS(), CopiedLV.getAddress());
6211 EmitNullabilityCheck(CopiedLV, RV, E->getExprLoc());
6212 EmitStoreThroughLValue(RValue::get(RV), CopiedLV);
6213 return LV;
6214 }
6215
6216 switch (E->getLHS()->getType().getObjCLifetime()) {
6218 return EmitARCStoreStrong(E, /*ignored*/ false).first;
6219
6221 return EmitARCStoreAutoreleasing(E).first;
6222
6223 // No reason to do any of these differently.
6227 break;
6228 }
6229
6230 // TODO: Can we de-duplicate this code with the corresponding code in
6231 // CGExprScalar, similar to the way EmitCompoundAssignmentLValue works?
6232 RValue RV;
6233 llvm::Value *Previous = nullptr;
6234 QualType SrcType = E->getRHS()->getType();
6235 // Check if LHS is a bitfield, if RHS contains an implicit cast expression
6236 // we want to extract that value and potentially (if the bitfield sanitizer
6237 // is enabled) use it to check for an implicit conversion.
6238 if (E->getLHS()->refersToBitField()) {
6239 llvm::Value *RHS =
6241 RV = RValue::get(RHS);
6242 } else
6243 RV = EmitAnyExpr(E->getRHS());
6244
6246
6247 if (RV.isScalar())
6249
6250 if (LV.isBitField()) {
6251 llvm::Value *Result = nullptr;
6252 // If bitfield sanitizers are enabled we want to use the result
6253 // to check whether a truncation or sign change has occurred.
6254 if (SanOpts.has(SanitizerKind::ImplicitBitfieldConversion))
6256 else
6258
6259 // If the expression contained an implicit conversion, make sure
6260 // to use the value before the scalar conversion.
6261 llvm::Value *Src = Previous ? Previous : RV.getScalarVal();
6262 QualType DstType = E->getLHS()->getType();
6263 EmitBitfieldConversionCheck(Src, SrcType, Result, DstType,
6264 LV.getBitFieldInfo(), E->getExprLoc());
6265 } else
6266 EmitStoreThroughLValue(RV, LV);
6267
6268 if (getLangOpts().OpenMP)
6269 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
6270 E->getLHS());
6271 return LV;
6272 }
6273
6274 case TEK_Complex:
6276
6277 case TEK_Aggregate:
6278 // If the lang opt is HLSL and the LHS is a constant array
6279 // then we are performing a copy assignment and call a special
6280 // function because EmitAggExprToLValue emits to a temporary LValue
6282 return EmitHLSLArrayAssignLValue(E);
6283
6284 return EmitAggExprToLValue(E);
6285 }
6286 llvm_unreachable("bad evaluation kind");
6287}
6288
6289// This function implements trivial copy assignment for HLSL's
6290// assignable constant arrays.
6292 // Don't emit an LValue for the RHS because it might not be an LValue
6293 LValue LHS = EmitLValue(E->getLHS());
6294 // In C the RHS of an assignment operator is an RValue.
6295 // EmitAggregateAssign takes anan LValue for the RHS. Instead we can call
6296 // EmitInitializationToLValue to emit an RValue into an LValue.
6298 return LHS;
6299}
6300
6302 llvm::CallBase **CallOrInvoke) {
6303 RValue RV = EmitCallExpr(E, ReturnValueSlot(), CallOrInvoke);
6304
6305 if (!RV.isScalar())
6306 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
6308
6309 assert(E->getCallReturnType(getContext())->isReferenceType() &&
6310 "Can't have a scalar return unless the return type is a "
6311 "reference type!");
6312
6314}
6315
6317 // FIXME: This shouldn't require another copy.
6318 return EmitAggExprToLValue(E);
6319}
6320
6323 && "binding l-value to type which needs a temporary");
6324 AggValueSlot Slot = CreateAggTemp(E->getType());
6325 EmitCXXConstructExpr(E, Slot);
6327}
6328
6329LValue
6333
6335 return CGM.GetAddrOfMSGuidDecl(E->getGuidDecl())
6336 .withElementType(ConvertType(E->getType()));
6337}
6338
6343
6344LValue
6352
6355
6356 if (!RV.isScalar())
6357 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
6359
6360 assert(E->getMethodDecl()->getReturnType()->isReferenceType() &&
6361 "Can't have a scalar return unless the return type is a "
6362 "reference type!");
6363
6365}
6366
6368 Address V =
6369 CGM.getObjCRuntime().GetAddrOfSelector(*this, E->getSelector());
6371}
6372
6374 const ObjCIvarDecl *Ivar) {
6375 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
6376}
6377
6378llvm::Value *
6380 const ObjCIvarDecl *Ivar) {
6381 llvm::Value *OffsetValue = EmitIvarOffset(Interface, Ivar);
6382 QualType PointerDiffType = getContext().getPointerDiffType();
6383 return Builder.CreateZExtOrTrunc(OffsetValue,
6384 getTypes().ConvertType(PointerDiffType));
6385}
6386
6388 llvm::Value *BaseValue,
6389 const ObjCIvarDecl *Ivar,
6390 unsigned CVRQualifiers) {
6391 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
6392 Ivar, CVRQualifiers);
6393}
6394
6396 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
6397 llvm::Value *BaseValue = nullptr;
6398 const Expr *BaseExpr = E->getBase();
6399 Qualifiers BaseQuals;
6400 QualType ObjectTy;
6401 if (E->isArrow()) {
6402 BaseValue = EmitScalarExpr(BaseExpr);
6403 ObjectTy = BaseExpr->getType()->getPointeeType();
6404 BaseQuals = ObjectTy.getQualifiers();
6405 } else {
6406 LValue BaseLV = EmitLValue(BaseExpr);
6407 BaseValue = BaseLV.getPointer(*this);
6408 ObjectTy = BaseExpr->getType();
6409 BaseQuals = ObjectTy.getQualifiers();
6410 }
6411
6412 LValue LV =
6413 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
6414 BaseQuals.getCVRQualifiers());
6416 return LV;
6417}
6418
6420 // Can only get l-value for message expression returning aggregate type
6421 RValue RV = EmitAnyExprToTemp(E);
6422 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
6424}
6425
6427 const CGCallee &OrigCallee, const CallExpr *E,
6429 llvm::Value *Chain,
6430 llvm::CallBase **CallOrInvoke,
6431 CGFunctionInfo const **ResolvedFnInfo) {
6432 // Get the actual function type. The callee type will always be a pointer to
6433 // function type or a block pointer type.
6434 assert(CalleeType->isFunctionPointerType() &&
6435 "Call must have function pointer type!");
6436
6437 const Decl *TargetDecl =
6438 OrigCallee.getAbstractInfo().getCalleeDecl().getDecl();
6439
6440 assert((!isa_and_present<FunctionDecl>(TargetDecl) ||
6441 !cast<FunctionDecl>(TargetDecl)->isImmediateFunction()) &&
6442 "trying to emit a call to an immediate function");
6443
6444 CalleeType = getContext().getCanonicalType(CalleeType);
6445
6446 auto PointeeType = cast<PointerType>(CalleeType)->getPointeeType();
6447
6448 CGCallee Callee = OrigCallee;
6449
6450 if (SanOpts.has(SanitizerKind::Function) &&
6451 (!TargetDecl || !isa<FunctionDecl>(TargetDecl)) &&
6452 !isa<FunctionNoProtoType>(PointeeType)) {
6453 if (llvm::Constant *PrefixSig =
6454 CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
6455 auto CheckOrdinal = SanitizerKind::SO_Function;
6456 auto CheckHandler = SanitizerHandler::FunctionTypeMismatch;
6457 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
6458 auto *TypeHash = getUBSanFunctionTypeHash(PointeeType);
6459
6460 llvm::Type *PrefixSigType = PrefixSig->getType();
6461 llvm::StructType *PrefixStructTy = llvm::StructType::get(
6462 CGM.getLLVMContext(), {PrefixSigType, Int32Ty}, /*isPacked=*/true);
6463
6464 llvm::Value *CalleePtr = Callee.getFunctionPointer();
6465 if (CGM.getCodeGenOpts().PointerAuth.FunctionPointers) {
6466 // Use raw pointer since we are using the callee pointer as data here.
6467 Address Addr =
6468 Address(CalleePtr, CalleePtr->getType(),
6470 CalleePtr->getPointerAlignment(CGM.getDataLayout())),
6471 Callee.getPointerAuthInfo(), nullptr);
6472 CalleePtr = Addr.emitRawPointer(*this);
6473 }
6474
6475 // On 32-bit Arm, the low bit of a function pointer indicates whether
6476 // it's using the Arm or Thumb instruction set. The actual first
6477 // instruction lives at the same address either way, so we must clear
6478 // that low bit before using the function address to find the prefix
6479 // structure.
6480 //
6481 // This applies to both Arm and Thumb target triples, because
6482 // either one could be used in an interworking context where it
6483 // might be passed function pointers of both types.
6484 llvm::Value *AlignedCalleePtr;
6485 if (CGM.getTriple().isARM() || CGM.getTriple().isThumb()) {
6486 llvm::Value *CalleeAddress =
6487 Builder.CreatePtrToInt(CalleePtr, IntPtrTy);
6488 llvm::Value *Mask = llvm::ConstantInt::get(IntPtrTy, ~1);
6489 llvm::Value *AlignedCalleeAddress =
6490 Builder.CreateAnd(CalleeAddress, Mask);
6491 AlignedCalleePtr =
6492 Builder.CreateIntToPtr(AlignedCalleeAddress, CalleePtr->getType());
6493 } else {
6494 AlignedCalleePtr = CalleePtr;
6495 }
6496
6497 llvm::Value *CalleePrefixStruct = AlignedCalleePtr;
6498 llvm::Value *CalleeSigPtr =
6499 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, -1, 0);
6500 llvm::Value *CalleeSig =
6501 Builder.CreateAlignedLoad(PrefixSigType, CalleeSigPtr, getIntAlign());
6502 llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
6503
6504 llvm::BasicBlock *Cont = createBasicBlock("cont");
6505 llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
6506 Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
6507
6508 EmitBlock(TypeCheck);
6509 llvm::Value *CalleeTypeHash = Builder.CreateAlignedLoad(
6510 Int32Ty,
6511 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, -1, 1),
6512 getPointerAlign());
6513 llvm::Value *CalleeTypeHashMatch =
6514 Builder.CreateICmpEQ(CalleeTypeHash, TypeHash);
6515 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(E->getBeginLoc()),
6516 EmitCheckTypeDescriptor(CalleeType)};
6517 EmitCheck(std::make_pair(CalleeTypeHashMatch, CheckOrdinal), CheckHandler,
6518 StaticData, {CalleePtr});
6519
6520 Builder.CreateBr(Cont);
6521 EmitBlock(Cont);
6522 }
6523 }
6524
6525 const auto *FnType = cast<FunctionType>(PointeeType);
6526
6527 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
6528 FD && DeviceKernelAttr::isOpenCLSpelling(FD->getAttr<DeviceKernelAttr>()))
6529 CGM.getTargetCodeGenInfo().setOCLKernelStubCallingConvention(FnType);
6530
6531 bool CFIUnchecked =
6533
6534 // If we are checking indirect calls and this call is indirect, check that the
6535 // function pointer is a member of the bit set for the function type.
6536 if (SanOpts.has(SanitizerKind::CFIICall) &&
6537 (!TargetDecl || !isa<FunctionDecl>(TargetDecl)) && !CFIUnchecked) {
6538 auto CheckOrdinal = SanitizerKind::SO_CFIICall;
6539 auto CheckHandler = SanitizerHandler::CFICheckFail;
6540 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
6541 EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
6542
6543 llvm::Metadata *MD =
6544 CGM.CreateMetadataIdentifierForFnType(QualType(FnType, 0));
6545
6546 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
6547
6548 llvm::Value *CalleePtr = Callee.getFunctionPointer();
6549 llvm::Value *TypeTest = Builder.CreateCall(
6550 CGM.getIntrinsic(llvm::Intrinsic::type_test), {CalleePtr, TypeId});
6551
6552 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
6553 llvm::Constant *StaticData[] = {
6554 llvm::ConstantInt::get(Int8Ty, CFITCK_ICall),
6557 };
6558 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
6559 EmitCfiSlowPathCheck(CheckOrdinal, TypeTest, CrossDsoTypeId, CalleePtr,
6560 StaticData);
6561 } else {
6562 EmitCheck(std::make_pair(TypeTest, CheckOrdinal), CheckHandler,
6563 StaticData, {CalleePtr, llvm::UndefValue::get(IntPtrTy)});
6564 }
6565 }
6566
6567 CallArgList Args;
6568 if (Chain)
6569 Args.add(RValue::get(Chain), CGM.getContext().VoidPtrTy);
6570
6571 // C++17 requires that we evaluate arguments to a call using assignment syntax
6572 // right-to-left, and that we evaluate arguments to certain other operators
6573 // left-to-right. Note that we allow this to override the order dictated by
6574 // the calling convention on the MS ABI, which means that parameter
6575 // destruction order is not necessarily reverse construction order.
6576 // FIXME: Revisit this based on C++ committee response to unimplementability.
6578 bool StaticOperator = false;
6579 if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6580 if (OCE->isAssignmentOp())
6582 else {
6583 switch (OCE->getOperator()) {
6584 case OO_LessLess:
6585 case OO_GreaterGreater:
6586 case OO_AmpAmp:
6587 case OO_PipePipe:
6588 case OO_Comma:
6589 case OO_ArrowStar:
6591 break;
6592 default:
6593 break;
6594 }
6595 }
6596
6597 if (const auto *MD =
6598 dyn_cast_if_present<CXXMethodDecl>(OCE->getCalleeDecl());
6599 MD && MD->isStatic())
6600 StaticOperator = true;
6601 }
6602
6603 auto Arguments = E->arguments();
6604 if (StaticOperator) {
6605 // If we're calling a static operator, we need to emit the object argument
6606 // and ignore it.
6607 EmitIgnoredExpr(E->getArg(0));
6608 Arguments = drop_begin(Arguments, 1);
6609 }
6610 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), Arguments,
6611 E->getDirectCallee(), /*ParamsToSkip=*/0, Order);
6612
6613 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
6614 Args, FnType, /*ChainCall=*/Chain);
6615
6616 if (ResolvedFnInfo)
6617 *ResolvedFnInfo = &FnInfo;
6618
6619 // HIP function pointer contains kernel handle when it is used in triple
6620 // chevron. The kernel stub needs to be loaded from kernel handle and used
6621 // as callee.
6622 if (CGM.getLangOpts().HIP && !CGM.getLangOpts().CUDAIsDevice &&
6624 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
6625 llvm::Value *Handle = Callee.getFunctionPointer();
6626 auto *Stub = Builder.CreateLoad(
6627 Address(Handle, Handle->getType(), CGM.getPointerAlign()));
6628 Callee.setFunctionPointer(Stub);
6629 }
6630 llvm::CallBase *LocalCallOrInvoke = nullptr;
6631 RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &LocalCallOrInvoke,
6632 E == MustTailCall, E->getExprLoc());
6633
6634 if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
6635 // Generate function declaration DISuprogram in order to be used
6636 // in debug info about call sites.
6637 if (CGDebugInfo *DI = getDebugInfo()) {
6638 FunctionArgList Args;
6639 QualType ResTy = BuildFunctionArgList(CalleeDecl, Args);
6640 DI->EmitFuncDeclForCallSite(LocalCallOrInvoke,
6641 DI->getFunctionType(CalleeDecl, ResTy, Args),
6642 CalleeDecl);
6643 }
6644 if (CalleeDecl->hasAttr<RestrictAttr>() ||
6645 CalleeDecl->hasAttr<AllocSizeAttr>()) {
6646 // Function has 'malloc' (aka. 'restrict') or 'alloc_size' attribute.
6647 if (SanOpts.has(SanitizerKind::AllocToken)) {
6648 // Set !alloc_token metadata.
6649 EmitAllocToken(LocalCallOrInvoke, E);
6650 }
6651 }
6652 }
6653 if (CallOrInvoke)
6654 *CallOrInvoke = LocalCallOrInvoke;
6655
6656 return Call;
6657}
6658
6661 Address BaseAddr = Address::invalid();
6662 if (E->getOpcode() == BO_PtrMemI) {
6663 BaseAddr = EmitPointerWithAlignment(E->getLHS());
6664 } else {
6665 BaseAddr = EmitLValue(E->getLHS()).getAddress();
6666 }
6667
6668 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
6669 const auto *MPT = E->getRHS()->getType()->castAs<MemberPointerType>();
6670
6671 LValueBaseInfo BaseInfo;
6672 TBAAAccessInfo TBAAInfo;
6673 bool IsInBounds = !getLangOpts().PointerOverflowDefined &&
6676 E, BaseAddr, OffsetV, MPT, IsInBounds, &BaseInfo, &TBAAInfo);
6677
6678 return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), BaseInfo, TBAAInfo);
6679}
6680
6681/// Given the address of a temporary variable, produce an r-value of
6682/// its type.
6684 QualType type,
6685 SourceLocation loc) {
6687 switch (getEvaluationKind(type)) {
6688 case TEK_Complex:
6689 return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
6690 case TEK_Aggregate:
6691 return lvalue.asAggregateRValue();
6692 case TEK_Scalar:
6693 return RValue::get(EmitLoadOfScalar(lvalue, loc));
6694 }
6695 llvm_unreachable("bad evaluation kind");
6696}
6697
6698void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
6699 assert(Val->getType()->isFPOrFPVectorTy());
6700 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
6701 return;
6702
6703 llvm::MDBuilder MDHelper(getLLVMContext());
6704 llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
6705
6706 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
6707}
6708
6710 llvm::Type *EltTy = Val->getType()->getScalarType();
6711 if (!EltTy->isFloatTy())
6712 return;
6713
6714 if ((getLangOpts().OpenCL &&
6715 !CGM.getCodeGenOpts().OpenCLCorrectlyRoundedDivSqrt) ||
6716 (getLangOpts().HIP && getLangOpts().CUDAIsDevice &&
6717 !CGM.getCodeGenOpts().HIPCorrectlyRoundedDivSqrt)) {
6718 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 3ulp
6719 //
6720 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt
6721 // build option allows an application to specify that single precision
6722 // floating-point divide (x/y and 1/x) and sqrt used in the program
6723 // source are correctly rounded.
6724 //
6725 // TODO: CUDA has a prec-sqrt flag
6726 SetFPAccuracy(Val, 3.0f);
6727 }
6728}
6729
6731 llvm::Type *EltTy = Val->getType()->getScalarType();
6732 if (!EltTy->isFloatTy())
6733 return;
6734
6735 if ((getLangOpts().OpenCL &&
6736 !CGM.getCodeGenOpts().OpenCLCorrectlyRoundedDivSqrt) ||
6737 (getLangOpts().HIP && getLangOpts().CUDAIsDevice &&
6738 !CGM.getCodeGenOpts().HIPCorrectlyRoundedDivSqrt)) {
6739 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 2.5ulp
6740 //
6741 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt
6742 // build option allows an application to specify that single precision
6743 // floating-point divide (x/y and 1/x) and sqrt used in the program
6744 // source are correctly rounded.
6745 //
6746 // TODO: CUDA has a prec-div flag
6747 SetFPAccuracy(Val, 2.5f);
6748 }
6749}
6750
6751namespace {
6752 struct LValueOrRValue {
6753 LValue LV;
6754 RValue RV;
6755 };
6756}
6757
6758static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
6759 const PseudoObjectExpr *E,
6760 bool forLValue,
6761 AggValueSlot slot) {
6763
6764 // Find the result expression, if any.
6765 const Expr *resultExpr = E->getResultExpr();
6766 LValueOrRValue result;
6767
6769 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
6770 const Expr *semantic = *i;
6771
6772 // If this semantic expression is an opaque value, bind it
6773 // to the result of its source expression.
6774 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
6775 // Skip unique OVEs.
6776 if (ov->isUnique()) {
6777 assert(ov != resultExpr &&
6778 "A unique OVE cannot be used as the result expression");
6779 continue;
6780 }
6781
6782 // If this is the result expression, we may need to evaluate
6783 // directly into the slot.
6785 OVMA opaqueData;
6786 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
6788 CGF.EmitAggExpr(ov->getSourceExpr(), slot);
6789 LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(),
6791 opaqueData = OVMA::bind(CGF, ov, LV);
6792 result.RV = slot.asRValue();
6793
6794 // Otherwise, emit as normal.
6795 } else {
6796 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
6797
6798 // If this is the result, also evaluate the result now.
6799 if (ov == resultExpr) {
6800 if (forLValue)
6801 result.LV = CGF.EmitLValue(ov);
6802 else
6803 result.RV = CGF.EmitAnyExpr(ov, slot);
6804 }
6805 }
6806
6807 opaques.push_back(opaqueData);
6808
6809 // Otherwise, if the expression is the result, evaluate it
6810 // and remember the result.
6811 } else if (semantic == resultExpr) {
6812 if (forLValue)
6813 result.LV = CGF.EmitLValue(semantic);
6814 else
6815 result.RV = CGF.EmitAnyExpr(semantic, slot);
6816
6817 // Otherwise, evaluate the expression in an ignored context.
6818 } else {
6819 CGF.EmitIgnoredExpr(semantic);
6820 }
6821 }
6822
6823 // Unbind all the opaques now.
6824 for (CodeGenFunction::OpaqueValueMappingData &opaque : opaques)
6825 opaque.unbind(CGF);
6826
6827 return result;
6828}
6829
6831 AggValueSlot slot) {
6832 return emitPseudoObjectExpr(*this, E, false, slot).RV;
6833}
6834
6838
6840 LValue Val, SmallVectorImpl<LValue> &AccessList) {
6841
6843 std::tuple<LValue, QualType, llvm::SmallVector<llvm::Value *, 4>>, 16>
6844 WorkList;
6845 llvm::IntegerType *IdxTy = llvm::IntegerType::get(getLLVMContext(), 32);
6846 WorkList.push_back({Val, Val.getType(), {llvm::ConstantInt::get(IdxTy, 0)}});
6847
6848 while (!WorkList.empty()) {
6849 auto [LVal, T, IdxList] = WorkList.pop_back_val();
6850 T = T.getCanonicalType().getUnqualifiedType();
6851 assert(!isa<MatrixType>(T) && "Matrix types not yet supported in HLSL");
6852
6853 if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) {
6854 uint64_t Size = CAT->getZExtSize();
6855 for (int64_t I = Size - 1; I > -1; I--) {
6856 llvm::SmallVector<llvm::Value *, 4> IdxListCopy = IdxList;
6857 IdxListCopy.push_back(llvm::ConstantInt::get(IdxTy, I));
6858 WorkList.emplace_back(LVal, CAT->getElementType(), IdxListCopy);
6859 }
6860 } else if (const auto *RT = dyn_cast<RecordType>(T)) {
6861 const RecordDecl *Record = RT->getDecl()->getDefinitionOrSelf();
6862 assert(!Record->isUnion() && "Union types not supported in flat cast.");
6863
6864 const CXXRecordDecl *CXXD = dyn_cast<CXXRecordDecl>(Record);
6865
6867 std::tuple<LValue, QualType, llvm::SmallVector<llvm::Value *, 4>>, 16>
6868 ReverseList;
6869 if (CXXD && CXXD->isStandardLayout())
6871
6872 // deal with potential base classes
6873 if (CXXD && !CXXD->isStandardLayout()) {
6874 if (CXXD->getNumBases() > 0) {
6875 assert(CXXD->getNumBases() == 1 &&
6876 "HLSL doesn't support multiple inheritance.");
6877 auto Base = CXXD->bases_begin();
6878 llvm::SmallVector<llvm::Value *, 4> IdxListCopy = IdxList;
6879 IdxListCopy.push_back(llvm::ConstantInt::get(
6880 IdxTy, 0)); // base struct should be at index zero
6881 ReverseList.emplace_back(LVal, Base->getType(), IdxListCopy);
6882 }
6883 }
6884
6885 const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(Record);
6886
6887 llvm::Type *LLVMT = ConvertTypeForMem(T);
6889 LValue RLValue;
6890 bool createdGEP = false;
6891 for (auto *FD : Record->fields()) {
6892 if (FD->isBitField()) {
6893 if (FD->isUnnamedBitField())
6894 continue;
6895 if (!createdGEP) {
6896 createdGEP = true;
6897 Address GEP = Builder.CreateInBoundsGEP(LVal.getAddress(), IdxList,
6898 LLVMT, Align, "gep");
6899 RLValue = MakeAddrLValue(GEP, T);
6900 }
6901 LValue FieldLVal = EmitLValueForField(RLValue, FD, true);
6902 ReverseList.push_back({FieldLVal, FD->getType(), {}});
6903 } else {
6904 llvm::SmallVector<llvm::Value *, 4> IdxListCopy = IdxList;
6905 IdxListCopy.push_back(
6906 llvm::ConstantInt::get(IdxTy, Layout.getLLVMFieldNo(FD)));
6907 ReverseList.emplace_back(LVal, FD->getType(), IdxListCopy);
6908 }
6909 }
6910
6911 std::reverse(ReverseList.begin(), ReverseList.end());
6912 llvm::append_range(WorkList, ReverseList);
6913 } else if (const auto *VT = dyn_cast<VectorType>(T)) {
6914 llvm::Type *LLVMT = ConvertTypeForMem(T);
6916 Address GEP = Builder.CreateInBoundsGEP(LVal.getAddress(), IdxList, LLVMT,
6917 Align, "vector.gep");
6918 LValue Base = MakeAddrLValue(GEP, T);
6919 for (unsigned I = 0, E = VT->getNumElements(); I < E; I++) {
6920 llvm::Constant *Idx = llvm::ConstantInt::get(IdxTy, I);
6921 LValue LV =
6922 LValue::MakeVectorElt(Base.getAddress(), Idx, VT->getElementType(),
6923 Base.getBaseInfo(), TBAAAccessInfo());
6924 AccessList.emplace_back(LV);
6925 }
6926 } else { // a scalar/builtin type
6927 if (!IdxList.empty()) {
6928 llvm::Type *LLVMT = ConvertTypeForMem(T);
6930 Address GEP = Builder.CreateInBoundsGEP(LVal.getAddress(), IdxList,
6931 LLVMT, Align, "gep");
6932 AccessList.emplace_back(MakeAddrLValue(GEP, T));
6933 } else // must be a bitfield we already created an lvalue for
6934 AccessList.emplace_back(LVal);
6935 }
6936 }
6937}
Defines the clang::ASTContext interface.
#define V(N, I)
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines enum values for all the target-independent builtin functions.
static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, LValue &LV, bool IsMemberAccess=false)
Definition CGExpr.cpp:2905
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:3174
static llvm::Value * emitHashMix(CGBuilderTy &Builder, llvm::Value *Acc, llvm::Value *Ptr)
Definition CGExpr.cpp:714
static const Expr * isSimpleArrayDecayOperand(const Expr *E)
isSimpleArrayDecayOperand - If the specified expr is a simple decay from an array to pointer,...
Definition CGExpr.cpp:4306
static bool getFieldOffsetInBits(CodeGenFunction &CGF, const RecordDecl *RD, const FieldDecl *Field, int64_t &Offset)
The offset of a field from the beginning of the record.
Definition CGExpr.cpp:4495
static bool hasBPFPreserveStaticOffset(const RecordDecl *D)
Definition CGExpr.cpp:4361
ConstantEmissionKind
Can we constant-emit a load of a reference to a variable of the given type?
Definition CGExpr.cpp:1876
@ CEK_AsReferenceOnly
Definition CGExpr.cpp:1878
@ CEK_AsValueOnly
Definition CGExpr.cpp:1880
@ CEK_None
Definition CGExpr.cpp:1877
@ CEK_AsValueOrReference
Definition CGExpr.cpp:1879
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:1849
static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, llvm::Value *ThisValue)
Definition CGExpr.cpp:3162
static std::optional< LValue > EmitLValueOrThrowExpression(CodeGenFunction &CGF, const Expr *Operand)
Emit the operand of a glvalue conditional operator.
Definition CGExpr.cpp:5532
static CheckRecoverableKind getRecoverableKind(SanitizerKind::SanitizerOrdinal Ordinal)
Definition CGExpr.cpp:3754
static llvm::Value * emitArraySubscriptGEP(CodeGenFunction &CGF, llvm::Type *elemType, llvm::Value *ptr, ArrayRef< llvm::Value * > indices, bool inbounds, bool signedIndices, SourceLocation loc, const llvm::Twine &name="arrayidx")
Definition CGExpr.cpp:4320
SmallVector< llvm::Value *, 8 > RecIndicesTy
Definition CGExpr.cpp:1154
static GlobalDecl getGlobalDeclForDirectCall(const FunctionDecl *FD)
Definition CGExpr.cpp:6095
static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E, GlobalDecl GD)
Definition CGExpr.cpp:3149
static RawAddress MaybeConvertMatrixAddress(RawAddress Addr, CodeGenFunction &CGF, bool IsVector=true)
Definition CGExpr.cpp:2247
static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, const PseudoObjectExpr *E, bool forLValue, AggValueSlot slot)
Definition CGExpr.cpp:6758
static Address wrapWithBPFPreserveStaticOffset(CodeGenFunction &CGF, Address &Addr)
Definition CGExpr.cpp:4377
static llvm::StringRef GetUBSanTrapForHandler(SanitizerHandler ID)
Definition CGExpr.cpp:91
static llvm::Value * getArrayIndexingBound(CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType, LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel)
If Base is known to point to the start of an array, return the length of that array.
Definition CGExpr.cpp:1006
static RValue EmitLoadOfMatrixLValue(LValue LV, SourceLocation Loc, CodeGenFunction &CGF)
Definition CGExpr.cpp:2351
static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type)
Definition CGExpr.cpp:1882
static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base, const FieldDecl *Field, bool IsInBounds)
Get the address of a zero-sized field within a record.
Definition CGExpr.cpp:5228
static QualType getConstantExprReferredType(const FullExpr *E, const ASTContext &Ctx)
Definition CGExpr.cpp:1681
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, const FieldDecl *field, bool IsInBounds)
Drill down to the storage of a field without walking into reference types.
Definition CGExpr.cpp:5245
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, llvm::APInt &Min, llvm::APInt &End, bool StrictEnums, bool IsBool)
Definition CGExpr.cpp:2021
static std::optional< int64_t > getOffsetDifferenceInBits(CodeGenFunction &CGF, const FieldDecl *FD1, const FieldDecl *FD2)
Returns the relative offset difference between FD1 and FD2.
Definition CGExpr.cpp:4526
static CGCallee EmitDirectCallee(CodeGenFunction &CGF, GlobalDecl GD)
Definition CGExpr.cpp:6043
static LValue EmitThreadPrivateVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, llvm::Type *RealVarTy, SourceLocation Loc)
Definition CGExpr.cpp:3002
static bool getGEPIndicesToField(CodeGenFunction &CGF, const RecordDecl *RD, const FieldDecl *Field, RecIndicesTy &Indices)
Definition CGExpr.cpp:1156
static bool OnlyHasInlineBuiltinDeclaration(const FunctionDecl *FD)
Definition CGExpr.cpp:6036
static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, const Expr *E, const VarDecl *VD)
Definition CGExpr.cpp:3098
static bool hasAnyVptr(const QualType Type, const ASTContext &Context)
Definition CGExpr.cpp:5274
static bool IsPreserveAIArrayBase(CodeGenFunction &CGF, const Expr *ArrayBase)
Given an array base, check whether its member access belongs to a record with preserve_access_index a...
Definition CGExpr.cpp:4390
static Address emitDeclTargetVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType T)
Definition CGExpr.cpp:3016
VariableTypeDescriptorKind
Definition CGExpr.cpp:76
@ TK_Float
A floating-point type.
Definition CGExpr.cpp:80
@ TK_Unknown
Any other type. The value representation is unspecified.
Definition CGExpr.cpp:84
@ TK_Integer
An integer type.
Definition CGExpr.cpp:78
@ TK_BitInt
An _BitInt(N) type.
Definition CGExpr.cpp:82
static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue, bool isInit, CodeGenFunction &CGF)
Definition CGExpr.cpp:2272
static Address EmitPointerWithAlignment(const Expr *E, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo, KnownNonNull_t IsKnownNonNull, CodeGenFunction &CGF)
Definition CGExpr.cpp:1413
static Address emitPreserveStructAccess(CodeGenFunction &CGF, LValue base, Address addr, const FieldDecl *field)
Definition CGExpr.cpp:5261
const SanitizerHandlerInfo SanitizerHandlers[]
Definition CGExpr.cpp:3771
static void emitCheckHandlerCall(CodeGenFunction &CGF, llvm::FunctionType *FnType, ArrayRef< llvm::Value * > FnArgs, SanitizerHandler CheckHandler, CheckRecoverableKind RecoverKind, bool IsFatal, llvm::BasicBlock *ContBB, bool NoMerge)
Definition CGExpr.cpp:3777
static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, LValueBaseInfo &BaseInfo, TBAAAccessInfo &TBAAInfo, QualType BaseTy, QualType ElTy, bool IsLowerBound)
Definition CGExpr.cpp:4837
static mlir::Value emitPointerArithmetic(CIRGenFunction &cgf, const BinOpInfo &op, bool isSubtraction)
Emit pointer + index arithmetic.
static Address createReferenceTemporary(CIRGenFunction &cgf, const MaterializeTemporaryExpr *m, const Expr *inner)
static bool isAAPCS(const TargetInfo &targetInfo)
Helper method to check if the underlying ABI is AAPCS.
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx, CharUnits eltSize)
static void pushTemporaryCleanup(CIRGenFunction &cgf, const MaterializeTemporaryExpr *m, const Expr *e, Address referenceTemporary)
static QualType getFixedSizeElementType(const ASTContext &astContext, const VariableArrayType *vla)
static bool canEmitSpuriousReferenceToVariable(CIRGenFunction &cgf, const DeclRefExpr *e, const VarDecl *vd)
Determine whether we can emit a reference to vd from the current context, despite not necessarily hav...
static DeclRefExpr * tryToConvertMemberExprToDeclRefExpr(CIRGenFunction &cgf, const MemberExpr *me)
FormatToken * Previous
The previous token in the unwrapped line.
static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target)
llvm::MachO::Record Record
Definition MachO.h:31
Defines the clang::Module class, which describes a module in the source code.
static const SanitizerMask AlwaysRecoverable
static const SanitizerMask Unrecoverable
#define LIST_SANITIZER_CHECKS
SanitizerHandler
Defines the SourceManager interface.
static QualType getPointeeType(const MemRegion *R)
a trap message and trap category.
const LValueBase getLValueBase() const
Definition APValue.cpp:983
bool isLValue() const
Definition APValue.h:472
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
SourceManager & getSourceManager()
Definition ASTContext.h:833
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
uint64_t getFieldOffset(const ValueDecl *FD) const
Get the offset of a FieldDecl or IndirectFieldDecl, in bits.
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
Builtin::Context & BuiltinInfo
Definition ASTContext.h:774
const LangOptions & getLangOpts() const
Definition ASTContext.h:926
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
CanQualType BoolTy
llvm::DenseMap< const CXXMethodDecl *, CXXCastPath > LambdaCastPaths
For capturing lambdas with an explicit object parameter whose type is derived from the lambda type,...
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const VariableArrayType * getAsVariableArrayType(QualType T) const
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
bool isSentinelNullExpr(const Expr *E)
uint64_t getCharWidth() const
Return the size of the character type, in bits.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition Expr.h:4287
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Definition Expr.h:4465
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4471
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4477
This class represents BOTH the OpenMP Array Section and OpenACC 'subarray', with a boolean differenti...
Definition Expr.h:7105
Expr * getBase()
Get base of the array section.
Definition Expr.h:7183
Expr * getLength()
Get length of array section.
Definition Expr.h:7193
static QualType getBaseOriginalType(const Expr *Base)
Return original type of the base expression for array section.
Definition Expr.cpp:5263
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:7222
Expr * getLowerBound()
Get lower bound of array section.
Definition Expr.h:7187
bool isOpenACCArraySection() const
Definition Expr.h:7180
SourceLocation getColonLocFirst() const
Definition Expr.h:7214
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition Expr.h:2721
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:2776
Expr * getLHS()
An array access can be written A[4] or 4[A] (both are equivalent).
Definition Expr.h:2750
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3722
QualType getElementType() const
Definition TypeBase.h:3734
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:3972
Expr * getLHS() const
Definition Expr.h:4022
SourceLocation getExprLoc() const
Definition Expr.h:4013
Expr * getRHS() const
Definition Expr.h:4024
static bool isAdditiveOp(Opcode Opc)
Definition Expr.h:4058
Opcode getOpcode() const
Definition Expr.h:4017
A fixed int type of a specified bitwidth.
Definition TypeBase.h:8130
unsigned getNumBits() const
Definition TypeBase.h:8142
bool isPredefinedLibFunction(unsigned ID) const
Determines whether this builtin is a predefined libc/libm function, such as "malloc",...
Definition Builtins.h:313
Represents binding an expression to a temporary.
Definition ExprCXX.h:1494
CXXTemporary * getTemporary()
Definition ExprCXX.h:1512
const Expr * getSubExpr() const
Definition ExprCXX.h:1516
Represents a call to a C++ constructor.
Definition ExprCXX.h:1549
Represents a C++ destructor within a class.
Definition DeclCXX.h:2869
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition DeclCXX.h:1366
bool isStandardLayout() const
Determine whether this class is standard-layout per C++ [class]p7.
Definition DeclCXX.h:1225
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition DeclCXX.h:602
base_class_iterator bases_begin()
Definition DeclCXX.h:615
bool isDynamicClass() const
Definition DeclCXX.h:574
bool hasDefinition() const
Definition DeclCXX.h:561
const CXXRecordDecl * getStandardLayoutBaseWithFields() const
If this is a standard-layout class or union, any and all data members will be declared in the same ty...
Definition DeclCXX.cpp:559
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:848
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition ExprCXX.h:1069
MSGuidDecl * getGuidDecl() const
Definition ExprCXX.h:1115
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3081
SourceLocation getBeginLoc() const
Definition Expr.h:3211
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3060
Expr * getCallee()
Definition Expr.h:3024
bool isCoroElideSafe() const
Definition Expr.h:3051
arg_range arguments()
Definition Expr.h:3129
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1599
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3610
path_iterator path_begin()
Definition Expr.h:3680
CastKind getCastKind() const
Definition Expr.h:3654
bool changesVolatileQualification() const
Return.
Definition Expr.h:3744
path_iterator path_end()
Definition Expr.h:3681
Expr * getSubExpr()
Definition Expr.h:3660
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
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:207
llvm::MaybeAlign getAsMaybeAlign() const
getAsMaybeAlign - Returns Quantity as a valid llvm::Align or std::nullopt, Beware llvm::MaybeAlign as...
Definition CharUnits.h:194
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
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:214
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
@ None
Trap Messages are omitted.
@ Detailed
Trap Message includes more context (e.g.
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Value * getBasePointer() const
Definition Address.h:198
static Address invalid()
Definition Address.h:176
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
CharUnits getAlignment() const
Definition Address.h:194
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:209
Address withPointer(llvm::Value *NewPointer, KnownNonNull_t IsKnownNonNull) const
Return address with different pointer, but same element type and alignment.
Definition Address.h:261
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
An aggregate value slot.
Definition CGValue.h:504
static AggValueSlot ignored()
ignored - Returns an aggregate value slot indicating that the aggregate value is being ignored.
Definition CGValue.h:572
Address getAddress() const
Definition CGValue.h:644
void setExternallyDestructed(bool destructed=true)
Definition CGValue.h:613
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition CGValue.h:602
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
forAddr - Make a slot for an aggregate value.
Definition CGValue.h:587
RValue asRValue() const
Definition CGValue.h:666
A scoped helper to set the current source atom group for CGDebugInfo::addInstToCurrentSourceAtom.
A scoped helper to set the current debug location to the specified location or preferred location of ...
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition CGBuilder.h:309
Address CreateGEP(CodeGenFunction &CGF, Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition CGBuilder.h:296
Address CreateConstGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::Twine &Name="")
Definition CGBuilder.h:335
Address CreateConstArrayGEP(Address Addr, uint64_t Index, const llvm::Twine &Name="")
Given addr = [n x T]* ... produce name = getelementptr inbounds addr, i64 0, i64 index where i64 is a...
Definition CGBuilder.h:245
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition CGBuilder.h:223
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:112
Address CreateConstByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Definition CGBuilder.h:319
Address CreatePreserveStructAccessIndex(Address Addr, unsigned Index, unsigned FieldIndex, llvm::MDNode *DbgInfo)
Definition CGBuilder.h:417
Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty, llvm::Type *ElementTy, const llvm::Twine &Name="")
Definition CGBuilder.h:193
virtual llvm::Function * getKernelStub(llvm::GlobalValue *Handle)=0
Get kernel stub by kernel handle.
virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr)=0
Emit code to force the execution of a destructor during global teardown.
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...
virtual bool usesThreadWrapperFunction(const VarDecl *VD) const =0
Abstract information about a function or function prototype.
Definition CGCall.h:41
const GlobalDecl getCalleeDecl() const
Definition CGCall.h:59
All available information about a concrete callee.
Definition CGCall.h:63
CGCalleeInfo getAbstractInfo() const
Definition CGCall.h:180
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition CGCall.h:172
bool isPseudoDestructor() const
Definition CGCall.h:169
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition CGCall.h:123
unsigned getBuiltinID() const
Definition CGCall.h:164
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:137
bool isBuiltin() const
Definition CGCall.h:157
const FunctionDecl * getBuiltinDecl() const
Definition CGCall.h:160
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition CGCall.h:131
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:59
llvm::DIType * getOrCreateStandaloneType(QualType Ty, SourceLocation Loc)
Emit standalone debug info for a type.
llvm::DILocation * CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation, StringRef Category, StringRef FailureMsg)
Create a debug location from TrapLocation that adds an artificial inline frame where the frame name i...
llvm::DIType * getOrCreateRecordType(QualType Ty, SourceLocation L)
Emit record type's standalone debug info.
CGFunctionInfo - Class to encapsulate the information about a function definition.
virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD, Address VDAddr, SourceLocation Loc)
Returns address of the threadprivate variable for the current thread.
virtual ConstantAddress getAddrOfDeclareTargetVar(const VarDecl *VD)
Returns the address of the variable marked as declare target with link clause OR as declare target wi...
bool hasRequiresUnifiedSharedMemory() const
Return whether the unified_shared_memory has been specified.
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
unsigned getLLVMFieldNo(const FieldDecl *FD) const
Return llvm::StructType element number that corresponds to the field FD.
bool containsFieldDecl(const FieldDecl *FD) const
CallArgList - Type for representing both the value and type of arguments in a call.
Definition CGCall.h:274
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse, const Expr *writebackExpr=nullptr)
Definition CGCall.h:320
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
An object to manage conditionally-evaluated expressions.
llvm::BasicBlock * getStartingBlock() const
Returns a block which will be executed prior to each evaluation of the conditional code.
static ConstantEmission forValue(llvm::Constant *C)
static ConstantEmission forReference(llvm::Constant *C)
LValue getReferenceLValue(CodeGenFunction &CGF, const Expr *RefExpr) const
A non-RAII class containing all the information about a bound opaque value.
static OpaqueValueMappingData bind(CodeGenFunction &CGF, const OpaqueValueExpr *ov, const Expr *e)
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
LValue EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E)
Definition CGExpr.cpp:4817
LValue EmitCoawaitLValue(const CoawaitExpr *E)
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass, VTableAuthMode AuthMode=VTableAuthMode::Authenticate)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
Definition CGClass.cpp:2752
llvm::Value * EmitObjCConsumeObject(QualType T, llvm::Value *Ptr)
Produce the code for a CK_ARCConsumeObject.
Definition CGObjC.cpp:2152
LValue EmitLoadOfReferenceLValue(LValue RefLVal)
Definition CGExpr.cpp:3071
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr, const VarDecl *ConditionalDecl=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, ReturnValueSlot Return=ReturnValueSlot())
Definition CGObjC.cpp:573
llvm::Value * emitBoolVecConversion(llvm::Value *SrcVec, unsigned NumElementsDst, const llvm::Twine &Name="")
void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest)
LValue EmitCXXConstructLValue(const CXXConstructExpr *E)
Definition CGExpr.cpp:6321
LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E)
Definition CGExpr.cpp:5635
std::pair< LValue, llvm::Value * > EmitARCStoreAutoreleasing(const BinaryOperator *e)
Definition CGObjC.cpp:3679
ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
Definition CGExpr.cpp:1313
void SetDivFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Definition CGExpr.cpp:6730
SanitizerSet SanOpts
Sanitizers enabled for this function.
LValue EmitInitListLValue(const InitListExpr *E)
Definition CGExpr.cpp:5519
bool isUnderlyingBasePointerConstantNull(const Expr *E)
Check whether the underlying base pointer is a constant null.
Definition CGExpr.cpp:5101
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2663
RawAddress CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
Definition CGExpr.cpp:183
LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E, bool Accessed=false)
Definition CGExpr.cpp:4603
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E)
Definition CGExpr.cpp:6353
llvm::Value * GetCountedByFieldExprGEP(const Expr *Base, const FieldDecl *FD, const FieldDecl *CountDecl)
Definition CGExpr.cpp:1187
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
const CastExpr * CurCast
If a cast expression is being visited, this holds the current cast's expression.
llvm::Type * ConvertType(QualType T)
Address EmitCXXUuidofExpr(const CXXUuidofExpr *E)
Definition CGExpr.cpp:6334
void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK)
CGCapturedStmtInfo * CapturedStmtInfo
RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E)
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
llvm::Value * EmitARCRetain(QualType type, llvm::Value *value)
Produce the code to do a retain.
Definition CGObjC.cpp:2328
llvm::Value * EmitPointerAuthQualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType ValueType, Address StorageAddress, bool IsKnownNonNull)
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
void EmitAggFinalDestCopy(QualType Type, AggValueSlot Dest, const LValue &Src, ExprValueKind SrcKind)
EmitAggFinalDestCopy - Emit copy of the specified aggregate into destination address.
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:286
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
Given a value of type T* that may not be to a complete object, construct an l-value with the natural ...
void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst)
Definition CGExpr.cpp:2800
RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke)
LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E)
Definition CGExpr.cpp:3537
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E)
Definition CGExpr.cpp:5492
RValue convertTempToRValue(Address addr, QualType type, SourceLocation Loc)
Given the address of a temporary variable, produce an r-value of its type.
Definition CGExpr.cpp:6683
LValue EmitObjCIsaExpr(const ObjCIsaExpr *E)
void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, llvm::Value **Result=nullptr)
EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints as EmitStoreThroughLValue.
Definition CGExpr.cpp:2721
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition CGExpr.cpp:3689
LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E)
Definition CGExpr.cpp:6339
llvm::Value * EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
void SetSqrtFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Definition CGExpr.cpp:6709
RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke=nullptr)
Emit a CallExpr without considering whether it might be a subclass.
Definition CGExpr.cpp:6025
static bool isNullPointerAllowed(TypeCheckKind TCK)
Determine whether the pointer type check TCK permits null pointers.
Definition CGExpr.cpp:723
void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index, QualType IndexType, bool Accessed)
Emit a check that Base points into an array object, which we can access at index Index.
Definition CGExpr.cpp:1238
RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot=AggValueSlot::ignored())
Definition CGExpr.cpp:6830
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction, llvm::Value *Backup)
See CGDebugInfo::addInstToCurrentSourceAtom.
unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex)
Get the record field index as represented in debug info.
Definition CGExpr.cpp:5211
const LangOptions & getLangOpts() const
void EmitCfiCheckFail()
Emit a cross-DSO CFI failure handling function.
Definition CGExpr.cpp:4060
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition CGExpr.cpp:686
llvm::Value * EmitARCStoreStrong(LValue lvalue, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition CGObjC.cpp:2545
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E)
Definition CGExpr.cpp:6660
LValue EmitLValueForIvar(QualType ObjectTy, llvm::Value *Base, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers)
Definition CGExpr.cpp:6387
Address GetAddressOfDerivedClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
Definition CGClass.cpp:394
void EmitIgnoredConditionalOperator(const AbstractConditionalOperator *E)
Definition CGExpr.cpp:5617
Address EmitFieldAnnotations(const FieldDecl *D, Address V)
Emit field annotations for the given field & value.
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:2278
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition CGDecl.cpp:787
void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc)
Given an assignment *LHS = RHS, emit a test that checks if RHS is nonnull, if LHS is marked _Nonnull.
Definition CGDecl.cpp:765
llvm::Value * EmitPointerAuthUnqualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType PointerType, Address StorageAddress, bool IsKnownNonNull)
void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init)
Address makeNaturalAddressForPointer(llvm::Value *Ptr, QualType T, CharUnits Alignment=CharUnits::Zero(), bool ForPointeeType=false, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
Construct an address with the natural alignment of T.
Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Load a pointer with type PtrTy stored at address Ptr.
Definition CGExpr.cpp:3080
RValue EmitLoadOfGlobalRegLValue(LValue LV)
Load of global named registers are always calls to intrinsics.
Definition CGExpr.cpp:2550
void EmitVTablePtrCheckForCast(QualType T, Address Derived, bool MayBeNull, CFITypeCheckKind TCK, SourceLocation Loc)
Derived is the presumed address of an object of type T after a cast.
Definition CGClass.cpp:2890
TypeCheckKind
Situations in which we might emit a check for the suitability of a pointer or glvalue.
@ TCK_DowncastPointer
Checking the operand of a static_cast to a derived pointer type.
@ TCK_DowncastReference
Checking the operand of a static_cast to a derived reference type.
@ TCK_MemberAccess
Checking the object expression in a non-static data member access.
@ TCK_Store
Checking the destination of a store. Must be suitably sized and aligned.
@ TCK_UpcastToVirtualBase
Checking the operand of a cast to a virtual base object.
@ TCK_MemberCall
Checking the 'this' pointer for a call to a non-static member function.
@ TCK_DynamicOperation
Checking the operand of a dynamic_cast or a typeid expression.
@ TCK_ReferenceBinding
Checking the bound value in a reference binding.
@ TCK_Upcast
Checking the operand of a cast to a base object.
LValue EmitBinaryOperatorLValue(const BinaryOperator *E)
Definition CGExpr.cpp:6176
bool InNoMergeAttributedStmt
True if the current statement has nomerge attribute.
LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E)
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition CGDecl.cpp:2251
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
void maybeAttachRangeForLoad(llvm::LoadInst *Load, QualType Ty, SourceLocation Loc)
Definition CGExpr.cpp:2049
void EmitBitfieldConversionCheck(llvm::Value *Src, QualType SrcType, llvm::Value *Dst, QualType DstType, const CGBitFieldInfo &Info, SourceLocation Loc)
Emit a check that an [implicit] conversion of a bitfield.
LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e)
Definition CGExpr.cpp:6835
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:3579
void EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound, llvm::Value *Index, QualType IndexType, QualType IndexedType, bool Accessed)
Definition CGExpr.cpp:1252
LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e)
Definition CGExpr.cpp:5874
llvm::Value * LoadPassedObjectSize(const Expr *E, QualType EltTy)
If E references a parameter with pass_object_size info or a constant array size modifier,...
Definition CGExpr.cpp:965
@ ForceLeftToRight
! Language semantics require left-to-right evaluation.
@ Default
! No language constraints on evaluation order.
@ ForceRightToLeft
! Language semantics require right-to-left evaluation.
llvm::Value * EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)
Definition CGExpr.cpp:6379
RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke)
RValue EmitLoadOfAnyValue(LValue V, AggValueSlot Slot=AggValueSlot::ignored(), SourceLocation Loc={})
Like EmitLoadOfLValue but also handles complex and aggregate types.
Definition CGExpr.cpp:2359
LValue EmitLValueForField(LValue Base, const FieldDecl *Field, bool IsInBounds=true)
Definition CGExpr.cpp:5293
RawAddress 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:176
const TargetInfo & getTarget() const
LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E)
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, bool IsInBounds, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Emit the address of a field using a member data pointer.
Definition CGClass.cpp:150
LValue EmitHLSLOutArgExpr(const HLSLOutArgExpr *E, CallArgList &Args, QualType Ty)
Definition CGExpr.cpp:5897
static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty)
Determine whether the pointer type check TCK requires a vptr check.
Definition CGExpr.cpp:728
CGCallee EmitCallee(const Expr *E)
Definition CGExpr.cpp:6101
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
Definition CGExpr.cpp:244
RValue EmitCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue=ReturnValueSlot(), llvm::CallBase **CallOrInvoke=nullptr)
Definition CGExpr.cpp:5975
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition CGExpr.cpp:2377
LValue EmitArraySectionExpr(const ArraySectionExpr *E, bool IsLowerBound=true)
Definition CGExpr.cpp:4876
Address GetAddrOfBlockDecl(const VarDecl *var)
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type,...
void pushCleanupAfterFullExpr(CleanupKind Kind, As... A)
Queue a cleanup to be pushed after finishing the current full-expression, potentially with an active ...
void EmitCfiCheckStub()
Emit a stub for the cross-DSO CFI check function.
Definition CGExpr.cpp:4021
void pushFullExprCleanup(CleanupKind kind, As... A)
pushFullExprCleanup - Push a cleanup to be run at the end of the current full-expression.
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
LValue EmitAggExprToLValue(const Expr *E)
EmitAggExprToLValue - Emit the computation of the specified expression of aggregate type into a tempo...
void SetFPAccuracy(llvm::Value *Val, float Accuracy)
SetFPAccuracy - Set the minimum required accuracy of the given floating point operation,...
Definition CGExpr.cpp:6698
Address mergeAddressesInConditionalExpr(Address LHS, Address RHS, llvm::BasicBlock *LHSBlock, llvm::BasicBlock *RHSBlock, llvm::BasicBlock *MergeBlock, QualType MergedType)
Address emitAddrOfImagComponent(Address complex, QualType complexType)
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Definition CGExpr.cpp:225
LValue EmitPredefinedLValue(const PredefinedExpr *E)
Definition CGExpr.cpp:3542
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs, const TrapReason *TR=nullptr)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition CGExpr.cpp:3829
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition CGExpr.cpp:3246
LValue EmitStringLiteralLValue(const StringLiteral *E)
Definition CGExpr.cpp:3532
AggValueSlot CreateAggTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateAggTemp - Create a temporary memory object for the given aggregate type.
RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its RValue mapping if it exists, otherwise create one.
Definition CGExpr.cpp:5928
RValue EmitAtomicLoad(LValue LV, SourceLocation SL, AggValueSlot Slot=AggValueSlot::ignored())
llvm::Value * emitScalarConstant(const ConstantEmission &Constant, Expr *E)
Definition CGExpr.cpp:2004
llvm::Value * getTypeSize(QualType Ty)
Returns calculated size of the specified type.
bool EmitLifetimeStart(llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition CGDecl.cpp:1356
LValue EmitUnsupportedLValue(const Expr *E, const char *Name)
EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue an ErrorUnsupported style ...
Definition CGExpr.cpp:1601
LValue MakeRawAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, AlignmentSource Source=AlignmentSource::Type)
Same as MakeAddrLValue above except that the pointer is known to be unsigned.
llvm::MDNode * buildAllocToken(QualType AllocType)
Build metadata used by the AllocToken instrumentation.
Definition CGExpr.cpp:1277
RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke=nullptr)
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
EmitLValueForFieldInitialization - Like EmitLValueForField, except that if the Field is a reference,...
Definition CGExpr.cpp:5467
llvm::Value * EmitToMemory(llvm::Value *Value, QualType Ty)
EmitToMemory - Change a scalar value from its value representation to its in-memory representation.
Definition CGExpr.cpp:2190
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
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:153
LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its LValue mapping if it exists, otherwise create one.
Definition CGExpr.cpp:5914
bool EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, SourceLocation Loc)
Check if the scalar Value is within the valid range for the given type Ty.
Definition CGExpr.cpp:2063
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5233
const TargetCodeGenInfo & getTargetHooks() const
llvm::ConstantInt * getUBSanFunctionTypeHash(QualType T) const
Return a type hash constant for a function instrumented by -fsanitize=function.
LValue EmitHLSLArrayAssignLValue(const BinaryOperator *E)
Definition CGExpr.cpp:6291
RawAddress CreateMemTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen without...
Definition CGExpr.cpp:217
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
LValue EmitVAArgExprLValue(const VAArgExpr *E)
Definition CGExpr.cpp:6316
bool IsInPreservedAIRegion
True if CodeGen currently emits code inside presereved access index region.
RValue EmitAnyExprToTemp(const Expr *E)
EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will always be accessible even if...
Definition CGExpr.cpp:285
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
LValue EmitStmtExprLValue(const StmtExpr *E)
Definition CGExpr.cpp:6419
llvm::Value * EmitARCLoadWeakRetained(Address addr)
i8* @objc_loadWeakRetained(i8** addr)
Definition CGObjC.cpp:2643
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
RawAddress CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates a alloca and inserts it into the entry block.
Definition CGExpr.cpp:105
llvm::Value * EmitWithOriginalRHSBitfieldAssignment(const BinaryOperator *E, llvm::Value **Previous, QualType *SrcType)
Retrieve the implicit cast expression of the rhs in a binary operator expression by passing pointers ...
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E)
Definition CGExpr.cpp:6395
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:2574
Address EmitArrayToPointerDecay(const Expr *Array, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Definition CGExpr.cpp:4269
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition CGDecl.cpp:2331
RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
RValue GetUndefRValue(QualType Ty)
GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
Definition CGExpr.cpp:1569
llvm::Instruction * getPostAllocaInsertPoint()
Return PostAllocaInsertPt.
void EmitAllocToken(llvm::CallBase *CB, QualType AllocType)
Emit and set additional metadata used by the AllocToken instrumentation.
Definition CGExpr.cpp:1291
LValue EmitComplexAssignmentLValue(const BinaryOperator *E)
Emit an l-value for an assignment (simple or compound) of complex type.
LValue EmitCastLValue(const CastExpr *E)
EmitCastLValue - Casts are never lvalues unless that cast is to a reference type.
Definition CGExpr.cpp:5685
llvm::Value * EmitPointerArithmetic(const BinaryOperator *BO, Expr *pointerOperand, llvm::Value *pointer, Expr *indexOperand, llvm::Value *index, bool isSubtraction)
Emit pointer + index arithmetic.
LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
Definition CGExpr.cpp:510
LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy)
Definition CGExpr.cpp:3090
llvm::Value * EmitCheckValue(llvm::Value *V)
Convert a value into a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:3651
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:295
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:266
LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E)
Definition CGExpr.cpp:5040
llvm::DenseMap< const ValueDecl *, FieldDecl * > LambdaCaptureFields
RValue EmitUnsupportedRValue(const Expr *E, const char *Name)
EmitUnsupportedRValue - Emit a dummy r-value using the type of E and issue an ErrorUnsupported style ...
Definition CGExpr.cpp:1595
CleanupKind getCleanupKind(QualType::DestructionKind kind)
std::pair< LValue, LValue > EmitHLSLOutArgLValues(const HLSLOutArgExpr *E, QualType Ty)
Definition CGExpr.cpp:5880
LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E)
Definition CGExpr.cpp:6367
llvm::Type * ConvertTypeForMem(QualType T)
LValue EmitCallExprLValue(const CallExpr *E, llvm::CallBase **CallOrInvoke=nullptr)
Definition CGExpr.cpp:6301
RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc)
Definition CGExpr.cpp:2448
llvm::Value * EmitARCLoadWeak(Address addr)
i8* @objc_loadWeak(i8** addr) Essentially objc_autorelease(objc_loadWeakRetained(addr)).
Definition CGObjC.cpp:2636
LValue EmitLValueForLambdaField(const FieldDecl *Field)
Definition CGExpr.cpp:5205
void markStmtMaybeUsed(const Stmt *S)
CodeGenTypes & getTypes() const
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
llvm::Value * EmitIvarOffset(const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)
Definition CGExpr.cpp:6373
bool IsSanitizerScope
True if CodeGen currently emits code implementing sanitizer checks.
void FlattenAccessAndTypeLValue(LValue LVal, SmallVectorImpl< LValue > &AccessList)
Definition CGExpr.cpp:6839
LValue EmitCoyieldLValue(const CoyieldExpr *E)
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, LValue LV, QualType Type, SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
void EmitCfiSlowPathCheck(SanitizerKind::SanitizerOrdinal Ordinal, llvm::Value *Cond, llvm::ConstantInt *TypeId, llvm::Value *Ptr, ArrayRef< llvm::Constant * > StaticArgs)
Emit a slow path cross-DSO CFI check which calls __cfi_slowpath if Cond if false.
Definition CGExpr.cpp:3973
llvm::SmallVector< const ParmVarDecl *, 4 > FnArgs
Save Parameter Decl for coroutine.
void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, Address Ptr)
Emits all the code to cause the given temporary to be cleaned up.
llvm::Value * authPointerToPointerCast(llvm::Value *ResultPtr, QualType SourceType, QualType DestType)
LValue EmitUnaryOpLValue(const UnaryOperator *E)
Definition CGExpr.cpp:3465
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition CGExpr.cpp:1552
bool LValueIsSuitableForInlineAtomic(LValue Src)
An LValue is a candidate for having its loads and stores be made atomic if we are operating under /vo...
LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK)
Same as EmitLValue but additionally we generate checking code to guard against undefined behavior.
Definition CGExpr.cpp:1633
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:188
Address EmitLoadOfReference(LValue RefLVal, LValueBaseInfo *PointeeBaseInfo=nullptr, TBAAAccessInfo *PointeeTBAAInfo=nullptr)
Definition CGExpr.cpp:3038
RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc)
Definition CGExpr.cpp:5947
llvm::Value * EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr)
Definition CGObjC.cpp:2160
LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E)
Definition CGExpr.cpp:6345
llvm::Type * convertTypeForLoadStore(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled.
Definition CGExpr.cpp:736
Address EmitExtVectorElementLValue(LValue V)
Generates lvalue for partial ext_vector access.
Definition CGExpr.cpp:2532
llvm::Value * EmitCheckedInBoundsGEP(llvm::Type *ElemTy, 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...
void EmitInitializationToLValue(const Expr *E, LValue LV, AggValueSlot::IsZeroed_t IsZeroed=AggValueSlot::IsNotZeroed)
EmitInitializationToLValue - Emit an initializer to an LValue.
Definition CGExpr.cpp:325
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
Address emitAddrOfRealComponent(Address complex, QualType complexType)
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
RValue EmitLoadOfExtVectorElementLValue(LValue V)
Definition CGExpr.cpp:2485
static bool hasAggregateEvaluationKind(QualType T)
static bool IsWrappedCXXThis(const Expr *E)
Check if E is a C++ "this" pointer wrapped in value-preserving casts.
Definition CGExpr.cpp:1610
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition CGCall.cpp:4673
llvm::Value * EmitMatrixIndexExpr(const Expr *E)
Definition CGExpr.cpp:4809
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID)
Emit a call to trap or debugtrap and attach function attribute "trap-func-name" if specified.
Definition CGExpr.cpp:4254
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID, bool NoMerge=false, const TrapReason *TR=nullptr)
Create a basic block that will call the trap intrinsic, and emit a conditional branch to it,...
Definition CGExpr.cpp:4181
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit)
llvm::Value * EmitFromMemory(llvm::Value *Value, QualType Ty)
EmitFromMemory - Change a scalar value from its memory representation to its value representation.
Definition CGExpr.cpp:2220
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
llvm::Value * EmitLoadOfCountedByField(const Expr *Base, const FieldDecl *FD, const FieldDecl *CountDecl)
Build an expression accessing the "counted_by" field.
Definition CGExpr.cpp:1230
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
void EmitUnreachable(SourceLocation Loc)
Emit a reached-unreachable diagnostic if Loc is valid and runtime checking is enabled.
Definition CGExpr.cpp:4169
bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, bool AllowLabels=false)
ConstantFoldsToSimpleInteger - If the specified expression does not fold to a constant,...
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E)
Definition CGExpr.cpp:6330
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.
LValue EmitMemberExpr(const MemberExpr *E)
Definition CGExpr.cpp:5108
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
ConstantEmission tryEmitAsConstant(const DeclRefExpr *RefExpr)
Try to emit a reference to the given value without producing it as an l-value.
Definition CGExpr.cpp:1901
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition CGExpr.cpp:1668
void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst)
Store of global named registers are always calls to intrinsics.
Definition CGExpr.cpp:2880
bool isOpaqueValueEmitted(const OpaqueValueExpr *E)
isOpaqueValueEmitted - Return true if the opaque value expression has already been emitted.
Definition CGExpr.cpp:5941
std::pair< llvm::Value *, CGPointerAuthInfo > EmitOrigPointerRValue(const Expr *E)
Retrieve a pointer rvalue and its ptrauth info.
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2651
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
llvm::LLVMContext & getLLVMContext()
RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke)
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:708
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...
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
static bool ShouldNullCheckClassCastValue(const CastExpr *Cast)
llvm::Value * EmitNonNullRValueCheck(RValue RV, QualType T)
Create a check that a scalar RValue is non-null.
Definition CGExpr.cpp:1562
void EmitCountedByBoundsChecking(const Expr *E, llvm::Value *Idx, Address Addr, QualType IdxTy, QualType ArrayTy, bool Accessed, bool FlexibleArray)
EmitCountedByBoundsChecking - If the array being accessed has a "counted_by" attribute,...
Definition CGExpr.cpp:4556
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
llvm::Value * EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE)
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:655
LValue MakeNaturalAlignRawAddrLValue(llvm::Value *V, QualType T)
QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args)
llvm::Value * EmitCXXTypeidExpr(const CXXTypeidExpr *E)
This class organizes the cross-function state that is used while generating LLVM code.
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition CGExpr.cpp:1348
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
llvm::Constant * getRawFunctionPointer(GlobalDecl GD, llvm::Type *Ty=nullptr)
Return a function pointer for a reference to the given function.
Definition CGExpr.cpp:3137
llvm::FunctionCallee getAddrAndTypeOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Definition CGCXX.cpp:217
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.
llvm::Constant * getFunctionPointer(GlobalDecl GD, llvm::Type *Ty=nullptr)
Return the ABI-correct function pointer value for a reference to the given function.
const LangOptions & getLangOpts() const
CGCUDARuntime & getCUDARuntime()
Return a reference to the configured CUDA runtime.
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
CGPointerAuthInfo getPointerAuthInfoForPointeeType(QualType type)
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD)
Returns LLVM linkage for a declarator.
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type.
ASTContext & getContext() const
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purposes of type casts.
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.
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
CharUnits getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
llvm::LLVMContext & getLLVMContext()
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
A specialization of Address that requires the address to be an LLVM Constant.
Definition Address.h:296
llvm::Constant * getPointer() const
Definition Address.h:308
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:375
AlignmentSource getAlignmentSource() const
Definition CGValue.h:171
LValue - This represents an lvalue references.
Definition CGValue.h:182
bool isBitField() const
Definition CGValue.h:280
bool isMatrixElt() const
Definition CGValue.h:283
Expr * getBaseIvarExp() const
Definition CGValue.h:332
llvm::Constant * getExtVectorElts() const
Definition CGValue.h:409
static LValue MakeGlobalReg(llvm::Value *V, CharUnits alignment, QualType type)
Definition CGValue.h:478
bool isObjCStrong() const
Definition CGValue.h:324
bool isGlobalObjCRef() const
Definition CGValue.h:306
bool isVectorElt() const
Definition CGValue.h:279
bool isSimple() const
Definition CGValue.h:278
bool isVolatileQualified() const
Definition CGValue.h:285
RValue asAggregateRValue() const
Definition CGValue.h:498
llvm::Value * getPointer(CodeGenFunction &CGF) const
llvm::Value * getMatrixIdx() const
Definition CGValue.h:395
llvm::Value * getGlobalReg() const
Definition CGValue.h:430
static LValue MakeAddr(Address Addr, QualType type, ASTContext &Context, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition CGValue.h:432
bool isVolatile() const
Definition CGValue.h:328
const Qualifiers & getQuals() const
Definition CGValue.h:338
bool isGlobalReg() const
Definition CGValue.h:282
static LValue MakeExtVectorElt(Address Addr, llvm::Constant *Elts, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition CGValue.h:452
bool isObjCWeak() const
Definition CGValue.h:321
Address getAddress() const
Definition CGValue.h:361
unsigned getVRQualifiers() const
Definition CGValue.h:287
LValue setKnownNonNull()
Definition CGValue.h:350
bool isNonGC() const
Definition CGValue.h:303
bool isExtVectorElt() const
Definition CGValue.h:281
llvm::Value * getVectorIdx() const
Definition CGValue.h:382
void setNontemporal(bool Value)
Definition CGValue.h:319
LValueBaseInfo getBaseInfo() const
Definition CGValue.h:346
void setARCPreciseLifetime(ARCPreciseLifetime_t value)
Definition CGValue.h:315
QualType getType() const
Definition CGValue.h:291
const CGBitFieldInfo & getBitFieldInfo() const
Definition CGValue.h:424
bool isThreadLocalRef() const
Definition CGValue.h:309
KnownNonNull_t isKnownNonNull() const
Definition CGValue.h:349
TBAAAccessInfo getTBAAInfo() const
Definition CGValue.h:335
void setNonGC(bool Value)
Definition CGValue.h:304
Address getVectorAddress() const
Definition CGValue.h:370
bool isNontemporal() const
Definition CGValue.h:318
static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Create a new object to represent a bit-field access.
Definition CGValue.h:468
bool isObjCIvar() const
Definition CGValue.h:297
static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition CGValue.h:442
void setAddress(Address address)
Definition CGValue.h:363
Address getExtVectorAddress() const
Definition CGValue.h:401
static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition CGValue.h:488
Address getMatrixAddress() const
Definition CGValue.h:387
Address getBitFieldAddress() const
Definition CGValue.h:415
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
bool isScalar() const
Definition CGValue.h:64
static RValue get(llvm::Value *V)
Definition CGValue.h:98
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
Definition CGValue.h:125
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition CGValue.h:108
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition CGValue.h:83
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:71
An abstract representation of an aligned address.
Definition Address.h:42
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition Address.h:93
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:77
llvm::Value * getPointer() const
Definition Address.h:66
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition Address.h:83
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition CGCall.h:379
Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, LangAS SrcAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3275
QualType getElementType() const
Definition TypeBase.h:3285
CompoundLiteralExpr - [C99 6.5.2.5].
Definition Expr.h:3539
bool isFileScope() const
Definition Expr.h:3571
const Expr * getInitializer() const
Definition Expr.h:3567
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4373
unsigned getNumRows() const
Returns the number of rows in the matrix.
Definition TypeBase.h:4389
RecordDecl * getOuterLexicalRecordContext()
Retrieve the outermost lexically enclosing record context.
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Definition Expr.h:1474
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
Definition Expr.cpp:484
ValueDecl * getDecl()
Definition Expr.h:1338
NonOdrUseReason isNonOdrUse() const
Is this expression a non-odr-use reference, and if so, why?
Definition Expr.h:1468
SourceLocation getLocation() const
Definition Expr.h:1346
T * getAttr() const
Definition DeclBase.h:573
SourceLocation getLocation() const
Definition DeclBase.h:439
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition DeclBase.cpp:575
DeclContext * getDeclContext()
Definition DeclBase.h:448
bool hasAttr() const
Definition DeclBase.h:577
ExplicitCastExpr - An explicit cast written in the source code.
Definition Expr.h:3862
This represents one expression.
Definition Expr.h:112
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:80
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParenNoopCasts(const ASTContext &Ctx) LLVM_READONLY
Skip past any parentheses and casts which do not change the value (including ptr->int casts of the sa...
Definition Expr.cpp:3112
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition Expr.h:444
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3085
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3073
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3081
bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsLValue - Evaluate an expression to see if we can fold it to an lvalue with link time known ...
bool isPRValue() const
Definition Expr.h:285
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
Decl * getReferencedDeclOfCallee()
Definition Expr.cpp:1542
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3665
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3065
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
bool refersToBitField() const
Returns true if this expression is a gl-value that potentially refers to a bit-field.
Definition Expr.h:476
QualType getType() const
Definition Expr.h:144
bool isOBJCGCCandidate(ASTContext &Ctx) const
isOBJCGCCandidate - Return true if this expression may be used in a read/ write barrier.
Definition Expr.cpp:2996
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition Expr.h:6498
bool isArrow() const
isArrow - Return true if the base expression is a pointer to vector, return false if the base express...
Definition Expr.cpp:4410
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
Definition Expr.cpp:4442
const Expr * getBase() const
Definition Expr.h:6515
Represents a member of a struct/union/class.
Definition Decl.h:3160
bool isBitField() const
Determines whether this field is a bitfield.
Definition Decl.h:3263
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition Decl.h:3245
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3396
const FieldDecl * findCountedByField() const
Find the FieldDecl specified in a FAM's "counted_by" attribute.
Definition Decl.cpp:4844
FullExpr - Represents a "full-expression" node.
Definition Expr.h:1049
const Expr * getSubExpr() const
Definition Expr.h:1062
Represents a function declaration or definition.
Definition Decl.h:2000
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3751
FunctionDecl * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5254
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4450
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
const Decl * getDecl() const
Definition GlobalDecl.h:106
This class represents temporary values used to represent inout and out arguments in HLSL.
Definition Expr.h:7283
const OpaqueValueExpr * getCastedTemporary() const
Definition Expr.h:7334
const OpaqueValueExpr * getOpaqueArgLValue() const
Definition Expr.h:7315
bool isInOut() const
returns true if the parameter is inout and false if the parameter is out.
Definition Expr.h:7342
const Expr * getWritebackCast() const
Definition Expr.h:7329
const Expr * getArgLValue() const
Return the l-value expression that was written as the argument in source.
Definition Expr.h:7324
Describes an C or C++ initializer list.
Definition Expr.h:5233
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
Definition Expr.cpp:2457
const Expr * getInit(unsigned Init) const
Definition Expr.h:5287
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4922
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4947
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4939
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:4972
MatrixSubscriptExpr - Matrix subscript expression for the MatrixType extension.
Definition Expr.h:2799
bool isIncomplete() const
Definition Expr.h:2819
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3298
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3381
NonOdrUseReason isNonOdrUse() const
Is this expression a non-odr-use reference, and if so, why?
Definition Expr.h:3522
Expr * getBase() const
Definition Expr.h:3375
bool isArrow() const
Definition Expr.h:3482
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:3493
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3653
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition NSAPI.cpp:481
This represents a decl that may have a name.
Definition Decl.h:274
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
A C++ nested-name-specifier augmented with source location information.
ObjCEncodeExpr, used for @encode in Objective-C.
Definition ExprObjC.h:409
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition ExprObjC.h:548
ObjCIvarDecl * getDecl()
Definition ExprObjC.h:578
bool isArrow() const
Definition ExprObjC.h:586
const Expr * getBase() const
Definition ExprObjC.h:582
An expression that sends a message to the given Objective-C object or class.
Definition ExprObjC.h:940
const ObjCMethodDecl * getMethodDecl() const
Definition ExprObjC.h:1364
QualType getReturnType() const
Definition DeclObjC.h:329
ObjCSelectorExpr used for @selector in Objective-C.
Definition ExprObjC.h:454
Selector getSelector() const
Definition ExprObjC.h:468
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition Expr.h:1178
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition Expr.h:1228
bool isUnique() const
Definition Expr.h:1236
const Expr * getSubExpr() const
Definition Expr.h:2199
Pointer-authentication qualifiers.
Definition TypeBase.h:152
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3328
QualType getPointeeType() const
Definition TypeBase.h:3338
[C99 6.4.2.2] - A predefined identifier such as func.
Definition Expr.h:2005
StringRef getIdentKindName() const
Definition Expr.h:2062
PredefinedIdentKind getIdentKind() const
Definition Expr.h:2040
StringLiteral * getFunctionName()
Definition Expr.h:2049
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition Expr.h:6690
semantics_iterator semantics_end()
Definition Expr.h:6755
semantics_iterator semantics_begin()
Definition Expr.h:6751
const Expr *const * const_semantics_iterator
Definition Expr.h:6750
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6738
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8362
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:1453
QualType withoutLocalFastQualifiers() const
Definition TypeBase.h:1214
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8404
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8318
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition TypeBase.h:1438
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8463
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8372
QualType withCVRQualifiers(unsigned CVR) const
Definition TypeBase.h:1179
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition TypeBase.h:1545
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1036
The collection of all-type qualifiers we support.
Definition TypeBase.h:331
unsigned getCVRQualifiers() const
Definition TypeBase.h:488
GC getObjCGCAttr() const
Definition TypeBase.h:519
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition TypeBase.h:361
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition TypeBase.h:354
@ OCL_None
There is no lifetime qualification on this type.
Definition TypeBase.h:350
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition TypeBase.h:364
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition TypeBase.h:367
bool hasConst() const
Definition TypeBase.h:457
void addCVRQualifiers(unsigned mask)
Definition TypeBase.h:502
void removeObjCGCAttr()
Definition TypeBase.h:523
void addQualifiers(Qualifiers Q)
Add the qualifiers from the given set to this set.
Definition TypeBase.h:650
void removePointerAuth()
Definition TypeBase.h:610
void setAddressSpace(LangAS space)
Definition TypeBase.h:591
bool hasVolatile() const
Definition TypeBase.h:467
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:603
ObjCLifetime getObjCLifetime() const
Definition TypeBase.h:545
Represents a struct/union/class.
Definition Decl.h:4312
field_range fields() const
Definition Decl.h:4515
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition Decl.h:4496
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition Expr.h:4529
StmtClass getStmtClass() const
Definition Stmt.h:1472
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:350
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1799
bool isUnion() const
Definition Decl.h:3922
Exposes information about the current target.
Definition TargetInfo.h:226
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual StringRef getABI() const
Get the ABI currently in use.
The base class of the type hierarchy.
Definition TypeBase.h:1833
bool isBlockPointerType() const
Definition TypeBase.h:8535
bool isVoidType() const
Definition TypeBase.h:8871
bool hasPointeeToToCFIUncheckedCalleeFunctionType() const
Definition TypeBase.h:8567
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition Type.cpp:2225
bool isPackedVectorBoolType(const ASTContext &ctx) const
Definition Type.cpp:418
bool hasAttr(attr::Kind AK) const
Determine whether this type had the specified attribute applied to it (looking through top-level type...
Definition Type.cpp:1951
const ArrayType * castAsArrayTypeUnsafe() const
A variant of castAs<> for array type which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9167
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isConstantArrayType() const
Definition TypeBase.h:8618
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8614
bool isFunctionPointerType() const
Definition TypeBase.h:8582
CXXRecordDecl * castAsCXXRecordDecl() const
Definition Type.h:36
bool isArithmeticType() const
Definition Type.cpp:2337
bool isConstantMatrixType() const
Definition TypeBase.h:8676
bool isPointerType() const
Definition TypeBase.h:8515
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:8915
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9158
bool isReferenceType() const
Definition TypeBase.h:8539
bool isEnumeralType() const
Definition TypeBase.h:8646
bool isVariableArrayType() const
Definition TypeBase.h:8626
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:752
bool isExtVectorBoolType() const
Definition TypeBase.h:8662
bool isBitIntType() const
Definition TypeBase.h:8780
bool isAnyComplexType() const
Definition TypeBase.h:8650
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition TypeBase.h:9044
bool isAtomicType() const
Definition TypeBase.h:8697
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2800
bool isObjectType() const
Determine whether this type is an object type.
Definition TypeBase.h:2510
bool isHLSLResourceRecord() const
Definition Type.cpp:5362
EnumDecl * getAsEnumDecl() const
Retrieves the EnumDecl this type refers to.
Definition Type.h:53
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2435
bool isFunctionType() const
Definition TypeBase.h:8511
bool isObjCObjectPointerType() const
Definition TypeBase.h:8684
bool isVectorType() const
Definition TypeBase.h:8654
bool isAnyPointerType() const
Definition TypeBase.h:8523
bool isSubscriptableVectorType() const
Definition TypeBase.h:8668
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9091
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:653
bool isRecordType() const
Definition TypeBase.h:8642
bool isHLSLResourceRecordArray() const
Definition Type.cpp:5366
bool hasBooleanRepresentation() const
Determine whether this type has a boolean representation – i.e., it is a boolean type,...
Definition Type.cpp:2354
bool isCFIUncheckedCalleeFunctionType() const
Definition TypeBase.h:8561
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2244
SourceLocation getExprLoc() const
Definition Expr.h:2368
Expr * getSubExpr() const
Definition Expr.h:2285
Opcode getOpcode() const
Definition Expr.h:2280
Represents a call to the builtin function __builtin_va_arg.
Definition Expr.h:4891
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
QualType getType() const
Definition Value.cpp:237
Represents a variable declaration or definition.
Definition Decl.h:926
TLSKind getTLSKind() const
Definition Decl.cpp:2168
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition Decl.cpp:2366
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1184
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition Decl.h:952
@ TLS_None
Not a TLS variable.
Definition Decl.h:946
Represents a C array with a specified size that is not an integer-constant-expression.
Definition TypeBase.h:3966
Represents a GCC generic vector type.
Definition TypeBase.h:4175
unsigned getNumElements() const
Definition TypeBase.h:4190
#define INT_MIN
Definition limits.h:55
Definition SPIR.cpp:35
AlignmentSource
The source of the alignment of an l-value; an expression of confidence in the alignment actually matc...
Definition CGValue.h:141
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:154
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:145
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD)
isEmptyFieldForLayout - Return true iff the field is "empty", that is, either a zero-width bit-field ...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
@ ARCImpreciseLifetime
Definition CGValue.h:136
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:159
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
const AstTypeMatcher< FunctionType > functionType
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
std::optional< llvm::AllocTokenMetadata > getAllocTokenMetadata(QualType T, const ASTContext &Ctx)
Get the information required for construction of an allocation token ID.
QualType inferPossibleType(const CallExpr *E, const ASTContext &Ctx, const CastExpr *CastE)
Infer the possible allocated type from an allocation call expression.
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
@ OK_BitField
A bitfield object is a bitfield on a C or C++ record.
Definition Specifiers.h:154
@ SC_Register
Definition Specifiers.h:257
Expr * Cond
};
@ Asm
Assembly: we accept this only so that we can preprocess it.
StorageDuration
The storage duration for an object (per C++ [basic.stc]).
Definition Specifiers.h:339
@ SD_Thread
Thread storage duration.
Definition Specifiers.h:342
@ SD_Static
Static storage duration.
Definition Specifiers.h:343
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition Specifiers.h:340
@ SD_Automatic
Automatic storage duration (most local variables).
Definition Specifiers.h:341
@ SD_Dynamic
Dynamic storage duration.
Definition Specifiers.h:344
@ Result
The result type of a method or function.
Definition TypeBase.h:905
const FunctionProtoType * T
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
LangAS
Defines the address space values used by the address space qualifier of QualType.
llvm::cl::opt< bool > ClSanitizeGuardChecks
SmallVector< CXXBaseSpecifier *, 4 > CXXCastPath
A simple array of base specifiers.
Definition ASTContext.h:149
U cast(CodeGen::Address addr)
Definition Address.h:327
LangAS getLangASFromTargetAS(unsigned TargetAS)
@ Interface
The "__interface" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5858
bool isLambdaMethod(const DeclContext *DC)
Definition ASTLambda.h:39
@ Other
Other implicit parameter.
Definition Decl.h:1746
@ NOUR_Unevaluated
This name appears in an unevaluated operand.
Definition Specifiers.h:177
@ NOUR_Constant
This name appears as a potential result of an lvalue-to-rvalue conversion that is a constant expressi...
Definition Specifiers.h:180
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
Structure with information about how a bitfield should be accessed.
CharUnits VolatileStorageOffset
The offset of the bitfield storage from the start of the struct.
unsigned VolatileOffset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned Offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned VolatileStorageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned Size
The total size of the bit-field, in bits.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned IsSigned
Whether the bit-field is signed.
static Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD, Address VDAddr, SourceLocation Loc)
Returns address of the threadprivate variable for the current thread.
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
unsigned char PointerWidthInBits
The width of a pointer into the generic address space.
llvm::MDNode * AccessType
AccessType - The final access type.
uint64_t Offset
Offset - The byte offset of the final access within the base one.
static TBAAAccessInfo getMayAliasInfo()
Definition CodeGenTBAA.h:63
uint64_t Size
Size - The size of access, in bytes.
llvm::MDNode * BaseType
BaseType - The base/leading access type.
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:645
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:647
bool HasSideEffects
Whether the evaluated expression has side effects.
Definition Expr.h:612
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition Sanitizers.h:187
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition Sanitizers.h:174
An adjustment to be made to the temporary created when emitting a reference binding,...
Definition Expr.h:68