-
Notifications
You must be signed in to change notification settings - Fork 14k
[InstCombine] Combine trunc (lshr X, BW-1) to i1 --> icmp slt X, 0 (#142593) #143846
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: None (mayanksolanki393) ChangesFixes #142593, the issue was fixed using the suggestion on the ticket itself. Full diff: https://github.com/llvm/llvm-project/pull/143846.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 2db79228bf0e6..24e34b6a09543 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -823,6 +823,14 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
Value *And = Builder.CreateAnd(X, MaskC);
return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
}
+
+ if (match(Src, m_AShr(m_Value(X), m_SpecificInt(SrcWidth - 1))) ||
+ match(Src, m_LShr(m_Value(X), m_SpecificInt(SrcWidth - 1)))) {
+ // trunc (ashr X, BW-1) to i1 --> icmp slt X, 0
+ // trunc (lshr X, BW-1) to i1 --> icmp slt X, 0
+ return new ICmpInst(ICmpInst::ICMP_SLT, X, Zero);
+ }
+
if (match(Src, m_OneUse(m_c_Or(m_LShr(m_Value(X), m_ImmConstant(C)),
m_Deferred(X))))) {
// trunc (or (lshr X, C), X) to i1 --> icmp ne (and X, C'), 0
diff --git a/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll b/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll
new file mode 100644
index 0000000000000..74576cda76204
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll
@@ -0,0 +1,23 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define i1 @src(i32 %0, ptr writeonly captures(none) initializes((0, 4)) %p) local_unnamed_addr #0 {
+; CHECK-LABEL: define i1 @src(
+; CHECK-SAME: i32 [[TMP0:%.*]], ptr writeonly captures(none) initializes((0, 4)) [[P:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[COMMON_RET1:.*:]]
+; CHECK-NEXT: [[DOTLOBIT:%.*]] = lshr i32 [[TMP0]], 31
+; CHECK-NEXT: store i32 [[DOTLOBIT]], ptr [[P]], align 1
+; CHECK-NEXT: ret i1 false
+;
+common.ret1:
+ %.lobit = lshr i32 %0, 31
+ %1 = trunc nuw i32 %.lobit to i1
+ %2 = icmp slt i32 %0, 0
+ %not. = xor i1 %1, true
+ %common.ret1.op = select i1 %not., i1 %2, i1 false
+ store i32 %.lobit, ptr %p, align 1
+ ret i1 %common.ret1.op
+}
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
+
|
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.
ideally you should have some simpler positive/negative tests as well (just add an extra use() of the lshr/ashr result) - plus tests with incorrect shift amounts etc.
sure - better to push additional commits to the branch instead of rebasing if you can |
@dtcxzyw I have implemented the suggested changes |
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.
don't use dates in the test filename - just call it trunc-shr.ll
Implemented the suggested changes. I have made some additional changes to the test cases to match the overall style from other InstCombine related testcases. |
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.
Can you please add a link to the alive2 proof to the PR description?
I can see that FAIL: LLVM :: Transforms/InstCombine/logical-select.ll but I cannot reproduce them locally. Any suggestions? |
Lets ignore the flang case for now - but what happens if you regenerate logical-select.ll - do you see a diff? |
It seems to be moving some line around, but this doesn't seem related to the changes we made. Edit: Its related to our change, The test doesn't fail if I revert the patch.
|
The transformation is kicking for the following lines in the already present In |
It is unrelated. |
Is there anything else required from myside on this? |
Co-authored-by: Nikita Popov <github@npopov.com>
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.
LG
The |
I've merged against trunk to kick off another CI build - it doesn't look like the patch is responsible for the fail but let's see if we can confirm |
@mayanksolanki393 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! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/157/builds/30872 Here is the relevant piece of the build log for the reference
|
Fixes #142593, the issue was fixed using the suggestion on the ticket itself. Thanks @RKSimon
Godbolt: https://godbolt.org/z/oW5b74jc4
alive2 proof: https://alive2.llvm.org/ce/z/QHnD7e