Added LLIMCJITMemoryManager to the lli. This manager will be used for MCJIT instead...
[oota-llvm.git] / lib / CodeGen / RegisterPressure.h
1 //===-- RegisterPressure.h - Dynamic Register Pressure -*- 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 RegisterPressure class which can be used to track
11 // MachineInstr level register pressure.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_REGISTERPRESSURE_H
16 #define LLVM_CODEGEN_REGISTERPRESSURE_H
17
18 #include "llvm/CodeGen/SlotIndexes.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/ADT/SparseSet.h"
21
22 namespace llvm {
23
24 class LiveIntervals;
25 class RegisterClassInfo;
26 class MachineInstr;
27
28 /// Base class for register pressure results.
29 struct RegisterPressure {
30   /// Map of max reg pressure indexed by pressure set ID, not class ID.
31   std::vector<unsigned> MaxSetPressure;
32
33   /// List of live in registers.
34   SmallVector<unsigned,8> LiveInRegs;
35   SmallVector<unsigned,8> LiveOutRegs;
36
37   /// Increase register pressure for each pressure set impacted by this register
38   /// class. Normally called by RegPressureTracker, but may be called manually
39   /// to account for live through (global liveness).
40   void increase(const TargetRegisterClass *RC, const TargetRegisterInfo *TRI);
41
42   /// Decrease register pressure for each pressure set impacted by this register
43   /// class. This is only useful to account for spilling or rematerialization.
44   void decrease(const TargetRegisterClass *RC, const TargetRegisterInfo *TRI);
45 };
46
47 /// RegisterPressure computed within a region of instructions delimited by
48 /// TopIdx and BottomIdx.  During pressure computation, the maximum pressure per
49 /// register pressure set is increased. Once pressure within a region is fully
50 /// computed, the live-in and live-out sets are recorded.
51 ///
52 /// This is preferable to RegionPressure when LiveIntervals are available,
53 /// because delimiting regions by SlotIndex is more robust and convenient than
54 /// holding block iterators. The block contents can change without invalidating
55 /// the pressure result.
56 struct IntervalPressure : RegisterPressure {
57   /// Record the boundary of the region being tracked.
58   SlotIndex TopIdx;
59   SlotIndex BottomIdx;
60
61   void reset();
62
63   void openTop(SlotIndex NextTop);
64
65   void openBottom(SlotIndex PrevBottom);
66 };
67
68 /// RegisterPressure computed within a region of instructions delimited by
69 /// TopPos and BottomPos. This is a less precise version of IntervalPressure for
70 /// use when LiveIntervals are unavailable.
71 struct RegionPressure : RegisterPressure {
72   /// Record the boundary of the region being tracked.
73   MachineBasicBlock::const_iterator TopPos;
74   MachineBasicBlock::const_iterator BottomPos;
75
76   void reset();
77
78   void openTop(MachineBasicBlock::const_iterator PrevTop);
79
80   void openBottom(MachineBasicBlock::const_iterator PrevBottom);
81 };
82
83 /// Store the results of a change in pressure.
84 ///
85 /// ExcessUnits is the value of the largest difference in register units beyond
86 /// the target's pressure limits across the affected pressure sets, where
87 /// largest is defined as the absolute value of the difference. Negative
88 /// ExcessUnits indicates a reduction in pressure that had already exceeded the
89 /// target's limits.
90 ///
91 /// MaxUnitIncrease is the largest increase in register units required across
92 /// the scheduled region across the affected pressure sets, regardless of the
93 /// target's pressure limits.
94 ///
95 /// If ExcessUnits == 0, then ExcessSetID is invalid.
96 /// If MaxUnitIncrease == 0, then MaxSetID is invalid.
97 struct RegPressureDelta {
98   int ExcessUnits;
99   unsigned ExcessSetID;
100   int MaxUnitIncrease;
101   unsigned MaxSetID;
102
103   RegPressureDelta():
104     ExcessUnits(0), ExcessSetID(~0U), MaxUnitIncrease(0), MaxSetID(~0U) {}
105 };
106
107 /// Track the current register pressure at some position in the instruction
108 /// stream, and remember the high water mark within the region traversed. This
109 /// does not automatically consider live-through ranges. The client may
110 /// independently adjust for global liveness.
111 ///
112 /// Each RegPressureTracker only works within a MachineBasicBlock. Pressure can
113 /// be tracked across a larger region by storing a RegisterPressure result at
114 /// each block boundary and explicitly adjusting pressure to account for block
115 /// live-in and live-out register sets.
116 ///
117 /// RegPressureTracker holds a reference to a RegisterPressure result that it
118 /// computes incrementally. During downward tracking, P.BottomIdx or P.BottomPos
119 /// is invalid until it reaches the end of the block or closeRegion() is
120 /// explicitly called. Similarly, P.TopIdx is invalid during upward
121 /// tracking. Changing direction has the side effect of closing region, and
122 /// traversing past TopIdx or BottomIdx reopens it.
123 class RegPressureTracker {
124   const MachineFunction     *MF;
125   const TargetRegisterInfo  *TRI;
126   const RegisterClassInfo   *RCI;
127   const MachineRegisterInfo *MRI;
128   const LiveIntervals       *LIS;
129
130   /// We currently only allow pressure tracking within a block.
131   const MachineBasicBlock *MBB;
132
133   /// Track the max pressure within the region traversed so far.
134   RegisterPressure &P;
135
136   /// Run in two modes dependending on whether constructed with IntervalPressure
137   /// or RegisterPressure. If requireIntervals is false, LIS are ignored.
138   bool RequireIntervals;
139
140   /// Register pressure corresponds to liveness before this instruction
141   /// iterator. It may point to the end of the block rather than an instruction.
142   MachineBasicBlock::const_iterator CurrPos;
143
144   /// Pressure map indexed by pressure set ID, not class ID.
145   std::vector<unsigned> CurrSetPressure;
146
147   /// List of live registers.
148   SparseSet<unsigned> LivePhysRegs;
149   SparseSet<unsigned, VirtReg2IndexFunctor> LiveVirtRegs;
150
151 public:
152   RegPressureTracker(IntervalPressure &rp) :
153     MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(true) {}
154
155   RegPressureTracker(RegionPressure &rp) :
156     MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(false) {}
157
158   void init(const MachineFunction *mf, const RegisterClassInfo *rci,
159             const LiveIntervals *lis, const MachineBasicBlock *mbb,
160             MachineBasicBlock::const_iterator pos);
161
162   /// Force liveness of registers. Particularly useful to initialize the
163   /// livein/out state of the tracker before the first call to advance/recede.
164   void addLiveRegs(ArrayRef<unsigned> Regs);
165
166   /// Get the MI position corresponding to this register pressure.
167   MachineBasicBlock::const_iterator getPos() const { return CurrPos; }
168
169   // Reset the MI position corresponding to the register pressure. This allows
170   // schedulers to move instructions above the RegPressureTracker's
171   // CurrPos. Since the pressure is computed before CurrPos, the iterator
172   // position changes while pressure does not.
173   void setPos(MachineBasicBlock::const_iterator Pos) { CurrPos = Pos; }
174
175   /// Recede across the previous instruction.
176   bool recede();
177
178   /// Advance across the current instruction.
179   bool advance();
180
181   /// Finalize the region boundaries and recored live ins and live outs.
182   void closeRegion();
183
184   /// Get the resulting register pressure over the traversed region.
185   /// This result is complete if either advance() or recede() has returned true,
186   /// or if closeRegion() was explicitly invoked.
187   RegisterPressure &getPressure() { return P; }
188
189   void discoverPhysLiveIn(unsigned Reg);
190   void discoverPhysLiveOut(unsigned Reg);
191
192   void discoverVirtLiveIn(unsigned Reg);
193   void discoverVirtLiveOut(unsigned Reg);
194
195   bool isTopClosed() const;
196   bool isBottomClosed() const;
197
198   void closeTop();
199   void closeBottom();
200
201   /// Consider the pressure increase caused by traversing this instruction
202   /// bottom-up. Find the pressure set with the most change beyond its pressure
203   /// limit based on the tracker's current pressure, and record the number of
204   /// excess register units of that pressure set introduced by this instruction.
205   void getMaxUpwardPressureDelta(const MachineInstr *MI,
206                                  RegPressureDelta &Delta);
207
208   /// Consider the pressure increase caused by traversing this instruction
209   /// top-down. Find the pressure set with the most change beyond its pressure
210   /// limit based on the tracker's current pressure, and record the number of
211   /// excess register units of that pressure set introduced by this instruction.
212   void getMaxDownwardPressureDelta(const MachineInstr *MI,
213                                    RegPressureDelta &Delta);
214
215   /// Find the pressure set with the most change beyond its pressure limit after
216   /// traversing this instruction either upward or downward depending on the
217   /// closed end of the current region.
218   void getMaxPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta) {
219     if (isTopClosed())
220       return getMaxDownwardPressureDelta(MI, Delta);
221     assert(isBottomClosed() && "Uninitialized pressure tracker");
222     return getMaxUpwardPressureDelta(MI, Delta);
223   }
224
225 protected:
226   void increasePhysRegPressure(ArrayRef<unsigned> Regs);
227   void decreasePhysRegPressure(ArrayRef<unsigned> Regs);
228
229   void increaseVirtRegPressure(ArrayRef<unsigned> Regs);
230   void decreaseVirtRegPressure(ArrayRef<unsigned> Regs);
231 };
232 } // end namespace llvm
233
234 #endif