72620f71ad4ee8e69cb7ebe40c6744a2924a28d4
[oota-llvm.git] / lib / CodeGen / RegAllocBase.h
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
37 #ifndef LLVM_CODEGEN_REGALLOCBASE
38 #define LLVM_CODEGEN_REGALLOCBASE
39
40 #include "llvm/ADT/OwningPtr.h"
41 #include "LiveIntervalUnion.h"
42 #include "RegisterClassInfo.h"
43
44 namespace llvm {
45
46 template<typename T> class SmallVectorImpl;
47 class TargetRegisterInfo;
48 class VirtRegMap;
49 class LiveIntervals;
50 class Spiller;
51
52 /// RegAllocBase provides the register allocation driver and interface that can
53 /// be extended to add interesting heuristics.
54 ///
55 /// Register allocators must override the selectOrSplit() method to implement
56 /// live range splitting. They must also override enqueue/dequeue to provide an
57 /// assignment order.
58 class RegAllocBase {
59   LiveIntervalUnion::Allocator UnionAllocator;
60
61   // Cache tag for PhysReg2LiveUnion entries. Increment whenever virtual
62   // registers may have changed.
63   unsigned UserTag;
64
65 protected:
66   // Array of LiveIntervalUnions indexed by physical register.
67   class LiveUnionArray {
68     unsigned NumRegs;
69     LiveIntervalUnion *Array;
70   public:
71     LiveUnionArray(): NumRegs(0), Array(0) {}
72     ~LiveUnionArray() { clear(); }
73
74     unsigned numRegs() const { return NumRegs; }
75
76     void init(LiveIntervalUnion::Allocator &, unsigned NRegs);
77
78     void clear();
79
80     LiveIntervalUnion& operator[](unsigned PhysReg) {
81       assert(PhysReg <  NumRegs && "physReg out of bounds");
82       return Array[PhysReg];
83     }
84   };
85
86   const TargetRegisterInfo *TRI;
87   MachineRegisterInfo *MRI;
88   VirtRegMap *VRM;
89   LiveIntervals *LIS;
90   RegisterClassInfo RegClassInfo;
91   LiveUnionArray PhysReg2LiveUnion;
92
93   // Current queries, one per physreg. They must be reinitialized each time we
94   // query on a new live virtual register.
95   OwningArrayPtr<LiveIntervalUnion::Query> Queries;
96
97   RegAllocBase(): UserTag(0), TRI(0), MRI(0), VRM(0), LIS(0) {}
98
99   virtual ~RegAllocBase() {}
100
101   // A RegAlloc pass should call this before allocatePhysRegs.
102   void init(VirtRegMap &vrm, LiveIntervals &lis);
103
104   // Get an initialized query to check interferences between lvr and preg.  Note
105   // that Query::init must be called at least once for each physical register
106   // before querying a new live virtual register. This ties Queries and
107   // PhysReg2LiveUnion together.
108   LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
109     Queries[PhysReg].init(UserTag, &VirtReg, &PhysReg2LiveUnion[PhysReg]);
110     return Queries[PhysReg];
111   }
112
113   // Invalidate all cached information about virtual registers - live ranges may
114   // have changed.
115   void invalidateVirtRegs() { ++UserTag; }
116
117   // The top-level driver. The output is a VirtRegMap that us updated with
118   // physical register assignments.
119   void allocatePhysRegs();
120
121   // Get a temporary reference to a Spiller instance.
122   virtual Spiller &spiller() = 0;
123
124   /// enqueue - Add VirtReg to the priority queue of unassigned registers.
125   virtual void enqueue(LiveInterval *LI) = 0;
126
127   /// dequeue - Return the next unassigned register, or NULL.
128   virtual LiveInterval *dequeue() = 0;
129
130   // A RegAlloc pass should override this to provide the allocation heuristics.
131   // Each call must guarantee forward progess by returning an available PhysReg
132   // or new set of split live virtual registers. It is up to the splitter to
133   // converge quickly toward fully spilled live ranges.
134   virtual unsigned selectOrSplit(LiveInterval &VirtReg,
135                                  SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
136
137   // A RegAlloc pass should call this when PassManager releases its memory.
138   virtual void releaseMemory();
139
140   // Helper for checking interference between a live virtual register and a
141   // physical register, including all its register aliases. If an interference
142   // exists, return the interfering register, which may be preg or an alias.
143   unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg);
144
145   /// assign - Assign VirtReg to PhysReg.
146   /// This should not be called from selectOrSplit for the current register.
147   void assign(LiveInterval &VirtReg, unsigned PhysReg);
148
149   /// unassign - Undo a previous assignment of VirtReg to PhysReg.
150   /// This can be invoked from selectOrSplit, but be careful to guarantee that
151   /// allocation is making progress.
152   void unassign(LiveInterval &VirtReg, unsigned PhysReg);
153
154   // Helper for spilling all live virtual registers currently unified under preg
155   // that interfere with the most recently queried lvr.  Return true if spilling
156   // was successful, and append any new spilled/split intervals to splitLVRs.
157   bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
158                           SmallVectorImpl<LiveInterval*> &SplitVRegs);
159
160   /// addMBBLiveIns - Add physreg liveins to basic blocks.
161   void addMBBLiveIns(MachineFunction *);
162
163 #ifndef NDEBUG
164   // Verify each LiveIntervalUnion.
165   void verify();
166 #endif
167
168   // Use this group name for NamedRegionTimer.
169   static const char *TimerGroupName;
170
171 public:
172   /// VerifyEnabled - True when -verify-regalloc is given.
173   static bool VerifyEnabled;
174
175 private:
176   void seedLiveRegs();
177
178   void spillReg(LiveInterval &VirtReg, unsigned PhysReg,
179                 SmallVectorImpl<LiveInterval*> &SplitVRegs);
180 };
181
182 } // end namespace llvm
183
184 #endif // !defined(LLVM_CODEGEN_REGALLOCBASE)