From d75064e6d502fb65265289a29468ff5dd7dd4685 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 3 Sep 2018 18:21:59 +0000 Subject: [PATCH] [InstCombine] allow add+not --> sub for arbitrary vector constants. llvm-svn: 341335 --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 9 ++++----- llvm/test/Transforms/InstCombine/not.ll | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index f34ec13f02e0..9af5ad05ae57 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2699,6 +2699,10 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { Constant *NotC = ConstantExpr::getNot(C); return BinaryOperator::CreateAShr(NotC, Y); } + + // ~(X + C) --> -(C + 1) - X + if (match(Op0, m_Add(m_Value(X), m_Constant(C)))) + return BinaryOperator::CreateSub(ConstantExpr::getNeg(AddOne(C)), X); } // not (cmp A, B) = !cmp A, B @@ -2720,11 +2724,6 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { return BinaryOperator::CreateSub(NewC, X); } } else if (match(Op0, m_Add(m_Value(X), m_APInt(C)))) { - // ~(X + C) --> (-C - 1) - X - if (RHSC->isAllOnesValue()) { - Constant *NewC = ConstantInt::get(I.getType(), -(*C) - 1); - return BinaryOperator::CreateSub(NewC, X); - } if (RHSC->isSignMask()) { // (X + C) ^ signmask -> (X + C + signmask) Constant *NewC = ConstantInt::get(I.getType(), *C + *RHSC); diff --git a/llvm/test/Transforms/InstCombine/not.ll b/llvm/test/Transforms/InstCombine/not.ll index 4e41f46f9534..42a910aa3f2c 100644 --- a/llvm/test/Transforms/InstCombine/not.ll +++ b/llvm/test/Transforms/InstCombine/not.ll @@ -243,8 +243,7 @@ define <2 x i32> @not_add_splat(<2 x i32> %x) { define <2 x i32> @not_add_vec(<2 x i32> %x) { ; CHECK-LABEL: @not_add_vec( -; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[R:%.*]] = xor <2 x i32> [[A]], +; CHECK-NEXT: [[R:%.*]] = sub <2 x i32> , [[X:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %a = add <2 x i32> %x,