You are on page 1of 21

COMPILER DESIGN

5.4
PART-B
sOLUTIONS
WITH
QUESTIONS
ESSAY OPTIMIZATION
sOURCES
OF
PRINCIPAL
5.1 THE
Q10. Discuss the following concepts,
Machine independent
optimization
(i) Model Paper-, Q10
(i) Function-preserving transformation.
Answer characteristic.
Machine Independent Optimization on the source languagevarious
target
machine) butldepends or target coae, machin
independent optimization isindependent of in order to improve
the efficiency
Machine program structure
technique uses appropriate
pml/zation
code optimization teehniques are,
(a) Local optimization
(b) Global optimization.
the inrormation regardino
not require
(a) Local Optimization technique do
basic block,This
out withina single
the optimizations carried
hese are of this technique is simple.)
control. Thus, implementation
the data and flow of
analyS1S is also known as
b) Global Optimization basic block) This
carried out across basic blocks, instead ofsingle blocks. Thus,
implementation of this
across basic
hese are the optimizations additional analysis is required
data-flow analysis. In this technique,
technique is complex. )
Function-preserving Transformations
the function it computes.
i) code without modity1ng
transformations helps in improving a program can be done by the following,
Semantic preserving transformations
Semantic preserving transformation (or) function preserving
common subexpression.
(a) Eliminating global
code
(b) Eliminating dead
()Applyingconstant folding
(d) Applying copy propagation.
(a) Eliminating Global Common Subexpression whose value was
subexpressions from the code. An expression
The code can be improved by eliminating
common
avoided
not changed, since its computation can be
of variables in the expression are
previously computed and the values
value.
by using the earlier computed
to recompute it
Example
Consider the following sequence of code,
a=b * c
z=b *c +d-c
In the above code the assignment to z have the common subexpression b* c. Since its value is not changed after the pom
follows:above code
it was computed, and its use in the expression z, we can avoid recomputing it by replacing the as
t b *c
a
Z +d-c.
expression if the value of one of its variable is changed.
However, it is not possible to eliminate an
on the TITLE COVER before you buy
Look for the 5IA GROUP LOGO
5.5
INIT-5 Machine-Independent Optimizations
Example
Consider the following code
z=bt+d-c
b° c. T hererore,
e r e , the expression b* c is not common because the value of variable e is changed after computing
cannot eliminate this expression.
Eliminating Dead Code
(b)
in the program ncn,"
never used anywhere
A piece of code which is not reachable, that is the values it computes is "
to a variable results
in
dead couc,
satd to be dead code and can be removed from the program safely. An assignment dead c o a c u
thcc
to a variable is a
the value of this variable is not usedin the subsequent program. Also an assignment
program.
is always another assignment to the same variable before its value is used in the subsequent
makes the copy statements into dead code.
Copy propagation often
Example
Consider the following code,
ab+c
lcopy statement
d=c
e b +d 3
-
propagation
followed by c o m m o n
program, then using copy
of d is not used in the subsequent to dead variable
d. Hence the
Suppose that the value elinmination removes the assignment
followed by dead code,
subexpression elimination
above code becomes as:
tb+c
d=-3
(c) Applying Constant Folding
Statements like,
can be evaluated at compile time itself.
constants, then these expressions
Ifexpressions involve
x=1;
X:*+1;
int maximum = 10;
Maximum
2
(d) Applying Copy Propagation
the
the use of variable r is replaced by
form r =y then in copy/variable propagation
If there are copy statements ofthe of the variable is changed after
this
The copy propagation is possible if none
variable y in the subsequent expressions.
arrangement. - Sio GROUP
NTT
cOMPILER DESIGN JNTU-HYDERABAD
ADI
Example Example
Consider the Consider the following while loop.
following code.
while(x != n-2)
ab+ c

d=c
copy statement x = x+2;

n -
2 1s a loop-invarian
eb+ d -3 Here the expression
the value evaluated by this expression
ion is
computation i.e.,
We may independent of the number oftimes the while loop is cuted
notice that the code has
bc and b +
d, if
common subexpressions In other words the value of n remains unchanged. The codo
we replace the variable d by the relocation places the expressionsn-2 before the while lon
variable both have the same
c as loop
value. Common begins as shown below:
Subexpression treats these expressions as different. After
applying copy propagation, the above code m=n-2,
becomes:
while(x != m)
a b + c

XX+ 2;

Which is equivalent to the above example.


e=b+c-3
Elimination of Induction Variables
By copy propagation we can eliminate the assignment An induction variable is a loop controi variable or
to d.
any other variable that depends on the induction variable in
Q11. What is a loop some fixed way. It can alsobe deñined as a variable whichis
optimization? Explain various incremented or decremented by a fixed number in a loop each
techniques in loop optimization.
time the loop is executed. Induction variables are oftheform
Answer i=i t c(where c is a constant).
Modei Paper-l, Q10(a)
If there are two or more induction variables in a loop
Loop Optimizatio then by the induction variable elimination process all can be
eliminated except one.
The major source of code optimization is loops,
especially the inner loops. Most of the run time is spent inside Example
Consider the part of a program.
.

the loops which can be reduced by reducing the number of


instructions in an inner loop. int a[10], b[ 10];

There are three techniques namely, void fun(void)

1. Code motion
int i. j. k;
2. Elimination of induction variables
forti 0,j =0, k =0; i< 10; i+)
3. Strength reduction.
ajt+]=b[k++]
Code Motion
return;

Code motion reduces the number of instructions in a loop


