Article View: comp.arch
Article #1021Re: reassociation and undefined behaviour
From: David Brown
Date: Wed, 02 Dec 2026 15:20
Date: Wed, 02 Dec 2026 15:20
159 lines
7672 bytes
7672 bytes
On 02/12/2026 13:24, Anton Ertl wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 01/12/2026 20:01, Stefan Monnier wrote:
>>> Is it? AFAIK, in C
>>>
>>> (-2 + INTMAX) + 1
>>>
>>> is not the same as
>>>
>>> -2 + (INTMAX + 1)
>>>
>>> because the second two bumps into a UB while the first doesn't.
> ...
>> If the generated code uses some kind of "trap on overflow" addition
>> instruction, then the compiler may not change the first expression into
>> the second. But if the compiler is generating two's complement wrapping
>> instructions, it certainly can do so.
>
> Two other options are:
>
> * saturating arithmetics, used for signal processing. It's not
> associative.
>
Yes. That can be useful semantics for some code, and often makes more
sense than wrapping semantics (though it typically gives fewer
optimisation or re-arrangement opportunities - as you say, it is not
associative).
Clearly if a compiler has saturating arithmetic semantics for signed
integer overflow, perhaps as a compiler flag, then it cannot re-arrange
expressions like this. But the fact that a compiler may offer such a
flag does not limit the associativity of C signed integer arithmetic
outside of that - it is exactly like trap-on-overflow flags in that respect.
> * turning the result of an operation with undefined behaviour into an
> "undef" value or somesuch. Compilers like gcc and clang tend to do
> this. When it is time to generate code, they can (and do) generate
> anything for "undef". E.g., I have seen one of these compilers
> produce 0 as the result of (x<<n)|(x>>(32-n)) when it knew that
> n==0.
Yes, compilers can, and sometimes do, express known execution of UB into
an internal "undef" expression type. That's fine - if you put garbage
in, you can expect garbage out. The compiler can only do this when it
knows that the behaviour is definitely UB - not when it merely knows
that there /might/ be UB.
In general, "(x<<n) | (x>>(32-n))" cannot be replaced with "undef"
because it is not necessarily UB. But if the compiler knows that "n" is
outside the range 1 to 31 (for 32-bit x), then it can replace it with
"undef". And that "undef" can lead to any kind of result or behaviour.
(It can also replace it with "undef" if it knows that "x" is negative,
though gcc will not do that as it gives left shifts of negative values
fully specified behaviour rather than UB, so that the C behaviour
matches that of C++.)
And "undef" is associative and commutative in expressions, and does not
affect the associativity of signed integer addition.
>
>> And the compiler knows if and
>> when it is using wrapping instructions.
>
> There's the rub. When the reassociation check occurs, gcc could 1)
> change the type from an undefined-overflow addition to a
> wrapping-overflow addition and report that reassociation is possible.
Yes, it could.
> But instead, gcc 2) reports that the operation cannot be reassociated.
It can do that too.
> My guess is that at the point of this check it is not easy to find out
> which of these two alternatives eventually leads to better code, and a
> gcc maintainer chose to bet on 2). It would be interesting to know on
> what information this decision is based, but in any case, here we have
> an example where the more-defined -fwrapv can lead to better code than
> the less-defined default.
>
That sounds like a reasonable explanation. The best choice will depend
on surrounding code and how the results of the expression are used
later. Keeping the "UB on overflow" semantics and skipping
re-association can mean the compiler has more information that could
later be used in other optimisations. My gut feeling is that option 1
would be better more often than option 2, but my gut might not be well
calibrated!
I suspect (but have not thought it through in detail) that some kinds of
strength reduction or other expression simplification could result in
code where some operations in the internal re-arranged expression would
have new UB if evaluated with "UB on overflow" semantics. To avoid
that, the compiler either has to limit some manipulations (such as not
doing re-association here), or it would have to change the type to "wrap
on overflow" signed integers as you said. Key, of course, is that the
code is correct - efficiency is important, but secondary to correctness.
Maybe someone thought that it didn't really matter because this would
all be handled further down the line. I don't know the details of gcc
passes, but perhaps the thought was that the operations could be
re-ordered in a later scheduling pass, or even fixed up in a peephole
match in the backend.
There may be some cases where "-fwrapv" in connection with this kind of
re-association generates better code, but not with my testing here. The
example functions I used were :
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);
}
and the 32-bit ARM code generated was :
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
The most efficient code is generated from "add_v2_int" - signed
integers, with manual parentheses. Enabling "-fwrapv" results in the
same sub-optimal code for "add_v2_int" as for "add_v1_int".
Technical Details
Message-ID:
<11epd07$362ph$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Wed, 2 Dec 2026 16:20:38 +0100 Organization: A noiseless patient Spider Lines: 159 Message-ID: <11epd07$362ph$1@dont-email.me> References: <11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 02 Dec 2026 15:20:39 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3345201"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O6AvygGO3K5hd93CA2/t83cMDssu6iyo="; posting-host="8f689f606fabca0b227a4c433cd7c78e" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:DXk5SRwhpCMZ7Qv/14gHcbGBhx0= sha256:1mOX17rj8nFnfsCjviDqtpBaC/4phoRiVXe2QPycA4g= sha1:IEaTnzdRmr/t9k5oHpohD2+HD7Y= sha256:DlUCOENtIgaeFhyJJOupK0C8cVwP9X3FVUUZMshVqoI= In-Reply-To: <2026Dec2.132411@mips.complang.tuwien.ac.at> Content-Language: en-GB, nb-NO