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 12.0.1) #51444

Closed
llvmbot opened this issue Oct 7, 2021 · 4 comments
Closed

Dead Code Elimination Regression at -O3 (trunk vs 12.0.1) #51444

llvmbot opened this issue Oct 7, 2021 · 4 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 7, 2021

Bugzilla Link 52102
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @topperc,@rotateright

Extended Description

cat case.c bisections
static int a, b = -2, c[1];
void foo(void);
int main() {
if ((c[0] &= (1 % b == 0)) > 1)
foo();
b = 1;
}

trunk can no longer eliminate foo:

clang-12 -v
clang version 12.0.1 (https://github.com/llvm/llvm-project.git fed4134)
Target: x86_64-unknown-linux-gnu

clang-12 case.c -S -O3 -o /dev/stdout
...

main: # @​main
.cfi_startproc

%bb.0:

movzbl	b(%rip), %eax
andl	%eax, c.0(%rip)
movb	$1, b(%rip)
xorl	%eax, %eax
retq

clang-trunk -v
clang version 14.0.0 (https://github.com/llvm/llvm-project.git 73346f5)
Target: x86_64-unknown-linux-gnu

clang-trunk case.c -S -O3 -o /dev/stdout
...
main: # @​main
.cfi_startproc

%bb.0:

pushq	%rax
.cfi_def_cfa_offset 16
movzbl	b(%rip), %eax
andl	c.0(%rip), %eax
movl	%eax, c.0(%rip)
cmpl	$2, %eax
jb	.LBB0_2

%bb.1:

callq	foo

.LBB0_2:
movb $1, b(%rip)
xorl %eax, %eax
popq %rcx
.cfi_def_cfa_offset 8
retq

183bbad introduced this regression

@rotateright
Copy link
Contributor

https://alive2.llvm.org/ce/z/WpYtHs

define i8 @​src(i1 %b) {
%s = select i1 %b, i8 1, i8 -2
%r = srem i8 1, %s
ret i8 %r
}

define i8 @​tgt(i1 %b) {
%s = select i1 %b, i8 0, i8 1
ret i8 %s
}

I'm not sure if this is the whole problem, but we seem to be missing an instcombine fold to allow "FoldOpIntoSelect" for srem with constant numerator.

@rotateright
Copy link
Contributor

Current IR for the C source:
https://godbolt.org/z/5hrYeTG4T

@rotateright
Copy link
Contributor

Note that we can't hoist integer div/rem above a select in general because that could leak poison or cause UB:
https://alive2.llvm.org/ce/z/smpA4p

So we'll need some extra safety checks to make that transform work.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@rotateright
Copy link
Contributor

Should be fixed with:
f65be72

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

No branches or pull requests

2 participants