Immutables converted to immediates

Timon Gehr timon.gehr at gmx.ch
Thu Jun 29 01:13:58 UTC 2023


On 6/28/23 19:27, Cecil Ward wrote:
> This is a question about either the front end or back end of GDC and 
> maybe also LDC`.
> 
> At compile time, should the value of arr[0] etc be calculable so that 
> the code below turns out to be something like *p == 1 where n is an 
> immediate constant in asm?
> 
> immutable uint arr = [1,2,3];
> 
> immutable uint * p;
> loop
>      {
>      if ( arr[0]== p[0] && arr[1] == p[1] && arr[2] == p[2]
>          …
>      }
> GDC is generating a structure in the code segment and then fetching it 
> even though the values of the elements ought to be known at 
> compile-time. I’m not sure why. The actual code, x86-64 in this case, 
> consists of a load of fetches from the code segment into successive 
> registers before the start of the loop, and the loop then consists of a 
> load of instructions like cmp [r8+4*rax], r9, where r9, r10 etc we’re 
> loaded up from the fetches from [rip+disp] before th4 loop, a minor 
> strength reduction compared to a compare-immediate, as it has plenty of 
> registers free. The mystery is why the fetches from [rip+disp] even 
> exist, given that they are known values.

You can force constant folding in the frontend like this:

enum uint[3] a = arr[0..3];
if(a[0]== p[0] && a[1] == p[1] && a[2] == p[2])


More information about the Digitalmars-d mailing list