View
219
Download
1
Category
Tags:
Preview:
Citation preview
Interprocedural Interprocedural Analysis and Analysis and OptimizationOptimization
Seminar on Optimizations for Modern Architectures
“Optimizing Compilers for Modern Architectures”,
Allen and Kennedy, Chapter 11 - through section 11.2.4
Presented by Gabi Kliot
©Gabi Kliot, Technion, 2006 2
Roadmap Examples of Interprocedural problems Classification of Interprocedural problems Solve two Interprocedural problems
MOD Analysis Alias Analysis
More by Li-Tal: Additional Interprocedural problems
Constant propagation, Kill Analysis, Symbolic analysis, Array section analysis, Call graph construction
Interprocedural Optimization Managing whole program compilation
©Gabi Kliot, Technion, 2006 3
Introduction
Interprocedural Analysis Gathering information about the whole
program instead of a single procedure Interprocedural Optimization
Program transformation that involves more than one procedure in the program (uses interprocedural analysis)
©Gabi Kliot, Technion, 2006 4
Today’s topics
Examples of Interprocedural problems Classification of Interprocedural
problems Solve two Interprocedural problems
MOD Analysis Alias Analysis
©Gabi Kliot, Technion, 2006 5
Some Interprocedural Problems
Modification and Reference Side-effectCOMMON X,Y...DO I = 1, N
S0: CALL PS1: X(I) = X(I) + Y(I)
ENDDO
Can vectorize if P 1. neither modifies nor uses X2. does not modify Y
©Gabi Kliot, Technion, 2006 6
MOD(s)MOD(s): set of variables that may be modified as a side effect of call at s
REF(s)REF(s): set of variables that may be referenced as a side effect of call at s
DO I = 1, N
S0: CALL PS1: X(I) = X(I) + Y(I)
ENDDO
Can vectorize S1 if ( 0) ( 0) ( 0)x REF S x MOD S y MOD S
Modification and Reference Side Effect
©Gabi Kliot, Technion, 2006 7
Alias AnalysisSUBROUTINE S(A,X,N)
COMMON Y /* Y is global variable */DO I = 1, N
S0: X = X + Y*A(I)ENDDO
END
It would be efficient to keep X and Y in different registers and store in X outside the loop
What happens when there is a call, CALL S(A,Y,N)? Then Y is aliased to X on entry to S Can’t delay update to X in the loop any more
ALIAS(ALIAS(pp,x),x): set of variables that may refer to the same location as formal parameter x on entry to p
©Gabi Kliot, Technion, 2006 8
Call Graph Construction
Call Graph G=(N,E) N: one vertex for each procedure E: one edge for each possible call
Edge (p,q) is in E if procedure p calls procedure q Looks easy Construction difficult in presence of procedure
variables Fortan: procedure parameters C: pointers to function C++: virtual functions
©Gabi Kliot, Technion, 2006 9
Call Graph Construction
SUBROUTINE S(X,P)S0: CALL P(X)
RETURNEND
P is a procedure parameter to S What values can P have on entry to S? CALL(s)CALL(s): set of all procedures that may be
invoked at s Resembles closely to the alias analysis problem Call graph construction will be presented later
©Gabi Kliot, Technion, 2006 10
Live and Use AnalysisDO I = 1, N
T = X(I)*CA(I) = T + B(I)C(I) = T + D(I)
ENDDO
This loop can be parallelized by making T a local variable in the loop
PARALLEL DO I = 1, NLOCAL tt = X(I)*CA(I) = t + B(I)C(I) = t + D(I)IF(I.EQ.N) T = t
ENDDO Copy of local version of T to the global
version of T is required to ensure correctness
What if T was not live outside the loop? x is Live at point sx is Live at point s : there is a control-
flow path from s to a use of x that contains no definition of x prior to usage
©Gabi Kliot, Technion, 2006 11
Live and Use Analysis Solve Live analysis using Use analysis USE(s) USE(s) : set of variables having an upward
exposed use in procedure p called at site s upward exposed useupward exposed use : use of a variable
that is not proceeded by a definition of the variable on some path to the use from the point of invocation
x is Live at a call site sx is Live at a call site s iff x in USE(s) or there is a path through p that doesn’t assign a new
value to x and x is live in some successor(s)
©Gabi Kliot, Technion, 2006 12
Kill AnalysisDO I = 1, N
S0: CALL INIT(T,I)T = T + B(I)A(I) = A(I) + T
ENDDO
To parallelize the loop: INIT must not create dependence with respect to
loop It must be possible to recognize that variable T
can be made private to each iteration It means that T is assigned before being used on
every path through INIT
©Gabi Kliot, Technion, 2006 13
Kill Analysis
DO I = 1, N
S0: CALL INIT(T,I)T = T + B(I)A(I) = A(I) + T
ENDDO
T has to be assignedbefore being used on every path through INIT
SUBROUTINE INIT(T,I)
REAL TINTEGER IT = X(I)
END
For this INIT we can seethat T can be made private to each iteration
©Gabi Kliot, Technion, 2006 14
KILL(s)KILL(s) : set of variables assigned on every path through procedure p called at s and through procedures invoked in p
T in the previous example can be privatized under the following condition:
Also we can express LIVE(s) as following:T (KILL(S0)USE(S0))
LIVE(s) USE(s) (KILL(s) LIVE(b))
bsucc(s)
Kill Analysis
©Gabi Kliot, Technion, 2006 15
Constant PropagationSUBROUTINE S(A,B,N,IS,I1)
REAL A(*), B(*)DO I = 0, N-1
S0: A(IS*I+I1) = A(IS*I+I1) + B(I+1)ENDDO
END If IS==0 the loop around S0 always accesses A(I1)
(output dependence) If IS!=0 the loop can be vectorized CONST(p)CONST(p) : set of variables with known
constant values on every invocation of procedure p
Knowledge of CONST(p) is useful for interprocedural constant propagation
©Gabi Kliot, Technion, 2006 16
Today’s topics
Examples of Interprocedural problems Classification of Interprocedural
problems Solve two Interprocedural problems
MOD Analysis Alias Analysis
©Gabi Kliot, Technion, 2006 17
Interprocedural Problem Classification
May and Must problems MOD, REF and USE are ‘May’ problems KILL is a ‘Must’ problem NOT May is Must, Not Must is May The solution of the inverse problem is usually a
universe complement Flow sensitive and flow insensitive problems
Flow sensitiveFlow sensitive: control flow info important Flow insensitiveFlow insensitive: control flow info unimportant
(in compilation time we assume all code is reachable)
©Gabi Kliot, Technion, 2006 18
A
BBA
R1 R2
MOD(R1) MOD(A) MOD(B)
MOD(R2) MOD(A) MOD(B)
REF(R1) REF(A) REF(B)
REF(R2) REF(A) REF(B)
KILL(R1) KILL(A) KILL(B)
KILL(R2)KILL(A) KILL(B)
USE(R1)USE(A) (KILL(A)USE(B))
USE(R2)USE(A)USE(B)
Flow Sensitive vs Flow Insensitive
©Gabi Kliot, Technion, 2006 19
Classification (continued)
A problem is flow insensitiveA problem is flow insensitive iff solution of both sequential and alternately composed regions is determined by taking union of subregions
Approximation of flow-sensitive problems with flow-insensitive analysis:
APUSE
Possible approximation: Flow-insensitive problems are easier
( ) ( )APUSE p REF p( ) ( ) ( ) ( )USE p APUSE p APUSE p USE p
©Gabi Kliot, Technion, 2006 20
Classification (continued)
Side-effect vs Propagation problems Side-effectSide-effect – what may or must happen as a side
effect of a this procedure call (backward effects) PropagationPropagation – what conditions may or must hold
upon entry to the current procedure (forward context)
problem type side effect propagation
flow-insensitive
MOD, REF ALIAS, CALLSCONST
flow-sensitive KILL, USE
©Gabi Kliot, Technion, 2006 21
Three Graphs Call Graph G=(N,E)
N: one vertex for each procedure E: one edge for each possible call
Edge (p,q) is in E if procedure p calls procedure q
Binding graph GB=(NB,EB) One vertex for each formal parameter of each
procedure Directed edge from formal parameter, f1 of p to formal
parameter, f2 of q if there exists a call site s=(p,q) in p such that f1 is bound to f2
|NB| <= |E|, |EB| <= |E|, while G=(N,E) is a call graph
©Gabi Kliot, Technion, 2006 22
Three Graphs – cont.
Control-Flow graph G=(N,E) N: the set of all instructions (or basic blocks) E: the set of control flow edges
Edge (p,q) is in E if instruction p may be followed by instruction q
©Gabi Kliot, Technion, 2006 23
Today’s topics Examples of Interprocedural problems Classification of Interprocedural
problems Solve two Interprocedural problems
MOD Analysis Alias Analysis
©Gabi Kliot, Technion, 2006 24
Assumptions No procedure nesting and no scope nesting
only global and local variables All parameters passed by reference No pointers or reference variables
Aliases are introduced only at call sites Size of the parameter list bounded by a constant
We will formulate and solve the MOD(s) problem
Flow InsensitiveSide-effect Analysis
©Gabi Kliot, Technion, 2006 25
Solving MOD DMOD(s)DMOD(s) : set of variables which are directly
modified as side-effect of call at s
GMOD(p)GMOD(p) : set of global variables and formal parameters of p that are modified, either directly or indirectly as a result of invocation of p
MOD(s) DMOD(s) ALIAS( p,x)
xDMOD(s)
DMOD(s) {v | s p,v s w,w GMOD(p)}
©Gabi Kliot, Technion, 2006 26
Example: DMOD and GMOD
S0: CALL P(A,B,C)
…SUBROUTINE P(X,Y,Z)
INTEGER X,Y,ZX = X*ZY = Y*Z
END
GMOD(P)={X,Y} DMOD(S0)={A,B}
©Gabi Kliot, Technion, 2006 27
Solving GMOD – method A
GMOD(p) contains two types of variables Variables explicitly modified in body of p
This constitutes the set IMOD(p)IMOD(p) Variables modified as a side-effect of some
procedure q invoked in p Includes formal parameters Includes global variables, which are viewed as
parameters to a called procedure Does not include local variables of q
GMOD(p) IMOD( p) {z | z s
s(p ,q) w,w GMOD(q)}
©Gabi Kliot, Technion, 2006 28
Solving GMOD – method A
The previous iterative method may take a long time to converge No limit on the number of times analysis must
traverse a loop in the problem graph Problem with recursive calls
©Gabi Kliot, Technion, 2006 29
Solving GMOD – method A
Recursive callsSUBROUTINE P(F0,F1,F2,…,Fn)INTEGER X,F0,F1,F2,..,Fn…IF(Fk==<some>) RETURN
S0: F0 = <some expr>…S1: CALL P(F1,F2,…,Fn,X)…END
IMOD
GMOD
P0 F0 F0+F1+.. Fk
P1 F0 F0 +.. Fk-1
P2 F0 F0 +.. Fk-2
...
Pk-2 F0 F0+F1+F2
Pk-1 F0 F0+F1
Pk F0 F0
©Gabi Kliot, Technion, 2006 30
Decompose GMOD(p) differently to get an efficient solution
Key: Treat side-effects to global Treat side-effects to global variables and reference formal variables and reference formal parameters separatelyparameters separately
IMOD(p)IMOD(p)+ + : IMOD(p) + all actual parameters modified in the methods invoked inside p
Solving GMOD – method B
( , )
( ) ( ) _
( ) ( ) ( )s p q
GMOD p IMOD p Global Variables
GMOD p IMOD p GMOD q LOCAL
©Gabi Kliot, Technion, 2006 31
IMOD(p)IMOD(p)+ + : Formally defined
RMOD(p)RMOD(p) : set of formal parameters in p that may be modified in p, either directly or by assignment to a reference formal parameter of q as a side effect of a call of q in p
IMOD( p) IMOD( p) {z | z s
s( p,q) w,wRMOD(q)}
Solving for IMOD+ by RMOD
©Gabi Kliot, Technion, 2006 32
Some Order
IMOD(p) : globals + formals + locals explicitly modified in a body of p
RMOD(p) : only formals modified directly in p or as parameters to q (p calls q)
IMOD(p)+ : everything directly in p + parameters to q
GMOD(p) :IMOD( p) IMOD( p) {z | z s
s( p,q) w,wRMOD(q)}
( ) ( ) _GMOD p IMOD p Global Variables
©Gabi Kliot, Technion, 2006 33
Some Order - example
CALL p(X, Y, Z)COMMON G1, LOCAL t, nX = 3G1 = 4t = 5 CALL q(Y, n)
SUBROUTINE q(A, B)COMMON G2, LOCAL mA = 16B = 17G2 = 18m = 19
IMOD RMOD IMOD+ GMOD
qA,B,G2,
mA, B
A,B,G2,m
A,B,G2,m
p X, G1, t X, YX, G1, t,
Y, nX, G1, t, Y, n, G2
IMOD(p) : globals + formals + locals explicitly modified in a body of p
RMOD(p) : only formals modified directly in p or as parameters to q (p calls q)
IMOD(p)+ =
GMOD(p) = ( ) _IMOD p Global Variables ( , )
( ) { | , ( )}s
s p q
IMOD p z z w w RMOD q
©Gabi Kliot, Technion, 2006 34
Solving for RMOD - 1
RMOD(p) construction needs binding graph GB=(NB,EB) One vertex for each formal parameter of each
procedure Directed edge from formal parameter, f1 of p to
formal parameter, f2 of q if there exists a call site s=(p,q) in p such that f1 is bound to f2
|NB| = |E|, |EB| = |E|, while G=(N,E) is a call graph
Use a marking algorithm to solve RMOD
©Gabi Kliot, Technion, 2006 35
Solving for RMOD - 2 Construct the binding graph GB=(NB,EB) with
forward and backward edges Calculate IMOD(p) for each procedure Mark all nodes that are in their IMOD and add
them to the worklist Run DFS/BFS from marked nodes on backward
edges From marked v visit every u, s.t. (u,v) є EB (from formal v back
to its actual u; u is formal of the inclosing method) Mark every reached, yet unmarked node u Add u to the list
Add every marked node to its RMOD(p)
©Gabi Kliot, Technion, 2006 36
Solving for RMOD - 3
SUBROUTINE p(A,B)INTEGER P,Q,II = 2CALL q(A,B,I)CALL q(B,A,I)
END
SUBROUTINE q(X,Y,Z)INTEGER X,Y,ZX = Z + 1Y = Z + 2
END
X Y Z
A B
IMOD(p)={I} IMOD(q)={X,Y}
(0) (0) (0)
(0)(0)
©Gabi Kliot, Technion, 2006 37
(0)
Solving for RMOD - 4
X Y Z
A B
IMOD(p)={I} IMOD(q)={X,Y} Worklist={X,Y}
(1) (1) (0)
(0)
SUBROUTINE p(A,B)INTEGER P,Q,II = 2 CALL q(A,B,I) CALL q(B,A,I)
END
SUBROUTINE q(X,Y,Z)INTEGER X,Y,ZX = Z + 1Y = Z + 2
END
©Gabi Kliot, Technion, 2006 38
X Y Z
A B
RMOD(p)={A,B} RMOD(q)={X,Y} Complexity:
Solving for RMOD - 5
( ) ( ) ( )B BO N E O N E O N E
(1)
(1) (1) (0)
(1)
SUBROUTINE p(A,B)INTEGER P,Q,II = 2 CALL q(A,B,I) CALL q(B,A,I)
END
SUBROUTINE q(X,Y,Z)INTEGER X,Y,ZX = Z + 1Y = Z + 2
END
©Gabi Kliot, Technion, 2006 39
Solving for IMOD+
After gathering RMOD(p) for all procedures, update RMOD(p) to IMOD+(p) using this equation
For each call site in p and each actual parameter Z to q , add Z to IMOD+(p) if Z->W and W є RMOD(q)
This can be done in O(NV+E) time Conversion from RMOD(p) to IMOD+(p): O(N+E) Initialization of IMOD+(p): O(NV)
N – vertices in call graph (number of procedures) V – all global variables
IMOD( p) IMOD( p) {z | z s
s( p,q) w,wRMOD(q)}
©Gabi Kliot, Technion, 2006 40
Solving for GMOD - 1 After gathering IMOD+(p) for all procedures,
calculate GMOD(p) according to the following equation:
Global x is in GMOD(p) if there is a nonempty path in the control flow graph from p to q, where x є IMOD+(q)
This can be solved using a DFS algorithm based on Tarjan’s SCR algorithm on the Call Graph
GMOD(p) IMOD( p)
s( p,q) GMOD(q) LOCAL
©Gabi Kliot, Technion, 2006 41
Solving for GMOD – 2
If we wanted to find global part of GMOD(p) only for procedure p – just run DFS from p on the call graph and collect all global parts of GMODs from the children
( ( )) ( )G GMOD p GMOD q LOCAL
But we want to calculate global part of GMOD(p) for EVERY procedure p
G(GMOD(p)) is the global part of GMOD(p)
©Gabi Kliot, Technion, 2006 42
All procedures in the same strongly connected component of the Call Graph have the same set of global modified variables
Recall we are solving flow-insensitive problem Init: GMOD(p) = IMOD+(p) Run DFS and update G(GMOD(p)) at every node
from its children G(GMOD(q))s When the head p of SCC is reached back, update
all nodes in this component with G(GMOD(p)) O((N+E)V) complexity
Solving for GMOD - 3 - FindGMOD
©Gabi Kliot, Technion, 2006 43
©Gabi Kliot, Technion, 2006 44
q
r
s
dfn=low= GGMOD = {P}
dfn=low= GGMOD = {Q}
dfn=low= GGMOD = {R}
dfn=low= GGMOD = {S}
findGMOD runs on call graph method i modifies global I eventually low will be the
same for all nodes in SCC
Stack = {}p
FindGMOD – the call graph
©Gabi Kliot, Technion, 2006 45
q
r
s
dfn=1low= 1 GGMOD = {P}
dfn=2low= 2 GGMOD = {Q}
dfn= 3low= 3 GGMOD = {R}
dfn=4low= 4 GGMOD = {S}
findGMOD run on call graph Method i modifies global I
Stack = {s,r,q,p}pp
q
r
dfn= 3low= 1 GGMOD = {R}
dfn=2low= 1 GGMOD = {Q, R}
ss
FindGMOD – forward steps
©Gabi Kliot, Technion, 2006 46
dfn=2low= 1 GGMOD = {Q, R, S}q
r
s
dfn=1low= 1 GGMOD = {P}
dfn=4low= 4 GGMOD = {S}
findGMOD run on call graph Method i modifies global I
Stack = {r,q,p}pp
q
r dfn= 3low= 1 GGMOD = {R}
dfn=2low= 1 GGMOD = {Q, R}
ss
dfn=1low= 1 GGMOD = {P, Q, R, S}
FindGMOD – updating from sons
©Gabi Kliot, Technion, 2006 47
q
r
sdfn=4low= 4 GGMOD = {S}
findGMOD run on call graph Method i modifies global I
Stack = {r,q,p}pp
q
r dfn= 3low= 1 GGMOD = {R}
ss
dfn=2low= 1 GGMOD = {Q, R, S}
dfn=1low= 1 GGMOD = {P, Q, R, S}
FindGMOD – updating SC components
q
dfn=2low= 1 GGMOD = {Q, R, S, P}
dfn= 3low= 1 GGMOD = {R, P, Q, S}
r
p
©Gabi Kliot, Technion, 2006 48
Solving for DMOD
After gathering GMOD(p) for all procedures, update DMOD(p) by using this equation
The entire computation of DMOD(p) takes O((N+E)V) steps
Still have to update DMOD to MOD with help of Alias analysis
DMOD(s) {v | s p,v s w,w GMOD(p)}
©Gabi Kliot, Technion, 2006 49
Today’s topics
Examples of Interprocedural problems Classification of Interprocedural
problems Solve two Interprocedural problems
MOD Analysis Alias Analysis
©Gabi Kliot, Technion, 2006 50
Recall definition of MOD(s)
Task is to Compute ALIAS(p,x) Update DMOD to MOD using ALIAS(p,x)
( )
( ) ( ) ( , )x DMOD s
MOD s DMOD s ALIAS p x
Alias Analysis
©Gabi Kliot, Technion, 2006 51
GMOD(Q)={Z}
MOD(S1)={X,Y}
DMOD(S1)={X}
Update DMOD to MOD - example
SUBROUTINE SINTEGER A
S0: CALL S(A,A)END
SUBROUTINE P(X,Y)INTEGER X,Y
S1: CALL Q(X)END
SUBROUTINE Q(Z)INTEGER ZZ = 0
END
ALIAS requires forward analysis MOD requires backward analysis
©Gabi Kliot, Technion, 2006 52
procedure findMod(N,E,n,IMOD+,LOCAL)for each call site s do begin
MOD[s]:=DMOD[s]; for each x in DMOD[s] do
for each v in ALIAS[p,x] doMOD[s]:=MOD[s] {v};
endend findMod O(EV2) algorithm; need to do better
Naïve Update of DMOD to MOD
©Gabi Kliot, Technion, 2006 53
S0: CALL P(G,B)
SUBROUTINE P(X,Y)COMMON G
S1: G = 1END
( )
( , )
G DMOD p
X ALIAS p G
S2: CALL P(G,B)
SUBROUTINE P(X,Y)COMMON G
S3: X = 1END
( )
( , )
X DMOD p
G ALIAS p X
Aliases examples
S4: CALL P(A,A)
SUBROUTINE P(X,Y)COMMON G
S5: X = 1END
( )
( , )
X DMOD p
Y ALIAS p X
Global is aliased to formal
Formal is aliased to global
Formal is aliased to formal
Actually these two are the same
©Gabi Kliot, Technion, 2006 54
Key Observations Two global variables can never be aliases of each
other. Global variables can only be aliased to a formal parameter
No reference variables in Fortran Only passing parameters by reference
ALIAS(p, G) contains less than entries if G is global ALIAS(p, x) for formal parameter x can contain any
global variable or other formal parameters (O(V))
Update of DMOD to MOD - better analysis
©Gabi Kliot, Technion, 2006 55
Break down the update analysis into two parts If x is global we need to add less than elements
but need to do it O(V) times If x is a formal parameter we need to add O(V)
elements but need to do it only O( ) times This gives O( V)=O(V) update time per call,
implying O(VE) for all calls
Update of DMOD to MOD - better analysis
©Gabi Kliot, Technion, 2006 56
Computing Aliases
Compute A(f): the set of global variables a formal parameter f can be aliased to Use binding graph [with cycles reduced to single
nodes]
DFS from the place where ALIAS is introduced (call p(G)) on the binding graph edges in topological order and update ALIASes to formals
O((N+E)V) A(G): the set of formal variables a global G can be
aliased to. It is the same set, just interpreted reversely
g f0 f1 ... fn f
©Gabi Kliot, Technion, 2006 57
Computing Aliases
Expand A(f) to Alias(p,f) for formal parameters Find alias introduction sites of formal to formal
e.g. CALL P(X,X) Propagate aliases
DFS is your best friend - again…
1 2 3 4 5 6, , ... ,f f f f f f f
©Gabi Kliot, Technion, 2006 58
Summary
Some Interprocedural problems and their application in parallelization and vectorization
Classified Interprocedural problems Solved the modification side-effect problem
and the alias propagation problem Flow insensitive side-effect problems are
solvable in the O((N+E)V) time
©Gabi Kliot, Technion, 2006 59
BACKUP
©Gabi Kliot, Technion, 2006 60
DFS numbering dfsnum(v) is the number of vertices visited before v in the DFS
If there is a back or cross edge out of the subtree of v, it is a node visited before v and therefore with a smaller dfsnum
low value low(v) is the smallest dfsnum of a vertex reachable by a back or cross edge from the subtree of v
Tarjan SCC algorithm
©Gabi Kliot, Technion, 2006 61
visit(p)
add p to L
low(p) = dfsnum(p) = N , N++
for each edge p->q
if q is not already in T // tree edge
add p->q to T
visit(q)
low(p) = min(low(p), low(q))
else // back or cross edge
low(p) = min(low(p), dfsnum(q))
if low(p)=dfsnum(p) // component
repeat
remove last element v from L
until v=p
Tarjan SCC algorithm
©Gabi Kliot, Technion, 2006 62
Solving for GMOD
rq
p
s
rq
p
s 1
2 3
4
Initialize GMOD(p) to IMOD+(p) on discovery
Update GMOD(p) computation while backing up
©Gabi Kliot, Technion, 2006 63
Solving for GMOD
rq
p
s
rq
p
s 1
3 2
4
Initialize GMOD(p) to IMOD+(p) on discovery
Update GMOD(p) computation while backing up
For each node u in a SCC update GMOD(u) in a cycle
O((N+E)V) Algorithm
Recommended