CSC 355 Notes
Tuesday Dec 6, 2005
 

Topics:


AST

Examine structure of AST

Why other representations may be used


Code optimization by transformation

Sample Program

 
Module Dataflow;
 
From IO import ReadInt, WriteInt;
 
Var a,b,c : integer;
1
begin
2
 a := 5 ;
3
 ReadInt(b);
4
 if b=3 then
5
   c := a - b;
6
 else
7
   a := b;
8
   b := 3;
9
 end
10
 writeInt(c+b);
11
end dataflow;

Forward data-flow analysis

 
Module Dataflow;
Constant after line
 
From IO import ReadInt, WriteInt;
 
 
Var a,b,c : integer;
 
1
begin
{}
2
 a := 5 ;
{0}
3
 ReadInt(b);
{0}
4
 if b=3 then
{0,1}
5
   c := a - b;
{0,1,2}
6
 else
{0}
7
   a := b;
{}
8
   b := 3;
{1}
9
 end
{0,1,2} INT {1} = {1}
10
 writeInt(c+b);
{b}
11
end dataflow;
 

 

Backward data-flow analysis

 
Module Dataflow;
Variable value needed
again (after line)
 
From IO import ReadInt, WriteInt;
 
 
Var a,b,c : integer;
 
1
begin
{2}
2
 a := 5 ;
{0,2}
3
 ReadInt(b);
{0,1}UNION {1,2}={0,1,2}
4
 if b=3 then
{0,1}
5
   c := a - b;
{1,2}
6
 else
{1,2}
7
   a := b;
{2}
8
   b := 3;
{1,2}
9
 end
{1,2}
10
 writeInt(c+b);
{}
11
end dataflow;
{}

 

So what types of information does this provide?


Other types of optimizations


Code Generation


Register allocation

Register allocation by graph coloring (only one of many approaches)


Code Optimization


Basic blocks

Transformations


Common subexpressions

An example


Dead-code elimination


Renaming and code movement


Loop optimizations