1. Gyanastra
  2. Compiler Design
  3. Local and Global Code Optimization
  4. Machine-Independent vs Machine-Dependent Optimizations
Free lesson, no account needed 4 min read 3 practice questions after it
Asked in past papers5 times
  • GATE CSE2008
  • ISRO2011
  • UGC NET201620122007

Compiler Design · Local and Global Code Optimization

Machine-Independent vs Machine-Dependent Optimizations

Overview

Code optimizations are divided into two types: machine-independent optimizations, which can be applied on every machine regardless of its architecture, and machine-dependent optimizations, which are applied only when the target machine's features and instruction set support them.
The distinction matters because the largest gains come from optimizing inside loops, and because some transformations (such as replacing a three-step increment with a single inc instruction) are possible only on certain architectures.

Key concepts

  • Machine-Independent Optimizations: Optimizations that do not depend on the type of machine the compiler is working on; they can be applied directly on every machine.
    Loop optimizations are the standard example, since every program is going to have a loop, so loop optimization applies universally.
  • Machine-Dependent Optimizations: Optimizations applied depending on the features available for a particular machine.
    Some machines have better features and better instruction sets, so the optimization is applied only where the hardware supports it.
  • Scope of optimization: Decreasing one line outside the loop is not of huge help; the big gains come from optimizing inside loops. This motivates loop optimization.

How it works

Why machine dependence matters — the increment example:

  1. To increment a variable, most machines need three steps: fetch the variable, increment the value, store it back.
  2. Some architectures have a single instruction inc that increments the variable in one instruction.
  3. Therefore, depending on the capacities of the machine, some optimizations can be applied; other optimizations are applied for all machines (machine independent).

Machine-independent optimization list:

  1. Loop optimizations, which include:
    • Code motion (also called frequency reduction)
    • Loop unrolling
    • Loop jamming
  2. Folding / constant propagation — explicitly not a loop optimization; it is a normal machine-independent optimization.
  3. Redundancy elimination
  4. Strength reduction

Machine-dependent optimization list:

  1. Register allocation — covered in the Computer Organization (CO) course, where graph coloring techniques are combined with register allocation to show how register allocation works.
  2. Use of addressing modes — addressing modes are very specific to the architecture; a particular addressing mode is not present on all computers / all platforms.
  3. Redundant load and store (peephole-style machine-level redundancy)
  4. Flow of control optimizations (not "options")
  5. Strength reduction (listed again in the machine-dependent table)
  6. Use of machine code
Figure. Strength reduction is listed in both machine-independent and machine-dependent tables. Loop optimizations are machine independent, and folding or constant propagation is not a loop optimization.

Comparison

AspectMachine-Independent OptimizationMachine-Dependent Optimization
Dependence on hardwareNone — works on every machineDepends on machine features / instruction set
ExamplesLoop optimization (code motion / frequency reduction, loop unrolling, loop jamming), folding / constant propagation, redundancy elimination, strength reductionRegister allocation, use of addressing modes, redundant load and store, flow of control optimizations, strength reduction, use of machine code
Increment example—3 instructions (fetch, increment, store) vs single inc instruction on some architectures

Revision summary

  • Optimizations are of two types: machine independent and machine dependent.
  • Machine independent = apply on every machine without worrying about the machine type.
  • Machine dependent = applied based on the features / instruction set available on the machine.
  • Loop optimizations are machine independent because they do not depend on target-machine features or instruction sets.
  • Loop optimization types: code motion (frequency reduction), loop unrolling, loop jamming.
  • Folding / constant propagation is a normal machine-independent optimization, not a loop optimization.
  • Register allocation is machine dependent and is covered later in CO using graph coloring; addressing modes are architecture-specific, hence machine dependent.
  • The machine-dependent list also includes redundant load and store, flow of control optimizations, strength reduction, and use of machine code — copy this table of optimizations, as it is important and useful later.

Previous-year exam insights

  • Strength reduction replaces a costly operation by a cheaper equivalent one using mathematical identities (e.g., multiplication by a power of two by a shift, multiplication by a constant by shifts and adds); it is a machine-independent optimization.
    Asked inUGC NET 2016UGC NET 2012ISRO 2011UGC NET 2007
  • Constant folding evaluates constant expressions at compile time (e.g., 3+4 becomes 7); constant propagation substitutes known constant values into expressions; both are machine-independent and are not loop optimizations.
    Asked inUGC NET 2016
  • Code motion (frequency reduction) moves loop-invariant computations outside the loop; it is a machine-independent loop optimization and must not be confused with strength reduction or common-subexpression elimination.
    Asked inISRO 2011
  • Common subexpression elimination removes repeated computations of the same expression; it is a machine-independent redundancy elimination, distinct from strength reduction.
    Asked inISRO 2011
  • Machine-dependent optimizations exploit specific target features: register allocation, instruction selection, addressing-mode selection, peephole optimization of machine instructions, and use of special instructions; they cannot be applied portably across architectures.
  • Loop unrolling replicates the loop body to reduce loop-control overhead, and loop jamming (fusion) combines adjacent loops over the same range into one; both are machine-independent loop optimizations.
Pages 1–2 of 2