Question

I am working on an optimizer for Java bytecode and decided to use SSA. However, most optimizations require all operations to be purely functional, so in order to handle side effects, I decided to add an extra opaque state parameter and return value for every operation which could potentially have side effects. This will prevent optimizing away or reordering operations with side effects. For example, ignoring exception handling, you'd get something like this pseudocode.

function arguments: x1, e1
if x1 != 0
    x2 = add(x1, 3)
    x3, e2 = invoke(foo, x2, e1)
x4 = phi(x1, x3)
e3 = phi(e1, e2)
return x4, e3

Is there a name for what I'm doing? Is it a good approach? I have heard that functional languages have a concept called Monads, which sounds similar but is not the same. Is using monads a better approach? If so, how can I modify this to use monads?

Was it helpful?

Solution

This was too long to fit in a comment, but it's not really meant to be an answer..

Firm calls it "memory edges", there may be more names. I've heard it being called "explicit state passing", but google appears to disagree.

But I disagree with your premises - most SSA optimizations work just fine (at times a little kludgy perhaps) in the presence of side effects. What doesn't work is using a graph representation without making side effects explicit (obviously the order would disappear). But you only need something to make that order explicit - SSA also works as a "list of operations", where the order is simply fixed (except perhaps in an explicit reordering phase), but in that case it can still be easier to make side effects explicit (leads to fewer special cases in optimizations etc).

It's a fine approach. I don't know how it relates to Monads though, I don't understand them and I probably never will.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top