0% found this document useful (0 votes)
1 views85 pages

Module 5

The document discusses loop optimization techniques and code optimization methods to improve program performance, focusing on reducing execution time and resource consumption. Key techniques include code motion, induction variable elimination, loop unrolling, and dead code elimination, among others. Additionally, it covers data flow analysis, basic block optimization, and machine-independent optimizations aimed at enhancing code efficiency without hardware dependencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views85 pages

Module 5

The document discusses loop optimization techniques and code optimization methods to improve program performance, focusing on reducing execution time and resource consumption. Key techniques include code motion, induction variable elimination, loop unrolling, and dead code elimination, among others. Additionally, it covers data flow analysis, basic block optimization, and machine-independent optimizations aimed at enhancing code efficiency without hardware dependencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Module 5

Loop Optimization
Loop Optimization

• Most execute time of a program is spend on loops

• Decreasing the number of instructions in an inner loop improves the


running time of a program.

• Loop Optimization Techniques


• Code Motion

• Induction variable elimination and reduction in strength

• Loop unrolling

• Loop Jamming
https://www.youtube.com/watch?v=4VgQiCcKUDM
Induction variable elimination and reduction in
strength
• The transformation of replacing an expensive operation, such as
multiplication by a cheaper one such as addition.
• A variable x is called an induction variable of loop L if the value
of the loop gets changed every time.
Loop unrolling
Loop Jamming
Principal Sources of Optimization
Code Optimization – Improves the intermediate code
- Less space and time

Code Optimization Techniques


1. Common subexpression elimination
2. Constant Folding
3. Copy Propagation
4. Dead code elimination
5. Code Motion
6. Induction variable elimination and strength reduction
Common subexpression elimination
Common subexpression –
i. if it was previously computed
ii. values of variables have not changed
Constant Folding

• If the value of an expression is constant, use the constant


value instead of expression
Copy Propagation
Dead Code elimination
• A variable is live, if its value can be used subsequently, otherwise it is
dead
Code Motion
• Move code outside a loop
Induction variable elimination and strength
reduction
Data Flow Analysis
Data Flow Analysis

• Data flow analysis is an analysis that determines the information


regarding the definition use of data in a program.

• With the help of this, optimization can be done.


Data Flow Properties
1. Available Expression
• An expression ‘a + b’ is said to be available at a program point ‘x’, if none of
its operands get modified before their use.
• It is used to eliminate common sub expression.
Data Flow Properties
2. Reaching Definition
• A definition D is reaching to a point ‘x’ if D is not killed or redefined before
that point.

• It is used in constant/variable propagation.


Data Flow Properties

3. Live Variable
A variable is said to be live at some point
p, if from p to end, the variable is used
before it is redefined else it becomes
dead.
Advantage:
• It is useful for register allocation
• It is used in dead code elimination
Basic Block
Basic Blocks
• A basic block is a sequence of consecutive instructions
which are always executed in sequence without halt.

• The basic block does not have any jump statements


among them

• When the first instruction is executed, all the instruction in


the same basic block can be executed in their sequence of
appearance without losing the flow control from the
program
• The representation of basic block is constructed as follows:

1. Partition the intermediate code into basic blocks which are maximal sequence
of consecutive three- address instruction with the properties that

i. The flow of control can only enter the basic block through the first
instruction in the block. There are no jumps in the middle of the block.

ii. Control will leave the block without halting or branching.

2. The basic blocks becomes the nodes of a flow graph whose edges indicate
which blocks can follow which other blocks.
Basic Blocks and flow graphs
c=0 Three address code for the given code is-
1.c = 0
do 2.if (a < b) goto (4)
{ 3.goto (7)
4.T1 = x + 1
if (a < b) then
5.x = T1
x++; 6.goto (9)
else 7.T2 = x – 1
8.x = T2
x–; 9.T3 = c + 1
c++; 10.c = T3
} while (c < 5) 11.if (c < 5) goto (2)
Basic Blocks and flow graphs -
Exercise
Optimization or transformation of basic blocks
• Optimization is applied to the basic blocks after the intermediate code
generation phase of the compiler.

• Optimization is the process of transforming a program that improves the code by


consuming fewer resources and delivering high speed.

• In optimization, high-level codes are replaced by their equivalent efficient low-


level codes.

• Optimization of basic blocks can be machine-dependent or machine-


independent.

• These transformations are useful for improving the quality of code that will be
ultimately generated from basic block.
Types of basic block optimizations
• Structure preserving transformations
• Dead Code Elimination
• Common Subexpression Elimination
• Renaming of Temporary variables
• Interchange of two independent adjacent statements
• Algebraic transformations
• Constant Folding
• Copy Propagation
• Strength Reduction
Common Subexpression Elimination
• The sub-expression which are common are used frequently are
calculated only once and reused when needed. DAG ( Directed
Acyclic Graph ) is used to eliminate common subexpressions.
Dead Code Elimination:

