Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 214 times |
Download: | 2 times |
A UNIFIED APPROACH TO GLOBAL PROGRAM
OPTIMIZATION
Proseminar „Programmanalyse”, Prof. Dr. Heike WehrheimUniversität Paderborn, WS 2011/2012
2
Different Data flow problems
Reaching definitions• For each use of a variable, find all definitions that reach it.
Live variables• For a point p and a variable v, determine whether v is live at p.
Upward exposed uses
• For each definition of a variable, find all uses that it reaches.
• Find all expressions whose value is available at some point p.
Available expressions
3
Global:Goal = to collect information at the beginning and end of each basic block
Iterative: Construct data flow equations that describe how information flows through each basic block and solve them by iteratively converging on a solution.
Global Iterative Data Flow Analysis
4
Components of data flow equations Sets containing collected information
in set: information coming into the BB from outside (following flow of dats)
gen set: information generated/collected within the BB kill set: information that, due to action within the BB, will
affect what has been collected outside the BB out set: information leaving the BB
Functions (operations on these sets) Transfer functions describe how information changes as it
flows through a basic block Meet functions describe how information from multiple
paths is combined.
Global Iterative Data Flow Analysis
5
Data fl ow analysis
Constantpropagation
Commonsubexpression
elimination
CP and CSE
6
CONSTANT PROPAGATION
begin integer i,a,b,c,d,ea:=1; c:=0; …for i:=1 step 1 until 10 do begin b:=2; … d:=a+b; … e:=b+c; … c:=4; ... endend
entry:a:=1
c:=0
b:=2
d:=a+b
e:=b+c
c:=4
A
B
C
D
E
F
PA = ∅PB = {(a,1)}PC = {(a,1)}PD = {(a,1),(b,2)}PE = {(a,1),(b,2),(d,3)}PF = {(a,1),(b,2),(d,3)}
Problem: Determining the poolof propagated constants
Pool of Constants
7
CONSTANT PROPAGATION
entry:a:=1
A
c:=0B
b:=2C
d:=a+b
D
e:=b+c
E
c:=4F
= {(a,1),(b,2),(c,4),(d,3),(e,2)}
Formally
Determining the Pool of propagated constants at node E
The first approximation for the path from A to E: (A,B,C,D,E)
= {(a,1),(c,0),(b,2),(d,3)}
The second approximationfor the path (A,B,C,D,E,F,C,D,E)
8
final constant pool
DATA FLOW ANALYSIS ALGORITHM
{(a,1),(b,2),(d,3),(e,2),(c,4)}P ={(a,1),(b,2),(d,3),(e,2),(c,4)}
= {(a,1)}
= {(a,1),(c,0)}
= {(a,1),(c,0),(b,2)}
= {(a,1),(c,0),(b,2),(d,3)}
= {(a,1),(c,0),(b,2),(d,3),(e,2)}
no changes anymore
For instance:
Formally:
The constant pool resulting from F entering C:
THE SECOND APPROXIMATION THE THIRD ONE
= {(a,1),(b,2)} = {(a,1),(b,2),(d,3)} = {(a,1),(b,2),(d,3)}
= {(a,1)}………
9
DATA FLOW ANALYSIS ALGORITHM INFORMAL
a. Start with an entry node Pi with a given entry pool• usually only one entry node• Usually entry pool is empty
b. Process Pi and produce optimizing function
c. Intersect the incoming optimization pools(for the first time assume as the first approximation)
d. Considering each successor node, if the amount of optimization information is reduced, then process the successor in the same manner as the initial entry node.
DATA FLOW ANALYSIS ALGORITHM
f(A, ∅) = {(a,1)}f(B, {(a,1)}) = {(a,1),(c,0)}
f(C, f(B, f(A, ∅))) = {(a,1), (c,0), (b,2)}
10
f : N × P(U) P(U)
f : N × P PHomomorphism:
Note f(N, x) < 1 and
N analyzed set of nodes U = V × C ordered pairs in any constant pool
V set of variablesC set of constants
P(U) power set (the set of all subsets of U)
So the function can be applied:
“Optimizing function” f is defined
11
1: [initialize] L ← ℇ
2: [terminate?] if L= ∅ then halt
3: [select node]Let L' ϵ L, L'=(N,Pi) for some N ϵ N and Pi ϵ P, L←L- {L'}
4: [traverse?] Let PN be the current approximate pool for node N (Initially, PN = 1). If PN ≤ Pi then go to step 2
5: [set pool] PN← PN∧ Pi, L←L {N', f(N, PN | N‘ ϵ I(N)
6: [loop] go to step 2:
DATA FLOW ANALYSIS ALGORITHM
12
Commonsubexpression
elimination
… r:=a+b; … r+x …(a+b)+X …
r:=a+b
r+x
(a+b)+x
entry:A
B
C
D
Before A PA = ∅
Before B PB =PA= {a I b I a+b, r }
Before C PC {a I b I a+b, r I x I x+r}
Before D PD {a I b I a+b, r I x I x+r I (a+b)+x}
(a+b)+x has the same evaluation as r+x
{a I b I a+b, r I x I r+x, (a+b)+x}
D e t e c tE l im inate
Problem
redundant expressionspurpose
Optimizing function f(N,P)
13
redundant
New class in P
Consider computation e
d:=e in N
remove expression with
d
1. Create e’’ for each e’ in P2. Place e’’ in class of e’
COMMON SUBEXPRESSION ELIMINATION
1. Consider each partial computation e at node N
2. If the computation in class of P then e is redundant
3. Create new class, add all computation, which have operands equivalent to e
4. If N contains d:=e, remove from class expression with d asa sub expression
14
CONSTANT PROPOGATION & COMMON SUBEXPRESSION ELIMINATIOM
Optimizing function f2
d:=e in N
New class in
P
…
…no
further processin
g
z:=e
z in P
combine
z & e
Add z to e
otherwise
15
QUESTIONS?