Skip to main content

cairo_lang_lowering/analysis/
mod.rs

1//! Dataflow analysis utilities for the lowering IR.
2//!
3//! This module provides generic analysis frameworks that can be used by various
4//! optimization passes and semantic checks.
5
6pub mod backward;
7pub use backward::{BackAnalysis, DataflowBackAnalysis};
8
9pub mod core;
10pub use core::{DataflowAnalyzer, Direction, Edge, StatementLocation};
11
12pub mod equality_analysis;
13pub mod forward;
14pub use forward::ForwardDataflowAnalysis;
15
16#[cfg(test)]
17mod equality_analysis_test;
18#[cfg(test)]
19mod test;
20
21use crate::{Block, BlockId, MatchInfo, Statement, VarRemapping, VarUsage};
22
23/// Analyzer trait to implement for each specific analysis.
24#[allow(unused_variables)]
25pub trait Analyzer<'db, 'a> {
26    type Info: Clone;
27    fn visit_block_start(&mut self, info: &mut Self::Info, block_id: BlockId, block: &Block<'db>) {}
28    fn visit_stmt(
29        &mut self,
30        info: &mut Self::Info,
31        statement_location: StatementLocation,
32        stmt: &'a Statement<'db>,
33    ) {
34    }
35    fn visit_goto(
36        &mut self,
37        info: &mut Self::Info,
38        statement_location: StatementLocation,
39        target_block_id: BlockId,
40        remapping: &'a VarRemapping<'db>,
41    ) {
42    }
43    fn merge_match(
44        &mut self,
45        statement_location: StatementLocation,
46        match_info: &'a MatchInfo<'db>,
47        infos: impl Iterator<Item = Self::Info>,
48    ) -> Self::Info;
49    fn info_from_return(
50        &mut self,
51        statement_location: StatementLocation,
52        vars: &'a [VarUsage<'db>],
53    ) -> Self::Info;
54
55    /// Default `info_from_panic` implementation for post 'lower_panics' phases.
56    /// Earlier phases need to override this implementation.
57    fn info_from_panic(
58        &mut self,
59        statement_location: StatementLocation,
60        var: &VarUsage<'db>,
61    ) -> Self::Info {
62        unreachable!("Panics should have been stripped in the `lower_panics` phase.");
63    }
64}