CSS Prepare

optional · CSS study notes

Code Optimization

Definition

Code optimization improves the intermediate (or target) code's time/space efficiency, without changing the program's observable output/meaning.

Key Points — Techniques

  • Elimination of Redundant Code: removing computations whose results are never used (dead code elimination) or repeated identical computations (common sub-expression elimination).
  • Constant Folding: evaluating constant expressions at compile time instead of runtime — e.g., replacing 2 + 3 with 5 directly in the code.
  • Loop Optimization: see dedicated page — biggest gains since loop bodies execute repeatedly.
  • Peephole Optimization: examining a small "peephole" (sliding window) of a few instructions at a time and replacing inefficient patterns with faster equivalents (e.g., removing redundant loads/stores, strength reduction: replacing x * 2 with x << 1).
  • Problems of Optimization: over-aggressive optimization can change program behavior in the presence of side effects, aliasing, or exceptions; optimizations must be safe (preserve semantics) and are limited by analysis cost (compile-time budget) and undecidability of some optimal transformations.

Example

  • Constant folding: int x = 4 * 5; → compiler directly stores x = 20
  • Peephole: MOV R1, x followed immediately by MOV x, R1 → second instruction is redundant, removed

📌 CSS Frequency: High (2017 repeated) — often asked as "differentiate the optimization techniques with one example each."

Model Answer (short)

"Code optimization improves efficiency without altering program meaning, using techniques such as dead-code and common sub-expression elimination, constant folding (evaluating constant expressions at compile time), loop optimization, and peephole optimization (local instruction-window improvements like strength reduction). A key challenge is that optimizations must remain semantically safe — aggressive transformations can break correctness in the presence of aliasing or side effects, and finding a truly optimal program is generally undecidable, so compilers use practical heuristics instead."

📜 Past-Paper Questions & MCQs (CSS 2016–2026)

2019 · Paper-I · Q8(c) · 5 marks

Q. Differentiate machine-dependent optimization and machine-independent optimization.

Ans.

BasisMachine-independentMachine-dependent
Works onIntermediate codeTarget (assembly or machine) code
Depends on hardwareNoYes (registers, instruction set, cache)
TechniquesConstant folding, dead-code elimination, common sub-expression elimination, loop-invariant code motionRegister allocation, instruction scheduling, peephole optimisation, using special instructions
ReusableYes, for any targetNo, must be redone per architecture

2023 · Paper-I · Q8(b) · 6 marks

Q. Describe constant folding using brief pseudocode.

Ans.

  • Constant folding evaluates expressions whose operands are all constants at compile time, so the work is not repeated at run time. x = 2 * 3.14 * 10 becomes x = 62.8.
foldConstants(node):
    if node is a binary operation:
        foldConstants(node.left); foldConstants(node.right)
        if node.left is a constant and node.right is a constant:
            replace node with constant( node.left  node.operator  node.right )
  • Benefit: fewer instructions, and it exposes further optimisations such as dead-code removal. It is applied together with constant propagation.

2017 · Paper-I · Q7(c) · peephole part

Q. Explain peephole optimization with an example.

Ans.

  • Peephole optimisation looks at a small window (a few instructions) of target code and replaces it with a faster or shorter sequence.
  • Examples:
    • redundant load/store: MOV R1, a; MOV a, R1 → the second instruction is removed
    • strength reduction: MUL R1, 2 → SHL R1, 1
    • algebraic simplification: ADD R1, 0 → removed
    • unreachable code after an unconditional jump is deleted