I found several questions discussing compiler optimizations for signed vs unsigned. The general conclusion is that signed allows for more optimizations due to the undefined behavior of overflow. However, I didn't find any discussion on how signedness plays with operation reordering.
Consider the expression
a - b + c
If all values are unsigned, the compiler can always reorder the operations (adding a
and c
first may improve performance).
If all values are signed, the compiler must prove that reordering will not cause an overflow to occur. In general a + c
could overflow so compiler is constrained in what it can reorder.
Am I correct that the compiler has more freedom to reorder operations for unsigned values?
EDIT
I was going to ask about other operations like *
and /
but I guess this question only makes sense for +
and -
. In general no reordering is possible for muliply and divide no matter whether the integers are signed or unsigned.