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);
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.
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.
63 void openTop(SlotIndex NextTop);
65 void openBottom(SlotIndex PrevBottom);
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;
78 void openTop(MachineBasicBlock::const_iterator PrevTop);
80 void openBottom(MachineBasicBlock::const_iterator PrevBottom);
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 {
89 PressureElement(): PSetID(~0U), UnitIncrease(0) {}
90 PressureElement(unsigned id, int inc): PSetID(id), UnitIncrease(inc) {}
92 bool isValid() const { return PSetID != ~0U; }
95 /// Store the effects of a change in pressure on things that MI scheduler cares
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
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.
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;
114 RegPressureDelta() {}
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.
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.
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;
140 /// We currently only allow pressure tracking within a block.
141 const MachineBasicBlock *MBB;
143 /// Track the max pressure within the region traversed so far.
146 /// Run in two modes dependending on whether constructed with IntervalPressure
147 /// or RegisterPressure. If requireIntervals is false, LIS are ignored.
148 bool RequireIntervals;
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;
154 /// Pressure map indexed by pressure set ID, not class ID.
155 std::vector<unsigned> CurrSetPressure;
157 /// List of live registers.
158 SparseSet<unsigned> LivePhysRegs;
159 SparseSet<unsigned, VirtReg2IndexFunctor> LiveVirtRegs;
162 RegPressureTracker(IntervalPressure &rp) :
163 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(true) {}
165 RegPressureTracker(RegionPressure &rp) :
166 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(false) {}
168 void init(const MachineFunction *mf, const RegisterClassInfo *rci,
169 const LiveIntervals *lis, const MachineBasicBlock *mbb,
170 MachineBasicBlock::const_iterator pos);
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);
176 /// Get the MI position corresponding to this register pressure.
177 MachineBasicBlock::const_iterator getPos() const { return CurrPos; }
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; }
185 /// Recede across the previous instruction.
188 /// Advance across the current instruction.
191 /// Finalize the region boundaries and recored live ins and live outs.
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; }
199 void discoverPhysLiveIn(unsigned Reg);
200 void discoverPhysLiveOut(unsigned Reg);
202 void discoverVirtLiveIn(unsigned Reg);
203 void discoverVirtLiveOut(unsigned Reg);
205 bool isTopClosed() const;
206 bool isBottomClosed() const;
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);
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);
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) {
236 return getMaxDownwardPressureDelta(MI, Delta, CriticalPSets,
239 assert(isBottomClosed() && "Uninitialized pressure tracker");
240 return getMaxUpwardPressureDelta(MI, Delta, CriticalPSets,
245 void increasePhysRegPressure(ArrayRef<unsigned> Regs);
246 void decreasePhysRegPressure(ArrayRef<unsigned> Regs);
248 void increaseVirtRegPressure(ArrayRef<unsigned> Regs);
249 void decreaseVirtRegPressure(ArrayRef<unsigned> Regs);
251 } // end namespace llvm