blob: c33363170c6cf077e3dd1d90291b97d9931c1437 [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:
95 LiveRegSet LiveRegs;
96 GCNRegPressure CurPressure, MaxPressure;
97 const MachineInstr *LastTrackedMI = nullptr;
98 mutable const MachineRegisterInfo *MRI = nullptr;
99 GCNRPTracker() {}
100public:
101 // live regs for the current state
102 const decltype(LiveRegs) &getLiveRegs() const { return LiveRegs; }
103 const MachineInstr *getLastTrackedMI() const { return LastTrackedMI; }
104
105 // returns MaxPressure, resetting it
106 decltype(MaxPressure) moveMaxPressure() {
107 auto Res = MaxPressure;
108 MaxPressure.clear();
109 return Res;
110 }
111 decltype(LiveRegs) moveLiveRegs() {
112 return std::move(LiveRegs);
113 }
114};
115
116class GCNUpwardRPTracker : public GCNRPTracker {
117 const LiveIntervals &LIS;
118 LaneBitmask getDefRegMask(const MachineOperand &MO) const;
119 LaneBitmask getUsedRegMask(const MachineOperand &MO) const;
120public:
121 GCNUpwardRPTracker(const LiveIntervals &LIS_) : LIS(LIS_) {}
122 // reset tracker to the point just below MI
123 // filling live regs upon this point using LIS
124 void reset(const MachineInstr &MI);
125
126 // move to the state just above the MI
127 void recede(const MachineInstr &MI);
128
129 // checks whether the tracker's state after receding MI corresponds
130 // to reported by LIS
131 bool isValid() const;
132};
133
134LaneBitmask getLiveLaneMask(unsigned Reg,
135 SlotIndex SI,
136 const LiveIntervals &LIS,
137 const MachineRegisterInfo &MRI);
138
139GCNRPTracker::LiveRegSet getLiveRegs(SlotIndex SI,
140 const LiveIntervals &LIS,
141 const MachineRegisterInfo &MRI);
142
143inline GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI,
144 const LiveIntervals &LIS) {
145 return getLiveRegs(LIS.getInstructionIndex(MI).getDeadSlot(), LIS,
146 MI.getParent()->getParent()->getRegInfo());
147}
148
149inline GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI,
150 const LiveIntervals &LIS) {
151 return getLiveRegs(LIS.getInstructionIndex(MI).getBaseIndex(), LIS,
152 MI.getParent()->getParent()->getRegInfo());
153}
154
155template <typename Range>
156GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI,
157 Range &&LiveRegs) {
158 GCNRegPressure Res;
159 for (const auto &RM : LiveRegs)
160 Res.inc(RM.first, LaneBitmask::getNone(), RM.second, MRI);
161 return Res;
162}
163
164void printLivesAt(SlotIndex SI,
165 const LiveIntervals &LIS,
166 const MachineRegisterInfo &MRI);
167
168} // End namespace llvm
169
170#endif // LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H