llvm / llvm/llvm-project

Missed Redundant Bit Store Elimination in the Presence of `store i1 false`

Open
#189,213 7 comments 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following reduced IR is derived from https://github.com/rui314/mold/blob/fc96c1b9efe6d30b4e06fe3a9d789735bce4510e/lib/atomics.h#L41

https://godbolt.org/z/f5GrK87x5
alive2: https://alive2.llvm.org/ce/z/TYt4uD

In both functions of the following reduced IR, the value written by the first store is overwritten by the second store. And src1, which truncates the loaded i8 to i1, always returns false; src2 always returns 0.

**If this behavior is due to special handling of `store i1 false, ptr %0, align 1` in LLVM, I would greatly appreciate any clarification or explanation.**

```llvm
define i1 @src1(ptr %0) {
store i8 0, ptr %0, align 1
store i1 false, ptr %0, align 1
%.0. = load i8, ptr %0, align 1
%2 = trunc i8 %.0. to i1
ret i1 %2
}

define i8 @src2(ptr %0) {
store i8 0, ptr %0, align 1
store i1 false, ptr %0, align 1
%.0. = load i8, ptr %0, align 1
ret i8 %.0.
}
```

Expected:
```llvm
define noundef i1 @src1(ptr writeonly captures(none) initializes((0, 1)) %0) local_unnamed_addr #0 {
store i1 false, ptr %0, align 1
ret i1 false
}

define noundef i8 @src2(ptr writeonly captures(none) initializes((0, 1)) %0) local_unnamed_addr #0 {
store i1 false, ptr %0, align 1
ret i8 0
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reduced IR in the linked Godbolt example and checking the Alive2 proof. Compare the generated output for both functions with the expected forms, then trace the LLVM optimization responsible for redundant-store elimination; done means the overwritten first store is removed and both results are folded as shown.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.