[llvm-branch-commits] [llvm] 42f2e6e - [AA] Fix comparison of AliasResults (PR63019)

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jun 1 09:45:05 PDT 2023


Author: Nikita Popov
Date: 2023-06-01T09:44:36-07:00
New Revision: 42f2e6efd84464e2d02f2524b7feecb00efd6ab7

URL: https://github.com/llvm/llvm-project/commit/42f2e6efd84464e2d02f2524b7feecb00efd6ab7
DIFF: https://github.com/llvm/llvm-project/commit/42f2e6efd84464e2d02f2524b7feecb00efd6ab7.diff

LOG: [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 https://github.com/llvm/llvm-project/issues/63019.

(cherry picked from commit 97f0e7b06e6b76fd85fb81b8c12eba2255ff1742)

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/AliasAnalysis.h
    llvm/test/Transforms/GVN/pr63019.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/AliasAnalysis.h b/llvm/include/llvm/Analysis/AliasAnalysis.h
index 953e15e358f1..8ac6e7dac63e 100644
--- a/llvm/include/llvm/Analysis/AliasAnalysis.h
+++ b/llvm/include/llvm/Analysis/AliasAnalysis.h
@@ -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!");

diff  --git a/llvm/test/Transforms/GVN/pr63019.ll b/llvm/test/Transforms/GVN/pr63019.ll
index cf3a4666a06e..f2628a32be5e 100644
--- a/llvm/test/Transforms/GVN/pr63019.ll
+++ b/llvm/test/Transforms/GVN/pr63019.ll
@@ -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:%.*]]) {
@@ -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


        


More information about the llvm-branch-commits mailing list