Docsity

Data Flow Analysis in CMSC 631: Syntax Trees, Control Flow, and Data Equations - Prof. Mic, Study notes of Computer Science

Toggle sidebar
bg1
1
Data Flow Analysis
CMSC 631
—
Program Analysis and
Understanding
Fall 2006
2
CMSC 631
•
Source code parsed to produce AST
•
AST transformed to CFG
•
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
Abstract Syntax Tree (AST)
•
Programs are written in text
¦
I.e., sequences of characters
¦
Awkward to work with
•
First step: Convert to structured
representation
¦
Use lexer (like flex) to recognize
tokens
-
Sequences of characters that make words in the language
¦
Use parser (like bison) to group words structurally
-
And, often, to produce AST
4
CMSC 631
Abstract Syntax Tree Example
•
x := a + b;
•
y := a * b;
•
while (y > a) {
•
a := a + 1;
•
x := a + b
•}
Program
While
Block
:=
…
x
a b
+
y a
a
a+
1
:=
…
>
5
CMSC 631
ASTs
•
ASTs are
abstract
¦
They don
’
t contain all information in the program
-
E.g., spacing, comments, brackets, parentheses
¦
Any ambiguity has been resolved
-
E.g., a + b + c produces the same AST as (a + b) + c
•
For more info, see CMSC 430
¦
In this class, we will generally begin with the AST
6
CMSC 631
Disadvantages of ASTs
•
AST has many similar forms
¦
E.g., for, while, repeat...until
¦
E.g., if, ?:, switch
•
Expressions in AST may be complex, nested
¦
(42 * y) + (z > 5 ? 12 * z : z + 20)
•
Want simpler representation for analysis
¦
...at least, for dataflow analysis
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data Flow Analysis in CMSC 631: Syntax Trees, Control Flow, and Data Equations - Prof. Mic and more Study notes Computer Science in PDF only on Docsity!

Data Flow Analysis

CMSC 631 — Program Analysis and

Understanding

Fall 2006

CMSC 631 2

  • Source code parsed to produce AST
  • AST transformed to CFG
  • Data flow analysis operates on control flow graph (and other intermediate representations)

Compiler Structure

Source Code

Abstract Syntax Tree

Control Flow Graph

Object Code

CMSC 631 3

Abstract Syntax Tree (AST)

  • Programs are written in text

¦ I.e., sequences of characters

¦ Awkward to work with

  • First step: Convert to structured representation

¦ Use lexer (like flex) to recognize tokens

  • Sequences of characters that make words in the language

¦ Use parser (like bison) to group words structurally

  • And, often, to produce AST CMSC 631 4

Abstract Syntax Tree Example

•x := a + b; •y := a * b; •while (y > a) {

  • a := a + 1;
  • x := a + b •}

Program

While

Block

x a b

y a

a

a (^) + 1

CMSC 631 5

ASTs

  • ASTs are abstract

¦ They don’t contain all information in the program

  • E.g., spacing, comments, brackets, parentheses

¦ Any ambiguity has been resolved

  • E.g., a + b + c produces the same AST as (a + b) + c
  • For more info, see CMSC 430

¦ In this class, we will generally begin with the AST

CMSC 631 6

Disadvantages of ASTs

  • AST has many similar forms

¦ E.g., for, while, repeat...until

¦ E.g., if, ?:, switch

  • Expressions in AST may be complex, nested

¦ (42 * y) + (z > 5? 12 * z : z + 20)

  • Want simpler representation for analysis

¦ ...at least, for dataflow analysis

CMSC 631 7

Control-Flow Graph (CFG)

  • A directed graph where

¦ Each node represents a statement

¦ Edges represent control flow

  • Statements may be

¦ Assignments x := y op z or x := op z

¦ Copy statements x := y

¦ Branches goto L or if x relop y goto L

¦ etc.

CMSC 631 8

•x := a + b; •y := a * b; •while (y > a) {

  • a := a + 1;
  • x := a + b •}

Control-Flow Graph Example

x := a + b

y := a * b

y > a

a := a + 1

x := a + b

CMSC 631 9

Variations on CFGs

  • Usually don’t include declarations (e.g., int x;)

¦ But there’s usually something in the

implementation

  • May want a unique entry and exit node

¦ Won’t matter for the examples we give

  • May group statements into basic blocks

¦ A sequence of instructions with no branches into

or out of the block

CMSC 631 10

Control-Flow Graph w/Basic Blocks

  • Can lead to more efficient implementations
  • But more complicated to explain, so...

¦ We’ll use single-statement blocks in lecture today

x := a + b; y := a * b; while (y > a + b) { a := a + 1; x := a + b }

x := a + b y := a * b y > a

a := a + 1 x := a + b

CMSC 631 11

CFG vs. AST

  • CFGs are much simpler than ASTs

¦ Fewer forms, less redundancy, simple expressions

  • But...AST is a more faithful representation

¦ CFGs introduce temporaries

¦ Lose block structure of program

  • So for AST,

¦ Easier to report error + other messages

¦ Easier to explain to programmer

¦ Easier to unparse to produce readable code

CMSC 631 12

  • 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

CMSC 631 19

  • 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

CMSC 631 20

  • 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 used on some path

  • Out(s) = U

s' succ(s)

In(s')

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

Data Flow Equations

CMSC 631 21

  • What is the effect of each statement on the set of facts?

Gen and Kill

Stmt Gen Kill

x := a + b a, b x

y := a * b a, b y

y > a a, y

a := a + 1 a a

x := a + b

y := a * b

y > a

a := a + 1

x := a + b

CMSC 631 22

Computing Live Variables

{x}

{x, y, a}

{x, y, a}

{y, a, b}

{y, a, b}

{x, a, b}

{a, b} x := a + b

y := a * b

y > a

a := a + 1

x := a + b

{x, y, a, b}

{x, y, a, b}

CMSC 631 23

•An expression e is very busy at point p if

¦ On every path from p, expression e is used before any

component of e is changed

•Optimization

¦ Can hoist very busy expression computation to p

•What kind of problem?

¦ Forward or backward?

¦ May or must?

Very Busy Expressions

backward

must

CMSC 631 24

  • A definition of a variable v is an assignment to v
  • A definition of variable v reaches point p if

¦ There is no intervening assignment to v

  • Also called def-use information
  • What kind of problem?

¦ Forward or backward?

¦ May or must?

Reaching Definitions

forward

may

CMSC 631 25

  • Most data flow analyses can be classified this way

¦ A few don’t fit: bidirectional analysis

  • Lots of literature on data flow analysis

Space of Data Flow Analyses

May Must

Forward (^) definitionsReaching expressionsAvailable

Backward (^) variablesLive expressionsVery busy

CMSC 631 26

  • Typically, data flow facts form a lattice

¦ Example: Available expressions

Data Flow Facts and Lattices

“top”

“bottom”

a+b, a*b, a+

a+b, ab ab, a+ a+b, a+

(none)

a*b a+

a+b

CMSC 631 27

  • A partial order is a pair such that ¦ ¦ ¦ ¦

Partial Orders

CMSC 631 28

Meet and Join Operations

  • is the meet or greatest lower bound operation: ¦ ¦
  • is the join or least upper bound operation: ¦ ¦

CMSC 631 29

Lattices

•A partial order is a lattice if meet and join exist for every pair of elements in P

•A lattice has unique elements and such that

¦ ¦

•In a lattice,

•A partial order is a complete lattice if meet and join are defined on any set S P CMSC 631 30

Useful Lattices

  • (

S

, ) forms a lattice for any set S

¦ 2 S^ is the powerset of S (set of all subsets)

  • If (S, <=) is a lattice, so is (S, >=)

¦ I.e., lattices can be flipped

  • The lattice for constant propagation

CMSC 631 37

Lattices (P, <= ), cont’d

  • Live variables

¦ P = sets of variables

¦ S1 S2 = S1 U S

¦ Top = empty set

  • Very busy expressions

¦ P = set of expressions

¦ S1 S2 = S1 n S

¦ Top = set of all expressions

CMSC 631 38

Forward vs. Backward

Out(s) = Top for all s

W := { all statements }

repeat

Take s from W

temp := f

s

s' pred(s)

Out(s'))

if (temp != Out(s)) {

Out(s) := temp

W := W U succ(s)

until W = Ø

In(s) = Top for all s

W := { all statements }

repeat

Take s from W

temp := f s

s' succ(s) In(s'))

if (temp != In(s)) {

In(s) := temp

W := W U pred(s)

until W = Ø

CMSC 631 39

Termination Revisited

  • How many times can we apply this step: ¦ temp := f s

s' pred(s) Out(s')) ¦ if (temp != Out(s)) { ... }

¦ Claim: Out(s) only “shrinks”

  • Proof: Out(s) starts out as top
    • So temp = Top after first step
  • Assume Out(s') shrinks for all predecessors s' of s
  • Then (^) s' pred(s) Out(s') shrinks
  • Since fs monotonic, fs( (^) s' pred(s) Out(s')) shrinks

CMSC 631 40

Termination Revisited (cont’d)

•A descending chain in a lattice is a sequence

¦ x0 > x1 > x2 > ...

•The height of a lattice is the length of the longest descending chain in the lattice

•Then, dataflow must terminate in O(nk) time

¦ n = # of statements in program

¦ k = height of lattice

¦ assumes meet operation takes O(1) time

CMSC 631 41

Least vs. Greatest Fixpoints

  • Dataflow tradition: Start with Top, use meet

¦ To do this, we need a complete meet semilattice

with top, of finite height

  • complete meet semilattice = meets defined for any set
  • finite height ensures termination

¦ Computes greatest fixpoint

  • Denotational semantics tradition: Start with Bottom, use join

¦ Computes least fixpoint

CMSC 631 42

  • By monotonicity, we also have
  • A function f is distributive if

Distributive Data Flow Problems

CMSC 631 43

  • Joins lose no information

Benefit of Distributivity

f

h

k

g

CMSC 631 44

  • Ideally, we would like to compute the meet over all paths (MOP) solution:

¦ Let fs be the transfer function for statement s

¦ If p is a path {s 1 , ..., s n }, let fp = fn ;...;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

CMSC 631 45

  • 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?

CMSC 631 46

  • Constant propagation
  • In general, analysis of what the program computes in not distributive

A Non-Distributive Example

x := 1 x := 2

y := 2 y := 1

z := x + y

CMSC 631 47

  • 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 48

  • A basic block is a sequence of statements s.t.

¦ No statement except the last in a branch

¦ There are no branches to any statement in the

block except the first

  • In practical data flow implementations,

¦ Compute Gen/Kill for each basic block

  • Compose transfer functions

¦ Store only In/Out for each basic block

¦ Typical basic block ~5 statements

Basic Blocks

CMSC 631 55

Elimination Methods: Conditionals

If

Else

Then

IfThenElse

CMSC 631 56

Elimination Methods: Loops

Head

Body

While

CMSC 631 57

Elimination Methods: Loops (cont’d)

  • Let f i = f o f o ... o f (i times)

¦ f^0 = id

  • Let
  • Need to compute limit as j goes to infinity

¦ Does such a thing exist?

  • Observe: g(j+1) <= g(j)

CMSC 631 58

Height of Function Lattice

  • Assume underlying lattice (P, <=) has finite height

¦ What is height of lattice of monotonic functions?

¦ Claim: finite (see homework)

  • Therefore, g(j) converges

CMSC 631 59

  • Elimination methods usually only applied to reducible flow graphs

¦ Ones that can be collapsed

¦ Standard constructs yield only reducible flow

graphs

  • Unrestricted goto can yield irreducible graphs

Non-Reducible Flow Graphs

A

C

B

CMSC 631 60

Comments

  • Can also do backwards elimination

¦ Not quite as nice (regions are usually single entry

but often not single exit )

  • For bit- vector problems, elimination efficient

¦ Easy to compose functions, compute meet, etc.

  • Elimination originally seemed like it might be faster than iteration

¦ Not really the case

CMSC 631 61

  • What happens at a function call?

¦ Lots of 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

CMSC 631 62

  • 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

CMSC 631 63

  • 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 write through x may affect any variable

whose address has been taken

  • In general, hard to analyze pointers

Data Flow Analysis and The Heap

CMSC 631 64

Data Flow Analysis and Optimization

  • Moore’s Law: Hardware advances double computing power every 18 months.
  • Proebsting’s Law: Compiler advances double computing power every 18 years.
  • We’ll focus on other uses of data flow analysis in this class (later in the semester)