This patch fixes an issue with -Oz and -Os compilation where it is profitable to reuse constants that are already in registers rather than canonicalizing an operation that changes the constant used, thereby having to materialize the constant again.
int test(int* x)
if (x > (int)655360000L) return *x - (int)655360000L; return x;
In the above code, although the constant used in the compare is reused in the subtraction, LLVM canonicalizes the subtraction to an addition with a negative constant. That means that the constant that was materialized in a register for the compare cannot be reused for the subsequent operation and another constant (which is the negative of the first one) is materialized.
We solve this by tracking the constants that are used in a function and wherever we see a new constant, we check is it is the negative of an earlier used constant or just one off from an earlier used constant. If so, then we reverse the canonicalization for that instruction so as to use the same constant as was used before.