1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts.

Slides:



Advertisements
Similar presentations
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Advertisements

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.
C Chuen-Liang Chen, NTUCS&IE / 321 OPTIMIZATION Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Procedures and Control Flow CS351 – Programming Paradigms.
1 Subroutines and Control Abstraction. 2 Control Abstraction Abstraction Abstraction associate a name N to a program part P associate a name N to a program.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 36: Global Optimization, Loop Improvement and Profiling COMP 144 Programming Language.
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 31: Building a Runnable Program COMP 144 Programming Language Concepts Spring 2002.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Previous finals up on the web page use them as practice problems look at them early.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Intermediate Code. Local Optimizations
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
Improving Code Generation Honors Compilers April 16 th 2002.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Chapter 8 :: Subroutines and Control Abstraction
5.3 Machine-Independent Compiler Features
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Lecture 19: Control Abstraction (Section )
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Copyright 2014 – Noah Mendelsohn Code Tuning Noah Mendelsohn Tufts University Web:
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
More Code Generation and Optimization Pat Morin COMP 3002.
Lecture 3 Translation.
Code Optimization Overview and Examples
Names and Attributes Names are a key programming language feature
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Optimization Code Optimization ©SoftMoore Consulting.
For Example: User level quicksort program Three address code.
Programming Languages (CS 550) Mini Language Compiler
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Understanding Program Address Space
Code Optimization Overview and Examples Control Flow Graph
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
CSC 533: Programming Languages Spring 2019
Programming Languages (CS 360) Mini Language Compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 19 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Subroutine In-line Expansion Subroutines may be expanded in-line at the point of the callSubroutines may be expanded in-line at the point of the call –Rather than use a stack-based calling convention In-line expansion saves subroutine overheads and help code improvementIn-line expansion saves subroutine overheads and help code improvement In-line expansion may be decided by the compiler based on some optimization heuristicsIn-line expansion may be decided by the compiler based on some optimization heuristics –E.g. short, non-recursive subroutines are always in-lined in some languages

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Subroutine In-line Expansion In-line expansion can also be suggested by the programmerIn-line expansion can also be suggested by the programmer –E.g. C++ inline int max (int a, int b) { return a > b ? a : b; } –E.g. Ada function max(a, b : integer) return integer is begin if a > b then return a; else return b; end if; end max; pragma inline (max);

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Macros and In-line Expansion What is the difference between a macro and a programmer suggested expansion?What is the difference between a macro and a programmer suggested expansion? –Optional in the second case –Most importantly, in-line expansion is an implementation technique with no effect in program semantics E.g.E.g. #define MAX(a,b) ((a) > (b) ? (a) : (b)) –No type checking –What happens after MAX(x++, y++) ? –The larger argument is incremented twice

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos In-line Expansion In-line expansion has some disadvantagesIn-line expansion has some disadvantages –Increase in code size –It cannot be used with recursive subroutines It is sometimes useful to expand the first case in a recursion subroutineIt is sometimes useful to expand the first case in a recursion subroutine –Optimize the common case rule Most hash chains are only one bucket long

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example Control Flow Graph Basic blocks are maximal-length set of sequential operationsBasic blocks are maximal-length set of sequential operations –Operations on a set of virtual registers »Unlimited »A new one for each computed value Arcs represent interblock control flowArcs represent interblock control flow

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Redundancy Elimination in Basic Blocks We will consider the example on the rightWe will consider the example on the right It computes the binomial coefficientsIt computes the binomial coefficients for 0  m  n It is based onIt is based on

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Syntax Tree for the combinations subroutine

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Naïve Control Flow Graph for the combinations subroutine

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Naïve Control Flow Graph Uses virtual registersUses virtual registers –A new register for each new value ra is the return addres, fp is the frame pointerra is the return addres, fp is the frame pointer n, A, I and t perform the appropriate displacement addressing with respect to the stack pointer (sp) registern, A, I and t perform the appropriate displacement addressing with respect to the stack pointer (sp) register Parameter passing using r4 and r5Parameter passing using r4 and r5

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Value Numbering How can we eliminate redundant loads and computations?How can we eliminate redundant loads and computations? –Expression DAG –Value numbering In value numbering, the compilers assigns the same name (i.e., number) to any two or symbolically equivalent computations (i.e., values)In value numbering, the compilers assigns the same name (i.e., number) to any two or symbolically equivalent computations (i.e., values) A dictionary is used to keep track of values that have already been loaded or computedA dictionary is used to keep track of values that have already been loaded or computed

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Value Numbering If a value is already in a register, reuse that registerIf a value is already in a register, reuse that register –E.g., the load instruction can be eliminated vi := x if the value x is already in register vj »Replace all uses of vi by vj Similarly, we can get rid of small constants using immediate valueSimilarly, we can get rid of small constants using immediate value

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Value Numbering In vi := vj op vk, we can use constant folding if the values in vj and vk are known to be constantsIn vi := vj op vk, we can use constant folding if the values in vj and vk are known to be constants –Local constant folding and constant propagation –At the same time, strength reduction and useless abstraction elimination A key that combine the registers and the operator is used to keep track of the previous operation in the dictionaryA key that combine the registers and the operator is used to keep track of the previous operation in the dictionary

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Control Flow Graph for combinations after local redundancy elimination and strength reduction

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott –Sect –Ch. 13.3

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

21 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques