LLVM  16.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"
24 #include "SIInstrInfo.h"
25 #include "Utils/AMDGPUBaseInfo.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringMap.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Debug.h"
34 #include <utility>
35 
36 #define DEBUG_TYPE "gcn-create-vopd"
37 STATISTIC(NumVOPDCreated, "Number of VOPD Insts Created.");
38 
39 using namespace llvm;
40 
41 namespace {
42 
43 class GCNCreateVOPD : public MachineFunctionPass {
44 private:
45 public:
46  static char ID;
47  const GCNSubtarget *ST = nullptr;
48 
49  GCNCreateVOPD() : MachineFunctionPass(ID) {}
50 
51  void getAnalysisUsage(AnalysisUsage &AU) const override {
52  AU.setPreservesCFG();
54  }
55 
56  StringRef getPassName() const override {
57  return "GCN Create VOPD Instructions";
58  }
59 
60  bool doReplace(const SIInstrInfo *SII,
61  std::pair<MachineInstr *, MachineInstr *> &Pair) {
62  auto *FirstMI = Pair.first;
63  auto *SecondMI = Pair.second;
64  unsigned Opc1 = FirstMI->getOpcode();
65  unsigned Opc2 = SecondMI->getOpcode();
66  int NewOpcode = AMDGPU::getVOPDFull(AMDGPU::getVOPDOpcode(Opc1),
67  AMDGPU::getVOPDOpcode(Opc2));
68  assert(NewOpcode != -1 &&
69  "Should have previously determined this as a possible VOPD\n");
70 
71  auto VOPDInst = BuildMI(*FirstMI->getParent(), FirstMI,
72  FirstMI->getDebugLoc(), SII->get(NewOpcode))
73  .setMIFlags(FirstMI->getFlags() | SecondMI->getFlags());
74 
75  namespace VOPD = AMDGPU::VOPD;
76  MachineInstr *MI[] = {FirstMI, SecondMI};
77  auto InstInfo =
78  AMDGPU::getVOPDInstInfo(FirstMI->getDesc(), SecondMI->getDesc());
79 
80  for (auto CompIdx : VOPD::COMPONENTS) {
81  auto MCOprIdx = InstInfo[CompIdx].getIndexOfDstInMCOperands();
82  VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
83  }
84 
85  for (auto CompIdx : VOPD::COMPONENTS) {
86  auto CompSrcOprNum = InstInfo[CompIdx].getCompSrcOperandsNum();
87  for (unsigned CompSrcIdx = 0; CompSrcIdx < CompSrcOprNum; ++CompSrcIdx) {
88  auto MCOprIdx = InstInfo[CompIdx].getIndexOfSrcInMCOperands(CompSrcIdx);
89  VOPDInst.add(MI[CompIdx]->getOperand(MCOprIdx));
90  }
91  }
92 
93  for (auto CompIdx : VOPD::COMPONENTS)
94  VOPDInst.copyImplicitOps(*MI[CompIdx]);
95 
96  LLVM_DEBUG(dbgs() << "VOPD Fused: " << *VOPDInst << " from\tX: "
97  << *Pair.first << "\tY: " << *Pair.second << "\n");
98 
99  for (auto CompIdx : VOPD::COMPONENTS)
100  MI[CompIdx]->eraseFromParent();
101 
102  ++NumVOPDCreated;
103  return true;
104  }
105 
106  bool runOnMachineFunction(MachineFunction &MF) override {
107  if (skipFunction(MF.getFunction()))
108  return false;
109  ST = &MF.getSubtarget<GCNSubtarget>();
110  if (!AMDGPU::hasVOPD(*ST) || !ST->isWave32())
111  return false;
112  LLVM_DEBUG(dbgs() << "CreateVOPD Pass:\n");
113 
114  const SIInstrInfo *SII = ST->getInstrInfo();
115  bool Changed = false;
116 
118 
119  for (auto &MBB : MF) {
120  auto MII = MBB.begin(), E = MBB.end();
121  while (MII != E) {
122  auto *FirstMI = &*MII;
123  MII = next_nodbg(MII, MBB.end());
124  if (MII == MBB.end())
125  break;
126  if (FirstMI->isDebugInstr())
127  continue;
128  auto *SecondMI = &*MII;
129  unsigned Opc = FirstMI->getOpcode();
130  unsigned Opc2 = SecondMI->getOpcode();
131  llvm::AMDGPU::CanBeVOPD FirstCanBeVOPD = AMDGPU::getCanBeVOPD(Opc);
132  llvm::AMDGPU::CanBeVOPD SecondCanBeVOPD = AMDGPU::getCanBeVOPD(Opc2);
133  std::pair<MachineInstr *, MachineInstr *> Pair;
134 
135  if (FirstCanBeVOPD.X && SecondCanBeVOPD.Y)
136  Pair = {FirstMI, SecondMI};
137  else if (FirstCanBeVOPD.Y && SecondCanBeVOPD.X)
138  Pair = {SecondMI, FirstMI};
139  else
140  continue;
141  // checkVOPDRegConstraints cares about program order, but doReplace
142  // cares about X-Y order in the constituted VOPD
143  if (llvm::checkVOPDRegConstraints(*SII, *FirstMI, *SecondMI)) {
144  ReplaceCandidates.push_back(Pair);
145  ++MII;
146  }
147  }
148  }
149  for (auto &Pair : ReplaceCandidates) {
150  Changed |= doReplace(SII, Pair);
151  }
152 
153  return Changed;
154  }
155 };
156 
157 } // namespace
158 
159 char GCNCreateVOPD::ID = 0;
160 
162 
163 INITIALIZE_PASS(GCNCreateVOPD, DEBUG_TYPE, "GCN Create VOPD Instructions",
164  false, false)
llvm::next_nodbg
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
Definition: MachineBasicBlock.h:1289
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AMDGPU::hasVOPD
bool hasVOPD(const MCSubtargetInfo &STI)
Definition: AMDGPUBaseInfo.cpp:1978
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
Statistic.h
llvm::checkVOPDRegConstraints
bool checkVOPDRegConstraints(const SIInstrInfo &TII, const MachineInstr &FirstMI, const MachineInstr &SecondMI)
Definition: GCNVOPDUtils.cpp:37
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
MachineBasicBlock.h
llvm::AMDGPU::getVOPDFull
int getVOPDFull(unsigned OpX, unsigned OpY)
Definition: AMDGPUBaseInfo.cpp:487
GCNVOPDUtils.h
llvm::GCNSubtarget
Definition: GCNSubtarget.h:31
llvm::AMDGPU::CanBeVOPD::X
bool X
Definition: AMDGPUBaseInfo.h:486
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:167
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::AMDGPU::CanBeVOPD::Y
bool Y
Definition: AMDGPUBaseInfo.h:487
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
GCNSubtarget.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
DEBUG_TYPE
#define DEBUG_TYPE
Definition: GCNCreateVOPD.cpp:36
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::AMDGPU::getVOPDInstInfo
VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
Definition: AMDGPUBaseInfo.cpp:585
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:657
AMDGPUMCTargetDesc.h
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
STATISTIC
STATISTIC(NumVOPDCreated, "Number of VOPD Insts Created.")
llvm::AMDGPU::CanBeVOPD
Definition: AMDGPUBaseInfo.h:485
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SIInstrInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
AMDGPU.h
llvm::AMDGPU::VOPD::COMPONENTS
constexpr unsigned COMPONENTS[]
Definition: AMDGPUBaseInfo.h:537
llvm::GCNCreateVOPDID
char & GCNCreateVOPDID
Definition: GCNCreateVOPD.cpp:161
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:623
Casting.h
llvm::AMDGPU::getVOPDOpcode
unsigned getVOPDOpcode(unsigned Opc)
Definition: AMDGPUBaseInfo.cpp:428
llvm::SIInstrInfo
Definition: SIInstrInfo.h:44
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
SmallVector.h
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:305
llvm::MachineInstrBuilder::setMIFlags
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Definition: MachineInstrBuilder.h:273
llvm::AMDGPU::getCanBeVOPD
CanBeVOPD getCanBeVOPD(unsigned Opc)
Definition: AMDGPUBaseInfo.cpp:420
MachineOperand.h
StringMap.h
Debug.h
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:307
AMDGPUBaseInfo.h