CSE 504: Compiler Design. Instruction Selection

Similar documents
Instruction Selection, II Tree-pattern matching

Topic 6 Basic Back-End Optimization

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

Instruction Selection and Scheduling

Instruction Selection: Preliminaries. Comp 412

Lecture 12: Compiler Backend

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

CS5363 Final Review. cs5363 1

CS 406/534 Compiler Construction Putting It All Together

Introduction to Parsing

CS415 Compilers. Intermediate Represeation & Code Generation

Parsing II Top-down parsing. Comp 412

CSE 504: Compiler Design. Code Generation

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

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

CS 406/534 Compiler Construction Parsing Part I

MODULE 32 DAG based Code Generation and Dynamic Programming

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Low-level optimization

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

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

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

Intermediate Code Generation (ICG)

Front End. Hwansoo Han

Compiler Optimization Techniques

Optimizing Finite Automata

Local Register Allocation (critical content for Lab 2) Comp 412

March 20/2003 Jayakanth Srinivasan,

Intermediate Code & Local Optimizations. Lecture 20

Semantic actions for expressions

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 P 501 Compilers. Instruc7on Selec7on Hal Perkins Winter UW CSE P 501 Winter 2016 N-1

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

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Code Generation. M.B.Chandak Lecture notes on Language Processing

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

The structure of a compiler

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

CS2383 Programming Assignment 3

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

Introduction to Compiler

A simple syntax-directed

Compilers CS S-08 Code Generation

Implementation of Lexical Analysis


Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

Intermediate Code Generation

Syntax Analysis, III Comp 412

Properties of Regular Expressions and Finite Automata

Introduction to Optimization Local Value Numbering

Syntax Analysis. Chapter 4

E E+E E E (E) id. id + id E E+E. id E + E id id + E id id + id. Overview. derivations and parse trees. Grammars and ambiguity. ambiguous grammars

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Chapter 20: Binary Trees

Semantic actions for declarations and expressions

CS153: Compilers Lecture 15: Local Optimization

EECS 6083 Intro to Parsing Context Free Grammars

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

Introduction to Parsing. Lecture 5

Syntax Analysis, III Comp 412

Semantic actions for declarations and expressions. Monday, September 28, 15

A Simple Tree Pattern Matching Algorithm for Code Generator

PRINCIPLES OF COMPILER DESIGN

specrtl: A Language for GCC Machine Descriptions

Chapter 4. Lexical and Syntax Analysis

Introduction to Syntax Analysis. The Second Phase of Front-End

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Intermediate Representations

Compiler Design (40-414)

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Context-Free Grammars

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Binary Search Tree (2A) Young Won Lim 5/17/18

CS 4201 Compilers 2014/2015 Handout: Lab 1

How to efficiently use the address register? Address register = contains the address of the operand to fetch from memory.

The View from 35,000 Feet

Intermediate Code & Local Optimizations

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Introduction to Parsing. Lecture 5

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

Introduction to Syntax Analysis

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Grammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1

Overview of a Compiler

Implementation of Lexical Analysis

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

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

Compiler Optimisation

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Transcription:

Instruction Selection Pradipta De pradipta.de@sunykorea.ac.kr

Current Topic Instruction Selection techniques Tree Walk Tiling based approach Peephole Optimization

Instruction Selection Difficulty of Instruction Selection Map IR (high-level or low-level Irs) assembly code A pattern matching problem Multiple choices to derive IR ASM Different operations to achieve same task in ISA Different addressing modes in ISA Find the best combination of ASM instructions Goal: Compile writer writes a description of the target ISA a tool constructs a selector for use at compile time

Illustrative Example Register-to-register copy in ILOC (ISA) r i r j Simple choice: i2i, r i, r j Other ASM instructions in ILOC: Ideally examine all the instructions and pick the best one for the specific context Need a systematic way to explore the space of alternative code sequences, or limit the search using pre-computed information to enable deep search.

