LLVM 24.0.0git
LoongArchDeadRegisterDefinitions.cpp
Go to the documentation of this file.
1//=== LoongArchDeadRegisterDefinitions.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 r0 for instrs whose return values are unused.
10//
11//===---------------------------------------------------------------------===//
12
13#include "LoongArch.h"
14#include "LoongArchSubtarget.h"
15#include "llvm/ADT/Statistic.h"
22#include "llvm/IR/Analysis.h"
23
24using namespace llvm;
25#define DEBUG_TYPE "loongarch-dead-register-definitions"
26#define LoongArch_DEAD_REG_DEF_NAME "LoongArch Dead register definitions"
27
28STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
29
30namespace {
31class LoongArchDeadRegisterDefinitionsLegacy : public MachineFunctionPass {
32public:
33 static char ID;
34
35 LoongArchDeadRegisterDefinitionsLegacy() : MachineFunctionPass(ID) {}
36 bool runOnMachineFunction(MachineFunction &MF) override;
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesCFG();
39 AU.addRequired<LiveIntervalsWrapperPass>();
40 AU.addPreserved<LiveIntervalsWrapperPass>();
41 AU.addPreserved<SlotIndexesWrapperPass>();
42 AU.addPreserved<LiveDebugVariablesWrapperLegacy>();
43 AU.addPreserved<LiveStacksWrapperLegacy>();
45 }
46
47 StringRef getPassName() const override { return LoongArch_DEAD_REG_DEF_NAME; }
48};
49} // end anonymous namespace
50
51char LoongArchDeadRegisterDefinitionsLegacy::ID = 0;
52INITIALIZE_PASS(LoongArchDeadRegisterDefinitionsLegacy, DEBUG_TYPE,
53 LoongArch_DEAD_REG_DEF_NAME, false, false)
54
56 return new LoongArchDeadRegisterDefinitionsLegacy();
57}
58
60 LiveIntervals &LIS) {
62 LLVM_DEBUG(dbgs() << "***** LoongArchDeadRegisterDefinitions *****\n");
63
64 bool MadeChange = false;
65 for (MachineBasicBlock &MBB : MF) {
66 for (MachineInstr &MI : MBB) {
67 // We only handle non-computational instructions.
68 const MCInstrDesc &Desc = MI.getDesc();
69 if (!Desc.mayLoad() && !Desc.mayStore() &&
70 !Desc.hasUnmodeledSideEffects())
71 continue;
72 for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
73 MachineOperand &MO = MI.getOperand(I);
74 if (!MO.isReg() || !MO.isDef() || MO.isEarlyClobber())
75 continue;
76 // Be careful not to change the register if it's a tied operand.
77 if (MI.isRegTiedToUseOperand(I)) {
78 LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
79 continue;
80 }
81 Register Reg = MO.getReg();
82 if (!Reg.isVirtual() || !MO.isDead())
83 continue;
84 LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
85 MI.print(dbgs()));
86 const TargetRegisterClass *RC = TII->getRegClass(Desc, I);
87 if (!(RC && RC->contains(LoongArch::R0))) {
88 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
89 continue;
90 }
93 MO.setReg(LoongArch::R0);
94 LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ";
95 MI.print(dbgs()));
96 ++NumDeadDefsReplaced;
97 MadeChange = true;
98 }
99 }
100 }
101
102 return MadeChange;
103}
104
105bool LoongArchDeadRegisterDefinitionsLegacy::runOnMachineFunction(
106 MachineFunction &MF) {
107 if (skipFunction(MF.getFunction()))
108 return false;
110 MF, getAnalysis<LiveIntervalsWrapperPass>().getLIS());
111}
112
115 bool Changed =
117 if (!Changed)
118 return PreservedAnalyses::all();
121 .preserve<LiveIntervalsAnalysis>()
122 .preserve<SlotIndexesAnalysis>()
123 .preserve<LiveDebugVariablesAnalysis>()
124 .preserve<LiveStacksAnalysis>();
125}
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 LoongArch_DEAD_REG_DEF_NAME
static bool runDeadRegisterDefinitions(MachineFunction &MF, LiveIntervals &LIS)
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:170
#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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
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
Wrapper class representing virtual and physical registers.
Definition Register.h:20
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
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.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createLoongArchDeadRegisterDefinitionsLegacyPass()
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58