LLVM 24.0.0git
NVPTXPeephole.cpp
Go to the documentation of this file.
1//===-- NVPTXPeephole.cpp - NVPTX Peephole Optimiztions -------------------===//
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// In NVPTX, NVPTXFrameLowering will emit following instruction at the beginning
10// of a MachineFunction.
11//
12// mov %SPL, %depot
13// cvta.local %SP, %SPL
14//
15// Because Frame Index is a generic address and alloca can only return generic
16// pointer, without this pass the instructions producing alloca'ed address will
17// be based on %SP. NVPTXLowerAlloca tends to help replace store and load on
18// this address with their .local versions, but this may introduce a lot of
19// cvta.to.local instructions. Performance can be improved if we avoid casting
20// address back and forth and directly calculate local address based on %SPL.
21// This peephole pass optimizes these cases, for example
22//
23// It will transform the following pattern
24// %0 = LEA_ADDRi64 %VRFrame64, 4
25// %1 = cvta_to_local_64 %0
26//
27// into
28// %1 = LEA_ADDRi64 %VRFrameLocal64, 4
29//
30// %VRFrameLocal64 is the virtual register name of %SPL
31//
32//===----------------------------------------------------------------------===//
33
34#include "NVPTX.h"
35#include "NVPTXRegisterInfo.h"
36#include "NVPTXSubtarget.h"
42
43using namespace llvm;
44
45#define DEBUG_TYPE "nvptx-peephole"
46
47namespace {
48struct NVPTXPeephole : public MachineFunctionPass {
49 public:
50 static char ID;
51 NVPTXPeephole() : MachineFunctionPass(ID) {}
52
53 bool runOnMachineFunction(MachineFunction &MF) override;
54
55 StringRef getPassName() const override {
56 return "NVPTX optimize redundant cvta.to.local instruction";
57 }
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 }
62};
63}
64
65char NVPTXPeephole::ID = 0;
66
67INITIALIZE_PASS(NVPTXPeephole, "nvptx-peephole", "NVPTX Peephole", false, false)
68
70 auto &MBB = *Root.getParent();
71 auto &MF = *MBB.getParent();
72 // Check current instruction is cvta.to.local
73 if (Root.getOpcode() != NVPTX::cvta_to_local_64 &&
74 Root.getOpcode() != NVPTX::cvta_to_local_32)
75 return false;
76
77 auto &Op = Root.getOperand(1);
78 const auto &MRI = MF.getRegInfo();
79 MachineInstr *GenericAddrDef = nullptr;
80 if (Op.isReg() && Op.getReg().isVirtual()) {
81 GenericAddrDef = MRI.getUniqueVRegDef(Op.getReg());
82 }
83
84 // Check the register operand is uniquely defined by LEA_ADDRi instruction
85 if (!GenericAddrDef || GenericAddrDef->getParent() != &MBB ||
86 (GenericAddrDef->getOpcode() != NVPTX::LEA_ADDRi64 &&
87 GenericAddrDef->getOpcode() != NVPTX::LEA_ADDRi)) {
88 return false;
89 }
90
91 const NVPTXRegisterInfo *NRI =
92 MF.getSubtarget<NVPTXSubtarget>().getRegisterInfo();
93
94 // LEA and %SPL must have the same width.
95 if ((GenericAddrDef->getOpcode() == NVPTX::LEA_ADDRi64) !=
96 (NRI->getFrameLocalRegister(MF) == NVPTX::VRFrameLocal64))
97 return false;
98
99 // Check the LEA_ADDRi operand is Frame index
100 auto &BaseAddrOp = GenericAddrDef->getOperand(1);
101 if (BaseAddrOp.isReg() && BaseAddrOp.getReg() == NRI->getFrameRegister(MF)) {
102 return true;
103 }
104
105 return false;
106}
107
109 auto &MBB = *Root.getParent();
110 auto &MF = *MBB.getParent();
111 const auto &MRI = MF.getRegInfo();
112 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
113 auto &Prev = *MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
114
115 const NVPTXRegisterInfo *NRI =
116 MF.getSubtarget<NVPTXSubtarget>().getRegisterInfo();
117
119 BuildMI(MF, Root.getDebugLoc(), TII->get(Prev.getOpcode()),
120 Root.getOperand(0).getReg())
122 .add(Prev.getOperand(2));
123
124 MBB.insert((MachineBasicBlock::iterator)&Root, MIB);
125
126 // Check if MRI has only one non dbg use, which is Root
127 if (MRI.hasOneNonDBGUse(Prev.getOperand(0).getReg())) {
128 Prev.eraseFromParent();
129 }
130 Root.eraseFromParent();
131}
132
133bool NVPTXPeephole::runOnMachineFunction(MachineFunction &MF) {
134 if (skipFunction(MF.getFunction()))
135 return false;
136
137 bool Changed = false;
138 // Loop over all of the basic blocks.
139 for (auto &MBB : MF) {
140 // Traverse the basic block.
141 auto BlockIter = MBB.begin();
142
143 while (BlockIter != MBB.end()) {
144 auto &MI = *BlockIter++;
147 Changed = true;
148 }
149 } // Instruction
150 } // Basic Block
151
152 const NVPTXRegisterInfo *NRI =
153 MF.getSubtarget<NVPTXSubtarget>().getRegisterInfo();
154
155 // Remove unnecessary %VRFrame = cvta.local %VRFrameLocal
156 const auto &MRI = MF.getRegInfo();
157 if (MRI.use_empty(NRI->getFrameRegister(MF))) {
158 if (auto MI = MRI.getUniqueVRegDef(NRI->getFrameRegister(MF))) {
159 MI->eraseFromParent();
160 }
161 }
162
163 return Changed;
164}
165
166MachineFunctionPass *llvm::createNVPTXPeephole() { return new NVPTXPeephole(); }
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
static bool isCVTAToLocalCombinationCandidate(MachineInstr &Root)
static void CombineCVTAToLocal(MachineInstr &Root)
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Represent the analysis usage information of a pass.
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Register getReg() const
getReg - Returns the register number.
Register getFrameLocalRegister(const MachineFunction &MF) const
Register getFrameRegister(const MachineFunction &MF) const override
const NVPTXRegisterInfo * getRegisterInfo() const override
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
Changed
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.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineFunctionPass * createNVPTXPeephole()
DWARFExpression::Operation Op