[llvm] fd80760 - [InstCombine] fold (rotate X) eq/ne (0/-1)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 3 11:52:49 PDT 2021


Author: Sanjay Patel
Date: 2021-09-03T14:51:35-04:00
New Revision: fd807601a7845a135ec16af674b70ef667c4d796

URL: https://github.com/llvm/llvm-project/commit/fd807601a7845a135ec16af674b70ef667c4d796
DIFF: https://github.com/llvm/llvm-project/commit/fd807601a7845a135ec16af674b70ef667c4d796.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/icmp-fsh.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 79b494379e1c..7d303164e5cf 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 15b631b72ea4..17e12d04b82f 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]], <i5 -1, i5 -1>
+; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i5> [[X:%.*]], <i5 -1, i5 -1>
 ; 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:%.*]])


        


More information about the llvm-commits mailing list