rust/src/librustc_mir/transform/remove_noop_landing_pads.rs

131 lines
4.7 KiB
Rust
Raw Normal View History

2019-02-08 06:28:15 +09:00
use crate::transform::{MirPass, MirSource};
use crate::util::patch::MirPatch;
2020-03-29 17:19:48 +02:00
use rustc_index::bit_set::BitSet;
2020-03-29 16:41:09 +02:00
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
2020-03-31 23:15:39 +01:00
use rustc_target::spec::PanicStrategy;
2019-02-08 14:53:55 +01:00
/// A pass that removes noop landing pads and replaces jumps to them with
/// `None`. This is important because otherwise LLVM generates terrible
/// code for these.
pub struct RemoveNoopLandingPads;
2020-04-12 10:31:00 -07:00
pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2020-03-31 23:15:39 +01:00
if tcx.sess.panic_strategy() == PanicStrategy::Abort {
2019-12-22 17:42:04 -05:00
return;
}
debug!("remove_noop_landing_pads({:?})", body);
RemoveNoopLandingPads.remove_nop_landing_pads(body)
}
2019-08-04 16:20:00 -04:00
impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
2020-04-12 10:31:00 -07:00
fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) {
remove_noop_landing_pads(tcx, body);
}
}
impl RemoveNoopLandingPads {
fn is_nop_landing_pad(
&self,
bb: BasicBlock,
body: &Body<'_>,
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs. Currently we have two files implementing bitsets (and 2D bit matrices). This commit combines them into one, taking the best features from each. This involves renaming a lot of things. The high level changes are as follows. - bitvec.rs --> bit_set.rs - indexed_set.rs --> (removed) - BitArray + IdxSet --> BitSet (merged, see below) - BitVector --> GrowableBitSet - {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet - BitMatrix --> BitMatrix - SparseBitMatrix --> SparseBitMatrix The changes within the bitset types themselves are as follows. ``` OLD OLD NEW BitArray<C> IdxSet<T> BitSet<T> -------- ------ ------ grow - grow new - (remove) new_empty new_empty new_empty new_filled new_filled new_filled - to_hybrid to_hybrid clear clear clear set_up_to set_up_to set_up_to clear_above - clear_above count - count contains(T) contains(&T) contains(T) contains_all - superset is_empty - is_empty insert(T) add(&T) insert(T) insert_all - insert_all() remove(T) remove(&T) remove(T) words words words words_mut words_mut words_mut - overwrite overwrite merge union union - subtract subtract - intersect intersect iter iter iter ``` In general, when choosing names I went with: - names that are more obvious (e.g. `BitSet` over `IdxSet`). - names that are more like the Rust libraries (e.g. `T` over `C`, `insert` over `add`); - names that are more set-like (e.g. `union` over `merge`, `superset` over `contains_all`, `domain_size` over `num_bits`). Also, using `T` for index arguments seems more sensible than `&T` -- even though the latter is standard in Rust collection types -- because indices are always copyable. It also results in fewer `&` and `*` sigils in practice.
2018-09-14 15:07:25 +10:00
nop_landing_pads: &BitSet<BasicBlock>,
) -> bool {
for stmt in &body[bb].statements {
match &stmt.kind {
2019-12-22 17:42:04 -05:00
StatementKind::FakeRead(..)
| StatementKind::StorageLive(_)
| StatementKind::StorageDead(_)
| StatementKind::AscribeUserType(..)
| StatementKind::Nop => {
// These are all nops in a landing pad
}
2019-12-22 17:42:04 -05:00
StatementKind::Assign(box (place, Rvalue::Use(_))) => {
if place.as_local().is_some() {
// Writing to a local (e.g., a drop flag) does not
// turn a landing pad to a non-nop
} else {
return false;
}
}
2019-12-22 17:42:04 -05:00
StatementKind::Assign { .. }
| StatementKind::SetDiscriminant { .. }
| StatementKind::LlvmInlineAsm { .. }
2019-12-22 17:42:04 -05:00
| StatementKind::Retag { .. } => {
return false;
}
}
}
let terminator = body[bb].terminator();
match terminator.kind {
2019-12-22 17:42:04 -05:00
TerminatorKind::Goto { .. }
| TerminatorKind::Resume
| TerminatorKind::SwitchInt { .. }
| TerminatorKind::FalseEdges { .. }
| TerminatorKind::FalseUnwind { .. } => {
terminator.successors().all(|&succ| nop_landing_pads.contains(succ))
}
2019-12-22 17:42:04 -05:00
TerminatorKind::GeneratorDrop
| TerminatorKind::Yield { .. }
| TerminatorKind::Return
| TerminatorKind::Abort
| TerminatorKind::Unreachable
| TerminatorKind::Call { .. }
| TerminatorKind::Assert { .. }
| TerminatorKind::DropAndReplace { .. }
2020-02-14 18:17:50 +00:00
| TerminatorKind::Drop { .. }
| TerminatorKind::InlineAsm { .. } => false,
}
}
2020-04-12 10:31:00 -07:00
fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
// make sure there's a single resume block
let resume_block = {
let patch = MirPatch::new(body);
let resume_block = patch.resume_block();
patch.apply(body);
resume_block
};
debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
let mut jumps_folded = 0;
let mut landing_pads_removed = 0;
let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks().len());
// This is a post-order traversal, so that if A post-dominates B
// then A will be visited before B.
let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
for bb in postorder {
debug!(" processing {:?}", bb);
for target in body[bb].terminator_mut().successors_mut() {
if *target != resume_block && nop_landing_pads.contains(*target) {
debug!(" folding noop jump to {:?} to resume block", target);
*target = resume_block;
jumps_folded += 1;
}
}
if let Some(unwind) = body[bb].terminator_mut().unwind_mut() {
if *unwind == Some(resume_block) {
debug!(" removing noop landing pad");
jumps_folded -= 1;
landing_pads_removed += 1;
*unwind = None;
}
}
let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
if is_nop_landing_pad {
nop_landing_pads.insert(bb);
}
debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
}
debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
}
}