Code optimization 2
Code optimization 2
OPTIMIZATION
Introduction
Optimization of a program is structured into following 2 phases:
1. Local Optimization
The optimizing transformations are applied over small segments
of a program consisting of a few statements.
or
Local optimization focuses on smaller sections
2. Global Optimization
The optimizing transformations are applied over a program
unit ,that is over a function or a procedure.
or
Global optimization considers the program as a whole
Local Optimization
Definition: Local optimization is confined to a basic block, meaning it
optimizes code within a straight-line sequence without branches (entry
and exit only). This localized approach focuses on quick,
straightforward improvements.
Primary Goal: To simplify calculations, reduce memory usage, and
eliminate unnecessary operations within a small section of code.
Key Techniques in Local Optimization:
1.Constant Folding
Explanation: Evaluates expressions with constant values during compile-
time rather than runtime.
Example:
int x = 5 * 3; // Replaced by int x = 15 during compilation.
Effect: Reduces runtime computation, making the code more efficient
2.Constant Propagation
Explanation: Replaces variables that hold constant values with those
values directly, making calculations faster.
Example:
int a = 10;
int b = a + 5; // Becomes int b = 15.
Effect: Reduces memory usage by eliminating the need for intermediate
variable storage
3.Dead Code Elimination
Explanation: Removes code that will never execute or does not affect
program results.
Example:
int x = 5;
x = 10; // Previous value of x = 5 is removed as it’s unused.
Effect: Results in cleaner, more efficient code with fewer unnecessary
instructions
4.Strength Reduction
Explanation: Replaces expensive operations (like multiplication) with
cheaper ones (like addition) where possible.
Example:
for (int i = 0; i < 10; i++)
{
int x = i * 2; // Replaced by x = i << 1 (bitwise shift).
}
Effect: Makes loop execution faster and reduces CPU usage
Global Optimization
Definition: Global optimization spans across multiple basic blocks and
optimizes the program as a whole. This phase aims to reduce
redundancies, improve code efficiency across the program, and identify
improvements beyond individual segments.
Primary Goal: To enhance performance by analyzing data and code
flows throughout the entire program, achieving optimizations not
possible within a single block
2.Reaching Definitions
Definition:Reaching definitions analysis identifies where each
variable takes on a value,allowing the compiler to reuse results
instead of calculations.
Example:
x=a+b;
y=a+b+c; //a+b is reused to avoid redundant calculation
//Optimized
temp=a+b;
x=temp;
y=temp+c;
3.Available Expressions
Definition: An expression is available at a point if it has already
been computed, and neither of its variables has changed.
Purpose: Allows the compiler to avoid redundant calculations by
reusing precomputed results instead of recalculating
expressions.
Example:
int result1=a+b;
int result2=a+b; //this can be replaced by result1
4.Constant Propagation
Explanation: This technique detects values that remain
constant through program execution. Once identified, these
constants can be substituted directly where they’re used,
removing unnecessary calculations.
Example:
int a=10;
int b=a+5; //becomes b=15
Local vs. Global Optimization with Examples
Thank
You