Skip to content

[InstCombine] Infer exact for lshr by cttz #136696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

houngkoungting
Copy link

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.

@houngkoungting houngkoungting requested a review from nikic as a code owner April 22, 2025 13:36
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2025

@llvm/pr-subscribers-llvm-transforms

Author: 黃國庭 (houngkoungting)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/136696.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (+12)
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.

@tclin914
Copy link
Contributor

Could you provide the testcase for this change

@tclin914 tclin914 self-requested a review April 22, 2025 14:12
@nikic
Copy link
Contributor

nikic commented Apr 22, 2025

Note that an existing InstCombine test is failing as well, please update it.

@@ -994,6 +994,18 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
I.setIsExact();
return true;
}
//Fix #131444
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't reference the issue, instead add a compact comment on what the transform does. Like // Infer exact on shift by cttz.

//Fix #131444
if (auto *Cttz = dyn_cast<IntrinsicInst>(I.getOperand(1))) {
if (Cttz->getIntrinsicID() == Intrinsic::cttz &&
Cttz->getOperand(0) == I.getOperand(0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the m_Intrinsic matcher.

if (Cttz->getIntrinsicID() == Intrinsic::cttz &&
Cttz->getOperand(0) == I.getOperand(0)) {
if (auto *Const = dyn_cast<ConstantInt>(Cttz->getOperand(1))) {
if (Const->isOne()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the second parameter to be one? Can you please also add an alive2 proof for the transform to the PR description?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI @nikic , I think it has to be this way, because if i8 src = 0 and we shift by 8 bits, it doesn't feel right.
From https://llvm.org/docs/LangRef.html#llvm-cttz-intrinsic

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index c39b7dae4..4faa719fa 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -994,12 +994,12 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
       I.setIsExact();
       return true;
     }
-    //Fix #131444
+    // 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()) {  
+          if (Const->isOne()) {
             I.setIsExact();
             return true;
           }

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests to demonstrate the change. See also https://llvm.org/docs/InstCombineContributorGuide.html#tests

@houngkoungting
Copy link
Author

HI @nikic @tclin914 @dtcxzyw , I think I missed several steps.
Should I close this PR and open a new one with everything fixed? Thanks!

@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 24, 2025

Should I close this PR and open a new one with everything fixed? Thanks!

It is okay to fix them in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[InstCombine] Infer exact for lshr by cttz
5 participants