LLVM 24.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"
23#include "llvm/IR/Analysis.h"
24
25using namespace llvm;
26#define DEBUG_TYPE "riscv-dead-defs"
27#define RISCV_DEAD_REG_DEF_NAME "RISC-V Dead register definitions"
28
29STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
30
31namespace {
32class RISCVDeadRegisterDefinitionsLegacy : public MachineFunctionPass {
33public:
34 static char ID;
35
36 RISCVDeadRegisterDefinitionsLegacy() : MachineFunctionPass(ID) {}
37 bool runOnMachineFunction(MachineFunction &MF) override;
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
40 AU.addRequired<LiveIntervalsWrapperPass>();
41 AU.addPreserved<LiveIntervalsWrapperPass>();
42 AU.addPreserved<SlotIndexesWrapperPass>();
43 AU.addPreserved<LiveDebugVariablesWrapperLegacy>();
44 AU.addPreserved<LiveStacksWrapperLegacy>();
46 }
47
48 StringRef getPassName() const override { return RISCV_DEAD_REG_DEF_NAME; }
49};
50} // end anonymous namespace
51
52char RISCVDeadRegisterDefinitionsLegacy::ID = 0;
53INITIALIZE_PASS(RISCVDeadRegisterDefinitionsLegacy, DEBUG_TYPE,
54 RISCV_DEAD_REG_DEF_NAME, false, false)
55
57 return new RISCVDeadRegisterDefinitionsLegacy();
58}
59
61 function_ref<LiveIntervals &()> GetLIS) {
63 LiveIntervals &LIS = GetLIS();
64 LLVM_DEBUG(dbgs() << "***** RISCVDeadRegisterDefinitions *****\n");
65
66 bool MadeChange = false;
67 for (MachineBasicBlock &MBB : MF) {
68 for (MachineInstr &MI : MBB) {
69 // We only handle non-computational instructions since some NOP encodings
70 // are reserved for HINT instructions.
71 const MCInstrDesc &Desc = MI.getDesc();
72 if (!Desc.mayLoad() && !Desc.mayStore() &&
73 !Desc.hasUnmodeledSideEffects() &&
74 MI.getOpcode() != RISCV::PseudoVSETVLI &&
75 MI.getOpcode() != RISCV::PseudoVSETIVLI)
76 continue;
77 for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
78 MachineOperand &MO = MI.getOperand(I);
79 if (!MO.isReg() || !MO.isDef() || MO.isEarlyClobber())
80 continue;
81 // Be careful not to change the register if it's a tied operand.
82 if (MI.isRegTiedToUseOperand(I)) {
83 LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
84 continue;
85 }
86 Register Reg = MO.getReg();
87 if (!Reg.isVirtual() || !MO.isDead())
88 continue;
89 LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
90 MI.print(dbgs()));
91 Register X0Reg;
92 const TargetRegisterClass *RC = TII->getRegClass(Desc, I);
93 if (RC && RC->contains(RISCV::X0)) {
94 X0Reg = RISCV::X0;
95 } else if (RC && RC->contains(RISCV::X0_W)) {
96 X0Reg = RISCV::X0_W;
97 } else if (RC && RC->contains(RISCV::X0_H)) {
98 X0Reg = RISCV::X0_H;
99 } else if (RC && RC->contains(RISCV::X0_Pair)) {
100 X0Reg = RISCV::X0_Pair;
101 } else {
102 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
103 continue;
104 }
105 assert(LIS.hasInterval(Reg));
106 LIS.removeInterval(Reg);
107 MO.setReg(X0Reg);
108 LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ";
109 MI.print(dbgs()));
110 ++NumDeadDefsReplaced;
111 MadeChange = true;
112 }
113 }
114 }
115
116 return MadeChange;
117}
118
119bool RISCVDeadRegisterDefinitionsLegacy::runOnMachineFunction(
120 MachineFunction &MF) {
121 if (skipFunction(MF.getFunction()))
122 return false;
123 return runDeadRegisterDefinitions(MF, [&]() -> LiveIntervals & {
124 return getAnalysis<LiveIntervalsWrapperPass>().getLIS();
125 });
126}
127
128PreservedAnalyses
131 bool Changed = runDeadRegisterDefinitions(MF, [&]() -> LiveIntervals & {
132 return MFAM.getResult<LiveIntervalsAnalysis>(MF);
133 });
134 if (!Changed)
135 return PreservedAnalyses::all();
138 .preserve<LiveIntervalsAnalysis>()
139 .preserve<SlotIndexesAnalysis>()
140 .preserve<LiveDebugVariablesAnalysis>()
141 .preserve<LiveStacksAnalysis>();
142}
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
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define RISCV_DEAD_REG_DEF_NAME
static bool runDeadRegisterDefinitions(MachineFunction &MF, function_ref< LiveIntervals &()> GetLIS)
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
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasInterval(Register Reg) const
void removeInterval(Register Reg)
Interval removal.
Describe properties that are true of each instruction in the target description file.
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.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Op::Description Desc
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createRISCVDeadRegisterDefinitionsLegacyPass()
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