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 |
Cameron Zwarich | 7fb95d4 | 2010-12-29 04:42:39 +0000 | [diff] [blame] | 33 | // quality trade-off without relying on a particular theoretical solver. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 42 | #include "RegisterClassInfo.h" |
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 | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 52 | /// RegAllocBase provides the register allocation driver and interface that can |
| 53 | /// be extended to add interesting heuristics. |
| 54 | /// |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 55 | /// Register allocators must override the selectOrSplit() method to implement |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 56 | /// live range splitting. They must also override enqueue/dequeue to provide an |
| 57 | /// assignment order. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 58 | class RegAllocBase { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 59 | LiveIntervalUnion::Allocator UnionAllocator; |
Jakob Stoklund Olesen | 2926733 | 2011-03-16 22:56:11 +0000 | [diff] [blame] | 60 | |
| 61 | // Cache tag for PhysReg2LiveUnion entries. Increment whenever virtual |
| 62 | // registers may have changed. |
| 63 | unsigned UserTag; |
| 64 | |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame^] | 65 | LiveIntervalUnion::Array PhysReg2LiveUnion; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 67 | // Current queries, one per physreg. They must be reinitialized each time we |
| 68 | // query on a new live virtual register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 69 | OwningArrayPtr<LiveIntervalUnion::Query> Queries; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 70 | |
Jakob Stoklund Olesen | 9384111 | 2012-01-11 23:19:08 +0000 | [diff] [blame] | 71 | protected: |
| 72 | const TargetRegisterInfo *TRI; |
| 73 | MachineRegisterInfo *MRI; |
| 74 | VirtRegMap *VRM; |
| 75 | LiveIntervals *LIS; |
| 76 | RegisterClassInfo RegClassInfo; |
| 77 | |
Jakob Stoklund Olesen | 2926733 | 2011-03-16 22:56:11 +0000 | [diff] [blame] | 78 | RegAllocBase(): UserTag(0), TRI(0), MRI(0), VRM(0), LIS(0) {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 79 | |
Andrew Trick | f433106 | 2010-10-22 23:33:19 +0000 | [diff] [blame] | 80 | virtual ~RegAllocBase() {} |
| 81 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 82 | // A RegAlloc pass should call this before allocatePhysRegs. |
Jakob Stoklund Olesen | 4680dec | 2010-12-10 23:49:00 +0000 | [diff] [blame] | 83 | void init(VirtRegMap &vrm, LiveIntervals &lis); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 84 | |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 85 | // Get an initialized query to check interferences between lvr and preg. Note |
| 86 | // 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] | 87 | // before querying a new live virtual register. This ties Queries and |
| 88 | // PhysReg2LiveUnion together. |
| 89 | LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | 2926733 | 2011-03-16 22:56:11 +0000 | [diff] [blame] | 90 | Queries[PhysReg].init(UserTag, &VirtReg, &PhysReg2LiveUnion[PhysReg]); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 91 | return Queries[PhysReg]; |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 92 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 93 | |
Jakob Stoklund Olesen | 9384111 | 2012-01-11 23:19:08 +0000 | [diff] [blame] | 94 | // Get direct access to the underlying LiveIntervalUnion for PhysReg. |
| 95 | LiveIntervalUnion &getLiveUnion(unsigned PhysReg) { |
| 96 | return PhysReg2LiveUnion[PhysReg]; |
| 97 | } |
| 98 | |
Jakob Stoklund Olesen | bdda37d | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 99 | // Invalidate all cached information about virtual registers - live ranges may |
| 100 | // have changed. |
| 101 | void invalidateVirtRegs() { ++UserTag; } |
| 102 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 103 | // The top-level driver. The output is a VirtRegMap that us updated with |
| 104 | // physical register assignments. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 105 | void allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 106 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 107 | // Get a temporary reference to a Spiller instance. |
| 108 | virtual Spiller &spiller() = 0; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 109 | |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 110 | /// enqueue - Add VirtReg to the priority queue of unassigned registers. |
| 111 | virtual void enqueue(LiveInterval *LI) = 0; |
| 112 | |
| 113 | /// dequeue - Return the next unassigned register, or NULL. |
| 114 | virtual LiveInterval *dequeue() = 0; |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame] | 115 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 116 | // A RegAlloc pass should override this to provide the allocation heuristics. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 117 | // Each call must guarantee forward progess by returning an available PhysReg |
| 118 | // 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] | 119 | // converge quickly toward fully spilled live ranges. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 120 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 121 | SmallVectorImpl<LiveInterval*> &splitLVRs) = 0; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 122 | |
| 123 | // A RegAlloc pass should call this when PassManager releases its memory. |
| 124 | virtual void releaseMemory(); |
| 125 | |
| 126 | // Helper for checking interference between a live virtual register and a |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 127 | // physical register, including all its register aliases. If an interference |
| 128 | // exists, return the interfering register, which may be preg or an alias. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 129 | unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 130 | |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 131 | /// assign - Assign VirtReg to PhysReg. |
| 132 | /// This should not be called from selectOrSplit for the current register. |
| 133 | void assign(LiveInterval &VirtReg, unsigned PhysReg); |
| 134 | |
| 135 | /// unassign - Undo a previous assignment of VirtReg to PhysReg. |
| 136 | /// This can be invoked from selectOrSplit, but be careful to guarantee that |
| 137 | /// allocation is making progress. |
| 138 | void unassign(LiveInterval &VirtReg, unsigned PhysReg); |
| 139 | |
Jakob Stoklund Olesen | 1b19dc1 | 2010-12-08 01:06:06 +0000 | [diff] [blame] | 140 | /// addMBBLiveIns - Add physreg liveins to basic blocks. |
| 141 | void addMBBLiveIns(MachineFunction *); |
| 142 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 143 | #ifndef NDEBUG |
| 144 | // Verify each LiveIntervalUnion. |
| 145 | void verify(); |
| 146 | #endif |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 147 | |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 148 | // Use this group name for NamedRegionTimer. |
| 149 | static const char *TimerGroupName; |
| 150 | |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 151 | public: |
| 152 | /// VerifyEnabled - True when -verify-regalloc is given. |
| 153 | static bool VerifyEnabled; |
| 154 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 155 | private: |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 156 | void seedLiveRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 159 | } // end namespace llvm |
| 160 | |
| 161 | #endif // !defined(LLVM_CODEGEN_REGALLOCBASE) |