Compiler Optimization Intermediate Representation

Size: px
Start display at page:

Download "Compiler Optimization Intermediate Representation"

Transcription

1 Compiler Optimization Intermediate Representation Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay A Short Course on Compiler Construction & Optimization Lecture 2 (23 May 2014)

2 From Source to Executable source program foo.c main() sub1() data Compiler sta;c library libc.a prin9 scanf gets fopen exit data... object modules foo.o main sub1 data Linkage Editor load module a.out main sub1 data prin9 exit data Load ;me (Run Time) Loader Machine memory? other programs... main sub1 data prin9 exit data other kernel (system calls) Dynamic library case not shown 23 May

3 General Structure of a Modern Compiler Source Program Lexical Analysis Scanner Context Symbol Table CFG Syntax Analysis Parser Front end Build high-level IR Semantic Analysis High-level IR to low-level IR conversion Controlflow/Dataflow Optimization Back end Assembly Code Code Generation Machine independent asm to machine dependent 23 May 2014 Compilers@EE-IITB 3

4 Intermediate Representation (aka IR) The compilers internal representa1on Is language- independent and machine- independent Enables machine independent and machine dependent optis AST optimize IR Pentium Java bytecode Itanium TI C5x ARM 23 May

5 What Makes a Good IR? Captures high- level language constructs Easy to translate from AST Supports high- level op1miza1ons Captures low- level machine features Easy to translate to assembly Supports machine- dependent op1miza1ons Narrow interface: small number of node types (instruc1ons) Easy to op1mize Easy to retarget 23 May 2014 Compilers@EE-IITB 5

6 Kinds of IR Abstract syntax trees (AST) Linear operator form of tree (e.g., posjix nota1on) Directed acyclic graphs (DAG) Control flow graphs (CFG) Program dependence graphs (PDG) Sta1c single assignment form (SSA) 3- address code Hybrid combina1ons 23 May

7 Categories of IR Structural graphically oriented (trees, DAGs) nodes and edges tend to be large heavily used on source- to- source translators Linear pseudo- code for abstract machine large varia1on in level of abstrac1on simple, compact data structures easier to rearrange Hybrid combina1on of graphs and linear code (e.g. CFGs) arempt to achieve best of both worlds 23 May

8 Important IR properties Ease of genera1on Ease of manipula1on Cost of manipula1on Level of abstrac1on Freedom of expression (!) Size of typical procedure Original or deriva1ve Subtle design decisions in the IR can have far-reaching effects on the speed and effectiveness of the compiler! è Degree of exposed detail can be crucial 23 May

9 Abstract Syntax Tree An AST is a parse tree A linear operator form of this tree (postfix) would be: x 2 y * -! 23 May 2014 Compilers@EE-IITB 9

10 Directed Acyclic Graph A DAG is an AST with unique, shared nodes for each value. x := 2 * y + sin(2*x)! z := x / 2! 23 May 2014 Compilers@EE-IITB 10

11 Control Flow Graph A CFG models transfer of control in a program nodes are basic blocks (straight- line blocks of code) edges represent control flow (loops, if/else, goto ) if x = y then!!s1! else!!s2! end! S3! 23 May 2014 Compilers@EE-IITB 11

12 3-address code Statements take the form: x = y op z! single operator and at most three names x 2 * y! t1 = 2 * y! t2 = x t1! Advantages: Compact form Names for intermediate values 23 May 2014 Compilers@EE-IITB 12

13 Typical 3-address codes assignments! x = y op z! x = op y! x = y[i]! x = y! branches! goto L! conditional branches! if x relop y goto L! procedure calls! address and pointer assignments! param x! param y! call p! x = &y! *y = z! 23 May 2014 Compilers@EE-IITB 13

14 Other hybrids exist IR choices combina1ons of graphs and linear codes CFG with 3- address code for basic blocks Many variants used in prac1ce no widespread agreement compilers may need several different IRs! Advice: choose IR with right level of detail keep manipula1on costs in mind 23 May

15 Static Single Assignment Form Goal: simplify procedure- global op1miza1ons Defini3on: Program is in SSA form if every variable is only assigned once 23 May

16 Static Single Assignment (SSA) Each assignment to a temporary is given a unique name All uses reached by that assignment are renamed Compact representa1on Useful for many kinds of compiler op1miza1on x := 3;! x := x + 1;! è x := 7;! x := x*2;! x 1 := 3;! x 2 := x 1 + 1;! x 3 := 7;! x 4 := x 3 *2;! Ron Cytron, et al., Efficiently compu3ng sta3c single assignment form and the control dependence graph, ACM TOPLAS., May 2014 Compilers@EE-IITB 16

17 Why Sta1c? Why Static? We only look at the sta3c program One assignment per variable in the program At run1me variables are assigned mul1ple 1mes! 23 May

18 Example: Sequence Easy to do for sequential programs: Original a := b + c! b := c + 1! d := b + c! a := a + 1! e := a + b! SSA a 1 := b 1 + c 1! b 2 := c 1 + 1! d 1 := b 2 + c 1! a 2 := a 1 + 1! e 1 := a 2 + b 2! 23 May 2014 Compilers@EE-IITB 18

19 Example: Condition Conditions: what to do on control-flow merge? Original if B then!!a := b! else!!a := c! end!! a! SSA if B then!!a 1 := b! else!!a 2 := c! End!!! a?! 23 May 2014 Compilers@EE-IITB 19

20 Solution: Φ-Function Conditions: what to do on control-flow merge? Original if B then!!a := b! else!!a := c! end!! a! SSA if B then!!a 1 := b! else!!a 2 := c! End! a 3 := Φ(a 1,a 2 )!! a 3! 23 May 2014 Compilers@EE-IITB 20

21 The Φ-Function Φ- func1ons are always at the beginning of a basic block Select between values depending on control- flow a k+1 := Φ (a 1 a k ): the block has k preceding blocks PHI- func3ons are evaluated simultaneously within a basic block. 23 May 2014 Compilers@EE-IITB 21

22 SSA and CFG SSA is normally used for control- flow graphs (CFG) Basic blocks are in 3- address form 23 May

23 Recall: Control flow graph A CFG models transfer of control in a program nodes are basic blocks (straight- line blocks of code) edges represent control flow (loops, if/else, goto ) if x = y then!!s1! else!!s2! end! S3! 23 May 2014 Compilers@EE-IITB 23

24 SSA: a Simple Example if B then!!a1 := 1! else!!a2 := 2! End! a3 := PHI(a1,a2)!! a3! 23 May 2014 Compilers@EE-IITB 24

25 Recall: IR front end produces IR optimizer transforms IR to more efficient program back end transform IR to target code 23 May

26 SSA as IR 23 May

27 Transforming to SSA Problem: Performance / Memory Minimize number of inserted Φ- func1ons Do not spend too much 1me Many rela1vely complex algorithms 23 May 2014 Compilers@EE-IITB 27 27

28 Two steps: Place Φ- func1ons Rename Variables Minimal SSA Where to place Φ- func1ons? We want minimal amount of needed Φ Save memory Algorithms will work faster 23 May

29 Optimization: The Idea Transform the program to improve efficiency Performance: faster execu1on Size: smaller executable, smaller memory footprint Tradeoffs: 1) Performance vs. Size 2) Compila;on speed and memory 23 May

30 Optimization on many levels Op1miza1ons both in the op1mizer and back- end 23 May

31 Examples for Optimizations Constant Folding / Propaga1on Copy Propaga1on Algebraic Simplifica1ons Strength Reduc1on Dead Code Elimina1on Structure Simplifica1ons Loop Op1miza1ons Par1al Redundancy Elimina1on Code Inlining 23 May 2014 Compilers@EE-IITB 31

32 Constant Folding Evaluate constant expressions at compile 1me Only possible when side- effect freeness guaranteed c:= c:= 4 true not false Caveat: Floats implementation could be different between machines! 23 May 2014 Compilers@EE-IITB 32

33 Constant Propagation Variables that have constant value, e.g. b := 3 Later uses of b can be replaced by the constant If no change of b between! b := 3! c := 1 + b! d := b + c! b := 3! c := 1 + 3! d := 3 + c! Analysis needed, as b can be assigned more than once! 23 May 2014 Compilers@EE-IITB 33

34 Copy Propagation for a statement x := y replace later uses of x with y, if x and y have not been changed. x := y! c := 1 + x! d := x + c! x := y! c := 1 + y! d := y + c! Analysis needed, as y and x can be assigned more than once! 23 May 2014 Compilers@EE-IITB Marcus Denker 34

