• No results found

Interprocedural Data Flow Analysis

N/A
N/A
Protected

Academic year: 2022

Share "Interprocedural Data Flow Analysis"

Copied!
411
0
0

Loading.... (view fulltext now)

Full text

(1)

Interprocedural Data Flow Analysis

Uday Khedker

Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

October 2017

(2)

Part 1

About These Slides

(3)

CS 618 Interprocedural DFA: About These Slides 1/98

Copyright

These slides constitute the lecture notes for CS618 Program Analysis course at IIT Bombay and have been made available as teaching material accompanying the book:

• Uday Khedker, Amitabha Sanyal, and Bageshri Karkare.

(Indian edition published by Ane Books in 2013)Data Flow Analysis:

Theory and Practice. CRC Press (Taylor and Francis Group). 2009.

Apart from the above book, some slides are based on the material from the following books

• S. S. Muchnick and N. D. Jones. Program Flow Analysis. Prentice Hall Inc. 1981.

These slides are being made available under GNU FDL v1.2 or later purely for academic or research use.

(4)

CS 618 Interprocedural DFA: Outline 2/98

Outline

• Issues in interprocedural analysis

• Functional approach

• Classical call strings approach

• Value context based approach

Oct 2017 IIT Bombay

(5)

Part 2

Issues in Interprocedural Analysis

(6)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 3/98

Interprocedural Analysis: Overview

• Extends the scope of data flow analysis across procedure boundaries Incorporates the effects of

procedure calls in the caller procedures, and

calling contexts in the callee procedures

• Approaches :

Generic : Call strings approach, functional approach

Problem specific : Alias analysis, Points-to analysis, Partial redundancy elimination, Constant propagation

Oct 2017 IIT Bombay

(7)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 4/98

Why Interprocedural Analysis?

• Answering questions about formal parameters and global variables:

Which variables are constant?

Which variables aliased with each other?

Which locations can a pointer variable point to?

• Answering questions about side effects of a procedure call:

Which variables are defined or used by a called procedure?

(Could be local/global/formal variables)

• Most of the above questions may have aMay orMust qualifier

(8)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

Oct 2017 IIT Bombay

(9)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

main

Call multi-graph

(10)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

main

Call multi-graph proc. p

Oct 2017 IIT Bombay

(11)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

main

Call multi-graph proc. p

proc. q

(12)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

main

Call multi-graph proc. p

proc. q

Oct 2017 IIT Bombay

(13)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 5/98

Program Representation for Interprocedural Data Flow Analysis: Call Multi-Graph

Smain

a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq

n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq Callp

n4 d=c n4

Supergraphs of procedures

main

Call multi-graph proc. p

proc. q

(14)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b

Callp

a∗b Emain

Sp a∗b Sp

Callq

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

Callp

n3 d+ 1 n3

Eq a∗b Eq

Callp

n4 d=c n4

Oct 2017 IIT Bombay

(15)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq

C4 Call p C4

R4 Call p R4

n4 d=c n4

(16)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq

C4 Call p C4

R4 Call p R4

n4 d=c n4

Oct 2017 IIT Bombay

(17)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq

C4 Call p C4

R4 Call p R4

n4 d=c n4

(18)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq

C4 Call p C4

R4 Call p R4

n4 d=c n4

Oct 2017 IIT Bombay

(19)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 6/98

Program Representation for Interprocedural Data Flow Analysis: Supergraph

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq

C4 Call p C4

R4 Call p R4

n4 d=c n4

(20)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 7/98

Top-down Vs. Bottom-up Interprocedural Analysis

• Bottom-up approach

Traverses the call graph bottom up

Computes a parameterized summary of each callee

Can be viewed as procedure inlining

Summary is inlined at the all site, not the entire procedure body

• Top-down approach

Traverses the call graph top down

Needs to visit a procedure separately for every calling context

Can be viewed as procedure inlining

Oct 2017 IIT Bombay

(21)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis Sp Sp

a∗b

Callq

Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d

Callq

Er Er

