From d8e175bca5919de13fe003cab6b10f23af3d055d Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sat, 14 Jul 2018 16:44:54 +0000 Subject: [PATCH] [InstCombine] Fold x u<= x & C to x u<= C https://bugs.llvm.org/show_bug.cgi?id=38123 https://rise4fun.com/Alive/Fqp This pattern is not commutative. But InstSimplify will already have taken care of the 'commutative' variant. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337102 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineCompares.cpp | 5 +++ ...t-low-bit-mask-and-icmp-ule-to-icmp-ule.ll | 34 ++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 1024cbafaaa..2a8d83fde2c 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2907,6 +2907,11 @@ static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I, assert(X == I.getOperand(1) && "instsimplify took care of commut. variant"); DstPred = ICmpInst::Predicate::ICMP_UGT; break; + case ICmpInst::Predicate::ICMP_ULE: + // x u<= x & (-1 >> y) -> x u<= (-1 >> y) + assert(X == I.getOperand(0) && "instsimplify took care of commut. variant"); + DstPred = ICmpInst::Predicate::ICMP_ULE; + break; // TODO: more folds are possible, https://bugs.llvm.org/show_bug.cgi?id=38123 default: return nullptr; diff --git a/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ule-to-icmp-ule.ll b/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ule-to-icmp-ule.ll index f29623f171e..0bab709618d 100644 --- a/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ule-to-icmp-ule.ll +++ b/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ule-to-icmp-ule.ll @@ -20,9 +20,8 @@ declare <3 x i8> @gen3x8() define i1 @p0() { ; CHECK-LABEL: @p0( ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8() -; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3 -; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]] -; CHECK-NEXT: ret i1 [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[X]], 4 +; CHECK-NEXT: ret i1 [[TMP1]] ; %x = call i8 @gen8() %tmp0 = and i8 %x, 3 @@ -34,9 +33,8 @@ define i1 @pv(i8 %y) { ; CHECK-LABEL: @pv( ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8() ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]] -; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[TMP1]] -; CHECK-NEXT: ret i1 [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i8 [[X]], [[TMP0]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %x = call i8 @gen8() %tmp0 = lshr i8 -1, %y @@ -52,9 +50,8 @@ define i1 @pv(i8 %y) { define <2 x i1> @p1_vec_splat() { ; CHECK-LABEL: @p1_vec_splat( ; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8() -; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], -; CHECK-NEXT: [[RET:%.*]] = icmp ule <2 x i8> [[X]], [[TMP0]] -; CHECK-NEXT: ret <2 x i1> [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i8> [[X]], +; CHECK-NEXT: ret <2 x i1> [[TMP1]] ; %x = call <2 x i8> @gen2x8() %tmp0 = and <2 x i8> %x, @@ -65,9 +62,8 @@ define <2 x i1> @p1_vec_splat() { define <2 x i1> @p2_vec_nonsplat() { ; CHECK-LABEL: @p2_vec_nonsplat( ; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8() -; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], -; CHECK-NEXT: [[RET:%.*]] = icmp ule <2 x i8> [[X]], [[TMP0]] -; CHECK-NEXT: ret <2 x i1> [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i8> [[X]], +; CHECK-NEXT: ret <2 x i1> [[TMP1]] ; %x = call <2 x i8> @gen2x8() %tmp0 = and <2 x i8> %x, ; doesn't have to be splat. @@ -78,9 +74,8 @@ define <2 x i1> @p2_vec_nonsplat() { define <3 x i1> @p3_vec_splat_undef() { ; CHECK-LABEL: @p3_vec_splat_undef( ; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8() -; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X]], -; CHECK-NEXT: [[RET:%.*]] = icmp ule <3 x i8> [[X]], [[TMP0]] -; CHECK-NEXT: ret <3 x i1> [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <3 x i8> [[X]], +; CHECK-NEXT: ret <3 x i1> [[TMP1]] ; %x = call <3 x i8> @gen3x8() %tmp0 = and <3 x i8> %x, @@ -109,9 +104,8 @@ define i1 @cv0(i8 %y) { ; CHECK-LABEL: @cv0( ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8() ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]] -; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[TMP1]] -; CHECK-NEXT: ret i1 [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i8 [[X]], [[TMP0]] +; CHECK-NEXT: ret i1 [[TMP1]] ; %x = call i8 @gen8() %tmp0 = lshr i8 -1, %y @@ -153,8 +147,8 @@ define i1 @oneuse0() { ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8() ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3 ; CHECK-NEXT: call void @use8(i8 [[TMP0]]) -; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]] -; CHECK-NEXT: ret i1 [[RET]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[X]], 4 +; CHECK-NEXT: ret i1 [[TMP1]] ; %x = call i8 @gen8() %tmp0 = and i8 %x, 3 -- 2.50.1