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