Professional Documents
Culture Documents
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;
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;
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'
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]
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.
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
= GEN[B]
(BGN[B] -KILL{B]) a
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,
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.
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.
-1110000
= 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
=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.
= 1111111
= 0011110
= 0000010+ (0011110-0010000)
= 0000010+0001110
0001110
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
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
Transitivee Property be
framework is said to
In other words, data-flow
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
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
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
ii) m' (x) is a constant c incase the right hand side of S is constant (c).
. 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
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
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
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
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,
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
Look for the SIA GROUP LOGO on the TITLE COVER before you buy
Machine-Independent Optimizations 5.21
UNIT
-(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)
- {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}
{1, 3,4,7, 8}
predecessor i.e..s node 8
Dominator (9): Node 9 has
one
5.22
9
10
Figure: Dominator Tree
Answer
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 ().
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
* 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
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