When extending a liveinterval by commuting, don't throw away the live ranges that...
[oota-llvm.git] / lib / CodeGen / TwoAddressInstructionPass.cpp
index 5bac6a6822aa67f38292cfd6c0c9ae5b0862727d..81864b7c0e4e81c6de067aae0d996d79db0d5beb 100644 (file)
@@ -330,8 +330,13 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
         InstructionRearranged:
           const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
           MachineInstr *Orig = MRI->getVRegDef(regB);
+          const TargetInstrDesc &OrigTID = Orig->getDesc();
+          bool SawStore = false;
 
-          if (EnableReMat && Orig && TII->isTriviallyReMaterializable(Orig)) {
+          if (EnableReMat && Orig && Orig->isSafeToMove(TII, SawStore) &&
+              OrigTID.isAsCheapAsAMove() && !OrigTID.mayLoad() &&
+              !OrigTID.isSimpleLoad()) {
+            DEBUG(cerr << "2addr: REMATTING : " << *Orig << "\n");
             TII->reMaterialize(*mbbi, mi, regA, Orig);
             ReMattedInstrs.insert(Orig);
           } else {
@@ -373,6 +378,8 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
   }
 
   if (EnableReMat) {
+    // Check to see if the instructions that we rematerialized are now dead. If
+    // they are, expunge them here.
     SmallPtrSet<MachineInstr*, 8>::iterator I = ReMattedInstrs.begin();
     SmallPtrSet<MachineInstr*, 8>::iterator E = ReMattedInstrs.end();
 
@@ -385,20 +392,17 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
         if (!MO.isRegister())
           continue;
         unsigned MOReg = MO.getReg();
-        if (!MOReg)
+
+        if (!MOReg || !MO.isDef() || (MO.isImplicit() && MO.isDead()))
           continue;
-        if (MO.isDef()) {
-          if (MO.isImplicit())
-            continue;
 
-          if (MRI->use_begin(MOReg) != MRI->use_end()) {
-            InstrDead = false;
-            break;
-          }
+        if (MRI->use_begin(MOReg) != MRI->use_end()) {
+          InstrDead = false;
+          break;
         }
       }
 
-      if (InstrDead && MI->getNumOperands() > 0)
+      if (InstrDead)
         MI->eraseFromParent();
     }
   }