LLVM 23.0.0git
RISCVDeadRegisterDefinitions.cpp
Go to the documentation of this file.
1//===- RISCVDeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --===//
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 rewrites Rd to x0 for instrs whose return values are unused.
10//
11//===---------------------------------------------------------------------===//
12
13#include "RISCV.h"
14#include "RISCVSubtarget.h"
15#include "llvm/ADT/Statistic.h"
20
21using namespace llvm;
22#define DEBUG_TYPE "riscv-dead-defs"
23#define RISCV_DEAD_REG_DEF_NAME "RISC-V Dead register definitions"
24
25STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
26
27namespace {
28class RISCVDeadRegisterDefinitions : public MachineFunctionPass {
29public:
30 static char ID;
31
32 RISCVDeadRegisterDefinitions() : MachineFunctionPass(ID) {}
33 bool runOnMachineFunction(MachineFunction &MF) override;
34 void getAnalysisUsage(AnalysisUsage &AU) const override {
35 AU.setPreservesCFG();
36 AU.addRequired<LiveIntervalsWrapperPass>();
37 AU.addPreserved<LiveIntervalsWrapperPass>();
38 AU.addPreserved<SlotIndexesWrapperPass>();
39 AU.addPreserved<LiveDebugVariablesWrapperLegacy>();
40 AU.addPreserved<LiveStacksWrapperLegacy>();
42 }
43
44 StringRef getPassName() const override { return RISCV_DEAD_REG_DEF_NAME; }
45};
46} // end anonymous namespace
47
48char RISCVDeadRegisterDefinitions::ID = 0;
49INITIALIZE_PASS(RISCVDeadRegisterDefinitions, DEBUG_TYPE,
50 RISCV_DEAD_REG_DEF_NAME, false, false)
51
53 return new RISCVDeadRegisterDefinitions();
54}
55
56bool RISCVDeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {
57 if (skipFunction(MF.getFunction()))
58 return false;
59
60 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
61 LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
62 LLVM_DEBUG(dbgs() << "***** RISCVDeadRegisterDefinitions *****\n");
63
64 bool MadeChange = false;
65 for (MachineBasicBlock &MBB : MF) {
66 for (MachineInstr &MI : MBB) {
67 // We only handle non-computational instructions since some NOP encodings
68 // are reserved for HINT instructions.
69 const MCInstrDesc &Desc = MI.getDesc();
70 if (!Desc.mayLoad() && !Desc.mayStore() &&
71 !Desc.hasUnmodeledSideEffects() &&
72 MI.getOpcode() != RISCV::PseudoVSETVLI &&
73 MI.getOpcode() != RISCV::PseudoVSETIVLI)
74 continue;
75 for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
76 MachineOperand &MO = MI.getOperand(I);
77 if (!MO.isReg() || !MO.isDef() || MO.isEarlyClobber())
78 continue;
79 // Be careful not to change the register if it's a tied operand.
80 if (MI.isRegTiedToUseOperand(I)) {
81 LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
82 continue;
83 }
84 Register Reg = MO.getReg();
85 if (!Reg.isVirtual() || !MO.isDead())
86 continue;
87 LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
88 MI.print(dbgs()));
89 Register X0Reg;
90 const TargetRegisterClass *RC = TII->getRegClass(Desc, I);
91 if (RC && RC->contains(RISCV::X0)) {
92 X0Reg = RISCV::X0;
93 } else if (RC && RC->contains(RISCV::X0_W)) {
94 X0Reg = RISCV::X0_W;
95 } else if (RC && RC->contains(RISCV::X0_H)) {
96 X0Reg = RISCV::X0_H;
97 } else if (RC && RC->contains(RISCV::X0_Pair)) {
98 X0Reg = RISCV::X0_Pair;
99 } else {
100 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
101 continue;
102 }
103 assert(LIS.hasInterval(Reg));
104 LIS.removeInterval(Reg);
105 MO.setReg(X0Reg);
106 LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ";
107 MI.print(dbgs()));
108 ++NumDeadDefsReplaced;
109 MadeChange = true;
110 }
111 }
112 }
113
114 return MadeChange;
115}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define RISCV_DEAD_REG_DEF_NAME
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:119
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasInterval(Register Reg) const
void removeInterval(Register Reg)
Interval removal.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
virtual const TargetInstrInfo * getInstrInfo() const
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.
Op::Description Desc
FunctionPass * createRISCVDeadRegisterDefinitionsPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58