Value Numbering
Value Numbering
REPRESENTATIVE OPTIMIZATION
CSE 521
Penn State University
Spring 2022
Traditional Three-pass Compiler
Errors
Errors
Typical Transformations
• Discover & propagate some constant value
• Move a computation to a less frequently executed place
• Specialize some computation based on context
• Discover a redundant computation & remove it
• Remove useless or unreachable code
• Encode an idiom in some particularly efficient form
The Role of the Optimizer
• The compiler can implement a procedure in many ways
• The optimizer tries to find an implementation that is “better”
→ Speed, code size, data space, …
To accomplish this, it
• Analyzes the code to derive knowledge about run-time behavior
→ Data-flow analysis, pointer disambiguation, …
→ General term is “static analysis”
• Uses that knowledge in an attempt to improve the code
→ Literally hundreds of transformations have been proposed
→ Large amount of overlap between them
Options:
• Use c3 ← b3
• Save a3 in t3
• Rename around it
Local Value Numbering
Example (continued)
Identities: (Click)
Algebraic identities
x←y, x+0, x-0, x∗1, x÷1, x-x,
• Must check (many) special cases x∗0, x÷x, x∨0, x ∧ 0xFF…FF,
max(x,MAXINT), min(x,MININT),
• Replace result with input VN max(x,x), min(y,y), and so on ...
A m ← a + b
n ← a + b Missed opportunities
(need stronger methods)
B p ← c + d C q ← a + b
r ← c + d r ← c + d
D e ← b + 18 E e ← a + 17
s ← a + b t ← c + d
u ← e + f u ← e + f
F v ← a + b
w ← c + d
x ← e + f Local Value Numbering
G y ← a + b
• 1 block at a time
z ← c + d • Strong local results
• No cross-block effects
An Aside on Terminology
D e ← b + 18 E e ← a + 17
s ← a + b t ← c + d
u ← e + f u ← e + f
F v ← a + b
w ← c + d For this CFG, G = (N,E)
x ← e + f
• N = {A,B,C,D,E,F,G}
G y ← a + b • E = {(A,B),(A,C),(B,G),(C,D),
z ← c + d (C,E),(D,F),(E,F),(F,E)}
• |N| = 7, |E| = 8
Superlocal Value Numbering
A m ← a + b
n ← a + b EBB: A set of blocks B1, B2, …, Bn
where Bi is the sole predecessor of
Bi+1, and B1 does not have a unique
B p ← c + d C q ← a + b predecessor
r ← c + d r ← c + d
D e ← b + 18 E e ← a + 17
s ← a + b t ← c + d
u ← e + f u ← e + f
F v ← a + b
The Concept
w ← c + d
x ← e + f • Apply local method to EBBs
G
• Do {A,B}, {A,C,D}, & {A,C,E}
y ← a + b
z ← c + d • Obtain reuse from ancestors
• Avoid re-analyzing A & C
• Does not help with F or G
*
Superlocal Value Numbering
Efficiency
• Use A’s hash table to initialize hash tables for B & C
• To avoid duplication, use a scoped hash table
→ A, AB, A, AC, ACD, AC, ACE, F, G
• Need a VN → name mapping to handle kills
→ Must restore map with scope
→ Adds complication, not cost A m ← a + b
n ← a + b
B C
To simplify matters p ←
r ←
c + d
c + d
q ← a + b
r ← c + d
F v ← a + b
w ← c + d
x ← e + f
G y ← a + b
z ← c + d
SSA Name Space (locally)
Example
x0 ← ... x1 ← ...
x ← ... x ← ...
becomes
x2 ←φ(x0,x1)
... ← x + ...
← x2 + ...
Superlocal Value Numbering
• To improve the results of value numbering, the compiler can
extend its scope from a single basic block to an EBB
• EBB-based version often capitalize on the tree structure of
the EBB
• To make value numbering over EBBs efficient, the compiler
must reuse the results of blocks that occur on multiple
paths through the EBB
Superlocal Value Numbering
D e0 ← b + 18 E e1 ← a + 17
s0 ← a + b t0 ← c + d
u0 ← e + f u1 ← e + f
F e3 ← φ(e0,e1)
u2 ← φ(u0,u1)
v0 ← a + b
This is in
w0 ← c + d
SSA Form
x0 ← e + f
G r2 ← φ(r0,r1)
y0 ← a + b
z0 ← c + d
Dominator-Based Value Numbering (a Regional
Optimization)
Immediate dominators
• For any node x, there must be a y in Dom(x ) closest to x
• We call this y the immediate dominator of x
• As a matter of notation, we write this as IDom(x )
Dominators
Dominators have many uses in analysis & transformation
• Finding loops A m0 ← a + b
n0 ← a + b
D e0 ← b + 18 E e1 ← a + 17
s0 ← a + b t0 ← c + d
u0 ← e + f u1 ← e + f
Dominator sets Dominator tree
F
Bl ock D om I Dom A e3 ← φ(e0,e1)
u2 ← φ(u0,u1)
A A – v0 ← a + b
w0 ← c + d
B A, B A B C G x0 ← e + f
C A, C A
G
D A, C D, C D E F
r2 ← φ(r0,r1)
y0 ← a + b
E A, C E, C z0 ← c + d
F A, C , F C
G A,G A
D E
• Can use table from IDom(x ) to start x
e0 ← b + 18 e1 ← a + 17
s0 ← a + b t0 ← c + d
u0 ← e + f u1 ← e + f
A DVNT advantages
m ← a + b
n ← a + b • Find more redundancy
• Little additional cost
B p ← c + d C q ← a + b
r ← c + d r ← c + d • Retains online character
D e ← b + 18 E e ← a + 17
s ← a + b t ← c + d
u ← e + f u ← e + f
F e3 ← φ(e1,e2)
u2 ← φ(u0,u1)
v ← a + b DVNT shortcomings
w ← c + d
x ← e + f • Misses some opportunities
• No loop-carried CSEs
G r2 ← φ(r0,r1)
y ← a + b
z ← c + d
The Story So Far
• Local algorithm (Balke, 1967)
• Superlocal extension of Balke (many)
• Dominator VN technique (Simpson, 1996)
⇒ All these propagate along forward edges
⇒ None are global methods
Global Methods
• Classic CSE (Cocke 1970)
• Partitioning algorithms (Alpern et al. 1988, Click 1995)
• Partial Redundancy Elimination (Morel & Renvoise 1979)
• SCC/VDCM (Simpson 1996)