Bug 43233 - x86 flags not combined across blocks
Summary: x86 flags not combined across blocks
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.5.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2010-03-02 19:17 UTC by astrange+gcc@gmail.com
Modified: 2021-07-26 08:29 UTC (History)
1 user (show)

See Also:
Host: x86_64-*-*, aarch64
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description astrange+gcc@gmail.com 2010-03-02 19:17:06 UTC
Source:
int g1,g2,g3;

int f1(int a, int b)
{
    a &= 1;
    
    if (a) return g1;
    return g2;
}

int f2(int a, int b)
{
    a &= 1;
    
    if (b)
        g3++;
    
    if (a) return g1;
    return g2;
}

Compiled with:
> gcc -O3 -fomit-frame-pointer -S and_flags.c

f1 is ok but f2 generates this:
_f2:
	andl	$1, %edi <-- #1
	testl	%esi, %esi
	je	L7
	movq	_g3@GOTPCREL(%rip), %rax
	incl	(%rax)
L7:
	testl	%edi, %edi <-- #2
	jne	L10
	movq	_g2@GOTPCREL(%rip), %rax
	movl	(%rax), %eax
	ret
	.align 4,0x90
L10:
	movq	_g1@GOTPCREL(%rip), %rax
	movl	(%rax), %eax
	ret

The andl and testl should be folded into one andl.

Code is reduced from ffmpeg h264 decoder. It's easy to work around by reordering source lines, so not too important.
Comment 1 Steven Bosscher 2010-05-13 23:33:29 UTC
Confirmed, this is a case where a def could be sunk closer to its first use.
Comment 2 Andrew Pinski 2021-07-26 08:29:34 UTC
This also happens on aarch64 where we could use tbnz.