[llvm] [InstComb] Fold ashr (xor x, y), x -> ashr y, x (PR #165866)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 31 07:04:15 PDT 2025


https://github.com/artagnon created https://github.com/llvm/llvm-project/pull/165866

Proof: https://alive2.llvm.org/ce/z/yWCmMd

>From 1f580a6c321cac8df05f1fde76529a0c239a1685 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Fri, 31 Oct 2025 13:57:42 +0000
Subject: [PATCH 1/2] [InstComb] Pre-commit ashr-xor test

---
 .../Transforms/InstCombine/shift-logic.ll     | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/shift-logic.ll b/llvm/test/Transforms/InstCombine/shift-logic.ll
index ab8d98a9523ba..d89c7c7990c65 100644
--- a/llvm/test/Transforms/InstCombine/shift-logic.ll
+++ b/llvm/test/Transforms/InstCombine/shift-logic.ll
@@ -186,6 +186,7 @@ define i32 @ashr_xor(i32 %x, i32 %py) {
   ret i32 %sh1
 }
 
+
 define i32 @shr_mismatch_xor(i32 %x, i32 %y) {
 ; CHECK-LABEL: @shr_mismatch_xor(
 ; CHECK-NEXT:    [[SH0:%.*]] = ashr i32 [[X:%.*]], 5
@@ -225,6 +226,28 @@ define <2 x i32> @ashr_poison_poison_xor(<2 x i32> %x, <2 x i32> %y) {
   ret <2 x i32> %sh1
 }
 
+define i32 @ashr_xor_operand_match(i32 %x, i32 %y) {
+; CHECK-LABEL: @ashr_xor_operand_match(
+; CHECK-NEXT:    [[SH1:%.*]] = xor i32 [[TMP1:%.*]], [[TMP2:%.*]]
+; CHECK-NEXT:    [[RET:%.*]] = ashr i32 [[SH1]], [[TMP1]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %r = xor i32 %x, %y
+  %ret = ashr i32 %r, %x
+  ret i32 %ret
+}
+
+define i32 @ashr_xor_operand_mismtach(i32 %x, i32 %y) {
+; CHECK-LABEL: @ashr_xor_operand_mismtach(
+; CHECK-NEXT:    [[R:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[RET:%.*]] = ashr i32 [[R]], [[Y]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %r = xor i32 %x, %y
+  %ret = ashr i32 %r, %y
+  ret i32 %ret
+}
+
 define i32 @lshr_or_extra_use(i32 %x, i32 %y, ptr %p) {
 ; CHECK-LABEL: @lshr_or_extra_use(
 ; CHECK-NEXT:    [[SH0:%.*]] = lshr i32 [[X:%.*]], 5

>From 1e717b741f3ab7d89cde5e7517c28b86d9791366 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Fri, 31 Oct 2025 13:59:20 +0000
Subject: [PATCH 2/2] [InstComb] Fold ashr (xor x, y), x -> ashr y, x

Proof: https://alive2.llvm.org/ce/z/yWCmMd

Co-authored-by: John Regehr <regehr at cs.utah.edu>
---
 llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 5 +++++
 llvm/test/Transforms/InstCombine/shift-logic.ll       | 3 +--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 899a3c16554c9..6410a509a48af 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1836,6 +1836,11 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
     return Lshr;
   }
 
+  // ashr (xor %x, %y), %x --> ashr %y, %x
+  Value *Y;
+  if (match(Op0, m_Xor(m_Value(X), m_Value(Y))) && Op1 == X)
+    return BinaryOperator::CreateAShr(Y, X);
+
   // ashr (xor %x, -1), %y  -->  xor (ashr %x, %y), -1
   if (match(Op0, m_OneUse(m_Not(m_Value(X))))) {
     // Note that we must drop 'exact'-ness of the shift!
diff --git a/llvm/test/Transforms/InstCombine/shift-logic.ll b/llvm/test/Transforms/InstCombine/shift-logic.ll
index d89c7c7990c65..2de2073a183b7 100644
--- a/llvm/test/Transforms/InstCombine/shift-logic.ll
+++ b/llvm/test/Transforms/InstCombine/shift-logic.ll
@@ -228,8 +228,7 @@ define <2 x i32> @ashr_poison_poison_xor(<2 x i32> %x, <2 x i32> %y) {
 
 define i32 @ashr_xor_operand_match(i32 %x, i32 %y) {
 ; CHECK-LABEL: @ashr_xor_operand_match(
-; CHECK-NEXT:    [[SH1:%.*]] = xor i32 [[TMP1:%.*]], [[TMP2:%.*]]
-; CHECK-NEXT:    [[RET:%.*]] = ashr i32 [[SH1]], [[TMP1]]
+; CHECK-NEXT:    [[RET:%.*]] = ashr i32 [[SH1:%.*]], [[TMP1:%.*]]
 ; CHECK-NEXT:    ret i32 [[RET]]
 ;
   %r = xor i32 %x, %y



More information about the llvm-commits mailing list