-
Notifications
You must be signed in to change notification settings - Fork 15k
[SimplifyCFG]: Switch on umin replaces default #164097
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
kper
wants to merge
11
commits into
llvm:main
Choose a base branch
from
kper:162111-simplify-cfg-umin
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.
+340
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fba6d47
[SimplifyCFG]: Remove unreachable when switch with umin
kper c131865
[SimplifyCFG]: Added test
kper 4866e8f
[SimplifyCFG]: Addressed feedback
kper 27cfa8b
[SimplifyCFG]: Updated weights
kper 8a5bd8c
[SimplifyCFG]: Remove dead cases
kper 64443b7
[SimplifyCFG]: Remove header
kper cf5a0c2
[SimplifyCFG]: Addressed feedback
kper 7443e6e
[SimplifyCFG]: Update default case weight
kper 8865c3f
[SimplifyCFG]: Addressed feedback
kper bf76fa4
[SimplifyCFG]: Remove dead cases when umin pattern matches
kper fb77844
[SimplifyCFG]: Fixed return when dead cases are eliminated
kper 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
Some comments aren't visible on the classic Files Changed page.
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7540,6 +7540,82 @@ static bool reduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, | |||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| /// Tries to transform the switch when the condition is umin with a constant. | ||||||||
| /// In that case, the default branch can be replaced by the constant's branch. | ||||||||
| /// This method also removes dead cases when the simplification cannot replace | ||||||||
| /// the default branch. | ||||||||
| /// | ||||||||
| /// For example: | ||||||||
| /// switch(umin(a, 3)) { | ||||||||
| /// case 0: | ||||||||
| /// case 1: | ||||||||
| /// case 2: | ||||||||
| /// case 3: | ||||||||
| /// case 4: | ||||||||
| /// // ... | ||||||||
| /// default: | ||||||||
| /// unreachable | ||||||||
| /// } | ||||||||
| /// | ||||||||
| /// Transforms into: | ||||||||
| /// | ||||||||
| /// switch(a) { | ||||||||
| /// case 0: | ||||||||
| /// case 1: | ||||||||
| /// case 2: | ||||||||
| /// default: | ||||||||
| /// // This is case 3 | ||||||||
| /// } | ||||||||
| static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) { | ||||||||
| Value *A; | ||||||||
| ConstantInt *Constant; | ||||||||
|
|
||||||||
| if (!match(SI->getCondition(), m_UMin(m_Value(A), m_ConstantInt(Constant)))) | ||||||||
| return false; | ||||||||
|
|
||||||||
| SmallVector<DominatorTree::UpdateType> Updates; | ||||||||
| SwitchInstProfUpdateWrapper SIW(*SI); | ||||||||
| BasicBlock *BB = SIW->getParent(); | ||||||||
|
|
||||||||
| // Dead cases are removed even when the simplification fails. | ||||||||
| // A case is dead when its value is higher than the Constant. | ||||||||
| SmallVector<ConstantInt *, 4> DeadCases; | ||||||||
| for (auto Case : SI->cases()) | ||||||||
| if (Case.getCaseValue()->getValue().ugt(Constant->getValue())) | ||||||||
| DeadCases.push_back(Case.getCaseValue()); | ||||||||
|
|
||||||||
| for (ConstantInt *DeadCaseVal : DeadCases) { | ||||||||
| SwitchInst::CaseIt DeadCase = SIW->findCaseValue(DeadCaseVal); | ||||||||
| BasicBlock *DeadCaseBB = DeadCase->getCaseSuccessor(); | ||||||||
| DeadCaseBB->removePredecessor(SIW->getParent()); | ||||||||
| SIW.removeCase(DeadCase); | ||||||||
| Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB}); | ||||||||
|
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.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| auto Case = SI->findCaseValue(Constant); | ||||||||
| // If the case value is not found, `findCaseValue` returns the default case. | ||||||||
| // In this scenario, since there is no explicit `case 3:`, the simplification | ||||||||
| // fails. The simplification also fails when the switch’s default destination | ||||||||
| // is reachable. | ||||||||
| if (!SI->defaultDestUnreachable() || Case == SI->case_default()) { | ||||||||
| if (DTU) | ||||||||
| DTU->applyUpdates(Updates); | ||||||||
| return !Updates.empty(); | ||||||||
| } | ||||||||
|
|
||||||||
| BasicBlock *Unreachable = SI->getDefaultDest(); | ||||||||
| SIW.replaceDefaultDest(Case); | ||||||||
| SIW.removeCase(Case); | ||||||||
| SIW->setCondition(A); | ||||||||
|
|
||||||||
| Updates.push_back({DominatorTree::Delete, BB, Unreachable}); | ||||||||
|
|
||||||||
| if (DTU) | ||||||||
| DTU->applyUpdates(Updates); | ||||||||
|
|
||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| /// Tries to transform switch of powers of two to reduce switch range. | ||||||||
| /// For example, switch like: | ||||||||
| /// switch (C) { case 1: case 2: case 64: case 128: } | ||||||||
|
|
@@ -7966,6 +8042,9 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { | |||||||
| if (simplifyDuplicateSwitchArms(SI, DTU)) | ||||||||
| return requestResimplify(); | ||||||||
|
|
||||||||
| if (simplifySwitchWhenUMin(SI, DTU)) | ||||||||
| return requestResimplify(); | ||||||||
|
|
||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
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,246 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: opt -S -passes=simplifycfg < %s | FileCheck %s | ||
|
|
||
| declare void @a() | ||
| declare void @b() | ||
| declare void @c() | ||
| declare void @d() | ||
|
|
||
| define void @switch_replace_default(i32 %x) { | ||
| ; CHECK-LABEL: define void @switch_replace_default( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 3) | ||
| ; CHECK-NEXT: switch i32 [[X]], label %[[COMMON_RET:.*]] [ | ||
| ; CHECK-NEXT: i32 0, label %[[CASE0:.*]] | ||
| ; CHECK-NEXT: i32 1, label %[[CASE1:.*]] | ||
| ; CHECK-NEXT: i32 2, label %[[CASE2:.*]] | ||
| ; CHECK-NEXT: ], !prof [[PROF0:![0-9]+]] | ||
| ; CHECK: [[COMMON_RET]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: [[CASE0]]: | ||
| ; CHECK-NEXT: call void @a() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE1]]: | ||
| ; CHECK-NEXT: call void @b() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE2]]: | ||
| ; CHECK-NEXT: call void @c() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; | ||
| %min = call i32 @llvm.umin.i32(i32 %x, i32 3) | ||
| switch i32 %min, label %unreachable [ | ||
| i32 0, label %case0 | ||
| i32 1, label %case1 | ||
| i32 2, label %case2 | ||
| i32 3, label %case3 | ||
| ], !prof !0 | ||
|
|
||
| case0: | ||
| call void @a() | ||
| ret void | ||
|
|
||
| case1: | ||
| call void @b() | ||
| ret void | ||
|
|
||
| case2: | ||
| call void @c() | ||
| ret void | ||
|
|
||
| case3: | ||
| ret void | ||
|
|
||
| unreachable: | ||
| unreachable | ||
| } | ||
|
|
||
| define void @switch_replace_default_and_remove_dead_cases(i32 %x) { | ||
| ; CHECK-LABEL: define void @switch_replace_default_and_remove_dead_cases( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 3) | ||
dianqk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ; CHECK-NEXT: switch i32 [[X]], label %[[COMMON_RET:.*]] [ | ||
| ; CHECK-NEXT: i32 2, label %[[CASE2:.*]] | ||
| ; CHECK-NEXT: i32 1, label %[[CASE1:.*]] | ||
| ; CHECK-NEXT: ] | ||
| ; CHECK: [[COMMON_RET]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: [[CASE1]]: | ||
| ; CHECK-NEXT: call void @b() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE2]]: | ||
| ; CHECK-NEXT: call void @c() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; | ||
| %min = call i32 @llvm.umin.i32(i32 %x, i32 3) | ||
| switch i32 %min, label %unreachable [ | ||
| i32 4, label %case4 | ||
| i32 1, label %case1 | ||
| i32 2, label %case2 | ||
| i32 3, label %case3 | ||
| ] | ||
|
|
||
| case4: | ||
| call void @a() | ||
| ret void | ||
|
|
||
| case1: | ||
| call void @b() | ||
| ret void | ||
|
|
||
| case2: | ||
| call void @c() | ||
| ret void | ||
|
|
||
| case3: | ||
| ret void | ||
|
|
||
| unreachable: | ||
| unreachable | ||
| } | ||
|
|
||
| define void @switch_replace_default_when_holes(i32 %x) { | ||
| ; CHECK-LABEL: define void @switch_replace_default_when_holes( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 3) | ||
| ; CHECK-NEXT: switch i32 [[X]], label %[[COMMON_RET:.*]] [ | ||
| ; CHECK-NEXT: i32 1, label %[[CASE1:.*]] | ||
| ; CHECK-NEXT: i32 2, label %[[CASE2:.*]] | ||
| ; CHECK-NEXT: ] | ||
| ; CHECK: [[COMMON_RET]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: [[CASE1]]: | ||
| ; CHECK-NEXT: call void @b() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE2]]: | ||
| ; CHECK-NEXT: call void @c() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; | ||
| %min = call i32 @llvm.umin.i32(i32 %x, i32 3) | ||
| switch i32 %min, label %unreachable [ | ||
| i32 1, label %case1 | ||
| i32 2, label %case2 | ||
| i32 3, label %case3 | ||
| ] | ||
|
|
||
| case1: | ||
| call void @b() | ||
| ret void | ||
|
|
||
| case2: | ||
| call void @c() | ||
| ret void | ||
|
|
||
| case3: | ||
| ret void | ||
|
|
||
| unreachable: | ||
| unreachable | ||
| } | ||
|
|
||
| define void @do_not_switch_replace_default(i32 %x, i32 %y) { | ||
| ; CHECK-LABEL: define void @do_not_switch_replace_default( | ||
| ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 [[Y]]) | ||
| ; CHECK-NEXT: switch i32 [[MIN]], label %[[UNREACHABLE:.*]] [ | ||
| ; CHECK-NEXT: i32 0, label %[[CASE0:.*]] | ||
| ; CHECK-NEXT: i32 1, label %[[CASE1:.*]] | ||
| ; CHECK-NEXT: i32 2, label %[[CASE2:.*]] | ||
| ; CHECK-NEXT: i32 3, label %[[COMMON_RET:.*]] | ||
| ; CHECK-NEXT: ] | ||
| ; CHECK: [[COMMON_RET]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: [[CASE0]]: | ||
| ; CHECK-NEXT: call void @a() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE1]]: | ||
| ; CHECK-NEXT: call void @b() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE2]]: | ||
| ; CHECK-NEXT: call void @c() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[UNREACHABLE]]: | ||
| ; CHECK-NEXT: unreachable | ||
| ; | ||
| %min = call i32 @llvm.umin.i32(i32 %x, i32 %y) | ||
| switch i32 %min, label %unreachable [ | ||
| i32 0, label %case0 | ||
| i32 1, label %case1 | ||
| i32 2, label %case2 | ||
| i32 3, label %case3 | ||
| ] | ||
|
|
||
| case0: | ||
| call void @a() | ||
| ret void | ||
|
|
||
| case1: | ||
| call void @b() | ||
| ret void | ||
|
|
||
| case2: | ||
| call void @c() | ||
| ret void | ||
|
|
||
| case3: | ||
| ret void | ||
|
|
||
| unreachable: | ||
| unreachable | ||
| } | ||
|
|
||
| define void @do_not_replace_switch_default_but_remove_dead_cases(i32 %x) { | ||
| ; CHECK-LABEL: define void @do_not_replace_switch_default_but_remove_dead_cases( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 3) | ||
| ; CHECK-NEXT: switch i32 [[MIN]], label %[[CASE0:.*]] [ | ||
| ; CHECK-NEXT: i32 3, label %[[COMMON_RET:.*]] | ||
| ; CHECK-NEXT: i32 1, label %[[CASE1:.*]] | ||
| ; CHECK-NEXT: i32 2, label %[[CASE2:.*]] | ||
| ; CHECK-NEXT: ] | ||
| ; CHECK: [[COMMON_RET]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: [[CASE0]]: | ||
| ; CHECK-NEXT: call void @a() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE1]]: | ||
| ; CHECK-NEXT: call void @b() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; CHECK: [[CASE2]]: | ||
| ; CHECK-NEXT: call void @c() | ||
| ; CHECK-NEXT: br label %[[COMMON_RET]] | ||
| ; | ||
| %min = call i32 @llvm.umin.i32(i32 %x, i32 3) | ||
| switch i32 %min, label %case0 [ ; default is reachable, therefore simplification not triggered | ||
| i32 0, label %case0 | ||
| i32 1, label %case1 | ||
| i32 2, label %case2 | ||
| i32 3, label %case3 | ||
| i32 4, label %case4 | ||
| ] | ||
|
|
||
| case0: | ||
| call void @a() | ||
| ret void | ||
|
|
||
| case1: | ||
| call void @b() | ||
| ret void | ||
|
|
||
| case2: | ||
| call void @c() | ||
| ret void | ||
|
|
||
| case3: | ||
| ret void | ||
|
|
||
| case4: | ||
| call void @d() | ||
| ret void | ||
|
|
||
| } | ||
|
|
||
|
|
||
| !0 = !{!"branch_weights", i32 1, i32 2, i32 3, i32 99, i32 5} | ||
| ;. | ||
| ; CHECK: [[PROF0]] = !{!"branch_weights", i32 5, i32 2, i32 3, i32 99} | ||
| ;. | ||
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.
What about
[4,1,2,3]or[1,2,3]?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.
Dead edges should be removed. Otherwise it will cause miscompilation: https://alive2.llvm.org/ce/z/Faeck4
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, I mean we can remove dead edges: https://alive2.llvm.org/ce/z/hC3Dbm.
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.
I think this needs to be explicitly checked in the transform, we shouldn't rely on eliminateDeadSwitchCases having removed such cases (looking at the implementation, it uses known bits rather than ranges, so I think it may not eliminate all dead cases if the umin is not at a power of two boundary).
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 :)
I have added a new commit which deletes cases where the value is higher than the constant
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.
Missing test cases? I think you need add both tests of [1,2,3] that has holes and [4,1,2,3]?