-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #74839 - alarsyo:multiple_return_terminators, r=oli-obk
Implement multiple return terminator optimization Closes #72022
- Loading branch information
Showing
12 changed files
with
149 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 38 additions & 0 deletions
38
compiler/rustc_mir/src/transform/multiple_return_terminators.rs
This file contains 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,38 @@ | ||
//! This pass removes jumps to basic blocks containing only a return, and replaces them with a | ||
//! return instead. | ||
use crate::transform::{simplify, MirPass, MirSource}; | ||
use rustc_index::bit_set::BitSet; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
|
||
pub struct MultipleReturnTerminators; | ||
|
||
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) { | ||
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 { | ||
return; | ||
} | ||
|
||
// find basic blocks with no statement and a return terminator | ||
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len()); | ||
let bbs = body.basic_blocks_mut(); | ||
for idx in bbs.indices() { | ||
if bbs[idx].statements.is_empty() | ||
&& bbs[idx].terminator().kind == TerminatorKind::Return | ||
{ | ||
bbs_simple_returns.insert(idx); | ||
} | ||
} | ||
|
||
for bb in bbs { | ||
if let TerminatorKind::Goto { target } = bb.terminator().kind { | ||
if bbs_simple_returns.contains(target) { | ||
bb.terminator_mut().kind = TerminatorKind::Return; | ||
} | ||
} | ||
} | ||
|
||
simplify::remove_dead_blocks(body) | ||
} | ||
} |
This file contains 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 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.