-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Fold select(X >s 0, 0, -X) | smax(X, 0) to abs(X) #165200
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
Merged
wenju-he
merged 13 commits into
llvm:main
from
wenju-he:instcombine-or-select-smax-to-abs
Nov 2, 2025
+124
−5
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4629547
[InstCombine] Fold select(X >s 0, 0, -X) | smax(X, 0) to abs(X)
wenju-he af62083
named value, add negative test with multiple uses of @llvm.smax
wenju-he 21ca09a
rename %e -> %add
wenju-he 1e9a8ce
add TODO: fold to smax(neg, fold two smax
wenju-he e5d284b
Update llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
wenju-he e19950b
Update llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
wenju-he 28f1987
clang-format
wenju-he bd8b791
select(X <s 0, -X, 0) | smax(X, 0) --> abs(X)
wenju-he 154c2c1
update clang/test/CodeGen/SystemZ/builtins-systemz-zvector.c
wenju-he 3aaa55a
Merge branch 'main' into instcombine-or-select-smax-to-abs
wenju-he c01eb5a
rename or_lgt_select_smax_to_abs -> or_slt_select_smax_to_abs
wenju-he 4c92c7c
Update llvm/test/Transforms/InstCombine/or.ll
wenju-he d6eecb3
use m_SMax
wenju-he File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2113,3 +2113,98 @@ define <4 x i32> @or_zext_nneg_minus_constant_splat(<4 x i8> %a) { | |
| %or = or <4 x i32> %zext, splat (i32 -9) | ||
| ret <4 x i32> %or | ||
| } | ||
|
|
||
| define i8 @or_positive_minus_non_positive_to_abs(i8 %a){ | ||
| ; CHECK-LABEL: @or_positive_minus_non_positive_to_abs( | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.abs.i8(i8 [[A:%.*]], i1 false) | ||
| ; CHECK-NEXT: ret i8 [[TMP2]] | ||
| ; | ||
| %b = icmp sgt i8 %a, 0 | ||
| %mask = sext i1 %b to i8 | ||
| %neg = sub i8 0, %a | ||
| %mask_inv = xor i8 %mask, -1 | ||
| %c = and i8 %neg, %mask_inv | ||
| %d = and i8 %a, %mask | ||
| %or = or i8 %c, %d | ||
| ret i8 %or | ||
| } | ||
|
|
||
| ; TODO: Fold to smax https://alive2.llvm.org/ce/z/wDiDh2 | ||
| define i8 @or_select_smax_neg_to_abs(i8 %a){ | ||
| ; CHECK-LABEL: @or_select_smax_neg_to_abs( | ||
| ; CHECK-NEXT: [[SGT0:%.*]] = icmp sgt i8 [[A:%.*]], 0 | ||
| ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i8 0, [[A]] | ||
| ; CHECK-NEXT: [[OR:%.*]] = select i1 [[SGT0]], i8 0, i8 [[NEG]] | ||
| ; CHECK-NEXT: ret i8 [[OR]] | ||
| ; | ||
| %sgt0 = icmp sgt i8 %a, 0 | ||
| %neg = sub nsw i8 0, %a | ||
| %sel = select i1 %sgt0, i8 0, i8 %neg | ||
| ret i8 %sel | ||
| } | ||
|
|
||
| ; TODO: Fold to abs https://alive2.llvm.org/ce/z/DybfHG | ||
| define i8 @or_select_smax_smax_to_abs(i8 %a){ | ||
| ; CHECK-LABEL: @or_select_smax_smax_to_abs( | ||
| ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i8 0, [[A:%.*]] | ||
| ; CHECK-NEXT: [[SEL:%.*]] = call i8 @llvm.smax.i8(i8 [[NEG]], i8 0) | ||
| ; CHECK-NEXT: [[MAX:%.*]] = call i8 @llvm.smax.i8(i8 [[A]], i8 0) | ||
| ; CHECK-NEXT: [[OR:%.*]] = or i8 [[SEL]], [[MAX]] | ||
| ; CHECK-NEXT: ret i8 [[OR]] | ||
| ; | ||
| %neg = sub nsw i8 0, %a | ||
| %sel = call i8 @llvm.smax.i8(i8 %neg, i8 0) | ||
| %max = call i8 @llvm.smax.i8(i8 %a, i8 0) | ||
| %or = or i8 %sel, %max | ||
| ret i8 %or | ||
| } | ||
|
|
||
| declare i8 @llvm.abs.i8(i8, i1) | ||
| declare <2 x i8> @llvm.abs.v2i8(<2 x i8>, i1) | ||
|
|
||
| define <2 x i8> @or_sgt_select_smax_to_abs(<2 x i8> %a){ | ||
| ; CHECK-LABEL: @or_sgt_select_smax_to_abs( | ||
| ; CHECK-NEXT: [[OR:%.*]] = call <2 x i8> @llvm.abs.v2i8(<2 x i8> [[A:%.*]], i1 false) | ||
| ; CHECK-NEXT: ret <2 x i8> [[OR]] | ||
| ; | ||
| %sgt0 = icmp sgt <2 x i8> %a, zeroinitializer | ||
| %neg = sub <2 x i8> zeroinitializer, %a | ||
| %sel = select <2 x i1> %sgt0, <2 x i8> zeroinitializer, <2 x i8> %neg | ||
| %max = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %a, <2 x i8> zeroinitializer) | ||
| %or = or <2 x i8> %sel, %max | ||
| ret <2 x i8> %or | ||
| } | ||
|
|
||
| define <2 x i8> @or_slt_select_smax_to_abs(<2 x i8> %a){ | ||
| ; CHECK-LABEL: @or_slt_select_smax_to_abs( | ||
| ; CHECK-NEXT: [[OR:%.*]] = call <2 x i8> @llvm.abs.v2i8(<2 x i8> [[A:%.*]], i1 false) | ||
| ; CHECK-NEXT: ret <2 x i8> [[OR]] | ||
| ; | ||
| %slt0 = icmp slt <2 x i8> %a, zeroinitializer | ||
| %neg = sub <2 x i8> zeroinitializer, %a | ||
| %sel = select <2 x i1> %slt0, <2 x i8> %neg, <2 x i8> zeroinitializer | ||
| %max = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %a, <2 x i8> zeroinitializer) | ||
| %or = or <2 x i8> %sel, %max | ||
| ret <2 x i8> %or | ||
| } | ||
|
|
||
| ; negative test - %d has multiple uses. %or is not folded to abs. | ||
|
|
||
| define <2 x i8> @or_select_smax_multi_uses(<2 x i8> %a){ | ||
| ; CHECK-LABEL: @or_select_smax_multi_uses( | ||
| ; CHECK-NEXT: [[B:%.*]] = icmp sgt <2 x i8> [[A:%.*]], zeroinitializer | ||
| ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[A]] | ||
| ; CHECK-NEXT: [[C:%.*]] = select <2 x i1> [[B]], <2 x i8> zeroinitializer, <2 x i8> [[NEG]] | ||
| ; CHECK-NEXT: [[D:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[A]], <2 x i8> zeroinitializer) | ||
| ; CHECK-NEXT: [[OR1:%.*]] = or <2 x i8> [[C]], [[D]] | ||
| ; CHECK-NEXT: [[OR:%.*]] = add <2 x i8> [[OR1]], [[D]] | ||
| ; CHECK-NEXT: ret <2 x i8> [[OR]] | ||
| ; | ||
| %sgt0 = icmp sgt <2 x i8> %a, zeroinitializer | ||
| %neg = sub <2 x i8> zeroinitializer, %a | ||
| %sel = select <2 x i1> %sgt0, <2 x i8> zeroinitializer, <2 x i8> %neg | ||
| %max = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %a, <2 x i8> zeroinitializer) | ||
| %or = or <2 x i8> %sel, %max | ||
| %add = add <2 x i8> %or, %max | ||
| ret <2 x i8> %add | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing negative test for the multiple use case
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
done |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The SystemZ changes LGTM. In fact, this fixes a regression I hadn't even been aware of, which was introduced here: 1a60ae0
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.
thanks @uweigand for review