• Dead code is defined as that part of the code that never executes
during the program execution.

• Eliminating the dead code increases the speed of the program as the
compiler does not have to translate the dead code.
Renaming of Temporary Variables
• Statements containing instances of a temporary variable can be
changed to instances of a new temporary variable without
changing the basic block value.
Statement t = a + b can be changed
to x = a + b where t is a temporary
variable and x is a new temporary
variable without changing the
value of the basic block.
Interchange of Two Independent Adjacent Statements

• If a block has two adjacent statements which are


independent can be interchanged without affecting the
basic block value.
Algebraic Transformation
• Countless algebraic transformations can be used to change the set of
expressions computed by a basic block into an algebraically
equivalent set.

1. Constant Folding

• Solve the constant terms which are continuous so that compiler does
not need to solve this expression.

x = 2 * 3 + y ⇒ x = 6 + y (Optimized code)
Copy Propagation

• It is of two types, Variable Propagation, and Constant Propagation.


Variable Propagation:

Constant Propagation:
Strength Reduction
• Replace expensive statement/ instruction with cheaper ones.
Peephole Optimization
Peephole Optimization
• Peephole optimization is a type of code Optimization performed on a
small part of the code. It is performed on a very small set of
instructions in a segment of code.

• The objective of peephole optimization is as follows:

• To improve performance

• To reduce memory

• To reduce code size


Typical Optimization
• Eliminating redundant loads and stores
• Eliminating Unreachable Code
• Flow-of-control Optimization
• Algebraic simplification
• Reduction in strength
Eliminating Redundant load and store
• In this technique, redundancy is eliminated.

Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;
Optimized code:
y = x + 5;
w = y * 3;
//* We've removed two redundant variables i & z
Eliminating Unreachable Code
a := a
b := b
DAG Representation of Basic
Blocks
DAG representation of basic blocks
• Useful data structures for implementing transformations on basic blocks

• Gives a picture of how value computed by a statement is used in subsequent


statements

• Good way of determining common sub-expressions

• A dag for a basic block has following labels on the nodes

- leaves are labeled by unique identifiers, either variable names or constants

- interior nodes are labeled by an operator symbol

- nodes are also optionally given a sequence of identifiers for labels


Consider following basic block and construct DAG
•t1=a+b
•t2=c+d
• t 3 = e -t 2
• X = t 1 -t 3
Exercise
Construct a DAG for the following :
1. a + a * (b - c) + (b - c) * d
2. DAG for the block
a=b+c
b=a-d
c=b+c
d=a-d
3. DAG for the block
a=b+c
b=b-d
c=c+d
e=b+c
Loops in Flow Graph
Machine Independent
Optimization
Machine Independent code optimization

• Machine Independent code optimization tries to make the intermediate


code more efficient by transforming a section of code that doesn’t involve
hardware components like CPU registers or any absolute memory
location.

• Generally, it optimizes code by eliminating redundancies, reducing the


number of lines in code, eliminating useless code or reducing the
frequency of repeated code.
• Machine independent code optimization can be achieved using the
following methods:
Function Preserving Optimization
• Function Preserving optimizations deals with the code in a given function in an
attempt of reducing the computational time. It can be achieved by following
methods:

• Common Subexpression elimination

• Constant Folding
//Applying constant propagation once
• Dead code elimination int c = 5 * 2;
int z = a;
• Copy Propagation

int a = 5 //Applying constant propagation second time


int c = 10;
int c = a * 2 int z = a;
Loop Optimization

• Frequency Reduction

• Algebraic expression simplification

• Strength Reduction

• Redundancy Elimination
Implementation of naive code Generation
of virtual machine

https://www.cs.wustl.edu/~cytron/cacweb/Chapters/11/lab.shtml
• we take a final step in program translation by traversing an AST and generating a form of code that is suitable for a virtual

machine

• The construction of an AST and its subsequent semantic processing have developed all of the information that is necessary

to translate a source program into some form of interpretable or executable code.

• The AST serves well for expressing the structure and the meaning of a source program. However, its design is purposefully

abstract, and thus independent of any particular architecture specification.

• Moreover, the AST nicely represents the nested structure of programs written in a modern programming language, while the

instructions executed by most architectures are more linear in nature.

• Parsing techniques are presented that check an input program's syntax based on a programming language's grammar.

• While the grammar provides an automatic structure for regulating the parser's activity, the translation of the source program

into a suitable AST requires actions that are inserted by hand.

• In this, code generation is essentially the inverse of the parsing process. A program's AST provides a structure that can be

traversed automatically.

You might also like