CS293S GCSE Yufei Ding Review So far, we have seen Local Value - - PowerPoint PPT Presentation
CS293S GCSE Yufei Ding Review So far, we have seen Local Value - - PowerPoint PPT Presentation
CS293S GCSE Yufei Ding Review So far, we have seen Local Value Numbering Finds redundancy, constants, & identities in a block Superlocal Value Numbering Extends local value numbering to EBBs Used SSA-like name space to
2
Review
So far, we have seen
Local Value Numbering Finds redundancy, constants, & identities in a block Superlocal Value Numbering Extends local value numbering to EBBs Used SSA-like name space to simplify bookkeeping Dominator Value Numbering Extends scope to “almost” global Uses dominance information to handle join points in CFG
Example
3 This is in SSA Form m0 ¬ a + b n0 ¬ a + b
A
p0 ¬ c + d r0 ¬ c + d
B
r2 ¬ f(r0,r1) y0 ¬ a + b z0 ¬ c + d
G
q0 ¬ a + b r1 ¬ c + d
C
e0 ¬ b + 18 s0 ¬ a + b u0 ¬ e + f
D
e1 ¬ a + 17 t0 ¬ c + d u1 ¬ e + f
E
e3 ¬ f(e0,e1) u2 ¬ f(u0,u1) v0 ¬ a + b w0 ¬ c + d x0 ¬ e + f
F
Examples
4
e = c + d; f = c + d; g = c + d; x = a + b; c = a - b;
Outline of This Class
Global Common Subexpression Elimination (GCSE) The first data-flow problem A global method
5
6
Some Expression Sets
For each block b Let AVAIL(b) be the set of expressions available on entry to b. Let EXPRKILL(b) be the set of expressions killed in b. i.e. one or more operands of the expression are redefined in b. !!!! Must consider all expressions in the whole graph. Let DEEXPR(b) include the downward exposed expressions in b. i.e. expressions defined in b and not subsequently killed in b
a = b + c f = a u = f + e l = b + u w = a - b ... AVAIL: {b+c, b+u} ExprKILL: {f+e, a-b} DEExpr: {b+c}
7
Formula to Compute AVAIL
Now, AVAIL(b) can be defined as:
AVAIL(b) = ÇxÎpred(b) (DEEXPR(x) È (AVAIL(x) Ç EXPRKILL(x) )) preds(b) is the set of b’s predecessors in the control-flow graph. (Note that a predecessor is an immediate parent, not including other ancestors.)
8
Computing Available Expressions
The Big Picture
- 1. Build a control-flow graph
- 2. Gather the initial data: DEEXPR(b) & EXPRKILL(b)
- 3. Propagate information around the graph, evaluating the
equation Works for loops through an iterative algorithm: finding the fixed- point. All data-flow problems are solved, essentially, this way.
9
First step is to compute DEEXPR & EXPRKILL
Computing Available Expressions
assume a block b with operations o1, o2, …, ok VARKILL ¬ Ø // compute DEExPR(b) DEEXPR(b) ¬ Ø for i = ??? (forward or backward) assume oi is “x ¬ y + z” ??? // compute EXPRKILL(b) EXPRKILL(b) ¬ Ø For each expression e // in the whole CFG for each variable v Î e ???
10
First step is to compute DEEXPR & EXPRKILL
Computing Available Expressions
assume a block b with operations o1, o2, …, ok VARKILL ¬ Ø DEEXPR(b) ¬ Ø for i = k to 1 assume oi is “x ¬ y + z” add x to VARKILL if (y Ï VARKILL) and (z Ï VARKILL) then add “y + z” to DEEXPR(b) EXPRKILL(b) ¬ Ø For each expression e for each variable v Î e if v Î VARKILL(b) then EXPRKILL(b) ¬ EXPRKILL(b) È {e}
Many data-flow problems have initial information that costs less to compute
O(k) steps O(N) steps
N is # operations Backward through block
11
Computing Available Expressions
The worklist iterative algorithm
Worklist ¬ { all blocks, bi } while (Worklist ¹ Ø) remove a block b from Worklist recompute AVAIL(b ) as AVAIL(b) = ÇxÎpred(b) (DEEXPR(x) È (AVAIL(x) Ç EXPRKILL(x) )) if ??? then Worklist ¬ ???
12
Computing Available Expressions
The worklist iterative algorithm
Worklist ¬ { all blocks, bi } while (Worklist ¹ Ø) remove a block b from Worklist recompute AVAIL(b ) as AVAIL(b) = ÇxÎpred(b) (DEEXPR(x) È (AVAIL(x) Ç EXPRKILL(x) )) if AVAIL(b ) changed then Worklist ¬ Worklist È successors(b )
- Finds fixed point solution to equation for AVAIL
- That solution is unique
13
Data-flow Analysis
Data-flow analysis is a collection of techniques for compile-time reasoning about run-time flow of values
- Almost always involves building a graph
Problems are trivial on a basic block Global problems Þ control-flow graph (or derivative) Whole program problems Þ call graph (or derivative)
- Usually formulated as a set of simultaneous equations
14
Making Theory Concrete
Computing AVAIL for the example
AVAIL(A) = Ø AVAIL(B) = AVAIL(C) = AVAIL(D) = … AVAIL(G) =
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
15
Making Theory Concrete
Computing AVAIL for the example
AVAIL(A) = Ø AVAIL(B) = {a+b} È (Ø Ç all) = {a+b} AVAIL(C) = {a+b} AVAIL(D) = {a+b,c+d} È ({a+b} Ç all) = {a+b,c+d} AVAIL(E) = {a+b,c+d} AVAIL(F) = [{b+18,a+b,e+f} È ({a+b,c+d} Ç {all - e+f})] Ç [{a+17,c+d,e+f} È ({a+b,c+d} Ç {all - e+f})] = {a+b,c+d,e+f} AVAIL(G) = [ {c+d} È ({a+b} Ç all)] Ç [{a+b,c+d,e+f} È ({a+b,c+d,e+f} Ç all)] = {a+b,c+d}
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
16
Summary: GCSE
AVAIL(b) = ÇxÎpred(b) (DEEXPR(x) È (AVAIL(x) Ç EXPRKILL(x) )) preds(b) is the set of b’s predecessors in the control-flow graph. (Again, a predecessor is an immediate parent, not including other ancestors.)
- 1. Build a control-flow graph
- 2. Gather the initial data: DEEXPR(b) & EXPRKILL(b)
- 3. Propagate information around the graph, evaluating the equation
through the worklist iterative algorithm.
- 1. Analysis step: compute AVAIL sets for every basic block
- 2. Replacement step: replace common expressions with names.
17
Replacement step in GCSE
Limit to textually identical expressions
(like DAG, unlike value numbering) e <- d + c
a <- b + c d <- b
e <- b + c a <- b + c f <- b + c
AVAIL(B) ={b+c}
B2 B1 B
AVAIL(B) ={b+c} Cannot find or remove the redundancy! Should replace b+c with ?
18
GCSE (replacement step)
Compute a static mapping from expression to name After analysis & before transformation " block b, " expression eÎAVAIL(b), assign e a global name by hashing on e During transformation step Evaluation of e Þ insert copy name(e) ¬ e (e is not available and needs to be evaluated) Reference to e Þ replace e with name(e) (e is available and should be replaced)
Example
m=a+b; n=c+d; c = 17; q=c+d; p=c+d; r=c+d; name expression t1 a+b t2 c+d B1 B2 B3 B4 t1 = a+b; m=t1; t2=c+d; n=t2; c = 17; t2=c+d; q=t2; t2=c+d; p=t2; r=t2; B1 B2 B3 B4
AVAIL(B4) ={c+d; a+b}
20
GCSE (replacement step)
The major problem with this approach Inserts extraneous copies At all definitions and uses of any eÎAVAIL(b), " b Not a big issue Those extra copies are dead and easy to remove The useful ones often coalesce away
21
Review
So far, we have seen
Local Value Numbering Finds redundancy, constants, & identities in a block Superlocal Value Numbering Extends local value numbering to EBBs Used SSA-like name space to simplify bookkeeping Dominator Value Numbering Extends scope to “almost” global (no back edges) Uses dominance information to handle join points in CFG Global Common Subexpression Elimination (GCSE) Applying data-flow analysis (AVAIL) to the problem
22
Comparison
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
LVN LVN SVN SVN SVN DVN DVN GCSE DVN GCSE
The VN methods are ordered
- LVN ≤ SVN ≤ DVN
- GCSE is different
- Based on names, not value
- But for this particular
example: DVN ≤ GCSE
- Not always!!!!
23
Name
- perate on
Scope
- n/offline replace
basis of identity LVN SVN DVN GCSE
24
25
Redundancy Elimination Wrap-up
Conclusions
Redundancy elimination has some depth & subtlety Variations on names, algorithms & analysis
DVN is probably the method of choice
Results quite close to the global methods (± 1%) Cost is low