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