Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
CS 31003: Compilers Introduction to Phases of Compiler.
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-
Program Representations. Representing programs Goals.
CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Intermediate Code. Local Optimizations
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
COP4020 Programming Languages
Chapter 1. Introduction.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Code Optimization Overview and Examples
Compiler Design (40-414) Main Text Book:
High-level optimization Jakub Yaghob
Code Optimization.
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Optimization Code Optimization ©SoftMoore Consulting.
CSC D70: Compiler Optimization
Compiler Construction
Course supervisor: Lubna Siddiqui
Compiler 薛智文 TH 6 7 8, DTH Spring.
Code Optimization Overview and Examples Control Flow Graph
8 Code Generation Topics A simple code generator algorithm
Intermediate Code Generation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Code Generation Part II
Code Optimization.
Presentation transcript:

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 1 Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen Melikyan

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 2 Course Overview Introduction: Overview of Optimizations  1 lecture Intermediate-Code Generation  2 lectures Machine-Independent Optimizations  3 lectures Code Generation  2 lectures

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 3 Introduction: Overview of Optimizations

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 4 The Function of Compilers Translate program in one language to executable program in other language.  Typically lower abstraction level E.g., convert C++ into ( x86, SPARC, HP PA, IBM PPC) object code Optimize the Code  E.g., make the code run faster (transforms a computation to an equivalent but better form ) Difference between optimizing and non-optimizing compiler ~ 4x ( Proebsting’s law )  “Optimize” is a bit of a misnomer, the result is not actually optimal

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 5 The Structure of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate Code Generator Machine-Independent Code Optimizer Source Code ( C, C++, Java, Verilog ) Target Machine Code ( Alpha, SPARC, x86, IA-64 ) Error handlerSymbol-table Code generator Machine-Dependent Code Optimizer

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 6 The Structure of a Compiler: Work Example Lexical analyzer Syntax analyzer Semantic analyzer Intermediate Code Generator Code generator position = initial + rate*60 = + * 60 = + * inttoflat 60 t1 = inttofloat (60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 Code Optimizer t1 = id3 * 60.0 id1 = id2 + t1 LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id1, R1

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 7 Lexical Analyzer The first phase of a compiler is called lexical analysis or scanning. The lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexemes. For each lexeme, the lexical analyzer produces as output a token of the form: token-name - abstract symbol that is used during syntax analysis. attribute-value - points to an entry in the symbol table for this token. Information from the symbol-table entry is needed for semantic analysis and code generation.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 8 Syntax Analyzer: Parser The second phase of the compiler is syntax analysis or parsing. The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream. A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 9 Semantic Analyzer The semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition. Gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation. An important part of semantic analysis is type checking, where the compiler checks that each operator has matching operands.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 10 How Compiler Improves Performance Execution time = Operation count * Machine cycles per operation Minimize the number of operations  Arithmetic operations, memory accesses Replace expensive operations with simpler ones  E.g., replace 4-cycle multiplication with1-cycle shift Minimize cache misses  Both data and instruction accesses Perform work in parallel  Instruction scheduling within a thread  Parallel execution across multiple threads

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 11 Global Steps of Optimization Formulate optimization problem:  Identify opportunities of optimization Representation:  Control-flow graph  Control-dependence graph  Def/use, use/def chains  SSA (Static Single Assignment) Analysis:  Control-flow  Data-flow Code Transformation Experimental Evaluation (and repeat process)

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 12 Other Optimization Goals Besides Performance Minimizing power and energy consumption Finding (and minimizing the impact of ) software bugs  Security vulnerabilities  Subtle interactions between parallel threads Increasing reliability, fault-tolerance

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 13 Types of Optimizations Peephole Local Global Loop Interprocedural, whole-program or link-time Machine code Data-flow SSA-based Code generator Functional language

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 14 Other Optimizations Bounds-checking elimination Dead code elimination Inline expansion or macro expansion Jump threading Macro compression Reduction of cache collisions Stack height reduction

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 15 Basic Blocks Basic blocks are maximal sequences of consecutive three-address instructions.  The flow of control can only enter the basic block through the first instruction in the block. (no jumps into the middle of the block )  Control will leave the block without halting or branching, except possibly at the last instruction in the block. The basic blocks become the nodes of a flow graph, whose edges indicate which blocks can follow which other blocks.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 16 Partitioning Three-address Instructions into Basic Blocks 4 Input: A sequence of three-address instructions Output: A list of the basic blocks for that sequence in which each instruction is assigned to exactly one basic block Method: Determine instructions in the intermediate code that are leaders: the first instructions in some basic block (instruction just past the end of the intermediate program is not included as a leader) The rules for finding leaders are: 1.The first three-address instruction in the intermediate code 2.Any instruction that is the target of a conditional or unconditional jump 3.Any instruction that immediately follows a conditional or unconditional jump

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 17 Partitioning Three-address Instructions into Basic Blocks: Example 1.i = 1 2.j = 1 3.t1 = 10 * i 4.t2 = t1 + j 5.j = j if j <= 10 goto (3) 7.i = i if i <= 10 goto (2) 9.i = 1 10.t3 = i – 1 11.if i <= 10 goto (10) First, instruction 1 is a leader by rule (1). Jumps are at instructions 6, 8, and 11. By rule (2), the targets of these jumps are leaders ( instructions 3, 2, and 10, respectively) By rule (3), each instruction following a jump is a leader; instructions 7 and 9. Leaders are instructions 1, 2, 3, 7, 9 and 10. The basic block of each leader contains all the instructions from itself until just before the next leader.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 18 Flow Graphs Flow Graph is the representation of control flow between basic blocks. The nodes of the flow graph are the basic blocks. There is an edge from block B to block C if and only if it is possible for the first instruction in block C to immediately follow the last instruction in block B. There are two ways that such an edge could be justified: 1. There is a conditional or unconditional jump from the end of B to the beginning of C. 2. C immediately follows B in the original order of the three- address instructions, and B does not end in an unconditional jump. B is a predecessor of C, and C is a successor of B.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 19 Flow Graphs: Example Flow Graph Example of program in Example(1). The block led by first statement of the program is the start, or entry node. B1: i = 1 B2: j = 1 B3: t1 = 10 * i t2 = t1 + j j = j + 1 if j <= 10 goto (3) B4: i = i + 1 if i <= 10 goto (2) B5: i = 1 B6: t3 = i – 1 if i <= 10 goto (10) Entry Exit

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 20 Flow Graphs: Representation Flow graphs, being quite ordinary graphs, can be represented by any of the data structures appropriate for graphs. The content of a node (basic block) might be represented by a pointer to the leader in the array of three-address instructions, together with a count of the number of instructions or a second pointer to the last instruction. Since the number of instructions may be changed in a basic block frequently, it is likely to be more efficient to create a linked list of instructions for each basic block.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 21 Local Optimizations Analysis and transformation performed within a basic block No control flow information is considered Examples of local optimizations:  Local common sub expression elimination analysis: same expression evaluated more than once in b. transformation: replace with single calculation  Local constant folding or elimination analysis: expression can be evaluated at compile time transformation: replace by constant, compile-time value  Dead code elimination

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 22 Global Optimizations: Intraprocedural Global versions of local optimizations  Global common sub-expression elimination  Global constant propagation  Dead code elimination Loop optimizations  Reduce code to be executed in each iteration  Code motion  Induction variable elimination Other control structures  Code hoisting: eliminates copies of identical code on parallel paths in a flow graph to reduce code size.

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 23 Induction Variable Elimination Intuitively  Loop indices are induction variables (counting iterations)  Linear functions of the loop indices are also induction variables (for accessing arrays) Analysis: detection of induction variable Optimizations  Strength reduction: replace multiplication by additions  Elimination of loop index: replace termination by tests on other induction variables

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 24 Loop Invariant Code Motion Analysis  A computation is done within a loop and result of the computation is the same as long as keep going around the loop Transformation  Move the computation outside the loop a = b + c t = b + c a = t

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 25 Loop Fusion (1) Loop fusion, also called loop jamming, is a compiler optimization, a loop transformation, which replaces multiple loops with a single one. After Loop Fusion int i, a[100], b[100]; for (i = 0; i < 100; i++) { a[i] = 1; b[i] = 2; } Original Loop int i, a[100], b[100]; for (i = 0; i < 100; i++) { a[i] = 1; } for (i = 0; i < 100; i++) { b[i] = 2; }

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 26 Loop Fusion (2) Loop fission (or loop distribution) is a compiler optimization technique attempting to break a loop into multiple loops over the same index range but each taking only a part of the loop's body. The goal is to break down large loop body into smaller ones to achieve better utilization of locality of reference. It is the reverse action to loop fusion. This optimization is most efficient in multi-core processors that can split a task into multiple tasks for each processor. After Loop Fission int i, a[100], b[100]; for (i = 0; i < 100; i++) { a[i] = 1; } for (i = 0; i < 100; i++) { b[i] = 2; } Original Loop int i, a[100], b[100]; for (i = 0; i < 100; i++) { a[i] = 1; b[i] = 2; }

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By: Vazgen Melikyan 27 Predictable Success