Download Data Flow Analysis – Program Analysis and Understanding | CMSC 631 and more Study notes Computer Science in PDF only on Docsity!
Data Flow Analysis
CMSC 631 — Program Analysis and
Understanding
Fall 2007
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
CMSC 631
Control-Flow Graph Example
x := a + b y := a * b y > a a := a + 1 x := a + b
CMSC 631
Variations on CFGs
• Usually don’t include declarations (e.g., int x;)
■ But there’s usually something in the implementation
• Useful to have a unique entry and exit node
■ Treat them differently during dataflow analysis
• May group statements into basic blocks
■ A sequence of instructions with no branches into or
out of the block
CMSC 631
Graph Example with Entry and Exit
x := a + b y := a * b y > a a := a + 1 x := a + b
exit
entry
CMSC 631
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
Available Expressions
CMSC 631
• 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
CMSC 631
• What is the effect of each
statement on the set of facts?
Gen and Kill
Stmt Gen Kill x := a + b a + b y := a * b a * b a := a + 1 a + 1, a + b, a * b x := a + b y := a * b y > a a := a + 1 x := a + b
exit
entry
CMSC 631
Computing Available Expressions
x := a + b y := a * b y > a a := a + 1 x := a + b
entry
exit
CMSC 631
Computing Available Expressions
{a + b}
x := a + b y := a * b y > a a := a + 1 x := a + b
entry
exit
CMSC 631
Computing Available Expressions
{a + b}
x := a + b y := a * b y > a a := a + 1 x := a + b
entry
exit
CMSC 631
Computing Available Expressions
{a + b}
{a + b, a * b}
x := a + b y := a * b y > a a := a + 1 x := a + b
entry
exit
CMSC 631
Computing Available Expressions
{a + b}
{a + b, a * b}
{a + b, a * b}
x := a + b y := a * b y > a a := a + 1 x := a + b
entry
exit