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 compares with non-zero constants for the Zibi extension. Also,
24// support compare with non-zero immediates where the immediate is stored in a
25// register.
26//
27//===----------------------------------------------------------------------===//
28
29#include "RISCV.h"
30#include "RISCVInstrInfo.h"
31#include "llvm/ADT/Statistic.h"
34#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "riscv-copyelim"
39
40STATISTIC(NumCopiesRemoved, "Number of copies removed.");
41
42namespace {
43class RISCVRedundantCopyElimination : public MachineFunctionPass {
46 const TargetInstrInfo *TII;
47
48public:
49 static char ID;
50 RISCVRedundantCopyElimination() : MachineFunctionPass(ID) {}
51
52 bool runOnMachineFunction(MachineFunction &MF) override;
53 MachineFunctionProperties getRequiredProperties() const override {
54 return MachineFunctionProperties().setNoVRegs();
55 }
56
57 StringRef getPassName() const override {
58 return "RISC-V Redundant Copy Elimination";
59 }
60
61private:
62 bool optimizeBlock(MachineBasicBlock &MBB);
63};
64
65} // end anonymous namespace
66
67char RISCVRedundantCopyElimination::ID = 0;
68
69INITIALIZE_PASS(RISCVRedundantCopyElimination, "riscv-copyelim",
70 "RISC-V Redundant Copy Elimination", false, false)
71
72static bool
73guaranteesZeroRegInBlock(MachineBasicBlock &MBB,
76 assert(Cond.size() == 3 && "Unexpected number of operands");
77 assert(TBB != nullptr && "Expected branch target basic block");
78 auto Opc = Cond[0].getImm();
79 if (Opc == RISCV::BEQ && Cond[2].isReg() && Cond[2].getReg() == RISCV::X0 &&
80 TBB == &MBB)
81 return true;
82 if (Opc == RISCV::BNE && Cond[2].isReg() && Cond[2].getReg() == RISCV::X0 &&
83 TBB != &MBB)
84 return true;
85 return false;
86}
87
88static bool
92 assert(Cond.size() == 3 && "Unexpected number of operands");
93 assert(TBB != nullptr && "Expected branch target basic block");
94 auto Opc = Cond[0].getImm();
95 if ((Opc == RISCV::QC_BEQI || Opc == RISCV::QC_E_BEQI ||
96 Opc == RISCV::NDS_BEQC) &&
97 Cond[2].isImm() && Cond[2].getImm() != 0 && TBB == &MBB)
98 return true;
99 if ((Opc == RISCV::QC_BNEI || Opc == RISCV::QC_E_BNEI ||
100 Opc == RISCV::NDS_BNEC) &&
101 Cond[2].isImm() && Cond[2].getImm() != 0 && TBB != &MBB)
102 return true;
103 return false;
104}
105
106bool RISCVRedundantCopyElimination::optimizeBlock(MachineBasicBlock &MBB) {
107 // Check if the current basic block has a single predecessor.
108 if (MBB.pred_size() != 1)
109 return false;
110
111 // Check if the predecessor has two successors, implying the block ends in a
112 // conditional branch.
113 MachineBasicBlock *PredMBB = *MBB.pred_begin();
114 if (PredMBB->succ_size() != 2)
115 return false;
116
117 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
119 if (TII->analyzeBranch(*PredMBB, TBB, FBB, Cond, /*AllowModify*/ false) ||
120 Cond.empty())
121 return false;
122
123 Register TargetReg = Cond[1].getReg();
124
125 if (!TargetReg)
126 return false;
127
128 bool IsZeroCopy = guaranteesZeroRegInBlock(MBB, Cond, TBB);
129
130 if (!IsZeroCopy && !guaranteesRegEqualsImmInBlock(MBB, Cond, TBB))
131 return false;
132
133 bool Changed = false;
135 // Remove redundant Copy instructions unless TargetReg is modified.
136 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
137 MachineInstr *MI = &*I;
138 ++I;
139 bool RemoveMI = false;
140 if (IsZeroCopy) {
141 if (MI->isCopy() && MI->getOperand(0).isReg() &&
142 MI->getOperand(1).isReg()) {
143 Register DefReg = MI->getOperand(0).getReg();
144 Register SrcReg = MI->getOperand(1).getReg();
145
146 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
147 TargetReg == DefReg)
148 RemoveMI = true;
149 }
150 } else {
151 // Xqcibi compare with non-zero immediate:
152 // remove redundant addi rd,x0,imm or qc.li rd,imm as applicable.
153 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(0).isReg() &&
154 MI->getOperand(1).isReg() && MI->getOperand(2).isImm()) {
155 Register DefReg = MI->getOperand(0).getReg();
156 Register SrcReg = MI->getOperand(1).getReg();
157 int64_t Imm = MI->getOperand(2).getImm();
158 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
159 TargetReg == DefReg && Imm == Cond[2].getImm())
160 RemoveMI = true;
161 } else if (MI->getOpcode() == RISCV::QC_LI && MI->getOperand(0).isReg() &&
162 MI->getOperand(1).isImm()) {
163 Register DefReg = MI->getOperand(0).getReg();
164 int64_t Imm = MI->getOperand(1).getImm();
165 if (!MRI->isReserved(DefReg) && TargetReg == DefReg &&
166 Imm == Cond[2].getImm())
167 RemoveMI = true;
168 }
169 }
170
171 if (RemoveMI) {
172 LLVM_DEBUG(dbgs() << "Remove redundant Copy: ");
173 LLVM_DEBUG(MI->print(dbgs()));
174
175 MI->eraseFromParent();
176 Changed = true;
177 LastChange = I;
178 ++NumCopiesRemoved;
179 continue;
180 }
181
182 if (MI->modifiesRegister(TargetReg, TRI))
183 break;
184 }
185
186 if (!Changed)
187 return false;
188
190 assert((CondBr->getOpcode() == RISCV::BEQ ||
191 CondBr->getOpcode() == RISCV::BNE ||
192 CondBr->getOpcode() == RISCV::QC_BEQI ||
193 CondBr->getOpcode() == RISCV::QC_BNEI ||
194 CondBr->getOpcode() == RISCV::QC_E_BEQI ||
195 CondBr->getOpcode() == RISCV::QC_E_BNEI ||
196 CondBr->getOpcode() == RISCV::NDS_BEQC ||
197 CondBr->getOpcode() == RISCV::NDS_BNEC) &&
198 "Unexpected opcode");
199 assert(CondBr->getOperand(0).getReg() == TargetReg && "Unexpected register");
200
201 // Otherwise, we have to fixup the use-def chain, starting with the
202 // BEQ(I)/BNE(I). Conservatively mark as much as we can live.
203 CondBr->clearRegisterKills(TargetReg, TRI);
204
205 // Add newly used reg to the block's live-in list if it isn't there already.
206 if (!MBB.isLiveIn(TargetReg))
207 MBB.addLiveIn(TargetReg);
208
209 // Clear any kills of TargetReg between CondBr and the last removed COPY.
210 for (MachineInstr &MMI : make_range(MBB.begin(), LastChange))
211 MMI.clearRegisterKills(TargetReg, TRI);
212
213 return true;
214}
215
216bool RISCVRedundantCopyElimination::runOnMachineFunction(MachineFunction &MF) {
217 if (skipFunction(MF.getFunction()))
218 return false;
219
222 MRI = &MF.getRegInfo();
223
224 bool Changed = false;
225 for (MachineBasicBlock &MBB : MF)
227
228 return Changed;
229}
230
232 return new RISCVRedundantCopyElimination();
233}
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...