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_Min:
69 OS << "min(";
70 break;
71 case AGVK_ExtraSGPRs:
72 OS << "extrasgprs(";
73 break;
75 OS << "totalnumvgprs(";
76 break;
77 case AGVK_AlignTo:
78 OS << "alignto(";
79 break;
80 case AGVK_Occupancy:
81 OS << "occupancy(";
82 break;
84 OS << "instprefsize(";
85 break;
86 case AGVK_Lit:
87 OS << "lit(";
88 break;
89 case AGVK_Lit64:
90 OS << "lit64(";
91 break;
92 }
93 for (const auto *It = Args.begin(); It != Args.end(); ++It) {
94 MAI->printExpr(OS, **It);
95 if ((It + 1) != Args.end())
96 OS << ", ";
97 }
98 OS << ')';
99}
100
101static int64_t op(AMDGPUMCExpr::VariantKind Kind, int64_t Arg1, int64_t Arg2) {
102 switch (Kind) {
103 default:
104 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
106 return std::max(Arg1, Arg2);
108 return Arg1 | Arg2;
110 return std::min(Arg1, Arg2);
111 }
112}
113
114static bool
116 std::initializer_list<std::reference_wrapper<uint64_t>> Vals) {
117 return llvm::all_of(llvm::zip_equal(Exprs, Vals), [&](const auto &Pair) {
118 auto [Expr, ValRef] = Pair;
119 uint64_t &Val = ValRef.get();
120 MCValue MCVal;
121 if (!Expr->evaluateAsRelocatable(MCVal, Asm) || !MCVal.isAbsolute())
122 return false;
123 Val = MCVal.getConstant();
124 return true;
125 });
126}
127
128bool AMDGPUMCExpr::evaluateExtraSGPRs(MCValue &Res,
129 const MCAssembler *Asm) const {
130 const MCSubtargetInfo &STI = *Ctx.getSubtargetInfo();
131 uint64_t VCCUsed = 0, FlatScrUsed = 0, XNACKUsed = 0;
132
133 if (!evaluateMCExprs(Args, Asm, {VCCUsed, FlatScrUsed, XNACKUsed}))
134 return false;
135
136 uint64_t ExtraSGPRs = IsaInfo::getNumExtraSGPRs(
137 STI, (bool)VCCUsed, (bool)FlatScrUsed, (bool)XNACKUsed);
138 Res = MCValue::get(ExtraSGPRs);
139 return true;
140}
141
142bool AMDGPUMCExpr::evaluateTotalNumVGPR(MCValue &Res,
143 const MCAssembler *Asm) const {
144 const MCSubtargetInfo &STI = *Ctx.getSubtargetInfo();
145 uint64_t NumAGPR = 0, NumVGPR = 0;
146
147 bool Has90AInsts = AMDGPU::isGFX90A(STI);
148
149 if (!evaluateMCExprs(Args, Asm, {NumAGPR, NumVGPR}))
150 return false;
151
152 uint64_t TotalNum = Has90AInsts && NumAGPR ? alignTo(NumVGPR, 4) + NumAGPR
153 : std::max(NumVGPR, NumAGPR);
154 Res = MCValue::get(TotalNum);
155 return true;
156}
157
158bool AMDGPUMCExpr::evaluateAlignTo(MCValue &Res, const MCAssembler *Asm) const {
159 uint64_t Value = 0, Align = 0;
160 if (!evaluateMCExprs(Args, Asm, {Value, Align}))
161 return false;
162
163 Res = MCValue::get(alignTo(Value, Align));
164 return true;
165}
166
167bool AMDGPUMCExpr::evaluateOccupancy(MCValue &Res,
168 const MCAssembler *Asm) const {
169 uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, Generation,
171
173 Args.slice(0, 5), Asm,
174 {MaxWaves, Granule, TargetTotalNumVGPRs, Generation, InitOccupancy});
175
176 assert(Success && "Arguments 1 to 5 for Occupancy should be known constants");
177
178 if (!Success || !evaluateMCExprs(Args.slice(5, 2), Asm, {NumSGPRs, NumVGPRs}))
179 return false;
180
181 unsigned Occupancy = InitOccupancy;
182 if (NumSGPRs)
183 Occupancy = std::min(
185 NumSGPRs, MaxWaves,
186 static_cast<AMDGPUSubtarget::Generation>(Generation)));
187 if (NumVGPRs)
188 Occupancy = std::min(Occupancy,
190 NumVGPRs, Granule, MaxWaves, TargetTotalNumVGPRs));
191
192 Res = MCValue::get(Occupancy);
193 return true;
194}
195
196/// Get the inst_pref_size field width for the given subtarget.
197static unsigned getInstPrefSizeFieldWidth(const MCSubtargetInfo &STI) {
198 if (AMDGPU::isGFX12Plus(STI))
199 return amdhsa::COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE_WIDTH;
200 return amdhsa::COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE_WIDTH;
201}
202
203bool AMDGPUMCExpr::evaluateInstPrefSize(MCValue &Res,
204 const MCAssembler *Asm) const {
205 uint64_t CodeSizeInBytes = 0;
206 if (!evaluateMCExprs(Args, Asm, {CodeSizeInBytes}))
207 return false;
208 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
209 unsigned FieldWidth = getInstPrefSizeFieldWidth(*STI);
211 uint64_t CodeSizeInLines = divideCeil(CodeSizeInBytes, CacheLineSize);
212 uint64_t MaxVal = (1u << FieldWidth) - 1;
213 Res = MCValue::get(std::min(CodeSizeInLines, MaxVal));
214 return true;
215}
216
218 const MCExpr *E) {
219 switch (E->getKind()) {
220 case MCExpr::Constant:
221 return false;
222 case MCExpr::Unary:
224 Sym, static_cast<const MCUnaryExpr *>(E)->getSubExpr());
225 case MCExpr::Binary: {
226 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(E);
227 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
229 }
230 case MCExpr::SymbolRef: {
231 const MCSymbol &S = static_cast<const MCSymbolRefExpr *>(E)->getSymbol();
232 if (S.isVariable())
234 return &S == Sym;
235 }
237 case MCExpr::Target: {
238 auto *TE = static_cast<const AMDGPUMCExpr *>(E);
239 for (const MCExpr *E : TE->getArgs())
240 if (isSymbolUsedInExpression(Sym, E))
241 return true;
242 return false;
243 }
244 }
245 llvm_unreachable("Unknown expr kind!");
246}
247
249 const MCAssembler *Asm) const {
250 std::optional<int64_t> Total;
251 switch (Kind) {
252 default:
253 break;
254 case AGVK_ExtraSGPRs:
255 return evaluateExtraSGPRs(Res, Asm);
256 case AGVK_AlignTo:
257 return evaluateAlignTo(Res, Asm);
259 return evaluateTotalNumVGPR(Res, Asm);
260 case AGVK_Occupancy:
261 return evaluateOccupancy(Res, Asm);
263 return evaluateInstPrefSize(Res, Asm);
264 case AGVK_Lit:
265 case AGVK_Lit64:
266 return Args[0]->evaluateAsRelocatable(Res, Asm);
267 }
268
269 for (const MCExpr *Arg : Args) {
270 MCValue ArgRes;
271 if (!Arg->evaluateAsRelocatable(ArgRes, Asm) || !ArgRes.isAbsolute())
272 return false;
273
274 if (!Total.has_value())
275 Total = ArgRes.getConstant();
276 Total = op(Kind, *Total, ArgRes.getConstant());
277 }
278
279 Res = MCValue::get(*Total);
280 return true;
281}
282
284 for (const MCExpr *Arg : Args)
285 Streamer.visitUsedExpr(*Arg);
286}
287
289 for (const MCExpr *Arg : Args) {
290 if (Arg->findAssociatedFragment())
291 return Arg->findAssociatedFragment();
292 }
293 return nullptr;
294}
295
296/// Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed
297/// are unresolvable but needed for further MCExprs). Derived from
298/// implementation of IsaInfo::getNumExtraSGPRs in AMDGPUBaseInfo.cpp.
299///
300const AMDGPUMCExpr *AMDGPUMCExpr::createExtraSGPRs(const MCExpr *VCCUsed,
301 const MCExpr *FlatScrUsed,
302 bool XNACKUsed,
303 MCContext &Ctx) {
304
305 return create(AGVK_ExtraSGPRs,
306 {VCCUsed, FlatScrUsed, MCConstantExpr::create(XNACKUsed, Ctx)},
307 Ctx);
308}
309
310const AMDGPUMCExpr *AMDGPUMCExpr::createTotalNumVGPR(const MCExpr *NumAGPR,
311 const MCExpr *NumVGPR,
312 MCContext &Ctx) {
313 return create(AGVK_TotalNumVGPRs, {NumAGPR, NumVGPR}, Ctx);
314}
315
316const AMDGPUMCExpr *
318 return create(AGVK_InstPrefSize, {CodeSizeBytes}, Ctx);
319}
320
321const AMDGPUMCExpr *AMDGPUMCExpr::createLit(LitModifier Lit, int64_t Value,
322 MCContext &Ctx) {
326 {MCConstantExpr::create(Value, Ctx, /*PrintInHex=*/true)}, Ctx);
327}
328
329static KnownBits fromOptionalToKnownBits(std::optional<bool> CompareResult) {
330 static constexpr unsigned BitWidth = 64;
331 const APInt True(BitWidth, 1);
332 const APInt False(BitWidth, 0);
333 if (CompareResult) {
334 return *CompareResult ? KnownBits::makeConstant(True)
336 }
337
338 KnownBits UnknownBool(/*BitWidth=*/1);
339 return UnknownBool.zext(BitWidth);
340}
341
343static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
344 unsigned Depth = 0);
345
346static void binaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
347 unsigned Depth) {
348 static constexpr unsigned BitWidth = 64;
349 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
350 const MCExpr *LHS = BExpr->getLHS();
351 const MCExpr *RHS = BExpr->getRHS();
352
353 knownBitsMapHelper(LHS, KBM, Depth + 1);
354 knownBitsMapHelper(RHS, KBM, Depth + 1);
355 KnownBits LHSKnown = KBM[LHS];
356 KnownBits RHSKnown = KBM[RHS];
357
358 switch (BExpr->getOpcode()) {
359 default:
360 KBM[Expr] = KnownBits(BitWidth);
361 return;
363 KBM[Expr] = KnownBits::add(LHSKnown, RHSKnown);
364 return;
366 KBM[Expr] = LHSKnown & RHSKnown;
367 return;
369 KBM[Expr] = KnownBits::sdiv(LHSKnown, RHSKnown);
370 return;
372 std::optional<bool> CompareRes = KnownBits::eq(LHSKnown, RHSKnown);
373 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
374 return;
375 }
377 std::optional<bool> CompareRes = KnownBits::ne(LHSKnown, RHSKnown);
378 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
379 return;
380 }
382 std::optional<bool> CompareRes = KnownBits::sgt(LHSKnown, RHSKnown);
383 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
384 return;
385 }
387 std::optional<bool> CompareRes = KnownBits::sge(LHSKnown, RHSKnown);
388 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
389 return;
390 }
392 std::optional<bool> CompareRes;
393 const APInt False(BitWidth, 0);
394 std::optional<bool> LHSBool =
395 KnownBits::ne(LHSKnown, KnownBits::makeConstant(False));
396 std::optional<bool> RHSBool =
397 KnownBits::ne(RHSKnown, KnownBits::makeConstant(False));
398 if (LHSBool && RHSBool)
399 CompareRes = *LHSBool && *RHSBool;
400 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
401 return;
402 }
404 const APInt False(BitWidth, 0);
405 KnownBits Bits = LHSKnown | RHSKnown;
406 std::optional<bool> CompareRes =
408 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
409 return;
410 }
412 std::optional<bool> CompareRes = KnownBits::slt(LHSKnown, RHSKnown);
413 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
414 return;
415 }
417 std::optional<bool> CompareRes = KnownBits::sle(LHSKnown, RHSKnown);
418 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
419 return;
420 }
422 KBM[Expr] = KnownBits::srem(LHSKnown, RHSKnown);
423 return;
425 KBM[Expr] = KnownBits::mul(LHSKnown, RHSKnown);
426 return;
428 KBM[Expr] = LHSKnown | RHSKnown;
429 return;
431 KBM[Expr] = KnownBits::shl(LHSKnown, RHSKnown);
432 return;
434 KBM[Expr] = KnownBits::ashr(LHSKnown, RHSKnown);
435 return;
437 KBM[Expr] = KnownBits::lshr(LHSKnown, RHSKnown);
438 return;
440 KBM[Expr] = KnownBits::sub(LHSKnown, RHSKnown);
441 return;
443 KBM[Expr] = LHSKnown ^ RHSKnown;
444 return;
445 }
446}
447
448static void unaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
449 unsigned Depth) {
450 static constexpr unsigned BitWidth = 64;
451 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
452 knownBitsMapHelper(UExpr->getSubExpr(), KBM, Depth + 1);
453 KnownBits KB = KBM[UExpr->getSubExpr()];
454
455 switch (UExpr->getOpcode()) {
456 default:
457 KBM[Expr] = KnownBits(BitWidth);
458 return;
460 KB.makeNegative();
461 KBM[Expr] = std::move(KB);
462 return;
463 }
466 AllOnes.setAllOnes();
467 KBM[Expr] = KB ^ AllOnes;
468 return;
469 }
471 KB.makeNonNegative();
472 KBM[Expr] = std::move(KB);
473 return;
474 }
475 }
476}
477
478static void targetOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
479 unsigned Depth) {
480 static constexpr unsigned BitWidth = 64;
481 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
482
483 switch (AGVK->getKind()) {
484 default:
485 KBM[Expr] = KnownBits(BitWidth);
486 return;
488 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
489 KnownBits KB = KBM[AGVK->getSubExpr(0)];
490 for (const MCExpr *Arg : AGVK->getArgs()) {
491 knownBitsMapHelper(Arg, KBM, Depth + 1);
492 KB |= KBM[Arg];
493 }
494 KBM[Expr] = std::move(KB);
495 return;
496 }
498 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
499 KnownBits KB = KBM[AGVK->getSubExpr(0)];
500 for (const MCExpr *Arg : AGVK->getArgs()) {
501 knownBitsMapHelper(Arg, KBM, Depth + 1);
502 KB = KnownBits::smax(KB, KBM[Arg]);
503 }
504 KBM[Expr] = std::move(KB);
505 return;
506 }
508 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
509 KnownBits KB = KBM[AGVK->getSubExpr(0)];
510 for (const MCExpr *Arg : AGVK->getArgs()) {
511 knownBitsMapHelper(Arg, KBM, Depth + 1);
512 KB = KnownBits::smin(KB, KBM[Arg]);
513 }
514 KBM[Expr] = std::move(KB);
515 return;
516 }
524 int64_t Val;
525 if (AGVK->evaluateAsAbsolute(Val)) {
526 APInt APValue(BitWidth, Val);
527 KBM[Expr] = KnownBits::makeConstant(APValue);
528 return;
529 }
531 // The result is clamped to (1 << FieldWidth) - 1, so upper bits are
532 // known zero. FieldWidth is derived from the subtarget.
533 const MCSubtargetInfo *STI = AGVK->getCtx().getSubtargetInfo();
534 unsigned FieldWidth = getInstPrefSizeFieldWidth(*STI);
536 KB.Zero.setBitsFrom(FieldWidth);
537 KBM[Expr] = KB;
538 return;
539 }
540 KBM[Expr] = KnownBits(BitWidth);
541 return;
542 }
543 }
544}
545
546static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
547 unsigned Depth) {
548 static constexpr unsigned BitWidth = 64;
549
550 int64_t Val;
551 if (Expr->evaluateAsAbsolute(Val)) {
552 APInt APValue(BitWidth, Val, /*isSigned=*/true);
553 KBM[Expr] = KnownBits::makeConstant(APValue);
554 return;
555 }
556
557 if (Depth == 16) {
558 KBM[Expr] = KnownBits(BitWidth);
559 return;
560 }
561
562 switch (Expr->getKind()) {
565 return;
566 }
568 const MCConstantExpr *CE = cast<MCConstantExpr>(Expr);
569 APInt APValue(BitWidth, CE->getValue(), /*isSigned=*/true);
570 KBM[Expr] = KnownBits::makeConstant(APValue);
571 return;
572 }
574 const MCSymbolRefExpr *RExpr = cast<MCSymbolRefExpr>(Expr);
575 const MCSymbol &Sym = RExpr->getSymbol();
576 if (!Sym.isVariable()) {
577 KBM[Expr] = KnownBits(BitWidth);
578 return;
579 }
580
581 // Variable value retrieval is not for actual use but only for knownbits
582 // analysis.
583 const MCExpr *SymVal = Sym.getVariableValue();
584 knownBitsMapHelper(SymVal, KBM, Depth + 1);
585
586 // Explicitly copy-construct so that there exists a local KnownBits in case
587 // KBM[SymVal] gets invalidated after a potential growth through KBM[Expr].
588 KBM[Expr] = KnownBits(KBM[SymVal]);
589 return;
590 }
593 return;
594 }
597 return;
599 llvm_unreachable("unused by this backend");
600 }
601 }
602}
603
604static const MCExpr *tryFoldHelper(const MCExpr *Expr, KnownBitsMap &KBM,
605 MCContext &Ctx) {
606 if (!KBM.count(Expr))
607 return Expr;
608
609 auto ValueCheckKnownBits = [](KnownBits &KB, unsigned Value) -> bool {
610 if (!KB.isConstant())
611 return false;
612
613 return Value == KB.getConstant();
614 };
615
616 if (Expr->getKind() == MCExpr::ExprKind::Constant)
617 return Expr;
618
619 // Resolving unary operations to constants may make the value more ambiguous.
620 // For example, `~62` becomes `-63`; however, to me it's more ambiguous if a
621 // bit mask value is represented through a negative number.
622 if (Expr->getKind() != MCExpr::ExprKind::Unary) {
623 if (KBM[Expr].isConstant()) {
624 APInt ConstVal = KBM[Expr].getConstant();
625 return MCConstantExpr::create(ConstVal.getSExtValue(), Ctx);
626 }
627
628 int64_t EvalValue;
629 if (Expr->evaluateAsAbsolute(EvalValue))
630 return MCConstantExpr::create(EvalValue, Ctx);
631 }
632
633 switch (Expr->getKind()) {
634 default:
635 return Expr;
637 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
638 const MCExpr *LHS = BExpr->getLHS();
639 const MCExpr *RHS = BExpr->getRHS();
640
641 switch (BExpr->getOpcode()) {
642 default:
643 return Expr;
645 if (ValueCheckKnownBits(KBM[RHS], 0))
646 return tryFoldHelper(LHS, KBM, Ctx);
647 break;
648 }
651 if (ValueCheckKnownBits(KBM[LHS], 0))
652 return tryFoldHelper(RHS, KBM, Ctx);
653 if (ValueCheckKnownBits(KBM[RHS], 0))
654 return tryFoldHelper(LHS, KBM, Ctx);
655 break;
656 }
658 if (ValueCheckKnownBits(KBM[LHS], 1))
659 return tryFoldHelper(RHS, KBM, Ctx);
660 if (ValueCheckKnownBits(KBM[RHS], 1))
661 return tryFoldHelper(LHS, KBM, Ctx);
662 break;
663 }
667 if (ValueCheckKnownBits(KBM[RHS], 0))
668 return tryFoldHelper(LHS, KBM, Ctx);
669 if (ValueCheckKnownBits(KBM[LHS], 0))
670 return MCConstantExpr::create(0, Ctx);
671 break;
672 }
674 if (ValueCheckKnownBits(KBM[LHS], 0) || ValueCheckKnownBits(KBM[RHS], 0))
675 return MCConstantExpr::create(0, Ctx);
676 break;
677 }
678 }
679 const MCExpr *NewLHS = tryFoldHelper(LHS, KBM, Ctx);
680 const MCExpr *NewRHS = tryFoldHelper(RHS, KBM, Ctx);
681 if (NewLHS != LHS || NewRHS != RHS)
682 return MCBinaryExpr::create(BExpr->getOpcode(), NewLHS, NewRHS, Ctx,
683 BExpr->getLoc());
684 return Expr;
685 }
687 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
688 const MCExpr *SubExpr = UExpr->getSubExpr();
689 const MCExpr *NewSubExpr = tryFoldHelper(SubExpr, KBM, Ctx);
690 if (SubExpr != NewSubExpr)
691 return MCUnaryExpr::create(UExpr->getOpcode(), NewSubExpr, Ctx,
692 UExpr->getLoc());
693 return Expr;
694 }
696 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
698 bool Changed = false;
699 for (const MCExpr *Arg : AGVK->getArgs()) {
700 const MCExpr *NewArg = tryFoldHelper(Arg, KBM, Ctx);
701 NewArgs.push_back(NewArg);
702 Changed |= Arg != NewArg;
703 }
704 return Changed ? AMDGPUMCExpr::create(AGVK->getKind(), NewArgs, Ctx) : Expr;
705 }
706 }
707 return Expr;
708}
709
711 MCContext &Ctx) {
712 KnownBitsMap KBM;
713 knownBitsMapHelper(Expr, KBM);
714 const MCExpr *NewExpr = tryFoldHelper(Expr, KBM, Ctx);
715
716 return Expr != NewExpr ? NewExpr : Expr;
717}
718
720 const MCAsmInfo *MAI) {
721 int64_t Val;
722 if (Expr->evaluateAsAbsolute(Val)) {
723 OS << Val;
724 return;
725 }
726
727 MAI->printExpr(OS, *Expr);
728}
729
730bool AMDGPU::isLitExpr(const MCExpr *Expr) {
731 const auto *E = dyn_cast<AMDGPUMCExpr>(Expr);
732 return E && (E->getKind() == AMDGPUMCExpr::AGVK_Lit ||
733 E->getKind() == AMDGPUMCExpr::AGVK_Lit64);
734}
735
736int64_t AMDGPU::getLitValue(const MCExpr *Expr) {
737 assert(isLitExpr(Expr));
738 return cast<MCConstantExpr>(cast<AMDGPUMCExpr>(Expr)->getArgs()[0])
739 ->getValue();
740}
741
743 const auto *E = dyn_cast<AMDGPUMCExpr>(Expr);
744 if (!E)
746 return E->getKind();
747}
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:221
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.
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
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.
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 smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(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