[llvm] [InstCombine] simplify average of lsb (PR #95684)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 18:20:22 PDT 2024


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/95684

close: #94737
alive2: https://alive2.llvm.org/ce/z/TtauVq

In this patch, we combine `((a % 2) + (b % 2)) / 2` into `(a & b & 1)`.


>From 3734ce9d7391f16781440d5d84df5ffa5fcef814 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 16 Jun 2024 09:17:02 +0800
Subject: [PATCH] simplify average of lsb

---
 .../InstCombine/InstCombineShifts.cpp         |  8 ++++
 llvm/test/Transforms/InstCombine/avg-lsb.ll   | 46 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/avg-lsb.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 4a014ab6e044e..bb68aa79ca970 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1284,6 +1284,14 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
     return NewSub;
   }
 
+  // ((X % 2) + (Y % 2)) / 2 --> (X & Y & 1)
+  if (match(Op0,
+            m_Add(m_And(m_Value(X), m_One()), m_And(m_Value(Y), m_One()))) &&
+      match(Op1, m_One())) {
+    Value *And = Builder.CreateAnd(X, Y);
+    return BinaryOperator::CreateAnd(And, ConstantInt::get(And->getType(), 1));
+  }
+
   // (sub nuw X, (Y << nuw Z)) >>u exact Z --> (X >>u exact Z) sub nuw Y
   if (I.isExact() &&
       match(Op0, m_OneUse(m_NUWSub(m_Value(X),
diff --git a/llvm/test/Transforms/InstCombine/avg-lsb.ll b/llvm/test/Transforms/InstCombine/avg-lsb.ll
new file mode 100644
index 0000000000000..e9fdb5bfe8dc9
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/avg-lsb.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i8 @avg_lsb(i8 %a, i8 %b) {
+; CHECK-LABEL: define i8 @avg_lsb(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[B]], [[A]]
+; CHECK-NEXT:    [[DIV2:%.*]] = and i8 [[TMP1]], 1
+; CHECK-NEXT:    ret i8 [[DIV2]]
+;
+  %rem = and i8 %a, 1
+  %rem1 = and i8 %b, 1
+  %add = add nuw nsw i8 %rem1, %rem
+  %div2 = lshr i8 %add, 1
+  ret i8 %div2
+}
+
+define i8 @avg_lsb_mismatch(i8 %a, i8 %b) {
+; CHECK-LABEL: define i8 @avg_lsb_mismatch(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT:    [[REM:%.*]] = and i8 [[A]], 1
+; CHECK-NEXT:    [[REM1:%.*]] = and i8 [[B]], 3
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw i8 [[REM1]], [[REM]]
+; CHECK-NEXT:    [[DIV2:%.*]] = lshr i8 [[ADD]], 1
+; CHECK-NEXT:    ret i8 [[DIV2]]
+;
+  %rem = and i8 %a, 1
+  %rem1 = and i8 %b, 3
+  %add = add nuw nsw i8 %rem1, %rem
+  %div2 = lshr i8 %add, 1
+  ret i8 %div2
+}
+
+define <2 x i8> @avg_lsb_vector(<2 x i8> %a, <2 x i8> %b) {
+; CHECK-LABEL: define <2 x i8> @avg_lsb_vector(
+; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[B]], [[A]]
+; CHECK-NEXT:    [[DIV2:%.*]] = and <2 x i8> [[TMP1]], <i8 1, i8 1>
+; CHECK-NEXT:    ret <2 x i8> [[DIV2]]
+;
+  %rem = and <2 x i8> %a, <i8 1, i8 1>
+  %rem1 = and <2 x i8> %b, <i8 1, i8 1>
+  %add = add nuw nsw <2 x i8> %rem1, %rem
+  %div2 = lshr <2 x i8> %add, <i8 1, i8 1>
+  ret <2 x i8> %div2
+}



More information about the llvm-commits mailing list