Docsity

Final Code Generation - Lecture Notes | CS 152, Study notes of Computer Science

Partial preview of the text

Download Final Code Generation - Lecture Notes | CS 152 and more Study notes Computer Science in PDF only on Docsity!

Final Code Generation

front end

src

pgm

intermediate code

o optimizer

final code generator

target

pgm

symbol table

Input to Code Generator :

  • intermediate code program
  • symbol table

Output of Code Generator : target program. This can be any of:

  • assembly language program
  • absolute machine-language program
  • relocatable machine-language program

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”) :

  • If an expression will be used soon after it is evalu- ated, try to compute it into an unused register.
  • If there are no free registers, we can either compute into memory (if addressing modes allow), or free up a register by storing its contents into memory. Choose the register cheapest to store to memory and least likely to be accessed soon.

Global Register Allocation : considers the entire body of a function or procedure:

  • Tries to keep frequently accessed values in registers, esp. across loops.
  • Uses loop nesting depth as a guide to frequency of access: variables in the most deeply nested loops are assumed to be accessed the most frequently.

Improving Code Quality : Code Optimization

  • Examine the program to find out about certain properties of interest (“Dataflow Analysis”).
  • Use this information to change the code in a way that improves performance. (“Code Optimization”). Examples of optimizations include: - Code Motion out of Loops : if a computation inside a loop produces the same result for all iterations (e.g., computing the base address of a local array), it may be possible to move the computation outside the loop. - Common Subexpression Elimination : if the same expression is computed in many places (e.g., ar- ray addresse computations; results of macro ex- pansion), compute it once and reuse the result. - Copy Propagation : If we have an intermediate code “copy” instruction ‘x := y’, replace subse- quent uses of x by y (where possible). - Dead Code Elimination : delete instructions whose results are not used.

Basic Blocks

Definition : A basic block is a sequence of consecutive instructions such that:

  1. control enters at the beginning;
  2. control leaves at the end; and
  3. control cannot halt or branch except at the end.

Identifying basic blocks :

  1. Determine the set of leaders, i.e., the first in- struction of each basic block: (a) The first instruction of the function is a leader.

(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.

  1. For each leader, its basic block consists of itself and all instructions upto, but not including, the next leader (or end of function).

Example

/* dot product: prod =

∑^ N

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

  • Nodes ' variables or symbolic registers.
  • There is an edge between two nodes if they can be simultaneously live.
  • If there are k assignable registers, we try to k-color the interference graph. This problem is NP-complete in general. The fol- lowing heuristic works well in practice: repeat

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

  • we get the empty graph: in this case, work back- wards to produce a k-coloring of the original graph; or
  • we get a graph where every node has ≥ k neigh- bors: in this case, choose a node to spill to memory, delete this node from the graph, and repeat the above process.

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:

  1. If a variable x is in a register r at a program point, and x is not live, then r can be used for another variable without having to store x to memory.
  2. For constructing the interference graph for register allocation by graph coloring.

Computing Liveness Information (within a basic block)

Suppose we know which variables are live at the exit from the basic block. Then:

  • Scan backwards from the end of the block. At the point immediately before an instruction

I : x := y op z

we have:

  • y and z are live; and
  • x is not live (unless x = y or x = z).