instructions outside a loop. It moves loop-invariant In the above example there are three induction variaoe
by moving , j and k which take on the values 1, 2, 3,..10 each n
instructions or expressions that result through the beginning of the loop. Suppose that the valuE
.computations i.e., those
of the number of times a loop of the loop then "
in the same value independent variables and k are not used after the end replacing
them at the beginning of the loop. The can eliminate them from the function fun() by
is executed and places the
becomes entry for loop. by variablei.
relocated expressions
on the TITLE COVER before you buy
LOok for the SIA GROUP LOGO
5.7
UNIT-5 Machine-Independent Optimizations construots 1.e.,
control flow
with
After induction variable elimination, the above code Consider a grammar
statements.
and sequential
becomes as, do-while, if-else, assignment Stmt
int a 10]. b[ 10]: Expr | Stmt;
Stmt| if Exprthen
Stmt-identifier:
void fun(void) else Stmt | do Stmt while Expr
identifier
Expr>identifier + identifier |
an
statement
and Expr
represents a be
int i Where, Stmt of 'Stmt'
can
for the yields
for(i 0; i< 10; it+) expression. The flow graphs would replace
the
these flow graphs
drawn. However, each of
alil b[il: general flow graph.
return;
> Dummy
block
The use of induction variables elimination reduces code
the number of additions or subtractions in a (Stmt
space by reducing
the run time performance.
loop. It also improves
Strength Reduction
3.
Strength reduction is the process of replacing expensive -Dummy
on the target block
operations by equivalent cheaper operations takes
machine. On many machines a multiplication operation
or subtraction. On such machines Flow Graph
more time than addition Figure: General
code can be increased by replacing
the speed of the object Expression
reduction in
=
identifier:
a subtraction. This is called Stmt-
a multiplication by i)
the form,
strength. When stmt takes
identifier: =
Expression
Example Stmt
Consider the following code is replaced by,
The general flow graph
for(i= 1; i<5; it+)
X4i Identifier:=Expression
4 *i in the loop can be replaced by
The instruction r =
equivalent addition as
instruction x = r +4.
is the
reduction in strength
Another example of of two
multiplication by a power Example
replacement of a fixed point statement,
by the cheaper shift operation. For the example assignment
TO DATA-FLOW
5.2 INTRODUCTION
d=et g
ANALYSIs
of structured The flow graph 1s,
data flow analysis
Q12. Explain about
programs.
Model Paper-i, Q11(b)
Answer
of statements DEF:d:=e+g
Definitions reaching the starting and end
as a useful property
can easily be
indicated in the flow graphs
constructs i.e., only one starting point
exhibited by control flow for
control and only one ending point
for the entry of flow of
its exit.
indicate the flow of control from The data flow equations for the
above fow graph are,
Flow graphs are used to
one point to another point.
Few small circles called the dummy
contain any statements) are used
to GEN stmt]= {DEF;
blocks (since they do not
set of nodes n' in the
indicatethe entry and exit of control.A
KILL[stmt) = D, - {DEF}
* also/includes the dominating
fowgraph is called aregion: "header". KILL{stmt])
GEN|stmt}u (IN[stmt]
-
node in the region called the
OUT[stmt =
STUDENTS SIA GROUP
SPECTRUM ALIN-ONE JoURNAL FOR ENGINEERING
5.8
cOMPILER DESIGN IJNTU-HYDERABADI
Where. The data flow equations for the above flow graph are
e t of all
definitioñs for variable'd in the program GEN Stmt= GEN[Stmt,]U GEN[Stmt]
GEN[Stmnt] -

Set of
definitions KILLStmt]= KILL[Stmt,]
n KILL[Stmt,]
generated by stmt
KILL[Stmt] -

Set of definitions
killed by stmt. IN[Stmt,]- INJStmt)
he
assignment statement d: +g is the definition OUT[Stmt]=OUT[Stmt,]u OUT[Smt,]
DE
=
e
of'd.
Irrespective
starting of the statement,ofitwhether definition DEF' reaches either branches of
When a definition is generated by
statement. Moreover, surely reaches the end of
by 'DEF* tne if-statement then surely that definition
reaches the end of the
d are killed. Dralall other prior definitions of
prior definitions or
statement. Moreover, the variable defined by DEF must be
in order to kill definition
(ii) Stmt Stmt,; Stmt, killed oneither branches of "if'

When Stmt takes the DEF.


form
do Stmt, while Expr
Stmt: Stmt,; Stmt, (iv) Stmt
When Stmt takes the form,
The general flow graph becomes,
Stmt > do Stmt, while Expr

Then the general flow graph becomes,


Stmt,

Stmt
(Stmt,
The data flow equations for the above flow graph are,

The data flow equations for the above flow graphs are, GEN[Stmt]= GEN[Stmt]
GEN[Stmt =GEN[Stmt,]u(GEN[Stnt,]-KILL[Stmt,) KILLIStmt]) = KILL[Stmt]

KILL[Stmt] =KILL[Stmt,]w(KILL[Stmt,]-GEN.Stmt.) IN[Stmt]= IN[Stmt]u GEN[Stmt,]


IN[Stmt,]= IN[Stmt] OUT[Stmt= OUT[Stmt,]
IN(Stmt,) = OUT(Stmt)
The GEN and KILL rules for loops are simple, if DEF
is generated by Stmt then it is also generated by Stmt,. And if
OUT[Stmt] =OUT[Stmt,] DEF is killed by Stmt then it is also killed by Stmt.
The cascade of statements Stmt = Stmt,; Stmt, has
Q13. Briefly explain the following,
a complicated rule for GEN, i.e., DEF is generated by Stmt
only if it is generated by Stmt,. Moreover, if DEF is generated ) Reaching definitions
by Stmt, but not killed Stmt,, then it is guaranteed that DEF
reaches the end of Stmt. Similarly, the rule for KILL is applied. (i) Live variable analysis.
Stmt > if Expr then Stmt, else Stmt, Answer
(ii)
When stmt takes the form, (i) Reaching Definitions
A definition d of variable Vis said to reach a pointp, if
Stmt-> if Expr then Stmt, else Stmt,
there exist a path (P")from dtop along which Vis not redefined.
Then the general flow graph becomes,
Example
Consider the following figure,
if Expr goto Stmt,
B B

4:p-4 d,:9p+2
Stmt, Figure: Reaching Definition from Block B, to Block B,
Stmt
The above definition is said to be reaching definn
from block B, to block B,
MT-5Machine
-Independent Optimiz
nizationss tor
BGN.B]
that
Le
ive riable Aanlysis initially
assumes

definitions
information

algorithm
he reaching
is as
tollows.

hle (say a) is said to be alive h B. It propagates

at point p if, the algorithm the


not killed
A vari.

as they are
block B on
s 1Ong each
value used for
1ts current is in the flow
graph beginning : GEN[B]
for all
B.
(a) 1 nitialize END.B]
BGN[B] ¢
at the point p. that initially of the
assumption in any
no
changes
there are
There exist a path (say p ) trom start point p to end until
following
( b ) T

