Docsity

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

Partial preview of the text

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

Code Generation

Jingke Li

Portland State University

Code Generation

IR code โ†’ Code Generator โ†’ target code

Goal: Generating correct and high-quality target code.

Main Issue: Instruction selection and register allocation.

The mapping from an IR code to target code is not unique.

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
  • (^) Expression Code-Gen with Dynamic Programming โ€” Associate a cost to each operation, and then perform optimization to find the best target code pattern for an entire expression tree. - Applicable only to tree code
  • (^) Code-Gen by Pattern Matching โ€” Map a group of IR statements (e.g. a subtree) as a unit. - (^) Allow local optimization

A Simple Code-Gen Algorithm

Input: A sequence of three-address statements from a basic block.

Approach: Generate code for each statement independently.

Internal Structure:

  • (^) Register descriptor โ€” Keeps tract of what is currently in each register. (Multiple variables may be available in the same register: x:= y).
  • Address descriptor โ€” Keeps track of the location(s) where the current value of a variable. A location for a value x is denoted Lx , which could be a register, a stack location, or a memory address.
  • (^) Location function โ€” Returns a location for storing the result of a three-address statement s.

Simple Code-Gen (cont.)

x := y op z

  1. Consult the address descriptor to determine Ly and Lz.
  2. Invoke the location function to determine Lx ; update the address descriptor for x; update the register descriptor, if Lx is a reg.
  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. (Optional optimization) If the current values of y and/or z have no next uses, and are not live on block-exit, and are regs, update the register descriptor.

Simple Code-Gen (cont.)

x := op y

(Similarly handled.)

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

Expression Code-Gen with Dynamic Programming

Goal: Generate optimal code for an expression tree for a broad class of register machines.

  • (^) Input: An expression tree E.
  • (^) Target: A set of instructions and a set of interchangeable registers r 0 , r 1 ,... , rkโˆ’ 1.
  • Cost Vector: Associate a cost to every (sub)expression. C (E ) = (c 0 c 1 ยท ยท ยท cr ), 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.

DP Approach Review (cont.)

  • (^) Cost formula: The cost of computing a sequence S based on a particular partition S = S 1 S 2 can be expressed as c(S 1 | S 2 ) = c(S 1 ) + c(S 2 ) + c 12
  • (^) Memorization: Create a table to record the lowest cost for computing every subsequence of abcde: c(ab), c(bc), c(cd), c(de), c(abc), c(bcd), c(cde), ...
  • (^) Divide and conquer: Consider all four possible ways to partition the sequence abcde: c(a|bcde), c(ab|cde), c(abc|de), c(abcd|e)
  • (^) Recursion: Recursively find the lowest cost for each case, then

c(abcde) = min(c(a|bcde), c(ab|cde), c(abc|de), c(abcd|e))

Applying DP to Expression Code-Gen

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

Example:

Input: (a โˆ’ b) + c โˆ— (d/e)

โˆ’ โˆ— a b c / d e

Target: uniform-cost instructions with 2 regs: r 0 , r 1. ri := M ri := ri op rj ri := ri op M ri := rj M := ri Cost Vector: C = (c 0 c 1 c 2 ).

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

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

Tree Patterns (Tiles) Instructions Effect (TEMP ) โ€” ri (BINOP + ) add ri โ† rj + rk (BINOP - ) sub ri โ† rj โˆ’ rk (BINOP * ) mul ri โ† rj ร— rk (BINOP / ) div ri โ† rj / rk (BINOP + (CONST )) (BINOP + (CONST ) ) addi ri โ† rj + c (CONST ) (BINOP - (CONST )) subi ri โ† rj โˆ’ c

An Example

(MOVE (MEM (BINOP + (MEM (BINOP + (TEMP tFP) (CONST a))) (BINOP MUL (TEMP i) (CONST 4)))) (MEM (BINOP + (TEMP tFP) (CONST x))))

  • (^) Solution 1: (MOVE (MEM (BINOP + (MEM (BINOP + (TEMP tFP) (CONST a)))

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

(MEM (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 โ† M[fp + x] store M[r 1 ] โ† r 2

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 ]