Issue 241475
Summary Globals referenced only by llvm.assume are kept and emitted
Labels missed-optimization
Assignees
Reporter earnol
    Reproduced with clang built from main at d9549814577b.

```c
int a1(const char *p) { __builtin_assume(p != "ASSUMESTR"); return *p; }
int main(void) { return a1("MAINSTR"); }
```

`clang --target=x86_64-linux-gnu -O2 -S`:

```asm
a1:
        movsbl  (%rdi), %eax
        retq
main:
 movl    $77, %eax
        retq
        .section .rodata.str1.1,"aMS",@progbits,1
.L.str:
        .asciz "ASSUMESTR"
.L.str.1:
        .asciz  "MAINSTR"
```

No instruction refers to either string, yet both are emitted. This happens at every optimization level (-O0 to -O3, -Os, -Oz) and with `-flto`. After -O2, the only users left are `llvm.assume` operands:

```llvm
define dso_local i32 @main() {
  %cmp.i = icmp ne ptr @.str.1, @.str
  tail call void @llvm.assume(i1 %cmp.i)
  ret i32 77
}
```

GlobalDCE, including whole-program GlobalDCE under LTO, treats these operands as real uses. Instruction selection then drops the assume, so the data is emitted with nothing referring to it. The `icmp` isn't folded either, because both globals are `unnamed_addr` and could be merged.

`-Wl,--gc-sections` with lld removes the strings, because lld tracks liveness per piece in `SHF_MERGE` sections. Without section GC, or on targets and linkers that place such data in ordinary sections, the strings stay in the final binary.

**Expected:** a global whose only users are `llvm.assume` operands shouldn't be kept alive. For example, such assumes, or their operand chains, could be dropped before the final GlobalDCE.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to