MIRVRegNamerUtils.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===------------ MIRVRegNamerUtils.h - MIR VReg Renaming Utilities -------===//
  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. // The purpose of these utilities is to abstract out parts of the MIRCanon pass
  10. // that are responsible for renaming virtual registers with the purpose of
  11. // sharing code with a MIRVRegNamer pass that could be the analog of the
  12. // opt -instnamer pass.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
  16. #define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
  17. #include "llvm/CodeGen/Register.h"
  18. #include <map>
  19. #include <vector>
  20. #include <string>
  21. namespace llvm {
  22. class MachineBasicBlock;
  23. class MachineInstr;
  24. class MachineRegisterInfo;
  25. class StringRef;
  26. /// VRegRenamer - This class is used for renaming vregs in a machine basic
  27. /// block according to semantics of the instruction.
  28. class VRegRenamer {
  29. class NamedVReg {
  30. Register Reg;
  31. std::string Name;
  32. public:
  33. NamedVReg(Register Reg, std::string Name = "") : Reg(Reg), Name(Name) {}
  34. NamedVReg(std::string Name = "") : Reg(~0U), Name(Name) {}
  35. const std::string &getName() const { return Name; }
  36. Register getReg() const { return Reg; }
  37. };
  38. MachineRegisterInfo &MRI;
  39. unsigned CurrentBBNumber = 0;
  40. /// Given an Instruction, construct a hash of the operands
  41. /// of the instructions along with the opcode.
  42. /// When dealing with virtual registers, just hash the opcode of
  43. /// the instruction defining that vreg.
  44. /// Handle immediates, registers (physical and virtual) explicitly,
  45. /// and return a common value for the other cases.
  46. /// Instruction will be named in the following scheme
  47. /// bb<block_no>_hash_<collission_count>.
  48. std::string getInstructionOpcodeHash(MachineInstr &MI);
  49. /// For all the VRegs that are candidates for renaming,
  50. /// return a mapping from old vregs to new vregs with names.
  51. std::map<unsigned, unsigned>
  52. getVRegRenameMap(const std::vector<NamedVReg> &VRegs);
  53. /// Perform replacing of registers based on the <old,new> vreg map.
  54. bool doVRegRenaming(const std::map<unsigned, unsigned> &VRegRenameMap);
  55. /// createVirtualRegister - Given an existing vreg, create a named vreg to
  56. /// take its place. The name is determined by calling
  57. /// getInstructionOpcodeHash.
  58. unsigned createVirtualRegister(unsigned VReg);
  59. /// Create a vreg with name and return it.
  60. unsigned createVirtualRegisterWithLowerName(unsigned VReg, StringRef Name);
  61. /// Linearly traverse the MachineBasicBlock and rename each instruction's
  62. /// vreg definition based on the semantics of the instruction.
  63. /// Names are as follows bb<BBNum>_hash_[0-9]+
  64. bool renameInstsInMBB(MachineBasicBlock *MBB);
  65. public:
  66. VRegRenamer() = delete;
  67. VRegRenamer(MachineRegisterInfo &MRI) : MRI(MRI) {}
  68. /// Same as the above, but sets a BBNum depending on BB traversal that
  69. /// will be used as prefix for the vreg names.
  70. bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) {
  71. CurrentBBNumber = BBNum;
  72. return renameInstsInMBB(MBB);
  73. }
  74. };
  75. } // namespace llvm
  76. #endif