0% found this document useful (0 votes)
3 views

Code optimization 2

Uploaded by

Mariya Shibi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code optimization 2

Uploaded by

Mariya Shibi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PHASES OF CODE

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

To apply global optimization, the compiler needs an internal structure


to represent the entire program and analyze it effectively. This is
usually done with Intermediate Representations (IRs) that make code
easier to analyze and transform. Here are some common program
representations:
1.Control Flow Graph (CFG)
Definition: A control flow graph is a graphical representation where
each node represents a basic block (a straight-line sequence of code),
and edges represent the flow of control between these blocks.
Purpose: The CFG helps the compiler understand the order in which
code segments (basic blocks) execute, allowing it to track how different
parts of the program connect and interact.
Example: If an if-else statement exists, the CFG will have branches for
each condition, showing possible paths the program might take.

2.Static Single Assignment (SSA) Form


Definition: SSA is a way to represent variables in a program so that each
variable is assigned only once. Any reassignments create a new version
of the variable.
Purpose: SSA form simplifies tracking data flow and enables
optimizations like constant propagation and dead code elimination
across the entire program.
Example: Instead of reusing a variable, SSA will rename it on each
assignment:
a = 10; // becomes a1 = 10
a = a + 5; // becomes a2 = a1 + 5

Control Flow Analysis


Explanation
Control Flow Analysis examines how control moves through the program,
allowing the compiler to identify loops, conditions, and paths that can be
optimized. Here are some key concepts in control flow analysis:
1.Loop Detection and Optimization
Purpose: Identify loops in the CFG to apply loop optimizations, such as
loop unrolling or moving invariant code outside loops.
Example: The compiler detects a loop that calculates the same value
each time it executes. Moving this invariant calculation outside the
loop can save computational resources by calculating it only once.
for(int i=0;i<n;i++)
{ int x=5*5;\\this can be moved outside the loop
}

2.Dominators and Natural Loops


The compiler also identifies Dominators-nodes in the CFG that must
be passed to reach a specific block.Using Dominator analysis the
compiler finds natural loops or loops with single entry point which
are ideal for optimizations like loop unrolling.
Example:
//original loop;
for(int i=0;i<4;i++)
{ sum+=arr[i];
}
//unrolled loop;
sum+=arr[0] +arr[1] +arr[2] +arr[3];
Data-Flow Analysis
Explanation:
Data-flow analysis examines how data moves and changes throughout
the program. It provides essential information for global optimizations,
helping compilers remove redundant operations and ensure correct
variable values across different code blocks. Key data-flow analysis
techniques include:
1. Live Variable Analysis
Definition: Determines if a variable holds a value that might be used
later in the program.
Purpose: Helps identify dead code by pinpointing variables that no
longer hold meaningful values.
Example: If a variable is assigned but not used again, live variable
analysis marks it as ‘dead,’ and the assignment can be removed.
int x=5;
x=10;//initial assignment to 5 is unnecessary and can be removed;

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

You might also like