Compiler Unit 4
Compiler Unit 4
Unit - IV
Chapter 9
Code Generation
(C) 2014, Prepared by Partha Sarathi Chakraborty
Outline
• Issues in the Design of Code Generator
• The Target Machine
• Runtime Storage Management
• Basic Blocks and Flow Graphs
• Next-use Information
(C) 2014, Prepared by Partha Sarathi Chakraborty
Code Generation
(C) 2014, Prepared by Partha Sarathi Chakraborty
The output code must be correct and of high quality, meaning that it should
make effective use of the resources of the target machine.
4
• Register Allocation
• Choice of Evaluation Order
• Approaches to Code Generation
5
op source, destination
7
Instruction Costs
• Machine is a simple, non-super-scalar processor
with fixed instruction costs
• Realistic machines have deep pipelines, I-cache,
D-cache, etc.
• Define the cost of instruction
= 1 + cost(source-mode) + cost(destination-mode)
9
Examples
Instruction Operation Cost
MOV R0,R1 Store content(R0) into register R1 1
MOV R0,M Store content(R0) into memory location M 2
MOV M,R0 Store content(M) into register R0 2
MOV 4(R0),M Store contents(4+contents(R0)) into M 3
MOV *4(R0),M Store contents(contents(4+contents(R0))) into M 3
MOV #1,R0 Store 1 into R0 2
ADD 4(R0),*12(R1) Add contents(4+contents(R0))
to contents(12+contents(R1)) 3
10
Instruction Selection
• Instruction selection is important to obtain
efficient code
• Suppose we translate three-address code
x:=y+z
to: MOV y,R0
MOV a,R0
ADD z,R0 a:=a+1
ADD #1,R0
MOV R0,x
MOV R0,a
Cost = 6
Better Better
Example
t:=a+b t:=a*b
t:=t*c t:=t+a
t:=t/d t:=t/d
500: …
564: MOV R0,12(SP) Store return value
572: GOTO *SP Return to caller
Basic Block
• A basic block is a sequence of consecutive statements
in which flow of control enters at the beginning and
leaves at the end without halt or possibility of
branching except at the end.
• The following sequence of three address code forms a
(C) 2014, Prepared by Partha Sarathi Chakraborty
basic block:
t1 = a * a
t2 = a * b
t3 = t1 + t2
18
Basic Blocks
MOV 1,R0
MOV n,R1
MOV 1,R0 JMP L2
MOV n,R1
JMP L2 L1: MUL 2,R0
L1: MUL 2,R0 SUB 1,R1
SUB 1,R1
L2: JMPNZ R1,L1
L2: JMPNZ R1,L1
19
a := c*a a := c*a
b := 0 b := 0
Blocks are equivalent, assuming t1 and t2 are dead: no longer used (no longer live)
22
Basic Block
• Transformations on Basic Blocks
• Structure-Preserving Transformations
• Algebraic Transformations
• Representations of Basic Blocks
(C) 2014, Prepared by Partha Sarathi Chakraborty
25
Common-Subexpression
Elimination
• Remove redundant computations
a := b + c a := b + c
b := a - d b := a - d
c := b + c c := b + c
d := a - d d := b
t1 := b * c
t1 := b * c
t2 := a - t1
t2 := a - t1
t3 := b * c
t4 := t2 + t1
t4 := t2 + t3
27
b := a + 1 b := a + 1
a := b + c …
…
Assuming a is dead (not used)
if true goto L2
b := x + y
…
Remove unreachable code
28
t1 := b + c t1 := b + c
t2 := a - t1 t2 := a - t1
t1 := t1 * d t3 := t1 * d
d := t2 + t1 d := t2 + t3
Normal-form block
29
Interchange of Statements
• Independent statements can be reordered
t1 := b + c t1 := b + c
t2 := a - t1 t3 := t1 * d
t3 := t1 * d t2 := a - t1
d := t2 + t3 d := t2 + t3
Algebraic Transformations
• Change arithmetic operations to transform
blocks to algebraic equivalent forms
t1 := a - a t1 := 0
t2 := b + t1 t2 := b
t3 := 2 * t2 t3 := t2 << 1
31
Flow Graph
• The representation of flow-of-control information to the set of
basic blocks making up a program by constructing a directed
graph called a flow graph.
• The nodes of the flow graph are the basic blocks.
• One node is distinguished as initial; it is the block whose
leader is the first statement. There is a directed edge from
(C) 2014, Prepared by Partha Sarathi Chakraborty
Flow Graph
Representation of Basic Blocks in
32
33
Loop
• A Loop is a collection of nodes in a flow graph such
that
– All nodes in the collection are strongly connected; that is,
from any node in the loop to any other, there is a path of
length one or more, wholly within the loop, and
(C) 2014, Prepared by Partha Sarathi Chakraborty
Loops (Example)
B1: MOV 1,R0 Strongly connected
MOV n,R1
JMP L2 components:
Next-Use
• Next-use information is needed for dead-code
elimination and register assignment
• Next-use is computed by a backward scan of a
basic block and performing the following actions
on statement
i: x := y op z
– Add liveness/next-use info on x, y, and z to statement i
– Set x to “not live” and “no next use”
– Set y and z to “live” and the next uses of y and z to i
36
Next-Use (Step 1)
i: a := b + c
Next-Use (Step 2)
Next-Use (Step 3)
Next-Use (Step 4)
A Code Generator
• Generates target code for a sequence of three-
address statements using next-use information
• Uses new function getreg to assign registers to
variables
• Computed results are kept in registers as long as
possible, which means:
– Result is needed in another computation
– Register is kept up to a procedure call or end of block
• Checks if operands to three-address code are
available in registers
41
[] <=
6. t6 := prod + t5
7. prod := t6 t1 t3
a b * + t7 i 20
8. t7 := i + 1
9. i := t7 i0 1
4
10. if i <= 20 goto (1)
48
Peephole Optimization
• A simple but effective technique for locally improving the target
code is peephole optimization, which is done by examining a sliding
window of target instructions (called the peephole) and replacing
instruction sequences within the peephole by a shorter or faster
sequence, whenever possible.
• Peephole optimization can also be applied directly after intermediate
(C) 2014, Prepared by Partha Sarathi Chakraborty
• Algebraic simplifications
x=x+0 x=x*1
• Reduction in Strength
x=x^2 x=x*x x=2*x x=x+x
• Use of machine idioms
auto increment, auto-decrement, right-shift, left-shift
50