14#ifndef LLVM_LIB_TARGET_AMDGPU_GCNREMATSTRATEGY_H
15#define LLVM_LIB_TARGET_AMDGPU_GCNREMATSTRATEGY_H
55 : Regions(Regions, LiveIns, LiveOuts, LIS, MBFI, MF.getRegInfo()),
56 Remater(MF, Regions, LIS), MF(MF), MRI(MF.getRegInfo()), LIS(LIS) {}
67 bool SpillReduction,
BitVector &AffectedRegions,
72 assert(!Recorder &&
"recorder already defined");
73 Recorder = std::make_unique<RematRecorder>(Remater);
78 assert(Recorder &&
"recorder not defined");
79 Remater.removeListener(&Recorder->Listener);
80 Recorder.reset(
nullptr);
101 unsigned size()
const {
return Bounds.
size(); }
103 uint64_t getFrequency(
unsigned RegionIdx)
const {
104 return Frequencies[RegionIdx];
108 uint64_t RegionFreq = Frequencies[RegionIdx];
109 return RegionFreq ? RegionFreq :
Default;
112 uint64_t getFrequencyOrMax(
unsigned RegionIdx)
const {
113 return getFrequencyOr(RegionIdx, MaxFreq);
116 uint64_t getFrequencyOrMin(
unsigned RegionIdx)
const {
117 return getFrequencyOr(RegionIdx, MinFreq);
122 assert(RegionIdx < size() &&
"out of bounds");
123 return LiveIns[RegionIdx];
128 assert(RegionIdx < size() &&
"out of bounds");
129 return LiveOuts[RegionIdx];
133 GCNRegPressure computeMaxRegPressure(
unsigned RegionIdx)
const;
137 void removeFromLiveMaps(
Register Reg,
const BitVector &LiveIn,
138 const BitVector &LiveOut);
142 void addToLiveMaps(
Register Reg, LaneBitmask Mask,
const BitVector &LiveIn,
143 const BitVector &LiveOut);
146 static const uint64_t FreqScaleFactor = 1024;
151 SmallVector<uint64_t> Frequencies;
156 const MachineRegisterInfo &MRI;
160 struct RematTargets {
166 const RematRegions &Regions;
168 const bool SpillReduction;
174 RematTargets(
unsigned MaxSGPRs,
unsigned MaxVGPRs,
bool SpillReduction,
179 bool allAchieved()
const {
return ExcessRP.none(); }
183 void updateRPTargets(
const BitVector &UpdateRegions,
184 const GCNRegPressure &RPSave);
190 bool updateAndVerifyRPTargets(
const BitVector &UpdateRegions);
192#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
193 Printable print()
const;
200 struct RematCandidate {
204 BitVector LiveIn, LiveOut, Live;
208 BitVector UnpredictableRPSave;
211 GCNRegPressure RPSave;
213 RematCandidate(
RegisterIdx RegIdx,
unsigned NumRegions)
214 : RegIdx(RegIdx), LiveIn(NumRegions), LiveOut(NumRegions),
215 Live(NumRegions), UnpredictableRPSave(NumRegions) {}
220 void init(
const RematRegions &RF,
const Rematerializer &Remater,
221 const MachineRegisterInfo &
MRI);
224 void update(
const RematTargets &Targets);
228 bool maybeBeneficial(
const RematTargets &Targets)
const;
231 void rematerialize(Rematerializer &Remater)
const;
235 bool hasNullScore()
const {
return !RegionImpact; }
239 bool operator<(
const RematCandidate &O)
const {
240 assert(!hasNullScore() &&
"this has null score");
241 assert(!
O.hasNullScore() &&
"other has null score");
242 if (MaxFreq !=
O.MaxFreq)
243 return MaxFreq <
O.MaxFreq;
244 if (FreqDiff !=
O.FreqDiff)
245 return FreqDiff <
O.FreqDiff;
246 if (RegionImpact !=
O.RegionImpact)
247 return RegionImpact <
O.RegionImpact;
257 return RegIdx >
O.RegIdx;
260#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
261 Printable print()
const;
278 unsigned RegionImpact;
281 struct RematRecorder {
300 RematRecorder(
Rematerializer &Remater) { Remater.addListener(&Listener); }
306 RematRegions Regions;
309 Rematerializer Remater;
311 SmallVector<RematCandidate, 0> Candidates;
314 bool CandidatesCreated =
false;
316 std::unique_ptr<RematRecorder> Recorder;
319 const MachineRegisterInfo &
MRI;
324 void createCandidates();
331 const SmallSet<Register, 4> &MarkedRegs)
const;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the GCNRegPressure class, which tracks registry pressure by bookkeeping number of S...
Promote Memory to Register
MachineRegisterInfo & MRI
Rematerializer::RegisterIdx RegisterIdx
MIR-level target-independent rematerialization helpers.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
DenseMap< unsigned, LaneBitmask > LiveRegSet
GCNRematStrategy(MachineFunction &MF, MutableArrayRef< RegionBoundaries > Regions, MutableArrayRef< GCNRPTracker::LiveRegSet > LiveIns, MutableArrayRef< GCNRPTracker::LiveRegSet > LiveOuts, LiveIntervals &LIS, const MachineBlockFrequencyInfo &MBFI)
Creates a strategy that can rematerialize across Regions whose live-ins LiveIns and live-outs LiveOut...
void rollbackRecordedRemats()
Rolls back all recorded rematerializations.
void clearRecordedRemats()
Stops recording rematerializations and clears all recorded ones.
void startRecordingRemats()
Starts recording rematerializations for eventual rollback.
void rematerializeFor(unsigned MaxSGPRs, unsigned MaxVGPRs, bool SpillReduction, BitVector &AffectedRegions, MutableArrayRef< GCNRegPressure > *MaxPressure=nullptr)
Rematerializes with the goal of reducing the maximum pressure in all Regions to MaxSGPRs SGPRs and Ma...
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
MIR-level target-independent rematerializer.
std::pair< MachineBasicBlock::iterator, MachineBasicBlock::iterator > RegionBoundaries
A region's boundaries i.e.
unsigned RegisterIdx
Index type for rematerializable registers.
Rematerializer listener with the ability to re-create deleted registers and rollback rematerializatio...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
ArrayRef(const T &OneElt) -> ArrayRef< T >
RegisterIdx RegIdx
The register index handle in the rematerializer.
BitVector LiveIn
Regions in which the original register was live-in or live-out.
LiveMapUpdate(RegisterIdx RegIdx, const BitVector &LiveIn, const BitVector &LiveOut)