35 Algebraic Simplifications Use algebraic proper1es to simplify expressions -(-i)! i! b or: true! true! Important to simplify code for later optimizations 23 May 2014 Compilers@EE-IITB Marcus Denker 35

36 Strength Reduction Replace expensive opera1ons with simpler ones Example: Mul1plica1ons replaced by addi1ons y := x * 2! y := x + x! Peephole optimizations are often strength reductions 23 May 2014 Compilers@EE-IITB 36

37 Dead Code Remove unnecessary code e.g. variables assigned but never read b := 3! c := 1 + 3! d := 3 + c! c := 1 + 3! d := 3 + c! > Remove code never reached if (false) {a := 5}! if (false) {}! 23 May 2014 Compilers@EE-IITB 37

38 Simplify Structure Similar to dead code: Simplify CFG Structure Op1miza1ons will degenerate CFG Needs to be cleaned to simplify further op1miza1on! 23 May

39 Delete Empty Basic Blocks 23 May

40 Fuse Basic Blocks 23 May

41 Common Subexpression Elimination (CSE) Common Subexpression: There is another occurrence of the expression whose evalua1on always precedes this one operands remain unchanged Local (inside one basic block): When building IR Global (complete flow- graph) 23 May

42 Example CSE b := a + 2! c := 4 * b! b < c?! t1 := a + 2! b := t1! c := 4 * b! b < c?! b := 1! b := 1! d := a + 2! d := t1! 23 May 2014 Compilers@EE-IITB 42

43 Loop Optimizations Op1mizing code in loops is important ojen executed, large payoff All op1miza1ons help when applied to loop- bodies Some op1miza1ons are loop specific 23 May

44 Loop Invariant Code Motion Move expressions that are constant over all itera1ons out of the loop 23 May

45 Induction Variable Optimizations Values of variables form an arithme1c progression integer a(100)! do i = 1, 100! a(i) = * i! endo! value assigned to a decreases by 2 integer a(100)! t1 := 202! do i = 1, 100! t1 := t1-2! a(i) = t1! endo! uses Strength Reduction 23 May 2014 Compilers@EE-IITB 45

46 Partial Redundancy Elimination (PRE) Combines mul1ple op1miza1ons: global common- subexpression elimina1on loop- invariant code mo1on Par;al Redundancy: computa1on done more than once on some path in the flow- graph PRE: insert and delete code to minimize redundancy. 23 May

47 Code Inlining All op1miza1ons up to now were local to one procedure Problem: procedures or func1ons are very short Especially in good OO code! Solu1on: Copy code of small procedures into the caller OO: Polymorphic calls. Which method is called? 23 May

48 Example: Inlining a := power2(b)! power2(x) {! return x*x! }! a := b * b! 23 May 2014 Compilers@EE-IITB 48

49 Optimizations in the Backend Register Alloca1on Instruc1on Selec1on Peep- hole Op1miza1on 23 May

50 Register Allocation Processor has only finite amount of registers Can be very small (x86) Temporary variables non- overlapping temporaries can share one register Passing arguments via registers Op1mizing register alloca1on very important for good performance Especially on x86 23 May

51 Instruction Selection For every expression, there are many ways to realize them for a processor Example: Mul1plica1on*2 can be done by bit- shij Instruc3on selec3on is a form of op3miza3on 23 May 2014 Compilers@EE-IITB Marcus Denker 51

52 Peephole Optimization Simple local op1miza1on Look at code through a hole replace sequences by known shorter ones table pre- computed store R,a;! load a,r! store R,a;! imul 2,R; ashl 1,R; Important when using simple instruc3on selec3on! 23 May 2014 Compilers@EE-IITB 52

53 Thank You 23 May

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! 7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! Roadmap > Introduction! > Optimizations in the Back-end! > The Optimizer! > SSA Optimizations! > Advanced Optimizations! 2 Literature!

More information

6. Intermediate Representation!

6. Intermediate Representation! 6. Intermediate Representation! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/!

More information

6. Intermediate Representation

6. Intermediate Representation 6. Intermediate Representation Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/

More information

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL Compiler Construction Virendra Singh Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail:

