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