Intermediate Representations Part II

Similar documents
Intermediate Representations

Intermediate Representations

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

Intermediate Representations & Symbol Tables

Intermediate Representations

Intermediate Representations

Intermediate Representation (IR)

CS415 Compilers. Intermediate Represeation & Code Generation

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

Intermediate representation

Alternatives for semantic processing

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Front End. Hwansoo Han

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

Intermediate Code Generation

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

Intermediate Representations

CSE 401/M501 Compilers

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

Code Shape II Expressions & Assignment

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

CS5363 Final Review. cs5363 1

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

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

CS293S Redundancy Removal. Yufei Ding

6. Intermediate Representation

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

Control flow graphs and loop optimizations. Thursday, October 24, 13

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

The Software Stack: From Assembly Language to Machine Code

Principles of Compiler Design

CS 406/534 Compiler Construction Putting It All Together

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

Pipelining and Exploiting Instruction-Level Parallelism (ILP)

Code generation for modern processors

Code generation for modern processors

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

Control Flow Analysis

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Compiler Design. Code Shaping. Hwansoo Han

The von Neumann Architecture. IT 3123 Hardware and Software Concepts. The Instruction Cycle. Registers. LMC Executes a Store.

Introduction to Optimization Local Value Numbering

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

Compiler Architecture

6. Intermediate Representation!

(just another operator) Ambiguous scalars or aggregates go into memory

Lecture 23 CIS 341: COMPILERS

Naming in OOLs and Storage Layout Comp 412

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Control Flow Analysis & Def-Use. Hwansoo Han

CS 432 Fall Mike Lam, Professor. Code Generation

Instruction Sets: Characteristics and Functions Addressing Modes

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Intermediate Code & Local Optimizations

Control-Flow Analysis

Code Generation. CS 540 George Mason University

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

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

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Arrays and Functions

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Topic I (d): Static Single Assignment Form (SSA)

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Combining Optimizations: Sparse Conditional Constant Propagation

Instruction Selection: Preliminaries. Comp 412

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

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

We can emit stack-machine-style code for expressions via recursion

CSc 453 Interpreters & Interpretation

Compiler Construction 2009/2010: Intermediate Representation

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

CSE 5317 Midterm Examination 4 March Solutions

CODE GENERATION Monday, May 31, 2010

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Compiler Optimization Intermediate Representation

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

Calvin Lin The University of Texas at Austin

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Instruction Set Design

Computer Architecture. The Language of the Machine

Static Single Assignment (SSA) Form

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Compiler Optimization Techniques

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Computing Static Single Assignment (SSA) Form. Control Flow Analysis. Overview. Last Time. Constant propagation Dominator relationships

CS301 - Data Structures Glossary By

Addressing Modes. Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

EC-801 Advanced Computer Architecture

Model-based Software Development

Transcription:

Intermediate Representations Part II

Types of Intermediate Representations Three major categories Structural Linear Hybrid

Directed Acyclic Graph A directed acyclic graph (DAG) is an AST with a unique node for each value - z w / x * x 2 2 y z x 2 * y w x / 2

Directed Acyclic Graph A directed acyclic graph (DAG) is an AST with a unique node for each value - z w / x * 2 y z x 2 * y w x / 2

Stack Machine Code Used for many dynamic languages, e.g., Java Example: x - 2 * y becomes push x! push 2! push y! multiply! subtract!

Stack Machine Code Operations take operands from a stack Compact form Introduced names are implicit, not explicit Simple to generate and execute code

Stack Machine Code Advantages Result is stored in a temporary! Explicit name for result. x - 2 * y Multiply pops two items off of stack and pushes result! Implicit name for result push 2! push y! multiply! push x! subtract!

Stack Machine Code Advantages push 2! push y! multiply! push x! subtract! 2 stack!

Stack Machine Code Advantages push 2! push y! multiply! push x! subtract! y 2 stack!

Stack Machine Code Advantages push 2! push y! multiply! push x! subtract! 2y stack!

Stack Machine Code Advantages push 2! push y! multiply! push x! subtract! x 2y stack!

Stack Machine Code Advantages push 2! push y! multiply! push x! subtract! x-2y stack!

Three Address Code Different representations of three address code In general, three address code has statements of the form: x y op z with 1 operator (op ) and (at most) 3 names (x, y, & z)

