constant propagation for loops with factored use-def chains

47
1 Constant Propagation for Loops with Factored Use-Def Chains Reporter Lai, Yen-Chang

Upload: hallam

Post on 04-Jan-2016

38 views

Category:

Documents


1 download

DESCRIPTION

Constant Propagation for Loops with Factored Use-Def Chains. Reporter : Lai, Yen-Chang. Outline. Introduction Background Constant Propagation Implementation and Experiment. Introduction. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Constant Propagation for Loops with Factored Use-Def Chains

1

Constant Propagation for Loops with Factored Use-Def Chains

Reporter: Lai, Yen-Chang

Page 2: Constant Propagation for Loops with Factored Use-Def Chains

2

Outline

Introduction Background Constant Propagation Implementation and Experiment

Page 3: Constant Propagation for Loops with Factored Use-Def Chains

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.

Page 4: Constant Propagation for Loops with Factored Use-Def Chains

4

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.

Page 5: Constant Propagation for Loops with Factored Use-Def Chains

5

Introduction

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.

Page 6: Constant Propagation for Loops with Factored Use-Def Chains

6

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.

Page 7: Constant Propagation for Loops with Factored Use-Def Chains

7

Introduction(cont.)

Page 8: Constant Propagation for Loops with Factored Use-Def Chains

8

Introduction(cont.)

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.

Page 9: Constant Propagation for Loops with Factored Use-Def Chains

9

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.

Page 10: Constant Propagation for Loops with Factored Use-Def Chains

10

Introduction(cont.)

Page 11: Constant Propagation for Loops with Factored Use-Def Chains

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.

Page 12: Constant Propagation for Loops with Factored Use-Def Chains

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.

Page 13: Constant Propagation for Loops with Factored Use-Def Chains

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)

Page 14: Constant Propagation for Loops with Factored Use-Def Chains

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

Page 15: Constant Propagation for Loops with Factored Use-Def Chains

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

Page 16: Constant Propagation for Loops with Factored Use-Def Chains

16

Background(cont.)

Page 17: Constant Propagation for Loops with Factored Use-Def Chains

17

Background(cont.)

Page 18: Constant Propagation for Loops with Factored Use-Def Chains

18

Background(cont.)

Page 19: Constant Propagation for Loops with Factored Use-Def Chains

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

Page 20: Constant Propagation for Loops with Factored Use-Def Chains

20

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.

u

z

v

w

Exit

Page 21: Constant Propagation for Loops with Factored Use-Def Chains

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

Page 22: Constant Propagation for Loops with Factored Use-Def Chains

22

Page 23: Constant Propagation for Loops with Factored Use-Def Chains

23

Page 24: Constant Propagation for Loops with Factored Use-Def Chains

24

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.

Page 25: Constant Propagation for Loops with Factored Use-Def Chains

25

Background(cont.)

APT Data Structure they are join (or merge) points determine their successor will be executed.

Page 26: Constant Propagation for Loops with Factored Use-Def Chains

26

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.

Page 27: Constant Propagation for Loops with Factored Use-Def Chains

27

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.

Page 28: Constant Propagation for Loops with Factored Use-Def Chains

28

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.

Page 29: Constant Propagation for Loops with Factored Use-Def Chains

29

Constant Propagation(Cont.)

Page 30: Constant Propagation for Loops with Factored Use-Def Chains

30

Constant Propagation(Cont.)

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.

Page 31: Constant Propagation for Loops with Factored Use-Def Chains

31

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.

Page 32: Constant Propagation for Loops with Factored Use-Def Chains

32

Page 33: Constant Propagation for Loops with Factored Use-Def Chains

33

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.

Page 34: Constant Propagation for Loops with Factored Use-Def Chains

34

Page 35: Constant Propagation for Loops with Factored Use-Def Chains

35

Constant Propagation(Cont.)

Page 36: Constant Propagation for Loops with Factored Use-Def Chains

36

Constant Propagation(Cont.)

Page 37: Constant Propagation for Loops with Factored Use-Def Chains

37

Constant Propagation(Cont.)

Page 38: Constant Propagation for Loops with Factored Use-Def Chains

38

Constant Propagation(Cont.)

Page 39: Constant Propagation for Loops with Factored Use-Def Chains

39

Page 40: Constant Propagation for Loops with Factored Use-Def Chains

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.

Page 41: Constant Propagation for Loops with Factored Use-Def Chains

41

Implementation and Experiment

Page 42: Constant Propagation for Loops with Factored Use-Def Chains

42

Implementation and Experiment

Page 43: Constant Propagation for Loops with Factored Use-Def Chains

43

Page 44: Constant Propagation for Loops with Factored Use-Def Chains

44

Page 45: Constant Propagation for Loops with Factored Use-Def Chains

45

Page 46: Constant Propagation for Loops with Factored Use-Def Chains

46

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.

Page 47: Constant Propagation for Loops with Factored Use-Def Chains

47

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.