-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[InstCombine] Optimize (select %x, op(%x), 0) to op(%x) for operations where op(0) == 0 #147605
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
bababuck
wants to merge
7
commits into
llvm:main
Choose a base branch
from
bababuck:bababuck/SelectFixed0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−14
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
31d8680
Add tests for cases with (select %x, op(%x), 0) for operations where …
7a12f56
[InstCombine] Optimize (select %x, op(%x), 0) to op(%x) for operation…
ef056de
Reorder so that matching occurs before any data extraction
bababuck e08d66e
Use m_c_Intrinsic<Intrinisic::umin> instead of m_c_UMin
bababuck ed88808
Remove FIXED-ZERO prefix from select-fixed-zero.ll
bababuck 252d55f
Don't match with `shl`, `ashr`, and `ashl` because if the `shfamt` is…
bababuck 09536bb
Don't match with `udiv` and `sdiv` because if the denominator is 0 we…
bababuck 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
; RUN: opt -S -passes=instcombine < %s | FileCheck %s | ||
|
||
; (select (icmp x, 0, eq), 0, (umin x, y)) -> (umin x, y) | ||
define i64 @umin_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @umin_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[UMIN:%.*]] = call i64 @llvm.umin.i64(i64 [[A:%.*]], i64 [[B_FR]]) | ||
; FIXED-ZERO-NEXT: ret i64 [[UMIN]] | ||
; | ||
; CHECK-LABEL: @umin_select( | ||
; CHECK-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; CHECK-NEXT: [[UMIN:%.*]] = call i64 @llvm.umin.i64(i64 [[A:%.*]], i64 [[B_FR]]) | ||
; CHECK-NEXT: ret i64 [[UMIN]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%umin = call i64 @llvm.umin.i64(i64 %a, i64 %b) | ||
%select = select i1 %cond, i64 0, i64 %umin | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (mul x, y)) -> (mul x, y) | ||
define i64 @mul_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @mul_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[MUL:%.*]] = mul i64 [[A:%.*]], [[B_FR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[MUL]] | ||
; | ||
; CHECK-LABEL: @mul_select( | ||
; CHECK-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; CHECK-NEXT: [[MUL:%.*]] = mul i64 [[A:%.*]], [[B_FR]] | ||
; CHECK-NEXT: ret i64 [[MUL]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%mul = mul i64 %a, %b | ||
%select = select i1 %cond, i64 0, i64 %mul | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (shl x, y)) -> (shl x, y) | ||
define i64 @shl_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @shl_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[SHL:%.*]] = shl i64 [[A:%.*]], [[B_FR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[SHL]] | ||
; | ||
; CHECK-LABEL: @shl_select( | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[A]], [[B_FR:%.*]] | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i64 0, i64 [[SHL]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%shl = shl i64 %a, %b | ||
%select = select i1 %cond, i64 0, i64 %shl | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (and x, y)) -> (and x, y) | ||
define i64 @and_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @and_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[AND:%.*]] = and i64 [[A:%.*]], [[B_FR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[AND]] | ||
; | ||
; CHECK-LABEL: @and_select( | ||
; CHECK-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; CHECK-NEXT: [[AND:%.*]] = and i64 [[A:%.*]], [[B_FR]] | ||
; CHECK-NEXT: ret i64 [[AND]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%and = and i64 %a, %b | ||
%select = select i1 %cond, i64 0, i64 %and | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, ne), (ashr x, y), 0) -> (ashr x, y) | ||
define i64 @ashr_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @ashr_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[ASHR:%.*]] = ashr i64 [[A:%.*]], [[B_FR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[ASHR]] | ||
; | ||
; CHECK-LABEL: @ashr_select( | ||
; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[ASHR:%.*]] = ashr i64 [[A]], [[B_FR:%.*]] | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND_NOT]], i64 0, i64 [[ASHR]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp ne i64 0, %a | ||
%ashr = ashr i64 %a, %b | ||
%select = select i1 %cond, i64 %ashr, i64 0 | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, ne), (lshr x, y), 0) -> (lshr x, y) | ||
define i64 @lshr_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @lshr_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[LSHR:%.*]] = lshr i64 [[A:%.*]], [[B_FR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[LSHR]] | ||
; | ||
; CHECK-LABEL: @lshr_select( | ||
; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[LSHR:%.*]] = lshr i64 [[A]], [[B_FR:%.*]] | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND_NOT]], i64 0, i64 [[LSHR]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp ne i64 0, %a | ||
%lshr = lshr i64 %a, %b | ||
%select = select i1 %cond, i64 %lshr, i64 0 | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, fshr(x, x, y)) -> fshr(x, x, y) | ||
define i64 @fshr_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @fshr_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[FSHR:%.*]] = call i64 @llvm.fshr.i64(i64 [[A:%.*]], i64 [[A]], i64 [[B_FR]]) | ||
; FIXED-ZERO-NEXT: ret i64 [[FSHR]] | ||
; | ||
; CHECK-LABEL: @fshr_select( | ||
; CHECK-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; CHECK-NEXT: [[FSHR:%.*]] = call i64 @llvm.fshr.i64(i64 [[A:%.*]], i64 [[A]], i64 [[B_FR]]) | ||
; CHECK-NEXT: ret i64 [[FSHR]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%fshr = call i64 @llvm.fshr.i64(i64 %a, i64 %a, i64 %b) | ||
%select = select i1 %cond, i64 0, i64 %fshr | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (fshl x, x, y)) -> (fshl x, x, y) | ||
define i64 @fshl_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @fshl_select( | ||
; FIXED-ZERO-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; FIXED-ZERO-NEXT: [[FSHL:%.*]] = call i64 @llvm.fshl.i64(i64 [[A:%.*]], i64 [[A]], i64 [[B_FR]]) | ||
; FIXED-ZERO-NEXT: ret i64 [[FSHL]] | ||
; | ||
; CHECK-LABEL: @fshl_select( | ||
; CHECK-NEXT: [[B_FR:%.*]] = freeze i64 [[B:%.*]] | ||
; CHECK-NEXT: [[FSHL:%.*]] = call i64 @llvm.fshl.i64(i64 [[A:%.*]], i64 [[A]], i64 [[B_FR]]) | ||
; CHECK-NEXT: ret i64 [[FSHL]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%fshl = call i64 @llvm.fshl.i64(i64 %a, i64 %a, i64 %b) | ||
%select = select i1 %cond, i64 0, i64 %fshl | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (fshr x, z, y)) -> leave as is | ||
define i64 @fshr_select_no_combine(i64 %a, i64 %b, i64 %c) { | ||
; FIXED-ZERO-LABEL: @fshr_select_no_combine( | ||
; FIXED-ZERO-NEXT: [[COND:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; FIXED-ZERO-NEXT: [[FSHR:%.*]] = call i64 @llvm.fshr.i64(i64 [[A]], i64 [[B:%.*]], i64 [[C:%.*]]) | ||
; FIXED-ZERO-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i64 0, i64 [[FSHR]] | ||
; FIXED-ZERO-NEXT: ret i64 [[SELECT]] | ||
; | ||
; CHECK-LABEL: @fshr_select_no_combine( | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[FSHR:%.*]] = call i64 @llvm.fshr.i64(i64 [[A]], i64 [[B:%.*]], i64 [[C:%.*]]) | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i64 0, i64 [[FSHR]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%fshr = call i64 @llvm.fshr.i64(i64 %a, i64 %b, i64 %c) | ||
%select = select i1 %cond, i64 0, i64 %fshr | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (sdiv x, y)) -> (sdiv x, y) | ||
define i64 @sdiv_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @sdiv_select( | ||
; FIXED-ZERO-NEXT: [[B:%.*]] = freeze i64 [[B1:%.*]] | ||
; FIXED-ZERO-NEXT: [[DIV:%.*]] = sdiv i64 [[A:%.*]], [[B]] | ||
; FIXED-ZERO-NEXT: ret i64 [[DIV]] | ||
; | ||
; CHECK-LABEL: @sdiv_select( | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[DIV:%.*]] = sdiv i64 [[A]], [[B_FR:%.*]] | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i64 0, i64 [[DIV]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%div = sdiv i64 %a, %b | ||
%select = select i1 %cond, i64 0, i64 %div | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (udiv x, y)) -> (udiv x, y) | ||
define i64 @udiv_select(i64 %a, i64 %b) { | ||
; FIXED-ZERO-LABEL: @udiv_select( | ||
; FIXED-ZERO-NEXT: [[B:%.*]] = freeze i64 [[B1:%.*]] | ||
; FIXED-ZERO-NEXT: [[DIV:%.*]] = udiv i64 [[A:%.*]], [[B]] | ||
; FIXED-ZERO-NEXT: ret i64 [[DIV]] | ||
; | ||
; CHECK-LABEL: @udiv_select( | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: [[DIV:%.*]] = udiv i64 [[A]], [[B_FR:%.*]] | ||
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], i64 0, i64 [[DIV]] | ||
; CHECK-NEXT: ret i64 [[SELECT]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%div = udiv i64 %a, %b | ||
%select = select i1 %cond, i64 0, i64 %div | ||
ret i64 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (icmp x, 0, slt)) -> (icmp x, 0, slt) | ||
define i1 @icmp_slt_select(i64 %a) { | ||
; FIXED-ZERO-LABEL: @icmp_slt_select( | ||
; FIXED-ZERO-NEXT: [[ICMP:%.*]] = icmp slt i64 [[A:%.*]], 0 | ||
; FIXED-ZERO-NEXT: ret i1 [[ICMP]] | ||
; | ||
; CHECK-LABEL: @icmp_slt_select( | ||
; CHECK-NEXT: [[ICMP:%.*]] = icmp slt i64 [[A:%.*]], 0 | ||
; CHECK-NEXT: ret i1 [[ICMP]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%icmp = icmp slt i64 %a, 0 | ||
%select = select i1 %cond, i1 0, i1 %icmp | ||
ret i1 %select | ||
} | ||
|
||
; (select (icmp x, 0, eq), 0, (sub 0, x)) -> (sub 0, x) | ||
define i64 @sub_select(i64 %a) { | ||
; FIXED-ZERO-LABEL: @sub_select( | ||
; FIXED-ZERO-NEXT: [[SUB:%.*]] = sub i64 0, [[A:%.*]] | ||
; FIXED-ZERO-NEXT: ret i64 [[SUB]] | ||
; | ||
; CHECK-LABEL: @sub_select( | ||
; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[A:%.*]] | ||
; CHECK-NEXT: ret i64 [[SUB]] | ||
; | ||
%cond = icmp eq i64 %a, 0 | ||
%sub = sub i64 0, %a | ||
%select = select i1 %cond, i64 0, i64 %sub | ||
ret i64 %select | ||
} |
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
Oops, something went wrong.
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.
Crash reproducer:
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.
The issue was that I moved the pattern match to the end of the function. However, one side effect of the pattern match is that it guarantees that the types of TrueV and the Conditional constant Match which is assumed to be true by:
Reordering to perform the pattern matching where it was before this MR solved this issue.