Skip to content

Conversation

XChy
Copy link
Member

@XChy XChy commented Sep 28, 2025

Fixes #157334.

@XChy XChy requested review from nikic and arichardson September 28, 2025 15:40
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms labels Sep 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2025

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: Hongyu Chen (XChy)

Changes

Fixes #157334.


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

2 Files Affected:

  • (modified) llvm/lib/IR/Instructions.cpp (+2-1)
  • (modified) llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll (+14)
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index daebf447a2107..fb47768974f2b 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -2847,6 +2847,7 @@ unsigned CastInst::isEliminableCastPair(
   // FPTRUNC       >       FloatPt      n/a        FloatPt      n/a
   // FPEXT         <       FloatPt      n/a        FloatPt      n/a
   // PTRTOINT     n/a      Pointer      n/a        Integral   Unsigned
+  // PTRTOADDR    n/a      Pointer      n/a        Integral   Unsigned
   // INTTOPTR     n/a      Integral   Unsigned     Pointer      n/a
   // BITCAST       =       FirstClass   n/a       FirstClass    n/a
   // ADDRSPCST    n/a      Pointer      n/a        Pointer      n/a
@@ -2878,7 +2879,7 @@ unsigned CastInst::isEliminableCastPair(
     { 99,99,99, 2, 2,99,99, 8, 2,99,99,99, 4, 0}, // FPExt          |
     {  1, 0, 0,99,99, 0, 0,99,99,99,99, 7, 3, 0}, // PtrToInt       |
     {  1, 0, 0,99,99, 0, 0,99,99,99,99, 0, 3, 0}, // PtrToAddr      |
-    { 99,99,99,99,99,99,99,99,99,11,99,99,15, 0}, // IntToPtr       |
+    { 99,99,99,99,99,99,99,99,99,11, 0,99,15, 0}, // IntToPtr       |
     {  5, 5, 5, 0, 0, 5, 5, 0, 0,16,16, 5, 1,14}, // BitCast        |
     {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
   };
diff --git a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
index 442089eecfcbb..42814cf6c3682 100644
--- a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
+++ b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
@@ -32,3 +32,17 @@ define i64 @OpenFilter(i64 %x) {
   %r = zext i8 %t to i64
   ret i64 %r
 }
+
+define i64 @ptr2addr1() {
+; CHECK-LABEL: @ptr2addr1(
+; CHECK-NEXT:    ret i64 ptrtoaddr (ptr inttoptr (i64 1 to ptr) to i64)
+;
+  ret i64 ptrtoaddr (ptr getelementptr (i8, ptr null, i64 1) to i64)
+}
+
+define i64 @ptr2addr2() {
+; CHECK-LABEL: @ptr2addr2(
+; CHECK-NEXT:    ret i64 ptrtoaddr (ptr inttoptr (i64 123 to ptr) to i64)
+;
+  ret i64 ptrtoaddr (ptr inttoptr (i64 123 to ptr) to i64)
+}

@XChy XChy changed the title [ConstantFold] Consider inttoptr, ptrtoaddr is valid cast pair [ConstantFold] Fold inttoptr, ptrtoaddr to bitcast Sep 28, 2025
Copy link

github-actions bot commented Sep 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@arichardson arichardson left a comment

Choose a reason for hiding this comment

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

Could you add a few tests where address size < pointer size? Otherwise this looks good to me.

; CHECK-NEXT: ret i32 ptrtoaddr (ptr addrspace(1) inttoptr (i16 -1 to ptr addrspace(1)) to i32)
;
ret i32 ptrtoaddr (ptr addrspace(1) inttoptr (i16 -1 to ptr addrspace(1)) to i32)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, we can make some of these fold, but we should really change the isEliminableCastPair() API to accept DataLayout for that. Leaving these cases alone is fine for now.

@XChy XChy force-pushed the fix-constantfold-inttoptr-ptrtoaddr branch from 42d9399 to 1101f3b Compare September 29, 2025 08:37
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@XChy XChy merged commit f628a54 into llvm:main Sep 29, 2025
9 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[GlobalOpt] Invalid Cast Combination UNREACHABLE executed at /root/llvm-project/llvm/lib/IR/Instructions.cpp:3033!
4 participants