Class: Optimize::Passes::IdentityElimPass
- Inherits:
-
Optimize::Pass
- Object
- Optimize::Pass
- Optimize::Passes::IdentityElimPass
- Defined in:
- lib/optimize/passes/identity_elim_pass.rb
Overview
Strip arithmetic identities: x * 1, 1 * x, x + 0, 0 + x, x - 0, x / 1. See docs/superpowers/specs/2026-04-21-pass-identity-elim-design.md.
Constant Summary collapse
- IDENTITY_OPS =
{ opt_plus: { identity: 0, sides: :either }, opt_mult: { identity: 1, sides: :either }, opt_minus: { identity: 0, sides: :right }, opt_div: { identity: 1, sides: :right }, }.freeze
- SAFE_PRODUCER_OPCODES =
Non-literal side must be in this whitelist. Shared with ArithReassocPass: any producer with observable side effects (send, invokesuper, …) is absent here so that eliding the op never elides a side effect.
ArithReassocPass::SINGLE_PUSH_OPERAND_OPCODES
Instance Method Summary collapse
Methods inherited from Optimize::Pass
Instance Method Details
#apply(function, type_env:, log:, object_table: nil, **_extras) ⇒ Object
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 54 |
# File 'lib/optimize/passes/identity_elim_pass.rb', line 25 def apply(function, type_env:, log:, object_table: nil, **_extras) _ = type_env return unless object_table insts = function.instructions return unless insts loop do eliminated_any = false i = 0 while i <= insts.size - 3 a = insts[i] b = insts[i + 1] op = insts[i + 2] entry = IDENTITY_OPS[op.opcode] if entry && SAFE_PRODUCER_OPCODES.include?(a.opcode) && SAFE_PRODUCER_OPCODES.include?(b.opcode) keep = try_eliminate(a, b, entry, object_table) if keep function.splice_instructions!(i..(i + 2), [keep]) log.rewrite(pass: :identity_elim, reason: :identity_eliminated, file: function.path, line: (op.line || a.line || function.first_lineno)) eliminated_any = true i = i - 1 if i.positive? next end end i += 1 end break unless eliminated_any end end |
#name ⇒ Object
23 |
# File 'lib/optimize/passes/identity_elim_pass.rb', line 23 def name = :identity_elim |