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);
122 unsigned ClauseRemaining;
125 unsigned ClauseBreaks;
136 for (OpMode &
Op :
Mode.Ops)
152 const AMDGPU::OpName
Ops[OpNum],
153 const AMDGPU::OpName *Ops2 =
nullptr);
178bool AMDGPULowerVGPREncoding::setMode(ModeTy NewMode,
181 int64_t OldModeBits = CurrentMode.encode() << ModeWidth;
183 bool Rewritten =
false;
184 if (!CurrentMode.update(NewMode, Rewritten))
187 if (MostRecentModeSet && !Rewritten) {
190 if (MostRecentModeSet->
getOpcode() == AMDGPU::S_SET_VGPR_MSB) {
193 int64_t OldModeBits =
Op.getImm() & (ModeMask << ModeWidth);
194 Op.setImm(CurrentMode.encode() | OldModeBits);
197 "unexpected MostRecentModeSet opcode");
198 updateSetregModeImm(*MostRecentModeSet, CurrentMode.encode());
205 I = handleCoissue(
I);
206 MostRecentModeSet =
BuildMI(*
MBB,
I, {},
TII->get(AMDGPU::S_SET_VGPR_MSB))
207 .
addImm(NewMode.encode() | OldModeBits);
209 CurrentMode = NewMode;
213std::optional<unsigned>
220 if (!RC || !
TRI->isVGPRClass(RC))
223 unsigned Idx =
TRI->getHWRegIndex(
Reg);
227void AMDGPULowerVGPREncoding::computeMode(ModeTy &NewMode,
MachineInstr &
MI,
228 const AMDGPU::OpName
Ops[OpNum],
229 const AMDGPU::OpName *Ops2) {
232 for (
unsigned I = 0;
I < OpNum; ++
I) {
235 std::optional<unsigned> MSBits;
237 MSBits = getMSBs(*
Op);
240 if (MSBits.has_value() && Ops2) {
241 auto Op2 =
TII->getNamedOperand(
MI, Ops2[
I]);
243 std::optional<unsigned> MSBits2;
244 MSBits2 = getMSBs(*Op2);
245 if (MSBits2.has_value() && MSBits != MSBits2)
251 if (!MSBits.has_value() && Ops2) {
252 Op =
TII->getNamedOperand(
MI, Ops2[
I]);
254 MSBits = getMSBs(*
Op);
257 if (!MSBits.has_value())
263 if (
Ops[
I] == AMDGPU::OpName::src2 && !
Op->isDef() &&
Op->isTied() &&
266 TII->hasVALU32BitEncoding(
MI.getOpcode()))))
269 NewMode.Ops[
I].MSBits = MSBits.value();
273bool AMDGPULowerVGPREncoding::runOnMachineInstr(
MachineInstr &
MI) {
277 computeMode(NewMode,
MI,
Ops.first,
Ops.second);
278 return setMode(NewMode,
MI.getIterator());
280 assert(!
TII->hasVGPRUses(
MI) ||
MI.isMetaInstruction() ||
MI.isPseudo());
287 if (!ClauseRemaining)
292 if (ClauseRemaining == ClauseLen) {
293 I =
Clause->getPrevNode()->getIterator();
301 Clause->eraseFromBundle();
311 Clause->getOperand(0).setImm(ClauseLen | (ClauseBreaks << 8));
327 unsigned Opc =
MI->getOpcode();
329 Opc == AMDGPU::S_DELAY_ALU;
332 while (!
I.isEnd() &&
I !=
I->getParent()->begin()) {
333 auto Prev = std::prev(
I);
334 if (!isProgramStateInstr(&*Prev))
346static int64_t convertModeToSetregFormat(int64_t
Mode) {
351bool AMDGPULowerVGPREncoding::updateSetregModeImm(
MachineInstr &
MI,
353 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32);
356 int64_t SetregMode = convertModeToSetregFormat(ModeValue);
359 int64_t OldImm = ImmOp->
getImm();
361 (OldImm &
~AMDGPU::Hwreg::VGPR_MSB_MASK) | (SetregMode << VGPRMSBShift);
363 return NewImm != OldImm;
369 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
370 "only S_SETREG_IMM32_B32 needs to be handled");
373 assert(SIMM16Op &&
"SIMM16Op must be present");
377 if (HwRegId != ID_MODE)
380 int64_t ModeValue = CurrentMode.encode();
385 if (
Size <= VGPRMSBShift) {
388 MostRecentModeSet = &
MI;
389 return updateSetregModeImm(
MI, ModeValue);
397 assert(ImmOp &&
"ImmOp must be present");
398 int64_t ImmBits12To19 = (ImmOp->
getImm() & VGPR_MSB_MASK) >> VGPRMSBShift;
399 int64_t SetregModeValue = convertModeToSetregFormat(ModeValue);
400 if (ImmBits12To19 == SetregModeValue) {
405 MostRecentModeSet =
nullptr;
412 MostRecentModeSet =
BuildMI(*
MBB, InsertPt,
MI.getDebugLoc(),
413 TII->get(AMDGPU::S_SET_VGPR_MSB))
420 if (!ST.has1024AddressableVGPRs())
423 TII = ST.getInstrInfo();
424 TRI = ST.getRegisterInfo();
427 ClauseLen = ClauseRemaining = 0;
429 for (
auto &
MBB : MF) {
430 MostRecentModeSet =
nullptr;
434 if (
MI.isMetaInstruction())
437 if (
MI.isTerminator() ||
MI.isCall()) {
438 if (
MI.getOpcode() == AMDGPU::S_ENDPGM ||
439 MI.getOpcode() == AMDGPU::S_ENDPGM_SAVED)
442 resetMode(
MI.getIterator());
446 if (
MI.isInlineAsm()) {
447 if (
TII->hasVGPRUses(
MI))
448 resetMode(
MI.getIterator());
452 if (
MI.getOpcode() == AMDGPU::S_CLAUSE) {
453 assert(!ClauseRemaining &&
"Nested clauses are not supported");
454 ClauseLen =
MI.getOperand(0).getImm();
455 ClauseBreaks = (ClauseLen >> 8) & 15;
456 ClauseLen = ClauseRemaining = (ClauseLen & 63) + 1;
461 if (
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
462 ST.hasSetregVGPRMSBFixup()) {
474 resetMode(
MBB.instr_end());
487 return AMDGPULowerVGPREncoding().run(MF);
498char AMDGPULowerVGPREncodingLegacy::ID = 0;
503 "AMDGPU Lower VGPR Encoding",
false,
false)
508 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)