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);