Class: Optimize::Passes::DeadBranchFoldPass

Inherits:
Optimize::Pass show all
Defined in:
lib/optimize/passes/dead_branch_fold_pass.rb

Overview

Peephole: fold a literal condition followed by a conditional branch. Window is two instructions: ‘<literal producer>; branchif|branchunless|branchnil`. If the branch is statically taken, collapse the pair to `jump target`. If it is statically not taken, drop the pair (fall through).

Runs after ConstFoldPass / IdentityElim so their folded results (e.g. ‘“a” == “a”` → `putobject true`) feed this pass in a single pipeline iteration.

Constant Summary collapse

BRANCH_OPCODES =
%i[branchif branchunless branchnil].freeze

Instance Method Summary collapse

Methods inherited from Optimize::Pass

#one_shot?

Instance Method Details

#apply(function, type_env:, log:, object_table: nil, **_extras) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/optimize/passes/dead_branch_fold_pass.rb', line 21

def apply(function, type_env:, log:, object_table: nil, **_extras)
  _ = type_env
  return unless object_table
  insts = function.instructions
  return unless insts

  loop do
    folded_any = false
    i = 0
    while i < insts.size
      if try_fold_short_circuit(function, insts, i, object_table, log)
        folded_any = true
        i = i.positive? ? i - 1 : 0
        next
      end
      if try_fold_simple(function, insts, i, object_table, log)
        folded_any = true
        i = i.positive? ? i - 1 : 0
        next
      end
      i += 1
    end
    break unless folded_any
  end
end

#nameObject



19
# File 'lib/optimize/passes/dead_branch_fold_pass.rb', line 19

def name = :dead_branch_fold