rust/compiler/rustc_mir_transform/src/ctfe_limit.rs

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

57 lines
1.8 KiB
Rust
Raw Normal View History

use crate::MirPass;
use rustc_middle::mir::{
2022-12-29 13:37:33 -06:00
BasicBlock, BasicBlockData, BasicBlocks, Body, Statement, StatementKind, TerminatorKind,
};
use rustc_middle::ty::TyCtxt;
pub struct CtfeLimit;
impl<'tcx> MirPass<'tcx> for CtfeLimit {
#[instrument(skip(self, _tcx, body))]
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2022-12-29 13:37:33 -06:00
let indices: Vec<BasicBlock> = body
.basic_blocks
.iter_enumerated()
.filter_map(|(node, node_data)| {
if matches!(node_data.terminator().kind, TerminatorKind::Call { .. })
// Back edges in a CFG indicate loops
2022-12-29 13:37:33 -06:00
|| has_back_edge(&body.basic_blocks, node, &node_data)
{
Some(node)
} else {
None
}
})
.collect();
2022-12-28 21:38:34 -06:00
for index in indices {
insert_counter(
body.basic_blocks_mut()
.get_mut(index)
.expect("basic_blocks index {index} should exist"),
);
}
}
}
2022-12-28 21:38:34 -06:00
2022-12-29 13:37:33 -06:00
fn has_back_edge(
basic_blocks: &BasicBlocks<'_>,
node: BasicBlock,
node_data: &BasicBlockData<'_>,
) -> bool {
let doms = basic_blocks.dominators();
basic_blocks.indices().any(|potential_dom| {
doms.is_reachable(potential_dom)
&& doms.is_reachable(node)
&& doms.is_dominated_by(node, potential_dom)
&& node_data.terminator().successors().into_iter().any(|succ| succ == potential_dom)
})
}
2022-12-28 21:38:34 -06:00
fn insert_counter(basic_block_data: &mut BasicBlockData<'_>) {
basic_block_data.statements.push(Statement {
source_info: basic_block_data.terminator().source_info,
kind: StatementKind::ConstEvalCounter,
});
}