[PATCH] D125220: [InstCombine] (rot X, ?) == 0/-1 --> X == 0/-1

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 9 04:33:25 PDT 2022


Chenbing.Zheng created this revision.
Chenbing.Zheng added reviewers: spatel, RKSimon, benshi001.
Chenbing.Zheng added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Chenbing.Zheng requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.

This transform is copied from foldICmpEqIntrinsicWithConstant,
it is safe to re-use undef elts in a vector. 
This patch complete it in foldICmpInstWithConstantNotInt.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125220

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


Index: llvm/test/Transforms/InstCombine/icmp-fsh.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-fsh.ll
+++ llvm/test/Transforms/InstCombine/icmp-fsh.ll
@@ -53,12 +53,19 @@
   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_n0_undef(<2 x i5> %x, <2 x i5> %y) {
+; CHECK-LABEL: @rotl_ne_n0_undef(
+; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i5> [[X:%.*]], <i5 undef, i5 0>
+; 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)
+  %r = icmp ne <2 x i5> %rot, <i5 undef, i5 0>
+  ret <2 x i1> %r
+}
 
 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:%.*]])
-; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i5> [[ROT]], <i5 -1, i5 undef>
+; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i5> [[X:%.*]], <i5 -1, i5 undef>
 ; 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)
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3250,8 +3250,6 @@
   case Intrinsic::fshr:
     if (II->getArgOperand(0) == II->getArgOperand(1)) {
       // (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.isZero() || C.isAllOnes())
         return new ICmpInst(Pred, II->getArgOperand(0), Cmp.getOperand(1));
 
@@ -3402,6 +3400,8 @@
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   Constant *RHSC = dyn_cast<Constant>(Op1);
   Instruction *LHSI = dyn_cast<Instruction>(Op0);
+  const ICmpInst::Predicate Pred = I.getPredicate();
+
   if (!RHSC || !LHSI)
     return nullptr;
 
@@ -3430,7 +3430,6 @@
           I.getPredicate(), LHSI->getOperand(0),
           Constant::getNullValue(LHSI->getOperand(0)->getType()));
     break;
-
   case Instruction::Load:
     // Try to optimize things like "A[i] > 4" to index computations.
     if (GetElementPtrInst *GEP =
@@ -3442,6 +3441,25 @@
     break;
   }
 
+  if (auto *II = dyn_cast<IntrinsicInst>(I.getOperand(0))) {
+    switch (II->getIntrinsicID()) {
+    default:
+      break;
+    case Intrinsic::fshl:
+    case Intrinsic::fshr:
+      if (II->getArgOperand(0) == II->getArgOperand(1)) {
+        // (rot X, ?) == 0/-1 --> X == 0/-1
+        // This transform is copied from foldICmpEqIntrinsicWithConstant,
+        // it is safe to re-use undef elts in a vector.
+        if (RHSC->getType()->isVectorTy() &&
+            (RHSC->getSplatValue(/*AllowUndefs*/ true)->isZeroValue() ||
+             RHSC->getSplatValue(/*AllowUndefs*/ true)->isAllOnesValue()))
+          return new ICmpInst(Pred, II->getArgOperand(0), I.getOperand(1));
+      }
+      break;
+    }
+  }
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125220.428032.patch
Type: text/x-patch
Size: 3251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220509/c3db5633/attachment.bin>


More information about the llvm-commits mailing list