1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.

Slides:



Advertisements
Similar presentations
Intermediate Representations CS 671 February 12, 2008.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
SSA.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
Compiler Construction Sohail Aslam Lecture IR Taxonomy IRs fall into three organizational categories 1.Graphical IRs encode the compiler’s knowledge.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
10/22/2002© 2002 Hal Perkins & UW CSEG-1 CSE 582 – Compilers Intermediate Representations Hal Perkins Autumn 2002.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
CSC 8505 Compiler Construction Intermediate Representations.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction Parser Semantic Actions Intermediate Representations AST Linear Next Spring 2014 Jim Hogg - UW - CSE - P501G-1.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Compiler Chapter# 5 Intermediate code generation.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Introduction to Code Generation and Intermediate Representations
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Intermediate Code Representations
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
Program Representations. Representing programs Goals.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Intermediate Code Generation CS 671 February 14, 2008.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
CS 404 Introduction to Compiler Design
High-level optimization Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate Representations Hal Perkins Autumn 2011
Intermediate Representations
Chapter 6 Intermediate-Code Generation
Intermediate Representations
Code Optimization Overview and Examples Control Flow Graph
Data Flow Analysis Compiler Design
Intermediate Code Generation
Compiler Construction
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning parsing semantic analysis HIR intermediate code gen. HIR optim LIR code gen. LIR

2 Intermediate representation Components –code representation –symbol table –analysis information –string table Issues –Use an existing IR or design a new one? –How close should it be to the source/target?

3 IR selection Using an existing IR –cost savings due to reuse –it must be expressive and appropriate for the compiler operations Designing an IR –decide how close to machine code it should be –decide how expressive it should be –decide its structure –consider combining different kinds of IRs

4 IR classification: Level High-level –closer to source language –used early in the process –usually converted to lower form later on –Example: AST

5 IR classification: Level Medium-level –try to reflect the range of features in the source language in a language-independent way –most optimizations are performed at this level algebraic simplification copy propagation dead-code elimination common subexpression elimination loop-invariant code motion etc.

6 IR classification: Level Low-level –very close to target-machine instructions –architecture dependent –useful for several optimizations loop unrolling branch scheduling instruction/data prefetching register allocation etc.

7 IR classification: Level for i := op1 to op2 step op3 instructions endfor i := op1 if step < 0 goto L2 L1: if i > op2 goto L3 instructions i := i + step goto L1 L2: if i < op2 goto L3 instructions i := i + step goto L2 L3: High-level Medium-level

8 IR classification: Structure Graphical –trees, graphs –not easy to rearrange –large structures Linear –looks like pseudocode –easy to rearrange Hybrid –combine graphical and linear IRs –Example: low-level linear IR for basic blocks, and graph to represent flow of control

9 (Basic blocks) Basic block = a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end.

10 (Basic blocks) Partitioning a sequence of statements into BBs –Determine leaders (first statements of BBs) the first statement is a leader the target of a conditional is a leader a statement following a branch is a leader –For each leader, its basic block consists of the leader and all the statements up to but not including the next leader.

11 Linear IRs Sequence of instructions that execute in order of appearance Control flow is represented by conditional branches and jumps Common representations –stack machine code –three-address code

12 Linear IRs stack machine code –assumes presence of operand stack –useful for stack architectures, JVM –operations typically pop operands and push results. –advantages easy code generation compact form –disadvantages difficult to rearrange difficult to reuse expressions

13 Linear IRs three-address code –compact –generates temp variables –level of abstraction may vary –loses syntactic structure –quadruples operator up to two operands destination –triples similar to quadruples but the results are not named explicitly (index of operation is implicit name) –Implement as table, array of pointers, or list

14 Linear IRs L1: i := 2 t1:= i+1 t2 := t1>0 if t2 goto L1 (1) 2 (2) i st (1) (3) i + 1 (4) (3) > 0 (5) if (4), (1) Quadruples Triples

15 Graphical IRs Parse tree Abstract syntax tree –high-level –useful for source-level information –retains syntactic structure –Common uses source-to-source translation semantic analysis syntax-directed editors

16 Graphical IRs Tree, for basic block –root: operator –up to two children: operands –can be combined Uses: –algebraic simplifications –may generate locally optimal code. L1: i := 2 t1:= i+1 t2 := t1>0 if t2 goto L1 assgn, i add, t1 gt, t2 2 i 1 t1 0 2 assgn, i 1 add, t1 0 gt, t2

17 Graphical IRs Directed acyclic graphs (DAGs) –Like compressed trees leaves: variables, constants available on entry internal nodes: operators –annotated with variable names? distinct left/right children –Used for basic blocks (doesn't show control flow) –Can generate efficient code. Note: DAGs encode common expressions –But difficult to transform –Better for analysis

18 Graphical IRs Generating DAGs –check whether an operand is already present if not, create a leaf for it –check whether there is a parent of the operand that represents the same operation if not create one, then label the node representing the result with the name of the destination variable, and remove that label from all other nodes in the DAG.

19 Graphical IRs Directed acyclic graphs (DAGs) –Example m := 2 * y * z n := 3 * y * z p := 2 * y - z

20 Graphical IRs Control flow graphs (CFGs) –Each node corresponds to a basic block, or –fewer nodes –may need to determine facts at specific points within BB a single statement –more space and time –Each edge represents flow of control

21 Graphical IRs Dependence graphs –Encode flow of values from definition to use –Nodes represent operations –Edges connect definitions to uses –Graph represents constraints on the sequencing of operations –Built for specific optimizations, then discarded

22 SSA form Static Single Assignment Form –Encodes information about data and control flow –Two constraints: each definition has a unique name each use refers to a single definition –all uses reached by a definition are renamed –Example: x := 5 x 0 := 5 x := x+1 becomes x 1 := x y := x *2 y 0 := x 1 * 2 What if we have a loop?

23 SSA form The compiler inserts special join functions (called  -functions) at points where different control flow paths meet. Example: read(x) read(x 0 ) if (x>0) if (x 0 >0) y:=5 y 0 := 5 else becomes else y:=10 y 1 := 10 x := y y 2 :=  (y 0, y 1 ) x 1 := y 2

24 SSA form Example 2: x := 0 x 0 := 0 i := 1 i 0 := 1 while (i =10) goto L2 x := x+i L1: i := i+1

25 SSA form Example 2: x := 0 x 0 := 0 i := 1 i 0 := 1 while (i =10) goto L2 x := x+i L1: x 1 :=  (x 0, x 2 ) i := i+1 i 1 :=  (i 0, i 2 ) x 2 := x 1 +i 1 i 2 := i 1 +1 if (i 2 <10) goto L1 L2: x 3 :=  (x 0, x 2 ) i 3 :=  (i 0, i 2 )

26 SSA form Note:  is not an executable function A program is in SSA form if –each variable is assigned a value in exactly one statement –each use of a variable is dominated by the definition. point x dominates point y if every path from the start to y goes through x

27 SSA form Why use SSA? –explicit def-use pairs –no write-after-read and write-after-write dependences –speeds up many dataflow optimizations But –too many temp variables,  -functions –limited to scalars how to handle arrays?