Skip to content

Commit b505c76

Browse files
committed
RegisterPressure: Expose RegisterOperands API
Previously the RegisterOperands have only been used internally in RegisterPressure.cpp. However this datastructure can be useful for other tasks as well and allows refactoring of PDiff initialisation out of RPTracker::recede(). This patch: - Exposes RegisterOperands as public API - Splits RPTracker::recede() into a part that skips DebugValues and maintains the region borders, and the core that changes register pressure when given a set of RegisterOperands. - This allows to move the PDiff initialisation out recede() into a method of the PressureDiffs class. - The upcoming subregister scheduling code will also use RegisterOperands to avoid pushing more unrelated functionality into recede()/advance(). Differential Revision: http://reviews.llvm.org/D15473 llvm-svn: 257535
1 parent 9aae445 commit b505c76

File tree

3 files changed

+102
-67
lines changed

3 files changed

+102
-67
lines changed

llvm/include/llvm/CodeGen/RegisterPressure.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,28 @@ class PressureDiff {
141141
LLVM_DUMP_METHOD void dump(const TargetRegisterInfo &TRI) const;
142142
};
143143

144+
/// List of registers defined and used by a machine instruction.
145+
class RegisterOperands {
146+
public:
147+
/// List of virtual regiserts and register units read by the instruction.
148+
SmallVector<unsigned, 8> Uses;
149+
/// \brief List of virtual registers and register units defined by the
150+
/// instruction which are not dead.
151+
SmallVector<unsigned, 8> Defs;
152+
/// \brief List of virtual registers and register units defined by the
153+
/// instruction but dead.
154+
SmallVector<unsigned, 8> DeadDefs;
155+
156+
/// Analyze the given instruction \p MI and fill in the Uses, Defs and
157+
/// DeadDefs list based on the MachineOperand flags.
158+
void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI,
159+
const MachineRegisterInfo &MRI, bool IgnoreDead = false);
160+
161+
/// Use liveness information to find dead defs not marked with a dead flag
162+
/// and move them to the DeadDefs vector.
163+
void detectDeadDefs(const MachineInstr &MI, const LiveIntervals &LIS);
164+
};
165+
144166
/// Array of PressureDiffs.
145167
class PressureDiffs {
146168
PressureDiff *PDiffArray;
@@ -161,6 +183,10 @@ class PressureDiffs {
161183
const PressureDiff &operator[](unsigned Idx) const {
162184
return const_cast<PressureDiffs*>(this)->operator[](Idx);
163185
}
186+
/// \brief Record pressure difference induced by the given operand list to
187+
/// node with index \p Idx.
188+
void addInstruction(unsigned Idx, const RegisterOperands &RegOpers,
189+
const MachineRegisterInfo &MRI);
164190
};
165191

166192
/// Store the effects of a change in pressure on things that MI scheduler cares
@@ -329,8 +355,17 @@ class RegPressureTracker {
329355
void setPos(MachineBasicBlock::const_iterator Pos) { CurrPos = Pos; }
330356

331357
/// Recede across the previous instruction.
332-
void recede(SmallVectorImpl<unsigned> *LiveUses = nullptr,
333-
PressureDiff *PDiff = nullptr);
358+
void recede(SmallVectorImpl<unsigned> *LiveUses = nullptr);
359+
360+
/// Recede across the previous instruction.
361+
/// This "low-level" variant assumes that recedeSkipDebugValues() was
362+
/// called previously and takes precomputed RegisterOperands for the
363+
/// instruction.
364+
void recede(const RegisterOperands &RegOpers,
365+
SmallVectorImpl<unsigned> *LiveUses = nullptr);
366+
367+
/// Recede until we find an instruction which is not a DebugValue.
368+
void recedeSkipDebugValues();
334369

335370
/// Advance across the current instruction.
336371
void advance();

llvm/lib/CodeGen/RegisterPressure.cpp

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,6 @@ static bool containsReg(ArrayRef<unsigned> RegUnits, unsigned RegUnit) {
313313

314314
namespace {
315315

316-
/// List of register defined and used by a machine instruction.
317-
class RegisterOperands {
318-
public:
319-
SmallVector<unsigned, 8> Uses;
320-
SmallVector<unsigned, 8> Defs;
321-
SmallVector<unsigned, 8> DeadDefs;
322-
323-
void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI,
324-
const MachineRegisterInfo &MRI, bool IgnoreDead = false);
325-
326-
/// Use liveness information to find dead defs not marked with a dead flag
327-
/// and move them to the DeadDefs vector.
328-
void detectDeadDefs(const MachineInstr &MI, const LiveIntervals &LIS);
329-
};
330-
331316
/// Collect this instruction's unique uses and defs into SmallVectors for
332317
/// processing defs and uses in order.
333318
///
@@ -385,9 +370,11 @@ class RegisterOperandsCollector {
385370
}
386371
}
387372

388-
friend class RegisterOperands;
373+
friend class llvm::RegisterOperands;
389374
};
390375

376+
} // namespace
377+
391378
void RegisterOperands::collect(const MachineInstr &MI,
392379
const TargetRegisterInfo &TRI,
393380
const MachineRegisterInfo &MRI,
@@ -417,8 +404,6 @@ void RegisterOperands::detectDeadDefs(const MachineInstr &MI,
417404
}
418405
}
419406