Three Address Code Example: z x - 2 * y becomes t 2 * y! z x - t! Explicit name for result.

Three Address Code Advantages Resembles many real (RISC) machines Introduces a new set of names Compact form

Three Address Code: Quadruples Naïve representation of three address code Table of k * 4 small integers Destination Two operands load r1, y! loadi r2, 2! mult r3, r2, r1! load r4, x! sub r5, r4, r3! RISC assembly code load! 1! y! loadi! 2! 2! mult! 3! 2! 1! load! 4! x! sub! 5! 4! 3! Array of instructions

Three Address Code: Array of Pointers Index causes level of indirection Easy (and cheap) to reorder Easy to add (delete) instructions

Three Address Code: Array of Pointers Index causes level of indirection Easy (and cheap) to reorder Easy to add (delete) instructions

Control-flow Graph Models the transfer of control in the procedure Nodes in the graph are basic blocks Can be represented with quads or any other linear representation Edges in the graph represent control flow Example a 2 b 5 if (x = y) a 3 b 4 Basic blocks Maximal length sequences of straight-line code c a * b

Control-Flow Graphs Node: an instruction or sequence of instructions (a basic block) Two instructions i, j in same basic block iff execution of i guarantees execution of j Directed edge: potential flow of control Distinguished start node Entry First instruction in program

Identifying Basic Blocks Input: sequence of instructions instr(i) Identify leaders: first instruction of basic block Iterate: add subsequent instructions to basic block until we reach another leader

Basic Block Partition Algorithm leaders = instr(1) // first instruction for i = 1 to n // iterate thru all instrs if instr(i) is a branch leaders = leaders targets of instr(i) leaders = leaders instr(i+1) // instr after branch worklist = leaders While worklist not empty x = first instruction in worklist worklist = worklist {x} block(x) = {x} for (i = x + 1; i <= n && i not in leaders; i++) block(x) = block(x) {i}

CFG Example

CFG Example

CFG Example This is a join point.

Single Static Assignment: High-Level Definition Each assignment to variable given a unique name All uses reached by that assignment are renamed

Single Static Assignment: High-Level Definition Each assignment to variable given a unique name All uses reached by that assignment are renamed Easy for straight-line code V 4 V 0 4 V + 5 V 0 + 5 V 6 V 1 6 V + 7 V 1 + 7 Before SSA After SSA

Single Static Assignment: High-Level Definition Each assignment to variable given a unique name All uses reached by that assignment are renamed Easy for straight-line code V 4 V 0 4 V + 5 V 0 + 5 V 6 V 1 6 V + 7 V 1 + 7 Before SSA After SSA

Single Static Assignment: High-Level Definition Each assignment to a variable is given a unique name All uses reached by that assignment are renamed Easy for straight-line code V 4 V 0 4 V + 5 V 0 + 5 V 6 V 1 6 V + 7 V 1 + 7 Before SSA After SSA

What About Control Flow? B1 if ( ) B1 if ( ) B2 B3 B2 B3 X 5 X 3 X 0 5 X 1 3 B4 Y X B4 X 2 Φ(X 0, X 1 ) Y 0 X 2 Before SSA After SSA Φ-nodes: combine values from distinct edges (join points)

Φ-Functions At each join, add special assignment: φ function : operands indicate which assignments reach join j th operand = j th predecessor If control reaches join from j th predecessor, then value of φ( ) is value of j th operand B1 if ( ) B2 B3 X 0 5 X 1 3 B4 X 2 Φ(X 0, X 1 ) Y 0 X 2

What About Control Flow? B1 X 1 B1 X 0 1 B2 X X + 1 B2 X 1 Φ(X 0, X 2 ) X 2 X 1 + 1 Before SSA After SSA

Class Example if P x=v then v = 4 else v = 6

Class Example if P x=v then v = 4 else v = 6 if P then v 0 = 4 else v 1 = 6 V 2 = φ(v 0, v 1 ) X 0 = V 2

Another SSA Example Original! x! y! while (x < k)! x x + 1! y y + x!

Another SSA Example Original! x! y! while (x < k)! x x + 1! y y + x! Same code using gotos!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:!

Another SSA Example Original! x! y! while (x < k)! x x + 1! y y + x! Same code using gotos!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:!

