2017-11-27 21:50:36 +02:00
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use rustc::mir::*;
|
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
|
|
|
use rustc_data_structures::bit_set::BitSet;
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
|
|
|
use crate::util::patch::MirPatch;
|
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;
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn remove_noop_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2018-01-15 23:57:44 +02:00
|
|
|
if tcx.sess.no_landing_pads() {
|
|
|
|
return
|
|
|
|
}
|
2019-06-03 18:26:48 -04:00
|
|
|
debug!("remove_noop_landing_pads({:?})", body);
|
2018-01-15 23:57:44 +02:00
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
RemoveNoopLandingPads.remove_nop_landing_pads(body)
|
2018-01-15 23:57:44 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:50:36 +02:00
|
|
|
impl MirPass for RemoveNoopLandingPads {
|
2019-06-14 01:32:15 +03:00
|
|
|
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) {
|
2019-06-03 18:26:48 -04: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 {
|
2017-11-27 21:50:36 +02:00
|
|
|
match stmt.kind {
|
2018-09-14 21:05:31 +02:00
|
|
|
StatementKind::FakeRead(..) |
|
2017-11-27 21:50:36 +02:00
|
|
|
StatementKind::StorageLive(_) |
|
|
|
|
StatementKind::StorageDead(_) |
|
2018-08-31 18:59:35 -04:00
|
|
|
StatementKind::AscribeUserType(..) |
|
2017-11-27 21:50:36 +02:00
|
|
|
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-04-30 18:58:24 +02:00
|
|
|
StatementKind::Assign(Place {
|
|
|
|
base: PlaceBase::Local(_),
|
|
|
|
projection: None,
|
|
|
|
}, box Rvalue::Use(_)) => {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Writing to a local (e.g., a drop flag) does not
|
2017-11-27 21:50:36 +02:00
|
|
|
// turn a landing pad to a non-nop
|
|
|
|
}
|
|
|
|
|
2018-11-06 11:04:10 +01:00
|
|
|
StatementKind::Assign { .. } |
|
2017-11-27 21:50:36 +02:00
|
|
|
StatementKind::SetDiscriminant { .. } |
|
|
|
|
StatementKind::InlineAsm { .. } |
|
2018-12-11 19:54:38 +01: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 {
|
|
|
|
TerminatorKind::Goto { .. } |
|
|
|
|
TerminatorKind::Resume |
|
|
|
|
TerminatorKind::SwitchInt { .. } |
|
2018-01-25 01:45:45 -05:00
|
|
|
TerminatorKind::FalseEdges { .. } |
|
|
|
|
TerminatorKind::FalseUnwind { .. } => {
|
2018-07-22 19:23:39 +03:00
|
|
|
terminator.successors().all(|&succ| {
|
|
|
|
nop_landing_pads.contains(succ)
|
2017-11-27 21:50:36 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
TerminatorKind::GeneratorDrop |
|
|
|
|
TerminatorKind::Yield { .. } |
|
|
|
|
TerminatorKind::Return |
|
2017-12-19 01:17:16 +01:00
|
|
|
TerminatorKind::Abort |
|
2017-11-27 21:50:36 +02:00
|
|
|
TerminatorKind::Unreachable |
|
|
|
|
TerminatorKind::Call { .. } |
|
|
|
|
TerminatorKind::Assert { .. } |
|
|
|
|
TerminatorKind::DropAndReplace { .. } |
|
|
|
|
TerminatorKind::Drop { .. } => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04: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-06-03 18:26:48 -04:00
|
|
|
let patch = MirPatch::new(body);
|
2017-11-27 21:50:36 +02:00
|
|
|
let resume_block = patch.resume_block();
|
2019-06-03 18:26:48 -04: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-06-03 18:26:48 -04: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-06-03 18:26:48 -04: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-06-03 18:26:48 -04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
match body[bb].terminator_mut().unwind_mut() {
|
2017-11-27 21:50:36 +02:00
|
|
|
Some(unwind) => {
|
|
|
|
if *unwind == Some(resume_block) {
|
|
|
|
debug!(" removing noop landing pad");
|
|
|
|
jumps_folded -= 1;
|
|
|
|
landing_pads_removed += 1;
|
|
|
|
*unwind = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04: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);
|
|
|
|
}
|
|
|
|
}
|