0% found this document useful (0 votes)
207 views

R10 Compiler Design Unit-7 Code Optimization

POST FIX NOTATION,Implementation of TAC,Code –optimization,Optimization of basic blocks,DAG,Control Flow graph,Local and Global optimization

Uploaded by

StephenKarunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

R10 Compiler Design Unit-7 Code Optimization

POST FIX NOTATION,Implementation of TAC,Code –optimization,Optimization of basic blocks,DAG,Control Flow graph,Local and Global optimization

Uploaded by

StephenKarunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIT-7

In the analysis-synthesis model of a compiler, the front end analyzes a source program and
creates an intermediate representation, from which the back end generates target code. We assume
that a compiler front end is organized as in Fig., where parsing, static checking, and intermediatecode generation are done sequentially; sometimes they can be combined and folded into parsing.
Many of the translation schemes can be implemented during either bottom-up or top-down parsing.

In the process of translating a program in a given source language into code for a given target
machine, a compiler may construct a sequence of intermediate representations , as in Fig. Highlevel representations are close to the source language and low-level representations are close to
the target machine.
Source programHigh level IC. Low Level IC Target program
Figure: A compiler might use a sequence of intermediate representations
A low-level representation is suitable for machine-dependent tasks like register allocation and
instruction selection. Three-address code can range from high- to low-level, depending on the
choice of operators.

INTERMEDIATE TERMS:

1. Polish notation/postfix notation


2. AST (Abstract Syntax Tree)
3. TAC (Three Address Code)

The front end of the most compilers, translate the input sources into one of the following
intermediate forms.

POST FIX NOTATION:


In postfix notation an operator is placed at the right end of the operands.
1. a+bab+
2. if(f<6)
z=p/q
else if(f==6)
z=p*q
else
z=p-q
zf6<pq/f6 ==pq*pq-??=
Syntax tree/ Abstract Syntax tree (AST):
A syntax tree is a condensed (comprised) from of parse tree useful for representing language
constructs.
S
if B then S1 else S2
If-then-else
B
S1
S2
In a syntax tree, operator and keyword do not appear as leaves but rather or associated with the
interior node that should be the parent of those leaves in the tree.
+
*
4
5
3
Constructing Syntax Trees for Expressions: We construct sub trees for the sub expressions by
creating a node for each operator & operand.
mk node (op,p1,p2): Each node in a syntax tree can be implemented as a record with several fields.
In the node for a generator, one field identifies the operator and the remaining fields contains
pointers to the nodes for the operands.
1|SIET

Department of CSE

mk leaf (id, entry):


Creates an identifier node with label id and a field containing entry, a pointer to the symbol table
entry for the identifier.
mk leaf (num, Val):
Creates a number node with label num & a field containing val, the value of the number.
Eg:
a-4+c
P1 = mk leaf(id ,enter a)
P2 = mk leaf(num, 4)
P3 = mk node(-, p1, p2)
P4 = mk leaf(id, enter c)
P5 = mk node(+, p3, p4)
+

Num 4

id

Enter
c

The tree is constructed bottom up the function calls mk leaf(id, entry a) and mk leaf(num, 4)
constructs the leaves for a and 4, the pointers to these nodes are saved using p1 and p2. The call mk
node(-,p1,p2) then constructs the interior node with the leaves for a and 4 as children.

THREE ADDRESS CODE:


Three Address Code is a sequence of statements of the general form.
X = Y op Z.
Where X,Y,Z are names,constants or compiler generated temporaries. op stands for any operator
such as arithmetic operators, Boolean or logical operators. There is only one operator on the right
side of the statement.
For example, a+b+c
T1=a+b
T2=T1+c
Types of Three Address Statements:
STMT TYPE
FORMAT

DESCRIPTION

Assignment stmt
(binary )

X=Y op Z

Assignment stmt
(unary)

X=op Z

In this TAC stmt X gets


assigned the result of op Z.

X=Y

X gets assign the result of Y.

Copy stmt
Unconditional Stmtgoto L

In this TAC stmt X gets


assigned the result of
Y op Z .

a=-b
a=b
100 : a=b
101 :goto 103
102 : X=Y
103 : Z= c
if(X < Y)
--------------------------

if X relop Y goto L

condition is true then


jumps to lable L otherwise
execute the next TAC stmt

Indexed assignments

Y= X[i]

X[i] denotes the content


of a=b[i] a location which
is I memory units.
X gets assigned the
address of Y

X = &Y

a = b+c

control jumps to label L.

Conditional jump

Address assignment

EXAMPLE

a= &b;

Implementation of TAC:
There are three common implementations for the stack statement in a complier.
They are
1. Quadruples
2. Triples
3. Indirect triples
2|SIET

Department of CSE

In a compiler these statements can be implemented as a records with fields for the operators and the
operands.

QUADRUPLES:
A quadruples is a record structure with in four fields, which we call op,arg1,arg2 and result. The
op field contains an internal code for the operator. The three address statement X =Y op Z is
represented by placing Y in arg1,Z in arg2 ,X in result.
Ex:
a=b * - c + b * - c
t1 = - c
t2=b * t1
t3= - c
t4= b * t3
t5 = t2 + t4
a = t5
Op
arg1
arg2
arg3
0) unary(-)
t1
1)
*
t2
2) unaryt3
3) *
t4
4) +
t5
5)
=
a

c
b

t1

c
b

t3

t2

t4

t5

The contents of fields arg1,arg2 and results are normally pointers the symbol table entries
for the names represented by these fields. If so , temporary names must be entered into the symbol
table as they are created.

TRIPLES:
To avoid entering temporary names into the symbol table we refer to an temporary value by the
position of the statement that computes it. The three address statement can be represented by records
with only three fields . i.e., op,arg1,arg2.
The fields arg1 and arg2, for the arguments of op, are either pointers to the symbol table or
pointers into the triple structure. Since ,three fields are used ,this intermediate code format is known
as triples.
Sometimes it is called two address code.
op
arg1
arg2
o) unary 1) *
2) unary 3) *
4) +
5) =

c
b
c
b
(1)
a

(0)
(2)
(3)
(4)

INDIRECT TRIPLES:
Another implementation of three address code that has been consider is that of listing pointers to
triples, rather than listing the triples themselves. This implementation is called indirect triples.
stmts
11)
12)
13)
14)
15)
16)

(0)
(1)
(2)
(3)
(4)
(5)

Q: Write quadtriples for following statements.


if a<b || c<d then X=Y+Z
Sol: 0) if a<b goto (3)
3|SIET

Department of CSE

1)if c<d goto (3)


2) goto (5)
3) t1=Y+Z
4) X=t1
op
0)
1)
2)
3)
4)

arg1

Jlt
Jlt
jump
+
=

arg2

a
c

b
d

Y
t1

Result
3
3
5
t1
X

Code optimization:The motivation behind code optimization is to produce target programs with high execution
efficiency. The two constraints on the techniques used its perform code optimization are that they
must ensure semantic equivalence with source program and that improvement of the program
efficiency must be achieved without changing the algorithms used in the program.
For example, the assignment a=a+1; where a is real requires a type conversion of 1 to 1.0. An
optimizing compiler can avoid type conversion during the execution of the program by using the
constant 1.0 instead of 1. The cost of the programmers time spent in optimizing a program is
very high. Hence there is a need to
Front End

Control Flow
analysis

Data flow
Analysis

Transformation

Code generator
The code optimizer in the above fig. uses the intermediate code and control flow, data
flow, transformations to generate an optimized code.
The i/p and o/p to the code optimizer is intermediate code. However, the code is an
optimized set of intermediate stmts. In between, the code optimizer uses control flow analysis then
data flow analysis and then performs some transformations to generate optimized code.
Advantages:
1. The optimizations needed to implement high level constructs are made explicit in the
intermediate code, so it is possible to optimize them.
2. The intermediate code is independent of the target machine. Hence if the target machine is
changed, only code generation phase need to be changed.
Intermediate code optimization:The principal sources of optimization:1. Function preserving transformations:A compiler can improve a program in number of ways without changing the meaning of the
program. The following types of function preserving transformations.
1. Common sub expression elimination.
2. Copy propagation
3. dead-code elimination and
4. Constant folding
Common sub expression elimination:4|SIET

Department of CSE

Consider the input source and the corresponding intermediate code in TAC format in below
table. We call the Intermediate code shown in table as unoptimized intermediate code to differentiate
it from the version of Intermediate code after optimization using transformations.

5|SIET

Department of CSE

Input source

TAC

int sum_n, sum_n2, sum_n3;


int sum (int n)

(0) proce_begin sum


(1) t0 = n+1
(2) t1=n*t0;
(3) t2=t1/2
(4) sum_n= t2
(5) t3 =n+1
(6) t4 =n*t3
(7) t5 =2*n
(8) t6 =t5+1
(9) t7 =t4 * t6
(10) t8 =t7/6
(11) sum_n2 =t8
(12) t9= n+1
(13) t10 =n*t9
(14) t11 =t10 /2
(15) t12 =n+1
(16) t13 =n*t12
(17) t14 =t13 / 2
(18) t15 =t11 * t14
(19) sum_n3 =t15
(20) proce end sum

{
sum_n = ((n)*(n+1))/2;
sum_n2 = ((n)*(n+1)*(2*n+1))/6;
sum_n3=
(((n)*(n+1))/2)*(((n)*(n+1))/2;
}

The above table indicates that the computation made in quads (1) through (3), (12) through (14), (15)
through (17) are essentially the same. The intermediate code compute the value of the common sub
expression ((n*(n+1))/2 which is used in all the three summations. If we look further the common sub
expression ((n*n+1)) is computed 4 times in the stmts {1,2},{5,6},{12,13},{15,16}.
It is possible to optimize the intermediate code to have common sub expressions computed only once in
the function and then re-use the computed values at the second instance.
(0) proce begin sum
(1) t0 =n+1
(2) t1 =n*t0
(3) sum_n =t1/2
(4) t5 =2*n
(5) t6 =t5+1
(6) t7 =t1*t6
(7) sum_n2 =t7/6
(8) sum_n3 =sumn* sum_n
(9) proce end sum
This process of identifying common sub expressions and eliminating their computation multiple times in
the intermediate code is known as common-sub expression elimination.
Copy propagation:Copy propagation is another commonly used transformation in order to improve the intermediate
code. In copy propagation, the use of the variable y instead of x is propagated in the stmts following a
copy stmt x=y.
(0)
(1)
(2)
(3)
(4)
(5)
(6)
(7)

proc begin main


t0 =0
t1 =&arr1
t1[t0] =3
t2 =4
t3=&arr1
t3[t2] =4
proce end main

In the above table, there are two assignment stmts (are called as copy stmts). They are
a. the assignment stmt(1) where the tempor variable t0 is assign 0.
b. The assign stmt(4), where the tempor variable t2 is assigned 4.
The use of the value 0 can be propagated in the place of t0 in the stmts following the assignment at
stmt(1). In other words, the variable t0 can be replaced with 0 in stmt(3). Similarly, the variable t2 can be

6|SIET

Department of CSE

replaced with value 4 in stmt(6) following the assignment at stmt(4). The resultant intermediate code
after the copy propagates is shown below table.
(0)
(1)
(2)
(3)
(4)
(5)
(6)
(7)

proc begin main


t0 =0
t1 = &arr1
t1[0] =3
t2 =4
t3 =& arr1
t3[4] =4
proc end main

The stmt(3) where t0 has been replaced with 0 and stmt(6) where t2 has been replaced with 4.
In the above example, during the copy propagation, the use of constant 0 was propagated in the place of t0
end the constant 4 was propagated in the place of t2. This kind of copy propagation is called constant
propagation.
It is also possible to propagate the use of another variable instead of the existing one in copy
propagation. This is known as variable copy propagation.
(0) proc begin func
(1) d: = a
(2) if a>10 go to .L0
(3) go to .L1
(4) label .L0
(5) e: = d + b
(6) go to .L2
(7) label .L1
(8) e: = d + c
(9) label .L2
(10)
f= d*e
(11) go to .L3
(12) Label .L3
(13) proc end
The use of variable a can be propagated in the place of d following the assignment at stmt (1). The
intermediate code after the variable a is used in the place of d at stmts (5),(8) and (10).
Elimination of Dead Code:Copy propagation does not vastly improve the quality of intermediate code. However, copy
propagation facilitates other optimizing transformations to be performed on the resultant intermediate code.
We will now see how copy propagation facilitates an optimizing transformation called dead store elimination
to be performed on the resultant code.
(0) proc begin main
(1) t0=0
(2) t1 -&arr1
(3) t1[0] =3
(4) t2=4
(5) t3=&arr1
(6) t3[4]=4
(7) proc end main
In the intermediate cod shown in above table, the assignment stmt (1) can be eliminated, because t0 is no
longer used in any of the stmts following the assignment. similarly, the assignment stmt(4) can also be
eliminated. since t2 is no longer used in any of the stmts following the assignment. the stmts(1) and (4) are
example of dead store_ stmts that computer values which are not used in the prgm. Dead store can be
eliminated from the intermediate coda, since it has no effect on the results of the prgm. The resultant
Intermediate code after the elimination of assignment stmts (1) and (4) is shown in table.
(0) proc begin main
(1) t1=&arr1
(2) t1[0]=3
(3) t3=&arr1
(4) t3[4]=4

7|SIET

Department of CSE

(5) proc end main


The dead store elimination improves the speed of execution because we have less Instructions to execute at
the run time. The dead store elimination also reduces the amount of memory requiried for storing the code.
Constant Floding:Another common optimization performed on the intermediate code as known as constant floding. In
constant floding, the constant expressions in the input source are evaluated and replace by the equilent values
at the time of compilation. A constant expression involving only constants like, say 4*1, 2*0 and so on.
constant floding improves the speed of execution, since the calculations involving constant expressions are
performed at compile time, not at a run time.
(0) proc begin main
(1) t0=0*4
(2) t1=&arr1
(3) t1[t0]=3
(4) t2=1*4
(5) t3=&arr1
(6) t3[t2]=4
(7) proc end main
In the quad(1), the value 0*4 is computed, which is known to be 0 at the compile time itself. similarly, in
stmt(4), the value 1*4 is computed, which is known to be 4 at the time of compilation itself.
As an optimizing transformation, the constant can be floded and the resultant value computed at
the compile time itself in the stmt(1) and stmt(4). in stmt(1), 0*4 can be floded into 0 and in stmt(4), 1*4 can
be floded into 4 at the time of compilation itself. the resulting stmts from the constant floding are t0=0 for (1)
and t2=4 for (4) respectively.
(0) proc begin main
(1) t0=0
(2) t1=&arr1
(3) t1[t0]=3
(4) t2=4
(5) t3=&arr1
(6) t3[t2]=4
(7) proc end main
Loop optimization:Inner most loops are most important source of optimization. Even if a few stmts are
eliminated in the inner most loops, then code improvement is high. Five important techniques of loop
optimization are.
1) loop invariant code motion
2) reduce in strength/strength reduction on induction variables
3) Induction variable.
4) Loop Unrolling
5.Loop fusion
1) Code motion:Code motion reduces the number of instructions in a loop by moving instruction outside a loop. It
moves loop invariant computations i.e; those instructions or expression that result in the same value
independent of the number of times a loop is executed and place them at the beginning of the loop.
Ex:
while(x!=n-2)
{
x=x+2;
}
In the above loop, the value evaluated by the expression 'n-2' is independent of number of times the
while loop is executed.Now the expression n-2 can be written before the while loop begins as shown below
Ex:
m=n-2;
while(x!=m)
{
x=x+2;
}
Which is equivalent to the above example
2) Elimination of induction variable:An induction variable is a loop control variable or any other variable that depends on
the induction variable in some fixed way. If there are two o more induction variables in a loop then by the
induction variable elimination process all can be eliminated.
Ex: int a[10],b[10];
void func(void)

8|SIET

Department of CSE

{
int i,j,k;
for(i=0,j=0,k=0;i<10;i++)
a[j++]=b[k++];
}
In the above example there are three induction variables i,j and k, which take on the values 1,2,3------10.
Each time through the beginning of the loop. Suppose that the values of variables j and 'k' are not used after
the end of the loop then we can eliminate then from the func() by replacing then by variable 'i'.
After induction variable elimination, the above code becomes
Ex:int a[10],b[10];
void func(void)
{
int i;
for(i=0;i<10;i++)
a[i]=b[i];
}
The use of induction variables elimination reduces code space and also improves the run time
performance.
3)Reduction in strength:Strength reduction is the process of replacing expensive operations by equivalent cheaper operations on the
target machine. On many machines a multiplication operation takes more time than addition or subtraction.
On such machines, the speed of the object code can be increased by replacing a multiplication by a sub/add.
This is called reduction in strength.
Ex:
X=0
for (i=1; i<5; i++)
{
X=4*i;
}
This instruction x=4*i in the above loop can be replaced by equivalent addition instruction as x=x+4.
X=0
for (i=1; i<5;i++)
{
X=x+4;
}
4.Loop unrolling:
In This method the number of jumps and tests can be reduced by written the code two times.
int i=1;
while(i<=100)
{
a[i]=b[i];
i++;
}

