Class: Kumi::Core::Analyzer::Passes::LIR::DeadCodeEliminationPass

Inherits:
PassBase
  • Object
show all
Defined in:
lib/kumi/core/analyzer/passes/lir/dead_code_elimination_pass.rb

Overview

DeadCodeEliminationPass


Removes instructions whose results are never used. This is a critical cleanup pass that runs after other optimizations (like CSE), which often leave behind unused variable assignments.

The algorithm works by performing a backward pass over the instructions of a declaration, tracking the set of “live” registers at each point. A register is considered “live” if its value is needed by a future instruction that has an observable effect on the program’s output.

In : state (or from fused/initial LIR) Out: state.with(:lir_03_cse, …)

Constant Summary collapse

LIR =
Kumi::Core::LIR

Instance Method Summary collapse

Methods inherited from PassBase

#debug, #debug_enabled?, #initialize

Methods included from ErrorReporting

#inferred_location, #raise_localized_error, #raise_syntax_error, #raise_type_error, #report_enhanced_error, #report_error, #report_semantic_error, #report_syntax_error, #report_type_error

Constructor Details

This class inherits a constructor from Kumi::Core::Analyzer::Passes::PassBase

Instance Method Details

#run(_errors) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kumi/core/analyzer/passes/lir/dead_code_elimination_pass.rb', line 26

def run(_errors)
  ops_by_decl =
    get_state(:lir_module, required: false)

  out = {}
  ops_by_decl.each do |name, payload|
    out[name] = { operations: optimize_decl(Array(payload[:operations]), name) }
  end

  out.freeze

  state.with(:lir_module, out)
end