[llvm] [InstCombine] Infer exact for lshr by cttz (PR #136696)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 27 08:47:50 PDT 2025


https://github.com/houngkoungting updated https://github.com/llvm/llvm-project/pull/136696

>From 19aa8f27dac6a3c7483bbd081143b5efc410da2c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?=
 <37643576+houngkoungting at users.noreply.github.com>
Date: Tue, 22 Apr 2025 21:32:43 +0800
Subject: [PATCH 1/4] Update InstCombineShifts.cpp

---
 .../lib/Transforms/InstCombine/InstCombineShifts.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 90cd279e8a457..c39b7dae434e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -994,6 +994,18 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
       I.setIsExact();
       return true;
     }
+    //Fix #131444
+    if (auto *Cttz = dyn_cast<IntrinsicInst>(I.getOperand(1))) {
+      if (Cttz->getIntrinsicID() == Intrinsic::cttz &&
+          Cttz->getOperand(0) == I.getOperand(0)) {
+        if (auto *Const = dyn_cast<ConstantInt>(Cttz->getOperand(1))) {
+          if (Const->isOne()) {  
+            I.setIsExact();
+            return true;
+          }
+        }
+      }
+    }
   }
 
   // Compute what we know about shift count.

>From 213dc72d165a3904b09b7a117e08fce25b518fe8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <we3223 at gmail.com>
Date: Sun, 27 Apr 2025 17:09:25 +0800
Subject: [PATCH 2/4] Update InstCombineShifts.cpp

---
 .../Transforms/InstCombine/InstCombineShifts.cpp | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index c39b7dae434e0..280a414c77dae 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -994,17 +994,11 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
       I.setIsExact();
       return true;
     }
-    //Fix #131444
-    if (auto *Cttz = dyn_cast<IntrinsicInst>(I.getOperand(1))) {
-      if (Cttz->getIntrinsicID() == Intrinsic::cttz &&
-          Cttz->getOperand(0) == I.getOperand(0)) {
-        if (auto *Const = dyn_cast<ConstantInt>(Cttz->getOperand(1))) {
-          if (Const->isOne()) {  
-            I.setIsExact();
-            return true;
-          }
-        }
-      }
+    // Infer 'exact' flag if shift amount is cttz(x, 1) on the same operand.
+    if (match(I.getOperand(1), m_Intrinsic<Intrinsic::cttz>(
+                                   m_Specific(I.getOperand(0)), m_One()))) {
+      I.setIsExact();
+      return true;
     }
   }
 

>From a352ab2f97dc10738e3e25811dad50de967b01a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <we3223 at gmail.com>
Date: Sun, 27 Apr 2025 17:38:39 +0800
Subject: [PATCH 3/4] Add files via upload

---
 llvm/test/Transforms/InstCombine/select.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 3d81b72dd232e..e16f6ad2cfc9b 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -2736,7 +2736,7 @@ define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
 ; CHECK-LABEL: @pr47322_more_poisonous_replacement(
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
 ; CHECK-NEXT:    [[TRAILING:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[ARG]], i1 true)
-; CHECK-NEXT:    [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
+; CHECK-NEXT:    [[SHIFTED:%.*]] = lshr exact i32 [[ARG]], [[TRAILING]]
 ; CHECK-NEXT:    [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
 ; CHECK-NEXT:    ret i32 [[R1_SROA_0_1]]
 ;

>From 23a45356c4fe1771f3e66fa4a1cb56b8a15b77e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <we3223 at gmail.com>
Date: Sun, 27 Apr 2025 20:05:06 +0800
Subject: [PATCH 4/4] Update InstCombineShifts.cpp

---
 llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 280a414c77dae..fcf064e660d9f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -996,7 +996,7 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
     }
     // Infer 'exact' flag if shift amount is cttz(x, 1) on the same operand.
     if (match(I.getOperand(1), m_Intrinsic<Intrinsic::cttz>(
-                                   m_Specific(I.getOperand(0)), m_One()))) {
+                                   m_Specific(I.getOperand(0)), m_Value()))) {
       I.setIsExact();
       return true;
     }



More information about the llvm-commits mailing list