written

int i=1;
while(i<=100)
{
a[i]=b[i];
i++;
a[i]=b[i];
i++;
}

5.Loop fusion:
In loop fusion method several loops are merged to one loop.
for i=1 to n do
a[i]=10
for j=1 to n do

b[j]=20
for i=1 to n do
a[i]=10
b[i]=20

Basic Blocks:
Basic Block is a sequence of three-address instructions. We begin a new basic block with the first
instruction and keep adding instructions until we meet either a j ump, a conditional jump , or a label on
the following instruction.
Algorithm: Partitioning three-address instructions into basic blocks.
INPUT: A sequence of three-address instructions.
OUTPUT: A list of the basic blocks
METHOD : First, we determine those instructions in the intermediate code that are leaders, that is, the
first instructions in some basic block. The rules for finding leaders are:

9|SIET

Department of CSE

1. The first three-address instruction in the intermediate code is a leader.


2. Any instruction that is the target of a conditional or unconditional jump is a leader.
3. Any instruction that immediately follows a conditional or unconditional jump is a leader.
Optimization of basic blocks:The code improving transformations that can be applied on basic blocks are
1. Structure preserving transformations
2. Algebraic transformations
1. Structure preserving transformations:Structure- preserving transformation includes
1. Common sub expression elimination
2. Dead code elimination
[These two are already explained and written
before]

2. Algebraic transformations:The quality of the Intermediate code can be improved by taking advantage of algebraic identities. An
Algebraic identity is a relation that holds true for all values of the symbols involved in it. Some of the
common algebraic identities that can be used to improve the intermediate code as shown below
Name of the identity
Additive identity
Multiplicative identity
Multiplicative with 0

Example
x+0=x
x*1=x
x*0=0

The algebraic identity is typically applied on a single intermediate code stmt and transformed to a copy
stmt. Some of the examples are:
Ic stmt
Identity applied
Ic stmt after transformation
Y=x+0
Additive identity
Y=x
Y=x*1
Multiplicative identity
Y=x
Y=x*0
Multiplicative with 0
Y=0
Example:
(0) Proc begin
func
(1) t0=&xyz
(2) t1=0
(3) t2=index*4
(4) t1=t2+t1
(5) t0[t1]=34
(6) proc end
func

(0) proc begin


func
(1) t0=&xyz
(2) t2=index*4
(3) t1=t2+0
(4) t0[t1]=34
(5) proc end
func

(0) proc begin


func
(1) t0=&xyz
(2) t2=index*4
(3) t1=t2
(4) t0[t1]=34
(5) proc end
func

(0) proc begin


func
(1) t0=&xyz
(2) t2=index*4
(3) to[t2]=34
(4) proc end
func

(A)unoptimized IC

