[llvm-branch-commits] [llvm] [IR] Introduce the `ptrtoaddr` instruction (PR #139357)

Nikita Popov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 6 01:18:09 PDT 2025


================
@@ -1482,6 +1482,20 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
   switch (Opcode) {
   default:
     llvm_unreachable("Missing case");
+  case Instruction::PtrToAddr:
+    if (auto *GEP = dyn_cast<GEPOperator>(C)) {
+      // For now just handle the basic case of GEPs on NULL for ptrtoaddr.
+      // (ptrtoaddr (gep null, x)) -> x
+      // (ptrtoaddr (gep (gep null, x), y) -> x + y, etc.
+      unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
+      APInt BaseOffset(BitWidth, 0);
+      auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets(
+          DL, BaseOffset, /*AllowNonInbounds=*/true));
+      if (Base->isNullValue()) {
+        return ConstantInt::get(C->getContext(), BaseOffset);
+      }
+    }
----------------
nikic wrote:

I think this code is untested? Probably best to leave it for a followup.

https://github.com/llvm/llvm-project/pull/139357


More information about the llvm-branch-commits mailing list