-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SimplifyCFG] Simplify switch instruction that has duplicate arms #114262
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 33 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
fbfa46a
[SimplifyCFG] precommit tests for simplify switch with duplicate arms
michaelmaitland 2db9f00
[SimplifyCFG] Simplify switch instruction that has duplicate arms
michaelmaitland 4e56067
fixup! respond to review
michaelmaitland 9e10655
fixup! refactor for general approach
michaelmaitland 64ddee6
fixup! move PHI checks
michaelmaitland c8eecb6
fixup! make it O(n)
michaelmaitland 9e9afc2
fixup! use successor BBs in hash
michaelmaitland ef1bc74
fixup! use hasNPredsOrMore for early exit capability
michaelmaitland 794d4e8
fixup! don't add to Cases if size() != 1 to improve performance
michaelmaitland 32b627f
fixup! precompute getIncomingValueForBlock
michaelmaitland 4190352
fixup! only support unconditional branches
michaelmaitland f04d67f
fixup! try improving getHashValue
michaelmaitland 4ca2777
fixup! nitty cleanup
michaelmaitland 28620df
fixup! don't reproccess map for same BB
michaelmaitland 9dcf124
fixup! avoid calls to getIncomingValueForBlock
michaelmaitland 41edd15
fixup! drop Seen vector since we no longer build phi map in loop
michaelmaitland 5c247db
fixup! respond to review
michaelmaitland c3a1361
fixup! some cleanup and add Seen checks
michaelmaitland b563080
fixup! try and avoid some resizes
michaelmaitland 67b3c80
fixup! presize PhiPredIVs based on first pass data
michaelmaitland 94ff86e
fixup! use reserve instead of resize
michaelmaitland c6a5e52
fixup! precompute incoming values from BB
michaelmaitland 241fef5
fixup! don't hash an empty set of values
michaelmaitland aa4cf43
fixup! add some comments
michaelmaitland 8153c4b
fixup! simplify getHashValue
michaelmaitland d1cd111
fixup! another attempt at fixing hashing
michaelmaitland 4949890
fixup! fix crashes
michaelmaitland e91253c
fixup~ Revert changes that precompute for getHashValue.
michaelmaitland d87ea1d
fixup! use SmallVector instead of vector
michaelmaitland be55920
fixup! update dom tree
michaelmaitland 085b048
fixup! use get/set Sucessor
michaelmaitland 3f0b020
fixup! avoid extra insert
michaelmaitland 3430a66
fixup! respond to review
michaelmaitland 852f3f0
fixup! respond to review
michaelmaitland 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -276,6 +276,7 @@ class SimplifyCFGOpt { | |||||||||||||||
| bool simplifyCleanupReturn(CleanupReturnInst *RI); | ||||||||||||||||
| bool simplifyUnreachable(UnreachableInst *UI); | ||||||||||||||||
| bool simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder); | ||||||||||||||||
| bool simplifyDuplicateSwitchArms(SwitchInst *SI, DomTreeUpdater *DTU); | ||||||||||||||||
| bool simplifyIndirectBr(IndirectBrInst *IBI); | ||||||||||||||||
| bool simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder); | ||||||||||||||||
| bool simplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder); | ||||||||||||||||
|
|
@@ -7436,6 +7437,185 @@ static bool simplifySwitchOfCmpIntrinsic(SwitchInst *SI, IRBuilderBase &Builder, | |||||||||||||||
| return true; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /// Checking whether two cases of SI are equal depends on the contents of the | ||||||||||||||||
| /// BasicBlock and the incoming values of their successor PHINodes. | ||||||||||||||||
| /// PHINode::getIncomingValueForBlock is O(|Preds|), so we'd like to avoid | ||||||||||||||||
| /// calling this function on each BasicBlock every time isEqual is called, | ||||||||||||||||
| /// especially since the same BasicBlock may be passed as an argument multiple | ||||||||||||||||
| /// times. To do this, we can precompute a map of PHINode -> Pred BasicBlock -> | ||||||||||||||||
| /// IncomingValue and add it in the Wrapper so isEqual can do O(1) checking | ||||||||||||||||
| /// of the incoming values. | ||||||||||||||||
| struct SwitchSuccWrapper { | ||||||||||||||||
| // Keep so we can use SwitchInst::setSuccessor to do the replacement. It won't | ||||||||||||||||
| // be important to equality though. | ||||||||||||||||
| unsigned SuccNum; | ||||||||||||||||
| BasicBlock *Dest; | ||||||||||||||||
| DenseMap<PHINode *, DenseMap<BasicBlock *, Value *>> *PhiPredIVs; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| namespace llvm { | ||||||||||||||||
| template <> struct DenseMapInfo<const SwitchSuccWrapper *> { | ||||||||||||||||
| static const SwitchSuccWrapper *getEmptyKey() { | ||||||||||||||||
| return static_cast<SwitchSuccWrapper *>( | ||||||||||||||||
| DenseMapInfo<void *>::getEmptyKey()); | ||||||||||||||||
| } | ||||||||||||||||
| static const SwitchSuccWrapper *getTombstoneKey() { | ||||||||||||||||
| return static_cast<SwitchSuccWrapper *>( | ||||||||||||||||
| DenseMapInfo<void *>::getTombstoneKey()); | ||||||||||||||||
| } | ||||||||||||||||
| static unsigned getHashValue(const SwitchSuccWrapper *SSW) { | ||||||||||||||||
| BasicBlock *Succ = SSW->Dest; | ||||||||||||||||
| BranchInst *BI = cast<BranchInst>(Succ->getTerminator()); | ||||||||||||||||
| assert(BI->isUnconditional() && | ||||||||||||||||
| "Only supporting unconditional branches for now"); | ||||||||||||||||
| assert(BI->getNumSuccessors() == 1 && | ||||||||||||||||
| "Expected unconditional branches to have one successor"); | ||||||||||||||||
| assert(Succ->size() == 1 && "Expected just a single branch in the BB"); | ||||||||||||||||
|
|
||||||||||||||||
| // Since we assume the BB is just a single BranchInst with a single | ||||||||||||||||
| // succsessor, we hash as the BB and the incoming Values of its successor | ||||||||||||||||
michaelmaitland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
| // PHIs. Initially, we tried to just use the successor BB as the hash, but | ||||||||||||||||
| // this had poor performance. We find that the extra computation of getting | ||||||||||||||||
| // the incoming PHI values here leads to better performance on overall Set | ||||||||||||||||
| // performance. We also tried to build a map from BB -> Succs.IncomingValues | ||||||||||||||||
|
||||||||||||||||
| // PHIs. Initially, we tried to just use the successor BB as the hash, but | |
| // this had poor performance. We find that the extra computation of getting | |
| // the incoming PHI values here leads to better performance on overall Set | |
| // performance. We also tried to build a map from BB -> Succs.IncomingValues | |
| // PHIs. Initially, we tried to just use the successor BB as the hash, but | |
| // including the incoming PHI values leads to better performance. | |
| // We also tried to build a map from BB -> Succs.IncomingValues |
There was a lot of performance in here.
michaelmaitland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
michaelmaitland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
michaelmaitland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
michaelmaitland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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
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.
Maybe the inner mapping can be a SmallDenseMap? I glanced over, but could you confirm the inner map is actually needed? It seems to be prepopulated with values that you can just retrieve from the PN?
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've updated to use a SmallDenseMap on the inner map. I think the inner map is important. In
isEqual, we need to check that for two BasicBlock A and B, that the incoming values for each Phi are the same for the two BasicBlocks. That requires us to callPHINode::getIncomingValueForBlock, which isO(|Preds|). If we do not precompute this inner map, then we callPHINode::getIncomingValueForBlockredundantly, since a single BasicBlock may be passed as argument to isEqual multiple times. I've noted this in the docstring of the data structure.