Article View: comp.arch
Article #814reassociation and undefined behaviour (was: vector length CSR?)
From: Anton Ertl
Date: Thu, 19 Nov 2026 18:38
Date: Thu, 19 Nov 2026 18:38
51 lines
3608 bytes
3608 bytes
Thomas Koenig <tkoenig@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. >(from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128737 ). 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. If the compiler rightly does not perform this optimization (e.g., because in other cases this would lead to miscompilation of the kind that even undefined behaviour fans consider to be miscompilation), this demonstrates that undefined behaviour not only prevents human optimizations, but also compiler optimizations. So undefined behaviour can be bad for optimization all around. But even if the gcc maintainers are mistaken, and the optimization can be safely applied, this case demonstrates that undefined behaviour has negative effects on compiler optimization capabilities. Does gcc reassociate the additions when you use -fwrapv? Interestingly, just today I wrote a piece of code that sums up signed longs in a loop, and gcc reassociates the additions in that compuation and vectorizes it. I guess that this happens at a later stage in the compiler where all undefined behaviour shenanigans are already behind it, so reassociation is now safe. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID:
<2026Nov19.193817@mips.complang.tuwien.ac.at>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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> <sgu0flpndaqltpdbft40fghmi7vk78q2hs@4ax.com> <11cq2v7$dh5b$1@dont-email.me> <8jk1fl182pskj3fsmp4ju6qf420pgl0htg@4ax.com> <11csull$1ef6e$1@dont-email.me> <2026Nov9.230104@mips.complang.tuwien.ac.at> <11cvcr4$28k7g$1@dont-email.me> <11d3npt$3mmho$1@dont-email.me> <11dn936$2ebc2$1@dont-email.me> <11dndfd$2fr9j$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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: reassociation and undefined behaviour (was: vector length CSR?) Date: Thu, 19 Nov 2026 18:38:17 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 51 Message-ID: <2026Nov19.193817@mips.complang.tuwien.ac.at> References: <11cpqih$acf5$1@dont-email.me> <sgu0flpndaqltpdbft40fghmi7vk78q2hs@4ax.com> <11cq2v7$dh5b$1@dont-email.me> <8jk1fl182pskj3fsmp4ju6qf420pgl0htg@4ax.com> <11csull$1ef6e$1@dont-email.me> <2026Nov9.230104@mips.complang.tuwien.ac.at> <11cvcr4$28k7g$1@dont-email.me> <11d3npt$3mmho$1@dont-email.me> <11dn936$2ebc2$1@dont-email.me> <11dndfd$2fr9j$1@dont-email.me> Injection-Date: Thu, 19 Nov 2026 19:04:46 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2681145"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DNP0w3gcNCXdUOQsWFiZX"; posting-host="eb2f5bd5746d223b058ab42788bfc498" Cancel-Lock: sha1:STV+hsxOQiayUo070nAyQKiopVg= sha256:/E52hC02gSBR9P3CLYi/zuI1NeIrJsKEQfzdT8IBD/U= sha1:+ow6MKG4nADVy6aJDZiMPo6knvA= sha256:E2swt+oxfy6BWl7WiSYIcXVee10VrVn7RJgjOot3ZRw= X-newsreader: xrn 10.11