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: open Util cannam@127: cannam@127: (* Here, we have functions to transform a sequence of assignments cannam@127: (variable = expression) into a DAG (a directed, acyclic graph). cannam@127: The nodes of the DAG are the assignments, and the edges indicate cannam@127: dependencies. (The DAG is analyzed in the scheduler to find an cannam@127: efficient ordering of the assignments.) cannam@127: cannam@127: This file also contains utilities to manipulate the DAG in various cannam@127: ways. *) cannam@127: cannam@127: (******************************************** cannam@127: * Dag structure cannam@127: ********************************************) cannam@127: type color = RED | BLUE | BLACK | YELLOW cannam@127: cannam@127: type dagnode = cannam@127: { assigned: Variable.variable; cannam@127: mutable expression: Expr.expr; cannam@127: input_variables: Variable.variable list; cannam@127: mutable successors: dagnode list; cannam@127: mutable predecessors: dagnode list; cannam@127: mutable label: int; cannam@127: mutable color: color} cannam@127: cannam@127: type dag = Dag of (dagnode list) cannam@127: cannam@127: (* true if node uses v *) cannam@127: let node_uses v node = cannam@127: List.exists (Variable.same v) node.input_variables cannam@127: cannam@127: (* true if assignment of v clobbers any input of node *) cannam@127: let node_clobbers node v = cannam@127: List.exists (Variable.same_location v) node.input_variables cannam@127: cannam@127: (* true if nodeb depends on nodea *) cannam@127: let depends_on nodea nodeb = cannam@127: node_uses nodea.assigned nodeb || cannam@127: node_clobbers nodea nodeb.assigned cannam@127: cannam@127: (* transform an assignment list into a dag *) cannam@127: let makedag alist = cannam@127: let dag = List.map cannam@127: (fun assignment -> cannam@127: let (v, x) = assignment in cannam@127: { assigned = v; cannam@127: expression = x; cannam@127: input_variables = Expr.find_vars x; cannam@127: successors = []; cannam@127: predecessors = []; cannam@127: label = 0; cannam@127: color = BLACK }) cannam@127: alist cannam@127: in begin cannam@127: for_list dag (fun i -> cannam@127: for_list dag (fun j -> cannam@127: if depends_on i j then begin cannam@127: i.successors <- j :: i.successors; cannam@127: j.predecessors <- i :: j.predecessors; cannam@127: end)); cannam@127: Dag dag; cannam@127: end cannam@127: cannam@127: let map f (Dag dag) = Dag (List.map f dag) cannam@127: let for_all (Dag dag) f = cannam@127: (* type system loophole *) cannam@127: let make_unit _ = () in cannam@127: make_unit (List.map f dag) cannam@127: let to_list (Dag dag) = dag cannam@127: cannam@127: let find_node f (Dag dag) = Util.find_elem f dag cannam@127: cannam@127: (* breadth-first search *) cannam@127: let rec bfs (Dag dag) node init_label = cannam@127: let _ = node.label <- init_label in cannam@127: let rec loop = function cannam@127: [] -> () cannam@127: | node :: rest -> cannam@127: let neighbors = node.predecessors @ node.successors in cannam@127: let m = min_list (List.map (fun node -> node.label) neighbors) in cannam@127: if (node.label > m + 1) then begin cannam@127: node.label <- m + 1; cannam@127: loop (rest @ neighbors); cannam@127: end else cannam@127: loop rest cannam@127: in let neighbors = node.predecessors @ node.successors in cannam@127: loop neighbors cannam@127: