From b5bf016015a5e2d224042eca02576a896318966f Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Sun, 6 Aug 2017 06:28:41 +0000 Subject: [InstCombine] Support ~(c-X) --> X+(-c-1) and ~(X-c) --> (-c-1)-X for splat vectors. llvm-svn: 310195 --- .../Transforms/InstCombine/InstCombineAndOrXor.cpp | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 7b32812a2c1..e684f58f0f4 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2419,17 +2419,33 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { return replaceInstUsesWith(I, Op0); } + { + const APInt *RHSC; + if (match(Op1, m_APInt(RHSC))) { + Value *V; + const APInt *C; + if (match(Op0, m_Sub(m_APInt(C), m_Value(V)))) { + // ~(c-X) == X-c-1 == X+(-c-1) + if (RHSC->isAllOnesValue()) { + Constant *NewC = ConstantInt::get(I.getType(), -(*C) - 1); + return BinaryOperator::CreateAdd(V, NewC); + } + } else if (match(Op0, m_Add(m_Value(V), m_APInt(C)))) { + // ~(X-c) --> (-c-1)-X + if (RHSC->isAllOnesValue()) { + Constant *NewC = ConstantInt::get(I.getType(), -(*C) - 1); + return BinaryOperator::CreateSub(NewC, V); + } + } + } + } + if (ConstantInt *RHSC = dyn_cast(Op1)) { if (BinaryOperator *Op0I = dyn_cast(Op0)) { - // ~(c-X) == X-c-1 == X+(-c-1) if (Op0I->getOpcode() == Instruction::Sub) if (ConstantInt *Op0I0C = dyn_cast(Op0I->getOperand(0))) { - if (RHSC->isMinusOne()) { - Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); - return BinaryOperator::CreateAdd(Op0I->getOperand(1), - SubOne(NegOp0I0C)); - } else if (RHSC->getValue().isSignMask()) { - // (C - X) ^ signmask -> (C + signmask - X) + // (C - X) ^ signmask -> (C + signmask - X) + if (RHSC->getValue().isSignMask()) { Constant *C = Builder.getInt(RHSC->getValue() + Op0I0C->getValue()); return BinaryOperator::CreateSub(C, Op0I->getOperand(1)); } @@ -2437,13 +2453,8 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (ConstantInt *Op0CI = dyn_cast(Op0I->getOperand(1))) { if (Op0I->getOpcode() == Instruction::Add) { - // ~(X-c) --> (-c-1)-X - if (RHSC->isMinusOne()) { - Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); - return BinaryOperator::CreateSub(SubOne(NegOp0CI), - Op0I->getOperand(0)); - } else if (RHSC->getValue().isSignMask()) { - // (X + C) ^ signmask -> (X + C + signmask) + // (X + C) ^ signmask -> (X + C + signmask) + if (RHSC->getValue().isSignMask()) { Constant *C = Builder.getInt(RHSC->getValue() + Op0CI->getValue()); return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); } -- cgit v1.2.3