[llvm] [InstSimplify] Ignore mask when combinining vp.reverse(vp.reverse). (PR #171542)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 9 16:49:43 PST 2025


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/171542

>From 647838558ee917b86ec9ac2ebefb681ffd66d6b2 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 9 Dec 2025 15:39:20 -0800
Subject: [PATCH 1/2] [InstSimplify] Ignore mask when combinining
 vp.reverse(vp.reverse).

The mask doesn't really affect the reverse. It only poisons the
masked off elements in the results. It should be ok to ignore the
mask if we can eliminate the pair.

I don't have a specific use case for this, but it matches what I
had implemented in our downstream before the current upstream
implementation. Submitting upstream so I can remove eliminate the
delta in my downstream.
---
 llvm/lib/Analysis/InstructionSimplify.cpp       | 7 +++----
 llvm/test/Transforms/InstSimplify/vp-reverse.ll | 9 +++++++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index bd85444d7d2b0..c28e6d2ef8217 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7306,10 +7306,9 @@ static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
     Value *EVL = Call->getArgOperand(2);
 
     Value *X;
-    // vp.reverse(vp.reverse(X)) == X (with all ones mask and matching EVL)
-    if (match(Mask, m_AllOnes()) &&
-        match(Vec, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
-                       m_Value(X), m_AllOnes(), m_Specific(EVL))))
+    // vp.reverse(vp.reverse(X)) == X (mask doesn't matter)
+    if (match(Vec, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
+                       m_Value(X), m_Value(), m_Specific(EVL))))
       return X;
 
     // vp.reverse(splat(X)) -> splat(X) (regardless of mask and EVL)
diff --git a/llvm/test/Transforms/InstSimplify/vp-reverse.ll b/llvm/test/Transforms/InstSimplify/vp-reverse.ll
index f19a2ac8ca9e1..0a9998a9f64bb 100644
--- a/llvm/test/Transforms/InstSimplify/vp-reverse.ll
+++ b/llvm/test/Transforms/InstSimplify/vp-reverse.ll
@@ -68,3 +68,12 @@ define <vscale x 4 x i32> @rev_of_splat2(i32 %a, <vscale x 4 x i1> %m, i32 %evl)
   %rev = tail call <vscale x 4 x i32> @llvm.experimental.vp.reverse(<vscale x 4 x i32> %a.vec, <vscale x 4 x i1> %m, i32 %evl)
   ret <vscale x 4 x i32> %rev
 }
+
+define <vscale x 4 x i32> @rev_of_rev_diffmask(<vscale x 4 x i32> %a, <vscale x 4 x i1> %mask1, <vscale x 4 x i1> %mask2, i32 %evl) {
+; CHECK-LABEL: @rev_of_rev_diffmask(
+; CHECK-NEXT:    ret <vscale x 4 x i32> [[RES:%.*]]
+;
+  %a.rev = tail call <vscale x 4 x i32> @llvm.experimental.vp.reverse(<vscale x 4 x i32> %a, <vscale x 4 x i1> %mask1, i32 %evl)
+  %res = tail call <vscale x 4 x i32> @llvm.experimental.vp.reverse(<vscale x 4 x i32> %a.rev, <vscale x 4 x i1> %mask2, i32 %evl)
+  ret <vscale x 4 x i32> %res
+}

>From e4b722ff462b988b3b90d5803fc2c43572764e8d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 9 Dec 2025 16:49:29 -0800
Subject: [PATCH 2/2] fixup! remove unused variable

---
 llvm/lib/Analysis/InstructionSimplify.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index c28e6d2ef8217..1d82515cd84f9 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7302,7 +7302,6 @@ static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
   }
   case Intrinsic::experimental_vp_reverse: {
     Value *Vec = Call->getArgOperand(0);
-    Value *Mask = Call->getArgOperand(1);
     Value *EVL = Call->getArgOperand(2);
 
     Value *X;



More information about the llvm-commits mailing list