rust/compiler/rustc_middle/src/mir/predecessors.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.3 KiB
Rust
Raw Normal View History

//! Lazily compute the reverse control-flow graph for the MIR.
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2020-05-16 04:56:33 +00:00
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
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)]
pub(super) struct PredecessorCache {
2020-05-16 04:56:33 +00:00
cache: OnceCell<Predecessors>,
}
impl PredecessorCache {
2020-04-12 17:48:56 +00:00
#[inline]
pub(super) fn new() -> Self {
2020-05-16 04:56:33 +00:00
PredecessorCache { cache: OnceCell::new() }
}
/// Invalidates the predecessor cache.
2020-04-12 17:48:56 +00:00
#[inline]
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-09-21 03:14:28 +00:00
/// Returns the predecessor graph for this MIR.
2020-04-12 17:48:56 +00:00
#[inline]
pub(super) fn compute(
&self,
basic_blocks: &IndexVec<BasicBlock, BasicBlockData<'_>>,
2020-05-16 04:56:33 +00:00
) -> &Predecessors {
self.cache.get_or_init(|| {
let mut preds = IndexVec::from_elem(SmallVec::new(), basic_blocks);
for (bb, data) in basic_blocks.iter_enumerated() {
if let Some(term) = &data.terminator {
for succ in term.successors() {
preds[succ].push(bb);
}
}
}
2020-05-16 04:56:33 +00:00
preds
})
}
}
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) {}
}
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 {
2022-01-18 02:22:50 +00:00
Self::new()
}
}
impl<CTX> HashStable<CTX> for PredecessorCache {
2020-04-12 17:48:56 +00:00
#[inline]
fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {
// do nothing
}
}
2020-10-24 07:27:15 +00:00
TrivialTypeFoldableAndLiftImpls! {
PredecessorCache,
}