LLVM 20.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"
10#include "GCNSubtarget.h"
12#include "llvm/IR/Function.h"
13#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCAssembler.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCStreamer.h"
17#include "llvm/MC/MCSymbol.h"
18#include "llvm/MC/MCValue.h"
21#include <optional>
22
23using namespace llvm;
24using namespace llvm::AMDGPU;
25
26AMDGPUMCExpr::AMDGPUMCExpr(VariantKind Kind, ArrayRef<const MCExpr *> Args,
27 MCContext &Ctx)
28 : Kind(Kind), Ctx(Ctx) {
29 assert(Args.size() >= 1 && "Needs a minimum of one expression.");
30 assert(Kind != AGVK_None && "Cannot construct AMDGPUMCExpr of kind none.");
31
32 // Allocating the variadic arguments through the same allocation mechanism
33 // that the object itself is allocated with so they end up in the same memory.
34 //
35 // Will result in an asan failure if allocated on the heap through standard
36 // allocation (e.g., through SmallVector's grow).
37 RawArgs = static_cast<const MCExpr **>(
38 Ctx.allocate(sizeof(const MCExpr *) * Args.size()));
39 std::uninitialized_copy(Args.begin(), Args.end(), RawArgs);
40 this->Args = ArrayRef<const MCExpr *>(RawArgs, Args.size());
41}
42
43AMDGPUMCExpr::~AMDGPUMCExpr() { Ctx.deallocate(RawArgs); }
44
47 MCContext &Ctx) {
48 return new (Ctx) AMDGPUMCExpr(Kind, Args, Ctx);
49}
50
51const MCExpr *AMDGPUMCExpr::getSubExpr(size_t Index) const {
52 assert(Index < Args.size() && "Indexing out of bounds AMDGPUMCExpr sub-expr");
53 return Args[Index];
54}
55
57 switch (Kind) {
58 default:
59 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
60 case AGVK_Or:
61 OS << "or(";
62 break;
63 case AGVK_Max:
64 OS << "max(";
65 break;
66 case AGVK_ExtraSGPRs:
67 OS << "extrasgprs(";
68 break;
70 OS << "totalnumvgprs(";
71 break;
72 case AGVK_AlignTo:
73 OS << "alignto(";
74 break;
75 case AGVK_Occupancy:
76 OS << "occupancy(";
77 break;
78 }
79 for (const auto *It = Args.begin(); It != Args.end(); ++It) {
80 (*It)->print(OS, MAI, /*InParens=*/false);
81 if ((It + 1) != Args.end())
82 OS << ", ";
83 }
84 OS << ')';
85}
86
87static int64_t op(AMDGPUMCExpr::VariantKind Kind, int64_t Arg1, int64_t Arg2) {
88 switch (Kind) {
89 default:
90 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
92 return std::max(Arg1, Arg2);
94 return Arg1 | Arg2;
95 }
96}
97
98bool AMDGPUMCExpr::evaluateExtraSGPRs(MCValue &Res, const MCAssembler *Asm,
99 const MCFixup *Fixup) const {
100 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
101 MCValue MCVal;
102 if (!Arg->evaluateAsRelocatable(MCVal, Asm, Fixup) || !MCVal.isAbsolute())
103 return false;
104
105 ConstantValue = MCVal.getConstant();
106 return true;
107 };
108
109 assert(Args.size() == 3 &&
110 "AMDGPUMCExpr Argument count incorrect for ExtraSGPRs");
111 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
112 uint64_t VCCUsed = 0, FlatScrUsed = 0, XNACKUsed = 0;
113
114 bool Success = TryGetMCExprValue(Args[2], XNACKUsed);
115
116 assert(Success && "Arguments 3 for ExtraSGPRs should be a known constant");
117 if (!Success || !TryGetMCExprValue(Args[0], VCCUsed) ||
118 !TryGetMCExprValue(Args[1], FlatScrUsed))
119 return false;
120
122 STI, (bool)VCCUsed, (bool)FlatScrUsed, (bool)XNACKUsed);
123 Res = MCValue::get(ExtraSGPRs);
124 return true;
125}
126
127bool AMDGPUMCExpr::evaluateTotalNumVGPR(MCValue &Res, const MCAssembler *Asm,
128 const MCFixup *Fixup) const {
129 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
130 MCValue MCVal;
131 if (!Arg->evaluateAsRelocatable(MCVal, Asm, Fixup) || !MCVal.isAbsolute())
132 return false;
133
134 ConstantValue = MCVal.getConstant();
135 return true;
136 };
137 assert(Args.size() == 2 &&
138 "AMDGPUMCExpr Argument count incorrect for TotalNumVGPRs");
139 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
140 uint64_t NumAGPR = 0, NumVGPR = 0;
141
142 bool Has90AInsts = AMDGPU::isGFX90A(*STI);
143
144 if (!TryGetMCExprValue(Args[0], NumAGPR) ||
145 !TryGetMCExprValue(Args[1], NumVGPR))
146 return false;
147
148 uint64_t TotalNum = Has90AInsts && NumAGPR ? alignTo(NumVGPR, 4) + NumAGPR
149 : std::max(NumVGPR, NumAGPR);
150 Res = MCValue::get(TotalNum);
151 return true;
152}
153
154bool AMDGPUMCExpr::evaluateAlignTo(MCValue &Res, const MCAssembler *Asm,
155 const MCFixup *Fixup) const {
156 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
157 MCValue MCVal;
158 if (!Arg->evaluateAsRelocatable(MCVal, Asm, Fixup) || !MCVal.isAbsolute())
159 return false;
160
161 ConstantValue = MCVal.getConstant();
162 return true;
163 };
164
165 assert(Args.size() == 2 &&
166 "AMDGPUMCExpr Argument count incorrect for AlignTo");
167 uint64_t Value = 0, Align = 0;
168 if (!TryGetMCExprValue(Args[0], Value) || !TryGetMCExprValue(Args[1], Align))
169 return false;
170
172 return true;
173}
174
175bool AMDGPUMCExpr::evaluateOccupancy(MCValue &Res, const MCAssembler *Asm,
176 const MCFixup *Fixup) const {
177 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
178 MCValue MCVal;
179 if (!Arg->evaluateAsRelocatable(MCVal, Asm, Fixup) || !MCVal.isAbsolute())
180 return false;
181
182 ConstantValue = MCVal.getConstant();
183 return true;
184 };
185 assert(Args.size() == 7 &&
186 "AMDGPUMCExpr Argument count incorrect for Occupancy");
187 uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, Generation,
189
190 bool Success = true;
191 Success &= TryGetMCExprValue(Args[0], MaxWaves);
192 Success &= TryGetMCExprValue(Args[1], Granule);
193 Success &= TryGetMCExprValue(Args[2], TargetTotalNumVGPRs);
194 Success &= TryGetMCExprValue(Args[3], Generation);
195 Success &= TryGetMCExprValue(Args[4], InitOccupancy);
196
197 assert(Success && "Arguments 1 to 5 for Occupancy should be known constants");
198
199 if (!Success || !TryGetMCExprValue(Args[5], NumSGPRs) ||
200 !TryGetMCExprValue(Args[6], NumVGPRs))
201 return false;
202
203 unsigned Occupancy = InitOccupancy;
204 if (NumSGPRs)
205 Occupancy = std::min(
207 NumSGPRs, MaxWaves,
208 static_cast<AMDGPUSubtarget::Generation>(Generation)));
209 if (NumVGPRs)
210 Occupancy = std::min(Occupancy,
212 NumVGPRs, Granule, MaxWaves, TargetTotalNumVGPRs));
213
214 Res = MCValue::get(Occupancy);
215 return true;
216}
217
219 const MCAssembler *Asm,
220 const MCFixup *Fixup) const {
221 std::optional<int64_t> Total;
222 switch (Kind) {
223 default:
224 break;
225 case AGVK_ExtraSGPRs:
226 return evaluateExtraSGPRs(Res, Asm, Fixup);
227 case AGVK_AlignTo:
228 return evaluateAlignTo(Res, Asm, Fixup);
230 return evaluateTotalNumVGPR(Res, Asm, Fixup);
231 case AGVK_Occupancy:
232 return evaluateOccupancy(Res, Asm, Fixup);
233 }
234
235 for (const MCExpr *Arg : Args) {
236 MCValue ArgRes;
237 if (!Arg->evaluateAsRelocatable(ArgRes, Asm, Fixup) || !ArgRes.isAbsolute())
238 return false;
239
240 if (!Total.has_value())
241 Total = ArgRes.getConstant();
242 Total = op(Kind, *Total, ArgRes.getConstant());
243 }
244
245 Res = MCValue::get(*Total);
246 return true;
247}
248
250 for (const MCExpr *Arg : Args)
251 Streamer.visitUsedExpr(*Arg);
252}
253
255 for (const MCExpr *Arg : Args) {
256 if (Arg->findAssociatedFragment())
257 return Arg->findAssociatedFragment();
258 }
259 return nullptr;
260}
261
262/// Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed
263/// are unresolvable but needed for further MCExprs). Derived from
264/// implementation of IsaInfo::getNumExtraSGPRs in AMDGPUBaseInfo.cpp.
265///
267 const MCExpr *FlatScrUsed,
268 bool XNACKUsed,
269 MCContext &Ctx) {
270
271 return create(AGVK_ExtraSGPRs,
272 {VCCUsed, FlatScrUsed, MCConstantExpr::create(XNACKUsed, Ctx)},
273 Ctx);
274}
275
277 const MCExpr *NumVGPR,
278 MCContext &Ctx) {
279 return create(AGVK_TotalNumVGPRs, {NumAGPR, NumVGPR}, Ctx);
280}
281
282/// Mimics GCNSubtarget::computeOccupancy for MCExpr.
283///
284/// Remove dependency on GCNSubtarget and depend only only the necessary values
285/// for said occupancy computation. Should match computeOccupancy implementation
286/// without passing \p STM on.
288 const MCExpr *NumSGPRs,
289 const MCExpr *NumVGPRs,
290 const GCNSubtarget &STM,
291 MCContext &Ctx) {
292 unsigned MaxWaves = IsaInfo::getMaxWavesPerEU(&STM);
293 unsigned Granule = IsaInfo::getVGPRAllocGranule(&STM);
294 unsigned TargetTotalNumVGPRs = IsaInfo::getTotalNumVGPRs(&STM);
295 unsigned Generation = STM.getGeneration();
296
297 auto CreateExpr = [&Ctx](unsigned Value) {
298 return MCConstantExpr::create(Value, Ctx);
299 };
300
301 return create(AGVK_Occupancy,
302 {CreateExpr(MaxWaves), CreateExpr(Granule),
303 CreateExpr(TargetTotalNumVGPRs), CreateExpr(Generation),
304 CreateExpr(InitOcc), NumSGPRs, NumVGPRs},
305 Ctx);
306}
307
309 for (const MCExpr *E : getArgs()) {
310 if (E->isSymbolUsedInExpression(Sym))
311 return true;
312 }
313 return false;
314}
315
316static KnownBits fromOptionalToKnownBits(std::optional<bool> CompareResult) {
317 static constexpr unsigned BitWidth = 64;
318 const APInt True(BitWidth, 1);
319 const APInt False(BitWidth, 0);
320 if (CompareResult) {
321 return *CompareResult ? KnownBits::makeConstant(True)
323 }
324
325 KnownBits UnknownBool(/*BitWidth=*/1);
326 return UnknownBool.zext(BitWidth);
327}
328
330static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
331 unsigned Depth = 0);
332
333static void binaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
334 unsigned Depth) {
335 static constexpr unsigned BitWidth = 64;
336 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
337 const MCExpr *LHS = BExpr->getLHS();
338 const MCExpr *RHS = BExpr->getRHS();
339
340 knownBitsMapHelper(LHS, KBM, Depth + 1);
341 knownBitsMapHelper(RHS, KBM, Depth + 1);
342 KnownBits LHSKnown = KBM[LHS];
343 KnownBits RHSKnown = KBM[RHS];
344
345 switch (BExpr->getOpcode()) {
346 default:
347 KBM[Expr] = KnownBits(BitWidth);
348 return;
350 KBM[Expr] = KnownBits::add(LHSKnown, RHSKnown);
351 return;
353 KBM[Expr] = LHSKnown & RHSKnown;
354 return;
356 KBM[Expr] = KnownBits::sdiv(LHSKnown, RHSKnown);
357 return;
359 std::optional<bool> CompareRes = KnownBits::eq(LHSKnown, RHSKnown);
360 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
361 return;
362 }
364 std::optional<bool> CompareRes = KnownBits::ne(LHSKnown, RHSKnown);
365 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
366 return;
367 }
369 std::optional<bool> CompareRes = KnownBits::sgt(LHSKnown, RHSKnown);
370 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
371 return;
372 }
374 std::optional<bool> CompareRes = KnownBits::sge(LHSKnown, RHSKnown);
375 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
376 return;
377 }
379 std::optional<bool> CompareRes;
380 const APInt False(BitWidth, 0);
381 std::optional<bool> LHSBool =
382 KnownBits::ne(LHSKnown, KnownBits::makeConstant(False));
383 std::optional<bool> RHSBool =
384 KnownBits::ne(RHSKnown, KnownBits::makeConstant(False));
385 if (LHSBool && RHSBool)
386 CompareRes = *LHSBool && *RHSBool;
387 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
388 return;
389 }
391 const APInt False(BitWidth, 0);
392 KnownBits Bits = LHSKnown | RHSKnown;
393 std::optional<bool> CompareRes =
395 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
396 return;
397 }
399 std::optional<bool> CompareRes = KnownBits::slt(LHSKnown, RHSKnown);
400 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
401 return;
402 }
404 std::optional<bool> CompareRes = KnownBits::sle(LHSKnown, RHSKnown);
405 KBM[Expr] = fromOptionalToKnownBits(CompareRes);
406 return;
407 }
409 KBM[Expr] = KnownBits::srem(LHSKnown, RHSKnown);
410 return;
412 KBM[Expr] = KnownBits::mul(LHSKnown, RHSKnown);
413 return;
415 KBM[Expr] = LHSKnown | RHSKnown;
416 return;
418 KBM[Expr] = KnownBits::shl(LHSKnown, RHSKnown);
419 return;
421 KBM[Expr] = KnownBits::ashr(LHSKnown, RHSKnown);
422 return;
424 KBM[Expr] = KnownBits::lshr(LHSKnown, RHSKnown);
425 return;
427 KBM[Expr] = KnownBits::sub(LHSKnown, RHSKnown);
428 return;
430 KBM[Expr] = LHSKnown ^ RHSKnown;
431 return;
432 }
433}
434
435static void unaryOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
436 unsigned Depth) {
437 static constexpr unsigned BitWidth = 64;
438 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
439 knownBitsMapHelper(UExpr->getSubExpr(), KBM, Depth + 1);
440 KnownBits KB = KBM[UExpr->getSubExpr()];
441
442 switch (UExpr->getOpcode()) {
443 default:
444 KBM[Expr] = KnownBits(BitWidth);
445 return;
447 KB.makeNegative();
448 KBM[Expr] = KB;
449 return;
450 }
453 AllOnes.setAllOnes();
454 KBM[Expr] = KB ^ AllOnes;
455 return;
456 }
458 KB.makeNonNegative();
459 KBM[Expr] = KB;
460 return;
461 }
462 }
463}
464
465static void targetOpKnownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
466 unsigned Depth) {
467 static constexpr unsigned BitWidth = 64;
468 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
469
470 switch (AGVK->getKind()) {
471 default:
472 KBM[Expr] = KnownBits(BitWidth);
473 return;
475 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
476 KnownBits KB = KBM[AGVK->getSubExpr(0)];
477 for (const MCExpr *Arg : AGVK->getArgs()) {
478 knownBitsMapHelper(Arg, KBM, Depth + 1);
479 KB |= KBM[Arg];
480 }
481 KBM[Expr] = KB;
482 return;
483 }
485 knownBitsMapHelper(AGVK->getSubExpr(0), KBM, Depth + 1);
486 KnownBits KB = KBM[AGVK->getSubExpr(0)];
487 for (const MCExpr *Arg : AGVK->getArgs()) {
488 knownBitsMapHelper(Arg, KBM, Depth + 1);
489 KB = KnownBits::umax(KB, KBM[Arg]);
490 }
491 KBM[Expr] = KB;
492 return;
493 }
498 int64_t Val;
499 if (AGVK->evaluateAsAbsolute(Val)) {
500 APInt APValue(BitWidth, Val);
501 KBM[Expr] = KnownBits::makeConstant(APValue);
502 return;
503 }
504 KBM[Expr] = KnownBits(BitWidth);
505 return;
506 }
507 }
508}
509
510static void knownBitsMapHelper(const MCExpr *Expr, KnownBitsMap &KBM,
511 unsigned Depth) {
512 static constexpr unsigned BitWidth = 64;
513
514 int64_t Val;
515 if (Expr->evaluateAsAbsolute(Val)) {
516 APInt APValue(BitWidth, Val, /*isSigned=*/true);
517 KBM[Expr] = KnownBits::makeConstant(APValue);
518 return;
519 }
520
521 if (Depth == 16) {
522 KBM[Expr] = KnownBits(BitWidth);
523 return;
524 }
525
526 switch (Expr->getKind()) {
529 return;
530 }
532 const MCConstantExpr *CE = cast<MCConstantExpr>(Expr);
533 APInt APValue(BitWidth, CE->getValue(), /*isSigned=*/true);
534 KBM[Expr] = KnownBits::makeConstant(APValue);
535 return;
536 }
538 const MCSymbolRefExpr *RExpr = cast<MCSymbolRefExpr>(Expr);
539 const MCSymbol &Sym = RExpr->getSymbol();
540 if (!Sym.isVariable()) {
541 KBM[Expr] = KnownBits(BitWidth);
542 return;
543 }
544
545 // Variable value retrieval is not for actual use but only for knownbits
546 // analysis.
547 const MCExpr *SymVal = Sym.getVariableValue(/*setUsed=*/false);
548 knownBitsMapHelper(SymVal, KBM, Depth + 1);
549
550 // Explicitly copy-construct so that there exists a local KnownBits in case
551 // KBM[SymVal] gets invalidated after a potential growth through KBM[Expr].
552 KBM[Expr] = KnownBits(KBM[SymVal]);
553 return;
554 }
557 return;
558 }
561 return;
562 }
563 }
564}
565
566static const MCExpr *tryFoldHelper(const MCExpr *Expr, KnownBitsMap &KBM,
567 MCContext &Ctx) {
568 if (!KBM.count(Expr))
569 return Expr;
570
571 auto ValueCheckKnownBits = [](KnownBits &KB, unsigned Value) -> bool {
572 if (!KB.isConstant())
573 return false;
574
575 return Value == KB.getConstant();
576 };
577
578 if (Expr->getKind() == MCExpr::ExprKind::Constant)
579 return Expr;
580
581 // Resolving unary operations to constants may make the value more ambiguous.
582 // For example, `~62` becomes `-63`; however, to me it's more ambiguous if a
583 // bit mask value is represented through a negative number.
584 if (Expr->getKind() != MCExpr::ExprKind::Unary) {
585 if (KBM[Expr].isConstant()) {
586 APInt ConstVal = KBM[Expr].getConstant();
587 return MCConstantExpr::create(ConstVal.getSExtValue(), Ctx);
588 }
589
590 int64_t EvalValue;
591 if (Expr->evaluateAsAbsolute(EvalValue))
592 return MCConstantExpr::create(EvalValue, Ctx);
593 }
594
595 switch (Expr->getKind()) {
596 default:
597 return Expr;
599 const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
600 const MCExpr *LHS = BExpr->getLHS();
601 const MCExpr *RHS = BExpr->getRHS();
602
603 switch (BExpr->getOpcode()) {
604 default:
605 return Expr;
607 if (ValueCheckKnownBits(KBM[RHS], 0))
608 return tryFoldHelper(LHS, KBM, Ctx);
609 break;
610 }
613 if (ValueCheckKnownBits(KBM[LHS], 0))
614 return tryFoldHelper(RHS, KBM, Ctx);
615 if (ValueCheckKnownBits(KBM[RHS], 0))
616 return tryFoldHelper(LHS, KBM, Ctx);
617 break;
618 }
620 if (ValueCheckKnownBits(KBM[LHS], 1))
621 return tryFoldHelper(RHS, KBM, Ctx);
622 if (ValueCheckKnownBits(KBM[RHS], 1))
623 return tryFoldHelper(LHS, KBM, Ctx);
624 break;
625 }
629 if (ValueCheckKnownBits(KBM[RHS], 0))
630 return tryFoldHelper(LHS, KBM, Ctx);
631 if (ValueCheckKnownBits(KBM[LHS], 0))
632 return MCConstantExpr::create(0, Ctx);
633 break;
634 }
636 if (ValueCheckKnownBits(KBM[LHS], 0) || ValueCheckKnownBits(KBM[RHS], 0))
637 return MCConstantExpr::create(0, Ctx);
638 break;
639 }
640 }
641 const MCExpr *NewLHS = tryFoldHelper(LHS, KBM, Ctx);
642 const MCExpr *NewRHS = tryFoldHelper(RHS, KBM, Ctx);
643 if (NewLHS != LHS || NewRHS != RHS)
644 return MCBinaryExpr::create(BExpr->getOpcode(), NewLHS, NewRHS, Ctx,
645 BExpr->getLoc());
646 return Expr;
647 }
649 const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
650 const MCExpr *SubExpr = UExpr->getSubExpr();
651 const MCExpr *NewSubExpr = tryFoldHelper(SubExpr, KBM, Ctx);
652 if (SubExpr != NewSubExpr)
653 return MCUnaryExpr::create(UExpr->getOpcode(), NewSubExpr, Ctx,
654 UExpr->getLoc());
655 return Expr;
656 }
658 const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
660 bool Changed = false;
661 for (const MCExpr *Arg : AGVK->getArgs()) {
662 const MCExpr *NewArg = tryFoldHelper(Arg, KBM, Ctx);
663 NewArgs.push_back(NewArg);
664 Changed |= Arg != NewArg;
665 }
666 return Changed ? AMDGPUMCExpr::create(AGVK->getKind(), NewArgs, Ctx) : Expr;
667 }
668 }
669 return Expr;
670}
671
673 MCContext &Ctx) {
674 KnownBitsMap KBM;
675 knownBitsMapHelper(Expr, KBM);
676 const MCExpr *NewExpr = tryFoldHelper(Expr, KBM, Ctx);
677
678 return Expr != NewExpr ? NewExpr : Expr;
679}
680
682 const MCAsmInfo *MAI) {
683 int64_t Val;
684 if (Expr->evaluateAsAbsolute(Val)) {
685 OS << Val;
686 return;
687 }
688
689 Expr->print(OS, MAI);
690}
#define Success
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)
Symbol * Sym
Definition: ELF_riscv.cpp:479
AMD GCN specific subclass of TargetSubtarget.
#define op(i)
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Value * LHS
AMDGPU target specific MCExpr operations.
Definition: AMDGPUMCExpr.h:30
ArrayRef< const MCExpr * > getArgs() const
Definition: AMDGPUMCExpr.h:93
MCFragment * findAssociatedFragment() const override
void visitUsedExpr(MCStreamer &Streamer) const override
static const AMDGPUMCExpr * createOccupancy(unsigned InitOcc, const MCExpr *NumSGPRs, const MCExpr *NumVGPRs, const GCNSubtarget &STM, MCContext &Ctx)
Mimics GCNSubtarget::computeOccupancy for MCExpr.
static const AMDGPUMCExpr * createTotalNumVGPR(const MCExpr *NumAGPR, const MCExpr *NumVGPR, MCContext &Ctx)
static const AMDGPUMCExpr * create(VariantKind Kind, ArrayRef< const MCExpr * > Args, MCContext &Ctx)
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...
bool isSymbolUsedInExpression(const MCSymbol *Sym) const override
const MCExpr * getSubExpr(size_t Index) const
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const override
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
VariantKind getKind() const
Definition: AMDGPUMCExpr.h:94
Class for arbitrary precision integers.
Definition: APInt.h:78
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1542
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
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:152
Generation getGeneration() const
Definition: GCNSubtarget.h:327
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Binary assembler expressions.
Definition: MCExpr.h:493
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:640
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:643
Opcode getOpcode() const
Get the kind of this binary expression.
Definition: MCExpr.h:637
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:211
@ Div
Signed division.
Definition: MCExpr.h:498
@ Shl
Shift left.
Definition: MCExpr.h:515
@ AShr
Arithmetic shift right.
Definition: MCExpr.h:516
@ LShr
Logical shift right.
Definition: MCExpr.h:517
@ GTE
Signed greater than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition: MCExpr.h:502
@ EQ
Equality comparison.
Definition: MCExpr.h:499
@ Sub
Subtraction.
Definition: MCExpr.h:518
@ Mul
Multiplication.
Definition: MCExpr.h:511
@ GT
Signed greater than comparison (result is either 0 or some target-specific non-zero value)
Definition: MCExpr.h:500
@ Mod
Signed remainder.
Definition: MCExpr.h:510
@ And
Bitwise and.
Definition: MCExpr.h:497
@ Or
Bitwise or.
Definition: MCExpr.h:513
@ Xor
Bitwise exclusive or.
Definition: MCExpr.h:519
@ LAnd
Logical and.
Definition: MCExpr.h:504
@ LOr
Logical or.
Definition: MCExpr.h:505
@ LT
Signed less than comparison (result is either 0 or some target-specific non-zero value).
Definition: MCExpr.h:506
@ Add
Addition.
Definition: MCExpr.h:496
@ LTE
Signed less than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition: MCExpr.h:508
@ NE
Inequality comparison.
Definition: MCExpr.h:512
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Context object for machine code objects.
Definition: MCContext.h:83
void * allocate(unsigned Size, unsigned Align=8)
Definition: MCContext.h:816
void deallocate(void *Ptr)
Definition: MCContext.h:820
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCContext.h:418
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm, const SectionAddrMap &Addrs) const
Try to evaluate the expression to an absolute value.
Definition: MCExpr.cpp:581
@ Unary
Unary expressions.
Definition: MCExpr.h:40
@ Constant
Constant expressions.
Definition: MCExpr.h:38
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:39
@ Target
Target specific expression.
Definition: MCExpr.h:41
@ Binary
Binary expressions.
Definition: MCExpr.h:37
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:819
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:1060
ExprKind getKind() const
Definition: MCExpr.h:78
SMLoc getLoc() const
Definition: MCExpr.h:79
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:213
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:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:411
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Unary assembler expressions.
Definition: MCExpr.h:437
Opcode getOpcode() const
Get the kind of this unary expression.
Definition: MCExpr.h:480
static const MCUnaryExpr * create(Opcode Op, const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:217
@ Minus
Unary minus.
Definition: MCExpr.h:441
@ Plus
Unary plus.
Definition: MCExpr.h:443
@ Not
Bitwise negation.
Definition: MCExpr.h:442
const MCExpr * getSubExpr() const
Get the child of this unary expression.
Definition: MCExpr.h:483
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:49
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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 getTotalNumVGPRs(const MCSubtargetInfo *STI)
unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI)
unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed, bool FlatScrUsed, bool XNACKUsed)
unsigned getNumWavesPerEUWithNumVGPRs(const MCSubtargetInfo *STI, unsigned NumVGPRs)
unsigned getOccupancyWithNumSGPRs(unsigned SGPRs, unsigned MaxWaves, AMDGPUSubtarget::Generation Gen)
unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI, std::optional< bool > EnableWavefrontSize32)
void printAMDGPUMCExpr(const MCExpr *Expr, raw_ostream &OS, const MCAsmInfo *MAI)
bool isGFX90A(const MCSubtargetInfo &STI)
const MCExpr * foldAMDGPUMCExpr(const MCExpr *Expr, MCContext &Ctx)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:293
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
Definition: KnownBits.cpp:488
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:428
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:496
void makeNegative()
Make this value negative.
Definition: KnownBits.h:111
static std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGE result.
Definition: KnownBits.cpp:536
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:164
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:53
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition: KnownBits.h:336
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLT result.
Definition: KnownBits.cpp:542
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
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:342
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
static std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLE result.
Definition: KnownBits.cpp:546
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
Definition: KnownBits.cpp:526
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:285
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:59