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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (download) (annotate)
Tue Oct 19 13:14:20 2010 UTC (9 years ago) by jhr
File size: 1991 byte(s)
  Upated URL in header to diderot-language.cs.uchicago.edu
(* backward-dfa-fn.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Backward data-flow analysis for IL code.
 *)

functor BackwardDFAFn (D : DOMAIN) :> sig

    structure D : DOMAIN

  (* given a function for defining out values for exits and the root node, do the
   * backward DFA on the CFG and return the list of nodes analysed.
   *)
    val analyse : (D.IL.node -> 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

    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 (exitVal, stm) = let
	(* use DFS order to get quicker convergence *)
	  val nodes = IL.sortNodes stm
	(* set initial values for exit nodes *)
	  val _ = List.app (fn nd => if IL.Node.hasSucc nd then () else setIn(nd, exitVal nd)) nodes
	  fun iterate () = let
		val anyChange = ref false
		fun doNode nd = let
		      val outValue = D.join (List.map getIn (IL.Node.succs nd))
		      in
			if D.same(getOut nd, outValue)
			  then ()(* output unchanged, so output will be unchanged *)
			  else let
			    val inValue = D.transfer (outValue, nd)
			    in
			      anyChange := true;
			      setOut (nd, outValue);
			      if D.same(getIn nd, inValue)
				then ()
				else setIn(nd, inValue)
			    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