[llvm-commits] [llvm] r52309 - /llvm/trunk/lib/CodeGen/VirtRegMap.cpp

Evan Cheng evan.cheng at apple.com
Mon Jun 16 00:34:17 PDT 2008


Author: evancheng
Date: Mon Jun 16 02:34:17 2008
New Revision: 52309

URL: http://llvm.org/viewvc/llvm-project?rev=52309&view=rev
Log:
Fix read after free found by valgrind.

Modified:
    llvm/trunk/lib/CodeGen/VirtRegMap.cpp

Modified: llvm/trunk/lib/CodeGen/VirtRegMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/VirtRegMap.cpp?rev=52309&r1=52308&r2=52309&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/VirtRegMap.cpp (original)
+++ llvm/trunk/lib/CodeGen/VirtRegMap.cpp Mon Jun 16 02:34:17 2008
@@ -1026,19 +1026,15 @@
            DefMI->getOperand(DefIdx).getReg() == SrcReg);
 
     // Now commute def instruction.
-    MachineInstr *CommutedMI = TII->commuteInstruction(DefMI);
+    MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true);
     if (!CommutedMI)
       return false;
     SmallVector<unsigned, 2> Ops;
     Ops.push_back(NewDstIdx);
     MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS);
-    if (!FoldedMI) {
-      if (CommutedMI == DefMI)
-        TII->commuteInstruction(CommutedMI);
-      else
-        MBB.erase(CommutedMI);
+    delete CommutedMI;  // Not needed since foldMemoryOperand returns new MI.
+    if (!FoldedMI)
       return false;
-    }
 
     VRM.addSpillSlotUse(SS, FoldedMI);
     VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
@@ -1052,17 +1048,16 @@
     MII = MBB.insert(MII, FoldedMI);  // Update MII to backtrack.
 
     // Delete all 3 old instructions.
-    InvalidateKills(MI, RegKills, KillOps);
-    VRM.RemoveMachineInstrFromMaps(&MI);
-    MBB.erase(&MI);
-    if (CommutedMI != DefMI)
-      MBB.erase(CommutedMI);
-    InvalidateKills(*DefMI, RegKills, KillOps);
-    VRM.RemoveMachineInstrFromMaps(DefMI);
-    MBB.erase(DefMI);
     InvalidateKills(*ReloadMI, RegKills, KillOps);
     VRM.RemoveMachineInstrFromMaps(ReloadMI);
     MBB.erase(ReloadMI);
+    InvalidateKills(*DefMI, RegKills, KillOps);
+    VRM.RemoveMachineInstrFromMaps(DefMI);
+    MBB.erase(DefMI);
+    InvalidateKills(MI, RegKills, KillOps);
+    VRM.RemoveMachineInstrFromMaps(&MI);
+    MBB.erase(&MI);
+
     ++NumCommutes;
     return true;
   }





More information about the llvm-commits mailing list