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