Optimisation: What is Global Value Numbering

Global Value Numbering is a technique that is used to detect when expressions are provably equivalent. When two expressions are shown to be the same, it is possible to assign the result of the first computation to the second so that instructions can be saved.

For example:
const int J = 10
const int K = 10
int A = J + 20
int B = K + 20

Becomes:
const int J = 10
const int K = J
int A = J + 20
int B = A

It is called Global Value Numbering because each unique expression is giving an ID, and any new expression found is compared with the table of ID’s to find if it matches any. If a match is found the new expression is also given the value of the existing ID. Once the code has been given it’s IDs, any repetition of an ID is replaced the value from the first instance of that ID.