Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in.

Slides:



Advertisements
Similar presentations
Tiling Examples for X86 ISA Slides Selected from Radu Ruginas CS412/413 Lecture on Instruction Selection at Cornell.
Advertisements

Intermediate Code Generation
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
1 CS 201 Compiler Construction Machine Code Generation.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
11/19/2002© 2002 Hal Perkins & UW CSEN-1 CSE 582 – Compilers Instruction Selection Hal Perkins Autumn 2002.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
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.
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.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Context-sensitive Analysis, III Ad-hoc syntax-directed translation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Instruction Selection II CS 671 February 26, 2008.
Spring 2014Jim Hogg - UW - CSE - P501N-1 CSE P501 – Compiler Construction Compiler Backend Organization Instruction Selection Instruction Scheduling Registers.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Local Register Allocation & Lab Exercise 1 Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp.
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.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Local Instruction Scheduling — A Primer for Lab 3 — Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
COMPILERS Instruction Selection hussein suleman uct csc305h 2005.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
Instruction Scheduling Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Selection CS 671 February 19, CS 671 – Spring The Back End Essential tasks: Instruction selection Map low-level IR to actual.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Introduction to Parsing
Introduction to Optimization
Instruction Selection II: Tree-pattern Matching Comp 412
COMPILERS Instruction Selection
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing III (Top-down parsing: recursive descent & LL(1) )
Local Register Allocation & Lab Exercise 1
Local Instruction Scheduling
Introduction CI612 Compiler Design CI612 Compiler Design.
Context-sensitive Analysis
Lexical Analysis - An Introduction
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.
Chapter 6 Intermediate-Code Generation
Lecture 7: Introduction to Parsing (Syntax Analysis)
Code Shape III Booleans, Relationals, & Control flow
Instruction Selection, II Tree-pattern matching
Intermediate Representations
The Last Lecture COMP 512 Rice University Houston, Texas Fall 2003
Introduction to Parsing
Introduction to Parsing
Context-sensitive Analysis, III Ad-hoc syntax-directed translation
Instruction Selection Hal Perkins Autumn 2005
Parsing — Part II (Top-down parsing, left-recursion removal)
Local Register Allocation & Lab Exercise 1
Introduction to Optimization
Lexical Analysis - An Introduction
Presentation transcript:

Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & 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.

The Concept Many compilers use tree-structured IRs Abstract syntax trees generated in the parser Trees or DAGs for expressions These systems might well use trees to represent target ISA Consider the ILOC add operators If we can match these “pattern trees” against IR trees, … + riri rjrj add r i,r j  r k + riri cjcj addI r i,c j  r k Operation trees

The Concept Low-level AST for w  x - 2 * y  + VAL ARP NUM 4 - REF VAL ARP NUM * NUM 2 REF NUM 12 + ARP: r arp NUM: constant LAB: ASM label w: at ARP+4 x: at ARP-26 Y:

The Concept Low-level AST for w  x - 2 * y  + VAL ARP NUM 4 - REF VAL ARP NUM * NUM 2 REF NUM 12 + ARP: r arp NUM: constant LAB: ASM label w: at ARP+4 x: at ARP-26 Y:

Notation To describe these trees, we need a concise notation + riri cjcj +(r i,c j ) + riri rjrj +(r i,r j ) Linear prefix form

Notation To describe these trees, we need a concise notation GETS + VAL ARP NUM 4 - REF VAL ARP NUM * NUM 2 REF NUM 12 + (+(VAL 1,NUM 1 ) (REF(REF(+(VAL 2,NUM 2 ))) *(NUM 3,(REF(+(LAB 1,NUM 3 )))))) -(REF(REF(+(VAL 2,NUM 2 ))), *(NUM 3,(REF(+(LAB 1,NUM 3 )))))) GETS(+(VAL 1,NUM 1 ), -(REF(REF(+(VAL 2,NUM 2 ))), *(NUM 3,(REF(+(LAB 1,NUM 3 ))))))

Goal is to “tile” AST with operation trees A tiling is collection of pairs  ast is a node in the AST  op is an operation tree  means that op could implement the subtree at ast A tiling ‘implements” an AST if it covers every node in the AST and the overlap between any two trees is limited to a single node   tiling means ast is also covered by a leaf in another operation tree in the tiling, unless it is the root  Where two operation trees meet, they must be compatible (expect the value in the same location) Tree-pattern matching

Tile 3 Tile 4 Tile 2 Tile 1 Tile 5 Tile 6 Tiling the Tree GETS + VAL ARP NUM 4 - REF VAL ARP NUM * NUM 2 REF NUM 12 + Each tile corresponds to a sequence of operations Emitting those operations in an appropriate order implements the tree.

Generating Code Given a tiled tree Postorder treewalk, with node-dependent order for children  Right child of GETS before its left child  Might impose “most demanding first” rule … (Sethi ) Emit code sequence for tiles, in order Tie boundaries together with register names  Tile 6 uses registers produced by tiles 1 & 5  Tile 6 emits “ store r tile 5  r tile 1 ”  Can incorporate a “real” allocator or can use “NextRegister++”

So, What’s Hard About This? Finding the matches to tile the tree Compiler writer connects operation trees to AST subtrees  Provides a set of rewrite rules  Encode tree syntax, in linear form  Associated with each is a code template

Rewrite rules: LL Integer AST into ILOC RuleCostTemplate 1Goal  Assign0 2Assign  GETS(Reg 1,Reg 2 )1 store r 2  r 1 3Assign  GETS(+(Reg 1,Reg 2 ),Reg 3 )1 storeAO r 3  r 1,r 2 4Assign  GETS(+(Reg 1,NUM 2 ),Reg 3 )1 storeAI r 3  r 1,n 2 5Assign  GETS(+(NUM 1,Reg 2 ),Reg 3 )1 storeAI r 3  r 2,n 1 6Reg  LAB 1 1 loadI l 1  r new 7Reg  VAL 1 0 8Reg  NUM 1 1 loadI n 1  r new 9Reg  REF(Reg 1 )1 load r 1  r new 10Reg  REF(+ (Reg 1,Reg 2 ))1 loadAO r 1,r 2  r new 11Reg  REF(+ (Reg 1,NUM 2 ))1 loadAI r 1,n 2  r new 12Reg  REF(+ (NUM 1,Reg 2 ))1 loadAI r 2,n 1  r new

Rewrite rules: LL Integer AST into ILOC ( part II ) RuleCostTemplate 13Reg  + (Reg 1,Reg 2 )1 add r 1,r 2  r new 14Reg  + (Reg 1,NUM 2 )1 addI r 1,n 2  r new 15Reg  + (NUM 1,Reg 2 )1 addI r 2,n 1  r new 16Reg  - (Reg 1,Reg 2 )1 sub r 1,r 2  r new 17Reg  - (Reg 1,NUM 2 )1 subI r 1,n 2  r new 18Reg  - (NUM 1,Reg 2 )1 rsubI r 2,n 1  r new 19Reg  x (Reg 1,Reg 2 )1 mult r 1,r 2  r new 20Reg  x (Reg 1,NUM 2 )1 multI r 1,n 2  r new 21Reg  x (NUM 1,Reg 2 )1 multI r 2,n 1  r new A real set of rules would cover more than signed integers …

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example Tile 3 REF NUM 12 +

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3?

6 So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3? 6: Reg  LAB 1 tiles the lower left node

68 So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3? 6: Reg  LAB 1 tiles the lower left node 8: Reg  NUM 1 tiles the bottom right node

13 68 So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3? 6: Reg  LAB 1 tiles the lower left node 8: Reg  NUM 1 tiles the bottom right node 13: Reg  + (Reg 1,Reg 2 ) tiles the + node

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3? 6: Reg  LAB 1 tiles the lower left node 8: Reg  NUM 1 tiles the bottom right node 13: Reg  + (Reg 1,Reg 2 ) tiles the + node 9: Reg  REF(Reg 1 ) tiles the REF

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example REF NUM 12 + What rules match tile 3? 6: Reg  LAB 1 tiles the lower left node 8: Reg  NUM 1 tiles the bottom right node 13: Reg  + (Reg 1,Reg 2 ) tiles the + node 9: Reg  REF(Reg 1 ) tiles the REF We denote this match as Of course, it implies Both have a cost of 4

