-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[MIR] Enhance the SimplifyCfg pass to merge consecutive blocks #29838
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
Closed
Closed
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
// except according to those terms. | ||
|
||
use repr::*; | ||
use visit::*; | ||
|
||
/// Update basic block ids in all terminators using the given replacements, | ||
/// useful e.g. after removal of several basic blocks to update all terminators | ||
|
@@ -49,3 +50,70 @@ pub fn retain_basic_blocks(mir: &mut Mir, keep: &[bool]) { | |
|
||
update_basic_block_ids(mir, &replacements); | ||
} | ||
|
||
// A simple map to perform quick lookups of the predecessors of a BasicBlock. | ||
// Since BasicBlocks usually only have a small number of predecessors, we use a | ||
// simple vector. Also, if a block has the same target more than once, for | ||
// example in a switch, it will appear in the target's predecessor list multiple | ||
// times. This allows to update the map more easily when modifying the graph. | ||
pub struct PredecessorMap { | ||
map: Vec<Vec<BasicBlock>>, | ||
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. As far as I can tell, this pass only cares about how many predecessors a block has, not what those predecessors are. It would be simpler and more efficient to just store a |
||
} | ||
|
||
impl PredecessorMap { | ||
pub fn new(num_blocks: usize) -> PredecessorMap { | ||
PredecessorMap { | ||
map: vec![Vec::new(); num_blocks], | ||
} | ||
} | ||
|
||
pub fn predecessors(&self, block: BasicBlock) -> &[BasicBlock] { | ||
&self.map[block.index()] | ||
} | ||
|
||
pub fn add_predecessor(&mut self, block: BasicBlock, predecessor: BasicBlock) { | ||
self.map[block.index()].push(predecessor); | ||
} | ||
|
||
pub fn remove_predecessor(&mut self, block: BasicBlock, predecessor: BasicBlock) { | ||
let pos = self.map[block.index()].iter().position(|&p| p == predecessor).expect( | ||
&format!("{:?} is not registered as a predecessor of {:?}", predecessor, block)); | ||
|
||
self.map[block.index()].swap_remove(pos); | ||
} | ||
|
||
pub fn replace_predecessor(&mut self, block: BasicBlock, old: BasicBlock, new: BasicBlock) { | ||
self.remove_predecessor(block, old); | ||
self.add_predecessor(block, new); | ||
} | ||
} | ||
|
||
struct PredecessorVisitor { | ||
predecessor_map: PredecessorMap, | ||
} | ||
|
||
impl PredecessorVisitor { | ||
fn new(num_blocks: usize) -> PredecessorVisitor { | ||
PredecessorVisitor { | ||
predecessor_map: PredecessorMap::new(num_blocks), | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for PredecessorVisitor { | ||
fn visit_mir(&mut self, mir: &Mir<'tcx>) { | ||
self.predecessor_map = PredecessorMap::new(mir.basic_blocks.len()); | ||
self.super_mir(mir); | ||
} | ||
|
||
fn visit_branch(&mut self, source: BasicBlock, target: BasicBlock) { | ||
self.predecessor_map.add_predecessor(target, source); | ||
} | ||
} | ||
|
||
pub fn build_predecessor_map(mir: &Mir) -> PredecessorMap { | ||
let mut v = PredecessorVisitor::new(mir.basic_blocks.len()); | ||
v.visit_mir(mir); | ||
|
||
v.predecessor_map | ||
} |
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.
I think that doing
merge_consecutive_blocks
without aremove_dead_blocks
pass beforehand is missing out on some optimizations. For example:Since
A
is dead,B
only has one "real" predecessor, and so can be merged intoEntry
. However, since dead blocks aren't removed until the afterwards,merge_consecutive_blocks
seesB
as having 2 predecessors and doesn't merge it intoEntry
.