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:
- To increment a variable, most machines need three steps: fetch the variable, increment the value, store it back.
- Some architectures have a single instruction
incthat increments the variable in one instruction. - 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:
- Loop optimizations, which include:
- Code motion (also called frequency reduction)
- Loop unrolling
- Loop jamming
- Folding / constant propagation — explicitly not a loop optimization; it is a normal machine-independent optimization.
- Redundancy elimination
- Strength reduction
Machine-dependent optimization list:
- Register allocation — covered in the Computer Organization (CO) course, where graph coloring techniques are combined with register allocation to show how register allocation works.
- Use of addressing modes — addressing modes are very specific to the architecture; a particular addressing mode is not present on all computers / all platforms.
- Redundant load and store (peephole-style machine-level redundancy)
- Flow of control optimizations (not "options")
- Strength reduction (listed again in the machine-dependent table)
- Use of machine code
Drawing the diagram…
Comparison
| Aspect | Machine-Independent Optimization | Machine-Dependent Optimization |
|---|---|---|
| Dependence on hardware | None — works on every machine | Depends on machine features / instruction set |
| Examples | Loop optimization (code motion / frequency reduction, loop unrolling, loop jamming), folding / constant propagation, redundancy elimination, strength reduction | Register 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.