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;
|
2017-11-27 21:50:36 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// A pass that removes noop landing pads and replaces jumps to them with
|
2017-11-27 21:50:36 +02:00
|
|
|
/// `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;
|
2018-01-15 23:57:44 +02:00
|
|
|
}
|
2019-11-06 12:00:46 -05:00
|
|
|
debug!("remove_noop_landing_pads({:?})", body);
|
2018-01-15 23:57:44 +02:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
RemoveNoopLandingPads.remove_nop_landing_pads(body)
|
2018-01-15 23:57:44 +02:00
|
|
|
}
|
|
|
|
|
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>) {
|
2019-11-06 12:00:46 -05:00
|
|
|
remove_noop_landing_pads(tcx, body);
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RemoveNoopLandingPads {
|
2018-07-22 19:23:39 +03:00
|
|
|
fn is_nop_landing_pad(
|
|
|
|
&self,
|
|
|
|
bb: BasicBlock,
|
2019-06-03 18:26:48 -04:00
|
|
|
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>,
|
2018-07-22 19:23:39 +03:00
|
|
|
) -> bool {
|
2019-06-03 18:26:48 -04:00
|
|
|
for stmt in &body[bb].statements {
|
2019-10-20 16:09:36 -04:00
|
|
|
match &stmt.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
StatementKind::FakeRead(..)
|
|
|
|
| StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
|
|
|
| StatementKind::AscribeUserType(..)
|
|
|
|
| StatementKind::Nop => {
|
2018-11-10 23:02:13 +00:00
|
|
|
// These are all nops in a landing pad
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
StatementKind::Assign(box (place, Rvalue::Use(_))) => {
|
2019-10-20 16:09:36 -04:00
|
|
|
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;
|
|
|
|
}
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
StatementKind::Assign { .. }
|
|
|
|
| StatementKind::SetDiscriminant { .. }
|
2020-01-14 13:40:42 +00:00
|
|
|
| StatementKind::LlvmInlineAsm { .. }
|
2019-12-22 17:42:04 -05:00
|
|
|
| StatementKind::Retag { .. } => {
|
2017-11-27 21:50:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
let terminator = body[bb].terminator();
|
2017-11-27 21:50:36 +02:00
|
|
|
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))
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
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,
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
|
2017-11-27 21:50:36 +02:00
|
|
|
// make sure there's a single resume block
|
|
|
|
let resume_block = {
|
2019-11-06 12:00:46 -05:00
|
|
|
let patch = MirPatch::new(body);
|
2017-11-27 21:50:36 +02:00
|
|
|
let resume_block = patch.resume_block();
|
2019-11-06 12:00:46 -05:00
|
|
|
patch.apply(body);
|
2017-11-27 21:50:36 +02:00
|
|
|
resume_block
|
|
|
|
};
|
|
|
|
debug!("remove_noop_landing_pads: resume block is {:?}", resume_block);
|
|
|
|
|
|
|
|
let mut jumps_folded = 0;
|
|
|
|
let mut landing_pads_removed = 0;
|
2019-11-06 12:00:46 -05:00
|
|
|
let mut nop_landing_pads = BitSet::new_empty(body.basic_blocks().len());
|
2017-11-27 21:50:36 +02:00
|
|
|
|
|
|
|
// This is a post-order traversal, so that if A post-dominates B
|
|
|
|
// then A will be visited before B.
|
2019-11-06 12:00:46 -05:00
|
|
|
let postorder: Vec<_> = traversal::postorder(body).map(|(bb, _)| bb).collect();
|
2017-11-27 21:50:36 +02:00
|
|
|
for bb in postorder {
|
|
|
|
debug!(" processing {:?}", bb);
|
2019-11-06 12:00:46 -05:00
|
|
|
for target in body[bb].terminator_mut().successors_mut() {
|
2018-07-22 19:23:39 +03:00
|
|
|
if *target != resume_block && nop_landing_pads.contains(*target) {
|
2017-11-27 21:50:36 +02:00
|
|
|
debug!(" folding noop jump to {:?} to resume block", target);
|
|
|
|
*target = resume_block;
|
|
|
|
jumps_folded += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 13:36:56 +01:00
|
|
|
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;
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let is_nop_landing_pad = self.is_nop_landing_pad(bb, body, &nop_landing_pads);
|
2017-11-27 21:50:36 +02:00
|
|
|
if is_nop_landing_pad {
|
2018-07-22 19:23:39 +03:00
|
|
|
nop_landing_pads.insert(bb);
|
2017-11-27 21:50:36 +02:00
|
|
|
}
|
|
|
|
debug!(" is_nop_landing_pad({:?}) = {}", bb, is_nop_landing_pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("removed {:?} jumps and {:?} landing pads", jumps_folded, landing_pads_removed);
|
|
|
|
}
|
|
|
|
}
|