From: Sanjay Patel Date: Fri, 3 Sep 2021 18:35:10 +0000 (-0400) Subject: [InstCombine] fold (rotate X) eq/ne (0/-1) X-Git-Tag: upstream/15.0.7~32273 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fd807601a7845a135ec16af674b70ef667c4d796;p=platform%2Fupstream%2Fllvm.git [InstCombine] fold (rotate X) eq/ne (0/-1) This generalizes the examples shown in: https://llvm.org/PR51566 https://alive2.llvm.org/ce/z/V-sEy9 --- diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 79b4943..7d30316 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3226,6 +3226,16 @@ Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant( break; } + case Intrinsic::fshl: + case Intrinsic::fshr: + // (rot X, ?) == 0/-1 --> X == 0/-1 + // TODO: This transform is safe to re-use undef elts in a vector, but + // the constant value passed in by the caller doesn't allow that. + if (C.isNullValue() || C.isAllOnesValue()) + if (II->getArgOperand(0) == II->getArgOperand(1)) + return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1)); + break; + case Intrinsic::uadd_sat: { // uadd.sat(a, b) == 0 -> (a | b) == 0 if (C.isNullValue()) { diff --git a/llvm/test/Transforms/InstCombine/icmp-fsh.ll b/llvm/test/Transforms/InstCombine/icmp-fsh.ll index 15b631b..17e12d0 100644 --- a/llvm/test/Transforms/InstCombine/icmp-fsh.ll +++ b/llvm/test/Transforms/InstCombine/icmp-fsh.ll @@ -8,8 +8,7 @@ declare void @use(i8) define i1 @rotl_eq_0(i8 %x, i8 %y) { ; CHECK-LABEL: @rotl_eq_0( -; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ROT]], 0 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshl.i8(i8 %x, i8 %x, i8 %y) @@ -17,11 +16,13 @@ define i1 @rotl_eq_0(i8 %x, i8 %y) { ret i1 %r } +; Extra use is ok. + define i1 @rotl_ne_0(i8 %x, i8 %y) { ; CHECK-LABEL: @rotl_ne_0( ; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) ; CHECK-NEXT: call void @use(i8 [[ROT]]) -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ROT]], 0 +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshl.i8(i8 %x, i8 %x, i8 %y) @@ -32,8 +33,7 @@ define i1 @rotl_ne_0(i8 %x, i8 %y) { define i1 @rotl_eq_n1(i8 %x, i8 %y) { ; CHECK-LABEL: @rotl_eq_n1( -; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ROT]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], -1 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshl.i8(i8 %x, i8 %x, i8 %y) @@ -41,10 +41,11 @@ define i1 @rotl_eq_n1(i8 %x, i8 %y) { ret i1 %r } +; Vectors work too. + define <2 x i1> @rotl_ne_n1(<2 x i5> %x, <2 x i5> %y) { ; CHECK-LABEL: @rotl_ne_n1( -; CHECK-NEXT: [[ROT:%.*]] = tail call <2 x i5> @llvm.fshl.v2i5(<2 x i5> [[X:%.*]], <2 x i5> [[X]], <2 x i5> [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i5> [[ROT]], +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i5> [[X:%.*]], ; CHECK-NEXT: ret <2 x i1> [[R]] ; %rot = tail call <2 x i5> @llvm.fshl.v2i5(<2 x i5>%x, <2 x i5> %x, <2 x i5> %y) @@ -52,6 +53,8 @@ define <2 x i1> @rotl_ne_n1(<2 x i5> %x, <2 x i5> %y) { ret <2 x i1> %r } +; TODO: We filter out vector constants with undef elts, but that isn't needed for this transform. + define <2 x i1> @rotl_ne_n1_undef(<2 x i5> %x, <2 x i5> %y) { ; CHECK-LABEL: @rotl_ne_n1_undef( ; CHECK-NEXT: [[ROT:%.*]] = tail call <2 x i5> @llvm.fshl.v2i5(<2 x i5> [[X:%.*]], <2 x i5> [[X]], <2 x i5> [[Y:%.*]]) @@ -67,7 +70,7 @@ define i1 @rotr_eq_0(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_eq_0( ; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) ; CHECK-NEXT: call void @use(i8 [[ROT]]) -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ROT]], 0 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshr.i8(i8 %x, i8 %x, i8 %y) @@ -78,8 +81,7 @@ define i1 @rotr_eq_0(i8 %x, i8 %y) { define i1 @rotr_ne_0(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_ne_0( -; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ROT]], 0 +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X:%.*]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshr.i8(i8 %x, i8 %x, i8 %y) @@ -89,8 +91,7 @@ define i1 @rotr_ne_0(i8 %x, i8 %y) { define i1 @rotr_eq_n1(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_eq_n1( -; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ROT]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X:%.*]], -1 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshr.i8(i8 %x, i8 %x, i8 %y) @@ -100,8 +101,7 @@ define i1 @rotr_eq_n1(i8 %x, i8 %y) { define i1 @rotr_ne_n1(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_ne_n1( -; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ROT]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[X:%.*]], -1 ; CHECK-NEXT: ret i1 [[R]] ; %rot = tail call i8 @llvm.fshr.i8(i8 %x, i8 %x, i8 %y) @@ -109,6 +109,8 @@ define i1 @rotr_ne_n1(i8 %x, i8 %y) { ret i1 %r } +; negative test - wrong constant value + define i1 @rotr_ne_1(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_ne_1( ; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) @@ -120,6 +122,8 @@ define i1 @rotr_ne_1(i8 %x, i8 %y) { ret i1 %r } +; negative test - wrong predicate + define i1 @rotr_sgt_n1(i8 %x, i8 %y) { ; CHECK-LABEL: @rotr_sgt_n1( ; CHECK-NEXT: [[ROT:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[Y:%.*]]) @@ -131,6 +135,8 @@ define i1 @rotr_sgt_n1(i8 %x, i8 %y) { ret i1 %r } +; negative test - must be a rotate, not general funnel shift + define i1 @fshr_sgt_n1(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @fshr_sgt_n1( ; CHECK-NEXT: [[FSH:%.*]] = tail call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[Y:%.*]], i8 [[Z:%.*]])