mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): fuse DCE AST passes (#8058)
This commit is contained in:
parent
be2c60dd10
commit
77d845a6b8
2 changed files with 52 additions and 9 deletions
|
|
@ -22,6 +22,8 @@ pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax;
|
|||
pub use remove_syntax::RemoveSyntax;
|
||||
pub use statement_fusion::StatementFusion;
|
||||
|
||||
use crate::CompressOptions;
|
||||
|
||||
pub trait CompressorPass<'a>: Traverse<'a> {
|
||||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>);
|
||||
}
|
||||
|
|
@ -287,3 +289,49 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
|
|||
self.x3_peephole_substitute_alternate_syntax.enter_binary_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DeadCodeElimination {
|
||||
x0_remove_syntax: RemoveSyntax,
|
||||
x1_peephole_fold_constants: PeepholeFoldConstants,
|
||||
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode,
|
||||
}
|
||||
|
||||
impl DeadCodeElimination {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
x0_remove_syntax: RemoveSyntax::new(CompressOptions::all_false()),
|
||||
x1_peephole_fold_constants: PeepholeFoldConstants::new(),
|
||||
x2_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CompressorPass<'a> for DeadCodeElimination {
|
||||
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
|
||||
traverse_mut_with_ctx(self, program, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Traverse<'a> for DeadCodeElimination {
|
||||
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_remove_dead_code.enter_statement(stmt, ctx);
|
||||
}
|
||||
|
||||
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_remove_dead_code.exit_statement(stmt, ctx);
|
||||
}
|
||||
|
||||
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_remove_dead_code.exit_program(program, ctx);
|
||||
}
|
||||
|
||||
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx);
|
||||
}
|
||||
|
||||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||
self.x0_remove_syntax.exit_expression(expr, ctx);
|
||||
self.x1_peephole_fold_constants.exit_expression(expr, ctx);
|
||||
self.x2_peephole_remove_dead_code.exit_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ use oxc_traverse::ReusableTraverseCtx;
|
|||
|
||||
use crate::{
|
||||
ast_passes::{
|
||||
CollapsePass, LatePeepholeOptimizations, PeepholeFoldConstants, PeepholeOptimizations,
|
||||
PeepholeRemoveDeadCode, RemoveSyntax,
|
||||
CollapsePass, DeadCodeElimination, LatePeepholeOptimizations, PeepholeOptimizations,
|
||||
RemoveSyntax,
|
||||
},
|
||||
CompressOptions, CompressorPass,
|
||||
};
|
||||
|
|
@ -44,10 +44,7 @@ impl<'a> Compressor<'a> {
|
|||
pub fn dead_code_elimination(self, program: &mut Program<'a>) {
|
||||
let (symbols, scopes) =
|
||||
SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree();
|
||||
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
|
||||
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
||||
PeepholeFoldConstants::new().build(program, &mut ctx);
|
||||
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
|
||||
self.dead_code_elimination_with_symbols_and_scopes(symbols, scopes, program);
|
||||
}
|
||||
|
||||
pub fn dead_code_elimination_with_symbols_and_scopes(
|
||||
|
|
@ -57,8 +54,6 @@ impl<'a> Compressor<'a> {
|
|||
program: &mut Program<'a>,
|
||||
) {
|
||||
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
|
||||
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
||||
PeepholeFoldConstants::new().build(program, &mut ctx);
|
||||
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
|
||||
DeadCodeElimination::new().build(program, &mut ctx);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue