https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109057
--- Comment #1 from Henry <hbucher at gmail dot com> ---
Two caveats:
1. If you add something like `xor %0,%0` inside the assembly text, LUT is not
optimized
inline void DoNotOptimize( uint8_t value) {
asm volatile("xor %0,%0" : : "r,m"(value) : "memory");
}
void func2(uint8_t val) {
DoNotOptimize(LUT[val]);
}
Produces
func2(unsigned char):
movzbl %dil, %edi
xor LUT(%rdi),LUT(%rdi)
ret
LUT:
.string "\001\005\003"
.ascii "\002\007\001\002"
https://godbolt.org/z/Mn5asGWe4
2. If you make value a uint32_t instead of a uint8_t, LUT is not optimized
#include <stdint.h>
static const uint8_t LUT[8] = {1,5,3,0,2,7,1,2};
inline void DoNotOptimize( uint32_t value) {
asm volatile("" : : "r,m"(value) : "memory");
}
void func2(uint8_t val) {
DoNotOptimize(LUT[val]);
}
Produces
func2(unsigned char):
movzbl %dil, %edi
movzbl LUT(%rdi), %eax
ret
LUT:
.string "\001\005\003"
.ascii "\002\007\001\002"
https://godbolt.org/z/rTfExvEbb