• No results found

Sparse Conditional Constant Propagation

N/A
N/A
Protected

Academic year: 2022

Share "Sparse Conditional Constant Propagation"

Copied!
72
0
0

Loading.... (view fulltext now)

Full text

(1)

CS618: Program Analysis 2016-17 I

st

Semester

Sparse Conditional Constant Propagation

Amey Karkare

karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in

Department of CSE, IIT Kanpur/Bombay

(2)

Sparse Simple Constant Propagation (SSC)

Improved analysis time over Simple Constant Propagation Finds all simple constant

Same class as Simple Constant Propagation

(3)

Sparse Simple Constant Propagation (SSC)

Improved analysis time over Simple Constant Propagation Finds all simple constant

Same class as Simple Constant Propagation

(4)

Sparse Simple Constant Propagation (SSC)

Improved analysis time over Simple Constant Propagation Finds all simple constant

Same class as Simple Constant Propagation

(5)

Motivating Example

Dashed edges denote SSA def-use chains

ENTRY a = 2 b = 3 a < b c1 = 4 c2 = 5

true false

(6)

Preparations for SSC Analysis

Convert the program to SSA form One statement per basic block Add connections calledSSA edges

Connect (unique) definition point of a variable to its use points

Same asdef-usechains

(7)

Preparations for SSC Analysis

Convert the program to SSA form One statement per basic block Add connections calledSSA edges

Connect (unique) definition point of a variable to its use points

Same asdef-usechains

(8)

Preparations for SSC Analysis

Convert the program to SSA form One statement per basic block Add connections calledSSA edges

Connect (unique) definition point of a variable to its use points

Same asdef-usechains

(9)

Preparations for SSC Analysis

Convert the program to SSA form One statement per basic block Add connections calledSSA edges

Connect (unique) definition point of a variable to its use points

Same asdef-usechains

(10)

Preparations for SSC Analysis

Convert the program to SSA form One statement per basic block Add connections calledSSA edges

Connect (unique) definition point of a variable to its use points

Same asdef-usechains

(11)

SSC Algorithm: Initialization

Evaluate expressions involving constants only and assign the value (c) to variable on LHS

If expression can not be evaluated at compile time, assign

Else (for expression contains variables) assign⊤

Initialize worklistWLwith SSA edges whose def is not⊤ Algorithm terminates whenWLis empty

(12)

SSC Algorithm: Initialization

Evaluate expressions involving constants only and assign the value (c) to variable on LHS

If expression can not be evaluated at compile time, assign

Else (for expression contains variables) assign⊤

Initialize worklistWLwith SSA edges whose def is not⊤ Algorithm terminates whenWLis empty

(13)

SSC Algorithm: Initialization

Evaluate expressions involving constants only and assign the value (c) to variable on LHS

If expression can not be evaluated at compile time, assign

Else (for expression contains variables) assign⊤

Initialize worklistWLwith SSA edges whose def is not⊤ Algorithm terminates whenWLis empty

(14)

SSC Algorithm: Initialization

Evaluate expressions involving constants only and assign the value (c) to variable on LHS

If expression can not be evaluated at compile time, assign

Else (for expression contains variables) assign⊤

Initialize worklistWLwith SSA edges whose def is not⊤ Algorithm terminates whenWLis empty

(15)

SSC Algorithm: Initialization

Evaluate expressions involving constants only and assign the value (c) to variable on LHS

If expression can not be evaluated at compile time, assign

Else (for expression contains variables) assign⊤

Initialize worklistWLwith SSA edges whose def is not⊤ Algorithm terminates whenWLis empty

(16)

SSC Algorithm: Iterative Actions

Take an SSA edgeE out ofWL

Take meet of the value at def end and the use end ofE for the variable defined at def end

If the meet value is different from use value, replace the use by the meet

Recompute the defd at the use end ofE

If the recomputed value islowerthan the stored value, add all SSA edges originating atd

(17)

SSC Algorithm: Iterative Actions

Take an SSA edgeE out ofWL

Take meet of the value at def end and the use end ofE for the variable defined at def end

If the meet value is different from use value, replace the use by the meet

Recompute the defd at the use end ofE

If the recomputed value islowerthan the stored value, add all SSA edges originating atd

(18)

SSC Algorithm: Iterative Actions

Take an SSA edgeE out ofWL

Take meet of the value at def end and the use end ofE for the variable defined at def end

If the meet value is different from use value, replace the use by the meet

Recompute the defd at the use end ofE

If the recomputed value islowerthan the stored value, add all SSA edges originating atd

(19)

SSC Algorithm: Iterative Actions

Take an SSA edgeE out ofWL

Take meet of the value at def end and the use end ofE for the variable defined at def end

If the meet value is different from use value, replace the use by the meet

Recompute the defd at the use end ofE

If the recomputed value islowerthan the stored value, add all SSA edges originating atd

(20)

SSC Algorithm: Iterative Actions

Take an SSA edgeE out ofWL

Take meet of the value at def end and the use end ofE for the variable defined at def end

If the meet value is different from use value, replace the use by the meet

Recompute the defd at the use end ofE

If the recomputed value islowerthan the stored value, add all SSA edges originating atd

(21)

Meet forφ-function

v =φ(v1,v2, . . . ,vk)

⇒ValueOf(v) =v1∧v2∧. . .∧vn

(22)

SSC Algorithm: Complexity

Height of CP lattice = 2

Each SSA edge is examined at most twice, for each lowering

Theoritical size of SSA graph: O(V ×E) Practical size: linear in the program size

(23)

SSC Algorithm: Complexity

Height of CP lattice = 2

Each SSA edge is examined at most twice, for each lowering

Theoritical size of SSA graph: O(V ×E) Practical size: linear in the program size

(24)

SSC Algorithm: Complexity

Height of CP lattice = 2

Each SSA edge is examined at most twice, for each lowering

Theoritical size of SSA graph: O(V ×E) Practical size: linear in the program size

(25)

SSC Algorithm: Complexity

Height of CP lattice = 2

Each SSA edge is examined at most twice, for each lowering

Theoritical size of SSA graph: O(V ×E) Practical size: linear in the program size

(26)

SSC: Practice Example ENTRY

a = 2 b = 3 a < b c1 = 4 c2 = 5

c3 =φ(c1, c2) true false

(27)

SSC: Practice Example

What if we change “c1 = 4” to “c1 = 5”?

(28)

Sparse Condtional Constant Propagation (SCC)

Constant Propagation withunreachable code elimination Ignore definitions that reach a use via a non-executable edge

(29)

Sparse Condtional Constant Propagation (SCC)

Constant Propagation withunreachable code elimination Ignore definitions that reach a use via a non-executable edge

(30)

SCC Algorithm: Key Idea

v =φ(v1,v2, . . . ,vk)

⇒ValueOf(v) = ^

i∈ExecutablePath

vi

We ignore paths that are not “yet” marked executable

(31)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(32)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(33)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(34)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(35)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(36)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(37)

SCC Algorithm: Preparations

Two Worklists

Flow Worklist (FWL)

Worklist of flow graph edges SSA Worklist (SWL)

Worlist of SSA graph edges

Execution Halts whenbothworklists are empty

Associate a flag, theExecutableFlag, with every flow graph edge to control the evaluation ofφ-function in the

destination node

(38)

SCC Algorithm: Initialization

InitializeFWLto contain edges leaving ENTRY node InitializeSWLto empty

EachExecutableFlagis false initially Each value is⊤initially (Optimistic)

(39)

SCC Algorithm: Initialization

InitializeFWLto contain edges leaving ENTRY node InitializeSWLto empty

EachExecutableFlagis false initially Each value is⊤initially (Optimistic)

(40)

SCC Algorithm: Initialization

InitializeFWLto contain edges leaving ENTRY node InitializeSWLto empty

EachExecutableFlagis false initially Each value is⊤initially (Optimistic)

(41)

SCC Algorithm: Initialization

InitializeFWLto contain edges leaving ENTRY node InitializeSWLto empty

EachExecutableFlagis false initially Each value is⊤initially (Optimistic)

(42)

SCC Algorithm: Iterations

Remove an item from either worklist process the item (described next)

(43)

SCC Algorithm: Iterations

Remove an item from either worklist process the item (described next)

(44)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(45)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(46)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(47)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(48)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(49)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(50)

SCC Algorithm: ProcessingFWLItem

Item is flow graph edge

IfExecutableFlagis true, do nothing Otherwise

Mark theExecutableFlagas true

Visit-φfor allφ-functions in the destination

If only one of theExecutableFlags of incoming flow graph edges for dest is true (dest visted for the first time), then VisitExpressionfor all expressions in dest

If the dest contains only one outgoing flow graph edge, add that edge toFWL

(51)

SCC Algorithm: ProcessingSWLItem

Item is SSA edge

If dest is aφ-function,Visit-φ

If dest is an expression and any ofExecutableFlags for the incoming flow graph edges of dest is true, perform

VisitExpression

(52)

SCC Algorithm: ProcessingSWLItem

Item is SSA edge

If dest is aφ-function,Visit-φ

If dest is an expression and any ofExecutableFlags for the incoming flow graph edges of dest is true, perform

VisitExpression

(53)

SCC Algorithm: ProcessingSWLItem

Item is SSA edge

If dest is aφ-function,Visit-φ

If dest is an expression and any ofExecutableFlags for the incoming flow graph edges of dest is true, perform

VisitExpression

(54)

SCC Algorithm: Visit-φ

v =φ(v1,v2, . . . ,vk)

Ifithincoming edge’sExecutableFlagis true, vali =ValueOf(vi)elsevali =⊤

ValueOf(v) =V

ivali

(55)

SCC Algorithm: Visit-φ

v =φ(v1,v2, . . . ,vk)

Ifithincoming edge’sExecutableFlagis true, vali =ValueOf(vi)elsevali =⊤

ValueOf(v) =V

ivali

(56)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(57)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(58)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(59)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(60)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(61)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(62)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(63)

SCC Algorithm: VisitExpression

Evaluate the expression using values of operands and rules for operators

If the result is same as old, nothing to do Otherwise

If the expression is part of assignment, add all outgoing SSA edges toSWL

if the expression controls a conditional branch, then if the result is, add all outgoing flow edges toFWL if the value is constantc, only the corresponding flow graph edge is added toFWL

Value can not be(why?)

(64)

SCC Algorithm: Complexity

Each SSA edge is examined twice

Flow graph nodes are visited once for every incoming edge Complexity = O(# of SSA edges + # of flow graph edges)

(65)

SCC Algorithm: Complexity

Each SSA edge is examined twice

Flow graph nodes are visited once for every incoming edge Complexity = O(# of SSA edges + # of flow graph edges)

(66)

SCC Algorithm: Complexity

Each SSA edge is examined twice

Flow graph nodes are visited once for every incoming edge Complexity = O(# of SSA edges + # of flow graph edges)

(67)

SCC Algorithm: Correctness and Precision

SCC is conservative

Never labels a variable value as a constant

SCC is at least as powerful as Conditional Constant Propagation (CC)

Finds all constants as CC does

PROOFs: In paperConstant propagation with

conditional branchesbyMark N. Wegman, F. Kenneth Zadeck, ACM TOPLAS 1991.

(68)

SCC Algorithm: Correctness and Precision

SCC is conservative

Never labels a variable value as a constant

SCC is at least as powerful as Conditional Constant Propagation (CC)

Finds all constants as CC does

PROOFs: In paperConstant propagation with

conditional branchesbyMark N. Wegman, F. Kenneth Zadeck, ACM TOPLAS 1991.

(69)

SCC Algorithm: Correctness and Precision

SCC is conservative

Never labels a variable value as a constant

SCC is at least as powerful as Conditional Constant Propagation (CC)

Finds all constants as CC does

PROOFs: In paperConstant propagation with

conditional branchesbyMark N. Wegman, F. Kenneth Zadeck, ACM TOPLAS 1991.

(70)

SCC Algorithm: Correctness and Precision

SCC is conservative

Never labels a variable value as a constant

SCC is at least as powerful as Conditional Constant Propagation (CC)

Finds all constants as CC does

PROOFs: In paperConstant propagation with

conditional branchesbyMark N. Wegman, F. Kenneth Zadeck, ACM TOPLAS 1991.

(71)

SCC Algorithm: Correctness and Precision

SCC is conservative

Never labels a variable value as a constant

SCC is at least as powerful as Conditional Constant Propagation (CC)

Finds all constants as CC does

PROOFs: In paperConstant propagation with

conditional branchesbyMark N. Wegman, F. Kenneth Zadeck, ACM TOPLAS 1991.

(72)

Practice Example

ENTRY a = 2 b = 3 a < b c1 = 4 c2 = 5

c3 =φ(c1, c2) true false

References

Related documents

SOCIO-ECONOMIC DEVELOPMENT SERVICES For the Multifarious Development of Society at large, Old, Youth, School Dropouts, Housewives and Children of Financially Downtrodden

e) If the seller has delivered goods before the date for delivery, he may, up to that date, deliver any missing part or make up any deficiency in the quantity of the goods

This report provides some important advances in our understanding of how the concept of planetary boundaries can be operationalised in Europe by (1) demonstrating how European

-s file Returns true if file exists and has a greater size that zero -s file Returns true if file exists and has a greater size that zero -w file Returns true if file exists and

• If such a valve is placed in a laboratory flow test piping system with constant differential pressure and constant fluid density, the relationship of flow rate to stem

only up to 5%, if there is no load. This is the result of Second Way of energy saving. However, if there is load F with constant direction, and if it can be used for support of

The scan line algorithm which is based on the platform of calculating the coordinate of the line in the image and then finding the non background pixels in those lines and

• Variance of u is likely to be underestimated when there is positive auto correlation. • Prediction would be inefficient Tests for