Optimisation: What is Algebraic Simplification?

When a software is writing some mathematical operations, they do not always write the most succinct version of that mathematics. For example, when writing array offsets:

arr[i + 0]
arr[i + 1]
arr[i + 2]

The programmer may leave the “ + 0” in the first index as a way to maintain visual coherence in the code and aid readability about what is happening.

Of course we dont actually want the compiled program to perform this addition, so we used Algebraic Simplification to detect pattern which do not have any effect, or can be expressed in a simpler form to reduce the required number of instructions without changing the behaviour.

Examples:
int i = 1 + K + 1 -> int i = 2 + K
int i = K - K -> int i = 0
int i = K * 0 -> int i = 0

Importantly some of these optimisations can only be performed on integers and not floating point numbers. As floating point numbers are inherently inaccurate, the order and number of operations can change the final result. To enable this type of optimisation on floating point numbers you must enable the fastmath flag in most languages.