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

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 06:36:33 PDT 2025


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

Fix #131444 
This patch adds a  pattern . It match to infer the 'exact' flag on an 'lshr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand.

>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] 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.



More information about the llvm-commits mailing list