https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128737

David Brown <david at westcontrol dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david at westcontrol dot com

--- Comment #4 from David Brown <david at westcontrol dot com> ---
I am seeing a similar difference in the generated code for 32-bit thunb2 ARM. 
This is my C code for the testing :

int add_v1_int(int a, int b, int c, int d) {
        return a + b + c + d;
}

int add_v2_int(int a, int b, int c, int d) {
        return (a + b) + (c + d);
}

typedef unsigned int uint;
uint add_v1_uint(uint a, uint b, uint c, uint d) {
        return a + b + c + d;
}

uint add_v2_uint(uint a, uint b, uint c, uint d) {
        return (a + b) + (c + d);
}

<https://godbolt.org/z/Eeo4o85vv>


add_v1_int:
        add     r0, r0, r1
        add     r0, r0, r2
        add     r0, r0, r3
        bx      lr
add_v2_int:
        add     r0, r0, r1
        add     r2, r2, r3
        add     r0, r0, r2
        bx      lr
add_v1_uint:
        add     r2, r2, r3
        add     r2, r2, r1
        add     r0, r0, r2
        bx      lr
add_v2_uint:
        add     r2, r2, r3
        add     r2, r2, r1
        add     r0, r0, r2
        bx      lr


I am compiling with trunk 32-bit ARM gcc on godbolt, but the results are the
same for different versions going back to at least gcc 9. The results are the
same with different "-mcpu" flags.  I used "cortex-m7" because it is
dual-issue, and can thus execute "add_v2_int" with one less cycle latency. 
-O1, -O2 and -O3 give the same code.

The semantics for both "int" and "unsigned int" are effectively the same here. 
Unsigned addition is always associative and commutative.  As long as there are
no flags like "-ftrapv" or sanitizers in use, signed integer addition is also
fully associative and commutative in contexts like this.  Since signed integer
overflow is UB, it is always correct to treat it as wrapping such as you would
get with unsigned types (converting back to signed types before any operation
dependent on the sign).

>From the code generated here, it seems that gcc is treating the signed integer
code as though signed integer addition is not associative.  Then the unsigned
integer code is treated as associative (effectively removing the parentheses),
but generates code based on a strict right-associative addition.  Without
parentheses, the signed version uses a strict left-associative ordering.  To
get a flatter and more efficient result, the source code has to use signed
types and manual parentheses.


So it seems to me that correcting the associativity flags - so that the signed
operations are seen as fully associative for maximum flexibility for the
optimiser - will currently result in slower code.  First you would have to look
at why the unsigned type operations are not evaluated in an order that reduces
the dependency chains, and then fix the signed code to match.

Reply via email to