38#define DEBUG_TYPE "amdgpu-rewrite-agpr-copy-mfma"
43 "Number of MFMA instructions rewritten to use AGPR form");
45class AMDGPURewriteAGPRCopyMFMAImpl {
65 LIS(LIS), RegClassInfo(RegClassInfo) {}
100 bool recomputeRegClassExceptRewritable(
109bool AMDGPURewriteAGPRCopyMFMAImpl::recomputeRegClassExceptRewritable(
115 while (!Worklist.
empty()) {
133 if (isRewriteCandidate(*
MI)) {
135 TII.getNamedOperand(*
MI, AMDGPU::OpName::vdst);
137 TII.getNamedOperand(*
MI, AMDGPU::OpName::src2);
146 if (OtherReg !=
Reg && RewriteRegs.
insert(OtherReg))
153 dbgs() <<
"Attempting to replace VGPR MFMA with AGPR version:"
172 unsigned OpNo = &MO - &
MI->getOperand(0);
173 NewRC =
MI->getRegClassConstraintEffect(OpNo, NewRC, &
TII, &
TRI);
174 if (!NewRC || NewRC == OldRC) {
176 <<
" cannot be reassigned to "
177 <<
TRI.getRegClassName(NewRC) <<
": " << *
MI);
186bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
195 RewriteRegs.
insert(MFMAHintReg);
206 if (!recomputeRegClassExceptRewritable(MFMAHintReg, RewriteCandidates,
208 LLVM_DEBUG(
dbgs() <<
"Could not recompute the regclass of dst reg "
229 using RecoloringStack =
231 RecoloringStack TentativeReassignments;
233 for (
Register RewriteReg : RewriteRegs) {
235 TentativeReassignments.push_back({&LI, VRM.
getPhys(RewriteReg)});
239 if (!attemptReassignmentsToAGPR(RewriteRegs, PhysRegHint)) {
241 for (
auto [LI, OldAssign] : TentativeReassignments) {
244 LRM.
assign(*LI, OldAssign);
252 for (
Register InterferingReg : RewriteRegs) {
254 TRI.getEquivalentAGPRClass(
MRI.getRegClass(InterferingReg));
255 MRI.setRegClass(InterferingReg, EquivalentAGPRRegClass);
258 for (
MachineInstr *RewriteCandidate : RewriteCandidates) {
261 RewriteCandidate->setDesc(
TII.get(NewMFMAOp));
262 ++NumMFMAsRewrittenToAGPR;
271bool AMDGPURewriteAGPRCopyMFMAImpl::attemptReassignmentsToAGPR(
277 for (
Register InterferingReg : InterferingRegs) {
280 TRI.getEquivalentAGPRClass(
MRI.getRegClass(InterferingReg));
282 MCPhysReg Assignable = AMDGPU::NoRegister;
283 if (EquivalentAGPRRegClass->
contains(PrefPhysReg) &&
293 Assignable = PrefPhysReg;
296 RegClassInfo.
getOrder(EquivalentAGPRRegClass);
308 <<
" to a free AGPR\n");
314 LRM.
assign(ReassignLI, Assignable);
326bool AMDGPURewriteAGPRCopyMFMAImpl::tryFoldCopiesToAGPR(
328 bool MadeChange =
false;
351 if (isRewriteCandidate(CopySrcDefMI) &&
352 tryReassigningMFMAChain(
353 CopySrcDefMI, CopySrcDefMI.getOperand(0).getReg(), AssignedAGPR))
368bool AMDGPURewriteAGPRCopyMFMAImpl::tryFoldCopiesFromAGPR(
370 bool MadeChange =
false;
379 if (!CopyUseMO.readsReg())
383 if (isRewriteCandidate(CopyUseMI)) {
384 if (tryReassigningMFMAChain(CopyUseMI, CopyDstReg,
402 LLVM_DEBUG(
dbgs() <<
"skipping function that did not allocate AGPRs\n");
406 bool MadeChange =
false;
408 for (
unsigned I = 0,
E =
MRI.getNumVirtRegs();
I !=
E; ++
I) {
410 MCRegister AssignedAGPR = getAssignedAGPR(VReg);
414 if (tryFoldCopiesToAGPR(VReg, AssignedAGPR))
416 if (tryFoldCopiesFromAGPR(VReg, AssignedAGPR))
436 return "AMDGPU Rewrite AGPR-Copy-MFMA";
455 "AMDGPU Rewrite AGPR-Copy-MFMA",
false,
false)
462char AMDGPURewriteAGPRCopyMFMALegacy::
ID = 0;
465 AMDGPURewriteAGPRCopyMFMALegacy::
ID;
467bool AMDGPURewriteAGPRCopyMFMALegacy::runOnMachineFunction(
469 if (skipFunction(MF.getFunction()))
472 RegClassInfo.runOnMachineFunction(MF);
474 auto &VRM = getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
475 auto &LRM = getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
476 auto &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
478 AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, RegClassInfo);
491 AMDGPURewriteAGPRCopyMFMAImpl Impl(MF, VRM, LRM, LIS, RegClassInfo);
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
AMDGPU Rewrite AGPR Copy MFMA
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Interface definition for SIRegisterInfo.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents analyses that only rely on functions' control flow.
bool hasGFX90AInsts() const
LiveInterval - This class represents the liveness of a register, or stack slot.
LiveInterval & getInterval(Register Reg)
void unassign(const LiveInterval &VirtReg)
Unassign VirtReg from its PhysReg.
bool isPhysRegUsed(MCRegister PhysReg) const
Returns true if the given PhysReg has any live intervals assigned.
@ IK_Free
No interference, go ahead and assign.
void assign(const LiveInterval &VirtReg, MCRegister PhysReg)
Assign VirtReg to PhysReg.
InterferenceKind checkInterference(const LiveInterval &VirtReg, MCRegister PhysReg)
Check for interference before assigning VirtReg to PhysReg.
Wrapper class representing physical registers. Should be passed by value.
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
LLVM_READONLY int getMFMASrcCVDstAGPROp(uint16_t Opcode)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void initializeAMDGPURewriteAGPRCopyMFMALegacyPass(PassRegistry &)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
DWARFExpression::Operation Op
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
char & AMDGPURewriteAGPRCopyMFMALegacyID
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.