Docsity

CS-322 Target Generation: Storing Variables at the End of Basic Blocks - Prof. Harry H. Po, Study notes of Computer Science

Toggle sidebar
bg1
1
CS-322 Target Generation, Part 2
© Harry H. Porter, 2006
Code Generation Algorithm #2
Focus on one Basic Block at a time
•!Ignore all other basic blocks
•!Generate best possible code for the basic block
2
CS-322 Target Generation, Part 2
© Harry H. Porter, 2006
Code Generation Algorithms #2 and #3
Focus on one Basic Block at a time
•!Ignore all other basic blocks
•!Generate best possible code for the basic block
Register Strategy:
•!Store all LIVE variables in memory between basic blocks.
• Within each basic block...
Use registers for variables and computation, as necessary
•!Each basic block will use registers independently
of other basic blocks
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download CS-322 Target Generation: Storing Variables at the End of Basic Blocks - Prof. Harry H. Po and more Study notes Computer Science in PDF only on Docsity!

© Harry H. Porter, 2006

Code Generation Algorithm

Focus on one Basic Block at a time

**- !Ignore all other basic blocks

  • !Generate best possible code for the basic block CS-322 Target Generation, Part 2 © Harry H. Porter, 2006**

Code Generation Algorithms #2 and

Focus on one Basic Block at a time

**- !Ignore all other basic blocks

  • !Generate best possible code for the basic block**

Register Strategy:

**- !Store all LIVE variables in memory between basic blocks.

  • Within each basic block...** **Use registers for variables and computation, as necessary
  • !Each basic block will use registers independently** of other basic blocks

© Harry H. Porter, 2006

Q: Why store all variables at the end of each Basic Block?

Why not leave them in registers?

CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

Q: Why store all variables at the end of each Basic Block?

Why not leave them in registers?

A: Each Basic Block is processed in isolation.

A variable might be put in different registers

in different blocks

“x” is in R7 “x” is in R ??

B 5

B 8

B 6

B 9

© Harry H. Porter, 2006

We Need “Live Variable” Information!

We’ll need to know which variables are LIVE

at the end of each basic block

Option 1:

Perform live variable analysis beforehand (during optimization phase) (^) We’ll only store LIVE variables at the end of each Basic Block CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

We Need “Live Variable” Information!

We’ll need to know which variables are LIVE

at the end of each basic block

Option 1:

Perform live variable analysis beforehand (during optimization phase)

Option 2:

Assume every variable is live at the end of every basic block We’ll only store LIVE variables at the end of each Basic Block

© Harry H. Porter, 2006

We Need “Live Variable” Information!

We’ll need to know which variables are LIVE

at the end of each basic block

Option 1:

Perform live variable analysis beforehand (during optimization phase)

Option 2:

Assume every variable is live at the end of every basic block

Option 3:

Distinguish temporaries from normal variables... Assume temps are not live between blocks. Assume normal variables are live between blocks. (For more precision, we may want to distinguish which variables are in any Use(Bi) sets We’ll only store LIVE variables at the end of each Basic Block CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

“Definition” and “Use” of Variables

A “Definition” of variable x is an instruction that changes the value x := ... A “Use” of x is an instruction that reads or uses the value ... := ... x ... This statement defines “y”... ...if control flow could allow this value to reach these uses. What are the next uses of the variable it defines? (stmts 106, 109, 112) 104: y := a + 5 105: ... 106: b := y * b 107: ... 108: ... 109: x := b * y 110: b := b - x 111: ... 112: c := y + b

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1:L(2) i:L(3) t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0)

Key:

L(4) Live; next-use in statement 4 L(0) Live; no next-use in this block D Dead CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

Next-Use Algorithm

**- !Identify all variables used in this block.

  • !Use a table** One entry for each variable For each variable, store... Its current status LIVE or DEAD If LIVE, its next-use in this block **(0=not used again in this block)
  • !Start with the LIVEness info**

at the BOTTOM of the block.

- !Work through the block in reverse order

instruction-by-instruction

- !Update the table, as we go upward.

A temporary data structure,

used only for this algorithm

(Implementation Idea: Add fields to “VarDecl” to hold this info)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(0) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... (^) i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(0)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: L(9) a: L(0) b: L(0) prod: L(0) i: D CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: L(9) a: L(0) b: L(0) prod: L(0) i: D

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(8)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: L(6) t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: L(6) t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: L(5) t5: D t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: L(5) t5: D t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(4) prod: L(6) i: L(3) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(4) prod: L(6) i: L(3)

© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: L(2) t2: D t3: D t4: D t5: D t6: D t7: D a: L(2) b: L(4) prod: L(6) i: L(3) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1:L(2) i:L(3) t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: L(2) t2: D t3: D t4: D t5: D t6: D t7: D a: L(2) b: L(4) prod: L(6) i: L(3)

© Harry H. Porter, 2006

Code Generation Algorithm

**- !Generate code for each Basic Block in isolation.

  • Assume that Next-Use info. is available (see previous algorithm).
  • !Go through the statements (in FORWARD order).
  • !Try to keep variables in registers...** Leave as long as possible in register. Store back to memory only when necessary. **Some variables may be left in registers for several instructions.
  • !At the end of the basic block,** Move all LIVE variables back to memory.

Data Structure:

From statement to statement, we need to remember... For each variable: Is it in a register? Which one? For each register: Which variable(s) does it contain, if any? CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

Code Gen. Alg.

Example

IR Code

t1 := 43 * a t1 := t1 + 7 a := t1 * 4

Code Gen. Alg.

© Harry H. Porter, 2006

Code Gen. Alg.

Example

IR Code

t1 := 43 * a t1 := t1 + 7 a := t1 * 4

Code Gen. Alg.

LD a,R MUL 43,R ST R1,t LD t1,R ADD 7,R ST R1,t LD t1,R MUL 4,R ST R1,a CS-322 Target Generation, Part 2 © Harry H. Porter, 2006

Example

IR Code

t1 := 43 * a t1 := t1 + 7 a := t1 * 4

Code Gen. Alg.

LD a,R MUL 43,R ST R1,t LD t1,R ADD 7,R ST R1,t LD t1,R MUL 4,R ST R1,a

Code Gen. Alg.

LD a,R MUL 43,R ADD 7,R ST R1,t MUL 4,R ST R1,a

Assuming t1 is LIVE at the end of the block, we need to store it.

If t1 is DEAD, this instruction would be omitted!