Value NumberingTopOptimization techniques

Optimization techniques

  1. Although "optimization" is the popular term for our next topic, it is traditional to start by admitting that it is an inappropriate term.
  2. Common forms of code improvement include:
    Constant Folding
    Recognizing expressions whose values are compile-time computable (even when program variables are involved).
    Common Sub-expression Elimination
    Avoiding the re-evaluation of expressions whose values have not changed. Can be done locally and globally.
    Code motion
    Moving loop-invariant computations to the header of a loop.
    Reduction in Operator Strength
    Replacing expensive operations (typically multiplications and divisions) with cheaper ones. Locally, this refers to using shifts instead of multiplies. Globally, it involves recognizing induction variables in loops.
    • For example, in the loop:

          for i := 1 to 1000 do
            begin
                . . .
                a[2*i] := ...
      
            end
      	   

      the multiplication "2*i" can be avoided by keeping a counter that is incremented by 2 each time around the loop.
    Copy Propagation
    If an assignment of the form x := y is found, replacing instances of x with y after the assignment make make it possible to eventually eliminate the assignment.
    Dead Code Elimination
    Optimizations like copy propagation may result in useless instructions (the assignments) that can be eliminated.
    Procedure inlining
    Replacing calls to procedures with copies of the procedure body itself.
    Register Allocation
    Try to avoid loads and stores of values to and from memory by keeping them in registers.
    Instruction Scheduling
    Ordering the instructions in the generated code to deal with hardware timing issues (memory access delays, branch delays, pipeline features).

  3. Optimizations can be classified according to the extend of code considered when they are applied.

    Peephole Optimization
    Looks at just a short segment of output machine code.

    Local Optimization
    Looks at just one statement of high-level code.

    Straight line code (or basic block) optimization
    Looks at sequences of instructions involving no branches (in or out).

    Global Optimization
    Looks at an entire procedure (i.e. not really global).

    Interprocedural Optimization
    Really global.

Computer Science 434
Department of Computer Science
Williams College

Value NumberingTopOptimization techniques