Skip to content

Commit 1d4e857

Browse files
authored
[InstCombine] simplify average of lsb (#95684)
close: #94737 alive2: https://alive2.llvm.org/ce/z/WF_7mX In this patch, we combine `(X + Y) / 2` into `(X & Y)` only when both X and Y are less than or equal to 1.
1 parent b7b3d17 commit 1d4e857

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,12 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
12841284
return NewSub;
12851285
}
12861286

1287+
// Fold (X + Y) / 2 --> (X & Y) iff (X u<= 1) && (Y u<= 1)
1288+
if (match(Op0, m_Add(m_Value(X), m_Value(Y))) && match(Op1, m_One()) &&
1289+
computeKnownBits(X, /*Depth=*/0, &I).countMaxActiveBits() <= 1 &&
1290+
computeKnownBits(Y, /*Depth=*/0, &I).countMaxActiveBits() <= 1)
1291+
return BinaryOperator::CreateAnd(X, Y);
1292+
12871293
// (sub nuw X, (Y << nuw Z)) >>u exact Z --> (X >>u exact Z) sub nuw Y
12881294
if (I.isExact() &&
12891295
match(Op0, m_OneUse(m_NUWSub(m_Value(X),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
define i8 @avg_lsb(i8 %a, i8 %b) {
5+
; CHECK-LABEL: define i8 @avg_lsb(
6+
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
7+
; CHECK-NEXT: [[REM:%.*]] = and i8 [[A]], 1
8+
; CHECK-NEXT: [[DIV2:%.*]] = and i8 [[REM]], [[B]]
9+
; CHECK-NEXT: ret i8 [[DIV2]]
10+
;
11+
%rem = and i8 %a, 1
12+
%rem1 = and i8 %b, 1
13+
%add = add nuw nsw i8 %rem1, %rem
14+
%div2 = lshr i8 %add, 1
15+
ret i8 %div2
16+
}
17+
18+
define i8 @avg_lsb_mismatch(i8 %a, i8 %b) {
19+
; CHECK-LABEL: define i8 @avg_lsb_mismatch(
20+
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
21+
; CHECK-NEXT: [[REM:%.*]] = and i8 [[A]], 1
22+
; CHECK-NEXT: [[REM1:%.*]] = and i8 [[B]], 3
23+
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i8 [[REM1]], [[REM]]
24+
; CHECK-NEXT: [[DIV2:%.*]] = lshr i8 [[ADD]], 1
25+
; CHECK-NEXT: ret i8 [[DIV2]]
26+
;
27+
%rem = and i8 %a, 1
28+
%rem1 = and i8 %b, 3
29+
%add = add nuw nsw i8 %rem1, %rem
30+
%div2 = lshr i8 %add, 1
31+
ret i8 %div2
32+
}
33+
34+
define <2 x i8> @avg_lsb_vector(<2 x i8> %a, <2 x i8> %b) {
35+
; CHECK-LABEL: define <2 x i8> @avg_lsb_vector(
36+
; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]]) {
37+
; CHECK-NEXT: [[REM:%.*]] = and <2 x i8> [[A]], <i8 1, i8 1>
38+
; CHECK-NEXT: [[DIV2:%.*]] = and <2 x i8> [[REM]], [[B]]
39+
; CHECK-NEXT: ret <2 x i8> [[DIV2]]
40+
;
41+
%rem = and <2 x i8> %a, <i8 1, i8 1>
42+
%rem1 = and <2 x i8> %b, <i8 1, i8 1>
43+
%add = add nuw nsw <2 x i8> %rem1, %rem
44+
%div2 = lshr <2 x i8> %add, <i8 1, i8 1>
45+
ret <2 x i8> %div2
46+
}

llvm/test/Transforms/InstCombine/lshr.ll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,8 @@ define i8 @not_signbit_trunc(i16 %x) {
13901390

13911391
define i2 @bool_add_lshr(i1 %a, i1 %b) {
13921392
; CHECK-LABEL: @bool_add_lshr(
1393-
; CHECK-NEXT: [[TMP1:%.*]] = and i1 [[A:%.*]], [[B:%.*]]
1394-
; CHECK-NEXT: [[LSHR:%.*]] = zext i1 [[TMP1]] to i2
1393+
; CHECK-NEXT: [[LSHR1:%.*]] = and i1 [[A:%.*]], [[B:%.*]]
1394+
; CHECK-NEXT: [[LSHR:%.*]] = zext i1 [[LSHR1]] to i2
13951395
; CHECK-NEXT: ret i2 [[LSHR]]
13961396
;
13971397
%zext.a = zext i1 %a to i2
@@ -1436,8 +1436,8 @@ define i2 @bool_add_ashr(i1 %a, i1 %b) {
14361436

14371437
define <2 x i8> @bool_add_lshr_vec(<2 x i1> %a, <2 x i1> %b) {
14381438
; CHECK-LABEL: @bool_add_lshr_vec(
1439-
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i1> [[A:%.*]], [[B:%.*]]
1440-
; CHECK-NEXT: [[LSHR:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
1439+
; CHECK-NEXT: [[LSHR1:%.*]] = and <2 x i1> [[A:%.*]], [[B:%.*]]
1440+
; CHECK-NEXT: [[LSHR:%.*]] = zext <2 x i1> [[LSHR1]] to <2 x i8>
14411441
; CHECK-NEXT: ret <2 x i8> [[LSHR]]
14421442
;
14431443
%zext.a = zext <2 x i1> %a to <2 x i8>
@@ -1453,8 +1453,7 @@ define i32 @bool_add_lshr_uses(i1 %a, i1 %b) {
14531453
; CHECK-NEXT: call void @use(i32 [[ZEXT_A]])
14541454
; CHECK-NEXT: [[ZEXT_B:%.*]] = zext i1 [[B:%.*]] to i32
14551455
; CHECK-NEXT: call void @use(i32 [[ZEXT_B]])
1456-
; CHECK-NEXT: [[TMP1:%.*]] = and i1 [[A]], [[B]]
1457-
; CHECK-NEXT: [[LSHR:%.*]] = zext i1 [[TMP1]] to i32
1456+
; CHECK-NEXT: [[LSHR:%.*]] = and i32 [[ZEXT_A]], [[ZEXT_B]]
14581457
; CHECK-NEXT: ret i32 [[LSHR]]
14591458
;
14601459
%zext.a = zext i1 %a to i32
@@ -1473,8 +1472,7 @@ define i32 @bool_add_lshr_uses2(i1 %a, i1 %b) {
14731472
; CHECK-NEXT: call void @use(i32 [[ZEXT_B]])
14741473
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[ZEXT_A]], [[ZEXT_B]]
14751474
; CHECK-NEXT: call void @use(i32 [[ADD]])
1476-
; CHECK-NEXT: [[TMP1:%.*]] = and i1 [[A]], [[B]]
1477-
; CHECK-NEXT: [[LSHR:%.*]] = zext i1 [[TMP1]] to i32
1475+
; CHECK-NEXT: [[LSHR:%.*]] = and i32 [[ZEXT_A]], [[ZEXT_B]]
14781476
; CHECK-NEXT: ret i32 [[LSHR]]
14791477
;
14801478
%zext.a = zext i1 %a to i32
@@ -1496,7 +1494,7 @@ define i32 @bool_add_lshr_uses3(i1 %a, i1 %b) {
14961494
; CHECK-NEXT: call void @use(i32 [[ZEXT_B]])
14971495
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[ZEXT_A]], [[ZEXT_B]]
14981496
; CHECK-NEXT: call void @use(i32 [[ADD]])
1499-
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[ADD]], 1
1497+
; CHECK-NEXT: [[LSHR:%.*]] = and i32 [[ZEXT_A]], [[ZEXT_B]]
15001498
; CHECK-NEXT: ret i32 [[LSHR]]
15011499
;
15021500
%zext.a = zext i1 %a to i32

0 commit comments

Comments
 (0)