Skip to content

Commit

Permalink
[AA] Fix comparison of AliasResults (PR63019)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed May 31, 2023
1 parent 4d64ffa commit 97f0e7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 9 additions & 0 deletions llvm/include/llvm/Analysis/AliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ class AliasResult {

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

bool operator==(const AliasResult &Other) const {
return Alias == Other.Alias && HasOffset == Other.HasOffset &&
Offset == Other.Offset;
}
bool operator!=(const AliasResult &Other) const { return !(*this == Other); }

bool operator==(Kind K) const { return Alias == K; }
bool operator!=(Kind K) const { return !(*this == K); }

constexpr bool hasOffset() const { return HasOffset; }
constexpr int32_t getOffset() const {
assert(HasOffset && "No offset!");
Expand Down
7 changes: 3 additions & 4 deletions llvm/test/Transforms/GVN/pr63019.ll
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
; RUN: opt -S -passes=gvn < %s | FileCheck %s

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

0 comments on commit 97f0e7b

Please sign in to comment.