ArticleslgStudy

science

Common subexpression elimination

Common subexpression elimination is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Common subexpression elimination rather than just read about it. In short: In compiler theory, common subexpression elimination (CSE) is a compiler optimization that searches for instances of identical expressions (i.e., they all evaluate to the same value), and analyzes whether it is worthwhile replacing them with a single variable holding the computed value. Example In the following code: a = b * c + g; d = b * c * e; it may be worth transforming the code to: tmp = b * c; a = tmp + g; d…

Key takeaways

  • Common subexpression elimination belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Common subexpression elimination to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Common subexpression elimination from memory before moving on to harder problems.

Reference excerpt

In compiler theory, common subexpression elimination (CSE) is a compiler optimization that searches for instances of identical expressions (i.e., they all evaluate to the same value), and analyzes whether it is worthwhile replacing them with a single variable holding the computed value.

Example In the following code:

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

it may be worth transforming the code to:

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

if the cost of storing and retrieving tmp is less than the cost of calculating b * c an extra time.

Principle The possibility to perform CSE is based on available expression analysis (a data flow analysis). An expression b*c is available at a point p in a program if:

every path from the initial node to p evaluates b*c before reaching p, and there are no assignments to b or c after the evaluation but before p. The cost/benefit analysis performed by an optimizer will calculate whether the cost of the store to tmp is less than the cost of the multiplication; in practice other factors such as which values are held in which registers are also significant. Compiler writers distinguish two kinds of CSE:

local common subexpression elimination works within a single basic block global common subexpression elimination works on an entire procedure, Both kinds rely on data flow analysis of which expressions are available at which points in a program.

Benefits

The benefits of performing CSE are great enough that it is a commonly used optimization. In simple cases like in the example above, programmers may manually eliminate the duplicate expressions while writing the code. The greatest source of CSEs are intermediate code sequences generated by the compiler, such as for array indexing calculations, where it is not possible for the developer to manually intervene. In some cases language features may create many duplicate expressions. For instance, C macros, where macro expansions may result in common subexpressions not apparent in the original source code. Compilers need to be judicious about the number of temporaries created to hold values. An excessive number of temporary values creates register pressure possibly resulting in spilling registers to memory, which may take longer than simply recomputing an arithmetic result when it is needed.

See also Global value numbering Loop-invariant code motion

References

Steven S. Muchnick, Advanced Compiler Design and Implementation (Morgan Kaufmann, 1997) pp. 378–396 John Cocke. "Global Common Subexpression Elimination." Proceedings of a Symposium on Compiler Construction, ACM SIGPLAN Notices 5(7), July 1970, pages 850–856. Briggs, Preston, Cooper, Keith D., and Simpson, L. Taylor. "Value Numbering." Software-Practice and Experience, 27(6), June 1997, pages 701-724.

Worked examples

Example 1 — a first encounter with Common subexpression elimination

Start with the simplest possible case. Write down what Common subexpression elimination claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Common subexpression elimination before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Common subexpression elimination ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Common subexpression elimination

In research
Common subexpression elimination appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Common subexpression elimination in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Common subexpression elimination is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler optimizations, so understanding it makes those chapters shorter.
In everyday life
Look for Common subexpression elimination outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Common subexpression elimination” →

How to study Common subexpression elimination in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Common subexpression elimination means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Common subexpression elimination out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Common subexpression elimination in simple terms?

In compiler theory, common subexpression elimination (CSE) is a compiler optimization that searches for instances of identical expressions (i.e., they all evaluate to the same value), and analyzes whether it is worthwhile replacing them with a single variable holding the computed value. Example In…

Why does Common subexpression elimination matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Common subexpression elimination?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Common subexpression elimination.

Tags

  • Compiler optimizations

Keep exploring