(22)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis

Procedureq needs to be processed multiple times

Sp Sp

a∗b

Callq

Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d

Callq

Er Er

Expressionb+c is available in procedurep Expressiona∗bis not available in procedure p

Oct 2017 IIT Bombay

(23)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis

Procedureq needs to be processed multiple times

Sp Sp

a∗b

Callq

Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d

Callq

Er Er

Expressionsb+c andc∗d are available in procedurer

(24)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis Bottom-Up Analysis for Available Expressions Analysis

Sp Sp

a∗b

Callq

Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d

Callq

Er Er

Oct 2017 IIT Bombay

(25)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis Bottom-Up Analysis for Available Expressions Analysis

Call is replaced by

procedure summary

Sp Sp

a∗b Gen:b+c Kill:a∗b Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d Gen:b+c Kill:a∗b Er Er

Using procedure summary ofg at call sites

(26)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis Bottom-Up Analysis for Available Expressions Analysis

Call is replaced by

procedure summary

Sp Sp

a∗b Gen:b+c Kill:a∗b Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d Gen:b+c Kill:a∗b Er Er

Expressionb+c is available in procedurep Expressiona∗bis not available in procedure p

Oct 2017 IIT Bombay

(27)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 8/98

Top-down Vs. Bottom-up Interprocedural Analysis

Top-down Analysis for Available Expressions Analysis Bottom-Up Analysis for Available Expressions Analysis

Call is replaced by

procedure summary

Sp Sp

a∗b Gen:b+c Kill:a∗b Ep Ep

Sq Sq

a=...

b+c

Eq Eq

Sr Sr

c∗d Gen:b+c Kill:a∗b Er Er

Expressionsb+c andc∗d are available in procedurer

(28)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 9/98

Issues in Top-down Vs. Bottom-up Interprocedural Analysis

• Bottom-up approach

Compact representation

Information may depend on the calling context

• Top-down approach

Exponentially large number of calling contexts

Many contexts may have no effect on the procedure

Oct 2017 IIT Bombay

(29)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main

Interprocedurally valid control flow path

(30)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main

Interprocedurally valid control flow path

Oct 2017 IIT Bombay

(31)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main stack q:C2

p:C1 main

Interprocedurally valid control flow path

(32)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main stack q:C2

p:C1 main

×

stack q:C2 p:C1 main

Interprocedurallyinvalidcontrol flow path

Oct 2017 IIT Bombay

(33)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main stack q:C2

p:C1 main

×

stack q:C2 p:C1 main

×

×

stack q:C2 p:C1 main

Interprocedurallyinvalidcontrol flow path

(34)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 10/98

Validity of Interprocedural Control Flow Paths

Smain a+b C1 Call p C1

R1 Call p R1

a∗b Emain

Sp a∗b Sp

C2 Call q C2

R2 Call q R2

Ep a∗b Ep

Sq a∗b Sq n1 d=a+b n1n2 a= 1 n2

C3 Call p C3

R3 Call pR3

n3 d+ 1 n3

Eq a∗b Eq C4 Call p C4

R4 Call p R4

n4 d=c n4 stack q:C2 p:C1 main stack q:C2

p:C1 main stack q:C2

p:C1 main

Interprocedurally valid control flow path

Oct 2017 IIT Bombay

(35)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

(36)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

Oct 2017 IIT Bombay

(37)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

A path which represents legal control flow

(38)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

• Ensuring Precision. Only valid paths should be covered A path which represents

legal control flow

Oct 2017 IIT Bombay

(39)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

• Ensuring Precision. Only valid paths should be covered A path which represents

legal control flow

Subject to merging data flow values at shared program points without creating invalid paths

(40)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

• Ensuring Precision. Only valid paths should be covered

• Ensuring Efficiency. Only relevant valid paths should be covered A path which represents

legal control flow

Subject to merging data flow values at shared program points without creating invalid paths

Oct 2017 IIT Bombay

(41)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 11/98

Soundness, Precision, and Efficiency of Data Flow Analysis

