[Bug tree-optimization/85585] switch to select a string based on an enum can profitably optimize away the table of pointers/offsets into fixed-length char[] blocks. Or use byte offsets into a string

2018-05-01 Thread joseph at codesourcery dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85585

--- Comment #2 from joseph at codesourcery dot com  ---
On Tue, 1 May 2018, peter at cordes dot ca wrote:

> The current strings + pointer-table implementation doesn't merge string
> literals where one string is a suffix of another; this is another a

The linker does that, as well as merging identical strings in different 
translation units, via strings going in specially marked string sections 
(".section .rodata.str1.1,"aMS",@progbits,1").  These proposed 
optimizations would interfere with such linker optimizations, so they 
might not be a code size win.

[Bug tree-optimization/85585] switch to select a string based on an enum can profitably optimize away the table of pointers/offsets into fixed-length char[] blocks. Or use byte offsets into a string

2018-05-01 Thread peter at cordes dot ca
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85585

--- Comment #1 from Peter Cordes  ---
By comparison, the no-PIE table of pointers only needs one instruction:

movqCSWTCH.4(,%rdi,8), %rax

So all my suggestions cost 1 extra instruction on x86 in no-PIE mode, but at a
massive savings in data size.

clang -fPIE compiles the plain switch to the obvious / sane 2 instruction
sequence which should be our baseline for normal cases.

# clang6.0 -fPIE -O3  (switch compilers on the Godbolt link)
leaq.Lswitch.table.phy_modes(%rip), %rcx
movq(%rcx,%rax,8), %rax

Clang is willing to make a table that needs relocations for the entries.  (My
suggestions all avoid that because they're based on offsets, not a table of
pointers.  Avoiding rodata relocations that dirty a page and prevent sharing
has some non-zero value, although it's low on many architectures where memory
is cheap.)