Unit 5 1 Basicblocks
Unit 5 1 Basicblocks
UNIT-5 TOPICS
• Code Generation: Issues in the design of code
generator, The Target language, Basic blocks & flow
graphs, Dag representation of basic blocks, A Simple
Code Generator, Peephole optimization. Transformation
of basic blocks,
• Machine-Independent Optimizations: The principal
sources of optimization: Global common Sub-
Expressions, Copy Propagation, Dead-Code Elimination
CODE GENERATION
• The final phase in the compiler model is the code generator.
• It takes as input the intermediate representation (IR) produced by the
front end of the compiler, along with relevant symbol table information,
and produces as output a semantically equivalent target program, as
shown in Fig.
2) x=x+1
B2
3) goto (5)
4) x=x-1 B3
B4
Construct basic block and flow graph for following
(1)if(a>b) goto
if (a < b) { (7) Leader
if (c < d) (2)if (c>d) goto
x = x + 1;
statements:
(5) (1) , (7) , (5), (2),
else
(3)x=x+1
x = x - 1;
(4)goto (8)
(3), (8) gotoB1
(1) if(a>b)
} else { (7)
x = 0; (5)x=x-1
(2) if (c>d) goto
B2
} (6)goto(8) (5)
(7)x=0
3) x=x+1 B3
4) goto (8)
(5) x=x-1 B4
(6) goto (8)
(7) x=0 B5
FLOW (8)
GRAPH
Construct basic blocks and flow graph for the following code
(1)prod:=0
Begin (2)i:=1
prods:=0 (3)t1:=4*i
i:=1 (4)t2:=a[t1] // compute a[i]
do (5)t3:=4*i
Prod:=Prod + (6)t4:=b[t3] // compute b[i]
a[i]*b[i] (7)t5:=t2*t4 // a[i]*b[i]
i:=i+1 (8)t6:=prod+t5
while i<=20 (9)prod:=t6 //
End prod:=prod+a[i]*b[i]
(10) t7:=i+1
(11) i:=t7
INPUT CODE (12) if i<=20 goto (3)
THREE ADDRESS
CODE
(1)prod:=0 B1
(1)prod:=0
(2)i:=1 (2)i:=1
(3)t1:=4*i
(4)t2:=a[t1] // compute a[i]
(5)t3:=4*i 3) t1:=4*i
(6)t4:=b[t3] // compute b[i] 4) t2:=a[t1] // compute a[i]
(7)t5:=t2*t4 // a[i]*b[i] 5) t3:=4*i
B2
(8)t6:=prod+t5 6) t4:=b[t3] // compute b[i]
(9)prod:=t6 // 7) t5:=t2*t4 // a[i]*b[i]
8) t6:=prod+t5
prod:=prod+a[i]*b[i]
9) prod:=t6 // prod:=prod+a[i]*b[i]
(10) t7:=i+1 10) t7:=i+1
(11) i:=t7 11) i:=t7
(12) if i<=20 goto (3) 12) if i<=20 goto (3)
Leaders are:
(1) Since it is the first statement FLOW GRAPH
(3) Since it is a target of conditional instruction
at (12)
Statement following (12) is a leader but not
shown
2) Construct basic blocks and flow graph for the following code:
c=0
1) c=0 // Initialize c
do 2) if a < b goto (5) // Check condition a < b
{ 3) x=x-1 // If false, decrement x
if (a < b) 4) goto (6)
then 5) x=x+1 // If true, increment x
x++; 6) c=c+1 // Increment c
else 7) if c < 5 goto (2) // Check loop condition,
repeat if c < 5
x–;
Leaders (1), (2), (3),
c++; (1)
(5), (6)
c=0
} while (c < 5) (2) (if a < b goto
(5)
(3) x=x-1
(4) goto
(6)
(5)
x=x+1
(6)
c=c+1
(7) if c < 5 goto
(2)
3) Construct basic blocks and flow graph for the following code:
op x
(ii) DAG for x:=op y
y
x,
op
(iii) x:=y y
Construct DAG for the following
1) a:=b+c 3) a=b+c
t:=c*d b=a-d
c=b+c
x:=m+a d=a-d
2) d:=b*c
e:=a+b
b:=b*c
a:=e-d
4) Construct DAG for the expression: a+a*(b-
c)+(b-c)*d
First, write three address code based on operator
precedence:
a+a*(b-c)+(b-c)*d
t1=b-c
t2=b-c
t3=a*t1
t4=t2*d
t5=a+t3
t6=t5+t4
Construct DAG for the
following
t1=b+c
6) a=b+c+d t2=t1+
d
b=c+a a=t2
t3=c+a
c=c+d b=t3
t4=c+d
c=t4
t1=b+c
7) a=b+c a=t1
b=a-d t2=a-d
b=t2
c=b+c t3=b+c
c=t3
5) Construct DAG for the following three address code
(1)t1:=4*i
(2)t2:=a[t1] // compute a[i]
(3)t3:=4*i
(4)t4:=b[t3] // compute b[i]
(5)t5:=t2*t4 // a[i]*b[i]
(6)t6:=prod+t5
(7)prod:=t6 //
prod:=prod+a[i]*b[i]
(8) t7:=i+1
(9) i:=t7
(10) if i<=20 goto (1)
6) Construct DAG for the
following three address code:
1) sqr := i * i
temp,
2) temp := sum + sum1 < (1)
+ =
sqr sqr
3) sum := temp *
i1
+ 1
sum0
4) i := i + 1 5
5) if (i<=15) goto
i0 i0 1
(1)
Applications of DAGs
• We can automatically detect common subexpressions
• We can determine which identifiers have their values
used in the block.
• We can determine which statements compute values
that could be used outside the block
SIMPLE CODE GENERATOR
• The code generation strategy here generates target code for three
address statements considering location of operands.
• Assumptions:
• ADD Rj, Ri Cost=1 (Register operations)
• ADD c, Ri Cost=2 (Memory operation, ‘c’ is a memory location)
• Register and address descriptors: Register descriptor keeps
track of what is currently in each register.
• It is consulted when a new register is needed.
• Address descriptor keeps track of the location where the current
value of the name can be found at run time.
• The location can be a register, a stack location or a memory
address.
• Format of instruction Instruction-Name Source,
Destination
CODE GENERATOR INSTRUCTIONS
STATEMENTS INSTRUCTIONS
a:=b[i] MOV i, R0 // R0 = i
MOV b(R0), a // a=b[i]
a[i]:=b MOV i,R0 // R0=i
MOV b, a(R0) // a[i]=b
c=a+b MOV a, R0 // R0=a
ADD b, R0 // R0=R0+b
STORE R0, c //c=R0
t=p*q MOV p, R0 // R0=p
MUL q, R0 // R0=R0*q
STORE R0, t // t=R0
s=i-j MOV i, R0 // R0=i
SUB j, R0 // R0 = R0-j
STORE R0, s //s=R0
MOV q, R0
t1=q-j SUB j, R0 // R0=q-j
t2=b*c MOV b, R1
t3=a+t2 MUL c, R1 //R1=b*c
t4=a-b ADD a, R1//R1=a+b*c
t5=t4+c MOV a, R2
t6=t1*t3 SUB b, R2 // R2=a-b
t7=t6+t5 ADD c, R2 // R2=a-b+c
MUL R1, R0//R0=(q-j)*(a+b*c)
ADD R2, R0//R0=(q-j)*(a+b*c)+(a-
THREE ADDRESS b+c)
CODE TARGET
CODE