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

SCM Repository

[diderot] View of /branches/charisee_dev/src/compiler/high-to-mid/avail-rhs.sml
ViewVC logotype

View of /branches/charisee_dev/src/compiler/high-to-mid/avail-rhs.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3595 - (download) (annotate)
Sat Jan 16 05:48:18 2016 UTC (4 years, 4 months ago) by cchiw
File size: 2927 byte(s)
setting 1
(* avail-rhs.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

(* tracking available MidIL rhs expressions *)
structure AvailRHS : sig

    type rhs = MidIL.rhs

  (* a table for tracking available applications *)
    type t

  (* create a new table *)
    val new : unit -> t

  (* add a MidIL assignment to the table and return the lhs variable.  If the assignment
   * is redundant, then we return the lhs of the previous assignment.
   *)
    val addAssign : t -> MidIL.var * rhs -> MidIL.var

  (* get the assignments from the table *)
    val getAssignments : t -> MidIL.assignment list

  end = struct

    structure IR = MidIL
    structure ST = Stats
    structure E = Ein
    val cntNewAssign	= ST.newCounter "high-to-mid:new-assignment"
    val cntReuseAssign	= ST.newCounter "high-to-mid:reuse-assignment"

    datatype rhs = datatype IR.rhs

    structure Tbl = HashTableFn (
      struct
	type hash_key = rhs
	fun addHashVar (x, h) = IR.Var.hash x + h
	fun hashVal rhs = (case rhs
        of (*IR.GLOBAL x => 0w9941 + IR.GlobalVar.hash x
		| IR.STATE x => 0w7477 + IR.StateVar.hash x
		| IR.VAR x => 0w7919 + IR.Var.hash x
		| IR.LIT lit => 0w6997 + Literal.hash lit
		| IR.OP(rator, args) => List.foldl addHashVar (IR.Op.hash rator) args
		| IR.CONS(args, _) => List.foldl addHashVar 0w5987 args
		| IR.SEQ(args, _) => List.foldl addHashVar 0w6011 args
		| *)IR.EINAPP(ein, args) => List.foldl addHashVar (HashEin.hashfn ein) args
	      (* end case *))
	fun sameKey (rhs1, rhs2) = (case (rhs1, rhs2)
        of (*(IR.GLOBAL x, IR.GLOBAL y) => IR.GlobalVar.same(x, y)
		| (IR.STATE x, IR.STATE y) => IR.StateVar.same(x, y)
		| (IR.VAR x, IR.VAR y) => IR.Var.same(x, y)
		| (IR.LIT a, IR.LIT b) => Literal.same(a, b)
		| (IR.OP(op1, xs), IR.OP(op2, ys)) =>
		    IR.Op.same(op1, op2) andalso ListPair.allEq IR.Var.same (xs, ys)
		| (IR.CONS(xs, _), IR.CONS(ys, _)) => ListPair.allEq IR.Var.same (xs, ys)
		| (IR.SEQ(xs, _), IR.SEQ(ys, _)) => ListPair.allEq IR.Var.same (xs, ys)
		| (IR.EINAPP(ein1, xs), IR.EINAPP(ein2, ys)) =>
		    EqualEin.isEinEqual(ein1, ein2) andalso ListPair.allEq IR.Var.same (xs, ys)
		| *) _ => false
	      (* end case *))
      end)

    datatype t = TBL of {
	assigns : IR.assignment list ref,
	avail : IR.var Tbl.hash_table
     }

    fun new () = TBL{
	    assigns = ref[],
	    avail = Tbl.mkTable (32, Fail "AvailRHS")
	  }

    fun addAssign (TBL{assigns, avail}) = let
	  val find = Tbl.find avail
	  val insert = Tbl.insert avail
	  fun add (lhs, rhs) = (case find rhs
		 of SOME y => (ST.tick cntReuseAssign; y)
		  | NONE => (
		      ST.tick cntNewAssign;
		      insert (rhs, lhs);
		      assigns := IR.ASSGN(lhs, rhs) :: !assigns;
		      lhs)
		(* end case *))
	  in
	    add
	  end

    fun getAssignments (TBL{assigns, ...}) = !assigns

  end

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