compiler-constructionssaanf

Calculating administrative normal form


Administrative Normal Form is an intermediate representation of code, suitable for use by compilers, that is logically equivalent to Single Static Assignment but has some advantages. For example, checking whether a program is a valid SSA form is an existential question about the set of possible paths through a graph. However, checking whether a program is a valid ANF expression is just a question of local syntax.

It is quite easy to generate ANF from strictly functional code, but I'm interested in producing it from imperative code that contains variable updates, loops, etc.

There are straightforward algorithms for converting SSA to ANF. However, generating SSA in the first place becomes nontrivial if you want to do it quickly. It seems intuitive that if what you want to end up with is the more straightforward, more transparent format, it should be more efficient to generate it directly rather than going via the more opaque form.

Is there a published algorithm for generating ANF directly from imperative code?


Solution

  • I know this question is quite old, but...

    The way you specify is precisely what we do in the Inferring Types and Effects via Static Single Assignment paper (disclaimer: I'm one of the authors). We first used Cytron's algorithm for turning an imperative-like surface language (with control flow, local assignment, etc) into a SSA intermediate representation, and then proceeded to translate from SSA form into ANF: this last step is quite trivial! You could follow the paper and take a similar approach, but it seems this is not what you want. Indeed, we've taken this road mostly because it was easier to analyse individual steps and compare representations. There's still a shortcut, though: you may implement Braun's algorithm instead; for reference, we can see here that libfirm, OpenJDK and Go implement it.

    Braun's algorithm does not rely on the control-flow graph, walking the program linearly (so it's fast). While it was designed to generate code in SSA form, the paper itself states that minor modifications to it allow you to generate CPS code instead. Similar modifications actually would allow you to generate ANF directly from the source syntax, without the need to convert to SSA first. Note the algorithm also generates minimal SSA for programs with reducible control-flow graph: so, unless you allow for goto, the generated code is optimized for the number of φ-nodes (or the number of parameters for local functions, in the case of ANF). I have implemented this in another project (Braun's algorithm producing ANF directly) and verified that it works, but I'm afraid I don't have access to that code anymore.

    In short: although there's no properly defined algorithm for that in the literature (as far I know!), Braun's algorithm is capable of quickly generating SSA, CPS or ANF directly.

    P.s.: the name "ANF" stands for "A-normal Form", where "A" is a set of axioms. While these axioms are derived from administrative reductions in CPS, the "A" does not stand for "administrative" on its own.