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) && Cond[2].isImm() &&
96 Cond[2].getImm() != 0 && TBB == &MBB)
97 return true;
98 if ((Opc == RISCV::QC_BNEI || Opc == RISCV::QC_E_BNEI) && Cond[2].isImm() &&
99 Cond[2].getImm() != 0 && TBB != &MBB)
100 return true;
101 return false;
102}
103
104bool RISCVRedundantCopyElimination::optimizeBlock(MachineBasicBlock &MBB) {
105 // Check if the current basic block has a single predecessor.
106 if (MBB.pred_size() != 1)
107 return false;
108
109 // Check if the predecessor has two successors, implying the block ends in a
110 // conditional branch.
111 MachineBasicBlock *PredMBB = *MBB.pred_begin();
112 if (PredMBB->succ_size() != 2)
113 return false;
114
115 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
117 if (TII->analyzeBranch(*PredMBB, TBB, FBB, Cond, /*AllowModify*/ false) ||
118 Cond.empty())
119 return false;
120
121 Register TargetReg = Cond[1].getReg();
122
123 if (!TargetReg)
124 return false;
125
126 bool IsZeroCopy = guaranteesZeroRegInBlock(MBB, Cond, TBB);
127
128 if (!IsZeroCopy && !guaranteesRegEqualsImmInBlock(MBB, Cond, TBB))
129 return false;
130
131 bool Changed = false;
133 // Remove redundant Copy instructions unless TargetReg is modified.
134 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
135 MachineInstr *MI = &*I;
136 ++I;
137 bool RemoveMI = false;
138 if (IsZeroCopy) {
139 if (MI->isCopy() && MI->getOperand(0).isReg() &&
140 MI->getOperand(1).isReg()) {
141 Register DefReg = MI->getOperand(0).getReg();
142 Register SrcReg = MI->getOperand(1).getReg();
143
144 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
145 TargetReg == DefReg)
146 RemoveMI = true;
147 }
148 } else {
149 // Xqcibi compare with non-zero immediate:
150 // remove redundant addi rd,x0,imm or qc.li rd,imm as applicable.
151 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(0).isReg() &&
152 MI->getOperand(1).isReg() && MI->getOperand(2).isImm()) {
153 Register DefReg = MI->getOperand(0).getReg();
154 Register SrcReg = MI->getOperand(1).getReg();
155 int64_t Imm = MI->getOperand(2).getImm();
156 if (SrcReg == RISCV::X0 && !MRI->isReserved(DefReg) &&
157 TargetReg == DefReg && Imm == Cond[2].getImm())
158 RemoveMI = true;
159 } else if (MI->getOpcode() == RISCV::QC_LI && MI->getOperand(0).isReg() &&
160 MI->getOperand(1).isImm()) {
161 Register DefReg = MI->getOperand(0).getReg();
162 int64_t Imm = MI->getOperand(1).getImm();
163 if (!MRI->isReserved(DefReg) && TargetReg == DefReg &&
164 Imm == Cond[2].getImm())
165 RemoveMI = true;
166 }
167 }
168
169 if (RemoveMI) {
170 LLVM_DEBUG(dbgs() << "Remove redundant Copy: ");
171 LLVM_DEBUG(MI->print(dbgs()));
172
173 MI->eraseFromParent();
174 Changed = true;
175 LastChange = I;
176 ++NumCopiesRemoved;
177 continue;
178 }
179
180 if (MI->modifiesRegister(TargetReg, TRI))
181 break;
182 }
183
184 if (!Changed)
185 return false;
186
188 assert((CondBr->getOpcode() == RISCV::BEQ ||
189 CondBr->getOpcode() == RISCV::BNE ||
190 CondBr->getOpcode() == RISCV::QC_BEQI ||
191 CondBr->getOpcode() == RISCV::QC_BNEI ||
192 CondBr->getOpcode() == RISCV::QC_E_BEQI ||
193 CondBr->getOpcode() == RISCV::QC_E_BNEI) &&
194 "Unexpected opcode");
195 assert(CondBr->getOperand(0).getReg() == TargetReg && "Unexpected register");
196
197 // Otherwise, we have to fixup the use-def chain, starting with the
198 // BEQ(I)/BNE(I). Conservatively mark as much as we can live.
199 CondBr->clearRegisterKills(TargetReg, TRI);
200
201 // Add newly used reg to the block's live-in list if it isn't there already.
202 if (!MBB.isLiveIn(TargetReg))
203 MBB.addLiveIn(TargetReg);
204
205 // Clear any kills of TargetReg between CondBr and the last removed COPY.
206 for (MachineInstr &MMI : make_range(MBB.begin(), LastChange))
207 MMI.clearRegisterKills(TargetReg, TRI);
208
209 return true;
210}
211
212bool RISCVRedundantCopyElimination::runOnMachineFunction(MachineFunction &MF) {
213 if (skipFunction(MF.getFunction()))
214 return false;
215
218 MRI = &MF.getRegInfo();
219
220 bool Changed = false;
221 for (MachineBasicBlock &MBB : MF)
223
224 return Changed;
225}
226
228 return new RISCVRedundantCopyElimination();
229}
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...