Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.

Slides:



Advertisements
Similar presentations
Code Optimization, Part II Regional Techniques Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Operator Strength Reduction From Cooper, Simpson, & Vick, “Operator Strength Reduction”, ACM TOPLAS, 23(5), See also § of EaC2e. 1COMP 512,
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
A Deeper Look at Data-flow Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University.
Program Representations. Representing programs Goals.
SSA-Based Constant Propagation, SCP, SCCP, & the Issue of Combining Optimizations 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon,
The Last Lecture Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission.
Lazy Code Motion Comp 512 Spring 2011
Partial Redundancy Elimination & Lazy Code Motion
Lazy Code Motion C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Loop Invariant Code Motion — classical approaches — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Scheduling II: Beyond Basic Blocks Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Global Common Subexpression Elimination with Data-flow Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Code Optimization, Part III Global Methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
First Principles (with examples from value numbering) C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Using SSA Dead Code Elimination & Constant Propagation C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Building SSA Form, III 1COMP 512, Rice University This lecture presents the problems inherent in out- of-SSA translation and some ways to solve them. Copyright.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Global Redundancy Elimination: Computing Available Expressions Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
11/23/2015© Hal Perkins & UW CSEQ-1 CSE P 501 – Compilers Introduction to Optimization Hal Perkins Autumn 2009.
Algebraic Reassociation of Expressions Briggs & Cooper, “Effective Partial Redundancy Elimination,” Proceedings of the ACM SIGPLAN 1994 Conference on Programming.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
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.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Instruction Selection, Part I Selection via Peephole Optimization Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Data flow analysis John Cavazos University.
Definition-Use Chains
Introduction to Optimization
Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm
Introduction to Optimization
Intermediate Representations
Introduction to Code Generation
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Code Shape III Booleans, Relationals, & Control flow
Introduction to Optimization Hal Perkins Summer 2004
Intermediate Representations
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
The Last Lecture COMP 512 Rice University Houston, Texas Fall 2003
Introduction to Parsing
Introduction to Parsing
Introduction to Optimization
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Algebraic Reassociation of Expressions COMP 512 Rice University Houston, Texas Fall 2003 P. Briggs & K.D. Cooper, “Effective Partial Redundancy Elimination,”
Compiler Construction
Lecture 19: Code Optimisation
The Partitioning Algorithm for Detecting Congruent Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Presentation transcript:

Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. COMP 412 FALL 2010 This lecture begins the material from Chapter 8 of EaC

Comp 412, Fall Traditional Three-Phase Compiler Optimization (or Code Improvement) Analyzes IR and rewrites (or transforms ) IR Primary goal is to reduce running time of the compiled code —May also improve space, power consumption, … Must preserve “meaning” of the code —Measured by values of named variables —A course (or two) unto itself Errors Source Code Optimizer Front End Machine code Back End IR

Comp 412, Fall The Optimizer Typical Transformations Discover & propagate some constant value Move a computation to a less frequently executed place Specialize some computation based on context Discover a redundant computation & remove it Remove useless or unreachable code Encode an idiom in some particularly efficient form Errors Opt1Opt1 Opt3Opt3 Opt2Opt2 OptnOptn... IR Modern optimizers are structured as a series of passes

Comp 412, Fall The Role of the Optimizer The compiler can implement a procedure in many ways The optimizer tries to find an implementation that is “better” —Speed, code size, data space, … To accomplish this, it Analyzes the code to derive knowledge about run-time behavior —Data-flow analysis, pointer disambiguation, … —General term is “static analysis” Uses that knowledge in an attempt to improve the code —Literally hundreds of transformations have been proposed —Large amount of overlap between them Nothing “optimal” about optimization Proofs of optimality assume restrictive & unrealistic conditions

Comp 412, Fall Redundancy Elimination as an Example An expression x+y is redundant if and only if, along every path from the procedure’s entry, it has been evaluated, and its constituent subexpressions ( x & y ) have not been re-defined. If the compiler can prove that an expression is redundant It can preserve the results of earlier evaluations It can replace the current evaluation with a reference Two pieces to the problem Proving that x+y is redundant, or available Rewriting the code to eliminate the redundant evaluation One technique for accomplishing both is called value numbering Assume a low-level, linear IR such as ILOC

