From 97f0e7b06e6b76fd85fb81b8c12eba2255ff1742 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 31 May 2023 14:53:44 +0200 Subject: [PATCH] [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. --- llvm/include/llvm/Analysis/AliasAnalysis.h | 9 +++++++++ llvm/test/Transforms/GVN/pr63019.ll | 7 +++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/Analysis/AliasAnalysis.h b/llvm/include/llvm/Analysis/AliasAnalysis.h index 68956bcf388ad..737aafcc3f60c 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(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 cf3a4666a06e9..f2628a32be5ed 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