clang 19.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 "CGCUDARuntime.h"
14#include "CGCXXABI.h"
15#include "CGCall.h"
16#include "CGCleanup.h"
17#include "CGDebugInfo.h"
18#include "CGObjCRuntime.h"
19#include "CGOpenMPRuntime.h"
20#include "CGRecordLayout.h"
21#include "CodeGenFunction.h"
22#include "CodeGenModule.h"
23#include "ConstantEmitter.h"
24#include "TargetInfo.h"
26#include "clang/AST/Attr.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/NSAPI.h"
33#include "llvm/ADT/Hashing.h"
34#include "llvm/ADT/STLExtras.h"
35#include "llvm/ADT/StringExtras.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/Intrinsics.h"
38#include "llvm/IR/IntrinsicsWebAssembly.h"
39#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/MDBuilder.h"
41#include "llvm/IR/MatrixBuilder.h"
42#include "llvm/Passes/OptimizationLevel.h"
43#include "llvm/Support/ConvertUTF.h"
44#include "llvm/Support/MathExtras.h"
45#include "llvm/Support/Path.h"
46#include "llvm/Support/SaveAndRestore.h"
47#include "llvm/Support/xxhash.h"
48#include "llvm/Transforms/Utils/SanitizerStats.h"
49
50#include <optional>
51#include <string>
52
53using namespace clang;
54using namespace CodeGen;
55
56// Experiment to make sanitizers easier to debug
57static llvm::cl::opt<bool> ClSanitizeDebugDeoptimization(
58 "ubsan-unique-traps", llvm::cl::Optional,
59 llvm::cl::desc("Deoptimize traps for UBSAN so there is 1 trap per check"),
60 llvm::cl::init(false));
61
62//===--------------------------------------------------------------------===//
63// Miscellaneous Helper Methods
64//===--------------------------------------------------------------------===//
65
66/// CreateTempAlloca - This creates a alloca and inserts it into the entry
67/// block.
68Address CodeGenFunction::CreateTempAllocaWithoutCast(llvm::Type *Ty,
69 CharUnits Align,
70 const Twine &Name,
71 llvm::Value *ArraySize) {
72 auto Alloca = CreateTempAlloca(Ty, Name, ArraySize);
73 Alloca->setAlignment(Align.getAsAlign());
74 return Address(Alloca, Ty, Align, KnownNonNull);
75}
76
77/// CreateTempAlloca - This creates a alloca and inserts it into the entry
78/// block. The alloca is casted to default address space if necessary.
80 const Twine &Name,
81 llvm::Value *ArraySize,
82 Address *AllocaAddr) {
83 auto Alloca = CreateTempAllocaWithoutCast(Ty, Align, Name, ArraySize);
84 if (AllocaAddr)
85 *AllocaAddr = Alloca;
86 llvm::Value *V = Alloca.getPointer();
87 // Alloca always returns a pointer in alloca address space, which may
88 // be different from the type defined by the language. For example,
89 // in C++ the auto variables are in the default address space. Therefore
90 // cast alloca to the default address space when necessary.
92 auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default);
93 llvm::IRBuilderBase::InsertPointGuard IPG(Builder);
94 // When ArraySize is nullptr, alloca is inserted at AllocaInsertPt,
95 // otherwise alloca is inserted at the current insertion point of the
96 // builder.
97 if (!ArraySize)
98 Builder.SetInsertPoint(getPostAllocaInsertPoint());
101 Ty->getPointerTo(DestAddrSpace), /*non-null*/ true);
102 }
103
104 return Address(V, Ty, Align, KnownNonNull);
105}
106
107/// CreateTempAlloca - This creates an alloca and inserts it into the entry
108/// block if \p ArraySize is nullptr, otherwise inserts it at the current
109/// insertion point of the builder.
110llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
111 const Twine &Name,
112 llvm::Value *ArraySize) {
113 if (ArraySize)
114 return Builder.CreateAlloca(Ty, ArraySize, Name);
115 return new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
116 ArraySize, Name, AllocaInsertPt);
117}
118
119/// CreateDefaultAlignTempAlloca - This creates an alloca with the
120/// default alignment of the corresponding LLVM type, which is *not*
121/// guaranteed to be related in any way to the expected alignment of
122/// an AST type that might have been lowered to Ty.
124 const Twine &Name) {
125 CharUnits Align =
126 CharUnits::fromQuantity(CGM.getDataLayout().getPrefTypeAlign(Ty));
127 return CreateTempAlloca(Ty, Align, Name);
128}
129
130Address CodeGenFunction::CreateIRTemp(QualType Ty, const Twine &Name) {
132 return CreateTempAlloca(ConvertType(Ty), Align, Name);
133}
134
136 Address *Alloca) {
137 // FIXME: Should we prefer the preferred type alignment here?
138 return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name, Alloca);
139}
140
142 const Twine &Name, Address *Alloca) {
144 /*ArraySize=*/nullptr, Alloca);
145
146 if (Ty->isConstantMatrixType()) {
147 auto *ArrayTy = cast<llvm::ArrayType>(Result.getElementType());
148 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
149 ArrayTy->getNumElements());
150
151 Result = Address(Result.getPointer(), VectorTy, Result.getAlignment(),
153 }
154 return Result;
155}
156
158 const Twine &Name) {
159 return CreateTempAllocaWithoutCast(ConvertTypeForMem(Ty), Align, Name);
160}
161
163 const Twine &Name) {
164 return CreateMemTempWithoutCast(Ty, getContext().getTypeAlignInChars(Ty),
165 Name);
166}
167
168/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
169/// expression and compare the result against zero, returning an Int1Ty value.
170llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
171 PGO.setCurrentStmt(E);
172 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
173 llvm::Value *MemPtr = EmitScalarExpr(E);
174 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
175 }
176
177 QualType BoolTy = getContext().BoolTy;
178 SourceLocation Loc = E->getExprLoc();
179 CGFPOptionsRAII FPOptsRAII(*this, E);
180 if (!E->getType()->isAnyComplexType())
181 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc);
182
184 Loc);
185}
186
187/// EmitIgnoredExpr - Emit code to compute the specified expression,
188/// ignoring the result.
190 if (E->isPRValue())
191 return (void)EmitAnyExpr(E, AggValueSlot::ignored(), true);
192
193 // if this is a bitfield-resulting conditional operator, we can special case
194 // emit this. The normal 'EmitLValue' version of this is particularly
195 // difficult to codegen for, since creating a single "LValue" for two
196 // different sized arguments here is not particularly doable.
197 if (const auto *CondOp = dyn_cast<AbstractConditionalOperator>(
199 if (CondOp->getObjectKind() == OK_BitField)
200 return EmitIgnoredConditionalOperator(CondOp);
201 }
202
203 // Just emit it as an l-value and drop the result.
204 EmitLValue(E);
205}
206
207/// EmitAnyExpr - Emit code to compute the specified expression which
208/// can have any type. The result is returned as an RValue struct.
209/// If this is an aggregate expression, AggSlot indicates where the
210/// result should be returned.
212 AggValueSlot aggSlot,
213 bool ignoreResult) {
214 switch (getEvaluationKind(E->getType())) {
215 case TEK_Scalar:
216 return RValue::get(EmitScalarExpr(E, ignoreResult));
217 case TEK_Complex:
218 return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
219 case TEK_Aggregate:
220 if (!ignoreResult && aggSlot.isIgnored())
221 aggSlot = CreateAggTemp(E->getType(), "agg-temp");
222 EmitAggExpr(E, aggSlot);
223 return aggSlot.asRValue();
224 }
225 llvm_unreachable("bad evaluation kind");
226}
227
228/// EmitAnyExprToTemp - Similar to EmitAnyExpr(), however, the result will
229/// always be accessible even if no aggregate location is provided.
232
234 AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
235 return EmitAnyExpr(E, AggSlot);
236}
237
238/// EmitAnyExprToMem - Evaluate an expression into a given memory
239/// location.
241 Address Location,
242 Qualifiers Quals,
243 bool IsInit) {
244 // FIXME: This function should take an LValue as an argument.
245 switch (getEvaluationKind(E->getType())) {
246 case TEK_Complex:
248 /*isInit*/ false);
249 return;
250
251 case TEK_Aggregate: {
252 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals,
257 return;
258 }
259
260 case TEK_Scalar: {
261 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
262 LValue LV = MakeAddrLValue(Location, E->getType());
264 return;
265 }
266 }
267 llvm_unreachable("bad evaluation kind");
268}
269
270static void
272 const Expr *E, Address ReferenceTemporary) {
273 // Objective-C++ ARC:
274 // If we are binding a reference to a temporary that has ownership, we
275 // need to perform retain/release operations on the temporary.
276 //
277 // FIXME: This should be looking at E, not M.
278 if (auto Lifetime = M->getType().getObjCLifetime()) {
279 switch (Lifetime) {
282 // Carry on to normal cleanup handling.
283 break;
284
286 // Nothing to do; cleaned up by an autorelease pool.
287 return;
288
291 switch (StorageDuration Duration = M->getStorageDuration()) {
292 case SD_Static:
293 // Note: we intentionally do not register a cleanup to release
294 // the object on program termination.
295 return;
296
297 case SD_Thread:
298 // FIXME: We should probably register a cleanup in this case.
299 return;
300
301 case SD_Automatic:
305 if (Lifetime == Qualifiers::OCL_Strong) {
306 const ValueDecl *VD = M->getExtendingDecl();
307 bool Precise =
308 VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
312 } else {
313 // __weak objects always get EH cleanups; otherwise, exceptions
314 // could cause really nasty crashes instead of mere leaks.
317 }
318 if (Duration == SD_FullExpression)
319 CGF.pushDestroy(CleanupKind, ReferenceTemporary,
320 M->getType(), *Destroy,
322 else
323 CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
324 M->getType(),
325 *Destroy, CleanupKind & EHCleanup);
326 return;
327
328 case SD_Dynamic:
329 llvm_unreachable("temporary cannot have dynamic storage duration");
330 }
331 llvm_unreachable("unknown storage duration");
332 }
333 }
334
335 CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
336 if (const RecordType *RT =
338 // Get the destructor for the reference temporary.
339 auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
340 if (!ClassDecl->hasTrivialDestructor())
341 ReferenceTemporaryDtor = ClassDecl->getDestructor();
342 }
343
344 if (!ReferenceTemporaryDtor)
345 return;
346
347 // Call the destructor for the temporary.
348 switch (M->getStorageDuration()) {
349 case SD_Static:
350 case SD_Thread: {
351 llvm::FunctionCallee CleanupFn;
352 llvm::Constant *CleanupArg;
353 if (E->getType()->isArrayType()) {
355 ReferenceTemporary, E->getType(),
357 dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
358 CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
359 } else {
360 CleanupFn = CGF.CGM.getAddrAndTypeOfCXXStructor(
361 GlobalDecl(ReferenceTemporaryDtor, Dtor_Complete));
362 CleanupArg = cast<llvm::Constant>(ReferenceTemporary.getPointer());
363 }
365 CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
366 break;
367 }
368
370 CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
372 CGF.getLangOpts().Exceptions);
373 break;
374
375 case SD_Automatic:
377 ReferenceTemporary, E->getType(),
379 CGF.getLangOpts().Exceptions);
380 break;
381
382 case SD_Dynamic:
383 llvm_unreachable("temporary cannot have dynamic storage duration");
384 }
385}
386
389 const Expr *Inner,
390 Address *Alloca = nullptr) {
391 auto &TCG = CGF.getTargetHooks();
392 switch (M->getStorageDuration()) {
394 case SD_Automatic: {
395 // If we have a constant temporary array or record try to promote it into a
396 // constant global under the same rules a normal constant would've been
397 // promoted. This is easier on the optimizer and generally emits fewer
398 // instructions.
399 QualType Ty = Inner->getType();
400 if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
401 (Ty->isArrayType() || Ty->isRecordType()) &&
402 Ty.isConstantStorage(CGF.getContext(), true, false))
403 if (auto Init = ConstantEmitter(CGF).tryEmitAbstract(Inner, Ty)) {
404 auto AS = CGF.CGM.GetGlobalConstantAddressSpace();
405 auto *GV = new llvm::GlobalVariable(
406 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
407 llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp", nullptr,
408 llvm::GlobalValue::NotThreadLocal,
410 CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty);
411 GV->setAlignment(alignment.getAsAlign());
412 llvm::Constant *C = GV;
413 if (AS != LangAS::Default)
414 C = TCG.performAddrSpaceCast(
415 CGF.CGM, GV, AS, LangAS::Default,
416 GV->getValueType()->getPointerTo(
418 // FIXME: Should we put the new global into a COMDAT?
419 return Address(C, GV->getValueType(), alignment);
420 }
421 return CGF.CreateMemTemp(Ty, "ref.tmp", Alloca);
422 }
423 case SD_Thread:
424 case SD_Static:
425 return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
426
427 case SD_Dynamic:
428 llvm_unreachable("temporary can't have dynamic storage duration");
429 }
430 llvm_unreachable("unknown storage duration");
431}
432
433/// Helper method to check if the underlying ABI is AAPCS
434static bool isAAPCS(const TargetInfo &TargetInfo) {
435 return TargetInfo.getABI().starts_with("aapcs");
436}
437
440 const Expr *E = M->getSubExpr();
441
442 assert((!M->getExtendingDecl() || !isa<VarDecl>(M->getExtendingDecl()) ||
443 !cast<VarDecl>(M->getExtendingDecl())->isARCPseudoStrong()) &&
444 "Reference should never be pseudo-strong!");
445
446 // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so
447 // as that will cause the lifetime adjustment to be lost for ARC
448 auto ownership = M->getType().getObjCLifetime();
449 if (ownership != Qualifiers::OCL_None &&
450 ownership != Qualifiers::OCL_ExplicitNone) {
452 if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) {
453 llvm::Type *Ty = ConvertTypeForMem(E->getType());
454 Object = Object.withElementType(Ty);
455
456 // createReferenceTemporary will promote the temporary to a global with a
457 // constant initializer if it can. It can only do this to a value of
458 // ARC-manageable type if the value is global and therefore "immune" to
459 // ref-counting operations. Therefore we have no need to emit either a
460 // dynamic initialization or a cleanup and we can just return the address
461 // of the temporary.
462 if (Var->hasInitializer())
463 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
464
465 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
466 }
467 LValue RefTempDst = MakeAddrLValue(Object, M->getType(),
469
470 switch (getEvaluationKind(E->getType())) {
471 default: llvm_unreachable("expected scalar or aggregate expression");
472 case TEK_Scalar:
473 EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
474 break;
475 case TEK_Aggregate: {
477 E->getType().getQualifiers(),
482 break;
483 }
484 }
485
486 pushTemporaryCleanup(*this, M, E, Object);
487 return RefTempDst;
488 }
489
492 E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
493
494 for (const auto &Ignored : CommaLHSs)
495 EmitIgnoredExpr(Ignored);
496
497 if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
498 if (opaque->getType()->isRecordType()) {
499 assert(Adjustments.empty());
500 return EmitOpaqueValueLValue(opaque);
501 }
502 }
503
504 // Create and initialize the reference temporary.
505 Address Alloca = Address::invalid();
506 Address Object = createReferenceTemporary(*this, M, E, &Alloca);
507 if (auto *Var = dyn_cast<llvm::GlobalVariable>(
508 Object.getPointer()->stripPointerCasts())) {
509 llvm::Type *TemporaryType = ConvertTypeForMem(E->getType());
510 Object = Object.withElementType(TemporaryType);
511 // If the temporary is a global and has a constant initializer or is a
512 // constant temporary that we promoted to a global, we may have already
513 // initialized it.
514 if (!Var->hasInitializer()) {
515 Var->setInitializer(CGM.EmitNullConstant(E->getType()));
516 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
517 }
518 } else {
519 switch (M->getStorageDuration()) {
520 case SD_Automatic:
521 if (auto *Size = EmitLifetimeStart(
522 CGM.getDataLayout().getTypeAllocSize(Alloca.getElementType()),
523 Alloca.getPointer())) {
524 pushCleanupAfterFullExpr<CallLifetimeEnd>(NormalEHLifetimeMarker,
525 Alloca, Size);
526 }
527 break;
528
529 case SD_FullExpression: {
530 if (!ShouldEmitLifetimeMarkers)
531 break;
532
533 // Avoid creating a conditional cleanup just to hold an llvm.lifetime.end
534 // marker. Instead, start the lifetime of a conditional temporary earlier
535 // so that it's unconditional. Don't do this with sanitizers which need
536 // more precise lifetime marks. However when inside an "await.suspend"
537 // block, we should always avoid conditional cleanup because it creates
538 // boolean marker that lives across await_suspend, which can destroy coro
539 // frame.
540 ConditionalEvaluation *OldConditional = nullptr;
541 CGBuilderTy::InsertPoint OldIP;
543 ((!SanOpts.has(SanitizerKind::HWAddress) &&
544 !SanOpts.has(SanitizerKind::Memory) &&
545 !CGM.getCodeGenOpts().SanitizeAddressUseAfterScope) ||
546 inSuspendBlock())) {
547 OldConditional = OutermostConditional;
548 OutermostConditional = nullptr;
549
550 OldIP = Builder.saveIP();
551 llvm::BasicBlock *Block = OldConditional->getStartingBlock();
552 Builder.restoreIP(CGBuilderTy::InsertPoint(
553 Block, llvm::BasicBlock::iterator(Block->back())));
554 }
555
556 if (auto *Size = EmitLifetimeStart(
557 CGM.getDataLayout().getTypeAllocSize(Alloca.getElementType()),
558 Alloca.getPointer())) {
559 pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, Alloca,
560 Size);
561 }
562
563 if (OldConditional) {
564 OutermostConditional = OldConditional;
565 Builder.restoreIP(OldIP);
566 }
567 break;
568 }
569
570 default:
571 break;
572 }
573 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
574 }
575 pushTemporaryCleanup(*this, M, E, Object);
576
577 // Perform derived-to-base casts and/or field accesses, to get from the
578 // temporary object we created (and, potentially, for which we extended
579 // the lifetime) to the subobject we're binding the reference to.
580 for (SubobjectAdjustment &Adjustment : llvm::reverse(Adjustments)) {
581 switch (Adjustment.Kind) {
583 Object =
584 GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
585 Adjustment.DerivedToBase.BasePath->path_begin(),
586 Adjustment.DerivedToBase.BasePath->path_end(),
587 /*NullCheckValue=*/ false, E->getExprLoc());
588 break;
589
592 LV = EmitLValueForField(LV, Adjustment.Field);
593 assert(LV.isSimple() &&
594 "materialized temporary field is not a simple lvalue");
595 Object = LV.getAddress(*this);
596 break;
597 }
598
600 llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
602 Adjustment.Ptr.MPT);
603 break;
604 }
605 }
606 }
607
608 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl);
609}
610
611RValue
613 // Emit the expression as an lvalue.
614 LValue LV = EmitLValue(E);
615 assert(LV.isSimple());
616 llvm::Value *Value = LV.getPointer(*this);
617
619 // C++11 [dcl.ref]p5 (as amended by core issue 453):
620 // If a glvalue to which a reference is directly bound designates neither
621 // an existing object or function of an appropriate type nor a region of
622 // storage of suitable size and alignment to contain an object of the
623 // reference's type, the behavior is undefined.
624 QualType Ty = E->getType();
626 }
627
628 return RValue::get(Value);
629}
630
631
632/// getAccessedFieldNo - Given an encoded value and a result number, return the
633/// input field number being accessed.
634unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
635 const llvm::Constant *Elts) {
636 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
637 ->getZExtValue();
638}
639
640/// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
641static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
642 llvm::Value *High) {
643 llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
644 llvm::Value *K47 = Builder.getInt64(47);
645 llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
646 llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
647 llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
648 llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
649 return Builder.CreateMul(B1, KMul);
650}
651
652bool CodeGenFunction::isNullPointerAllowed(TypeCheckKind TCK) {
653 return TCK == TCK_DowncastPointer || TCK == TCK_Upcast ||
655}
656
657bool CodeGenFunction::isVptrCheckRequired(TypeCheckKind TCK, QualType Ty) {
659 return (RD && RD->hasDefinition() && RD->isDynamicClass()) &&
660 (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
663}
664
666 return SanOpts.has(SanitizerKind::Null) ||
667 SanOpts.has(SanitizerKind::Alignment) ||
668 SanOpts.has(SanitizerKind::ObjectSize) ||
669 SanOpts.has(SanitizerKind::Vptr);
670}
671
672void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
673 llvm::Value *Ptr, QualType Ty,
674 CharUnits Alignment,
675 SanitizerSet SkippedChecks,
676 llvm::Value *ArraySize) {
678 return;
679
680 // Don't check pointers outside the default address space. The null check
681 // isn't correct, the object-size check isn't supported by LLVM, and we can't
682 // communicate the addresses to the runtime handler for the vptr check.
683 if (Ptr->getType()->getPointerAddressSpace())
684 return;
685
686 // Don't check pointers to volatile data. The behavior here is implementation-
687 // defined.
688 if (Ty.isVolatileQualified())
689 return;
690
691 SanitizerScope SanScope(this);
692
694 llvm::BasicBlock *Done = nullptr;
695
696 // Quickly determine whether we have a pointer to an alloca. It's possible
697 // to skip null checks, and some alignment checks, for these pointers. This
698 // can reduce compile-time significantly.
699 auto PtrToAlloca = dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCasts());
700
701 llvm::Value *True = llvm::ConstantInt::getTrue(getLLVMContext());
702 llvm::Value *IsNonNull = nullptr;
703 bool IsGuaranteedNonNull =
704 SkippedChecks.has(SanitizerKind::Null) || PtrToAlloca;
705 bool AllowNullPointers = isNullPointerAllowed(TCK);
706 if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) &&
707 !IsGuaranteedNonNull) {
708 // The glvalue must not be an empty glvalue.
709 IsNonNull = Builder.CreateIsNotNull(Ptr);
710
711 // The IR builder can constant-fold the null check if the pointer points to
712 // a constant.
713 IsGuaranteedNonNull = IsNonNull == True;
714
715 // Skip the null check if the pointer is known to be non-null.
716 if (!IsGuaranteedNonNull) {
717 if (AllowNullPointers) {
718 // When performing pointer casts, it's OK if the value is null.
719 // Skip the remaining checks in that case.
720 Done = createBasicBlock("null");
721 llvm::BasicBlock *Rest = createBasicBlock("not.null");
722 Builder.CreateCondBr(IsNonNull, Rest, Done);
723 EmitBlock(Rest);
724 } else {
725 Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null));
726 }
727 }
728 }
729
730 if (SanOpts.has(SanitizerKind::ObjectSize) &&
731 !SkippedChecks.has(SanitizerKind::ObjectSize) &&
732 !Ty->isIncompleteType()) {
734 llvm::Value *Size = llvm::ConstantInt::get(IntPtrTy, TySize);
735 if (ArraySize)
736 Size = Builder.CreateMul(Size, ArraySize);
737
738 // Degenerate case: new X[0] does not need an objectsize check.
739 llvm::Constant *ConstantSize = dyn_cast<llvm::Constant>(Size);
740 if (!ConstantSize || !ConstantSize->isNullValue()) {
741 // The glvalue must refer to a large enough storage region.
742 // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation
743 // to check this.
744 // FIXME: Get object address space
745 llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy };
746 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
747 llvm::Value *Min = Builder.getFalse();
748 llvm::Value *NullIsUnknown = Builder.getFalse();
749 llvm::Value *Dynamic = Builder.getFalse();
750 llvm::Value *LargeEnough = Builder.CreateICmpUGE(
751 Builder.CreateCall(F, {Ptr, Min, NullIsUnknown, Dynamic}), Size);
752 Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
753 }
754 }
755
756 llvm::MaybeAlign AlignVal;
757 llvm::Value *PtrAsInt = nullptr;
758
759 if (SanOpts.has(SanitizerKind::Alignment) &&
760 !SkippedChecks.has(SanitizerKind::Alignment)) {
761 AlignVal = Alignment.getAsMaybeAlign();
762 if (!Ty->isIncompleteType() && !AlignVal)
763 AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr,
764 /*ForPointeeType=*/true)
766
767 // The glvalue must be suitably aligned.
768 if (AlignVal && *AlignVal > llvm::Align(1) &&
769 (!PtrToAlloca || PtrToAlloca->getAlign() < *AlignVal)) {
770 PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
771 llvm::Value *Align = Builder.CreateAnd(
772 PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal->value() - 1));
773 llvm::Value *Aligned =
774 Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
775 if (Aligned != True)
776 Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
777 }
778 }
779
780 if (Checks.size() > 0) {
781 llvm::Constant *StaticData[] = {
783 llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2(*AlignVal) : 1),
784 llvm::ConstantInt::get(Int8Ty, TCK)};
785 EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData,
786 PtrAsInt ? PtrAsInt : Ptr);
787 }
788
789 // If possible, check that the vptr indicates that there is a subobject of
790 // type Ty at offset zero within this object.
791 //
792 // C++11 [basic.life]p5,6:
793 // [For storage which does not refer to an object within its lifetime]
794 // The program has undefined behavior if:
795 // -- the [pointer or glvalue] is used to access a non-static data member
796 // or call a non-static member function
797 if (SanOpts.has(SanitizerKind::Vptr) &&
798 !SkippedChecks.has(SanitizerKind::Vptr) && isVptrCheckRequired(TCK, Ty)) {
799 // Ensure that the pointer is non-null before loading it. If there is no
800 // compile-time guarantee, reuse the run-time null check or emit a new one.
801 if (!IsGuaranteedNonNull) {
802 if (!IsNonNull)
803 IsNonNull = Builder.CreateIsNotNull(Ptr);
804 if (!Done)
805 Done = createBasicBlock("vptr.null");
806 llvm::BasicBlock *VptrNotNull = createBasicBlock("vptr.not.null");
807 Builder.CreateCondBr(IsNonNull, VptrNotNull, Done);
808 EmitBlock(VptrNotNull);
809 }
810
811 // Compute a hash of the mangled name of the type.
812 //
813 // FIXME: This is not guaranteed to be deterministic! Move to a
814 // fingerprinting mechanism once LLVM provides one. For the time
815 // being the implementation happens to be deterministic.
816 SmallString<64> MangledName;
817 llvm::raw_svector_ostream Out(MangledName);
819 Out);
820
821 // Contained in NoSanitizeList based on the mangled type.
822 if (!CGM.getContext().getNoSanitizeList().containsType(SanitizerKind::Vptr,
823 Out.str())) {
824 llvm::hash_code TypeHash = hash_value(Out.str());
825
826 // Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
827 llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
828 Address VPtrAddr(Ptr, IntPtrTy, getPointerAlign());
829 llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
830 llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
831
832 llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
833 Hash = Builder.CreateTrunc(Hash, IntPtrTy);
834
835 // Look the hash up in our cache.
836 const int CacheSize = 128;
837 llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
838 llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
839 "__ubsan_vptr_type_cache");
840 llvm::Value *Slot = Builder.CreateAnd(Hash,
841 llvm::ConstantInt::get(IntPtrTy,
842 CacheSize-1));
843 llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
844 llvm::Value *CacheVal = Builder.CreateAlignedLoad(
845 IntPtrTy, Builder.CreateInBoundsGEP(HashTable, Cache, Indices),
847
848 // If the hash isn't in the cache, call a runtime handler to perform the
849 // hard work of checking whether the vptr is for an object of the right
850 // type. This will either fill in the cache and return, or produce a
851 // diagnostic.
852 llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash);
853 llvm::Constant *StaticData[] = {
857 llvm::ConstantInt::get(Int8Ty, TCK)
858 };
859 llvm::Value *DynamicData[] = { Ptr, Hash };
860 EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr),
861 SanitizerHandler::DynamicTypeCacheMiss, StaticData,
862 DynamicData);
863 }
864 }
865
866 if (Done) {
867 Builder.CreateBr(Done);
868 EmitBlock(Done);
869 }
870}
871
872llvm::Value *CodeGenFunction::LoadPassedObjectSize(const Expr *E,
873 QualType EltTy) {
875 uint64_t EltSize = C.getTypeSizeInChars(EltTy).getQuantity();
876 if (!EltSize)
877 return nullptr;
878
879 auto *ArrayDeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
880 if (!ArrayDeclRef)
881 return nullptr;
882
883 auto *ParamDecl = dyn_cast<ParmVarDecl>(ArrayDeclRef->getDecl());
884 if (!ParamDecl)
885 return nullptr;
886
887 auto *POSAttr = ParamDecl->getAttr<PassObjectSizeAttr>();
888 if (!POSAttr)
889 return nullptr;
890
891 // Don't load the size if it's a lower bound.
892 int POSType = POSAttr->getType();
893 if (POSType != 0 && POSType != 1)
894 return nullptr;
895
896 // Find the implicit size parameter.
897 auto PassedSizeIt = SizeArguments.find(ParamDecl);
898 if (PassedSizeIt == SizeArguments.end())
899 return nullptr;
900
901 const ImplicitParamDecl *PassedSizeDecl = PassedSizeIt->second;
902 assert(LocalDeclMap.count(PassedSizeDecl) && "Passed size not loadable");
903 Address AddrOfSize = LocalDeclMap.find(PassedSizeDecl)->second;
904 llvm::Value *SizeInBytes = EmitLoadOfScalar(AddrOfSize, /*Volatile=*/false,
905 C.getSizeType(), E->getExprLoc());
906 llvm::Value *SizeOfElement =
907 llvm::ConstantInt::get(SizeInBytes->getType(), EltSize);
908 return Builder.CreateUDiv(SizeInBytes, SizeOfElement);
909}
910
911/// If Base is known to point to the start of an array, return the length of
912/// that array. Return 0 if the length cannot be determined.
913static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
914 const Expr *Base,
915 QualType &IndexedType,
917 StrictFlexArraysLevel) {
918 // For the vector indexing extension, the bound is the number of elements.
919 if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
920 IndexedType = Base->getType();
921 return CGF.Builder.getInt32(VT->getNumElements());
922 }
923
924 Base = Base->IgnoreParens();
925
926 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
927 if (CE->getCastKind() == CK_ArrayToPointerDecay &&
928 !CE->getSubExpr()->isFlexibleArrayMemberLike(CGF.getContext(),
929 StrictFlexArraysLevel)) {
930 CodeGenFunction::SanitizerScope SanScope(&CGF);
931
932 IndexedType = CE->getSubExpr()->getType();
933 const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
934 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
935 return CGF.Builder.getInt(CAT->getSize());
936
937 if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
938 return CGF.getVLASize(VAT).NumElts;
939 // Ignore pass_object_size here. It's not applicable on decayed pointers.
940 }
941 }
942
943 CodeGenFunction::SanitizerScope SanScope(&CGF);
944
945 QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0};
946 if (llvm::Value *POS = CGF.LoadPassedObjectSize(Base, EltTy)) {
947 IndexedType = Base->getType();
948 return POS;
949 }
950
951 return nullptr;
952}
953
954namespace {
955
956/// \p StructAccessBase returns the base \p Expr of a field access. It returns
957/// either a \p DeclRefExpr, representing the base pointer to the struct, i.e.:
958///
959/// p in p-> a.b.c
960///
961/// or a \p MemberExpr, if the \p MemberExpr has the \p RecordDecl we're
962/// looking for:
963///
964/// struct s {
965/// struct s *ptr;
966/// int count;
967/// char array[] __attribute__((counted_by(count)));
968/// };
969///
970/// If we have an expression like \p p->ptr->array[index], we want the
971/// \p MemberExpr for \p p->ptr instead of \p p.
972class StructAccessBase
973 : public ConstStmtVisitor<StructAccessBase, const Expr *> {
974 const RecordDecl *ExpectedRD;
975
976 bool IsExpectedRecordDecl(const Expr *E) const {
977 QualType Ty = E->getType();
978 if (Ty->isPointerType())
979 Ty = Ty->getPointeeType();
980 return ExpectedRD == Ty->getAsRecordDecl();
981 }
982
983public:
984 StructAccessBase(const RecordDecl *ExpectedRD) : ExpectedRD(ExpectedRD) {}
985
986 //===--------------------------------------------------------------------===//
987 // Visitor Methods
988 //===--------------------------------------------------------------------===//
989
990 // NOTE: If we build C++ support for counted_by, then we'll have to handle
991 // horrors like this:
992 //
993 // struct S {
994 // int x, y;
995 // int blah[] __attribute__((counted_by(x)));
996 // } s;
997 //
998 // int foo(int index, int val) {
999 // int (S::*IHatePMDs)[] = &S::blah;
1000 // (s.*IHatePMDs)[index] = val;
1001 // }
1002
1003 const Expr *Visit(const Expr *E) {
1005 }
1006
1007 const Expr *VisitStmt(const Stmt *S) { return nullptr; }
1008
1009 // These are the types we expect to return (in order of most to least
1010 // likely):
1011 //
1012 // 1. DeclRefExpr - This is the expression for the base of the structure.
1013 // It's exactly what we want to build an access to the \p counted_by
1014 // field.
1015 // 2. MemberExpr - This is the expression that has the same \p RecordDecl
1016 // as the flexble array member's lexical enclosing \p RecordDecl. This
1017 // allows us to catch things like: "p->p->array"
1018 // 3. CompoundLiteralExpr - This is for people who create something
1019 // heretical like (struct foo has a flexible array member):
1020 //
1021 // (struct foo){ 1, 2 }.blah[idx];
1022 const Expr *VisitDeclRefExpr(const DeclRefExpr *E) {
1023 return IsExpectedRecordDecl(E) ? E : nullptr;
1024 }
1025 const Expr *VisitMemberExpr(const MemberExpr *E) {
1026 if (IsExpectedRecordDecl(E) && E->isArrow())
1027 return E;
1028 const Expr *Res = Visit(E->getBase());
1029 return !Res && IsExpectedRecordDecl(E) ? E : Res;
1030 }
1031 const Expr *VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1032 return IsExpectedRecordDecl(E) ? E : nullptr;
1033 }
1034 const Expr *VisitCallExpr(const CallExpr *E) {
1035 return IsExpectedRecordDecl(E) ? E : nullptr;
1036 }
1037
1038 const Expr *VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1039 if (IsExpectedRecordDecl(E))
1040 return E;
1041 return Visit(E->getBase());
1042 }
1043 const Expr *VisitCastExpr(const CastExpr *E) {
1044 return Visit(E->getSubExpr());
1045 }
1046 const Expr *VisitParenExpr(const ParenExpr *E) {
1047 return Visit(E->getSubExpr());
1048 }
1049 const Expr *VisitUnaryAddrOf(const UnaryOperator *E) {
1050 return Visit(E->getSubExpr());
1051 }
1052 const Expr *VisitUnaryDeref(const UnaryOperator *E) {
1053 return Visit(E->getSubExpr());
1054 }
1055};
1056
1057} // end anonymous namespace
1058
1061
1063 const FieldDecl *FD, RecIndicesTy &Indices) {
1064 const CGRecordLayout &Layout = CGF.CGM.getTypes().getCGRecordLayout(RD);
1065 int64_t FieldNo = -1;
1066 for (const Decl *D : RD->decls()) {
1067 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
1068 FieldNo = Layout.getLLVMFieldNo(Field);
1069 if (FD == Field) {
1070 Indices.emplace_back(std::make_pair(RD, CGF.Builder.getInt32(FieldNo)));
1071 return true;
1072 }
1073 }
1074
1075 if (const auto *Record = dyn_cast<RecordDecl>(D)) {
1076 ++FieldNo;
1077 if (getGEPIndicesToField(CGF, Record, FD, Indices)) {
1078 if (RD->isUnion())
1079 FieldNo = 0;
1080 Indices.emplace_back(std::make_pair(RD, CGF.Builder.getInt32(FieldNo)));
1081 return true;
1082 }
1083 }
1084 }
1085
1086 return false;
1087}
1088
1089/// This method is typically called in contexts where we can't generate
1090/// side-effects, like in __builtin_dynamic_object_size. When finding
1091/// expressions, only choose those that have either already been emitted or can
1092/// be loaded without side-effects.
1093///
1094/// - \p FAMDecl: the \p Decl for the flexible array member. It may not be
1095/// within the top-level struct.
1096/// - \p CountDecl: must be within the same non-anonymous struct as \p FAMDecl.
1098 const Expr *Base, const FieldDecl *FAMDecl, const FieldDecl *CountDecl) {
1099 const RecordDecl *RD = CountDecl->getParent()->getOuterLexicalRecordContext();
1100
1101 // Find the base struct expr (i.e. p in p->a.b.c.d).
1102 const Expr *StructBase = StructAccessBase(RD).Visit(Base);
1103 if (!StructBase || StructBase->HasSideEffects(getContext()))
1104 return nullptr;
1105
1106 llvm::Value *Res = nullptr;
1107 if (const auto *DRE = dyn_cast<DeclRefExpr>(StructBase)) {
1108 Res = EmitDeclRefLValue(DRE).getPointer(*this);
1109 Res = Builder.CreateAlignedLoad(ConvertType(DRE->getType()), Res,
1110 getPointerAlign(), "dre.load");
1111 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(StructBase)) {
1112 LValue LV = EmitMemberExpr(ME);
1113 Address Addr = LV.getAddress(*this);
1114 Res = Addr.getPointer();
1115 } else if (StructBase->getType()->isPointerType()) {
1116 LValueBaseInfo BaseInfo;
1117 TBAAAccessInfo TBAAInfo;
1118 Address Addr = EmitPointerWithAlignment(StructBase, &BaseInfo, &TBAAInfo);
1119 Res = Addr.getPointer();
1120 } else {
1121 return nullptr;
1122 }
1123
1124 llvm::Value *Zero = Builder.getInt32(0);
1125 RecIndicesTy Indices;
1126
1127 getGEPIndicesToField(*this, RD, CountDecl, Indices);
1128
1129 for (auto I = Indices.rbegin(), E = Indices.rend(); I != E; ++I)
1130 Res = Builder.CreateInBoundsGEP(
1131 ConvertType(QualType(I->first->getTypeForDecl(), 0)), Res,
1132 {Zero, I->second}, "..counted_by.gep");
1133
1134 return Builder.CreateAlignedLoad(ConvertType(CountDecl->getType()), Res,
1135 getIntAlign(), "..counted_by.load");
1136}
1137
1139 if (!FD || !FD->hasAttr<CountedByAttr>())
1140 return nullptr;
1141
1142 const auto *CBA = FD->getAttr<CountedByAttr>();
1143 if (!CBA)
1144 return nullptr;
1145
1146 auto GetNonAnonStructOrUnion =
1147 [](const RecordDecl *RD) -> const RecordDecl * {
1148 while (RD && RD->isAnonymousStructOrUnion()) {
1149 const auto *R = dyn_cast<RecordDecl>(RD->getDeclContext());
1150 if (!R)
1151 return nullptr;
1152 RD = R;
1153 }
1154 return RD;
1155 };
1156 const RecordDecl *EnclosingRD = GetNonAnonStructOrUnion(FD->getParent());
1157 if (!EnclosingRD)
1158 return nullptr;
1159
1160 DeclarationName DName(CBA->getCountedByField());
1161 DeclContext::lookup_result Lookup = EnclosingRD->lookup(DName);
1162
1163 if (Lookup.empty())
1164 return nullptr;
1165
1166 const NamedDecl *ND = Lookup.front();
1167 if (const auto *IFD = dyn_cast<IndirectFieldDecl>(ND))
1168 ND = IFD->getAnonField();
1169
1170 return dyn_cast<FieldDecl>(ND);
1171}
1172
1173void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
1174 llvm::Value *Index, QualType IndexType,
1175 bool Accessed) {
1176 assert(SanOpts.has(SanitizerKind::ArrayBounds) &&
1177 "should not be called unless adding bounds checks");
1178 const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
1179 getLangOpts().getStrictFlexArraysLevel();
1180 QualType IndexedType;
1181 llvm::Value *Bound =
1182 getArrayIndexingBound(*this, Base, IndexedType, StrictFlexArraysLevel);
1183
1184 EmitBoundsCheckImpl(E, Bound, Index, IndexType, IndexedType, Accessed);
1185}
1186
1187void CodeGenFunction::EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound,
1188 llvm::Value *Index,
1189 QualType IndexType,
1190 QualType IndexedType, bool Accessed) {
1191 if (!Bound)
1192 return;
1193
1194 SanitizerScope SanScope(this);
1195
1196 bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
1197 llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
1198 llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
1199
1200 llvm::Constant *StaticData[] = {
1202 EmitCheckTypeDescriptor(IndexedType),
1203 EmitCheckTypeDescriptor(IndexType)
1204 };
1205 llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
1206 : Builder.CreateICmpULE(IndexVal, BoundVal);
1207 EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds),
1208 SanitizerHandler::OutOfBounds, StaticData, Index);
1209}
1210
1213 bool isInc, bool isPre) {
1214 ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
1215
1216 llvm::Value *NextVal;
1217 if (isa<llvm::IntegerType>(InVal.first->getType())) {
1218 uint64_t AmountVal = isInc ? 1 : -1;
1219 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
1220
1221 // Add the inc/dec to the real part.
1222 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1223 } else {
1224 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
1225 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
1226 if (!isInc)
1227 FVal.changeSign();
1228 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
1229
1230 // Add the inc/dec to the real part.
1231 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
1232 }
1233
1234 ComplexPairTy IncVal(NextVal, InVal.second);
1235
1236 // Store the updated result through the lvalue.
1237 EmitStoreOfComplex(IncVal, LV, /*init*/ false);
1238 if (getLangOpts().OpenMP)
1240 E->getSubExpr());
1241
1242 // If this is a postinc, return the value read from memory, otherwise use the
1243 // updated value.
1244 return isPre ? IncVal : InVal;
1245}
1246
1248 CodeGenFunction *CGF) {
1249 // Bind VLAs in the cast type.
1250 if (CGF && E->getType()->isVariablyModifiedType())
1252
1253 if (CGDebugInfo *DI = getModuleDebugInfo())
1254 DI->EmitExplicitCastType(E->getType());
1255}
1256
1257//===----------------------------------------------------------------------===//
1258// LValue Expression Emission
1259//===----------------------------------------------------------------------===//
1260
1262 TBAAAccessInfo *TBAAInfo,
1263 KnownNonNull_t IsKnownNonNull,
1264 CodeGenFunction &CGF) {
1265 // We allow this with ObjC object pointers because of fragile ABIs.
1266 assert(E->getType()->isPointerType() ||
1268 E = E->IgnoreParens();
1269
1270 // Casts:
1271 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1272 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE))
1273 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
1274
1275 switch (CE->getCastKind()) {
1276 // Non-converting casts (but not C's implicit conversion from void*).
1277 case CK_BitCast:
1278 case CK_NoOp:
1279 case CK_AddressSpaceConversion:
1280 if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) {
1281 if (PtrTy->getPointeeType()->isVoidType())
1282 break;
1283
1284 LValueBaseInfo InnerBaseInfo;
1285 TBAAAccessInfo InnerTBAAInfo;
1287 CE->getSubExpr(), &InnerBaseInfo, &InnerTBAAInfo, IsKnownNonNull);
1288 if (BaseInfo) *BaseInfo = InnerBaseInfo;
1289 if (TBAAInfo) *TBAAInfo = InnerTBAAInfo;
1290
1291 if (isa<ExplicitCastExpr>(CE)) {
1292 LValueBaseInfo TargetTypeBaseInfo;
1293 TBAAAccessInfo TargetTypeTBAAInfo;
1295 E->getType(), &TargetTypeBaseInfo, &TargetTypeTBAAInfo);
1296 if (TBAAInfo)
1297 *TBAAInfo =
1298 CGF.CGM.mergeTBAAInfoForCast(*TBAAInfo, TargetTypeTBAAInfo);
1299 // If the source l-value is opaque, honor the alignment of the
1300 // casted-to type.
1301 if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
1302 if (BaseInfo)
1303 BaseInfo->mergeForCast(TargetTypeBaseInfo);
1304 Addr = Address(Addr.getPointer(), Addr.getElementType(), Align,
1305 IsKnownNonNull);
1306 }
1307 }
1308
1309 if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
1310 CE->getCastKind() == CK_BitCast) {
1311 if (auto PT = E->getType()->getAs<PointerType>())
1312 CGF.EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr,
1313 /*MayBeNull=*/true,
1315 CE->getBeginLoc());
1316 }
1317
1318 llvm::Type *ElemTy =
1320 Addr = Addr.withElementType(ElemTy);
1321 if (CE->getCastKind() == CK_AddressSpaceConversion)
1322 Addr = CGF.Builder.CreateAddrSpaceCast(Addr,
1323 CGF.ConvertType(E->getType()));
1324 return Addr;
1325 }
1326 break;
1327
1328 // Array-to-pointer decay.
1329 case CK_ArrayToPointerDecay:
1330 return CGF.EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo, TBAAInfo);
1331
1332 // Derived-to-base conversions.
1333 case CK_UncheckedDerivedToBase:
1334 case CK_DerivedToBase: {
1335 // TODO: Support accesses to members of base classes in TBAA. For now, we
1336 // conservatively pretend that the complete object is of the base class
1337 // type.
1338 if (TBAAInfo)
1339 *TBAAInfo = CGF.CGM.getTBAAAccessInfo(E->getType());
1341 CE->getSubExpr(), BaseInfo, nullptr,
1342 (KnownNonNull_t)(IsKnownNonNull ||
1343 CE->getCastKind() == CK_UncheckedDerivedToBase));
1344 auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl();
1345 return CGF.GetAddressOfBaseClass(
1346 Addr, Derived, CE->path_begin(), CE->path_end(),
1347 CGF.ShouldNullCheckClassCastValue(CE), CE->getExprLoc());
1348 }
1349
1350 // TODO: Is there any reason to treat base-to-derived conversions
1351 // specially?
1352 default:
1353 break;
1354 }
1355 }
1356
1357 // Unary &.
1358 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1359 if (UO->getOpcode() == UO_AddrOf) {
1360 LValue LV = CGF.EmitLValue(UO->getSubExpr(), IsKnownNonNull);
1361 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
1362 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
1363 return LV.getAddress(CGF);
1364 }
1365 }
1366
1367 // std::addressof and variants.
1368 if (auto *Call = dyn_cast<CallExpr>(E)) {
1369 switch (Call->getBuiltinCallee()) {
1370 default:
1371 break;
1372 case Builtin::BIaddressof:
1373 case Builtin::BI__addressof:
1374 case Builtin::BI__builtin_addressof: {
1375 LValue LV = CGF.EmitLValue(Call->getArg(0), IsKnownNonNull);
1376 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
1377 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
1378 return LV.getAddress(CGF);
1379 }
1380 }
1381 }
1382
1383 // TODO: conditional operators, comma.
1384
1385 // Otherwise, use the alignment of the type.
1386 CharUnits Align =
1387 CGF.CGM.getNaturalPointeeTypeAlignment(E->getType(), BaseInfo, TBAAInfo);
1388 llvm::Type *ElemTy = CGF.ConvertTypeForMem(E->getType()->getPointeeType());
1389 return Address(CGF.EmitScalarExpr(E), ElemTy, Align, IsKnownNonNull);
1390}
1391
1392/// EmitPointerWithAlignment - Given an expression of pointer type, try to
1393/// derive a more accurate bound on the alignment of the pointer.
1395 const Expr *E, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo,
1396 KnownNonNull_t IsKnownNonNull) {
1397 Address Addr =
1398 ::EmitPointerWithAlignment(E, BaseInfo, TBAAInfo, IsKnownNonNull, *this);
1399 if (IsKnownNonNull && !Addr.isKnownNonNull())
1400 Addr.setKnownNonNull();
1401 return Addr;
1402}
1403
1405 llvm::Value *V = RV.getScalarVal();
1406 if (auto MPT = T->getAs<MemberPointerType>())
1407 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, V, MPT);
1408 return Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
1409}
1410
1412 if (Ty->isVoidType())
1413 return RValue::get(nullptr);
1414
1415 switch (getEvaluationKind(Ty)) {
1416 case TEK_Complex: {
1417 llvm::Type *EltTy =
1419 llvm::Value *U = llvm::UndefValue::get(EltTy);
1420 return RValue::getComplex(std::make_pair(U, U));
1421 }
1422
1423 // If this is a use of an undefined aggregate type, the aggregate must have an
1424 // identifiable address. Just because the contents of the value are undefined
1425 // doesn't mean that the address can't be taken and compared.
1426 case TEK_Aggregate: {
1427 Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
1428 return RValue::getAggregate(DestPtr);
1429 }
1430
1431 case TEK_Scalar:
1432 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
1433 }
1434 llvm_unreachable("bad evaluation kind");
1435}
1436
1438 const char *Name) {
1439 ErrorUnsupported(E, Name);
1440 return GetUndefRValue(E->getType());
1441}
1442
1444 const char *Name) {
1445 ErrorUnsupported(E, Name);
1446 llvm::Type *ElTy = ConvertType(E->getType());
1447 llvm::Type *Ty = UnqualPtrTy;
1448 return MakeAddrLValue(
1449 Address(llvm::UndefValue::get(Ty), ElTy, CharUnits::One()), E->getType());
1450}
1451
1452bool CodeGenFunction::IsWrappedCXXThis(const Expr *Obj) {
1453 const Expr *Base = Obj;
1454 while (!isa<CXXThisExpr>(Base)) {
1455 // The result of a dynamic_cast can be null.
1456 if (isa<CXXDynamicCastExpr>(Base))
1457 return false;
1458
1459 if (const auto *CE = dyn_cast<CastExpr>(Base)) {
1460 Base = CE->getSubExpr();
1461 } else if (const auto *PE = dyn_cast<ParenExpr>(Base)) {
1462 Base = PE->getSubExpr();
1463 } else if (const auto *UO = dyn_cast<UnaryOperator>(Base)) {
1464 if (UO->getOpcode() == UO_Extension)
1465 Base = UO->getSubExpr();
1466 else
1467 return false;
1468 } else {
1469 return false;
1470 }
1471 }
1472 return true;
1473}
1474
1475LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
1476 LValue LV;
1477 if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
1478 LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
1479 else
1480 LV = EmitLValue(E);
1481 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) {
1482 SanitizerSet SkippedChecks;
1483 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
1484 bool IsBaseCXXThis = IsWrappedCXXThis(ME->getBase());
1485 if (IsBaseCXXThis)
1486 SkippedChecks.set(SanitizerKind::Alignment, true);
1487 if (IsBaseCXXThis || isa<DeclRefExpr>(ME->getBase()))
1488 SkippedChecks.set(SanitizerKind::Null, true);
1489 }
1490 EmitTypeCheck(TCK, E->getExprLoc(), LV.getPointer(*this), E->getType(),
1491 LV.getAlignment(), SkippedChecks);
1492 }
1493 return LV;
1494}
1495
1496/// EmitLValue - Emit code to compute a designator that specifies the location
1497/// of the expression.
1498///
1499/// This can return one of two things: a simple address or a bitfield reference.
1500/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
1501/// an LLVM pointer type.
1502///
1503/// If this returns a bitfield reference, nothing about the pointee type of the
1504/// LLVM value is known: For example, it may not be a pointer to an integer.
1505///
1506/// If this returns a normal address, and if the lvalue's C type is fixed size,
1507/// this method guarantees that the returned pointer type will point to an LLVM
1508/// type of the same size of the lvalue's type. If the lvalue has a variable
1509/// length type, this is not possible.
1510///
1512 KnownNonNull_t IsKnownNonNull) {
1513 LValue LV = EmitLValueHelper(E, IsKnownNonNull);
1514 if (IsKnownNonNull && !LV.isKnownNonNull())
1515 LV.setKnownNonNull();
1516 return LV;
1517}
1518
1520 const ASTContext &Ctx) {
1521 const Expr *SE = E->getSubExpr()->IgnoreImplicit();
1522 if (isa<OpaqueValueExpr>(SE))
1523 return SE->getType();
1524 return cast<CallExpr>(SE)->getCallReturnType(Ctx)->getPointeeType();
1525}
1526
1527LValue CodeGenFunction::EmitLValueHelper(const Expr *E,
1528 KnownNonNull_t IsKnownNonNull) {
1529 ApplyDebugLocation DL(*this, E);
1530 switch (E->getStmtClass()) {
1531 default: return EmitUnsupportedLValue(E, "l-value expression");
1532
1533 case Expr::ObjCPropertyRefExprClass:
1534 llvm_unreachable("cannot emit a property reference directly");
1535
1536 case Expr::ObjCSelectorExprClass:
1537 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
1538 case Expr::ObjCIsaExprClass:
1539 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
1540 case Expr::BinaryOperatorClass:
1541 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
1542 case Expr::CompoundAssignOperatorClass: {
1543 QualType Ty = E->getType();
1544 if (const AtomicType *AT = Ty->getAs<AtomicType>())
1545 Ty = AT->getValueType();
1546 if (!Ty->isAnyComplexType())
1547 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
1548 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
1549 }
1550 case Expr::CallExprClass:
1551 case Expr::CXXMemberCallExprClass:
1552 case Expr::CXXOperatorCallExprClass:
1553 case Expr::UserDefinedLiteralClass:
1554 return EmitCallExprLValue(cast<CallExpr>(E));
1555 case Expr::CXXRewrittenBinaryOperatorClass:
1556 return EmitLValue(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(),
1557 IsKnownNonNull);
1558 case Expr::VAArgExprClass:
1559 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
1560 case Expr::DeclRefExprClass:
1561 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
1562 case Expr::ConstantExprClass: {
1563 const ConstantExpr *CE = cast<ConstantExpr>(E);
1564 if (llvm::Value *Result = ConstantEmitter(*this).tryEmitConstantExpr(CE)) {
1566 return MakeNaturalAlignAddrLValue(Result, RetType);
1567 }
1568 return EmitLValue(cast<ConstantExpr>(E)->getSubExpr(), IsKnownNonNull);
1569 }
1570 case Expr::ParenExprClass:
1571 return EmitLValue(cast<ParenExpr>(E)->getSubExpr(), IsKnownNonNull);
1572 case Expr::GenericSelectionExprClass:
1573 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr(),
1574 IsKnownNonNull);
1575 case Expr::PredefinedExprClass:
1576 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
1577 case Expr::StringLiteralClass:
1578 return EmitStringLiteralLValue(cast<StringLiteral>(E));
1579 case Expr::ObjCEncodeExprClass:
1580 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
1581 case Expr::PseudoObjectExprClass:
1582 return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
1583 case Expr::InitListExprClass:
1584 return EmitInitListLValue(cast<InitListExpr>(E));
1585 case Expr::CXXTemporaryObjectExprClass:
1586 case Expr::CXXConstructExprClass:
1587 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
1588 case Expr::CXXBindTemporaryExprClass:
1589 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
1590 case Expr::CXXUuidofExprClass:
1591 return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
1592 case Expr::LambdaExprClass:
1593 return EmitAggExprToLValue(E);
1594
1595 case Expr::ExprWithCleanupsClass: {
1596 const auto *cleanups = cast<ExprWithCleanups>(E);
1597 RunCleanupsScope Scope(*this);
1598 LValue LV = EmitLValue(cleanups->getSubExpr(), IsKnownNonNull);
1599 if (LV.isSimple()) {
1600 // Defend against branches out of gnu statement expressions surrounded by
1601 // cleanups.
1602 Address Addr = LV.getAddress(*this);
1603 llvm::Value *V = Addr.getPointer();
1604 Scope.ForceCleanup({&V});
1605 return LValue::MakeAddr(Addr.withPointer(V, Addr.isKnownNonNull()),
1606 LV.getType(), getContext(), LV.getBaseInfo(),
1607 LV.getTBAAInfo());
1608 }
1609 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1610 // bitfield lvalue or some other non-simple lvalue?
1611 return LV;
1612 }
1613
1614 case Expr::CXXDefaultArgExprClass: {
1615 auto *DAE = cast<CXXDefaultArgExpr>(E);
1616 CXXDefaultArgExprScope Scope(*this, DAE);
1617 return EmitLValue(DAE->getExpr(), IsKnownNonNull);
1618 }
1619 case Expr::CXXDefaultInitExprClass: {
1620 auto *DIE = cast<CXXDefaultInitExpr>(E);
1621 CXXDefaultInitExprScope Scope(*this, DIE);
1622 return EmitLValue(DIE->getExpr(), IsKnownNonNull);
1623 }
1624 case Expr::CXXTypeidExprClass:
1625 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
1626
1627 case Expr::ObjCMessageExprClass:
1628 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
1629 case Expr::ObjCIvarRefExprClass:
1630 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
1631 case Expr::StmtExprClass:
1632 return EmitStmtExprLValue(cast<StmtExpr>(E));
1633 case Expr::UnaryOperatorClass:
1634 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
1635 case Expr::ArraySubscriptExprClass:
1636 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
1637 case Expr::MatrixSubscriptExprClass:
1638 return EmitMatrixSubscriptExpr(cast<MatrixSubscriptExpr>(E));
1639 case Expr::OMPArraySectionExprClass:
1640 return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E));
1641 case Expr::ExtVectorElementExprClass:
1642 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
1643 case Expr::CXXThisExprClass:
1645 case Expr::MemberExprClass:
1646 return EmitMemberExpr(cast<MemberExpr>(E));
1647 case Expr::CompoundLiteralExprClass:
1648 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
1649 case Expr::ConditionalOperatorClass:
1650 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
1651 case Expr::BinaryConditionalOperatorClass:
1652 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
1653 case Expr::ChooseExprClass:
1654 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(), IsKnownNonNull);
1655 case Expr::OpaqueValueExprClass:
1656 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
1657 case Expr::SubstNonTypeTemplateParmExprClass:
1658 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
1659 IsKnownNonNull);
1660 case Expr::ImplicitCastExprClass:
1661 case Expr::CStyleCastExprClass:
1662 case Expr::CXXFunctionalCastExprClass:
1663 case Expr::CXXStaticCastExprClass:
1664 case Expr::CXXDynamicCastExprClass:
1665 case Expr::CXXReinterpretCastExprClass:
1666 case Expr::CXXConstCastExprClass:
1667 case Expr::CXXAddrspaceCastExprClass:
1668 case Expr::ObjCBridgedCastExprClass:
1669 return EmitCastLValue(cast<CastExpr>(E));
1670
1671 case Expr::MaterializeTemporaryExprClass:
1672 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
1673
1674 case Expr::CoawaitExprClass:
1675 return EmitCoawaitLValue(cast<CoawaitExpr>(E));
1676 case Expr::CoyieldExprClass:
1677 return EmitCoyieldLValue(cast<CoyieldExpr>(E));
1678 case Expr::PackIndexingExprClass:
1679 return EmitLValue(cast<PackIndexingExpr>(E)->getSelectedExpr());
1680 }
1681}
1682
1683/// Given an object of the given canonical type, can we safely copy a
1684/// value out of it based on its initializer?
1686 assert(type.isCanonical());
1687 assert(!type->isReferenceType());
1688
1689 // Must be const-qualified but non-volatile.
1690 Qualifiers qs = type.getLocalQualifiers();
1691 if (!qs.hasConst() || qs.hasVolatile()) return false;
1692
1693 // Otherwise, all object types satisfy this except C++ classes with
1694 // mutable subobjects or non-trivial copy/destroy behavior.
1695 if (const auto *RT = dyn_cast<RecordType>(type))
1696 if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
1697 if (RD->hasMutableFields() || !RD->isTrivial())
1698 return false;
1699
1700 return true;
1701}
1702
1703/// Can we constant-emit a load of a reference to a variable of the
1704/// given type? This is different from predicates like
1705/// Decl::mightBeUsableInConstantExpressions because we do want it to apply
1706/// in situations that don't necessarily satisfy the language's rules
1707/// for this (e.g. C++'s ODR-use rules). For example, we want to able
1708/// to do this with const float variables even if those variables
1709/// aren't marked 'constexpr'.
1717 type = type.getCanonicalType();
1718 if (const auto *ref = dyn_cast<ReferenceType>(type)) {
1719 if (isConstantEmittableObjectType(ref->getPointeeType()))
1721 return CEK_AsReferenceOnly;
1722 }
1724 return CEK_AsValueOnly;
1725 return CEK_None;
1726}
1727
1728/// Try to emit a reference to the given value without producing it as
1729/// an l-value. This is just an optimization, but it avoids us needing
1730/// to emit global copies of variables if they're named without triggering
1731/// a formal use in a context where we can't emit a direct reference to them,
1732/// for instance if a block or lambda or a member of a local class uses a
1733/// const int variable or constexpr variable from an enclosing function.
1734CodeGenFunction::ConstantEmission
1736 ValueDecl *value = refExpr->getDecl();
1737
1738 // The value needs to be an enum constant or a constant variable.
1740 if (isa<ParmVarDecl>(value)) {
1741 CEK = CEK_None;
1742 } else if (auto *var = dyn_cast<VarDecl>(value)) {
1743 CEK = checkVarTypeForConstantEmission(var->getType());
1744 } else if (isa<EnumConstantDecl>(value)) {
1745 CEK = CEK_AsValueOnly;
1746 } else {
1747 CEK = CEK_None;
1748 }
1749 if (CEK == CEK_None) return ConstantEmission();
1750
1751 Expr::EvalResult result;
1752 bool resultIsReference;
1753 QualType resultType;
1754
1755 // It's best to evaluate all the way as an r-value if that's permitted.
1756 if (CEK != CEK_AsReferenceOnly &&
1757 refExpr->EvaluateAsRValue(result, getContext())) {
1758 resultIsReference = false;
1759 resultType = refExpr->getType();
1760
1761 // Otherwise, try to evaluate as an l-value.
1762 } else if (CEK != CEK_AsValueOnly &&
1763 refExpr->EvaluateAsLValue(result, getContext())) {
1764 resultIsReference = true;
1765 resultType = value->getType();
1766
1767 // Failure.
1768 } else {
1769 return ConstantEmission();
1770 }
1771
1772 // In any case, if the initializer has side-effects, abandon ship.
1773 if (result.HasSideEffects)
1774 return ConstantEmission();
1775
1776 // In CUDA/HIP device compilation, a lambda may capture a reference variable
1777 // referencing a global host variable by copy. In this case the lambda should
1778 // make a copy of the value of the global host variable. The DRE of the
1779 // captured reference variable cannot be emitted as load from the host
1780 // global variable as compile time constant, since the host variable is not
1781 // accessible on device. The DRE of the captured reference variable has to be
1782 // loaded from captures.
1783 if (CGM.getLangOpts().CUDAIsDevice && result.Val.isLValue() &&
1785 auto *MD = dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl);
1786 if (MD && MD->getParent()->isLambda() &&
1787 MD->getOverloadedOperator() == OO_Call) {
1788 const APValue::LValueBase &base = result.Val.getLValueBase();
1789 if (const ValueDecl *D = base.dyn_cast<const ValueDecl *>()) {
1790 if (const VarDecl *VD = dyn_cast<const VarDecl>(D)) {
1791 if (!VD->hasAttr<CUDADeviceAttr>()) {
1792 return ConstantEmission();
1793 }
1794 }
1795 }
1796 }
1797 }
1798
1799 // Emit as a constant.
1800 auto C = ConstantEmitter(*this).emitAbstract(refExpr->getLocation(),
1801 result.Val, resultType);
1802
1803 // Make sure we emit a debug reference to the global variable.
1804 // This should probably fire even for
1805 if (isa<VarDecl>(value)) {
1806 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
1807 EmitDeclRefExprDbgValue(refExpr, result.Val);
1808 } else {
1809 assert(isa<EnumConstantDecl>(value));
1810 EmitDeclRefExprDbgValue(refExpr, result.Val);
1811 }
1812
1813 // If we emitted a reference constant, we need to dereference that.
1814 if (resultIsReference)
1816
1818}
1819
1821 const MemberExpr *ME) {
1822 if (auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
1823 // Try to emit static variable member expressions as DREs.
1824 return DeclRefExpr::Create(
1826 /*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(),
1827 ME->getType(), ME->getValueKind(), nullptr, nullptr, ME->isNonOdrUse());
1828 }
1829 return nullptr;
1830}
1831
1832CodeGenFunction::ConstantEmission
1835 return tryEmitAsConstant(DRE);
1836 return ConstantEmission();
1837}
1838
1840 const CodeGenFunction::ConstantEmission &Constant, Expr *E) {
1841 assert(Constant && "not a constant");
1842 if (Constant.isReference())
1843 return EmitLoadOfLValue(Constant.getReferenceLValue(*this, E),
1844 E->getExprLoc())
1845 .getScalarVal();
1846 return Constant.getValue();
1847}
1848
1849llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
1850 SourceLocation Loc) {
1851 return EmitLoadOfScalar(lvalue.getAddress(*this), lvalue.isVolatile(),
1852 lvalue.getType(), Loc, lvalue.getBaseInfo(),
1853 lvalue.getTBAAInfo(), lvalue.isNontemporal());
1854}
1855
1857 if (Ty->isBooleanType())
1858 return true;
1859
1860 if (const EnumType *ET = Ty->getAs<EnumType>())
1861 return ET->getDecl()->getIntegerType()->isBooleanType();
1862
1863 if (const AtomicType *AT = Ty->getAs<AtomicType>())
1864 return hasBooleanRepresentation(AT->getValueType());
1865
1866 return false;
1867}
1868
1870 llvm::APInt &Min, llvm::APInt &End,
1871 bool StrictEnums, bool IsBool) {
1872 const EnumType *ET = Ty->getAs<EnumType>();
1873 bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums &&
1874 ET && !ET->getDecl()->isFixed();
1875 if (!IsBool && !IsRegularCPlusPlusEnum)
1876 return false;
1877
1878 if (IsBool) {
1879 Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
1880 End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
1881 } else {
1882 const EnumDecl *ED = ET->getDecl();
1883 ED->getValueRange(End, Min);
1884 }
1885 return true;
1886}
1887
1888llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
1889 llvm::APInt Min, End;
1890 if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
1892 return nullptr;
1893
1894 llvm::MDBuilder MDHelper(getLLVMContext());
1895 return MDHelper.createRange(Min, End);
1896}
1897
1899 SourceLocation Loc) {
1900 bool HasBoolCheck = SanOpts.has(SanitizerKind::Bool);
1901 bool HasEnumCheck = SanOpts.has(SanitizerKind::Enum);
1902 if (!HasBoolCheck && !HasEnumCheck)
1903 return false;
1904
1905 bool IsBool = hasBooleanRepresentation(Ty) ||
1907 bool NeedsBoolCheck = HasBoolCheck && IsBool;
1908 bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
1909 if (!NeedsBoolCheck && !NeedsEnumCheck)
1910 return false;
1911
1912 // Single-bit booleans don't need to be checked. Special-case this to avoid
1913 // a bit width mismatch when handling bitfield values. This is handled by
1914 // EmitFromMemory for the non-bitfield case.
1915 if (IsBool &&
1916 cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
1917 return false;
1918
1919 llvm::APInt Min, End;
1920 if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
1921 return true;
1922
1923 auto &Ctx = getLLVMContext();
1924 SanitizerScope SanScope(this);
1925 llvm::Value *Check;
1926 --End;
1927 if (!Min) {
1928 Check = Builder.CreateICmpULE(Value, llvm::ConstantInt::get(Ctx, End));
1929 } else {
1930 llvm::Value *Upper =
1931 Builder.CreateICmpSLE(Value, llvm::ConstantInt::get(Ctx, End));
1932 llvm::Value *Lower =
1933 Builder.CreateICmpSGE(Value, llvm::ConstantInt::get(Ctx, Min));
1934 Check = Builder.CreateAnd(Upper, Lower);
1935 }
1936 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc),
1939 NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool;
1940 EmitCheck(std::make_pair(Check, Kind), SanitizerHandler::LoadInvalidValue,
1941 StaticArgs, EmitCheckValue(Value));
1942 return true;
1943}
1944
1945llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
1946 QualType Ty,
1947 SourceLocation Loc,
1948 LValueBaseInfo BaseInfo,
1949 TBAAAccessInfo TBAAInfo,
1950 bool isNontemporal) {
1951 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getPointer()))
1952 if (GV->isThreadLocal())
1953 Addr = Addr.withPointer(Builder.CreateThreadLocalAddress(GV),
1955
1956 if (const auto *ClangVecTy = Ty->getAs<VectorType>()) {
1957 // Boolean vectors use `iN` as storage type.
1958 if (ClangVecTy->isExtVectorBoolType()) {
1959 llvm::Type *ValTy = ConvertType(Ty);
1960 unsigned ValNumElems =
1961 cast<llvm::FixedVectorType>(ValTy)->getNumElements();
1962 // Load the `iP` storage object (P is the padded vector size).
1963 auto *RawIntV = Builder.CreateLoad(Addr, Volatile, "load_bits");
1964 const auto *RawIntTy = RawIntV->getType();
1965 assert(RawIntTy->isIntegerTy() && "compressed iN storage for bitvectors");
1966 // Bitcast iP --> <P x i1>.
1967 auto *PaddedVecTy = llvm::FixedVectorType::get(
1968 Builder.getInt1Ty(), RawIntTy->getPrimitiveSizeInBits());
1969 llvm::Value *V = Builder.CreateBitCast(RawIntV, PaddedVecTy);
1970 // Shuffle <P x i1> --> <N x i1> (N is the actual bit size).
1971 V = emitBoolVecConversion(V, ValNumElems, "extractvec");
1972
1973 return EmitFromMemory(V, Ty);
1974 }
1975
1976 // Handle vectors of size 3 like size 4 for better performance.
1977 const llvm::Type *EltTy = Addr.getElementType();
1978 const auto *VTy = cast<llvm::FixedVectorType>(EltTy);
1979
1980 if (!CGM.getCodeGenOpts().PreserveVec3Type && VTy->getNumElements() == 3) {
1981
1982 llvm::VectorType *vec4Ty =
1983 llvm::FixedVectorType::get(VTy->getElementType(), 4);
1984 Address Cast = Addr.withElementType(vec4Ty);
1985 // Now load value.
1986 llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4");
1987
1988 // Shuffle vector to get vec3.
1989 V = Builder.CreateShuffleVector(V, ArrayRef<int>{0, 1, 2}, "extractVec");
1990 return EmitFromMemory(V, Ty);
1991 }
1992 }
1993
1994 // Atomic operations have to be done on integral types.
1995 LValue AtomicLValue =
1996 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
1997 if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) {
1998 return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal();
1999 }
2000
2001 llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile);
2002 if (isNontemporal) {
2003 llvm::MDNode *Node = llvm::MDNode::get(
2004 Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
2005 Load->setMetadata(llvm::LLVMContext::MD_nontemporal, Node);
2006 }
2007
2008 CGM.DecorateInstructionWithTBAA(Load, TBAAInfo);
2009
2010 if (EmitScalarRangeCheck(Load, Ty, Loc)) {
2011 // In order to prevent the optimizer from throwing away the check, don't
2012 // attach range metadata to the load.
2013 } else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
2014 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) {
2015 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
2016 Load->setMetadata(llvm::LLVMContext::MD_noundef,
2017 llvm::MDNode::get(getLLVMContext(), std::nullopt));
2018 }
2019
2020 return EmitFromMemory(Load, Ty);
2021}
2022
2023llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
2024 // Bool has a different representation in memory than in registers.
2025 if (hasBooleanRepresentation(Ty)) {
2026 // This should really always be an i1, but sometimes it's already
2027 // an i8, and it's awkward to track those cases down.
2028 if (Value->getType()->isIntegerTy(1))
2029 return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool");
2030 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
2031 "wrong value rep of bool");
2032 }
2033
2034 return Value;
2035}
2036
2037llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
2038 // Bool has a different representation in memory than in registers.
2039 if (hasBooleanRepresentation(Ty)) {
2040 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
2041 "wrong value rep of bool");
2042 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
2043 }
2044 if (Ty->isExtVectorBoolType()) {
2045 const auto *RawIntTy = Value->getType();
2046 // Bitcast iP --> <P x i1>.
2047 auto *PaddedVecTy = llvm::FixedVectorType::get(
2048 Builder.getInt1Ty(), RawIntTy->getPrimitiveSizeInBits());
2049 auto *V = Builder.CreateBitCast(Value, PaddedVecTy);
2050 // Shuffle <P x i1> --> <N x i1> (N is the actual bit size).
2051 llvm::Type *ValTy = ConvertType(Ty);
2052 unsigned ValNumElems = cast<llvm::FixedVectorType>(ValTy)->getNumElements();
2053 return emitBoolVecConversion(V, ValNumElems, "extractvec");
2054 }
2055
2056 return Value;
2057}
2058
2059// Convert the pointer of \p Addr to a pointer to a vector (the value type of
2060// MatrixType), if it points to a array (the memory type of MatrixType).
2062 bool IsVector = true) {
2063 auto *ArrayTy = dyn_cast<llvm::ArrayType>(Addr.getElementType());
2064 if (ArrayTy && IsVector) {
2065 auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
2066 ArrayTy->getNumElements());
2067
2068 return Addr.withElementType(VectorTy);
2069 }
2070 auto *VectorTy = dyn_cast<llvm::VectorType>(Addr.getElementType());
2071 if (VectorTy && !IsVector) {
2072 auto *ArrayTy = llvm::ArrayType::get(
2073 VectorTy->getElementType(),
2074 cast<llvm::FixedVectorType>(VectorTy)->getNumElements());
2075
2076 return Addr.withElementType(ArrayTy);
2077 }
2078
2079 return Addr;
2080}
2081
2082// Emit a store of a matrix LValue. This may require casting the original
2083// pointer to memory address (ArrayType) to a pointer to the value type
2084// (VectorType).
2085static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue,
2086 bool isInit, CodeGenFunction &CGF) {
2087 Address Addr = MaybeConvertMatrixAddress(lvalue.getAddress(CGF), CGF,
2088 value->getType()->isVectorTy());
2089 CGF.EmitStoreOfScalar(value, Addr, lvalue.isVolatile(), lvalue.getType(),
2090 lvalue.getBaseInfo(), lvalue.getTBAAInfo(), isInit,
2091 lvalue.isNontemporal());
2092}
2093
2094void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
2095 bool Volatile, QualType Ty,
2096 LValueBaseInfo BaseInfo,
2097 TBAAAccessInfo TBAAInfo,
2098 bool isInit, bool isNontemporal) {
2099 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getPointer()))
2100 if (GV->isThreadLocal())
2101 Addr = Addr.withPointer(Builder.CreateThreadLocalAddress(GV),
2103
2104 llvm::Type *SrcTy = Value->getType();
2105 if (const auto *ClangVecTy = Ty->getAs<VectorType>()) {
2106 auto *VecTy = dyn_cast<llvm::FixedVectorType>(SrcTy);
2107 if (VecTy && ClangVecTy->isExtVectorBoolType()) {
2108 auto *MemIntTy = cast<llvm::IntegerType>(Addr.getElementType());
2109 // Expand to the memory bit width.
2110 unsigned MemNumElems = MemIntTy->getPrimitiveSizeInBits();
2111 // <N x i1> --> <P x i1>.
2112 Value = emitBoolVecConversion(Value, MemNumElems, "insertvec");
2113 // <P x i1> --> iP.
2114 Value = Builder.CreateBitCast(Value, MemIntTy);
2115 } else if (!CGM.getCodeGenOpts().PreserveVec3Type) {
2116 // Handle vec3 special.
2117 if (VecTy && cast<llvm::FixedVectorType>(VecTy)->getNumElements() == 3) {
2118 // Our source is a vec3, do a shuffle vector to make it a vec4.
2119 Value = Builder.CreateShuffleVector(Value, ArrayRef<int>{0, 1, 2, -1},
2120 "extractVec");
2121 SrcTy = llvm::FixedVectorType::get(VecTy->getElementType(), 4);
2122 }
2123 if (Addr.getElementType() != SrcTy) {
2124 Addr = Addr.withElementType(SrcTy);
2125 }
2126 }
2127 }
2128
2129 Value = EmitToMemory(Value, Ty);
2130
2131 LValue AtomicLValue =
2132 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo);
2133 if (Ty->isAtomicType() ||
2134 (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) {
2135 EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit);
2136 return;
2137 }
2138
2139 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
2140 if (isNontemporal) {
2141 llvm::MDNode *Node =
2142 llvm::MDNode::get(Store->getContext(),
2143 llvm::ConstantAsMetadata::get(Builder.getInt32(1)));
2144 Store->setMetadata(llvm::LLVMContext::MD_nontemporal, Node);
2145 }
2146
2147 CGM.DecorateInstructionWithTBAA(Store, TBAAInfo);
2148}
2149
2150void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
2151 bool isInit) {
2152 if (lvalue.getType()->isConstantMatrixType()) {
2153 EmitStoreOfMatrixScalar(value, lvalue, isInit, *this);
2154 return;
2155 }
2156
2157 EmitStoreOfScalar(value, lvalue.getAddress(*this), lvalue.isVolatile(),
2158 lvalue.getType(), lvalue.getBaseInfo(),
2159 lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal());
2160}
2161
2162// Emit a load of a LValue of matrix type. This may require casting the pointer
2163// to memory address (ArrayType) to a pointer to the value type (VectorType).
2165 CodeGenFunction &CGF) {
2166 assert(LV.getType()->isConstantMatrixType());
2167 Address Addr = MaybeConvertMatrixAddress(LV.getAddress(CGF), CGF);
2168 LV.setAddress(Addr);
2169 return RValue::get(CGF.EmitLoadOfScalar(LV, Loc));
2170}
2171
2172/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
2173/// method emits the address of the lvalue, then loads the result as an rvalue,
2174/// returning the rvalue.
2176 if (LV.isObjCWeak()) {
2177 // load of a __weak object.
2178 Address AddrWeakObj = LV.getAddress(*this);
2180 AddrWeakObj));
2181 }
2183 // In MRC mode, we do a load+autorelease.
2184 if (!getLangOpts().ObjCAutoRefCount) {
2185 return RValue::get(EmitARCLoadWeak(LV.getAddress(*this)));
2186 }
2187
2188 // In ARC mode, we load retained and then consume the value.
2189 llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress(*this));
2190 Object = EmitObjCConsumeObject(LV.getType(), Object);
2191 return RValue::get(Object);
2192 }
2193
2194 if (LV.isSimple()) {
2195 assert(!LV.getType()->isFunctionType());
2196
2197 if (LV.getType()->isConstantMatrixType())
2198 return EmitLoadOfMatrixLValue(LV, Loc, *this);
2199
2200 // Everything needs a load.
2201 return RValue::get(EmitLoadOfScalar(LV, Loc));
2202 }
2203
2204 if (LV.isVectorElt()) {
2205 llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(),
2206 LV.isVolatileQualified());
2207 return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
2208 "vecext"));
2209 }
2210
2211 // If this is a reference to a subset of the elements of a vector, either
2212 // shuffle the input or extract/insert them as appropriate.
2213 if (LV.isExtVectorElt()) {
2215 }
2216
2217 // Global Register variables always invoke intrinsics
2218 if (LV.isGlobalReg())
2219 return EmitLoadOfGlobalRegLValue(LV);
2220
2221 if (LV.isMatrixElt()) {
2222 llvm::Value *Idx = LV.getMatrixIdx();
2223 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2224 const auto *const MatTy = LV.getType()->castAs<ConstantMatrixType>();
2225 llvm::MatrixBuilder MB(Builder);
2226 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
2227 }
2228 llvm::LoadInst *Load =
2230 return RValue::get(Builder.CreateExtractElement(Load, Idx, "matrixext"));
2231 }
2232
2233 assert(LV.isBitField() && "Unknown LValue type!");
2234 return EmitLoadOfBitfieldLValue(LV, Loc);
2235}
2236
2238 SourceLocation Loc) {
2239 const CGBitFieldInfo &Info = LV.getBitFieldInfo();
2240
2241 // Get the output type.
2242 llvm::Type *ResLTy = ConvertType(LV.getType());
2243
2244 Address Ptr = LV.getBitFieldAddress();
2245 llvm::Value *Val =
2246 Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load");
2247
2248 bool UseVolatile = LV.isVolatileQualified() &&
2249 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
2250 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
2251 const unsigned StorageSize =
2252 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
2253 if (Info.IsSigned) {
2254 assert(static_cast<unsigned>(Offset + Info.Size) <= StorageSize);
2255 unsigned HighBits = StorageSize - Offset - Info.Size;
2256 if (HighBits)
2257 Val = Builder.CreateShl(Val, HighBits, "bf.shl");
2258 if (Offset + HighBits)
2259 Val = Builder.CreateAShr(Val, Offset + HighBits, "bf.ashr");
2260 } else {
2261 if (Offset)
2262 Val = Builder.CreateLShr(Val, Offset, "bf.lshr");
2263 if (static_cast<unsigned>(Offset) + Info.Size < StorageSize)
2264 Val = Builder.CreateAnd(
2265 Val, llvm::APInt::getLowBitsSet(StorageSize, Info.Size), "bf.clear");
2266 }
2267 Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
2268 EmitScalarRangeCheck(Val, LV.getType(), Loc);
2269 return RValue::get(Val);
2270}
2271
2272// If this is a reference to a subset of the elements of a vector, create an
2273// appropriate shufflevector.
2275 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(),
2276 LV.isVolatileQualified());
2277
2278 // HLSL allows treating scalars as one-element vectors. Converting the scalar
2279 // IR value to a vector here allows the rest of codegen to behave as normal.
2280 if (getLangOpts().HLSL && !Vec->getType()->isVectorTy()) {
2281 llvm::Type *DstTy = llvm::FixedVectorType::get(Vec->getType(), 1);
2282 llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
2283 Vec = Builder.CreateInsertElement(DstTy, Vec, Zero, "cast.splat");
2284 }
2285
2286 const llvm::Constant *Elts = LV.getExtVectorElts();
2287
2288 // If the result of the expression is a non-vector type, we must be extracting
2289 // a single element. Just codegen as an extractelement.
2290 const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
2291 if (!ExprVT) {
2292 unsigned InIdx = getAccessedFieldNo(0, Elts);
2293 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
2294 return RValue::get(Builder.CreateExtractElement(Vec, Elt));
2295 }
2296
2297 // Always use shuffle vector to try to retain the original program structure
2298 unsigned NumResultElts = ExprVT->getNumElements();
2299
2301 for (unsigned i = 0; i != NumResultElts; ++i)
2302 Mask.push_back(getAccessedFieldNo(i, Elts));
2303
2304 Vec = Builder.CreateShuffleVector(Vec, Mask);
2305 return RValue::get(Vec);
2306}
2307
2308/// Generates lvalue for partial ext_vector access.
2310 Address VectorAddress = LV.getExtVectorAddress();
2311 QualType EQT = LV.getType()->castAs<VectorType>()->getElementType();
2312 llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
2313
2314 Address CastToPointerElement = VectorAddress.withElementType(VectorElementTy);
2315
2316 const llvm::Constant *Elts = LV.getExtVectorElts();
2317 unsigned ix = getAccessedFieldNo(0, Elts);
2318
2319 Address VectorBasePtrPlusIx =
2320 Builder.CreateConstInBoundsGEP(CastToPointerElement, ix,
2321 "vector.elt");
2322
2323 return VectorBasePtrPlusIx;
2324}
2325
2326/// Load of global gamed gegisters are always calls to intrinsics.
2328 assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) &&
2329 "Bad type for register variable");
2330 llvm::MDNode *RegName = cast<llvm::MDNode>(
2331 cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata());
2332
2333 // We accept integer and pointer types only
2334 llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType());
2335 llvm::Type *Ty = OrigTy;
2336 if (OrigTy->isPointerTy())
2337 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2338 llvm::Type *Types[] = { Ty };
2339
2340 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
2341 llvm::Value *Call = Builder.CreateCall(
2342 F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
2343 if (OrigTy->isPointerTy())
2344 Call = Builder.CreateIntToPtr(Call, OrigTy);
2345 return RValue::get(Call);
2346}
2347
2348/// EmitStoreThroughLValue - Store the specified rvalue into the specified
2349/// lvalue, where both are guaranteed to the have the same type, and that type
2350/// is 'Ty'.
2352 bool isInit) {
2353 if (!Dst.isSimple()) {
2354 if (Dst.isVectorElt()) {
2355 // Read/modify/write the vector, inserting the new element.
2356 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(),
2357 Dst.isVolatileQualified());
2358 auto *IRStoreTy = dyn_cast<llvm::IntegerType>(Vec->getType());
2359 if (IRStoreTy) {
2360 auto *IRVecTy = llvm::FixedVectorType::get(
2361 Builder.getInt1Ty(), IRStoreTy->getPrimitiveSizeInBits());
2362 Vec = Builder.CreateBitCast(Vec, IRVecTy);
2363 // iN --> <N x i1>.
2364 }
2365 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
2366 Dst.getVectorIdx(), "vecins");
2367 if (IRStoreTy) {
2368 // <N x i1> --> <iN>.
2369 Vec = Builder.CreateBitCast(Vec, IRStoreTy);
2370 }
2372 Dst.isVolatileQualified());
2373 return;
2374 }
2375
2376 // If this is an update of extended vector elements, insert them as
2377 // appropriate.
2378 if (Dst.isExtVectorElt())
2380
2381 if (Dst.isGlobalReg())
2382 return EmitStoreThroughGlobalRegLValue(Src, Dst);
2383
2384 if (Dst.isMatrixElt()) {
2385 llvm::Value *Idx = Dst.getMatrixIdx();
2386 if (CGM.getCodeGenOpts().OptimizationLevel > 0) {
2387 const auto *const MatTy = Dst.getType()->castAs<ConstantMatrixType>();
2388 llvm::MatrixBuilder MB(Builder);
2389 MB.CreateIndexAssumption(Idx, MatTy->getNumElementsFlattened());
2390 }
2391 llvm::Instruction *Load = Builder.CreateLoad(Dst.getMatrixAddress());
2392 llvm::Value *Vec =
2393 Builder.CreateInsertElement(Load, Src.getScalarVal(), Idx, "matins");
2395 Dst.isVolatileQualified());
2396 return;
2397 }
2398
2399 assert(Dst.isBitField() && "Unknown LValue type");
2400 return EmitStoreThroughBitfieldLValue(Src, Dst);
2401 }
2402
2403 // There's special magic for assigning into an ARC-qualified l-value.
2404 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
2405 switch (Lifetime) {
2407 llvm_unreachable("present but none");
2408
2410 // nothing special
2411 break;
2412
2414 if (isInit) {
2415 Src = RValue::get(EmitARCRetain(Dst.getType(), Src.getScalarVal()));
2416 break;
2417 }
2418 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
2419 return;
2420
2422 if (isInit)
2423 // Initialize and then skip the primitive store.
2424 EmitARCInitWeak(Dst.getAddress(*this), Src.getScalarVal());
2425 else
2426 EmitARCStoreWeak(Dst.getAddress(*this), Src.getScalarVal(),
2427 /*ignore*/ true);
2428 return;
2429
2432 Src.getScalarVal()));
2433 // fall into the normal path
2434 break;
2435 }
2436 }
2437
2438 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
2439 // load of a __weak object.
2440 Address LvalueDst = Dst.getAddress(*this);
2441 llvm::Value *src = Src.getScalarVal();
2442 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
2443 return;
2444 }
2445
2446 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
2447 // load of a __strong object.
2448 Address LvalueDst = Dst.getAddress(*this);
2449 llvm::Value *src = Src.getScalarVal();
2450 if (Dst.isObjCIvar()) {
2451 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
2452 llvm::Type *ResultType = IntPtrTy;
2454 llvm::Value *RHS = dst.getPointer();
2455 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
2456 llvm::Value *LHS =
2457 Builder.CreatePtrToInt(LvalueDst.getPointer(), ResultType,
2458 "sub.ptr.lhs.cast");
2459 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
2460 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
2461 BytesBetween);
2462 } else if (Dst.isGlobalObjCRef()) {
2463 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
2464 Dst.isThreadLocalRef());
2465 }
2466 else
2467 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
2468 return;
2469 }
2470
2471 assert(Src.isScalar() && "Can't emit an agg store with this method");
2472 EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
2473}
2474
2476 llvm::Value **Result) {
2477 const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
2478 llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
2479 Address Ptr = Dst.getBitFieldAddress();
2480
2481 // Get the source value, truncated to the width of the bit-field.
2482 llvm::Value *SrcVal = Src.getScalarVal();
2483
2484 // Cast the source to the storage type and shift it into place.
2485 SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(),
2486 /*isSigned=*/false);
2487 llvm::Value *MaskedVal = SrcVal;
2488
2489 const bool UseVolatile =
2490 CGM.getCodeGenOpts().AAPCSBitfieldWidth && Dst.isVolatileQualified() &&
2491 Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
2492 const unsigned StorageSize =
2493 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
2494 const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
2495 // See if there are other bits in the bitfield's storage we'll need to load
2496 // and mask together with source before storing.
2497 if (StorageSize != Info.Size) {
2498 assert(StorageSize > Info.Size && "Invalid bitfield size.");
2499 llvm::Value *Val =
2500 Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
2501
2502 // Mask the source value as needed.
2504 SrcVal = Builder.CreateAnd(
2505 SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
2506 "bf.value");
2507 MaskedVal = SrcVal;
2508 if (Offset)
2509 SrcVal = Builder.CreateShl(SrcVal, Offset, "bf.shl");
2510
2511 // Mask out the original value.
2512 Val = Builder.CreateAnd(
2513 Val, ~llvm::APInt::getBitsSet(StorageSize, Offset, Offset + Info.Size),
2514 "bf.clear");
2515
2516 // Or together the unchanged values and the source value.
2517 SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
2518 } else {
2519 assert(Offset == 0);
2520 // According to the AACPS:
2521 // When a volatile bit-field is written, and its container does not overlap
2522 // with any non-bit-field member, its container must be read exactly once
2523 // and written exactly once using the access width appropriate to the type
2524 // of the container. The two accesses are not atomic.
2525 if (Dst.isVolatileQualified() && isAAPCS(CGM.getTarget()) &&
2526 CGM.getCodeGenOpts().ForceAAPCSBitfieldLoad)
2527 Builder.CreateLoad(Ptr, true, "bf.load");
2528 }
2529
2530 // Write the new value back out.
2531 Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified());
2532
2533 // Return the new value of the bit-field, if requested.
2534 if (Result) {
2535 llvm::Value *ResultVal = MaskedVal;
2536
2537 // Sign extend the value if needed.
2538 if (Info.IsSigned) {
2539 assert(Info.Size <= StorageSize);
2540 unsigned HighBits = StorageSize - Info.Size;
2541 if (HighBits) {
2542 ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
2543 ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
2544 }
2545 }
2546
2547 ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
2548 "bf.result.cast");
2549 *Result = EmitFromMemory(ResultVal, Dst.getType());
2550 }
2551}
2552
2554 LValue Dst) {
2555 // HLSL allows storing to scalar values through ExtVector component LValues.
2556 // To support this we need to handle the case where the destination address is
2557 // a scalar.
2558 Address DstAddr = Dst.getExtVectorAddress();
2559 if (!DstAddr.getElementType()->isVectorTy()) {
2560 assert(!Dst.getType()->isVectorType() &&
2561 "this should only occur for non-vector l-values");
2562 Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified());
2563 return;
2564 }
2565
2566 // This access turns into a read/modify/write of the vector. Load the input
2567 // value now.
2568 llvm::Value *Vec = Builder.CreateLoad(DstAddr, Dst.isVolatileQualified());
2569 const llvm::Constant *Elts = Dst.getExtVectorElts();
2570
2571 llvm::Value *SrcVal = Src.getScalarVal();
2572
2573 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
2574 unsigned NumSrcElts = VTy->getNumElements();
2575 unsigned NumDstElts =
2576 cast<llvm::FixedVectorType>(Vec->getType())->getNumElements();
2577 if (NumDstElts == NumSrcElts) {
2578 // Use shuffle vector is the src and destination are the same number of
2579 // elements and restore the vector mask since it is on the side it will be
2580 // stored.
2581 SmallVector<int, 4> Mask(NumDstElts);
2582 for (unsigned i = 0; i != NumSrcElts; ++i)
2583 Mask[getAccessedFieldNo(i, Elts)] = i;
2584
2585 Vec = Builder.CreateShuffleVector(SrcVal, Mask);
2586 } else if (NumDstElts > NumSrcElts) {
2587 // Extended the source vector to the same length and then shuffle it
2588 // into the destination.
2589 // FIXME: since we're shuffling with undef, can we just use the indices
2590 // into that? This could be simpler.
2591 SmallVector<int, 4> ExtMask;
2592 for (unsigned i = 0; i != NumSrcElts; ++i)
2593 ExtMask.push_back(i);
2594 ExtMask.resize(NumDstElts, -1);
2595 llvm::Value *ExtSrcVal = Builder.CreateShuffleVector(SrcVal, ExtMask);
2596 // build identity
2598 for (unsigned i = 0; i != NumDstElts; ++i)
2599 Mask.push_back(i);
2600
2601 // When the vector size is odd and .odd or .hi is used, the last element
2602 // of the Elts constant array will be one past the size of the vector.
2603 // Ignore the last element here, if it is greater than the mask size.
2604 if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
2605 NumSrcElts--;
2606
2607 // modify when what gets shuffled in
2608 for (unsigned i = 0; i != NumSrcElts; ++i)
2609 Mask[getAccessedFieldNo(i, Elts)] = i + NumDstElts;
2610 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, Mask);
2611 } else {
2612 // We should never shorten the vector
2613 llvm_unreachable("unexpected shorten vector length");
2614 }
2615 } else {
2616 // If the Src is a scalar (not a vector), and the target is a vector it must
2617 // be updating one element.
2618 unsigned InIdx = getAccessedFieldNo(0, Elts);
2619 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
2620 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
2621 }
2622
2624 Dst.isVolatileQualified());
2625}
2626
2627/// Store of global named registers are always calls to intrinsics.
2629 assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) &&
2630 "Bad type for register variable");
2631 llvm::MDNode *RegName = cast<llvm::MDNode>(
2632 cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata());
2633 assert(RegName && "Register LValue is not metadata");
2634
2635 // We accept integer and pointer types only
2636 llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType());
2637 llvm::Type *Ty = OrigTy;
2638 if (OrigTy->isPointerTy())
2639 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
2640 llvm::Type *Types[] = { Ty };
2641
2642 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
2643 llvm::Value *Value = Src.getScalarVal();
2644 if (OrigTy->isPointerTy())
2645 Value = Builder.CreatePtrToInt(Value, Ty);
2646 Builder.CreateCall(
2647 F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value});
2648}
2649
2650// setObjCGCLValueClass - sets class of the lvalue for the purpose of
2651// generating write-barries API. It is currently a global, ivar,
2652// or neither.
2653static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
2654 LValue &LV,
2655 bool IsMemberAccess=false) {
2656 if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
2657 return;
2658
2659 if (isa<ObjCIvarRefExpr>(E)) {
2660 QualType ExpTy = E->getType();
2661 if (IsMemberAccess && ExpTy->isPointerType()) {
2662 // If ivar is a structure pointer, assigning to field of
2663 // this struct follows gcc's behavior and makes it a non-ivar
2664 // writer-barrier conservatively.
2665 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2666 if (ExpTy->isRecordType()) {
2667 LV.setObjCIvar(false);
2668 return;
2669 }
2670 }
2671 LV.setObjCIvar(true);
2672 auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E));
2673 LV.setBaseIvarExp(Exp->getBase());
2674 LV.setObjCArray(E->getType()->isArrayType());
2675 return;
2676 }
2677
2678 if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
2679 if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
2680 if (VD->hasGlobalStorage()) {
2681 LV.setGlobalObjCRef(true);
2682 LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
2683 }
2684 }
2685 LV.setObjCArray(E->getType()->isArrayType());
2686 return;
2687 }
2688
2689 if (const auto *Exp = dyn_cast<UnaryOperator>(E)) {
2690 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2691 return;
2692 }
2693
2694 if (const auto *Exp = dyn_cast<ParenExpr>(E)) {
2695 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2696 if (LV.isObjCIvar()) {
2697 // If cast is to a structure pointer, follow gcc's behavior and make it
2698 // a non-ivar write-barrier.
2699 QualType ExpTy = E->getType();
2700 if (ExpTy->isPointerType())
2701 ExpTy = ExpTy->castAs<PointerType>()->getPointeeType();
2702 if (ExpTy->isRecordType())
2703 LV.setObjCIvar(false);
2704 }
2705 return;
2706 }
2707
2708 if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
2709 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
2710 return;
2711 }
2712
2713 if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
2714 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2715 return;
2716 }
2717
2718 if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
2719 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2720 return;
2721 }
2722
2723 if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
2724 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
2725 return;
2726 }
2727
2728 if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
2729 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
2730 if (LV.isObjCIvar() && !LV.isObjCArray())
2731 // Using array syntax to assigning to what an ivar points to is not
2732 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
2733 LV.setObjCIvar(false);
2734 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
2735 // Using array syntax to assigning to what global points to is not
2736 // same as assigning to the global itself. {id *G;} G[i] = 0;
2737 LV.setGlobalObjCRef(false);
2738 return;
2739 }
2740
2741 if (const auto *Exp = dyn_cast<MemberExpr>(E)) {
2742 setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
2743 // We don't know if member is an 'ivar', but this flag is looked at
2744 // only in the context of LV.isObjCIvar().
2745 LV.setObjCArray(E->getType()->isArrayType());
2746 return;
2747 }
2748}
2749
2751 CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr,
2752 llvm::Type *RealVarTy, SourceLocation Loc) {
2753 if (CGF.CGM.getLangOpts().OpenMPIRBuilder)
2755 CGF, VD, Addr, Loc);
2756 else
2757 Addr =
2758 CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc);
2759
2760 Addr = Addr.withElementType(RealVarTy);
2761 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2762}
2763
2765 const VarDecl *VD, QualType T) {
2766 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2767 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2768 // Return an invalid address if variable is MT_To (or MT_Enter starting with
2769 // OpenMP 5.2) and unified memory is not enabled. For all other cases: MT_Link
2770 // and MT_To (or MT_Enter) with unified memory, return a valid address.
2771 if (!Res || ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
2772 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
2774 return Address::invalid();
2775 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
2776 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
2777 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
2779 "Expected link clause OR to clause with unified memory enabled.");
2780 QualType PtrTy = CGF.getContext().getPointerType(VD->getType());
2782 return CGF.EmitLoadOfPointer(Addr, PtrTy->castAs<PointerType>());
2783}
2784
2785Address
2787 LValueBaseInfo *PointeeBaseInfo,
2788 TBAAAccessInfo *PointeeTBAAInfo) {
2789 llvm::LoadInst *Load =
2790 Builder.CreateLoad(RefLVal.getAddress(*this), RefLVal.isVolatile());
2792
2793 QualType PointeeType = RefLVal.getType()->getPointeeType();
2795 PointeeType, PointeeBaseInfo, PointeeTBAAInfo,
2796 /* forPointeeType= */ true);
2797 return Address(Load, ConvertTypeForMem(PointeeType), Align);
2798}
2799
2801 LValueBaseInfo PointeeBaseInfo;
2802 TBAAAccessInfo PointeeTBAAInfo;
2803 Address PointeeAddr = EmitLoadOfReference(RefLVal, &PointeeBaseInfo,
2804 &PointeeTBAAInfo);
2805 return MakeAddrLValue(PointeeAddr, RefLVal.getType()->getPointeeType(),
2806 PointeeBaseInfo, PointeeTBAAInfo);
2807}
2808
2810 const PointerType *PtrTy,
2811 LValueBaseInfo *BaseInfo,
2812 TBAAAccessInfo *TBAAInfo) {
2813 llvm::Value *Addr = Builder.CreateLoad(Ptr);
2814 return Address(Addr, ConvertTypeForMem(PtrTy->getPointeeType()),
2815 CGM.getNaturalTypeAlignment(PtrTy->getPointeeType(), BaseInfo,
2816 TBAAInfo,
2817 /*forPointeeType=*/true));
2818}
2819
2821 const PointerType *PtrTy) {
2822 LValueBaseInfo BaseInfo;
2823 TBAAAccessInfo TBAAInfo;
2824 Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &BaseInfo, &TBAAInfo);
2825 return MakeAddrLValue(Addr, PtrTy->getPointeeType(), BaseInfo, TBAAInfo);
2826}
2827
2829 const Expr *E, const VarDecl *VD) {
2830 QualType T = E->getType();
2831
2832 // If it's thread_local, emit a call to its wrapper function instead.
2833 if (VD->getTLSKind() == VarDecl::TLS_Dynamic &&
2835 return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T);
2836 // Check if the variable is marked as declare target with link clause in
2837 // device codegen.
2838 if (CGF.getLangOpts().OpenMPIsTargetDevice) {
2839 Address Addr = emitDeclTargetVarDeclLValue(CGF, VD, T);
2840 if (Addr.isValid())
2841 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl);
2842 }
2843
2844 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
2845
2846 if (VD->getTLSKind() != VarDecl::TLS_None)
2847 V = CGF.Builder.CreateThreadLocalAddress(V);
2848
2849 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
2850 CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
2851 Address Addr(V, RealVarTy, Alignment);
2852 // Emit reference to the private copy of the variable if it is an OpenMP
2853 // threadprivate variable.
2854 if (CGF.getLangOpts().OpenMP && !CGF.getLangOpts().OpenMPSimd &&
2855 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
2856 return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy,
2857 E->getExprLoc());
2858 }
2859 LValue LV = VD->getType()->isReferenceType() ?
2860 CGF.EmitLoadOfReferenceLValue(Addr, VD->getType(),
2863 setObjCGCLValueClass(CGF.getContext(), E, LV);
2864 return LV;
2865}
2866
2867static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM,
2868 GlobalDecl GD) {
2869 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2870 if (FD->hasAttr<WeakRefAttr>()) {
2871 ConstantAddress aliasee = CGM.GetWeakRefReference(FD);
2872 return aliasee.getPointer();
2873 }
2874
2875 llvm::Constant *V = CGM.GetAddrOfFunction(GD);
2876 return V;
2877}
2878
2880 GlobalDecl GD) {
2881 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2882 llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, GD);
2883 CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
2884 return CGF.MakeAddrLValue(V, E->getType(), Alignment,
2886}
2887
2889 llvm::Value *ThisValue) {
2890
2891 return CGF.EmitLValueForLambdaField(FD, ThisValue);
2892}
2893
2894/// Named Registers are named metadata pointing to the register name
2895/// which will be read from/written to as an argument to the intrinsic
2896/// @llvm.read/write_register.
2897/// So far, only the name is being passed down, but other options such as
2898/// register type, allocation type or even optimization options could be
2899/// passed down via the metadata node.
2901 SmallString<64> Name("llvm.named.register.");
2902 AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
2903 assert(Asm->getLabel().size() < 64-Name.size() &&
2904 "Register name too big");
2905 Name.append(Asm->getLabel());
2906 llvm::NamedMDNode *M =
2907 CGM.getModule().getOrInsertNamedMetadata(Name);
2908 if (M->getNumOperands() == 0) {
2909 llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(),
2910 Asm->getLabel());
2911 llvm::Metadata *Ops[] = {Str};
2912 M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
2913 }
2914
2915 CharUnits Alignment = CGM.getContext().getDeclAlign(VD);
2916
2917 llvm::Value *Ptr =
2918 llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0));
2919 return LValue::MakeGlobalReg(Ptr, Alignment, VD->getType());
2920}
2921
2922/// Determine whether we can emit a reference to \p VD from the current
2923/// context, despite not necessarily having seen an odr-use of the variable in
2924/// this context.
2926 const DeclRefExpr *E,
2927 const VarDecl *VD) {
2928 // For a variable declared in an enclosing scope, do not emit a spurious
2929 // reference even if we have a capture, as that will emit an unwarranted
2930 // reference to our capture state, and will likely generate worse code than
2931 // emitting a local copy.
2933 return false;
2934
2935 // For a local declaration declared in this function, we can always reference
2936 // it even if we don't have an odr-use.
2937 if (VD->hasLocalStorage()) {
2938 return VD->getDeclContext() ==
2939 dyn_cast_or_null<DeclContext>(CGF.CurCodeDecl);
2940 }
2941
2942 // For a global declaration, we can emit a reference to it if we know
2943 // for sure that we are able to emit a definition of it.
2944 VD = VD->getDefinition(CGF.getContext());
2945 if (!VD)
2946 return false;
2947
2948 // Don't emit a spurious reference if it might be to a variable that only
2949 // exists on a different device / target.
2950 // FIXME: This is unnecessarily broad. Check whether this would actually be a
2951 // cross-target reference.
2952 if (CGF.getLangOpts().OpenMP || CGF.getLangOpts().CUDA ||
2953 CGF.getLangOpts().OpenCL) {
2954 return false;
2955 }
2956
2957 // We can emit a spurious reference only if the linkage implies that we'll
2958 // be emitting a non-interposable symbol that will be retained until link
2959 // time.
2960 switch (CGF.CGM.getLLVMLinkageVarDefinition(VD)) {
2961 case llvm::GlobalValue::ExternalLinkage:
2962 case llvm::GlobalValue::LinkOnceODRLinkage:
2963 case llvm::GlobalValue::WeakODRLinkage:
2964 case llvm::GlobalValue::InternalLinkage:
2965 case llvm::GlobalValue::PrivateLinkage:
2966 return true;
2967 default:
2968 return false;
2969 }
2970}
2971
2973 const NamedDecl *ND = E->getDecl();
2974 QualType T = E->getType();
2975
2976 assert(E->isNonOdrUse() != NOUR_Unevaluated &&
2977 "should not emit an unevaluated operand");
2978
2979 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
2980 // Global Named registers access via intrinsics only
2981 if (VD->getStorageClass() == SC_Register &&
2982 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
2983 return EmitGlobalNamedRegister(VD, CGM);
2984
2985 // If this DeclRefExpr does not constitute an odr-use of the variable,
2986 // we're not permitted to emit a reference to it in general, and it might
2987 // not be captured if capture would be necessary for a use. Emit the
2988 // constant value directly instead.
2989 if (E->isNonOdrUse() == NOUR_Constant &&
2990 (VD->getType()->isReferenceType() ||
2991 !canEmitSpuriousReferenceToVariable(*this, E, VD))) {
2992 VD->getAnyInitializer(VD);
2993 llvm::Constant *Val = ConstantEmitter(*this).emitAbstract(
2994 E->getLocation(), *VD->evaluateValue(), VD->getType());
2995 assert(Val && "failed to emit constant expression");
2996
2997 Address Addr = Address::invalid();
2998 if (!VD->getType()->isReferenceType()) {
2999 // Spill the constant value to a global.
3000 Addr = CGM.createUnnamedGlobalFrom(*VD, Val,
3001 getContext().getDeclAlign(VD));
3002 llvm::Type *VarTy = getTypes().ConvertTypeForMem(VD->getType());
3003 auto *PTy = llvm::PointerType::get(
3004 VarTy, getTypes().getTargetAddressSpace(VD->getType()));
3005 Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PTy, VarTy);
3006 } else {
3007 // Should we be using the alignment of the constant pointer we emitted?
3008 CharUnits Alignment =
3010 /* BaseInfo= */ nullptr,
3011 /* TBAAInfo= */ nullptr,
3012 /* forPointeeType= */ true);
3013 Addr = Address(Val, ConvertTypeForMem(E->getType()), Alignment);
3014 }
3015 return MakeAddrLValue(Addr, T, AlignmentSource::Decl);
3016 }
3017
3018 // FIXME: Handle other kinds of non-odr-use DeclRefExprs.
3019
3020 // Check for captured variables.
3022 VD = VD->getCanonicalDecl();
3023 if (auto *FD = LambdaCaptureFields.lookup(VD))
3024 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
3025 if (CapturedStmtInfo) {
3026 auto I = LocalDeclMap.find(VD);
3027 if (I != LocalDeclMap.end()) {
3028 LValue CapLVal;
3029 if (VD->getType()->isReferenceType())
3030 CapLVal = EmitLoadOfReferenceLValue(I->second, VD->getType(),
3032 else
3033 CapLVal = MakeAddrLValue(I->second, T);
3034 // Mark lvalue as nontemporal if the variable is marked as nontemporal
3035 // in simd context.
3036 if (getLangOpts().OpenMP &&
3038 CapLVal.setNontemporal(/*Value=*/true);
3039 return CapLVal;
3040 }
3041 LValue CapLVal =
3044 Address LValueAddress = CapLVal.getAddress(*this);
3045 CapLVal = MakeAddrLValue(
3046 Address(LValueAddress.getPointer(), LValueAddress.getElementType(),
3047 getContext().getDeclAlign(VD)),
3049 CapLVal.getTBAAInfo());
3050 // Mark lvalue as nontemporal if the variable is marked as nontemporal
3051 // in simd context.
3052 if (getLangOpts().OpenMP &&
3054 CapLVal.setNontemporal(/*Value=*/true);
3055 return CapLVal;
3056 }
3057
3058 assert(isa<BlockDecl>(CurCodeDecl));
3059 Address addr = GetAddrOfBlockDecl(VD);
3060 return MakeAddrLValue(addr, T, AlignmentSource::Decl);
3061 }
3062 }
3063
3064 // FIXME: We should be able to assert this for FunctionDecls as well!
3065 // FIXME: We should be able to assert this for all DeclRefExprs, not just
3066 // those with a valid source location.
3067 assert((ND->isUsed(false) || !isa<VarDecl>(ND) || E->isNonOdrUse() ||
3068 !E->getLocation().isValid()) &&
3069 "Should not use decl without marking it used!");
3070
3071 if (ND->hasAttr<WeakRefAttr>()) {
3072 const auto *VD = cast<ValueDecl>(ND);
3074 return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl);
3075 }
3076
3077 if (const auto *VD = dyn_cast<VarDecl>(ND)) {
3078 // Check if this is a global variable.
3079 if (VD->hasLinkage() || VD->isStaticDataMember())
3080 return EmitGlobalVarDeclLValue(*this, E, VD);
3081
3082 Address addr = Address::invalid();
3083
3084 // The variable should generally be present in the local decl map.
3085 auto iter = LocalDeclMap.find(VD);
3086 if (iter != LocalDeclMap.end()) {
3087 addr = iter->second;
3088
3089 // Otherwise, it might be static local we haven't emitted yet for
3090 // some reason; most likely, because it's in an outer function.
3091 } else if (VD->isStaticLocal()) {
3092 llvm::Constant *var = CGM.getOrCreateStaticVarDecl(
3094 addr = Address(
3095 var, ConvertTypeForMem(VD->getType()), getContext().getDeclAlign(VD));
3096
3097 // No other cases for now.
3098 } else {
3099 llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?");
3100 }
3101
3102 // Handle threadlocal function locals.
3103 if (VD->getTLSKind() != VarDecl::TLS_None)
3104 addr = addr.withPointer(
3105 Builder.CreateThreadLocalAddress(addr.getPointer()), NotKnownNonNull);
3106
3107 // Check for OpenMP threadprivate variables.
3108 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
3109 VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
3111 *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()),
3112 E->getExprLoc());
3113 }
3114
3115 // Drill into block byref variables.
3116 bool isBlockByref = VD->isEscapingByref();
3117 if (isBlockByref) {
3118 addr = emitBlockByrefAddress(addr, VD);
3119 }
3120
3121 // Drill into reference types.
3122 LValue LV = VD->getType()->isReferenceType() ?
3123 EmitLoadOfReferenceLValue(addr, VD->getType(), AlignmentSource::Decl) :
3125
3126 bool isLocalStorage = VD->hasLocalStorage();
3127
3128 bool NonGCable = isLocalStorage &&
3129 !VD->getType()->isReferenceType() &&
3130 !isBlockByref;
3131 if (NonGCable) {
3133 LV.setNonGC(true);
3134 }
3135
3136 bool isImpreciseLifetime =
3137 (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
3138 if (isImpreciseLifetime)
3141 return LV;
3142 }
3143
3144 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
3145 LValue LV = EmitFunctionDeclLValue(*this, E, FD);
3146
3147 // Emit debuginfo for the function declaration if the target wants to.
3148 if (getContext().getTargetInfo().allowDebugInfoForExternalRef()) {
3149 if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) {
3150 auto *Fn =
3151 cast<llvm::Function>(LV.getPointer(*this)->stripPointerCasts());
3152 if (!Fn->getSubprogram())
3153 DI->EmitFunctionDecl(FD, FD->getLocation(), T, Fn);
3154 }
3155 }
3156
3157 return LV;
3158 }
3159
3160 // FIXME: While we're emitting a binding from an enclosing scope, all other
3161 // DeclRefExprs we see should be implicitly treated as if they also refer to
3162 // an enclosing scope.
3163 if (const auto *BD = dyn_cast<BindingDecl>(ND)) {
3165 auto *FD = LambdaCaptureFields.lookup(BD);
3166 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
3167 }
3168 return EmitLValue(BD->getBinding());
3169 }
3170
3171 // We can form DeclRefExprs naming GUID declarations when reconstituting
3172 // non-type template parameters into expressions.
3173 if (const auto *GD = dyn_cast<MSGuidDecl>(ND))
3176
3177 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
3178 auto ATPO = CGM.GetAddrOfTemplateParamObject(TPO);
3179 auto AS = getLangASFromTargetAS(ATPO.getAddressSpace());
3180
3181 if (AS != T.getAddressSpace()) {
3182 auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace());
3183 auto PtrTy = ATPO.getElementType()->getPointerTo(TargetAS);
3185 CGM, ATPO.getPointer(), AS, T.getAddressSpace(), PtrTy);
3186 ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment());
3187 }
3188
3189 return MakeAddrLValue(ATPO, T, AlignmentSource::Decl);
3190 }
3191
3192 llvm_unreachable("Unhandled DeclRefExpr");
3193}
3194
3196 // __extension__ doesn't affect lvalue-ness.
3197 if (E->getOpcode() == UO_Extension)
3198 return EmitLValue(E->getSubExpr());
3199
3201 switch (E->getOpcode()) {
3202 default: llvm_unreachable("Unknown unary operator lvalue!");
3203 case UO_Deref: {
3205 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
3206
3207 LValueBaseInfo BaseInfo;
3208 TBAAAccessInfo TBAAInfo;
3209 Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo,
3210 &TBAAInfo);
3211 LValue LV = MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo);
3213
3214 // We should not generate __weak write barrier on indirect reference
3215 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
3216 // But, we continue to generate __strong write barrier on indirect write
3217 // into a pointer to object.
3218 if (getLangOpts().ObjC &&
3219 getLangOpts().getGC() != LangOptions::NonGC &&
3220 LV.isObjCWeak())
3222 return LV;
3223 }
3224 case UO_Real:
3225 case UO_Imag: {
3226 LValue LV = EmitLValue(E->getSubExpr());
3227 assert(LV.isSimple() && "real/imag on non-ordinary l-value");
3228
3229 // __real is valid on scalars. This is a faster way of testing that.
3230 // __imag can only produce an rvalue on scalars.
3231 if (E->getOpcode() == UO_Real &&
3232 !LV.getAddress(*this).getElementType()->isStructTy()) {
3233 assert(E->getSubExpr()->getType()->isArithmeticType());
3234 return LV;
3235 }
3236
3237 QualType T = ExprTy->castAs<ComplexType>()->getElementType();
3238
3239 Address Component =
3240 (E->getOpcode() == UO_Real
3241 ? emitAddrOfRealComponent(LV.getAddress(*this), LV.getType())
3242 : emitAddrOfImagComponent(LV.getAddress(*this), LV.getType()));
3243 LValue ElemLV = MakeAddrLValue(Component, T, LV.getBaseInfo(),
3245 ElemLV.getQuals().addQualifiers(LV.getQuals());
3246 return ElemLV;
3247 }
3248 case UO_PreInc:
3249 case UO_PreDec: {
3250 LValue LV = EmitLValue(E->getSubExpr());
3251 bool isInc = E->getOpcode() == UO_PreInc;
3252
3253 if (E->getType()->isAnyComplexType())
3254 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
3255 else
3256 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
3257 return LV;
3258 }
3259 }
3260}
3261
3265}
3266
3270}
3271
3273 auto SL = E->getFunctionName();
3274 assert(SL != nullptr && "No StringLiteral name in PredefinedExpr");
3275 StringRef FnName = CurFn->getName();
3276 if (FnName.starts_with("\01"))
3277 FnName = FnName.substr(1);
3278 StringRef NameItems[] = {
3280 std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
3281 if (auto *BD = dyn_cast_or_null<BlockDecl>(CurCodeDecl)) {
3282 std::string Name = std::string(SL->getString());
3283 if (!Name.empty()) {
3284 unsigned Discriminator =
3286 if (Discriminator)
3287 Name += "_" + Twine(Discriminator + 1).str();
3288 auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str());
3290 } else {
3291 auto C =
3292 CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str());
3294 }
3295 }
3296 auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
3298}
3299
3300/// Emit a type description suitable for use by a runtime sanitizer library. The
3301/// format of a type descriptor is
3302///
3303/// \code
3304/// { i16 TypeKind, i16 TypeInfo }
3305/// \endcode
3306///
3307/// followed by an array of i8 containing the type name. TypeKind is 0 for an
3308/// integer, 1 for a floating point value, and -1 for anything else.
3310 // Only emit each type's descriptor once.
3311 if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
3312 return C;
3313
3314 uint16_t TypeKind = -1;
3315 uint16_t TypeInfo = 0;
3316
3317 if (T->isIntegerType()) {
3318 TypeKind = 0;
3319 TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
3320 (T->isSignedIntegerType() ? 1 : 0);
3321 } else if (T->isFloatingType()) {
3322 TypeKind = 1;
3324 }
3325
3326 // Format the type name as if for a diagnostic, including quotes and
3327 // optionally an 'aka'.
3328 SmallString<32> Buffer;
3331 StringRef(), std::nullopt, Buffer, std::nullopt);
3332
3333 llvm::Constant *Components[] = {
3334 Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
3335 llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
3336 };
3337 llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
3338
3339 auto *GV = new llvm::GlobalVariable(
3340 CGM.getModule(), Descriptor->getType(),
3341 /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
3342 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3344
3345 // Remember the descriptor for this type.
3347
3348 return GV;
3349}
3350
3351llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
3352 llvm::Type *TargetTy = IntPtrTy;
3353
3354 if (V->getType() == TargetTy)
3355 return V;
3356
3357 // Floating-point types which fit into intptr_t are bitcast to integers
3358 // and then passed directly (after zero-extension, if necessary).
3359 if (V->getType()->isFloatingPointTy()) {
3360 unsigned Bits = V->getType()->getPrimitiveSizeInBits().getFixedValue();
3361 if (Bits <= TargetTy->getIntegerBitWidth())
3362 V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
3363 Bits));
3364 }
3365
3366 // Integers which fit in intptr_t are zero-extended and passed directly.
3367 if (V->getType()->isIntegerTy() &&
3368 V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
3369 return Builder.CreateZExt(V, TargetTy);
3370
3371 // Pointers are passed directly, everything else is passed by address.
3372 if (!V->getType()->isPointerTy()) {
3373 Address Ptr = CreateDefaultAlignTempAlloca(V->getType());
3374 Builder.CreateStore(V, Ptr);
3375 V = Ptr.getPointer();
3376 }
3377 return Builder.CreatePtrToInt(V, TargetTy);
3378}
3379
3380/// Emit a representation of a SourceLocation for passing to a handler
3381/// in a sanitizer runtime library. The format for this data is:
3382/// \code
3383/// struct SourceLocation {
3384/// const char *Filename;
3385/// int32_t Line, Column;
3386/// };
3387/// \endcode
3388/// For an invalid SourceLocation, the Filename pointer is null.
3390 llvm::Constant *Filename;
3391 int Line, Column;
3392
3394 if (PLoc.isValid()) {
3395 StringRef FilenameString = PLoc.getFilename();
3396
3397 int PathComponentsToStrip =
3398 CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip;
3399 if (PathComponentsToStrip < 0) {
3400 assert(PathComponentsToStrip != INT_MIN);
3401 int PathComponentsToKeep = -PathComponentsToStrip;
3402 auto I = llvm::sys::path::rbegin(FilenameString);
3403 auto E = llvm::sys::path::rend(FilenameString);
3404 while (I != E && --PathComponentsToKeep)
3405 ++I;
3406
3407 FilenameString = FilenameString.substr(I - E);
3408 } else if (PathComponentsToStrip > 0) {
3409 auto I = llvm::sys::path::begin(FilenameString);
3410 auto E = llvm::sys::path::end(FilenameString);
3411 while (I != E && PathComponentsToStrip--)
3412 ++I;
3413
3414 if (I != E)
3415 FilenameString =
3416 FilenameString.substr(I - llvm::sys::path::begin(FilenameString));
3417 else
3418 FilenameString = llvm::sys::path::filename(FilenameString);
3419 }
3420
3421 auto FilenameGV =
3422 CGM.GetAddrOfConstantCString(std::string(FilenameString), ".src");
3424 cast<llvm::GlobalVariable>(
3425 FilenameGV.getPointer()->stripPointerCasts()));
3426 Filename = FilenameGV.getPointer();
3427 Line = PLoc.getLine();
3428 Column = PLoc.getColumn();
3429 } else {
3430 Filename = llvm::Constant::getNullValue(Int8PtrTy);
3431 Line = Column = 0;
3432 }
3433
3434 llvm::Constant *Data[] = {Filename, Builder.getInt32(Line),
3435 Builder.getInt32(Column)};
3436
3437 return llvm::ConstantStruct::getAnon(Data);
3438}
3439
3440namespace {
3441/// Specify under what conditions this check can be recovered
3442enum class CheckRecoverableKind {
3443 /// Always terminate program execution if this check fails.
3445 /// Check supports recovering, runtime has both fatal (noreturn) and
3446 /// non-fatal handlers for this check.
3447 Recoverable,
3448 /// Runtime conditionally aborts, always need to support recovery.
3450};
3451}
3452
3453static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind) {
3454 assert(Kind.countPopulation() == 1);
3455 if (Kind == SanitizerKind::Vptr)
3456 return CheckRecoverableKind::AlwaysRecoverable;
3457 else if (Kind == SanitizerKind::Return || Kind == SanitizerKind::Unreachable)
3458 return CheckRecoverableKind::Unrecoverable;
3459 else
3460 return CheckRecoverableKind::Recoverable;
3461}
3462
3463namespace {
3464struct SanitizerHandlerInfo {
3465 char const *const Name;
3466 unsigned Version;
3467};
3468}
3469
3470const SanitizerHandlerInfo SanitizerHandlers[] = {
3471#define SANITIZER_CHECK(Enum, Name, Version) {#Name, Version},
3473#undef SANITIZER_CHECK
3474};
3475
3477 llvm::FunctionType *FnType,
3479 SanitizerHandler CheckHandler,
3480 CheckRecoverableKind RecoverKind, bool IsFatal,
3481 llvm::BasicBlock *ContBB) {
3482 assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable);
3483 std::optional<ApplyDebugLocation> DL;
3484 if (!CGF.Builder.getCurrentDebugLocation()) {
3485 // Ensure that the call has at least an artificial debug location.
3486 DL.emplace(CGF, SourceLocation());
3487 }
3488 bool NeedsAbortSuffix =
3489 IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
3490 bool MinimalRuntime = CGF.CGM.getCodeGenOpts().SanitizeMinimalRuntime;
3491 const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler];
3492 const StringRef CheckName = CheckInfo.Name;
3493 std::string FnName = "__ubsan_handle_" + CheckName.str();
3494 if (CheckInfo.Version && !MinimalRuntime)
3495 FnName += "_v" + llvm::utostr(CheckInfo.Version);
3496 if (MinimalRuntime)
3497 FnName += "_minimal";
3498 if (NeedsAbortSuffix)
3499 FnName += "_abort";
3500 bool MayReturn =
3501 !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
3502
3503 llvm::AttrBuilder B(CGF.getLLVMContext());
3504 if (!MayReturn) {
3505 B.addAttribute(llvm::Attribute::NoReturn)
3506 .addAttribute(llvm::Attribute::NoUnwind);
3507 }
3508 B.addUWTableAttr(llvm::UWTableKind::Default);
3509
3510 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(
3511 FnType, FnName,
3512 llvm::AttributeList::get(CGF.getLLVMContext(),
3513 llvm::AttributeList::FunctionIndex, B),
3514 /*Local=*/true);
3515 llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
3516 if (!MayReturn) {
3517 HandlerCall->setDoesNotReturn();
3518 CGF.Builder.CreateUnreachable();
3519 } else {
3520 CGF.Builder.CreateBr(ContBB);
3521 }
3522}
3523
3525 ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
3526 SanitizerHandler CheckHandler, ArrayRef<llvm::Constant *> StaticArgs,
3527 ArrayRef<llvm::Value *> DynamicArgs) {
3528 assert(IsSanitizerScope);
3529 assert(Checked.size() > 0);
3530 assert(CheckHandler >= 0 &&
3531 size_t(CheckHandler) < std::size(SanitizerHandlers));
3532 const StringRef CheckName = SanitizerHandlers[CheckHandler].Name;
3533
3534 llvm::Value *FatalCond = nullptr;
3535 llvm::Value *RecoverableCond = nullptr;
3536 llvm::Value *TrapCond = nullptr;
3537 for (int i = 0, n = Checked.size(); i < n; ++i) {
3538 llvm::Value *Check = Checked[i].first;
3539 // -fsanitize-trap= overrides -fsanitize-recover=.
3540 llvm::Value *&Cond =
3541 CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second)
3542 ? TrapCond
3543 : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second)
3544 ? RecoverableCond
3545 : FatalCond;
3546 Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check;
3547 }
3548
3549 if (TrapCond)
3550 EmitTrapCheck(TrapCond, CheckHandler);
3551 if (!FatalCond && !RecoverableCond)
3552 return;
3553
3554 llvm::Value *JointCond;
3555 if (FatalCond && RecoverableCond)
3556 JointCond = Builder.CreateAnd(FatalCond, RecoverableCond);
3557 else
3558 JointCond = FatalCond ? FatalCond : RecoverableCond;
3559 assert(JointCond);
3560
3561 CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second);
3562 assert(SanOpts.has(Checked[0].second));
3563#ifndef NDEBUG
3564 for (int i = 1, n = Checked.size(); i < n; ++i) {
3565 assert(RecoverKind == getRecoverableKind(Checked[i].second) &&
3566 "All recoverable kinds in a single check must be same!");
3567 assert(SanOpts.has(Checked[i].second));
3568 }
3569#endif
3570
3571 llvm::BasicBlock *Cont = createBasicBlock("cont");
3572 llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName);
3573 llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers);
3574 // Give hint that we very much don't expect to execute the handler
3575 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
3576 llvm::MDBuilder MDHelper(getLLVMContext());
3577 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
3578 Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
3579 EmitBlock(Handlers);
3580
3581 // Handler functions take an i8* pointing to the (handler-specific) static
3582 // information block, followed by a sequence of intptr_t arguments
3583 // representing operand values.
3586 if (!CGM.getCodeGenOpts().SanitizeMinimalRuntime) {
3587 Args.reserve(DynamicArgs.size() + 1);
3588 ArgTypes.reserve(DynamicArgs.size() + 1);
3589
3590 // Emit handler arguments and create handler function type.
3591 if (!StaticArgs.empty()) {
3592 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3593 auto *InfoPtr = new llvm::GlobalVariable(
3594 CGM.getModule(), Info->getType(), false,
3595 llvm::GlobalVariable::PrivateLinkage, Info, "", nullptr,
3596 llvm::GlobalVariable::NotThreadLocal,
3597 CGM.getDataLayout().getDefaultGlobalsAddressSpace());
3598 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3600 Args.push_back(InfoPtr);
3601 ArgTypes.push_back(Args.back()->getType());
3602 }
3603
3604 for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
3605 Args.push_back(EmitCheckValue(DynamicArgs[i]));
3606 ArgTypes.push_back(IntPtrTy);
3607 }
3608 }
3609
3610 llvm::FunctionType *FnType =
3611 llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
3612
3613 if (!FatalCond || !RecoverableCond) {
3614 // Simple case: we need to generate a single handler call, either
3615 // fatal, or non-fatal.
3616 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind,
3617 (FatalCond != nullptr), Cont);
3618 } else {
3619 // Emit two handler calls: first one for set of unrecoverable checks,
3620 // another one for recoverable.
3621 llvm::BasicBlock *NonFatalHandlerBB =
3622 createBasicBlock("non_fatal." + CheckName);
3623 llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName);
3624 Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
3625 EmitBlock(FatalHandlerBB);
3626 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, true,
3627 NonFatalHandlerBB);
3628 EmitBlock(NonFatalHandlerBB);
3629 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, false,
3630 Cont);
3631 }
3632
3633 EmitBlock(Cont);
3634}
3635
3637 SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId,
3638 llvm::Value *Ptr, ArrayRef<llvm::Constant *> StaticArgs) {
3639 llvm::BasicBlock *Cont = createBasicBlock("cfi.cont");
3640
3641 llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath");
3642 llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB);
3643
3644 llvm::MDBuilder MDHelper(getLLVMContext());
3645 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
3646 BI->setMetadata(llvm::LLVMContext::MD_prof, Node);
3647
3648 EmitBlock(CheckBB);
3649
3650 bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind);
3651
3652 llvm::CallInst *CheckCall;
3653 llvm::FunctionCallee SlowPathFn;
3654 if (WithDiag) {
3655 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
3656 auto *InfoPtr =
3657 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
3658 llvm::GlobalVariable::PrivateLinkage, Info);
3659 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3661
3662 SlowPathFn = CGM.getModule().getOrInsertFunction(
3663 "__cfi_slowpath_diag",
3664 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy},
3665 false));
3666 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr, InfoPtr});
3667 } else {
3668 SlowPathFn = CGM.getModule().getOrInsertFunction(
3669 "__cfi_slowpath",
3670 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false));
3671 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr});
3672 }
3673
3675 cast<llvm::GlobalValue>(SlowPathFn.getCallee()->stripPointerCasts()));
3676 CheckCall->setDoesNotThrow();
3677
3678 EmitBlock(Cont);
3679}
3680
3681// Emit a stub for __cfi_check function so that the linker knows about this
3682// symbol in LTO mode.
3684 llvm::Module *M = &CGM.getModule();
3685 auto &Ctx = M->getContext();
3686 llvm::Function *F = llvm::Function::Create(
3687 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false),
3688 llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
3689 F->setAlignment(llvm::Align(4096));
3690 CGM.setDSOLocal(F);
3691 llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
3692 // CrossDSOCFI pass is not executed if there is no executable code.
3693 SmallVector<llvm::Value*> Args{F->getArg(2), F->getArg(1)};
3694 llvm::CallInst::Create(M->getFunction("__cfi_check_fail"), Args, "", BB);
3695 llvm::ReturnInst::Create(Ctx, nullptr, BB);
3696}
3697
3698// This function is basically a switch over the CFI failure kind, which is
3699// extracted from CFICheckFailData (1st function argument). Each case is either
3700// llvm.trap or a call to one of the two runtime handlers, based on
3701// -fsanitize-trap and -fsanitize-recover settings. Default case (invalid
3702// failure kind) traps, but this should really never happen. CFICheckFailData
3703// can be nullptr if the calling module has -fsanitize-trap behavior for this
3704// check kind; in this case __cfi_check_fail traps as well.
3706 SanitizerScope SanScope(this);
3707 FunctionArgList Args;
3712 Args.push_back(&ArgData);
3713 Args.push_back(&ArgAddr);
3714
3715 const CGFunctionInfo &FI =
3717
3718 llvm::Function *F = llvm::Function::Create(
3719 llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false),
3720 llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule());
3721
3722 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
3724 F->setVisibility(llvm::GlobalValue::HiddenVisibility);
3725
3726 StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args,
3727 SourceLocation());
3728
3729 // This function is not affected by NoSanitizeList. This function does
3730 // not have a source location, but "src:*" would still apply. Revert any
3731 // changes to SanOpts made in StartFunction.
3733
3734 llvm::Value *Data =
3735 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false,
3736 CGM.getContext().VoidPtrTy, ArgData.getLocation());
3737 llvm::Value *Addr =
3738 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false,
3739 CGM.getContext().VoidPtrTy, ArgAddr.getLocation());
3740
3741 // Data == nullptr means the calling module has trap behaviour for this check.
3742 llvm::Value *DataIsNotNullPtr =
3743 Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy));
3744 EmitTrapCheck(DataIsNotNullPtr, SanitizerHandler::CFICheckFail);
3745
3746 llvm::StructType *SourceLocationTy =
3747 llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty);
3748 llvm::StructType *CfiCheckFailDataTy =
3749 llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy);
3750
3751 llvm::Value *V = Builder.CreateConstGEP2_32(
3752 CfiCheckFailDataTy,
3753 Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0,
3754 0);
3755
3756 Address CheckKindAddr(V, Int8Ty, getIntAlign());
3757 llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr);
3758
3759 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
3761 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
3762 llvm::Value *ValidVtable = Builder.CreateZExt(
3763 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
3764 {Addr, AllVtables}),
3765 IntPtrTy);
3766
3767 const std::pair<int, SanitizerMask> CheckKinds[] = {
3768 {CFITCK_VCall, SanitizerKind::CFIVCall},
3769 {CFITCK_NVCall, SanitizerKind::CFINVCall},
3770 {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast},
3771 {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast},
3772 {CFITCK_ICall, SanitizerKind::CFIICall}};
3773
3775 for (auto CheckKindMaskPair : CheckKinds) {
3776 int Kind = CheckKindMaskPair.first;
3777 SanitizerMask Mask = CheckKindMaskPair.second;
3778 llvm::Value *Cond =
3779 Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
3780 if (CGM.getLangOpts().Sanitize.has(Mask))
3781 EmitCheck(std::make_pair(Cond, Mask), SanitizerHandler::CFICheckFail, {},
3782 {Data, Addr, ValidVtable});
3783 else
3784 EmitTrapCheck(Cond, SanitizerHandler::CFICheckFail);
3785 }
3786
3788 // The only reference to this function will be created during LTO link.
3789 // Make sure it survives until then.
3790 CGM.addUsedGlobal(F);
3791}
3792
3794 if (SanOpts.has(SanitizerKind::Unreachable)) {
3795 SanitizerScope SanScope(this);
3796 EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()),
3797 SanitizerKind::Unreachable),
3798 SanitizerHandler::BuiltinUnreachable,
3799 EmitCheckSourceLocation(Loc), std::nullopt);
3800 }
3801 Builder.CreateUnreachable();
3802}
3803
3804void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked,
3805 SanitizerHandler CheckHandlerID) {
3806 llvm::BasicBlock *Cont = createBasicBlock("cont");
3807
3808 // If we're optimizing, collapse all calls to trap down to just one per
3809 // check-type per function to save on code size.
3810 if ((int)TrapBBs.size() <= CheckHandlerID)
3811 TrapBBs.resize(CheckHandlerID + 1);
3812
3813 llvm::BasicBlock *&TrapBB = TrapBBs[CheckHandlerID];
3814
3816 CGM.getCodeGenOpts().OptimizationLevel && TrapBB &&
3817 (!CurCodeDecl || !CurCodeDecl->hasAttr<OptimizeNoneAttr>())) {
3818 auto Call = TrapBB->begin();
3819 assert(isa<llvm::CallInst>(Call) && "Expected call in trap BB");
3820
3821 Call->applyMergedLocation(Call->getDebugLoc(),
3822 Builder.getCurrentDebugLocation());
3823 Builder.CreateCondBr(Checked, Cont, TrapBB);
3824 } else {
3825 TrapBB = createBasicBlock("trap");
3826 Builder.CreateCondBr(Checked, Cont, TrapBB);
3827 EmitBlock(TrapBB);
3828
3829 llvm::CallInst *TrapCall = Builder.CreateCall(
3830 CGM.getIntrinsic(llvm::Intrinsic::ubsantrap),
3831 llvm::ConstantInt::get(CGM.Int8Ty, ClSanitizeDebugDeoptimization
3832 ? TrapBB->getParent()->size()
3833 : CheckHandlerID));
3834
3835 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
3836 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
3838 TrapCall->addFnAttr(A);
3839 }
3840 TrapCall->setDoesNotReturn();
3841 TrapCall->setDoesNotThrow();
3842 Builder.CreateUnreachable();
3843 }
3844
3845 EmitBlock(Cont);
3846}
3847
3848llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) {
3849 llvm::CallInst *TrapCall =
3850 Builder.CreateCall(CGM.getIntrinsic(IntrID));
3851
3852 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) {
3853 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name",
3855 TrapCall->addFnAttr(A);
3856 }
3857
3858 return TrapCall;
3859}
3860
3862 LValueBaseInfo *BaseInfo,
3863 TBAAAccessInfo *TBAAInfo) {
3864 assert(E->getType()->isArrayType() &&
3865 "Array to pointer decay must have array source type!");
3866
3867 // Expressions of array type can't be bitfields or vector elements.
3868 LValue LV = EmitLValue(E);
3869 Address Addr = LV.getAddress(*this);
3870
3871 // If the array type was an incomplete type, we need to make sure
3872 // the decay ends up being the right type.
3873 llvm::Type *NewTy = ConvertType(E->getType());
3874 Addr = Addr.withElementType(NewTy);
3875
3876 // Note that VLA pointers are always decayed, so we don't need to do
3877 // anything here.
3878 if (!E->getType()->isVariableArrayType()) {
3879 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
3880 "Expected pointer to array");
3881 Addr = Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
3882 }
3883
3884 // The result of this decay conversion points to an array element within the
3885 // base lvalue. However, since TBAA currently does not support representing
3886 // accesses to elements of member arrays, we conservatively represent accesses
3887 // to the pointee object as if it had no any base lvalue specified.
3888 // TODO: Support TBAA for member arrays.
3890 if (BaseInfo) *BaseInfo = LV.getBaseInfo();
3891 if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(EltType);
3892
3893 return Addr.withElementType(ConvertTypeForMem(EltType));
3894}
3895
3896/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
3897/// array to pointer, return the array subexpression.
3898static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
3899 // If this isn't just an array->pointer decay, bail out.
3900 const auto *CE = dyn_cast<CastExpr>(E);
3901 if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
3902 return nullptr;
3903
3904 // If this is a decay from variable width array, bail out.
3905 const Expr *SubExpr = CE->getSubExpr();
3906 if (SubExpr->getType()->isVariableArrayType())
3907 return nullptr;
3908
3909 return SubExpr;
3910}
3911
3913 llvm::Type *elemType,
3914 llvm::Value *ptr,
3915 ArrayRef<llvm::Value*> indices,
3916 bool inbounds,
3917 bool signedIndices,
3918 SourceLocation loc,
3919 const llvm::Twine &name = "arrayidx") {
3920 if (inbounds) {
3921 return CGF.EmitCheckedInBoundsGEP(elemType, ptr, indices, signedIndices,
3923 name);
3924 } else {
3925 return CGF.Builder.CreateGEP(elemType, ptr, indices, name);
3926 }
3927}
3928
3930 llvm::Value *idx,
3931 CharUnits eltSize) {
3932 // If we have a constant index, we can use the exact offset of the
3933 // element we're accessing.
3934 if (auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) {
3935 CharUnits offset = constantIdx->getZExtValue() * eltSize;
3936 return arrayAlign.alignmentAtOffset(offset);
3937
3938 // Otherwise, use the worst-case alignment for any element.
3939 } else {
3940 return arrayAlign.alignmentOfArrayElement(eltSize);
3941 }
3942}
3943
3945 const VariableArrayType *vla) {
3946 QualType eltType;
3947 do {
3948 eltType = vla->getElementType();
3949 } while ((vla = ctx.getAsVariableArrayType(eltType)));
3950 return eltType;
3951}
3952
3954 return D && D->hasAttr<BPFPreserveStaticOffsetAttr>();
3955}
3956
3957static bool hasBPFPreserveStaticOffset(const Expr *E) {
3958 if (!E)
3959 return false;
3960 QualType PointeeType = E->getType()->getPointeeType();
3961 if (PointeeType.isNull())
3962 return false;
3963 if (const auto *BaseDecl = PointeeType->getAsRecordDecl())
3964 return hasBPFPreserveStaticOffset(BaseDecl);
3965 return false;
3966}
3967
3968// Wraps Addr with a call to llvm.preserve.static.offset intrinsic.
3970 Address &Addr) {
3971 if (!CGF.getTarget().getTriple().isBPF())
3972 return Addr;
3973
3974 llvm::Function *Fn =
3975 CGF.CGM.getIntrinsic(llvm::Intrinsic::preserve_static_offset);
3976 llvm::CallInst *Call = CGF.Builder.CreateCall(Fn, {Addr.getPointer()});
3977 return Address(Call, Addr.getElementType(), Addr.getAlignment());
3978}
3979
3980/// Given an array base, check whether its member access belongs to a record
3981/// with preserve_access_index attribute or not.
3982static bool IsPreserveAIArrayBase(CodeGenFunction &CGF, const Expr *ArrayBase) {
3983 if (!ArrayBase || !CGF.getDebugInfo())
3984 return false;
3985
3986 // Only support base as either a MemberExpr or DeclRefExpr.
3987 // DeclRefExpr to cover cases like:
3988 // struct s { int a; int b[10]; };
3989 // struct s *p;
3990 // p[1].a
3991 // p[1] will generate a DeclRefExpr and p[1].a is a MemberExpr.
3992 // p->b[5] is a MemberExpr example.
3993 const Expr *E = ArrayBase->IgnoreImpCasts();
3994 if (const auto *ME = dyn_cast<MemberExpr>(E))
3995 return ME->getMemberDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
3996
3997 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3998 const auto *VarDef = dyn_cast<VarDecl>(DRE->getDecl());
3999 if (!VarDef)
4000 return false;
4001
4002 const auto *PtrT = VarDef->getType()->getAs<PointerType>();
4003 if (!PtrT)
4004 return false;
4005
4006 const auto *PointeeT = PtrT->getPointeeType()
4008 if (const auto *RecT = dyn_cast<RecordType>(PointeeT))
4009 return RecT->getDecl()->hasAttr<BPFPreserveAccessIndexAttr>();
4010 return false;
4011 }
4012
4013 return false;
4014}
4015
4018 QualType eltType, bool inbounds,
4019 bool signedIndices, SourceLocation loc,
4020 QualType *arrayType = nullptr,
4021 const Expr *Base = nullptr,
4022 const llvm::Twine &name = "arrayidx") {
4023 // All the indices except that last must be zero.
4024#ifndef NDEBUG
4025 for (auto *idx : indices.drop_back())
4026 assert(isa<llvm::ConstantInt>(idx) &&
4027 cast<llvm::ConstantInt>(idx)->isZero());
4028#endif
4029
4030 // Determine the element size of the statically-sized base. This is
4031 // the thing that the indices are expressed in terms of.
4032 if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) {
4033 eltType = getFixedSizeElementType(CGF.getContext(), vla);
4034 }
4035
4036 // We can use that to compute the best alignment of the element.
4037 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
4038 CharUnits eltAlign =
4039 getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize);
4040
4042 addr = wrapWithBPFPreserveStaticOffset(CGF, addr);
4043
4044 llvm::Value *eltPtr;
4045 auto LastIndex = dyn_cast<llvm::ConstantInt>(indices.back());
4046 if (!LastIndex ||
4048 eltPtr = emitArraySubscriptGEP(
4049 CGF, addr.getElementType(), addr.getPointer(), indices, inbounds,
4050 signedIndices, loc, name);
4051 } else {
4052 // Remember the original array subscript for bpf target
4053 unsigned idx = LastIndex->getZExtValue();
4054 llvm::DIType *DbgInfo = nullptr;
4055 if (arrayType)
4056 DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(*arrayType, loc);
4057 eltPtr = CGF.Builder.CreatePreserveArrayAccessIndex(addr.getElementType(),
4058 addr.getPointer(),
4059 indices.size() - 1,
4060 idx, DbgInfo);
4061 }
4062
4063 return Address(eltPtr, CGF.ConvertTypeForMem(eltType), eltAlign);
4064}
4065
4066/// The offset of a field from the beginning of the record.
4068 const FieldDecl *FD, int64_t &Offset) {
4069 ASTContext &Ctx = CGF.getContext();
4070 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
4071 unsigned FieldNo = 0;
4072
4073 for (const Decl *D : RD->decls()) {
4074 if (const auto *Record = dyn_cast<RecordDecl>(D))
4075 if (getFieldOffsetInBits(CGF, Record, FD, Offset)) {
4076 Offset += Layout.getFieldOffset(FieldNo);
4077 return true;
4078 }
4079
4080 if (const auto *Field = dyn_cast<FieldDecl>(D))
4081 if (FD == Field) {
4082 Offset += Layout.getFieldOffset(FieldNo);
4083 return true;
4084 }
4085
4086 if (isa<FieldDecl>(D))
4087 ++FieldNo;
4088 }
4089
4090 return false;
4091}
4092
4093/// Returns the relative offset difference between \p FD1 and \p FD2.
4094/// \code
4095/// offsetof(struct foo, FD1) - offsetof(struct foo, FD2)
4096/// \endcode
4097/// Both fields must be within the same struct.
4098static std::optional<int64_t> getOffsetDifferenceInBits(CodeGenFunction &CGF,
4099 const FieldDecl *FD1,
4100 const FieldDecl *FD2) {
4101 const RecordDecl *FD1OuterRec =
4103 const RecordDecl *FD2OuterRec =
4105
4106 if (FD1OuterRec != FD2OuterRec)
4107 // Fields must be within the same RecordDecl.
4108 return std::optional<int64_t>();
4109
4110 int64_t FD1Offset = 0;
4111 if (!getFieldOffsetInBits(CGF, FD1OuterRec, FD1, FD1Offset))
4112 return std::optional<int64_t>();
4113
4114 int64_t FD2Offset = 0;
4115 if (!getFieldOffsetInBits(CGF, FD2OuterRec, FD2, FD2Offset))
4116 return std::optional<int64_t>();
4117
4118 return std::make_optional<int64_t>(FD1Offset - FD2Offset);
4119}
4120
4122 bool Accessed) {
4123 // The index must always be an integer, which is not an aggregate. Emit it
4124 // in lexical order (this complexity is, sadly, required by C++17).
4125 llvm::Value *IdxPre =
4126 (E->getLHS() == E->getIdx()) ? EmitScalarExpr(E->getIdx()) : nullptr;
4127 bool SignedIndices = false;
4128 auto EmitIdxAfterBase = [&, IdxPre](bool Promote) -> llvm::Value * {
4129 auto *Idx = IdxPre;
4130 if (E->getLHS() != E->getIdx()) {
4131 assert(E->getRHS() == E->getIdx() && "index was neither LHS nor RHS");
4132 Idx = EmitScalarExpr(E->getIdx());
4133 }
4134
4135 QualType IdxTy = E->getIdx()->getType();
4136 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
4137 SignedIndices |= IdxSigned;
4138
4139 if (SanOpts.has(SanitizerKind::ArrayBounds))
4140 EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
4141
4142 // Extend or truncate the index type to 32 or 64-bits.
4143 if (Promote && Idx->getType() != IntPtrTy)
4144 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
4145
4146 return Idx;
4147 };
4148 IdxPre = nullptr;
4149
4150 // If the base is a vector type, then we are forming a vector element lvalue
4151 // with this subscript.
4152 if (E->getBase()->getType()->isVectorType() &&
4153 !isa<ExtVectorElementExpr>(E->getBase())) {
4154 // Emit the vector as an lvalue to get its address.
4155 LValue LHS = EmitLValue(E->getBase());
4156 auto *Idx = EmitIdxAfterBase(/*Promote*/false);
4157 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
4158 return LValue::MakeVectorElt(LHS.getAddress(*this), Idx,
4159 E->getBase()->getType(), LHS.getBaseInfo(),
4160 TBAAAccessInfo());
4161 }
4162
4163 // All the other cases basically behave like simple offsetting.
4164
4165 // Handle the extvector case we ignored above.
4166 if (isa<ExtVectorElementExpr>(E->getBase())) {
4167 LValue LV = EmitLValue(E->getBase());
4168 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4170
4171 QualType EltType = LV.getType()->castAs<VectorType>()->getElementType();
4172 Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true,
4173 SignedIndices, E->getExprLoc());
4174 return MakeAddrLValue(Addr, EltType, LV.getBaseInfo(),
4175 CGM.getTBAAInfoForSubobject(LV, EltType));
4176 }
4177
4178 LValueBaseInfo EltBaseInfo;
4179 TBAAAccessInfo EltTBAAInfo;
4180 Address Addr = Address::invalid();
4181 if (const VariableArrayType *vla =
4182 getContext().getAsVariableArrayType(E->getType())) {
4183 // The base must be a pointer, which is not an aggregate. Emit
4184 // it. It needs to be emitted first in case it's what captures
4185 // the VLA bounds.
4186 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4187 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4188
4189 // The element count here is the total number of non-VLA elements.
4190 llvm::Value *numElements = getVLASize(vla).NumElts;
4191
4192 // Effectively, the multiply by the VLA size is part of the GEP.
4193 // GEP indexes are signed, and scaling an index isn't permitted to
4194 // signed-overflow, so we use the same semantics for our explicit
4195 // multiply. We suppress this if overflow is not undefined behavior.
4196 if (getLangOpts().isSignedOverflowDefined()) {
4197 Idx = Builder.CreateMul(Idx, numElements);
4198 } else {
4199 Idx = Builder.CreateNSWMul(Idx, numElements);
4200 }
4201
4202 Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(),
4203 !getLangOpts().isSignedOverflowDefined(),
4204 SignedIndices, E->getExprLoc());
4205
4206 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
4207 // Indexing over an interface, as in "NSString *P; P[4];"
4208
4209 // Emit the base pointer.
4210 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4211 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4212
4213 CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT);
4214 llvm::Value *InterfaceSizeVal =
4215 llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity());
4216
4217 llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal);
4218
4219 // We don't necessarily build correct LLVM struct types for ObjC
4220 // interfaces, so we can't rely on GEP to do this scaling
4221 // correctly, so we need to cast to i8*. FIXME: is this actually
4222 // true? A lot of other things in the fragile ABI would break...
4223 llvm::Type *OrigBaseElemTy = Addr.getElementType();
4224
4225 // Do the GEP.
4226 CharUnits EltAlign =
4227 getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize);
4228 llvm::Value *EltPtr =
4229 emitArraySubscriptGEP(*this, Int8Ty, Addr.getPointer(), ScaledIdx,
4230 false, SignedIndices, E->getExprLoc());
4231 Addr = Address(EltPtr, OrigBaseElemTy, EltAlign);
4232 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
4233 // If this is A[i] where A is an array, the frontend will have decayed the
4234 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
4235 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
4236 // "gep x, i" here. Emit one "gep A, 0, i".
4237 assert(Array->getType()->isArrayType() &&
4238 "Array to pointer decay must have array source type!");
4239 LValue ArrayLV;
4240 // For simple multidimensional array indexing, set the 'accessed' flag for
4241 // better bounds-checking of the base expression.
4242 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
4243 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
4244 else
4245 ArrayLV = EmitLValue(Array);
4246 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4247
4248 if (SanOpts.has(SanitizerKind::ArrayBounds)) {
4249 // If the array being accessed has a "counted_by" attribute, generate
4250 // bounds checking code. The "count" field is at the top level of the
4251 // struct or in an anonymous struct, that's also at the top level. Future
4252 // expansions may allow the "count" to reside at any place in the struct,
4253 // but the value of "counted_by" will be a "simple" path to the count,
4254 // i.e. "a.b.count", so we shouldn't need the full force of EmitLValue or
4255 // similar to emit the correct GEP.
4256 const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
4257 getLangOpts().getStrictFlexArraysLevel();
4258
4259 if (const auto *ME = dyn_cast<MemberExpr>(Array);
4260 ME &&
4261 ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
4262 ME->getMemberDecl()->hasAttr<CountedByAttr>()) {
4263 const FieldDecl *FAMDecl = dyn_cast<FieldDecl>(ME->getMemberDecl());
4264 if (const FieldDecl *CountFD = FindCountedByField(FAMDecl)) {
4265 if (std::optional<int64_t> Diff =
4266 getOffsetDifferenceInBits(*this, CountFD, FAMDecl)) {
4267 CharUnits OffsetDiff = CGM.getContext().toCharUnitsFromBits(*Diff);
4268
4269 // Create a GEP with a byte offset between the FAM and count and
4270 // use that to load the count value.
4272 ArrayLV.getAddress(*this), Int8PtrTy, Int8Ty);
4273
4274 llvm::Type *CountTy = ConvertType(CountFD->getType());
4275 llvm::Value *Res = Builder.CreateInBoundsGEP(
4276 Int8Ty, Addr.getPointer(),
4277 Builder.getInt32(OffsetDiff.getQuantity()), ".counted_by.gep");
4278 Res = Builder.CreateAlignedLoad(CountTy, Res, getIntAlign(),
4279 ".counted_by.load");
4280
4281 // Now emit the bounds checking.
4282 EmitBoundsCheckImpl(E, Res, Idx, E->getIdx()->getType(),
4283 Array->getType(), Accessed);
4284 }
4285 }
4286 }
4287 }
4288
4289 // Propagate the alignment from the array itself to the result.
4290 QualType arrayType = Array->getType();
4291 Addr = emitArraySubscriptGEP(
4292 *this, ArrayLV.getAddress(*this), {CGM.getSize(CharUnits::Zero()), Idx},
4293 E->getType(), !getLangOpts().isSignedOverflowDefined(), SignedIndices,
4294 E->getExprLoc(), &arrayType, E->getBase());
4295 EltBaseInfo = ArrayLV.getBaseInfo();
4296 EltTBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, E->getType());
4297 } else {
4298 // The base must be a pointer; emit it with an estimate of its alignment.
4299 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
4300 auto *Idx = EmitIdxAfterBase(/*Promote*/true);
4301 QualType ptrType = E->getBase()->getType();
4302 Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(),
4303 !getLangOpts().isSignedOverflowDefined(),
4304 SignedIndices, E->getExprLoc(), &ptrType,
4305 E->getBase());
4306 }
4307
4308 LValue LV = MakeAddrLValue(Addr, E->getType(), EltBaseInfo, EltTBAAInfo);
4309
4310 if (getLangOpts().ObjC &&
4311 getLangOpts().getGC() != LangOptions::NonGC) {
4314 }
4315 return LV;
4316}
4317
4319 assert(
4320 !E->isIncomplete() &&
4321 "incomplete matrix subscript expressions should be rejected during Sema");
4322 LValue Base = EmitLValue(E->getBase());
4323 llvm::Value *RowIdx = EmitScalarExpr(E->getRowIdx());
4324 llvm::Value *ColIdx = EmitScalarExpr(E->getColumnIdx());
4325 llvm::Value *NumRows = Builder.getIntN(
4326 RowIdx->getType()->getScalarSizeInBits(),
4328 llvm::Value *FinalIdx =
4329 Builder.CreateAdd(Builder.CreateMul(ColIdx, NumRows), RowIdx);
4330 return LValue::MakeMatrixElt(
4331 MaybeConvertMatrixAddress(Base.getAddress(*this), *this), FinalIdx,
4332 E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo());
4333}
4334
4336 LValueBaseInfo &BaseInfo,
4337 TBAAAccessInfo &TBAAInfo,
4338 QualType BaseTy, QualType ElTy,
4339 bool IsLowerBound) {
4340 LValue BaseLVal;
4341 if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts())) {
4342 BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound);
4343 if (BaseTy->isArrayType()) {
4344 Address Addr = BaseLVal.getAddress(CGF);
4345 BaseInfo = BaseLVal.getBaseInfo();
4346
4347 // If the array type was an incomplete type, we need to make sure
4348 // the decay ends up being the right type.
4349 llvm::Type *NewTy = CGF.ConvertType(BaseTy);
4350 Addr = Addr.withElementType(NewTy);
4351
4352 // Note that VLA pointers are always decayed, so we don't need to do
4353 // anything here.
4354 if (!BaseTy->isVariableArrayType()) {
4355 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
4356 "Expected pointer to array");
4357 Addr = CGF.Builder.CreateConstArrayGEP(Addr, 0, "arraydecay");
4358 }
4359
4360 return Addr.withElementType(CGF.ConvertTypeForMem(ElTy));
4361 }
4362 LValueBaseInfo TypeBaseInfo;
4363 TBAAAccessInfo TypeTBAAInfo;
4364 CharUnits Align =
4365 CGF.CGM.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, &TypeTBAAInfo);
4366 BaseInfo.mergeForCast(TypeBaseInfo);
4367 TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo);
4368 return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress(CGF)),
4369 CGF.ConvertTypeForMem(ElTy), Align);
4370 }
4371 return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
4372}
4373
4375 bool IsLowerBound) {
4377 QualType ResultExprTy;
4378 if (auto *AT = getContext().getAsArrayType(BaseTy))
4379 ResultExprTy = AT->getElementType();
4380 else
4381 ResultExprTy = BaseTy->getPointeeType();
4382 llvm::Value *Idx = nullptr;
4383 if (IsLowerBound || E->getColonLocFirst().isInvalid()) {
4384 // Requesting lower bound or upper bound, but without provided length and
4385 // without ':' symbol for the default length -> length = 1.
4386 // Idx = LowerBound ?: 0;
4387 if (auto *LowerBound = E->getLowerBound()) {
4388 Idx = Builder.CreateIntCast(
4389 EmitScalarExpr(LowerBound), IntPtrTy,
4390 LowerBound->getType()->hasSignedIntegerRepresentation());
4391 } else
4392 Idx = llvm::ConstantInt::getNullValue(IntPtrTy);
4393 } else {
4394 // Try to emit length or lower bound as constant. If this is possible, 1
4395 // is subtracted from constant length or lower bound. Otherwise, emit LLVM
4396 // IR (LB + Len) - 1.
4397 auto &C = CGM.getContext();
4398 auto *Length = E->getLength();
4399 llvm::APSInt ConstLength;
4400 if (Length) {
4401 // Idx = LowerBound + Length - 1;
4402 if (std::optional<llvm::APSInt> CL = Length->getIntegerConstantExpr(C)) {
4403 ConstLength = CL->zextOrTrunc(PointerWidthInBits);
4404 Length = nullptr;
4405 }
4406 auto *LowerBound = E->getLowerBound();
4407 llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false);
4408 if (LowerBound) {
4409 if (std::optional<llvm::APSInt> LB =
4410 LowerBound->getIntegerConstantExpr(C)) {
4411 ConstLowerBound = LB->zextOrTrunc(PointerWidthInBits);
4412 LowerBound = nullptr;
4413 }
4414 }
4415 if (!Length)
4416 --ConstLength;
4417 else if (!LowerBound)
4418 --ConstLowerBound;
4419
4420 if (Length || LowerBound) {
4421 auto *LowerBoundVal =
4422 LowerBound
4423 ? Builder.CreateIntCast(
4424 EmitScalarExpr(LowerBound), IntPtrTy,
4425 LowerBound->getType()->hasSignedIntegerRepresentation())
4426 : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound);
4427 auto *LengthVal =
4428 Length
4429 ? Builder.CreateIntCast(
4430 EmitScalarExpr(Length), IntPtrTy,
4431 Length->getType()->hasSignedIntegerRepresentation())
4432 : llvm::ConstantInt::get(IntPtrTy, ConstLength);
4433 Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len",
4434 /*HasNUW=*/false,
4435 !getLangOpts().isSignedOverflowDefined());
4436 if (Length && LowerBound) {
4437 Idx = Builder.CreateSub(
4438 Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1",
4439 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined());
4440 }
4441 } else
4442 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound);
4443 } else {
4444 // Idx = ArraySize - 1;
4445 QualType ArrayTy = BaseTy->isPointerType()
4447 : BaseTy;
4448 if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) {
4449 Length = VAT->getSizeExpr();
4450 if (std::optional<llvm::APSInt> L = Length->getIntegerConstantExpr(C)) {
4451 ConstLength = *L;
4452 Length = nullptr;
4453 }
4454 } else {
4455 auto *CAT = C.getAsConstantArrayType(ArrayTy);
4456 assert(CAT && "unexpected type for array initializer");
4457 ConstLength = CAT->getSize();
4458 }
4459 if (Length) {
4460 auto *LengthVal = Builder.CreateIntCast(
4461 EmitScalarExpr(Length), IntPtrTy,
4462 Length->getType()->hasSignedIntegerRepresentation());
4463 Idx = Builder.CreateSub(
4464 LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1",
4465 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined());
4466 } else {
4467 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits);
4468 --ConstLength;
4469 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength);
4470 }
4471 }
4472 }
4473 assert(Idx);
4474
4475 Address EltPtr = Address::invalid();
4476 LValueBaseInfo BaseInfo;
4477 TBAAAccessInfo TBAAInfo;
4478 if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) {
4479 // The base must be a pointer, which is not an aggregate. Emit
4480 // it. It needs to be emitted first in case it's what captures
4481 // the VLA bounds.
4482 Address Base =
4483 emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, TBAAInfo,
4484 BaseTy, VLA->getElementType(), IsLowerBound);
4485 // The element count here is the total number of non-VLA elements.
4486 llvm::Value *NumElements = getVLASize(VLA).NumElts;
4487
4488 // Effectively, the multiply by the VLA size is part of the GEP.
4489 // GEP indexes are signed, and scaling an index isn't permitted to
4490 // signed-overflow, so we use the same semantics for our explicit
4491 // multiply. We suppress this if overflow is not undefined behavior.
4492 if (getLangOpts().isSignedOverflowDefined())
4493 Idx = Builder.CreateMul(Idx, NumElements);
4494 else
4495 Idx = Builder.CreateNSWMul(Idx, NumElements);
4496 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(),
4497 !getLangOpts().isSignedOverflowDefined(),
4498 /*signedIndices=*/false, E->getExprLoc());
4499 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
4500 // If this is A[i] where A is an array, the frontend will have decayed the
4501 // base to be a ArrayToPointerDecay implicit cast. While correct, it is
4502 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
4503 // "gep x, i" here. Emit one "gep A, 0, i".
4504 assert(Array->getType()->isArrayType() &&
4505 "Array to pointer decay must have array source type!");
4506 LValue ArrayLV;
4507 // For simple multidimensional array indexing, set the 'accessed' flag for
4508 // better bounds-checking of the base expression.
4509 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
4510 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
4511 else
4512 ArrayLV = EmitLValue(Array);
4513
4514 // Propagate the alignment from the array itself to the result.
4515 EltPtr = emitArraySubscriptGEP(
4516 *this, ArrayLV.getAddress(*this), {CGM.getSize(CharUnits::Zero()), Idx},
4517 ResultExprTy, !getLangOpts().isSignedOverflowDefined(),
4518 /*signedIndices=*/false, E->getExprLoc());
4519 BaseInfo = ArrayLV.getBaseInfo();
4520 TBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, ResultExprTy);
4521 } else {
4522 Address Base = emitOMPArraySectionBase(*this, E->getBase(), BaseInfo,
4523 TBAAInfo, BaseTy, ResultExprTy,
4524 IsLowerBound);
4525 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy,
4526 !getLangOpts().isSignedOverflowDefined(),
4527 /*signedIndices=*/false, E->getExprLoc());
4528 }
4529
4530 return MakeAddrLValue(EltPtr, ResultExprTy, BaseInfo, TBAAInfo);
4531}
4532
4535 // Emit the base vector as an l-value.
4536 LValue Base;
4537
4538 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
4539 if (E->isArrow()) {
4540 // If it is a pointer to a vector, emit the address and form an lvalue with
4541 // it.
4542 LValueBaseInfo BaseInfo;
4543 TBAAAccessInfo TBAAInfo;
4544 Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo, &TBAAInfo);
4545 const auto *PT = E->getBase()->getType()->castAs<PointerType>();
4546 Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo, TBAAInfo);
4547 Base.getQuals().removeObjCGCAttr();
4548 } else if (E->getBase()->isGLValue()) {
4549 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
4550 // emit the base as an lvalue.
4551 assert(E->getBase()->getType()->isVectorType());
4552 Base = EmitLValue(E->getBase());
4553 } else {
4554 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
4555 assert(E->getBase()->getType()->isVectorType() &&
4556 "Result must be a vector");
4557 llvm::Value *Vec = EmitScalarExpr(E->getBase());
4558
4559 // Store the vector to memory (because LValue wants an address).
4560 Address VecMem = CreateMemTemp(E->getBase()->getType());
4561 Builder.CreateStore(Vec, VecMem);
4562 Base = MakeAddrLValue(VecMem, E->getBase()->getType(),
4564 }
4565
4566 QualType type =
4567 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
4568
4569 // Encode the element access list into a vector of unsigned indices.
4571 E->getEncodedElementAccess(Indices);
4572
4573 if (Base.isSimple()) {
4574 llvm::Constant *CV =
4575 llvm::ConstantDataVector::get(getLLVMContext(), Indices);
4576 return LValue::MakeExtVectorElt(Base.getAddress(*this), CV, type,
4577 Base.getBaseInfo(), TBAAAccessInfo());
4578 }
4579 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
4580
4581 llvm::Constant *BaseElts = Base.getExtVectorElts();
4583
4584 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
4585 CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
4586 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
4587 return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type,
4588 Base.getBaseInfo(), TBAAAccessInfo());
4589}
4590
4592 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) {
4594 return EmitDeclRefLValue(DRE);
4595 }
4596
4597 Expr *BaseExpr = E->getBase();
4598 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
4599 LValue BaseLV;
4600 if (E->isArrow()) {
4601 LValueBaseInfo BaseInfo;
4602 TBAAAccessInfo TBAAInfo;
4603 Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo, &TBAAInfo);
4604 QualType PtrTy = BaseExpr->getType()->getPointeeType();
4605 SanitizerSet SkippedChecks;
4606 bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr);
4607 if (IsBaseCXXThis)
4608 SkippedChecks.set(SanitizerKind::Alignment, true);
4609 if (IsBaseCXXThis || isa<DeclRefExpr>(BaseExpr))
4610 SkippedChecks.set(SanitizerKind::Null, true);
4612 /*Alignment=*/CharUnits::Zero(), SkippedChecks);
4613 BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo, TBAAInfo);
4614 } else
4615 BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
4616
4617 NamedDecl *ND = E->getMemberDecl();
4618 if (auto *Field = dyn_cast<FieldDecl>(ND)) {
4619 LValue LV = EmitLValueForField(BaseLV, Field);
4621 if (getLangOpts().OpenMP) {
4622 // If the member was explicitly marked as nontemporal, mark it as
4623 // nontemporal. If the base lvalue is marked as nontemporal, mark access
4624 // to children as nontemporal too.
4625 if ((IsWrappedCXXThis(BaseExpr) &&
4627 BaseLV.isNontemporal())
4628 LV.setNontemporal(/*Value=*/true);
4629 }
4630 return LV;
4631 }
4632
4633 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
4634 return EmitFunctionDeclLValue(*this, E, FD);
4635
4636 llvm_unreachable("Unhandled member declaration!");
4637}
4638
4639/// Given that we are currently emitting a lambda, emit an l-value for
4640/// one of its members.
4641///
4643 llvm::Value *ThisValue) {
4644 bool HasExplicitObjectParameter = false;
4645 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(CurCodeDecl)) {
4646 HasExplicitObjectParameter = MD->isExplicitObjectMemberFunction();
4647 assert(MD->getParent()->isLambda());
4648 assert(MD->getParent() == Field->getParent());
4649 }
4650 LValue LambdaLV;
4651 if (HasExplicitObjectParameter) {
4652 const VarDecl *D = cast<CXXMethodDecl>(CurCodeDecl)->getParamDecl(0);
4653 auto It = LocalDeclMap.find(D);
4654 assert(It != LocalDeclMap.end() && "explicit parameter not loaded?");
4655 Address AddrOfExplicitObject = It->getSecond();
4656 if (D->getType()->isReferenceType())
4657 LambdaLV = EmitLoadOfReferenceLValue(AddrOfExplicitObject, D->getType(),
4659 else
4660 LambdaLV = MakeNaturalAlignAddrLValue(AddrOfExplicitObject.getPointer(),
4662 } else {
4663 QualType LambdaTagType = getContext().getTagDeclType(Field->getParent());
4664 LambdaLV = MakeNaturalAlignAddrLValue(ThisValue, LambdaTagType);
4665 }
4666 return EmitLValueForField(LambdaLV, Field);
4667}
4668
4670 return EmitLValueForLambdaField(Field, CXXABIThisValue);
4671}
4672
4673/// Get the field index in the debug info. The debug info structure/union
4674/// will ignore the unnamed bitfields.
4676 unsigned FieldIndex) {
4677 unsigned I = 0, Skipped = 0;
4678
4679 for (auto *F : Rec->getDefinition()->fields()) {
4680 if (I == FieldIndex)
4681 break;
4682 if (F->isUnnamedBitfield())
4683 Skipped++;
4684 I++;
4685 }
4686
4687 return FieldIndex - Skipped;
4688}
4689
4690/// Get the address of a zero-sized field within a record. The resulting
4691/// address doesn't necessarily have the right type.
4693 const FieldDecl *Field) {
4695 CGF.getContext().getFieldOffset(Field));
4696 if (Offset.isZero())
4697 return Base;
4698 Base = Base.withElementType(CGF.Int8Ty);
4699 return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
4700}
4701
4702/// Drill down to the storage of a field without walking into
4703/// reference types.
4704///
4705/// The resulting address doesn't necessarily have the right type.
4707 const FieldDecl *field) {
4708 if (field->isZeroSize(CGF.getContext()))
4709 return emitAddrOfZeroSizeField(CGF, base, field);
4710
4711 const RecordDecl *rec = field->getParent();
4712
4713 unsigned idx =
4714 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
4715
4716 return CGF.Builder.CreateStructGEP(base, idx, field->getName());
4717}
4718
4720 Address addr, const FieldDecl *field) {
4721 const RecordDecl *rec = field->getParent();
4722 llvm::DIType *DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(
4723 base.getType(), rec->getLocation());
4724
4725 unsigned idx =
4726 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
4727
4729 addr, idx, CGF.getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo);
4730}
4731
4732static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
4733 const auto *RD = Type.getTypePtr()->getAsCXXRecordDecl();
4734 if (!RD)
4735 return false;
4736
4737 if (RD->isDynamicClass())
4738 return true;
4739
4740 for (const auto &Base : RD->bases())
4741 if (hasAnyVptr(Base.getType(), Context))
4742 return true;
4743
4744 for (const FieldDecl *Field : RD->fields())
4745 if (hasAnyVptr(Field->getType(), Context))
4746 return true;
4747
4748 return false;
4749}
4750
4752 const FieldDecl *field) {
4753 LValueBaseInfo BaseInfo = base.getBaseInfo();
4754
4755 if (field->isBitField()) {
4756 const CGRecordLayout &RL =
4758 const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
4759 const bool UseVolatile = isAAPCS(CGM.getTarget()) &&
4760 CGM.getCodeGenOpts().AAPCSBitfieldWidth &&
4761 Info.VolatileStorageSize != 0 &&
4762 field->getType()
4765 Address Addr = base.getAddress(*this);
4766 unsigned Idx = RL.getLLVMFieldNo(field);
4767 const RecordDecl *rec = field->getParent();
4769 Addr = wrapWithBPFPreserveStaticOffset(*this, Addr);
4770 if (!UseVolatile) {
4771 if (!IsInPreservedAIRegion &&
4772 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
4773 if (Idx != 0)
4774 // For structs, we GEP to the field that the record layout suggests.
4775 Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
4776 } else {
4777 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType(
4778 getContext().getRecordType(rec), rec->getLocation());
4780 Addr, Idx, getDebugInfoFIndex(rec, field->getFieldIndex()),
4781 DbgInfo);
4782 }
4783 }
4784 const unsigned SS =
4785 UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
4786 // Get the access type.
4787 llvm::Type *FieldIntTy = llvm::Type::getIntNTy(getLLVMContext(), SS);
4788 Addr = Addr.withElementType(FieldIntTy);
4789 if (UseVolatile) {
4790 const unsigned VolatileOffset = Info.VolatileStorageOffset.getQuantity();
4791 if (VolatileOffset)
4792 Addr = Builder.CreateConstInBoundsGEP(Addr, VolatileOffset);
4793 }
4794
4795 QualType fieldType =
4796 field->getType().withCVRQualifiers(base.getVRQualifiers());
4797 // TODO: Support TBAA for bit fields.
4798 LValueBaseInfo FieldBaseInfo(BaseInfo.getAlignmentSource());
4799 return LValue::MakeBitfield(Addr, Info, fieldType, FieldBaseInfo,
4800 TBAAAccessInfo());
4801 }
4802
4803 // Fields of may-alias structures are may-alias themselves.
4804 // FIXME: this should get propagated down through anonymous structs
4805 // and unions.
4806 QualType FieldType = field->getType();
4807 const RecordDecl *rec = field->getParent();
4808 AlignmentSource BaseAlignSource = BaseInfo.getAlignmentSource();
4809 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(BaseAlignSource));
4810 TBAAAccessInfo FieldTBAAInfo;
4811 if (base.getTBAAInfo().isMayAlias() ||
4812 rec->hasAttr<MayAliasAttr>() || FieldType->isVectorType()) {
4813 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
4814 } else if (rec->isUnion()) {
4815 // TODO: Support TBAA for unions.
4816 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo();
4817 } else {
4818 // If no base type been assigned for the base access, then try to generate
4819 // one for this base lvalue.
4820 FieldTBAAInfo = base.getTBAAInfo();
4821 if (!FieldTBAAInfo.BaseType) {
4822 FieldTBAAInfo.BaseType = CGM.getTBAABaseTypeInfo(base.getType());
4823 assert(!FieldTBAAInfo.Offset &&
4824 "Nonzero offset for an access with no base type!");
4825 }
4826
4827 // Adjust offset to be relative to the base type.
4828 const ASTRecordLayout &Layout =
4830 unsigned CharWidth = getContext().getCharWidth();
4831 if (FieldTBAAInfo.BaseType)
4832 FieldTBAAInfo.Offset +=
4833 Layout.getFieldOffset(field->getFieldIndex()) / CharWidth;
4834
4835 // Update the final access type and size.
4836 FieldTBAAInfo.AccessType = CGM.getTBAATypeInfo(FieldType);
4837 FieldTBAAInfo.Size =
4839 }
4840
4841 Address addr = base.getAddress(*this);
4843 addr = wrapWithBPFPreserveStaticOffset(*this, addr);
4844 if (auto *ClassDef = dyn_cast<CXXRecordDecl>(rec)) {
4845 if (CGM.getCodeGenOpts().StrictVTablePointers &&
4846 ClassDef->isDynamicClass()) {
4847 // Getting to any field of dynamic object requires stripping dynamic
4848 // information provided by invariant.group. This is because accessing
4849 // fields may leak the real address of dynamic object, which could result
4850 // in miscompilation when leaked pointer would be compared.
4851 auto *stripped = Builder.CreateStripInvariantGroup(addr.getPointer());
4852 addr = Address(stripped, addr.getElementType(), addr.getAlignment());
4853 }
4854 }
4855
4856 unsigned RecordCVR = base.getVRQualifiers();
4857 if (rec->isUnion()) {
4858 // For unions, there is no pointer adjustment.
4859 if (CGM.getCodeGenOpts().StrictVTablePointers &&
4860 hasAnyVptr(FieldType, getContext()))
4861 // Because unions can easily skip invariant.barriers, we need to add
4862 // a barrier every time CXXRecord field with vptr is referenced.
4864
4866 (getDebugInfo() && rec->hasAttr<BPFPreserveAccessIndexAttr>())) {
4867 // Remember the original union field index
4868 llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateStandaloneType(base.getType(),
4869 rec->getLocation());
4870 addr = Address(
4871 Builder.CreatePreserveUnionAccessIndex(
4872 addr.getPointer(), getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo),
4873 addr.getElementType(), addr.getAlignment());
4874 }
4875
4876 if (FieldType->isReferenceType())
4877 addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType));
4878 } else {
4879 if (!IsInPreservedAIRegion &&
4880 (!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
4881 // For structs, we GEP to the field that the record layout suggests.
4882 addr = emitAddrOfFieldStorage(*this, addr, field);
4883 else
4884 // Remember the original struct field index
4885 addr = emitPreserveStructAccess(*this, base, addr, field);
4886 }
4887
4888 // If this is a reference field, load the reference right now.
4889 if (FieldType->isReferenceType()) {
4890 LValue RefLVal =
4891 MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
4892 if (RecordCVR & Qualifiers::Volatile)
4893 RefLVal.getQuals().addVolatile();
4894 addr = EmitLoadOfReference(RefLVal, &FieldBaseInfo, &FieldTBAAInfo);
4895
4896 // Qualifiers on the struct don't apply to the referencee.
4897 RecordCVR = 0;
4898 FieldType = FieldType->getPointeeType();
4899 }
4900
4901 // Make sure that the address is pointing to the right type. This is critical
4902 // for both unions and structs.
4903 addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType));
4904
4905 if (field->hasAttr<AnnotateAttr>())
4906 addr = EmitFieldAnnotations(field, addr);
4907
4908 LValue LV = MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
4909 LV.getQuals().addCVRQualifiers(RecordCVR);
4910
4911 // __weak attribute on a field is ignored.
4914
4915 return LV;
4916}
4917
4918LValue
4920 const FieldDecl *Field) {
4921 QualType FieldType = Field->getType();
4922
4923 if (!FieldType->isReferenceType())
4924 return EmitLValueForField(Base, Field);
4925
4926 Address V = emitAddrOfFieldStorage(*this, Base.getAddress(*this), Field);
4927
4928 // Make sure that the address is pointing to the right type.
4929 llvm::Type *llvmType = ConvertTypeForMem(FieldType);
4930 V = V.withElementType(llvmType);
4931
4932 // TODO: Generate TBAA information that describes this access as a structure
4933 // member access and not just an access to an object of the field's type. This
4934 // should be similar to what we do in EmitLValueForField().
4935 LValueBaseInfo BaseInfo = Base.getBaseInfo();
4936 AlignmentSource FieldAlignSource = BaseInfo.getAlignmentSource();
4937 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(FieldAlignSource));
4938 return MakeAddrLValue(V, FieldType, FieldBaseInfo,
4939 CGM.getTBAAInfoForSubobject(Base, FieldType));
4940}
4941
4943 if (E->isFileScope()) {
4945 return MakeAddrLValue(GlobalPtr, E->getType(), AlignmentSource::Decl);
4946 }
4947 if (E->getType()->isVariablyModifiedType())
4948 // make sure to emit the VLA size.
4950
4951 Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
4952 const Expr *InitExpr = E->getInitializer();
4954
4955 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
4956 /*Init*/ true);
4957
4958 // Block-scope compound literals are destroyed at the end of the enclosing
4959 // scope in C.
4960 if (!getLangOpts().CPlusPlus)
4963 E->getType(), getDestroyer(DtorKind),
4964 DtorKind & EHCleanup);
4965
4966 return Result;
4967}
4968
4970 if (!E->isGLValue())
4971 // Initializing an aggregate temporary in C++11: T{...}.
4972 return EmitAggExprToLValue(E);
4973
4974 // An lvalue initializer list must be initializing a reference.
4975 assert(E->isTransparent() && "non-transparent glvalue init list");
4976 return EmitLValue(E->getInit(0));
4977}
4978
4979/// Emit the operand of a glvalue conditional operator. This is either a glvalue
4980/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
4981/// LValue is returned and the current block has been terminated.
4982static std::optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF,
4983 const Expr *Operand) {
4984 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) {
4985 CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false);
4986 return std::nullopt;
4987 }
4988
4989 return CGF.EmitLValue(Operand);
4990}
4991
4992namespace {
4993// Handle the case where the condition is a constant evaluatable simple integer,
4994// which means we don't have to separately handle the true/false blocks.
4995std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
4997 const Expr *condExpr = E->getCond();
4998 bool CondExprBool;
4999 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
5000 const Expr *Live = E->getTrueExpr(), *Dead = E->getFalseExpr();
5001 if (!CondExprBool)
5002 std::swap(Live, Dead);
5003
5004 if (!CGF.ContainsLabel(Dead)) {
5005 // If the true case is live, we need to track its region.
5006 if (CondExprBool)
5008 // If a throw expression we emit it and return an undefined lvalue
5009 // because it can't be used.
5010 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Live->IgnoreParens())) {
5011 CGF.EmitCXXThrowExpr(ThrowExpr);
5012 llvm::Type *ElemTy = CGF.ConvertType(Dead->getType());
5013 llvm::Type *Ty = CGF.UnqualPtrTy;
5014 return CGF.MakeAddrLValue(
5015 Address(llvm::UndefValue::get(Ty), ElemTy, CharUnits::One()),
5016 Dead->getType());
5017 }
5018 return CGF.EmitLValue(Live);
5019 }
5020 }
5021 return std::nullopt;
5022}
5023struct ConditionalInfo {
5024 llvm::BasicBlock *lhsBlock, *rhsBlock;
5025 std::optional<LValue> LHS, RHS;
5026};
5027
5028// Create and generate the 3 blocks for a conditional operator.
5029// Leaves the 'current block' in the continuation basic block.
5030template<typename FuncTy>
5031ConditionalInfo EmitConditionalBlocks(CodeGenFunction &CGF,
5033 const FuncTy &BranchGenFunc) {
5034 ConditionalInfo Info{CGF.createBasicBlock("cond.true"),
5035 CGF.createBasicBlock("cond.false"), std::nullopt,
5036 std::nullopt};
5037 llvm::BasicBlock *endBlock = CGF.createBasicBlock("cond.end");
5038
5039 CodeGenFunction::ConditionalEvaluation eval(CGF);
5040 CGF.EmitBranchOnBoolExpr(E->getCond(), Info.lhsBlock, Info.rhsBlock,
5041 CGF.getProfileCount(E));
5042
5043 // Any temporaries created here are conditional.
5044 CGF.EmitBlock(Info.lhsBlock);
5046 eval.begin(CGF);
5047 Info.LHS = BranchGenFunc(CGF, E->getTrueExpr());
5048 eval.end(CGF);
5049 Info.lhsBlock = CGF.Builder.GetInsertBlock();
5050
5051 if (Info.LHS)
5052 CGF.Builder.CreateBr(endBlock);
5053
5054 // Any temporaries created here are conditional.
5055 CGF.EmitBlock(Info.rhsBlock);
5056 eval.begin(CGF);
5057 Info.RHS = BranchGenFunc(CGF, E->getFalseExpr());
5058 eval.end(CGF);
5059 Info.rhsBlock = CGF.Builder.GetInsertBlock();
5060 CGF.EmitBlock(endBlock);
5061
5062 return Info;
5063}
5064} // namespace
5065
5067 const AbstractConditionalOperator *E) {
5068 if (!E->isGLValue()) {
5069 // ?: here should be an aggregate.
5070 assert(hasAggregateEvaluationKind(E->getType()) &&
5071 "Unexpected conditional operator!");
5072 return (void)EmitAggExprToLValue(E);
5073 }
5074
5075 OpaqueValueMapping binding(*this, E);
5076 if (HandleConditionalOperatorLValueSimpleCase(*this, E))
5077 return;
5078
5079 EmitConditionalBlocks(*this, E, [](CodeGenFunction &CGF, const Expr *E) {
5080 CGF.EmitIgnoredExpr(E);
5081 return LValue{};
5082 });
5083}
5086 if (!expr->isGLValue()) {
5087 // ?: here should be an aggregate.
5088 assert(hasAggregateEvaluationKind(expr->getType()) &&
5089 "Unexpected conditional operator!");
5090 return EmitAggExprToLValue(expr);
5091 }
5092
5093 OpaqueValueMapping binding(*this, expr);
5094 if (std::optional<LValue> Res =
5095 HandleConditionalOperatorLValueSimpleCase(*this, expr))
5096 return *Res;
5097
5098 ConditionalInfo Info = EmitConditionalBlocks(
5099 *this, expr, [](CodeGenFunction &CGF, const Expr *E) {
5100 return EmitLValueOrThrowExpression(CGF, E);
5101 });
5102
5103 if ((Info.LHS && !Info.LHS->isSimple()) ||
5104 (Info.RHS && !Info.RHS->isSimple()))
5105 return EmitUnsupportedLValue(expr, "conditional operator");
5106
5107 if (Info.LHS && Info.RHS) {
5108 Address lhsAddr = Info.LHS->getAddress(*this);
5109 Address rhsAddr = Info.RHS->getAddress(*this);
5110 llvm::PHINode *phi = Builder.CreatePHI(lhsAddr.getType(), 2, "cond-lvalue");
5111 phi->addIncoming(lhsAddr.getPointer(), Info.lhsBlock);
5112 phi->addIncoming(rhsAddr.getPointer(), Info.rhsBlock);
5113 Address result(phi, lhsAddr.getElementType(),
5114 std::min(lhsAddr.getAlignment(), rhsAddr.getAlignment()));
5115 AlignmentSource alignSource =
5116 std::max(Info.LHS->getBaseInfo().getAlignmentSource(),
5117 Info.RHS->getBaseInfo().getAlignmentSource());
5119 Info.LHS->getTBAAInfo(), Info.RHS->getTBAAInfo());
5120 return MakeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource),
5121 TBAAInfo);
5122 } else {
5123 assert((Info.LHS || Info.RHS) &&
5124 "both operands of glvalue conditional are throw-expressions?");
5125 return Info.LHS ? *Info.LHS : *Info.RHS;
5126 }
5127}
5128
5129/// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
5130/// type. If the cast is to a reference, we can have the usual lvalue result,
5131/// otherwise if a cast is needed by the code generator in an lvalue context,
5132/// then it must mean that we need the address of an aggregate in order to
5133/// access one of its members. This can happen for all the reasons that casts
5134/// are permitted with aggregate result, including noop aggregate casts, and
5135/// cast from scalar to union.
5137 switch (E->getCastKind()) {
5138 case CK_ToVoid:
5139 case CK_BitCast:
5140 case CK_LValueToRValueBitCast:
5141 case CK_ArrayToPointerDecay:
5142 case CK_FunctionToPointerDecay:
5143 case CK_NullToMemberPointer:
5144 case CK_NullToPointer:
5145 case CK_IntegralToPointer:
5146 case CK_PointerToIntegral:
5147 case CK_PointerToBoolean:
5148 case CK_IntegralCast:
5149 case CK_BooleanToSignedIntegral:
5150 case CK_IntegralToBoolean:
5151 case CK_IntegralToFloating:
5152 case CK_FloatingToIntegral:
5153 case CK_FloatingToBoolean:
5154 case CK_FloatingCast:
5155 case CK_FloatingRealToComplex:
5156 case CK_FloatingComplexToReal:
5157 case CK_FloatingComplexToBoolean:
5158 case CK_FloatingComplexCast:
5159 case CK_FloatingComplexToIntegralComplex:
5160 case CK_IntegralRealToComplex:
5161 case CK_IntegralComplexToReal:
5162 case CK_IntegralComplexToBoolean:
5163 case CK_IntegralComplexCast:
5164 case CK_IntegralComplexToFloatingComplex:
5165 case CK_DerivedToBaseMemberPointer:
5166 case CK_BaseToDerivedMemberPointer:
5167 case CK_MemberPointerToBoolean:
5168 case CK_ReinterpretMemberPointer:
5169 case CK_AnyPointerToBlockPointerCast:
5170 case CK_ARCProduceObject:
5171 case CK_ARCConsumeObject:
5172 case CK_ARCReclaimReturnedObject:
5173 case CK_ARCExtendBlockObject:
5174 case CK_CopyAndAutoreleaseBlockObject:
5175 case CK_IntToOCLSampler:
5176 case CK_FloatingToFixedPoint:
5177 case CK_FixedPointToFloating:
5178 case CK_FixedPointCast:
5179 case CK_FixedPointToBoolean:
5180 case CK_FixedPointToIntegral:
5181 case CK_IntegralToFixedPoint:
5182 case CK_MatrixCast:
5183 case CK_HLSLVectorTruncation:
5184 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
5185
5186 case CK_Dependent:
5187 llvm_unreachable("dependent cast kind in IR gen!");
5188
5189 case CK_BuiltinFnToFnPtr:
5190 llvm_unreachable("builtin functions are handled elsewhere");
5191
5192 // These are never l-values; just use the aggregate emission code.
5193 case CK_NonAtomicToAtomic:
5194 case CK_AtomicToNonAtomic:
5195 return EmitAggExprToLValue(E);
5196
5197 case CK_Dynamic: {
5198 LValue LV = EmitLValue(E->getSubExpr());
5199 Address V = LV.getAddress(*this);
5200 const auto *DCE = cast<CXXDynamicCastExpr>(E);
5202 }
5203
5204 case CK_ConstructorConversion:
5205 case CK_UserDefinedConversion:
5206 case CK_CPointerToObjCPointerCast:
5207 case CK_BlockPointerToObjCPointerCast:
5208 case CK_LValueToRValue:
5209 return EmitLValue(E->getSubExpr());
5210
5211 case CK_NoOp: {
5212 // CK_NoOp can model a qualification conversion, which can remove an array
5213 // bound and change the IR type.
5214 // FIXME: Once pointee types are removed from IR, remove this.
5215 LValue LV = EmitLValue(E->getSubExpr());
5216 // Propagate the volatile qualifer to LValue, if exist in E.
5218 LV.getQuals() = E->getType().getQualifiers();
5219 if (LV.isSimple()) {
5220 Address V = LV.getAddress(*this);
5221 if (V.isValid()) {
5222 llvm::Type *T = ConvertTypeForMem(E->getType());
5223 if (V.getElementType() != T)
5224 LV.setAddress(V.withElementType(T));
5225 }
5226 }
5227 return LV;
5228 }
5229
5230 case CK_UncheckedDerivedToBase:
5231 case CK_DerivedToBase: {
5232 const auto *DerivedClassTy =
5233 E->getSubExpr()->getType()->castAs<RecordType>();
5234 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
5235
5236 LValue LV = EmitLValue(E->getSubExpr());
5237 Address This = LV.getAddress(*this);
5238
5239 // Perform the derived-to-base conversion
5241 This, DerivedClassDecl, E->path_begin(), E->path_end(),
5242 /*NullCheckValue=*/false, E->getExprLoc());
5243
5244 // TODO: Support accesses to members of base classes in TBAA. For now, we
5245 // conservatively pretend that the complete object is of the base class
5246 // type.
5247 return MakeAddrLValue(Base, E->getType(), LV.getBaseInfo(),
5249 }
5250 case CK_ToUnion:
5251 return EmitAggExprToLValue(E);
5252 case CK_BaseToDerived: {
5253 const auto *DerivedClassTy = E->getType()->castAs<RecordType>();
5254 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
5255
5256 LValue LV = EmitLValue(E->getSubExpr());
5257
5258 // Perform the base-to-derived conversion
5260 LV.getAddress(*this), DerivedClassDecl, E->path_begin(), E->path_end(),
5261 /*NullCheckValue=*/false);
5262
5263 // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
5264 // performed and the object is not of the derived type.
5267 Derived.getPointer(), E->getType());
5268
5269 if (SanOpts.has(SanitizerKind::CFIDerivedCast))
5270 EmitVTablePtrCheckForCast(E->getType(), Derived,
5271 /*MayBeNull=*/false, CFITCK_DerivedCast,
5272 E->getBeginLoc());
5273
5274 return MakeAddrLValue(Derived, E->getType(), LV.getBaseInfo(),
5276 }
5277 case CK_LValueBitCast: {
5278 // This must be a reinterpret_cast (or c-style equivalent).
5279 const auto *CE = cast<ExplicitCastExpr>(E);
5280
5281 CGM.EmitExplicitCastExprType(CE, this);
5282 LValue LV = EmitLValue(E->getSubExpr());
5283 Address V = LV.getAddress(*this).withElementType(
5284 ConvertTypeForMem(CE->getTypeAsWritten()->getPointeeType()));
5285
5286 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast))
5288 /*MayBeNull=*/false, CFITCK_UnrelatedCast,
5289 E->getBeginLoc());
5290
5291 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
5293 }
5294 case CK_AddressSpaceConversion: {
5295 LValue LV = EmitLValue(E->getSubExpr());
5296 QualType DestTy = getContext().getPointerType(E->getType());
5297 llvm::Value *V = getTargetHooks().performAddrSpaceCast(
5298 *this, LV.getPointer(*this),
5300 E->getType().getAddressSpace(), ConvertType(DestTy));
5302 LV.getAddress(*this).getAlignment()),
5303 E->getType(), LV.getBaseInfo(), LV.getTBAAInfo());
5304 }
5305 case CK_ObjCObjectLValueCast: {
5306 LValue LV = EmitLValue(E->getSubExpr());
5308 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(),
5310 }
5311 case CK_ZeroToOCLOpaqueType:
5312 llvm_unreachable("NULL to OpenCL opaque type lvalue cast is not valid");
5313
5314 case CK_VectorSplat: {
5315 // LValue results of vector splats are only supported in HLSL.
5316 if (!getLangOpts().HLSL)
5317 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
5318 return EmitLValue(E->getSubExpr());
5319 }
5320 }
5321
5322 llvm_unreachable("Unhandled lvalue cast kind?");
5323}
5324
5328}
5329
5330LValue
5333
5334 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
5335 it = OpaqueLValues.find(e);
5336
5337 if (it != OpaqueLValues.end())
5338 return it->second;
5339
5340 assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted");
5341 return EmitLValue(e->getSourceExpr());
5342}
5343
5344RValue
5347
5348 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
5349 it = OpaqueRValues.find(e);
5350
5351 if (it != OpaqueRValues.end())
5352 return it->second;
5353
5354 assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted");
5355 return EmitAnyExpr(e->getSourceExpr());
5356}
5357
5359 const FieldDecl *FD,
5360 SourceLocation Loc) {
5361 QualType FT = FD->getType();
5362 LValue FieldLV = EmitLValueForField(LV, FD);
5363 switch (getEvaluationKind(FT)) {
5364 case TEK_Complex:
5365 return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
5366 case TEK_Aggregate:
5367 return FieldLV.asAggregateRValue(*this);
5368 case TEK_Scalar:
5369 // This routine is used to load fields one-by-one to perform a copy, so
5370 // don't load reference fields.
5371 if (FD->getType()->isReferenceType())
5372 return RValue::get(FieldLV.getPointer(*this));
5373 // Call EmitLoadOfScalar except when the lvalue is a bitfield to emit a
5374 // primitive load.
5375 if (FieldLV.isBitField())
5376 return EmitLoadOfLValue(FieldLV, Loc);
5377 return RValue::get(EmitLoadOfScalar(FieldLV, Loc));
5378 }
5379 llvm_unreachable("bad evaluation kind");
5380}
5381
5382//===--------------------------------------------------------------------===//
5383// Expression Emission
5384//===--------------------------------------------------------------------===//
5385
5387 ReturnValueSlot ReturnValue) {
5388 // Builtins never have block type.
5389 if (E->getCallee()->getType()->isBlockPointerType())
5390 return EmitBlockCallExpr(E, ReturnValue);
5391
5392 if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
5394
5395 if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
5397
5398 // A CXXOperatorCallExpr is created even for explicit object methods, but
5399 // these should be treated like static function call.
5400 if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
5401 if (const auto *MD =
5402 dyn_cast_if_present<CXXMethodDecl>(CE->getCalleeDecl());
5403 MD && MD->isImplicitObjectMemberFunction())
5405
5406 CGCallee callee = EmitCallee(E->getCallee());
5407
5408 if (callee.isBuiltin()) {
5409 return EmitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(),
5410 E, ReturnValue);
5411 }
5412
5413 if (callee.isPseudoDestructor()) {
5415 }
5416
5417 return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue);
5418}
5419
5420/// Emit a CallExpr without considering whether it might be a subclass.
5422 ReturnValueSlot ReturnValue) {
5424 return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue);
5425}
5426
5427// Detect the unusual situation where an inline version is shadowed by a
5428// non-inline version. In that case we should pick the external one
5429// everywhere. That's GCC behavior too.
5431 for (const FunctionDecl *PD = FD; PD; PD = PD->getPreviousDecl())
5432 if (!PD->isInlineBuiltinDeclaration())
5433 return false;
5434 return true;
5435}
5436
5438 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
5439
5440 if (auto builtinID = FD->getBuiltinID()) {
5441 std::string NoBuiltinFD = ("no-builtin-" + FD->getName()).str();
5442 std::string NoBuiltins = "no-builtins";
5443
5444 StringRef Ident = CGF.CGM.getMangledName(GD);
5445 std::string FDInlineName = (Ident + ".inline").str();
5446
5447 bool IsPredefinedLibFunction =
5449 bool HasAttributeNoBuiltin =
5450 CGF.CurFn->getAttributes().hasFnAttr(NoBuiltinFD) ||
5451 CGF.CurFn->getAttributes().hasFnAttr(NoBuiltins);
5452
5453 // When directing calling an inline builtin, call it through it's mangled
5454 // name to make it clear it's not the actual builtin.
5455 if (CGF.CurFn->getName() != FDInlineName &&
5457 llvm::Constant *CalleePtr = EmitFunctionDeclPointer(CGF.CGM, GD);
5458 llvm::Function *Fn = llvm::cast<llvm::Function>(CalleePtr);
5459 llvm::Module *M = Fn->getParent();
5460 llvm::Function *Clone = M->getFunction(FDInlineName);
5461 if (!Clone) {
5462 Clone = llvm::Function::Create(Fn->getFunctionType(),
5463 llvm::GlobalValue::InternalLinkage,
5464 Fn->getAddressSpace(), FDInlineName, M);
5465 Clone->addFnAttr(llvm::Attribute::AlwaysInline);
5466 }
5467 return CGCallee::forDirect(Clone, GD);
5468 }
5469
5470 // Replaceable builtins provide their own implementation of a builtin. If we
5471 // are in an inline builtin implementation, avoid trivial infinite
5472 // recursion. Honor __attribute__((no_builtin("foo"))) or
5473 // __attribute__((no_builtin)) on the current function unless foo is
5474 // not a predefined library function which means we must generate the
5475 // builtin no matter what.
5476 else if (!IsPredefinedLibFunction || !HasAttributeNoBuiltin)
5477 return CGCallee::forBuiltin(builtinID, FD);
5478 }
5479
5480 llvm::Constant *CalleePtr = EmitFunctionDeclPointer(CGF.CGM, GD);
5481 if (CGF.CGM.getLangOpts().CUDA && !CGF.CGM.getLangOpts().CUDAIsDevice &&
5482 FD->hasAttr<CUDAGlobalAttr>())
5483 CalleePtr = CGF.CGM.getCUDARuntime().getKernelStub(
5484 cast<llvm::GlobalValue>(CalleePtr->stripPointerCasts()));
5485
5486 return CGCallee::forDirect(CalleePtr, GD);
5487}
5488
5490 E = E->IgnoreParens();
5491
5492 // Look through function-to-pointer decay.
5493 if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) {
5494 if (ICE->getCastKind() == CK_FunctionToPointerDecay ||
5495 ICE->getCastKind() == CK_BuiltinFnToFnPtr) {
5496 return EmitCallee(ICE->getSubExpr());
5497 }
5498
5499 // Resolve direct calls.
5500 } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) {
5501 if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
5502 return EmitDirectCallee(*this, FD);
5503 }
5504 } else if (auto ME = dyn_cast<MemberExpr>(E)) {
5505 if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) {
5506 EmitIgnoredExpr(ME->getBase());
5507 return EmitDirectCallee(*this, FD);
5508 }
5509
5510 // Look through template substitutions.
5511 } else if (auto NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
5512 return EmitCallee(NTTP->getReplacement());
5513
5514 // Treat pseudo-destructor calls differently.
5515 } else if (auto PDE = dyn_cast<CXXPseudoDestructorExpr>(E)) {
5517 }
5518
5519 // Otherwise, we have an indirect reference.
5520 llvm::Value *calleePtr;
5522 if (auto ptrType = E->getType()->getAs<PointerType>()) {
5523 calleePtr = EmitScalarExpr(E);
5524 functionType = ptrType->getPointeeType();
5525 } else {
5526 functionType = E->getType();
5527 calleePtr = EmitLValue(E, KnownNonNull).getPointer(*this);
5528 }
5529 assert(functionType->isFunctionType());
5530
5531 GlobalDecl GD;
5532 if (const auto *VD =
5533 dyn_cast_or_null<VarDecl>(E->getReferencedDeclOfCallee()))
5534 GD = GlobalDecl(VD);
5535
5536 CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), GD);
5537 CGCallee callee(calleeInfo, calleePtr);
5538 return callee;
5539}
5540
5542 // Comma expressions just emit their LHS then their RHS as an l-value.
5543 if (E->getOpcode() == BO_Comma) {
5544 EmitIgnoredExpr(E->getLHS());
5546 return EmitLValue(E->getRHS());
5547 }
5548
5549 if (E->getOpcode() == BO_PtrMemD ||
5550 E->getOpcode() == BO_PtrMemI)
5552
5553 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
5554
5555 // Note that in all of these cases, __block variables need the RHS
5556 // evaluated first just in case the variable gets moved by the RHS.
5557
5558 switch (getEvaluationKind(E->getType())) {
5559 case TEK_Scalar: {
5560 switch (E->getLHS()->getType().getObjCLifetime()) {
5562 return EmitARCStoreStrong(E, /*ignored*/ false).first;
5563
5565 return EmitARCStoreAutoreleasing(E).first;
5566
5567 // No reason to do any of these differently.
5571 break;
5572 }
5573
5574 RValue RV = EmitAnyExpr(E->getRHS());
5576 if (RV.isScalar())
5578 EmitStoreThroughLValue(RV, LV);
5579 if (getLangOpts().OpenMP)
5581 E->getLHS());
5582 return LV;
5583 }
5584
5585 case TEK_Complex:
5587
5588 case TEK_Aggregate:
5589 return EmitAggExprToLValue(E);
5590 }
5591 llvm_unreachable("bad evaluation kind");
5592}
5593
5595 RValue RV = EmitCallExpr(E);
5596
5597 if (!RV.isScalar())
5598 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5600
5601 assert(E->getCallReturnType(getContext())->isReferenceType() &&
5602 "Can't have a scalar return unless the return type is a "
5603 "reference type!");
5604
5606}
5607
5609 // FIXME: This shouldn't require another copy.
5610 return EmitAggExprToLValue(E);
5611}
5612
5615 && "binding l-value to type which needs a temporary");
5616 AggValueSlot Slot = CreateAggTemp(E->getType());
5617 EmitCXXConstructExpr(E, Slot);
5619}
5620
5621LValue
5624}
5625
5629}
5630
5634}
5635
5636LValue
5638 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
5640 EmitAggExpr(E->getSubExpr(), Slot);
5641 EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddress());
5643}
5644
5647
5648 if (!RV.isScalar())
5649 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5651
5652 assert(E->getMethodDecl()->getReturnType()->isReferenceType() &&
5653 "Can't have a scalar return unless the return type is a "
5654 "reference type!");
5655
5657}
5658
5660 Address V =
5663}
5664
5666 const ObjCIvarDecl *Ivar) {
5667 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
5668}
5669
5670llvm::Value *
5672 const ObjCIvarDecl *Ivar) {
5673 llvm::Value *OffsetValue = EmitIvarOffset(Interface, Ivar);
5674 QualType PointerDiffType = getContext().getPointerDiffType();
5675 return Builder.CreateZExtOrTrunc(OffsetValue,
5676 getTypes().ConvertType(PointerDiffType));
5677}
5678
5680 llvm::Value *BaseValue,
5681 const ObjCIvarDecl *Ivar,
5682 unsigned CVRQualifiers) {
5683 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
5684 Ivar, CVRQualifiers);
5685}
5686
5688 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
5689 llvm::Value *BaseValue = nullptr;
5690 const Expr *BaseExpr = E->getBase();
5691 Qualifiers BaseQuals;
5692 QualType ObjectTy;
5693 if (E->isArrow()) {
5694 BaseValue = EmitScalarExpr(BaseExpr);
5695 ObjectTy = BaseExpr->getType()->getPointeeType();
5696 BaseQuals = ObjectTy.getQualifiers();
5697 } else {
5698 LValue BaseLV = EmitLValue(BaseExpr);
5699 BaseValue = BaseLV.getPointer(*this);
5700 ObjectTy = BaseExpr->getType();
5701 BaseQuals = ObjectTy.getQualifiers();
5702 }
5703
5704 LValue LV =
5705 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
5706 BaseQuals.getCVRQualifiers());
5708 return LV;
5709}
5710
5712 // Can only get l-value for message expression returning aggregate type
5713 RValue RV = EmitAnyExprToTemp(E);
5714 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(),
5716}
5717
5718RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee,
5719 const CallExpr *E, ReturnValueSlot ReturnValue,
5720 llvm::Value *Chain) {
5721 // Get the actual function type. The callee type will always be a pointer to
5722 // function type or a block pointer type.
5723 assert(CalleeType->isFunctionPointerType() &&
5724 "Call must have function pointer type!");
5725
5726 const Decl *TargetDecl =
5727 OrigCallee.getAbstractInfo().getCalleeDecl().getDecl();
5728
5729 assert((!isa_and_present<FunctionDecl>(TargetDecl) ||
5730 !cast<FunctionDecl>(TargetDecl)->isImmediateFunction()) &&
5731 "trying to emit a call to an immediate function");
5732
5733 CalleeType = getContext().getCanonicalType(CalleeType);
5734
5735 auto PointeeType = cast<PointerType>(CalleeType)->getPointeeType();
5736
5737 CGCallee Callee = OrigCallee;
5738
5739 if (SanOpts.has(SanitizerKind::Function) &&
5740 (!TargetDecl || !isa<FunctionDecl>(TargetDecl)) &&
5741 !isa<FunctionNoProtoType>(PointeeType)) {
5742 if (llvm::Constant *PrefixSig =
5744 SanitizerScope SanScope(this);
5745 auto *TypeHash = getUBSanFunctionTypeHash(PointeeType);
5746
5747 llvm::Type *PrefixSigType = PrefixSig->getType();
5748 llvm::StructType *PrefixStructTy = llvm::StructType::get(
5749 CGM.getLLVMContext(), {PrefixSigType, Int32Ty}, /*isPacked=*/true);
5750
5751 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5752
5753 // On 32-bit Arm, the low bit of a function pointer indicates whether
5754 // it's using the Arm or Thumb instruction set. The actual first
5755 // instruction lives at the same address either way, so we must clear
5756 // that low bit before using the function address to find the prefix
5757 // structure.
5758 //
5759 // This applies to both Arm and Thumb target triples, because
5760 // either one could be used in an interworking context where it
5761 // might be passed function pointers of both types.
5762 llvm::Value *AlignedCalleePtr;
5763 if (CGM.getTriple().isARM() || CGM.getTriple().isThumb()) {
5764 llvm::Value *CalleeAddress =
5765 Builder.CreatePtrToInt(CalleePtr, IntPtrTy);
5766 llvm::Value *Mask = llvm::ConstantInt::get(IntPtrTy, ~1);
5767 llvm::Value *AlignedCalleeAddress =
5768 Builder.CreateAnd(CalleeAddress, Mask);
5769 AlignedCalleePtr =
5770 Builder.CreateIntToPtr(AlignedCalleeAddress, CalleePtr->getType());
5771 } else {
5772 AlignedCalleePtr = CalleePtr;
5773 }
5774
5775 llvm::Value *CalleePrefixStruct = AlignedCalleePtr;
5776 llvm::Value *CalleeSigPtr =
5777 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, -1, 0);
5778 llvm::Value *CalleeSig =
5779 Builder.CreateAlignedLoad(PrefixSigType, CalleeSigPtr, getIntAlign());
5780 llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
5781
5782 llvm::BasicBlock *Cont = createBasicBlock("cont");
5783 llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
5784 Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
5785
5786 EmitBlock(TypeCheck);
5787 llvm::Value *CalleeTypeHash = Builder.CreateAlignedLoad(
5788 Int32Ty,
5789 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, -1, 1),
5790 getPointerAlign());
5791 llvm::Value *CalleeTypeHashMatch =
5792 Builder.CreateICmpEQ(CalleeTypeHash, TypeHash);
5793 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(E->getBeginLoc()),
5794 EmitCheckTypeDescriptor(CalleeType)};
5795 EmitCheck(std::make_pair(CalleeTypeHashMatch, SanitizerKind::Function),
5796 SanitizerHandler::FunctionTypeMismatch, StaticData,
5797 {CalleePtr});
5798
5799 Builder.CreateBr(Cont);
5800 EmitBlock(Cont);
5801 }
5802 }
5803
5804 const auto *FnType = cast<FunctionType>(PointeeType);
5805
5806 // If we are checking indirect calls and this call is indirect, check that the
5807 // function pointer is a member of the bit set for the function type.
5808 if (SanOpts.has(SanitizerKind::CFIICall) &&
5809 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
5810 SanitizerScope SanScope(this);
5811 EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
5812
5813 llvm::Metadata *MD;
5814 if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
5816 else
5818
5819 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
5820
5821 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5822 llvm::Value *TypeTest = Builder.CreateCall(
5823 CGM.getIntrinsic(llvm::Intrinsic::type_test), {CalleePtr, TypeId});
5824
5825 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
5826 llvm::Constant *StaticData[] = {
5827 llvm::ConstantInt::get(Int8Ty, CFITCK_ICall),
5830 };
5831 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
5832 EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId,
5833 CalleePtr, StaticData);
5834 } else {
5835 EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall),
5836 SanitizerHandler::CFICheckFail, StaticData,
5837 {CalleePtr, llvm::UndefValue::get(IntPtrTy)});
5838 }
5839 }
5840
5841 CallArgList Args;
5842 if (Chain)
5843 Args.add(RValue::get(Chain), CGM.getContext().VoidPtrTy);
5844
5845 // C++17 requires that we evaluate arguments to a call using assignment syntax
5846 // right-to-left, and that we evaluate arguments to certain other operators
5847 // left-to-right. Note that we allow this to override the order dictated by
5848 // the calling convention on the MS ABI, which means that parameter
5849 // destruction order is not necessarily reverse construction order.
5850 // FIXME: Revisit this based on C++ committee response to unimplementability.
5852 bool StaticOperator = false;
5853 if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
5854 if (OCE->isAssignmentOp())
5856 else {
5857 switch (OCE->getOperator()) {
5858 case OO_LessLess:
5859 case OO_GreaterGreater:
5860 case OO_AmpAmp:
5861 case OO_PipePipe:
5862 case OO_Comma:
5863 case OO_ArrowStar:
5865 break;
5866 default:
5867 break;
5868 }
5869 }
5870
5871 if (const auto *MD =
5872 dyn_cast_if_present<CXXMethodDecl>(OCE->getCalleeDecl());
5873 MD && MD->isStatic())
5874 StaticOperator = true;
5875 }
5876
5877 auto Arguments = E->arguments();
5878 if (StaticOperator) {
5879 // If we're calling a static operator, we need to emit the object argument
5880 // and ignore it.
5881 EmitIgnoredExpr(E->getArg(0));
5882 Arguments = drop_begin(Arguments, 1);
5883 }
5884 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), Arguments,
5885 E->getDirectCallee(), /*ParamsToSkip=*/0, Order);
5886
5888 Args, FnType, /*ChainCall=*/Chain);
5889
5890 // C99 6.5.2.2p6:
5891 // If the expression that denotes the called function has a type
5892 // that does not include a prototype, [the default argument
5893 // promotions are performed]. If the number of arguments does not
5894 // equal the number of parameters, the behavior is undefined. If
5895 // the function is defined with a type that includes a prototype,
5896 // and either the prototype ends with an ellipsis (, ...) or the
5897 // types of the arguments after promotion are not compatible with
5898 // the types of the parameters, the behavior is undefined. If the
5899 // function is defined with a type that does not include a
5900 // prototype, and the types of the arguments after promotion are
5901 // not compatible with those of the parameters after promotion,
5902 // the behavior is undefined [except in some trivial cases].
5903 // That is, in the general case, we should assume that a call
5904 // through an unprototyped function type works like a *non-variadic*
5905 // call. The way we make this work is to cast to the exact type
5906 // of the promoted arguments.
5907 //
5908 // Chain calls use this same code path to add the invisible chain parameter
5909 // to the function type.
5910 if (isa<FunctionNoProtoType>(FnType) || Chain) {
5911 llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
5912 int AS = Callee.getFunctionPointer()->getType()->getPointerAddressSpace();
5913 CalleeTy = CalleeTy->getPointerTo(AS);
5914
5915 llvm::Value *CalleePtr = Callee.getFunctionPointer();
5916 CalleePtr = Builder.CreateBitCast(CalleePtr, CalleeTy, "callee.knr.cast");
5917 Callee.setFunctionPointer(CalleePtr);
5918 }
5919
5920 // HIP function pointer contains kernel handle when it is used in triple
5921 // chevron. The kernel stub needs to be loaded from kernel handle and used
5922 // as callee.
5923 if (CGM.getLangOpts().HIP && !CGM.getLangOpts().CUDAIsDevice &&
5924 isa<CUDAKernelCallExpr>(E) &&
5925 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
5926 llvm::Value *Handle = Callee.getFunctionPointer();
5927 auto *Stub = Builder.CreateLoad(
5928 Address(Handle, Handle->getType(), CGM.getPointerAlign()));
5929 Callee.setFunctionPointer(Stub);
5930 }
5931 llvm::CallBase *CallOrInvoke = nullptr;
5932 RValue Call = EmitCall(FnInfo, Callee, ReturnValue, Args, &CallOrInvoke,
5933 E == MustTailCall, E->getExprLoc());
5934
5935 // Generate function declaration DISuprogram in order to be used
5936 // in debug info about call sites.
5937 if (CGDebugInfo *DI = getDebugInfo()) {
5938 if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
5939 FunctionArgList Args;
5940 QualType ResTy = BuildFunctionArgList(CalleeDecl, Args);
5941 DI->EmitFuncDeclForCallSite(CallOrInvoke,
5942 DI->getFunctionType(CalleeDecl, ResTy, Args),
5943 CalleeDecl);
5944 }
5945 }
5946
5947 return Call;
5948}
5949
5952 Address BaseAddr = Address::invalid();
5953 if (E->getOpcode() == BO_PtrMemI) {
5954 BaseAddr = EmitPointerWithAlignment(E->getLHS());
5955 } else {
5956 BaseAddr = EmitLValue(E->getLHS()).getAddress(*this);
5957 }
5958
5959 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
5960 const auto *MPT = E->getRHS()->getType()->castAs<MemberPointerType>();
5961
5962 LValueBaseInfo BaseInfo;
5963 TBAAAccessInfo TBAAInfo;
5964 Address MemberAddr =
5965 EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT, &BaseInfo,
5966 &TBAAInfo);
5967
5968 return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), BaseInfo, TBAAInfo);
5969}
5970
5971/// Given the address of a temporary variable, produce an r-value of
5972/// its type.
5974 QualType type,
5975 SourceLocation loc) {
5977 switch (getEvaluationKind(type)) {
5978 case TEK_Complex:
5979 return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
5980 case TEK_Aggregate:
5981 return lvalue.asAggregateRValue(*this);
5982 case TEK_Scalar:
5983 return RValue::get(EmitLoadOfScalar(lvalue, loc));
5984 }
5985 llvm_unreachable("bad evaluation kind");
5986}
5987
5988void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
5989 assert(Val->getType()->isFPOrFPVectorTy());
5990 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
5991 return;
5992
5993 llvm::MDBuilder MDHelper(getLLVMContext());
5994 llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
5995
5996 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
5997}
5998
5999void CodeGenFunction::SetSqrtFPAccuracy(llvm::Value *Val) {
6000 llvm::Type *EltTy = Val->getType()->getScalarType();
6001 if (!EltTy->isFloatTy())
6002 return;
6003
6004 if ((getLangOpts().OpenCL &&
6005 !CGM.getCodeGenOpts().OpenCLCorrectlyRoundedDivSqrt) ||
6006 (getLangOpts().HIP && getLangOpts().CUDAIsDevice &&
6007 !CGM.getCodeGenOpts().HIPCorrectlyRoundedDivSqrt)) {
6008 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 3ulp
6009 //
6010 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt
6011 // build option allows an application to specify that single precision
6012 // floating-point divide (x/y and 1/x) and sqrt used in the program
6013 // source are correctly rounded.
6014 //
6015 // TODO: CUDA has a prec-sqrt flag
6016 SetFPAccuracy(Val, 3.0f);
6017 }
6018}
6019
6020void CodeGenFunction::SetDivFPAccuracy(llvm::Value *Val) {
6021 llvm::Type *EltTy = Val->getType()->getScalarType();
6022 if (!EltTy->isFloatTy())
6023 return;
6024
6025 if ((getLangOpts().OpenCL &&
6026 !CGM.getCodeGenOpts().OpenCLCorrectlyRoundedDivSqrt) ||
6027 (getLangOpts().HIP && getLangOpts().CUDAIsDevice &&
6028 !CGM.getCodeGenOpts().HIPCorrectlyRoundedDivSqrt)) {
6029 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 2.5ulp
6030 //
6031 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt
6032 // build option allows an application to specify that single precision
6033 // floating-point divide (x/y and 1/x) and sqrt used in the program
6034 // source are correctly rounded.
6035 //
6036 // TODO: CUDA has a prec-div flag
6037 SetFPAccuracy(Val, 2.5f);
6038 }
6039}
6040
6041namespace {
6042 struct LValueOrRValue {
6043 LValue LV;
6044 RValue RV;
6045 };
6046}
6047
6048static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
6049 const PseudoObjectExpr *E,
6050 bool forLValue,
6051 AggValueSlot slot) {
6053
6054 // Find the result expression, if any.
6055 const Expr *resultExpr = E->getResultExpr();
6056 LValueOrRValue result;
6057
6059 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
6060 const Expr *semantic = *i;
6061
6062 // If this semantic expression is an opaque value, bind it
6063 // to the result of its source expression.
6064 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
6065 // Skip unique OVEs.
6066 if (ov->isUnique()) {
6067 assert(ov != resultExpr &&
6068 "A unique OVE cannot be used as the result expression");
6069 continue;
6070 }
6071
6072 // If this is the result expression, we may need to evaluate
6073 // directly into the slot.
6074 typedef CodeGenFunction::OpaqueValueMappingData OVMA;
6075 OVMA opaqueData;
6076 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
6078 CGF.EmitAggExpr(ov->getSourceExpr(), slot);
6079 LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(),
6081 opaqueData = OVMA::bind(CGF, ov, LV);
6082 result.RV = slot.asRValue();
6083
6084 // Otherwise, emit as normal.
6085 } else {
6086 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
6087
6088 // If this is the result, also evaluate the result now.
6089 if (ov == resultExpr) {
6090 if (forLValue)
6091 result.LV = CGF.EmitLValue(ov);
6092 else
6093 result.RV = CGF.EmitAnyExpr(ov, slot);
6094 }
6095 }
6096
6097 opaques.push_back(opaqueData);
6098
6099 // Otherwise, if the expression is the result, evaluate it
6100 // and remember the result.
6101 } else if (semantic == resultExpr) {
6102 if (forLValue)
6103 result.LV = CGF.EmitLValue(semantic);
6104 else
6105 result.RV = CGF.EmitAnyExpr(semantic, slot);
6106
6107 // Otherwise, evaluate the expression in an ignored context.
6108 } else {
6109 CGF.EmitIgnoredExpr(semantic);
6110 }
6111 }
6112
6113 // Unbind all the opaques now.
6114 for (unsigned i = 0, e = opaques.size(); i != e; ++i)
6115 opaques[i].unbind(CGF);
6116
6117 return result;
6118}
6119
6121 AggValueSlot slot) {
6122 return emitPseudoObjectExpr(*this, E, false, slot).RV;
6123}
6124
6126 return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
6127}
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3259
DynTypedNode Node
Defines enum values for all the target-independent builtin functions.
CodeGenFunction::ComplexPairTy ComplexPairTy
static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, LValue &LV, bool IsMemberAccess=false)
Definition: CGExpr.cpp:2653
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:2900
static const Expr * isSimpleArrayDecayOperand(const Expr *E)
isSimpleArrayDecayOperand - If the specified expr is a simple decay from an array to pointer,...
Definition: CGExpr.cpp:3898
static bool hasBooleanRepresentation(QualType Ty)
Definition: CGExpr.cpp:1856
static llvm::cl::opt< bool > ClSanitizeDebugDeoptimization("ubsan-unique-traps", llvm::cl::Optional, llvm::cl::desc("Deoptimize traps for UBSAN so there is 1 trap per check"), llvm::cl::init(false))
static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind)
Definition: CGExpr.cpp:3453
static bool hasBPFPreserveStaticOffset(const RecordDecl *D)
Definition: CGExpr.cpp:3953
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, const FieldDecl *field)
Drill down to the storage of a field without walking into reference types.
Definition: CGExpr.cpp:4706
ConstantEmissionKind
Can we constant-emit a load of a reference to a variable of the given type? This is different from pr...
Definition: CGExpr.cpp:1710
@ CEK_AsReferenceOnly
Definition: CGExpr.cpp:1712
@ CEK_AsValueOnly
Definition: CGExpr.cpp:1714
@ CEK_None
Definition: CGExpr.cpp:1711
@ CEK_AsValueOrReference
Definition: CGExpr.cpp:1713
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:1685
static llvm::Value * emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, llvm::Value *High)
Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
Definition: CGExpr.cpp:641
static QualType getFixedSizeElementType(const ASTContext &ctx, const VariableArrayType *vla)
Definition: CGExpr.cpp:3944
static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, llvm::Value *ThisValue)
Definition: CGExpr.cpp:2888
static std::optional< LValue > EmitLValueOrThrowExpression(CodeGenFunction &CGF, const Expr *Operand)
Emit the operand of a glvalue conditional operator.
Definition: CGExpr.cpp:4982
static Address createReferenceTemporary(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, const Expr *Inner, Address *Alloca=nullptr)
Definition: CGExpr.cpp:387
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:3912
static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E, GlobalDecl GD)
Definition: CGExpr.cpp:2879
static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, const PseudoObjectExpr *E, bool forLValue, AggValueSlot slot)
Definition: CGExpr.cpp:6048
static Address wrapWithBPFPreserveStaticOffset(CodeGenFunction &CGF, Address &Addr)
Definition: CGExpr.cpp:3969
static DeclRefExpr * tryToConvertMemberExprToDeclRefExpr(CodeGenFunction &CGF, const MemberExpr *ME)
Definition: CGExpr.cpp:1820
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:913
static RValue EmitLoadOfMatrixLValue(LValue LV, SourceLocation Loc, CodeGenFunction &CGF)
Definition: CGExpr.cpp:2164
static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type)
Definition: CGExpr.cpp:1716
static Address MaybeConvertMatrixAddress(Address Addr, CodeGenFunction &CGF, bool IsVector=true)
Definition: CGExpr.cpp:2061
static QualType getConstantExprReferredType(const FullExpr *E, const ASTContext &Ctx)
Definition: CGExpr.cpp:1519
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, llvm::APInt &Min, llvm::APInt &End, bool StrictEnums, bool IsBool)
Definition: CGExpr.cpp:1869
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:4098
static CGCallee EmitDirectCallee(CodeGenFunction &CGF, GlobalDecl GD)
Definition: CGExpr.cpp:5437
static bool getGEPIndicesToField(CodeGenFunction &CGF, const RecordDecl *RD, const FieldDecl *FD, RecIndicesTy &Indices)
Definition: CGExpr.cpp:1062
static LValue EmitThreadPrivateVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, llvm::Type *RealVarTy, SourceLocation Loc)
Definition: CGExpr.cpp:2750
static bool OnlyHasInlineBuiltinDeclaration(const FunctionDecl *FD)
Definition: CGExpr.cpp:5430
static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, const Expr *E, const VarDecl *VD)
Definition: CGExpr.cpp:2828
static bool hasAnyVptr(const QualType Type, const ASTContext &Context)
Definition: CGExpr.cpp:4732
static bool getFieldOffsetInBits(CodeGenFunction &CGF, const RecordDecl *RD, const FieldDecl *FD, int64_t &Offset)
The offset of a field from the beginning of the record.
Definition: CGExpr.cpp:4067
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:3982
static Address emitDeclTargetVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType T)
Definition: CGExpr.cpp:2764
static bool canEmitSpuriousReferenceToVariable(CodeGenFunction &CGF, const DeclRefExpr *E, const VarDecl *VD)
Determine whether we can emit a reference to VD from the current context, despite not necessarily hav...
Definition: CGExpr.cpp:2925
static CharUnits getArrayElementAlign(CharUnits arrayAlign, llvm::Value *idx, CharUnits eltSize)
Definition: CGExpr.cpp:3929
static bool isAAPCS(const TargetInfo &TargetInfo)
Helper method to check if the underlying ABI is AAPCS.
Definition: CGExpr.cpp:434
static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue, bool isInit, CodeGenFunction &CGF)
Definition: CGExpr.cpp:2085
static llvm::Constant * EmitFunctionDeclPointer(CodeGenModule &CGM, GlobalDecl GD)
Definition: CGExpr.cpp:2867
static Address EmitPointerWithAlignment(const Expr *E, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo, KnownNonNull_t IsKnownNonNull, CodeGenFunction &CGF)
Definition: CGExpr.cpp:1261
static Address emitPreserveStructAccess(CodeGenFunction &CGF, LValue base, Address addr, const FieldDecl *field)
Definition: CGExpr.cpp:4719
const SanitizerHandlerInfo SanitizerHandlers[]
Definition: CGExpr.cpp:3470
static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base, const FieldDecl *Field)
Get the address of a zero-sized field within a record.
Definition: CGExpr.cpp:4692
static void emitCheckHandlerCall(CodeGenFunction &CGF, llvm::FunctionType *FnType, ArrayRef< llvm::Value * > FnArgs, SanitizerHandler CheckHandler, CheckRecoverableKind RecoverKind, bool IsFatal, llvm::BasicBlock *ContBB)
Definition: CGExpr.cpp:3476
static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, LValueBaseInfo &BaseInfo, TBAAAccessInfo &TBAAInfo, QualType BaseTy, QualType ElTy, bool IsLowerBound)
Definition: CGExpr.cpp:4335
static void pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, const Expr *E, Address ReferenceTemporary)
Definition: CGExpr.cpp:271
StringRef Filename
Definition: Format.cpp:2952
llvm::MachO::Record Record
Definition: MachO.h:28
static const SanitizerMask AlwaysRecoverable
static const SanitizerMask Unrecoverable
static const RecordType * getRecordType(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType.
Defines the SourceManager interface.
const LValueBase getLValueBase() const
Definition: APValue.cpp:970
bool isLValue() const
Definition: APValue.h:406
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
SourceManager & getSourceManager()
Definition: ASTContext.h:700
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.
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2549
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CanQualType VoidPtrTy
Definition: ASTContext.h:1113
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:641
const LangOptions & getLangOpts() const
Definition: ASTContext.h:770
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
CanQualType BoolTy
Definition: ASTContext.h:1087
const NoSanitizeList & getNoSanitizeList() const
Definition: ASTContext.h:780
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.
Definition: ASTContext.h:2315
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
Definition: ASTContext.h:1086
const VariableArrayType * getAsVariableArrayType(QualType T) const
Definition: ASTContext.h:2745
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
unsigned getTargetAddressSpace(LangAS AS) const
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2319
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:200
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:4135
Expr * getCond() const
getCond - Return the expression representing the condition for the ?: operator.
Definition: Expr.h:4313
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition: Expr.h:4319
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition: Expr.h:4325
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2663
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:2718
Expr * getLHS()
An array access can be written A[4] or 4[A] (both are equivalent).
Definition: Expr.h:2692
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3147
QualType getElementType() const
Definition: Type.h:3159
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3834
Expr * getLHS() const
Definition: Expr.h:3883
SourceLocation getExprLoc() const
Definition: Expr.h:3874
Expr * getRHS() const
Definition: Expr.h:3885
Opcode getOpcode() const
Definition: Expr.h:3878
bool isPredefinedLibFunction(unsigned ID) const
Determines whether this builtin is a predefined libc/libm function, such as "malloc",...
Definition: Builtins.h:160
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1475
CXXTemporary * getTemporary()
Definition: ExprCXX.h:1493
const Expr * getSubExpr() const
Definition: ExprCXX.h:1497
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1530
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2792
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 isDynamicClass() const
Definition: DeclCXX.h:584
bool hasDefinition() const
Definition: DeclCXX.h:571
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:845
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1062
MSGuidDecl * getGuidDecl() const
Definition: ExprCXX.h:1108
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2819
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: Expr.h:3010
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1618
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2989
Expr * getCallee()
Definition: Expr.h:2969
arg_range arguments()
Definition: Expr.h:3058
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition: Expr.cpp:1570
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3489
path_iterator path_begin()
Definition: Expr.h:3559
CastKind getCastKind() const
Definition: Expr.h:3533
bool changesVolatileQualification() const
Return.
Definition: Expr.h:3606
path_iterator path_end()
Definition: Expr.h:3560
Expr * getSubExpr()
Definition: Expr.h:3539
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
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
SanitizerSet SanitizeRecover
Set of sanitizer checks that are non-fatal (i.e.
std::string TrapFuncName
If not an empty string, trap intrinsics are lowered to calls to this function instead of to trap inst...
An aligned address.
Definition: Address.h:29
static Address invalid()
Definition: Address.h:46
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:78
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:62
Address withPointer(llvm::Value *NewPointer, KnownNonNull_t IsKnownNonNull) const
Return address with different pointer, but same element type and alignment.
Definition: Address.h:85
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition: Address.h:100
KnownNonNull_t isKnownNonNull() const
Whether the pointer is known not to be null.
Definition: Address.h:105
Address setKnownNonNull()
Set the non-null bit.
Definition: Address.h:111
llvm::Value * getPointer() const
Definition: Address.h:51
bool isValid() const
Definition: Address.h:47
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:57
An aggregate value slot.
Definition: CGValue.h:512
static AggValueSlot ignored()
ignored - Returns an aggregate value slot indicating that the aggregate value is being ignored.
Definition: CGValue.h:580
Address getAddress() const
Definition: CGValue.h:650
void setExternallyDestructed(bool destructed=true)
Definition: CGValue.h:621
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:595
RValue asRValue() const
Definition: CGValue.h:674
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:823
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:97
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:259
Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty, llvm::Type *ElementTy, const llvm::Twine &Name="")
Definition: CGBuilder.h:156
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:193
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:172
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:71
Address CreatePreserveStructAccessIndex(Address Addr, unsigned Index, unsigned FieldIndex, llvm::MDNode *DbgInfo)
Definition: CGBuilder.h:341
Address CreateLaunderInvariantGroup(Address Addr)
Definition: CGBuilder.h:356
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:89
Address CreateConstInBoundsGEP(Address Addr, uint64_t Index, const llvm::Twine &Name="")
Given addr = T* ... produce name = getelementptr inbounds addr, i64 index where i64 is actually the t...
Definition: CGBuilder.h:213
Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:149
Address CreateGEP(Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:246
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 llvm::Value * EmitMemberPointerIsNotNull(CodeGenFunction &CGF, llvm::Value *MemPtr, const MemberPointerType *MPT)
Determine if a member pointer is non-null. Returns an i1.
Definition: CGCXXABI.cpp:87
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
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:117
Abstract information about a function or function prototype.
Definition: CGCall.h:40
const GlobalDecl getCalleeDecl() const
Definition: CGCall.h:58
All available information about a concrete callee.
Definition: CGCall.h:62
CGCalleeInfo getAbstractInfo() const
Definition: CGCall.h:172
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition: CGCall.h:164
bool isPseudoDestructor() const
Definition: CGCall.h:161
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition: CGCall.h:115
unsigned getBuiltinID() const
Definition: CGCall.h:156
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:129
bool isBuiltin() const
Definition: CGCall.h:149
const FunctionDecl * getBuiltinDecl() const
Definition: CGCall.h:152
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition: CGCall.h:123
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:55
llvm::DIType * getOrCreateStandaloneType(QualType Ty, SourceLocation Loc)
Emit standalone debug info for a type.
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 void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest, llvm::Value *ivarOffset)=0
virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest)=0
virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, QualType ObjectTy, llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers)=0
virtual llvm::Value * EmitIvarOffset(CodeGen::CodeGenFunction &CGF, const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)=0
virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, Address AddrWeakObj)=0
virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel)=0
Get the address of a selector for the specified name and type values.
virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest)=0
virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest, bool threadlocal=false)=0
virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD, Address VDAddr, SourceLocation Loc)
Returns address of the threadprivate variable for the current thread.
virtual Address 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.
bool isNontemporalDecl(const ValueDecl *VD) const
Checks if the VD variable is marked as nontemporal declaration in current context.
virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF, const Expr *LHS)
Checks if the provided LVal is lastprivate conditional and emits the code to update the value of the ...
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.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:258
void add(RValue rvalue, QualType type)
Definition: CGCall.h:282
virtual const FieldDecl * lookup(const VarDecl *VD) const
Lookup the captured field decl for a variable.
static ConstantEmission forValue(llvm::Constant *C)
static ConstantEmission forReference(llvm::Constant *C)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitFromMemory(llvm::Value *Value, QualType Ty)
EmitFromMemory - Change a scalar value from its memory representation to its value representation.
LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e)
RValue EmitLoadOfGlobalRegLValue(LValue LV)
LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E)
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E)
llvm::Value * EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr)
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, bool IsLowerBound=true)
Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
llvm::Value * EmitNonNullRValueCheck(RValue RV, QualType T)
Create a check that a scalar RValue is non-null.
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
LValue EmitCastLValue(const CastExpr *E)
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID)
Emit a call to trap or debugtrap and attach function attribute "trap-func-name" if specified.
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled.
void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK)
SanitizerSet SanOpts
Sanitizers enabled for this function.
LValue EmitCoawaitLValue(const CoawaitExpr *E)
LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its LValue mapping if it exists, otherwise create one.
LValue EmitAggExprToLValue(const Expr *E)
EmitAggExprToLValue - Emit the computation of the specified expression of aggregate type into a tempo...
llvm::Value * EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue)
LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E)
CGCapturedStmtInfo * CapturedStmtInfo
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
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.
Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Load a pointer with type PtrTy stored at address Ptr.
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc)
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
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.
RValue EmitAtomicLoad(LValue LV, SourceLocation SL, AggValueSlot Slot=AggValueSlot::ignored())
llvm::Value * EmitIvarOffset(const ObjCInterfaceDecl *Interface, const ObjCIvarDecl *Ivar)
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
LValue EmitBinaryOperatorLValue(const BinaryOperator *E)
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E)
LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
llvm::Value * EmitARCLoadWeakRetained(Address addr)
Address CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
const LangOptions & getLangOpts() const
llvm::Value * LoadPassedObjectSize(const Expr *E, QualType EltTy)
If E references a parameter with pass_object_size info or a constant array size modifier,...
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
EmitLValueForFieldInitialization - Like EmitLValueForField, except that if the Field is a reference,...
LValue EmitInitListLValue(const InitListExpr *E)
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID)
Create a basic block that will call the trap intrinsic, and emit a conditional branch to it,...
void EmitUnreachable(SourceLocation Loc)
Emit a reached-unreachable diagnostic if Loc is valid and runtime checking is enabled.
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...
Address EmitLoadOfReference(LValue RefLVal, LValueBaseInfo *PointeeBaseInfo=nullptr, TBAAAccessInfo *PointeeTBAAInfo=nullptr)
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
RValue convertTempToRValue(Address addr, QualType type, SourceLocation Loc)
Address EmitExtVectorElementLValue(LValue V)
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...
@ 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.
void SetDivFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, ReturnValueSlot Return=ReturnValueSlot())
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
LValue EmitCallExprLValue(const CallExpr *E)
LValue EmitUnsupportedLValue(const Expr *E, const char *Name)
EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue an ErrorUnsupported style ...
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type,...
llvm::Type * ConvertTypeForMem(QualType T)
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex)
Get the record field index as represented in debug info.
LValue EmitLValueForField(LValue Base, const FieldDecl *Field)
LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK)
Same as EmitLValue but additionally we generate checking code to guard against undefined behavior.
@ ForceLeftToRight
! Language semantics require left-to-right evaluation.
@ Default
! No language constraints on evaluation order.
@ ForceRightToLeft
! Language semantics require right-to-left evaluation.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, llvm::Value **Result=nullptr)
EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints as EmitStoreThroughLValue.
void EmitCfiCheckStub()
Emit a stub for the cross-DSO CFI check function.
llvm::Value * EmitObjCConsumeObject(QualType T, llvm::Value *Ptr)
ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr)
llvm::Value * EmitARCLoadWeak(Address addr)
const TargetInfo & getTarget() const
LValue EmitCXXConstructLValue(const CXXConstructExpr *E)
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
llvm::Value * getTypeSize(QualType Ty)
Returns calculated size of the specified type.
std::pair< LValue, llvm::Value * > EmitARCStoreAutoreleasing(const BinaryOperator *e)
LValue EmitVAArgExprLValue(const VAArgExpr *E)
bool EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, SourceLocation Loc)
Check if the scalar Value is within the valid range for the given type Ty.
llvm::Function * generateDestroyHelper(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray, const VarDecl *VD)
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...
llvm::Value * EmitCountedByFieldExpr(const Expr *Base, const FieldDecl *FAMDecl, const FieldDecl *CountDecl)
Build an expression accessing the "counted_by" field.
LValue EmitCoyieldLValue(const CoyieldExpr *E)
RValue EmitAnyExprToTemp(const Expr *E)
EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will always be accessible even if...
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue)
Address EmitArrayToPointerDecay(const Expr *Array, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
LValue EmitUnaryOpLValue(const UnaryOperator *E)
ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
RValue EmitUnsupportedRValue(const Expr *E, const char *Name)
EmitUnsupportedRValue - Emit a dummy r-value using the type of E and issue an ErrorUnsupported style ...
static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts)
getAccessedFieldNo - Given an encoded value and a result number, return the input field number being ...
RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, const CallExpr *E, ReturnValueSlot ReturnValue)
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.
void EmitBoundsCheckImpl(const Expr *E, llvm::Value *Bound, llvm::Value *Index, QualType IndexType, QualType IndexedType, bool Accessed)
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.
void EmitCfiCheckFail()
Emit a cross-DSO CFI failure handling function.
RValue EmitLoadOfExtVectorElementLValue(LValue V)
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
static bool ShouldNullCheckClassCastValue(const CastExpr *Cast)
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type,...
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
LValue EmitComplexAssignmentLValue(const BinaryOperator *E)
Emit an l-value for an assignment (simple or compound) of complex type.
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E)
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...
LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E)
Address emitAddrOfImagComponent(Address complex, QualType complexType)
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
const TargetCodeGenInfo & getTargetHooks() const
llvm::Value * emitBoolVecConversion(llvm::Value *SrcVec, unsigned NumElementsDst, const llvm::Twine &Name="")
LValue EmitPredefinedLValue(const PredefinedExpr *E)
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
Address GetAddressOfDerivedClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst)
llvm::Value * EmitToMemory(llvm::Value *Value, QualType Ty)
EmitToMemory - Change a scalar value from its value representation to its in-memory representation.
llvm::Value * EmitCheckValue(llvm::Value *V)
Convert a value into a format suitable for passing to a runtime sanitizer handler.
void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, Address Ptr)
bool IsInPreservedAIRegion
True if CodeGen currently emits code inside presereved access index region.
llvm::Value * EmitARCRetain(QualType type, llvm::Value *value)
void SetSqrtFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Address CreateMemTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen without...
RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot=AggValueSlot::ignored())
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * EmitARCStoreStrong(LValue lvalue, llvm::Value *value, bool resultIgnored)
LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E)
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...
CGCallee EmitCallee(const Expr *E)
Address CreateMemTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
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.
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy)
void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init)
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
llvm::Instruction * getPostAllocaInsertPoint()
Return PostAllocaInsertPt.
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 ...
LValue EmitMemberExpr(const MemberExpr *E)
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E, bool Accessed=false)
RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue)
bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, bool AllowLabels=false)
ConstantFoldsToSimpleInteger - If the specified expression does not fold to a constant,...
llvm::ConstantInt * getUBSanFunctionTypeHash(QualType T) const
Return a type hash constant for a function instrumented by -fsanitize=function.
llvm::DenseMap< const ValueDecl *, FieldDecl * > LambdaCaptureFields
CleanupKind getCleanupKind(QualType::DestructionKind kind)
void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest)
llvm::Type * ConvertType(QualType T)
llvm::Value * EmitCXXTypeidExpr(const CXXTypeidExpr *E)
RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, ReturnValueSlot ReturnValue)
Address GetAddrOfBlockDecl(const VarDecl *var)
CodeGenTypes & getTypes() const
void EmitARCInitWeak(Address addr, llvm::Value *value)
bool IsSanitizerScope
True if CodeGen currently emits code implementing sanitizer checks.
LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E)
void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst)
Address EmitCXXUuidofExpr(const CXXUuidofExpr *E)
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args)
LValue EmitStringLiteralLValue(const StringLiteral *E)
static Destroyer destroyARCStrongPrecise
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Address CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
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...
llvm::Value * EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr)
llvm::Value * EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE)
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
LValue EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E)
LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e)
static bool hasAggregateEvaluationKind(QualType T)
llvm::Value * EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
const FieldDecl * FindCountedByField(const FieldDecl *FD)
Find the FieldDecl specified in a FAM's "counted_by" attribute.
void SetFPAccuracy(llvm::Value *Val, float Accuracy)
SetFPAccuracy - Set the minimum required accuracy of the given floating point operation,...
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
LValue EmitLoadOfReferenceLValue(LValue RefLVal)
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
LValue EmitObjCIsaExpr(const ObjCIsaExpr *E)
void EmitCfiSlowPathCheck(SanitizerMask Kind, 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.
void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit)
Address EmitFieldAnnotations(const FieldDecl *D, Address V)
Emit field annotations for the given field & value.
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...
AggValueSlot CreateAggTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateAggTemp - Create a temporary memory object for the given aggregate type.
LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E)
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
static bool isNullPointerAllowed(TypeCheckKind TCK)
Determine whether the pointer type check TCK permits null pointers.
static Destroyer destroyARCStrongImprecise
RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its RValue mapping if it exists, otherwise create one.
void EmitIgnoredConditionalOperator(const AbstractConditionalOperator *E)
RValue GetUndefRValue(QualType Ty)
GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
LValue EmitLValueForIvar(QualType ObjectTy, llvm::Value *Base, const ObjCIvarDecl *Ivar, unsigned CVRQualifiers)
RValue EmitCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue=ReturnValueSlot())
RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue)
llvm::Value * emitScalarConstant(const ConstantEmission &Constant, Expr *E)
LValue EmitStmtExprLValue(const StmtExpr *E)
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
llvm::LLVMContext & getLLVMContext()
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
bool LValueIsSuitableForInlineAtomic(LValue Src)
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
Address emitAddrOfRealComponent(Address complex, QualType complexType)
static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty)
Determine whether the pointer type check TCK requires a vptr check.
LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E)
LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E)
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...
LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E)
RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc)
LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E)
LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E)
LValue EmitLValueForLambdaField(const FieldDecl *Field)
static bool IsWrappedCXXThis(const Expr *E)
Check if E is a C++ "this" pointer wrapped in value-preserving casts.
This class organizes the cross-function state that is used while generating LLVM code.
ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD)
Get the address of a GUID.
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition: CGExpr.cpp:1247
void setDSOLocal(llvm::GlobalValue *GV) const
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.
CGDebugInfo * getModuleDebugInfo()
ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E)
Returns a pointer to a constant global variable for the given file-scope compound literal expression.
llvm::ConstantInt * CreateCrossDsoCfiTypeId(llvm::Metadata *MD)
Generate a cross-DSO type identifier for MD.
void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C)
llvm::FunctionCallee getAddrAndTypeOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Definition: CGCXX.cpp:220
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
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.
Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, CharUnits Align)
Definition: CGDecl.cpp:1131
DiagnosticsEngine & getDiags() const
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)
const TargetInfo & getTarget() const
llvm::Metadata * CreateMetadataIdentifierForType(QualType T)
Create a metadata identifier for the given type.
llvm::Constant * getTypeDescriptorFromMap(QualType Ty)
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
llvm::MDNode * getTBAABaseTypeInfo(QualType QTy)
getTBAABaseTypeInfo - Get metadata that describes the given base access type.
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD)
Returns LLVM linkage for a declarator.
const llvm::DataLayout & getDataLayout() const
CGCXXABI & getCXXABI() const
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
SanitizerMetadata * getSanitizerMetadata()
llvm::Metadata * CreateMetadataIdentifierGeneralized(QualType T)
Create a metadata identifier for the generalization of the given type.
const llvm::Triple & getTriple() const
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:243
void DecorateInstructionWithTBAA(llvm::Instruction *Inst, TBAAAccessInfo TBAAInfo)
DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType)
getTBAAInfoForSubobject - Get TBAA information for an access with a given base lvalue.
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type.
ConstantAddress GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
ASTContext & getContext() const
ConstantAddress GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO)
Get the address of a template parameter object.
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.
llvm::MDNode * getTBAATypeInfo(QualType QTy)
getTBAATypeInfo - Get metadata used to describe accesses to objects of the given type.
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
CharUnits getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purposes of conditional ope...
llvm::LLVMContext & getLLVMContext()
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
CharUnits getMinimumObjectSize(QualType Ty)
Returns the minimum object size for an object of the given type.
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=std::nullopt)
ConstantAddress GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
ConstantAddress GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr)
Returns a pointer to a character array containing the literal and a terminating '\0' character.
void setCurrentStmt(const Stmt *S)
If the execution count for the current statement is known, record that as the current count.
Definition: CodeGenPGO.h:76
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1625
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:674
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:104
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments.
Definition: CGCall.cpp:633
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:120
ConstantAddress withElementType(llvm::Type *ElemTy) const
Definition: Address.h:136
llvm::Constant * getPointer() const
Definition: Address.h:132
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
llvm::Constant * tryEmitConstantExpr(const ConstantExpr *CE)
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:352
void mergeForCast(const LValueBaseInfo &Info)
Definition: CGValue.h:163
AlignmentSource getAlignmentSource() const
Definition: CGValue.h:160
LValue - This represents an lvalue references.
Definition: CGValue.h:171
bool isBitField() const
Definition: CGValue.h:268
bool isMatrixElt() const
Definition: CGValue.h:271
Expr * getBaseIvarExp() const
Definition: CGValue.h:320
llvm::Constant * getExtVectorElts() const
Definition: CGValue.h:398
static LValue MakeGlobalReg(llvm::Value *V, CharUnits alignment, QualType type)
Definition: CGValue.h:480
void setObjCIvar(bool Value)
Definition: CGValue.h:286
bool isObjCArray() const
Definition: CGValue.h:288
static LValue MakeExtVectorElt(Address vecAddress, llvm::Constant *Elts, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:446
bool isObjCStrong() const
Definition: CGValue.h:312
bool isGlobalObjCRef() const
Definition: CGValue.h:294
bool isVectorElt() const
Definition: CGValue.h:267
void setObjCArray(bool Value)
Definition: CGValue.h:289
bool isSimple() const
Definition: CGValue.h:266
bool isVolatileQualified() const
Definition: CGValue.h:273
static LValue MakeAddr(Address address, QualType type, ASTContext &Context, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:417
CharUnits getAlignment() const
Definition: CGValue.h:331
llvm::Value * getMatrixIdx() const
Definition: CGValue.h:384
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:350
llvm::Value * getGlobalReg() const
Definition: CGValue.h:415
bool isVolatile() const
Definition: CGValue.h:316
const Qualifiers & getQuals() const
Definition: CGValue.h:326
bool isGlobalReg() const
Definition: CGValue.h:270
bool isObjCWeak() const
Definition: CGValue.h:309
unsigned getVRQualifiers() const
Definition: CGValue.h:275
void setThreadLocalRef(bool Value)
Definition: CGValue.h:298
LValue setKnownNonNull()
Definition: CGValue.h:340
bool isNonGC() const
Definition: CGValue.h:291
void setGlobalObjCRef(bool Value)
Definition: CGValue.h:295
bool isExtVectorElt() const
Definition: CGValue.h:269
llvm::Value * getVectorIdx() const
Definition: CGValue.h:371
void setNontemporal(bool Value)
Definition: CGValue.h:307
LValueBaseInfo getBaseInfo() const
Definition: CGValue.h:334
llvm::Value * getPointer(CodeGenFunction &CGF) const
Definition: CGValue.h:346
void setARCPreciseLifetime(ARCPreciseLifetime_t value)
Definition: CGValue.h:303
QualType getType() const
Definition: CGValue.h:279
const CGBitFieldInfo & getBitFieldInfo() const
Definition: CGValue.h:409
bool isThreadLocalRef() const
Definition: CGValue.h:297
KnownNonNull_t isKnownNonNull() const
Definition: CGValue.h:337
TBAAAccessInfo getTBAAInfo() const
Definition: CGValue.h:323
void setNonGC(bool Value)
Definition: CGValue.h:292
Address getVectorAddress() const
Definition: CGValue.h:363
bool isNontemporal() const
Definition: CGValue.h:306
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:466
bool isObjCIvar() const
Definition: CGValue.h:285
static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:432
void setAddress(Address address)
Definition: CGValue.h:354
void setBaseIvarExp(Expr *V)
Definition: CGValue.h:321
RValue asAggregateRValue(CodeGenFunction &CGF) const
Definition: CGValue.h:506
Address getExtVectorAddress() const
Definition: CGValue.h:390
static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx, QualType type, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:492
Address getMatrixAddress() const
Definition: CGValue.h:376
Address getBitFieldAddress() const
Definition: CGValue.h:404
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
bool isScalar() const
Definition: CGValue.h:54
static RValue get(llvm::Value *V)
Definition: CGValue.h:89
static RValue getAggregate(Address addr, bool isVolatile=false)
Definition: CGValue.h:110
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:96
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition: CGValue.h:73
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:61
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:356
void disableSanitizerForGlobal(llvm::GlobalVariable *GV)
virtual llvm::Value * performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, llvm::Value *V, LangAS SrcAddr, LangAS DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Perform address space cast of an expression of pointer type.
Definition: TargetInfo.cpp:132
virtual llvm::Constant * getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const
Return a constant used by UBSan as a signature to identify functions possessing type information,...
Definition: TargetInfo.h:212
Complex values, per C99 6.2.5p11.
Definition: Type.h:2845
QualType getElementType() const
Definition: Type.h:2855
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3419
bool isFileScope() const
Definition: Expr.h:3446
const Expr * getInitializer() const
Definition: Expr.h:3442
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:195
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1072
Represents a concrete matrix type with constant number of rows and columns.
Definition: Type.h:3710
unsigned getNumRows() const
Returns the number of rows in the matrix.
Definition: Type.h:3728
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1379
reference front() const
Definition: DeclBase.h:1402
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1785
RecordDecl * getOuterLexicalRecordContext()
Retrieve the outermost lexically enclosing record context.
Definition: DeclBase.cpp:1947
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2332
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Definition: Expr.h:1458
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:488
ValueDecl * getDecl()
Definition: Expr.h:1328
NonOdrUseReason isNonOdrUse() const
Is this expression a non-odr-use reference, and if so, why?
Definition: Expr.h:1452
SourceLocation getLocation() const
Definition: Expr.h:1336
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
T * getAttr() const
Definition: DeclBase.h:578
SourceLocation getLocation() const
Definition: DeclBase.h:444
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition: DeclBase.cpp:530
DeclContext * getDeclContext()
Definition: DeclBase.h:453
bool hasAttr() const
Definition: DeclBase.h:582
The name of a declaration.
void ConvertArgToString(ArgumentKind Kind, intptr_t Val, StringRef Modifier, StringRef Argument, ArrayRef< ArgumentValue > PrevArgs, SmallVectorImpl< char > &Output, ArrayRef< intptr_t > QualTypeVals) const
Converts a diagnostic argument (as an intptr_t) into the string that represents it.
Definition: Diagnostic.h:880
Represents an enum.
Definition: Decl.h:3832
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition: Decl.h:4046
void getValueRange(llvm::APInt &Max, llvm::APInt &Min) const
Calculates the [Min,Max) values the enum can store based on the NumPositiveBits and NumNegativeBits.
Definition: Decl.cpp:4940
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:5118
EnumDecl * getDecl() const
Definition: Type.h:5125
ExplicitCastExpr - An explicit cast written in the source code.
Definition: Expr.h:3724
This represents one expression.
Definition: Expr.h:110
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:82
bool isGLValue() const
Definition: Expr.h:280
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:3072
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:437
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:3045
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3033
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3041
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:278
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:1525
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:3542
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3025
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
bool isFlexibleArrayMemberLike(ASTContext &Context, LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, bool IgnoreTemplateOrMacroSubstitution=false) const
Check whether this array fits the idiom of a flexible array member, depending on the value of -fstric...
Definition: Expr.cpp:206
QualType getType() const
Definition: Expr.h:142
bool isOBJCGCCandidate(ASTContext &Ctx) const
isOBJCGCCandidate - Return true if this expression may be used in a read/ write barrier.
Definition: Expr.cpp:2956
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition: Expr.h:6107
bool isArrow() const
isArrow - Return true if the base expression is a pointer to vector, return false if the base express...
Definition: Expr.cpp:4264
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
Definition: Expr.cpp:4296
const Expr * getBase() const
Definition: Expr.h:6124
Represents a member of a struct/union/class.
Definition: Decl.h:3025
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:3116
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition: Decl.cpp:4611
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3238
bool isZeroSize(const ASTContext &Ctx) const
Determine if this field is a subobject of zero size, that is, either a zero-length bit-field or a fie...
Definition: Decl.cpp:4569
FullExpr - Represents a "full-expression" node.
Definition: Expr.h:1039
const Expr * getSubExpr() const
Definition: Expr.h:1052
Represents a function declaration or definition.
Definition: Decl.h:1959
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3597
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4199
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
const Decl * getDecl() const
Definition: GlobalDecl.h:103
Describes an C or C++ initializer list.
Definition: Expr.h:4841
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
Definition: Expr.cpp:2418
const Expr * getInit(unsigned Init) const
Definition: Expr.h:4887
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:424
virtual void mangleCXXRTTI(QualType T, raw_ostream &)=0
unsigned getBlockId(const BlockDecl *BD, bool Local)
Definition: Mangle.h:84
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4679
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition: ExprCXX.h:4704
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition: ExprCXX.h:4696
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition: ExprCXX.h:4729
MatrixSubscriptExpr - Matrix subscript expression for the MatrixType extension.
Definition: Expr.h:2741
bool isIncomplete() const
Definition: Expr.h:2761
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3182
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3261
NonOdrUseReason isNonOdrUse() const
Is this expression a non-odr-use reference, and if so, why? This is only meaningful if the named memb...
Definition: Expr.h:3402
Expr * getBase() const
Definition: Expr.h:3255
bool isArrow() const
Definition: Expr.h:3362
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:3373
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:3089
bool isObjCBOOLType(QualType T) const
Returns true if.
Definition: NSAPI.cpp:506
This represents a decl that may have a name.
Definition: Decl.h:249
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:276
A C++ nested-name-specifier augmented with source location information.
bool containsType(SanitizerMask Mask, StringRef MangledTypeName, StringRef Category=StringRef()) const
OpenMP 5.0 [2.1.5, Array Sections].
Definition: ExprOpenMP.h:56
Expr * getLength()
Get length of array section.
Definition: ExprOpenMP.h:102
SourceLocation getColonLocFirst() const
Definition: ExprOpenMP.h:118
SourceLocation getExprLoc() const LLVM_READONLY
Definition: ExprOpenMP.h:127
Expr * getBase()
An array section can be written only as Base[LowerBound:Length].
Definition: ExprOpenMP.h:85
static QualType getBaseOriginalType(const Expr *Base)
Return original type of the base expression for array section.
Definition: Expr.cpp:5049
Expr * getLowerBound()
Get lower bound of array section.
Definition: ExprOpenMP.h:94
ObjCEncodeExpr, used for @encode in Objective-C.
Definition: ExprObjC.h:410
Represents an ObjC class declaration.
Definition: DeclObjC.h:1150
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1947
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:549
ObjCIvarDecl * getDecl()
Definition: ExprObjC.h:579
bool isArrow() const
Definition: ExprObjC.h:587
const Expr * getBase() const
Definition: ExprObjC.h:583
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:945
const ObjCMethodDecl * getMethodDecl() const
Definition: ExprObjC.h:1356
QualType getReturnType() const
Definition: DeclObjC.h:329
Represents a class type in Objective C.
Definition: Type.h:6297
ObjCSelectorExpr used for @selector in Objective-C.
Definition: ExprObjC.h:455
Selector getSelector() const
Definition: ExprObjC.h:469
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1168
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition: Expr.h:1218
bool isUnique() const
Definition: Expr.h:1226
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:2129
const Expr * getSubExpr() const
Definition: Expr.h:2144
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2898
QualType getPointeeType() const
Definition: Type.h:2908
[C99 6.4.2.2] - A predefined identifier such as func.
Definition: Expr.h:1986
StringRef getIdentKindName() const
Definition: Expr.h:2043
PredefinedIdentKind getIdentKind() const
Definition: Expr.h:2021
StringLiteral * getFunctionName()
Definition: Expr.h:2030
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.
bool isValid() const
unsigned getLine() const
Return the presumed line number of this location.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6299
semantics_iterator semantics_end()
Definition: Expr.h:6371
semantics_iterator semantics_begin()
Definition: Expr.h:6365
const Expr *const * const_semantics_iterator
Definition: Expr.h:6364
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition: Expr.h:6352
A (possibly-)qualified type.
Definition: Type.h:737
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6985
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:804
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:7027
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6942
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1229
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:7102
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6995
QualType withCVRQualifiers(unsigned CVR) const
Definition: Type.h:971
void * getAsOpaquePtr() const
Definition: Type.h:784
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition: Type.h:1323
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition: Type.h:836
The collection of all-type qualifiers we support.
Definition: Type.h:147
unsigned getCVRQualifiers() const
Definition: Type.h:295
GC getObjCGCAttr() const
Definition: Type.h:326
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition: Type.h:175
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition: Type.h:168
@ OCL_None
There is no lifetime qualification on this type.
Definition: Type.h:164
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition: Type.h:178
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition: Type.h:181
bool hasConst() const
Definition: Type.h:264
void addCVRQualifiers(unsigned mask)
Definition: Type.h:309
void removeObjCGCAttr()
Definition: Type.h:330
void addQualifiers(Qualifiers Q)
Add the qualifiers from the given set to this set.
Definition: Type.h:443
void setAddressSpace(LangAS space)
Definition: Type.h:398
bool hasVolatile() const
Definition: Type.h:274
ObjCLifetime getObjCLifetime() const
Definition: Type.h:352
void addVolatile()
Definition: Type.h:277
Represents a struct/union/class.
Definition: Decl.h:4133
field_range fields() const
Definition: Decl.h:4339
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4324
bool isAnonymousStructOrUnion() const
Whether this is an anonymous struct or union.
Definition: Decl.h:4185
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5092
decl_type * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
Definition: Redeclarable.h:204
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4377
Stmt - This represents one statement.
Definition: Stmt.h:84
StmtClass getStmtClass() const
Definition: Stmt.h:1356
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:338
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1773
bool isUnion() const
Definition: Decl.h:3755
Exposes information about the current target.
Definition: TargetInfo.h:213
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1220
virtual StringRef getABI() const
Get the ABI currently in use.
Definition: TargetInfo.h:1288
The type-property cache.
Definition: Type.cpp:4261
The base class of the type hierarchy.
Definition: Type.h:1606
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1819
bool isBlockPointerType() const
Definition: Type.h:7162
bool isVoidType() const
Definition: Type.h:7443
bool isBooleanType() const
Definition: Type.h:7567
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:2104
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:1836
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition: Type.cpp:2083
const ArrayType * castAsArrayTypeUnsafe() const
A variant of castAs<> for array type which silently discards qualifiers from the outermost type.
Definition: Type.h:7733
bool isArrayType() const
Definition: Type.h:7220
bool isFunctionPointerType() const
Definition: Type.h:7188
bool isArithmeticType() const
Definition: Type.cpp:2218
bool isConstantMatrixType() const
Definition: Type.h:7274
bool isPointerType() const
Definition: Type.h:7154
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition: Type.h:7479
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7724
bool isReferenceType() const
Definition: Type.h:7166
bool isVariableArrayType() const
Definition: Type.h:7232
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:651
bool isExtVectorBoolType() const
Definition: Type.h:7264
bool isAnyComplexType() const
Definition: Type.h:7252
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:7607
bool isAtomicType() const
Definition: Type.h:7295
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:2438
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2299
bool isFunctionType() const
Definition: Type.h:7150
bool isObjCObjectPointerType() const
Definition: Type.h:7282
bool isVectorType() const
Definition: Type.h:7256
bool isFloatingType() const
Definition: Type.cpp:2186
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7657
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition: Type.cpp:565
bool isRecordType() const
Definition: Type.h:7244
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1823
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2182
SourceLocation getExprLoc() const
Definition: Expr.h:2310
Expr * getSubExpr() const
Definition: Expr.h:2227
Opcode getOpcode() const
Definition: Expr.h:2222
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:4661
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:706
QualType getType() const
Definition: Decl.h:717
QualType getType() const
Definition: Value.cpp:234
Represents a variable declaration or definition.
Definition: Decl.h:918
TLSKind getTLSKind() const
Definition: Decl.cpp:2165
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition: Decl.cpp:2363
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1168
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition: Decl.h:944
@ TLS_None
Not a TLS variable.
Definition: Decl.h:938
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: Type.h:3290
Represents a GCC generic vector type.
Definition: Type.h:3512
unsigned getNumElements() const
Definition: Type.h:3527
#define INT_MIN
Definition: limits.h:51
AlignmentSource
The source of the alignment of an l-value; an expression of confidence in the alignment actually matc...
Definition: CGValue.h:130
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
@ ARCImpreciseLifetime
Definition: CGValue.h:125
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:148
@ NotKnownNonNull
Definition: Address.h:26
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const AstTypeMatcher< FunctionType > functionType
Matches FunctionType nodes.
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:27
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1834
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1809
bool Load(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1369
bool Cast(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1675
The JSON file list parser is used to communicate input to InstallAPI.
@ OpenCL
Definition: LangStandard.h:64
@ CPlusPlus
Definition: LangStandard.h:54
@ OK_BitField
A bitfield object is a bitfield on a C or C++ record.
Definition: Specifiers.h:151
@ SC_Register
Definition: Specifiers.h:254
@ 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:323
@ SD_Thread
Thread storage duration.
Definition: Specifiers.h:326
@ SD_Static
Static storage duration.
Definition: Specifiers.h:327
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition: Specifiers.h:324
@ SD_Automatic
Automatic storage duration (most local variables).
Definition: Specifiers.h:325
@ SD_Dynamic
Dynamic storage duration.
Definition: Specifiers.h:328
@ Result
The result type of a method or function.
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
llvm::hash_code hash_value(const CustomizableOptional< T > &O)
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:86
@ Interface
The "__interface" keyword introduces the elaborated-type-specifier.
@ Other
Other implicit parameter.
@ NOUR_Unevaluated
This name appears in an unevaluated operand.
Definition: Specifiers.h:174
@ NOUR_Constant
This name appears as a potential result of an lvalue-to-rvalue conversion that is a constant expressi...
Definition: Specifiers.h:177
unsigned long uint64_t
__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.
Definition: CodeGenTBAA.h:105
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:109
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:63
uint64_t Size
Size - The size of access, in bytes.
Definition: CodeGenTBAA.h:112
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:101
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:642
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:644
bool HasSideEffects
Whether the evaluated expression has side effects.
Definition: Expr.h:609
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:168
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:159
An adjustment to be made to the temporary created when emitting a reference binding,...
Definition: Expr.h:66