Docsity

Data Flow Analysis in CMSC 631: Understanding Program Analysis, Fall 2003, Study notes of Computer Science

Toggle sidebar
bg1
Data Flow Analysis
CMSC 631 — Program Analysis and
Understanding
Fall 2003
2
CMSC 631, Fall 2003
• Source code parsed to produce AST
• AST transformed to CFG
■Simpler representation of the program
■Fewer forms to reason about
• Data flow analysis operates on control flow
graph (and other intermediate representations)
Compiler Structure
Source
Code
Abstract
Syntax
Tree
Control
Flow
Graph
Object
Code
3
CMSC 631, Fall 2003
x:=a+b;
y:=a*b;
while(y>a+b){
a:=a+1;
x:=a+b
}
Control-Flow Graph
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
4
CMSC 631, Fall 2003
• A framework for proving facts about programs
• Reasons about lots of little facts
• Little or no interaction between facts
■Works best on properties about how program
computes
• Based on all paths through program
■Including infeasible paths
Data Flow Analysis
5
CMSC 631, Fall 2003
• An expression e is available at program point p if
■e is computed on every path to p, and
■the value of e has not changed since the last time e is
computed on p
• Optimization
■If an expression is available, need not be recomputed
-(At least, if it’s still in a register somewhere)
Available Expressions
6
CMSC 631, Fall 2003
• Is expression e available?
• Facts:
■a + b is available
■a * b is available
■y > a + b is available
Data Flow Facts
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
pf3
pf4
pf5

Partial preview of the text

Download Data Flow Analysis in CMSC 631: Understanding Program Analysis, Fall 2003 and more Study notes Computer Science in PDF only on Docsity!

Data Flow Analysis

CMSC 631 — Program Analysis and

Understanding

Fall 2003

CMSC 631, Fall 2003 2

• Source code parsed to produce AST

• AST transformed to CFG

■ Simpler representation of the program

■ Fewer forms to reason about

• Data flow analysis operates on control flow

graph (and other intermediate representations)

Compiler Structure

Source

Code

Abstract

Syntax

Tree

Control

Flow

Graph

Object

Code

3

CMSC 631, Fall 2003

Control-Flow Graph

x := a + b

y := a * b

y > a + b

a := a + 1

x := a + b

4

CMSC 631, Fall 2003

• A framework for proving facts about programs

• Reasons about lots of little facts

• Little or no interaction between facts

■ Works best on properties about how program

computes

• Based on all paths through program

■ Including infeasible paths

Data Flow Analysis

• An expression e is available at program point p if

■ (^) e is computed on every path to p, and

■ (^) the value of e has not changed since the last time e is

computed on p

• Optimization

■ (^) If an expression is available, need not be recomputed

  • (At least, if it’s still in a register somewhere)

Available Expressions

• Is expression e available?

• Facts:

■ (^) a + b is available

■ (^) a * b is available

■ y > a + b is available

Data Flow Facts

x := a + b

y := a * b

y > a + b

a := a + 1

x := a + b

CMSC 631, Fall 2003 7

• What is the effect of each

statement on the set of facts?

Gen and Kill

x := a + b

y := a * b

y > a + b

a := a + 1

x := a + b

Stmt Gen Kill

x := a + b a + b

y := a * b a * b

y > a + b y > a + b

a := a + 1

a + b,

a * b,

y > a + b

CMSC 631, Fall 2003 8

• Let s be a statement

■ succ(s) = { immediate successor statements of s }

■ (^) pred(s) = { immediate predecessor statements of s}

■ (^) In(s) = program point just before executing s

■ (^) Out(s) = program point just after executing s

• In(s) =

s pred(s)

Out(s )

• Out(s) = Gen(s) (In(s) - Kill(s))

■ Note: These are also called transfer functions

Data Flow Equations

9

CMSC 631, Fall 2003

Computing Available Expressions

x := a + b

y := a * b

y > a + b

a := a + 1

x := a + b

{a + b}

{a + b, a * b}

{a + b, a * b,

y > a + b}

{a + b}

{a + b}

{a + b}

{a + b}

10

CMSC 631, Fall 2003

• A variable v is live at program point p if

■ (^) v will be used on some execution path originating

from p...

■ before v is overwritten

• Optimization

■ (^) If a variable is not live, no need to keep it in a register

■ (^) If variable is dead at assignment, can eliminate

assignment

Liveness Analysis

• Available expressions is a forward must analysis

■ (^) Data flow propagate in same dir as CFG edges

■ (^) Expr is available only if available on all paths

■

• Liveness is a backward may problem

■ To know if variable live, need to look at future uses

■ Variable is live if available on some path

• In(s) = Gen(s) (Out(s) - Kill(s))

• Out(s) =

s succ(s)

In(s )

Data Flow Equations

• What is the effect of each

statement on the set of facts?

Gen and Kill

x := a + b

y := a * b

y > a + b

a := a + 1

x := a + b

Stmt Gen Kill

x := a + b a, b x

y := a * b a, b y

y > a + b y, a, b

a := a + 1 a a

CMSC 631, Fall 2003 19

• A finite partial order is a lattice if meet and join

exist for every pair of elements

• A lattice has unique elements and such that

■

■

• In a lattice,

Lattices (cont’d)

x! ⊥ = ⊥

x! " = x

x! ⊥ = x

x! " = "

x ≤ y iff x " y = x

x ≤ y iff x # y = y

CMSC 631, Fall 2003 20

• Typically, data flow facts form a lattice

■ Example: Available expressions

Data Flow Facts and Lattices

a+b, a*b, y > a+b

a+b, a*b a+b, y > a+b

a+b

a*b, y > a+b

a*b y > a+b

(none)

21

CMSC 631, Fall 2003

  • Out(s) = Gen(s) for all statements s

■ Or, if you like, Out(s) = Top

  • W := { all statements } (worklist)
  • repeat

■ Take s from W

■

In(s) := s pred(s)

Out(s )

■ temp := Gen(s) (In(s) - Kill(s))

■ if (temp != Out(s)) {

  • Out(s) := temp
  • W := W succ(s)

■ }

  • until W =

Forward May Data Flow Algorithm

22

CMSC 631, Fall 2003

• A function f on a partial order is monotonic if

• Easy to check that operations to compute In and

Out are monotonic

■ s pred(s)

Out(s)

■ (^) Gen(s) (In(s) - Kill(s))

Monotonicity

x ≤ y ⇒ f (x) ≤ f (y)

  • Out(s) = Gen(s) for all statements s

■ Or, if you like, Out(s) = Top

  • W := { all statements } (worklist)
  • repeat

■ Take s from W

■ (^) temp :=

■ (^) if (temp != Out(s)) {

  • Out(s) := temp
  • W :=^ W^ succ(s)

■ }

  • until W =

Forward May Data Flow Algorithm

f s

s ′^ ∈pred(s)

Out(s

′ ))

• We know the algorithm terminates because

■ (^) The lattice has finite height

■ (^) The operations to compute In and Out are monotonic

■ (^) On every iteration, we remove a statement from the

worklist and/or move down the lattice

Termination

CMSC 631, Fall 2003 25

• By monotonicity, we also have

• A function f is distributive if

Distributive Data Flow Problems

f (x! y) ≤ f (x)! f (y)

f (x! y) = f (x)! f (y)

CMSC 631, Fall 2003 26

• Joins lose no information

Benefit of Distributivity

f g

h

k

k(h(f (!) " g(!))) =

k(h(f (!)) " h(g(!))) =

k(h(f (!))) " k(h(g(!)))

27

CMSC 631, Fall 2003

• Ideally, we would like to compute the meet over

all paths (MOP) solution:

■ Let f s

be the transfer function for statement s

■ If p is a path {s 1

, ..., s n

}, let f p

= f n

;...;f 1

■ Let path(s) be the set of paths from the entry to s

• If a data flow problem is distributive, then solving

the data flow equations in the standard way

yields the MOP solution

Accuracy of Data Flow Analysis

MOP(s) =! p∈ path(s)

fp (")

28

CMSC 631, Fall 2003

• Analyses of how the program computes

■ (^) Live variables

■ (^) Available expressions

■ (^) Reaching definitions

■ (^) Very busy expressions

• All Gen/Kill problems are distributive

What Problems are Distributive?

• Constant propagation

• In general, analysis of what the program

computes in not distributive

A Non-Distributive Example

x := 1

y := 2

x := 2

y := 1

z := x + y

• Data flow facts = assertions that are true or

false at a program point

• Represent set of facts as bit vector

■ Fact i

represented by bit i

■ Intersection = bitwise and, union = bitwise or, etc

• “Only” a constant factor speedup

■ (^) But very useful in practice

Practical Implementation

CMSC 631, Fall 2003 37

• Elimination methods only work on reducible flow

graphs

■ Ones that can be collapsed

■ Standard constructs yield only reducible flow graphs

■

• Unrestricted goto can yield non-reducible graphs

Non-Reducible Flow Graphs

A

B C

z w

CMSC 631, Fall 2003 38

• Data flow analysis is flow-sensitive

■ The order of statements is taken into account

■ (^) I.e., we keep track of facts per program point

• Alternative: Flow-insensitive analysis

■ (^) Analysis the same regardless of statement order

■ Standard example: types

  • /* x : int / x := ... / x : int */

Flow-Sensitivity

39

CMSC 631, Fall 2003

• Must vs. May

■ (^) (Not always followed in literature)

• Forwards vs. Backwards

• Flow-sensitive vs. Flow-insensitive

• Distributive vs. Non-distributive

Terminology Review

40

CMSC 631, Fall 2003

• What happens at a function call?

■ (^) Lots of proposed solutions in data flow analysis

literature

• In practice, only analyze one procedure at a time

■

• Consequences

■ Call to function kills all data flow facts

■ (^) May be able to improve depending on language, e.g.,

function call may not affect locals

Data Flow Analysis and Functions

• An analysis that models only a single function at

a time is intraprocedural

• An analysis that takes multiple functions into

account is interprocedural

• An analysis that takes the whole program into

account is...guess?

• Note: global analysis means “more than one

basic block,” but still within a function

More Terminology

• Data Flow is good at analyzing local variables

■ (^) But what about values stored in the heap?

■ (^) Not modeled in traditional data flow

• In practice: *x := e

■ Assume all data flow facts killed (!)

■ Or, assume x may affect any variable whose address

has not been taken

• In general, hard to analyze pointers

Data Flow Analysis and The Heap