Code Optimization Techniques
Code Optimization Techniques
Code Optimization Techniques
techniques
CODE OPTIMISATION
do
{
item = 10;
amount= amount + item;
} while(amount<100);
item = 10;
do
{
amount= amount + item;
} while(amount<100
Machine-Independent Optimization Techniques
1. Compile Time Evaluation
2. Common Subexpression Elimination
3. Variable Propagation
4. Dead Code Elimination
5. Code Movement
6. Strength Reduction
7. Redundant load and store elimination
8. Null sequence deletion
9. Loop unrolling
Compile Time Evaluation
Two techniques that falls under compile time evaluation are-
A) Constant Folding
● Constant folding is an optimization technique in programming languages
where the compiler evaluates constant expressions at compile time,
rather than at runtime.
● This optimization helps to reduce the amount of work required during
execution by precomputing values that are constant.
Constant folding example
int main() {
int a = 5;
int b = 10; Code after Constant Folding:
int c = a + b;
int d = c * 2; int main() {
return d; int c = 15; // 5 + 10
}
int d = 30; // 15 * 2
return 30;
In this example: }
● The expressions a + b and c * 2
only involve constant values.
● Instead of calculating a + b and c *
2 at runtime, the compiler will
evaluate them at compile time.
Compile Time Evaluation
B) Constant Propagation-
● If some variable has been assigned some constant value, then it replaces that variable with its
constant value in the further program during compilation.
● The condition is that the value of variable must not get alter in between.
pi = 3.14
● This technique substitutes the value of variables
radius = 10 ‘pi’ and ‘radius’ at compile time.
● It then evaluates the expression 3.14 x 10 x 10.
Area of circle = pi x radius x radius ● The expression is then replaced with its result 314.
● This saves the time at run time.
Common Subexpression Elimination
AFTER:
BEFORE:
int main() {
int main() { int a = 5;
int a = 5; int b = 10;
int b = 10; int temp = a + b; // common sub-
int c = (a + b) * 2; expression
int d = (a + b) * 3; int c = temp * 2;
return c + d; int d = temp * 3;
} return c + d;
}
Code Movement
● Code movement, also known as loop-invariant code motion or hoisting, is an optimization
technique used by compilers to improve efficiency by relocating pieces of code that do not
need to be executed multiple times.
● Specifically, this technique identifies computations within a loop that yield the same result on
each iteration and moves them outside the loop, reducing redundant calculations and
saving runtime.
x=y+z; {
a[j] = 6 x j; a[j] = 6 x j;
} }
Dead Code Elimination
● Dead Code Elimination (DCE) is an optimization technique where the compiler removes code that
does not affect the program's outcome.
● This includes code that is never executed (unreachable code) and code that computes values
which are never used (useless code).
● By eliminating such dead code, the compiler reduces the size of the program and can also improve
its execution speed.
Before Optimization
After Optimization
int main() {
int main() {
int a = 5;
int a = 5;
int b = 10;
int b = 10;
int c = a + b;
int c = a + b;
int d = 20; // Dead code: 'd' is not
return c;
used
}
return c;
}
Redundant load and store elimination
● If a value that has been loaded from memory is not modified afterward and is loaded
again, the second load is redundant.
● The compiler can replace the second load with a simple register reference to the
previously loaded value.
● Similarly If a variable is stored to memory but that stored value is never read (or is
overwritten before being read), the store operation is redundant.
● The compiler can safely eliminate such stores.
int x = arr[1]; // Load from arr[1] int x = arr[1]; // Load from arr[1]
x = x + 10; // Modify x x = x + 10; // Modify x
arr[1] = x; // Store back to arr[1] int y = x; // Reuse x directly
int y = arr[1]; // Load again from instead of reloading arr[1]
arr[1]
cont'd
a= b+c
d= a+e
Before Optimization
After Optimization
MOV b, R0;
ADD c, R0; MOV b, R0 ;
MOV R0, a; ADD c, R0 ;
MOV a, R0; MOV R0, a ;
ADD e, R0 ; ADD e, R0 ;
MOV R0, d; MOV R0, d;
Null sequences deletion