• Data flow analysis uses static representation of programs to compute summary information along paths

• Ensuring Soundness. All valid paths must be covered

• Ensuring Precision. Only valid paths should be covered

• Ensuring Efficiency. Only relevant valid paths should be covered A path which represents

legal control flow

Subject to merging data flow values at shared program points without creating invalid paths

A path which yields information that affects the summary information

(42)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 12/98

Flow and Context Sensitivity

• Flow sensitive analysis:

Considersintraprocedurally valid paths

Oct 2017 IIT Bombay

(43)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 12/98

Flow and Context Sensitivity

• Flow sensitive analysis:

Considersintraprocedurally valid paths

• Context sensitive analysis:

Considersinterprocedurallyvalid paths

(44)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 12/98

Flow and Context Sensitivity

• Flow sensitive analysis:

Considersintraprocedurally valid paths

• Context sensitive analysis:

Considersinterprocedurallyvalid paths

• For maximum statically attainable precision , analysis must be both flow and context sensitive

Oct 2017 IIT Bombay

(45)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 12/98

Flow and Context Sensitivity

• Flow sensitive analysis:

Considersintraprocedurally valid paths

• Context sensitive analysis:

Considersinterprocedurallyvalid paths

• For maximum statically attainable precision , analysis must be both flow and context sensitive

MFP computation restricted to valid paths only

(46)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x=fr(x) x

y

y

y=fr(y) y fr

Oct 2017 IIT Bombay

(47)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x

y

y

y fr

(48)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x

y

y

y fr

×

Oct 2017 IIT Bombay

(49)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x

y

y

y fr

(50)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x

y

y

y fr

×

Oct 2017 IIT Bombay

(51)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 13/98

Context Sensitivity in Interprocedural Analysis

Sr

Er

Ss

Es

Ci

Ri ci

St

Et

Cj

Rj cj

x

x

x

y

y

y fr

Context sensitivity is all about

• returning the right value from a callee to the right caller, and

• not about passing the right value from a caller to the right callee.

(52)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 14/98

Increasing Precision in Data Flow Analysis

Flow insensitive intraprocedural

Flow sensitive intraprocedural

Context insensitive flow insensitive

Context insensitive flow sensitive

Context sensitive flow insensitive

Context sensitive flow sensitive

Oct 2017 IIT Bombay

(53)

CS 618 Interprocedural DFA: Issues in Interprocedural Analysis 14/98

Increasing Precision in Data Flow Analysis

Flow insensitive intraprocedural

Flow sensitive intraprocedural

Context insensitive flow insensitive

Context insensitive flow sensitive

Context sensitive flow insensitive

Context sensitive flow sensitive

actually, only caller sensitive

(54)

Part 3

Classical Functional Approach

(55)

CS 618 Interprocedural DFA: Classical Functional Approach 15/98

Functional Approach

Sr

Er

Ss

Es

Ci

Ri

fr

ci

x

x

x=fr(x) x

(56)

CS 618 Interprocedural DFA: Classical Functional Approach 15/98

Functional Approach

Sr

Er

Ss

Es

Ci

Ri

fr

x

x =fr(x)

• Bottom-up Approach

• Compute summary flow functions for each procedure

• Use summary flow functions as the flow function for a call block

• Main challenge:

Appropriate representation for summary flow functions

Oct 2017 IIT Bombay

(57)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

(58)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

Oct 2017 IIT Bombay

(59)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

(60)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

Oct 2017 IIT Bombay

(61)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

Φr(u3)≡f1 Φr(u4)≡f1

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

(62)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

Φr(u3)≡f1 Φr(u4)≡f1

Φr(u5)≡f2◦f1

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

Oct 2017 IIT Bombay

(63)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

Φr(u3)≡f1 Φr(u4)≡f1

Φr(u5)≡f2◦f1 Φr(u6)≡f3◦f1

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

(64)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

Φr(u3)≡f1 Φr(u4)≡f1

Φr(u5)≡f2◦f1 Φr(u6)≡f3◦f1

Φr(u7)≡f2◦f1⊓f3◦f1

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

Oct 2017 IIT Bombay

(65)

CS 618 Interprocedural DFA: Classical Functional Approach 16/98

Notation for Summary Flow Function

For simplicity forward flow is assumed

Procedurer

f1

u1

u2

f2

u3

u5

f3

u4

u6

f4 u7

u8

Φr(u1)≡φid

Φr(u2)≡f1

Φr(u3)≡f1 Φr(u4)≡f1

Φr(u5)≡f2◦f1 Φr(u6)≡f3◦f1

Φr(u7)≡f2◦f1⊓f3◦f1

Φr(u8)≡f4◦(f2◦f1⊓f3◦f1)

• ui: Program points

• fi: Node flow functions

• Φr(ui): Summary flow functions mapping data flow value fromSr toui

(66)

CS 618 Interprocedural DFA: Classical Functional Approach 17/98

Equations for Constructing Summary Flow Functions

For simplicity forward flow is assumed. In is Entry ofn,On is Exit ofn

Φr(In) =



φid ifnisSr

p∈pred(n)

Φr(Op)

otherwise

Φr(On) =



Φs(u)◦Φr(In) ifncalls procedures andu isOEs

fn◦Φr(In) otherwise The summary flow function of a given procedurer

• is influenced by summary flow functions of the callees ofr

• is not influenced by summary flow functions of the callers ofr

Fixed point computation may be required in the presence of loops or recursion

Oct 2017 IIT Bombay

(67)

CS 618 Interprocedural DFA: Classical Functional Approach 18/98

Constructing Summary Flow Functions Iteratively

f1

r

f2

(68)

CS 618 Interprocedural DFA: Classical Functional Approach 18/98

Constructing Summary Flow Functions Iteratively

f1

r

f2

Φr(u1) =φid

Φr(u2) =f1

Iteration #1

Φr(u3) =f1

Φr(u4) =f2◦f1

Oct 2017 IIT Bombay

(69)

CS 618 Interprocedural DFA: Classical Functional Approach 18/98

Constructing Summary Flow Functions Iteratively

f1

r

f2

Φr(u1) =φid

Φr(u2) =f1

Iteration #2

Φr(u3) =f1⊓f2◦f1

Φr(u4) =f2◦(f1⊓f2◦f1)

(70)

CS 618 Interprocedural DFA: Classical Functional Approach 18/98

Constructing Summary Flow Functions Iteratively

f1

r

f2

Φr(u1) =φid

Φr(u2) =f1

Iteration #3

Φr(u3) =f1⊓f2◦(f1⊓f2◦f1)

Φr(u4) =f2◦(f1⊓f2◦(f1⊓f2◦f1))

Termination is possible only if all function compositions and confluences can be reduced to a finite set of functions

Oct 2017 IIT Bombay

(71)

CS 618 Interprocedural DFA: Classical Functional Approach 19/98

Lattice of Flow Functions for Live Variables Analysis

Component functions (i.e. for a single variable) Lattice of

data flow

values All possible flow functions Lattice of flow functions

⊤b =∅

⊥b ={a}

Genn Killn bfn bfn(x), ∀x∈ {⊤,b ⊥}b

∅ ∅ φbid x

∅ {a} φb ⊤b

{a} ∅ φb ⊥b

{a} {a}

φb φbid φb

(72)

CS 618 Interprocedural DFA: Classical Functional Approach 20/98

Reducing Component Flow Functions for Live Variables Analysis

Letφb∈ {bφ,φbid,φb} andx∈ {1,0}. Then,

• φb⊓φb=φb (because 0 +x =x)

• φb⊓φb=φb (because 1 +x= 1)

• φb◦φb=φb (becauseφb is a constant function)

• φb◦φb=φb (becauseφb is a constant function)

• φbid◦φb=φb (becauseφbid is the identity function)

Oct 2017 IIT Bombay

(73)

CS 618 Interprocedural DFA: Classical Functional Approach 21/98

Reducing Function Compositions in Bit Vector Frameworks

Killndenoted byKn and Genndenoted byGn

f3(x) =f2(f1(x))

(74)

CS 618 Interprocedural DFA: Classical Functional Approach 21/98

Reducing Function Compositions in Bit Vector Frameworks

Killndenoted byKn and Genndenoted byGn

f3(x) =f2(f1(x))

=f2 (x−K1)∪G1

G1,K1

Oct 2017 IIT Bombay

(75)

CS 618 Interprocedural DFA: Classical Functional Approach 21/98

Reducing Function Compositions in Bit Vector Frameworks

Killndenoted byKn and Genndenoted byGn

f3(x) =f2(f1(x))

=f2 (x−K1)∪G1

= (x−K1)∪G1

−K2

∪ G2

G1,K1

G2,K2

(76)

CS 618 Interprocedural DFA: Classical Functional Approach 21/98

Reducing Function Compositions in Bit Vector Frameworks

Killndenoted byKn and Genndenoted byGn

f3(x) =f2(f1(x))

=f2 (x−K1)∪G1

= (x−K1)∪G1

−K2

∪ G2

= x−(K1∪K2)

∪ (G1−K2) ∪ G2

G1,K1

G2,K2

Oct 2017 IIT Bombay

(77)

CS 618 Interprocedural DFA: Classical Functional Approach 21/98

Reducing Function Compositions in Bit Vector Frameworks

Killndenoted byKn and Genndenoted byGn

f3(x) =f2(f1(x))

=f2 (x−K1)∪G1

= (x−K1)∪G1

−K2

∪ G2

= x−(K1∪K2)

∪ (G1−K2) ∪ G2

Hence, K3=K1∪K2

G3= (G1−K2) ∪ G2

G1,K1

G2,K2

(78)

CS 618 Interprocedural DFA: Classical Functional Approach 22/98

Reducing Bit Vector Flow Function Confluences (1)

Killndenoted byKn and Genndenoted byGn

• When⊓is∪, f3(x) =f2(x)∪f1(x)

= (x−K2)∪G2

∪ (x−K1)∪G1

= x−(K1∩K2)

∪ G1∪G2

Hence, K3=K1∩K2

G3=G1∪G2

G1,K1 G2,K2

Oct 2017 IIT Bombay

(79)

CS 618 Interprocedural DFA: Classical Functional Approach 22/98

Reducing Bit Vector Flow Function Confluences (1)

Killndenoted byKn and Genndenoted byGn

• When⊓is∪, f3(x) =f2(x)∪f1(x)

= (x−K2)∪G2

∪ (x−K1)∪G1

= x−(K1∩K2)

∪ G1∪G2

Hence, K3=K1∩K2

G3=G1∪G2

G1,K1 G2,K2

(80)

CS 618 Interprocedural DFA: Classical Functional Approach 23/98

Reducing Bit Vector Flow Function Confluences (2)

Killndenoted byKn and Genndenoted byGn

• When⊓is∩, f3(x) =f2(x)∩f1(x)

= (x−K2)∪G2

∩ (x−K1)∪G1

= x−(K1∪K2)

∪ G1∩G2

Hence, K3=K1∪K2

G3=G1∩G2

G1,K1 G2,K2

Oct 2017 IIT Bombay

(81)

CS 618 Interprocedural DFA: Classical Functional Approach 23/98

Reducing Bit Vector Flow Function Confluences (2)

Killndenoted byKn and Genndenoted byGn

• When⊓is∩, f3(x) =f2(x)∩f1(x)

= (x−K2)∪G2

∩ (x−K1)∪G1

= x−(K1∪K2)

∪ G1∩G2

Hence, K3=K1∪K2

G3=G1∩G2

G1,K1 G2,K2

(82)

CS 618 Interprocedural DFA: Classical Functional Approach 24/98

Lattice of Flow Functions for Live Variables Analysis

Flow functions for two variables

• Product of lattices for independent variables (because of separability)

