Presentation on theme: "1 Constant Propagation for Loops with Factored Use-Def Chains Reporter ： Lai, Yen-Chang."— Presentation transcript:
1 Constant Propagation for Loops with Factored Use-Def Chains Reporter ： Lai, Yen-Chang
2 Outline Introduction Background Constant Propagation Implementation and Experiment
3 Introduction Constant propagation analysis is the optimization based on data-flow analysis which is a static (compiler-time) analysis of providing global information about how a procedure manipulates its data. The solution of constant propagation of one program point is related to the other program points. Constant propagation is a code reduction technique that determines whether all assignments to a particular variable is a constant and provides the same constant value at certain particular points.
Introduction A use of the variable at that point can be replaced by the constant. “Immediate” Ex. Result in faster execution than the case that a register must be used to store the constant. 4
Introduction 5 The number of times that the loop runs depends on the variables a and b. Since variables a and b are both constants, the loop iterates constant times.
Introduction A factored use-def (FUD) chain is the SSA (static single assignment) version of the Use-Def chains which readily lend themselves to demand-driven analysis. Since we want to detect constant values at compile time, it is useful to be able to track constants as they are assigned to variables and then used later. 6
8 A loop is a collection of nodes in a flow graph such that: All nodes in the collection are strongly connected; that is, from any node in the loop there is a directed path to any other node. The collection of nodes has a unique entry point, called the ”header”, through which it is the only possible way to reach a node in the loop from a node outside the loop. There must be at least one way to iterate the loop; ie. there is at least one path that goes back to the header from the nodes in the loop.
Introduction(cont.) Optimizations often require to collect some information before the loop. For simplicity of analysis, compilers usually introduce two predecessors to a loop header, namely a preheader and a postbody. A preheader node is the only one predecessor of the header outside the loop. The source node of unique back edge is a postbody node. 9
11 Introduction(cont.) Reaching Definition This determines which definition of a variable may reach each use of the variable in a procedure, and is not killed by another definition. Dead-Code Elimination Unreachable codes or their removal does not affect the operations of a program, and hence can be eliminated freely.
12 Introduction(cont.) Def-Use link and Use-Def link A Def-Use link for a variable is a list of all uses that can be reached by a given definition of a variable, while A Use-Def link is a list of all definitions that can reach a given use.
13 Background Control Flow Graph The statements of a program are grouped into a set of basic blocks. We write to mean there is a path from X to Y with length greater than zero.(X ≠ Y)
14 Background(cont.) Dominate A node X is said to dominate a node Y in a directed graph if every path from Entry to Y includes X, written as X DOM Y. If X dominates Y and X≠Y, then X strictly dominates Y, written as X SDOM Y. Every node n has a unique immediate dominator m such that m≠n, m IDOM n ?? Entry Y X
15 Background(cont.) Postdominate A node w is said to posdominate node v in a directed graph if every path from v to Exit includes w, written as w PDOM v. If w≠v, then w is said to strictly postdominate v, written as w SPDOM v. v w Exit
19 Background(cont.) Control Dependence Control dependence relations are a more general method to capture the information of essential conditions controlling the execution of code in the program. A node w is control dependent on edge u → v if u is a decision-point that determines whether w will be executed or not. That means if control flows from node u to node v along edges u → v, then it will eventually reach node w. u z v w Exit
Background(cont.) A node w is said to be control dependent on edge (u → v) ∈ E w postdominates v, and if w ≠u, then w does not postdominate u. 20 u z v w Exit
21 Background(cont.) Static Single Assignment (SSA) SSA is a representation technique which makes a clear correspondence between the use and the definitions of a variable by giving a unique name. The two properties of SSA form can be achieved by two key phases knownas Ф-placement and renaming. At the join nodes or merge definitions, there must add a special pseudo form of assignment called a φ-function. The form of φ-function is
Background(cont.) In a SSA form, each use is reached by a unique definition, and hence program transformations are simplified. The code motion of a variable use depends primarily on motion of its unique reaching definition. Intuitively, the program has been transformed to the representation of flow of values. 24
Background(cont.) APT Data Structure they are join (or merge) points determine their successor will be executed. 25
Background(cont.) For each use, a variable has a unique reaching definition even in a merge conflicting definitions created by control flow, which is represented by a φ-term. Each use of a variable is marked with a new subscript, while the first one is its label and the others are the definitions that reach this use. 26
Constant Propagation ConstantProp – It is the main procedure that analyzes a procedure starting at the Entry node of the program. VisitPhi – It is performed when a normal φ-function is encountered. The number of times that φ-function is visited is the same as the number of operands within the φ-function. VisitExp – It is called to evaluate the effect of each expression. The two situations that VisitExp is called are: VisitExp is called once when a flow edge of a node is processed first time VisitExp is called once each time an incoming SSA edge is processed. 27
Constant Propagation(cont.) VisitLoop – It is called when a loop-header φ is encountered, then visits all operations in the loop. Check – It does the recursive call to VisitLoop when a operand is never been visited, and hinges on the computation of Lowlink(X ) to determine whether some node are in the same strongly connected components or not. 28
V is the set of CFG nodes in the program. E is the set of CFG edges in the program. SUCC (v) is the set of successors of CFG node v. Count(SUCC(v)) is the number of successors of node v. Target(s) is the use for a Def-Use link s. Source(s) is the definition of φ-term for a Def-Use link s. CFGWorkList is a work list of CFG edges, initialized as a list of the successor(s) of the Entry node. SSAWorkList is a work list of Def-Use links. It is initially empty. 30
Constant Propagation(Cont.) ReachableEdge(e) is a flag that determines whether a CFG edge e has been marked as being reachable, or executable; it is initially false for all edges. ReachableNode(v) for CFG node v is a flag that tells whether the node v is identified as reachable by our algorithm. It is initially false for all node. 31
Constant Propagation(Cont.) N is the global counter for assigning preorder numbers, initialized to zero. LoopStack is a push-down(LIFO) stack of operations, initialized to empty. NPre(P) is the preorder number assigned to each operation as the al- gorithm builds a depth-first spanning forest, initialized as zero for each operation. Lowlink(P) keeps track of whether each operation has a path to a spanning forest predecessor. InStack(P) is a flag to keep track of whether each operation is on theWorkList. The flag is initialized as false. 33
40 Constant Propagation(cont.) The techniques of constant propagation support several purposes in optimizing compilers: Expressions evaluated at compile time need not be evaluated at execution time. Code that is never executed can be deleted. Detection of the paths never taken simplifies the control flow of a program. Since many of the parameters to procedures are constants, using constant propagation with procedure integration can avoid the expansion of code (inline) that often results from native implementations of procedure integration.
Conclusions The approach preselted here is based ol three fundamental results col- cerlilg colstalt propagatiol: Wegmal ald Zadeck’s SCC algorithm for conditional constant propagation Algorithm by Wolfe et al. of finding induction variables The framework of FUD chains. Our implementation of constant propagation concentrates on the iterative solver. 46
Conclusions To find the constant-valued parameters passed through procedures is a more aggressive of constant propagation analysis. A destructive solution is treating aly assiglmelt to al array as al assiglmelt of ⊥ regardless of the real content of the array. 47