David Brown <davi…@hesbynett.no> wrote:
> On 21/11/2026 15:54, Thomas Koenig wrote:
>> David Brown <davi…@hesbynett.no> schrieb:
>>> On 21/11/2026 15:01, Thomas Koenig wrote:
>>>> David Brown <davi…@hesbynett.no> schrieb:
>>>>> On 20/11/2026 19:57, Thomas Koenig wrote:
>>>>>> Anton Ertl <ant…@mips.complang.tuwien.ac.at> schrieb:
>>>>>>> Stephen Fuld <sfu…@alumni.cmu.edu.invalid> writes:
>>>>>>>> On 11/19/2026 10:00 AM, Thomas Koenig wrote:
>>>>>>>>> Stephen Fuld <sfu…@alumni.cmu.edu.invalid> schrieb:
>>>>>>>>>> On 11/11/2026 10:53 PM, Thomas Koenig wrote:
>>>>>>>>>>> Stephen Fuld <sfu…@alumni.cmu.edu.invalid> schrieb: >>>>>>>>>>> s = 0; >>>>>>>>>>> for (long int i=0; i<n; i++) >>>>>>>>>>> s = s + a[i];
>> >> [...] >>
>>> Without additional effort from the compiler user, a loop like this can >>> be re-arranged using commutative and associative laws and any other >>> information the compiler has. I would not expect the compiler to handle >>> this differently for signed or unsigned types.
>> >> gcc treats wrapping and non-wrapping differently. Here's a function >> from gcc/tree-ssa-reassoc.cc: >> >> /* Returns true if we can reassociate operations of TYPE. >> That is for integral or non-saturating fixed-point types, and for >> floating point type when associative-math is enabled. */ >> >> static bool >> can_reassociate_type_p (tree type) >> { >> if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) >> || NON_SAT_FIXED_POINT_TYPE_P (type) >> || (flag_associative_math && FLOAT_TYPE_P (type))) >> return true; >> return false; >> } >> >> Reassociation is possible with -frwapv, not possible with >> normal semantics. >>
> > That surprises me, but okay. There is certainly no reason why signed > integer types cannot be treated as wrapping if it results in better code > (assuming, of course, you don't have "-ftrapv" or relevant sanitizer > flags in effect).
No pass within GCC (and probably any other compiler) may introduce undefined behaviour where there was none in the program. That is because a later pass might detect the introduced UB and miscompile it, or even drop it onto the floor altogether. Andrew.