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

https://godbolt.org/z/z3d5s1EdY

Given the following code:

void foo(void);
static struct {
    int b;
} f;
static int c, d = 3;
static int *g = &c;
static void h();
static unsigned i(int, int *, int *);
static int j() {
    h();
    d = 0;
    return 1;
}
static void h() { i(8, &d, g); }
static unsigned i(int k, int *, int *l) {
    short a;
    c = 0;
    for (;; ++c) {
        int b;
        if (k) {
            int e = a >= 0;
            if (e) return 0;
            a = 0;
            for (; a >= 0; a--) k = 0;
            if (*l) continue;
            __builtin_unreachable();
        } else
            foo();
        return f.b;
    }
}
int main() { d || j(); }

clang-trunk -O3 does not eliminate the call to foo:

main:                                   # @main
# %bb.0:
	cmpb	$1, d(%rip)
	jne	.LBB0_2
# %bb.1:
	pushq	%rax
	callq	foo@PLT
	movb	$1, d(%rip)
	addq	$8, %rsp
.LBB0_2:
	xorl	%eax, %eax
	retq
.Lfunc_end0:

clang-16.0.6 -O3 eliminates the call to foo:

main:                                   # @main
# %bb.0:
	xorl	%eax, %eax
	retq
.Lfunc_end0:

Bisects to 15d5c59280c9943b23a372ca5fdd8a88ce930514 (@vfdff )

thetheodor avatar Jun 28 '23 10:06 thetheodor