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.)
type _ pattern_kind = | Expr : Cmm.expression Cmm_peephole_engine.pattern_kind| Int : int Cmm_peephole_engine.pattern_kind| Natint : Stdlib.Nativeint.t Cmm_peephole_engine.pattern_kind
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.
val create_var :
'a Cmm_peephole_engine.pattern_kind ->
string ->
'a Cmm_peephole_engine.pattern_varmodule Default_variables : sig ... endmodule Env : sig ... endtype cmm_pattern = | Any of Cmm.expression Cmm_peephole_engine.pattern_var(*Wildcard pattern, binding a variable
*)| As of Cmm.expression Cmm_peephole_engine.pattern_var * Cmm_peephole_engine.cmm_pattern(*Variable binding with nested pattern
*)| Const_int_fixed of int(*Matches
*)Cconst_intwith a given integer| Const_int of int Cmm_peephole_engine.pattern_var(*Matches any
*)Cconst_intand binds the underlying integer| Const_natint_fixed of Stdlib.Nativeint.t(*Matches
*)Cconst_natintwith a given integer| Const_natint of Stdlib.Nativeint.t Cmm_peephole_engine.pattern_var(*Matches any
*)Cconst_natintand binds the underlying integer| Binop of Cmm_peephole_engine.binop * Cmm_peephole_engine.cmm_pattern * Cmm_peephole_engine.cmm_pattern(*Matches the corresponding
*)Copterms| Guarded of {pat : Cmm_peephole_engine.cmm_pattern;guard : Cmm_peephole_engine.Env.t -> bool;
}(*When
*)patmatches, the corresponding environment is passed toguard. If this returnstruethen 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 Cmm_peephole_engine.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 Cmm_peephole_engine.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.