LLVM 23.0.0git
AMDGPUMCExpr.cpp
Go to the documentation of this file.
1//===- AMDGPUMCExpr.cpp - AMDGPU specific MC expression classes -----------===//
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#include "AMDGPUMCExpr.h"
11#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCSymbol.h"
17#include "llvm/MC/MCValue.h"
22#include <functional>
23#include <optional>
24
25using namespace llvm;
26using namespace llvm::AMDGPU;
27
28AMDGPUMCExpr::AMDGPUMCExpr(VariantKind Kind, ArrayRef<const MCExpr *> Args,
29 MCContext &Ctx)
30 : Kind(Kind), Ctx(Ctx) {
31 assert(Args.size() >= 1 && "Needs a minimum of one expression.");
32 assert(Kind != AGVK_None && "Cannot construct AMDGPUMCExpr of kind none.");
33
34 // Allocating the variadic arguments through the same allocation mechanism
35 // that the object itself is allocated with so they end up in the same memory.
36 //
37 // Will result in an asan failure if allocated on the heap through standard
38 // allocation (e.g., through SmallVector's grow).
39 RawArgs = static_cast<const MCExpr **>(
40 Ctx.allocate(sizeof(const MCExpr *) * Args.size()));
41 llvm::uninitialized_copy(Args, RawArgs);
42 this->Args = ArrayRef<const MCExpr *>(RawArgs, Args.size());
43}
44
45AMDGPUMCExpr::~AMDGPUMCExpr() { Ctx.deallocate(RawArgs); }
46
47const AMDGPUMCExpr *AMDGPUMCExpr::create(VariantKind Kind,
49 MCContext &Ctx) {
50 return new (Ctx) AMDGPUMCExpr(Kind, Args, Ctx);
51}
52
53const MCExpr *AMDGPUMCExpr::getSubExpr(size_t Index) const {
54 assert(Index < Args.size() && "Indexing out of bounds AMDGPUMCExpr sub-expr");
55 return Args[Index];
56}
57
58void AMDGPUMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
59 switch (Kind) {
60 default:
61 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
62 case AGVK_Or:
63 OS << "or(";
64 break;
65 case AGVK_Max:
66 OS << "max(";
67 break;
68 case AGVK_ExtraSGPRs:
69 OS << "extrasgprs(";
70 break;
72 OS << "totalnumvgprs(";
73 break;
74 case AGVK_AlignTo:
75 OS << "alignto(";
76 break;
77 case AGVK_Occupancy:
78 OS << "occupancy(";
79 break;
81 OS << "instprefsize(";
82 break;
83 case AGVK_Lit:
84 OS << "lit(";
85 break;
86 case AGVK_Lit64:
87 OS << "lit64(";
88 break;
89 }
90 for (const auto *It = Args.begin(); It != Args.end(); ++It) {
91 MAI->printExpr(OS, **It);
92 if ((It + 1) != Args.end())
93 OS << ", ";
94 }
95 OS << ')';
96}
97
98static int64_t op(AMDGPUMCExpr::VariantKind Kind, int64_t Arg1, int64_t Arg2) {
99 switch (Kind) {
100 default:
101 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
103 return std::max(Arg1, Arg2);
105 return Arg1 | Arg2;
106 }
107}
108
109static bool
111 std::initializer_list<std::reference_wrapper<uint64_t>> Vals) {
112 return llvm::all_of(llvm::zip_equal(Exprs, Vals), [&](const auto &Pair) {
113 auto [Expr, ValRef] = Pair;
114 uint64_t &Val = ValRef.get();
115 MCValue MCVal;
116 if (!Expr->evaluateAsRelocatable(MCVal, Asm) || !MCVal.isAbsolute())
117 return false;
118 Val = MCVal.getConstant();
119 return true;
120 });
121}
122
123bool AMDGPUMCExpr::evaluateExtraSGPRs(MCValue &Res,
124 const MCAssembler *Asm) const {
125 const MCSubtargetInfo &STI = *Ctx.getSubtargetInfo();
126 uint64_t VCCUsed = 0, FlatScrUsed = 0, XNACKUsed = 0;
127
128 if (!evaluateMCExprs(Args, Asm, {VCCUsed, FlatScrUsed, XNACKUsed}))
129 return false;
130
131 uint64_t ExtraSGPRs = IsaInfo::getNumExtraSGPRs(
132 STI, (bool)VCCUsed, (bool)FlatScrUsed, (bool)XNACKUsed);
133 Res = MCValue::get(ExtraSGPRs);
134 return true;
135}
136
137bool AMDGPUMCExpr::evaluateTotalNumVGPR(MCValue &Res,
138 const MCAssembler *Asm) const {
139 const MCSubtargetInfo &STI = *Ctx.getSubtargetInfo();
140 uint64_t NumAGPR = 0, NumVGPR = 0;
141
142 bool Has90AInsts = AMDGPU::isGFX90A(STI);
143
144 if (!evaluateMCExprs(Args, Asm, {NumAGPR, NumVGPR}))
145 return false;
146
147 uint64_t TotalNum = Has90AInsts && NumAGPR ? alignTo(NumVGPR, 4) + NumAGPR
148 : std::max(NumVGPR, NumAGPR);
149 Res = MCValue::get(TotalNum);
150 return true;
151}
152
153bool AMDGPUMCExpr::evaluateAlignTo(MCValue &Res, const MCAssembler *Asm) const {
154 uint64_t Value = 0, Align = 0;
155 if (!evaluateMCExprs(Args, Asm, {Value, Align}))
156 return false;
157
158 Res = MCValue::get(alignTo(Value, Align));
159 return true;
160}
161
162bool AMDGPUMCExpr::evaluateOccupancy(MCValue &Res,
163 const MCAssembler *Asm) const {
164 uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, Generation,
166
168 Args.slice(0, 5), Asm,
169 {MaxWaves, Granule, TargetTotalNumVGPRs, Generation, InitOccupancy});
170
171 assert(Success && "Arguments 1 to 5 for Occupancy should be known constants");
172
173 if (!Success || !evaluateMCExprs(Args.slice(5, 2), Asm, {NumSGPRs, NumVGPRs}))
174 return false;
175
176 unsigned Occupancy = InitOccupancy;
177 if (NumSGPRs)
178 Occupancy = std::min(
180 NumSGPRs, MaxWaves,
181 static_cast<AMDGPUSubtarget::Generation>(Generation)));
182 if (NumVGPRs)
183 Occupancy = std::min(Occupancy,
185 NumVGPRs, Granule, MaxWaves, TargetTotalNumVGPRs));
186
187 Res = MCValue::get(Occupancy);
188 return true;
189}
190
191/// Get the inst_pref_size field width for the given subtarget.
192static unsigned getInstPrefSizeFieldWidth(const MCSubtargetInfo &STI) {
193 if (AMDGPU::isGFX12Plus(STI))
194 return amdhsa::COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE_WIDTH;
195 return amdhsa::COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE_WIDTH;
196}
197
198bool AMDGPUMCExpr::evaluateInstPrefSize(MCValue &Res,
199 const MCAssembler *Asm) const {
200 uint64_t CodeSizeInBytes = 0;
201 if (!evaluateMCExprs(Args, Asm, {CodeSizeInBytes}))
202 return false;
203 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
204 unsigned FieldWidth = getInstPrefSizeFieldWidth(*STI);
206 uint64_t CodeSizeInLines = divideCeil(CodeSizeInBytes, CacheLineSize);
207 uint64_t MaxVal = (1u << FieldWidth) - 1;
208 Res = MCValue::get(std::min(CodeSizeInLines, MaxVal));
209 return true;
210}
211
213 const MCExpr *E) {
214 switch (E->getKind()) {
215 case MCExpr::Constant:
216 return false;
217 case MCExpr::Unary:
219 Sym, static_cast<const MCUnaryExpr *>(E)->getSubExpr());
220 case MCExpr::Binary: {
221 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(E);
222 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
224 }
225 case MCExpr::SymbolRef: {
226 const MCSymbol &S = static_cast<const MCSymbolRefExpr *>(E)->getSymbol();
227 if (S.isVariable())
229 return &S == Sym;
230 }
232 case MCExpr::Target: {
233 auto *TE = static_cast<const AMDGPUMCExpr *>(E);
234 for (const MCExpr *E : TE->getArgs())
235 if (isSymbolUsedInExpression(Sym, E))
236 return true;
237 return false;
238 }
239 }
240 llvm_unreachable("Unknown expr kind!");
241}
242
244 const MCAssembler *Asm) const {
245 std::optional<int64_t> Total;
246 switch (Kind) {
247 default:
248 break;
249 case AGVK_ExtraSGPRs:
250 return evaluateExtraSGPRs(Res, Asm);
251 case AGVK_AlignTo:
252 return evaluateAlignTo(Res, Asm);
254 return evaluateTotalNumVGPR(Res, Asm);
255 case AGVK_Occupancy:
256 return evaluateOccupancy(Res, Asm);
258 return evaluateInstPrefSize(Res, Asm);
259 case AGVK_Lit:
260 case AGVK_Lit64:
261 return Args[0]->evaluateAsRelocatable(Res, Asm);
262 }
263
264 for (const MCExpr *Arg : Args) {
265 MCValue ArgRes;
266 if (!Arg->evaluateAsRelocatable(ArgRes, Asm) || !ArgRes.isAbsolute())
267 return false;
268
269 if (!Total.has_value())
270 Total = ArgRes.getConstant();
271 Total = op(Kind, *Total, ArgRes.getConstant());
272 }
273
274 Res = MCValue::get(*Total);
275 return true;
276}
277
279 for (const MCExpr *Arg : Args)
280 Streamer.visitUsedExpr(*Arg);
281}
282
284 for (const MCExpr *Arg : Args) {
285 if (Arg->findAssociatedFragment())
286 return Arg->findAssociatedFragment();
287 }
288 return nullptr;
289}
290
291/// Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed
292/// are unresolvable but needed for further MCExprs). Derived from
293/// implementation of IsaInfo::getNumExtraSGPRs in AMDGPUBaseInfo.cpp.
294///
295const AMDGPUMCExpr *AMDGPUMCExpr::createExtraSGPRs(const MCExpr *VCCUsed,
296 const MCExpr *FlatScrUsed,
297 bool XNACKUsed,
298 MCContext &Ctx) {
299
300 return create(AGVK_ExtraSGPRs,
301 {VCCUsed, FlatScrUsed, MCConstantExpr::create(XNACKUsed, Ctx)},
302 Ctx);
303}
304
305const AMDGPUMCExpr *AMDGPUMCExpr::createTotalNumVGPR(const MCExpr *NumAGPR,
306 const MCExpr *NumVGPR,
307 MCContext &Ctx) {
308 return create(AGVK_TotalNumVGPRs, {NumAGPR, NumVGPR}, Ctx);
309}
310
311const AMDGPUMCExpr *
313 return create(AGVK_InstPrefSize, {CodeSizeBytes}, Ctx);
314}
315
316const AMDGPUMCExpr *AMDGPUMCExpr::createLit(LitModifier Lit, int64_t Value,
317 MCContext &Ctx) {
321 {MCConstantExpr::create(Value, Ctx, /*PrintInHex=*/true)}, Ctx);
322}
323
324static KnownBits fromOptionalToKnownBits(std::optional<bool> CompareResult) {
325 static constexpr unsigned BitWidth = 64;
326 const APInt True(BitWidth, 1);
327 const APInt False(BitWidth, 0);
328 if (CompareResult) {
329 return *CompareResult ? KnownBits::makeConstant(True)
331 }
332
333 KnownBits UnknownBool(/*BitWidth=*/1);
334 return UnknownBool.zext(BitWidth);
335}
336
338static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
339 unsigned Depth = 0);
340
341static void binaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
342 unsigned Depth) {
343 static constexpr unsigned BitWidth = 64;
344 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
345 const MCExpr *LHS = BExpr->getLHS();
346 const MCExpr *RHS = BExpr->getRHS();
347
348 knownBitsMapHelper(LHS, KBM, Depth + 1);
349 knownBitsMapHelper(RHS, KBM, Depth + 1);
350 KnownBits LHSKnown = KBM[LHS];
351 KnownBits RHSKnown = KBM[RHS];
352
353 switch (BExpr->getOpcode()) {
354 default:
355 KBM[Expr] = KnownBits(BitWidth);
356 return;
358 KBM[Expr] = KnownBits::add(LHSKnown, RHSKnown);
359 return;
361 KBM[Expr] = LHSKnown & RHSKnown;
362 return;
364 KBM[Expr] = KnownBits::sdiv(LHSKnown, RHSKnown);
365 return;
367 std::optional<bool> CompareRes = KnownBits::eq(LHSKnown, RHSKnown);
368 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
369 return;
370 }
372 std::optional<bool> CompareRes = KnownBits::ne(LHSKnown, RHSKnown);
373 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
374 return;
375 }
377 std::optional<bool> CompareRes = KnownBits::sgt(LHSKnown, RHSKnown);
378 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
379 return;
380 }
382 std::optional<bool> CompareRes = KnownBits::sge(LHSKnown, RHSKnown);
383 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
384 return;
385 }
387 std::optional<bool> CompareRes;
388 const APInt False(BitWidth, 0);
389 std::optional<bool> LHSBool =
390 KnownBits::ne(LHSKnown, KnownBits::makeConstant(False));
391 std::optional<bool> RHSBool =
392 KnownBits::ne(RHSKnown, KnownBits::makeConstant(False));
393 if (LHSBool && RHSBool)
394 CompareRes = *LHSBool && *RHSBool;
395 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
396 return;
397 }
399 const APInt False(BitWidth, 0);
400 KnownBits Bits = LHSKnown | RHSKnown;
401 std::optional<bool> CompareRes =
403 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
404 return;
405 }
407 std::optional<bool> CompareRes = KnownBits::slt(LHSKnown, RHSKnown);
408 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
409 return;
410 }
412 std::optional<bool> CompareRes = KnownBits::sle(LHSKnown, RHSKnown);
413 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
414 return;
415 }
417 KBM[Expr] = KnownBits::srem(LHSKnown, RHSKnown);
418 return;
420 KBM[Expr] = KnownBits::mul(LHSKnown, RHSKnown);
421 return;
423 KBM[Expr] = LHSKnown | RHSKnown;
424 return;
426 KBM[Expr] = KnownBits::shl(LHSKnown, RHSKnown);
427 return;
429 KBM[Expr] = KnownBits::ashr(LHSKnown, RHSKnown);
430 return;
432 KBM[Expr] = KnownBits::lshr(LHSKnown, RHSKnown);
433 return;
435 KBM[Expr] = KnownBits::sub(LHSKnown, RHSKnown);
436 return;
438 KBM[Expr] = LHSKnown ^ RHSKnown;
439 return;
440 }
441}
442
443static void unaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
444 unsigned Depth) {
445 static constexpr unsigned BitWidth = 64;
446 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
447 knownBitsMapHelper(UExpr->getSubExpr(), KBM, Depth + 1);
448 KnownBits KB = KBM[UExpr->getSubExpr()];
449
450 switch (UExpr->getOpcode()) {
451 default:
452 KBM[Expr] = KnownBits(BitWidth);
453 return;
455 KB.makeNegative();
456 KBM[Expr] = std::move(KB);
457 return;
458 }
461 AllOnes.setAllOnes();
462 KBM[Expr] = KB ^ AllOnes;
463 return;
464 }
466 KB.makeNonNegative();
467 KBM[Expr] = std::move(KB);
468 return;
469 }
470 }
471}
472
473static void targetOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
474 unsigned Depth) {
475 static constexpr unsigned BitWidth = 64;
476 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
477
478 switch (AGVK->getKind()) {
479 default:
480 KBM[Expr] = KnownBits(BitWidth);
481 return;
483 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
484 KnownBits KB = KBM[AGVK->getSubExpr(0)];
485 for (const MCExpr *Arg : AGVK->getArgs()) {
486 knownBitsMapHelper(Arg, KBM, Depth + 1);
487 KB |= KBM[Arg];
488 }
489 KBM[Expr] = std::move(KB);
490 return;
491 }
493 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
494 KnownBits KB = KBM[AGVK->getSubExpr(0)];
495 for (const MCExpr *Arg : AGVK->getArgs()) {
496 knownBitsMapHelper(Arg, KBM, Depth + 1);
497 KB = KnownBits::umax(KB, KBM[Arg]);
498 }
499 KBM[Expr] = std::move(KB);
500 return;
501 }
509 int64_t Val;
510 if (AGVK->evaluateAsAbsolute(Val)) {
511 APInt APValue(BitWidth, Val);
512 KBM[Expr] = KnownBits::makeConstant(APValue);
513 return;
514 }
516 // The result is clamped to (1 << FieldWidth) - 1, so upper bits are
517 // known zero. FieldWidth is derived from the subtarget.
518 const MCSubtargetInfo *STI = AGVK->getCtx().getSubtargetInfo();
519 unsigned FieldWidth = getInstPrefSizeFieldWidth(*STI);
521 KB.Zero.setBitsFrom(FieldWidth);
522 KBM[Expr] = KB;
523 return;
524 }
525 KBM[Expr] = KnownBits(BitWidth);
526 return;
527 }
528 }
529}
530
531static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
532 unsigned Depth) {
533 static constexpr unsigned BitWidth = 64;
534
535 int64_t Val;
536 if (Expr->evaluateAsAbsolute(Val)) {
537 APInt APValue(BitWidth, Val, /*isSigned=*/true);
538 KBM[Expr] = KnownBits::makeConstant(APValue);
539 return;
540 }
541
542 if (Depth == 16) {
543 KBM[Expr] = KnownBits(BitWidth);
544 return;
545 }
546
547 switch (Expr->getKind()) {
550 return;
551 }
553 const MCConstantExpr *CE = cast<MCConstantExpr>(Expr);
554 APInt APValue(BitWidth, CE->getValue(), /*isSigned=*/true);
555 KBM[Expr] = KnownBits::makeConstant(APValue);
556 return;
557 }
559 const MCSymbolRefExpr *RExpr = cast<MCSymbolRefExpr>(Expr);
560 const MCSymbol &Sym = RExpr->getSymbol();
561 if (!Sym.isVariable()) {
562 KBM[Expr] = KnownBits(BitWidth);
563 return;
564 }
565
566 // Variable value retrieval is not for actual use but only for knownbits
567 // analysis.
568 const MCExpr *SymVal = Sym.getVariableValue();
569 knownBitsMapHelper(SymVal, KBM, Depth + 1);
570
571 // Explicitly copy-construct so that there exists a local KnownBits in case
572 // KBM[SymVal] gets invalidated after a potential growth through KBM[Expr].
573 KBM[Expr] = KnownBits(KBM[SymVal]);
574 return;
575 }
578 return;
579 }
582 return;
584 llvm_unreachable("unused by this backend");
585 }
586 }
587}
588
589static const MCExpr *tryFoldHelper(const MCExpr *Expr, KnownBitsMap &KBM,
590 MCContext &Ctx) {
591 if (!KBM.count(Expr))
592 return Expr;
593
594 auto ValueCheckKnownBits = [](KnownBits &KB, unsigned Value) -> bool {
595 if (!KB.isConstant())
596 return false;
597
598 return Value == KB.getConstant();
599 };
600
601 if (Expr->getKind() == MCExpr::ExprKind::Constant)
602 return Expr;
603
604 // Resolving unary operations to constants may make the value more ambiguous.
605 // For example, `~62` becomes `-63`; however, to me it's more ambiguous if a
606 // bit mask value is represented through a negative number.
607 if (Expr->getKind() != MCExpr::ExprKind::Unary) {
608 if (KBM[Expr].isConstant()) {
609 APInt ConstVal = KBM[Expr].getConstant();
610 return MCConstantExpr::create(ConstVal.getSExtValue(), Ctx);
611 }
612
613 int64_t EvalValue;
614 if (Expr->evaluateAsAbsolute(EvalValue))
615 return MCConstantExpr::create(EvalValue, Ctx);
616 }
617
618 switch (Expr->getKind()) {
619 default:
620 return Expr;
622 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
623 const MCExpr *LHS = BExpr->getLHS();
624 const MCExpr *RHS = BExpr->getRHS();
625
626 switch (BExpr->getOpcode()) {
627 default:
628 return Expr;
630 if (ValueCheckKnownBits(KBM[RHS], 0))
631 return tryFoldHelper(LHS, KBM, Ctx);
632 break;
633 }
636 if (ValueCheckKnownBits(KBM[LHS], 0))
637 return tryFoldHelper(RHS, KBM, Ctx);
638 if (ValueCheckKnownBits(KBM[RHS], 0))
639 return tryFoldHelper(LHS, KBM, Ctx);
640 break;
641 }
643 if (ValueCheckKnownBits(KBM[LHS], 1))
644 return tryFoldHelper(RHS, KBM, Ctx);
645 if (ValueCheckKnownBits(KBM[RHS], 1))
646 return tryFoldHelper(LHS, KBM, Ctx);
647 break;
648 }
652 if (ValueCheckKnownBits(KBM[RHS], 0))
653 return tryFoldHelper(LHS, KBM, Ctx);
654 if (ValueCheckKnownBits(KBM[LHS], 0))
655 return MCConstantExpr::create(0, Ctx);
656 break;
657 }
659 if (ValueCheckKnownBits(KBM[LHS], 0) || ValueCheckKnownBits(KBM[RHS], 0))
660 return MCConstantExpr::create(0, Ctx);
661 break;
662 }
663 }
664 const MCExpr *NewLHS = tryFoldHelper(LHS, KBM, Ctx);
665 const MCExpr *NewRHS = tryFoldHelper(RHS, KBM, Ctx);
666 if (NewLHS != LHS || NewRHS != RHS)
667 return MCBinaryExpr::create(BExpr->getOpcode(), NewLHS, NewRHS, Ctx,
668 BExpr->getLoc());
669 return Expr;
670 }
672 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
673 const MCExpr *SubExpr = UExpr->getSubExpr();
674 const MCExpr *NewSubExpr = tryFoldHelper(SubExpr, KBM, Ctx);
675 if (SubExpr != NewSubExpr)
676 return MCUnaryExpr::create(UExpr->getOpcode(), NewSubExpr, Ctx,
677 UExpr->getLoc());
678 return Expr;
679 }
681 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
683 bool Changed = false;
684 for (const MCExpr *Arg : AGVK->getArgs()) {
685 const MCExpr *NewArg = tryFoldHelper(Arg, KBM, Ctx);
686 NewArgs.push_back(NewArg);
687 Changed |= Arg != NewArg;
688 }
689 return Changed ? AMDGPUMCExpr::create(AGVK->getKind(), NewArgs, Ctx) : Expr;
690 }
691 }
692 return Expr;
693}
694
696 MCContext &Ctx) {
697 KnownBitsMap KBM;
698 knownBitsMapHelper(Expr, KBM);
699 const MCExpr *NewExpr = tryFoldHelper(Expr, KBM, Ctx);
700
701 return Expr != NewExpr ? NewExpr : Expr;
702}
703
705 const MCAsmInfo *MAI) {
706 int64_t Val;
707 if (Expr->evaluateAsAbsolute(Val)) {
708 OS << Val;
709 return;
710 }
711
712 MAI->printExpr(OS, *Expr);
713}
714
715bool AMDGPU::isLitExpr(const MCExpr *Expr) {
716 const auto *E = dyn_cast<AMDGPUMCExpr>(Expr);
717 return E && (E->getKind() == AMDGPUMCExpr::AGVK_Lit ||
718 E->getKind() == AMDGPUMCExpr::AGVK_Lit64);
719}
720
721int64_t AMDGPU::getLitValue(const MCExpr *Expr) {
722 assert(isLitExpr(Expr));
723 return cast<MCConstantExpr>(cast<AMDGPUMCExpr>(Expr)->getArgs()[0])
724 ->getValue();
725}
726
728 const auto *E = dyn_cast<AMDGPUMCExpr>(Expr);
729 if (!E)
731 return E->getKind();
732}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
static void targetOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM, unsigned Depth)
static void unaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM, unsigned Depth)
static KnownBits fromOptionalToKnownBits(std::optional< bool > CompareResult)
static void binaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM, unsigned Depth)
static const MCExpr * tryFoldHelper(const MCExpr *Expr, KnownBitsMap &KBM, MCContext &Ctx)
static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM, unsigned Depth=0)
static bool evaluateMCExprs(ArrayRef< const MCExpr * > Exprs, const MCAssembler *Asm, std::initializer_list< std::reference_wrapper< uint64_t > > Vals)
static unsigned getInstPrefSizeFieldWidth(const MCSubtargetInfo &STI)
Get the inst_pref_size field width for the given subtarget.
DenseMap< const MCExpr *, KnownBits > KnownBitsMap
AMDHSA kernel descriptor definitions.
#define op(i)
static cl::opt< unsigned > CacheLineSize("cache-line-size", cl::init(0), cl::Hidden, cl::desc("Use this to override the target cache line size when " "specified by the user."))
Value * RHS
Value * LHS
AMDGPU target specific MCExpr operations.
ArrayRef< const MCExpr * > getArgs() const
MCFragment * findAssociatedFragment() const override
static const AMDGPUMCExpr * createInstPrefSize(const MCExpr *CodeSizeBytes, MCContext &Ctx)
Create an expression for instruction prefetch size computation: min(divideCeil(CodeSizeBytes,...
void visitUsedExpr(MCStreamer &Streamer) const override
static const AMDGPUMCExpr * createTotalNumVGPR(const MCExpr *NumAGPR, const MCExpr *NumVGPR, MCContext &Ctx)
static const AMDGPUMCExpr * createLit(LitModifier Lit, int64_t Value, MCContext &Ctx)
static const AMDGPUMCExpr * create(VariantKind Kind, ArrayRef< const MCExpr * > Args, MCContext &Ctx)
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm) const override
MCContext & getCtx() const
static const AMDGPUMCExpr * createExtraSGPRs(const MCExpr *VCCUsed, const MCExpr *FlatScrUsed, bool XNACKUsed, MCContext &Ctx)
Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed are unresolvable but neede...
const MCExpr * getSubExpr(size_t Index) const
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
VariantKind getKind() const
static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *E)
Class for arbitrary precision integers.
Definition APInt.h:78
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1408
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1585
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:174
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:66
void printExpr(raw_ostream &, const MCExpr &) const
Binary assembler expressions.
Definition MCExpr.h:299
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:446
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:449
Opcode getOpcode() const
Get the kind of this binary expression.
Definition MCExpr.h:443
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:201
@ Div
Signed division.
Definition MCExpr.h:304
@ Shl
Shift left.
Definition MCExpr.h:321
@ AShr
Arithmetic shift right.
Definition MCExpr.h:322
@ LShr
Logical shift right.
Definition MCExpr.h:323
@ GTE
Signed greater than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:308
@ EQ
Equality comparison.
Definition MCExpr.h:305
@ Sub
Subtraction.
Definition MCExpr.h:324
@ Mul
Multiplication.
Definition MCExpr.h:317
@ GT
Signed greater than comparison (result is either 0 or some target-specific non-zero value)
Definition MCExpr.h:306
@ Mod
Signed remainder.
Definition MCExpr.h:316
@ And
Bitwise and.
Definition MCExpr.h:303
@ Or
Bitwise or.
Definition MCExpr.h:319
@ Xor
Bitwise exclusive or.
Definition MCExpr.h:325
@ LAnd
Logical and.
Definition MCExpr.h:310
@ LOr
Logical or.
Definition MCExpr.h:311
@ LT
Signed less than comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:312
@ Add
Addition.
Definition MCExpr.h:302
@ LTE
Signed less than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:314
@ NE
Inequality comparison.
Definition MCExpr.h:318
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
const MCSubtargetInfo * getSubtargetInfo() const
Definition MCContext.h:415
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ Unary
Unary expressions.
Definition MCExpr.h:44
@ Constant
Constant expressions.
Definition MCExpr.h:42
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Target
Target specific expression.
Definition MCExpr.h:46
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
@ Binary
Binary expressions.
Definition MCExpr.h:41
LLVM_ABI bool evaluateAsAbsolute(int64_t &Res) const
Try to evaluate the expression to an absolute value.
Definition MCExpr.cpp:238
ExprKind getKind() const
Definition MCExpr.h:85
SMLoc getLoc() const
Definition MCExpr.h:86
Streaming machine code generation interface.
Definition MCStreamer.h:222
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
const MCSymbol & getSymbol() const
Definition MCExpr.h:227
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
Unary assembler expressions.
Definition MCExpr.h:243
Opcode getOpcode() const
Get the kind of this unary expression.
Definition MCExpr.h:286
static LLVM_ABI const MCUnaryExpr * create(Opcode Op, const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:207
@ Minus
Unary minus.
Definition MCExpr.h:247
@ Plus
Unary plus.
Definition MCExpr.h:249
@ Not
Bitwise negation.
Definition MCExpr.h:248
const MCExpr * getSubExpr() const
Get the child of this unary expression.
Definition MCExpr.h:289
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
Definition MCValue.h:56
int64_t getConstant() const
Definition MCValue.h:44
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition MCValue.h:54
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char NumVGPRs[]
Key for Kernel::CodeProps::Metadata::mNumVGPRs.
constexpr char NumSGPRs[]
Key for Kernel::CodeProps::Metadata::mNumSGPRs.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned getNumWavesPerEUWithNumVGPRs(const MCSubtargetInfo &STI, unsigned NumVGPRs, unsigned DynamicVGPRBlockSize)
unsigned getInstCacheLineSize(const MCSubtargetInfo &STI)
unsigned getNumExtraSGPRs(const MCSubtargetInfo &STI, bool VCCUsed, bool FlatScrUsed, bool XNACKUsed)
unsigned getOccupancyWithNumSGPRs(unsigned SGPRs, unsigned MaxWaves, AMDGPUSubtarget::Generation Gen)
LLVM_READONLY bool isLitExpr(const MCExpr *Expr)
void printAMDGPUMCExpr(const MCExpr *Expr, raw_ostream &OS, const MCAsmInfo *MAI)
bool isGFX12Plus(const MCSubtargetInfo &STI)
bool isGFX90A(const MCSubtargetInfo &STI)
LLVM_READONLY AMDGPUMCExpr::VariantKind getExprKind(const MCExpr *Expr)
LLVM_READONLY int64_t getLitValue(const MCExpr *Expr)
const MCExpr * foldAMDGPUMCExpr(const MCExpr *Expr, MCContext &Ctx)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:840
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto uninitialized_copy(R &&Src, IterTy Dst)
Definition STLExtras.h:2110
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ Success
The lock was released successfully.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:120
static LLVM_ABI std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGE result.
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLT result.
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
static LLVM_ABI std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLE result.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:58