Title: Phase ordering problem
1Phase ordering problem
- Optimizations can interact in mutually beneficial
ways, and no order exploits all of these
interactions. - Classic example constant propagation and
unreachable code elimination.
x 11 DoSomething() y x // value of y?
x 11 if (x 11) DoSomething() else
DoSomethingElse() x x 1 y x
// value of y?
true
x 11 DoSomething() y 11
2One known solution Iterate individual analyses
until the results dont change
x 11 do if (x 11)
DoSomething() else
DoSomethingElse() x x 1 while
(...) y x // value of y?
- Compiler is slow.
- In the presence of loops in the source program,
might not yield best possible results.
3Another known solution hand writtensuper-analysi
s
- Examples
- conditional constant propagation Wegman and
Zadeck 91 - class analysis, splitting and inlining Chambers
and Ungar 90 - const prop and pointer analysis Pioli and Hind
99
- Lose modularity
- difficult to write, reuse, and extend such
analyses
4Ideally...
- ... we want to
- Write analyses modularly
- Exploit mutually beneficial interactions
- Have a fast compiler
- We present a framework that achieves this.
5The key to modular composition
- Traditionally, optimizations are defined in two
parts - A dataflow analysis.
- Rules for transforming the program representation
after the analysis is solved. - The key insight is to merge these two parts
- Dataflow functions return either a dataflow value
OR a replacement graph with which to replace the
current statement.
6Flow function returning a dataflow value
y 5
7Flow function returning a dataflow value
...
y 5
PROPAGATE
..., y ? 5
8Flow function returning a replacement graph
y x2
9Flow function returning a replacement graph
Step 1 Initialize input edges with dataflow
information
Replacement graph
x ? 3
x ? 3
y x2
10Flow function returning a replacement graph
Step 2 Perform recursive dataflow analysis on
the replacement graph
Step 1 Initialize input edges with dataflow
information
x ? 3
x ? 3
y x2
PROPAGATE
x ? 3, y ? 5
11Flow function returning a replacement graph
Step 2 Perform recursive dataflow analysis on
the replacement graph
Step 1 Initialize input edges with dataflow
information
x ? 3
x ? 3
y x2
PROPAGATE
x ? 3, y ? 5
x ? 3, y ? 5
Step 3 Propagate dataflow information from
output edges.
12Flow function returning a replacement graph
- Replacement graphs
- used to compute outgoing dataflow information for
the current statement. - a convenient way of specifying what might
otherwise be a complicated flow function.
- Replacement graphs
- used to compute outgoing dataflow information for
the current statement.
x ? 3
y x2
x ? 3, y ? 5
13Flow function returning a replacement graph
- Soundness requirement
- Replacement graph must have the same concrete
semantics as the original statement, but only on
concrete inputs that are consistent with the
current dataflow facts.
x ? 3
y x2
x ? 3, y ? 5
14Flow function returning a replacement graph
Lets assume weve reached a fixed point.
x ? 3
y x2
x ? 3, y ? 5
15Flow function returning a replacement graph
Lets assume weve reached a fixed point.
x ? 3
y x2
y 5
x ? 3, y ? 5
16Flow function returning a replacement graph
Lets assume weve reached a fixed point.
- Replacement graphs
- used to transform the program once a fixed point
has been reached.
x ? 3
y 5
x ? 3, y ? 5
17Iterative analysis example
Now, lets assume we havent reached a fixed
point.
x ? 3
x ? T
y x2
x ? 3, y ? 5
18Iterative analysis example
Now, lets assume we havent reached a fixed
point.
x ? 3
x ? T
y x2
PROPAGATE
x ? 3, y ? 5
x ? T, y ? T
19Branch folding example
if (x 11)
F
T
20Branch folding example
x ? 11
x ? 11
if (x 11)
F
T
21Branch folding example
x ? 11
x ? 11
if (x 11)
x ? 11
x ? 11
F
T
22Branch folding example
x ? 11
if (x 11)
x ? 11
F
T
23Composing several analyses
24x new C
merge
b x instanceof C
if (b)
T
F
x new D
x x.foo()
merge
while()
25x new C
PROPAGATE
PROPAGATE
PROPAGATE
PROPAGATE
x ? T
x ? C
T
T
merge
b x instanceof C
if (b)
T
F
x new D
x x.foo()
merge
while()
26x new C
PROPAGATE
PROPAGATE
PROPAGATE
PROPAGATE
PROPAGATE
x ? T
x ? C
(x ? T, x ? C, T, T)
T
T
merge
b x instanceof C
if (b)
T
F
x new D
x x.foo()
merge
while()
27x new C
(x ? T, x ? C, T, T)
merge
PROPAGATE
(x ? T, x ? C, T, T)
b x instanceof C
if (b)
T
F
x new D
x x.foo()
merge
while()
28x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
PROPAGATE
x ? T, b ? T
if (b)
T
F
x new D
x x.foo()
merge
while()
29x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
(x ? T, x ? C, T, T)
b x instanceof C
x ? T, b ? T
if (b)
T
F
x new D
x x.foo()
merge
while()
30x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, x ? C, T, T)
PROPAGATE
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
31x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, x ? C, T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
32x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
- Replacement graph is analyzed by composed
analysis. - When one analysis chooses a replacement graph,
other analyses see it immediately. - Analyses communicate implicitly through graph
transformations
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
T
F
x new D
x x.foo()
merge
while()
33x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
if (b)
T
F
x new D
x x.foo()
merge
while()
34x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
s
if (b)
T
F
x new D
x x.foo()
merge
while()
35x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
s
s
if (b)
s
T
F
x new D
x x.foo()
merge
while()
36x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
s
if (b)
s
s
T
F
x new D
x x.foo()
merge
while()
37x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
38x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
39x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
40x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x new D
x x.foo()
merge
while()
41x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
s
T
F
x new D
x x.foo()
merge
while()
42x new C
class C extends A A foo() return self
(x ? T, x ? C, T, T)
merge
s
s
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
F
T
x new D
x x.foo()
merge
while()
43x new C
(x ? T, x ? C, T, T)
merge
s
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
F
T
x new D
x x.foo()
s
PROPAGATE
merge
s
while()
44x new C
(x ? T, x ? C, T, T)
merge
s
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
F
T
x new D
x x.foo()
s
merge
s
s
while()
45x new C
(x ? T, x ? C, T, T)
merge
s
(x ? T, x ? C, T, T)
b x instanceof C
s
s
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
F
T
x new D
x x.foo()
merge
while()
46x new C
(x ? T, x ? C, T, T)
merge
s
(x ? T, x ? C, T, T)
b x instanceof C
s
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
s
F
T
x new D
x x.foo()
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
merge
while()
47x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x x.foo()
x new D
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
merge
while()
48x new C
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x x.foo()
x new D
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
merge
PROPAGATE
(x ? T, b ? true, x ? C, b ? Bool,
T , T)
while()
49x new C
(x ? T, x ? C, T, T)
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x x.foo()
x new D
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
merge
(x ? T, b ? true, x ? C, b ? Bool,
T , T)
while()
PROPAGATE
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
50x new C
(x ? T, x ? C, T, T)
(x ? T, x ? C, T, T)
merge
(x ? T, x ? C, T, T)
b x instanceof C
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
if (b)
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
T
F
x x.foo()
x new D
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
merge
(x ? T, b ? true, x ? C, b ? Bool,
T , T)
while()
(x ? T, b ? true, x ? C, b ? Bool,
T, T)
51x new C
merge
b x instanceof C
b true
if (b)
T
F
x x.foo()
x x
x new D
merge
while()
52x new C
merge
b true
x x
merge
while()
53- Analyses are defined modularly and separately.
- Combining them achieves the results of a
monolithic analysis. - If the analyses were run separately in any order
any number of times, no optimizations could be
performed.
54Analysis followed by transformations
55Integrating analysis and transformations
56Composing analyses and transformations
57(No Transcript)
58(No Transcript)