1 code generation the target machine instruction selection and register allocation basic blocks and...

108
1 Code Generation • The target machine • Instruction selection and registe r allocation • Basic blocks and flow graphs • A simple code generator • Peephole optimization • Instruction selector generator • Graph-coloring register allocator

Upload: kya-leeper

Post on 31-Mar-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

1

Code Generation

• The target machine

• Instruction selection and register allocation

• Basic blocks and flow graphs

• A simple code generator

• Peephole optimization

• Instruction selector generator

• Graph-coloring register allocator

Page 2: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

2

The Target Machine

• A byte addressable machine with four bytes to a word and n general purpose registers

• Two address instructions– op source, destination

• Six addressing modes– absolute M M 1– register R R 0– indexed c(R) c+content(R) 1– ind register *R content(R) 0– ind indexed *c(R) content(c+content(R)) 1– literal #c c 1

Page 3: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

3

Examples

MOV R0, MMOV 4 (R0), MMOV *R0, MMOV *4 (R0), MMOV #1, R0

Page 4: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

4

Instruction Costs

• Cost of an instruction = 1 + costs of source and destination addressing modes

• This cost corresponds to the length (in words) of the instruction

• Minimize instruction length also tend to minimize the instruction execution time

Page 5: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

5

Examples

MOV R0, R1 1MOV R0, M 2MOV #1, R0 2MOV 4 (R0), *12 (R1) 3

Page 6: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

6

An Example

Consider a := b + c

1. MOV b, R0 2. MOV b, a ADD c, R0 ADD c, a MOV R0, a

3. R0, R1, R2 contains 4. R1, R2 contains the addresses of a, b, c the values of b, c MOV *R1, *R0 ADD R2, R1 ADD *R2, *R0 MOV R1, a

Page 7: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

7

Instruction Selection

• Code skeleton x := y + z a := b + c d := a + e MOV y, R0 MOV b, R0 MOV a, R0 ADD z, R0 ADD c, R0 ADD e, R0 MOV R0, x MOV R0, a MOV R0, d

• Multiple choices a := a + 1 MOV a, R0 INC a

ADD #1, R0 MOV R0, a

Page 8: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

8

Register Allocation

• Register allocation: select the set of variables that will reside in registers

• Register assignment: pick the specific register that a variable will reside in

• The problem is NP-complete

Page 9: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

9

An Example

t := a + b t := a + bt := t * c t := t + ct := t / d t := t / d

MOV a, R1 MOV a, R0ADD b, R1 ADD b, R0MUL c, R0 ADD c, R0DIV d, R0 SRDA R0, 32MOV R1, t DIV d, R0

MOV R1, t

Page 10: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

10

Basic Blocks

• A basic block is a sequence of consecutive statements in which control enters at the beginning and leaves at the end without halt or possibility of branching except at the end

Page 11: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

11

An Example

(1) prod := 0(2) i := 1(3) t1 := 4 * i(4) t2 := a[t1](5) t3 := 4 * i(6) t4 := b[t3](7) t5 := t2 * t4(8) t6 := prod + t5(9) prod := t6(10) t7 := i + 1(11) i := t7(12) if i <= 20 goto (3)

Page 12: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

12

Flow Graphs

• A flow graph is a directed graph

• The nodes in the graph are basic blocks

• There is an edge from B1 to B2 iff B2 immediately follows B1 in some execution sequence

– B2 immediately follows B1 in program text

– there is a jump from B1 to B2

• B1 is a predecessor of B2, B2 is a successor of B1

Page 13: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

13

An Example

(1) prod := 0(2) i := 1

(3) t1 := 4 * i(4) t2 := a[t1](5) t3 := 4 * i(6) t4 := b[t3](7) t5 := t2 * t4(8) t6 := prod + t5(9) prod := t6(10) t7 := i + 1(11) i := t7(12) if i <= 20 goto (3)

B0

B1

Page 14: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

14

Construction of Basic Blocks

• Determine the set of leaders– the first statement is a leader– the target of a jump is a leader– any statement immediately following a jump is

a leader

• For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program

Page 15: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

15

Representation of Basic Blocks

• Each basic block is represented by a record consisting of– a count of the number of statements– a pointer to the leader– a list of predecessors– a list of successors

Page 16: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

16

Define and Use

• A three address statement x := y + z is said to define x and to use y and z

• A name is live in a basic block at a given point if its value is used after that point, perhaps in another basic block

Page 17: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

17

Next-Use Information

i: x := …

… no assignment to x

j: y := … x …

Statement j uses the value of x defined at i

Page 18: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

18

An Example

(1) a := b + c a:(2,3,5), c:(4), d:(2)

(2) e := a + d a:(3,5), c:(4), e:(3)

(3) f := e - a a:(5), c:(4), f:(4)

(4) e := f + c a:(5), e:(5)

(5) g := e - a g:(?)

b, c, d are live at the beginning of the block

b:(1), c:(1,4), d:(2)

Page 19: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

19

Computing Next Uses

• Scan statements “i: x := y op z” backward

• Attach to statement i the information currently found in the symbol table regarding the next uses and liveness of x, y, and z

• In the symbol table, set x to “not live” and clear the next uses” of x

• In the symbol table, set y and z to “live” and add i to the “next uses” of y and z among

blockswithin blocks

Page 20: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

20

A Simple Code Generator

• Consider each statement in a basic block in turn, remembering if operands are in registers

• Assume that– each operator has a corresponding target language

operator– computed results can be left in registers as long as

possible, unless• out of registers

• at the end of a basic block

Page 21: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

21

Register and Address Descriptors

• A register descriptor keeps track of what is currently in each register

• An address descriptor keeps track of the location(s) where the current value of the name can be found at run time

Page 22: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

22

An Example

d := (a - b) + (a - c) + (a - c)

[ ] [ ]t := a - b MOV a, R0 [R0:(t)]

SUB b, R0 [t:(R0)]u := a - c MOV a, R1 [R0:(t), R1:(u)]

SUB c, R1 [t:(R0), u:(R1)]v := t + u ADD R1, R0 [R0:(v), R1:(u)]

[v:(R0), u:(R1)]d := v + u ADD R1, R0 [R0:(d)]

[d:(R0)]MOV R0, d [ ] [ ]

Page 23: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

23

Code Generation Algorithm

• Consider an instruction of the form “x := y op z”• Invoke getreg to determine the location L where th

e result of “y op z” will be placed• Determine a current location y’ of y from the addre

ss descriptor (register location preferred). If y’ is not L, generate “MOV y’, L”

• Generate “op z’, L”, where z’ is a current location of z from the address descriptor.

• Update the address and register descriptors for x, y, z, and L

Page 24: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

24

Code Generation Algorithm

• Consider an instruction of the form “x := y”

• If y is in a register, change the register and address descriptors

• If y is in memory, – if x has next use in the block, invoke getreg to f

ind a register r, generate “MOV y, r”, and make r the location of x

– otherwise, generate “MOV y, x”

Page 25: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

25

Code Generation Algorithm

• Once all statements in the basic block are processed, we store those names that are live on exit and not in their memory locations

Page 26: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

26

The Function getreg

• Consider an instruction of the form “x := y op z”• If y is in a register r that holds the value of no other

names, and y is not live and no next uses after this statement, return r

• Otherwise, return an empty register r if there is one• Otherwise, if x has a next use in the block, or op is

an operator requiring a register, find an occupied register r. Store the value of r, update address descriptor, and return r

• If x has no next use, or no suitable occupied register can be found, return the memory location of x

Page 27: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

27

An Example

d := (a - b) + (a - c) + (a - c)

[ ] [ ]t := a - b MOV a, R0 [R0:(t)]

SUB b, R0 [t:(R0)]u := a - c MOV a, R1 [R0:(t), R1:(u)]

SUB c, R1 [t:(R0), u:(R1)]v := t + u ADD R1, R0 [R0:(v), R1:(u)]

