Class: MilkTea::ControlFlow::ConstantPropagation

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/core/control_flow/constant_propagation.rb

Defined Under Namespace

Classes: ConstVal, Result

Constant Summary collapse

UNDEF =
:undef
NAC =
:nac

Class Method Summary collapse

Class Method Details

.constant_value_of(expr, in_state, binding_resolution: nil, strict_binding_ids: false) ⇒ Object



126
127
128
129
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 126

def self.constant_value_of(expr, in_state, binding_resolution: nil, strict_binding_ids: false)
  result = send(:eval_expr_const, expr, in_state, binding_resolution:, strict_binding_ids:)
  result.is_a?(ConstVal) ? result.value : nil
end

.solve(graph, binding_resolution: nil, strict_binding_ids: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 23

def self.solve(graph, binding_resolution: nil, strict_binding_ids: false)
  result = Dataflow.solve(
    graph,
    direction: :forward,
    initial: -> { {} },
    join: lambda do |states|
      return {} if states.empty?

      states.reduce do |acc, state|
        keys = acc.keys | state.keys
        keys.each_with_object({}) do |k, merged|
          a = acc[k]   || UNDEF
          b = state[k] || UNDEF
          merged[k] = join_lattice(a, b)
        end
      end
    end,
    transfer: lambda do |node, in_state|
      out = in_state.dup
      node.writes_info.each do |write|
        key = write[:binding_key]
        val = eval_const(node.statement, write, in_state, binding_resolution:, strict_binding_ids:)
        out[key] = val
      end
      out
    end,
    boundary_in: { graph.entry_id => {} }
  )

  Result.new(in_states: result.in_states, out_states: result.out_states)
end