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

SCM Repository

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

View of /branches/pure-cfg/src/compiler/IL/census-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 736 - (download) (annotate)
Sat Apr 2 18:50:43 2011 UTC (8 years, 5 months ago) by jhr
File size: 3812 byte(s)
  Overhaul in the way that the MidIL and LowIL represent tensor types (especially
  for orders > 1).  We still need to deal with the flattening of tensor-value
  construction
(* census-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Compute use counts for IL variables and initialize their bindings.
 *)

functor CensusFn (IL : SSA) : sig

    structure IL : SSA

    val init : IL.program -> unit

    val inc : IL.var -> unit

  end = struct

    structure IL = IL

    fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
    fun setBinding (IL.V{bind, ...}, vb) = bind := vb

    fun init (IL.Program{globals, globalInit, initially, strands}) = let
	  fun clearVar (IL.V{useCnt, ...}) = useCnt := 0
	(* clear the counts of the variables defined in a node *)
	  fun clearNode (IL.ND{kind, ...}) = (case kind
		 of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis)
		  | IL.ASSIGN{stm=(x, _), ...} => clearVar x
		  | _ => ()
		(* end case *))
	(* clear the counts of the initially code *)
	  fun clearInitially (IL.Initially{rangeInit, iters, create, ...}) = let
		fun clearIter (param, lo, hi) = clearVar param
		in
		  IL.CFG.apply clearNode rangeInit;
		  List.app clearIter iters;
		  IL.CFG.apply clearNode (#1 create)
		end
	(* clear the counts of the variables defined in an strand *)
	  fun clearStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
		fun clearMethod (IL.Method{stateIn, body, ...}) = (
		      List.app clearVar stateIn;
		      IL.CFG.apply clearNode body)
		in
		  List.app clearVar params;
		  List.app (clearVar o #2) state;
		  IL.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 counts of the variables used in a node *)
	  fun incNode (IL.ND{kind, ...}) = (case kind
		 of IL.JOIN{phis, ...} => let
		      fun f (y, xs) = (
			    setBinding (y, IL.VB_PHI xs);
			    incList xs)
		      in
			List.app f (!phis)
		      end
		  | IL.COND{cond, ...} => inc cond
		  | IL.ASSIGN{stm = (y, rhs), ...} => (
		      setBinding (y, IL.VB_RHS rhs);
		      case rhs
		       of (IL.VAR x) => inc x
			| (IL.LIT _) => ()
			| (IL.OP(_, args)) => incList args
			| (IL.APPLY(_, args)) => incList args
			| (IL.CONS(_, args)) => incList args
		      (* end case *))
		  | IL.NEW{args, ...} => incList args
		  | IL.EXIT{live, ...} => incList live
		  | _ => ()
		(* end case *))
	(* increment the counts of variables used in the initially code *)
	  fun incInitially (IL.Initially{create, rangeInit, iters, ...}) = let
		fun incIter (param, lo, hi) = (
		      setBinding (param, IL.VB_PARAM);	(* QUESTION: should there be a special kind for this? *)
		      inc lo; inc hi)
		in
		  IL.CFG.apply incNode rangeInit;
		  List.app incIter iters;
		  IL.CFG.apply incNode (#1 create);
		  List.app inc (#3 create)
		end
	(* increment the counts of the variables used in a strand *)
	  fun incStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
		fun incMethod (IL.Method{stateIn, stateOut, body, ...}) = (
		      List.app (fn x => setBinding(x, IL.VB_STATE_VAR)) stateIn;
(* now that we are counting the live variables at EXIT nodes, I think that we don't
 * need to count the stateOut occurances. [JHR 2011-03-08]
		      incList stateOut;
 *)
		      IL.CFG.apply incNode body)
		in
		  List.app (fn x => setBinding(x, IL.VB_PARAM)) params;
		  List.app (inc o #2) state;  (* state variables are implicitly used *)
		  IL.CFG.apply incNode stateInit;
		  List.app incMethod methods
		end
	  in
	  (* first clear the counts of all variables *)
	    List.app clearVar globals;
	    IL.CFG.apply clearNode globalInit;
	    clearInitially initially;
	    List.app clearStrand strands;
	  (* then count uses *)
	    IL.CFG.apply incNode globalInit;
	    incInitially initially;
	    List.app incStrand strands
	  end

  end

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