PSUCS322 HM 1 Languages and Compiler Design II IR Canonicalization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

CSE 5317/4305 L9: Instruction Selection1 Instruction Selection Leonidas Fegaras.
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
1 CS 201 Compiler Construction Machine Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
COMPILERS Basic Blocks and Traces hussein suleman uct csc3005h 2006.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
PSUCS322 HM 1 Languages and Compiler Design II Runtime System Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Basic Blocks Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter 8.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
PSUCS322 HM 1 Languages and Compiler Design II Re-Introduction from CS 321 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb.
PSUCS322 HM 1 Languages and Compiler Design II Strongly Connected Components Herbert G. Mayer PSU Spring 2010 rev.: 5/28/2010.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation 2 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
PSUCS322 HM 1 Languages and Compiler Design II Code Generation Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Cse322, Programming Languages and Compilers 1 6/26/2015 Lecture #6, April 17, 2007 Project #1 description, Changes to Program Types, IR1 intermediate language,
PSUCS322 HM 1 Languages and Compiler Design II Project 4 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
PSUCS322 HM 1 Languages and Compiler Design II Project 2 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
The TINY sample language and it’s compiler
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Simple Code Generation CS153: Compilers. Code Generation Next PS: Map Fish code to MIPS code Issues: –eliminating compound expressions –eliminating variables.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Semantics (1).
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Introduction to Parsing
CS 404 Introduction to Compiler Design
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
System Software Unit-1 (Language Processors) A TOY Compiler
Intermediate code Jakub Yaghob
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
6.001 SICP Compilation Context: special purpose vs. universal machines
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
COMPILERS Basic Blocks and Traces
Syntax versus Semantics
COMPILERS Basic Blocks and Traces
CMPE 152: Compiler Design April 5 Class Meeting
Compiler Construction
Basic Block Optimizations
Programming Languages (CS 550) Mini Language Compiler
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Introduction to Parsing
Introduction to Parsing
UNIT V Run Time Environments.
Local Optimizations.
Rudra Dutta CSC Spring 2007, Section 001
Programming Languages (CS 360) Mini Language Compiler
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

PSUCS322 HM 1 Languages and Compiler Design II IR Canonicalization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010

PSUCS322 HM 2 Agenda Definition Problems with IR Representation IR Tree Canonicalization Implementation Handling CALL Nodes Handling Other EXP Nodes Handling STMT Nodes

PSUCS322 HM 3 Definition Definition: In Computer Science, canonicalization is the formal process of converting data with more than one representation from one into another, "standard“ ( or "normal", or canonical) form of representation Sometimes abbreviated c14n, where 14 represents the number of letters between the ‘c’ and ‘n’. Synonyms are standardization or normalization Not to be confused with the process in the Catholic Church of declaring a person a saint = canonization

PSUCS322 HM 4 Problems with IR Representation Two areas in our IR tree language may cause representational problems: Multiple CALL nodes within the same expression may cause problems with call-related register usage when mapped to lower-level code ESEQ and CALL nodes within expressions may bring side effects to different evaluation orders of sub-expressions, causing problems for low-level optimizations

PSUCS322 HM 5 IR Tree Canonicalization To resolve these problems, we could perform tree canonicalization: Assign each return value immediately into a TEMP; turn a CALL node into an ESEQ node: (CALL (NAME f) ((CONST 1))) => (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100)) Pull statements in ESEQ nodes out of an expression tree, effectively eliminating all ESEQ nodes: [ASSIGN (TEMP 101) (BINOP + (CONST 2) (CALL (NAME f) ((CONST 1))))] => [ASSIGN (TEMP 101) (BINOP + (CONST 2) (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100)))] => [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] [ASSIGN (TEMP 101) (BINOP + (CONST 2) (TEMP 100))] The resulting tree is called a canonical IR tree

