











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Final Code Generation
front end
src
pgm
intermediate code
o optimizer
final code generator
target
pgm
symbol table
Input to Code Generator :
Output of Code Generator : target program. This can be any of:
Translating 3-address code to final code
This is almost a macro expansion process. Examples:
3-Address Code MIPS assembly code x = A[i] load i into reg 1 la reg 2 , A add reg 2 , reg 2 , reg 1 lw reg 2 , ( reg 2 ) sw reg 2 , x x = y+z load y into reg 1 load z into reg 2 add reg 3 , reg 1 , reg 2 sw reg 3 , x if x >= y goto L load x into reg 1 load y into reg 2 bge reg 1 , reg 2 , L
The resulting code may not be very efficient, but can be improved via various code optimization techniques.
Improving Code Quality : Register Allocation
A value in a register can be accessed much more effi- ciently than one in memory, so we should try to keep (frequently used) values in registers.
Local Register Allocation : considers only small seg- ments of code (“basic blocks”) :
Global Register Allocation : considers the entire body of a function or procedure:
Improving Code Quality : Code Optimization
Basic Blocks
Definition : A basic block is a sequence of consecutive instructions such that:
Identifying basic blocks :
(b) Any instruction that is the target of a branch is a leader.
(c) Any instruction immediately following a (con- ditional or unconditional) branch is a leader.
Example
/* dot product: prod =
i=
a[i] ∗ b[i] */
No. leader? Instruction basic block (1)
prod = 0 1 (2) i = 1 1 (3)
t1 = 4i 2 (4) t2 = a[t1] 2 (5) t3 = 4i 2 (6) t4 = b[t3] 2 (7) t5 = t2*t4 2 (8) t6 = prod+t5 2 (9) prod = t6 2 (10) t7 = i+1 2 (11) i = t7 2 (12) if i ≤ N goto (3) 2
Example :
L1: prod = 0 i = 1 L2: t1 = 4i t2 = a[t1] t3 = 4i t4 = b[t3] t5 = t2*t t6 = prod+t prod = t t7 = i+ i = t if i ≤ N goto L
prod = 0 i = 1
t1 = 4 * i t2 = a[t1] t3 = 4 * i
B2:
t4 = b[t2] t5 = t2 * t t6 = prod + t prod = t t7 = i + 1 i = t if i<=N goto B
B1:
Representing Basic Blocks in a Flow Graph
Many different representations are possible, with differ- ent tradeoffs. One possibility:
struct bblk_struct { int bblno; instruction *first_inst; instruction *last_inst; struct bblk_struct *preds, *succs; struct bblk_struct *prev, *next;
/* + information computed during analysis */
}
Register Interference Graphs
if a node n has fewer than k neighbors, we can always find a color for it. Delete n and its edges from the graph and try to k-color the resulting graph.
until either
General rule for spilling: avoid introducing code into inner loops.
Variable Liveness
Definition : A variable is live at a point in a program if it may be used at a later point before being redefined.
Example :
x
x not live
x live
x live
x live x not live
live
y = y-x
x = 1
x = x+
y = x+y
x = 2
z = z+
Using liveness information:
Computing Liveness Information (within a basic block)
Suppose we know which variables are live at the exit from the basic block. Then:
I : x := y op z
we have: