[llvm] [ValueTracking] Allow getUnderlyingPointer to look through inttoptr/ptrtoint round trip casts (PR #146432)
Drew Kersnar via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 1 08:20:38 PDT 2025
https://github.com/dakersnar updated https://github.com/llvm/llvm-project/pull/146432
>From f716a09141b201df574b91b627d50b101c6c9d91 Mon Sep 17 00:00:00 2001
From: Drew Kersnar <dkersnar at nvidia.com>
Date: Mon, 30 Jun 2025 22:36:56 +0000
Subject: [PATCH 1/2] [ValueTracking] Allow getUnderlyingPointer to look
through inttoptr/ptrtoint round trip casts
---
llvm/lib/Analysis/ValueTracking.cpp | 4 ++++
llvm/unittests/Analysis/ValueTrackingTest.cpp | 12 ++++++++++++
2 files changed, 16 insertions(+)
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<Operator>(V)->getOperand(0)) ==
+ Instruction::PtrToInt) {
+ V = cast<Operator>(cast<Operator>(V)->getOperand(0))->getOperand(0);
} else if (auto *GA = dyn_cast<GlobalAlias>(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;
>From 0b0e08cc0c1cc1a8774f5f16b180e49e20b6c4b4 Mon Sep 17 00:00:00 2001
From: Drew Kersnar <dkersnar at nvidia.com>
Date: Tue, 1 Jul 2025 15:20:04 +0000
Subject: [PATCH 2/2] Pseudocode for alternate suggestion
---
llvm/lib/Analysis/BasicAliasAnalysis.cpp | 2 ++
1 file changed, 2 insertions(+)
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.
More information about the llvm-commits
mailing list