blob: 32a199d510c56a6ac013373032168a197055a5cc [file] [log] [blame]
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +00001//===- GCNRegPressure.h -----------------------------------------*- C++ -*-===//
Valery Pykhtinfd4c4102017-03-21 13:15:46 +00002//
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//===----------------------------------------------------------------------===//
Valery Pykhtinfd4c4102017-03-21 13:15:46 +00009
10#ifndef LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H
11#define LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H
12
13#include "AMDGPUSubtarget.h"
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000014#include "llvm/ADT/DenseMap.h"
15#include "llvm/CodeGen/LiveIntervalAnalysis.h"
16#include "llvm/CodeGen/MachineBasicBlock.h"
17#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/SlotIndexes.h"
19#include "llvm/MC/LaneBitmask.h"
20#include "llvm/Support/Debug.h"
21#include <algorithm>
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000022#include <limits>
23
24namespace llvm {
25
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000026class MachineRegisterInfo;
27class raw_ostream;
28
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000029struct GCNRegPressure {
30 enum RegKind {
31 SGPR32,
32 SGPR_TUPLE,
33 VGPR32,
34 VGPR_TUPLE,
35 TOTAL_KINDS
36 };
37
38 GCNRegPressure() {
39 clear();
40 }
41
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000042 bool empty() const { return getSGPRNum() == 0 && getVGPRNum() == 0; }
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000043
44 void clear() { std::fill(&Value[0], &Value[TOTAL_KINDS], 0); }
45
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000046 unsigned getSGPRNum() const { return Value[SGPR32]; }
47 unsigned getVGPRNum() const { return Value[VGPR32]; }
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000048
49 unsigned getVGPRTuplesWeight() const { return Value[VGPR_TUPLE]; }
50 unsigned getSGPRTuplesWeight() const { return Value[SGPR_TUPLE]; }
51
52 unsigned getOccupancy(const SISubtarget &ST) const {
Stanislav Mekhanoshin7e3794d2017-05-09 20:50:04 +000053 return std::min(ST.getOccupancyWithNumSGPRs(getSGPRNum()),
54 ST.getOccupancyWithNumVGPRs(getVGPRNum()));
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000055 }
56
57 void inc(unsigned Reg,
58 LaneBitmask PrevMask,
59 LaneBitmask NewMask,
60 const MachineRegisterInfo &MRI);
61
62 bool higherOccupancy(const SISubtarget &ST, const GCNRegPressure& O) const {
63 return getOccupancy(ST) > O.getOccupancy(ST);
64 }
65
66 bool less(const SISubtarget &ST, const GCNRegPressure& O,
67 unsigned MaxOccupancy = std::numeric_limits<unsigned>::max()) const;
68
69 bool operator==(const GCNRegPressure &O) const {
70 return std::equal(&Value[0], &Value[TOTAL_KINDS], O.Value);
71 }
72
73 bool operator!=(const GCNRegPressure &O) const {
74 return !(*this == O);
75 }
76
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000077 void print(raw_ostream &OS, const SISubtarget *ST = nullptr) const;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000078 void dump() const { print(dbgs()); }
79
80private:
81 unsigned Value[TOTAL_KINDS];
82
83 static unsigned getRegKind(unsigned Reg, const MachineRegisterInfo &MRI);
84
85 friend GCNRegPressure max(const GCNRegPressure &P1,
86 const GCNRegPressure &P2);
87};
88
89inline GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2) {
90 GCNRegPressure Res;
91 for (unsigned I = 0; I < GCNRegPressure::TOTAL_KINDS; ++I)
92 Res.Value[I] = std::max(P1.Value[I], P2.Value[I]);
93 return Res;
94}
95
96class GCNRPTracker {
97public:
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000098 using LiveRegSet = DenseMap<unsigned, LaneBitmask>;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000099
100protected:
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000101 const LiveIntervals &LIS;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000102 LiveRegSet LiveRegs;
103 GCNRegPressure CurPressure, MaxPressure;
104 const MachineInstr *LastTrackedMI = nullptr;
105 mutable const MachineRegisterInfo *MRI = nullptr;
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000106
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000107 GCNRPTracker(const LiveIntervals &LIS_) : LIS(LIS_) {}
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000108
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000109public:
110 // live regs for the current state
111 const decltype(LiveRegs) &getLiveRegs() const { return LiveRegs; }
112 const MachineInstr *getLastTrackedMI() const { return LastTrackedMI; }
113
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000114 void clearMaxPressure() { MaxPressure.clear(); }
115
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000116 // returns MaxPressure, resetting it
117 decltype(MaxPressure) moveMaxPressure() {
118 auto Res = MaxPressure;
119 MaxPressure.clear();
120 return Res;
121 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000122
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000123 decltype(LiveRegs) moveLiveRegs() {
124 return std::move(LiveRegs);
125 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000126
Stanislav Mekhanoshinacca0f52017-05-16 16:31:45 +0000127 static void printLiveRegs(raw_ostream &OS, const LiveRegSet& LiveRegs,
128 const MachineRegisterInfo &MRI);
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000129};
130
131class GCNUpwardRPTracker : public GCNRPTracker {
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000132public:
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000133 GCNUpwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {}
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000134
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000135 // reset tracker to the point just below MI
136 // filling live regs upon this point using LIS
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000137 void reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr);
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000138
139 // move to the state just above the MI
140 void recede(const MachineInstr &MI);
141
142 // checks whether the tracker's state after receding MI corresponds
143 // to reported by LIS
144 bool isValid() const;
145};
146
Stanislav Mekhanoshin464cecf2017-05-16 15:43:52 +0000147class GCNDownwardRPTracker : public GCNRPTracker {
148 // Last position of reset or advanceBeforeNext
149 MachineBasicBlock::const_iterator NextMI;
150
151 MachineBasicBlock::const_iterator MBBEnd;
152
153public:
154 GCNDownwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {}
155
156 const MachineBasicBlock::const_iterator getNext() const { return NextMI; }
157
158 // Reset tracker to the point before the MI
159 // filling live regs upon this point using LIS.
160 // Returns false if block is empty except debug values.
161 bool reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr);
162
163 // Move to the state right before the next MI. Returns false if reached
164 // end of the block.
165 bool advanceBeforeNext();
166
167 // Move to the state at the MI, advanceBeforeNext has to be called first.
168 void advanceToNext();
169
170 // Move to the state at the next MI. Returns false if reached end of block.
171 bool advance();
172
173 // Advance instructions until before End.
174 bool advance(MachineBasicBlock::const_iterator End);
175
176 // Reset to Begin and advance to End.
177 bool advance(MachineBasicBlock::const_iterator Begin,
178 MachineBasicBlock::const_iterator End,
179 const LiveRegSet *LiveRegsCopy = nullptr);
180};
181
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000182LaneBitmask getLiveLaneMask(unsigned Reg,
183 SlotIndex SI,
184 const LiveIntervals &LIS,
185 const MachineRegisterInfo &MRI);
186
187GCNRPTracker::LiveRegSet getLiveRegs(SlotIndex SI,
188 const LiveIntervals &LIS,
189 const MachineRegisterInfo &MRI);
190
191inline GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI,
192 const LiveIntervals &LIS) {
193 return getLiveRegs(LIS.getInstructionIndex(MI).getDeadSlot(), LIS,
194 MI.getParent()->getParent()->getRegInfo());
195}
196
197inline GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI,
198 const LiveIntervals &LIS) {
199 return getLiveRegs(LIS.getInstructionIndex(MI).getBaseIndex(), LIS,
200 MI.getParent()->getParent()->getRegInfo());
201}
202
203template <typename Range>
204GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI,
205 Range &&LiveRegs) {
206 GCNRegPressure Res;
207 for (const auto &RM : LiveRegs)
208 Res.inc(RM.first, LaneBitmask::getNone(), RM.second, MRI);
209 return Res;
210}
211
212void printLivesAt(SlotIndex SI,
213 const LiveIntervals &LIS,
214 const MachineRegisterInfo &MRI);
215
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000216} // end namespace llvm
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000217
218#endif // LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H