RegAllocBase.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- RegAllocBase.h - basic regalloc interface and driver -----*- 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 defines the RegAllocBase class, which is the skeleton of a basic
  10. // register allocation algorithm and interface for extending it. It provides the
  11. // building blocks on which to construct other experimental allocators and test
  12. // the validity of two principles:
  13. //
  14. // - If virtual and physical register liveness is modeled using intervals, then
  15. // on-the-fly interference checking is cheap. Furthermore, interferences can be
  16. // lazily cached and reused.
  17. //
  18. // - Register allocation complexity, and generated code performance is
  19. // determined by the effectiveness of live range splitting rather than optimal
  20. // coloring.
  21. //
  22. // Following the first principle, interfering checking revolves around the
  23. // LiveIntervalUnion data structure.
  24. //
  25. // To fulfill the second principle, the basic allocator provides a driver for
  26. // incremental splitting. It essentially punts on the problem of register
  27. // coloring, instead driving the assignment of virtual to physical registers by
  28. // the cost of splitting. The basic allocator allows for heuristic reassignment
  29. // of registers, if a more sophisticated allocator chooses to do that.
  30. //
  31. // This framework provides a way to engineer the compile time vs. code
  32. // quality trade-off without relying on a particular theoretical solver.
  33. //
  34. //===----------------------------------------------------------------------===//
  35. #ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H
  36. #define LLVM_LIB_CODEGEN_REGALLOCBASE_H
  37. #include "llvm/ADT/SmallPtrSet.h"
  38. #include "llvm/CodeGen/RegAllocCommon.h"
  39. #include "llvm/CodeGen/RegisterClassInfo.h"
  40. namespace llvm {
  41. class LiveInterval;
  42. class LiveIntervals;
  43. class LiveRegMatrix;
  44. class MachineInstr;
  45. class MachineRegisterInfo;
  46. template<typename T> class SmallVectorImpl;
  47. class Spiller;
  48. class TargetRegisterInfo;
  49. class VirtRegMap;
  50. /// RegAllocBase provides the register allocation driver and interface that can
  51. /// be extended to add interesting heuristics.
  52. ///
  53. /// Register allocators must override the selectOrSplit() method to implement
  54. /// live range splitting. They must also override enqueue/dequeue to provide an
  55. /// assignment order.
  56. class RegAllocBase {
  57. virtual void anchor();
  58. protected:
  59. const TargetRegisterInfo *TRI = nullptr;
  60. MachineRegisterInfo *MRI = nullptr;
  61. VirtRegMap *VRM = nullptr;
  62. LiveIntervals *LIS = nullptr;
  63. LiveRegMatrix *Matrix = nullptr;
  64. RegisterClassInfo RegClassInfo;
  65. const RegClassFilterFunc ShouldAllocateClass;
  66. /// Inst which is a def of an original reg and whose defs are already all
  67. /// dead after remat is saved in DeadRemats. The deletion of such inst is
  68. /// postponed till all the allocations are done, so its remat expr is
  69. /// always available for the remat of all the siblings of the original reg.
  70. SmallPtrSet<MachineInstr *, 32> DeadRemats;
  71. RegAllocBase(const RegClassFilterFunc F = allocateAllRegClasses) :
  72. ShouldAllocateClass(F) {}
  73. virtual ~RegAllocBase() = default;
  74. // A RegAlloc pass should call this before allocatePhysRegs.
  75. void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
  76. // The top-level driver. The output is a VirtRegMap that us updated with
  77. // physical register assignments.
  78. void allocatePhysRegs();
  79. // Include spiller post optimization and removing dead defs left because of
  80. // rematerialization.
  81. virtual void postOptimization();
  82. // Get a temporary reference to a Spiller instance.
  83. virtual Spiller &spiller() = 0;
  84. /// enqueue - Add VirtReg to the priority queue of unassigned registers.
  85. virtual void enqueueImpl(const LiveInterval *LI) = 0;
  86. /// enqueue - Add VirtReg to the priority queue of unassigned registers.
  87. void enqueue(const LiveInterval *LI);
  88. /// dequeue - Return the next unassigned register, or NULL.
  89. virtual const LiveInterval *dequeue() = 0;
  90. // A RegAlloc pass should override this to provide the allocation heuristics.
  91. // Each call must guarantee forward progess by returning an available PhysReg
  92. // or new set of split live virtual registers. It is up to the splitter to
  93. // converge quickly toward fully spilled live ranges.
  94. virtual MCRegister selectOrSplit(const LiveInterval &VirtReg,
  95. SmallVectorImpl<Register> &splitLVRs) = 0;
  96. // Use this group name for NamedRegionTimer.
  97. static const char TimerGroupName[];
  98. static const char TimerGroupDescription[];
  99. /// Method called when the allocator is about to remove a LiveInterval.
  100. virtual void aboutToRemoveInterval(const LiveInterval &LI) {}
  101. public:
  102. /// VerifyEnabled - True when -verify-regalloc is given.
  103. static bool VerifyEnabled;
  104. private:
  105. void seedLiveRegs();
  106. };
  107. } // end namespace llvm
  108. #endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H