Lattice of data flow

values All possible flow functions Lattice of

flow functions

⊤=∅ {a} {b}

⊥={a,b}

Genn Killn fn Genn Killn fn

∅ ∅ φII {b} ∅ φI

∅ {a} φ⊤I {b} {a} φ⊤⊥

∅ {b} φI⊤ {b} {b} φI

∅ {a,b} φ⊤⊤ {b} {a,b} φ⊤⊥

{a} ∅ φ⊥I {a,b} ∅ φ⊥⊥

{a} {a} φ⊥I {a,b} {a} φ⊥⊥

{a} {b} φ⊥⊤ {a,b} {b} φ⊥⊥

{a} {a,b} φ⊥⊤ {a,b} {a,b} φ⊥⊥

φ⊤⊤

φ⊤I φI⊤

φ⊤⊥ φII φ⊥⊤

φI φ⊥I

φ⊥⊥

Oct 2017 IIT Bombay

(83)

CS 618 Interprocedural DFA: Classical Functional Approach 25/98

An Example of Interprocedural Liveness Analysis

a= 5;b= 3 c= 7;read d Smain

Call p c1

n1 a=a+ 2 e=c+d n1

n2 d=a∗b n2

Call q c2

print a+c+e Emain

b= 2 if (b<d) Sp

n3 c=a+b n3c4 Call q

print c+d Ep

T F

a= 1 Sq

Call p c3

a=a∗b Eq

(84)

CS 618 Interprocedural DFA: Classical Functional Approach 26/98

Summary Flow Functions for Interprocedural Liveness Analysis

Pro

c. Flow

Function Defining Expression

Iteration #1 Changes in iteration #2

Gen Kill Gen Kill

p

Φp(Ep) fEp {c,d} ∅

Φp(n3) fn3◦Φp(Ep) {a,b,d} {c}

Φp(c4) fq◦Φp(Ep) =φ ∅ {a,b,c,d,e} {d} {a,b,c}

Φp(Sp) fSp◦ Φp(n3)⊓Φp(c4)

{a,d} {b,c}

fp Φp(Sp) {a,d} {b,c}

q

Φq(Eq) fEq {a,b} {a}

Φq(c3) fp◦Φq(Eq) {a,d} {a,b,c}

Φq(Sq) fSq◦Φq(c3) {d} {a,b,c}

fq Φq(Sq) {d} {a,b,c}

Oct 2017 IIT Bombay

(85)

CS 618 Interprocedural DFA: Classical Functional Approach 27/98

Computed Summary Flow Functions

b= 2 if (b<d) Sp

n3 c=a+b nc34 Call q

print c+d Ep

T F

a= 1 Sq

Call p c3

a=a∗b Eq

Summary Flow Function Φp(Ep) BIp∪ {c,d}

Φp(n3) BIp− {c}

∪ {a,b,d}

Φp(c4) BIp− {a,b,c}

∪ {d}

Φp(Sp) BIp− {b,c}

∪ {a,d}

Φq(Eq) BIq− {a}

∪ {a,b}

Φq(c3) BIq− {a,b,c}

∪ {a,d}

Φq(Sq) BIq− {a,b,c}

∪ {d}

References

Related documents

• Pointer analysis enables not only precise data analysis but also precise control flow analysis. • Needs to scale to

• in defining other strongly live variables in an assignment statement (this case is different from simple liveness analysis)... Using Data Flow Information of Live

1 July 2012 Introduction to DFA: Live Variables Analysis 6/34.. Local Data Flow Properties for Live

function used at call site to compute the effect of procedure on program state..

Is it possible to avoid explicit function compositions and meets?.?.

July 2010 Introduction to DFA: Live Variables Analysis 8/20. Local Data Flow Properties for Live

These slides constitute the lecture notes for CS618 Program Analysis course at IIT Bombay and have been made available as teaching material accompanying the book:.. • Uday

• Data flow analysis uses static representation of programs to compute summary information along paths... • Data flow analysis uses static representation of programs to compute