Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f.

Slides:



Advertisements
Similar presentations
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Advertisements

CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
14/14/2015 Assalam Alaikum VLSI-2009 Gul Munir Ujjan Lecturer CISE Department, NEDUET Karachi.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
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-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
Optimization Compiler Baojian Hua
Compiler Construction Sohail Aslam Lecture Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
Chapter 8 Intermediate code generation Section 0 Overview 1.Position of Intermediate code generator parser Token stream static checker Syntax tree Intermedi.
Backpatching דוגמא : switch. דוגמא switch : הדקדוק.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Theory of Compilation Erez Petrank Lecture 7: Intermediate Representation Part 2: Backpatching 1.
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation 1.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
CStar Optimizing a C Compiler
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Intermediate Code. Local Optimizations
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
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.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Intermediate Code Generation CS308 Compiler Theory1.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Chap. 4, Intermediate Code Generation
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation and Attribute Grammars 1.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
Optimization Simone Campanoni
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Compiler Construction
Basic Block Optimizations
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Code Generation Part III
Three Address Code Generation Control Statement
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part I
Code Generation Part III
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
Syntax-Directed Translation Part II
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Three Address Code Generation – Backpatching
Intermediate Code Generation Part II
Intermediate Code Generation
Syntax-Directed Translation Part II
Compiler Construction
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Basic Block Optimizations
Presentation transcript:

Test Yourself #2

Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example Algebraic optimization: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

An Example Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

An Example Copy propagation: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

An Example Copy propagation: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

An Example Common subexpression elimination: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

An Example Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

An Example Dead code elimination: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

An Example Dead code elimination: a := x * x f := a + a g := 6 * f This is the final form

Test Yourself #4

תכנוה נורשת.

Int id1,id2 D TL INT L ID1 ID2, L.in T.type L.in

D  L L  L1,id {L.in = L1.in; addtype(id.entery,L1.in);} L  T id {L.in = T.type; addtype(id.entery,T.type);} T  int | {T.type= integer;} real {T.type= real;} פתרון:

D Int id1,id2 L L1,id1 T id2

תרגיל 9

פתרון E.true: S1.next=E.false S2.next=E.true E.place++ If(E.place=3) Goto S.next S1.code E.place++ If(E.place=3) Goto S.next S2.code Goto E.true E.false: S.next: E.true E.place=0 E.code E.false

S  if E then start with S1 else start with S2 {S.code=Gen(E.place’=0’) || E.code || gen(E.true ‘:’ E.place ‘++ if(’ E.place ‘=3) goto’ S.next ) || S1.code || gen(E.false ‘:’ E.place ‘++ if(’ E.place ‘=3) goto’ S.next ) || S2.code || gen(‘goto’ E.true) }

פתרון עם Backpatching S  if R E then start with M1 T1 S1 else start with M2 T2 S2 {S.next_list= merge(T1.next_list,T2.next_list) backpatch (E.true_list, M1.quad) backpatch (E.false_list, M2.quad) backpatch (S1.next_list,M2.quad) backpatch (S2.next_list,M1_quad) Emit(‘goto’ M1.quad) } R  ε {Emit(E.place=0(} T  ε {Emit(E.place ‘++( T.next_list=makelist(nextquad); Emit(if(’ E.place ‘=3) goto’ _______) )}

דוגמא: S  if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* Produced by R  ε*/ 101: if a < b goto______ /*Produced by E  a <b*/ 102: goto_______ /* Produced by E  a <b */ 103: E.place++; /*Produced by T1  ε*/ 104: if E.place ==3 goto________ /*Produced by T1  ε*/ 105: a:=1 /*Produces by S1  a:=1*/ 106: E.place++; /*Produced by T2  ε*/ 107: if E.place ==3 goto________ /*Produced by T2  ε*/ 108: goto___ /*Produced by E  TRUE*/ 109: b:=2 /*Produced by b:=2*/ 110: goto 103 /*Produces by S  if E then start with ….*/

דוגמא: S  if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S  if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S  if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /**/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /**/ 108: goto___ /**/ 109: b:=2 /**/ 110: goto 103 /**/

דוגמא: S  if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S  if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S  if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /**/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /**/ 108: goto__109 /*Backpatched by S  if E then S1*/ 109: b:=2 /**/ 110: goto 103 /**/

דוגמא: S  if a<b then start with a:=1 else start with if true then b:=2 100: E.place =0 /* */ 101: if a < b goto__103 (M1.quad) /* Backpatched by S  if E then start with…*/ 102: goto__106 (M2.quad) /* Backpatched by S  if E then start with…*/ 103: E.place++; /**/ 104: if E.place ==3 goto________ /* Can’t BAckpatched but S.next_list  104*/ 105: a:=1 /**/ 106: E.place++; /**/ 107: if E.place ==3 goto________ /*Can’t BAckpatched but S.next_list  107*/ 108: goto__109 /*Backpatched by S  if E then S1*/ 109: b:=2 /**/ 110: goto 103 /**/

Test yourself #5