[v:(R0), u:(R1)]d := v + u ADD R1, R0 [R0:(d)]

[d:(R0)]MOV R0, d [ ] [ ]

Page 28: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

28

Indexing and Pointer Operations

i in Ri i in Mi i in Si(A)a := b[i] MOV b(Ri), R MOV Mi, R MOV Si(A), R MOV b(R), R MOV b(R), Ra[i] := b MOV b, a(Ri) MOV Mi, R MOV Si(A), R MOV b, a(R) MOV b, a(R)

p in Rp p in Mp p in Sp(A)a := *p MOV *Rp, R MOV Mp, R MOV Sp(A), R MOV *R, R MOV *R, R*p := a MOV a, *Rp MOV Mp, R Mov a, R MOV a, *R MOV R, *Sp(A)

Page 29: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

29

Conditional Statements

• Condition codesif x < y goto z CMP x, y

CJ< z• Conditon code descriptors

x := y + z MOV y, R0if x < 0 goto z ADD z, R0

MOV R0, xCJ< z

Page 30: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

30

Global Register Allocation

• Keep live variables in registers across block boundaries

• Keep variables frequently used in inner loops in registers

Page 31: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

31

Loops

• A loop is a collection of nodes such that– all nodes in the collection are strongly

connected– the collection of nodes has a unique entry

• An inner loop is one that contains no other loops

Page 32: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

32

Variable Usage Counts

• Savings– Count a saving of one for each use of x in loop L

that is not preceded by an assignment to x in the same block

– Save two units if we can avoid a store of x at the end of a block

• Costs– Cost two units if x is live at the entry or exit of

the inner loop

Page 33: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

33

An Example

a := b + cd := d - be := a + f

f := a - d b := d + fe := a - c

b := d + c

B1

B2 B3

B4

b,c,d,e,f

b,c,d,f

b,c,d,e,f

c,d,e,fc,d,e,f b,c,d,e,f b,d,e,f

a,c,d,fa,c,d,e a,c,d,e,f

Page 34: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

34

An Example

use(a, B1) = 0, use(a, B2) = 1use(a, B3) = 1, use(a, B4) = 0live(a, B1) = 1, live(a, B2) = 0live(a, B3) = 0, live(a, B4) = 0

save(a) = (0+1+1+0) + 2 (1+0+0+0) = 4

save(b) = 5 save(c) = 3 save(d) = 6 save(e) = 4 save(f) = 4

Page 35: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

35

An Example

MOV R1, R0; ADD c, RoSUB R1, R2; MOV R0, R3ADD f, R3; MOV R3, e

MOV R0, R3; SUB R2, R3MOV R3, f

MOV R2, R1; ADD f, R1MOV R0, R1; SUB c, R3MOV R3, e

MOV R2, R1; ADD c, R1

B1

B2 B3

B4

MOV R1, b; MOV R2, d

MOV R1, b; MOV R2, d

MOV b, R1; MOV d, R2

Page 36: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

36

Register Assignment for Outer Loops

• Apply the same idea for inner loops to progressively larger loops

• If an outer loop L1 contains an inner loop L2, a name allocated a register in L2 need not be allocated a register in L1-L2

• If name x is allocated a register in L1 but not L2, need store x on entrance to L2 and load x on exit from L2

• If name x is allocated a register in L2 but not L1, need load x on entrance to L2 and store x on exit from L2

Page 37: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

37

Peephole Optimization

• Improve the performance of the target program by examining and transforming a short sequence of target instructions

• May need repeated passes over the code

• Can also be applied directly after intermediate code generation

Page 38: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

38

Examples

• Redundant loads and storesMOV R0, aMOV a, Ro

• Algebraic Simplificationx := x + 0x := x * 1

• Constant foldingx := 2 + 3 x := 5y := x + 3 y := 8

Page 39: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

39

Examples

• Unreachable code#define debug 0if (debug) (print debugging information)

if 0 <> 1 goto L1 print debugging informationL1:

if 1 goto L1 print debugging informationL1:

Page 40: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

40

Examples

• Flow-of-control optimization

goto L1 goto L2… …

L1: goto L2 L2: goto L2

goto L1 if a < b goto L2… goto L3

L1: if a < b goto L2 …L3: L3:

Page 41: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

41

Examples

• Reduction in strength: replace expensive operations by cheaper ones– x2 x * x– fixed-point multiplication and division by a

power of 2 shift– floating-point division by a constant floating-

point multiplication by a constant

Page 42: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

42

Examples

• Use of machine Idioms: hardware instructions for certain specific operations– auto-increment and auto-decrement addressing

mode (push or pop stack in parameter passing)

Page 43: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

43

DAG Representation of Blocks

• Easy to determine:

• common subexpressions

• names used in the block but evaluated outside the block

• names whose values could be used outside the block

Page 44: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

44

DAG Representation of Blocks

• Leaves labeled by unique identifiers

• Interior nodes labeled by operator symbols

• Nodes optionally given a sequence of

identifiers, having the value represented by

the nodes

Page 45: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

45

An Example

(1) t1 := 4 * i(2) t2 := a[t1](3) t3 := 4 * i(4) t4 := b[t3](5) t5 := t2 * t4(6) t6 := prod + t5(7) prod := t6(8) t7 := i + 1(9) i := t7(10) if i <= 20 goto (1) i04 1

<=

*

[]

+

b

[]

a

*prod0

+ 20t1,t3

t4t2

t5

t6, prod

(1)

t7, i

Page 46: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

46

Constructing a DAG

• Consider x := y op z. Other statements can be

handled similarly

• If node(y) is undefined, create a leaf labeled y

and let node(y) be this leaf. If node(z) is

undefined, create a leaf labeled z and let

node(z) be that leaf

Page 47: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

47

Constructing a DAG

• Determine if there is a node labeled op, whose left child is node(y) and its right child is node(z). If not, create such a node. Let n be the node found or created.

• Delete x from the list of attached identifiers for node(x). Append x to the list of attached identifiers for the node n and set node(x) to n

Page 48: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

48

Reconstructing Quadruples• Evaluate the interior nodes in topological order

• Assign the evaluated value to one of its attached identifier x, preferring one whose value is needed outside the block

• If there is no attached identifier, create a new temp to hold the value

• If there are additional attached identifiers y1, y2, …, yk whose values are also needed outside the block, add

y1 := x, y2 := x, …, yk := x

Page 49: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

49

An Example

(1) t1 := 4 * i(2) t2 := a[t1](3) t3 := b[t1](4) t4 := t2 * t3(5) prod := prod + t4(6) i := i + 1(7) if i <= 20 goto (1)

i04 1

<=

*

[]

+

b

[]

a

*prod0

+ 20

prod

(1)

i

Page 50: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

50

Arrays, Pointers, Procedure Calls

x := a[i] x := a[i]a[j] := y z := xz := a[i] a[j] := y=> range analysis

*p := w=> aliasing analysis

side effects caused by procedure calls=> inter-procedural analysis

Page 51: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

51

Ordering Rules

• Any evaluation of or assignment to an

element of array a must follow the previous

assignment of that array if there is one

• Any assignment to an element of array a

must follow any previous evaluation of a

Page 52: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

52

Ordering Rules

• Any use of any identifier must follow the

previous procedure call or indirect

assignment through a pointer if there is one

• Any procedure call or indirect assignment

through a pointer must follow all previous

evaluations of any identifier

Page 53: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

53

Generating Code From DAGs

t1 := a + bt2 := c + dt3 := e - t2t4 := t1 - t3

(1) MOV a, R0(2) ADD b, R0(3) MOV c, R1(4) ADD d, R1(5) MOV R0, t1(6) MOV e, R0(7) SUB R1, R0(8) MOV t1, R1(9) SUB R0, R1(10) MOV R1, t4

-

+ -

+a0 b0 e0

