Explore the fundamentals of peephole optimization and loop optimization strategies in compiler design and code improvement. This quiz covers basic principles, common techniques, and practical examples to reinforce your understanding of optimizing code for efficiency.
Which of the following best describes the main goal of peephole optimization in code optimization?
Explanation: Peephole optimization focuses on scanning small sections of code and making simple local transformations to improve efficiency. Unlike full program redesign, it does not make global changes or alter program architecture. The option about direct conversion to machine code describes a compilation process, not an optimization. Increasing the number of instructions runs contrary to optimization goals.
If a register is loaded with the same value more than once without any modification in between, which peephole optimization removes this redundancy?
Explanation: Redundant Load Elimination specifically removes unnecessary repeated loading of the same value into a register, improving efficiency. Constant propagation deals with replacing variables with their constant values. Loop unrolling is a loop optimization, not a peephole one. Common subexpression elimination is similar but operates on expression repeats, not loads.
In the example 'x = y * 2' replaced by 'x = y + y', which optimization is being demonstrated?
Explanation: Strength reduction replaces expensive operations, like multiplication, with cheaper ones, such as addition, for efficiency gains. Dead code elimination removes code that never affects output. Register allocation chooses where variables live in registers or memory. Code motion moves computations to less frequently executed regions but doesn't affect operation types.
What is the main effect of applying dead code elimination in a program?
Explanation: Dead code elimination deletes instructions whose results are never used, reducing program size and improving efficiency. Reordering instructions relates to scheduling, not to removing dead code. Increasing redundancy lowers optimization. Adding comments is documentation, not optimization.
Given a loop where 'a = b + c' does not change across iterations, which optimization moves this calculation outside the loop?
Explanation: Loop Invariant Code Motion moves computations that yield the same result in each iteration, like 'a = b + c', before the loop to avoid repeated work. Loop fusion combines multiple loops into one. Constant folding computes constant expressions at compile time but doesn’t move code. Strength enhancement is not a standard compiler term.
Which statement accurately defines loop unrolling as an optimization technique?
Explanation: Loop unrolling duplicates the body of the loop multiple times, reducing the number of loop control operations needed, thus potentially improving performance. Dividing a loop describes loop fission, not unrolling. Removing all iterations would eliminate the loop, which is not the purpose. Function calls are unrelated to loop unrolling.
If 'z = x + y' appears multiple times in close succession without changes to 'x' or 'y', which optimization replaces repeated calculations with a single result?
Explanation: Common Subexpression Elimination detects repeated expressions, like 'x + y', and stores the result to avoid recalculating. Dead variable removal is not a classic compiler optimization. Loop unswitching moves loop-invariant conditions outside the loop. Branch prediction is a hardware execution optimization, not a code transformation.
Why are loop optimizations particularly important in code optimization?
Explanation: Loops are frequently the core performance bottleneck as they repeat computations many times, making optimization particularly beneficial. Readability is a separate concern from optimization goals. Although optimizations can reduce complexity, it is not always guaranteed. Not all loops deal with input/output; they can process data or perform calculations.
When 'a * 1' is replaced with 'a' or 'b + 0' with 'b', which type of peephole optimization is being used?
Explanation: Algebraic simplification removes unnecessary operations using basic algebraic identities, such as multiplying by one or adding zero. Constant propagation replaces variables with constant values, but does not remove identity operations directly. Code hoisting moves statements, not simplifies them. Loop fusion only operates on loops.
If a compiler rewrites 'x = 3 + 4' as 'x = 7' during compilation, which optimization technique is it using?
Explanation: Constant folding evaluates constant expressions at compile time, replacing them with their computed result to make code more efficient. Loop unrolling concerns reducing loop overhead. Register allocation decides where variables are stored. Code inlining replaces function calls with their bodies, but does not compute constants.