1 //===-- RegisterPressure.h - Dynamic Register Pressure -*- C++ -*-------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RegisterPressure class which can be used to track
11 // MachineInstr level register pressure.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_REGISTERPRESSURE_H
16 #define LLVM_CODEGEN_REGISTERPRESSURE_H
18 #include "llvm/CodeGen/SlotIndexes.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/ADT/SparseSet.h"
25 class RegisterClassInfo;
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;
33 /// List of live in registers.
34 SmallVector<unsigned,8> LiveInRegs;
35 SmallVector<unsigned,8> LiveOutRegs;
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);
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);
46 void dump(const TargetRegisterInfo *TRI);
49 /// RegisterPressure computed within a region of instructions delimited by
50 /// TopIdx and BottomIdx. During pressure computation, the maximum pressure per
51 /// register pressure set is increased. Once pressure within a region is fully
52 /// computed, the live-in and live-out sets are recorded.
54 /// This is preferable to RegionPressure when LiveIntervals are available,
55 /// because delimiting regions by SlotIndex is more robust and convenient than
56 /// holding block iterators. The block contents can change without invalidating
57 /// the pressure result.
58 struct IntervalPressure : RegisterPressure {
59 /// Record the boundary of the region being tracked.
65 void openTop(SlotIndex NextTop);
67 void openBottom(SlotIndex PrevBottom);
70 /// RegisterPressure computed within a region of instructions delimited by
71 /// TopPos and BottomPos. This is a less precise version of IntervalPressure for
72 /// use when LiveIntervals are unavailable.
73 struct RegionPressure : RegisterPressure {
74 /// Record the boundary of the region being tracked.
75 MachineBasicBlock::const_iterator TopPos;
76 MachineBasicBlock::const_iterator BottomPos;
80 void openTop(MachineBasicBlock::const_iterator PrevTop);
82 void openBottom(MachineBasicBlock::const_iterator PrevBottom);
85 /// An element of pressure difference that identifies the pressure set and
86 /// amount of increase or decrease in units of pressure.
87 struct PressureElement {
91 PressureElement(): PSetID(~0U), UnitIncrease(0) {}
92 PressureElement(unsigned id, int inc): PSetID(id), UnitIncrease(inc) {}
94 bool isValid() const { return PSetID != ~0U; }
97 /// Store the effects of a change in pressure on things that MI scheduler cares
100 /// Excess records the value of the largest difference in register units beyond
101 /// the target's pressure limits across the affected pressure sets, where
102 /// largest is defined as the absolute value of the difference. Negative
103 /// ExcessUnits indicates a reduction in pressure that had already exceeded the
106 /// CriticalMax records the largest increase in the tracker's max pressure that
107 /// exceeds the critical limit for some pressure set determined by the client.
109 /// CurrentMax records the largest increase in the tracker's max pressure that
110 /// exceeds the current limit for some pressure set determined by the client.
111 struct RegPressureDelta {
112 PressureElement Excess;
113 PressureElement CriticalMax;
114 PressureElement CurrentMax;
116 RegPressureDelta() {}
119 /// Track the current register pressure at some position in the instruction
120 /// stream, and remember the high water mark within the region traversed. This
121 /// does not automatically consider live-through ranges. The client may
122 /// independently adjust for global liveness.
124 /// Each RegPressureTracker only works within a MachineBasicBlock. Pressure can
125 /// be tracked across a larger region by storing a RegisterPressure result at
126 /// each block boundary and explicitly adjusting pressure to account for block
127 /// live-in and live-out register sets.
129 /// RegPressureTracker holds a reference to a RegisterPressure result that it
130 /// computes incrementally. During downward tracking, P.BottomIdx or P.BottomPos
131 /// is invalid until it reaches the end of the block or closeRegion() is
132 /// explicitly called. Similarly, P.TopIdx is invalid during upward
133 /// tracking. Changing direction has the side effect of closing region, and
134 /// traversing past TopIdx or BottomIdx reopens it.
135 class RegPressureTracker {
136 const MachineFunction *MF;
137 const TargetRegisterInfo *TRI;
138 const RegisterClassInfo *RCI;
139 const MachineRegisterInfo *MRI;
140 const LiveIntervals *LIS;
142 /// We currently only allow pressure tracking within a block.
143 const MachineBasicBlock *MBB;
145 /// Track the max pressure within the region traversed so far.
148 /// Run in two modes dependending on whether constructed with IntervalPressure
149 /// or RegisterPressure. If requireIntervals is false, LIS are ignored.
150 bool RequireIntervals;
152 /// Register pressure corresponds to liveness before this instruction
153 /// iterator. It may point to the end of the block rather than an instruction.
154 MachineBasicBlock::const_iterator CurrPos;
156 /// Pressure map indexed by pressure set ID, not class ID.
157 std::vector<unsigned> CurrSetPressure;
159 /// List of live registers.
160 SparseSet<unsigned> LivePhysRegs;
161 SparseSet<unsigned, VirtReg2IndexFunctor> LiveVirtRegs;
164 RegPressureTracker(IntervalPressure &rp) :
165 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(true) {}
167 RegPressureTracker(RegionPressure &rp) :
168 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(false) {}
170 void init(const MachineFunction *mf, const RegisterClassInfo *rci,
171 const LiveIntervals *lis, const MachineBasicBlock *mbb,
172 MachineBasicBlock::const_iterator pos);
174 /// Force liveness of registers. Particularly useful to initialize the
175 /// livein/out state of the tracker before the first call to advance/recede.
176 void addLiveRegs(ArrayRef<unsigned> Regs);
178 /// Get the MI position corresponding to this register pressure.
179 MachineBasicBlock::const_iterator getPos() const { return CurrPos; }
181 // Reset the MI position corresponding to the register pressure. This allows
182 // schedulers to move instructions above the RegPressureTracker's
183 // CurrPos. Since the pressure is computed before CurrPos, the iterator
184 // position changes while pressure does not.
185 void setPos(MachineBasicBlock::const_iterator Pos) { CurrPos = Pos; }
187 /// Recede across the previous instruction.
190 /// Advance across the current instruction.
193 /// Finalize the region boundaries and recored live ins and live outs.
196 /// Get the resulting register pressure over the traversed region.
197 /// This result is complete if either advance() or recede() has returned true,
198 /// or if closeRegion() was explicitly invoked.
199 RegisterPressure &getPressure() { return P; }
201 /// Get the register set pressure at the current position, which may be less
202 /// than the pressure across the traversed region.
203 std::vector<unsigned> &getRegSetPressureAtPos() { return CurrSetPressure; }
205 void discoverPhysLiveIn(unsigned Reg);
206 void discoverPhysLiveOut(unsigned Reg);
208 void discoverVirtLiveIn(unsigned Reg);
209 void discoverVirtLiveOut(unsigned Reg);
211 bool isTopClosed() const;
212 bool isBottomClosed() const;
217 /// Consider the pressure increase caused by traversing this instruction
218 /// bottom-up. Find the pressure set with the most change beyond its pressure
219 /// limit based on the tracker's current pressure, and record the number of
220 /// excess register units of that pressure set introduced by this instruction.
221 void getMaxUpwardPressureDelta(const MachineInstr *MI,
222 RegPressureDelta &Delta,
223 ArrayRef<PressureElement> CriticalPSets,
224 ArrayRef<unsigned> MaxPressureLimit);
226 /// Consider the pressure increase caused by traversing this instruction
227 /// top-down. Find the pressure set with the most change beyond its pressure
228 /// limit based on the tracker's current pressure, and record the number of
229 /// excess register units of that pressure set introduced by this instruction.
230 void getMaxDownwardPressureDelta(const MachineInstr *MI,
231 RegPressureDelta &Delta,
232 ArrayRef<PressureElement> CriticalPSets,
233 ArrayRef<unsigned> MaxPressureLimit);
235 /// Find the pressure set with the most change beyond its pressure limit after
236 /// traversing this instruction either upward or downward depending on the
237 /// closed end of the current region.
238 void getMaxPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
239 ArrayRef<PressureElement> CriticalPSets,
240 ArrayRef<unsigned> MaxPressureLimit) {
242 return getMaxDownwardPressureDelta(MI, Delta, CriticalPSets,
245 assert(isBottomClosed() && "Uninitialized pressure tracker");
246 return getMaxUpwardPressureDelta(MI, Delta, CriticalPSets,
250 /// Get the pressure of each PSet after traversing this instruction bottom-up.
251 void getUpwardPressure(const MachineInstr *MI,
252 std::vector<unsigned> &PressureResult,
253 std::vector<unsigned> &MaxPressureResult);
255 /// Get the pressure of each PSet after traversing this instruction top-down.
256 void getDownwardPressure(const MachineInstr *MI,
257 std::vector<unsigned> &PressureResult,
258 std::vector<unsigned> &MaxPressureResult);
260 void getPressureAfterInst(const MachineInstr *MI,
261 std::vector<unsigned> &PressureResult,
262 std::vector<unsigned> &MaxPressureResult) {
264 return getUpwardPressure(MI, PressureResult, MaxPressureResult);
266 assert(isBottomClosed() && "Uninitialized pressure tracker");
267 return getDownwardPressure(MI, PressureResult, MaxPressureResult);
271 void increasePhysRegPressure(ArrayRef<unsigned> Regs);
272 void decreasePhysRegPressure(ArrayRef<unsigned> Regs);
274 void increaseVirtRegPressure(ArrayRef<unsigned> Regs);
275 void decreaseVirtRegPressure(ArrayRef<unsigned> Regs);
277 void bumpUpwardPressure(const MachineInstr *MI);
278 void bumpDownwardPressure(const MachineInstr *MI);
280 } // end namespace llvm