Skip to content

[SCEV] Don't create SCEVPtrToAddr for unstable pointer representations. - #180718

Merged
fhahn merged 2 commits into
llvm:mainfrom
fhahn:scev-ptrtoaddr-unstable
Feb 10, 2026
Merged

[SCEV] Don't create SCEVPtrToAddr for unstable pointer representations.#180718
fhahn merged 2 commits into
llvm:mainfrom
fhahn:scev-ptrtoaddr-unstable

Conversation

@fhahn

@fhahn fhahn commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR.

This surfaced as part of the discussion in
#178861.

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.
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Feb 10, 2026
@llvmbot

llvmbot commented Feb 10, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-analysis

Author: Florian Hahn (fhahn)

Changes

Conservatively treat unstable pointers as SCEVCouldNotCompute in getPtrToAddrExpr, and return SCEVUnknown when constructing from IR.

This surfaced as part of the discussion in
#178861.


Full diff: https://github.com/llvm/llvm-project/pull/180718.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+12-2)
  • (modified) llvm/test/Analysis/ScalarEvolution/ptrtoaddr.ll (+3-6)
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

@nikic nikic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fhahn
fhahn enabled auto-merge (squash) February 10, 2026 13:17
@fhahn
fhahn merged commit f8d5a00 into llvm:main Feb 10, 2026
9 of 10 checks passed
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
@fhahn
fhahn deleted the scev-ptrtoaddr-unstable branch February 11, 2026 14:50
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants