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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/high-il/normalize.sml
ViewVC logotype

View of /branches/charisee/src/compiler/high-il/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2838 - (download) (annotate)
Tue Nov 25 03:40:24 2014 UTC (4 years, 8 months ago) by cchiw
File size: 4982 byte(s)
edit split-ein
(* normalize.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure Normalize : sig

    val transform : HighIL.program -> HighIL.program

  end = struct


    structure IL = HighIL
    structure Op = HighOps
    structure V = IL.Var
    structure Ty = HighILTypes
    structure ST = Stats
    structure NE = NormalizeEin
    structure P=Printer
    structure Order=OrderEin
    structure mk=mkOperators
    structure App=App
    structure S=SummationEin


(********** Counters for statistics **********)
    val cntInsideScale          = ST.newCounter "high-opt:inside-scale"
    val cntInsideOffset         = ST.newCounter "high-opt:inside-offset"
    val cntInsideNeg            = ST.newCounter "high-opt:inside-meg"
    val cntInsideCurl           = ST.newCounter "high-opt:inside-curl"
    val cntInsideDiff           = ST.newCounter "high-opt:inside-diff"
    val cntProbeAdd             = ST.newCounter "high-opt:probe-add"
    val cntProbeSub             = ST.newCounter "high-opt:probe-sub"
    val cntProbeScale           = ST.newCounter "high-opt:probe-scale"
    val cntProbeOffset          = ST.newCounter "high-opt:probe-offset"
    val cntProbeNeg             = ST.newCounter "high-opt:probe-neg"
    val cntProbeCurl            = ST.newCounter "high-opt:probe-curl"
    val cntDiffField            = ST.newCounter "high-opt:diff-field"
    val cntDiffAdd              = ST.newCounter "high-opt:diff-add"
    val cntDiffScale            = ST.newCounter "high-opt:diff-scale"
    val cntDiffOffset           = ST.newCounter "high-opt:diff-offset"
    val cntDiffNeg              = ST.newCounter "high-opt:diff-neg"
    val cntCurlScale            = ST.newCounter "high-opt:curl-scale"
    val cntCurlNeg              = ST.newCounter "high-opt:curl-neg"
    val cntUnused               = ST.newCounter "high-opt:unused"
    val firstCounter            = cntInsideScale
    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)
    fun use x = (incUse x; x)
    fun isEin x = (case V.binding x
        of IL.VB_RHS(IL.EINAPP (e,arg))=>SOME(IL.EINAPP (e,arg))
        | IL.VB_RHS(IL.VAR x') => isEin x'
        |_=>NONE
    (* end case *))


val testing=0
fun testp e=(case testing
    of 1 => (print(String.concat e);1)
    | _ => 1
(*end case*))


fun doNormalize e=let
    val _ = testp ["\n in do Normalize"]
    val ordered=Order.orderfn e
    val (n,change)=NE.normalize ordered
    val e'=S.cleanSummation n
    val _  =testp ["original \n =>\n",P.printerE(e),"\n Ordered:\n",P.printerE(ordered),"\n normalized\n=>",P.printerE(n),"\n Move Sums \n =>", P.printerE(e')]
    in
        e'
    end

fun foundEin(paramCount,place,change,newE,newArgs,done,newEinApp,orig,lhs)= (case (List.nth(paramCount, place),testing)
    of (Ein.TEN(0,_),_)=> (change,orig, place+1,  done@[newEinApp])
    | (_,0)=>(case App.app(orig,place,newE)
        of (0,subst) => (1,subst, place+length(newArgs), done@newArgs)
        | (c,subst) => (incUse lhs;(*decUse e;*)(1,subst, place+length(newArgs), done@newArgs))
        (*end case*))
    | (_,_)=> let
        val (c,subst)=App.app(orig,place,newE)
        val _ = testp["\n Apply at ", Int.toString(place),":--",P.printerE(newE),
                "\n","Subst Result:\t", P.printerE(subst), "\n"]
        in (case c
            of 0 => (1,subst, place+length(newArgs), done@newArgs)
            |_ => (incUse lhs;(*decUse e;*)(1,subst, place+length(newArgs), done@newArgs))
            (*end case*))
        end
    (*end case*))




fun doRHS (lhs, IL.EINAPP (ein, args))=let
    val _ = testp ["\n\n**************************\n\n\n",V.toString  lhs,"=="," ---Current: ", P.printerE(ein), (String.concatWith "," (List.map V.toString args))]
    fun rewrite(0,_,_, [], _)= NONE
        | rewrite(_,orig,_, [], done)= SOME[(lhs,IL.EINAPP((doNormalize orig), done))]
        | rewrite(change,orig, place, e::es,done)=(case (isEin e)
            of NONE => rewrite(change,orig, place+1, es, done@[e])
            | SOME(IL.EINAPP (newE,newA))=> let
                val Ein.EIN{params, index, body}=orig
                val (change',e',place',done')=foundEin(params,place,change,newE,newA,done,e,orig,lhs)
                in rewrite(change',e',place',es,done')
                end
            | _ => raise Fail"isEin did not work"
            (* end case *))
    in rewrite(0,ein, 0,args,[])
    end
| doRHS _ = NONE

    structure Rewrite = RewriteFn (
      struct
        structure IL = IL
        val doAssign = doRHS
        fun doMAssign _ = NONE
        val elimUnusedVars = UnusedElim.reduce
      end)

    val transform = Rewrite.transform

  end

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