Bug 14721 - jump optimization involving a sibling call within a jump table
Summary: jump optimization involving a sibling call within a jump table
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: code-size, missed-optimization
Depends on:
Blocks: 16996
  Show dependency treegraph
 
Reported: 2004-03-24 17:42 UTC by Kazu Hirata
Modified: 2024-10-24 23:36 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-03-24 17:42:08 UTC
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
Comment 1 Andrew Pinski 2004-03-24 17:48:16 UTC
Confirmed, yes that would be nice.
Comment 3 Steven Bosscher 2019-03-06 08:39:37 UTC
Maybe something for tree-switch-conversion?
Comment 4 Martin Liška 2019-03-06 09:26:39 UTC
(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 :)
Comment 5 Peter Dimov 2021-01-13 01:48:05 UTC
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.
Comment 6 Andrew Pinski 2021-08-19 03:39:18 UTC
Related to PR 47253.