[llvm] r342548 - [InstCombine] foldICmpWithLowBitMaskedVal(): handle uncanonical ((-1 << y) >> y) mask
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 19 06:35:47 PDT 2018
Author: lebedevri
Date: Wed Sep 19 06:35:46 2018
New Revision: 342548
URL: http://llvm.org/viewvc/llvm-project?rev=342548&view=rev
Log:
[InstCombine] foldICmpWithLowBitMaskedVal(): handle uncanonical ((-1 << y) >> y) mask
Summary:
The last low-bit-mask-pattern-producing-pattern i can think of.
https://rise4fun.com/Alive/UGzE <- non-canonical
But we can not canonicalize it because of extra uses.
https://bugs.llvm.org/show_bug.cgi?id=38123
Reviewers: spatel, craig.topper, RKSimon
Reviewed By: spatel
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D52148
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-eq-to-icmp-ule.ll
llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-ne-to-icmp-ugt.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=342548&r1=342547&r2=342548&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Sep 19 06:35:46 2018
@@ -2886,6 +2886,7 @@ Instruction *InstCombiner::foldICmpInstW
/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask
/// Where Mask is some pattern that produces all-ones in low bits:
/// (-1 >> y)
+/// ((-1 << y) >> y) <- non-canonical, has extra uses
/// ~(-1 << y)
/// ((1 << y) + (-1)) <- non-canonical, has extra uses
/// The Mask can be a constant, too.
@@ -2894,11 +2895,12 @@ Instruction *InstCombiner::foldICmpInstW
static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I,
InstCombiner::BuilderTy &Builder) {
ICmpInst::Predicate SrcPred;
- Value *X, *M;
- auto m_VariableMask =
- m_CombineOr(m_CombineOr(m_Not(m_Shl(m_AllOnes(), m_Value())),
- m_Add(m_Shl(m_One(), m_Value()), m_AllOnes())),
- m_LShr(m_AllOnes(), m_Value()));
+ Value *X, *M, *Y;
+ auto m_VariableMask = m_CombineOr(
+ m_CombineOr(m_Not(m_Shl(m_AllOnes(), m_Value())),
+ m_Add(m_Shl(m_One(), m_Value()), m_AllOnes())),
+ m_CombineOr(m_LShr(m_AllOnes(), m_Value()),
+ m_LShr(m_Shl(m_AllOnes(), m_Value(Y)), m_Deferred(Y))));
auto m_Mask = m_CombineOr(m_VariableMask, m_LowBitMask());
if (!match(&I, m_c_ICmp(SrcPred,
m_c_And(m_CombineAnd(m_Mask, m_Value(M)), m_Value(X)),
Modified: llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-eq-to-icmp-ule.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-eq-to-icmp-ule.ll?rev=342548&r1=342547&r2=342548&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-eq-to-icmp-ule.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-eq-to-icmp-ule.ll Wed Sep 19 06:35:46 2018
@@ -23,9 +23,8 @@ define i1 @p0(i8 %x, i8 %y) {
; CHECK-NEXT: [[T0:%.*]] = shl i8 -1, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i8 [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -44,9 +43,8 @@ define <2 x i1> @p1_vec(<2 x i8> %x, <2
; CHECK-NEXT: [[T0:%.*]] = shl <2 x i8> <i8 -1, i8 -1>, [[Y:%.*]]
; CHECK-NEXT: call void @use2i8(<2 x i8> [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr <2 x i8> [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and <2 x i8> [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq <2 x i8> [[T2]], [[X]]
-; CHECK-NEXT: ret <2 x i1> [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge <2 x i8> [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret <2 x i1> [[TMP1]]
;
%t0 = shl <2 x i8> <i8 -1, i8 -1>, %y
call void @use2i8(<2 x i8> %t0)
@@ -61,9 +59,8 @@ define <3 x i1> @p2_vec_undef0(<3 x i8>
; CHECK-NEXT: [[T0:%.*]] = shl <3 x i8> <i8 -1, i8 undef, i8 -1>, [[Y:%.*]]
; CHECK-NEXT: call void @use3i8(<3 x i8> [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr <3 x i8> [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and <3 x i8> [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq <3 x i8> [[T2]], [[X]]
-; CHECK-NEXT: ret <3 x i1> [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge <3 x i8> [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret <3 x i1> [[TMP1]]
;
%t0 = shl <3 x i8> <i8 -1, i8 undef, i8 -1>, %y
call void @use3i8(<3 x i8> %t0)
@@ -85,9 +82,8 @@ define i1 @c0(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[X]], [[T1]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -104,9 +100,8 @@ define i1 @c1(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[X]], [[T2]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -123,9 +118,8 @@ define i1 @c2(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[X]], [[T1]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[X]], [[T2]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -146,9 +140,8 @@ define i1 @oneuse0(i8 %x, i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i8 [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0) ; needed anyway
@@ -166,8 +159,8 @@ define i1 @oneuse1(i8 %x, i8 %y) {
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T2]])
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i8 [[T1]], [[X]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0) ; needed anyway
@@ -186,8 +179,8 @@ define i1 @oneuse2(i8 %x, i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T1]])
; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T2]])
-; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i8 [[T1]], [[X]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
Modified: llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-ne-to-icmp-ugt.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-ne-to-icmp-ugt.ll?rev=342548&r1=342547&r2=342548&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-ne-to-icmp-ugt.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/canonicalize-low-bit-mask-v4-and-icmp-ne-to-icmp-ugt.ll Wed Sep 19 06:35:46 2018
@@ -23,9 +23,8 @@ define i1 @p0(i8 %x, i8 %y) {
; CHECK-NEXT: [[T0:%.*]] = shl i8 -1, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -44,9 +43,8 @@ define <2 x i1> @p1_vec(<2 x i8> %x, <2
; CHECK-NEXT: [[T0:%.*]] = shl <2 x i8> <i8 -1, i8 -1>, [[Y:%.*]]
; CHECK-NEXT: call void @use2i8(<2 x i8> [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr <2 x i8> [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and <2 x i8> [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne <2 x i8> [[T2]], [[X]]
-; CHECK-NEXT: ret <2 x i1> [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i8> [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret <2 x i1> [[TMP1]]
;
%t0 = shl <2 x i8> <i8 -1, i8 -1>, %y
call void @use2i8(<2 x i8> %t0)
@@ -61,9 +59,8 @@ define <3 x i1> @p2_vec_undef0(<3 x i8>
; CHECK-NEXT: [[T0:%.*]] = shl <3 x i8> <i8 -1, i8 undef, i8 -1>, [[Y:%.*]]
; CHECK-NEXT: call void @use3i8(<3 x i8> [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr <3 x i8> [[T0]], [[Y]]
-; CHECK-NEXT: [[T2:%.*]] = and <3 x i8> [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne <3 x i8> [[T2]], [[X]]
-; CHECK-NEXT: ret <3 x i1> [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <3 x i8> [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret <3 x i1> [[TMP1]]
;
%t0 = shl <3 x i8> <i8 -1, i8 undef, i8 -1>, %y
call void @use3i8(<3 x i8> %t0)
@@ -85,9 +82,8 @@ define i1 @c0(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[X]], [[T1]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -104,9 +100,8 @@ define i1 @c1(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X]], [[T2]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -123,9 +118,8 @@ define i1 @c2(i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[X]], [[T1]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X]], [[T2]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 [[X]], [[T1]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
@@ -146,9 +140,8 @@ define i1 @oneuse0(i8 %x, i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
-; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[T1]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0) ; needed anyway
@@ -166,8 +159,8 @@ define i1 @oneuse1(i8 %x, i8 %y) {
; CHECK-NEXT: [[T1:%.*]] = lshr i8 [[T0]], [[Y]]
; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T2]])
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[T1]], [[X]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0) ; needed anyway
@@ -186,8 +179,8 @@ define i1 @oneuse2(i8 %x, i8 %y) {
; CHECK-NEXT: call void @use8(i8 [[T1]])
; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T2]])
-; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[X]]
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[T1]], [[X]]
+; CHECK-NEXT: ret i1 [[TMP1]]
;
%t0 = shl i8 -1, %y
call void @use8(i8 %t0)
More information about the llvm-commits
mailing list