[PATCH] D123374: [InstCombine] Fold (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit (PR21929)
Simon Pilgrim via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 8 03:05:40 PDT 2022
RKSimon created this revision.
RKSimon added reviewers: spatel, lebedev.ri, nikic.
Herald added a subscriber: hiraditya.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added a project: LLVM.
This is the final missing fold to handle the modulo2 simplification: https://github.com/llvm/llvm-project/issues/22303
Alive2: https://alive2.llvm.org/ce/z/Ygq26C
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D123374
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/modulo.ll
Index: llvm/test/Transforms/InstCombine/modulo.ll
===================================================================
--- llvm/test/Transforms/InstCombine/modulo.ll
+++ llvm/test/Transforms/InstCombine/modulo.ll
@@ -4,9 +4,7 @@
; PR21929
define i32 @modulo2(i32 %x) {
; CHECK-LABEL: @modulo2(
-; CHECK-NEXT: [[REM_I:%.*]] = srem i32 [[X:%.*]], 2
-; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[REM_I]], 2
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw i32 [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and i32 [[X:%.*]], 1
; CHECK-NEXT: ret i32 [[RET_I]]
;
%rem.i = srem i32 %x, 2
@@ -18,9 +16,7 @@
define <2 x i32> @modulo2_vec(<2 x i32> %x) {
; CHECK-LABEL: @modulo2_vec(
-; CHECK-NEXT: [[REM_I:%.*]] = srem <2 x i32> [[X:%.*]], <i32 2, i32 2>
-; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[REM_I]], <i32 2, i32 2>
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw <2 x i32> [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and <2 x i32> [[X:%.*]], <i32 1, i32 1>
; CHECK-NEXT: ret <2 x i32> [[RET_I]]
;
%rem.i = srem <2 x i32> %x, <i32 2, i32 2>
@@ -62,9 +58,7 @@
define i32 @modulo4(i32 %x) {
; CHECK-LABEL: @modulo4(
-; CHECK-NEXT: [[REM_I:%.*]] = srem i32 [[X:%.*]], 4
-; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[REM_I]], 4
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw i32 [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and i32 [[X:%.*]], 3
; CHECK-NEXT: ret i32 [[RET_I]]
;
%rem.i = srem i32 %x, 4
@@ -76,9 +70,7 @@
define <2 x i32> @modulo4_vec(<2 x i32> %x) {
; CHECK-LABEL: @modulo4_vec(
-; CHECK-NEXT: [[REM_I:%.*]] = srem <2 x i32> [[X:%.*]], <i32 4, i32 4>
-; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[REM_I]], <i32 4, i32 4>
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw <2 x i32> [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and <2 x i32> [[X:%.*]], <i32 3, i32 3>
; CHECK-NEXT: ret <2 x i32> [[RET_I]]
;
%rem.i = srem <2 x i32> %x, <i32 4, i32 4>
@@ -120,9 +112,7 @@
define i32 @modulo32(i32 %x) {
; CHECK-LABEL: @modulo32(
-; CHECK-NEXT: [[REM_I:%.*]] = srem i32 [[X:%.*]], 32
-; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[REM_I]], 32
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw i32 [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and i32 [[X:%.*]], 31
; CHECK-NEXT: ret i32 [[RET_I]]
;
%rem.i = srem i32 %x, 32
@@ -134,9 +124,7 @@
define <2 x i32> @modulo32_vec(<2 x i32> %x) {
; CHECK-LABEL: @modulo32_vec(
-; CHECK-NEXT: [[REM_I:%.*]] = srem <2 x i32> [[X:%.*]], <i32 32, i32 32>
-; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[REM_I]], <i32 32, i32 32>
-; CHECK-NEXT: [[RET_I:%.*]] = add nsw <2 x i32> [[TMP1]], [[REM_I]]
+; CHECK-NEXT: [[RET_I:%.*]] = and <2 x i32> [[X:%.*]], <i32 31, i32 31>
; CHECK-NEXT: ret <2 x i32> [[RET_I]]
;
%rem.i = srem <2 x i32> %x, <i32 32, i32 32>
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1369,6 +1369,13 @@
}
}
+ // (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit
+ if (match(&I, m_c_Add(m_And(m_Value(A), m_APInt(C1)), m_Deferred(A))) &&
+ C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countLeadingZeros())) {
+ Constant *NewMask = ConstantInt::get(RHS->getType(), *C1 - 1);
+ return BinaryOperator::CreateAnd(A, NewMask);
+ }
+
// A+B --> A|B iff A and B have no bits set in common.
if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
return BinaryOperator::CreateOr(LHS, RHS);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123374.421470.patch
Type: text/x-patch
Size: 3602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220408/5cd276d2/attachment.bin>
More information about the llvm-commits
mailing list