llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Dead Code Elimination Regression when using __builtin_unreachable (trunk vs 16)

Open thetheodor opened this issue 2 years ago • 0 comments

void foo(void);
static int e, f, h = 1;
static int *g = &f;
static unsigned i = 4;
static char j, k;
static char(a)(char b, int c) { return b >> c; }
static short(d)(short b) {
    if (!(((b) >= 0) && ((b) <= 0))) {
        __builtin_unreachable();
    }
    return 0;
}
static int l() {
    if (i)
        ;
    else {
        h = 0;
        for (; h;) foo();
        j = a(5 & e, f);
        k = j ^ 33;
        d(k);
        e++;
    }
    return *g;
}
int main() {
    d(0);
    *g = l();
    for (; h; h--) i = 0;
}

Clang trunk -O1 does not eliminate the call to foo:

main:                                   # @main
# %bb.0:
	pushq	%rax
	cmpb	$0, i(%rip)
	je	.LBB0_1
# %bb.4:
	movb	$1, h(%rip)
	.p2align	4, 0x90
.LBB0_5:                                # =>This Inner Loop Header: Depth=1
	callq	foo@PLT
	jmp	.LBB0_5
.LBB0_1:
	cmpb	$0, h(%rip)
	jne	.LBB0_3
# %bb.2:
	movb	$1, i(%rip)
	movb	$1, h(%rip)
.LBB0_3:
	xorl	%eax, %eax
	popq	%rcx
	retq
.Lfunc_end0:

Clang 16.0.6 -O1 eliminates the call to foo:

main:                                   # @main
# %bb.0:
	cmpb	$0, h(%rip)
	jne	.LBB0_2
# %bb.1:
	movb	$1, i(%rip)
	movb	$1, h(%rip)
.LBB0_2:
	xorl	%eax, %eax
	retq
.Lfunc_end0:

Bisects to dfb369399d2a54c8dd8752c47ecbf7a8c3c11421 (@nikic)

thetheodor avatar Jun 26 '23 15:06 thetheodor