CSE negated operations
Right now things like a×b and a×-b are not detected as common expressions by CSE. Given that negation is generally free on GPUs, replacing users of the latter expression with the negation of the former will almost always (ignoring the possibility of increased register pressure) be an improvement.
Several cases where I have observed this in real-world shaders:
- multiplication: -a×b = -(a×b)
- multiplication: -a×b = a×-b
🤦 - addition: -a+-b = -(a+b)
- sine: sin(-x) = -sin(x)
Other operations, like reciprocal and cosine, are possible candidates, but I do not recall having seen occurrences in the wild.
A few years ago @mattst88 enhanced CSE in the Intel fs backend to do some of this (see commit 47c4b385), and I had a branch that did some of this in the Intel vec4 backend. Doing it directly in NIR should be even better.