Dead store elimination deficient
| 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, #​1
ldr r2, [r1]
add r2, r2, #​1
str r2, [r1] <--- Unnecessary store into "x"
str r3, [r0]
ldr r0, [r1] <--- Unnecessary reload of "x"
add r0, r0, #​1
str r0, [r1]
mov pc, lr
Fixed in LLVM v3.8 (run clang/opt with -O1 or above).
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.
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?
Globals mod/ref AA should handle this because the address of x doesn't escape.
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;
- when *p were not int, but (float *p)
- 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}
This is typical problem seen in many EEMBC passes. It is solved by adding additional GVN call in the middle of pass pipeline.