Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Similar documents
CS293S Redundancy Removal. Yufei Ding

Intermediate representation

CS415 Compilers. Intermediate Represeation & Code Generation

CS 406/534 Compiler Construction Putting It All Together

Lecture 3 Local Optimizations, Intro to SSA

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Introduction to Optimization. CS434 Compiler Construction Joel Jones Department of Computer Science University of Alabama

Introduction to Optimization Local Value Numbering

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Programming Language Processor Theory

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Intermediate Representations

Intermediate Representations

Intermediate Representations Part II

Intermediate Representations

Intermediate Code & Local Optimizations. Lecture 20

Compiler Design Spring 2017

Control-Flow Analysis

Intermediate Representation (IR)

The Static Single Assignment Form:

SSA, CPS, ANF: THREE APPROACHES

Compiler Design (40-414)

Intermediate Representations

EECS 583 Class 8 Classic Optimization

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Goals of Program Optimization (1 of 2)

Code Shape II Expressions & Assignment

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

Instruction Selection, II Tree-pattern matching

6. Intermediate Representation!

Principles of Compiler Design

fast code (preserve flow of data)

Intermediate Code & Local Optimizations

6. Intermediate Representation

CS5363 Final Review. cs5363 1

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Calvin Lin The University of Texas at Austin

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

ECE573 Introduction to Compilers & Translators

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

A Sparse Algorithm for Predicated Global Value Numbering

Code Generation. Lecture 30

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

Example. Example. Constant Propagation in SSA

Compiler Construction LECTURE # 1

Lecture Compiler Middle-End

IR Optimization. May 15th, Tuesday, May 14, 13

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Instruction Selection: Preliminaries. Comp 412

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CA Compiler Construction

Compilers Crash Course

CS 432 Fall Mike Lam, Professor. Code Generation

An Overview of Compilation

Control Flow Analysis

Advanced C Programming

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Induction Variable Identification (cont)

Parsing II Top-down parsing. Comp 412

Intermediate Representations

Code Optimization. Code Optimization

Introduction to Machine-Independent Optimizations - 6

Compilers and Code Optimization EDOARDO FUSELLA

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Compiler Optimization Techniques

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

Compiler Optimization Intermediate Representation

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

Data Flow Analysis. Program Analysis

CSE 12 Abstract Syntax Trees

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Lecture Notes on Static Single Assignment Form

Control Flow Analysis

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

CSE 401/M501 Compilers

Intermediate Representations & Symbol Tables

Compilers. Compiler Construction Tutorial The Front-end

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Combining Analyses, Combining Optimizations - Summary

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Intermediate Code Generation

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

CS 406/534 Compiler Construction Instruction Scheduling

Lecture Notes on Common Subexpression Elimination

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Control-Flow Graph and. Local Optimizations

EECS 6083 Intro to Parsing Context Free Grammars

Transcription:

Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1

What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce a result Writing a compiler makes large use of different data structures such as graphs, trees, and sets 2

High Level Organization Source Program Front End IR Optimizer IR Back End Target Program 3

Front End IR Data Structures Abstract Syntax Tree Created during parsing and usually replaced by another IR that is better for analysis Control Flow Graph Usually remains throughout the life of the compiler Back End Data flow analysis Sets Created and destroyed during compiler optimization. Attached to nodes of the CFG to hold facts about the program at that point in the graph 4

Intermediate Representations Graph Based - the program is represented as a graph. Example: AST, DAG Linear - the program is represented as a straight line sequence of instructions Example: assembly code with jumps and branches for an abstract machine Hybrid - the program is represented as a combination of linear and graph structures. 5

original code AST IR q = a + d; if false then e = b + 8; else e = a + 7; Parsing v = a + d; P = if = q + F = = v + a d e + e + a d a 8 a 7 6

Linear IR original code q = a + d; if false then e = b + 8; else e = a + 7; v = a + d; Parsing Translation ADD q a d LDi c 1 BReq A B c A: ADDi e b 8 JMP C B: ADDi e a 7 JMP C C: ADD v a d 7

Hybrid IR (Control Flow Graph) original code q = a + d if false then e = b + 8 else e = a + 7 v = a + d Parsing + Translation ADD q a d LDi c 1 c = 0 c = 1 ADDi e b 8 ADDi e a 7 JMP ADD v a d JMP 8

Control Flow Graph Terminology Nodes are basic blocks Edges represent control flow instructions ADD q a d ADD b a d Basic block - maximal sequence of straight line instructions ADDi e b 8 ADDi e a 7 ADD v a d If one instruction in the basic block executes they all execute 9

CFG Cont. Extended Basic Blocks Sequence of basic blocks such that each block (except the first) has a single predecessor B A D C E Forms a tree rooted at the entry to the EBB G F 10

CFG Cont. Extended Basic Blocks Sequence of basic blocks such that each block (except the first) has a single predecessor B A D C E Forms a tree rooted at the entry to the EBB G F EBB EBB EBB 10

SSA Form (Static Single Assignment) A particular way of choosing variable names in the intermediate representation q = a + d q = g + h q0 = a0 + d0 q1 = g0 + h0 e = b + 8 e = a + 7 SSA e0 = b0 + 8 e1 = a0 + 7 v = e + d e2 = ϕ(e0, e1) v0 = e2 + d0 11

SSA Form Two invariants that characterize SSA Each variable is defined (assigned a value) exactly once Every use refers to exactly one definition Encodes information about control flow and data flow into the variable names in the program Phi-Nodes indicate join points in the CFG Variable names show where a particular definition is used 12

Why Use SSA? Simplifies compiler optimizations by providing strong links between definitions and uses Single transformation can be used for analysis in many data flow problems Makes detecting certain errors trivial (e.g. variable used before it is initialized) 13

Compiler Optimization Optimization is a misused word, we are really talking about transformations of the IR Scope Local - within a single basic block Superlocal - within an extended block Global - within an entire procedure Interprocedural - between procedures 14

Example Optimization: Value Numbering This optimization finds and eliminates redundant computations (common subexpression elimination) Instead of recomputing an answer we save the value and reuse it in a later computation This is a standard optimization performed by many compilers 15

Local Value Numbering Original Code Value Numbered a := b + c b := a - d d := a - d Value Number a 3 := b 1 + c 2 b 5 := a 3 - d 4 d 5 := a 3 - d 4 Transformed Code Rewrite a := b + c b := a - d d := b 16

Value Numbering Technique originally designed for linear IRs, graphical IRs would use a DAG for this optimization instead Each expression is assigned a value number Value number is computed as a hash of value numbers in the expression and the operands Hashtable maps expressions to value 17

Value Numbering Algorithm for each instruction (assume instruction is of the form x := y op z) look up the value numbers of y and z build the hash key yvn op zvn lookup key in the hash if key in hash replace the instruction with a copy operation record value number for x else add key to hash with a new value number 18

Local Value Numbering (a problem) Original Code a := b + c b := a - d b := d + a d := a - d Value Number Value Numbered a 3 := b 1 + c 2 b 5 := a 3 - d 4 b 6 := a 3 + d 4 d 5 := a 3 - d 4 Transformed Code Rewrite a := b + c b := a - d b := a + d d :=??? 19

Local Value Numbering With SSA Original Code Value Numbered a0 := b0 + c0 b1 := a0 - d0 b2 := d0 + a0 d1 := a0 - d0 Value Number a0 3 := b0 1 + c0 2 b1 5 := a0 3 - d0 4 b2 6 := a0 3 + d0 4 d1 5 := a0 3 - d0 4 Transformed Code Rewrite a0 := b0 + c0 b1 := a0 - d0 b2 := d0 + a0 d1 := b1 20

Superlocal Value Numbering Operates over extended basic blocks (EBBs) Allows us to capture more redundant computations Treat individual paths through an EBB as if it were a single basic block 21

Treat each path through the EBB as a basic block A B C D E F G 22

Treat each path through the EBB as a basic block A A A A B C B C C D E D E F F G G 22

Value Numbering Over EBBs Treat each path through the EBB as a single basic block Initialize the hash table from the previous basic block in the path Remove the entries from hash table for the basic block when recursing up the path 23

Value Numbering Example m0 = a0 + b0 n0 = a0 + b0 p0 = c0 + d0 r0 = d0 + c0 q0 = a0 + b0 r1 = c0 + d0 e0 = b0 + 18 s0 = a0 + b0 u0 = e0 + f0 e1 = a0 + 17 t0 = c0 + d0 u1 = e1 + f0 e2 = ϕ(e0, e1) u2 = ϕ(u0, u1) v0 = a0 + b0 w0 = c0 + d0 x0 = e2 + f0 r2 = ϕ(r0, r1) y0 = a0 + b0 z0 = c0 + d0 24

Value Numbering Example m0 = a0 + b0 n0 = a0 + b0 p0 = c0 + d0 r0 = c0 + d0 q0 = a0 + b0 r1 = c0 + d0 e0 = b0 +18 s0 = a0 + b0 u0 = e0 + f0 e1 = a0 + 17 t0 = c0 + d0 u1 = e1 + f0 e2 = ϕ(e0, e1) u2 = ϕ(u0, u1) v0 = a0 + b0 w0 = c0 + d0 Local x0 = e2 + f0 r2 = ϕ(r0, r1) y0 = a0 + b0 z0 = c0 + d0 24

Value Numbering Example m0 = a0 + b0 n0 = a0 + b0 p0 = c0 + d0 r0 = c0 + d0 q0 = a0 + b0 r1 = c0 + d0 e0 = b0 + 18 s0 = a0 + b0 u0 = e0 + f0 e1 = a0 + 17 t0 = c0 + d0 u1 = e1 + f0 e2 = ϕ(e0, e1) u2 = ϕ(u0, u1) v0 = a0 + b0 w0 = c0 + d0 Local x0 = e2 + f0 Superlocal r2 = ϕ(r0, r1) y0 = a0 + b0 z0 = c0 + d0 24

Room for improvment Still miss some opportunities because we must discard the value table each time we reach a node with multiple predecessors Would like to keep some information about values we have already seen There is another technique we can use to find more opportunities for optimization 25

Dominators Control flow Graph A Dominator Tree A B C B C G D E D F E F G A node m dominates a node n if every path from the start node to n goes through m. By definition a node dominates itself 26

Dominator Based Value Numbering Preorder traversal of the dominator tree Initialize the value table with the blocks immediate dominator in the tree Remove the entries from the table when returning from the block 27

m0 = a0 + b0 n0 = a0 + b0 p0 = c0 + d0 r0 = d0 + c0 q0 = a0 + b0 r1 = c0 + d0 e0 = b0 + 18 s0 = a0 + b0 u0 = e0 + f0 e1 = a0 + 17 t0 = c0 + d0 u1 = e1 + f0 e2 = ϕ(e0, e1) u2 = ϕ(u0, u1) v0 = a0 + b0 w0 = c0 + d0 x0 = e2 + f0 Local Superlocal r2 = ϕ(r0, r1) y0 = a0 + b0 z0 = c0 + d0 Dominator 28

Can we do better? Yes, using global value numbering. Technique uses data flow analysis to compute which expressions are available at any point in the program Take comp 512 for all the data flow analysis you could ever want 29

Resources Engineering a Compiler Cooper and Torczon The Dragon Book Aho, Sethi and Ullman Comp 412 30