@@ -40,25 +40,24 @@ namespace {
4040class MachineLateInstrsCleanup {
4141 const TargetRegisterInfo *TRI = nullptr ;
4242 const TargetInstrInfo *TII = nullptr ;
43+ const MachineRegisterInfo *MRI = nullptr ;
4344
44- // Data structures to map regs to their definitions and kills per MBB.
45+ // Data structure to map regs to their definitions per MBB.
4546 struct Reg2MIMap : public SmallDenseMap <Register, MachineInstr *> {
4647 bool hasIdentical (Register Reg, MachineInstr *ArgMI) {
4748 MachineInstr *MI = lookup (Reg);
4849 return MI && MI->isIdenticalTo (*ArgMI);
4950 }
5051 };
51- typedef SmallDenseMap<Register, TinyPtrVector<MachineInstr *>> Reg2MIVecMap;
5252 std::vector<Reg2MIMap> RegDefs;
53- std::vector<Reg2MIVecMap> RegKills;
5453
5554 // Walk through the instructions in MBB and remove any redundant
5655 // instructions.
5756 bool processBlock (MachineBasicBlock *MBB);
5857
5958 void removeRedundantDef (MachineInstr *MI);
60- void clearKillsForDef (Register Reg, MachineBasicBlock *MBB,
61- BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
59+ void updateLiveInLists (Register Reg, MachineBasicBlock *MBB,
60+ BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
6261
6362public:
6463 bool run (MachineFunction &MF);
@@ -116,11 +115,10 @@ MachineLateInstrsCleanupPass::run(MachineFunction &MF,
116115bool MachineLateInstrsCleanup::run (MachineFunction &MF) {
117116 TRI = MF.getSubtarget ().getRegisterInfo ();
118117 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 ());
124122
125123 // Visit all MBBs in an order that maximises the reuse from predecessors.
126124 bool Changed = false ;
@@ -133,41 +131,36 @@ bool MachineLateInstrsCleanup::run(MachineFunction &MF) {
133131
134132// Clear any preceding kill flag on Reg after removing a redundant
135133// definition.
136- void MachineLateInstrsCleanup::clearKillsForDef (Register Reg,
134+ void MachineLateInstrsCleanup::updateLiveInLists (Register Reg,
137135 MachineBasicBlock *MBB,
138136 BitVector &VisitedPreds,
139137 MachineInstr *ToRemoveMI) {
140138 VisitedPreds.set (MBB->getNumber ());
141139
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-
151140 // Definition in current MBB: done.
152141 Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber ()];
153142 MachineInstr *DefMI = MBBDefs[Reg];
154143 assert (DefMI->isIdenticalTo (*ToRemoveMI) && " Previous def not identical?" );
155144 if (DefMI->getParent () == MBB)
156145 return ;
157146
158- // If an earlier def is not in MBB, continue in predecessors.
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.
159149 if (!MBB->isLiveIn (Reg))
160150 MBB->addLiveIn (Reg);
161151 assert (!MBB->pred_empty () && " Predecessor def not found!" );
162152 for (MachineBasicBlock *Pred : MBB->predecessors ())
163153 if (!VisitedPreds.test (Pred->getNumber ()))
164- clearKillsForDef (Reg, Pred, VisitedPreds, ToRemoveMI);
154+ updateLiveInLists (Reg, Pred, VisitedPreds, ToRemoveMI);
165155}
166156
167157void MachineLateInstrsCleanup::removeRedundantDef (MachineInstr *MI) {
168158 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);
169162 BitVector VisitedPreds (MI->getMF ()->getNumBlockIDs ());
170- clearKillsForDef (Reg, MI->getParent (), VisitedPreds, MI);
163+ updateLiveInLists (Reg, MI->getParent (), VisitedPreds, MI);
171164 MI->eraseFromParent ();
172165 ++NumRemoved;
173166}
@@ -203,7 +196,6 @@ static bool isCandidate(const MachineInstr *MI, Register &DefedReg,
203196bool MachineLateInstrsCleanup::processBlock (MachineBasicBlock *MBB) {
204197 bool Changed = false ;
205198 Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber ()];
206- Reg2MIVecMap &MBBKills = RegKills[MBB->getNumber ()];
207199
208200 // Find reusable definitions in the predecessor(s).
209201 if (!MBB->pred_empty () && !MBB->isEHPad () &&
@@ -230,7 +222,6 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
230222 // it) are valid.
231223 if (MI.modifiesRegister (FrameReg, TRI)) {
232224 MBBDefs.clear ();
233- MBBKills.clear ();
234225 continue ;
235226 }
236227
@@ -249,20 +240,15 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
249240 // Clear any entries in map that MI clobbers.
250241 for (auto DefI : llvm::make_early_inc_range (MBBDefs)) {
251242 Register Reg = DefI.first ;
252- if (MI.modifiesRegister (Reg, TRI)) {
243+ if (MI.modifiesRegister (Reg, TRI))
253244 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);
258245 }
259246
260247 // Record this MI for potential later reuse.
261248 if (IsCandidate) {
262249 LLVM_DEBUG (dbgs () << " Found interesting instruction in "
263250 << printMBBReference (*MBB) << " : " << MI);
264251 MBBDefs[DefedReg] = &MI;
265- assert (!MBBKills.count (DefedReg) && " Should already have been removed." );
266252 }
267253 }
268254
0 commit comments