From d65460f133f392953492888201afe06dce84ee8e Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 5 Nov 2003 01:06:05 +0000 Subject: Fix bug with previous implementation: - // ~(c-X) == X-(c-1) == X+(-c+1) + // ~(c-X) == X-c-1 == X+(-c-1) Implement: C - ~X == X + (1+C) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9715 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index dafd756..4a5f1fb 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -488,11 +488,18 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { if (Value *V = dyn_castNegVal(Op1)) return BinaryOperator::create(Instruction::Add, Op0, V); - // Replace (-1 - A) with (~A)... - if (ConstantInt *C = dyn_cast(Op0)) + if (ConstantInt *C = dyn_cast(Op0)) { + // Replace (-1 - A) with (~A)... if (C->isAllOnesValue()) return BinaryOperator::createNot(Op1); + // C - ~X == X + (1+C) + if (BinaryOperator::isNot(Op1)) + return BinaryOperator::create(Instruction::Add, + BinaryOperator::getNotArgument(cast(Op1)), + *C + *ConstantInt::get(I.getType(), 1)); + } + if (BinaryOperator *Op1I = dyn_cast(Op1)) if (Op1I->hasOneUse()) { // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression @@ -1001,10 +1008,10 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { return new SetCondInst(SCI->getInverseCondition(), SCI->getOperand(0), SCI->getOperand(1)); - // ~(c-X) == X-(c-1) == X+(-c+1) + // ~(c-X) == X-c-1 == X+(-c-1) if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue() && isa(Op0I->getOperand(0))) { - Constant *ConstantRHS = *-*cast(Op0I->getOperand(0)) + + Constant *ConstantRHS = *-*cast(Op0I->getOperand(0)) - *ConstantInt::get(I.getType(), 1); return BinaryOperator::create(Instruction::Add, Op0I->getOperand(1), ConstantRHS); -- cgit v1.1