ant…@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> Thomas Koenig <tkoe…@netcologne.de> writes:
>
>> On an OoO machine, there can be considerable advantage of
>> doing this kind of thing. For example, compare (if you will
>> excuse my French)
>>
>> add_v1_:
>> addq %rsi, %rdi
>> addq %rdx, %rdi
>> leaq (%rdi,%rcx), %rax
>> ret
>> add_v2_:
>> addq %rsi, %rdi
>> addq %rcx, %rdx
>> leaq (%rdi,%rdx), %rax
>> ret
>
> On an in-order machine, it's even clearer. In add_v2_, the two
> adds can be done in the same cycle on an in-order machine. On
> an OoO machine, this is not always relevant, in particular, if
> rdx is only ready after rsi and rdi are ready.
>
>
> If the undefined behaviour nonsense would not be so sad, it
> would be funny. The undefined behaviour advocates love to
> claim that undefined behaviour allows more optimization than
> defined behaviour, but in Comment 1 Richard Biener writes that
> gcc does not perform this optimization because of undefined
> behaviour. [...]
I suspect you are misunderstanding the meaning of RB's comment.
The rules of C do not prohibit the optimization from being done.
gcc does not perform the transformation because of a choice gcc
has made about how code is compiled. If gcc had made different
choices then changing the order of integer adds would still be
conforming. The holdup here is not undefined behavior but how
gcc has chosen to exploit, or not exploit, some cases where some
circumstances of undefined should be treated.