@@ -40,24 +40,25 @@ namespace {
4040class 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
6263public:
6364 bool run (MachineFunction &MF);
@@ -115,10 +116,11 @@ MachineLateInstrsCleanupPass::run(MachineFunction &MF,
115116bool 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
157167void 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,
196203bool 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
0 commit comments