regpressure: physreg livein/out fix
[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 /// An element of pressure difference that identifies the pressure set and
84 /// amount of increase or decrease in units of pressure.
85 struct PressureElement {
86   unsigned PSetID;
87   int UnitIncrease;
88
89   PressureElement(): PSetID(~0U), UnitIncrease(0) {}
90   PressureElement(unsigned id, int inc): PSetID(id), UnitIncrease(inc) {}
91
92   bool isValid() const { return PSetID != ~0U; }
93 };
94
95 /// Store the effects of a change in pressure on things that MI scheduler cares
96 /// about.
97 ///
98 /// Excess records the value of the largest difference in register units beyond
99 /// the target's pressure limits across the affected pressure sets, where
100 /// largest is defined as the absolute value of the difference. Negative
101 /// ExcessUnits indicates a reduction in pressure that had already exceeded the
102 /// target's limits.
103 ///
104 /// CriticalMax records the largest increase in the tracker's max pressure that
105 /// exceeds the critical limit for some pressure set determined by the client.
106 ///
107 /// CurrentMax records the largest increase in the tracker's max pressure that
108 /// exceeds the current limit for some pressure set determined by the client.
109 struct RegPressureDelta {
110   PressureElement Excess;
111   PressureElement CriticalMax;
112   PressureElement CurrentMax;
113
114   RegPressureDelta() {}
115 };
116
117 /// Track the current register pressure at some position in the instruction
118 /// stream, and remember the high water mark within the region traversed. This
119 /// does not automatically consider live-through ranges. The client may
120 /// independently adjust for global liveness.
121 ///
122 /// Each RegPressureTracker only works within a MachineBasicBlock. Pressure can
123 /// be tracked across a larger region by storing a RegisterPressure result at
124 /// each block boundary and explicitly adjusting pressure to account for block
125 /// live-in and live-out register sets.
126 ///
127 /// RegPressureTracker holds a reference to a RegisterPressure result that it
128 /// computes incrementally. During downward tracking, P.BottomIdx or P.BottomPos
129 /// is invalid until it reaches the end of the block or closeRegion() is
130 /// explicitly called. Similarly, P.TopIdx is invalid during upward
131 /// tracking. Changing direction has the side effect of closing region, and
132 /// traversing past TopIdx or BottomIdx reopens it.
133 class RegPressureTracker {
134   const MachineFunction     *MF;
135   const TargetRegisterInfo  *TRI;
136   const RegisterClassInfo   *RCI;
137   const MachineRegisterInfo *MRI;
138   const LiveIntervals       *LIS;
139
140   /// We currently only allow pressure tracking within a block.
141   const MachineBasicBlock *MBB;
142
143   /// Track the max pressure within the region traversed so far.
144   RegisterPressure &P;
145
146   /// Run in two modes dependending on whether constructed with IntervalPressure
147   /// or RegisterPressure. If requireIntervals is false, LIS are ignored.
148   bool RequireIntervals;
149
150   /// Register pressure corresponds to liveness before this instruction
151   /// iterator. It may point to the end of the block rather than an instruction.
152   MachineBasicBlock::const_iterator CurrPos;
153
154   /// Pressure map indexed by pressure set ID, not class ID.
155   std::vector<unsigned> CurrSetPressure;
156
157   /// List of live registers.
158   SparseSet<unsigned> LivePhysRegs;
159   SparseSet<unsigned, VirtReg2IndexFunctor> LiveVirtRegs;
160
161 public:
162   RegPressureTracker(IntervalPressure &rp) :
163     MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(true) {}
164
165   RegPressureTracker(RegionPressure &rp) :
166     MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(false) {}
167
168   void init(const MachineFunction *mf, const RegisterClassInfo *rci,
169             const LiveIntervals *lis, const MachineBasicBlock *mbb,
170             MachineBasicBlock::const_iterator pos);
171
172   /// Force liveness of registers. Particularly useful to initialize the
173   /// livein/out state of the tracker before the first call to advance/recede.
174   void addLiveRegs(ArrayRef<unsigned> Regs);
175
176   /// Get the MI position corresponding to this register pressure.
177   MachineBasicBlock::const_iterator getPos() const { return CurrPos; }
178
179   // Reset the MI position corresponding to the register pressure. This allows
180   // schedulers to move instructions above the RegPressureTracker's
181   // CurrPos. Since the pressure is computed before CurrPos, the iterator
182   // position changes while pressure does not.
183   void setPos(MachineBasicBlock::const_iterator Pos) { CurrPos = Pos; }
184
185   /// Recede across the previous instruction.
186   bool recede();
187
188   /// Advance across the current instruction.
189   bool advance();
190
191   /// Finalize the region boundaries and recored live ins and live outs.
192   void closeRegion();
193
194   /// Get the resulting register pressure over the traversed region.
195   /// This result is complete if either advance() or recede() has returned true,
196   /// or if closeRegion() was explicitly invoked.
197   RegisterPressure &getPressure() { return P; }
198
199   void discoverPhysLiveIn(unsigned Reg);
200   void discoverPhysLiveOut(unsigned Reg);
201
202   void discoverVirtLiveIn(unsigned Reg);
203   void discoverVirtLiveOut(unsigned Reg);
204
205   bool isTopClosed() const;
206   bool isBottomClosed() const;
207
208   void closeTop();
209   void closeBottom();
210
211   /// Consider the pressure increase caused by traversing this instruction
212   /// bottom-up. Find the pressure set with the most change beyond its pressure
213   /// limit based on the tracker's current pressure, and record the number of
214   /// excess register units of that pressure set introduced by this instruction.
215   void getMaxUpwardPressureDelta(const MachineInstr *MI,
216                                  RegPressureDelta &Delta,
217                                  ArrayRef<PressureElement> CriticalPSets,
218                                  ArrayRef<unsigned> MaxPressureLimit);
219
220   /// Consider the pressure increase caused by traversing this instruction
221   /// top-down. Find the pressure set with the most change beyond its pressure
222   /// limit based on the tracker's current pressure, and record the number of
223   /// excess register units of that pressure set introduced by this instruction.
224   void getMaxDownwardPressureDelta(const MachineInstr *MI,
225                                    RegPressureDelta &Delta,
226                                    ArrayRef<PressureElement> CriticalPSets,
227                                    ArrayRef<unsigned> MaxPressureLimit);
228
229   /// Find the pressure set with the most change beyond its pressure limit after
230   /// traversing this instruction either upward or downward depending on the
231   /// closed end of the current region.
232   void getMaxPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
233                            ArrayRef<PressureElement> CriticalPSets,
234                            ArrayRef<unsigned> MaxPressureLimit) {
235     if (isTopClosed())
236       return getMaxDownwardPressureDelta(MI, Delta, CriticalPSets,
237                                          MaxPressureLimit);
238
239     assert(isBottomClosed() && "Uninitialized pressure tracker");
240     return getMaxUpwardPressureDelta(MI, Delta, CriticalPSets,
241                                      MaxPressureLimit);
242   }
243
244 protected:
245   void increasePhysRegPressure(ArrayRef<unsigned> Regs);
246   void decreasePhysRegPressure(ArrayRef<unsigned> Regs);
247
248   void increaseVirtRegPressure(ArrayRef<unsigned> Regs);
249   void decreaseVirtRegPressure(ArrayRef<unsigned> Regs);
250 };
251 } // end namespace llvm
252
253 #endif