1. Gyanastra
  2. Compiler Design
  3. Local and Global Code Optimization
  4. Introduction to Code Optimization and Its Goals
Free lesson, no account needed 4 min read 3 practice questions after it
Asked in past papers3 times
  • GATE CSE2009
  • UGC NET20082004

Compiler Design · Local and Global Code Optimization

Introduction to Code Optimization and Its Goals

Overview

Code optimization is the compiler phase that follows intermediate code generation, and its main intention is to improve the efficiency of the generated target code — reducing execution time and memory/space consumption — without changing the program's meaning.. Because most of a program's execution time is spent inside loops, optimization effort concentrates on the loop body, where a single saved line pays off once per iteration.

Key concepts

  • Code optimization: the compiler phase that comes after intermediate code generation; its goal is to produce more efficient target code (less execution time and/or less memory) while preserving program semantics..
  • Loops as the real target: statistical analysis shows that in any program the majority of execution time is spent inside loops.
    Almost every program (about 99%) contains loops, and for iterative programs most of the time is spent inside the loop body.
  • Non-iterative programs: even in recursive programs there is still some block where most of the time is spent, and in most programs that block is an iterative block (a loop).
  • Effect of reducing lines inside a loop: reducing the number of lines inside the loop reduces the overall running time of the program.
    Removing even one line inside a loop is equivalent to removing as many lines as the number of times the loop executes.
  • Where optimization effort goes: most optimization is focused on eliminating redundancies inside a loop or decreasing the number of lines inside the loop.

How it works

  1. The compiler phases run in order; after intermediate code generation comes the optimization phase.
  2. The goal of this phase is to produce efficient target code — minimizing execution time and memory usage — while preserving semantics.
  3. Since most execution time is spent inside loops (or inside the most-executed block of a recursive program), the loop body is the primary target.
  4. Reducing lines inside the loop reduces overall running time, because each line removed is saved once per iteration.
  5. Therefore optimization concentrates on removing redundancy inside loops and reducing the number of lines inside loops.
Figure. Optimization follows intermediate code generation and aims at efficient target code while preserving semantics. Effort concentrates on loops, where removing one line with a loop count of 1,000,000 removes 1,000,000 effective lines.

Formulas

  • effective lines removed=(lines removed inside the loop)×(loop execution count)\text{effective lines removed} = (\text{lines removed inside the loop}) \times (\text{loop execution count})
    • lines removed inside the loop: number of lines eliminated from the loop body
    • loop execution count: number of times the loop executes

Worked examples

Value of removing one line from a loop

  • Loop execution count = 1,000,000 (1 million).
  • Lines removed from inside the loop = 1.
  • Effective lines removed = 1×1,000,000=1,000,0001 \times 1{,}000{,}000 = 1{,}000{,}000 lines.
  • Conclusion: one line saved inside the loop is worth a million lines saved overall.

Revision summary

  • Code optimization is the phase after intermediate code generation.
  • Main aim: improve the efficiency of the target code (reduce execution time and space) without changing program semantics.
  • Most of a program's execution time is spent inside loops, or inside the most-executed block of a recursive program.
  • Removing one line inside a loop saves that line once per iteration: 1 line × 1 million iterations = 1 million lines saved.
  • Optimization effort concentrates on removing redundancy inside loops and reducing the number of lines inside loops.

Previous-year exam insights

  • Code optimization is performed by the compiler itself (it is part of the compiler, i.e. the system software), not by the application programmer or the operating system.
    Asked inUGC NET 2004
  • Dead-code elimination removes code whose computed values are never used (unreachable or useless definitions), not labels, functions, or modules.
    Asked inUGC NET 2008
  • Optimization must preserve program semantics: a transformation is only valid if the observable behavior of the program is unchanged.
  • Optimization is classified as machine-independent (e.g. common subexpression elimination, dead-code elimination, constant folding, strength reduction) versus machine-dependent (e.g. register allocation, instruction scheduling, peephole optimization).
  • Local optimization works within a single basic block; global optimization works across basic blocks of a whole procedure (using control-flow and data-flow analysis).
  • Optimization is optional and is typically applied to the intermediate representation before final code generation; the front end (lexical, syntax, semantic analysis) and intermediate code generation precede it.
Pages 1–2 of 2