blob: 9875ca6a6d1615cfa8402f8830bdf995c462150e [file] [log] [blame]
Valery Pykhtinfd4c4102017-03-21 13:15:46 +00001//===---------------------- GCNRegPressure.h -*- 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/// \file
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H
15#define LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H
16
17#include "AMDGPUSubtarget.h"
18
19#include <limits>
20
21namespace llvm {
22
23struct GCNRegPressure {
24 enum RegKind {
25 SGPR32,
26 SGPR_TUPLE,
27 VGPR32,
28 VGPR_TUPLE,
29 TOTAL_KINDS
30 };
31
32 GCNRegPressure() {
33 clear();
34 }
35
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000036 bool empty() const { return getSGPRNum() == 0 && getVGPRNum() == 0; }
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000037
38 void clear() { std::fill(&Value[0], &Value[TOTAL_KINDS], 0); }
39
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000040 unsigned getSGPRNum() const { return Value[SGPR32]; }
41 unsigned getVGPRNum() const { return Value[VGPR32]; }
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000042
43 unsigned getVGPRTuplesWeight() const { return Value[VGPR_TUPLE]; }
44 unsigned getSGPRTuplesWeight() const { return Value[SGPR_TUPLE]; }
45
46 unsigned getOccupancy(const SISubtarget &ST) const {
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000047 return std::min(ST.getOccupancyWithNumSGPRs(getSGPRNum()),
48 ST.getOccupancyWithNumVGPRs(getVGPRNum()));
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000049 }
50
51 void inc(unsigned Reg,
52 LaneBitmask PrevMask,
53 LaneBitmask NewMask,
54 const MachineRegisterInfo &MRI);
55
56 bool higherOccupancy(const SISubtarget &ST, const GCNRegPressure& O) const {
57 return getOccupancy(ST) > O.getOccupancy(ST);
58 }
59
60 bool less(const SISubtarget &ST, const GCNRegPressure& O,
61 unsigned MaxOccupancy = std::numeric_limits<unsigned>::max()) const;
62
63 bool operator==(const GCNRegPressure &O) const {
64 return std::equal(&Value[0], &Value[TOTAL_KINDS], O.Value);
65 }
66
67 bool operator!=(const GCNRegPressure &O) const {
68 return !(*this == O);
69 }
70
71 void print(raw_ostream &OS, const SISubtarget *ST=nullptr) const;
72 void dump() const { print(dbgs()); }
73
74private:
75 unsigned Value[TOTAL_KINDS];
76
77 static unsigned getRegKind(unsigned Reg, const MachineRegisterInfo &MRI);
78
79 friend GCNRegPressure max(const GCNRegPressure &P1,
80 const GCNRegPressure &P2);
81};
82
83inline GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2) {
84 GCNRegPressure Res;
85 for (unsigned I = 0; I < GCNRegPressure::TOTAL_KINDS; ++I)
86 Res.Value[I] = std::max(P1.Value[I], P2.Value[I]);
87 return Res;
88}
89
90class GCNRPTracker {
91public:
92 typedef DenseMap<unsigned, LaneBitmask> LiveRegSet;
93
94protected:
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +000095 const LiveIntervals &LIS;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000096 LiveRegSet LiveRegs;
97 GCNRegPressure CurPressure, MaxPressure;
98 const MachineInstr *LastTrackedMI = nullptr;
99 mutable const MachineRegisterInfo *MRI = nullptr;
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000100 GCNRPTracker(const LiveIntervals &LIS_) : LIS(LIS_) {}
101 LaneBitmask getDefRegMask(const MachineOperand &MO) const;
102 LaneBitmask getUsedRegMask(const MachineOperand &MO) const;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000103public:
104 // live regs for the current state
105 const decltype(LiveRegs) &getLiveRegs() const { return LiveRegs; }
106 const MachineInstr *getLastTrackedMI() const { return LastTrackedMI; }
107
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000108 void clearMaxPressure() { MaxPressure.clear(); }
109
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000110 // returns MaxPressure, resetting it
111 decltype(MaxPressure) moveMaxPressure() {
112 auto Res = MaxPressure;
113 MaxPressure.clear();
114 return Res;
115 }
116 decltype(LiveRegs) moveLiveRegs() {
117 return std::move(LiveRegs);
118 }
Stanislav Mekhanoshinacca0f52017-05-16 16:31:45 +0000119 static void printLiveRegs(raw_ostream &OS, const LiveRegSet& LiveRegs,
120 const MachineRegisterInfo &MRI);
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000121};
122
123class GCNUpwardRPTracker : public GCNRPTracker {
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000124public:
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000125 GCNUpwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {}
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000126 // reset tracker to the point just below MI
127 // filling live regs upon this point using LIS
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000128 void reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr);
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000129
130 // move to the state just above the MI
131 void recede(const MachineInstr &MI);
132
133 // checks whether the tracker's state after receding MI corresponds
134 // to reported by LIS
135 bool isValid() const;
136};
137
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000138class GCNDownwardRPTracker : public GCNRPTracker {
139 // Last position of reset or advanceBeforeNext
140 MachineBasicBlock::const_iterator NextMI;
141
142 MachineBasicBlock::const_iterator MBBEnd;
143
144public:
145 GCNDownwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {}
146
147 const MachineBasicBlock::const_iterator getNext() const { return NextMI; }
148
149 // Reset tracker to the point before the MI
150 // filling live regs upon this point using LIS.
151 // Returns false if block is empty except debug values.
152 bool reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr);
153
154 // Move to the state right before the next MI. Returns false if reached
155 // end of the block.
156 bool advanceBeforeNext();
157
158 // Move to the state at the MI, advanceBeforeNext has to be called first.
159 void advanceToNext();
160
161 // Move to the state at the next MI. Returns false if reached end of block.
162 bool advance();
163
164 // Advance instructions until before End.
165 bool advance(MachineBasicBlock::const_iterator End);
166
167 // Reset to Begin and advance to End.
168 bool advance(MachineBasicBlock::const_iterator Begin,
169 MachineBasicBlock::const_iterator End,
170 const LiveRegSet *LiveRegsCopy = nullptr);
171};
172
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000173LaneBitmask getLiveLaneMask(unsigned Reg,
174 SlotIndex SI,
175 const LiveIntervals &LIS,
176 const MachineRegisterInfo &MRI);
177
178GCNRPTracker::LiveRegSet getLiveRegs(SlotIndex SI,
179 const LiveIntervals &LIS,
180 const MachineRegisterInfo &MRI);
181
182inline GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI,
183 const LiveIntervals &LIS) {
184 return getLiveRegs(LIS.getInstructionIndex(MI).getDeadSlot(), LIS,
185 MI.getParent()->getParent()->getRegInfo());
186}
187
188inline GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI,
189 const LiveIntervals &LIS) {
190 return getLiveRegs(LIS.getInstructionIndex(MI).getBaseIndex(), LIS,
191 MI.getParent()->getParent()->getRegInfo());
192}
193
194template <typename Range>
195GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI,
196 Range &&LiveRegs) {
197 GCNRegPressure Res;
198 for (const auto &RM : LiveRegs)
199 Res.inc(RM.first, LaneBitmask::getNone(), RM.second, MRI);
200 return Res;
201}
202
203void printLivesAt(SlotIndex SI,
204 const LiveIntervals &LIS,
205 const MachineRegisterInfo &MRI);
206
207} // End namespace llvm
208
209#endif // LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H