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