RegisterClassInfo.h 5.1 KB

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