Another SSA Example Same code using gotos SSA-form!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:!!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Another SSA Example Same code using gotos SSA-form!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:!!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Another SSA Example Same code using gotos SSA-form!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:!!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Another SSA Example Same code using gotos!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:! SSA-form!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Static Single Assignment Form Same code using gotos SSA-form!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:! This is new! Inserted! at points where diff.! control flow merge.!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Static Single Assignment Form Same code using gotos SSA-form!!x!!!y!!!if (x >= k)! goto next! loop: x x + 1! y y + x!!! if(x < k)! goto loop! next:! Keeps single assign.! property.!!x 0!!!y 0!!!if (x 0 >= k) goto next! loop: x 1 φ(x 0,x 2 )!!!! y 1 φ(y 0,y 2 )!!x 2 x 1 + 1!!y 2 y 1 + x 2!!! if(x 2 < k) goto loop! next:!

Dependence Graph

Dependence Graph

Dependence Graph

Dependence Graph

Using Multiple Representations Source Code Front End IR 1 Middle IR 2 Middle IR 3 End End Back End Target Code Repeatedly lower the level of the intermediate representation Each intermediate representation is suited towards certain optimizations Example: the Open64 compiler WHIRL intermediate format Consists of 5 different IRs that are progressively more detailed and less abstract

Memory Models Two major models Register-to-register model Keep all values that can legally be stored in a register in registers Ignore machine limitations on number of registers Compiler back-end must insert loads and stores Memory-to-memory model Keep all values in memory Only promote values to registers directly before they are used Compiler back-end can remove loads and stores Compilers for RISC machines usually use register-to-register Reflects programming model Easier to determine when registers are used

The Rest of the Story Representing the code is only part of an IR There are other necessary components Symbol table Constant table Representation, type Storage class, offset Storage map Overall storage layout Overlap information Virtual register assignments

Symbol Tables Classic approach to building a symbol table uses hashing Personal preference: a two-table scheme Sparse index to reduce chance of collisions Dense table to hold actual data See B.3 in EaC for a longer explanation Easy to expand, to traverse, to read & write from/to files Use chains in index to handle collisions Collision occurs when h() returns a slot in the sparse index that is already full. h( foe ) h( fee ) NextSlot fum float Stack-like growth scalar fee integer scalar fie char * array Comp 412, Fall 2010 51 Sparse index Dense table

Hash-less Symbol Tables Classic approach to building a symbol table uses hashing Some concern about worst-case behavior Collisions in the hash function can lead to linear search Some authors advocate perfect hash for keyword lookup Automata theory lets us avoid worst-case behavior Collision occurs when h() returns a slot in the sparse index that is already full. h( foe ) My favorite hash table organization Stack-like growth h( fee ) fum float scalar NextSlot fee integer scalar Comp 412, Fall 2010 52 Sparse index fie char * array Dense table

Hash-less Symbol Tables One alternative is Paige & Cai s multiset discrimination Order the name space offline Assign indices to each name Replace the names in the input with their encoded indices Digression on page 241 of EaC Using DFA techniques, we can build a guaranteed linear-time replacement for the hash function h DFA that results from a list of words is acyclic RE looks like r 1 r 2 r 3 r k Could process input twice, once to build DFA, once to use it We can do even better Comp 412, Fall 2010 53

Hash-less Symbol Tables Classic approach to building a symbol table uses hashing Some concern about worst-case behavior Collisions in the hash function can lead to linear search Some authors advocate perfect hash for keyword lookup Automata theory lets us avoid worst-case behavior Replace the hash function, h, and the sparse index with an efficient direct map, d, d( fie ) d( fee ) d( fum ) d( foe ) Stack-like growth fum float scalar NextSlot fee integer scalar Comp 412, Fall 2010 54 Sparse index fie char * array Dense table

Hash-less Symbol Tables Incremental construction of an acyclic DFA To add a word, run it through the DFA At some point, it will face a transition to the error state At that point, start building states & transitions to recognize it Requires a memory access per character in the key If DFA grows too large, memory access costs become excessive For small key sets (e.g., names in a procedure), not a problem Optimizations Last state on each path can be explicit Substantial reduction in memory costs Instantiate when path is lengthened Trade off granularity against size of state representation Encode capitalization separately Bit strings tied to final state? Comp 412, Fall 2010 55