diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1699,6 +1699,7 @@ return Result; } else if (const GEPOperator *GV2 = dyn_cast(V2)) { AliasResult Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI); + Result.swap(); if (Result != AliasResult::MayAlias) return Result; } @@ -1709,6 +1710,7 @@ return Result; } else if (const PHINode *PN = dyn_cast(V2)) { AliasResult Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI); + Result.swap(); if (Result != AliasResult::MayAlias) return Result; } @@ -1719,6 +1721,7 @@ return Result; } else if (const SelectInst *S2 = dyn_cast(V2)) { AliasResult Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI); + Result.swap(); if (Result != AliasResult::MayAlias) return Result; } diff --git a/llvm/unittests/Analysis/AliasAnalysisTest.cpp b/llvm/unittests/Analysis/AliasAnalysisTest.cpp --- a/llvm/unittests/Analysis/AliasAnalysisTest.cpp +++ b/llvm/unittests/Analysis/AliasAnalysisTest.cpp @@ -333,6 +333,39 @@ EXPECT_EQ(4, AR.getOffset()); } +// Check that swapping the order of parameters to `AA.alias()` changes offset +// sign and that the sign is such that FirstLoc + Offset == SecondLoc. +TEST_F(AliasAnalysisTest, PartialAliasOffsetSign) { + LLVMContext C; + SMDiagnostic Err; + std::unique_ptr M = parseAssemblyString(R"( + define void @f(i64* %p) { + %L1 = load i64, i64* %p + %p.i8 = bitcast i64* %p to i8* + %q = getelementptr i8, i8* %p.i8, i32 1 + %L2 = load i8, i8* %q + ret void + } + )", + Err, C); + + if (!M) + Err.print("PartialAliasOffsetSign", errs()); + + Function *F = M->getFunction("f"); + const auto Loc1 = MemoryLocation::get(getInstructionByName(*F, "L1")); + const auto Loc2 = MemoryLocation::get(getInstructionByName(*F, "L2")); + + auto &AA = getAAResults(*F); + + auto AR = AA.alias(Loc1, Loc2); + EXPECT_EQ(AR, AliasResult::PartialAlias); + EXPECT_EQ(1, AR.getOffset()); + + AR = AA.alias(Loc2, Loc1); + EXPECT_EQ(AR, AliasResult::PartialAlias); + EXPECT_EQ(-1, AR.getOffset()); +} class AAPassInfraTest : public testing::Test { protected: LLVMContext C;