53#define DEBUG_TYPE "amdgpu-lower-vgpr-encoding"
57class AMDGPULowerVGPREncoding {
58 static constexpr unsigned OpNum = 4;
59 static constexpr unsigned BitsPerField = 2;
60 static constexpr unsigned NumFields = 4;
61 static constexpr unsigned ModeWidth = NumFields * BitsPerField;
62 static constexpr unsigned ModeMask = (1 << ModeWidth) - 1;
63 static constexpr unsigned VGPRMSBShift =
68 std::optional<unsigned> MSBits;
70 bool update(
const OpMode &New,
bool &Rewritten) {
73 if (*New.MSBits != MSBits.value_or(0)) {
75 Rewritten |= MSBits.has_value();
86 bool update(
const ModeTy &New,
bool &Rewritten) {
88 for (
unsigned I :
seq(OpNum))
89 Updated |=
Ops[
I].update(New.Ops[
I], Rewritten);
97 V |=
Op.MSBits.value_or(0) << (
I * 2);
103 bool isCompatible(
const ModeTy NewMode)
const {
104 for (
unsigned I :
seq(OpNum)) {
105 if (!NewMode.Ops[
I].MSBits.has_value())
107 if (
Ops[
I].MSBits.value_or(0) != NewMode.Ops[
I].MSBits.value_or(0))
134 unsigned ClauseRemaining;
137 unsigned ClauseBreaks;
148 for (OpMode &
Op :
Mode.Ops)
164 const AMDGPU::OpName
Ops[OpNum],
165 const AMDGPU::OpName *Ops2 =
nullptr);
190bool AMDGPULowerVGPREncoding::setMode(ModeTy NewMode,
193 int64_t OldModeBits = CurrentMode.encode() << ModeWidth;
195 bool Rewritten =
false;
196 if (!CurrentMode.update(NewMode, Rewritten))
199 if (MostRecentModeSet && !Rewritten) {
202 if (MostRecentModeSet->
getOpcode() == AMDGPU::S_SET_VGPR_MSB) {
205 int64_t OldModeBits =
Op.getImm() & (ModeMask << ModeWidth);
206 Op.setImm(CurrentMode.encode() | OldModeBits);
209 "unexpected MostRecentModeSet opcode");
210 updateSetregModeImm(*MostRecentModeSet, CurrentMode.encode());
217 I = handleCoissue(
I);
218 MostRecentModeSet =
BuildMI(*
MBB,
I, {},
TII->get(AMDGPU::S_SET_VGPR_MSB))
219 .
addImm(NewMode.encode() | OldModeBits);
221 CurrentMode = NewMode;
225std::optional<unsigned>
232 if (!RC || !
TRI->isVGPRClass(RC))
235 unsigned Idx =
TRI->getHWRegIndex(
Reg);
239void AMDGPULowerVGPREncoding::computeMode(ModeTy &NewMode,
241 const AMDGPU::OpName
Ops[OpNum],
242 const AMDGPU::OpName *Ops2) {
245 for (
unsigned I = 0;
I < OpNum; ++
I) {
248 std::optional<unsigned> MSBits;
250 MSBits = getMSBs(*
Op);
253 if (MSBits.has_value() && Ops2) {
256 std::optional<unsigned> MSBits2;
257 MSBits2 = getMSBs(*Op2);
258 if (MSBits2.has_value() && MSBits != MSBits2)
264 if (!MSBits.has_value() && Ops2) {
265 Op =
TII->getNamedOperand(
MI, Ops2[
I]);
267 MSBits = getMSBs(*
Op);
270 if (!MSBits.has_value())
276 if (
Ops[
I] == AMDGPU::OpName::src2 && !
Op->isDef() &&
Op->isTied() &&
279 TII->hasVALU32BitEncoding(
MI.getOpcode()))))
282 NewMode.Ops[
I].MSBits = MSBits.value();
286bool AMDGPULowerVGPREncoding::runOnMachineInstr(
MachineInstr &
MI) {
290 computeMode(NewMode,
MI,
Ops.first,
Ops.second);
291 if (!CurrentMode.isCompatible(NewMode) &&
MI.isCommutable() &&
292 TII->commuteInstruction(
MI)) {
293 ModeTy NewModeCommuted;
294 computeMode(NewModeCommuted,
MI,
Ops.first,
Ops.second);
295 if (CurrentMode.isCompatible(NewModeCommuted))
298 if (!
TII->commuteInstruction(
MI))
301 return setMode(NewMode,
MI.getIterator());
303 assert(!
TII->hasVGPRUses(
MI) ||
MI.isMetaInstruction() ||
MI.isPseudo());
310 if (!ClauseRemaining)
315 if (ClauseRemaining == ClauseLen) {
316 I =
Clause->getPrevNode()->getIterator();
324 Clause->eraseFromBundle();
334 Clause->getOperand(0).setImm(ClauseLen | (ClauseBreaks << 8));
350 unsigned Opc =
MI->getOpcode();
352 Opc == AMDGPU::S_DELAY_ALU;
355 while (!
I.isEnd() &&
I !=
I->getParent()->begin()) {
356 auto Prev = std::prev(
I);
357 if (!isProgramStateInstr(&*Prev))
369static int64_t convertModeToSetregFormat(int64_t
Mode) {
374bool AMDGPULowerVGPREncoding::updateSetregModeImm(
MachineInstr &
MI,
376 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32);
379 int64_t SetregMode = convertModeToSetregFormat(ModeValue);
382 int64_t OldImm = ImmOp->
getImm();
384 (OldImm &
~AMDGPU::Hwreg::VGPR_MSB_MASK) | (SetregMode << VGPRMSBShift);
386 return NewImm != OldImm;
392 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
393 "only S_SETREG_IMM32_B32 needs to be handled");
396 assert(SIMM16Op &&
"SIMM16Op must be present");
400 if (HwRegId != ID_MODE)
403 int64_t ModeValue = CurrentMode.encode();
408 if (
Size <= VGPRMSBShift) {
411 MostRecentModeSet = &
MI;
412 return updateSetregModeImm(
MI, ModeValue);
420 assert(ImmOp &&
"ImmOp must be present");
421 int64_t ImmBits12To19 = (ImmOp->
getImm() & VGPR_MSB_MASK) >> VGPRMSBShift;
422 int64_t SetregModeValue = convertModeToSetregFormat(ModeValue);
423 if (ImmBits12To19 == SetregModeValue) {
428 MostRecentModeSet =
nullptr;
435 MostRecentModeSet =
BuildMI(*
MBB, InsertPt,
MI.getDebugLoc(),
436 TII->get(AMDGPU::S_SET_VGPR_MSB))
443 if (!ST.has1024AddressableVGPRs())
446 TII = ST.getInstrInfo();
447 TRI = ST.getRegisterInfo();
450 ClauseLen = ClauseRemaining = 0;
452 for (
auto &
MBB : MF) {
453 MostRecentModeSet =
nullptr;
457 if (
MI.isMetaInstruction())
460 if (
MI.isTerminator() ||
MI.isCall()) {
461 if (
MI.getOpcode() == AMDGPU::S_ENDPGM ||
462 MI.getOpcode() == AMDGPU::S_ENDPGM_SAVED)
465 resetMode(
MI.getIterator());
469 if (
MI.isInlineAsm()) {
470 if (
TII->hasVGPRUses(
MI))
471 resetMode(
MI.getIterator());
475 if (
MI.getOpcode() == AMDGPU::S_CLAUSE) {
476 assert(!ClauseRemaining &&
"Nested clauses are not supported");
477 ClauseLen =
MI.getOperand(0).getImm();
478 ClauseBreaks = (ClauseLen >> 8) & 15;
479 ClauseLen = ClauseRemaining = (ClauseLen & 63) + 1;
484 if (
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
485 ST.hasSetregVGPRMSBFixup()) {
497 resetMode(
MBB.instr_end());
510 return AMDGPULowerVGPREncoding().run(MF);
521char AMDGPULowerVGPREncodingLegacy::ID = 0;
526 "AMDGPU Lower VGPR Encoding",
false,
false)
531 if (!AMDGPULowerVGPREncoding().run(MF))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Interface definition for SIInstrInfo.
This file implements the C++20 <bit> header.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
Wrapper class representing physical registers. Should be passed by value.
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
static bool isVOP2(const MachineInstr &MI)
static bool isVOP3(const MCInstrDesc &Desc)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< const AMDGPU::OpName *, const AMDGPU::OpName * > getVGPRLoweringOperandTables(const MCInstrDesc &Desc)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
DWARFExpression::Operation Op
constexpr int countr_zero_constexpr(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
char & AMDGPULowerVGPREncodingLegacyID
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
constexpr T rotl(T V, int R)