2020-04-23 01:56:23 +00:00
|
|
|
//! Lazily compute the reverse control-flow graph for the MIR.
|
|
|
|
|
2020-04-12 17:30:07 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-05-16 04:56:33 +00:00
|
|
|
use rustc_data_structures::sync::OnceCell;
|
2020-04-12 17:30:07 +00:00
|
|
|
use rustc_index::vec::IndexVec;
|
2022-06-14 04:52:01 +00:00
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2020-04-12 17:30:07 +00:00
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
|
|
|
use crate::mir::{BasicBlock, BasicBlockData};
|
|
|
|
|
|
|
|
// Typically 95%+ of basic blocks have 4 or fewer predecessors.
|
|
|
|
pub type Predecessors = IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2020-04-23 01:56:23 +00:00
|
|
|
pub(super) struct PredecessorCache {
|
2020-05-16 04:56:33 +00:00
|
|
|
cache: OnceCell<Predecessors>,
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PredecessorCache {
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
2020-04-23 01:56:23 +00:00
|
|
|
pub(super) fn new() -> Self {
|
2020-05-16 04:56:33 +00:00
|
|
|
PredecessorCache { cache: OnceCell::new() }
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:15:07 +00:00
|
|
|
/// Invalidates the predecessor cache.
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
2020-04-23 01:56:23 +00:00
|
|
|
pub(super) fn invalidate(&mut self) {
|
2020-05-16 18:26:21 +00:00
|
|
|
// Invalidating the predecessor cache requires mutating the MIR, which in turn requires a
|
|
|
|
// unique reference (`&mut`) to the `mir::Body`. Because of this, we can assume that all
|
|
|
|
// callers of `invalidate` have a unique reference to the MIR and thus to the predecessor
|
|
|
|
// cache. This means we never need to do synchronization when `invalidate` is called, we can
|
|
|
|
// simply reinitialize the `OnceCell`.
|
2020-05-16 04:56:33 +00:00
|
|
|
self.cache = OnceCell::new();
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 03:14:28 +00:00
|
|
|
/// Returns the predecessor graph for this MIR.
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
2020-04-23 01:56:23 +00:00
|
|
|
pub(super) fn compute(
|
2020-04-12 17:30:07 +00:00
|
|
|
&self,
|
|
|
|
basic_blocks: &IndexVec<BasicBlock, BasicBlockData<'_>>,
|
2020-05-16 04:56:33 +00:00
|
|
|
) -> &Predecessors {
|
|
|
|
self.cache.get_or_init(|| {
|
2020-04-21 17:15:07 +00:00
|
|
|
let mut preds = IndexVec::from_elem(SmallVec::new(), basic_blocks);
|
|
|
|
for (bb, data) in basic_blocks.iter_enumerated() {
|
|
|
|
if let Some(term) = &data.terminator {
|
2022-05-17 00:41:01 +00:00
|
|
|
for succ in term.successors() {
|
2020-04-21 17:15:07 +00:00
|
|
|
preds[succ].push(bb);
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:15:07 +00:00
|
|
|
}
|
2020-04-12 17:30:07 +00:00
|
|
|
|
2020-05-16 04:56:33 +00:00
|
|
|
preds
|
|
|
|
})
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 04:52:01 +00:00
|
|
|
impl<S: Encoder> Encodable<S> for PredecessorCache {
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 03:30:45 +00:00
|
|
|
fn encode(&self, _s: &mut S) {}
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 04:52:01 +00:00
|
|
|
impl<D: Decoder> Decodable<D> for PredecessorCache {
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
2022-02-22 23:14:51 +00:00
|
|
|
fn decode(_: &mut D) -> Self {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 02:22:50 +00:00
|
|
|
Self::new()
|
2020-04-12 17:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CTX> HashStable<CTX> for PredecessorCache {
|
2020-04-12 17:48:56 +00:00
|
|
|
#[inline]
|
2020-04-12 17:30:07 +00:00
|
|
|
fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:05:17 +00:00
|
|
|
TrivialTypeTraversalAndLiftImpls! {
|
2020-04-12 17:30:07 +00:00
|
|
|
PredecessorCache,
|
|
|
|
}
|