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