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 | 1525260 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/OwningPtr.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
| 42 | #include "llvm/CodeGen/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; |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 50 | class LiveRegMatrix; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 51 | class Spiller; |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 52 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 53 | /// RegAllocBase provides the register allocation driver and interface that can |
| 54 | /// be extended to add interesting heuristics. |
| 55 | /// |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 56 | /// Register allocators must override the selectOrSplit() method to implement |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 57 | /// live range splitting. They must also override enqueue/dequeue to provide an |
| 58 | /// assignment order. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 59 | class RegAllocBase { |
Jakob Stoklund Olesen | 9384111 | 2012-01-11 23:19:08 +0000 | [diff] [blame] | 60 | protected: |
| 61 | const TargetRegisterInfo *TRI; |
| 62 | MachineRegisterInfo *MRI; |
| 63 | VirtRegMap *VRM; |
| 64 | LiveIntervals *LIS; |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 65 | LiveRegMatrix *Matrix; |
Jakob Stoklund Olesen | 9384111 | 2012-01-11 23:19:08 +0000 | [diff] [blame] | 66 | RegisterClassInfo RegClassInfo; |
| 67 | |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 68 | RegAllocBase(): TRI(0), MRI(0), VRM(0), LIS(0), Matrix(0) {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 69 | |
Andrew Trick | f433106 | 2010-10-22 23:33:19 +0000 | [diff] [blame] | 70 | virtual ~RegAllocBase() {} |
| 71 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 72 | // A RegAlloc pass should call this before allocatePhysRegs. |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 73 | void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat); |
Jakob Stoklund Olesen | bdda37d | 2011-05-10 17:37:41 +0000 | [diff] [blame] | 74 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 75 | // The top-level driver. The output is a VirtRegMap that us updated with |
| 76 | // physical register assignments. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 77 | void allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 78 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 79 | // Get a temporary reference to a Spiller instance. |
| 80 | virtual Spiller &spiller() = 0; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 81 | |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 82 | /// enqueue - Add VirtReg to the priority queue of unassigned registers. |
| 83 | virtual void enqueue(LiveInterval *LI) = 0; |
| 84 | |
| 85 | /// dequeue - Return the next unassigned register, or NULL. |
| 86 | virtual LiveInterval *dequeue() = 0; |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame] | 87 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 88 | // A RegAlloc pass should override this to provide the allocation heuristics. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 89 | // Each call must guarantee forward progess by returning an available PhysReg |
| 90 | // 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] | 91 | // converge quickly toward fully spilled live ranges. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 92 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 93 | SmallVectorImpl<LiveInterval*> &splitLVRs) = 0; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 94 | |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 95 | // Use this group name for NamedRegionTimer. |
| 96 | static const char *TimerGroupName; |
| 97 | |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 98 | public: |
| 99 | /// VerifyEnabled - True when -verify-regalloc is given. |
| 100 | static bool VerifyEnabled; |
| 101 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 102 | private: |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 103 | void seedLiveRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 106 | } // end namespace llvm |
| 107 | |
| 108 | #endif // !defined(LLVM_CODEGEN_REGALLOCBASE) |