Chris@10: (* Chris@10: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: *) Chris@10: Chris@10: open Util Chris@10: Chris@10: (* Here, we have functions to transform a sequence of assignments Chris@10: (variable = expression) into a DAG (a directed, acyclic graph). Chris@10: The nodes of the DAG are the assignments, and the edges indicate Chris@10: dependencies. (The DAG is analyzed in the scheduler to find an Chris@10: efficient ordering of the assignments.) Chris@10: Chris@10: This file also contains utilities to manipulate the DAG in various Chris@10: ways. *) Chris@10: Chris@10: (******************************************** Chris@10: * Dag structure Chris@10: ********************************************) Chris@10: type color = RED | BLUE | BLACK | YELLOW Chris@10: Chris@10: type dagnode = Chris@10: { assigned: Variable.variable; Chris@10: mutable expression: Expr.expr; Chris@10: input_variables: Variable.variable list; Chris@10: mutable successors: dagnode list; Chris@10: mutable predecessors: dagnode list; Chris@10: mutable label: int; Chris@10: mutable color: color} Chris@10: Chris@10: type dag = Dag of (dagnode list) Chris@10: Chris@10: (* true if node uses v *) Chris@10: let node_uses v node = Chris@10: List.exists (Variable.same v) node.input_variables Chris@10: Chris@10: (* true if assignment of v clobbers any input of node *) Chris@10: let node_clobbers node v = Chris@10: List.exists (Variable.same_location v) node.input_variables Chris@10: Chris@10: (* true if nodeb depends on nodea *) Chris@10: let depends_on nodea nodeb = Chris@10: node_uses nodea.assigned nodeb or Chris@10: node_clobbers nodea nodeb.assigned Chris@10: Chris@10: (* transform an assignment list into a dag *) Chris@10: let makedag alist = Chris@10: let dag = List.map Chris@10: (fun assignment -> Chris@10: let (v, x) = assignment in Chris@10: { assigned = v; Chris@10: expression = x; Chris@10: input_variables = Expr.find_vars x; Chris@10: successors = []; Chris@10: predecessors = []; Chris@10: label = 0; Chris@10: color = BLACK }) Chris@10: alist Chris@10: in begin Chris@10: for_list dag (fun i -> Chris@10: for_list dag (fun j -> Chris@10: if depends_on i j then begin Chris@10: i.successors <- j :: i.successors; Chris@10: j.predecessors <- i :: j.predecessors; Chris@10: end)); Chris@10: Dag dag; Chris@10: end Chris@10: Chris@10: let map f (Dag dag) = Dag (List.map f dag) Chris@10: let for_all (Dag dag) f = Chris@10: (* type system loophole *) Chris@10: let make_unit _ = () in Chris@10: make_unit (List.map f dag) Chris@10: let to_list (Dag dag) = dag Chris@10: Chris@10: let find_node f (Dag dag) = Util.find_elem f dag Chris@10: Chris@10: (* breadth-first search *) Chris@10: let rec bfs (Dag dag) node init_label = Chris@10: let _ = node.label <- init_label in Chris@10: let rec loop = function Chris@10: [] -> () Chris@10: | node :: rest -> Chris@10: let neighbors = node.predecessors @ node.successors in Chris@10: let m = min_list (List.map (fun node -> node.label) neighbors) in Chris@10: if (node.label > m + 1) then begin Chris@10: node.label <- m + 1; Chris@10: loop (rest @ neighbors); Chris@10: end else Chris@10: loop rest Chris@10: in let neighbors = node.predecessors @ node.successors in Chris@10: loop neighbors Chris@10: