The standard doesn't specify the order of evaluation of arguments with this line:
The order of evaluation of arguments is unspecified.
What does
Better code can be generated in the absence of restrictions on expression evaluation order
imply?
What is the drawback in asking all the compilers to evaluate the function arguments Left to Right for example? What kinds of optimizations do compilers perform because of this unspecified spec?
Allowing the compiler to re-order the evaluation of the operands adds more room for optimization.
Here's a completely made up example for illustration purposes.
Suppose the processor can:
Now suppose you have a function call as follows:
foo(a += 1, b += 2, c += 3, d *= 10);
If you were to execute this left-to-right on a processor without OOE:
Cycle - Operation
0 - a += 1
1 - b += 2
2 - c += 3
3 - d *= 10
4 - d *= 10
5 - d *= 10
Now if you allow the compiler to re-order them: (and start the multiplication first)
Cycle - Operation
0 - d *= 10
1 - a += 1, d *= 10
2 - b += 2, d *= 10
3 - c += 3
So 6 cycles vs. 4 cycles.
Again this is completely contrived. Modern processors are much more complicated than that. But you get the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With