[llvm] cc0216b - [NFC][InstCombine] '(X & (- Y)) - X' -> '- (X & (Y - 1))' fold (PR44448)

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 09:27:53 PST 2020


Author: Roman Lebedev
Date: 2020-01-03T20:27:29+03:00
New Revision: cc0216bedb85642e085a53bf046966fc87dd8afc

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

LOG: [NFC][InstCombine] '(X & (- Y)) - X' -> '- (X & (Y - 1))' fold (PR44448)

Name: (X & (- Y)) - X  ->  - (X & (Y - 1))  (PR44448)
  %negy = sub i8 0, %y
  %unbiasedx = and i8 %negy, %x
  %r = sub i8 %unbiasedx, %x
=>
  %ymask = add i8 %y, -1
  %xmasked = and i8 %ymask, %x
  %r = sub i8 0, %xmasked

https://rise4fun.com/Alive/OIpla

This decreases use count of %x, may allow us to
later hoist said negation even further,
and results in marginally nicer X86 codegen.

See
  https://bugs.llvm.org/show_bug.cgi?id=44448
  https://reviews.llvm.org/D71499

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/hoist-negation-out-of-bias-calculation.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index ad42f3dabfe9..33803aaf359a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1879,6 +1879,16 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
           Y, Builder.CreateNot(Op1, Op1->getName() + ".not"));
   }
 
+  {
+    // (sub (and Op1, (neg X)), Op1) --> neg (and Op1, (add X, -1))
+    Value *X;
+    if (match(Op0, m_OneUse(m_c_And(m_Specific(Op1),
+                                    m_OneUse(m_Neg(m_Value(X))))))) {
+      return BinaryOperator::CreateNeg(Builder.CreateAnd(
+          Op1, Builder.CreateAdd(X, Constant::getAllOnesValue(I.getType()))));
+    }
+  }
+
   if (Op1->hasOneUse()) {
     Value *X = nullptr, *Y = nullptr, *Z = nullptr;
     Constant *C = nullptr;

diff  --git a/llvm/test/Transforms/InstCombine/hoist-negation-out-of-bias-calculation.ll b/llvm/test/Transforms/InstCombine/hoist-negation-out-of-bias-calculation.ll
index b0e8884cea06..bcb93c7a50de 100644
--- a/llvm/test/Transforms/InstCombine/hoist-negation-out-of-bias-calculation.ll
+++ b/llvm/test/Transforms/InstCombine/hoist-negation-out-of-bias-calculation.ll
@@ -14,9 +14,9 @@
 
 define i8 @t0(i8 %x, i8 %y) {
 ; CHECK-LABEL: @t0(
-; CHECK-NEXT:    [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
-; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X:%.*]]
-; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 0, [[TMP2]]
 ; CHECK-NEXT:    ret i8 [[NEGBIAS]]
 ;
   %negy = sub i8 0, %y
@@ -30,9 +30,9 @@ declare i8 @gen8()
 define i8 @t1_commutative(i8 %y) {
 ; CHECK-LABEL: @t1_commutative(
 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT:    [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
-; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and i8 [[X]], [[NEGY]]
-; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 0, [[TMP2]]
 ; CHECK-NEXT:    ret i8 [[NEGBIAS]]
 ;
   %x = call i8 @gen8()
@@ -44,9 +44,9 @@ define i8 @t1_commutative(i8 %y) {
 
 define <2 x i8> @t2_vec(<2 x i8> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @t2_vec(
-; CHECK-NEXT:    [[NEGY:%.*]] = sub <2 x i8> zeroinitializer, [[Y:%.*]]
-; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and <2 x i8> [[NEGY]], [[X:%.*]]
-; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> [[UNBIASEDX]], [[X]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], <i8 -1, i8 -1>
+; CHECK-NEXT:    [[TMP2:%.*]] = and <2 x i8> [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP2]]
 ; CHECK-NEXT:    ret <2 x i8> [[NEGBIAS]]
 ;
   %negy = sub <2 x i8> <i8 0, i8 0>, %y
@@ -57,9 +57,9 @@ define <2 x i8> @t2_vec(<2 x i8> %x, <2 x i8> %y) {
 
 define <2 x i8> @t3_vec_undef(<2 x i8> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @t3_vec_undef(
-; CHECK-NEXT:    [[NEGY:%.*]] = sub <2 x i8> <i8 0, i8 undef>, [[Y:%.*]]
-; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and <2 x i8> [[NEGY]], [[X:%.*]]
-; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> [[UNBIASEDX]], [[X]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], <i8 -1, i8 -1>
+; CHECK-NEXT:    [[TMP2:%.*]] = and <2 x i8> [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP2]]
 ; CHECK-NEXT:    ret <2 x i8> [[NEGBIAS]]
 ;
   %negy = sub <2 x i8> <i8 0, i8 undef>, %y


        


More information about the llvm-commits mailing list