Code Optimization and Runtime Environments, Cheat Sheet of Computer science

A comprehensive overview of code optimization techniques and runtime environments in compiler design. It covers the principal sources of optimization, including common subexpression elimination, copy propagation, dead-code elimination, and constant folding. The document also delves into peephole optimization, optimization of basic blocks, and an introduction to global data flow analysis. Additionally, it explores runtime environments, source language issues, storage organization, storage allocation strategies, access to non-local names, and parameter passing. The detailed coverage of these topics makes this document a valuable resource for students and researchers interested in compiler design and optimization.

Typology: Cheat Sheet

2020/2021

Uploaded on 04/28/2023

kingsman-sees
kingsman-sees 🇮🇳

1 document

1 / 75

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
07/20/2024 Mr.R.Raja Sekar, AP/CSE, KARE 1
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
School of Computing
Kalasalingam Academy of Research and Education
Academic Year : 2020 -2021
Summer Semester
Course Code : CSE18R174
Course Name : Compiler Design
UNIT V
FACULTY NAME:
Mr.R.Raja Sekar, AP- CSE
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b

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.