Finding matches REF NUM 12 + Many Sequences Match Our Subtree CostSequences 26,118,12 36,8,108,6,106,14,98,15,9 46,8,13,98,6,13,9 In general, we want the low cost sequence Each unit of cost is an operation ( 1 cycle ) We should favor short sequences

Finding matches REF NUM 12 + Low Cost Matches Sequences with Cost of 2 6: Reg  LAB 1 11: Reg  REF(+(Reg 1,NUM 2 ))  r i loadAI r i,12  r j 8: Reg  NUM 1 12: Reg  REF(+(NUM 1,Reg 2 )) loadI 12  r i loadAI r  r j These two are equivalent in cost 6,11 might be better, may be longer than the immediate field

Tiling the Tree Still need an algorithm Assume each rule implements one operator Assume operator takes 0, 1, or 2 operands Now, …

Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) for each rule r that implements n if (left(r)  Label(child(n)) then Label(n)  Label(n)  { r } else /* n is a leaf */ Label(n)  {all rules that implement n } Match binary nodes against binary rules Match unary nodes against unary rules Handle leaves with lookup in rule table

Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) for each rule r that implements n if (left(r)  Label(child(n)) then Label(n)  Label(n)  { r } else /* n is a leaf */ Label(n)  {all rules that implement n } This algorithm Finds all matches in rule set Labels node n with that set Can keep lowest cost match at each point Leads to a notion of local optimality — lowest cost at each point Spends its time in the two matching loops

Oversimplifications 1. Only handles 1 storage class 2. Must track low cost sequence in each class 3. Must choose lowest cost for subtree, across all classes Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) for each rule r that implements n if (left(r)  Label(child(n)) then Label(n)  Label(n)  { r } else /* n is a leaf */ Label(n)  {all rules that implement n } The extensions to handle these complications are pretty straightforward.

Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) for each rule r that implements n if (left(r)  Label(child(n)) then Label(n)  Label(n)  { r } else /* n is a leaf */ Label(n)  {all rules that implement n } Can turn matching code (inner loop) into a table lookup Table can get huge and sparse |op trees| x |labels| x |labels| 200 x 1000 x 1000 leads to 200,000,000 entries Fortunately, they are quite sparse & have reasonable encodings (e.g., Chase’s work)

The Big Picture Tree patterns represent AST and ASM Can use matching algorithms to find low-cost tiling of AST Can turn a tiling into code using templates for matched rules Techniques (& tools) exist to do this efficiently Hand-coded matcher like Tile Avoids large sparse table Lots of work Encode matching as an automaton O(1) cost per node Tools like BURS, BURG Use parsing techniques Uses known technology Very ambiguous grammars Linearize tree into string and use Aho-Corasick Finds all matches

Extra Slides Start Here

11 6 Other Sequences REF NUM ,11 6: Reg  LAB 1 11: Reg  REF( + (Reg 1,NUM 2 )) Two operator rule

12 8 Other Sequences REF NUM ,12 8: Reg  NUM 1 12: Reg  REF( + (NUM 1,Reg 2 )) Two operator rule

10 68 Other Sequences REF NUM ,8,10 6: Reg  LAB 1 8: Reg  NUM 1 11: Reg  REF( + (Reg 1,Reg 2 )) 8,6,10 looks the same Two operator rule

Other Sequences REF NUM ,14,9 6: Reg  LAB 1 14: Reg  + (Reg 1,NUM 2 ) 9: Reg  REF(Reg 1 ) All single operator rules

Other Sequences REF NUM ,15,9 8: Reg  NUM 1 15: Reg  + (NUM 1,Reg 2 ) 9: Reg  REF(Reg 1 ) All single operator rules

Other Sequences 6,8,13,9 6: Reg  LAB 1 8: Reg  NUM 1 13: Reg  + (Reg 1,Reg 2 ) 9: Reg  REF(Reg 1 ) All single operator rules REF NUM ,6,13,9 looks the same