Lecture 25 Generating Code for Basic Blocks - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Lecture 25 Generating Code for Basic Blocks

Description:

then shift one place to the left to get 14*i. 4. CSCE 531 Spring 2006 ... Dead-code elimination. Renaming temporary variables. Reordering independent statements ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 20
Provided by: mantonm5
Learn more at: https://www.cse.sc.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 25 Generating Code for Basic Blocks


1
Lecture 25 Generating Code for Basic Blocks
CSCE 531 Compiler Construction
  • Topics
  • Code Generation
  • Readings 9.4-9.6

April 19, 2006
2
Overview
  • Last Time Lec24 slides 1-15
  • Finishing touches on Project 5
  • RET E.place
  • Whats in PROLOGUE/EPILOGUE
  • Overview of Code Generation
  • Instruction selection
  • Basic Blocks
  • Todays Lecture
  • Questions on Project 5 Functions
  • Code Generation for Basic Blocks
  • Register Allocation
  • Optimizations
  • Error Recovery
  • References
  • Code generation 9.4-9.6
  • Error recovery p 264, p 364-365

3
Review Instruction selection
  • Choosing instructions / sequences of instructions
    to minimize some metric
  • Example
  • double a614
  • In addressing aij, a00 (inumcols
    j) w
  • Need to multiply by 14 and 8
  • 8 handled by the scale in IA32
  • Mult by 14
  • Shift copy of value (i) 3 bits to the left to
    obtain 8i (8 23)
  • Subtract original (i) from 8i to get 7i,
  • then shift one place to the left to get 14i

4
ReviewBasic Blocks and Flow Graphs
  • To generate better code, we will need to analyze
    the structure of code written as list of
    quadruples
  • A Basic Block is a sequence of consecutive
    statements in which flow of control enters at the
    beginning and leaves at the end without
    possibility of branching except at the end
  • Define/Use a b c
  • This statement defines a
  • It uses b and c
  • A name (identifier) is said to be live at a given
    point if its value is after that point in the
    program

5
Leaders of the Block
  • A leader is the first statement of a basic block.
  • The algorithm for Basic Blocks
  • Determine the leaders first.
  • The first statement is a leader.
  • Any statement that is the target of a branch is a
    leader.
  • Any statement immediately following a branch is a
    leader.
  • For each leader the block extends from the leader
    up to the statement just prior to the next
    leader.

6
Example fig 9.8
  1. prod 0
  2. k 1
  3. t1 4 k
  4. t2 a t1
  5. t3 4 k
  6. t4 a t1
  7. t5 t2 t4
  8. t6 prod t5
  9. prod t6
  10. t7 k 1
  11. k t7 1
  12. If k lt 20 goto 3

7
Example matrix multiply
  • void
  • matmult(int n, int aNN, int bNN, int
    cNN)
  • int i, j, k, n, sum
  • for (i0 iltn i)
  • for (j0 jltn j)
  • sum 0
  • for (k0 kltn k) / inner loop /
  • sum sum aik bkj
  • cij sum

8
Matmult Naïve Basic blocks
  1. i 0
  2. if i gt n goto 28
  3. j 0
  4. if j gt n goto 26
  5. sum 0
  6. k 0
  7. if k gt n goto 20
  8. T1 i N
  9. T2 T1 k
  10. T3 T2 4
  11. Taik aT3
  12. T4 k N
  13. T5 T4 j
  14. T6 T5 4
  15. Takj a T6
  1. T7 Taik Takj
  2. sum sum T7
  3. k k 1
  4. if k lt n goto 8
  5. T8 i N
  6. T9 T8 k
  7. T10 T9 4
  8. c T10 sum
  9. j j 1
  10. if j lt n go to 5
  11. i i 1
  12. if i lt n goto 3
  13. return

9
Transformations on Basic Blocks
  • Common subexpression elimination
  • Dead-code elimination
  • Renaming temporary variables
  • Reordering independent statements
  • Code movement

10
Flow Graphs
  • Successor block
  • Predecessor block
  • Loops
  • Strongly connected
  • Inner loop

11
Computing Next Uses
  • Scan block backwards from last statement to
    leader
  • For statement (i) x y op z
  • Attach to statement I, current info from symbol
    table on the liveness of x y and z
  • In the symbol table Mark x not live no next
    use
  • In the symbol table change next uses of y and z
    to (i) statement number i

12
Basic Blocks
  • int looper(int n, inta)
  • int i
  • int x 0
  • for(i0 i lt x i)
  • if( x gt ai) x ai
  • x
  • return x

13
Basic Blocks in Assembly
  • looper
  • pushl ebp
  • movl esp, ebp
  • subl 8, esp
  • movl 0, -8(ebp)
  • movl 0, -4(ebp)
  • .L2
  • movl -4(ebp), eax
  • cmpl -8(ebp), eax
  • jge .L3
  • movl -4(ebp), eax
  • leal 0(,eax,4), edx
  • movl 12(ebp), eax
  • movl (edx,eax), eax
  • cmpl -8(ebp), eax
  • jge .L5
  • movl -4(ebp), eax
  • leal 0(,eax,4), edx
  • movl 12(ebp), eax
  • movl (edx,eax), eax
  • movl eax, -8(ebp)
  • .L5
  • leal -8(ebp), eax
  • incl (eax)
  • leal -4(ebp), eax
  • incl (eax)
  • jmp .L2
  • .L3
  • movl -8(ebp), eax
  • leave
  • ret

14
Quadruples for t2looper.c
15
Now Optimized!
  • looper
  • pushl ebp
  • movl esp, ebp
  • xorl eax, eax
  • popl ebp
  • ret
  • What happened?

16
Storage for Temporaries
  • implicitly defined identifiers
  • install in symbol table
  • Save space individually
  • After no longer live we can reuse space

17
Register Descriptors
  • Register descriptors what values are in a
    register at the moment
  • If x is in eax and y is in ebx then
  • After mov eax, ebx what is in ebx? eax?
  • Address descriptor for an identifier keep track
    of where the current value is stored
  • mov y, eax y is now in memory and in eax
  • Getreg function a function that will find a
    location (hopefully a register) for storing a
    result

18
A Simple Code Generation Algorithm
  • For each three address statement x y op z
  • Invoke getreg to find location L for result of y
    op z
  • Consult address descriptor for y to determine y
    the preferred location of y
  • If y is not already in L generate mov y, L
  • Generate OP z, L where z is the preferred
    location for the current value of z
  • Update address descriptor for x (now in L)
  • If L is a register update its descriptor
  • Remove x from other register descriptors
  • Alter register descriptors for y and z based on
    next uses

19
GetReg
Write a Comment
User Comments (0)
About PowerShow.com