-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Simplify switch with selects #84143
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
||
define void @test_ult_rhsc(i8 %x) { | ||
; CHECK-LABEL: define void @test_ult_rhsc( | ||
; CHECK-SAME: i8 [[X:%.*]]) { | ||
; CHECK-NEXT: switch i8 [[X]], label [[BB1:%.*]] [ | ||
; CHECK-NEXT: i8 2, label [[BB2:%.*]] | ||
; CHECK-NEXT: i8 12, label [[BB3:%.*]] | ||
; CHECK-NEXT: ] | ||
; CHECK: bb1: | ||
; CHECK-NEXT: call void @func1() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb2: | ||
; CHECK-NEXT: call void @func2() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb3: | ||
; CHECK-NEXT: call void @func3() | ||
; CHECK-NEXT: unreachable | ||
; | ||
%val = add nsw i8 %x, -2 | ||
%cmp = icmp ult i8 %val, 11 | ||
%cond = select i1 %cmp, i8 %val, i8 6 | ||
switch i8 %cond, label %bb1 [ | ||
i8 0, label %bb2 | ||
i8 10, label %bb3 | ||
] | ||
|
||
bb1: | ||
call void @func1() | ||
unreachable | ||
bb2: | ||
call void @func2() | ||
unreachable | ||
bb3: | ||
call void @func3() | ||
unreachable | ||
} | ||
|
||
define void @test_eq_lhsc(i8 %x) { | ||
; CHECK-LABEL: define void @test_eq_lhsc( | ||
; CHECK-SAME: i8 [[X:%.*]]) { | ||
; CHECK-NEXT: switch i8 [[X]], label [[BB1:%.*]] [ | ||
; CHECK-NEXT: i8 0, label [[BB2:%.*]] | ||
; CHECK-NEXT: i8 10, label [[BB3:%.*]] | ||
; CHECK-NEXT: ] | ||
; CHECK: bb1: | ||
; CHECK-NEXT: call void @func1() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb2: | ||
; CHECK-NEXT: call void @func2() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb3: | ||
; CHECK-NEXT: call void @func3() | ||
; CHECK-NEXT: unreachable | ||
; | ||
%cmp = icmp eq i8 %x, 4 | ||
%cond = select i1 %cmp, i8 6, i8 %x | ||
switch i8 %cond, label %bb1 [ | ||
i8 0, label %bb2 | ||
i8 10, label %bb3 | ||
] | ||
|
||
bb1: | ||
call void @func1() | ||
unreachable | ||
bb2: | ||
call void @func2() | ||
unreachable | ||
bb3: | ||
call void @func3() | ||
unreachable | ||
} | ||
|
||
define void @test_ult_rhsc_invalid_cond(i8 %x, i8 %y) { | ||
; CHECK-LABEL: define void @test_ult_rhsc_invalid_cond( | ||
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[VAL:%.*]] = add nsw i8 [[X]], -2 | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[Y]], 11 | ||
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i8 [[VAL]], i8 6 | ||
; CHECK-NEXT: switch i8 [[COND]], label [[BB1:%.*]] [ | ||
; CHECK-NEXT: i8 0, label [[BB2:%.*]] | ||
; CHECK-NEXT: i8 10, label [[BB3:%.*]] | ||
; CHECK-NEXT: i8 13, label [[BB3]] | ||
; CHECK-NEXT: ] | ||
; CHECK: bb1: | ||
; CHECK-NEXT: call void @func1() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb2: | ||
; CHECK-NEXT: call void @func2() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb3: | ||
; CHECK-NEXT: call void @func3() | ||
; CHECK-NEXT: unreachable | ||
; | ||
%val = add nsw i8 %x, -2 | ||
%cmp = icmp ult i8 %y, 11 | ||
%cond = select i1 %cmp, i8 %val, i8 6 | ||
switch i8 %cond, label %bb1 [ | ||
i8 0, label %bb2 | ||
i8 10, label %bb3 | ||
i8 13, label %bb3 | ||
] | ||
|
||
bb1: | ||
call void @func1() | ||
unreachable | ||
bb2: | ||
call void @func2() | ||
unreachable | ||
bb3: | ||
call void @func3() | ||
unreachable | ||
} | ||
|
||
define void @test_ult_rhsc_fail(i8 %x) { | ||
; CHECK-LABEL: define void @test_ult_rhsc_fail( | ||
; CHECK-SAME: i8 [[X:%.*]]) { | ||
; CHECK-NEXT: [[VAL:%.*]] = add nsw i8 [[X]], -2 | ||
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[VAL]], 11 | ||
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i8 [[VAL]], i8 6 | ||
; CHECK-NEXT: switch i8 [[COND]], label [[BB1:%.*]] [ | ||
; CHECK-NEXT: i8 0, label [[BB2:%.*]] | ||
; CHECK-NEXT: i8 10, label [[BB3:%.*]] | ||
; CHECK-NEXT: i8 13, label [[BB3]] | ||
; CHECK-NEXT: ] | ||
; CHECK: bb1: | ||
; CHECK-NEXT: call void @func1() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb2: | ||
; CHECK-NEXT: call void @func2() | ||
; CHECK-NEXT: unreachable | ||
; CHECK: bb3: | ||
; CHECK-NEXT: call void @func3() | ||
; CHECK-NEXT: unreachable | ||
; | ||
%val = add nsw i8 %x, -2 | ||
%cmp = icmp ult i8 %val, 11 | ||
%cond = select i1 %cmp, i8 %val, i8 6 | ||
switch i8 %cond, label %bb1 [ | ||
i8 0, label %bb2 | ||
i8 10, label %bb3 | ||
i8 13, label %bb3 | ||
] | ||
|
||
bb1: | ||
call void @func1() | ||
unreachable | ||
bb2: | ||
call void @func2() | ||
unreachable | ||
bb3: | ||
call void @func3() | ||
unreachable | ||
} | ||
|
||
declare void @func1() | ||
declare void @func2() | ||
declare void @func3() |
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.
Is there a reason to only handle
RHSC
?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.
We can improve this by using KnownBits/ConstantRange in the future. But I guess this patch covers most of the cases in rust applications.
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.
Think you could also technically generalize to
RHSC
(or Other arm really) implying any single case and not condition implying the same condition for the arm, although not sure if that would practically ever come up. Think we would just simplify the select.