DO
noint (E) otherwise it is said to be in dead state. END[B] sets.
calculate
block B
i) For each
ENDIPI
BGNB:
nsider the following figure. Predecessor
B
B

:=
END[B]
a15 PREV _END[B] KILLIB|);
u(BGN[B]
END[B]:= GEN[B] END[BJ|
changes in
are any
and END.B].
whether
there
Check
(in) PREV_END[B]
the
comparing terminating
b:-
10 sets by
result this helps
in
Record the

B; algorithm. algorithm
us-
above
B of the
the working
Q15. Explain example.
9a+z B, suitable
ing a

Answer:
shown
below.
Consider the graph

a:q+z d:x i + 2
GEN[B1]
=

{d,. d, d,}
d,}
B1d y =k KILL[B1] {d,, d,, d.
Figure d: Z a

that a variable a is live


be noticed from above figure,
It can
and Block B, and is
killed at block B
to Block B,
iom block B, defini-
an algorithm
to compute reaching
B2
d:xXx+ GEN[B2]
KILL{B2]=- {d.
{d,,dd, d,, d
Q14. Write d: y:y-
for a flow graph.
tion informatory

Answer: V is a B3 d:zb d:xcB4


a value
to a variable
that assigns to reach
A statement variable V is said GEN[B4]= {d,}
definition d of a
which V is GEN B3]=id}
definition of V. A KILL[B4] {d,,d,}
from d to p along KILL[B3] - {d,}
exists a path
a point P,
if there is to compute
definition problem
redefined. The reaching which reach the Figure: Flow Graph
not
d e f i n i t i o n s of
each variable
for each block, all sets computed
block. GEN and KILL
eginning of the definitions are
as
The above figure shows
for reaching there a r e s e v e n
equations the above flow graph
The data flow for each block. In
and z.
the variables x, y
follows,
END[P]
definitions d, d . d, defining detinitions. In
BGN[B] the set of
P is a bit v e c t o r s to represent
Predecessor
We use
and only if the
definition d,
i contains 1 if
bit vector, the bit
of B

= GEN[B]
(BGN[B] -KILL{B]) a

END[B] indicates is in the set.


for BGN[B] initializes END[B]
in equation of the algorithm
union operator (U) reaches the
end of any The first step of
The if it that initially
a block
reaches block B on the assumption
nat as definition GEN[B] for each are shown
Its predecessors. BGN[B]=ofor all
B. These initial
values of END{B]
below.
table
in the given
Algorithm definitions
takes
first
computing and KlLL[B]
reaching
enters into the while loop and starts
Ihe algorithm for The algorithm and
the sets GEN[B] of the takes B1, B2, B3
which for loop B
hgraph
as input for
block B. The
output
iteration. Suppose in the inner
each for each
nputed for
have been compi computed B1.
and END[B] B4 in that order with B
algorithm is thesets BGN[B]
-SIA GROUP 2
block B. ENGINEERING
STUDENTS

JOURNAL FOR
ALL-IN-ONE
TRUM
nouo
initial
5.10 since
BI is
an
0000
set to true
rue.
represented
by 000 Is not
BGN[BI]=¢ repr
" p
variable any
changes
the
END[BI]
END[BI]=GEN[B] END[B1]=
PREV
since
2 o m p u t e da s f o l l o w s ,
Which also remains equ to GEN[BI]
yal
sets are
compu
END
BGN and
hich
Next for loop takes B =
B2 for
END[B4||
END[B3] U
BGNIB2]= ENDIB1] U
001
= 1110000 +0000 010 +0000
= 1110 011
KILL[B2|)
END[B2] GEN[B2] U (BGN[B2]
-
=
1100 001)
0001 100 +(1110 011
-
computations.
= 0011 110 these
shows
below
the table given
Y B G N
and END sets for B3 and B4 are
computed
Iteration 2
Iteration1
Initial Values ENDIB]
BGN[B] END[B]
Block ENDIB] BGN.B]|
ENDIB]BGN|B]
B
BGNIB] 1110000 0000000 1110000
0000000
BI 0000000 1110000 0011110 1111111| 0011110
1110011
B2 0000000 0001100
0001110
0011110| 0001110
0011110
0000000 0000010
B3
0011110 0010111
0011110 0010111
B4 0000000 0000001
Table: Computation of BGN and END Sets
for Example Flow Graph
is generated in B4 and definitions
At the end ofthe first iteration the setEND[B4]
=
0010111 represents that the definition d,
d, d, and d, reach B4 without being killed in B4.
The second iteration of while loop starts. In this iteration there are no changes to any of the END sets so the algorithm
terminates.
a16. Write the iterative algorithm for reaching definition. Compute in and out for the following figure.
dl:i=m-1|
BI d2:jn
d3:a =ul
d4:i i+ 1
B2 d5:j=j-1
d6:a = u2
B3
d7:i=u3
B4
Figure
Answer
Model Paper-1, Q10(a)
Iterative Algorithm for Reaching Definition
For answer refer Unit-V, Q14.
Given that,
Look for the SIA GROUP LOGO o n the TITE CO
5.11
UNIT-5 Machine-Independent Optimizations
dl:i m- 1| 2fide(Umo
B1 d2:jn
B:atul

d4:iit
B2 d5:=j-1

B3 d6:a=u2
B4d7:i=u3

Figure
and
d, defining the variables i,j
a.
In the figure of flow graph, there are seven definitions d, d, . . .

The GEN and KILL sets for each block are as follows,

For B1: GEN[B1]={d, d, d,}


KILL[BIJ= {d,d. d, d,}
For B2: GEN[B2] = {d, d}

KILL[B2]= {d, d, d,
For B3: GEN[B3]= {d,}
KILL[B3]= {d,}
For B4: GEN[B4]= {d,)
KILL[B4] {d4}
bit vectors to represent the set of definitions. In
a bit vector, the bit i contains 1 if and only» if the detinition d, is
We use

in the set.

for each block B on the assumption that initially BGN[B]=


The first step the algorithm initializes END[B] GEN[B]
=

of the table.
for all B. These initial values of END[B] are shown in

First Iteration
in the inner for loop B takes B1, B2, B3 and
The algorithm enters into the while loop and startsfirst iteration. Suppose
B4 in that order with B = B1.

initial node which have predecessors and


BGN[B1]= ¢ represented by 000 0000 since Bl is an no

END[B1]= GEN[B1]- 1110090

since PREV_END[B1]=END[B1] the variable any changes is not set to true.


Which also remains equal to GEN[B1]
Next, for loop takes B B2 for which BGN and END sets are computed as follows,

BGNIB2]+ END[B1]U END[B3] U END[B4]


= GEN[B1]u 0000000 0000000

= 1110000 +0000000 +0000000

-1110000

ENDIB2]= GEN{B2] u(BGN[B2] KILL{B2])


-

= 0001100+(1110000- 1100001)

0001100+0010000

= 0011100
S.12
cOMPILER DESIGN IJNTU-HYDERABAn
ADI
Now, for
loop takes BGN and END sets are
computed as follows,
BGN B3] =

