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 /// Store the results of a change in pressure.
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
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.
95 /// If ExcessUnits == 0, then ExcessSetID is invalid.
96 /// If MaxUnitIncrease == 0, then MaxSetID is invalid.
97 struct RegPressureDelta {
104 ExcessUnits(0), ExcessSetID(~0U), MaxUnitIncrease(0), MaxSetID(~0U) {}
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.
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.
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;
130 /// We currently only allow pressure tracking within a block.
131 const MachineBasicBlock *MBB;
133 /// Track the max pressure within the region traversed so far.
136 /// Run in two modes dependending on whether constructed with IntervalPressure
137 /// or RegisterPressure. If requireIntervals is false, LIS are ignored.
138 bool RequireIntervals;
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;
144 /// Pressure map indexed by pressure set ID, not class ID.
145 std::vector<unsigned> CurrSetPressure;
147 /// List of live registers.
148 SparseSet<unsigned> LivePhysRegs;
149 SparseSet<unsigned, VirtReg2IndexFunctor> LiveVirtRegs;
152 RegPressureTracker(IntervalPressure &rp) :
153 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(true) {}
155 RegPressureTracker(RegionPressure &rp) :
156 MF(0), TRI(0), RCI(0), LIS(0), MBB(0), P(rp), RequireIntervals(false) {}
158 void init(const MachineFunction *mf, const RegisterClassInfo *rci,
159 const LiveIntervals *lis, const MachineBasicBlock *mbb,
160 MachineBasicBlock::const_iterator pos);
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);
166 /// Get the MI position corresponding to this register pressure.
167 MachineBasicBlock::const_iterator getPos() const { return CurrPos; }
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; }
175 /// Recede across the previous instruction.
178 /// Advance across the current instruction.
181 /// Finalize the region boundaries and recored live ins and live outs.
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; }
189 void discoverPhysLiveIn(unsigned Reg);
190 void discoverPhysLiveOut(unsigned Reg);
192 void discoverVirtLiveIn(unsigned Reg);
193 void discoverVirtLiveOut(unsigned Reg);
195 bool isTopClosed() const;
196 bool isBottomClosed() const;
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);
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);
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) {
220 return getMaxDownwardPressureDelta(MI, Delta);
221 assert(isBottomClosed() && "Uninitialized pressure tracker");
222 return getMaxUpwardPressureDelta(MI, Delta);
226 void increasePhysRegPressure(ArrayRef<unsigned> Regs);
227 void decreasePhysRegPressure(ArrayRef<unsigned> Regs);
229 void increaseVirtRegPressure(ArrayRef<unsigned> Regs);
230 void decreaseVirtRegPressure(ArrayRef<unsigned> Regs);
232 } // end namespace llvm