Skip to content

Commit 76cfdbe

Browse files
committed
[RegScavenger] Remove support for forwards register scavenging
Differential Revision: https://reviews.llvm.org/D156987
1 parent 25ec267 commit 76cfdbe

File tree

3 files changed

+8
-180
lines changed

3 files changed

+8
-180
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ Changes to the CodeGen infrastructure
127127
during forwards frame index elimination. Targets should use
128128
backwards frame index elimination instead.
129129

130+
* ``RegScavenger`` no longer supports forwards register
131+
scavenging. Clients should use backwards register scavenging
132+
instead, which is preferred because it does not depend on accurate
133+
kill flags.
134+
130135
Changes to the Metadata Info
131136
---------------------------------
132137

llvm/include/llvm/CodeGen/RegisterScavenging.h

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class RegScavenger {
3737
MachineRegisterInfo *MRI = nullptr;
3838
MachineBasicBlock *MBB = nullptr;
3939
MachineBasicBlock::iterator MBBI;
40-
unsigned NumRegUnits = 0;
4140

4241
/// True if RegScavenger is currently tracking the liveness of registers.
4342
bool Tracking = false;
@@ -62,11 +61,6 @@ class RegScavenger {
6261

6362
LiveRegUnits LiveUnits;
6463

65-
// These BitVectors are only used internally to forward(). They are members
66-
// to avoid frequent reallocations.
67-
BitVector KillRegUnits, DefRegUnits;
68-
BitVector TmpRegUnits;
69-
7064
public:
7165
RegScavenger() = default;
7266

@@ -94,24 +88,11 @@ class RegScavenger {
9488
void enterBasicBlock(MachineBasicBlock &MBB);
9589

9690
/// Start tracking liveness from the end of basic block \p MBB.
97-
/// Use backward() to move towards the beginning of the block. This is
98-
/// preferred to enterBasicBlock() and forward() because it does not depend
99-
/// on the presence of kill flags.
91+
/// Use backward() to move towards the beginning of the block.
10092
void enterBasicBlockEnd(MachineBasicBlock &MBB);
10193

102-
/// Move the internal MBB iterator and update register states.
103-
void forward();
104-
105-
/// Move the internal MBB iterator and update register states until
106-
/// it has processed the specific iterator.
107-
void forward(MachineBasicBlock::iterator I) {
108-
while (!Tracking || MBBI != I)
109-
forward();
110-
}
111-
112-
/// Update internal register state and move MBB iterator backwards.
113-
/// Contrary to unprocess() this method gives precise results even in the
114-
/// absence of kill flags.
94+
/// Update internal register state and move MBB iterator backwards. This
95+
/// method gives precise results even in the absence of kill flags.
11596
void backward();
11697

11798
/// Call backward() as long as the internal iterator does not point to \p I.
@@ -181,25 +162,6 @@ class RegScavenger {
181162
/// Returns true if a register is reserved. It is never "unused".
182163
bool isReserved(Register Reg) const { return MRI->isReserved(Reg); }
183164

184-
/// setUsed / setUnused - Mark the state of one or a number of register units.
185-
///
186-
void setUsed(const BitVector &RegUnits) {
187-
LiveUnits.addUnits(RegUnits);
188-
}
189-
void setUnused(const BitVector &RegUnits) {
190-
LiveUnits.removeUnits(RegUnits);
191-
}
192-
193-
/// Processes the current instruction and fill the KillRegUnits and
194-
/// DefRegUnits bit vectors.
195-
void determineKillsAndDefs();
196-
197-
/// Add all Reg Units that Reg contains to BV.
198-
void addRegUnits(BitVector &BV, MCRegister Reg);
199-
200-
/// Remove all Reg Units that \p Reg contains from \p BV.
201-
void removeRegUnits(BitVector &BV, MCRegister Reg);
202-
203165
/// Initialize RegisterScavenger.
204166
void init(MachineBasicBlock &MBB);
205167

llvm/lib/CodeGen/RegisterScavenging.cpp

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ void RegScavenger::init(MachineBasicBlock &MBB) {
5959
MRI = &MF.getRegInfo();
6060
LiveUnits.init(*TRI);
6161

62-
assert((NumRegUnits == 0 || NumRegUnits == TRI->getNumRegUnits()) &&
63-
"Target changed?");
64-
65-
// Self-initialize.
66-
if (!this->MBB) {
67-
NumRegUnits = TRI->getNumRegUnits();
68-
KillRegUnits.resize(NumRegUnits);
69-
DefRegUnits.resize(NumRegUnits);
70-
TmpRegUnits.resize(NumRegUnits);
71-
}
7262
this->MBB = &MBB;
7363

7464
for (ScavengedInfo &SI : Scavenged) {
@@ -95,135 +85,6 @@ void RegScavenger::enterBasicBlockEnd(MachineBasicBlock &MBB) {
9585
}
9686
}
9787

98-
void RegScavenger::addRegUnits(BitVector &BV, MCRegister Reg) {
99-
for (MCRegUnit Unit : TRI->regunits(Reg))
100-
BV.set(Unit);
101-
}
102-
103-
void RegScavenger::removeRegUnits(BitVector &BV, MCRegister Reg) {
104-
for (MCRegUnit Unit : TRI->regunits(Reg))
105-
BV.reset(Unit);
106-
}
107-
108-
void RegScavenger::determineKillsAndDefs() {
109-
assert(Tracking && "Must be tracking to determine kills and defs");
110-
111-
MachineInstr &MI = *MBBI;
112-
assert(!MI.isDebugInstr() && "Debug values have no kills or defs");
113-
114-
// Find out which registers are early clobbered, killed, defined, and marked
115-
// def-dead in this instruction.
116-
KillRegUnits.reset();
117-
DefRegUnits.reset();
118-
for (const MachineOperand &MO : MI.operands()) {
119-
if (MO.isRegMask()) {
120-
TmpRegUnits.reset();
121-
for (unsigned RU = 0, RUEnd = TRI->getNumRegUnits(); RU != RUEnd; ++RU) {
122-
for (MCRegUnitRootIterator RURI(RU, TRI); RURI.isValid(); ++RURI) {
123-
if (MO.clobbersPhysReg(*RURI)) {
124-
TmpRegUnits.set(RU);
125-
break;
126-
}
127-
}
128-
}
129-
130-
// Apply the mask.
131-
KillRegUnits |= TmpRegUnits;
132-
}
133-
if (!MO.isReg())
134-
continue;
135-
if (!MO.getReg().isPhysical() || isReserved(MO.getReg()))
136-
continue;
137-
MCRegister Reg = MO.getReg().asMCReg();
138-
139-
if (MO.isUse()) {
140-
// Ignore undef uses.
141-
if (MO.isUndef())
142-
continue;
143-
if (MO.isKill())
144-
addRegUnits(KillRegUnits, Reg);
145-
} else {
146-
assert(MO.isDef());
147-
if (MO.isDead())
148-
addRegUnits(KillRegUnits, Reg);
149-
else
150-
addRegUnits(DefRegUnits, Reg);
151-
}
152-
}
153-
}
154-
155-
void RegScavenger::forward() {
156-
// Move ptr forward.
157-
if (!Tracking) {
158-
MBBI = MBB->begin();
159-
Tracking = true;
160-
} else {
161-
assert(MBBI != MBB->end() && "Already past the end of the basic block!");
162-
MBBI = std::next(MBBI);
163-
}
164-
assert(MBBI != MBB->end() && "Already at the end of the basic block!");
165-
166-
MachineInstr &MI = *MBBI;
167-
168-
for (ScavengedInfo &I : Scavenged) {
169-
if (I.Restore != &MI)
170-
continue;
171-
172-
I.Reg = 0;
173-
I.Restore = nullptr;
174-
}
175-
176-
if (MI.isDebugOrPseudoInstr())
177-
return;
178-
179-
determineKillsAndDefs();
180-
181-
// Verify uses and defs.
182-
#ifndef NDEBUG
183-
for (const MachineOperand &MO : MI.operands()) {
184-
if (!MO.isReg())
185-
continue;
186-
Register Reg = MO.getReg();
187-
if (!Reg.isPhysical() || isReserved(Reg))
188-
continue;
189-
if (MO.isUse()) {
190-
if (MO.isUndef())
191-
continue;
192-
if (!isRegUsed(Reg)) {
193-
// Check if it's partial live: e.g.
194-
// D0 = insert_subreg undef D0, S0
195-
// ... D0
196-
// The problem is the insert_subreg could be eliminated. The use of
197-
// D0 is using a partially undef value. This is not *incorrect* since
198-
// S1 is can be freely clobbered.
199-
// Ideally we would like a way to model this, but leaving the
200-
// insert_subreg around causes both correctness and performance issues.
201-
if (none_of(TRI->subregs(Reg),
202-
[&](MCPhysReg SR) { return isRegUsed(SR); }) &&
203-
none_of(TRI->superregs(Reg),
204-
[&](MCPhysReg SR) { return isRegUsed(SR); })) {
205-
MBB->getParent()->verify(nullptr, "In Register Scavenger");
206-
llvm_unreachable("Using an undefined register!");
207-
}
208-
}
209-
} else {
210-
assert(MO.isDef());
211-
#if 0
212-
// FIXME: Enable this once we've figured out how to correctly transfer
213-
// implicit kills during codegen passes like the coalescer.
214-
assert((KillRegs.test(Reg) || isUnused(Reg) ||
215-
isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
216-
"Re-defining a live register!");
217-
#endif
218-
}
219-
}
220-
#endif // NDEBUG
221-
222-
// Commit the changes.
223-
setUnused(KillRegUnits);
224-
setUsed(DefRegUnits);
225-
}
226-
22788
void RegScavenger::backward() {
22889
assert(Tracking && "Must be tracking to determine kills and defs");
22990

0 commit comments

Comments
 (0)