RegAllocBase.h 4.0 KB

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