Algorithm Search⌘K

Optimizations

Problems

  • Global value numbering

    Partition the values of a procedure into classes that surely hold equal values, so that a computation whose class is already computed can be removed.

    core
  • Partial redundancy elimination

    Insert and remove computations so that no expression is computed twice on any path, which subsumes CSE and loop-invariant code motion.

    core

Algorithms

  • Copy propagation

    Replaces uses of x after a copy x = y by y while neither changes, so that the copy can be removed as dead code.

    O(R b n c + c b n v) worst1 languagesintro
  • Local value numbering

    Gives each value in a basic block a number by hashing operator and operand numbers, so equal expressions get the same number.

    Θ(n) expected1 languagesintro
  • Aggressive dead code elimination

    Marks instructions with side effects as live, then marks what they depend on through data and control dependence, and deletes the rest.

    O(b^2 n + n^2) worst1 languagescore
  • Lazy code motion

    Solves PRE with unidirectional bit-vector analyses, placing each computation as late as possible to keep register pressure low.

    O(n^2 m) worst1 languagescore
  • Alpern-Wegman-Zadeck value numbering

    Finds congruent SSA values by refining a partition of the value graph until operands of congruent values are congruent, as in DFA minimization.

    O((n + e) log n) worst1 languagesadvanced
  • Ball-Larus path profiling

    Numbers the acyclic paths of a procedure so that adding values on some edges yields a unique path number, counting paths cheaply.

    O(V + E log E) worst1 languagesadvanced
  • Binding-time analysis

    Marks each expression static or dynamic by whether it depends only on known input, which guides offline partial evaluation.

    O(N T) worst1 languagesadvanced
  • Interprocedural constant propagation

    Finds parameters that are constant on every call by propagating jump functions over the call graph.

    O((v + 1)(n + b v) + (k + 1) n) worst1 languagesadvanced
  • Operator strength reduction on SSA

    Finds induction variables as strongly connected components of the SSA graph and rewrites their multiplications into additions.

    O(b^3 + (n + m)^2) worst1 languagesadvanced
  • Pettis-Hansen code positioning

    Orders basic blocks and procedures by greedily merging chains along the heaviest edges of the profile, to improve instruction cache use.

    O(E log E + C E) worst1 languagesadvanced
  • SSAPRE

    Performs PRE on SSA form one expression at a time by building a sparse SSA graph of the expression's occurrences.

    O((m + v)(n + d)) worst1 languagesadvanced
  • Stochastic superoptimization

    Searches the space of instruction sequences by Markov chain Monte Carlo with a cost of correctness on tests plus speed.

    O(s L (n + I) + (L + I) L I) worst1 languagesresearch

Techniques

  • Common subexpression elimination

    Computes an expression once and reuses the value where the same expression is computed again with the same operands.

    intro
  • Constant folding

    Evaluates at compile time the operations whose operands are all constants and replaces them by the result, using the arithmetic of the target machine.

    intro
  • Dead code elimination

    Removes instructions whose results are never used and that have no side effects.

    intro
  • Loop-invariant code motion

    Moves computations whose operands do not change inside a loop to a preheader before the loop, when this is safe.

    intro
  • Peephole optimization

    Scans a short window of adjacent instructions and replaces patterns by shorter or faster equivalent sequences, until no rule applies.

    intro
  • Devirtualization

    Replaces a virtual or indirect call by a direct call when analysis shows one target, often guarded by a type test.

    core
  • Function inlining

    Replaces a call by a copy of the body of the callee, which removes call overhead and exposes the body to optimization at the call site.

    core
  • Profile-guided optimization

    Runs the program on sample input to measure branch and call frequencies, then uses them to guide inlining, layout and allocation.

    core
  • Strength reduction

    Replaces costly operations by cheaper ones, such as a multiply by an induction variable in a loop by an add each iteration.

    core
  • Tail call optimization

    Compiles a call in tail position as a jump that reuses the caller's frame, so tail recursion runs in constant stack space.

    core
  • Deforestation

    Removes the intermediate trees and lists that one function builds and the next consumes, by fusing the two into one pass.

    advanced
  • Partial evaluation

    Specializes a program to the part of its input known in advance, giving a residual program that takes only the rest of the input.

    advanced
  • Scalar replacement of aggregates

    Splits a local record or array that does not escape into separate scalar variables, which can then live in registers.

    advanced
  • Superoptimization

    Searches all short instruction sequences for the cheapest one that is equivalent to a given sequence, checked by tests and proof.

    advanced
  • Equality saturation

    Applies all rewrite rules to an e-graph without choosing, until no rule adds anything, and then extracts the cheapest equivalent program.

    research
  • Supercompilation

    Drives a program symbolically on unknown input, folds repeated configurations into recursion and generalizes to stop, giving a residual program.

    research

Theorems

  • Full employment theorem for compiler writers

    No algorithm maps every program to a smallest equivalent program, because such an algorithm would decide the halting problem. So every optimizer can be improved.

    core
  • Futamura projections

    Specializing an interpreter to a program gives a compiled program, specializing the specializer to the interpreter gives a compiler.

    advanced