Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/cfg-ir/census-fn.sml
ViewVC logotype

View of /branches/vis15/src/compiler/cfg-ir/census-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3475 - (download) (annotate)
Wed Dec 2 18:48:58 2015 UTC (3 years, 8 months ago) by jhr
File size: 4628 byte(s)
working on merge
(* census-fn.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *
 * Compute use counts for IR variables and initialize their bindings.
 *)

functor CensusFn (IR : SSA) : sig

    structure IR : SSA

    val init : IR.program -> unit

    val inc : IR.var -> unit
    val dec : IR.var -> unit

  end = struct

    structure IR = IR

    fun inc (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
    fun dec (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)

    fun init (IR.Program{globals, inputInit, globalInit, initially, strands, ...}) = let
          fun clearVar (IR.V{useCnt, ...}) = useCnt := 0
        (* clear the counts of the variables defined in a node *)
          fun clearNode (IR.ND{kind, ...}) = (case kind
                 of IR.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis)
		  | IR.FOREACH{var, phis, ...} => (
		      clearVar var;
		      List.app (fn (x, _) => clearVar x) (!phis))
                  | IR.ASSIGN{stm=(x, _), ...} => clearVar x
                  | IR.MASSIGN{stm=(xs, _, _), ...} => List.app clearVar xs
                  | _ => ()
                (* end case *))
        (* clear the counts of the initially code *)
          fun clearInitially (IR.Initially{rangeInit, iters, create, ...}) = let
                fun clearIter (param, lo, hi) = clearVar param
                in
                  IR.CFG.apply clearNode rangeInit;
                  List.app clearIter iters;
                  IR.CFG.apply clearNode (#1 create)
                end
        (* clear the counts of the variables defined in an strand *)
          fun clearStrand (IR.Strand{params, state, stateInit, methods, ...}) = let
                fun clearMethod (IR.Method{body, ...}) = IR.CFG.apply clearNode body
                in
                  List.app clearVar params;
                  IR.CFG.apply clearNode stateInit;
                  List.app clearMethod methods
                end
        (* increment the use counts of a list of variables *)
          val incList = List.app inc
	(* increment the RHS variables of a phi *)
	  fun incPhi (_, ys) = List.app (Option.app inc) ys
        (* increment the counts of the variables used in a node.  The exitCount function
         * is used to count the live variables at exits, since the context affects the
         * treatment of these.
         *)
          fun incNode exitCount (IR.ND{kind, ...}) = (case kind
                 of IR.JOIN{phis, ...} => List.app incPhi (!phis)
                  | IR.COND{cond, ...} => inc cond
		  | IR.FOREACH{src, phis, ...} => (
		      inc src;
		      List.app incPhi (!phis))
                  | IR.ASSIGN{stm = (y, IR.GLOBAL gv), ...} => ()
                  | IR.ASSIGN{stm = (y, rhs), ...} => IR.RHS.app inc rhs
                  | IR.MASSIGN{stm = (ys, rator, xs), ...} => List.app inc xs
                  | IR.GASSIGN{rhs, ...} => inc rhs
                  | IR.NEW{args, ...} => incList args
                  | IR.SAVE{rhs, ...} => inc rhs
                  | IR.EXIT{live, ...} => exitCount live
                  | _ => ()
                (* end case *))
        (* increment the counts of variables used in the initially code *)
          fun incInitially (IR.Initially{create, rangeInit, iters, ...}) = let
                fun incIter (param, lo, hi) = (inc lo; inc hi)
                in
                  IR.CFG.apply (incNode (fn live => incList live)) rangeInit;
                  List.app incIter iters;
                  IR.CFG.apply (incNode (fn _ => ())) (#1 create);
                  List.app inc (#3 create)
                end
        (* increment the counts of the variables used in a strand *)
          fun incStrand (IR.Strand{params, state, stateInit, methods, ...}) = let
                fun incMethod (IR.Method{body, ...}) = 
                      IR.CFG.apply (incNode (fn live => incList live)) body
                in
                  IR.CFG.apply (incNode (fn live => incList live)) stateInit;
                  List.app incMethod methods
                end
          in
          (* first clear the counts of all variables *)
            IR.CFG.apply clearNode inputInit;
            IR.CFG.apply clearNode globalInit;
            clearInitially initially;
            List.app clearStrand strands;
          (* then count uses *)
            IR.CFG.apply (incNode (fn _ => ())) inputInit;
            IR.CFG.apply (incNode (fn _ => ())) globalInit;
            incInitially initially;
            List.app incStrand strands
          end

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0