Unit - 4 Pushdown Automata: Code Optimization and Code Generation
Unit - 4 Pushdown Automata: Code Optimization and Code Generation
Pushdown
Code optimization and code generation
Automata
L1:…
▪ Replace a jump around jump
Before After
if T = 0 goto L1 if T != 0 goto L2
L1:…
else goto L2
L1:…
3 4
Preheader
Header Header
B0 B0
Before After
Chapter – 6 : Code Optimization & Generation 22 Bahir Dar Institute of Technology
Loops in Flow Graphs: Reducible Flow Graph
▪ The reducible graph is a flow graph in which there are two types of
edges forward edges and backward edges.
▪ Exclusive use of structured flow-of-control statements such as if-
then-else, while-do, continue, and break statements produces
programs whose flow graphs are always reducible.
▪ There are no jumps into the middle of loops from outside; the only entry to a
loop is through its header.
▪ These edges have following properties, 1
▪ The forward edges form an acyclic graph
(a graph without cycles) in which every node can 2
be reached from the initial node of graph G.
3 4
2 3
▪ Here the fourth statement is redundantsince it loads a value that has just been
stored so we can eliminate that statement.
• Example:
• When inst ruct ions are independent, t heir evaluat ion order can
be changed.
LD R0 , a
t1 := a + b ADD R0 ,b
a + b – (c + d) * e t2 := c + d ST t1 , R0
t3 := e * t2 LD R1 , c
t4 := t1 – t3 ADD LD R0 , c
LD R0R1 , e, d
MUL R0 , R1 ADD R0 , d
Reorder LD R1 , t1 LD R1, e
SUB R1 , R0 MUL R1, R0
ST t4 , R1 LD R0 , a
t2 := c + d ADD R0 , b
t3 := e * t2 SUB R0 , R1
t1 := a + b ST t4 , R0
t4 := t1 – t3
32
Address modes
Mode Form Address Added Cost
Absolute M M 1
Register R R 0
Indexed a(R) a + contents (R) 1
Indirect Register *R contents (R) 0
Indirect Indexed *a(R) contents(a + contents (R)) 1
Literal #c c 1
x = *p → LD R1, p //R1=p
LD R2, 0(R1) //R2=content(0+content(R1))
ST x, R2 //x=R2
The assignment through a pointer *p = y is similarly implemented in machine
code by:
*p = y → LD R1, p //R1=p
LD R2, y //R2=y
ST 0(R1), R2 //content(0+content(R1))=R2
Finally, consider a conditional-jump three-address instruction like: if x < y goto L.
and implemented as:
if x < y goto L → LD R1, x //R1=x
LD R2, y //R2=y
SUB R1, R1, R2 //R1=R1-R2
BLTZ R1, L //if R1 < 0 jump to L
Fig.
When we construct the node for a third statement c = b + c, we know that the use of
b in b + c refers to the node of Fig. labeled -; because that is the most recent
definition of b. Thus, we do not confuse the values computed at statements one and
three.
However, the node corresponding to the fourth statement d = a- d has the operator -
and the nodes with attached variables a and d0 as children. Since
the operator and the children are the same as those for the node corresponding to
statement two, we do not create this node, but add d to the list of definitions for the
node labeled -.
▪ But by avoiding common expressions we get the block
Example 2:
(1) t1 := 4*i
(2) t2 := a [t1]
(3) t3 := 4*i t6 , prod
+
(4) t4 :=b [t3]
(5) t5 := t2*t4
prod ∗ t5
(6) t6 := prod +t5
(7) prod := t6 t4
t ≤ (1)
(8) t7 := i+1 [] 2 []
(9) i := t7 t1 ,t3
(10) if i<=20 goto (1) ∗ + t7 , i 20
a b
4 i 1