LLVM 20.0.0git
GCNCreateVOPD.cpp
Go to the documentation of this file.
1//===- GCNCreateVOPD.cpp - Create VOPD Instructions ----------------------===//
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/// \file
10/// Combine VALU pairs into VOPD instructions
11/// Only works on wave32
12/// Has register requirements, we reject creating VOPD if the requirements are
13/// not met.
14/// shouldCombineVOPD mutator in postRA machine scheduler puts candidate
15/// instructions for VOPD back-to-back
16///
17//
18//===----------------------------------------------------------------------===//
19
20#include "AMDGPU.h"
21#include "GCNSubtarget.h"
22#include "GCNVOPDUtils.h"
23#include "SIInstrInfo.h"
26#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Debug.h"
31
32#define DEBUG_TYPE "gcn-create-vopd"
33STATISTIC(NumVOPDCreated, "Number of VOPD Insts Created.");
34
35using namespace llvm;
36
37namespace {
38
39class GCNCreateVOPD : public MachineFunctionPass {
40private:
41 class VOPDCombineInfo {
42 public:
43 VOPDCombineInfo() = default;
44 VOPDCombineInfo(MachineInstr *First, MachineInstr *Second)
45 : FirstMI(First), SecondMI(Second) {}
46
47 MachineInstr *FirstMI;
48 MachineInstr *SecondMI;
49 };
50
51public:
52 static char ID;
53 const GCNSubtarget *ST = nullptr;
54
55 GCNCreateVOPD() : MachineFunctionPass(ID) {}
56
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
58 AU.setPreservesCFG();
60 }
61
62 StringRef getPassName() const override {
63 return "GCN Create VOPD Instructions";
64 }
65
66 bool doReplace(const SIInstrInfo *SII, VOPDCombineInfo &CI) {
67 auto *FirstMI = CI.FirstMI;
68 auto *SecondMI = CI.SecondMI;
69 unsigned Opc1 = FirstMI->getOpcode();
70 unsigned Opc2 = SecondMI->getOpcode();
71 unsigned EncodingFamily =
73 int NewOpcode =
75 AMDGPU::getVOPDOpcode(Opc2), EncodingFamily);
76 assert(NewOpcode != -1 &&
77 "Should have previously determined this as a possible VOPD\n");
78
79 auto VOPDInst = BuildMI(*FirstMI->getParent(), FirstMI,
80 FirstMI->getDebugLoc(), SII->get(NewOpcode))
81 .setMIFlags(FirstMI->getFlags() | SecondMI->getFlags());
82
83 namespace VOPD = AMDGPU::VOPD;
84 MachineInstr *MI[] = {FirstMI, SecondMI};
85 auto InstInfo =
86 AMDGPU::getVOPDInstInfo(FirstMI->getDesc(), SecondMI->getDesc());
87
88 for (auto CompIdx : VOPD::COMPONENTS) {
89 auto MCOprIdx = InstInfo[CompIdx].getIndexOfDstInMCOperands();
90 VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
91 }
92
93 for (auto CompIdx : VOPD::COMPONENTS) {
94 auto CompSrcOprNum = InstInfo[CompIdx].getCompSrcOperandsNum();
95 for (unsigned CompSrcIdx = 0; CompSrcIdx < CompSrcOprNum; ++CompSrcIdx) {
96 auto MCOprIdx = InstInfo[CompIdx].getIndexOfSrcInMCOperands(CompSrcIdx);
97 VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
98 }
99 }
100
101 SII->fixImplicitOperands(*VOPDInst);
102 for (auto CompIdx : VOPD::COMPONENTS)
103 VOPDInst.copyImplicitOps(*MI[CompIdx]);
104
105 LLVM_DEBUG(dbgs() << "VOPD Fused: " << *VOPDInst << " from\tX: "
106 << *CI.FirstMI << "\tY: " << *CI.SecondMI << "\n");
107
108 for (auto CompIdx : VOPD::COMPONENTS)
109 MI[CompIdx]->eraseFromParent();
110
111 ++NumVOPDCreated;
112 return true;
113 }
114
115 bool runOnMachineFunction(MachineFunction &MF) override {
116 if (skipFunction(MF.getFunction()))
117 return false;
119 if (!AMDGPU::hasVOPD(*ST) || !ST->isWave32())
120 return false;
121 LLVM_DEBUG(dbgs() << "CreateVOPD Pass:\n");
122
123 const SIInstrInfo *SII = ST->getInstrInfo();
124 bool Changed = false;
125
126 SmallVector<VOPDCombineInfo> ReplaceCandidates;
127
128 for (auto &MBB : MF) {
129 auto MII = MBB.begin(), E = MBB.end();
130 while (MII != E) {
131 auto *FirstMI = &*MII;
132 MII = next_nodbg(MII, MBB.end());
133 if (MII == MBB.end())
134 break;
135 if (FirstMI->isDebugInstr())
136 continue;
137 auto *SecondMI = &*MII;
138 unsigned Opc = FirstMI->getOpcode();
139 unsigned Opc2 = SecondMI->getOpcode();
140 llvm::AMDGPU::CanBeVOPD FirstCanBeVOPD = AMDGPU::getCanBeVOPD(Opc);
141 llvm::AMDGPU::CanBeVOPD SecondCanBeVOPD = AMDGPU::getCanBeVOPD(Opc2);
142 VOPDCombineInfo CI;
143
144 if (FirstCanBeVOPD.X && SecondCanBeVOPD.Y)
145 CI = VOPDCombineInfo(FirstMI, SecondMI);
146 else if (FirstCanBeVOPD.Y && SecondCanBeVOPD.X)
147 CI = VOPDCombineInfo(SecondMI, FirstMI);
148 else
149 continue;
150 // checkVOPDRegConstraints cares about program order, but doReplace
151 // cares about X-Y order in the constituted VOPD
152 if (llvm::checkVOPDRegConstraints(*SII, *FirstMI, *SecondMI)) {
153 ReplaceCandidates.push_back(CI);
154 ++MII;
155 }
156 }
157 }
158 for (auto &CI : ReplaceCandidates) {
159 Changed |= doReplace(SII, CI);
160 }
161
162 return Changed;
163 }
164};
165
166} // namespace
167
168char GCNCreateVOPD::ID = 0;
169
170char &llvm::GCNCreateVOPDID = GCNCreateVOPD::ID;
171
172INITIALIZE_PASS(GCNCreateVOPD, DEBUG_TYPE, "GCN Create VOPD Instructions",
173 false, false)
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DEBUG(...)
Definition: Debug.h:106
#define DEBUG_TYPE
AMD GCN specific subclass of TargetSubtarget.
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Interface definition for SIInstrInfo.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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)
Definition: Statistic.h:166
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
Definition: MachineInstr.h:69
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
const GCNSubtarget & getSubtarget() const
Definition: SIInstrInfo.h:231
void fixImplicitOperands(MachineInstr &MI) const
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
CanBeVOPD getCanBeVOPD(unsigned Opc)
unsigned getVOPDOpcode(unsigned Opc)
unsigned getVOPDEncodingFamily(const MCSubtargetInfo &ST)
int getVOPDFull(unsigned OpX, unsigned OpY, unsigned EncodingFamily)
VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
bool hasVOPD(const MCSubtargetInfo &STI)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
char & GCNCreateVOPDID
bool checkVOPDRegConstraints(const SIInstrInfo &TII, const MachineInstr &FirstMI, const MachineInstr &SecondMI)