(B) optimized IC
(C) optimized IC
(D) optimized IC
after copy
after applying
after copy
propagation & dead
algebraic identity
propagation & dead
code elimination
store elimination
In the optimized code (column A) of above table, there is an opportunity to do copy propagation
following the assignment at stmt (2). i.e. (t1=0). The use of t1 can be replaced with 0 in the stmt (4).
After the copy propagation, the copy stmt (2) is dead and can be eliminated. The intermediate code after
copy propagation and dead code elimination is shown in column (B) of the table.
In the IC after copy propagation in column B, we can apply the additive identity on the stmt (3)
and transform it to a copy stmt t1=t2.
The IC in column (C) offers an opportunity to perform copy propagation following the assign
stmt (3). Then the use of t1 can be replaced with t2 in stmt (4). The copy stmt at (3) becomes dead store
and hence can be eliminated.

DAG:
Optimization of basic blocks is performed using Directed Acyclic graphs. The DAG is a data
structure used for implementing optimizing transformations on the intermediate code with in a basic
block.
A DAG for a basic block consists of two kinds of nodes.
1. Leaf nodes.
2. Interior nodes.
The leaf nodes are the nodes that do not have children. The leaf nodes are labelled by unique identifiers
or constants. The label for a node is written inside the bubble representing the node.

10 | S I E T

Department of CSE

