Fix cases where we generated horrible code like this:
authorChris Lattner <sabre@nondot.org>
Wed, 21 Jul 2004 21:28:26 +0000 (21:28 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 21 Jul 2004 21:28:26 +0000 (21:28 +0000)
        mov %EDI, 12
        add %EDI, %ECX
        mov %ECX, 12
        add %ECX, %EDX
        mov %EDX, 12
        add %EDX, %ESI

instead (really!) generate this:

        add %ECX, 12
        add %EDX, 12
        add %ESI, 12

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

lib/Target/X86/InstSelectSimple.cpp
lib/Target/X86/X86ISelSimple.cpp

index cb57c12889bc15387bffcd44a51587e16dca6d27..4b43896d37db825d205d9153e671faa35ee49477 100644 (file)
@@ -2967,8 +2967,9 @@ void ISel::visitLoadInst(LoadInst &I) {
       // Okay, we found a user.  If the load is the first operand and there is
       // no second operand load, reverse the operand ordering.  Note that this
       // can fail for a subtract (ie, no change will be made).
+      bool Swapped = false;
       if (!isa<LoadInst>(User->getOperand(1)))
-        cast<BinaryOperator>(User)->swapOperands();
+        Swapped = !cast<BinaryOperator>(User)->swapOperands();
       
       // Okay, now that everything is set up, if this load is used by the second
       // operand, and if there are no instructions that invalidate the load
@@ -2985,6 +2986,11 @@ void ISel::visitLoadInst(LoadInst &I) {
            User->getOpcode() == Instruction::Div) &&
           isSafeToFoldLoadIntoInstruction(I, *User))
         return;  // Eliminate the load!
+
+      // If we swapped the operands to the instruction, but couldn't fold the
+      // load anyway, swap them back.  We don't want to break add X, int 
+      // folding.
+      if (Swapped) cast<BinaryOperator>(User)->swapOperands();
     }
   }
 
index cb57c12889bc15387bffcd44a51587e16dca6d27..4b43896d37db825d205d9153e671faa35ee49477 100644 (file)
@@ -2967,8 +2967,9 @@ void ISel::visitLoadInst(LoadInst &I) {
       // Okay, we found a user.  If the load is the first operand and there is
       // no second operand load, reverse the operand ordering.  Note that this
       // can fail for a subtract (ie, no change will be made).
+      bool Swapped = false;
       if (!isa<LoadInst>(User->getOperand(1)))
-        cast<BinaryOperator>(User)->swapOperands();
+        Swapped = !cast<BinaryOperator>(User)->swapOperands();
       
       // Okay, now that everything is set up, if this load is used by the second
       // operand, and if there are no instructions that invalidate the load
@@ -2985,6 +2986,11 @@ void ISel::visitLoadInst(LoadInst &I) {
            User->getOpcode() == Instruction::Div) &&
           isSafeToFoldLoadIntoInstruction(I, *User))
         return;  // Eliminate the load!
+
+      // If we swapped the operands to the instruction, but couldn't fold the
+      // load anyway, swap them back.  We don't want to break add X, int 
+      // folding.
+      if (Swapped) cast<BinaryOperator>(User)->swapOperands();
     }
   }