[PATCH] D141090: [InstCombine] Canonicalize (A & B_Pow2) eq/ne B_Pow2 patterns
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 6 09:51:28 PST 2023
goldstein.w.n updated this revision to Diff 486909.
goldstein.w.n marked an inline comment as done.
goldstein.w.n added a comment.
Make non-zero more clean
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D141090/new/
https://reviews.llvm.org/D141090
Files:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-and-shift.ll
Index: llvm/test/Transforms/InstCombine/icmp-and-shift.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-and-shift.ll
+++ llvm/test/Transforms/InstCombine/icmp-and-shift.ll
@@ -437,7 +437,7 @@
; CHECK-LABEL: @eq_and_shl_one(
; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[Y:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i8 [[POW2]], [[X:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[AND]], [[POW2]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[AND]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%pow2 = shl i8 1, %y
@@ -450,7 +450,7 @@
; CHECK-LABEL: @ne_and_shl_one_commute(
; CHECK-NEXT: [[POW2:%.*]] = shl nuw <2 x i8> <i8 1, i8 poison>, [[Y:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and <2 x i8> [[POW2]], [[X:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[POW2]], [[AND]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[AND]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%pow2 = shl <2 x i8> <i8 1, i8 poison>, %y
@@ -464,7 +464,7 @@
; CHECK-NEXT: [[X:%.*]] = mul i8 [[PX:%.*]], [[PX]]
; CHECK-NEXT: [[POW2:%.*]] = lshr i8 -128, [[Y:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[POW2]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[AND]], [[POW2]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[AND]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%x = mul i8 %px, %px ; thwart complexity-based canonicalization
@@ -481,7 +481,7 @@
; CHECK-NEXT: call void @use(i8 [[POW2]])
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[POW2]]
; CHECK-NEXT: call void @use(i8 [[AND]])
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[POW2]], [[AND]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[AND]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%x = mul i8 %px, %px ; thwart complexity-based canonicalization
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4783,6 +4783,20 @@
Add, ConstantInt::get(A->getType(), C.shl(1)));
}
+ // Canonicalize:
+ // Assume B_Pow2 != 0
+ // 1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
+ // 2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
+ if (match(Op0, m_c_And(m_Specific(Op1), m_Value())) &&
+ isKnownToBeAPowerOfTwo(Op1, /* orZero */ false, 0, &I))
+ return new ICmpInst(CmpInst::getInversePredicate(Pred), Op0,
+ ConstantInt::getNullValue(Op0->getType()));
+
+ if (match(Op1, m_c_And(m_Specific(Op0), m_Value())) &&
+ isKnownToBeAPowerOfTwo(Op0, /* orZero */ false, 0, &I))
+ return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
+ ConstantInt::getNullValue(Op1->getType()));
+
return nullptr;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141090.486909.patch
Type: text/x-patch
Size: 2841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230106/4a6326b2/attachment.bin>
More information about the llvm-commits
mailing list