Download presentation
Presentation is loading. Please wait.
Published byBeryl Chandler Modified over 9 years ago
1
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.
2
Missed opportunities (need stronger methods) m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Value Numbering EaC, Figure 8.5 Local Value Numbering 1 block at a time Strong local results No cross-block effects *
3
An Aside on Terminology m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Control-flow graph (CFG) Nodes for basic blocks Edges for branches Basis for much of program analysis & transformation This CFG, G = (N,E) N = {A,B,C,D,E,F,G} E = {(A,B),(A,C),(B,G),(C,D), (C,E),(D,F),(E,F),(F,E)} |N| = 7, |E| = 8
4
Superlocal Value Numbering m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F The Concept Apply local method to EBBs Do { A,B }, { A,C,D }, & { A,C,E } Obtain reuse from ancestors Avoid re-analyzing A & C Does not help with F or G * EBB: A set of blocks B 1, B 2, …, B n where B i is the sole predecessor of B i+1, and B 1 does not have a unique predecessor
5
Superlocal Value Numbering Efficiency Use A’s table to initialize 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 To simplify matters Unique name for each definition Makes name VN Use the SSA name space EaC: § 5 & App. B m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Subscripted names from example in last lecture
6
SSA Name Space ( locally ) Example ( from last lecture ) With VNs a 0 3 x 0 1 + y 0 2 b 0 3 x 0 1 + y 0 2 a 1 4 17 c 0 3 x 0 1 + y 0 2 Notation: While complex, the meaning is clear Original Code a 0 x 0 + y 0 b 0 x 0 + y 0 a 1 17 c 0 x 0 + y 0 Renaming: Give each value a unique name Makes it clear Rewritten a 0 3 x 0 1 + y 0 2 b 0 3 a 0 3 a 1 4 17 c 0 3 a 0 3 Result: a 0 3 is available rewriting works
7
SSA Name Space ( in general ) Two principles Each name is defined by exactly one operation Each operand refers to exactly one definition To reconcile these principles with real code Insert -functions at merge points to reconcile name space Add subscripts to variable names for uniqueness x ...... x +... x 0 ... x 1 ... x 2 (x 0,x 1) x 2 +... becomes
8
This is in SSA Form Superlocal Value Numbering m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d G q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F EaC, Figure 8.6 With all the bells & whistles Find more redundancy Pay little additional cost Still does nothing for F & G Superlocal techniques Some local methods extend cleanly to superlocal scopes VN does not back up If C adds to A, it’s a problem
9
What About Larger Scopes? We have not helped with F or G Multiple predecessors Must decide what facts hold in F and in G For G, combine B & F? Merging state is expensive Fall back on what’s known m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d G q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F
10
Dominators Definitions x dominates y if and only if every path from the entry of the control-flow graph to the node for y includes x By definition, x dominates x We associate a Dom set with each node |Dom(x )| ≥ 1 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 )
11
Dominators Dominators have many uses in analysis & transformation Finding loops Building SSA form Making code motion decisions We’ll look at how to compute dominators later A BCG FED Dominator treeDominator sets Back to the discussion of value numbering over larger scopes... * m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d G q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F
12
What About Larger Scopes? We have not helped with F or G Multiple predecessors Must decide what facts hold in F and in G For G, combine B & F? Merging state is expensive Fall back on what’s known Can use table from IDom(x ) to start x Use C for F and A for G Imposes a Dom-based application order Leads to Dominator VN Technique ( DVNT ) * m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d G q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F
13
Dominator Value Numbering The DVNT Algorithm Use superlocal algorithm on extended basic blocks Retain use of scoped hash tables & SSA name space Start each node with table from its IDom DVNT generalizes the superlocal algorithm No values flow along back edges ( i.e., around loops ) Constant folding, algebraic identities as before Larger scope leads to ( potentially ) better results Local + Superlocal + good start for new EBBs
14
Dominator Value Numbering m a + b n a + b A p c + d r c + d B r 2 (r 0,r 1 ) y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E e 3 (e 1,e 2 ) u 2 (u 0,u 1 ) v a + b w c + d x e + f F DVNT advantages Find more redundancy Little additional cost Retains online character DVNT shortcomings Misses some opportunities No loop-carried CSEs or constants
15
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 ) We will look at several global methods *
16
Roadmap To recap … We have seen value numbering Local, superlocal, dominator scopes We may look at global value numbering later We will look at global redundancy elimination today We have used dominators Chapter 9 in EaC shows how to compute dominators Gives world’s fastest algorithm (also simplest) We have used SSA Chapter 9 in EaC shows how to build SSA form & tear it down
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.