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

Dead store elimination deficient

Open llvmbot opened this issue 15 years ago • 6 comments

Bugzilla Link 11032
Resolution FIXED
Resolved on Dec 01, 2019 03:15
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @asl,@atrick,@lattner,@ekatz

Extended Description

Given the following test case:

int foo(int *p){ static int x = 0; x++; *p = 1; x++; return x; }

dead store elimination fails to observe that the assignment into "*p" cannot possibly affect "x" because the address of "x" is never taken. Thus, LLVM generates an unnecessary store into "x" and an unnecessary reload of "x". GCC does not generate the extra code.

This issue affects the performance and code size of the EEMBC "matrix01" benchmark.

Here is the ARM output at -O3 that demonstrates the problem:

    ldr     r1, .LCPI0_0    <-- address of "x"
    mov     r3, #&#8203;1
    ldr     r2, [r1]
    add     r2, r2, #&#8203;1
    str     r2, [r1]      <--- Unnecessary store into "x"
    str     r3, [r0]
    ldr     r0, [r1]      <--- Unnecessary reload of "x"
    add     r0, r0, #&#8203;1
    str     r0, [r1]
    mov     pc, lr

llvmbot avatar Sep 28 '11 18:09 llvmbot

Fixed in LLVM v3.8 (run clang/opt with -O1 or above).

ekatz avatar Dec 01 '19 11:12 ekatz

Yes, we should be running at at -O3 at least, and possibly also -O2 (should be quantified). DanG would be the one to know how feasible that is though.

lattner avatar Sep 28 '11 23:09 lattner

Globals mod/ref AA should handle this because the address of x doesn't escape.

Oh yeah, it could be resolved with lto :p

@​foo.x = internal unnamed_addr global i32 0, align 4

define i32 @​foo(i32* nocapture %p) nounwind { %1 = load i32* @​foo.x, align 4, !tbaa !​0 store i32 1, i32* %p, align 4, !tbaa !​0 %2 = add i32 %1, 2 store i32 %2, i32* @​foo.x, align 4, !tbaa !​0 ret i32 %2 }

Shall we run globalsmodref-aa in module compilation?

llvmbot avatar Sep 28 '11 23:09 llvmbot

Globals mod/ref AA should handle this because the address of x doesn't escape.

lattner avatar Sep 28 '11 23:09 lattner

I think it would be alias analysis issue.

llvm does not have knowledge that "static int x" should be private and not escaped out in the function. It could be resolved if "static int x and int *p do not alias". Consider a few cases;

  1. when *p were not int, but (float *p)
  2. when p were marked as restricted, (int *restrict p)

from http://llvm.org/demo/

@​foo.x = internal unnamed_addr global i32 0, align 4

define i32 @​foo(i32* nocapture %p) nounwind { %1 = load i32* @​foo.x, align 4, !tbaa !​0 %2 = add nsw i32 %1, 1 store i32 %2, i32* @​foo.x, align 4, !tbaa !​0 store i32 1, i32* %p, align 4, !tbaa !​0 %3 = load i32* @​foo.x, align 4, !tbaa !​0 %4 = add nsw i32 %3, 1 store i32 %4, i32* @​foo.x, align 4, !tbaa !​0 ret i32 %4 }

!​0 = metadata !{metadata !"int", metadata !​1}

llvmbot avatar Sep 28 '11 22:09 llvmbot

This is typical problem seen in many EEMBC passes. It is solved by adding additional GVN call in the middle of pass pipeline.

asl avatar Sep 28 '11 21:09 asl