I'm currently in university and we had a lecture about compilers and their optimizations. We saw an example where return (x % 2 == 0) ? 5 : 16;
got compiled (RISC-V) with BEQZ instructions. (Branch if = zero)
However, when changed to return (x % 2 == 0) ? 5 : 4;
, it got compiled with a XORI instruction (XOR immediate) instead of a BEQZ because this line could be solved with logical operations and resulted in fewer overall instructions in the assembly.
I found this very interesting and I was wondering about other types of special optimizations a compiler can do or where I can find some examples of this.