8 Optimisation. 8.2 Machine-Independent Optimisation

Size: px
Start display at page:

Download "8 Optimisation. 8.2 Machine-Independent Optimisation"

Transcription

1 8 8.2 Machine-Independent Replacing binary with Unary operations Replacing binary with Unary operators The following operations do not produce redundant quads: a=c-d; 1. (-, c, d, t1) b=d-c; => 2. (=, t1,, a) 3. (-, d, c, t2) 4. (=, t2,, b) However, as humans we can note that t1 will hold the same value as t2, only negated We can extend our redundancy elimination routine to look for such cases, and replace the 3rd quadruple with a unary minus: 1. (-, c, d, t1) 2. (=, t1,, a) 3. (_, t1,, t2) 4. (=, t2,, b) Using Unary Operators We have the same number of operations, But unary operations are cheaper than binary 2 1

2 8 8.2 Machine-Independent Reducing number of intermediate variables Reducing Intermediate Variables Reducing number of intermediate variables The intermediate variables (t1, t2, etc.) used in quads allow partial results from one sub-expression to be passed to the next sub-expression. Each of these variables would ideally be held in a machine register, as operations on registers are fast, while storage in the stack or in addressable memory are slower. However, the number of registers on a given machine is restricted. If we can reduce the number of intermediate variables used by our code, then we can perhaps optimise use of registers. 4 2

3 Reducing Intermediate Variables Reducing number of intermediate variables: reusing temp vars A first step here would be to note where an intermediate variable has no more acceses, and allow it to be re-used. 1.(*, a, b, t1) (a*b)+(c+d) => 2. (+, c, d, t2) 3. (+, t1, t2, t3) Analysing the quadruples after (3) would show that t1 is not needed again. We could thus replace all references to t3 in the quadruples with t1, thus reducing the number of temp vars. 5 Reducing Intermediate Variables Reducing number of intermediate variables: example Some orderings of operations allow more of this type of optimisation. For example, the following two expressions are equivalent: (a*b)+(c+d) and ((a*b)+c)+d However, the quadrupes they produce require different numbers of intermediate variables: 1.(*, a, b, t1) (a*b)+(c+d) => 2. (+, c, d, t2) 3. (+, t1, t2, t1) 1.(*, a, b, t1) ((a*b)+c)+d => 2. (+, t1, c, t1) 3. (+, t1, d, t1) 6 3

4 Reducing Intermediate Variables Reducing number of intermediate variables: example Similar result for the following: (a+b)*(c*d) and ((a+b)*c)*d In general, we can optimise by recognising where a bracketed expression has the same operator as that which applies to it, and changing the order in which we apply the operators. Note also that in some cases we first need to reorder the bracketed expressions: (c*d)*(a+b) We will still need two operations here, unless we reorder: (a+b)*(c*d) Machine-Independent Optimising Loops 4

5 Loop Optimising Loops Much of the execution time in a program will be consumed with a loop, or within nested loops. Any simplification we make within the loop will thus have substantial effect on efficiency as a whole. We will look at two types of optimisation here: 1. Moving Loop-invariant instructions outside of the loop 2. Reduction of Force: replacing expensive operations with cheaper ones 9 Loop Intermediate Representation of Loops The direct Translation of a loop can be as follows: int a, b=10, c=5; for (i=0; i<10; i++) { a=i*(b+c); } Initialisation: Body: Increment: 1. (:=, 10,, b) 2. (:=, 5,, c) 3. (:=, 0,, i) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (+, b, c, t2) 7. (*, i, t2, t3) 8. (:=, t3,, a) 9. (+, i, 1, i) 10.(jmp, 4,, )

6 Some concepts: int a, b=10, c=5; for (i=0; i<10; i++) { a=i*(b+c); } Loop Variable: the loop counter (i) Loop Loop Increment: the quantity added to the loop variable on each loop Loop Invariants: variables used within the loop which do not change value within the loop (b, c) 1. (:=, 10,, b) 2. (:=, 5,, c) 3. (:=, 0,, i) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (+, b, c, t2) 7. (*, i, t2, t3) 8. (:=, t3,, a) 9. (+, i, 1, i) 10.(jmp, 4,, ) : Loop-Invariant Instructions In this optimisation, we look for quadruples within the loop body where the operands do not change within the loop. For example: int a, b=10, c=5; for (i=0; i<10; i++) { a=i*(b+c); } Loop Body: 1. (:=, 10,, b) 2. (:=, 5,, c) 3. (:=, 0,, i) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (+, b, c, t2) 7. (*, i, t2, t3) 8. (:=, t3,, a) 9. (+, i, 1, i) 10.(jmp, 4,, )

7 Loop Loop-Invariant Instructions In such cases, we can move the loop invariant code BEFORE the loop, into the Initialisation part 1. (:=, 10,, b) 2. (:=, 5,, c) 3. (:=, 0,, i) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (+, b, c, t2) 7. (*, i, t2, t3) 8. (:=, t3,, a) 9. (+, i, 1, i) 10.(jmp, 4,, ) 11. Init Body 1. (:=, 10,, b) 2. (:=, 5,, c) 3. (:=, 0,, i) 4. (+, b, c, t2) 5. (<, i, 10, t1) 6. (jfalse, 11,, ) 7. (*, i, t2, t3) 8. (:=, t3,, a) 9. (+, i, 1, i) 10.(jmp, 4,, ) 11. Init Body 13 Loop Loop-Invariant Instructions To spot such cases, we do the following: Calculate the Loop-variant variables: 1. Set LOOP_VARS to { } 2. For each quadruple within the loop: If there is a RES field, place it in LOOP_VARS Locate Loop-invariant Instructions: 3. For each quadruple within the loop: If operator is not a jump: if neither OP1 nor OP2 is in LOOP_VARS: Move the quad outside of the loop If no other quad in the loop sets RES, delete RES from LOOP_VARS 14 7

8 Loop 1: Reduction of Force Often loops contain multiplication of the loop variable: for (i=1; i<10; i++) { a=i*d; } Such cases can be re-written using a less expensive operation, addition: a=0; for (i=1; i<10; i++) { a = a+d; } The result is exactly the same 15 Loop Spotting Candidates for reduction of force We can look at code within a loop for quadruples which match the following: (*, loop_invariant, loop_variable, result) or (*, loop_variable, loop_invariant, result) 16 8

9 Loop Reduction of force: the simple case The simple case: loop increment is 1, Assume we recognise a quadrule: (*, loop_invariant, loop_variable, result) In such cases, we: 1. Move the Quad into the initialisation step (outside the loop), but after the loop variable is initialised 2. Place in the increment part: (+, result, loop_invariant, result) 17 Loop Reduction of force: the simple case 1. (:=, 1,, i) 2. (<, i, 10, t1) 3. (jfalse, 8,, ) 4. (*, i, d, t2) 5. (:=, t2,, a) 6. (+, i, 1, i) 7. (jmp, 2,, ) (:=, 1,, i) Init 2. (*, i, d, t2) 3. (<, i, 9, t1) 4. (jfalse, 9,, ) 5. (:=, t2,, a) 6. (+, i, 1, i) 7. (+, t2, d, t2) Incr 8. (jmp, 3,, )

10 Loop Reduction of force: More complex cases Loop increment might not be 1 (could be -1 or > 1): Loop initial value might not be 1 Assume we recognise a quadrule: (*, loop_invariant, loop_variable, result) In such cases, we: 1. Move the matched quadruple to the init phase (after the initialisation of the loop variable ) 2. Insert AFTER this line: (*, loop_increm, loop_invariant, new_invar) ; the increm 3. Place in the Increment part : (+, result, new_invar, result) 19 Loop Reduction of force: complex case Initial value of i = 4, increment = 2 1. (:=, 4,, i) 2. (<, i, 10, t1) 3. (jfalse, 8,, ) 4. (*, i, d, t2) 5. (:=, t2,, a) 6. (+, i, 2, i) 7. (jmp, 2,, ) (:=, 4,, i) 2. (*, i, d, t2) 3. (*, 2, d, ni) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (:=, t2,, a) 7. (+, i, 2, i) 8. (+, t2, ni, t2) 9. (jmp, 4,, )

11 Loop Reduction of force: complex case Note that if the loop is executed few times, the extra executions OUTSIDE the loop may outway the gain. 1. (:=, 4,, i) 2. (<, i, 10, t1) 3. (jfalse, 8,, ) 4. (*, i, d, t2) 5. (:=, t2,, a) 6. (+, i, 2, i) 7. (jmp, 2,, ) (:=, 4,, i) 2. (*, i, d, t2) 3. (*, 2, d, ni) 4. (<, i, 10, t1) 5. (jfalse, 11,, ) 6. (:=, t2,, a) 7. (+, i, 2, i) 8. (+, t2, ni, t2) 9. (jmp, 4,, ) Reduction of force: Example Given the following source code: for (j=1; j<50; j=j+2;) { k= ( d+j*f ) } Loop The following quadruples could be generated for this code: INIT: (=, 1,, j) LOOP: (*, j, f, t1) (+, t1, d, t2) INCR: (+, j, 2, j) Consider: d and f as loop invariant. The label INIT indicates the start of the INITIALISATION part of the loop. The label LOOP, indicates the start of the BODY of the loop. The label INCR indicates the start of the INCREMENT part of the loop Questions: What is the Loop Variable? What is the Loop Increment? Apply the reduction of force optimisation to the quadruples above 22 11

12 8 8.2 Machine-Independent Dead Code Elimination Dead Code Elimination Identification and Elimination of Dead Code During the coding of applications, involving frequent modification of the source code, sometimes it occurs that variables are set without their value being utilised. Also, as a result of reduction of force and similar optimisations, some assignments might cease to be useful

13 Dead Code Elimination Identification and Elimination of Dead Code Take the following code: void foo() { int a = 24; int b = 25; int c; c = a << 2; return; b = 24; } The last line is unreachable due to the return before it, it can be eliminated. The assignment to b above can also be eliminated, as b is never used. Since c is a local variable, not accessible from outside the function, c and thus a can be eliminated from the function. In fact, since the function returns nothing, and has no global effects, the whole function could be eliminated. 25 Dead Assignments Dead Code Elimination Where an assignment is made to a variable, but the value is never utilised Algorithm: For each occurrence of an assignment to variable V Look at the quadruples below in the same block If we reach another assignment to V BEFORE V is referenced (in Op1 or Op2 field), then we have a dead assignment We can eliminate the earlier assignment to V Note this may create new dead assignments, if the operands of the deleted quadruple have no other reference. For each operand in the deleted quadruple If the operand is a variable Find the previous occuring assignment to that variable repeat this algorithm on that assignment

14 8 8.3 Machine-Dependent Re-arrangement of code Re-arrangement of code Re-arrangement of code In some circumstances, the rearrangement of instructions permits reduction of the size or the complication of the object code. Example In many machines: fixed point multiplication of two integer operands produces a doublelength result, division takes a double and a long as operands, and generates a quotient and a remainder of long length. A simple rearrangement of the operations can give rise to optimizations

Lesson 7: Code optimisation. Compilers. Code optimisation. Code optimisation. Kinds of optimisations. Introductory concepts

Lesson 7: Code optimisation. Compilers. Code optimisation. Code optimisation. Kinds of optimisations. Introductory concepts Compilers Lesson 7: Code optimisation Third year Spring term Alfonso Ortega: alfonso.ortega@uam.es Enrique Alfonseca: Enrique.Alfonseca@uam.es Code optimisation Introductory concepts This process can be

More information

8 Optimisation. 8.1 Introduction to Optimisation

