|
| 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 "RISCVInstrInfo.h" |
| 15 | +#include "RISCVSubtarget.h" |
| 16 | +#include "llvm/ADT/Statistic.h" |
| 17 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 | +#include "llvm/CodeGen/MachineRegisterInfo.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | +#define DEBUG_TYPE "riscv-dead-defs" |
| 22 | +#define RISCV_DEAD_REG_DEF_NAME "RISC-V Dead register definitions" |
| 23 | + |
| 24 | +STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced"); |
| 25 | + |
| 26 | +namespace { |
| 27 | +class RISCVDeadRegisterDefinitions : public MachineFunctionPass { |
| 28 | +public: |
| 29 | + static char ID; |
| 30 | + |
| 31 | + RISCVDeadRegisterDefinitions() : MachineFunctionPass(ID) { |
| 32 | + initializeRISCVDeadRegisterDefinitionsPass( |
| 33 | + *PassRegistry::getPassRegistry()); |
| 34 | + } |
| 35 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 36 | + void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 37 | + AU.setPreservesCFG(); |
| 38 | + MachineFunctionPass::getAnalysisUsage(AU); |
| 39 | + } |
| 40 | + |
| 41 | + StringRef getPassName() const override { return RISCV_DEAD_REG_DEF_NAME; } |
| 42 | +}; |
| 43 | +} // end anonymous namespace |
| 44 | + |
| 45 | +char RISCVDeadRegisterDefinitions::ID = 0; |
| 46 | +INITIALIZE_PASS(RISCVDeadRegisterDefinitions, DEBUG_TYPE, |
| 47 | + RISCV_DEAD_REG_DEF_NAME, false, false) |
| 48 | + |
| 49 | +FunctionPass *llvm::createRISCVDeadRegisterDefinitionsPass() { |
| 50 | + return new RISCVDeadRegisterDefinitions(); |
| 51 | +} |
| 52 | + |
| 53 | +bool RISCVDeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) { |
| 54 | + if (skipFunction(MF.getFunction())) |
| 55 | + return false; |
| 56 | + |
| 57 | + const MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 58 | + const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 59 | + const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 60 | + LLVM_DEBUG(dbgs() << "***** RISCVDeadRegisterDefinitions *****\n"); |
| 61 | + |
| 62 | + bool MadeChange = false; |
| 63 | + for (MachineBasicBlock &MBB : MF) { |
| 64 | + for (MachineInstr &MI : MBB) { |
| 65 | + // We only handle non-computational instructions since some NOP encodings |
| 66 | + // are reserved for HINT instructions. |
| 67 | + const MCInstrDesc &Desc = MI.getDesc(); |
| 68 | + if (!Desc.mayLoad() && !Desc.mayStore() && |
| 69 | + !Desc.hasUnmodeledSideEffects()) |
| 70 | + continue; |
| 71 | + // For PseudoVSETVLIX0, Rd = X0 has special meaning. |
| 72 | + if (MI.getOpcode() == RISCV::PseudoVSETVLIX0) |
| 73 | + continue; |
| 74 | + for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) { |
| 75 | + MachineOperand &MO = MI.getOperand(I); |
| 76 | + if (!MO.isReg() || !MO.isDef() || MO.isEarlyClobber()) |
| 77 | + continue; |
| 78 | + // Be careful not to change the register if it's a tied operand. |
| 79 | + if (MI.isRegTiedToUseOperand(I)) { |
| 80 | + LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n"); |
| 81 | + continue; |
| 82 | + } |
| 83 | + // We should not have any relevant physreg defs that are replacable by |
| 84 | + // zero before register allocation. So we just check for dead vreg defs. |
| 85 | + Register Reg = MO.getReg(); |
| 86 | + if (!Reg.isVirtual() || (!MO.isDead() && !MRI->use_nodbg_empty(Reg))) |
| 87 | + continue; |
| 88 | + LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n "; |
| 89 | + MI.print(dbgs())); |
| 90 | + const TargetRegisterClass *RC = TII->getRegClass(Desc, I, TRI, MF); |
| 91 | + if (!(RC && RC->contains(RISCV::X0))) { |
| 92 | + LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n"); |
| 93 | + continue; |
| 94 | + } |
| 95 | + MO.setReg(RISCV::X0); |
| 96 | + MO.setIsDead(); |
| 97 | + LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n "; |
| 98 | + MI.print(dbgs())); |
| 99 | + ++NumDeadDefsReplaced; |
| 100 | + MadeChange = true; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return MadeChange; |
| 106 | +} |
0 commit comments