220 likes | 384 Views
More Interprocedural Analysis. Chapter 11, Sections 11.2.5 to end. Review. We analyzed several interprocedural problems. MOD -- set of variables modified in procedure ALIAS -- set of aliased variables in a procedure.
E N D
More Interprocedural Analysis Chapter 11, Sections 11.2.5 to end
Review • We analyzed several interprocedural problems. • MOD -- set of variables modified in procedure • ALIAS -- set of aliased variables in a procedure. • The problem of aliasing make interprocedural problems harder than global problems.
Constant Propagation • Propagating constants between procedures can cause significant improvements. • Dependence testing can be made more precise. SUBROUTINE FOO(N) INTEGER N,M CALL INIT(M,N) DO I = 1,P B(M*I + 1) = 2*B(1) ENDDO END SUBROUTINE INIT(M,N) M = N END If N = 0 on entry to FOO, the loop is a reduction. Otherwise, we can vectorize the loop.
Constant Propagation • Definition: Let s = (p,q) be a call site in procedure p, and let x be a parameter of q. Then , the jump function for x at s, gives the value of x in terms of parameters of p. • The support of is the set of p-parameters that is dependent on.
Constant Propagation • Instead of a Def-Use graph, we construct an interprocedural value graph: • Add a node to the graph for each jump function • If x belongs to the support of , where t lies in the procedure q, then add an edge between and for every call site s = (p,q) for some p. • We can now apply the constant propagation algorithm to this graph. • Might want to iterate with global propagation
Example PROGRAM MAIN INTEGER A,B A = 1 B = 2 CALL S(A,B) END SUBROUTINE S(X,Y) INTEGER X,Y,Z,W Z = X + Y W = X - Y CALL T(Z,W) END SUBROUTINE T(U,V) PRINT U,V END 1 2 3 -1 The constant-propagation algorithm will Eventually converge to above values. (might need to iterate with global)
Jump Functions • Need a way of building • For x output of p, define to be the value of x on return from p in terms of p-parameters. • The support of is defined as above. PROGRAM MAIN INTEGER A CALL PROCESS(15,A) PRINT A END SUBROUTINE PROCESS(N,B) INTEGER N,B,I CALL INIT(I,N) CALL SOLVE(B,I) END SUBROUTINE INIT(X,Y) INTEGER X,Y X = 2*Y END SUBROUTINE SOLVE(C,T) INTEGER C,T C = T*10 END
Kill Analysis • The NotKilled problem is easily solved globally: • To extend NotKilled to the procedure level, construct the reduced-control-flow graph for the procedure: • Vertices consist of procedure entry, procedure exit, and call sites. • Every edge (x,y) in the graph is annotated with the set THRU(x,y) of variables not killed on that edge.
Reduced Control Graph ComputeReducedCFG(G) remove back edges from G for each successsor of entry node s, add (entry, s) to worklist while worklist isn’t empty remove a ready element (b,s) from the worklist if s is a call site add (s,t) to worlist for each sucessor t of s otherwise if s isn’t the exit node for each successor t of s if THRU[b,t] undefined then THRU[b,t] {} THRU[b,t] THRU[b,t] (THRU[b,s] THRU[s,t]) end
Kill Analysis ComputeNKILL(p) for each b in reduced graph in reverse top order if b is exit node then NKILL[b] {all variables} else NKILL[b] {} for each successor s of b NKILL[b] NKILL[b] (NKILL[s] THRU[b,s] if b is a call site (p,q) then NKILL[b] NKILL[b] NKILL[q] NKILL[p] NKILL[entry node] end
Kill Analysis • ComputeNKill only works in the absence of formal parameters. • A more complicated algorithm takes parameter binding into account using a binding graph. • This algorithm ignores aliasing for efficiency.
Symbolic Analysis • Prove facts about variables other than constancy: • Find a symbolic expression for a variable in terms of other variables. • Establish a relationship between pairs of variables at some point in program. • Establish a range of values for a variable at a given point.
Symbolic Analysis • Range analysis and symbolic evaluation can be solved using a lattice framework. Range Analysis: • Jump functions and return jump functions return ranges. • Meet operation is now more complicated. • If we can bound number of times upper bound increases and lower bound decreases, the finite-descending-chain property is satisfied. [-∞ 60 [1:100] [50:∞] : ] [-∞:100] [1:∞] [-∞,∞]
Array Section Analysis • Consider the following code: We want to know if this loop carries a dependence. MOD and USE are some use, but not much. DO I = 1,N CALL SOURCE(A,I) CALL SINK(A,I) ENDDO Let be the set of locations in array modified on iteration I and set of locations used on iteration I. Then has a carried true dependence iff
Array Section Analysis • One possible lattice representation are sections of the form: • A(I,L), A(I,*), A(*,L), A(*,*) • The depth of the lattice is now on the order of the number of array subscripts., and the meet operation is efficient. • A better representation is one in which upper and lower bounds for each subscript are allowed.
Call Graph Construction • This problem is complicated by procedure parameters: SUBROUTINE SUB1(X,Y,P) INTEGER X,Y CALL P(X,Y) END What procedure names can be passed into P? To avoid loss of precision, we need to record sets of procedure-name tuples when a subroutine has multiple procedure parameters.
Call Graph Construction Procedure ComputeProcParms ProcParms(p) {} for each procedure p for each call site s = (p,q) passing in procedure names Let t = <N1,N2, …, Nk> be procedure names passed in. worklist worklist {<t,q>} while worklist isn’t empty remove <t= <N1,N2, …, Nk>,p> from worklist ProcParms[p] ProcParms[p] {t} <P1,P2,…,Pk> are parameters bound to <N1,N2, …, Nk> for each call site (p,q) passing in some Pi Let u=<M2,M2,…,Ml> set of procedure names and instances of Pi passed into q if u is not in ProcParms[q] then worklist worklist {<u,q>} end
Inlining • Inlining procedure calls has several advantages: • Eliminates procedure call overhead. • Allows more optimizations to take place • However, a study by Mary Hall showed that overuse of inlining can cause slowdowns: • Breaks compiler procedure assumptions. • Function calls add needed register spills. • Changing function forces global recompilation.
Procedure Cloning • Often specific values of function parameters result in better optimizations. If we knew that IS != 0 at a call, then loop can be vectorized. PROCEDURE UPDATE(A,N,IS) REAL A(N) INTEGER I = 1,N A(I*IS-IS+1)=A(I*IS-IS+1)+PI ENDDO END If we know that IS != 0 at specific call sites, clone a vectorized version of the procedure and use it at those sites.
Hybrid optimizations • Combinations of procedures can have benefit. • One example is loop embedding: DO I = 1,N CALL FOO() ENDDO PROCEDURE FOO() … END CALL FOO() PROCEDURE FOO() DO I = 1,N … ENDDO END
Whole Program • Want to efficiently do interprocedural analysis without constant recompilation. • This is a software engineering problem. • Basic idea: every time the optimizer passes over a component, calculate information telling what other procedures must be rescanned. • Include a feedback loop that optimizes until no procedures are out of date.
Summary • Solution of flow sensitive probems: • Constant propagation • Kill analysis • Solutions to related problems such as symbolic analysis and array section analysis. • Other optimizations and ways to integrate these into whole-program compilation.