Optimizer

Struct Optimizer 

Source
pub struct Optimizer {
    level: u8,
    stats: OptimizationStats,
    inline_threshold: usize,
    enabled_passes: OptimizationPasses,
}
Expand description

Yul AST optimizer with configurable optimization levels

Fields§

§level: u8§stats: OptimizationStats§inline_threshold: usize

Maximum function size for inlining (in AST nodes)

§enabled_passes: OptimizationPasses

Track which optimizations are enabled

Implementations§

§

impl Optimizer

pub fn new(level: u8) -> Self

Create a new optimizer with the specified level (0-3)

pub fn with_config(level: u8, inline_threshold: usize) -> Self

Create optimizer with custom settings

pub fn optimize(&mut self, ast: AstNode) -> Result<AstNode, CompilerError>

Run all enabled optimization passes

pub fn get_stats(&self) -> &OptimizationStats

Get optimization statistics

pub fn level(&self) -> u8

Get current optimization level

fn count_nodes(&self, node: &AstNode) -> usize

Count AST nodes for statistics

§

impl Optimizer

fn constant_folding(&mut self, ast: AstNode) -> Result<AstNode, CompilerError>

fn fold_constants_recursive(&mut self, node: AstNode) -> AstNode

fn evaluate_constant_expression( &self, name: &str, arguments: &[AstNode], ) -> Option<u64>

fn simplify_identity_ops( &self, name: &str, args: Vec<AstNode>, line: usize, column: usize, ) -> Option<AstNode>

Simplify identity operations like x + 0 = x, x * 1 = x

fn nodes_equal(&self, a: &AstNode, b: &AstNode) -> bool

Check if two AST nodes are structurally equal

fn extract_constant(&self, node: &AstNode) -> Option<u64>

§

impl Optimizer

fn dead_code_elimination( &mut self, ast: AstNode, ) -> Result<AstNode, CompilerError>

fn eliminate_dead_code_recursive( &mut self, node: AstNode, _after_return: bool, ) -> AstNode

fn process_statement_list(&mut self, statements: Vec<AstNode>) -> Vec<AstNode>

Process a list of statements, removing dead code after terminators

fn is_terminator(&self, node: &AstNode) -> bool

Check if a statement is a control flow terminator

fn is_label(&self, _node: &AstNode) -> bool

Check if a node is a label (should be preserved) Note: Labels are not currently supported in this compiler’s AST. This function exists for future extension when label support is added.

fn is_empty_block(&self, node: &AstNode) -> bool

Check if a block is empty or contains only empty blocks

fn is_noop(&self, node: &AstNode) -> bool

Check if a statement is a no-op (can be safely removed)

fn is_pure_expression(&self, node: &AstNode) -> bool

Check if an expression has no side effects

fn remove_empty_blocks(&self, node: AstNode) -> AstNode

Remove nested empty blocks

§

impl Optimizer

fn function_inlining(&mut self, ast: AstNode) -> Result<AstNode, CompilerError>

fn count_call_sites(&self, node: &AstNode) -> HashMap<String, usize>

Count how many times each function is called

fn count_calls_recursive( &self, node: &AstNode, counts: &mut HashMap<String, usize>, )

fn collect_inline_candidates( &self, node: &AstNode, candidates: &mut HashMap<String, InlineCandidate>, )

fn is_inlineable_function(&self, body: &AstNode, name: &str) -> bool

Check if a function is suitable for inlining

fn contains_call_to(&self, node: &AstNode, target: &str) -> bool

Check if body contains a call to the named function (recursion check)

fn is_simple_function(&self, body: &AstNode) -> bool

fn estimate_inline_cost(&self, body: &AstNode) -> usize

Estimate the cost of inlining a function body

fn inline_functions_recursive( &mut self, node: AstNode, candidates: &HashMap<String, InlineCandidate>, ) -> AstNode

fn substitute_parameters( node: AstNode, substitutions: &HashMap<String, AstNode>, ) -> AstNode

Substitute parameter references with actual argument expressions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.