RISCVStripWSuffix.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-------------- RISCVStripWSuffix.cpp - -w Suffix Removal -------------===//
  2. //
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===---------------------------------------------------------------------===//
  9. //
  10. // This pass removes the -w suffix from each addiw and slliw instructions
  11. // whenever all users are dependent only on the lower word of the result of the
  12. // instruction. We do this only for addiw and slliw because the -w forms are
  13. // less compressible.
  14. //
  15. //===---------------------------------------------------------------------===//
  16. #include "RISCV.h"
  17. #include "RISCVMachineFunctionInfo.h"
  18. using namespace llvm;
  19. static cl::opt<bool> DisableStripWSuffix("riscv-disable-strip-w-suffix",
  20. cl::desc("Disable strip W suffix"),
  21. cl::init(false), cl::Hidden);
  22. namespace {
  23. class RISCVStripWSuffix : public MachineFunctionPass {
  24. public:
  25. static char ID;
  26. RISCVStripWSuffix() : MachineFunctionPass(ID) {
  27. initializeRISCVStripWSuffixPass(*PassRegistry::getPassRegistry());
  28. }
  29. bool runOnMachineFunction(MachineFunction &MF) override;
  30. void getAnalysisUsage(AnalysisUsage &AU) const override {
  31. AU.setPreservesCFG();
  32. MachineFunctionPass::getAnalysisUsage(AU);
  33. }
  34. StringRef getPassName() const override { return "RISCV Strip W Suffix"; }
  35. };
  36. } // end anonymous namespace
  37. char RISCVStripWSuffix::ID = 0;
  38. INITIALIZE_PASS(RISCVStripWSuffix, "riscv-strip-w-suffix",
  39. "RISCV Strip W Suffix", false, false)
  40. FunctionPass *llvm::createRISCVStripWSuffixPass() {
  41. return new RISCVStripWSuffix();
  42. }
  43. bool RISCVStripWSuffix::runOnMachineFunction(MachineFunction &MF) {
  44. if (skipFunction(MF.getFunction()) || DisableStripWSuffix)
  45. return false;
  46. MachineRegisterInfo &MRI = MF.getRegInfo();
  47. const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
  48. const RISCVInstrInfo &TII = *ST.getInstrInfo();
  49. if (!ST.is64Bit())
  50. return false;
  51. bool MadeChange = false;
  52. for (MachineBasicBlock &MBB : MF) {
  53. for (auto I = MBB.begin(), IE = MBB.end(); I != IE; ++I) {
  54. MachineInstr &MI = *I;
  55. switch (MI.getOpcode()) {
  56. case RISCV::ADDW:
  57. case RISCV::SLLIW:
  58. if (TII.hasAllWUsers(MI, MRI)) {
  59. unsigned Opc =
  60. MI.getOpcode() == RISCV::ADDW ? RISCV::ADD : RISCV::SLLI;
  61. MI.setDesc(TII.get(Opc));
  62. MadeChange = true;
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. return MadeChange;
  69. }