8 Optimisation. 8.1 Introduction to Optimisation 8 8.1 Introduction to 8.1 Introduction refers to compiler strategies which result in generated object code which is more efficient, either in terms of speed or memory use. can be performed in two locations:

More information

8 Optimisation. 8.2 Machine-Independent Optimisation

8 Optimisation. 8.2 Machine-Independent Optimisation 8 8.2 Machine-Independent 8.2.2 Eliminating Redundant Instructions The aim of this optimisation is to identify instructions that do not need to be performed, and eliminating them. This can be done at the

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

COMPILER DESIGN - CODE OPTIMIZATION

COMPILER DESIGN - CODE OPTIMIZATION COMPILER DESIGN - CODE OPTIMIZATION http://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm Copyright tutorialspoint.com Optimization is a program transformation technique,

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

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

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES (2018-19) (ODD) Code Optimization Prof. Jonita Roman Date: 30/06/2018 Time: 9:45 to 10:45 Venue: MCA

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

Machine-Independent Optimizations

Machine-Independent Optimizations Chapter 9 Machine-Independent Optimizations High-level language constructs can introduce substantial run-time overhead if we naively translate each construct independently into machine code. This chapter

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

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

UNIT-V. Symbol Table & Run-Time Environments Symbol Table 1 P a g e UNIT-V Symbol Table & Run-Time Environments Symbol Table Symbol table is a data structure used by compiler to keep track of semantics of variable. i.e. symbol table stores the information about

More information

Operators and Expressions:

Operators and Expressions: Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Expression and Operator

Expression and Operator Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a

More information

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

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

Operators & Expressions

Operators & Expressions Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

More information

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary

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

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

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Program Analysis Compile-time reasoning about run-time behavior

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

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

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

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

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff C Programming Language 1 C C is better to use than assembly for embedded systems programming. You can program at a higher level of logic than in assembly, so programs are shorter and easier to understand.

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

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

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

Computer Systems Lecture 9

Computer Systems Lecture 9 Computer Systems Lecture 9 CPU Registers in x86 CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions

More information

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013 Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

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

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Lecture 3 COP 3014 Spring January 16, 2018 Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

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

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion Outline Loop Optimizations Induction Variables Recognition Induction Variables Combination of Analyses Copyright 2010, Pedro C Diniz, all rights reserved Students enrolled in the Compilers class at the

More information

CA4003 Compiler Construction Assignment Language Definition

CA4003 Compiler Construction Assignment Language Definition CA4003 Compiler Construction Assignment Language Definition David Sinclair 2017-2018 1 Overview The language is not case sensitive. A nonterminal, X, is represented by enclosing it in angle brackets, e.g.

More information

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur Control Structures Code can be purely arithmetic assignments At some point we will need some kind of control or decision making process to occur C uses the if keyword as part of it s control structure

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

More information

SECOND PUBLIC EXAMINATION. Compilers

SECOND PUBLIC EXAMINATION. Compilers A10401W1 SECOND PUBLIC EXAMINATION Honour School of Computer Science Honour School of Mathematics and Computer Science Honour School of Computer Science and Philosophy Compilers TRINITY TERM 2016 Thursday

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

Storage in Programs. largest. address. address

Storage in Programs. largest. address. address Storage in rograms Almost all operand storage used by programs is provided by memory. Even though registers are more efficiently accessed by instructions, there are too few registers to hold the stored

More information

Programming Language Implementation

Programming Language Implementation A Practical Introduction to Programming Language Implementation 2014: Week 12 Optimisation College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics why primitives

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 Grading survey

More information

Fixed-Point Math and Other Optimizations

Fixed-Point Math and Other Optimizations Fixed-Point Math and Other Optimizations Embedded Systems 8-1 Fixed Point Math Why and How Floating point is too slow and integers truncate the data Floating point subroutines: slower than native, overhead

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

LECTURE 17. Expressions and Assignment

LECTURE 17. Expressions and Assignment LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)

More information

More Code Generation and Optimization. Pat Morin COMP 3002

