Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small.

Slides:



Advertisements
Similar presentations
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Advertisements

CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
Carnegie Mellon Lecture 7 Instruction Scheduling I. Basic Block Scheduling II.Global Scheduling (for Non-Numeric Code) Reading: Chapter 10.3 – 10.4 M.
1 CS 201 Compiler Construction Machine Code Generation.
CS 6461: Computer Architecture Basic Compiler Techniques for Exposing ILP Instructor: Morris Lancaster Corresponding to Hennessey and Patterson Fifth Edition.
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.
Code optimization: –A transformation to a program to make it run faster and/or take up less space –Optimization should be safe, preserve the meaning of.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
C Chuen-Liang Chen, NTUCS&IE / 321 OPTIMIZATION Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
1 Classical Optimization Types of classical optimizations  Operation level: one operation in isolation  Local: optimize pairs of operations in same basic.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 5 Program Design and Analysis.
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
11/19/2002© 2002 Hal Perkins & UW CSEN-1 CSE 582 – Compilers Instruction Selection Hal Perkins Autumn 2002.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
CS 536 Spring Code generation I Lecture 20.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Software Pipelining in Pegasus/CASH Cody Hartwig Elie Krevat
University of Maryland Compiler-Assisted Binary Parsing Tugrul Ince PD Week – 27 March 2012.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Network Coding Testbed Jeremy Bergan, Ben Green, Alex Lee.
UNIT - 1Topic - 3. Computer software is a program that tells a computer what to do. Computer software, or just software, is any set of machine-readable.
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.
Instruction Selection and Scheduling. The Problem Writing a compiler is a lot of work Would like to reuse components whenever possible Would like to automate.
CPSC 388 – Compiler Design and Construction Optimization.
Compilers for Embedded Systems Ram, Vasanth, and VJ Instructor : Dr. Edwin Sha Synthesis and Optimization of High-Performance Systems.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
RISC and CISC. What is CISC? CISC is an acronym for Complex Instruction Set Computer and are chips that are easy to program and which make efficient use.
Computer Science 313 – Advanced Programming Topics.
Survey of Program Compilation and Execution Bangor High School Ali Shareef 2/28/06.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Intermediate Code Representations
Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation:  Have each value in a register when it is used. Instruction selection.
COMPILERS Instruction Selection hussein suleman uct csc305h 2005.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Some string optimization tips Haiyang Yu /14 Outline  Background  Tips for dealing with strings.
COMPILERS Instruction Selection hussein suleman uct csc3003s 2007.
Instruction Selection, Part I Selection via Peephole Optimization Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Computer Organization Instructions Language of The Computer (MIPS) 2.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 614: Theory and Construction of Compilers Lecture 8 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
What Do Compilers Do 1 A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages. Ignore.
©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.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
COMPILERS Instruction Selection
Optimization Code Optimization ©SoftMoore Consulting.
Eugene Gavrin – MSc student
Computer Programming Machine and Assembly.
MIPS Instructions.
المدخل إلى تكنولوجيا التعليم في ضوء الاتجاهات الحديثة
Compiler Construction
Instruction Selection Hal Perkins Autumn 2005
The Big 6 Research Model Step 3: Location and Access
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
CSc 453 Final Code Generation
Intermediate Code Generating machine-independent intermediate form.
Code Optimization.
Presentation transcript:

Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small sequence (peephole) of code at a time. Does not require “expensive” dataflow analysis. Does not require “expensive” dataflow analysis. Pattern match for improvements Pattern match for improvements

Possible Patterns Store r0, a Load r0, a r5 = r5 r7 = r5 r6 = r7 r7 = whatever Others?

Example – C Code X = A + B * C – (D * A)

Example – Intermediate Code t1 = D * A t2 = B * C t3 = A + t2 t4 = t3 – t1 X = t4

Example – “Assembly” Code r5 = *(fp + D  offset) r6 = *(fp + A  offset) r7 = r5 * r6 *(fp + t1  offset) = r7 r5 = *(fp + B  offset) r6 = *(fp + C  offset) r7 = r5 * r6 *(fp + t2  offset) = r7

“Assembly” Code (cont.) “Assembly” Code (cont.) r5 = *(fp + A  offset) r6 = *(fp + t2  offset) r7 = r5 + r6 *(fp + t3  offset) = r7 r5 = *(fp + t3  offset) r6 = *(fp + t1  offset) r7 = r6 – r5 *(fp + t4  offset) = r7 r5 = *(fp + t4  offset) *(fp + X  offset) = r5

Assembler After Peephole r5 = *(fp + D  offset) r6 = *(fp + A  offset) r7 = r5 * r6 *(fp + t1  offset) = r7 r5 = *(fp + B  offset) r6 = *(fp + C  offset) r7 = r5 * r6 *(fp + t2  offset) = r7

After Peephole (cont.) After Peephole (cont.) r5 = *(fp + A  offset) r6 = *(fp + t2  offset) r7 = r5 + r7 *(fp + t3  offset) = r7 r5 = *(fp + t3  offset) r6 = *(fp + t1  offset) r7 = r6 – r7 *(fp + t4  offset) = r7 r5 = *(fp + t4  offset) *(fp + X  offset) = r7

So How big a peephole do we need? How big a peephole do we need? How could you represent code to ease How could you represent code to ease Re-write your code to generate intermediate Re-write your code to generate intermediate Do peephole optimization Do peephole optimization Generate CMachine code from intermediate Generate CMachine code from intermediate

Instruction Selection by Peephole Optimization Represent each instruction by RTL Represent each instruction by RTL Look for pairs of instructions whose combined RTL == that of a single instruction Look for pairs of instructions whose combined RTL == that of a single instruction PO, Hop, Chop (late 1970s – early 1980s) PO, Hop, Chop (late 1970s – early 1980s) Used for code selection by GNU compilers Used for code selection by GNU compilers