Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dead Code Elimination Regression at -O3 (trunk vs. 14.0.4) #56046

Closed
thetheodor opened this issue Jun 15, 2022 · 4 comments
Closed

Dead Code Elimination Regression at -O3 (trunk vs. 14.0.4) #56046

thetheodor opened this issue Jun 15, 2022 · 4 comments

Comments

@thetheodor
Copy link
Contributor

static short a;
static int b;

void foo(void);
void bar9_(void);
int main() {
    char c;
    b = a = 23;
    for (; b <= 1;)
        a = 1;
    bar9_();
    c = a >= 18 || 2 > 2 >> a ? 1 : 1 << a;
    if (c == 0)
        foo();
}

llvm-4c2bccfda3892ae13e97b6bfdbc99ec8cf5d095d (trunk) -O3 can not eliminate foo but llvm-llvmorg-14.0.4 -O3 can.

Target: x86_64-unknown-linux-gnu


llvm-4c2bccfda3892ae13e97b6bfdbc99ec8cf5d095d (trunk) -O3 [-emit-llvm] -S -o /dev/stdout case.c

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movb	$1, a(%rip)
	callq	bar9_@PLT
	xorl	%eax, %eax
	cmpb	$0, a(%rip)
	movl	$23, %ecx
	cmovel	%eax, %ecx
	movl	$2, %eax
	shrl	%cl, %eax
	addl	$-8, %ecx
	cmpw	$9, %cx
	ja	.LBB0_3
# %bb.1:
	cmpl	$2, %eax
	jb	.LBB0_3
# %bb.2:
	callq	foo@PLT
.LBB0_3:
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main


llvm-llvmorg-14.0.4 -O3 [-emit-llvm] -S -o /dev/stdout case.c

Reduced assembly

main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movb	$1, a(%rip)
	callq	bar9_
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main


Bisection

Bisected to: c0e06c7
Committed by: @nikic

@rotateright
Copy link
Contributor

I'm guessing this will be solved if we add an icmp fold that is similar to some recent patches for shift+mask:
https://alive2.llvm.org/ce/z/fEDQXU

@rotateright
Copy link
Contributor

On 2nd thought, we can generalize further than the above. As long as we're shifting a power-of-2 constant, the compare constant can be almost anything - it's just a check of leading zeros:
https://alive2.llvm.org/ce/z/TyGiT0
...and there should be a sibling fold for 'ult'.

rotateright added a commit that referenced this issue Jun 19, 2022
@rotateright
Copy link
Contributor

Should be fixed with:
0399473

@fhahn
Copy link
Contributor

fhahn commented Jul 7, 2022

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants