Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: 9.4-9.6 April 19, 2006 CSCE 531 Compiler Construction.

Similar presentations


Presentation on theme: "Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: 9.4-9.6 April 19, 2006 CSCE 531 Compiler Construction."— Presentation transcript:

1 Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: 9.4-9.6 April 19, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Overview Last Time – Lec24 slides 1-15 Finishing touches on Project 5 RET E.place What’s in PROLOGUE/EPILOGUE Overview of Code Generation Instruction selection Basic Blocks Today’s Lecture Questions on Project 5 – Functions Code Generation for Basic Blocks Register Allocation Optimizations Error RecoveryReferences: Code generation 9.4-9.6 Code generation 9.4-9.6 Error recovery: p 264, p 364-365 Error recovery: p 264, p 364-365

3 – 3 – CSCE 531 Spring 2006 Review: Instruction selection Choosing instructions / sequences of instructions to minimize some metric Example: double a[6][14]; double a[6][14]; In addressing a[i][j], &a[0][0] + (i*numcols + j)* w In addressing a[i][j], &a[0][0] + (i*numcols + j)* w Need to multiply by 14 and 8 Need to multiply by 14 and 8 8 handled by the scale in IA32 Mult by 14 Mult by 14 Shift copy of value (i) 3 bits to the left to obtain 8*i (8 = 2 3 ) Subtract original (i) from 8i to get 7*i, then shift one place to the left to get 14*i

4 – 4 – CSCE 531 Spring 2006 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 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 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 A name (identifier) is said to be live at a given point if its value is after that point in the program

5 – 5 – CSCE 531 Spring 2006 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 – 6 – CSCE 531 Spring 2006 Example fig 9.8  prod := 0  k := 1  t1 :=4 * k  t2 := a [ t1 ]  t3 := 4 * k  t4 := a [ t1 ]  t5 := t2 * t4  t6 := prod + t5  prod := t6  t7 := k + 1  k := t7 + 1  If k <= 20 goto 3

7 – 7 – CSCE 531 Spring 2006 Example matrix multiply void matmult(int n, int a[N][N], int b[N][N], int c[N][N]){ int i, j, k, n, sum; int i, j, k, n, sum; for (i=0; i<n; i++) { for (i=0; i<n; i++) { for (j=0; j<n; j++) { for (j=0; j<n; j++) { sum = 0; sum = 0; for (k=0; k<n; k++) { /* inner loop */ for (k=0; k<n; k++) { /* inner loop */ sum = sum + a[i][k] * b[k][j]; sum = sum + a[i][k] * b[k][j]; } c[i][j] = sum; c[i][j] = sum; } }}

8 – 8 – CSCE 531 Spring 2006 Matmult – Naïve Basic blocks  i := 0  if i >= n goto 28  j := 0  if j >= n goto 26  sum := 0  k := 0  if k >= n goto 20  T1 := i * N  T2 := T1 + k  T3 := T2 * 4  Taik := a[T3]  T4 := k * N  T5 := T4 + j  T6 := T5 * 4  Takj := a [ T6 ]  T7 := Taik * Takj  sum := sum + T7  k := k + 1  if k < n goto 8  T8 := i * N  T9 := T8 + k  T10 := T9 * 4  c [ T10 ] := sum  j := j + 1  if j < n go to 5  i := i + 1  if i < n goto 3  return

9 – 9 – CSCE 531 Spring 2006 Transformations on Basic Blocks Common subexpression elimination Common subexpression elimination Dead-code elimination Dead-code elimination Renaming temporary variables Renaming temporary variables Reordering independent statements Reordering independent statements Code movement Code movement

10 – 10 – CSCE 531 Spring 2006 Flow Graphs Successor block Predecessor block Loops Strongly connected Inner loop

11 – 11 – CSCE 531 Spring 2006 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 – 12 – CSCE 531 Spring 2006 Basic Blocks int looper(int n, int*a) { int i; int i; int x = 0; int x = 0; for(i=0; i < x; ++i){ for(i=0; i < x; ++i){ if( x > a[i]) x = a[i]; if( x > a[i]) x = a[i]; x++; x++; } return x; return x;}

13 – 13 – CSCE 531 Spring 2006 Basic Blocks “in Assembly” looper: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp subl $8, %esp subl $8, %esp movl $0, -8(%ebp) movl $0, -8(%ebp) movl $0, -4(%ebp) movl $0, -4(%ebp).L2: movl -4(%ebp), %eax movl -4(%ebp), %eax cmpl -8(%ebp), %eax cmpl -8(%ebp), %eax jge.L3 jge.L3 movl -4(%ebp), %eax movl -4(%ebp), %eax leal 0(,%eax,4), %edx leal 0(,%eax,4), %edx movl 12(%ebp), %eax movl 12(%ebp), %eax movl (%edx,%eax), %eax movl (%edx,%eax), %eax cmpl -8(%ebp), %eax cmpl -8(%ebp), %eax jge.L5 jge.L5 movl -4(%ebp), %eax movl -4(%ebp), %eax leal 0(,%eax,4), %edx leal 0(,%eax,4), %edx movl 12(%ebp), %eax movl 12(%ebp), %eax movl (%edx,%eax), %eax movl (%edx,%eax), %eax movl %eax, -8(%ebp) movl %eax, -8(%ebp).L5: leal -8(%ebp), %eax leal -8(%ebp), %eax incl (%eax) incl (%eax) leal -4(%ebp), %eax leal -4(%ebp), %eax incl (%eax) incl (%eax) jmp.L2 jmp.L2.L3: movl -8(%ebp), %eax movl -8(%ebp), %eax leave leave ret ret

14 – 14 – CSCE 531 Spring 2006 Quadruples for t2looper.c

15 – 15 – CSCE 531 Spring 2006 Now Optimized! looper: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp xorl %eax, %eax xorl %eax, %eax popl %ebp popl %ebp ret ret What happened?

16 – 16 – CSCE 531 Spring 2006 Storage for Temporaries “implicitly defined identifiers” “implicitly defined identifiers” install in symbol table Save space individually After no longer live we can reuse space After no longer live we can reuse space

17 – 17 – CSCE 531 Spring 2006 Register Descriptors Register descriptors – what values are in a register at the moment 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 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 Getreg function – a function that will find a location (hopefully a register) for storing a result

18 – 18 – CSCE 531 Spring 2006 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 – 19 – CSCE 531 Spring 2006 GetReg


Download ppt "Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: 9.4-9.6 April 19, 2006 CSCE 531 Compiler Construction."

Similar presentations


Ads by Google