RegAllocBase.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/RegisterClassInfo.h"
  39. namespace llvm {
  40. class LiveInterval;
  41. class LiveIntervals;
  42. class LiveRegMatrix;
  43. class MachineInstr;
  44. class MachineRegisterInfo;
  45. template<typename T> class SmallVectorImpl;
  46. class Spiller;
  47. class TargetRegisterInfo;
  48. class VirtRegMap;
  49. /// RegAllocBase provides the register allocation driver and interface that can
  50. /// be extended to add interesting heuristics.
  51. ///
  52. /// Register allocators must override the selectOrSplit() method to implement
  53. /// live range splitting. They must also override enqueue/dequeue to provide an
  54. /// assignment order.
  55. class RegAllocBase {
  56. virtual void anchor();
  57. protected:
  58. const TargetRegisterInfo *TRI = nullptr;
  59. MachineRegisterInfo *MRI = nullptr;
  60. VirtRegMap *VRM = nullptr;
  61. LiveIntervals *LIS = nullptr;
  62. LiveRegMatrix *Matrix = nullptr;
  63. RegisterClassInfo RegClassInfo;
  64. /// Inst which is a def of an original reg and whose defs are already all
  65. /// dead after remat is saved in DeadRemats. The deletion of such inst is
  66. /// postponed till all the allocations are done, so its remat expr is
  67. /// always available for the remat of all the siblings of the original reg.
  68. SmallPtrSet<MachineInstr *, 32> DeadRemats;
  69. RegAllocBase() = default;
  70. virtual ~RegAllocBase() = default;
  71. // A RegAlloc pass should call this before allocatePhysRegs.
  72. void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
  73. // The top-level driver. The output is a VirtRegMap that us updated with
  74. // physical register assignments.
  75. void allocatePhysRegs();
  76. // Include spiller post optimization and removing dead defs left because of
  77. // rematerialization.
  78. virtual void postOptimization();
  79. // Get a temporary reference to a Spiller instance.
  80. virtual Spiller &spiller() = 0;
  81. /// enqueue - Add VirtReg to the priority queue of unassigned registers.
  82. virtual void enqueue(LiveInterval *LI) = 0;
  83. /// dequeue - Return the next unassigned register, or NULL.
  84. virtual LiveInterval *dequeue() = 0;
  85. // A RegAlloc pass should override this to provide the allocation heuristics.
  86. // Each call must guarantee forward progess by returning an available PhysReg
  87. // or new set of split live virtual registers. It is up to the splitter to
  88. // converge quickly toward fully spilled live ranges.
  89. virtual MCRegister selectOrSplit(LiveInterval &VirtReg,
  90. SmallVectorImpl<Register> &splitLVRs) = 0;
  91. // Use this group name for NamedRegionTimer.
  92. static const char TimerGroupName[];
  93. static const char TimerGroupDescription[];
  94. /// Method called when the allocator is about to remove a LiveInterval.
  95. virtual void aboutToRemoveInterval(LiveInterval &LI) {}
  96. public:
  97. /// VerifyEnabled - True when -verify-regalloc is given.
  98. static bool VerifyEnabled;
  99. private:
  100. void seedLiveRegs();
  101. };
  102. } // end namespace llvm
  103. #endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H