END{B2]
=0011100
N
END[B3] =GEN[B3|U (BGN[B3]- KILLIB
0000010+ (0011100-0010000)
0000010 +0001100
=0001110
W, IOr
loop takes B B4 for which BGN and END sets are compureu
a *
BGN[B4]- END{B2]
= 0011100

END B4] GEN[B4] U(BGN[B4] KILL[B4])


=
-

0000001 +(0011100- 1001000)


= 0000001 +0010100

=0010101
Second iterstion
The algorithm enters into the while loop again and starts second iteration. Here, we consider the BGN and END sets of
each biock obtained in first iteration.

For B B1; BGN[B1]= ¢ = 0000000

ENDIB1]= GEN[BI] eNou


1110000

For B B2; BGN[B2]=ENDIB1]U ENDIB3] U END[B4]


= 1110000+0001110 +0010101

= 1111111

ENDEB2]= GEN{B2] U (BGN [B2] - KILL[B2])

0001100 +(1111111 1100001)


= 0001100+0011110

= 0011110

For BB3; BGNIB3]= END[B2]


=0011110

ENDIB3]= GEN.B3] U (BGN[B3] KILL[B3])


-

= 0000010+ (0011110-0010000)

= 0000010+0001110

0001110

For B B4; BGN[B4]=ENDIB2]


= 0011110

TLE COVER hefore vou buy


5 achine-Inde 5.13
mizations
END[B4]= GEN[B4] U
(BGN[B4] KILLB41)
-
0000001+(0011110- 1001000)
0000001 +(0010110)
=0010111
d lteration
of eacn
lgorithm enters into the while loop consider the
BGN and
END sets
again and starts third iteration. Here,
htained in Second iteration.
we
ForB= Bl; BGN[B1]= ¢=0000000
ENDIBI]= GEN[B1]
= 1110000
.R=B2;
For E
BGN[B2]=END[B1J U
END[B3] U ENDB4]
= 1110000 +0001110+0010111
= 1111111
END B2]- GEN[B2] U
(BGN[B2] KILL[B2))
-
0001100+ (1111111 - 1100001)
0001100 +0011110
= 0011110
ForB-B3, BGN[B3]=END[B2]
= 0011110
END[B3]= GEN[B3] U(BGN[B3] -KILL[B3])
0000010+(0011110-0010000)
= 0001110
For BB4; BGN[B4]= END[B2]
= 0011110
ENDIB4]=GEN[B4] U(BGN[B4]-KILL[B4)
000001 + (0011110- 1001000)
= 000001+0010110
0010111
the END sets. So, the algorithm terminates.
In the third iteration of while loop there are no changes to any of
The table shows all the BGN and END sets for the given flow graph:
Initial Values Iteration 1 Iteration 2
Block
ENDIB] BGN[B ENDIB BGN B ENDIB]
B BGNB]
BI 0000000 1110000 0000000 1110000 00000001110000
0000000 0001100 1110011 0011110 1111111 0011110
B2
0000000 0000010 0011110 0001110 0011110 0001110
B3
B4 0000000 0000001 0011110 0010111 0011110 0010111
Table: Computation of BGNand END Sets for Given Flow Graph
ETRUM Au-IN-ONE JOuRNAL FOR ENGINEERING STUDENTS -SIA GROUP
5.14 cOMPILER DESIGN JNTU-HYDERABADI

Q17. Discuss briefly 'SS


about avaiiable express
essions. S:Aset ofvalues
Answer A:A binaiy operator
called meet operator.

Available Expressi»n F:A set of transfer


functions F:S > S. F shouid
constant transier
the nodes
funciions for
An ncorporate
expression a +
b is said to be EXIT in data-fow graph
available at point Pl, labelled with ENTRY and
1) All possible paths from initial node of
flow graph
Feaching P computes a+b. (i) Semilatice
as algebraic siructure (S,
(11).1here exist no assignment toeither a or b
reaching. Asemilattice can be deined s u c h that for all
a meet operator
with set ofvalues 'S' and
) The operands gets modified before its use the iollowing properties
elements ofr, y and z in "Sit hoids
Examp?e ldempotent: r Ar =x
a
Consider the toliowing figure,
( Commutative:r^y=y^
B
Associative: x
^
(y ^z)
=
(r ^y) ^z.
(c
4 A simiattice contains top e'ement 't VxES, x ^T=r
and a bottom element 1'VrES, 1^ x =r.

Partial Order
B B. A reatien Srepresents a patial-erdar.on set S" V x, y
P y+z [| t,4 *i and z e S i saiisties the following properties
a Reflexive: x Sx

Antisymmetric : lixSy and yErthen x =y


B
(CTransitive: Ifr Sy and y : then a S 2
x [t,]
The ordered pair (S, ) refers to poset or partially ordered
set. A poset can also have a relalion '< which is given by x <
Figure
y if and only if (r<y} and x * .
It can be inferred from above figure that, the expression
4 i in biock B,is availabie in all the three blocks B, B Q19 Define the partiaB order for a semilattice.
ard B Auswer
Advantage
A partiai order *< for a semilattice (S, ^) is defined ,
It helps in elnminating common sub expression. S.

5.3 FOUNDATIONs OF DATA-FLOW As the binary operator 'meet' satisfies idem potent,
ANALYSIS commutat1ve and assOCiative properties, the partiai order
satisfies refiexive, aniisymmetric and transitive properties.
Q18. Write a short note on,
Reflexive Property
(i) Foundations of data-fiow analysis
framework

(ii) Semilattice Proof: r"Y=r as meet (") satisfies idempotent propery


(ii) Partial order. (ii Antisymmetric Property
Answer: Model Paper-1, Q10(b)
ifry and y<x then x =y
i) Foundation of Data-fow Analysis Framework
Proof: rSy inplies x ^y =
A data-flow analysis framework. (D, S, ^. F) includes
the following
y trom commulative prope
D:A data-fow direction which can be either forward
or backward.
of meet() operator
Machine-independent Optimizations 5.15

Transitivee Property be
framework is said to
In other words, data-flow

nay > 2 then r < monotone


If'r
Sandfe F
Proof:
f : < J ' andVSzimpliees x ^y =x and y Az=y XSy>Jx)sSV) V x,y e

be described as,
z = ( r ^ y) ^ z) Similarly, monotonicity can

Sandfe F
Sx ^y) s fr) ^0) V x,y
e
=
(x ^(v ^ :))
that resultant value
equation, it is noticed
In the above
= (x^y) of function f after calculating the meet
of two members say
the result achieved by
'*and 'y' can never be greater than with
individual members applied
of meet
applying meet between two
From
assoC1ative property operator.
function f .

shown that x ^z =r
ltis Distributive Frameworks
iff it
said to be distributive
('XSy*^y =x) A data flow framework is
is proved. satisfies the following condition.
Thus, tranisttivity property fin F
Sx ^y)=f) ^10) x, y Sand
V e

the properties of,


What are property
Ifp =q then p q = p by idempotent
^

Transfer functions in a data flow framework


Sop 9
(i) iterative algorithm. framework
distributive framework is
monotone
Hence, framework.
distributive
monotone framework is not a
nswer:
but
for forward and
Properties of
Transfer Functions in a Data Flow
Q22. Write an iterative algorithm
backward data-flow frameworks.
Framework

Model Paper-ll, Q10(b)


functions
In a data
flow framework, the set of transfer Answer :
Framework
the following properties, Data-flow
SS includes Iterative Algorithm
for Forward
forward
fiunction I such that V xeS, I(x) framework for
=x.
Fincludes identity Iterative solution to general
functions. In other data-flow problem.
under composition of
F is closed h defined by
words, vS, g e F there exists function
htr)=gf(r)) in F. Input ENTRY and EXIT
nodes.
A data-flow graph with
Iterative Algorithm forward.
Properties of the A data-flow direction D; //D is
are as follows, (i)
iterative algorithm
The properties of the A domain of values
'S°.
then the result (ii)
converges
If the iterative algorithm
considered as a solution of the
data-flow
(iv) A binary meet operator transfer
obtained is
A family of functions
'F°where F, represents the
equations. (V) B.
Maximum Fixed function for block
framework, the condition
in case of indicating boundary
monotone
data-flow equations. Aconstant value "VENTRY
solution to the
Point(MFP) is a are n e v e r greater
(vi) for forward framework.
values of IN[B] and OUT[B]
Here, the solutions.
in other
than the values of MFP Output: OUTIB]
if the
algorithm definitely converges OUT ENTRY] = VENTRY
Ihe iterative f r a m e w o r k is

height and if the .


