RISCVMacroFusion.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===- RISCVMacroFusion.cpp - RISCV Macro Fusion --------------------------===//
  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. /// \file This file contains the RISCV implementation of the DAG scheduling
  10. /// mutation to pair instructions back to back.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. #include "RISCVMacroFusion.h"
  15. #include "RISCVSubtarget.h"
  16. #include "llvm/CodeGen/MacroFusion.h"
  17. #include "llvm/CodeGen/TargetInstrInfo.h"
  18. using namespace llvm;
  19. // Fuse LUI followed by ADDI or ADDIW.
  20. // rd = imm[31:0] which decomposes to
  21. // lui rd, imm[31:12]
  22. // addi(w) rd, rd, imm[11:0]
  23. static bool isLUIADDI(const MachineInstr *FirstMI,
  24. const MachineInstr &SecondMI) {
  25. if (SecondMI.getOpcode() != RISCV::ADDI &&
  26. SecondMI.getOpcode() != RISCV::ADDIW)
  27. return false;
  28. // Assume the 1st instr to be a wildcard if it is unspecified.
  29. if (!FirstMI)
  30. return true;
  31. if (FirstMI->getOpcode() != RISCV::LUI)
  32. return false;
  33. // The first operand of ADDI might be a frame index.
  34. if (!SecondMI.getOperand(1).isReg())
  35. return false;
  36. Register FirstDest = FirstMI->getOperand(0).getReg();
  37. // Destination of LUI should be the ADDI(W) source register.
  38. if (SecondMI.getOperand(1).getReg() != FirstDest)
  39. return false;
  40. // If the input is virtual make sure this is the only user.
  41. if (FirstDest.isVirtual()) {
  42. auto &MRI = SecondMI.getMF()->getRegInfo();
  43. return MRI.hasOneNonDBGUse(FirstDest);
  44. }
  45. // If the FirstMI destination is non-virtual, it should match the SecondMI
  46. // destination.
  47. return SecondMI.getOperand(0).getReg() == FirstDest;
  48. }
  49. static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
  50. const TargetSubtargetInfo &TSI,
  51. const MachineInstr *FirstMI,
  52. const MachineInstr &SecondMI) {
  53. const RISCVSubtarget &ST = static_cast<const RISCVSubtarget &>(TSI);
  54. if (ST.hasLUIADDIFusion() && isLUIADDI(FirstMI, SecondMI))
  55. return true;
  56. return false;
  57. }
  58. std::unique_ptr<ScheduleDAGMutation> llvm::createRISCVMacroFusionDAGMutation() {
  59. return createMacroFusionDAGMutation(shouldScheduleAdjacent);
  60. }