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

SCM Repository

[diderot] View of /branches/pure-cfg/src/compiler/mid-il/mid-contract.sml
ViewVC logotype

View of /branches/pure-cfg/src/compiler/mid-il/mid-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (download) (annotate)
Tue Oct 27 15:16:36 2015 UTC (4 years, 10 months ago) by jhr
File size: 3042 byte(s)
making copyrights consistent for all code in the repository
(* mid-contract.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.
 *
 * Contraction phase for MidIL.
 *)

structure MidContract : sig

    val transform : MidIL.program -> MidIL.program

  end = struct

    structure IL = MidIL
    structure Op = MidOps
    structure V = IL.Var
    structure ST = Stats

  (********** Counters for statistics **********)
    val cntEigenVals		= ST.newCounter "mid-opt:eigen-vals"
    val cntUnused		= ST.newCounter "mid-opt:unused"
    val firstCounter            = cntEigenVals
    val lastCounter             = cntUnused

    structure UnusedElim = UnusedElimFn (
	structure IL = IL
	val cntUnused = cntUnused)

    fun useCount (IL.V{useCnt, ...}) = !useCnt

  (* adjust a variable's use count *)
    fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
    fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)

  (* simplify expressions *)
    fun simplify nd = (case IL.Node.kind nd
           of IL.MASSIGN{stm=(ys, rator, xs), ...} => (case (ys, rator)
                 of ([vals, vecs], Op.EigenVecs2x2) =>
                      if (useCount vecs = 0)
                        then let
                          val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals2x2, xs))
                          in
                            ST.tick cntEigenVals;
                            IL.CFG.replaceNode (nd, newNd)
                          end
                        else ()
                  | ([vals, vecs], Op.EigenVecs3x3) =>
                      if (useCount vecs = 0)
                        then let
                          val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals3x3, xs))
                          in
                            ST.tick cntEigenVals;
                            IL.CFG.replaceNode (nd, newNd)
                          end
                        else ()
                  | _ => ()
                (* end case *))
            | _ => ()
          (* end case *))

    fun loopToFixPt f = let
	  fun loop n = let
		val () = f ()
		val n' = Stats.sum{from=firstCounter, to=lastCounter}
		in
		  if (n = n') then () else loop n'
		end
	  in
	    loop (Stats.sum{from=firstCounter, to=lastCounter})
	  end

    fun transform (prog as IL.Program{props, globalInit, initially, strands}) = let
	  fun doCFG cfg = (
		loopToFixPt (fn () => IL.CFG.apply simplify cfg);
		loopToFixPt (fn () => ignore(UnusedElim.reduce cfg)))
	  fun doMethod (IL.Method{body, ...}) = doCFG body
	  fun doStrand (IL.Strand{stateInit, methods, ...}) = (
		doCFG stateInit;
		List.app doMethod methods)
	  fun optPass () = (
		doCFG globalInit;
		List.app doStrand strands)
	  in
	    loopToFixPt optPass;
(* FIXME: after optimization, we should filter out any globals that are now unused *)
	    IL.Program{
		props = props,
		globalInit = globalInit,
		initially = initially,	(* FIXME: we should optimize this code *)
		strands = strands
	      }
	  end

  end

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