MIRVRegNamerUtils.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/ADT/PostOrderIterator.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/Passes.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <queue>
  25. namespace llvm {
  26. /// NamedVRegCursor - The cursor is an object that keeps track of what the next
  27. /// vreg name should be. It does book keeping to determine when to skip the
  28. /// index value and by how much, or if the next vreg name should be an increment
  29. /// from the previous.
  30. class NamedVRegCursor {
  31. MachineRegisterInfo &MRI;
  32. /// virtualVRegNumber - Book keeping of the last vreg position.
  33. unsigned virtualVRegNumber;
  34. /// SkipGapSize - Used to calculate a modulo amount to skip by after every
  35. /// sequence of instructions starting from a given side-effecting
  36. /// MachineInstruction for a given MachineBasicBlock. The general idea is that
  37. /// for a given program compiled with two different opt pipelines, there
  38. /// shouldn't be greater than SkipGapSize difference in how many vregs are in
  39. /// play between the two and for every def-use graph of vregs we rename we
  40. /// will round up to the next SkipGapSize'th number so that we have a high
  41. /// change of landing on the same name for two given matching side-effects
  42. /// for the two compilation outcomes.
  43. const unsigned SkipGapSize;
  44. /// RenamedInOtherBB - VRegs that we already renamed: ie breadcrumbs.
  45. std::vector<Register> RenamedInOtherBB;
  46. public:
  47. NamedVRegCursor() = delete;
  48. /// 1000 for the SkipGapSize was a good heuristic at the time of the writing
  49. /// of the MIRCanonicalizerPass. Adjust as needed.
  50. NamedVRegCursor(MachineRegisterInfo &MRI, unsigned SkipGapSize = 1000)
  51. : MRI(MRI), virtualVRegNumber(0), SkipGapSize(SkipGapSize) {}
  52. /// SkipGapSize - Skips modulo a gap value of indices. Indices are used to
  53. /// produce the next vreg name.
  54. void skipVRegs();
  55. unsigned getVirtualVReg() const { return virtualVRegNumber; }
  56. /// incrementVirtualVReg - This increments an index value that us used to
  57. /// create a new vreg name. This is not a Register.
  58. unsigned incrementVirtualVReg(unsigned incr = 1) {
  59. virtualVRegNumber += incr;
  60. return virtualVRegNumber;
  61. }
  62. /// createVirtualRegister - Given an existing vreg, create a named vreg to
  63. /// take its place.
  64. unsigned createVirtualRegister(unsigned VReg);
  65. /// renameVRegs - For a given MachineBasicBlock, scan for side-effecting
  66. /// instructions, walk the def-use from each side-effecting root (in sorted
  67. /// root order) and rename the encountered vregs in the def-use graph in a
  68. /// canonical ordering. This method maintains book keeping for which vregs
  69. /// were already renamed in RenamedInOtherBB.
  70. // @return changed
  71. bool renameVRegs(MachineBasicBlock *MBB);
  72. };
  73. } // namespace llvm
  74. #endif