Comp 412, Fall Value Numbering The key notion Assign an identifying number, V( n ), to each expression —V(x+y) = V(j) iff x+y and j always have the same value —Use hashing over the value numbers to make it efficient Use these numbers to improve the code Improving the code Replace redundant expressions —Same VN  refer rather than recompute Simplify algebraic identities Discover constant-valued expressions, fold & propagate them Technique designed for low-level, linear IR s, similar methods exist for trees (e.g., build a DAG) Local algorithm due to Balke (1968) or Ershov (1954) Within a basic block; definition becomes more complex across blocks

Comp 412, Fall Local Value Numbering The Algorithm For each operation o = in the block, in order 1 Get value numbers for operands from hash lookup 2 Hash to get a value number for o 3 If o already had a value number, replace o with a reference 4 If o 1 & o 2 are constant, evaluate it & replace with a loadI If hashing behaves, the algorithm runs in linear time —If not, use multi-set discrimination t or acyclic DFAs tt Handling algebraic identities Case statement on operator type Handle special cases within each operator t see p. 251 in EaC tt DFAs for REs without closure can be built online to provide a “perfect hash”

Comp 412, Fall Local Value Numbering An example With VNs a 3  x 1 + y 2  b 3  x 1 + y 2 a 4  17  c 3  x 1 + y 2 Rewritten a 3  x 1 + y 2  b 3  a 3 a 4  17  c 3  a 3 (oops!) Options Use c 3  b 3 Save a 3 in t 3 Rename around it Options Use c 3  b 3 Save a 3 in t 3 Rename around it Original Code a  x + y  b  x + y a  17  c  x + y Two redundancies Eliminate stmts with a  Coalesce results ? Two redundancies Eliminate stmts with a  Coalesce results ?

Comp 412, Fall Local Value Numbering Example ( continued ): With VNs a 0 3  x y 0 2  b 0 3  x y 0 2 a 1 4  17  c 0 3  x y 0 2 Notation: While complex, the meaning is clear Notation: While complex, the meaning is clear Original Code a 0  x 0 + y 0  b 0  x 0 + y 0 a 1  17  c 0  x 0 + y 0 Renaming: Give each value a unique name Makes it clear Renaming: Give each value a unique name Makes it clear Rewritten a 0 3  x y 0 2  b 0 3  a 0 3 a 1 4  17  c 0 3  a 0 3 Result: a 0 3 is available Rewriting now works Result: a 0 3 is available Rewriting now works

Comp 412, Fall Local Value Numbering The Algorithm For each operation o = in the block, in order 1 Get value numbers for operands from hash lookup 2 Hash to get a value number for o 3 If o already had a value number, replace o with a reference 4 If o 1 & o 2 are constant, evaluate it & replace with a loadI Complexity & Speed Issues  “Get value numbers” — linear search versus hash  “Hash ” — linear search versus hash  Copy folding — set value number of result  Commutative ops — double hash versus sorting the operands asymptotic constants

Comp 412, Fall Simple Extensions to Value Numbering Constant folding Add a bit that records when a value is constant Evaluate constant values at compile-time Replace with load immediate or immediate operand No stronger local algorithm Algebraic identities Must check (many) special cases Replace result with input VN Build a decision tree on operation Identities (on VNs) x  y, x+0, x-0, x  1, x÷1, x-x, x  0, x  x, x  0, x  0x FF … FF, max(x, MAXINT ), min(x, MININT ), max(x,x), min(y,y), and so on... Identities (on VNs) x  y, x+0, x-0, x  1, x÷1, x-x, x  0, x  x, x  0, x  0x FF … FF, max(x, MAXINT ), min(x, MININT ), max(x,x), min(y,y), and so on...

Comp 412, Fall Missed opportunities (need stronger methods) m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F Value Numbering Local Value Numbering  1 block at a time  Strong local results  No cross-block effects LVN finds these redundant ops