Docsity

Code Optimization and Runtime Environments, Cheat Sheet of Computer science

Partial preview of the text

Download Code Optimization and Runtime Environments and more Cheat Sheet Computer science in PDF only on Docsity!

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING

School of Computing

Kalasalingam Academy of Research and Education

Academic Year : 2020 -

Summer Semester

Course Code : CSE18R

Course Name : Compiler Design

UNIT V

FACULTY NAME:

Mr.R.Raja Sekar, AP- CSE

Course Outcome โ€“ Unit V

C

K

UNIT V Code Optimization and Run Time Environments 9

Introductionโ€“ Principal Sources of Optimization โ€“ Peephole

Optimization- Optimization of basic Blocks โ€“ Introduction to Global Data Flow

Analysis โ€“ Runtime Environments โ€“ Source Language issues โ€“ Storage

Organization โ€“ Storage Allocation strategies โ€“ Access to non-local names โ€“

Parameter Passing.

Apply for various optimization techniques for

dataflow analysis.

Code Optimization - Introduction

๏ƒ˜ Optimization is a program transformation

technique, which tries to improve the code by

making it consume less resources (i.e. CPU,

Memory) and deliver high speed.

Code Optimization โ€“ Introduction (..contd)

A code optimizing process must follow the

three rules given below:

  • The output code must not, in any way, change the meaning of the

program.

  • Optimization should increase the speed of the program and if

possible, the program should demand less number of resources.

  • Optimization should itself be fast and should not delay the

overall compiling process.

Code Optimization โ€“ Types

๏ƒ˜ Machine-independent Optimization

๏ƒ˜ Machine-dependent Optimization

Machine-independent Optimization

This code optimization phase attempts to improve

the intermediate code to get a better target code as

the output.

The part of the intermediate code which is

transformed here does not involve any CPU registers

or absolute memory locations.

Code Optimization โ€“ Phases

๏ƒ˜ Global Optimization:

Transformations are applied to large program segments that

includes functions, procedures and loops.

๏ƒ˜ Local Optimization:

Transformations are applied to small blocks of statements. The

local optimization is done prior to global optimization.

Principal sources Code Optimization

Common Subexpressions elimination

Copy Propagation

Dead-Code elimination

Constant Folding

DAG for Blocks โ€“ An Example

Copy Propagation

Assignments of the form f : = g called copy statements, or

copies for short.

The idea behind the copy-propagation transformation is to use

g for f, whenever possible after the copy statement f: = g.

Copy propagation means use of one variable instead of another.

This may not appear to be an improvement, but as we shall see

it gives us an opportunity to eliminate x.

  • For example:

x=Pi;

A=xrr;

The optimization using copy propagation can be done as follows: A=Pirr;

Here the variable x is eliminated

Constant folding

Deducing at compile time that the value of an expression is a constant

and using the constant instead is known as constant folding. One

advantage of copy propagation is that it often turns the copy

statement into dead code.

For example,

a=3.14157 /2 can be replaced by

a=1.570 there by eliminating a division operation.

Loop Optimizations

Code motion, which moves code outside a loop

Induction-variable elimination , which we apply to replace

variables from inner loop.

Reduction in strength , which replaces and expensive

operation by a cheaper one, such as a multiplication by an

addition.

Code motion

An important modification that decreases the amount of code

in a loop is code motion.

This transformation takes an expression that yields the same

result independent of the number of times a loop is executed (a

loop-invariant computation) and places the expression before

the loop.

For example, evaluation of limit-2 is a loop-invariant

computation

in the following while-statement:

while (i <= limit-2) /* statement does not change limit*/

Code motion will result in the equivalent of

t= limit-2;

while (i<=t) /* statement does not change limit or t */

Induction Variables

Loops are usually processed inside out. For example consider

the loop around B3.

Note that the values of j and t4 remain in lock-step; every time

the value of j decreases by 1, that of t4 decreases by 4 because

4*j is assigned to t4.

Such identifiers are called induction variables.