llvm / llvm/llvm-project

Missed Redundant Load Elimination Despite TBAA-Proven NoAlias

Open
#189,168 0 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/llvm/llvm-project/blob/3b76b85b15a3e7aa004814944f6237f131b95961/llvm/include/llvm/ADT/SmallPtrSet.h#L170

https://godbolt.org/z/1E587qejY

In the reduced IR, `opt -passes='aa-eval' -evaluate-aa-metadata -print-all-alias-modref-info`:
`NoAlias: store ptr null, ptr %0, align 8, !tbaa !7 <-> store i32 0, ptr %1, align 4, !tbaa !0`,
which means the store to `%0` cannot affect `%1`, so the final load from `%1` always reads 0 and can be eliminated.

**If my understanding of the aliasing result is incorrect, I would greatly appreciate any clarification.**

```llvm
define i32 @_ZN4llvm19SmallPtrSetImplBase10insert_impEPKv(ptr %0, ptr %1) {
store i32 0, ptr %1, align 4, !tbaa !0
store ptr null, ptr %0, align 8, !tbaa !7
%3 = load i32, ptr %1, align 4
ret i32 %3
}

!0 = !{!1, !5, i64 12}
!1 = !{!"_ZTSN4llvm19SmallPtrSetImplBaseE", !2, i64 0, !5, i64 8, !5, i64 12, !5, i64 16, !6, i64 20}
!2 = !{!"any pointer", !3, i64 0}
!3 = !{!"omnipotent char", !4, i64 0}
!4 = !{!"Simple C++ TBAA"}
!5 = !{!"int", !3, i64 0}
!6 = !{!"bool", !3, i64 0}
!7 = !{!2, !2, i64 0}
```

Expected:
```llvm
define i32 @tgt(ptr %0, ptr %1) {
store i32 0, ptr %1, align 4
store ptr null, ptr %0, align 8
ret i32 0
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.