jon.recoil.org

Module Cmm_peephole_engine

This module provides an engine for performing peephole optimisations on Cmm terms. A peephole optimisation is given as a rewriting rule, with a pattern on the left-hand side and a rewriting function on the right-hand side. The rewriting function takes as parameter an environment from which the sub-expressions that were matched to pattern variables can be retrieved.

Currently the intended use is to call run on a newly created expression with a sequence of rewriting rules likely to match it, allowing to emulate smart constructors for Cmm expressions that would manually pattern-match on their arguments. Following that, the implementation only checks whether the whole expression matches a given pattern, and does not try to find sub-expressions that match.

However, the API is compatible with a global engine that would apply a global set of rules to the whole program. (Although the engine itself is not suited for that and would have to be rewritten.)

Pattern variables usually match sub-expressions, but some expressions have integer payloads that may be relevant. This type defines the various cases supported by the engine.

type 'a pattern_var
module Default_variables : sig ... end
module Env : sig ... end
type binop =
  1. | Add
  2. | Sub
  3. | Lsl
  4. | Lsr
  5. | Asr
  6. | Or
  7. | And
  8. | Comparison
    (*

    Matches all versions of the Ccmpi and Ccmpf operations

    *)
  9. | Bitwise_op
    (*

    All binary bit-wise operations: Cand, Cor, Cxor

    *)
type cmm_pattern =
  1. | Any of Cmm.expression Cmm_peephole_engine.pattern_var
    (*

    Wildcard pattern, binding a variable

    *)
  2. | As of Cmm.expression Cmm_peephole_engine.pattern_var * Cmm_peephole_engine.cmm_pattern
    (*

    Variable binding with nested pattern

    *)
  3. | Const_int_fixed of int
    (*

    Matches Cconst_int with a given integer

    *)
  4. | Const_int of int Cmm_peephole_engine.pattern_var
    (*

    Matches any Cconst_int and binds the underlying integer

    *)
  5. | Const_natint_fixed of Stdlib.Nativeint.t
    (*

    Matches Cconst_natint with a given integer

    *)
  6. | Const_natint of Stdlib.Nativeint.t Cmm_peephole_engine.pattern_var
    (*

    Matches any Cconst_natint and binds the underlying integer

    *)
  7. | Binop of Cmm_peephole_engine.binop * Cmm_peephole_engine.cmm_pattern * Cmm_peephole_engine.cmm_pattern
    (*

    Matches the corresponding Cop terms

    *)
  8. | Guarded of {
    1. pat : Cmm_peephole_engine.cmm_pattern;
    2. guard : Cmm_peephole_engine.Env.t -> bool;
    }
    (*

    When pat matches, the corresponding environment is passed to guard. If this returns true then the whole pattern matches with the same environment, otherwise the pattern doesn't match.

    *)
type 'a clause

The type of rewriting rules. Creating rules is done using the Syntax module below. The type parameter 'a allows to write clauses that are not rewriting rules but compute an arbitrary value of type 'a by matching on a Cmm expression

The entry point for the engine. Tries the rules in order, and applies the first that matches. If no rules match, returns the original expression.

val run_default : default:(Cmm.expression -> 'a) -> Cmm.expression -> 'a Cmm_peephole_engine.clause list -> 'a

An extension of the engine allowing to run on arbitrary clauses. The default parameter is called if none of the clauses match.

module Syntax : sig ... end
module Cmm_comparator : sig ... end

Check equivalence of Cmm terms for the purpose of checking that the engine produces terms equivalent to the ones produced by the original code.