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