We could put a function address in a jump table. void bar0 (void); void bar1 (void); void bar2 (void); void bar3 (void); void bar4 (void); void foo (int i) { switch (i) { case 0: bar0 (); break; case 1: bar1 (); break; case 2: bar2 (); break; case 3: bar3 (); break; case 4: bar4 (); break; } } Here is what I get on i686-pc-linux-gnu. foo: movl 4(%esp), %eax cmpl $4, %eax ja .L1 jmp *.L8(,%eax,4) .section .rodata .align 4 .align 4 .L8: .long .L3 .long .L4 .long .L5 .long .L6 .long .L7 .text .p2align 2,,3 .L1: ret .L7: jmp bar4 .L3: jmp bar0 .L4: jmp bar1 .L5: jmp bar2 .L6: jmp bar3 It would be nice if I can get something like: foo: movl 4(%esp), %eax cmpl $4, %eax ja .L1 jmp *.L8(,%eax,4) .section .rodata .align 4 .align 4 .L8: .long bar0 .long bar1 .long bar2 .long bar3 .long bar4 .text .p2align 2,,3 .L1: ret
Confirmed, yes that would be nice.
Qualcomm does someting like this, see: http://www.capsl.udel.edu/conferences/open64/2009/Papers/101-codeSizeOpen64_Qualcomm.pdf http://www.capsl.udel.edu/conferences/open64/2009/Slides/001-101-codeSizeOpen64Conf_QCOM.pdf
Maybe something for tree-switch-conversion?
(In reply to Steven Bosscher from comment #3) > Maybe something for tree-switch-conversion? Yes, would be possible to implement, but I'm not fully convinced it's a common pattern that happens in switch statements :)
It's becoming more common with the use of std::variant and compatible libraries (such as Boost.Variant2.) https://godbolt.org/z/414e6j shows a reduced example.
Related to PR 47253.