Program Analysis and Transformation Survey and Links
Table of Contents:
Glossary
- Critical edge (of a graph) - Edge between a vertex which also has other successors and a vertex which has other predecessors.
(wikipedia)
- DCE - Dead Code Elimination (wikipedia)
- Graph - (wikipedia)
- LOLSSA - [ this entry is a joke! due to lolspeak ]
a) SSA as defined in some early papers on the matter, especially
in the part of out-of-SSA conversion (see epigraph to
SSA Deconstruction section below);
b) a similar version of SSA used in some (oftentimes amateur) projects
decades later.
Names
As a dedication:
We study Program Analysis because it's objective and complex phenomena of
nature devoid of subjectivities of the mankind. But then, we can't separate
it from the work of great human minds who laid the paths in this area, whose
steps we now follow.
These are people who contributed to the Program Analysis field of study
(with all the apology to many more who are not listed here). The emphasis
here is on well-knownness and public availability of their works:
- Gregory Chaitin
- Jeffrey Ullman
- Alfred Aho
- Keith Cooper
- thesis: 1983 "Interprocedural Data Flow Analysis in a Programming Environment"
- Andrew Appel
- thesis: 1985 "Compile-time Evaluation and Code Generation in Semantics-Directed Compilers"
- book 1998: "Modern Compiler Implementation in ML/Java/C"
- 2000: Optimal Register Coalescing Challenge
- Preston Briggs
- thesis: 1992 "Register Allocation via Graph Coloring"
- Clifford Click @cliffclick
- thesis: 1995 "Combining Analyses, Combining Optimizations"
- John Aycock
- thesis: 2001 "Practical Earley Parsing and the SPARK Toolkit"
- Hacked on Python compilation: [1], [2], [3], [4]
- Now hacks on retrogaming: [1], [2]
- Sebastian Hack
- thesis: 2006 "Register Allocation for Programs in SSA Form"
- Matthias Braun @MatzeB
- thesis: 2006 "Heuristisches Auslagern in einem SSA-basierten Registerzuteiler" in German, "Heuristic spilling in an SSA-based register allocator"
- Sebastian Buchwald
- thesis: 2008 "Befehlsauswahl auf expliziten Abhangigkeitsgraphen" in German, "Instruction selection on explicit dependency graphs"
- Florent Bouchez
- thesis: 2009 "A Study of Spilling and Coalescing in Register Allocation as Two Separate Phases"
- Benoit Boissinot
- thesis: 2010 "Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions"
- Quentin Colombet
- thesis 2012: "Decoupled (SSA-based) Register Allocators: from Theory to Practice, Coping with Just-In-Time Compilation and Embedded Processors Constraints"
Intermediate Representation Forms/Types
- Imperative
- Functional
- Static Single-Assignment (SSA) - As argued by Appel, SSA is a functional representation.
- (Lambda-)Functional
- Continuation-passing Style (CPS)
SSA Form
Put simple, in an SSA program, each variable is (statically, syntactically)
assigned only once.
Wikipedia: https://en.wikipedia.org/wiki/Static_single_assignment_form
General reference: "SSA Book" aka "Static Single Assignment Book" aka
"SSA-based Compiler Design" is open, collaborative effort of many SSA
researchers to write a definitive reference for all things SSA.
Classification of SSA types
- Axis 1: Minimality. There're 2 poles: fully minimal vs fully maximal SSA form.
Between those, there's continuum of intermediate cases.
- Fully maximal
- Optimized maximal
- An obvious optimization of avoiding placing phi functions in blocks with
a single predecessor, as they never needed there. While cuts the number
of phi functions, makes renaming algorithm a bit more complex: while for
maximal form renaming could process blocks in arbitrary order (because
each of program's variables has a local definition in every basic block),
optimized maximal form requires processing predecessor first for each
such single-predecessor block.
- Minimal for reducible CFGs
- Some algorithms (e.g. optimized for simplicity) naturally produce minimal
form only for reducible CFGs. Applied to non-reducible CFGs, they may
generate extra Phi functions. There're usually extensions to such
algorithms to generate minimal form even for non-reducible CFGs too (but
such extensions may add noticeable complexity to otherwise "simple"
algorithm). Examplem of such an algorithm ins 2013 Braun et al.
- Fully minimal
- This is usually what's sought for SSA form, where there're no superflous
phi functions, based only on graph properties of the CFG (with consulting
semantics of the underlying program).
- Axis 2: Prunedness. As argued (implied) by 2013 Braun et al., prunedness is
a separate trait from minimality. E.g., their algorithm constructs not fully
minimal, yet pruned form. Between pruned and non-pruned forms, there're
intermediate types again.
- Pruned
- Minimal form can still have dead phi functions, i.e. phi functions which
reference variables which are not actually used in the rest of the program.
Note that such references are problematic, as they artificially extend live
ranges of referenced variables. Likewise, it defines new variables which
aren't really live. The pruned SSA form is devoid of the dead phi functions.
Two obvious way to achieve this: a) perform live variable analysis prior to
SSA construction and use it to avoid placing dead phi functions; b) run
dead code elimination (DCE) pass after the construction (which requires
live variable analysis first, this time on SSA form of the program already).
Due to these additional passes, pruned SSA construction is more expensive
than just the minimal form. Note that if we intend to run DCE pass on the
program anyway, which is often happens, we don't really need to be concerned
to construct pruned form, as we will get it after the DCE pass "for free".
Except of course that minimal and especially maximal form require more
space to store and more time to go thru it during DCE.
- Semi-pruned
- Sometimes called "Briggs-Minimal" form. A compromise between fully
pruned and minimal form. From Wikipedia:
Semi-pruned SSA form[6] is an attempt to reduce the number of Φ
functions without incurring the relatively high cost of computing
live variable information. It is based on the following observation:
if a variable is never live upon entry into a basic block, it never
needs a Φ function. During SSA construction, Φ functions for any
"block-local" variables are omitted.
- Not pruned
- Axis 2: Conventional vs Transformed SSA
- Conventional
- Allows for easy deconstruction algorithm (literally, just drop
SSA variables subscripts and remove Phi functions). Usually,
after construction, SSA is in conventional form (if during
construction, additional optimizations were not performed).
- Transformed
- Some optimizations applied to an SSA program make simple deconstruction
algorithm outlined above not possible (not producing correct
results). This is known as "transformed SSA". There're algorithms
to convert transformed SSA into conventional form.
- Axis 3: Strict vs non-strict SSA
- Non-strict SSA allows some variables to be undefined on some paths
(just like conventional imperative programs).
- Strict form requires each use to be dominated by definition. This
in turn means that every variable must be explicitly initialized.
Non-strict program can be trivially converted into strict form, by
initializing variables with special values, like "undef" for truly
undefined values, "param" for function paramters, etc. Most of SSA
algorithms requires/assume strict SSA form, so non-strict is not
further considered.
Discussion: There's one and true SSA type - the maximal one. It has a
straightforward, easy to understand construction algorithm which does
not depend on any other special algorithms. Running a generic
DCE algorithm on it will remove any redundancies of the maximal form
(oftentimes, together with other dead code). All other types are
optimizations of the maximal form, allowing to generate less Phi
functions, so less are removed later. Optimizations are useful, but
the usual warning about premature optimization applies.
History
According to Aycock/Horspool:
The genesis of SSA form was in the 1960s with the work of Shapiro and
Saint [23,19]. Their conversion algorithm was based upon finding equivalence
classes of variables by walking the control-flow graph.
R. M. Shapiro and H. Saint. The Representation of Algorithms. Rome Air
Development Center TR-69-313, Volume II, September 1969.
Given the possibility of concurrent operation, we might also wish to question the automatic one-one mapping of variable names to equipment locations. Two uses of the same variable name might be entirely unrelated in terms of data dependency and thus potentially concurrent if mapped to different equipment locations.
Continues on the p.31 of the paper (p.39 of the PDF) under the title:
VI. Variable-Names and Data Dependency Relations
Then, following Wikipedia, "SSA was proposed by Barry K. Rosen, Mark N. Wegman, and F. Kenneth Zadeck in 1988."
Barry Rosen; Mark N. Wegman; F. Kenneth Zadeck (1988). "Global value numbers and redundant computations"
Construction Algorithms
Based on excerpts from "Simple Generation of Static Single-Assignment Form",
Aycock/Horspool
For Reducible CFGs (i.e. special case)
1986 R. Cytron, A. Lowry, K. Zadeck. Code Motion of Control Structures in High-Level
Languages. Proceedings of the Thirteenth Annual ACM Symposium on Principles
of Programming Languages, 1986, pp. 70–85.