Skip to content

Commit 1a55770

Browse files
committed
Go back to clearing of specific kill-flags.
1 parent d07f3ff commit 1a55770

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

llvm/lib/CodeGen/MachineLateInstrsCleanup.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,25 @@ namespace {
4040
class MachineLateInstrsCleanup {
4141
const TargetRegisterInfo *TRI = nullptr;
4242
const TargetInstrInfo *TII = nullptr;
43-
const MachineRegisterInfo *MRI = nullptr;
4443

45-
// Data structure to map regs to their definitions per MBB.
44+
// Data structures to map regs to their definitions and kills per MBB.
4645
struct Reg2MIMap : public SmallDenseMap<Register, MachineInstr *> {
4746
bool hasIdentical(Register Reg, MachineInstr *ArgMI) {
4847
MachineInstr *MI = lookup(Reg);
4948
return MI && MI->isIdenticalTo(*ArgMI);
5049
}
5150
};
51+
typedef SmallDenseMap<Register, TinyPtrVector<MachineInstr *>> Reg2MIVecMap;
5252
std::vector<Reg2MIMap> RegDefs;
53+
std::vector<Reg2MIVecMap> RegKills;
5354

5455
// Walk through the instructions in MBB and remove any redundant
5556
// instructions.
5657
bool processBlock(MachineBasicBlock *MBB);
5758

5859
void removeRedundantDef(MachineInstr *MI);
59-
void updateLiveInLists(Register Reg, MachineBasicBlock *MBB,
60-
BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
60+
void clearKillsForDef(Register Reg, MachineBasicBlock *MBB,
61+
BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
6162

6263
public:
6364
bool run(MachineFunction &MF);
@@ -115,10 +116,11 @@ MachineLateInstrsCleanupPass::run(MachineFunction &MF,
115116
bool MachineLateInstrsCleanup::run(MachineFunction &MF) {
116117
TRI = MF.getSubtarget().getRegisterInfo();
117118
TII = MF.getSubtarget().getInstrInfo();
118-
MRI = &MF.getRegInfo();
119119

120120
RegDefs.clear();
121121
RegDefs.resize(MF.getNumBlockIDs());
122+
RegKills.clear();
123+
RegKills.resize(MF.getNumBlockIDs());
122124

123125
// Visit all MBBs in an order that maximises the reuse from predecessors.
124126
bool Changed = false;
@@ -131,36 +133,41 @@ bool MachineLateInstrsCleanup::run(MachineFunction &MF) {
131133

132134
// Clear any preceding kill flag on Reg after removing a redundant
133135
// definition.
134-
void MachineLateInstrsCleanup::updateLiveInLists(Register Reg,
136+
void MachineLateInstrsCleanup::clearKillsForDef(Register Reg,
135137
MachineBasicBlock *MBB,
136138
BitVector &VisitedPreds,
137139
MachineInstr *ToRemoveMI) {
138140
VisitedPreds.set(MBB->getNumber());
139141

142+
// Clear kill flag(s) in MBB, that have been seen after the preceding
143+
// definition. If Reg or one of its subregs was killed, it would actually
144+
// be ok to stop after removing that (and any other) kill-flag, but it
145+
// doesn't seem noticeably faster while it would be a bit more complicated.
146+
Reg2MIVecMap &MBBKills = RegKills[MBB->getNumber()];
147+
if (MBBKills.contains(Reg))
148+
for (auto *KillMI : MBBKills[Reg])
149+
KillMI->clearRegisterKills(Reg, TRI);
150+
140151
// Definition in current MBB: done.
141152
Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber()];
142153
MachineInstr *DefMI = MBBDefs[Reg];
143154
assert(DefMI->isIdenticalTo(*ToRemoveMI) && "Previous def not identical?");
144155
if (DefMI->getParent() == MBB)
145156
return;
146157

147-
// If the earlier def is not in MBB, it has now become live in. Continue in
148-
// predecessors until the defining MBB has been reached.
158+
// If an earlier def is not in MBB, continue in predecessors.
149159
if (!MBB->isLiveIn(Reg))
150160
MBB->addLiveIn(Reg);
151161
assert(!MBB->pred_empty() && "Predecessor def not found!");
152162
for (MachineBasicBlock *Pred : MBB->predecessors())
153163
if (!VisitedPreds.test(Pred->getNumber()))
154-
updateLiveInLists(Reg, Pred, VisitedPreds, ToRemoveMI);
164+
clearKillsForDef(Reg, Pred, VisitedPreds, ToRemoveMI);
155165
}
156166

157167
void MachineLateInstrsCleanup::removeRedundantDef(MachineInstr *MI) {
158168
Register Reg = MI->getOperand(0).getReg();
159-
// Clear any and all kill flags.
160-
for (MCPhysReg SReg : TRI->superregs_inclusive(Reg))
161-
MRI->clearKillFlags(SReg);
162169
BitVector VisitedPreds(MI->getMF()->getNumBlockIDs());
163-
updateLiveInLists(Reg, MI->getParent(), VisitedPreds, MI);
170+
clearKillsForDef(Reg, MI->getParent(), VisitedPreds, MI);
164171
MI->eraseFromParent();
165172
++NumRemoved;
166173
}
@@ -196,6 +203,7 @@ static bool isCandidate(const MachineInstr *MI, Register &DefedReg,
196203
bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
197204
bool Changed = false;
198205
Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber()];
206+
Reg2MIVecMap &MBBKills = RegKills[MBB->getNumber()];
199207

200208
// Find reusable definitions in the predecessor(s).
201209
if (!MBB->pred_empty() && !MBB->isEHPad() &&
@@ -222,6 +230,7 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
222230
// it) are valid.
223231
if (MI.modifiesRegister(FrameReg, TRI)) {
224232
MBBDefs.clear();
233+
MBBKills.clear();
225234
continue;
226235
}
227236

@@ -240,15 +249,20 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
240249
// Clear any entries in map that MI clobbers.
241250
for (auto DefI : llvm::make_early_inc_range(MBBDefs)) {
242251
Register Reg = DefI.first;
243-
if (MI.modifiesRegister(Reg, TRI))
252+
if (MI.modifiesRegister(Reg, TRI)) {
244253
MBBDefs.erase(Reg);
254+
MBBKills.erase(Reg);
255+
} else if (MI.findRegisterUseOperandIdx(Reg, TRI, true /*isKill*/) != -1)
256+
// Keep track of all instructions that fully or partially kills Reg.
257+
MBBKills[Reg].push_back(&MI);
245258
}
246259

247260
// Record this MI for potential later reuse.
248261
if (IsCandidate) {
249262
LLVM_DEBUG(dbgs() << "Found interesting instruction in "
250263
<< printMBBReference(*MBB) << ": " << MI);
251264
MBBDefs[DefedReg] = &MI;
265+
assert(!MBBKills.count(DefedReg) && "Should already have been removed.");
252266
}
253267
}
254268

llvm/test/CodeGen/SystemZ/machine-latecleanup-kills.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ body: |
1919
; CHECK-NEXT: {{ $}}
2020
; CHECK-NEXT: renamable $r1d = LGFI 0
2121
; CHECK-NEXT: ST128 renamable $r0q, $r15d, 168, $noreg
22-
; CHECK-NEXT: ST renamable $r0l, $r15d, 160, $noreg
22+
; CHECK-NEXT: ST killed renamable $r0l, $r15d, 160, $noreg
2323
; CHECK-NEXT: Return
2424
bb.0:
2525
renamable $r0l = LHIMux -1
@@ -44,7 +44,7 @@ body: |
4444
; CHECK-NEXT: STG renamable $r1d, killed $r15d, 8, $noreg
4545
; CHECK-NEXT: renamable $r0d = LLILL 0
4646
; CHECK-NEXT: ST128 renamable $r0q, $r15d, 0, $noreg
47-
; CHECK-NEXT: STG renamable $r1d, killed $r15d, 8, $noreg
47+
; CHECK-NEXT: STG killed renamable $r1d, killed $r15d, 8, $noreg
4848
; CHECK-NEXT: Return
4949
renamable $r1d = LLILL 1
5050
STG killed renamable $r1d, killed $r15d, 8, $noreg

0 commit comments

Comments
 (0)