More information

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

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 representations source code front end ir opt. ir back end target code front end produce an intermediate representation (IR) for the program. optimizer transforms the code in IR form into an

More information

Lecture 1 Introduc-on

Lecture 1 Introduc-on Lecture 1 Introduc-on What would you get out of this course? Structure of a Compiler Op9miza9on Example 15-745: Introduc9on 1 What Do Compilers Do? 1. Translate one language into another e.g., convert

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

Intermediate Representations

Intermediate Representations Intermediate Representations Intermediate Representations (EaC Chaper 5) Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end

More information

High-Level Synthesis Creating Custom Circuits from High-Level Code

High-Level Synthesis Creating Custom Circuits from High-Level Code High-Level Synthesis Creating Custom Circuits from High-Level Code Hao Zheng Comp Sci & Eng University of South Florida Exis%ng Design Flow Register-transfer (RT) synthesis - Specify RT structure (muxes,

More information

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

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

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

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

8. Static Single Assignment Form. Marcus Denker

8. Static Single Assignment Form. Marcus Denker 8. Static Single Assignment Form Marcus Denker Roadmap > Static Single Assignment Form (SSA) > Converting to SSA Form > Examples > Transforming out of SSA 2 Static Single Assignment Form > Goal: simplify

More information

Compiler: Control Flow Optimization

Compiler: Control Flow Optimization Compiler: Control Flow Optimization Virendra Singh Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/

More information

Intermediate representation

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

More information

Intermediate Representations

Intermediate Representations COMP 506 Rice University Spring 2018 Intermediate Representations source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

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

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit Intermediate Representations 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

More information

Sta$c Single Assignment (SSA) Form

Sta$c Single Assignment (SSA) Form Sta$c Single Assignment (SSA) Form SSA form Sta$c single assignment form Intermediate representa$on of program in which every use of a variable is reached by exactly one defini$on Most programs do not

More information

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013 1 COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013 Lecture Outline 1. Code optimization strategies 2. Peephole optimization 3. Common subexpression elimination

More information

Ways to implement a language

Ways to implement a language Interpreters Implemen+ng PLs Most of the course is learning fundamental concepts for using PLs Syntax vs. seman+cs vs. idioms Powerful constructs like closures, first- class objects, iterators (streams),

More information

Intermediate Representations

Intermediate Representations Intermediate Representations Where In The Course Are We? Rest of the course: compiler writer needs to choose among alternatives Choices affect the quality of compiled code time to compile There may be

More information

Intermediate Representation (IR)

Intermediate Representation (IR) Intermediate Representation (IR) Components and Design Goals for an IR IR encodes all knowledge the compiler has derived about source program. Simple compiler structure source code More typical compiler

More information

Compiler Optimization

Compiler Optimization Compiler Optimization The compiler translates programs written in a high-level language to assembly language code Assembly language code is translated to object code by an assembler Object code modules

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1 Table of Contents About the Authors... iii Introduction... xvii Chapter 1: System Software... 1 1.1 Concept of System Software... 2 Types of Software Programs... 2 Software Programs and the Computing Machine...

More information

Intermediate Code & Local Optimizations

Intermediate Code & Local Optimizations Lecture Outline Intermediate Code & Local Optimizations Intermediate code Local optimizations Compiler Design I (2011) 2 Code Generation Summary We have so far discussed Runtime organization Simple stack

More information

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

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) Intermediate Code Generation Optimization Before and after generating machine

More information

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation CSE 501: Compiler Construction Course outline Main focus: program analysis and transformation how to represent programs? how to analyze programs? what to analyze? how to transform programs? what transformations

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Tour of common optimizations

Tour of common optimizations Tour of common optimizations Simple example foo(z) { x := 3 + 6; y := x 5 return z * y } Simple example foo(z) { x := 3 + 6; y := x 5; return z * y } x:=9; Applying Constant Folding Simple example foo(z)

More information

CS153: Compilers Lecture 15: Local Optimization

CS153: Compilers Lecture 15: Local Optimization CS153: Compilers Lecture 15: Local Optimization Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (2 days) Project 5 out Due Tuesday Nov 13 (21 days)

More information

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example Lecture 36: Local Optimization [Adapted from notes by R. Bodik and G. Necula] Introduction to Code Optimization Code optimization is the usual term, but is grossly misnamed, since code produced by optimizers

More information

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

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction Group B Assignment 8 Att (2) Perm(3) Oral(5) Total(10) Sign Title of Assignment: Code optimization using DAG. 8.1.1 Problem Definition: Code optimization using DAG. 8.1.2 Perquisite: Lex, Yacc, Compiler

More information

: Advanced Compiler Design. 8.0 Instruc?on scheduling

: Advanced Compiler Design. 8.0 Instruc?on scheduling 6-80: Advanced Compiler Design 8.0 Instruc?on scheduling Thomas R. Gross Computer Science Department ETH Zurich, Switzerland Overview 8. Instruc?on scheduling basics 8. Scheduling for ILP processors 8.

More information

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210 Intermediate Representations CS2210 Lecture 11 Reading & Topics Muchnick: chapter 6 Topics today: Intermediate representations Automatic code generation with pattern matching Optimization Overview Control

More information

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations. Lecture 20 Intermediate Code & Local Optimizations Lecture 20 Lecture Outline Intermediate code Local optimizations Next time: global optimizations 2 Code Generation Summary We have discussed Runtime organization

More information

CS415 Compilers. Intermediate Represeation & Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation CS415 Compilers Intermediate Represeation & Code Generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review - Types of Intermediate Representations

More information

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code) Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while

More information

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

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature:

More information

Program Op*miza*on and Analysis. Chenyang Lu CSE 467S

Program Op*miza*on and Analysis. Chenyang Lu CSE 467S Program Op*miza*on and Analysis Chenyang Lu CSE 467S 1 Program Transforma*on op#mize Analyze HLL compile assembly assemble Physical Address Rela5ve Address assembly object load executable link Absolute

More information

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised: EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages Görel Hedin Revised: 2014-10- 13 This lecture Regular expressions Context- free grammar ATribute grammar Lexical analyzer (scanner)

More information

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

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer CS 61C: Great Ideas in Computer Architecture Everything is a Number Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13 9/19/13 Fall 2013 - - Lecture #7 1 New- School Machine Structures

More information

Code Genera*on for Control Flow Constructs

Code Genera*on for Control Flow Constructs Code Genera*on for Control Flow Constructs 1 Roadmap Last *me: Got the basics of MIPS CodeGen for some AST node types This *me: Do the rest of the AST nodes Introduce control flow graphs Scanner Parser

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

Compiler Code Generation COMP360

Compiler Code Generation COMP360 Compiler Code Generation COMP360 Students who acquire large debts putting themselves through school are unlikely to think about changing society. When you trap people in a system of debt, they can t afford

More information

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Other Forms of Intermediate Code. Local Optimizations. Lecture 34 Other Forms of Intermediate Code. Local Optimizations Lecture 34 (Adapted from notes by R. Bodik and G. Necula) 4/18/08 Prof. Hilfinger CS 164 Lecture 34 1 Administrative HW #5 is now on-line. Due next

More information

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages? Administrative Other Forms of Intermediate Code. Local Optimizations HW #5 is now on-line. Due next Friday. If your test grade is not glookupable, please tell us. Please submit test regrading pleas to

More information

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

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

Goals of Program Optimization (1 of 2)

Goals of Program Optimization (1 of 2) Goals of Program Optimization (1 of 2) Goal: Improve program performance within some constraints Ask Three Key Questions for Every Optimization 1. Is it legal? 2. Is it profitable? 3. Is it compile-time

More information

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2] CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2] 1 his Lecture 2 1 Remember: Phases of a Compiler his lecture: Code Optimization means floating point 3 What

More information

Languages and Compiler Design II IR Code Optimization

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

More information

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

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University Code Generation Frédéric Haziza Department of Computer Systems Uppsala University Spring 2008 Operating Systems Process Management Memory Management Storage Management Compilers Compiling

More information

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

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

Intermediate Representations

Intermediate Representations Most of the material in this lecture comes from Chapter 5 of EaC2 Intermediate Representations Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP

More information

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky Compila(on 0368-3133 2014/15a Lecture 6 Seman(c Analysis Noam Rinetzky 1 You are here Source text txt Process text input characters Lexical Analysis tokens Annotated AST Syntax Analysis AST Seman(c Analysis

More information

Induction Variable Identification (cont)

Induction Variable Identification (cont) Loop Invariant Code Motion Last Time Uses of SSA: reaching constants, dead-code elimination, induction variable identification Today Finish up induction variable identification Loop invariant code motion

More information

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 13 : Intermediate Code I Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Introduction Compiler Phases

More information

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

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations Lecture Outline Intermediate code Intermediate Code & Local Optimizations Lecture 14 Local optimizations Next time: global optimizations Prof. Aiken CS 143 Lecture 14 1 Prof. Aiken CS 143 Lecture 14 2

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Intermediate Representations Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 G-1 Agenda Survey of Intermediate Representations Graphical Concrete/Abstract Syntax Trees (ASTs)

More information

Intermediate Representations Part II

Intermediate Representations Part II 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

More information

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006) Data Flow Analysis Suman Jana Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006) Data flow analysis Derives informa=on about the dynamic behavior of a program by only

More information

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

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask? Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the

More information

Compiler Optimization Techniques

Compiler Optimization Techniques Compiler Optimization Techniques Department of Computer Science, Faculty of ICT February 5, 2014 Introduction Code optimisations usually involve the replacement (transformation) of code from one sequence

More information

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

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

The View from 35,000 Feet

The View from 35,000 Feet The View from 35,000 Feet This lecture is taken directly from the Engineering a Compiler web site with only minor adaptations for EECS 6083 at University of Cincinnati Copyright 2003, Keith D. Cooper,

More information

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://www.cs.columbia.

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://www.cs.columbia. W1005 Intro to CS and Programming in MATLAB Brief History of Compu?ng Fall 2014 Instructor: Ilia Vovsha hip://www.cs.columbia.edu/~vovsha/w1005 Computer Philosophy Computer is a (electronic digital) device

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Code Generation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Intermediate Code Code Generation Target Program

More information

CST-402(T): Language Processors

CST-402(T): Language Processors CST-402(T): Language Processors Course Outcomes: On successful completion of the course, students will be able to: 1. Exhibit role of various phases of compilation, with understanding of types of grammars

More information

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

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code Traditional Three-pass Compiler Source Code Front End IR Middle End IR Back End Machine code Errors Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce

More information

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization? What Do Compilers Do? Lecture 1 Introduction I What would you get out of this course? II Structure of a Compiler III Optimization Example Reference: Muchnick 1.3-1.5 1. Translate one language into another

More information

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

CS577 Modern Language Processors. Spring 2018 Lecture Optimization CS577 Modern Language Processors Spring 2018 Lecture Optimization 1 GENERATING BETTER CODE What does a conventional compiler do to improve quality of generated code? Eliminate redundant computation Move

More information

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

USC 227 Office hours: 3-4 Monday and Wednesday  CS553 Lecture 1 Introduction 4 CS553 Compiler Construction Instructor: URL: Michelle Strout mstrout@cs.colostate.edu USC 227 Office hours: 3-4 Monday and Wednesday http://www.cs.colostate.edu/~cs553 CS553 Lecture 1 Introduction 3 Plan

More information

ECE 486/586. Computer Architecture. Lecture # 7

ECE 486/586. Computer Architecture. Lecture # 7 ECE 486/586 Computer Architecture Lecture # 7 Spring 2015 Portland State University Lecture Topics Instruction Set Principles Instruction Encoding Role of Compilers The MIPS Architecture Reference: Appendix

More information

CS 132 Compiler Construction

CS 132 Compiler Construction CS 132 Compiler Construction 1. Introduction 2 2. Lexical analysis 31 3. LL parsing 58 4. LR parsing 110 5. JavaCC and JTB 127 6. Semantic analysis 150 7. Translation and simplification 165 8. Liveness

More information

Optimization Prof. James L. Frankel Harvard University

Optimization Prof. James L. Frankel Harvard University Optimization Prof. James L. Frankel Harvard University Version of 4:24 PM 1-May-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reasons to Optimize Reduce execution time Reduce memory

More information

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure

More information

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II Robin Cockett Department of Computer Science University of Calgary Alberta, Canada robin@cpsc.ucalgary.ca Sept. 2007 Introduction to the course

More information

Research opportuni/es with me

Research opportuni/es with me Research opportuni/es with me Independent study for credit - Build PL tools (parsers, editors) e.g., JDial - Build educa/on tools (e.g., Automata Tutor) - Automata theory problems e.g., AutomatArk - Research

More information

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA Contents i SYLLABUS UNIT - I CHAPTER - 1 : INTRODUCTION Programs Related to Compilers. Translation Process, Major Data Structures, Other Issues in Compiler Structure, Boot Strapping and Porting. CHAPTER

More information

Sta$c Analysis Dataflow Analysis

Sta$c Analysis Dataflow Analysis Sta$c Analysis Dataflow Analysis Roadmap Overview. Four Analysis Examples. Analysis Framework Soot. Theore>cal Abstrac>on of Dataflow Analysis. Inter- procedure Analysis. Taint Analysis. Overview Sta>c

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the

More information

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space Code optimization Have we achieved optimal code? Impossible to answer! We make improvements to the code Aim: faster code and/or less space Types of optimization machine-independent In source code or internal

More information

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Crafting a Compiler with C (II) Compiler V. S. Interpreter Crafting a Compiler with C (II) 資科系 林偉川 Compiler V S Interpreter Compilation - Translate high-level program to machine code Lexical Analyzer, Syntax Analyzer, Intermediate code generator(semantics Analyzer),

More information

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools

More information

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation Comp 204: Computer Systems and Their Implementation Lecture 22: Code Generation and Optimisation 1 Today Code generation Three address code Code optimisation Techniques Classification of optimisations

More information

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

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013 A Bad Name Optimization is the process by which we turn a program into a better one, for some definition of better. CS 2210: Optimization This is impossible in the general case. For instance, a fully optimizing

More information

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons CS 61C: Great Ideas in Computer Architecture Strings and Func.ons Instructor: Krste Asanovic, Randy H. Katz hdp://inst.eecs.berkeley.edu/~cs61c/sp12 Fall 2012 - - Lecture #7 1 New- School Machine Structures

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Announcements Be sure to

More information

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano Building a Runnable Program and Code Improvement Dario Marasco, Greg Klepic, Tess DiStefano Building a Runnable Program Review Front end code Source code analysis Syntax tree Back end code Target code

More information

Programming Language Processor Theory

Programming Language Processor Theory Programming Language Processor Theory Munehiro Takimoto Course Descriptions Method of Evaluation: made through your technical reports Purposes: understanding various theories and implementations of modern

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 3 JVM and optimization. A first look at optimization: Peephole optimization. A simple example A Java class public class A { public static int f (int x) { int r = 3; int

More information

COMPILER CONSTRUCTION Seminar 03 TDDB

COMPILER CONSTRUCTION Seminar 03 TDDB COMPILER CONSTRUCTION Seminar 03 TDDB44 2016 Martin Sjölund (martin.sjolund@liu.se) Mahder Gebremedhin (mahder.gebremedhin@liu.se) Department of Computer and Information Science Linköping University LABS

More information

Front End. Hwansoo Han

Front End. Hwansoo Han Front nd Hwansoo Han Traditional Two-pass Compiler Source code Front nd IR Back nd Machine code rrors High level functions Recognize legal program, generate correct code (OS & linker can accept) Manage

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 00 043 INFORMATION TECHNOLOGY TUTORIAL QUESTION BANK Name AUTOMATA AND COMPILER DESIGN Code A03 Class III B. Tech I Semester Branch

More information

CODE GENERATION Monday, May 31, 2010

CODE GENERATION Monday, May 31, 2010 CODE GENERATION memory management returned value actual parameters commonly placed in registers (when possible) optional control link optional access link saved machine status local data temporaries A.R.

More information

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers 9/11/12 Instructor: Krste Asanovic, Randy H. Katz hcp://inst.eecs.berkeley.edu/~cs61c/sp12 Fall 2012 - - Lecture #8 1 New- School Machine

More information

Intermediate Representations

Intermediate Representations Compiler Design 1 Intermediate Representations Compiler Design 2 Front End & Back End The portion of the compiler that does scanning, parsing and static semantic analysis is called the front-end. The translation

More information

Running class Timing on Java HotSpot VM, 1

Running class Timing on Java HotSpot VM, 1 Compiler construction 2009 Lecture 3. A first look at optimization: Peephole optimization. A simple example A Java class public class A { public static int f (int x) { int r = 3; int s = r + 5; return

More information