diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp index 86a2edbd8bd41..169ab5a6da445 100644 --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1602,6 +1602,8 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size, // Figure out what objects these things are pointing to if we can. const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth); const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth); + removeRoundTripCasts(O1); + removeRoundTripCasts(O2); // Null values in the default address space don't point to any object, so they // don't alias any other pointer. diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index e576f4899810a..837c6a6caa4b2 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -6661,6 +6661,10 @@ const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) { if (!NewV->getType()->isPointerTy()) return V; V = NewV; + } else if (Operator::getOpcode(V) == Instruction::IntToPtr && + Operator::getOpcode(cast(V)->getOperand(0)) == + Instruction::PtrToInt) { + V = cast(cast(V)->getOperand(0))->getOperand(0); } else if (auto *GA = dyn_cast(V)) { if (GA->isInterposable()) return V; diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp index 129052fbe08b8..b40f38d464ed9 100644 --- a/llvm/unittests/Analysis/ValueTrackingTest.cpp +++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp @@ -3350,6 +3350,18 @@ TEST_F(ValueTrackingTest, ComputeConstantRange) { } } +TEST_F(ValueTrackingTest, GetUnderlyingObject) { + parseAssembly(R"( + @globalmem = external global i8 + define void @test() { + %A = getelementptr i8, ptr @globalmem, i64 0 + %A2 = getelementptr i8, ptr inttoptr (i32 ptrtoint (ptr @globalmem to i32) to ptr), i64 0 + ret void + } + )"); + EXPECT_EQ(getUnderlyingObject(A), getUnderlyingObject(A2)); +} + struct FindAllocaForValueTestParams { const char *IR; bool AnyOffsetResult;