[llvm] c2f0101 - [InstCombine] ~(~X + Y) -> X - Y

Dávid Bolvanský via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 02:06:01 PDT 2020


Author: Dávid Bolvanský
Date: 2020-08-11T11:05:42+02:00
New Revision: c2f0101310133ed282d6dd187b2d3f4cac356a83

URL: https://github.com/llvm/llvm-project/commit/c2f0101310133ed282d6dd187b2d3f4cac356a83
DIFF: https://github.com/llvm/llvm-project/commit/c2f0101310133ed282d6dd187b2d3f4cac356a83.diff

LOG: [InstCombine] ~(~X + Y) -> X - Y

Proof:
https://alive2.llvm.org/ce/z/4xharr

Solves PR47051

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D85593

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/test/Transforms/InstCombine/not-add.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 9d7effc724be..17a96aa213e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3219,6 +3219,11 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
     // ~(X + C) --> -(C + 1) - X
     if (match(Op0, m_Add(m_Value(X), m_Constant(C))))
       return BinaryOperator::CreateSub(ConstantExpr::getNeg(AddOne(C)), X);
+
+    // ~(~X + Y) --> X - Y
+    if (match(NotVal, m_c_Add(m_Not(m_Value(X)), m_Value(Y))))
+      return BinaryOperator::CreateWithCopiedFlags(Instruction::Sub, X, Y,
+                                                   NotVal);
   }
 
   // Use DeMorgan and reassociation to eliminate a 'not' op.

diff  --git a/llvm/test/Transforms/InstCombine/not-add.ll b/llvm/test/Transforms/InstCombine/not-add.ll
index 1bd6dbe4e933..6891fdd5fcc5 100644
--- a/llvm/test/Transforms/InstCombine/not-add.ll
+++ b/llvm/test/Transforms/InstCombine/not-add.ll
@@ -2,13 +2,10 @@
 ; RUN: opt -instcombine -S < %s | FileCheck %s
 
 declare void @use(i8)
-declare i8 @get()
 
 define i8 @basic(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[A:%.*]] = add i8 [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -19,9 +16,7 @@ define i8 @basic(i8 %x, i8 %y) {
 
 define i8 @basic_com_add(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic_com_add(
-; CHECK-NEXT:    [[NOTY:%.*]] = xor i8 [[Y:%.*]], -1
-; CHECK-NEXT:    [[A:%.*]] = add i8 [[NOTY]], [[X:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub i8 [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %noty = xor i8 %y, -1
@@ -34,8 +29,7 @@ define i8 @basic_use_xor(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic_use_xor(
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
 ; CHECK-NEXT:    call void @use(i8 [[NOTX]])
-; CHECK-NEXT:    [[A:%.*]] = add i8 [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub i8 [[X]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -50,7 +44,7 @@ define i8 @basic_use_add(i8 %x, i8 %y) {
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
 ; CHECK-NEXT:    [[A:%.*]] = add i8 [[NOTX]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[A]])
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub i8 [[X]], [[Y]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -66,7 +60,7 @@ define i8 @basic_use_both(i8 %x, i8 %y) {
 ; CHECK-NEXT:    call void @use(i8 [[NOTX]])
 ; CHECK-NEXT:    [[A:%.*]] = add i8 [[NOTX]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[A]])
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub i8 [[X]], [[Y]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -79,9 +73,7 @@ define i8 @basic_use_both(i8 %x, i8 %y) {
 
 define i8 @basic_preserve_nsw(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic_preserve_nsw(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[A:%.*]] = add nsw i8 [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -92,9 +84,7 @@ define i8 @basic_preserve_nsw(i8 %x, i8 %y) {
 
 define i8 @basic_preserve_nuw(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic_preserve_nuw(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[A:%.*]] = add nuw i8 [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub nuw i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -105,9 +95,7 @@ define i8 @basic_preserve_nuw(i8 %x, i8 %y) {
 
 define i8 @basic_preserve_nuw_nsw(i8 %x, i8 %y) {
 ; CHECK-LABEL: @basic_preserve_nuw_nsw(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor i8 [[X:%.*]], -1
-; CHECK-NEXT:    [[A:%.*]] = add nuw nsw i8 [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor i8 [[A]], -1
+; CHECK-NEXT:    [[NOTA:%.*]] = sub nuw nsw i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[NOTA]]
 ;
   %notx = xor i8 %x, -1
@@ -118,9 +106,7 @@ define i8 @basic_preserve_nuw_nsw(i8 %x, i8 %y) {
 
 define <4 x i32> @vector_test(<4 x i32> %x, <4 x i32> %y) {
 ; CHECK-LABEL: @vector_test(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
-; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor <4 x i32> [[A]], <i32 -1, i32 -1, i32 -1, i32 -1>
+; CHECK-NEXT:    [[NOTA:%.*]] = sub <4 x i32> [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret <4 x i32> [[NOTA]]
 ;
   %notx = xor <4 x i32> %x, <i32 -1, i32 -1, i32 -1, i32 -1>
@@ -131,9 +117,7 @@ define <4 x i32> @vector_test(<4 x i32> %x, <4 x i32> %y) {
 
 define <4 x i32> @vector_test_undef(<4 x i32> %x, <4 x i32> %y) {
 ; CHECK-LABEL: @vector_test_undef(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <4 x i32> [[X:%.*]], <i32 -1, i32 undef, i32 undef, i32 -1>
-; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor <4 x i32> [[A]], <i32 -1, i32 -1, i32 undef, i32 undef>
+; CHECK-NEXT:    [[NOTA:%.*]] = sub <4 x i32> [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret <4 x i32> [[NOTA]]
 ;
   %notx = xor <4 x i32> %x, <i32 -1, i32 undef, i32 undef, i32 -1>
@@ -145,9 +129,7 @@ define <4 x i32> @vector_test_undef(<4 x i32> %x, <4 x i32> %y) {
 
 define <4 x i32> @vector_test_undef_nsw_nuw(<4 x i32> %x, <4 x i32> %y) {
 ; CHECK-LABEL: @vector_test_undef_nsw_nuw(
-; CHECK-NEXT:    [[NOTX:%.*]] = xor <4 x i32> [[X:%.*]], <i32 -1, i32 undef, i32 undef, i32 -1>
-; CHECK-NEXT:    [[A:%.*]] = add nuw nsw <4 x i32> [[NOTX]], [[Y:%.*]]
-; CHECK-NEXT:    [[NOTA:%.*]] = xor <4 x i32> [[A]], <i32 -1, i32 -1, i32 undef, i32 undef>
+; CHECK-NEXT:    [[NOTA:%.*]] = sub nuw nsw <4 x i32> [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret <4 x i32> [[NOTA]]
 ;
   %notx = xor <4 x i32> %x, <i32 -1, i32 undef, i32 undef, i32 -1>


        


More information about the llvm-commits mailing list