Skip to content

Commit 97f0e7b

Browse files
committed
[AA] Fix comparison of AliasResults (PR63019)
Comparison between two AliasResults implicitly decayed to comparison of AliasResult::Kind. As a result, MergeAliasResults() ended up considering two PartialAlias results with different offsets as equivalent. Fix this by adding an operator== implementation. To stay compatible with extensive use of comparisons between AliasResult and AliasResult::Kind, add an overload for that as well, which will ignore the offset. In the future, it would probably be a good idea to remove these implicit decays to AliasResult::Kind and add dedicated methods to check for specific AliasResult kinds. Fixes #63019.
1 parent 4d64ffa commit 97f0e7b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

+9
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ class AliasResult {
116116

117117
operator Kind() const { return static_cast<Kind>(Alias); }
118118

119+
bool operator==(const AliasResult &Other) const {
120+
return Alias == Other.Alias && HasOffset == Other.HasOffset &&
121+
Offset == Other.Offset;
122+
}
123+
bool operator!=(const AliasResult &Other) const { return !(*this == Other); }
124+
125+
bool operator==(Kind K) const { return Alias == K; }
126+
bool operator!=(Kind K) const { return !(*this == K); }
127+
119128
constexpr bool hasOffset() const { return HasOffset; }
120129
constexpr int32_t getOffset() const {
121130
assert(HasOffset && "No offset!");

llvm/test/Transforms/GVN/pr63019.ll

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
22
; RUN: opt -S -passes=gvn < %s | FileCheck %s
33

4-
; FIXME: This is a miscompile.
4+
; Make sure the two offsets from the phi don't get merged incorrectly.
55
define i8 @test(i1 %c, i64 %offset, ptr %ptr) {
66
; CHECK-LABEL: define i8 @test
77
; CHECK-SAME: (i1 [[C:%.*]], i64 [[OFFSET:%.*]], ptr [[PTR:%.*]]) {
@@ -18,9 +18,8 @@ define i8 @test(i1 %c, i64 %offset, ptr %ptr) {
1818
; CHECK-NEXT: store i8 0, ptr [[ALLOCA]], align 8
1919
; CHECK-NEXT: [[LOAD1:%.*]] = load i64, ptr [[ALLOCA]], align 8
2020
; CHECK-NEXT: store i64 [[LOAD1]], ptr [[PTR]], align 8
21-
; CHECK-NEXT: [[TMP0:%.*]] = lshr i64 [[LOAD1]], 16
22-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[TMP0]] to i8
23-
; CHECK-NEXT: ret i8 [[TMP1]]
21+
; CHECK-NEXT: [[LOAD2:%.*]] = load i8, ptr [[PHI]], align 1
22+
; CHECK-NEXT: ret i8 [[LOAD2]]
2423
;
2524
start:
2625
%alloca = alloca [8 x i8], align 8

0 commit comments

Comments
 (0)