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 13.0.0) #51602

Closed
llvmbot opened this issue Oct 22, 2021 · 3 comments
Closed

Dead Code Elimination Regression at -O3 (trunk vs 13.0.0) #51602

llvmbot opened this issue Oct 22, 2021 · 3 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 22, 2021

Bugzilla Link 52260
Resolution FIXED
Resolved on Nov 18, 2021 05:17
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @rotateright
Fixed by commit(s) acabad9

Extended Description

cat test.c
void foo(void);

static int a;
static int *b = &a;
static int **c = &b;
static char e;

static unsigned d() {
if (c)
return a;
else
return 1;
}

int main() {
int f = d();
unsigned char g = f;
e = g >> 4;
if (e) {
for (;;) {
if (f)
break;
c = 0;
foo();
}
for (;;)
;
}
}

13.0.0 at -O3 can eliminate the call to foo but trunk cannot:

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

%bb.0:

testb	$-16, a(%rip)
je	.LBB0_2
.p2align	4, 0x90

.LBB0_1: # =>This Inner Loop Header: Depth=1
jmp .LBB0_1
.LBB0_2:
xorl %eax, %eax
retq

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

%bb.0:

pushq	%rax
.cfi_def_cfa_offset 16
cmpq	$0, c(%rip)
movl	$1, %eax
je	.LBB0_2

%bb.1:

movl	a(%rip), %eax

.LBB0_2:
cmpb $16, %al
jae .LBB0_3

%bb.6:

xorl	%eax, %eax
popq	%rcx
.cfi_def_cfa_offset 8
retq

.LBB0_3:
.cfi_def_cfa_offset 16
testl %eax, %eax
je .LBB0_4
.p2align 4, 0x90
.LBB0_5: # =>This Inner Loop Header: Depth=1
jmp .LBB0_5
.p2align 4, 0x90
.LBB0_4: # =>This Inner Loop Header: Depth=1
movq $0, c(%rip)
callq foo
jmp .LBB0_4

clang-trunk -v
clang version 14.0.0 (https://github.com/llvm/llvm-project.git eda2ebd)
Target: x86_64-unknown-linux-gnu
Thread model: posix

f32c0fe introduced this regression

@llvmbot
Copy link
Collaborator Author

llvmbot commented Oct 22, 2021

void foo(void);
static int a[] = {0, 0, 1};
static int b;
static unsigned char c;

int main() {
for (; b >= 0; b--) {
unsigned char d = a[b];
c = d >> 1;
if (c)
foo();
}
}

this test case also triggers the regression

@rotateright
Copy link
Contributor

Still stepping through, so I'm not sure if this is the entire problem, but are missing an icmp canonicalization and/or analysis to recognize this:

https://alive2.llvm.org/ce/z/7-6UPc

declare void @​llvm.assume(i1)
declare i8 @​llvm.ctpop.i8(i8)

define i1 @​src(i32 %x, i8 %C) {
; compare constant must be power-of-2
%pop = call i8 @​llvm.ctpop.i8(i8 %C)
%ispow2 = icmp eq i8 %pop, 1
call void @​llvm.assume(i1 %ispow2)

%t = trunc i32 %x to i8
%tobool = icmp ult i8 %t, %C
ret i1 %tobool
}

define i1 @​tgt(i32 %x, i8 %C) {
; create mask constant for source type
%zextC = zext i8 %C to i32
%maskC = sub i32 256, %zextC

%a = and i32 %x, %maskC
%tobool = icmp eq i32 %a, 0
ret i1 %tobool
}

@rotateright
Copy link
Contributor

I plan to extend this:
https://reviews.llvm.org/rGacabad9ff6bf
...but that should solve the examples in this report.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
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