CS 201 Compiler Construction

12
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation

description

CS 201 Compiler Construction. Lecture 10 SSA-Based Sparse Conditional Constant Propagation. Conditional Constant Propagation. SSA-based algorithm is faster than the previous algorithm that was presented earlier. - PowerPoint PPT Presentation

Transcript of CS 201 Compiler Construction

Page 1: CS 201 Compiler Construction

1

CS 201Compiler Construction

Lecture 10SSA-Based Sparse Conditional

Constant Propagation

Page 2: CS 201 Compiler Construction

Conditional Constant Propagation

SSA-based algorithm is faster than the previous algorithm that was presented earlier.

Basic Idea: Use SSA-edges for faster propagation of constants from definitions to uses.

2

Page 3: CS 201 Compiler Construction

Contd..

Handling Executable vs Non-Executable Edges

- At ϕ-functions non-executable operands have lattice value of top-undef- - This allows conditional constant propagation.- Process nodes only when they are known to be executable.- Non-executable SSA edges will transmit

3

Page 4: CS 201 Compiler Construction

Contd..

When can we process a SSA edge ?

- When we know the destination of the edge is executable.- Destination statement is executable when at least one of the incoming control flow edge is executable.Justification: In SSA form ecah use receives value from only one definition. Thus, no matter how we get to the use it will get the value from the same SSA-edge.

4

Page 5: CS 201 Compiler Construction

Contd..

Maintain two worklists:FlowWorkList (for control flow edges)SSAWorkList (for SSA edges)

1.Initialize: FlowWorkList Edges leaving the entry node. SSAWorkList {} ExecutableFlag(FlowEdge)False Lattice cells initialized to UNDEF or top2. Halt execution when both worklists are empty. Execution may proceed by processing edges from either worklist.

5

Page 6: CS 201 Compiler Construction

Contd..

Maintain two worklists:FlowWorkList (for control flow edges)SSAWorkList (for SSA edges)

1.Initialize: FlowWorkList Edges leaving the entry node. SSAWorkList {} ExecutableFlag(FlowEdge)False Lattice cells initialized to UNDEF or top2. Halt execution when both worklists are empty. Execution may proceed by processing edges from either worklist.

6

Page 7: CS 201 Compiler Construction

Contd..

3. if e is CFG-edge from FlowWorkList then if ExecutableFlag(e)=false then ExecutableFlag(e) = true Perform Visitϕ for all ϕ-nodes at destination node.

if only one incoming CFG-edges is TRUE then this

this is the first visit to the node then Perform VisitExpression at the nodeif the node contains one outgoing CFGedge then add the edge to FlowWorkList

7

Page 8: CS 201 Compiler Construction

Contd..

4. if e is SSA-edge from SSAWorkList then if destination of e is a ϕ-node then Perform Visitϕ

elseif destination of e is an expression then examine ExecutableFlags for the CFG

edges reaching that node and if any one flag is

true then perform VisitExpression.

8

Page 9: CS 201 Compiler Construction

Contd..

Visitϕ

For each operand in the ϕ-node examine the ExecutableFlag of the corresponding CFG-edge and set the operand’s lattice value as follows:CFG-edge executable lattice value same as lattice value at the definition edge of the SSA-edge.CFG-edge non-executable lattice value is UNDEF or top.

Compute the lattice value of the variable on the left hand side.

9

Page 10: CS 201 Compiler Construction

Contd..

VisitExpressionEvaluate lattice value of the expression using lattice values of operands from places where they were defined.If lattice value changes then

if expression is part of an assignment statement then add SSA-edge starting from left hand side to SSAWorkList

elseif expression is conditional/predicate then based upon the result add appropriate CFG-edges to the FlowWorkList: TRUEtrue edge; FALSEfalse edge; and bottom both edges.

10

Page 11: CS 201 Compiler Construction

Def-Use Edges vs SSA-edges

We cannot take advantage of non-executable edges if def-use edges are used for propagation.

Why? Because def-use chains include def-use edges formed through non-executable edges.

11

executable

executable

executable

Page 12: CS 201 Compiler Construction

Def-Use Edges vs SSA-edges

Algorithm based upon Def-Use chains will be less efficient.SSA-based algorithm: Time spent is proportional to number of SSA-edges. Number of SSA-edges per variable is O(N) where N is the number of statements. Therefore time complexity is O(NxV) where V is the number of variables.Def-Use based algorithm: There can be O(N) definitions and uses of one variable. Thus, number of def-use edges is and hence the time complexity is O(N2xV).

12