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

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Course Outline Traditional Static Program Analysis Software Testing
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
1 CS 201 Compiler Construction Machine Code Generation.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
A simple register allocation optimization scheme.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
Code Generation Professor Yihjia Tsai Tamkang University.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
Compiler Construction
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Compilers Modern Compiler Design
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
CS 3214 Computer Systems Godmar Back Lecture 8. Announcements Stay tuned for Project 2 & Exercise 4 Project 1 due Sep 16 Auto-fail rule 1: –Need at least.
1 Assembly Language: Function Calls Jennifer Rexford.
Carnegie Mellon About the New Slides for Introduction to Computer Systems /18-243, spring 2009 Markus Püschel Electrical and Computer Engineering.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Compilation (Semester A, 2013/14)
Reading Condition Codes (Cont.)
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Conditional Branch Example
Homework In-line Assembly Code Machine Language
Code Optimization I: Machine Independent Optimizations
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 4 Procedures
Condition Codes Single Bit Registers
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 5 Structured Data
TARGET CODE -Next Usage
Machine-Level Programming 5 Structured Data
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
C structures and Compilation to IA32
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Code Generation Part II
Compiler Construction CS 606 Sohail Aslam Lecture 1.
Presentation transcript:

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

– 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 Code generation Error recovery: p 264, p Error recovery: p 264, p

– 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 – 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 – 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 – 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 – 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 – 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 – 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 – CSCE 531 Spring 2006 Flow Graphs Successor block Predecessor block Loops Strongly connected Inner loop

– 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 – 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 – 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 – CSCE 531 Spring 2006 Quadruples for t2looper.c

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