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"
16#include "llvm/CodeGen/LiveDebugVariables.h"
17#include "llvm/CodeGen/LiveIntervals.h"
18#include "llvm/CodeGen/LiveStacks.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20
21using namespace llvm;
22#define DEBUG_TYPE "loongarch-dead-defs"
23#define LoongArch_DEAD_REG_DEF_NAME "LoongArch Dead register definitions"
24
25STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
26
27namespace {
28class LoongArchDeadRegisterDefinitions : public MachineFunctionPass {
29public:
30 static char ID;
31
32 LoongArchDeadRegisterDefinitions() : 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>();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 StringRef getPassName() const override { return LoongArch_DEAD_REG_DEF_NAME; }
45};
46} // end anonymous namespace
47
48char LoongArchDeadRegisterDefinitions::ID = 0;
49INITIALIZE_PASS(LoongArchDeadRegisterDefinitions, DEBUG_TYPE,
50 LoongArch_DEAD_REG_DEF_NAME, false, false)
51
52FunctionPass *llvm::createLoongArchDeadRegisterDefinitionsPass() {
53 return new LoongArchDeadRegisterDefinitions();
54}
55
56bool LoongArchDeadRegisterDefinitions::runOnMachineFunction(
57 MachineFunction &MF) {
58 if (skipFunction(F: MF.getFunction()))
59 return false;
60
61 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
62 LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
63 LLVM_DEBUG(dbgs() << "***** LoongArchDeadRegisterDefinitions *****\n");
64
65 bool MadeChange = false;
66 for (MachineBasicBlock &MBB : MF) {
67 for (MachineInstr &MI : MBB) {
68 // We only handle non-computational instructions.
69 const MCInstrDesc &Desc = MI.getDesc();
70 if (!Desc.mayLoad() && !Desc.mayStore() &&
71 !Desc.hasUnmodeledSideEffects())
72 continue;
73 for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
74 MachineOperand &MO = MI.getOperand(i: I);
75 if (!MO.isReg() || !MO.isDef() || MO.isEarlyClobber())
76 continue;
77 // Be careful not to change the register if it's a tied operand.
78 if (MI.isRegTiedToUseOperand(DefOpIdx: I)) {
79 LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
80 continue;
81 }
82 Register Reg = MO.getReg();
83 if (!Reg.isVirtual() || !MO.isDead())
84 continue;
85 LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
86 MI.print(dbgs()));
87 const TargetRegisterClass *RC = TII->getRegClass(MCID: Desc, OpNum: I);
88 if (!(RC && RC->contains(Reg: LoongArch::R0))) {
89 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
90 continue;
91 }
92 assert(LIS.hasInterval(Reg));
93 LIS.removeInterval(Reg);
94 MO.setReg(LoongArch::R0);
95 LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ";
96 MI.print(dbgs()));
97 ++NumDeadDefsReplaced;
98 MadeChange = true;
99 }
100 }
101 }
102
103 return MadeChange;
104}
105