RegisterCoalescer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===- RegisterCoalescer.h - Register Coalescing Interface ------*- C++ -*-===//
  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 file contains the abstract interface for register coalescers,
  10. // allowing them to interact with and query register allocators.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
  14. #define LLVM_LIB_CODEGEN_REGISTERCOALESCER_H
  15. #include "llvm/CodeGen/Register.h"
  16. namespace llvm {
  17. class MachineInstr;
  18. class TargetRegisterClass;
  19. class TargetRegisterInfo;
  20. /// A helper class for register coalescers. When deciding if
  21. /// two registers can be coalesced, CoalescerPair can determine if a copy
  22. /// instruction would become an identity copy after coalescing.
  23. class CoalescerPair {
  24. const TargetRegisterInfo &TRI;
  25. /// The register that will be left after coalescing. It can be a
  26. /// virtual or physical register.
  27. Register DstReg;
  28. /// The virtual register that will be coalesced into dstReg.
  29. Register SrcReg;
  30. /// The sub-register index of the old DstReg in the new coalesced register.
  31. unsigned DstIdx = 0;
  32. /// The sub-register index of the old SrcReg in the new coalesced register.
  33. unsigned SrcIdx = 0;
  34. /// True when the original copy was a partial subregister copy.
  35. bool Partial = false;
  36. /// True when both regs are virtual and newRC is constrained.
  37. bool CrossClass = false;
  38. /// True when DstReg and SrcReg are reversed from the original
  39. /// copy instruction.
  40. bool Flipped = false;
  41. /// The register class of the coalesced register, or NULL if DstReg
  42. /// is a physreg. This register class may be a super-register of both
  43. /// SrcReg and DstReg.
  44. const TargetRegisterClass *NewRC = nullptr;
  45. public:
  46. CoalescerPair(const TargetRegisterInfo &tri) : TRI(tri) {}
  47. /// Create a CoalescerPair representing a virtreg-to-physreg copy.
  48. /// No need to call setRegisters().
  49. CoalescerPair(Register VirtReg, MCRegister PhysReg,
  50. const TargetRegisterInfo &tri)
  51. : TRI(tri), DstReg(PhysReg), SrcReg(VirtReg) {}
  52. /// Set registers to match the copy instruction MI. Return
  53. /// false if MI is not a coalescable copy instruction.
  54. bool setRegisters(const MachineInstr*);
  55. /// Swap SrcReg and DstReg. Return false if swapping is impossible
  56. /// because DstReg is a physical register, or SubIdx is set.
  57. bool flip();
  58. /// Return true if MI is a copy instruction that will become
  59. /// an identity copy after coalescing.
  60. bool isCoalescable(const MachineInstr*) const;
  61. /// Return true if DstReg is a physical register.
  62. bool isPhys() const { return !NewRC; }
  63. /// Return true if the original copy instruction did not copy
  64. /// the full register, but was a subreg operation.
  65. bool isPartial() const { return Partial; }
  66. /// Return true if DstReg is virtual and NewRC is a smaller
  67. /// register class than DstReg's.
  68. bool isCrossClass() const { return CrossClass; }
  69. /// Return true when getSrcReg is the register being defined by
  70. /// the original copy instruction.
  71. bool isFlipped() const { return Flipped; }
  72. /// Return the register (virtual or physical) that will remain
  73. /// after coalescing.
  74. Register getDstReg() const { return DstReg; }
  75. /// Return the virtual register that will be coalesced away.
  76. Register getSrcReg() const { return SrcReg; }
  77. /// Return the subregister index that DstReg will be coalesced into, or 0.
  78. unsigned getDstIdx() const { return DstIdx; }
  79. /// Return the subregister index that SrcReg will be coalesced into, or 0.
  80. unsigned getSrcIdx() const { return SrcIdx; }
  81. /// Return the register class of the coalesced register.
  82. const TargetRegisterClass *getNewRC() const { return NewRC; }
  83. };
  84. } // end namespace llvm
  85. #endif // LLVM_LIB_CODEGEN_REGISTERCOALESCER_H