🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

180 total messages Page 4 of 4 Started by Robert Finch Sun, 08 Nov 2026 12:39
Page 4 of 4 • 180 total messages
Re: reassociation and undefined behaviour (was: vector length CSR?)
#983
Author: Tim Rentsch
Date: Mon, 30 Nov 2026 08:02
41 lines
3198 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:

> 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.  [...]

I suspect you are misunderstanding the meaning of RB's comment.
The rules of C do not prohibit the optimization from being done.
gcc does not perform the transformation because of a choice gcc
has made about how code is compiled.  If gcc had made different
choices then changing the order of integer adds would still be
conforming.  The holdup here is not undefined behavior but how
gcc has chosen to exploit, or not exploit, some cases where some
circumstances of undefined should be treated.
Re: reassociation and undefined behaviour (was: vector length CSR?)
#984
Author: Anton Ertl
Date: Mon, 30 Nov 2026 09:22
42 lines
3221 bytes
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>
>> Thomas Koenig <tkoenig@netcologne.de> writes:
>>> (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.  [...]
>
>I suspect you are misunderstanding the meaning of RB's comment.

The comment in it's entirety is:

|probably a duplicate, the GIMPLE level re-association pass doesn't
|consider signed integers (due to overflow UB)

He uses the word "due" and not "because", but it's clear that the
reason he states for not performing this optimization is "overflow
UB".

In the part of my posting that you deleted, I discuss why the gcc
maintainers may have decided not to reassociate in this case.

>The rules of C do not prohibit the optimization from being done.
>gcc does not perform the transformation because of a choice gcc
>has made about how code is compiled.

This does not change anything about the fact that gcc does not
reassociate the supposedly optimization-friendly overflow-UB integers,
while it does reassociate integers where overflow is defined to
perform modulo arithmetics.  And it does not change the fact that
Richard Biener has written that gcc makes this choice "due to overflow
UB".

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: reassociation and undefined behaviour (was: vector length CSR?)
#985
Author: Dan Cross
Date: Mon, 30 Nov 2026 12:45
59 lines
3296 bytes
In article <86o6b6kaqf.fsf@linuxsc.com>,
Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>
>> 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.  [...]
>
>I suspect you are misunderstanding the meaning of RB's comment.
>The rules of C do not prohibit the optimization from being done.

The code in question isn't even C; it is Fortran.  It says that
quite clearly in both the bug description as well as the code
snippets in the bug that Thomas was referring to.

Rules for undefined behavior in Fortran obviously different from
those of C, since Fortran is a different langauge.

>gcc does not perform the transformation because of a choice gcc
>has made about how code is compiled.  If gcc had made different
>choices then changing the order of integer adds would still be
>conforming.  The holdup here is not undefined behavior but how
>gcc has chosen to exploit, or not exploit, some cases where some
>circumstances of undefined should be treated.

C came up in this context as a parenthetical, "if one considers
the assembly equivalent of this...".  Here, it was clear that C
syntax was merely used as a pseudocode.  Arguments about C's
behavior and definition are beside the point.

	- Dan C.
Re: reassociation and undefined behaviour (was: vector length CSR?)
#986
Author: Thomas Koenig
Date: Mon, 30 Nov 2026 13:29
32 lines
2662 bytes
Dan Cross <cross@spitfire.i.gajendra.net> schrieb:

> Rules for undefined behavior in Fortran obviously different from
> those of C, since Fortran is a different langauge.

Fortran has not concept of undefined behavior in the standard per se.
It has correct code, code that violates numbered constraints (which
require a diagnostic) and code that violates a "shall" directive,
which is errouneous but for which no diagnostic is required.

Integer overflow is among the restrictions that a processor (Fortran
standardese for the combination of compiler, run-time system etc) is
not required to catch, a.k.a. the programmer's fault.  And Fortran
allows reassociation as long as parentheses are not violated.
>
> C came up in this context as a parenthetical, "if one considers
> the assembly equivalent of this...".  Here, it was clear that C
> syntax was merely used as a pseudocode.  Arguments about C's
> behavior and definition are beside the point.

There are a few points to consider here.  Gcc is fairly C-centric,
even now. Also, tools like the sanitizers could cause problems
with integer reassociation.

But I have been thinking about introducing a -freassoc-integer
flag to gcc, and enabling it by default by gfortran.  Maybe at
the beginning of stage 1 for gcc 18.

-- 
This USENET posting was made without artificial intelligence,
artificial impertinence, artificial arrogance, artificial stupidity,
artificial flavorings or artificial colorants.
Re: reassociation and undefined behaviour (was: vector length CSR?)
#987
Author: Tim Rentsch
Date: Mon, 30 Nov 2026 14:48
55 lines
3663 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>
>>> Thomas Koenig <tkoenig@netcologne.de> writes:
>>>
>>>> (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.  [...]
>>
>> I suspect you are misunderstanding the meaning of RB's comment.
>
> The comment in it's entirety is:
>
> |probably a duplicate, the GIMPLE level re-association pass doesn't
> |consider signed integers (due to overflow UB)

I read the referenced bug report prior to posting my comments.

> He uses the word "due" and not "because", but it's clear that the
> reason he states for not performing this optimization is "overflow
> UB".

There is no question about what he wrote.  The question is what
was meant by it.

> In the part of my posting that you deleted, I discuss why the gcc
> maintainers may have decided not to reassociate in this case.

Yes, I read your entire posting.

>> The rules of C do not prohibit the optimization from being done.
>> gcc does not perform the transformation because of a choice gcc
>> has made about how code is compiled.
>
> This does not change anything about the fact that gcc does not
> reassociate the supposedly optimization-friendly overflow-UB
> integers, while it does reassociate integers where overflow is
> defined to perform modulo arithmetics.

I don't see any inconsistency between this statement and what I
wrote.

> And it does not change the fact that Richard Biener has written
> that gcc makes this choice "due to overflow UB".

Again, there is no question about what he wrote.  The question is
what was meant by it.
Re: vector length CSR?
#988
Author: Tim Rentsch
Date: Mon, 30 Nov 2026 15:12
16 lines
2477 bytes
aph@littlepinkcloud.invalid writes:

[...]

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

I understand what you're trying to say, but this isn't how to say
it.  "Undefined behavior" is a circumstance that depends on
program source code and on the state of the abstract machine
during a putative execution.  A compiler cannot "introduce
undefined behavior";  what it can do (but shouldn't) is transform
the program being compiled in a way that changes the semantics
required by the language being translated.
Re: CISC are not "unreduced"
#990
Author: Waldek Hebisch
Date: Mon, 30 Nov 2026 17:33
74 lines
5116 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>Anton Ertl [2026-11-12 06:21:21] wrote:
>>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>>>Yeah, if we take CISC as motivated by the desire to provide an assembly
>>>>language that is comfortable for humans, we can also take RISC as
>>>>motivated by the desire to provide an assembly language that's
>>>>convenient for compilers (which would rather not need to worry about
>>>>tradeoffs between using LD+ADD+ST vs using some fancy addressing mode
>>>>on ADD).
>>> The fancy addressing mode is the same whether you use it with a LD, a
>>> ST, or an ADD.
>>
>>That's beside the point.  With a load/store architecture you don't need
>>to choose between using the ADD-with-addressing mode or using explicit
>>loads and stores.  Makes life simpler for the compiler.
> 
> No.  With an architecture like the VAX, RISC is actually harder:
> 
> VAX: If operand src1 is in memory, accessed with d(SP), use the ADD
> instruction with d(SP) as second operand.  Likewise for src2 and dst.
> 
> RISC: If operand src2 is in memory, allocate a register for it (but
> why is it in memory? Probably because there are no registers
> available, so first spill some other value); then load the operand
> from d(SP) into the register; possibly do the same for the other
> source operand (but don't spill the register for src2). If the
> destination is in memory, allocate a register for storing it in the
> meantime (again with the potential for spilling).  Finally do the add.
> Store the result if necessary.  And now refill all the spilled values.
> 
> It's all doable, but simpler?  No.

Partially playing devil's advocate: if you reserve registers for
arguments and for handlinig addressing modes, then all one has
to do is generating code for loads.  But some loads seem to be
neccessary even for VAX, so you need function which generates
loads in the compiler anyway.  For RISC, you generate much more
loads than for VAX, but decision when to generate a load is
quite simple.  I estimate that for typical instructions one
needs to reserve between 3 and 8 registers, that is 2 for
arguments, one for result and additional registers to handle
addressing modes.  IIUC Risc-V and ARM need at least one
register to generate large constants which may be needed for
addressing.  Now, with 4 registers reserved on 32 bit ARM
there remain 11 general purpose registers (plus PC), that is
significantly less than 15 general purpose registers on VAX.
But Risc-V and 64 bit ARM even after reserving a few still
have more usable general purpose registers than VAX.

Of course, that assumes out of order RISC.  For superscalar
in-order RISC one needs to schedule instructions and that
significantly complicates register allocation.
 
> For the more common CISC style with load-and-op and read-modify-write,
> things are harder than on the VAX, but RISC still is not simpler.

In simple (almost naive) compiler main issue is how much you
care about possibly redundant temporary registers.  Due to
software conventions, it seems that one actually does not
need to care much.  That is, there is limited supply of
registers usable for local variables.  More precisely,
to avoid excessive spill code around calls variables that
need to persists after function call need to be allocated
in call-preserved registers, and those are relatively small
fraction of all registers.  And in a simple compiler it
is reasonable to assume that all variables that are allocated
to registers should survive after function call (otherwise
compiler need to do some real work like livenes ananlysis,
rage splitting etc.).  So there are plenty of registers that
are usable only for temporaries.

-- 
                              Waldek Hebisch
Re: reassociation and undefined behaviour (was: vector length CSR?)
#992
Author: Michael S
Date: Mon, 30 Nov 2026 21:54
55 lines
3862 bytes
On Mon, 30 Nov 2026 09:22:07 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >  
> >> Thomas Koenig <tkoenig@netcologne.de> writes:  
> >>> (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.  [...]  
> >
> >I suspect you are misunderstanding the meaning of RB's comment.  
> 
> The comment in it's entirety is:
> 
> |probably a duplicate, the GIMPLE level re-association pass doesn't
> |consider signed integers (due to overflow UB)
> 
> He uses the word "due" and not "because", but it's clear that the
> reason he states for not performing this optimization is "overflow
> UB".
> 
> In the part of my posting that you deleted, I discuss why the gcc
> maintainers may have decided not to reassociate in this case.
> 
> >The rules of C do not prohibit the optimization from being done.
> >gcc does not perform the transformation because of a choice gcc
> >has made about how code is compiled.  
> 
> This does not change anything about the fact that gcc does not
> reassociate the supposedly optimization-friendly overflow-UB integers,
> while it does reassociate integers where overflow is defined to
> perform modulo arithmetics.

Show me.
I I spent dozens of minutes looking for such cases and did not find
them.
In all my tests gcc either generate poorly optimized latency bound code
both for signed and unsigned integers or it generates better-looking
code (I didn't measure if it really is faster) again, both for signed
and unsigned integers. I never encountered the latter cases in the
"open" code, only in loops and only in association with SIMDification.

> And it does not change the fact that
> Richard Biener has written that gcc makes this choice "due to overflow
> UB".
> 
> - anton

Re: reassociation and undefined behaviour (was: vector length CSR?)
#993
Author: Michael S
Date: Mon, 30 Nov 2026 22:01
37 lines
2931 bytes
On Mon, 30 Nov 2026 13:29:50 -0000 (UTC)
Thomas Koenig <tkoenig@netcologne.de> wrote:

> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
> 
> > Rules for undefined behavior in Fortran obviously different from
> > those of C, since Fortran is a different langauge.  
> 
> Fortran has not concept of undefined behavior in the standard per se.
> It has correct code, code that violates numbered constraints (which
> require a diagnostic) and code that violates a "shall" directive,
> which is errouneous but for which no diagnostic is required.
> 
> Integer overflow is among the restrictions that a processor (Fortran
> standardese for the combination of compiler, run-time system etc) is
> not required to catch, a.k.a. the programmer's fault.  And Fortran
> allows reassociation as long as parentheses are not violated.

In more simple words, in case of arithmetic operations on signed
integers there is no semantical difference between C and Fortran.

> >
> > C came up in this context as a parenthetical, "if one considers
> > the assembly equivalent of this...".  Here, it was clear that C
> > syntax was merely used as a pseudocode.  Arguments about C's
> > behavior and definition are beside the point.  
> 
> There are a few points to consider here.  Gcc is fairly C-centric,
> even now. Also, tools like the sanitizers could cause problems
> with integer reassociation.
> 
> But I have been thinking about introducing a -freassoc-integer
> flag to gcc, and enabling it by default by gfortran.  Maybe at
> the beginning of stage 1 for gcc 18.
> 

Re: reassociation and undefined behaviour (was: vector length CSR?)
#995
Author: Dan Cross
Date: Mon, 30 Nov 2026 23:54
44 lines
2654 bytes
In article <11ejtoe$eoj7$1@dont-email.me>,
Thomas Koenig  <tkoenig@netcologne.de> wrote:
>Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>
>> Rules for undefined behavior in Fortran obviously different from
>> those of C, since Fortran is a different langauge.
>
>Fortran has not concept of undefined behavior in the standard per se.

It was pretty clear to me that the reference to UB in the ticket
you linked to was just a shorthand.

>It has correct code, code that violates numbered constraints (which
>require a diagnostic) and code that violates a "shall" directive,
>which is errouneous but for which no diagnostic is required.
>
>Integer overflow is among the restrictions that a processor (Fortran
>standardese for the combination of compiler, run-time system etc) is
>not required to catch, a.k.a. the programmer's fault.  And Fortran
>allows reassociation as long as parentheses are not violated.

Ok.  The point I was trying to make is that the context was not
C; it was Fortran.  Thus, references to whatever C's UB rules
are don't really apply.

>> C came up in this context as a parenthetical, "if one considers
>> the assembly equivalent of this...".  Here, it was clear that C
>> syntax was merely used as a pseudocode.  Arguments about C's
>> behavior and definition are beside the point.
>
>There are a few points to consider here.  Gcc is fairly C-centric,
>even now. Also, tools like the sanitizers could cause problems
>with integer reassociation.

Ok, but that's a different matter.

>But I have been thinking about introducing a -freassoc-integer
>flag to gcc, and enabling it by default by gfortran.  Maybe at
>the beginning of stage 1 for gcc 18.

Cool.

	- Dan C.
Re: vector length CSR?
#996
Author: BGB
Date: Mon, 30 Nov 2026 23:57
146 lines
7755 bytes
On 11/30/2026 9:12 AM, Tim Rentsch wrote:
> aph@littlepinkcloud.invalid writes:
> 
> [...]
> 
>> 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.
> 
> I understand what you're trying to say, but this isn't how to say
> it.  "Undefined behavior" is a circumstance that depends on
> program source code and on the state of the abstract machine
> during a putative execution.  A compiler cannot "introduce
> undefined behavior";  what it can do (but shouldn't) is transform
> the program being compiled in a way that changes the semantics
> required by the language being translated.

Yeah.


Most of us would probably describe this as "a compiler bug" or similar.


I did find one in BGBCC not too long ago, where DLL's were broken in the 
RISC-V and XG3 modes:
Instead of doing the correct thing:
   Save GP;
   Load pointer from 0(GP)
     Load an address relative to this pointer into GP.
   ...
   Restore GP;
   Return

It was doing:
   Save GP;
   AUIPC+ADDI to get the start of ".data" (within the PE image space);
   ...

Which, works if compiling a one-off binary image, but does not match my 
ABI. So, it might seem to work in a narrow sense, but then fails 
dramatically in another.


TODO:
Update the posted version of TestKern again to include the relevant 
reloc for RV64G and XG3, as my PE loader had also been lacking this case.

Though, I suspect this was more a case of the initial implementation for 
RISC-V support in BGBCC didn't really account for multi-instancing or 
DLLs (it wouldn't matter for a single-instanced EXE, matters a lot more 
for DLLs).

Also TODO:
Debate how to better handle this for plain RV64G in a way that allows 
for more than 256 DLLs;
For XG3 and RV64G+Jx, the limit is drastically larger, but in the 
present implementation, for RV64G it sets a limit of 256.

Though, the most likely option is to change it to a LUI+ADD+LD sequence 
if RV64G/RV64GC and not JX; with a Disp33 load being used if XG3 (or 
RV64+Jx).

Can also note that my port of Quake 3 Arena has broken, but I had yet to 
go up the debugging treadmill here. Don't know if due to more compiler 
bugs or something else.

So, it is like, the ISA itself is mostly stabilized it seems, but BGBCC 
and TestKern still have a lot of corner cases.

There is still the theoretical question of if/how to eventually handle 
EXEs and DLLs being modified, but Windows seems to give clues. In older 
Windows, it was simply not allowed to change an EXE or DLL if it was 
still in use (it seems to still enforce this for EXEs, but was partly 
relaxed for DLLs). However, can note that for DLLs, it is seemingly 
necessary to close all programs using said DLL, before the OS will 
upgrade newly launched programs to the more recent version.

This would appear to imply that the windows behavior is essentially to 
unload the EXE or DLL once no running program instances exist (and if a 
new program instance is created for an EXE or DLL that was still active, 
it will reuse the previously loaded version). Also the unload behavior 
seems to be "lazy", in that if the close, recompile, restart cycle 
happens too quickly, the program may still launch with a stale version 
of the EXE or DLL (sometimes a bit of an annoyance when debugging 
stuff). From the way it behave, I suspect it may be something like a 5 
or 10 second timer delay between the time the program is clones, and the 
relevant PE images being unloaded.

Well, say, vs more clever options, like if the OS's PE-loader allow 
multiple versions of a DLL to exist at the same time, or actively 
keeping track of the filesystem to know whether the previously loaded 
version is still the most recent version.

...



Well, in the off chance anyone cares, wrote another fiction/sci-fi story:
https://github.com/cr88192/bgbtech_fiction/blob/main/stories/2026-10-28_Bradley1.txt

It is admittedly one I have more mixed feelings about, more heavily 
focused on world-building and psychology; very little action.

partly it is based on stuff that had been beating around in my head for 
a while, but hadn't been written out in a story.

Setting is mostly present day, but in different timelines that differ 
from our own (one that had technology like our own, but a much more 
strictly heteronormative culture; the other is more progressive, but 
went down a very different path regarding technology).


I did implement something following the general structure of the VQ 
codec idea from the story, which became BTIC6A:
https://github.com/cr88192/bgbtech_misc/blob/master/test_btic6a/2026-11-05_BTIC6A.txt

Which basically worked, and while it doesn't exactly break any Q/bpp 
records, it is fast enough to do 2160p30 in a single-threaded plain-C 
decoder.

It was also added to my BTIC1H2 codec driver as an option, requires 
editing a config file to select for encoding though (due to an annoyance 
of how VfW works, would either need to make a configuration dialog/tool, 
or create a separate codec DLL for each encoder to have them in the main 
codec list).


Differs from the in-story version primarily in that it was implemented 
around Little-Endian, one can just sort of imagine the story version as 
being like BT6A but with the TLV endianess swapped, and the command 
tag-packing transposed (starts at MSB in story world).

Say:
   0iiiiiii
   1001pppp-pppppppp
Etc.

I went with an LE design mostly because it would be more efficient on 
real-world targets. Even if not 1:1 for that imagined for the story 
setting (in effect, the version I implemented was more a "proof of 
concept" of the design).

...

Re: reassociation and undefined behaviour (was: vector length CSR?)
#1001
Author: Anton Ertl
Date: Tue, 01 Dec 2026 08:46
29 lines
2713 bytes
Michael S <already5chosen@yahoo.com> writes:
>On Mon, 30 Nov 2026 09:22:07 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>> This does not change anything about the fact that gcc does not
>> reassociate the supposedly optimization-friendly overflow-UB integers,
>> while it does reassociate integers where overflow is defined to
>> perform modulo arithmetics.
>
>Show me.

See <2026Nov22.101624@mips.complang.tuwien.ac.at>, to which you
replied yourself.

>I I spent dozens of minutes looking for such cases and did not find
>them.
>In all my tests gcc either generate poorly optimized latency bound code
>both for signed and unsigned integers

In the example shown above, gcc produces reassociated code with the
same tree-height as the original code.  I do not know why the authors
of the gcc reassociation transformation reassociate the computation in
this way, but I suspect that either a SPEC benchmark or a customer
application benefits from it.  I would expect that, on average,
minimizing the tree height is best.


-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: reassociation and undefined behaviour
#1002
Author: David Brown
Date: Tue, 01 Dec 2026 11:53
75 lines
4577 bytes
On 01/12/2026 00:54, Dan Cross wrote:
> In article <11ejtoe$eoj7$1@dont-email.me>,
> Thomas Koenig  <tkoenig@netcologne.de> wrote:
>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>>
>>> Rules for undefined behavior in Fortran obviously different from
>>> those of C, since Fortran is a different langauge.
>>
>> Fortran has not concept of undefined behavior in the standard per se.
> 
> It was pretty clear to me that the reference to UB in the ticket
> you linked to was just a shorthand.
> 
>> It has correct code, code that violates numbered constraints (which
>> require a diagnostic) and code that violates a "shall" directive,
>> which is errouneous but for which no diagnostic is required.
>>
>> Integer overflow is among the restrictions that a processor (Fortran
>> standardese for the combination of compiler, run-time system etc) is
>> not required to catch, a.k.a. the programmer's fault.  And Fortran
>> allows reassociation as long as parentheses are not violated.
> 
> Ok.  The point I was trying to make is that the context was not
> C; it was Fortran.  Thus, references to whatever C's UB rules
> are don't really apply.

While that's true, AFAIUI (and Thomas will hopefully correct me if I am 
wrong!) the Fortran rules for signed and unsigned integer arithmetic 
here are the same as for C.  Fortran does not use the term "undefined 
behaviour", but the meaning is the same.  And in this particular case, 
in both C and Fortran, addition is fully commutative and associative for 
both signed and unsigned types in the standard language.  It is only 
when additional semantics are applied (such as trapping on overflow) 
that there is any difference.

> 
>>> C came up in this context as a parenthetical, "if one considers
>>> the assembly equivalent of this...".  Here, it was clear that C
>>> syntax was merely used as a pseudocode.  Arguments about C's
>>> behavior and definition are beside the point.
>>
>> There are a few points to consider here.  Gcc is fairly C-centric,
>> even now. Also, tools like the sanitizers could cause problems
>> with integer reassociation.

Sanitizers should not be reducing or limiting the efficiency of code 
generated when the sanitizer is not enabled!  It's perfectly fine to 
disable re-association (or whatever else is appropriate) when generating 
code with run-time checks from sanitizers - that is to be expected.

> 
> Ok, but that's a different matter.
> 
>> But I have been thinking about introducing a -freassoc-integer
>> flag to gcc, and enabling it by default by gfortran.  Maybe at
>> the beginning of stage 1 for gcc 18.
> 
> Cool.
> 
It sounds to me that this is a limitation - or missed optimisation 
opportunity - in general in gcc, not a Fortran specific thing or 
something that should need a flag to enable.

Note that I have no idea how any of this plays out in real timing on big 
modern processors - register renaming, OOO, super-scaler, etc., all make 
these things complicated.  But I can see it is definitely suboptimal for 
the kind of processors I do know about, such as embedded microcontrollers.

I've made an additional comment in the bug report, and hope that will be 
of some use.




Re: reassociation and undefined behaviour
#1003
Author: Thomas Koenig
Date: Tue, 01 Dec 2026 17:15
24 lines
2217 bytes
David Brown <david.brown@hesbynett.no> schrieb:

> Note that I have no idea how any of this plays out in real timing on big 
> modern processors - register renaming, OOO, super-scaler, etc., all make 
> these things complicated.  But I can see it is definitely suboptimal for 
> the kind of processors I do know about, such as embedded microcontrollers.

Even with OoO, latency is king; reducing it is always good because
if it ends up on the critical path, it will matter.

Just about the only exception where rearranging a+b+c+d into
(a+b)+(c+d) is not profitable are single-issue processors where
the result is available on the next cacle.

> I've made an additional comment in the bug report, and hope that will be 
> of some use.

Thanks for that. I think I will split that off from the
Fortran case because it concerns the general case.

-- 
This USENET posting was made without artificial intelligence,
artificial impertinence, artificial arrogance, artificial stupidity,
artificial flavorings or artificial colorants.
Re: CISC are not "unreduced"
#1004
Author: MitchAlsup
Date: Tue, 01 Dec 2026 18:54
97 lines
5580 bytes
antispam@fricas.org (Waldek Hebisch) posted:

> Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
> > Stefan Monnier <monnier@iro.umontreal.ca> writes:
--------------
> >>That's beside the point.  With a load/store architecture you don't need
> >>to choose between using the ADD-with-addressing mode or using explicit
> >>loads and stores.  Makes life simpler for the compiler.
> > 
> > No.  With an architecture like the VAX, RISC is actually harder:
> > 
> > VAX: If operand src1 is in memory, accessed with d(SP), use the ADD
> > instruction with d(SP) as second operand.  Likewise for src2 and dst.
> > 
> > RISC: If operand src2 is in memory, allocate a register for it (but
> > why is it in memory? Probably because there are no registers
> > available, so first spill some other value); then load the operand
> > from d(SP) into the register; possibly do the same for the other
> > source operand (but don't spill the register for src2). If the
> > destination is in memory, allocate a register for storing it in the
> > meantime (again with the potential for spilling).  Finally do the add.
> > Store the result if necessary.  And now refill all the spilled values.
> > 
> > It's all doable, but simpler?  No.
> 
> Partially playing devil's advocate: if you reserve registers for
> arguments and for handlinig addressing modes, then all one has
> to do is generating code for loads.  But some loads seem to be
> neccessary even for VAX, so you need function which generates
> loads in the compiler anyway.
>                                For RISC, you generate much more
> loads than for VAX, but decision when to generate a load is
> quite simple.

The data does not substantiate that statement. CISC machines operated
with an inbound memory reference about 50% of instructions. RISC
machines trimmed this down to 20%. 

A variable is used (typically) 1.2 times after it gets loaded.
A CISC will simply use the D$ and produce 2 address modes,
whereas RISC will LD the location and use it 1.2 times.

>                I estimate that for typical instructions one
> needs to reserve between 3 and 8 registers, that is 2 for
> arguments, one for result and additional registers to handle
> addressing modes.

Registers 00..08 are for arguments {R0 is return address}
Registers 09..15 are temporary {unused arguments are also temp}
Registers 16..31 are preserved. R30 is FP, R31 is SP

>                    IIUC Risc-V and ARM need at least one
> register to generate large constants which may be needed for
> addressing.

That is a typical RISC problem--but not for my ISA

>              Now, with 4 registers reserved on 32 bit ARM
> there remain 11 general purpose registers (plus PC), that is
> significantly less than 15 general purpose registers on VAX.

Don't forget the free access to memory via address modes.

> But Risc-V and 64 bit ARM even after reserving a few still
> have more usable general purpose registers than VAX.

But less than me !!
 
> Of course, that assumes out of order RISC.  For superscalar
> in-order RISC one needs to schedule instructions and that
> significantly complicates register allocation.
>  
> > For the more common CISC style with load-and-op and read-modify-write,
> > things are harder than on the VAX, but RISC still is not simpler.
> 
> In simple (almost naive) compiler main issue is how much you
> care about possibly redundant temporary registers.  Due to

Does redundant mean the same value is in more than one register ??

> software conventions, it seems that one actually does not
> need to care much.  That is, there is limited supply of
> registers usable for local variables.  More precisely,
> to avoid excessive spill code around calls variables that
> need to persists after function call need to be allocated
> in call-preserved registers, and those are relatively small
> fraction of all registers.'

Where "small fraction" = a full 50% of them.

>                              And in a simple compiler it
> is reasonable to assume that all variables that are allocated
> to registers should survive after function call (otherwise
> compiler need to do some real work like livenes ananlysis,
> rage splitting etc.).  So there are plenty of registers that
> are usable only for temporaries.
> 
Re: reassociation and undefined behaviour
#1012
Author: Stefan Monnier
Date: Tue, 01 Dec 2026 19:01
20 lines
1899 bytes
David Brown [2026-12-01 12:53:59] wrote:
> And in this particular case, in both C and Fortran, addition is fully
> commutative and associative for both signed and unsigned types in the
> standard language.

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.

Of course, UB allows the compiler to choose to give the same
semantics to both forms (e.g. using wraparound).


=== Stefan
Re: reassociation and undefined behaviour
#1005
Author: MitchAlsup
Date: Tue, 01 Dec 2026 19:02
24 lines
1968 bytes
Thomas Koenig <tkoenig@netcologne.de> posted:

> David Brown <david.brown@hesbynett.no> schrieb:
> 
> > Note that I have no idea how any of this plays out in real timing on big 
> > modern processors - register renaming, OOO, super-scaler, etc., all make 
> > these things complicated.  But I can see it is definitely suboptimal for 
> > the kind of processors I do know about, such as embedded microcontrollers.
> 
> Even with OoO, latency is king; reducing it is always good because
> if it ends up on the critical path, it will matter.
> 
> Just about the only exception where rearranging a+b+c+d into
> (a+b)+(c+d) is not profitable are single-issue processors where
> the result is available on the next cacle.

I always thought a 'cacle' was what a witch emitted when laughing.
 
> > I've made an additional comment in the bug report, and hope that will be 
> > of some use.
> 
> Thanks for that. I think I will split that off from the
> Fortran case because it concerns the general case.
> 
Re: CISC are not "unreduced"
#1006
Author: EricP
Date: Tue, 01 Dec 2026 19:48
51 lines
3349 bytes
On 2026-Dec-01 13:54, MitchAlsup wrote:
> antispam@fricas.org (Waldek Hebisch) posted:
> 
> 
>>                 I estimate that for typical instructions one
>> needs to reserve between 3 and 8 registers, that is 2 for
>> arguments, one for result and additional registers to handle
>> addressing modes.
> 
> Registers 00..08 are for arguments {R0 is return address}
> Registers 09..15 are temporary {unused arguments are also temp}
> Registers 16..31 are preserved. R30 is FP, R31 is SP
> 
>>                     IIUC Risc-V and ARM need at least one
>> register to generate large constants which may be needed for
>> addressing.
> 
> That is a typical RISC problem--but not for my ISA
> 
>>               Now, with 4 registers reserved on 32 bit ARM
>> there remain 11 general purpose registers (plus PC), that is
>> significantly less than 15 general purpose registers on VAX.
> 
> Don't forget the free access to memory via address modes.

Yes, VAX had a bunch of temp registers for holding intermediate
address results. IIRC the 780 had a whole 16 register file for them.
The instruction's operand specifiers were processed as they were
parsed so any opspec accesses and side effects took place serially
before the next opspec was fetched, parsed and executed.

VAX reserved use of some registers:
R15   PC
R14   SP
R13   FP
R12   AP Argument Pointer
R3,R5 Address counter in character and decimal instructions
R2,R4 Length counter in character and decimal instructions
R1    High function result value (optional), result of varous instructions
R0    Low  function result value, result of various instructions

The CALLS, CALLG and RET instructions and routine entry mask required
R12..R15 to be used this way.

There was no separate FPU register file. Double, quad and octal precision
FP values took 2, 4, or 8 sets of aligned register ids.

There was no multi threading then, otherwise they'd have needed a
TP thread pointer register too.

Re: CISC are not "unreduced"
#1007
Author: EricP
Date: Tue, 01 Dec 2026 19:51
51 lines
3352 bytes
On 2026-Dec-01 13:54, MitchAlsup wrote:
> antispam@fricas.org (Waldek Hebisch) posted:
> 
> 
>>                 I estimate that for typical instructions one
>> needs to reserve between 3 and 8 registers, that is 2 for
>> arguments, one for result and additional registers to handle
>> addressing modes.
> 
> Registers 00..08 are for arguments {R0 is return address}
> Registers 09..15 are temporary {unused arguments are also temp}
> Registers 16..31 are preserved. R30 is FP, R31 is SP
> 
>>                     IIUC Risc-V and ARM need at least one
>> register to generate large constants which may be needed for
>> addressing.
> 
> That is a typical RISC problem--but not for my ISA
> 
>>               Now, with 4 registers reserved on 32 bit ARM
>> there remain 11 general purpose registers (plus PC), that is
>> significantly less than 15 general purpose registers on VAX.
> 
> Don't forget the free access to memory via address modes.

Yes, VAX had a bunch of temp registers for holding intermediate
addressing results. IIRC the 780 had a whole 16 register file for them.
The instruction's operand specifiers were processed as they were
parsed so any opspec accesses and side effects took place serially
before the next opspec was fetched, parsed and executed.

VAX reserved use of some registers:
R15   PC
R14   SP
R13   FP
R12   AP Argument Pointer
R3,R5 Address counter in character and decimal instructions
R2,R4 Length counter in character and decimal instructions
R1    High function result value (optional), result of varous instructions
R0    Low  function result value, result of various instructions

The CALLS, CALLG and RET instructions and routine entry mask required
R12..R15 to be used this way.

There was no separate FPU register file. Double, quad and octal precision
FP values took 2, 4, or 8 sets of aligned register ids.

There was no multi threading then, otherwise they'd have needed a
TP thread pointer register too.

Re: reassociation and undefined behaviour
#1008
Author: Dan Cross
Date: Tue, 01 Dec 2026 19:57
44 lines
2936 bytes
In article <11emcgn$1k681$1@dont-email.me>,
David Brown  <david.brown@hesbynett.no> wrote:
>On 01/12/2026 00:54, Dan Cross wrote:
>> In article <11ejtoe$eoj7$1@dont-email.me>,
>> Thomas Koenig  <tkoenig@netcologne.de> wrote:
>>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>>>
>>>> Rules for undefined behavior in Fortran obviously different from
>>>> those of C, since Fortran is a different langauge.
>>>
>>> Fortran has not concept of undefined behavior in the standard per se.
>>
>> It was pretty clear to me that the reference to UB in the ticket
>> you linked to was just a shorthand.
>>
>>> It has correct code, code that violates numbered constraints (which
>>> require a diagnostic) and code that violates a "shall" directive,
>>> which is errouneous but for which no diagnostic is required.
>>>
>>> Integer overflow is among the restrictions that a processor (Fortran
>>> standardese for the combination of compiler, run-time system etc) is
>>> not required to catch, a.k.a. the programmer's fault.  And Fortran
>>> allows reassociation as long as parentheses are not violated.
>>
>> Ok.  The point I was trying to make is that the context was not
>> C; it was Fortran.  Thus, references to whatever C's UB rules
>> are don't really apply.
>
>While that's true, AFAIUI (and Thomas will hopefully correct me if I am
>wrong!) the Fortran rules for signed and unsigned integer arithmetic
>here are the same as for C.  Fortran does not use the term "undefined
>behaviour", but the meaning is the same.  And in this particular case,
>in both C and Fortran, addition is fully commutative and associative for
>both signed and unsigned types in the standard language.  It is only
>when additional semantics are applied (such as trapping on overflow)
>that there is any difference.

Perhaps that's so, but it is, again, besides the point that I
was making: suggesting that Thomas misunderstood the response on
the bug and citing the rules of C is ironic, when the context
was not C.

	- Dan C.
Re: CISC are not "unreduced"
#1009
Author: EricP
Date: Tue, 01 Dec 2026 20:08
28 lines
2607 bytes
On 2026-Dec-01 14:51, EricP wrote:
> 
> Yes, VAX had a bunch of temp registers for holding intermediate
> addressing results. IIRC the 780 had a whole 16 register file for them.
> The instruction's operand specifiers were processed as they were
> parsed so any opspec accesses and side effects took place serially
> before the next opspec was fetched, parsed and executed.
> 
> VAX reserved use of some registers:
> R15   PC
> R14   SP
> R13   FP
> R12   AP Argument Pointer
> R3,R5 Address counter in character and decimal instructions
> R2,R4 Length counter in character and decimal instructions
> R1    High function result value (optional), result of varous instructions
> R0    Low  function result value, result of various instructions
> 
> The CALLS, CALLG and RET instructions and routine entry mask required
> R12..R15 to be used this way.
> 
> There was no separate FPU register file. Double, quad and octal precision
> FP values took 2, 4, or 8 sets of aligned register ids.

Oops.. brain fart... there was no octal FP, just 4 formats for single fp-F,
two double fp-G and fp-D, and quad fp-H

Re: reassociation and undefined behaviour
#1010
Author: David Brown
Date: Tue, 01 Dec 2026 20:19
49 lines
3699 bytes
On 01/12/2026 20:57, Dan Cross wrote:
> In article <11emcgn$1k681$1@dont-email.me>,
> David Brown  <david.brown@hesbynett.no> wrote:
>> On 01/12/2026 00:54, Dan Cross wrote:
>>> In article <11ejtoe$eoj7$1@dont-email.me>,
>>> Thomas Koenig  <tkoenig@netcologne.de> wrote:
>>>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>>>>
>>>>> Rules for undefined behavior in Fortran obviously different from
>>>>> those of C, since Fortran is a different langauge.
>>>>
>>>> Fortran has not concept of undefined behavior in the standard per se.
>>>
>>> It was pretty clear to me that the reference to UB in the ticket
>>> you linked to was just a shorthand.
>>>
>>>> It has correct code, code that violates numbered constraints (which
>>>> require a diagnostic) and code that violates a "shall" directive,
>>>> which is errouneous but for which no diagnostic is required.
>>>>
>>>> Integer overflow is among the restrictions that a processor (Fortran
>>>> standardese for the combination of compiler, run-time system etc) is
>>>> not required to catch, a.k.a. the programmer's fault.  And Fortran
>>>> allows reassociation as long as parentheses are not violated.
>>>
>>> Ok.  The point I was trying to make is that the context was not
>>> C; it was Fortran.  Thus, references to whatever C's UB rules
>>> are don't really apply.
>>
>> While that's true, AFAIUI (and Thomas will hopefully correct me if I am
>> wrong!) the Fortran rules for signed and unsigned integer arithmetic
>> here are the same as for C.  Fortran does not use the term "undefined
>> behaviour", but the meaning is the same.  And in this particular case,
>> in both C and Fortran, addition is fully commutative and associative for
>> both signed and unsigned types in the standard language.  It is only
>> when additional semantics are applied (such as trapping on overflow)
>> that there is any difference.
> 
> Perhaps that's so, but it is, again, besides the point that I
> was making: suggesting that Thomas misunderstood the response on
> the bug and citing the rules of C is ironic, when the context
> was not C.
> 

Agreed.  Talking about C can make sense since a lot more people are 
familiar with the details of C than those of Fortran, but the post 
should have put in the connection (that the rules here are the same) 
rather than just changing languages.
Re: reassociation and undefined behaviour
#1011
Author: Dan Cross
Date: Tue, 01 Dec 2026 20:40
59 lines
3723 bytes
In article <11ena4h$24n1m$1@dont-email.me>,
David Brown  <david.brown@hesbynett.no> wrote:
>On 01/12/2026 20:57, Dan Cross wrote:
>> In article <11emcgn$1k681$1@dont-email.me>,
>> David Brown  <david.brown@hesbynett.no> wrote:
>>> On 01/12/2026 00:54, Dan Cross wrote:
>>>> In article <11ejtoe$eoj7$1@dont-email.me>,
>>>> Thomas Koenig  <tkoenig@netcologne.de> wrote:
>>>>> Dan Cross <cross@spitfire.i.gajendra.net> schrieb:
>>>>>
>>>>>> Rules for undefined behavior in Fortran obviously different from
>>>>>> those of C, since Fortran is a different langauge.
>>>>>
>>>>> Fortran has not concept of undefined behavior in the standard per se.
>>>>
>>>> It was pretty clear to me that the reference to UB in the ticket
>>>> you linked to was just a shorthand.
>>>>
>>>>> It has correct code, code that violates numbered constraints (which
>>>>> require a diagnostic) and code that violates a "shall" directive,
>>>>> which is errouneous but for which no diagnostic is required.
>>>>>
>>>>> Integer overflow is among the restrictions that a processor (Fortran
>>>>> standardese for the combination of compiler, run-time system etc) is
>>>>> not required to catch, a.k.a. the programmer's fault.  And Fortran
>>>>> allows reassociation as long as parentheses are not violated.
>>>>
>>>> Ok.  The point I was trying to make is that the context was not
>>>> C; it was Fortran.  Thus, references to whatever C's UB rules
>>>> are don't really apply.
>>>
>>> While that's true, AFAIUI (and Thomas will hopefully correct me if I am
>>> wrong!) the Fortran rules for signed and unsigned integer arithmetic
>>> here are the same as for C.  Fortran does not use the term "undefined
>>> behaviour", but the meaning is the same.  And in this particular case,
>>> in both C and Fortran, addition is fully commutative and associative for
>>> both signed and unsigned types in the standard language.  It is only
>>> when additional semantics are applied (such as trapping on overflow)
>>> that there is any difference.
>>
>> Perhaps that's so, but it is, again, besides the point that I
>> was making: suggesting that Thomas misunderstood the response on
>> the bug and citing the rules of C is ironic, when the context
>> was not C.
>>
>
>Agreed.  Talking about C can make sense since a lot more people are
>familiar with the details of C than those of Fortran, but the post
>should have put in the connection (that the rules here are the same)
>rather than just changing languages.

Indeed.  And we are taking it as a supposition that the rules
are the same; as has been pointed out, Fortran doesn't have C's
notion "undefined behavior", though I gather there is a similar
construct.  Regardless, without knowing Fortran better, I cannot
say that they are exactly equivalent.

	- Dan C.
Re: CISC are not "unreduced"
#1015
Author: Waldek Hebisch
Date: Wed, 02 Dec 2026 02:54
159 lines
8905 bytes
EricP <ThatWouldBeTelling@thevillage.com> wrote:
> On 2026-Dec-01 13:54, MitchAlsup wrote:
>> antispam@fricas.org (Waldek Hebisch) posted:
>> 
>> 
>>>                 I estimate that for typical instructions one
>>> needs to reserve between 3 and 8 registers, that is 2 for
>>> arguments, one for result and additional registers to handle
>>> addressing modes.
>> 
>> Registers 00..08 are for arguments {R0 is return address}
>> Registers 09..15 are temporary {unused arguments are also temp}
>> Registers 16..31 are preserved. R30 is FP, R31 is SP
>> 
>>>                     IIUC Risc-V and ARM need at least one
>>> register to generate large constants which may be needed for
>>> addressing.
>> 
>> That is a typical RISC problem--but not for my ISA
>> 
>>>               Now, with 4 registers reserved on 32 bit ARM
>>> there remain 11 general purpose registers (plus PC), that is
>>> significantly less than 15 general purpose registers on VAX.
>> 
>> Don't forget the free access to memory via address modes.
> 
> Yes, VAX had a bunch of temp registers for holding intermediate
> addressing results. IIRC the 780 had a whole 16 register file for them.
> The instruction's operand specifiers were processed as they were
> parsed so any opspec accesses and side effects took place serially
> before the next opspec was fetched, parsed and executed.

I did not look at all VAX addressing modes.  But AFAICS scheme
that I propose can handle most (if not all) VAX modes and many
modes which I believe are not available on the VAX.  To explain
it more, with 4 reserved registers I can use all of them when
computing address of first operand.  After computing its address
I load it to register.  So I have 3 registers left that I can
use to compute address of the second operand.  After that
I load the operand and perform the operation.  After that
I have 1 register that I need to preserve and 3 registers that
I can use to compute address of destination.  Once address
of destination is computed, I store it.

Note that Intel style SIB mode (IIUC not available on the VAX)
can be done using 2 registers: in 1 register we generate
constant displacement, then we add to it base.  We copy
index to the second register, then we shift (scale) it,
then we add scaled index to previousy computed sum of
base and constant.

At any stage of this we can copy intermediate result back
to different register, so this convers preinctement and
predecrement.  For postincrement and postdecrement we
can simply emit separate instructions that modify register.
And we can do this either after whole instruction or
after a selected operand.  So, if needed we can emulate
VAX behaviour of updating registers after each operand.
Similarly, we can easily introduce extra indirections
by emiting extra load(s) after computing initial address.
Note that at such time we have at most one register holding
operand or value and the second containing address, so we
still have to registers to do something extra (if needed).

If you thing that this scheme is complex, note that we
can put result of operation in the same register as the
first operand.  So "load to register" needs to preserve
one register and can use 3.  We could use all four
reserved registers for computing address of the first
operand but for simplicity we can reuse address calculation.

Let me add that simple compiler is likely to use only
some address modes.  A naive toy backend that I have just
lowers expression to 3 address form, introducing stack
temporaries if needed.  It is very simple, generated
code is quite bad, but it only generates RISC instructions.
Sligtly better version of this toy backend posses result
of previous operation to the next in a register.  Generated
code is still bad, but visibly better than from the first
one.  It also only generates RISC instructions.  I have
looked at CDC6000 Pascal compiler by U. Amman.  Amman
added a bunch of special cases for frequent Pascal
constructs.  More importantly, he treats registers as
a cache for recently computed values.  CDC 6000 in
a sense is most strict RISC, that is IIUC it does not
allow constants as arguments to computational instructions.
But similar compiler structure was used for CISC-s
and AFAICS while some CISC instructions reduce number
of instrucions in generated code, having them does not
seem to lead to simplification.

In different compiler, there is machine independent part
that translates program to abstract instructions and
performs some simlifications.  This machine dependent
part generates code for for each abstract instruction
separately.  The abstract instruction set has visible
VAX smell, but uses only small part of VAX instruction
set and only selected addressing modes.  So it is no
wonder that VAX code generator is the smallest one
(1164 lines).  ARM64 and Risc-V code generators are
significantly bigger (1731 and 1767 lines respectively).
Part of that is that VAX handles 3 sizes for loads and
stores, that is 1, 2 and 4 bytes, while ARM64 and Risc-V
handle 4 sizes (that is also 8 bytes) which adds
additional cases in few places.  On Risc-V 20 lines
emulate autoindexing (due to compiler structure and
structure of Risc-V instruction set emulated
autoindexing gives better code than handling this
in machine independent part).  Part of difference may
be due to different authors and differnt code styles
(VAX backed was probably created 45 years ago and
last change seem to be 31 years ago, ARM64 and Risc-V
are very recent).  But the biggest difference comes
from fact that VAX backend has very simple map from
addresing modes to VAX assembler, while other targets
need to emulate (some) VAX addressing modes.

But that is historical artifact, VAX was one of early
targets.  More importantly, VAX was first 32-bit target
which allowed system expansion, so it is natural
that compiler structure was made to match VAX well.
Without such history it is likely that compier would
have different structure, making difference smaller.
Also, whole compiler is some tens thousend lines,
(the compiler if part of larger system and it is not so
easy to tell what is compiler proper and what has other
function).  Comparing  to the whole compiler difference
in backend size is modest.

> VAX reserved use of some registers:
> R15   PC
> R14   SP
> R13   FP
> R12   AP Argument Pointer
> R3,R5 Address counter in character and decimal instructions
> R2,R4 Length counter in character and decimal instructions
> R1    High function result value (optional), result of varous instructions
> R0    Low  function result value, result of various instructions
> 
> The CALLS, CALLG and RET instructions and routine entry mask required
> R12..R15 to be used this way.
> 
> There was no separate FPU register file. Double, quad and octal precision
> FP values took 2, 4, or 8 sets of aligned register ids.
> 
> There was no multi threading then, otherwise they'd have needed a
> TP thread pointer register too.

In normal use modern machines need stack pointer, either as
hardware feature or as software convention, so I do not count
it separately.  Maybe I am wrong, but my impression was that
FP and AP are usable as temporary registers between calls, so
this use is similar to link register on machines that use it
and to register(s) reserved on some machines for use by dynamic
linking stubs.  From software point of view they do not differ
much from other registers which are not preserved by calls.

-- 
                              Waldek Hebisch
Re: CISC are not "unreduced"
#1019
Author: Stefan Monnier
Date: Wed, 02 Dec 2026 03:45
39 lines
3635 bytes
Anton Ertl [2026-11-13 18:26:42] wrote:
> No.  With an architecture like the VAX, RISC is actually harder:
>
> VAX: If operand src1 is in memory, accessed with d(SP), use the ADD
> instruction with d(SP) as second operand.  Likewise for src2 and dst.
>
> RISC: If operand src2 is in memory, allocate a register for it (but
> why is it in memory? Probably because there are no registers
> available, so first spill some other value);

I can't see how that would happen: if SRC is in memory it's because it
comes from a memory allocated object.  And you don't allocate
a "register" to place the result of the load: you place it in a variable
(of which there's an infinite supply).

*Later* you'll have a register allocation phase which of those variables
may nee to be spilled, but that phase is needed for all ISAs anyway
because of the finite number of registers.

Of course, in olden times maybe you'd write a simpler compiler without
register allocation, in which case all your variables live on the
stack, but in that case you always have plenty of registers available in
a RISC CPU to place the temporary value between the LD and the ADD.

> then load the operand
> from d(SP) into the register; possibly do the same for the other
> source operand (but don't spill the register for src2). If the
> destination is in memory, allocate a register for storing it in the
> meantime (again with the potential for spilling).  Finally do the add.
> Store the result if necessary.  And now refill all the spilled values.
>
> It's all doable, but simpler?  No.

All the "extra steps" you may need to add are steps you also sometimes
have to do in CISCs as well, so even if you may need to do them more
often in RISCs, it imposes no extra implementation work in the compiler.


=== Stefan
Re: CISC are not "unreduced"
#1016
Author: Stephen Fuld
Date: Wed, 02 Dec 2026 05:16
42 lines
3496 bytes
On 12/1/2026 10:54 AM, MitchAlsup wrote:
> antispam@fricas.org (Waldek Hebisch) posted:

snip

>>                                 For RISC, you generate much more
>> loads than for VAX, but decision when to generate a load is
>> quite simple.
> 
> The data does not substantiate that statement. CISC machines operated
> with an inbound memory reference about 50% of instructions. RISC
> machines trimmed this down to 20%.
> 
> A variable is used (typically) 1.2 times after it gets loaded.
> A CISC will simply use the D$ and produce 2 address modes,
> whereas RISC will LD the location and use it 1.2 times.

Something in this seemed "off" to me.  After all, the CISC machines I am 
used to had load instructions and register to register adds, so if the 
"RISC way" were better, they could have used it - just generate the same 
instructions a RISC system would use.

After thinking about it for a while, I realized that the CISC people got 
it right - *for the technology of the time*.  Back then, the disparity 
between CPU speed and memory access time was much less than it is now, 
memory was relativly more expensive than it is now, and there was 
minimal OoO execution.

So for the CISC systems of the day, the cost of the two memory reference 
instructions was relativly less than it is now, and the cost of the 
extra memory usage (three instructions versus two) was relativly more 
than it is now.  So the choice of the non "RISC way" was the right choice.

Now, of course, the relative speeds of CPU and memory, lower cost of 
memory, and the capabilities of OoO to move the load earlier have 
changed the game and the "RISC way" is superior.



-- 
  - Stephen Fuld
(e-mail address disguised to prevent spam)
Re: reassociation and undefined behaviour
#1017
Author: Anton Ertl
Date: Wed, 02 Dec 2026 07:40
53 lines
2926 bytes
Thomas Koenig <tkoenig@netcologne.de> writes:
>Even with OoO, latency is king; reducing it is always good because
>if it ends up on the critical path, it will matter.
>
>Just about the only exception where rearranging a+b+c+d into
>(a+b)+(c+d) is not profitable are single-issue processors where
>the result is available on the next cacle.

There are lots of cases where (a+b)+(c+d) is not the fastest way to
get the sum of a,b,c,d.  E.g., if a is available by an addition
latency (or more) after b, and c and d are available by two addition
latencies or more after a, then a+(b+(c+d)) is better.  An example
where this is easily visible is with linked lists:  If you have

d = l->val;
l = l->next;
c = l->val;
l = l->next;
b = l->val;
l = l->next;
a = l->val;

and the latency of a load is at least as long as the latency of the
addition, the optimal order is

r=c+d
r=b+r
r=a+r

Of course, if you write that evaluation as usual:

r=0;
for (; l!=NULL; l =  l->next)
  r += l->val;

... you get this evaluation order without any reassociation.  OTOH, for

r=0;
for (size_t i=0; i<n; i+=stride)
  r += a[i];

unrolling the loop and reassociating the additions is beneficial on
processors that support more than one load per cycle (i.e., modern
high-end cores).

In any case, the best association depends on the latencies of how the
operands are computed.  If I don't know that, I would go for
(a+b)+(c+d), where the latencies are the same for all operands.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: reassociation and undefined behaviour
#1018
Author: David Brown
Date: Wed, 02 Dec 2026 08:45
50 lines
3461 bytes
On 01/12/2026 20:01, Stefan Monnier wrote:
> David Brown [2026-12-01 12:53:59] wrote:
>> And in this particular case, in both C and Fortran, addition is fully
>> commutative and associative for both signed and unsigned types in the
>> standard language.
> 
> 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.

Sort of, but...

> 
> Of course, UB allows the compiler to choose to give the same
> semantics to both forms (e.g. using wraparound).
> 
Exactly.

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.  And the compiler knows if and 
when it is using wrapping instructions.  That's how it knows it can turn 
"(-2 + x) + 1" into "x - 1" that will give the correct result whenever 
there is no UB in the original expression.

"UB" is something you might have in the original source code.  Compilers 
can do any manipulation and re-arrangement of expressions as long as any 
input in the original expression that did not lead to UB, gives the same 
results in the generated code.  (Sometimes you might consider a 
particular re-arrangement unhelpful, but that's a different matter from 
correctness.)

Remember that the original source code is in "real C", while any 
re-arrangements the compiler makes are in "internal compiler language" 
that can have different semantics or details - as long as the final 
results match up on observable behaviour in the end.  And the semantics 
of this "internal compiler language" don't have to be consistent - in 
one expression, the compiler might treat signed integer overflow as 
wrapping, and in another it might treat it as resulting in the number 42.



Re: reassociation and undefined behaviour
#1020
Author: Anton Ertl
Date: Wed, 02 Dec 2026 12:24
47 lines
3267 bytes
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.

* 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 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.
But instead, gcc 2) reports that the operation cannot be reassociated.
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.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Page 4 of 4 • 180 total messages
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads