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