mod branch_exit;
mod shared;
mod value_merge;
use std::collections::BTreeMap;
use crate::cfg::{BlockRef, Cfg, DataflowFacts, GraphFacts};
use crate::transformer::{LoweredProto, Reg};
use super::common::{BranchCandidate, ShortCircuitCandidate};
pub(super) fn analyze_short_circuits(
proto: &LoweredProto,
cfg: &Cfg,
graph_facts: &GraphFacts,
dataflow: &DataflowFacts,
branch_candidates: &[BranchCandidate],
) -> Vec<ShortCircuitCandidate> {
let branch_by_header = branch_candidates
.iter()
.map(|candidate| (candidate.header, candidate))
.collect::<BTreeMap<BlockRef, _>>();
let mut candidates = branch_exit::analyze_linear_branch_exit_candidates(
proto,
cfg,
&branch_by_header,
branch_candidates,
);
candidates.extend(branch_exit::analyze_guard_branch_exit_dag_candidates(
proto,
cfg,
graph_facts,
&branch_by_header,
branch_candidates,
));
candidates.extend(value_merge::analyze_value_merge_candidates(
proto,
cfg,
graph_facts,
dataflow,
&branch_by_header,
branch_candidates,
));
candidates.sort_by_key(|candidate| {
(
candidate.header,
candidate.blocks.len(),
candidate.nodes.len(),
candidate.result_reg.map(Reg::index),
)
});
candidates
}