LLVM: lib/Target/AMDGPU/GCNCreateVOPD.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
32
33#define DEBUG_TYPE "gcn-create-vopd"
34STATISTIC(NumVOPDCreated, "Number of VOPD Insts Created.");
35
36using namespace llvm;
37
38namespace {
39
40class GCNCreateVOPD {
41private:
42 class VOPDCombineInfo {
43 public:
44 VOPDCombineInfo() = default;
46 bool VOPD3 = false)
47 : FirstMI(First), SecondMI(Second), IsVOPD3(VOPD3) {}
48
49 MachineInstr *FirstMI;
50 MachineInstr *SecondMI;
51 bool IsVOPD3;
52 };
53
54public:
55 const GCNSubtarget *ST = nullptr;
56
57 bool doReplace(const SIInstrInfo *SII, VOPDCombineInfo &CI) {
58 auto *FirstMI = CI.FirstMI;
59 auto *SecondMI = CI.SecondMI;
60 unsigned Opc1 = FirstMI->getOpcode();
61 unsigned Opc2 = SecondMI->getOpcode();
62 unsigned EncodingFamily =
66 EncodingFamily, CI.IsVOPD3);
67 assert(NewOpcode != -1 &&
68 "Should have previously determined this as a possible VOPD\n");
69
70 auto VOPDInst = BuildMI(*FirstMI->getParent(), FirstMI,
71 FirstMI->getDebugLoc(), SII->get(NewOpcode))
72 .setMIFlags(FirstMI->getFlags() | SecondMI->getFlags());
73
74 namespace VOPD = AMDGPU::VOPD;
75 MachineInstr *MI[] = {FirstMI, SecondMI};
76 auto InstInfo =
78
79 for (auto CompIdx : VOPD::COMPONENTS) {
80 auto MCOprIdx = InstInfo[CompIdx].getIndexOfDstInMCOperands();
81 VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
82 }
83
84 const AMDGPU::OpName Mods[2][3] = {
85 {AMDGPU::OpName::src0X_modifiers, AMDGPU::OpName::vsrc1X_modifiers,
86 AMDGPU::OpName::vsrc2X_modifiers},
87 {AMDGPU::OpName::src0Y_modifiers, AMDGPU::OpName::vsrc1Y_modifiers,
88 AMDGPU::OpName::vsrc2Y_modifiers}};
89 const AMDGPU::OpName SrcMods[3] = {AMDGPU::OpName::src0_modifiers,
90 AMDGPU::OpName::src1_modifiers,
91 AMDGPU::OpName::src2_modifiers};
92 const unsigned VOPDOpc = VOPDInst->getOpcode();
93
94 for (auto CompIdx : VOPD::COMPONENTS) {
95 auto CompSrcOprNum = InstInfo[CompIdx].getCompSrcOperandsNum();
96 bool IsVOP3 = SII->isVOP3(*MI[CompIdx]);
97 for (unsigned CompSrcIdx = 0; CompSrcIdx < CompSrcOprNum; ++CompSrcIdx) {
99 const MachineOperand *Mod =
101 VOPDInst.addImm(Mod ? Mod->getImm() : 0);
102 }
103 auto MCOprIdx =
104 InstInfo[CompIdx].getIndexOfSrcInMCOperands(CompSrcIdx, IsVOP3);
105 VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
106 }
107 if (MI[CompIdx]->getOpcode() == AMDGPU::V_CNDMASK_B32_e32 && CI.IsVOPD3)
108 VOPDInst.addReg(AMDGPU::VCC_LO);
109 }
110
111 if (CI.IsVOPD3) {
113 VOPDInst.addImm(BitOp2);
114 }
115
117 for (auto CompIdx : VOPD::COMPONENTS)
118 VOPDInst.copyImplicitOps(*MI[CompIdx]);
119
120 LLVM_DEBUG(dbgs() << "VOPD Fused: " << *VOPDInst << " from\tX: "
121 << *CI.FirstMI << "\tY: " << *CI.SecondMI << "\n");
122
123 for (auto CompIdx : VOPD::COMPONENTS)
124 MI[CompIdx]->eraseFromParent();
125
126 ++NumVOPDCreated;
127 return true;
128 }
129
130 bool run(MachineFunction &MF) {
133 return false;
135
136 const SIInstrInfo *SII = ST->getInstrInfo();
139 bool HasVOPD3 = ST->hasVOPD3();
140
142
143 for (auto &MBB : MF) {
145 while (MII != E) {
146 auto *FirstMI = &*MII;
149 break;
150 if (FirstMI->isDebugInstr())
151 continue;
152 auto *SecondMI = &*MII;
153 unsigned Opc = FirstMI->getOpcode();
154 unsigned Opc2 = SecondMI->getOpcode();
155 VOPDCombineInfo CI;
156
157 const auto checkVOPD = [&](bool VOPD3) -> bool {
158 llvm::AMDGPU::CanBeVOPD FirstCanBeVOPD =
160 llvm::AMDGPU::CanBeVOPD SecondCanBeVOPD =
162
163 if (FirstCanBeVOPD.X && SecondCanBeVOPD.Y)
164 CI = VOPDCombineInfo(FirstMI, SecondMI, VOPD3);
165 else if (FirstCanBeVOPD.Y && SecondCanBeVOPD.X)
166 CI = VOPDCombineInfo(SecondMI, FirstMI, VOPD3);
167 else
168 return false;
169
170
172 VOPD3);
173 };
174
175 if (checkVOPD(false) || (HasVOPD3 && checkVOPD(true))) {
177 ++MII;
178 }
179 }
180 }
181 for (auto &CI : ReplaceCandidates) {
182 Changed |= doReplace(SII, CI);
183 }
184
186 }
187};
188
190public:
191 static char ID;
192 GCNCreateVOPDLegacy() : MachineFunctionPass(ID) {}
193
194 void getAnalysisUsage(AnalysisUsage &AU) const override {
197 }
198
199 StringRef getPassName() const override {
200 return "GCN Create VOPD Instructions";
201 }
202 bool runOnMachineFunction(MachineFunction &MF) override {
204 return false;
205
206 return GCNCreateVOPD().run(MF);
207 }
208};
209
210}
211
215 if (!GCNCreateVOPD().run(MF))
218}
219
220char GCNCreateVOPDLegacy::ID = 0;
221
223
225 false, false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Interface definition for SIInstrInfo.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &AM)
Definition GCNCreateVOPD.cpp:213
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
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.
const GCNSubtarget & getSubtarget() const
static bool isVOP3(const MCInstrDesc &Desc)
void fixImplicitOperands(MachineInstr &MI) const
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, AMDGPU::OpName OperandName) const
Returns the operand named Op.
void push_back(const T &Elt)
unsigned getVOPDOpcode(unsigned Opc, bool VOPD3)
CanBeVOPD getCanBeVOPD(unsigned Opc, unsigned EncodingFamily, bool VOPD3)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
unsigned getVOPDEncodingFamily(const MCSubtargetInfo &ST)
unsigned getBitOp2(unsigned Opc)
VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
bool hasVOPD(const MCSubtargetInfo &STI)
int getVOPDFull(unsigned OpX, unsigned OpY, unsigned EncodingFamily, bool VOPD3)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool checkVOPDRegConstraints(const SIInstrInfo &TII, const MachineInstr &FirstMI, const MachineInstr &SecondMI, bool IsVOPD3)
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.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Mod
The access may modify the value stored in memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
char & GCNCreateVOPDID
Definition GCNCreateVOPD.cpp:222