local refs

Matthew Fluet Matthew Fluet <fluet@CS.Cornell.EDU>
Fri, 30 Nov 2001 10:28:02 -0500 (EST)


I checked in the current incarnation of localRef (see the cvs commit log
for more details.)

It is commented out of the SSA simplification list, but people can take a
look at it.

There is currently one bug with the localization of global refs.  It
doesn't suffice that the global ref is only used in one function -- it
needs to be only used in one function that is invoked at most once.
For example, the following "breaks" under the current localRef:

val ps' = _ffi "printf": string * string -> int;
val ps = fn s => ignore(ps'("%s%n\000", s))

local
   val c = ref 0
in
   fun fib n
     = let
          val _ = if !c mod 5 = 0
                    then ps (concat [Int.toString (!c),
				     "th invocation of fib\n"])
                    else ()
          val _ = c := !c + 1
       in
          case n
            of 0 => 1
             | 1 => 1
             | n => (fib (n-1)) + (fib (n-2))
       end
end

val n = fib 5 

val _ = ps (concat ["fib(5) = ", Int.toString n, "\n"])

(Using print ends up putting c into an environement passed to fib, and
nothing eliminates it as a useless component of the environment tuple;
since c is used outside the fib function it is definitely not globalized.)

I'm working on a fix and on speeding up the pass;
Overall, though, I'm afraid that it is going to be overly expensive for
little gain.