https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127329
Bug ID: 127329
Summary: Missed Optimization: short memcpy to stack variable
not optimized on some targets
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: rsaxvc at gmail dot com
Target Milestone: ---
GCC 14.2 and 16.2, -O2, on several device targets (Xtensa, TI C6x, RISC-V
32bit, Tricore, HPPA) GCC implements the below fetch_memcpy() by loading two
bytes from the pointed memory, then storing both bytes to the stack, then
fetching them into the return register with a 16-bit load. For these cores, a
shorter approach is to fetch the two bytes with two 8-bit loads, then combine
them into the return register without round-tripping through the stack.
```
unsigned short fetch_memcpy(const void * p){
unsigned short x;
__builtin_memcpy(&x, p, sizeof(x));
return x;
}
//I think fetch_memcpy() should be optimizable to the equivalent fetch_2u8:
uint16_t fetch_2u8(const unsigned char * p){
return p[0] | ((unsigned short)p[1]<<8);
}
```
This was also mentioned at https://github.com/espressif/esp-idf/issues/19059