Use the TargetInstrInfo::commuteInstruction method to commute instructions
authorChris Lattner <sabre@nondot.org>
Wed, 19 Jan 2005 07:08:42 +0000 (07:08 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 19 Jan 2005 07:08:42 +0000 (07:08 +0000)
instead of doing it manually.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19685 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/TwoAddressInstructionPass.cpp

index a03966802b52eb66479be4cf6b674f59056263ee..df632100de6c1d6d9191415503fa2372c6ad3aed 100644 (file)
@@ -141,12 +141,23 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
             unsigned regC = mi->getOperand(2).getReg();
             if (LV.KillsRegister(mi, regC)) {
               DEBUG(std::cerr << "2addr: COMMUTING  : " << *mi);
-              mi->SetMachineOperandReg(2, regB);
-              mi->SetMachineOperandReg(1, regC);
-              DEBUG(std::cerr << "2addr: COMMUTED TO: " << *mi);
-              ++NumCommuted;
-              regB = regC;
-              goto InstructionRearranged;
+              MachineInstr *NewMI = TII.commuteInstruction(mi);
+              if (NewMI == 0) {
+                DEBUG(std::cerr << "2addr: COMMUTING FAILED!\n");
+              } else {
+                DEBUG(std::cerr << "2addr: COMMUTED TO: " << *NewMI);
+                // If the instruction changed to commute it, update livevar.
+                if (NewMI != mi) {
+                  LV.instructionChanged(mi, NewMI);  // Update live variables
+                  mbbi->insert(mi, NewMI);           // Insert the new inst
+                  mbbi->erase(mi);                   // Nuke the old inst.
+                  mi = NewMI;
+                }                  
+
+                ++NumCommuted;
+                regB = regC;
+                goto InstructionRearranged;
+              }
             }
           }
           // If this instruction is potentially convertible to a true