🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1022

Re: reassociation and undefined behaviour

#1022
From: Anton Ertl
Date: Wed, 02 Dec 2026 16:25
93 lines
4493 bytes
David Brown <david.brown@hesbynett.no> writes:
>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.
...
>> * 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.
...
>And "undef" is associative and commutative in expressions, and does not 
>affect the associativity of signed integer addition.

Addition that overflows into "undef" is not associative.  Consider the
example above with this kind of addition:

(-2 + INTMAX) + 1 = "INTMAX-2" + 1 = INTMAX-1
-2 + (INTMAX + 1) = -2 + "undef" = undef

>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".

Yes, when you optimize manually, and have the compiler transformation
disabled, you get the optimal result (for our definition of
optimility, which is to minimize the maximum tree-height).

(The normally relevant optimization criterion is execution time,
space, or both, but let's ignore that for the moment).

The question is why gcc associates addition the way it does when it
has the choice.  My guess is that it minimizes the simultaneously-live
values.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Dec2.172552@mips.complang.tuwien.ac.at>
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
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> <11epd07$362ph$1@dont-email.me>

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: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: reassociation and undefined behaviour
Date: Wed, 02 Dec 2026 16:25:52 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 93
Message-ID: <2026Dec2.172552@mips.complang.tuwien.ac.at>
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> <11epd07$362ph$1@dont-email.me>
Injection-Date: Wed, 02 Dec 2026 16:49:28 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3442719"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19PIekMD4hKGQjN5CBEqWrT"; posting-host="9e58e1045521d54b1b4af042e3ed402f"
Cancel-Lock: sha1:y3gfZgNsvaDne+VxLRggVJnq1yo= sha256:40EgqBrd5YNhP09BdFg38KN5rSyy92zW9CodjFdOf9E=
	sha1:KdOAFGk8/mbyh5DQmaVKacLE/2M= sha256:r7QsJHAHyJ6J1TYvNWFQxVREb2TWkwji+l8blCBV61Y=
X-newsreader: xrn 10.11