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

SCM Repository

[diderot] View of /trunk/src/compiler/IL/forward-dfa-fn.sml
ViewVC logotype

View of /trunk/src/compiler/IL/forward-dfa-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 417 - (download) (annotate)
Sun Oct 17 00:32:30 2010 UTC (8 years, 11 months ago) by jhr
File size: 1903 byte(s)
  Added checking for HighIL representation
(* forward-dfa-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 *
 * Forward data-flow analysis for IL code.
 *)

functor ForwardDFAFn (D : DOMAIN) :> sig

    structure D : DOMAIN = D

  (* given the entry value and root statement, do the forward DFA on the CFG and
   * return the list of nodes analysed.
   *)
    val analyse : D.t * D.IL.stmt -> D.IL.node list

  (* get results for a node *)
    val inValue : D.IL.node -> D.t
    val outValue : D.IL.node -> D.t

  (* scrub results to reclaim space *)
    val scrub : D.IL.node list -> unit

  end = struct

    structure D = D
    structure IL = D.IL

    type result = IL.node list

    val {setFn=setIn, getFn=getIn, clrFn= clrIn, ...} =
	  PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)
    val {setFn=setOut, getFn=getOut, clrFn=clrOut, ...} =
	  PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)

    fun clear node = (clrIn node; clrOut node)

    fun analyse (entryVal, stm) = let
	  val _ = setOut(IL.Stmt.entry stm, entryVal)
	(* use reverse DFS order to get quicker convergence *)
	  val nodes = List.rev (IL.sortNodes stm)
	  fun iterate () = let
		val anyChange = ref false
		fun doNode nd = let
		      val inValue = D.join (List.map getOut (IL.Node.preds nd))
		      in
			if D.same(getIn nd, inValue)
			  then () (* input unchanged, so output will be unchanged *)
			  else let
			    val outValue = D.transfer (inValue, nd)
			    in
			      anyChange := true;
			      setIn (nd, inValue);
			      if D.same(getOut nd, outValue)
				then ()
				else setOut(nd, outValue)
			    end
		      end
		in
		  List.app doNode nodes;
		  if !anyChange then iterate() else ()
		end
	  in
	    iterate (); nodes
	  end

    fun inValue nd = getIn nd
    fun outValue nd = getOut nd

    fun scrub nodes = List.app clear nodes

  end

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