c0 d0

t1

t2

t3

t4

Page 54: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

54

Rearranging the Order

t2 := c + dt3 := e - t2t1 := a + bt4 := t1 - t3

(1) MOV c, R0(2) ADD d, R0(3) MOV e, R1(4) SUB R0, R1(5) MOV a, R0(6) ADD b, R0(7) SUB R1, R0(8) MOV R0, t4

-

+ -

+a0 b0 e0

c0 d0

t1

t2

t3

t4

Page 55: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

55

A Heuristic Ordering for DAG

• Attempt as far as possible to make the

evaluation of a node immediately follow the

evaluation of its left most argument

Page 56: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

56

Node Listing Algorithm

while unlisted interior nodes remain do begin select an unlisted node n, all of whose parents have been listed; list n; while the leftmost child m of n has no unlisted parents and is not a leaf do begin list m; n := m; endend

Page 57: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

57

An Example

32

1

-

+

*

a0 b0

c0

+

d0 e0

6

-+

*

4

5 7

t7 := d + et6 := a + bt5 := t6 - ct4 := t5 * t7t3 := t4 - et2 := t6 + t4t1 := t2 * t3

Page 58: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

58

Generating Code From Trees

• There exists an algorithm that determines th

e optimal order in which to evaluate stateme

nts in a block when the dag representation o

f the block is a tree

• Optimal order here means the order that yiel

ds the shortest instruction sequence

Page 59: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

59

Optimal Ordering for Trees

• Label each node of the tree bottom-up with an integer denoting fewest number of registers required to evaluate the tree with no stores of immediate results

• Generate code during a tree traversal by first evaluating the operand requiring more registers

Page 60: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

60

The Labeling Algorithm

if n is a leaf then if n is the leftmost child of its parent then label(n) := 1 else label(n) := 0else begin let n1, n2, …, nk be the children of n ordered by label so that label(n1) label(n2) … label(nk); label(n) := max1 i k(label(ni) + i - 1)end

Page 61: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

61

An Example

t1

t4

t2a b

c

t3

d

e1

2

1 2

0

1 0

11

For binary interior nodes:

label(n) = max(l1, l2), if l1 l2l1 + 1, if l1 = l2

Page 62: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

62

Code Generation From a Labeled Tree

• Use a stack rstack to allocate registers R0, R1, …, R(r-1)

• The value of a tree is always computed in the top register on rstack

• The function swap(rstack) interchanges the top two registers on rstack

• Use a stack tstack to allocate temporary memory locations T0, T1, ...

Page 63: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

63

Cases Analysis

op

n1 n2n name name

op

n1 n2

op

n1 n2

op

n1 n2

label(n1) < label(n2) label(n2) label(n1) both labels r

Page 64: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

64

The Function gencodeprocedure gencode(n);begin if n is a left leaf representing operand name and n is the leftmost child of its parent then print 'MOV' || name || ',' || top(rstack) else if n is an interior node with operator op, left child n1, and right child n2 then

if label(n2) = 0 then /* case 1 */

else if 1 label(n1) < label(n2) and label(n1) < r then /* case 2 */

else if 1 label(n2) label(n1) and label(n2) < r then /* case 3 */

else /* case 4, both labels r */end

Page 65: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

65

The Function gencode/* case 1 */begin let name be the operand represented by n2; gencode(n1); print op || name || ',' || top(rstack)end

/* case 2 */begin swap(rstack); gencode(n2); R := pop(rstack); gencode(n1); print op || R || ',' || top(rstack); push(rstack, R); swap(rstack);end

Page 66: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

66

The Function gencode/* case 3 */begin gencode(n1); R := pop(rstack); gencode(n2); print op || R || ',' || top(rstack); push(rstack, R);end

/* case 4 */begin gencode(n2); T := pop(tstack); print 'MOV' || top(rstack) || ',' || T; gencode(n1); push(tstack, T); print op || T || ',' || top(rstack);end

Page 67: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

67

An Example

t1

t4

t2a b

c

t3

d

e1

2

1 2

0

1 0

11

gencode(t4) [R1, R0] /* 2 */ gencode(t3) [R0, R1] /* 3 */ gencode(e) [R0, R1] /* 0 */ print MOV e, R1 gencode(t2) [R0] /* 1 */ gencode(c) [R0] /* 0 */ print MOV c, R0 print ADD d, R0 print SUB R0, R1 gencode(t1) [R0] /* 1 */ gencode(a) [R0] /* 0 */ print MOV a, R0 print ADD b, R0 print SUB R1, R0

+

-+

-

Page 68: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

68

Multiregister Operations

• Some operations like multiplication, division, or a function call normally require more than one register

• The labeling algorithm needs to ensure that label(n) is always at least the number of registers required by the operation

label(n) = max(2, l1, l2), if l1 l2l1 + 1, if l1 = l2

Page 69: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

69

Algebraic Properties

+

T1

+

T1

1 l

max(2, l)

l 0

l

Ti3

+

Ti1 Ti2

+

Ti4

++

T1

T4

+

T2 T3

+

commutative

commutative

associative

largest

Page 70: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

70

Common Subexpressions

• Nodes with more than one parent in a dag are

called shared nodes

• Optimal code generation for dags on both a o

ne-register machine or an unlimited number o

f registers machine are NP-complete

Page 71: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

71

Partitioning a DAG into Trees

• Partition a dag into a set of trees by finding for each root and shared node n, the maximal subtree with n as root that includes no other shared nodes, except as leaves

• Determine a code generation ordering for the trees

• Generate code for each tree using the algorithms for generating code from trees

Page 72: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

72

An Example

32

1

-

+

*

a0 b0

c0

+

d0 e0

6

-+

*

4

5 7

1

32

+ * * e06

-+

*

44

4

75

+ e06

+-

*

e0

d0c0

+

a0 b0

6

Page 73: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

73

Dynamic Programming Code Dynamic Programming Code GenerationGeneration

• The dynamic programming algorithm applies to a broad class of register machines with complex instruction sets

• Machines has r interchangeable registers

• Machines has instructions of the formRi = E

where E is any expression containing operators, registers, and memory locations. If E involves registers, then Ri must be one of them

Page 74: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

74

Dynamic ProgrammingDynamic Programming

• The dynamic programming algorithm partitions the problem of generating optimal code for an expression into sub-problems of generating optimal code for the sub-expressions of the given expression

+

T1 T2

Page 75: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

75

Contiguous EvaluationContiguous Evaluation

• We say a program P evaluates a tree T contiguously if

• it first evaluates those subtrees of T that need to be computed into memory

• it then evaluates the subtrees of the root in either order

• it finally evaluates the root

Page 76: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

76

Optimally Contiguous ProgramOptimally Contiguous Program

• For the machines defined above, given any program P to evaluate an expression tree T, we can find an equivalent program P' such that– P' is of no higher cost than P

– P' uses no more registers than P

– P' evaluates the tree in a contiguous fashion

• This implies that every expression tree can be evaluated optimally by a contiguous program

Page 77: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

77

Dynamic Programming AlgorithmDynamic Programming Algorithm

• Phase 1: compute bottom-up for each node n of the expression tree T an array C of costs, in which the ith component C[i] is the optimal cost of computing the subtree S rooted at n into a register, assuming i registers are available for the computation. C[0] is the optimal cost of computing the subtree S into memory

Page 78: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

78

Dynamic Programming AlgorithmDynamic Programming Algorithm

• To compute C[i] at node n, consider each ma

chine instruction R := E whose expression E

matches the subexpression rooted at node n

• Determine the costs of evaluating the operan

ds of E by examining the cost vectors at the c

orresponding descendants of n

Page 79: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

79

Dynamic Programming AlgorithmDynamic Programming Algorithm

• For those operands of E that are registers, consider all possible orders in which the corresponding subtrees of T can be evaluated into registers

• In each ordering, the first subtree corresponding to a register operand can be evaluated using i available registers, the second using i-1 registers, and so on

Page 80: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

80

Dynamic Programming AlgorithmDynamic Programming Algorithm

• For node n, add in the cost of the instruction R := E that was used to match node n

• The value C[i] is then the minimum cost over all possible orders

• At each node, store the instruction used to achieve the best cost for C[i] for each i

• The smallest cost in the vector gives the minimum cost of evaluating T

Page 81: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

81

Dynamic Programming AlgorithmDynamic Programming Algorithm

• Phase 2: traverse T and use the cost vectors

to determine which subtrees of T must be co

mputed into memory

• Phase 3: traverse T and use the cost vectors

and associated instructions to generate the fi

nal target code

Page 82: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

82

An ExampleAn Example

Consider a machine with two registers R0 and R1and instructions Ri := Mj Mi := Ri Ri := Rj Ri := Ri op Rj Ri := Ri op Mj

-

+

(0, 1, 1)

(8, 8, 7)(3, 2, 2)

a b /*

(5, 5, 4)

(0, 1, 1)

(0, 1, 1)

(3, 2, 2)

e

c d

(0, 1, 1)

(0, 1, 1)

Page 83: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

83

An ExampleAn Example

-

+

(0, 1, 1)

(8, 8, 7)(3, 2, 2)

a b /*

(5, 5, 4)

(0, 1, 1)

(0, 1, 1)

(3, 2, 2)

c

d e

(0, 1, 1)

(0, 1, 1)R0 := cR1 := dR1 := R1 / eR0 := R0 * R1R1 := aR1 := R1 - bR1 := R1 + R0

Page 84: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

84

Code Generator GeneratorsCode Generator Generators

• A tool to automatically construct the instruction selection phrase of a code generator

• Such tools may use tree grammars or context free grammars to describe the target machines

• Register allocation will be implemented as a separate mechanism

• Graph coloring is one of the approaches for register allocation

Page 85: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

85

Tree RewritingTree Rewriting

:=

ind +

memb const1+

+

+

ind

consti

consta regsp

regsp

a[i] := b + 1

Page 86: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

86

Tree RewritingTree Rewriting• The code is generated by reducing the input

tree into a single node using a sequence of tree-rewriting rules

• Each tree rewriting rule is of the formreplacement template { action }

– replacement is a single node– template is a tree– action is a code fragment

• A set of tree-rewriting rules is called a tree-translation scheme

Page 87: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

87

An ExampleAn Example

regi

+

regi regj

{ ADD Rj, Ri }

Each tree template represents a computation performed

by the sequence of machines instructions emitted by the

associated action

Page 88: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

88

Tree Rewriting RulesTree Rewriting Rules

(1) regi constc { MOV #c, Ri }

(2) regi mema { MOV a, Ri }

(3):=

mema regi

mem { MOV Ri, a }

(4):=

ind regj

mem

regi

{ MOV Rj, *Ri }

+

constc regj

regi

ind

(5) { MOV c(Rj), Ri }

Page 89: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

89

Tree Rewriting RulesTree Rewriting Rules

(6) regi { ADD c(Rj), Ri }

+const1

regi (8) { INC Ri }regi

+

regj

regi (7) { ADD Rj, Ri }regi

+

ind

regj

regi

+constc

Page 90: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

90

An ExampleAn Example

:=

ind +

memb const1+

+

+

ind

consti

consta regsp

regsp(1){ MOV #a, R0 }

Page 91: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

91

An ExampleAn Example

:=

ind +

memb const1+

+

+

ind

consti

reg0 regsp

regsp(7)

{ ADD SP, R0 }

Page 92: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

92

An ExampleAn Example

:=

ind +

memb const1+

+

ind

consti

reg0

regsp

(5)(6)

{ MOV i (SP), R1 }

{ ADD i (SP), R0 }

Page 93: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

93

An ExampleAn Example

:=

ind +

memb const1reg0

(2)

{ MOV b, R1 }

Page 94: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

94

An ExampleAn Example

:=

ind +

reg1 const1reg0

(8)

{ INC R1 }

Page 95: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

95

An ExampleAn Example

:=

ind reg1

reg0

(4)

{ MOV R1, *R0 }

Page 96: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

96

Tree Pattern MatchingTree Pattern Matching

• The tree pattern matching algorithm can be implemented by extending the multiple-keyword pattern matching algorithm

• Each tree template is represented by a set of strings, each of which represents a path from the root to a leave

• Each rule is associated with cost information

• The dynamic programming algorithm can be used to select an optimal sequence of matches

Page 97: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

97

Semantic PredicatesSemantic Predicates

regi

+

regi constc

{ if c = 1 thenINC Ri

elseADD #c, Ri }

The general use of semantic actions and predicates can

provide greater flexibility and ease of description than

a purely grammatical specification

Page 98: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

98

Pattern Matching by ParsingPattern Matching by Parsing

• Use an LR parser to do the pattern matching

• The input tree can be treated as a string by using its prefix representation

:= ind + + consta regsp ind +consti regsp + memb const1

• The tree-translation scheme can be converted into a syntax-directed translation scheme by replacing the tree templates with their prefix representations

Page 99: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

99

Syntax-Directed Translation Syntax-Directed Translation SchemeScheme

(1) regi constc { MOV #c, Ri }

(2) regi mema { MOV a, Ri }

(3) mem := mema regi { MOV Ri, a }

(4) mem := ind regi regj { MOV Rj, *Ri }

(5) regi ind + constc regj { MOV c(Rj), Ri }

(6) regi + regi ind + constc regj { ADD c(Rj), Ri }

(7) regi + regi regj { ADD Rj, Ri }

(8) regi + regi const1 { INC Ri }

Page 100: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

100

Advantages of Syntax-Directed Advantages of Syntax-Directed Translation SchemeTranslation Scheme

• The parsing method is efficient and well

understood

• It is relatively easy to retarget the code

generator

• The code generator can be made more

efficient by adding special-case productions

Page 101: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

101

Disadvantages of Syntax-Disadvantages of Syntax-Directed Translation SchemeDirected Translation Scheme

• A left-to-right order of evaluation is fixed

• The machine description grammar can

become inordinately large

• Context free grammar is usually highly

ambiguous

Page 102: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

102

Graph ColoringGraph Coloring

• In the first pass, target machine instructions are selected as though there were an infinite number of symbolic registers

• In the second pass, physical registers are assigned to symbolic registers using graph coloring algorithms

• During the second pass, if a register is needed when all available registers are used, some of the used registers must be spilled

Page 103: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

103

Interference GraphInterference Graph

• For each procedure, a register-interference

graph is constructed

• The nodes in the graph are symbolic

registers

• An edge connects two nodes if one is live at

a point where the other is defined

Page 104: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

104

K-Colorable GraphsK-Colorable Graphs

• A graph is said to be k-colorable if each node can be assigned one of the k colors such that no two adjacent nodes have the same color

• A color represents a register

• The problem of determining whether a graph is k-colorable is NP-complete

Page 105: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

105

A Graph Coloring AlgorithmA Graph Coloring Algorithm

• Remove a node n and its edges if it has fewer than k neighbors

• Repeat the removing step above until we end up with the empty graph or a graph in which each node has k or more adjacent nodes

• In the latter case, a node is selected and spilled by deleting that node and its edges, and the removing step above continues

Page 106: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

106

A Graph Coloring AlgorithmA Graph Coloring Algorithm

• The nodes in the graph can be colored in the

reverse order in which they are removed

• Each node can be assigned a color not

assigned to any of its neighbors

• Spilled nodes can be assigned any color

Page 107: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

107

An ExampleAn Example

1

3

4

2

5

3

4

2

5

3

4 5 4 5 5

Page 108: 1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization

108

An ExampleAn Example

G

B

G

R

R

B

G

R

R

B

G R G R R