Add ability to transform (x - (y - z)) into (x + (z - y))
authorChris Lattner <sabre@nondot.org>
Thu, 9 May 2002 01:29:19 +0000 (01:29 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 9 May 2002 01:29:19 +0000 (01:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2566 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index a77bcb95864ca1b9331b2d061dbabd83094c77cf..9625398017a03c4a89af1cc8b5e7d5609d24ed0c 100644 (file)
@@ -180,6 +180,19 @@ Instruction *InstCombiner::visitSub(BinaryOperator *I) {
   if (Value *V = dyn_castNegInst(Op1))
     return BinaryOperator::create(Instruction::Add, Op0, V);
 
+  // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
+  // not used by anyone else...
+  //
+  if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
+    if (Op1I->use_size() == 1) {
+      // Swap the two operands of the subexpr...
+      Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
+      Op1I->setOperand(0, IIOp1);
+      Op1I->setOperand(1, IIOp0);
+
+      // Create the new top level add instruction...
+      return BinaryOperator::create(Instruction::Add, Op0, Op1);
+    }
   return 0;
 }