From fff3e191b959bfc00e266b47f5c142464bb50ebf Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Tue, 14 Aug 2007 09:11:18 +0000 Subject: [PATCH] If a spilled value is being reused and the use is a kill, that means there are no more uses within the MBB and the spilled value isn't live out of the MBB. Then it's safe to delete the spill store. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41069 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/VirtRegMap.cpp | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/CodeGen/VirtRegMap.cpp b/lib/CodeGen/VirtRegMap.cpp index e996374367f..fa7951eeea4 100644 --- a/lib/CodeGen/VirtRegMap.cpp +++ b/lib/CodeGen/VirtRegMap.cpp @@ -554,7 +554,7 @@ namespace { /// a new register to use, or evict the previous reload and use this reg. unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI, AvailableSpills &Spills, - std::map &MaybeDeadStores, + std::vector &MaybeDeadStores, SmallSet &Rejected, BitVector &RegKills, std::vector &KillOps, @@ -609,14 +609,13 @@ namespace { } else { MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg, NewOp.StackSlotOrReMat, AliasRC); + // Any stores to this stack slot are not dead anymore. + MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL; ++NumLoads; } Spills.ClobberPhysReg(NewPhysReg); Spills.ClobberPhysReg(NewOp.PhysRegReused); - // Any stores to this stack slot are not dead anymore. - MaybeDeadStores.erase(NewOp.StackSlotOrReMat); - MI->getOperand(NewOp.Operand).setReg(NewPhysReg); Spills.addAvailable(NewOp.StackSlotOrReMat, MI, NewPhysReg); @@ -649,7 +648,7 @@ namespace { /// sees r1 is taken by t2, tries t2's reload register r0 ... unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI, AvailableSpills &Spills, - std::map &MaybeDeadStores, + std::vector &MaybeDeadStores, BitVector &RegKills, std::vector &KillOps, VirtRegMap &VRM) { @@ -666,6 +665,8 @@ namespace { void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { DOUT << MBB.getBasicBlock()->getName() << ":\n"; + MachineFunction &MF = *MBB.getParent(); + // Spills - Keep track of which spilled values are available in physregs so // that we can choose to reuse the physregs instead of emitting reloads. AvailableSpills Spills(MRI, TII); @@ -676,14 +677,14 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { // 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 MaybeDeadStores; + std::vector MaybeDeadStores; + MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL); // Keep track of kill information. BitVector RegKills(MRI->getNumRegs()); std::vector KillOps; KillOps.resize(MRI->getNumRegs(), NULL); - MachineFunction &MF = *MBB.getParent(); for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end(); MII != E; ) { MachineInstr &MI = *MII; @@ -806,6 +807,21 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { // Only mark it clobbered if this is a use&def operand. ReusedOperands.markClobbered(PhysReg); ++NumReused; + + if (MI.getOperand(i).isKill() && + ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) { + // This was the last use and the spilled value is still available + // for reuse. That means the spill was unnecessary! + MachineInstr* DeadStore = MaybeDeadStores[ReuseSlot]; + if (DeadStore) { + DOUT << "Removed dead store:\t" << *DeadStore; + InvalidateKills(*DeadStore, RegKills, KillOps); + MBB.erase(DeadStore); + VRM.RemoveFromFoldedVirtMap(DeadStore); + MaybeDeadStores[ReuseSlot] = NULL; + ++NumDSE; + } + } continue; } @@ -892,7 +908,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { // Any stores to this stack slot are not dead anymore. if (!DoReMat) - MaybeDeadStores.erase(SSorRMId); + MaybeDeadStores[SSorRMId] = NULL; Spills.addAvailable(SSorRMId, &MI, PhysReg); // Assumes this is the last use. IsKill will be unset if reg is reused // unless it's a two-address operand. @@ -953,20 +969,18 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { // If this reference is not a use, any previous store is now dead. // Otherwise, the store to this stack slot is not dead anymore. - std::map::iterator MDSI = MaybeDeadStores.find(SS); - if (MDSI != MaybeDeadStores.end()) { - if (MR & VirtRegMap::isRef) // Previous store is not dead. - MaybeDeadStores.erase(MDSI); - else { + MachineInstr* DeadStore = MaybeDeadStores[SS]; + if (DeadStore) { + if (!(MR & VirtRegMap::isRef)) { // Previous store is dead. // If we get here, the store is dead, nuke it now. assert(VirtRegMap::isMod && "Can't be modref!"); - DOUT << "Removed dead store:\t" << *MDSI->second; - InvalidateKills(*MDSI->second, RegKills, KillOps); - MBB.erase(MDSI->second); - VRM.RemoveFromFoldedVirtMap(MDSI->second); - MaybeDeadStores.erase(MDSI); + DOUT << "Removed dead store:\t" << *DeadStore; + InvalidateKills(*DeadStore, RegKills, KillOps); + MBB.erase(DeadStore); + VRM.RemoveFromFoldedVirtMap(DeadStore); ++NumDSE; } + MaybeDeadStores[SS] = NULL; } // If the spill slot value is available, and this is a new definition of -- 2.34.1