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