@@ -39,25 +39,24 @@ namespace {
3939class MachineLateInstrsCleanup : public MachineFunctionPass {
4040 const TargetRegisterInfo *TRI = nullptr ;
4141 const TargetInstrInfo *TII = nullptr ;
42+ const MachineRegisterInfo *MRI = nullptr ;
4243
43- // Data structures to map regs to their definitions and kills per MBB.
44+ // Data structure to map regs to their definitions per MBB.
4445 struct Reg2MIMap : public SmallDenseMap <Register, MachineInstr *> {
4546 bool hasIdentical (Register Reg, MachineInstr *ArgMI) {
4647 MachineInstr *MI = lookup (Reg);
4748 return MI && MI->isIdenticalTo (*ArgMI);
4849 }
4950 };
50- typedef SmallDenseMap<Register, TinyPtrVector<MachineInstr *>> Reg2MIVecMap;
5151 std::vector<Reg2MIMap> RegDefs;
52- std::vector<Reg2MIVecMap> RegKills;
5352
5453 // Walk through the instructions in MBB and remove any redundant
5554 // instructions.
5655 bool processBlock (MachineBasicBlock *MBB);
5756
5857 void removeRedundantDef (MachineInstr *MI);
59- void clearKillsForDef (Register Reg, MachineBasicBlock *MBB,
60- BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
58+ void updateLiveInLists (Register Reg, MachineBasicBlock *MBB,
59+ BitVector &VisitedPreds, MachineInstr *ToRemoveMI);
6160
6261public:
6362 static char ID; // Pass identification, replacement for typeid
@@ -94,11 +93,10 @@ bool MachineLateInstrsCleanup::runOnMachineFunction(MachineFunction &MF) {
9493
9594 TRI = MF.getSubtarget ().getRegisterInfo ();
9695 TII = MF.getSubtarget ().getInstrInfo ();
96+ MRI = &MF.getRegInfo ();
9797
9898 RegDefs.clear ();
9999 RegDefs.resize (MF.getNumBlockIDs ());
100- RegKills.clear ();
101- RegKills.resize (MF.getNumBlockIDs ());
102100
103101 // Visit all MBBs in an order that maximises the reuse from predecessors.
104102 bool Changed = false ;
@@ -111,41 +109,36 @@ bool MachineLateInstrsCleanup::runOnMachineFunction(MachineFunction &MF) {
111109
112110// Clear any preceding kill flag on Reg after removing a redundant
113111// definition.
114- void MachineLateInstrsCleanup::clearKillsForDef (Register Reg,
112+ void MachineLateInstrsCleanup::updateLiveInLists (Register Reg,
115113 MachineBasicBlock *MBB,
116114 BitVector &VisitedPreds,
117115 MachineInstr *ToRemoveMI) {
118116 VisitedPreds.set (MBB->getNumber ());
119117
120- // Clear kill flag(s) in MBB, that have been seen after the preceding
121- // definition. If Reg or one of its subregs was killed, it would actually
122- // be ok to stop after removing that (and any other) kill-flag, but it
123- // doesn't seem noticeably faster while it would be a bit more complicated.
124- Reg2MIVecMap &MBBKills = RegKills[MBB->getNumber ()];
125- if (MBBKills.contains (Reg))
126- for (auto *KillMI : MBBKills[Reg])
127- KillMI->clearRegisterKills (Reg, TRI);
128-
129118 // Definition in current MBB: done.
130119 Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber ()];
131120 MachineInstr *DefMI = MBBDefs[Reg];
132121 assert (DefMI->isIdenticalTo (*ToRemoveMI) && " Previous def not identical?" );
133122 if (DefMI->getParent () == MBB)
134123 return ;
135124
136- // If an earlier def is not in MBB, continue in predecessors.
125+ // If the earlier def is not in MBB, it has now become live in. Continue in
126+ // predecessors until the defining MBB has been reached.
137127 if (!MBB->isLiveIn (Reg))
138128 MBB->addLiveIn (Reg);
139129 assert (!MBB->pred_empty () && " Predecessor def not found!" );
140130 for (MachineBasicBlock *Pred : MBB->predecessors ())
141131 if (!VisitedPreds.test (Pred->getNumber ()))
142- clearKillsForDef (Reg, Pred, VisitedPreds, ToRemoveMI);
132+ updateLiveInLists (Reg, Pred, VisitedPreds, ToRemoveMI);
143133}
144134
145135void MachineLateInstrsCleanup::removeRedundantDef (MachineInstr *MI) {
146136 Register Reg = MI->getOperand (0 ).getReg ();
137+ // Clear any and all kill flags.
138+ for (MCPhysReg SReg : TRI->superregs_inclusive (Reg))
139+ MRI->clearKillFlags (SReg);
147140 BitVector VisitedPreds (MI->getMF ()->getNumBlockIDs ());
148- clearKillsForDef (Reg, MI->getParent (), VisitedPreds, MI);
141+ updateLiveInLists (Reg, MI->getParent (), VisitedPreds, MI);
149142 MI->eraseFromParent ();
150143 ++NumRemoved;
151144}
@@ -181,7 +174,6 @@ static bool isCandidate(const MachineInstr *MI, Register &DefedReg,
181174bool MachineLateInstrsCleanup::processBlock (MachineBasicBlock *MBB) {
182175 bool Changed = false ;
183176 Reg2MIMap &MBBDefs = RegDefs[MBB->getNumber ()];
184- Reg2MIVecMap &MBBKills = RegKills[MBB->getNumber ()];
185177
186178 // Find reusable definitions in the predecessor(s).
187179 if (!MBB->pred_empty () && !MBB->isEHPad () &&
@@ -208,7 +200,6 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
208200 // it) are valid.
209201 if (MI.modifiesRegister (FrameReg, TRI)) {
210202 MBBDefs.clear ();
211- MBBKills.clear ();
212203 continue ;
213204 }
214205
@@ -227,20 +218,15 @@ bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) {
227218 // Clear any entries in map that MI clobbers.
228219 for (auto DefI : llvm::make_early_inc_range (MBBDefs)) {
229220 Register Reg = DefI.first ;
230- if (MI.modifiesRegister (Reg, TRI)) {
221+ if (MI.modifiesRegister (Reg, TRI))
231222 MBBDefs.erase (Reg);
232- MBBKills.erase (Reg);
233- } else if (MI.findRegisterUseOperandIdx (Reg, TRI, true /* isKill*/ ) != -1 )
234- // Keep track of all instructions that fully or partially kills Reg.
235- MBBKills[Reg].push_back (&MI);
236223 }
237224
238225 // Record this MI for potential later reuse.
239226 if (IsCandidate) {
240227 LLVM_DEBUG (dbgs () << " Found interesting instruction in "
241228 << printMBBReference (*MBB) << " : " << MI;);
242229 MBBDefs[DefedReg] = &MI;
243- assert (!MBBKills.count (DefedReg) && " Should already have been removed." );
244230 }
245231 }
246232
0 commit comments