Every compiler faces a simple but surprisingly common situation: the program asks for the same value more than once. Consider
x = a * b + c;
y = a * b - d;
The product is computed in both lines. A naive compiler translates each line faithfully, running the multiplication twice. Common-subexpression elimination (CSE) notices that the two subexpressions are identical and the values of and have not changed between them, so it replaces the second computation with a reference to the result already sitting in a temporary register:
t = a * b;
x = t + c;
y = t - d;
One multiplication instead of two. The saving is trivial in isolation, but inside a tight loop that executes a million times, every eliminated operation matters.
CSE is one of the oldest and most studied compiler optimizations. Its roots trace back to John Cocke and the late 1960s, when the first optimizing compilers were being built. Today it appears in every serious compiler â GCC, LLVM/Clang, the JVM's JIT â applied automatically before the code ever reaches the CPU.
Comments
Loading comments...