[SCEV] Don't create SCEVPtrToAddr for unstable pointer representations. - #180718
Merged
Conversation
Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR. This surfaced as part of the discussion in llvm#178861.
Member
|
@llvm/pr-subscribers-llvm-analysis Author: Florian Hahn (fhahn) ChangesConservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR. This surfaced as part of the discussion in Full diff: https://github.com/llvm/llvm-project/pull/180718.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 0603b9feaa860..45f1b543b8fc7 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -1127,6 +1127,12 @@ const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op) {
const SCEV *ScalarEvolution::getPtrToAddrExpr(const SCEV *Op) {
assert(Op->getType()->isPointerTy() && "Op must be a pointer");
+
+ // Treat pointers with unstable representation conservatively, since the
+ // address bits may change.
+ if (DL.hasUnstableRepresentation(Op->getType()))
+ return getCouldNotCompute();
+
Type *Ty = DL.getAddressType(Op->getType());
// Use the rewriter to sink the cast down to SCEVUnknown leaves.
@@ -8175,8 +8181,12 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
return getSCEV(U->getOperand(0));
break;
- case Instruction::PtrToAddr:
- return getPtrToAddrExpr(getSCEV(U->getOperand(0)));
+ case Instruction::PtrToAddr: {
+ const SCEV *IntOp = getPtrToAddrExpr(getSCEV(U->getOperand(0)));
+ if (isa<SCEVCouldNotCompute>(IntOp))
+ return getUnknown(V);
+ return IntOp;
+ }
case Instruction::PtrToInt: {
// Pointer to integer cast is straight-forward, so do model it.
diff --git a/llvm/test/Analysis/ScalarEvolution/ptrtoaddr.ll b/llvm/test/Analysis/ScalarEvolution/ptrtoaddr.ll
index 2de02b8a319e0..82b8302db7cfd 100644
--- a/llvm/test/Analysis/ScalarEvolution/ptrtoaddr.ll
+++ b/llvm/test/Analysis/ScalarEvolution/ptrtoaddr.ll
@@ -89,13 +89,12 @@ define void @ptrtoaddr_of_gep_with_unknown_int(ptr %in, ptr %out0, ptr %offset)
ret void
}
-; FIXME: Currently not handled correctly.
; ptrtoaddr of a pointer with unstable representation should return SCEVUnknown.
define void @ptrtoaddr_unstable(ptr addrspace(2) %in, ptr %out) {
; CHECK-LABEL: 'ptrtoaddr_unstable'
; CHECK-NEXT: Classifying expressions for: @ptrtoaddr_unstable
; CHECK-NEXT: %p = ptrtoaddr ptr addrspace(2) %in to i64
-; CHECK-NEXT: --> (ptrtoaddr ptr addrspace(2) %in to i64) U: full-set S: full-set
+; CHECK-NEXT: --> %p U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @ptrtoaddr_unstable
;
%p = ptrtoaddr ptr addrspace(2) %in to i64
@@ -103,7 +102,6 @@ define void @ptrtoaddr_unstable(ptr addrspace(2) %in, ptr %out) {
ret void
}
-; FIXME: Currently not handled correctly.
; ptrtoaddr of GEP on unstable pointer should return SCEVUnknown.
define void @ptrtoaddr_unstable_gep(ptr addrspace(2) %in, ptr %out) {
; CHECK-LABEL: 'ptrtoaddr_unstable_gep'
@@ -111,7 +109,7 @@ define void @ptrtoaddr_unstable_gep(ptr addrspace(2) %in, ptr %out) {
; CHECK-NEXT: %gep = getelementptr inbounds i8, ptr addrspace(2) %in, i64 42
; CHECK-NEXT: --> (42 + %in) U: full-set S: full-set
; CHECK-NEXT: %p = ptrtoaddr ptr addrspace(2) %gep to i64
-; CHECK-NEXT: --> (42 + (ptrtoaddr ptr addrspace(2) %in to i64)) U: full-set S: full-set
+; CHECK-NEXT: --> %p U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @ptrtoaddr_unstable_gep
;
%gep = getelementptr inbounds i8, ptr addrspace(2) %in, i64 42
@@ -120,13 +118,12 @@ define void @ptrtoaddr_unstable_gep(ptr addrspace(2) %in, ptr %out) {
ret void
}
-; FIXME: Currently not handled correctly.
; ptrtoaddr of nullptr in unstable address space should return SCEVUnknown.
define void @ptrtoaddr_unstable_null(ptr %out) {
; CHECK-LABEL: 'ptrtoaddr_unstable_null'
; CHECK-NEXT: Classifying expressions for: @ptrtoaddr_unstable_null
; CHECK-NEXT: %p = ptrtoaddr ptr addrspace(2) null to i64
-; CHECK-NEXT: --> 0 U: [0,1) S: [0,1)
+; CHECK-NEXT: --> %p U: [0,1) S: [0,1)
; CHECK-NEXT: Determining loop execution counts for: @ptrtoaddr_unstable_null
;
%p = ptrtoaddr ptr addrspace(2) null to i64
|
arichardson
approved these changes
Feb 10, 2026
fhahn
enabled auto-merge (squash)
February 10, 2026 13:17
llvm-sync Bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
Feb 10, 2026
…resentations. (#180718) Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR. This surfaced as part of the discussion in llvm/llvm-project#178861. PR: llvm/llvm-project#180718
markrvmurray
pushed a commit
to markrvmurray/llvm-mc6809
that referenced
this pull request
Jun 14, 2026
…s. (#180718) Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR. This surfaced as part of the discussion in llvm/llvm-project#178861. PR: llvm/llvm-project#180718
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR.
This surfaced as part of the discussion in
#178861.