Semilattice has finite than ENTRY)
blocks of B other
for (all basic
monotone.
frameworks
and
monotone 3 OUT [B]=T;
Discuss about
OUT exists)
distributive frameworks. While (changes to
4.
basic blocks B
other than ENTRY){
nswer for (all
ROUTIPI.
tonotone Frameworks IN[B]= APapredecessorf
is said to be 6
A data-flow
framework (D, S, ^, F) member is less
ess .7. OUT[B]= f(IN[B]):
from the first
if the result generated function to any
One applying
transfer
anthe
e Second member after
SIA GROUP
9 iembers of set S. n n FNGINEERING STUDENTS
5.16 cOMPILER DESIGN LJNTU-HYDERABAD
Iterative Algorithm for Backward
Data-flow Framework Data-flow Values for the Constant Propagation Framework
Iterative solution to The set of data-flow values for the constant-propagation
data-flow problem. general framework for backward
is a product lattice containing a single component for everv
Input variable in a program. A lattice for a variable contains the

i) A following
data-fiow graph with ENTRY and EXIT
nodes An undefined value called UNDEF which is assigned
(1) A data-flow
direction D ; //D is backward to a variable irrespectively.
(1ii) A domain of values Not-a-constant value called NAC. When this value does
'S not contain any constant value then it is mapped to a
(iv) A
binary meet operator "^ variable. This variable can take any of the following
(v A. family of functions 'F'
where, values.
transfer functions for each block 'B°.F, represents the
An input value
(vi) A constant value
'V=T EXIT
for backward framework. A value that have been derived from NAC variable.
Output: IN.B| Various constants in various paths leading to the

IN[EXIT}=VExIT
for
same points in the progran.
All suitable constants for each type of the variable.
(all
basic blocks of B other than
EXIT) However, the UNDEF and NAC values are unique to
3. IN[B] = T;
each other. Here the former one specifies that a variable
4. while (change to IN exists) which is not constant can be defined in numerous
5. ways. While the latter one specifies that it is difficult to
for(ali basic blocks B other than EXIT) determine any information about a variable.
The following figure illustrates a semilattice for a
. OUT[B] =^S,.
a success or
variable whose value is an integer,

1. IN[B] = f,(OUT[B):
UNDEF

5.4 cONSTANT PROPAGATION


Q23. Explain constant propagation.
Answer: Model Paper-, Q11(a)

Constant Propagation NAC


Constant propagation can be defined as the process of
providing the constant yalues to the variable during compile Figure: Semi
Lattice for an Integer
time. It is a forward data-flow problem. As shown in the above figure, the lattice contains three
elements namely, UNDEF at the
Example and the NAC at the bottom.
top,constants in the middle
These/elements represents
the
Consider the code given below, following,
int a= ]00; UNDEF value is greatest in partial order.
NAC value is smallest in
int b = 200;
partial order.
Constants are less than UNDEF and
int x = d + (b - a); greater than NAC.
The meet of two values is their
During compile time the values gets substituted as, greatest lower bound
V
int a = 100;
values, UNDEF v=vand -NAC Av= NAC
For any different constants a and b
int b-200, ab= NAC
intx = 100 +(200 100): For any constant c,

The characteristics of constant propagation includes, c''c C

It does not support distribution property. In general,


the data value for this
framework can be
1. defined the mapping from the
as
valúes for a fixed program variable to a value
It provides unbounded set of data-flow in the constant semilattice. The variable
value v in a map m i
flow graph represented as m(v).
AD NIT
5
Machinabout
cuSs
e-independent Optimizations
the
vork 224.
transfer
ation AnSwer: functions for the .17
constant
very
the
process ot
generating transter
each statement.
propagation tra
enerate

For functions for the


olds the ollowing. simplicity, consider thatblocks having
ving multiple statements involves the
block cartriesstatemen composition of functions
ned the basic
sfer funetions that ment. Also consiaet
a
singe a

to generate
variable values case of map as output when
of in
a
es uput
constant lattice an input
input map
map is provided. The input map usually reiers
1
) a
tdentity function that produces the to the
map
ng same
Constant transter function for map output which is as

v.m(v) undefined. Ihis is entry node that takes the


provided as input.
because, variales are not map as input and produces the
m,
If f represents the transfer defined prior to the d
execution or any pe
snect m and m'
to function of statement
relationship can be described
nent and
and m, m'm' s
the represents Tow values
represents the data flow
that
va
so m fr\m), then ,
identity funetion if is a
It is an
as
follows,
s
non-statement.
m'(v)= m(v) V #X its is an
V
assignment statement where m'(x) is.
) Equal to any of the following incase if
right hand side of S is in y+ z form.
m'(x)= m(y) +
m{:) IWhen m(y) and
m(=) are constants
m'(x) = NAC
/When m(y) or m(z) is not a constant

m'(x)=Undefined /When the above


two conditions are not satisfied.

ii) m' (x) is a constant c incase the right hand side of S is constant (c).

m' (x) is NAC incase the RHS is any other expression.

5.5 PARTIAL-REDUNDANCY ELIMINATION


025. Explain in detail the procedure that eliminating global common subexpressions.
OR

Eliminates", can be done at global level in a given program.


Explain how "Redundant Subexpression
Model Paper-Hl, Q11(a)
Answer:
available expression information as
subexpression elimination
takes a fiow graph with
An algorithm for global common

flow graph as output. The


algorithm works as follows:
uput and produces a revised no variables
least once at the beginning of E's block and
a =btc
which S evaluated at
the form
Forevery expression of the last evaluation of E, do the following:
E
after
expression is redefined flow graph and
ne block. This IS
obtained by following the edges ofthe
reach E's found is and evaluation that
ofb + c that evaluation of b + c
Determine the evaluations direction. In each
block the last

from E's block in backward


searching
reaches E.

. 1.
Define a new variable statements,
following
found in step I by the
d =b
t
statement
Keplace each

t=btc

dt
#=bt c bya:=1
4 Keplace the
expression a

Example flow graph. - SIA GROUP 2


following F O R ENGINEERING STUDENTs
Consider the
5.18

ti+2
cOMPILER DESIGN IJNTU-HYDER
HYDERABAD
(9)i+2
(12):a[(12)]|

ti+2
t(9)
-a[t (12)
Figure: Flow Graph Figure: Flow Graph
Assume that there are no
graph then the flow graphassignments
the flow to the array a in In the above flow graph, temporary variable , is usele
has common andit is removed during local live variable analvsis
i+2. subexpression The
temporary variable 1, is replaced by
value number 12 beracause
Byapplying the above algorithm it is same as value number 12.
the common
subexpression is eliminated. The resulting flow Q26. Explain detection of loop invariant computation
below: graph is shown
Answer:
K Loop Invariant Computation
t=i+2
t Loop invariant computations are the expressions in a loopD that
result in the value independent of the number of timet a
:a[t] same

loop is executed. A loop is a set of basic block with a header


which is an entry point for the loop.
A statement a := b +c at a point in the loop is loop
invariant if all the definitions of b and c are outside the loop.
4=t A Since the value of b c remains same each time the
loop is
5:alt] executed. If there is another statement e = a t dwith d defined
outside the loop then this statement is also
loop invariant.
Figure: Flow Graph after Eliminating Common Subexpression i +2 The
first step in the elimination of loop invariant
mthe above flow graph at,] and ait] are also common computations is to find Joop invariant statements in the program.
subexpressions. Using copy propagations transformation we Having determined loop invariants the optimization technique
replace, and , by t, so these expressions then become alt] called code motion eliminates them by moving such statements
which can be eliminated by applying the
algorithm once again. before (to preheader of) the loop. Such a transformation does
not change what the
program computes.
Another method to eliminate global common
subexpressions is to take into account the fact that the compiler Algorithm
for Detecting
Loop Invariant Computation
inserts the temporary variables and use corresponding blocks. The algorithm takes an
input a loop L consisting of a set
Another fact to consider is that the different temporary variables of basic blocks where each block
is a sequence of three address
may represent the same expressions. This method exploits these statements. It also takes ud-chains
statements. It produces a set of three
computed for individual
facts and assigns a number to each statement. It also uses the address statements
that
bit vectors where the bit i represents the expression numbered are loop invariant as an
output.
i. To treat temporary variables in a special way, the method The algorithm works as follows:
uses the value-numbering techniques during the numbering of 1 Mark "invariant" those
expressions. statements whose operands are a
either constant or have been
defined outside the loop L.
Consider the same example, suppose i+ 2 has the value .
Repeat step 3 until no new statement can be markea
number 9, when we use this value number instead oftemporary "invariant".
variables , and , then the expressions al,] and aft,l get the 3. Mark "invariant" all those
If the value number 12 is the resultant statement that are no
same value number.
previously so marked and whose operands are all
number then the bit 12 represents
both a[r] and af,]. Since
constant have been defined outside L or have exactly oneetn
it can be eliminated. The resulting flow graph
alt,1 is available definition which is a statement already marked invanan
is shown below: nL.

a k far the SlA GROUP LOGo on the TITIEcaver


achine-indepen
andent Optimizations 5.19
T-5.

iscuss abo artial redundant expression


e l i m i n a t i o n . In the flow yrapli,

aswer
Node I do1n ali the nodes
The fCaure illustratmg partial redundant expression and Node 2 dom Node 2
nation is grven,below,
Node 2
eilimat Node I and
Node 3 dom ali
the nodes except
Node 1, Node 2 and
nodes except
Node 4 dom all the
Node 3
B.
5
=b) (1-b+e Node 5 dom Node

6
Node 6 dom Node
Node 10
Node 9 and
Node 7,
Node 8,
(d-b+B Node 7 dom
d-B dom Node 8,
Node 9 and
Node 10
Node 8

Redundant Expression Elinination


Finure: Partial Node 9 dom Node
9

As showintihe above figure contains two paths B,B


Node 10.
RBB The fovmer path contains no redurndancy Node 10 dom
-
information
B,
and
the
dominator

,N/hereasthe latter path contains redundant expression 'b t c'


The best way ofrepresenting
this tree, the
initiai node is
the

k The expression is said to be redundant because its a


dominaior tree.
In
its
descendants.
n block
done already before the control reaches
1s by using dominates only
tation has been result of root node and every
node D'

ndancy can be eliminated by storing the


mpui

This redund.
above fiow graph is.
variable say 't for the
tation b tc in block B, in a temporary The dominator tree
putatio
the computation with
't in block B,
nd then replacing
GRAPHs
5.6 LOOPS IN FLOw
dominator
the algorithm for finding
028. Describe
with an example.
Model Paper-ill, Q11(a)

Answer
Dominators dominates
dominator node, ifit
Anode 'D is said to be a as D dom
N.
domination is represented
The iion the
another node 'N*. path
in the flow graph
every

D dominates N means, D. Hence, the entry of


through the node
nitial node to N goes and every node
the nodes in the loop 8
dominates on al!
a loop
dorninates itself.

9
Figure: Dominator Tree

of
an important. property
dominator tree fellows mmediate
The (unique)
node N has only one
dominators every dominator of N on any path
W which is the last
dominator
N".
initial node to
from the

1.e.

domNthen
IfDNand D
9 Ddom M

SiA GROUP
Eiaw Graph TI ENTS
3.20 cOMPILER DESIGN JNTU-HYDERABAD
Algorithm for Finding Dominators
input
A Tiow
graph G, containing 'N nodes, 'E' edges ând entry node 'BN.
Output
The set of nodes that dominate node n, V nodes n, in Ni.e., dominator

Procedure
n s aigorithm uses forward data flow algorithm for finding dominators in a flow graph. The algorithm is as follows,

For all nodes (N)


DOM (N) {power set of N}
For each block
B, if entry
OUTEJ{E}
. For each biock B, other than entry Ep
F ) u {B}
Where,
OUT [B] =Fp,[IN (B)|
IN[B] = A OUTIPJ

Predecessor
of

(B)
Initialize OUT .B] = N

Exampie

10
Figure: A Flow Graph G
The given flow graph G, has 10 nodes i.e.,
{1,2,3,4,5,6,7,8,9,10} and l15 edges. The algorithm for computing given
dominators works as follows

Let dominator (n) be set of nodes in out (N) then,


Doninator (1): As node I is the entry node
Dominator (i) = {1}

Look for the SIA GROUP LOGO on the TITLE COVER before you buy
Machine-Independent Optimizations 5.21
UNIT

aigator (2): Node 2 has only I predecessor i.e., dominator 1


pomin.

Dominator (2)= Dominator (2) U Dominator (1)

-(2) ut1)
= {1,2}

minator (3): Node 3 has 4 predecessor 1, 2,4 and 8. So, intersection of predecessors is performed.