Simple TreeWalk Tree structured IR is common in compilers Can we represent the target ISA using trees? r i, r j represent registers, and c j represents a known constant Match ISA trees against IR trees Level of detail in IR tree leads to better matching High level IR Instruction selector must provide additional details to make better choices Low level IR Easier for selector to map choices

ILOC ISA (subset of instructions)

Exposing low level detail Expression: a b 2 * c Front-end can produce the low level IR Val: value that resides in register, e.g. ARP Lab: relocatable symbol, e.g. labels : level of indirection child is an address, and it produces the value stored at that address Apply postorder treewalk, and match against ISA trees

Convenient Notation for Trees Prefix notation for the low level IR tree: Prefix notation for the ILOC instructions: add addi Commutative variant of addi Given an AST and a collection of operation trees, the goal is to map the AST to operations by constructing a tiling of the AST with operation trees

Tiling Goal is to tile the AST with operation trees A tile is a collection of <ast-node, op-tree> A tile implies that op-tree can implement the ast-node A tiling implements a AST if, It implements every operation (subtree in AST) Each tile connects with its neighbors Given a tiling that implements a AST, the compiler can generate assembly code in a bottom-up traversal Associate costs with operation trees, and produce minimal cost tiling

Rewrite Rules The relationship between operation trees and subtrees in the AST are encoded as rewrite rules Rewrite rules consists of: A production in a tree grammar A code template Associated cost

How to apply rewrite rules The first match in the sequence recognizes that the left leaf (a Lab node) matches rule 6 rewrite it as a Reg The rewritten tree now matches the right-hand side of rule 11 rewrite the entire subtree as Reg This sequence, denoted <6,11>, reduces the entire subtree to a Reg.

Ambiguity in Rewrite Rule Selection

Code Sequences for the Tiles The instruction selector should choose a tiling that produces the lowest-cost assembly-code sequence Both <6,11> and <8,14> produce the lowest cost of two pick any Other sequences are more costly hence rejected

How to tile? Need an algorithm to construct tiling that produces efficient code Assumptions: Each operation has at most two operands A rule s RHS contains at most one operation Easy to generalize:

How to tile? The compiler computes sequences of rule numbers, or patterns, for each node in a postorder traversal of the tree. Finds tiling for a tree rooted at node n in the AST Label(n) is a set of rule numbers that is applicable for tree rooted at n

Speeding up Tiling Most time spent in computing matches in the two for loops Improvement: replace for loop with table lookup Precompute all possible matches and store the result in a 3-dim table <operation, label set (left tree), label set (right tree)> Find lowest cost match accumulate cost while tiling at each node, retain the lowest cost matches (locally optimal tiling)

Code Generator Implementations Hand coded matcher Avoids large space; tedious Encode matching as an automaton Use parsing techniques Linearize tree into strings O(1) cost per node; Tools BURS, BURG Standard techniques used in parsing; Have to deal with ambiguous grammar Finds all matches

Peephole Optimization Discover local improvements by examining short sequences of adjacent instructions Takes LLIR (or ASM) as input, and produces target machine code (ASM) Intuition Use a sliding window or peephole over the code Examine the operations within the window and search for specific patterns that can be improved Rewrite the pattern that can be improved with the better code Limited pattern sets and small window size can lead to fast processing

Illustrative Examples This code may even make the storeai operation dead can eliminate storeai Simple Algebraic Identities Target of branch is another branch

Structure of Peephole Optimizer Simple peephole optimizer: Use a limited set of hand-coded patterns Use exhaustive search to match the patterns Expander: recognizes input code in IR form and builds an internal representation Simplifier: rewriting operations on the IR forward substitution algebraic simplification evaluating constant valued expressions eliminate useless effects (creation of unused condition code) Matcher: transforms the IR into target machine code

Matcher Final pattern matching step uses familiar concepts Hand-coded matchers Use LR() parsers

Summary Two instruction selection techniques Tiling based approach on IR trees Peephole optimization