[llvm-commits] [llvm] r127287 - in /llvm/trunk/lib/CodeGen: LiveRangeEdit.cpp LiveRangeEdit.h SplitKit.cpp SplitKit.h

Jakob Stoklund Olesen stoklund at 2pi.dk
Tue Mar 8 14:46:11 PST 2011


Author: stoklund
Date: Tue Mar  8 16:46:11 2011
New Revision: 127287

URL: http://llvm.org/viewvc/llvm-project?rev=127287&view=rev
Log:
Delete dead code after rematerializing.

LiveRangeEdit::eliminateDeadDefs() will eventually be used by coalescing,
splitting, and spilling for dead code elimination. It can delete chains of dead
instructions as long as there are no dependency loops.

Modified:
    llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp
    llvm/trunk/lib/CodeGen/LiveRangeEdit.h
    llvm/trunk/lib/CodeGen/SplitKit.cpp
    llvm/trunk/lib/CodeGen/SplitKit.h

Modified: llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp?rev=127287&r1=127286&r2=127287&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp (original)
+++ llvm/trunk/lib/CodeGen/LiveRangeEdit.cpp Tue Mar  8 16:46:11 2011
@@ -13,9 +13,12 @@
 
 #include "LiveRangeEdit.h"
 #include "VirtRegMap.h"
+#include "llvm/ADT/SetVector.h"
 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 
@@ -128,3 +131,59 @@
   return lis.InsertMachineInstrInMaps(--MI).getDefIndex();
 }
 
+void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
+                                      LiveIntervals &LIS,
+                                      const TargetInstrInfo &TII) {
+  SetVector<LiveInterval*,
+            SmallVector<LiveInterval*, 8>,
+            SmallPtrSet<LiveInterval*, 8> > ToShrink;
+
+  for (;;) {
+    // Erase all dead defs.
+    while (!Dead.empty()) {
+      MachineInstr *MI = Dead.pop_back_val();
+      assert(MI->allDefsAreDead() && "Def isn't really dead");
+
+      // Never delete inline asm.
+      if (MI->isInlineAsm())
+        continue;
+
+      // Use the same criteria as DeadMachineInstructionElim.
+      bool SawStore = false;
+      if (!MI->isSafeToMove(&TII, 0, SawStore))
+        continue;
+
+      SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
+      DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
+
+      // Check for live intervals that may shrink
+      for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
+             MOE = MI->operands_end(); MOI != MOE; ++MOI) {
+        if (!MOI->isReg())
+          continue;
+        unsigned Reg = MOI->getReg();
+        if (!TargetRegisterInfo::isVirtualRegister(Reg))
+          continue;
+        LiveInterval &LI = LIS.getInterval(Reg);
+        // Remove defined value.
+        if (MOI->isDef())
+          if (VNInfo *VNI = LI.getVNInfoAt(Idx))
+            LI.removeValNo(VNI);
+        // Shrink read registers.
+        if (MI->readsVirtualRegister(Reg))
+          ToShrink.insert(&LI);
+      }
+
+      LIS.RemoveMachineInstrFromMaps(MI);
+      MI->eraseFromParent();
+    }
+
+    if (ToShrink.empty())
+      break;
+
+    // Shrink just one live interval. Then delete new dead defs.
+    LIS.shrinkToUses(ToShrink.back(), &Dead);
+    ToShrink.pop_back();
+  }
+}
+

Modified: llvm/trunk/lib/CodeGen/LiveRangeEdit.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LiveRangeEdit.h?rev=127287&r1=127286&r2=127287&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/LiveRangeEdit.h (original)
+++ llvm/trunk/lib/CodeGen/LiveRangeEdit.h Tue Mar  8 16:46:11 2011
@@ -128,6 +128,14 @@
   bool didRematerialize(const VNInfo *ParentVNI) const {
     return rematted_.count(ParentVNI);
   }
+
+  /// eliminateDeadDefs - Try to delete machine instructions that are now dead
+  /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
+  /// and further dead efs to be eliminated.
+  void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
+                         LiveIntervals&,
+                         const TargetInstrInfo&);
+
 };
 
 }

Modified: llvm/trunk/lib/CodeGen/SplitKit.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.cpp?rev=127287&r1=127286&r2=127287&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SplitKit.cpp (original)
+++ llvm/trunk/lib/CodeGen/SplitKit.cpp Tue Mar  8 16:46:11 2011
@@ -797,6 +797,40 @@
   }
 }
 
+void SplitEditor::deleteRematVictims() {
+  SmallVector<MachineInstr*, 8> Dead;
+  for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
+         E = Edit->getParent().vni_end(); I != E; ++I) {
+    const VNInfo *VNI = *I;
+    // Was VNI rematted anywhere?
+    if (VNI->isUnused() || VNI->isPHIDef() || !Edit->didRematerialize(VNI))
+      continue;
+    unsigned RegIdx = RegAssign.lookup(VNI->def);
+    LiveInterval *LI = Edit->get(RegIdx);
+    LiveInterval::const_iterator LII = LI->FindLiveRangeContaining(VNI->def);
+    assert(LII != LI->end() && "Missing live range for rematted def");
+
+    // Is this a dead def?
+    if (LII->end != VNI->def.getNextSlot())
+      continue;
+
+    MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
+    assert(MI && "Missing instruction for dead def");
+    MI->addRegisterDead(LI->reg, &TRI);
+
+    if (!MI->allDefsAreDead())
+      continue;
+
+    DEBUG(dbgs() << "All defs dead: " << *MI);
+    Dead.push_back(MI);
+  }
+
+  if (Dead.empty())
+    return;
+
+  Edit->eliminateDeadDefs(Dead, LIS, TII);
+}
+
 void SplitEditor::finish() {
   assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
   ++NumFinished;
@@ -835,7 +869,9 @@
   // Rewrite virtual registers, possibly extending ranges.
   rewriteAssigned(Complex);
 
-  // FIXME: Delete defs that were rematted everywhere.
+  // Delete defs that were rematted everywhere.
+  if (Complex)
+    deleteRematVictims();
 
   // Get rid of unused values and set phi-kill flags.
   for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)

Modified: llvm/trunk/lib/CodeGen/SplitKit.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.h?rev=127287&r1=127286&r2=127287&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SplitKit.h (original)
+++ llvm/trunk/lib/CodeGen/SplitKit.h Tue Mar  8 16:46:11 2011
@@ -272,6 +272,9 @@
   void rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
                          const ConnectedVNInfoEqClasses &ConEq);
 
+  /// deleteRematVictims - Delete defs that are dead after rematerializing.
+  void deleteRematVictims();
+
 public:
   /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
   /// Newly created intervals will be appended to newIntervals.





More information about the llvm-commits mailing list