420-
} // namespace
421-
422407
/// Initialize an array of N PressureDiffs.
423408
void PressureDiffs::init(unsigned N) {
424409
Size = N;
@@ -431,6 +416,18 @@ void PressureDiffs::init(unsigned N) {
431416
PDiffArray = reinterpret_cast<PressureDiff*>(calloc(N, sizeof(PressureDiff)));
432417
}
433418

419+
void PressureDiffs::addInstruction(unsigned Idx,
420+
const RegisterOperands &RegOpers,
421+
const MachineRegisterInfo &MRI) {
422+
PressureDiff &PDiff = (*this)[Idx];
423+
assert(!PDiff.begin()->isValid() && "stale PDiff");
424+
for (unsigned Reg : RegOpers.Defs)
425+
PDiff.addPressureChange(Reg, true, &MRI);
426+
427+
for (unsigned Reg : RegOpers.Uses)
428+
PDiff.addPressureChange(Reg, false, &MRI);
429+
}
430+
434431
/// Add a change in pressure to the pressure diff of a given instruction.
435432
void PressureDiff::addPressureChange(unsigned RegUnit, bool IsDec,
436433
const MachineRegisterInfo *MRI) {
@@ -467,18 +464,6 @@ void PressureDiff::addPressureChange(unsigned RegUnit, bool IsDec,
467464
}
468465
}
469466

470-
/// Record the pressure difference induced by the given operand list.
471-
static void collectPDiff(PressureDiff &PDiff, RegisterOperands &RegOpers,
472-
const MachineRegisterInfo *MRI) {
473-
assert(!PDiff.begin()->isValid() && "stale PDiff");
474-
475-
for (unsigned Reg : RegOpers.Defs)
476-
PDiff.addPressureChange(Reg, true, MRI);
477-
478-
for (unsigned Reg : RegOpers.Uses)
479-
PDiff.addPressureChange(Reg, false, MRI);
480-
}
481-
482467
/// Force liveness of registers.
483468
void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) {
484469
for (unsigned Reg : Regs) {
@@ -514,39 +499,10 @@ void RegPressureTracker::discoverLiveOut(unsigned Reg) {
514499
/// registers that are both defined and used by the instruction. If a pressure
515500
/// difference pointer is provided record the changes is pressure caused by this
516501
/// instruction independent of liveness.
517-
void RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses,
518-
PressureDiff *PDiff) {
519-
assert(CurrPos != MBB->begin());
520-
if (!isBottomClosed())
521-
closeBottom();
522-
523-
// Open the top of the region using block iterators.
524-
if (!RequireIntervals && isTopClosed())
525-
static_cast<RegionPressure&>(P).openTop(CurrPos);
526-
527-
// Find the previous instruction.
528-
do
529-
--CurrPos;
530-
while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
502+
void RegPressureTracker::recede(const RegisterOperands &RegOpers,
503+
SmallVectorImpl<unsigned> *LiveUses) {
531504
assert(!CurrPos->isDebugValue());
532505

533-
SlotIndex SlotIdx;
534-
if (RequireIntervals)
535-
SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
536-
537-
// Open the top of the region using slot indexes.
538-
if (RequireIntervals && isTopClosed())
539-
static_cast<IntervalPressure&>(P).openTop(SlotIdx);
540-
541-
const MachineInstr &MI = *CurrPos;
542-
RegisterOperands RegOpers;
543-
RegOpers.collect(MI, *TRI, *MRI);
544-
if (RequireIntervals)
545-
RegOpers.detectDeadDefs(MI, *LIS);
546-
547-
if (PDiff)
548-
collectPDiff(*PDiff, RegOpers, MRI);
549-
550506
// Boost pressure for all dead defs together.
551507
increaseRegPressure(RegOpers.DeadDefs);
552508
decreaseRegPressure(RegOpers.DeadDefs);
@@ -560,6 +516,10 @@ void RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses,
560516
discoverLiveOut(Reg);
561517
}
562518

519+
SlotIndex SlotIdx;
520+
if (RequireIntervals)
521+
SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
522+
563523
// Generate liveness for uses.
564524
for (unsigned Reg : RegOpers.Uses) {
565525
if (!LiveRegs.contains(Reg)) {
@@ -586,6 +546,41 @@ void RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses,
586546
}
587547
}
588548

549+
void RegPressureTracker::recedeSkipDebugValues() {
550+
assert(CurrPos != MBB->begin());
551+
if (!isBottomClosed())
552+
closeBottom();
553+
554+
// Open the top of the region using block iterators.
555+
if (!RequireIntervals && isTopClosed())
556+
static_cast<RegionPressure&>(P).openTop(CurrPos);
557+
558+
// Find the previous instruction.
559+
do
560+
--CurrPos;
561+
while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
562+
563+
SlotIndex SlotIdx;
564+
if (RequireIntervals)
565+
SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
566+
567+
// Open the top of the region using slot indexes.
568+
if (RequireIntervals && isTopClosed())
569+
static_cast<IntervalPressure&>(P).openTop(SlotIdx);
570+
}
571+
572+
void RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses) {
573+
recedeSkipDebugValues();
574+
575+
const MachineInstr &MI = *CurrPos;
576+
RegisterOperands RegOpers;
577+
RegOpers.collect(MI, *TRI, *MRI);
578+
if (RequireIntervals)
579+
RegOpers.detectDeadDefs(MI, *LIS);
580+
581+
recede(RegOpers, LiveUses);
582+
}
583+
589584
/// Advance across the current instruction.
590585
void RegPressureTracker::advance() {
591586
assert(!TrackUntiedDefs && "unsupported mode");

llvm/lib/CodeGen/ScheduleDAGInstrs.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,16 @@ void ScheduleDAGInstrs::buildSchedGraph(AliasAnalysis *AA,
896896
assert(SU && "No SUnit mapped to this MI");
897897

898898
if (RPTracker) {
899-
PressureDiff *PDiff = PDiffs ? &(*PDiffs)[SU->NodeNum] : nullptr;
900-
RPTracker->recede(/*LiveUses=*/nullptr, PDiff);
901-
assert(RPTracker->getPos() == std::prev(MII) &&
902-
"RPTracker can't find MI");
903899
collectVRegUses(SU);
900+
901+
RegisterOperands RegOpers;
902+
RegOpers.collect(*MI, *TRI, MRI);
903+
if (PDiffs != nullptr)
904+
PDiffs->addInstruction(SU->NodeNum, RegOpers, MRI);
905+
906+
RPTracker->recedeSkipDebugValues();
907+
assert(&*RPTracker->getPos() == MI && "RPTracker in sync");
908+
RPTracker->recede(RegOpers);
904909
}
905910

906911
assert(

0 commit comments

Comments
 (0)