LLVM 22.0.0git
RISCVRedundantCopyElimination.cpp
Go to the documentation of this file.
1//=- RISCVRedundantCopyElimination.cpp - Remove useless copy for RISC-V -----=//
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// This pass removes unnecessary zero copies in BBs that are targets of
10// beqz/bnez instructions. For instance, the copy instruction in the code below
11// can be removed because the beqz jumps to BB#2 when a0 is zero.
12// BB#1:
13// beqz %a0, <BB#2>
14// BB#2:
15// %a0 = COPY %x0
16//
17// This pass also recognizes Xqcibi branch-immediate forms when compared
18// against non-zero immediates.
19//
20// This pass should be run after register allocation and is based on the
21// earliest versions of AArch64RedundantCopyElimination.
22//
23// FIXME: Support compare with non-zero immediates where the immediate is stored
24// in a register.
25//
26//===----------------------------------------------------------------------===//
27
28#include "RISCV.h"
29#include "RISCVInstrInfo.h"
30#include "llvm/ADT/Statistic.h"
33#include "llvm/Support/Debug.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "riscv-copyelim"
38
39STATISTIC(NumCopiesRemoved, "Number of copies removed.");
40
41namespace {
42class RISCVRedundantCopyElimination : public MachineFunctionPass {
45 const TargetInstrInfo *TII;
46
47public:
48 static char ID;
49 RISCVRedundantCopyElimination() : MachineFunctionPass(ID) {}
50
51 bool runOnMachineFunction(MachineFunction &MF) override;
52 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().setNoVRegs();
54 }
55
56 StringRef getPassName() const override {
57 return "RISC-V Redundant Copy Elimination";
58 }
59
60private:
61 bool optimizeBlock(MachineBasicBlock &MBB);
62};
63
64} // end anonymous namespace
65
66char RISCVRedundantCopyElimination::ID = 0;
67
68INITIALIZE_PASS(RISCVRedundantCopyElimination, "riscv-copyelim",
69 "RISC-V Redundant Copy Elimination", false, false)
70
71static bool
72guaranteesZeroRegInBlock(MachineBasicBlock &MBB,
75 assert(Cond.size() == 3 && "Unexpected number of operands");
76 assert(TBB != nullptr && "Expected branch target basic block");
77 auto Opc = Cond[0].getImm();
78 if (Opc == RISCV::BEQ && Cond[2].isReg() && Cond[2].getReg() == RISCV::X0 &&
79 TBB == &MBB)
80 return true;
81 if (Opc == RISCV::BNE && Cond[2].isReg() && Cond[2].getReg() == RISCV::X0 &&
82 TBB != &MBB)
83 return true;
84 return false;
85}
86
87static bool
91 assert(Cond.size() == 3 && "Unexpected number of operands");
92 assert(TBB != nullptr && "Expected branch target basic block");
93 auto Opc = Cond[0].getImm();
94 if ((Opc == RISCV::QC_BEQI || Opc == RISCV::QC_E_BEQI ||
95 Opc == RISCV::NDS_BEQC || Opc == RISCV::BEQI) &&
96 Cond[2].isImm() && Cond[2].getImm() != 0 && TBB == &MBB)
97 return true;
98 if ((Opc == RISCV::QC_BNEI || Opc == RISCV::QC_E_BNEI ||
99 Opc == RISCV::NDS_BNEC || Opc == RISCV::BNEI) &&
100 Cond[2].isImm() && Cond[2].getImm() != 0 && TBB != &MBB)
101 return true;
102 return false;
103}
104
105bool RISCVRedundantCopyElimination::optimizeBlock(MachineBasicBlock &MBB) {
106 // Check if the current basic block has a single predecessor.
107 if (MBB.pred_size() != 1)
108 return false;
109
110 // Check if the predecessor has two successors, implying the block ends in a
111 // conditional branch.
112 MachineBasicBlock *PredMBB = *MBB.pred_begin();
113 if (PredMBB->succ_size() != 2)
114 return false;
115
116 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
118 if (TII->analyzeBranch(*PredMBB, TBB, FBB, Cond, /*AllowModify*/ false) ||
119 Cond.empty())
120 return false;
121
122 Register TargetReg = Cond[1].getReg();
123
124 if (!TargetReg)
125 return false;
126
127 bool IsZeroCopy = guaranteesZeroRegInBlock(MBB, Cond, TBB);
128
129 if (!IsZeroCopy && !guaranteesRegEqualsImmInBlock(MBB, Cond, TBB))
130 return false;
131
132 bool Changed = false;
134 // Remove redundant Copy instructions unless TargetReg is modified.
135 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
136 MachineInstr *MI = &*I;
137 ++I;
138 bool RemoveMI = false;
139 if (IsZeroCopy) {
140 if (MI->isCopy() && MI->getOperand(0).isReg() &&
141 MI->getOperand(1).isReg()) {
142 Register DefReg = MI->getOperand(0).getReg();
143 Register SrcReg = MI->getOperand(1).getReg();
144
145 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
146 TargetReg == DefReg)
147 RemoveMI = true;
148 }
149 } else {
150 // Xqcibi, XAndesPref and Zibi compare with non-zero immediate:
151 // remove redundant addi rd,x0,imm or qc.li rd,imm as applicable.
152 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(0).isReg() &&
153 MI->getOperand(1).isReg() && MI->getOperand(2).isImm()) {
154 Register DefReg = MI->getOperand(0).getReg();
155 Register SrcReg = MI->getOperand(1).getReg();
156 int64_t Imm = MI->getOperand(2).getImm();
157 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
158 TargetReg == DefReg && Imm == Cond[2].getImm())
159 RemoveMI = true;
160 } else if (MI->getOpcode() == RISCV::QC_LI && MI->getOperand(0).isReg() &&
161 MI->getOperand(1).isImm()) {
162 Register DefReg = MI->getOperand(0).getReg();
163 int64_t Imm = MI->getOperand(1).getImm();
164 if (!MRI->isReserved(DefReg) && TargetReg == DefReg &&
165 Imm == Cond[2].getImm())
166 RemoveMI = true;
167 }
168 }
169
170 if (RemoveMI) {
171 LLVM_DEBUG(dbgs() << "Remove redundant Copy: ");
172 LLVM_DEBUG(MI->print(dbgs()));
173
174 MI->eraseFromParent();
175 Changed = true;
176 LastChange = I;
177 ++NumCopiesRemoved;
178 continue;
179 }
180
181 if (MI->modifiesRegister(TargetReg, TRI))
182 break;
183 }
184
185 if (!Changed)
186 return false;
187
189 assert((CondBr->getOpcode() == RISCV::BEQ ||
190 CondBr->getOpcode() == RISCV::BNE ||
191 CondBr->getOpcode() == RISCV::BEQI ||
192 CondBr->getOpcode() == RISCV::BNEI ||
193 CondBr->getOpcode() == RISCV::QC_BEQI ||
194 CondBr->getOpcode() == RISCV::QC_BNEI ||
195 CondBr->getOpcode() == RISCV::QC_E_BEQI ||
196 CondBr->getOpcode() == RISCV::QC_E_BNEI ||
197 CondBr->getOpcode() == RISCV::NDS_BEQC ||
198 CondBr->getOpcode() == RISCV::NDS_BNEC) &&
199 "Unexpected opcode");
200 assert(CondBr->getOperand(0).getReg() == TargetReg && "Unexpected register");
201
202 // Otherwise, we have to fixup the use-def chain, starting with the
203 // BEQ(I)/BNE(I). Conservatively mark as much as we can live.
204 CondBr->clearRegisterKills(TargetReg, TRI);
205
206 // Add newly used reg to the block's live-in list if it isn't there already.
207 if (!MBB.isLiveIn(TargetReg))
208 MBB.addLiveIn(TargetReg);
209
210 // Clear any kills of TargetReg between CondBr and the last removed COPY.
211 for (MachineInstr &MMI : make_range(MBB.begin(), LastChange))
212 MMI.clearRegisterKills(TargetReg, TRI);
213
214 return true;
215}
216
217bool RISCVRedundantCopyElimination::runOnMachineFunction(MachineFunction &MF) {
218 if (skipFunction(MF.getFunction()))
219 return false;
220
223 MRI = &MF.getRegInfo();
224
225 bool Changed = false;
226 for (MachineBasicBlock &MBB : MF)
228
229 return Changed;
230}
231
233 return new RISCVRedundantCopyElimination();
234}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool guaranteesRegEqualsImmInBlock(MachineBasicBlock &MBB, const SmallVectorImpl< MachineOperand > &Cond, MachineBasicBlock *TBB)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
assert(TBB !=nullptr &&"Expected branch target basic block")
static bool optimizeBlock(BasicBlock &BB, bool &ModifiedDT, const TargetTransformInfo &TTI, const DataLayout &DL, bool HasBranchDivergence, DomTreeUpdater *DTU)
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:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
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.
Definition Types.h:26
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
FunctionPass * createRISCVRedundantCopyEliminationPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...