Misfeature of Cmm optimiser: no way to extract a branch of expression into a separate statement
AFAIK, optimisations in Cmm are performed using cmmMachOpFold. However, this function is pure and does not allow detaching a branch of expression in order to make sure that it is executed only once.
Let's say we have (Op1 arg1 arg2) and we want to transform it to (Op2 arg1 (Op3 arg2 arg1)). Doing this would mean that arg1 would be computed more than once. Instead, the following should be possible:
arg1_reg <- arg1 (Op2 arg1_reg (Op3 arg2 arg1_reg))
The lack of this feature already stops one of optimisations from happening in most cases. See:
CmmReg _ <- x -> -- We duplicate x below, hence require -- it is a reg. FIXME: remove this restriction.
There is a number of other useful optimisations which can be implemented only with this feature available.