More Code Generation and Optimization. Pat Morin COMP 3002 More Code Generation and Optimization Pat Morin COMP 3002 Outline DAG representation of basic blocks Peephole optimization Register allocation by graph coloring 2 Basic Blocks as DAGs 3 Basic Blocks as

More information

Operators And Expressions

Operators And Expressions Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

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

Department of Computer Science

Department of Computer Science Department of Computer Science Definition An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and

More information

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002 University of Arizona, Department of Computer Science CSc 453 Assignment 5 Due 23:59, Dec 4 100 points Christian Collberg November 19, 2002 1 Introduction Your task is to write a code generator for the

More information

Compilers and computer architecture Code-generation (2): register-machines

Compilers and computer architecture Code-generation (2): register-machines 1 / 1 Compilers and computer architecture Code-generation (2): register-machines Martin Berger November 2018 Recall the function of compilers 2 / 1 3 / 1 Plan for this week Source program Lexical analysis

More information

Midterm 2 Review Chapters 4-16 LC-3

Midterm 2 Review Chapters 4-16 LC-3 Midterm 2 Review Chapters 4-16 LC-3 ISA You will be allowed to use the one page summary. 8-2 LC-3 Overview: Instruction Set Opcodes 15 opcodes Operate instructions: ADD, AND, NOT Data movement instructions:

More information

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( ) Sir Muhammad Naveed Arslan Ahmed Shaad (1163135 ) Muhammad Bilal ( 1163122 ) www.techo786.wordpress.com CHAPTER: 2 NOTES:- VARIABLES AND OPERATORS The given Questions can also be attempted as Long Questions.

More information

Chapter 3 Structure of a C Program

Chapter 3 Structure of a C Program Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand

More information

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic

More information

Efficient Incremental Dynamic Invariant Detection

Efficient Incremental Dynamic Invariant Detection Efficient Incremental Dynamic Invariant Detection and Michael Ernst MIT CSAIL Page 1 Dynamic invariant detection Program analysis that generalizes over observed runtime values to hypothesize program properties

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC. XC Specification IN THIS DOCUMENT Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage

More information

Lecture 6: Arithmetic and Threshold Circuits

Lecture 6: Arithmetic and Threshold Circuits IAS/PCMI Summer Session 2000 Clay Mathematics Undergraduate Program Advanced Course on Computational Complexity Lecture 6: Arithmetic and Threshold Circuits David Mix Barrington and Alexis Maciel July

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

Computer Programming CS F111

Computer Programming CS F111 Computer Programming CS F111 BITS Pilani Dubai Campus NAND KUMAR Basics of C Programming BITS Pilani Dubai Campus Write a program that 1. Asks 5 marks from the user, find the average of the marks and print

More information

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College CS5 Programming Languages Handout # 9 Prof. Lyn Turbak March, 00 Wellesley College Postfix: A Simple Stack Language Several exercises and examples in this course will involve the Postfix mini-language.

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation. UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are

More information

File Management. Logical Structure. Positioning Fields and Records. Primary and Secondary Keys. Sequential and Direct Access.

File Management. Logical Structure. Positioning Fields and Records. Primary and Secondary Keys. Sequential and Direct Access. File Management Logical Structure Positioning Fields and Records Primary and Secondary Keys Sequential and Direct Access Binary Search File Management File Indexing Chapter 2 Logical Structure File, on

More information

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit-2 (Operators) ANAND KR.SRIVASTAVA Unit-2 (Operators) ANAND KR.SRIVASTAVA 1 Operators in C ( use of operators in C ) Operators are the symbol, to perform some operation ( calculation, manipulation). Set of Operations are used in completion

More information

Instruction Sets: Characteristics and Functions Addressing Modes

Instruction Sets: Characteristics and Functions Addressing Modes Instruction Sets: Characteristics and Functions Addressing Modes Chapters 10 and 11, William Stallings Computer Organization and Architecture 7 th Edition What is an Instruction Set? The complete collection

More information

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1 CSIS1120A 10. Instruction Set & Addressing Mode CSIS1120A 10. Instruction Set & Addressing Mode 1 Elements of a Machine Instruction Operation Code specifies the operation to be performed, e.g. ADD, SUB

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

Shift and Rotate Instructions

Shift and Rotate Instructions Shift and Rotate Instructions Shift and rotate instructions facilitate manipulations of data (that is, modifying part of a 32-bit data word). Such operations might include: Re-arrangement of bytes in a

More information

Spark verification features

Spark verification features Spark verification features Paul Jackson School of Informatics University of Edinburgh Formal Verification Spring 2018 Adding specification information to programs Verification concerns checking whether

More information

CT 229. Java Syntax 26/09/2006 CT229

CT 229. Java Syntax 26/09/2006 CT229 CT 229 Java Syntax 26/09/2006 CT229 Lab Assignments Assignment Due Date: Oct 1 st Before submission make sure that the name of each.java file matches the name given in the assignment sheet!!!! Remember:

More information

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers ECE 601 - Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers Fall 2001 Final Version (Important changes from original posted Exercise 1 shown in color) Variables

More information

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL 6.035 Project 3: Unoptimized Code Generation Jason Ansel MIT - CSAIL Quiz Monday 50 minute quiz Monday Covers everything up to yesterdays lecture Lexical Analysis (REs, DFAs, NFAs) Syntax Analysis (CFGs,

More information

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision

More information

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler ECE220: Computer Systems and Programming Spring 2018 Honors Section Machine Problem 11 due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler This assignment requires you to use recursion

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

DK2. Handel-C code optimization

DK2. Handel-C code optimization DK2 Handel-C code optimization Celoxica, the Celoxica logo and Handel-C are trademarks of Celoxica Limited. All other products or services mentioned herein may be trademarks of their respective owners.

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures Homework 3 Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) 1 CSCI 402: Computer Architectures Arithmetic for Computers (2) Fengguang Song Department

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

Expression Evaluation and Control Flow. Outline

Expression Evaluation and Control Flow. Outline Expression Evaluation and Control Flow In Text: Chapter 6 Outline Notation Operator Evaluation Order Operand Evaluation Order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler Computer Science Department Indiana University Bloomington, IN Februa

Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler Computer Science Department Indiana University Bloomington, IN Februa Destination-Driven Code Generation R. Kent Dybvig, Robert Hieb, Tom Butler dyb@cs.indiana.edu Indiana University Computer Science Department Technical Report #302 February 1990 Destination-Driven Code

More information

Compiler Design and Construction Optimization

Compiler Design and Construction Optimization Compiler Design and Construction Optimization Generating Code via Macro Expansion Macroexpand each IR tuple or subtree A := B+C; D := A * C; lw $t0, B, lw $t1, C, add $t2, $t0, $t1 sw $t2, A lw $t0, A

More information

Embedded Controller Programming 2

Embedded Controller Programming 2 Embedded Controller Programming 2 Section 3: C Language for Embedded Systems - Ken Arnold ecp2@hte.com Copyright 2006 Ken Arnold Overview Structures Unions Scope of Variables Pointers Operators and Precedence

More information

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation CS 91.406/534 Compiler Construction University of Massachusetts Lowell Professor Li Xu Fall 2004 NOTHING: A Language for Practice Implementation 1 Introduction NOTHING is a programming language designed

More information

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis Loop Invariant Code Motion Loop Invariant Code Motion Background: ud- and du-chains Last Time Control flow analysis Today Loop invariant code motion ud-chains A ud-chain connects a use of a variable to

More information

CPSC 467b: Cryptography and Computer Security

CPSC 467b: Cryptography and Computer Security CPSC 467b: Cryptography and Computer Security Michael J. Fischer Lecture 7 January 30, 2012 CPSC 467b, Lecture 7 1/44 Public-key cryptography RSA Factoring Assumption Computing with Big Numbers Fast Exponentiation

More information