1. Describe Constant Propagation with an example.
Definition: If the value of a variable is known at compile-time, replace its occurrences with the
constant value.
Example:
n = 10;
c = 2;
for (i=0; i s = s + i*c;
After optimization:
for (i=0; i<10; i++)
s = s + i*2;
2. Explain Dead Code Elimination with an example.
Definition: Dead code is code that is either never executed or produces results that are never used.
Example:
x = y + 1;
y = 1; // Dead code, value of y unused
Optimized:
x = y + 1;
3. What is Common Subexpression Elimination? Give an example.
Definition: If the same expression is computed multiple times, compute it once and reuse the result.
Example:
a = b + c;
d = b + c;
Optimized:
t = b + c;
a = t;
d = t;
4. Define Algebraic Simplification with example.
Definition: Simplify arithmetic expressions using algebraic rules.
Example:
x = y * 1; // Simplifies to x = y
z = w + 0; // Simplifies to z = w
5. What is Constant Folding? Show with an example.
Definition: If operands are known at compile-time, evaluate expression during compilation.
Example:
r = 3.14 * 10;
Optimized: r = 31.4;
6. Explain Loop-Invariant Code Motion with example.
Definition: If a computation inside a loop produces the same result in every iteration, move it
outside.
Example:
for (i=0; i A[i] = A[i] + x*x;
Optimized:
t = x*x;
for (i=0; i A[i] = A[i] + t;
7. Describe Unreachable Code Elimination with example.
Definition: Remove code that can never be executed.
Example:
if (false) {
printf("This will never run");
}
8. Explain Inlining with an example.
Definition: Replace a function call with the actual function body.
Example:
int add(int a, int b) { return a + b; }
c = add(2,3);
Optimized:
c = 2 + 3;
9. What is Control-Flow Simplification? Example.
Definition: If the result of a branch condition is known at compile-time, remove the unused branch.
Example:
if (10 > 5)
x = 1;
else
x = 2;
Optimized:
x = 1;
10. Describe Loop Unrolling with example.
Definition: Expand the loop body multiple times to reduce loop overhead.
Example:
for (i=0; i<4; i++) A[i] = 0;
Optimized:
A[0]=0; A[1]=0; A[2]=0; A[3]=0;
11. Define Strength Reduction with example.
Definition: Replace costly operations with cheaper ones.
Example:
for (i=0; i v = 4*i;
A[v] = 0;
}
Optimized:
v = 0;
for (i=0; i A[v] = 0;
v = v + 4;
}
12. Difference between Traditional and Enabling Transformations.
Traditional Optimizations: Directly reduce redundant work. Example: Constant folding.
Enabling Transformations: Allow further optimizations. Example: Inlining.
13. State the requirements of optimization techniques.
1. Safety – Must not change program meaning.
2. Profitability – Should improve performance/size.
3. Risk – Must not conflict with other optimizations.
14. Differentiate Local, Intraprocedural, and Interprocedural Optimization.
Local: Within a block.
Intraprocedural: Within a function.
Interprocedural: Across entire program.
15. Limitations of Compiler Optimizations.
1. Cannot fix bad algorithms.
2. Cannot fix poor data structures.
3. May increase code size.
4. Order affects results.