Chris@19: (* Chris@19: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@19: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@19: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@19: * Chris@19: * This program is free software; you can redistribute it and/or modify Chris@19: * it under the terms of the GNU General Public License as published by Chris@19: * the Free Software Foundation; either version 2 of the License, or Chris@19: * (at your option) any later version. Chris@19: * Chris@19: * This program is distributed in the hope that it will be useful, Chris@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@19: * GNU General Public License for more details. Chris@19: * Chris@19: * You should have received a copy of the GNU General Public License Chris@19: * along with this program; if not, write to the Free Software Chris@19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@19: * Chris@19: *) Chris@19: Chris@19: Chris@19: open Util Chris@19: open Expr Chris@19: Chris@19: let node_insert x = Assoctable.insert Expr.hash x Chris@19: let node_lookup x = Assoctable.lookup Expr.hash (==) x Chris@19: Chris@19: (************************************************************* Chris@19: * Algebraic simplifier/elimination of common subexpressions Chris@19: *************************************************************) Chris@19: module AlgSimp : sig Chris@19: val algsimp : expr list -> expr list Chris@19: end = struct Chris@19: Chris@19: open Monads.StateMonad Chris@19: open Monads.MemoMonad Chris@19: open Assoctable Chris@19: Chris@19: let fetchSimp = Chris@19: fetchState >>= fun (s, _) -> returnM s Chris@19: let storeSimp s = Chris@19: fetchState >>= (fun (_, c) -> storeState (s, c)) Chris@19: let lookupSimpM key = Chris@19: fetchSimp >>= fun table -> Chris@19: returnM (node_lookup key table) Chris@19: let insertSimpM key value = Chris@19: fetchSimp >>= fun table -> Chris@19: storeSimp (node_insert key value table) Chris@19: Chris@19: let subset a b = Chris@19: List.for_all (fun x -> List.exists (fun y -> x == y) b) a Chris@19: Chris@19: let structurallyEqualCSE a b = Chris@19: match (a, b) with Chris@19: | (Num a, Num b) -> Number.equal a b Chris@19: | (NaN a, NaN b) -> a == b Chris@19: | (Load a, Load b) -> Variable.same a b Chris@19: | (Times (a, a'), Times (b, b')) -> Chris@19: ((a == b) && (a' == b')) or Chris@19: ((a == b') && (a' == b)) Chris@19: | (CTimes (a, a'), CTimes (b, b')) -> Chris@19: ((a == b) && (a' == b')) or Chris@19: ((a == b') && (a' == b)) Chris@19: | (CTimesJ (a, a'), CTimesJ (b, b')) -> ((a == b) && (a' == b')) Chris@19: | (Plus a, Plus b) -> subset a b && subset b a Chris@19: | (Uminus a, Uminus b) -> (a == b) Chris@19: | _ -> false Chris@19: Chris@19: let hashCSE x = Chris@19: if (!Magic.randomized_cse) then Chris@19: Oracle.hash x Chris@19: else Chris@19: Expr.hash x Chris@19: Chris@19: let equalCSE a b = Chris@19: if (!Magic.randomized_cse) then Chris@19: (structurallyEqualCSE a b || Oracle.likely_equal a b) Chris@19: else Chris@19: structurallyEqualCSE a b Chris@19: Chris@19: let fetchCSE = Chris@19: fetchState >>= fun (_, c) -> returnM c Chris@19: let storeCSE c = Chris@19: fetchState >>= (fun (s, _) -> storeState (s, c)) Chris@19: let lookupCSEM key = Chris@19: fetchCSE >>= fun table -> Chris@19: returnM (Assoctable.lookup hashCSE equalCSE key table) Chris@19: let insertCSEM key value = Chris@19: fetchCSE >>= fun table -> Chris@19: storeCSE (Assoctable.insert hashCSE key value table) Chris@19: Chris@19: (* memoize both x and Uminus x (unless x is already negated) *) Chris@19: let identityM x = Chris@19: let memo x = memoizing lookupCSEM insertCSEM returnM x in Chris@19: match x with Chris@19: Uminus _ -> memo x Chris@19: | _ -> memo x >>= fun x' -> memo (Uminus x') >> returnM x' Chris@19: Chris@19: let makeNode = identityM Chris@19: Chris@19: (* simplifiers for various kinds of nodes *) Chris@19: let rec snumM = function Chris@19: n when Number.is_zero n -> Chris@19: makeNode (Num (Number.zero)) Chris@19: | n when Number.negative n -> Chris@19: makeNode (Num (Number.negate n)) >>= suminusM Chris@19: | n -> makeNode (Num n) Chris@19: Chris@19: and suminusM = function Chris@19: Uminus x -> makeNode x Chris@19: | Num a when (Number.is_zero a) -> snumM Number.zero Chris@19: | a -> makeNode (Uminus a) Chris@19: Chris@19: and stimesM = function Chris@19: | (Uminus a, b) -> stimesM (a, b) >>= suminusM Chris@19: | (a, Uminus b) -> stimesM (a, b) >>= suminusM Chris@19: | (NaN I, CTimes (a, b)) -> stimesM (NaN I, b) >>= Chris@19: fun ib -> sctimesM (a, ib) Chris@19: | (NaN I, CTimesJ (a, b)) -> stimesM (NaN I, b) >>= Chris@19: fun ib -> sctimesjM (a, ib) Chris@19: | (Num a, Num b) -> snumM (Number.mul a b) Chris@19: | (Num a, Times (Num b, c)) -> Chris@19: snumM (Number.mul a b) >>= fun x -> stimesM (x, c) Chris@19: | (Num a, b) when Number.is_zero a -> snumM Number.zero Chris@19: | (Num a, b) when Number.is_one a -> makeNode b Chris@19: | (Num a, b) when Number.is_mone a -> suminusM b Chris@19: | (a, b) when is_known_constant b && not (is_known_constant a) -> Chris@19: stimesM (b, a) Chris@19: | (a, b) -> makeNode (Times (a, b)) Chris@19: Chris@19: and sctimesM = function Chris@19: | (Uminus a, b) -> sctimesM (a, b) >>= suminusM Chris@19: | (a, Uminus b) -> sctimesM (a, b) >>= suminusM Chris@19: | (a, b) -> makeNode (CTimes (a, b)) Chris@19: Chris@19: and sctimesjM = function Chris@19: | (Uminus a, b) -> sctimesjM (a, b) >>= suminusM Chris@19: | (a, Uminus b) -> sctimesjM (a, b) >>= suminusM Chris@19: | (a, b) -> makeNode (CTimesJ (a, b)) Chris@19: Chris@19: and reduce_sumM x = match x with Chris@19: [] -> returnM [] Chris@19: | [Num a] -> Chris@19: if (Number.is_zero a) then Chris@19: returnM [] Chris@19: else returnM x Chris@19: | [Uminus (Num a)] -> Chris@19: if (Number.is_zero a) then Chris@19: returnM [] Chris@19: else returnM x Chris@19: | (Num a) :: (Num b) :: s -> Chris@19: snumM (Number.add a b) >>= fun x -> Chris@19: reduce_sumM (x :: s) Chris@19: | (Num a) :: (Uminus (Num b)) :: s -> Chris@19: snumM (Number.sub a b) >>= fun x -> Chris@19: reduce_sumM (x :: s) Chris@19: | (Uminus (Num a)) :: (Num b) :: s -> Chris@19: snumM (Number.sub b a) >>= fun x -> Chris@19: reduce_sumM (x :: s) Chris@19: | (Uminus (Num a)) :: (Uminus (Num b)) :: s -> Chris@19: snumM (Number.add a b) >>= Chris@19: suminusM >>= fun x -> Chris@19: reduce_sumM (x :: s) Chris@19: | ((Num _) as a) :: b :: s -> reduce_sumM (b :: a :: s) Chris@19: | ((Uminus (Num _)) as a) :: b :: s -> reduce_sumM (b :: a :: s) Chris@19: | a :: s -> Chris@19: reduce_sumM s >>= fun s' -> returnM (a :: s') Chris@19: Chris@19: and collectible1 = function Chris@19: | NaN _ -> false Chris@19: | Uminus x -> collectible1 x Chris@19: | _ -> true Chris@19: and collectible (a, b) = collectible1 a Chris@19: Chris@19: (* collect common factors: ax + bx -> (a+b)x *) Chris@19: and collectM which x = Chris@19: let rec findCoeffM which = function Chris@19: | Times (a, b) when collectible (which (a, b)) -> returnM (which (a, b)) Chris@19: | Uminus x -> Chris@19: findCoeffM which x >>= fun (coeff, b) -> Chris@19: suminusM coeff >>= fun mcoeff -> Chris@19: returnM (mcoeff, b) Chris@19: | x -> snumM Number.one >>= fun one -> returnM (one, x) Chris@19: and separateM xpr = function Chris@19: [] -> returnM ([], []) Chris@19: | a :: b -> Chris@19: separateM xpr b >>= fun (w, wo) -> Chris@19: (* try first factor *) Chris@19: findCoeffM (fun (a, b) -> (a, b)) a >>= fun (c, x) -> Chris@19: if (xpr == x) && collectible (c, x) then returnM (c :: w, wo) Chris@19: else Chris@19: (* try second factor *) Chris@19: findCoeffM (fun (a, b) -> (b, a)) a >>= fun (c, x) -> Chris@19: if (xpr == x) && collectible (c, x) then returnM (c :: w, wo) Chris@19: else returnM (w, a :: wo) Chris@19: in match x with Chris@19: [] -> returnM x Chris@19: | [a] -> returnM x Chris@19: | a :: b -> Chris@19: findCoeffM which a >>= fun (_, xpr) -> Chris@19: separateM xpr x >>= fun (w, wo) -> Chris@19: collectM which wo >>= fun wo' -> Chris@19: splusM w >>= fun w' -> Chris@19: stimesM (w', xpr) >>= fun t' -> Chris@19: returnM (t':: wo') Chris@19: Chris@19: and mangleSumM x = returnM x Chris@19: >>= reduce_sumM Chris@19: >>= collectM (fun (a, b) -> (a, b)) Chris@19: >>= collectM (fun (a, b) -> (b, a)) Chris@19: >>= reduce_sumM Chris@19: >>= deepCollectM !Magic.deep_collect_depth Chris@19: >>= reduce_sumM Chris@19: Chris@19: and reorder_uminus = function (* push all Uminuses to the end *) Chris@19: [] -> [] Chris@19: | ((Uminus _) as a' :: b) -> (reorder_uminus b) @ [a'] Chris@19: | (a :: b) -> a :: (reorder_uminus b) Chris@19: Chris@19: and canonicalizeM = function Chris@19: [] -> snumM Number.zero Chris@19: | [a] -> makeNode a (* one term *) Chris@19: | a -> generateFusedMultAddM (reorder_uminus a) Chris@19: Chris@19: and generateFusedMultAddM = Chris@19: let rec is_multiplication = function Chris@19: | Times (Num a, b) -> true Chris@19: | Uminus (Times (Num a, b)) -> true Chris@19: | _ -> false Chris@19: and separate = function Chris@19: [] -> ([], [], Number.zero) Chris@19: | (Times (Num a, b)) as this :: c -> Chris@19: let (x, y, max) = separate c in Chris@19: let newmax = if (Number.greater a max) then a else max in Chris@19: (this :: x, y, newmax) Chris@19: | (Uminus (Times (Num a, b))) as this :: c -> Chris@19: let (x, y, max) = separate c in Chris@19: let newmax = if (Number.greater a max) then a else max in Chris@19: (this :: x, y, newmax) Chris@19: | this :: c -> Chris@19: let (x, y, max) = separate c in Chris@19: (x, this :: y, max) Chris@19: in fun l -> Chris@19: if !Magic.enable_fma && count is_multiplication l >= 2 then Chris@19: let (w, wo, max) = separate l in Chris@19: snumM (Number.div Number.one max) >>= fun invmax' -> Chris@19: snumM max >>= fun max' -> Chris@19: mapM (fun x -> stimesM (invmax', x)) w >>= splusM >>= fun pw' -> Chris@19: stimesM (max', pw') >>= fun mw' -> Chris@19: splusM (wo @ [mw']) Chris@19: else Chris@19: makeNode (Plus l) Chris@19: Chris@19: Chris@19: and negative = function Chris@19: Uminus _ -> true Chris@19: | _ -> false Chris@19: Chris@19: (* Chris@19: * simplify patterns of the form Chris@19: * Chris@19: * ((c_1 * a + ...) + ...) + (c_2 * a + ...) Chris@19: * Chris@19: * The pattern includes arbitrary coefficients and minus signs. Chris@19: * A common case of this pattern is the butterfly Chris@19: * (a + b) + (a - b) Chris@19: * (a + b) - (a - b) Chris@19: *) Chris@19: (* this whole procedure needs much more thought *) Chris@19: and deepCollectM maxdepth l = Chris@19: let rec findTerms depth x = match x with Chris@19: | Uminus x -> findTerms depth x Chris@19: | Times (Num _, b) -> (findTerms (depth - 1) b) Chris@19: | Plus l when depth > 0 -> Chris@19: x :: List.flatten (List.map (findTerms (depth - 1)) l) Chris@19: | x -> [x] Chris@19: and duplicates = function Chris@19: [] -> [] Chris@19: | a :: b -> if List.memq a b then a :: duplicates b Chris@19: else duplicates b Chris@19: Chris@19: in let rec splitDuplicates depth d x = Chris@19: if (List.memq x d) then Chris@19: snumM (Number.zero) >>= fun zero -> Chris@19: returnM (zero, x) Chris@19: else match x with Chris@19: | Times (a, b) -> Chris@19: splitDuplicates (depth - 1) d a >>= fun (a', xa) -> Chris@19: splitDuplicates (depth - 1) d b >>= fun (b', xb) -> Chris@19: stimesM (a', b') >>= fun ab -> Chris@19: stimesM (a, xb) >>= fun xb' -> Chris@19: stimesM (xa, b) >>= fun xa' -> Chris@19: stimesM (xa, xb) >>= fun xab -> Chris@19: splusM [xa'; xb'; xab] >>= fun x -> Chris@19: returnM (ab, x) Chris@19: | Uminus a -> Chris@19: splitDuplicates depth d a >>= fun (x, y) -> Chris@19: suminusM x >>= fun ux -> Chris@19: suminusM y >>= fun uy -> Chris@19: returnM (ux, uy) Chris@19: | Plus l when depth > 0 -> Chris@19: mapM (splitDuplicates (depth - 1) d) l >>= fun ld -> Chris@19: let (l', d') = List.split ld in Chris@19: splusM l' >>= fun p -> Chris@19: splusM d' >>= fun d'' -> Chris@19: returnM (p, d'') Chris@19: | x -> Chris@19: snumM (Number.zero) >>= fun zero' -> Chris@19: returnM (x, zero') Chris@19: Chris@19: in let l' = List.flatten (List.map (findTerms maxdepth) l) Chris@19: in match duplicates l' with Chris@19: | [] -> returnM l Chris@19: | d -> Chris@19: mapM (splitDuplicates maxdepth d) l >>= fun ld -> Chris@19: let (l', d') = List.split ld in Chris@19: splusM l' >>= fun l'' -> Chris@19: let rec flattenPlusM = function Chris@19: | Plus l -> returnM l Chris@19: | Uminus x -> Chris@19: flattenPlusM x >>= mapM suminusM Chris@19: | x -> returnM [x] Chris@19: in Chris@19: mapM flattenPlusM d' >>= fun d'' -> Chris@19: splusM (List.flatten d'') >>= fun d''' -> Chris@19: mangleSumM [l''; d'''] Chris@19: Chris@19: and splusM l = Chris@19: let fma_heuristics x = Chris@19: if !Magic.enable_fma then Chris@19: match x with Chris@19: | [Uminus (Times _); Times _] -> Some false Chris@19: | [Times _; Uminus (Times _)] -> Some false Chris@19: | [Uminus (_); Times _] -> Some true Chris@19: | [Times _; Uminus (Plus _)] -> Some true Chris@19: | [_; Uminus (Times _)] -> Some false Chris@19: | [Uminus (Times _); _] -> Some false Chris@19: | _ -> None Chris@19: else Chris@19: None Chris@19: in Chris@19: mangleSumM l >>= fun l' -> Chris@19: (* no terms are negative. Don't do anything *) Chris@19: if not (List.exists negative l') then Chris@19: canonicalizeM l' Chris@19: (* all terms are negative. Negate them all and collect the minus sign *) Chris@19: else if List.for_all negative l' then Chris@19: mapM suminusM l' >>= splusM >>= suminusM Chris@19: else match fma_heuristics l' with Chris@19: | Some true -> mapM suminusM l' >>= splusM >>= suminusM Chris@19: | Some false -> canonicalizeM l' Chris@19: | None -> Chris@19: (* Ask the Oracle for the canonical form *) Chris@19: if (not !Magic.randomized_cse) && Chris@19: Oracle.should_flip_sign (Plus l') then Chris@19: mapM suminusM l' >>= splusM >>= suminusM Chris@19: else Chris@19: canonicalizeM l' Chris@19: Chris@19: (* monadic style algebraic simplifier for the dag *) Chris@19: let rec algsimpM x = Chris@19: memoizing lookupSimpM insertSimpM Chris@19: (function Chris@19: | Num a -> snumM a Chris@19: | NaN _ as x -> makeNode x Chris@19: | Plus a -> Chris@19: mapM algsimpM a >>= splusM Chris@19: | Times (a, b) -> Chris@19: (algsimpM a >>= fun a' -> Chris@19: algsimpM b >>= fun b' -> Chris@19: stimesM (a', b')) Chris@19: | CTimes (a, b) -> Chris@19: (algsimpM a >>= fun a' -> Chris@19: algsimpM b >>= fun b' -> Chris@19: sctimesM (a', b')) Chris@19: | CTimesJ (a, b) -> Chris@19: (algsimpM a >>= fun a' -> Chris@19: algsimpM b >>= fun b' -> Chris@19: sctimesjM (a', b')) Chris@19: | Uminus a -> Chris@19: algsimpM a >>= suminusM Chris@19: | Store (v, a) -> Chris@19: algsimpM a >>= fun a' -> Chris@19: makeNode (Store (v, a')) Chris@19: | Load _ as x -> makeNode x) Chris@19: x Chris@19: Chris@19: let initialTable = (empty, empty) Chris@19: let simp_roots = mapM algsimpM Chris@19: let algsimp = runM initialTable simp_roots Chris@19: end Chris@19: Chris@19: (************************************************************* Chris@19: * Network transposition algorithm Chris@19: *************************************************************) Chris@19: module Transpose = struct Chris@19: open Monads.StateMonad Chris@19: open Monads.MemoMonad Chris@19: open Littlesimp Chris@19: Chris@19: let fetchDuals = fetchState Chris@19: let storeDuals = storeState Chris@19: Chris@19: let lookupDualsM key = Chris@19: fetchDuals >>= fun table -> Chris@19: returnM (node_lookup key table) Chris@19: Chris@19: let insertDualsM key value = Chris@19: fetchDuals >>= fun table -> Chris@19: storeDuals (node_insert key value table) Chris@19: Chris@19: let rec visit visited vtable parent_table = function Chris@19: [] -> (visited, parent_table) Chris@19: | node :: rest -> Chris@19: match node_lookup node vtable with Chris@19: | Some _ -> visit visited vtable parent_table rest Chris@19: | None -> Chris@19: let children = match node with Chris@19: | Store (v, n) -> [n] Chris@19: | Plus l -> l Chris@19: | Times (a, b) -> [a; b] Chris@19: | CTimes (a, b) -> [a; b] Chris@19: | CTimesJ (a, b) -> [a; b] Chris@19: | Uminus x -> [x] Chris@19: | _ -> [] Chris@19: in let rec loop t = function Chris@19: [] -> t Chris@19: | a :: rest -> Chris@19: (match node_lookup a t with Chris@19: None -> loop (node_insert a [node] t) rest Chris@19: | Some c -> loop (node_insert a (node :: c) t) rest) Chris@19: in Chris@19: (visit Chris@19: (node :: visited) Chris@19: (node_insert node () vtable) Chris@19: (loop parent_table children) Chris@19: (children @ rest)) Chris@19: Chris@19: let make_transposer parent_table = Chris@19: let rec termM node candidate_parent = Chris@19: match candidate_parent with Chris@19: | Store (_, n) when n == node -> Chris@19: dualM candidate_parent >>= fun x' -> returnM [x'] Chris@19: | Plus (l) when List.memq node l -> Chris@19: dualM candidate_parent >>= fun x' -> returnM [x'] Chris@19: | Times (a, b) when b == node -> Chris@19: dualM candidate_parent >>= fun x' -> Chris@19: returnM [makeTimes (a, x')] Chris@19: | CTimes (a, b) when b == node -> Chris@19: dualM candidate_parent >>= fun x' -> Chris@19: returnM [CTimes (a, x')] Chris@19: | CTimesJ (a, b) when b == node -> Chris@19: dualM candidate_parent >>= fun x' -> Chris@19: returnM [CTimesJ (a, x')] Chris@19: | Uminus n when n == node -> Chris@19: dualM candidate_parent >>= fun x' -> Chris@19: returnM [makeUminus x'] Chris@19: | _ -> returnM [] Chris@19: Chris@19: and dualExpressionM this_node = Chris@19: mapM (termM this_node) Chris@19: (match node_lookup this_node parent_table with Chris@19: | Some a -> a Chris@19: | None -> failwith "bug in dualExpressionM" Chris@19: ) >>= fun l -> Chris@19: returnM (makePlus (List.flatten l)) Chris@19: Chris@19: and dualM this_node = Chris@19: memoizing lookupDualsM insertDualsM Chris@19: (function Chris@19: | Load v as x -> Chris@19: if (Variable.is_constant v) then Chris@19: returnM (Load v) Chris@19: else Chris@19: (dualExpressionM x >>= fun d -> Chris@19: returnM (Store (v, d))) Chris@19: | Store (v, x) -> returnM (Load v) Chris@19: | x -> dualExpressionM x) Chris@19: this_node Chris@19: Chris@19: in dualM Chris@19: Chris@19: let is_store = function Chris@19: | Store _ -> true Chris@19: | _ -> false Chris@19: Chris@19: let transpose dag = Chris@19: let _ = Util.info "begin transpose" in Chris@19: let (all_nodes, parent_table) = Chris@19: visit [] Assoctable.empty Assoctable.empty dag in Chris@19: let transposerM = make_transposer parent_table in Chris@19: let mapTransposerM = mapM transposerM in Chris@19: let duals = runM Assoctable.empty mapTransposerM all_nodes in Chris@19: let roots = List.filter is_store duals in Chris@19: let _ = Util.info "end transpose" in Chris@19: roots Chris@19: end Chris@19: Chris@19: Chris@19: (************************************************************* Chris@19: * Various dag statistics Chris@19: *************************************************************) Chris@19: module Stats : sig Chris@19: type complexity Chris@19: val complexity : Expr.expr list -> complexity Chris@19: val same_complexity : complexity -> complexity -> bool Chris@19: val leq_complexity : complexity -> complexity -> bool Chris@19: val to_string : complexity -> string Chris@19: end = struct Chris@19: type complexity = int * int * int * int * int * int Chris@19: let rec visit visited vtable = function Chris@19: [] -> visited Chris@19: | node :: rest -> Chris@19: match node_lookup node vtable with Chris@19: Some _ -> visit visited vtable rest Chris@19: | None -> Chris@19: let children = match node with Chris@19: Store (v, n) -> [n] Chris@19: | Plus l -> l Chris@19: | Times (a, b) -> [a; b] Chris@19: | Uminus x -> [x] Chris@19: | _ -> [] Chris@19: in visit (node :: visited) Chris@19: (node_insert node () vtable) Chris@19: (children @ rest) Chris@19: Chris@19: let complexity dag = Chris@19: let rec loop (load, store, plus, times, uminus, num) = function Chris@19: [] -> (load, store, plus, times, uminus, num) Chris@19: | node :: rest -> Chris@19: loop Chris@19: (match node with Chris@19: | Load _ -> (load + 1, store, plus, times, uminus, num) Chris@19: | Store _ -> (load, store + 1, plus, times, uminus, num) Chris@19: | Plus x -> (load, store, plus + (List.length x - 1), times, uminus, num) Chris@19: | Times _ -> (load, store, plus, times + 1, uminus, num) Chris@19: | Uminus _ -> (load, store, plus, times, uminus + 1, num) Chris@19: | Num _ -> (load, store, plus, times, uminus, num + 1) Chris@19: | CTimes _ -> (load, store, plus, times, uminus, num) Chris@19: | CTimesJ _ -> (load, store, plus, times, uminus, num) Chris@19: | NaN _ -> (load, store, plus, times, uminus, num)) Chris@19: rest Chris@19: in let (l, s, p, t, u, n) = Chris@19: loop (0, 0, 0, 0, 0, 0) (visit [] Assoctable.empty dag) Chris@19: in (l, s, p, t, u, n) Chris@19: Chris@19: let weight (l, s, p, t, u, n) = Chris@19: l + s + 10 * p + 20 * t + u + n Chris@19: Chris@19: let same_complexity a b = weight a = weight b Chris@19: let leq_complexity a b = weight a <= weight b Chris@19: Chris@19: let to_string (l, s, p, t, u, n) = Chris@19: Printf.sprintf "ld=%d st=%d add=%d mul=%d uminus=%d num=%d\n" Chris@19: l s p t u n Chris@19: Chris@19: end Chris@19: Chris@19: (* simplify the dag *) Chris@19: let algsimp v = Chris@19: let rec simplification_loop v = Chris@19: let () = Util.info "simplification step" in Chris@19: let complexity = Stats.complexity v in Chris@19: let () = Util.info ("complexity = " ^ (Stats.to_string complexity)) in Chris@19: let v = (AlgSimp.algsimp @@ Transpose.transpose @@ Chris@19: AlgSimp.algsimp @@ Transpose.transpose) v in Chris@19: let complexity' = Stats.complexity v in Chris@19: let () = Util.info ("complexity = " ^ (Stats.to_string complexity')) in Chris@19: if (Stats.leq_complexity complexity' complexity) then Chris@19: let () = Util.info "end algsimp" in Chris@19: v Chris@19: else Chris@19: simplification_loop v Chris@19: Chris@19: in Chris@19: let () = Util.info "begin algsimp" in Chris@19: let v = AlgSimp.algsimp v in Chris@19: if !Magic.network_transposition then simplification_loop v else v Chris@19: