Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 1 | //===---------------------- 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 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | struct 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 Mekhanoshin | 7e3794d | 2017-05-09 20:50:04 +0000 | [diff] [blame] | 36 | bool empty() const { return getSGPRNum() == 0 && getVGPRNum() == 0; } |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 37 | |
| 38 | void clear() { std::fill(&Value[0], &Value[TOTAL_KINDS], 0); } |
| 39 | |
Stanislav Mekhanoshin | 7e3794d | 2017-05-09 20:50:04 +0000 | [diff] [blame] | 40 | unsigned getSGPRNum() const { return Value[SGPR32]; } |
| 41 | unsigned getVGPRNum() const { return Value[VGPR32]; } |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 42 | |
| 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 Mekhanoshin | 7e3794d | 2017-05-09 20:50:04 +0000 | [diff] [blame] | 47 | return std::min(ST.getOccupancyWithNumSGPRs(getSGPRNum()), |
| 48 | ST.getOccupancyWithNumVGPRs(getVGPRNum())); |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 49 | } |
| 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 | |
| 74 | private: |
| 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 | |
| 83 | inline 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 | |
| 90 | class GCNRPTracker { |
| 91 | public: |
| 92 | typedef DenseMap<unsigned, LaneBitmask> LiveRegSet; |
| 93 | |
| 94 | protected: |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 95 | const LiveIntervals &LIS; |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 96 | LiveRegSet LiveRegs; |
| 97 | GCNRegPressure CurPressure, MaxPressure; |
| 98 | const MachineInstr *LastTrackedMI = nullptr; |
| 99 | mutable const MachineRegisterInfo *MRI = nullptr; |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 100 | GCNRPTracker(const LiveIntervals &LIS_) : LIS(LIS_) {} |
| 101 | LaneBitmask getDefRegMask(const MachineOperand &MO) const; |
| 102 | LaneBitmask getUsedRegMask(const MachineOperand &MO) const; |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 103 | public: |
| 104 | // live regs for the current state |
| 105 | const decltype(LiveRegs) &getLiveRegs() const { return LiveRegs; } |
| 106 | const MachineInstr *getLastTrackedMI() const { return LastTrackedMI; } |
| 107 | |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 108 | void clearMaxPressure() { MaxPressure.clear(); } |
| 109 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 110 | // 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 | } |
| 119 | }; |
| 120 | |
| 121 | class GCNUpwardRPTracker : public GCNRPTracker { |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 122 | public: |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 123 | GCNUpwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {} |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 124 | // reset tracker to the point just below MI |
| 125 | // filling live regs upon this point using LIS |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 126 | void reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr); |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 127 | |
| 128 | // move to the state just above the MI |
| 129 | void recede(const MachineInstr &MI); |
| 130 | |
| 131 | // checks whether the tracker's state after receding MI corresponds |
| 132 | // to reported by LIS |
| 133 | bool isValid() const; |
| 134 | }; |
| 135 | |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame^] | 136 | class GCNDownwardRPTracker : public GCNRPTracker { |
| 137 | // Last position of reset or advanceBeforeNext |
| 138 | MachineBasicBlock::const_iterator NextMI; |
| 139 | |
| 140 | MachineBasicBlock::const_iterator MBBEnd; |
| 141 | |
| 142 | public: |
| 143 | GCNDownwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {} |
| 144 | |
| 145 | const MachineBasicBlock::const_iterator getNext() const { return NextMI; } |
| 146 | |
| 147 | // Reset tracker to the point before the MI |
| 148 | // filling live regs upon this point using LIS. |
| 149 | // Returns false if block is empty except debug values. |
| 150 | bool reset(const MachineInstr &MI, const LiveRegSet *LiveRegs = nullptr); |
| 151 | |
| 152 | // Move to the state right before the next MI. Returns false if reached |
| 153 | // end of the block. |
| 154 | bool advanceBeforeNext(); |
| 155 | |
| 156 | // Move to the state at the MI, advanceBeforeNext has to be called first. |
| 157 | void advanceToNext(); |
| 158 | |
| 159 | // Move to the state at the next MI. Returns false if reached end of block. |
| 160 | bool advance(); |
| 161 | |
| 162 | // Advance instructions until before End. |
| 163 | bool advance(MachineBasicBlock::const_iterator End); |
| 164 | |
| 165 | // Reset to Begin and advance to End. |
| 166 | bool advance(MachineBasicBlock::const_iterator Begin, |
| 167 | MachineBasicBlock::const_iterator End, |
| 168 | const LiveRegSet *LiveRegsCopy = nullptr); |
| 169 | }; |
| 170 | |
Valery Pykhtin | fd4c410 | 2017-03-21 13:15:46 +0000 | [diff] [blame] | 171 | LaneBitmask getLiveLaneMask(unsigned Reg, |
| 172 | SlotIndex SI, |
| 173 | const LiveIntervals &LIS, |
| 174 | const MachineRegisterInfo &MRI); |
| 175 | |
| 176 | GCNRPTracker::LiveRegSet getLiveRegs(SlotIndex SI, |
| 177 | const LiveIntervals &LIS, |
| 178 | const MachineRegisterInfo &MRI); |
| 179 | |
| 180 | inline GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI, |
| 181 | const LiveIntervals &LIS) { |
| 182 | return getLiveRegs(LIS.getInstructionIndex(MI).getDeadSlot(), LIS, |
| 183 | MI.getParent()->getParent()->getRegInfo()); |
| 184 | } |
| 185 | |
| 186 | inline GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI, |
| 187 | const LiveIntervals &LIS) { |
| 188 | return getLiveRegs(LIS.getInstructionIndex(MI).getBaseIndex(), LIS, |
| 189 | MI.getParent()->getParent()->getRegInfo()); |
| 190 | } |
| 191 | |
| 192 | template <typename Range> |
| 193 | GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI, |
| 194 | Range &&LiveRegs) { |
| 195 | GCNRegPressure Res; |
| 196 | for (const auto &RM : LiveRegs) |
| 197 | Res.inc(RM.first, LaneBitmask::getNone(), RM.second, MRI); |
| 198 | return Res; |
| 199 | } |
| 200 | |
| 201 | void printLivesAt(SlotIndex SI, |
| 202 | const LiveIntervals &LIS, |
| 203 | const MachineRegisterInfo &MRI); |
| 204 | |
| 205 | } // End namespace llvm |
| 206 | |
| 207 | #endif // LLVM_LIB_TARGET_AMDGPU_GCNREGPRESSURE_H |