Issue 101680
Summary [GVN] Redundant `memmove` in zero-initialized array not simplified to zero
Labels missed-optimization, llvm:GVN
Assignees
Reporter antoniofrighetto
    The following C function:
```cpp
#define SIZE 25

int test() {
 int x[SIZE] = {0};

    for (int i = 0; i < SIZE - 1; ++i)
 x[i] = x[i + 1];
    
    return x[0];
}
```
Is lowered to the following IR with `-O2` by latest clang:
```llvm
define i32 @test() {
entry:
  %x = alloca [25 x i32], align 16
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100) %x, i8 0, i64 100, i1 false)
  %x.4.sroa_idx9 = getelementptr inbounds i8, ptr %x, i64 4
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(96) %x, ptr noundef nonnull align 4 dereferenceable(96) %x.4.sroa_idx9, i64 96, i1 false)
  %x.0.x.0. = load i32, ptr %x, align 16
  ret i32 %x.0.x.0.
}
```
Failure at reasoning on the memmove, which here happens not to change the content of the array, leads to a missed return zero simplification that GCC seems to catch via FRE.

Godbolt: https://godbolt.org/z/a4PejT3Mr.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to