Dominator (3)= Dominator (3)lo {Dominator (1)A Dominator (2) n Dominator (4) nDominator (8)
(3 u1} n{1,2}n {1,2,3,4,5,6,7.8,9,10} n (1.2,3,4,5,6,7,8,9,10}
{3} {{1}} = {1,3}
Dominator (4): Node 4 has 2 predecessors 3 and 7
Dominator (4)= Dominator (4) u (Dominator (3)n Dominator (7)

{4} u{1, 3} n{1,2,3,4,5,6,7,8,9,10}


=
{4} u {1, 3) =
{1,3,4}
Dominator (5): Node 5 has only one predecessor i.e., 4
Dominator (5)- Dominator (5) u Dominator (4)
=
{5} u{1,3,4)
{1,3,4, 5}
Dominator (6): Node 6 has only one predecessor i.e., 4
Dominator (6)= Dominator (6) U Dominator (4)

- {6} u {1, 3, 4}

{1, 3,4,6}
Node 7 has predecessors 5, 6, 8
Dominator (7):
Dominator (7)- Dominator (7)U {Dominator (5) n Dominator (6) n Dominator (8)
Dominator (7)- {7} u{{1,3,4,
5}n{1,3,4, 6} n{1,2,3, 4, 5,6. 7.8,9, 10}}
-{7) u1, 3,4}
= {1,3,4, 7}

Node 8 has 1 predecessor


i.e., node 7
Dominator (8):
LU Dominator (7)
Dominator (8)= Dominator (8)
= {8} U{1, 3, 4, 7}

{1, 3,4,7, 8}
predecessor i.e..s node 8
Dominator (9): Node 9 has
one

Dominator (9) L Dominator (8)


Dominator (9)=
{9} u{1, 3, 4, 7. 8}
={1.3,4, 7, 8, 9
predecessor i.e.,
node 8
Node.10 has one
Dominator (10):
(10) U Dominator (8)
Dominator
Dominator (10)=
= {10} {1,3, 4, 7., 8}
10}
={1, 3,4, 7, 8,
is below,
u n r tree for flow graph G' of figure
ERABADADI
c O M P I L E R D E S I G N 1JNTU-HYDE

5.22

9
10
Figure: Dominator Tree

flow graphs with


an exampie
Q29. Explain reducible and non-reducible Model Paper-|l, Q11(b)

Answer

Reducible Flow Graphs 1.e., Torward edges and back


disjoint groups
if its edges can be divided into two
A flow graph G, is said to be reducible,
follows:
edges and it has two properties. They are as
those edges whose heads dominates
their tails.
1 The back edges group contains only
node reachable from the
initial node of Gp
The forward edges make an acyclic graph, with every
2. statements like while-do,
continue, if-then-else,
for
graphs are reducible. For example, flow graphs
Almost all the flow
break and goto are reducit
from outside. Hence, entry
into a loop is through
In reducible flow graphs, there are no jumps in the middle ofthe loop
its header.

graph is as shown in figure (1).


The example ofreducible flow

10
Figure (1: Reducible Flow Graph
lT-5 Machine-Independent Optimizations 5.23
The back edges in a flow graph can be found and eliminated if DOM (Dominance)relation for a flow graph is known.To
cross
checkwhethertheabove flow graph is reducible or not, remove the back edges 43, 107,8>3 and 9 -> 1. As these
the back edges and the remaining are forward edges, the graph is acyclic (after removal of back edges) and hence reducible.
are
Non-reducible Flow Graph

There are some now graphs which are not reducible, such a flow graph is as shown in ngure ().

Figure (2): Non-reducible Flow Graph


Here, there are no back edges as no head ofan edge dominates its tail. As, the cycle 2 3 can be enteredattwo diterent
nlaces, node 2 and node 3, the flow graph is non-reducible. However, it can if the complete fow graph 15 acyclic.
bereducible
Non-reducible flow graphs are very rare. Languages like Modula 2 and Bliss allow programs with reducible fiowW graphs

only.
a30. Explain natural loops and inner loops of a flow graph with an example.
Model Paper-ll, Q11(b)
Answer:

Natural Loops
In a flow graph, if there exist a back edge n ^ b , then the natural loop ofthe edge 'b' is given along with a set of nodes

thatdo not go through b' to reach n'. In the edge n b, b is the head and n is the tail.
Algorithm for Constructing the Natural Loops

Input
>d
A flow graph G and a back edge n

Output
A set loop of all nodes in the natural loop of n >d

Procedure Insert(M)
Mis not in loop then

Loop {Loop} u{M}


Push(M)
end

* Main program */

Stack Empty
Loop { d
Insert (n)
while(stack not empty)

pop(M)

for(each predecessor P of M)
Insert(P)

A useful property of natural loops is that, the two loops in a ow graph can be neither nested (one inside anotherl nor

header. Moreover, when


the n e d e r 1s same, but still the loops are not nested then tha. are
disjoint ifthey don't have the same
treated as a single loopP
ENGINEERING STUDENTS SIA GROoUP 2
-

SPECTRUM ALL-IN-ONE JoURNAL FOR


cOMPILER DESIGN IJNTU-HYDEE
5.24
Inner Loops
DERABAD
in figure (1
A the flow graph given has an iome
loop that does contain any other an inner loop.
For example,
loop 4 2 i.e., the loop
ner loop
is called an
is called inne
path from 2>34

Figure (1): Flow Graph with an Inner Loop 4 2


Determining an inner loop when two loop have the same header is a bit difficult task. One such situa
a figure (2).
tuation is depicted in

Block 1

Block 2 Block 3
Figure (2: Two Loops with the Same Header
At the end of Block 1, if there is a test to be made in order to branch to either Block 2 or Block 3, say,
ifi-15 goto B
Then Block 1> Block 3 is most
is deeply examined. likely to be the inner loop than Block 1 > Block 2. However, it is not sure
until the code

You might also like