Two suggestions for GCC beginners projects

Aaron Peter Bachmann aaron_ng@inode.at
Thu Nov 28 11:18:35 GMT 2024


Two suggestions for GCC beginners projects

I watched some of the 2024 Gnu Cauldron videos. The question of what 
could be a suitable project for a beginner came up. I have two suggestions:

1.    Add a warning when users use reserved or potentially reserved 
identifiers in their code. This could cover C and posit-identifiers. I 
think clang has that already. It is exceptionally simple, but some 
guidance might help here anyway. E. g. would it be acceptable to produce 
the classification function with [f]lex?

2.    The patterns for bit field insert – at least for small devices 
with no instruction parallelism - have room for improvement on small 
cpus. I use cortex-M0 as an example.

struct ms{
     uint32_t r:21;
     uint32_t m:5;
     uint32_t l:6;
};

struct ms im(struct ms s,int32_t u){
     s.m=u;
     return s;
}

https://godbolt.org/z/4s19xK5E1
The pc-relative load is particularly bad. It wastes space and cycles.  6 
instructions, 32 bit const data, 7 cycles (without bx for return) or 
more if there are (flash) wait states. (Flash wait states in sequential 
code execution are usually hidden by flash interfaces with 
(prefetch)buffers.)

A human would use 5 instructions, no const data, 5 cycles. This always 
works for all 32 bit and 64 bit architectures I know (i. e. with xor, 
and a barrel shifter for shift left, shift right with shift amount as 
immediate).

im:
     lsrs    r3, r0, #21
     eors    r1, r3
     lsls    r1, r1, #26
     lsrs    r1, r1, #6
     eors    r0, r1
     bx      lr

There are many other possibilities to explore. When the bit field is on 
the left or on the right or when small immediates do not require to 
materialize large constants.

Is the shift field insert pattern the right place to address the 
problem? It must be redone in every backend. There might be further 
opportunities for code improvement, when there are several adjacent bit 
field assignments in the source code. Even for this well contained 
little problem guidance from an experienced GCC developer seems desirable.

Clang is a bit better here: 6 instructions, no const data, 6 cycles.
https://godbolt.org/z/neWsWGqbs

RISC-V has no bit field insert instruction. We find the same 
shortcomings regarding code generation for bit field insertion.

Unfortunately, I am not the one asking for guidance myself. It is quite 
sure I will not be able to contribute code for at least the next 7 years.

This is no critique on GCC which is for sure a high quality compiler. 
Just a suggestion for beginner’s projects as the title indicates.

Regards, Aaron Peter Bachmann





More information about the Gcc mailing list