Code Generation - Languages and Compiler Design II | CS 322, Study notes of Computer Science

Material Type: Notes; Professor: Li; Class: LANG COMPILER DESIGN; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-9d0
koofers-user-9d0 🇺🇸

9 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Code Generation
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Code Generation 1/1
Code Generation
IR code Code Generator target code
Goal: Generating correct and high-quality target code.
Main Issue: Instruction selection (The mapping from an IR code to target
code is not unique.)
Factors that affect selection:
variety of instructions
variety of address modes
Jingke Li (Portland State University) CS322 Code Generation 2/1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Code Generation - Languages and Compiler Design II | CS 322 and more Study notes Computer Science in PDF only on Docsity!

Code Generation

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 Code Generation 1 / 1

Code Generation

IR code → Code Generator → target code

Goal: Generating correct and high-quality target code.

Main Issue: Instruction selection (The mapping from an IR code to target code is not unique.)

Factors that affect selection:

  • variety of instructions
  • (^) variety of address modes

An Example

t1 := fp[-12] (MOVE (MEM (BINOP + (NAME fp) 8)) fp[8] := t1 (MEM (BINOP - (NAME fp) 12)))

Target code:

Extreme RISC: # Moderate RISC:

add %fp,-12,%r3 ld [%fp-12],%r ld [%r3], %r5 st %r5,[%fp+8] add %fp,8,%r st %r5,%r4 # CISC: move [%fp-12],[%fp+8]

Jingke Li (Portland State University) CS322 Code Generation 3 / 1

Code-Gen Algorithms

  • Naive Code-Gen — Map each IR statement independently to a fixed set of instructions.
    • Simple, can be automated (i.e. codegen generators exist)
    • Not flexible, code quality may be poor
  • (^) Code-Gen by Pattern Matching — Map a group of IR statements (e.g. a subtree) as a unit.
    • Allow local optimization
  • Optimizing Code-Gen with Dynamic Programming — Associate a cost to each IR statement, and then perform global optimization to find the best target code pattern for the IR program.
    • Most expensive, but still acceptable in general

Simple Code-Gen (cont.)

x := y op z

  1. Consult the address descriptor to determine Ly and Lz.
  2. Invoke getreg() to determine the location Lx ; update the address descriptor for x; update the register descriptor, if Lx is a register.
  3. If Lx = Ly or Lx = Lz , generate “op Ly ,Lz ” or “op Lz ,Lx ”
  4. Otherwise, generate “mov Ly ,Lx ,” then “op Lz ,Lx .”
  5. If the current values of y and/or z have no next uses, and are not live on block-exit, and are registers, update the register descriptor. x := op y (Similarly handled.)

Jingke Li (Portland State University) CS322 Code Generation 7 / 1

Simple Code-Gen (cont.)

a := b[i]

b in register %rb b in memory Mb b in stack slot Sb mov [%rb+i],a mov Mb,%r mov [%fp+Sb],%r mov %r+i,a mov [%r+i],a

a[i] := b

a in register %ra a in memory Ma a in stack slot Sa mov b,[%ra+i] mov Ma,%r mov [%fp+Sa],%r mov b,[%r+i] mov b,[%r+i]

if x rop y goto z

  1. Move x and y to registers, if they are not there yet.
  2. Generate “cmp Lx ,Ly .”
  3. Generate “cjump rop Lz .”

An Example

t := a - b u := a - c v := t + u d := v + u

Statement Code Generated Register Descriptor Address Descriptor registers empty t := a-b mov a, %r0 %r0 contains t t in %r sub b, %r u := a-c mov a, %r1 %r0 contains t t in %r sub c, %r1 %r1 contains u u in %r v := t+u add %r1, %r0 %r0 contains v v in %r %r1 contains u u in %r d := v+u add %r1 %r0 %r0 contains d d in %r mov %r0, d d in %r0, memory

Jingke Li (Portland State University) CS322 Code Generation 9 / 1

Code-Gen with Dynamic Programming

Goal: Try to generate optimal code for a broad class of register machines.

Machine Model:

  • k interchangeable registers r 0 , r 1 ,... , rk− 1.
  • Instructions are of the form ri := E , where E is an expression containing operators, registers, and memory locations (denoted M).
  • Every instruction has an associated cost.

Cost Vector: C (E ) = (c 0 c 1 · · · cr ) — it’s defined for an expression E , where c 0 — the cost of computing E into memory, with the use of unbounded number of registers. ci — the cost of computing E into a register, with the use of up to i registers.

Applying DP to Code-Gen

  1. Compute bottom-up for each node n of the expression tree T an array C of costs.
  2. Traverse T , using the cost vectors to determine which subtrees of T must be computed into memory.
  3. Traverse T and generate the final target code. The code for the subtrees computed into memory locations is generated first.

Example: (a − b) + c ∗ (d/e)

− ∗ a b c / d e

Target Machine Model: Two registers: r 0 , r 1 Uniform-cost instructions: ri := M ri := ri op rj ri := ri op M ri := rj M := ri Cost Vector: C = (c 0 c 1 c 2 ). Jingke Li (Portland State University) CS322 Code Generation 13 / 1

Example (cont.)

Step 1:

a b c’ = (0 1 1) c” = (0 1 1)

c =?

Select the best from the 9 possible cases: c 0 ′ c 0 ′′ ⇒ c =(3 2 2) c 0 ′ c 1 ′′ ⇒ c =(4 3 3) c 0 ′ c 2 ′′ ⇒ c =(4 3 3) c 1 ′ c 0 ′′ ⇒ c =(3 2 2) c 1 ′ c 1 ′′ ⇒ c =(3 2 2) c 1 ′ c 2 ′′ ⇒ c =(3 2 2) c 2 ′ c 0 ′′ ⇒ c =(3 2 2) c 2 ′ c 1 ′′ ⇒ c =(3 2 2) c 2 ′ c 2 ′′ ⇒ c =(3 2 2)

Results for the whole tree:

− ∗

a b c /

d e

(0 1 1) (0 1 1) (0 1 1)

(0 1 1) (0 1 1)

(3 2 2)

(3 2 2)

(5 5 4)

(8 8 7)

Example (cont.)

Step 2:

− ∗

a b c /

d e

(0 1 1) ( 0 1 1) (0 1 1)

(0 1 1) ( 0 1 1)

(3 2 2)

(3 2 2)

(5 5 4 )

(8 8 7 )

R 1

R 1 R 0

R 1 R 0 R 1

R 1

Step 3: r 0 := c r 1 := d r 1 := r 1 /e r 0 := r 0 ∗ r 1 r 1 := a r 1 := r 1 − b r 1 := r 1 + r 0

Jingke Li (Portland State University) CS322 Code Generation 15 / 1

Code-Gen by Pattern Matching

Assume an IR tree language.

  • Defining tiles (i.e. patterns) — each corresponds to an instruction
  • Tiling the IR tree — covering the tree with nonoverlapping tiles

An Example (cont.)

  • Solution 2: (MOVE (MEM

(MEM

(BINOP + (MEM (BINOP + (TEMP tFP) (CONST a)))

(BINOP MUL (TEMP i) (CONST 4) )))

(BINOP + (TEMP tFP) (CONST x))) )

load r 1 ← M[fp + a] addi r 2 ← r 0 + 4 mul r 2 ← ri × r 2 add r 1 ← r 1 + r 2 load r 2 ← fp + x movem M[r 1 ] ← M[r 2 ]

Jingke Li (Portland State University) CS322 Code Generation 19 / 1

Finding the Best Tiling

We can associate a cost to each tile, then we can measure quantitatively the quality of a tiling of an IR tree — the best tiling corresponds to an instruction sequence of least cost.

Two Levels of Optimality:

  • Optimal Tiling — no two adjacent tiles can be combined into a single tile of lower cost. A greedy algorithm can be used to find an optimal tiling.
  • (^) Optimum Tiling — the tiles sum to the lowest possible value. Dynamic programming technique can be applied to tiling to find the optimum tiling for an IR tree.

Maximal Munch Algorithm

For finding an optimal tiling.

  1. Start at the root of the tree, find the “largest” fitting tile (which covers the root and maybe several other nodes hear the root). Several subtrees may result from this step.
  2. Repeat the same step for each subtree.

“Largest” means most nodes. If two tiles of equal size match at the root, the choice between them is arbitrary.

Example: Applying this algorithm to the tree on the previous page will result in Solution 2.

Jingke Li (Portland State University) CS322 Code Generation 21 / 1

Dynamic Programming Algorithm

For finding the optimum tiling. The algorithm assigns a cost to every node in the tree, which is the sum of the instructions costs of the best instruction sequence that can tile the subtree rooted at that node. It works bottom-up. For each tile t of cost c that matches at node n, there will be zero or more subtrees si corresponding the leaves of the tile. Assume the cost for the subtree si is ci (which has already been computed), then the total cost of matching tile t at node n is c +

ci.

  1. Start at the leaves of the tree. For each node n, find all the tiles that matches at n, and compute their costs. The tile with the minimum cost is chosen, and the (minimum) cost is assigned to n.
  2. Repeat this step for other nodes, until the root node’s cost is computed.
  3. Now the algorithm goes top-down to emit instructions by calling Emission(root). The Emission routine is defined as follows: Emission(node n) — for each leaf li of the tile selected at node n, recursively perform Emission(li ). Then emit the instruction matched

Jingke Liat (Portland State University)^ n. CS322 Code Generation 22 / 1