Optimisation: What is Common Sub-expression Elimination

Common sub-expression Elimination (CSE) is used to identify when duplicate work is being done. The duplicated work is extracted and stored for use by all the instances instead of being repeatedly computed.

Example:
int r = arr[(y * width) + x]
int g = arr[(y * width) + x + 1]
int b = arr[(y * width) + x + 2]

Becomes:
int v = (y * width) + x
int r = arr[v]
int g = arr[v+1]
int b = arr[v+2]

This optimisation can be limited to certain data types (like the floating-point limitation of other optimisations) and its application and scope varies from one compiler to another.