Common subexpression elimination



         


In computer programming, common subexpression elimination (CSE) is the practice of finding repeated redundant expression evaluations, and replacing them with a single computation assigned to a temporary variable. Although it can be and is done manually by human programmers, usage of the term usually refers to a compiler optimization.

Here is a simple example:

a = b * c + g; d = b * c * d; e = b * c;

which does four multiplies and one addition, can be replaced by

tmp = b * c; a = tmp + g; d = tmp * d; e = tmp;

which does only two multiplies and one addition.

Although in simple cases like this, most programmer will habitually create "tmp" while writing the code, the compiler optimization becomes more important in the case of C macros, where macro expansions may result in common subexpressions not apparent in the original source, or in code sequences generated by the compiler, such as for array indexing calculations, or after function inlining.

CSE is usually advantageous, but the compiler needs to be judicious about the number of subexpressions it saves in temporaries; a excessive number of temporary values creates register pressure possibly resulting in register allocation saving and loading temporaries to/from memory, which may take longer than simply recomputing an arithmetic result when it is needed.

Compiler writers distinguish two kinds of CSE:

[Top]

Reference





  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License