Skip to content

Commit f151fcb

Browse files
committed
[AMDGPU] NFC: Provide RPTracker interface for external iterators
Change-Id: I79b54722e6e858961486248d94766c3f3c161160
1 parent f5423ab commit f151fcb

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

llvm/lib/Target/AMDGPU/GCNRegPressure.cpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,25 @@ void GCNRPTracker::reset(const MachineInstr &MI,
343343
MaxPressure = CurPressure = getRegPressure(*MRI, LiveRegs);
344344
}
345345

346-
////////////////////////////////////////////////////////////////////////////////
347-
// GCNUpwardRPTracker
348-
349-
void GCNUpwardRPTracker::reset(const MachineRegisterInfo &MRI_,
350-
const LiveRegSet &LiveRegs_) {
346+
void GCNRPTracker::reset(const MachineRegisterInfo &MRI_,
347+
const LiveRegSet &LiveRegs_) {
351348
MRI = &MRI_;
352349
LiveRegs = LiveRegs_;
353350
LastTrackedMI = nullptr;
354351
MaxPressure = CurPressure = getRegPressure(MRI_, LiveRegs_);
355352
}
356353

357-
void GCNUpwardRPTracker::recede(const MachineInstr &MI) {
354+
////////////////////////////////////////////////////////////////////////////////
355+
// GCNUpwardRPTracker
356+
357+
bool GCNUpwardRPTracker::recede(const MachineInstr &MI, bool ShouldTrackIt) {
358358
assert(MRI && "call reset first");
359359

360-
LastTrackedMI = &MI;
360+
if (ShouldTrackIt)
361+
LastTrackedMI = &MI;
361362

362363
if (MI.isDebugInstr())
363-
return;
364+
return false;
364365

365366
// Kill all defs.
366367
GCNRegPressure DefPressure, ECDefPressure;
@@ -412,6 +413,7 @@ void GCNUpwardRPTracker::recede(const MachineInstr &MI) {
412413
: max(CurPressure, MaxPressure);
413414

414415
assert(CurPressure == getRegPressure(*MRI, LiveRegs));
416+
return false;
415417
}
416418

417419
////////////////////////////////////////////////////////////////////////////////
@@ -430,28 +432,44 @@ bool GCNDownwardRPTracker::reset(const MachineInstr &MI,
430432
return true;
431433
}
432434

433-
bool GCNDownwardRPTracker::advanceBeforeNext() {
435+
bool GCNDownwardRPTracker::advanceBeforeNext(MachineInstr *MI,
436+
bool ShouldTrackIt,
437+
LiveIntervals *TheLIS) {
434438
assert(MRI && "call reset first");
435-
if (!LastTrackedMI)
436-
return NextMI == MBBEnd;
439+
SlotIndex SI;
440+
LiveIntervals *CurrLIS;
441+
MachineInstr *CurrMI;
442+
if (ShouldTrackIt) {
443+
if (!LastTrackedMI)
444+
return NextMI == MBBEnd;
445+
446+
assert(NextMI == MBBEnd || !NextMI->isDebugInstr());
447+
CurrLIS = const_cast<LiveIntervals *>(&LIS);
448+
CurrMI = const_cast<MachineInstr *>(LastTrackedMI);
449+
450+
SI = NextMI == MBBEnd
451+
? CurrLIS->getInstructionIndex(*LastTrackedMI).getDeadSlot()
452+
: CurrLIS->getInstructionIndex(*NextMI).getBaseIndex();
453+
}
437454

438-
assert(NextMI == MBBEnd || !NextMI->isDebugInstr());
455+
else { //! ShouldTrackIt
456+
CurrLIS = TheLIS;
457+
SI = CurrLIS->getInstructionIndex(*MI).getBaseIndex();
458+
CurrMI = MI;
459+
}
439460

440-
SlotIndex SI = NextMI == MBBEnd
441-
? LIS.getInstructionIndex(*LastTrackedMI).getDeadSlot()
442-
: LIS.getInstructionIndex(*NextMI).getBaseIndex();
443461
assert(SI.isValid());
444462

445463
// Remove dead registers or mask bits.
446464
SmallSet<Register, 8> SeenRegs;
447-
for (auto &MO : LastTrackedMI->operands()) {
465+
for (auto &MO : CurrMI->operands()) {
448466
if (!MO.isReg() || !MO.getReg().isVirtual())
449467
continue;
450468
if (MO.isUse() && !MO.readsReg())
451469
continue;
452470
if (!SeenRegs.insert(MO.getReg()).second)
453471
continue;
454-
const LiveInterval &LI = LIS.getInterval(MO.getReg());
472+
const LiveInterval &LI = CurrLIS->getInterval(MO.getReg());
455473
if (LI.hasSubRanges()) {
456474
auto It = LiveRegs.end();
457475
for (const auto &S : LI.subranges()) {
@@ -481,15 +499,18 @@ bool GCNDownwardRPTracker::advanceBeforeNext() {
481499

482500
LastTrackedMI = nullptr;
483501

484-
return NextMI == MBBEnd;
502+
return ShouldTrackIt && (NextMI == MBBEnd);
485503
}
486504

487-
void GCNDownwardRPTracker::advanceToNext() {
505+
void GCNDownwardRPTracker::advanceToNext(MachineInstr *MI, bool ShouldTrackIt) {
488506
LastTrackedMI = &*NextMI++;
489507
NextMI = skipDebugInstructionsForward(NextMI, MBBEnd);
490508

509+
MachineInstr *CurrMI =
510+
ShouldTrackIt ? const_cast<MachineInstr *>(LastTrackedMI) : MI;
511+
491512
// Add new registers or mask bits.
492-
for (const auto &MO : LastTrackedMI->all_defs()) {
513+
for (const auto &MO : CurrMI->all_defs()) {
493514
Register Reg = MO.getReg();
494515
if (!Reg.isVirtual())
495516
continue;
@@ -502,11 +523,12 @@ void GCNDownwardRPTracker::advanceToNext() {
502523
MaxPressure = max(MaxPressure, CurPressure);
503524
}
504525

505-
bool GCNDownwardRPTracker::advance() {
506-
if (NextMI == MBBEnd)
526+
bool GCNDownwardRPTracker::advance(MachineInstr *MI, bool ShouldTrackIt,
527+
LiveIntervals *TheLIS) {
528+
if (ShouldTrackIt && NextMI == MBBEnd)
507529
return false;
508-
advanceBeforeNext();
509-
advanceToNext();
530+
advanceBeforeNext(MI, ShouldTrackIt, TheLIS);
531+
advanceToNext(MI, ShouldTrackIt);
510532
return true;
511533
}
512534

llvm/lib/Target/AMDGPU/GCNRegPressure.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class GCNRPTracker {
160160
bool After);
161161

162162
public:
163+
// reset tracker and set live register set to the specified value.
164+
void reset(const MachineRegisterInfo &MRI_, const LiveRegSet &LiveRegs_);
165+
163166
// live regs for the current state
164167
const decltype(LiveRegs) &getLiveRegs() const { return LiveRegs; }
165168
const MachineInstr *getLastTrackedMI() const { return LastTrackedMI; }
@@ -180,12 +183,9 @@ class GCNUpwardRPTracker : public GCNRPTracker {
180183
public:
181184
GCNUpwardRPTracker(const LiveIntervals &LIS_) : GCNRPTracker(LIS_) {}
182185

183-
// reset tracker and set live register set to the specified value.
184-
void reset(const MachineRegisterInfo &MRI_, const LiveRegSet &LiveRegs_);
185-
186186
// reset tracker at the specified slot index.
187187
void reset(const MachineRegisterInfo &MRI, SlotIndex SI) {
188-
reset(MRI, llvm::getLiveRegs(SI, LIS, MRI));
188+
GCNRPTracker::reset(MRI, llvm::getLiveRegs(SI, LIS, MRI));
189189
}
190190

191191
// reset tracker to the end of the MBB.
@@ -200,7 +200,7 @@ class GCNUpwardRPTracker : public GCNRPTracker {
200200
}
201201

202202
// move to the state just before the MI (in program order).
203-
void recede(const MachineInstr &MI);
203+
bool recede(const MachineInstr &MI, bool ShouldTrackIt = true);
204204

205205
// checks whether the tracker's state after receding MI corresponds
206206
// to reported by LIS.
@@ -242,13 +242,15 @@ class GCNDownwardRPTracker : public GCNRPTracker {
242242

243243
// Move to the state right before the next MI or after the end of MBB.
244244
// Returns false if reached end of the block.
245-
bool advanceBeforeNext();
245+
bool advanceBeforeNext(MachineInstr *MI = nullptr, bool ShouldTrackIt = true,
246+
LiveIntervals *TheLIS = nullptr);
246247

247248
// Move to the state at the MI, advanceBeforeNext has to be called first.
248-
void advanceToNext();
249+
void advanceToNext(MachineInstr *MI = nullptr, bool ShouldTrackIt = true);
249250

250251
// Move to the state at the next MI. Returns false if reached end of block.
251-
bool advance();
252+
bool advance(MachineInstr *MI = nullptr, bool ShouldTrackIt = true,
253+
LiveIntervals *TheLIS = nullptr);
252254

253255
// Advance instructions until before End.
254256
bool advance(MachineBasicBlock::const_iterator End);

0 commit comments

Comments
 (0)