Cmm_peephole_engineThis 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.)
type _ pattern_kind = | Expr : Cmm.expression pattern_kind| Int : int pattern_kind| Natint : Nativeint.t pattern_kindPattern 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.
val create_var : 'a pattern_kind -> string -> 'a pattern_varmodule Default_variables : sig ... endmodule Env : sig ... endtype cmm_pattern = | Any of Cmm.expression pattern_varWildcard pattern, binding a variable
*)| As of Cmm.expression pattern_var * cmm_patternVariable binding with nested pattern
*)| Const_int_fixed of intMatches Cconst_int with a given integer
| Const_int of int pattern_varMatches any Cconst_int and binds the underlying integer
| Const_natint_fixed of Nativeint.tMatches Cconst_natint with a given integer
| Const_natint of Nativeint.t pattern_varMatches any Cconst_natint and binds the underlying integer
| Binop of binop * cmm_pattern * cmm_patternMatches the corresponding Cop terms
| Guarded of {pat : cmm_pattern;guard : 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.
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
val run : Cmm.expression -> Cmm.expression clause list -> Cmm.expressionThe 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 clause list ->
'aAn extension of the engine allowing to run on arbitrary clauses. The default parameter is called if none of the clauses match.
module Syntax : sig ... endmodule Cmm_comparator : sig ... endCheck equivalence of Cmm terms for the purpose of checking that the engine produces terms equivalent to the ones produced by the original code.