The interior nodes are the nodes that have children. The children could be either another interior node or
a leaf node. The interior nodes are labelled by an operator.
The leaf nodes and the interior nodes can have an attached identifier list (shown outside the bubble. The
attached identifier list represents the identifier holding the computed value in the case of interior nodes.
Ex:

a=b+5
e=d*a

The above fig contains two interior nodes i.e. +,* and three leaf nodes (d, b, 5). The
attached identifier list of the operates node+, contain identifier a. The identifier
a holds the computed value (b+5). Similarly e attached to the node * it holds
the computed value d*a.
Applications of DAG:1. We can detect the common sub-expression within a block.
2. We can determine names evaluated outside the block but are used inside the
block.
3. We can determine stmts whose value is computed inside the block but can be
used outside the block.

2.Explain the following expression can be converted in a DAG.


a+b*(a+b) +c+d.
A DAG is constructed from TAC.
t1=a+b
t2=b*t1
t3=a+t2
t4=t3+c
t5=t4+d

DAG for a+b*(a+b) +c+d.

Inline expansion:
Inline expansion, or inlining is a compiler optimization that replaces a function call site with the body
of the callee. This optimization may improve time and space usage at runtime, at the possible cost of
increasing the final size of the program. Ordinarily, when a function is invoked, control is transferred to
its definition by a branch or call instruction. With inlining, control drops through directly to the code for
the function, without a branch or call instruction.
In lining improves performance in several ways:
It removes the cost of the function call and return instructions.

11 | S I E T

Department of CSE

Eliminating branches and keeping code that is executed close together in memory improves instruction
cache performance by improving locality of reference.
Once inlining has been performed, additional intraprocedural optimizations become possible on the
inlined function body.
Inlining may also decrease performance in some cases - for instance, multiple copies of a function may
increase code size enough that the code no longer fits in the cache, resulting in more cache misses. Some
languages (for example, C and C++) support the inline keyword in function definitions.
A programmer might inline a function manually through copy and paste programming, as a one-time
operation on the source code. Once the compiler has decided to inline a particular function, performing
the inlining operation itself is usually simple. The compiler can do inlining on either a highlevel intermediate representation (like abstract syntax trees) or a low-level intermediate representation.
Here is a simple example of inline expansion performed "by hand" at the source level in the C
programming language:
int pred(int x) {
if (x == 0)
return 0;
else
return x - 1;
}
Before inlining:
int f(int y) {
return pred(y) + pred(0) + pred(y+1);
}
After inlining:
int f(int y) {
int temp = 0;
if (y == 0) temp += 0; else temp += y - 1; /* (1) */
if (0 == 0) temp += 0; else temp += 0-1; /* (2) */
if (y+1 == 0) temp += 0; else temp += (y + 1)- 1; /* (3) */
return temp;
}
The compiler may follow this sequence of steps:
The temp += 0 statements in the lines marked (1), (2) and (3) do nothing. The compiler can remove
them.
The condition 0 == 0 is always true, so the compiler can replace the line marked (2) with the
consequent, temp += 0 (which does nothing).
The compiler can rewrite the condition y+1 == 0 to y == -1.
The compiler can reduce the expression (y + 1) - 1 to y (assuming wraparound overflow semantics)
The expressions y and y+1 cannot both equal zero. This lets the compiler eliminate one test.
The new function looks like:
int f(int y) {
if (y == 0)
return y;

/* or return 0 */

else if (y == -1)
return y - 1;

/* or return -2 */

else
return y + y - 1;
}

12 | S I E T

Department of CSE

Problems:
inlining is usually disadvantageous except for very small functions.
If code size is increased too much, resource constraints such as RAM size may be exceeded
Control Flow graph:A flow graph is a graphical representation of three address statements. It is used to show the flow of
control information to the set of basic blocks of a program. It consists of nodes and edges. A node of a
flow graph is basic block that performs some computations. An initial node consists of a block whose
leader is the first statement. There is a directed edge from one node to another node representing the
flow of control between blocks.
INPUT SOURCE
TAC
Int v1, v2, v3, v4, v5
(0) proc begin
Int func (int c)
(1) v3=v1+v2
{
(2) If c>100 goto L0
V3=v1+v2;
(3)goto L1
If(c>100)
(4) Label L0
{
(5) V4 =v1+v2
V4=v1+v2;
(6) v1 =0
V1=0;
(7) Label L1
}
(8) v5 =v1+v2
V5=v1+v2;
(9) proc end
}
Proc begin
V3 = v1+v2

B0

If c>100 goto Lo
(3) goto L1

B1
(4) Label L0
(5) V4 =v1+v2
(6) v1 =0

(7) Label L1
(8) V5 =v1+v2

B3

(9) Proc end

B4

B2

The about the flow of entire procedure i.e. the possible blocks to get executed after a particular
block B is represented in a graph called as the flow graph. Each node in a flow graph is a basic block. In
a flow graph, there is a directed edge from the block B1 and B2, only if
(a) There is a conditional jump to the block B2 from the last quad of B1 (or)
(b) The flow of control goes to B2 after B1 sequentially.
The starting node in a flow graph is called as initial node. This is the basic block
whose leader is the first TAC stmt in the procedure. The node B 0 is the initial node. The flow of control
can go to back B1 or B2 from the block b0.
Point and Path
In the below fig different points of the basic block B0 of the previous example. We
can see that there are 4 points in basic block B0, given by p0-b0, p1-b0,p2-b0,p3-b0. We have suffered the
block number b0 to the point name to clearly identify that the point belongs to the block B0. They are 3
lines and 4 points in the basic block B0.Ingeneral, there are (i+1) points for a basic block containing (i)
lines. A part of the flow graph containing the basic blocks B0, B1 , B2 and B3 shows all the points in each
of those basic blocks.

13 | S I E T

Department of CSE

P0_b0
Proc begin

B0

P1_b0
V3 =v1+v2
P2_b0
P3_b1
If c>100 goto L0
B1
(3) goto L1
P4_b1
3
0
P7_b3
(7) Label L1
P8_b3
(8) V5 = v1+v2
P9_b3

P4_b2

(4)Label L0
P5_b2
(5) V4 =v1+v2
P6_b2
(6) V1=0
P7_b2

B3

B2

P9_b4

(9) Proc end


P10_b4

B4

A path is a sequence of points in which the control can flow. For example, there is a path between the
points p0_b0 and p6_b2 given by the sequence of points, p0_b0, p1_b0, p2_b0, p3_b0, p4_b2, p5_b2, p6_b2.
There is no path between p3_b1 and p6_b2.
Local and Global optimization :- (out of syllabus )
Local and global optimization uses a number of function preserving transformations. They are
1. Common sub-expression.
2. Copy propagation.
3. Dead code elimination.
4. Constant folding.

14 | S I E T

Department of CSE

You might also like