From 7edc8c223e7a783ed837f0f6fd6033c41fd3e7a3 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 7 Apr 2005 17:14:51 +0000 Subject: [PATCH] Implement the following xforms: (X-Y)-X --> -Y A + (B - A) --> B (B - A) + A --> B git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21138 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index c1eb3ebe45f..2a54366728a 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -629,6 +629,17 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // X + X --> X << 1 if (I.getType()->isInteger()) { if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; + + if (Instruction *RHSI = dyn_cast(RHS)) { + if (RHSI->getOpcode() == Instruction::Sub) + if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B + return ReplaceInstUsesWith(I, RHSI->getOperand(0)); + } + if (Instruction *LHSI = dyn_cast(LHS)) { + if (LHSI->getOpcode() == Instruction::Sub) + if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B + return ReplaceInstUsesWith(I, LHSI->getOperand(0)); + } } // -A + B --> B - A @@ -640,6 +651,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (Value *V = dyn_castNegVal(RHS)) return BinaryOperator::createSub(LHS, V); + ConstantInt *C2; if (Value *X = dyn_castFoldableMul(LHS, C2)) { if (X == RHS) // X*C + X --> X * (C+1) @@ -851,13 +863,16 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { } } - if (BinaryOperator *Op0I = dyn_cast(Op0)) - if (Op0I->getOpcode() == Instruction::Add) - if (!Op0->getType()->isFloatingPoint()) { + if (!Op0->getType()->isFloatingPoint()) + if (BinaryOperator *Op0I = dyn_cast(Op0)) + if (Op0I->getOpcode() == Instruction::Add) { if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X return ReplaceInstUsesWith(I, Op0I->getOperand(1)); else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X return ReplaceInstUsesWith(I, Op0I->getOperand(0)); + } else if (Op0I->getOpcode() == Instruction::Sub) { + if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y + return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); } ConstantInt *C1; -- 2.34.1