Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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: |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 14 | // |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 18 | // |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 a particular theoretical solver. |
| 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #ifndef LLVM_CODEGEN_REGALLOCBASE |
| 38 | #define LLVM_CODEGEN_REGALLOCBASE |
| 39 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/OwningPtr.h" |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 41 | #include "LiveIntervalUnion.h" |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame^] | 42 | #include <queue> |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 43 | |
| 44 | namespace llvm { |
| 45 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 46 | template<typename T> class SmallVectorImpl; |
| 47 | class TargetRegisterInfo; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 48 | class VirtRegMap; |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 49 | class LiveIntervals; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 50 | class Spiller; |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 51 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 52 | // Forward declare a priority queue of live virtual registers. If an |
| 53 | // implementation needs to prioritize by anything other than spill weight, then |
| 54 | // this will become an abstract base class with virtual calls to push/get. |
| 55 | class LiveVirtRegQueue; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 56 | |
| 57 | /// RegAllocBase provides the register allocation driver and interface that can |
| 58 | /// be extended to add interesting heuristics. |
| 59 | /// |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 60 | /// Register allocators must override the selectOrSplit() method to implement |
| 61 | /// live range splitting. LessSpillWeightPriority is provided as a standard |
| 62 | /// comparator, but we may add an interface to override it if necessary. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 63 | class RegAllocBase { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 64 | LiveIntervalUnion::Allocator UnionAllocator; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 65 | protected: |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 | // Array of LiveIntervalUnions indexed by physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 67 | class LiveUnionArray { |
| 68 | unsigned NumRegs; |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 69 | LiveIntervalUnion *Array; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 70 | public: |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 71 | LiveUnionArray(): NumRegs(0), Array(0) {} |
| 72 | ~LiveUnionArray() { clear(); } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 73 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 74 | unsigned numRegs() const { return NumRegs; } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 75 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 76 | void init(LiveIntervalUnion::Allocator &, unsigned NRegs); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 77 | |
| 78 | void clear(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 79 | |
| 80 | LiveIntervalUnion& operator[](unsigned PhysReg) { |
| 81 | assert(PhysReg < NumRegs && "physReg out of bounds"); |
| 82 | return Array[PhysReg]; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 83 | } |
| 84 | }; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 85 | |
| 86 | const TargetRegisterInfo *TRI; |
| 87 | VirtRegMap *VRM; |
| 88 | LiveIntervals *LIS; |
| 89 | LiveUnionArray PhysReg2LiveUnion; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 90 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 91 | // Current queries, one per physreg. They must be reinitialized each time we |
| 92 | // query on a new live virtual register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 93 | OwningArrayPtr<LiveIntervalUnion::Query> Queries; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 94 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 95 | RegAllocBase(): TRI(0), VRM(0), LIS(0) {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 96 | |
Andrew Trick | f433106 | 2010-10-22 23:33:19 +0000 | [diff] [blame] | 97 | virtual ~RegAllocBase() {} |
| 98 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 99 | // A RegAlloc pass should call this before allocatePhysRegs. |
| 100 | void init(const TargetRegisterInfo &tri, VirtRegMap &vrm, LiveIntervals &lis); |
| 101 | |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 102 | // Get an initialized query to check interferences between lvr and preg. Note |
| 103 | // that Query::init must be called at least once for each physical register |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 104 | // before querying a new live virtual register. This ties Queries and |
| 105 | // PhysReg2LiveUnion together. |
| 106 | LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) { |
| 107 | Queries[PhysReg].init(&VirtReg, &PhysReg2LiveUnion[PhysReg]); |
| 108 | return Queries[PhysReg]; |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 109 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 110 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 111 | // The top-level driver. The output is a VirtRegMap that us updated with |
| 112 | // physical register assignments. |
| 113 | // |
| 114 | // If an implementation wants to override the LiveInterval comparator, we |
| 115 | // should modify this interface to allow passing in an instance derived from |
| 116 | // LiveVirtRegQueue. |
| 117 | void allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 118 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 119 | // Get a temporary reference to a Spiller instance. |
| 120 | virtual Spiller &spiller() = 0; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 121 | |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame^] | 122 | // getPriority - Calculate the allocation priority for VirtReg. |
| 123 | // Virtual registers with higher priorities are allocated first. |
| 124 | virtual float getPriority(LiveInterval *LI) = 0; |
| 125 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 126 | // A RegAlloc pass should override this to provide the allocation heuristics. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 127 | // Each call must guarantee forward progess by returning an available PhysReg |
| 128 | // or new set of split live virtual registers. It is up to the splitter to |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 129 | // converge quickly toward fully spilled live ranges. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 130 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 131 | SmallVectorImpl<LiveInterval*> &splitLVRs) = 0; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 132 | |
| 133 | // A RegAlloc pass should call this when PassManager releases its memory. |
| 134 | virtual void releaseMemory(); |
| 135 | |
| 136 | // Helper for checking interference between a live virtual register and a |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 137 | // physical register, including all its register aliases. If an interference |
| 138 | // exists, return the interfering register, which may be preg or an alias. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 139 | unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 140 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 141 | // Helper for spilling all live virtual registers currently unified under preg |
| 142 | // that interfere with the most recently queried lvr. Return true if spilling |
| 143 | // was successful, and append any new spilled/split intervals to splitLVRs. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 144 | bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
| 145 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 146 | |
Jakob Stoklund Olesen | 1b19dc1 | 2010-12-08 01:06:06 +0000 | [diff] [blame] | 147 | /// addMBBLiveIns - Add physreg liveins to basic blocks. |
| 148 | void addMBBLiveIns(MachineFunction *); |
| 149 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 150 | #ifndef NDEBUG |
| 151 | // Verify each LiveIntervalUnion. |
| 152 | void verify(); |
| 153 | #endif |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 154 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 155 | private: |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame^] | 156 | void seedLiveVirtRegs(std::priority_queue<std::pair<float, unsigned> >&); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 157 | |
| 158 | void spillReg(LiveInterval &VirtReg, unsigned PhysReg, |
| 159 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 162 | } // end namespace llvm |
| 163 | |
| 164 | #endif // !defined(LLVM_CODEGEN_REGALLOCBASE) |