RegisterClassInfo.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //===-- RegisterClassInfo.h - Dynamic Register Class Info -*- C++ -*-------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the RegisterClassInfo class which provides dynamic
  11. // information about target register classes. Callee saved and reserved
  12. // registers depends on calling conventions and other dynamic information, so
  13. // some things cannot be determined statically.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CODEGEN_REGISTERCLASSINFO_H
  17. #define LLVM_CODEGEN_REGISTERCLASSINFO_H
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/ADT/BitVector.h"
  20. #include "llvm/Target/TargetRegisterInfo.h"
  21. namespace llvm {
  22. class RegisterClassInfo {
  23. struct RCInfo {
  24. unsigned Tag;
  25. unsigned NumRegs;
  26. bool ProperSubClass;
  27. uint8_t MinCost;
  28. uint16_t LastCostChange;
  29. std::unique_ptr<MCPhysReg[]> Order;
  30. RCInfo()
  31. : Tag(0), NumRegs(0), ProperSubClass(false), MinCost(0),
  32. LastCostChange(0) {}
  33. operator ArrayRef<MCPhysReg>() const {
  34. return makeArrayRef(Order.get(), NumRegs);
  35. }
  36. };
  37. // Brief cached information for each register class.
  38. std::unique_ptr<RCInfo[]> RegClass;
  39. // Tag changes whenever cached information needs to be recomputed. An RCInfo
  40. // entry is valid when its tag matches.
  41. unsigned Tag;
  42. const MachineFunction *MF;
  43. const TargetRegisterInfo *TRI;
  44. // Callee saved registers of last MF. Assumed to be valid until the next
  45. // runOnFunction() call.
  46. const MCPhysReg *CalleeSaved;
  47. // Map register number to CalleeSaved index + 1;
  48. SmallVector<uint8_t, 4> CSRNum;
  49. // Reserved registers in the current MF.
  50. BitVector Reserved;
  51. std::unique_ptr<unsigned[]> PSetLimits;
  52. // Compute all information about RC.
  53. void compute(const TargetRegisterClass *RC) const;
  54. // Return an up-to-date RCInfo for RC.
  55. const RCInfo &get(const TargetRegisterClass *RC) const {
  56. const RCInfo &RCI = RegClass[RC->getID()];
  57. if (Tag != RCI.Tag)
  58. compute(RC);
  59. return RCI;
  60. }
  61. public:
  62. RegisterClassInfo();
  63. /// runOnFunction - Prepare to answer questions about MF. This must be called
  64. /// before any other methods are used.
  65. void runOnMachineFunction(const MachineFunction &MF);
  66. /// getNumAllocatableRegs - Returns the number of actually allocatable
  67. /// registers in RC in the current function.
  68. unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
  69. return get(RC).NumRegs;
  70. }
  71. /// getOrder - Returns the preferred allocation order for RC. The order
  72. /// contains no reserved registers, and registers that alias callee saved
  73. /// registers come last.
  74. ArrayRef<MCPhysReg> getOrder(const TargetRegisterClass *RC) const {
  75. return get(RC);
  76. }
  77. /// isProperSubClass - Returns true if RC has a legal super-class with more
  78. /// allocatable registers.
  79. ///
  80. /// Register classes like GR32_NOSP are not proper sub-classes because %esp
  81. /// is not allocatable. Similarly, tGPR is not a proper sub-class in Thumb
  82. /// mode because the GPR super-class is not legal.
  83. bool isProperSubClass(const TargetRegisterClass *RC) const {
  84. return get(RC).ProperSubClass;
  85. }
  86. /// getLastCalleeSavedAlias - Returns the last callee saved register that
  87. /// overlaps PhysReg, or 0 if Reg doesn't overlap a CSR.
  88. unsigned getLastCalleeSavedAlias(unsigned PhysReg) const {
  89. assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
  90. if (unsigned N = CSRNum[PhysReg])
  91. return CalleeSaved[N-1];
  92. return 0;
  93. }
  94. /// Get the minimum register cost in RC's allocation order.
  95. /// This is the smallest value returned by TRI->getCostPerUse(Reg) for all
  96. /// the registers in getOrder(RC).
  97. unsigned getMinCost(const TargetRegisterClass *RC) {
  98. return get(RC).MinCost;
  99. }
  100. /// Get the position of the last cost change in getOrder(RC).
  101. ///
  102. /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
  103. /// same cost according to TRI->getCostPerUse().
  104. unsigned getLastCostChange(const TargetRegisterClass *RC) {
  105. return get(RC).LastCostChange;
  106. }
  107. /// Get the register unit limit for the given pressure set index.
  108. ///
  109. /// RegisterClassInfo adjusts this limit for reserved registers.
  110. unsigned getRegPressureSetLimit(unsigned Idx) const {
  111. if (!PSetLimits[Idx])
  112. PSetLimits[Idx] = computePSetLimit(Idx);
  113. return PSetLimits[Idx];
  114. }
  115. protected:
  116. unsigned computePSetLimit(unsigned Idx) const;
  117. };
  118. } // end namespace llvm
  119. #endif