Statistic<> NumStores("spiller", "Number of stores added");
Statistic<> NumLoads ("spiller", "Number of loads added");
Statistic<> NumReused("spiller", "Number of values reused");
+ Statistic<> NumDSE ("spiller", "Number of dead stores elided");
enum SpillerName { simple, local };
// and ".second" is the virtual register that is spilled.
std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
+ // MaybeDeadStores - When we need to write a value back into a stack slot,
+ // keep track of the inserted store. If the stack slot value is never read
+ // (because the value was used from some available register, for example), and
+ // subsequently stored to, the original store is dead. This map keeps track
+ // of inserted stores that are not used. If we see a subsequent store to the
+ // same stack slot, the original store is deleted.
+ std::map<int, MachineInstr*> MaybeDeadStores;
+
for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
MII != E; ) {
MachineInstr &MI = *MII;
ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
PhysRegsAvailable);
+ // Any stores to this stack slot are not dead anymore.
+ MaybeDeadStores.erase(Op.StackSlot);
+
MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot;
SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
// This invalidates PhysReg.
ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
+ // Any stores to this stack slot are not dead anymore.
+ MaybeDeadStores.erase(StackSlot);
+
MI.SetMachineOperandReg(i, PhysReg);
PhysRegsAvailable[PhysReg] = StackSlot;
SpillSlotsAvailable[StackSlot] = PhysReg;
int SS = VRM.getStackSlot(I->second);
DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
+ // Any stores to this stack slot are not dead anymore.
+ MaybeDeadStores.erase(SS);
+
std::map<int, unsigned>::iterator I = SpillSlotsAvailable.find(SS);
if (I != SpillSlotsAvailable.end()) {
PhysRegsAvailable.erase(I->second);
DEBUG(std::cerr << "Store:\t" << *next(MII));
MI.SetMachineOperandReg(i, PhysReg);
+ // If there is a dead store to this stack slot, nuke it now.
+ MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
+ if (LastStore) {
+ ++NumDSE;
+ MBB.erase(LastStore);
+ }
+ LastStore = next(MII);
+
// If the stack slot value was previously available in some other
// register, change it now. Otherwise, make the register available,
// in PhysReg.