llvm / llvm/llvm-project

dead stack stores after expand-memcmp

Open
#180,991 1 comment 0 reactions 0 assignees View on GitHub
llvm:codegen missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://codingmarginalia.blogspot.com/2026/02/both-gcc-and-clang-generate.html mentions that
```c++
#include

static constexpr int arraySize = 1;

bool isAllZeros (const std::array &array) {
std::array allZeros {};

return array == allZeros;
}
```
produces a dead store to the stack for optimized builds:
```asm
isAllZeros: # @isAllZeros
movq $0, -8(%rsp)
cmpq $0, (%rdi)
sete %al
retq
```

https://godbolt.org/z/Kb736onb4
```llvm
\*\* IR Dump After Expand memcmp() to load/stores (expand-memcmp) \*\*
; Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef zeroext i1 @isAllZeros(ptr noundef nonnull readonly align 4 captures(none) dereferenceable(8) %0) local_unnamed_addr #0 {
%2 = alloca %"struct.std::array", align 8
call void @llvm.lifetime.start.p0(ptr %2)
store i64 0, ptr %2, align 8
%3 = load i64, ptr %0, align 4
%4 = load i64, ptr %2, align 8
%5 = icmp ne i64 %3, %4
%6 = zext i1 %5 to i32
%7 = icmp eq i32 %6, 0
call void @llvm.lifetime.end.p0(ptr %2)
ret i1 %7
}
```
I wonder if `expand-memcmp` should be doing a round of store-to-load forwarding?

Contributor guide

Open the contributing guide

Research direction

Start with the C++ reproducer and the linked Godbolt IR dump after the expand-memcmp stage. Read the expand-memcmp implementation and reproduce the optimized assembly to determine whether the stack store can be forwarded or eliminated. Done means reaching a clear behavior decision and confirming it against the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.