-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
Conversation
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 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. |
@llvm/pr-subscribers-llvm-transforms Author: 黃國庭 (houngkoungting) ChangesFix #131444 Full diff: https://github.com/llvm/llvm-project/pull/136696.diff 1 Files Affected:
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.
|
Could you provide the testcase for this change |
Note that an existing InstCombine test is failing as well, please update it. |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this 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
It is okay to fix them in this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the proofs, please prefer linking to alive2.llvm.org instead of directly embedding the proof in the PR, e.g. like this: https://alive2.llvm.org/ce/z/CQR2PG
Note that this also shows that you do not need the second argument to be true. The reason false works is that if the input is zero, cttz will return the bit width and shifting by the bit width returns poison. Adding an extra exact flag doesn't affect this.
HI @nikic , I’m not sure if it should be like the cttz-shift-exact.ll I updated.Please take a look when you have time. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good now.
%sh = ashr i32 %x, %cttz | ||
ret i32 %sh | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add one negative test where the cttz operand is an unrelated value.
HI @nikic ,I've added the negative test case where the cttz operand is unrelated.Please take a look when you have time. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@houngkoungting Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand. Proof: https://alive2.llvm.org/ce/z/CQR2PG Fixes llvm#131444.
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand. Proof: https://alive2.llvm.org/ce/z/CQR2PG Fixes llvm#131444.
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand. Proof: https://alive2.llvm.org/ce/z/CQR2PG Fixes llvm#131444.
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand. Proof: https://alive2.llvm.org/ce/z/CQR2PG Fixes llvm#131444.
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand. Proof: https://alive2.llvm.org/ce/z/CQR2PG Fixes llvm#131444.
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand.
Proof: https://alive2.llvm.org/ce/z/CQR2PG
Fixes #131444.