PSUCS322 HM 6 Implementation Canonicalization can be implemented as a visitor program over IR trees. Visitor touches every expression node, extracts statements out, and up-levels them. public class Canon implements IrVI { public PROG visit( PROG t ) { return new PROG(t.funcs.accept(this)); } //end PROG public FUNClist visit( FUNClist t ) { FUNClist funcs = new FUNClist(); for( int i = 0; i < t.size(); i++ ) { funcs.add(((FUNC) t.elementAt( I )).accept( this )); } //end for return funcs; } //end FUNClist public FUNC visit( FUNC t ) { return new FUNC( t.label, t.varCnt, t.argCnt, (STMTlist) t.stmts.accept(this)); } //end FUNC } //end Canon

PSUCS322 HM 7 Handling CALL Nodes New temp saves return value; then replace the node with an ESEQ node: public EXP visit( CALL t ) { EXP args = t.args.accept( this ); STMT s = getStmt(args); TEMP tmp = new TEMP(); STMT s1 = new MOVE( tmp, new CALL( t.func, (EXPlist) getExp( args ))); return new ESEQ( mergeStmts( s, s1 ), tmp ); } //end EXP private STMT getStmt( EXP e ) { if( e instanceof ESEQ) return ((ESEQ) e).stmt; return null; } //end STMT private EXP getExp( EXP e ) { if( e instanceof ESEQ ) return(( ESEQ ) e ).exp; return e; } //end EXPs

PSUCS322 HM 8 Handling Other EXP Nodes Assuming components are commutative. public EXP visit( MEM t ) { EXP exp = t.exp.accept( this ); STMT s = getStmt( exp ); if( s != null ) return new ESEQ(s, new MEM( getExp(exp) )); return t; } //end EXP public EXP visit( BINOP t ) { EXP left = t.left.accept( this ); EXP right = t.right.accept( this ); STMT s = mergeStmts(getStmt( left ), getStmt( right )); if( s != null ) return new ESEQ(s, new BINOP( t.op, getExp( left ), getExp( right ))); return t; } //end EXP

PSUCS322 HM 9 Handling Other EXP Nodes, Cont’d public EXP visit( EXPlist t ) { STMT s = null; EXPlist args = new EXPlist(); for( int i = 0; i < t.size(); i++ ) { EXP e = ((EXP) t.elementAt(i)).accept( this ); STMT s1 = getStmt(e); if( s1 != null ) s = mergeStmts( s, s1 ); args.add( getExp( e )); } //end for if( s != null ) return new ESEQ(s, args); return t; } //end EXP public EXP visit( ESEQ t ) { STMT stmt = t.stmt.accept(this); EXP exp = t.exp.accept(this); STMT s = mergeStmts(stmt, getStmt(exp)); if( s != null ) return new ESEQ( s, getExp( exp )); return t; } //end EXP

PSUCS322 HM 10 Handling STMT Nodes public STMT visit( MOVE t ) { EXP dst = t.dst.accept(this); EXP src = t.src.accept(this); STMT s = mergeStmts( getStmt(dst), getStmt(src) ); if( s != null) return mergeStmts( s, new MOVE(getExp(dst), getExp(src))); return t; } //end STMT public STMT visit( CJUMP t ) { EXP left = t.left.accept( this ); EXP right = t.right.accept( this ); STMT s = mergeStmts(getStmt( left ), getStmt( right )); if( s != null) return mergeStmts(s, new CJUMP( t.op, getExp(left), getExp( right ), t.target )); return t; } //end STMT

PSUCS322 HM 11 Handling STMT Nodes, Cont’d public STMT visit(CALLST t) { EXP args = t.args.accept(this); STMT s = getStmt(args); if( s != null ) return mergeStmts( s, new CALLST( t.func, (EXPlist) getExp( args ))); return t; } //end STMT public STMT visit(RETURN t) { EXP exp = t.exp.accept(this); STMT s = getStmt(exp); if( s != null ) return mergeStmts(s, new RETURN( getExp( exp ) )); return t; } //end STMT