[clang] [lld] [llvm] [WIP][IR][Constants] Change the semantic of `ConstantPointerNull` to represent an actual `nullptr` instead of a zero-value pointer (PR #166667)

Alexander Richardson via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 17 10:23:19 PST 2025


================
@@ -1561,6 +1583,24 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
       }
     }
     break;
+  case Instruction::AddrSpaceCast:
+    // A null pointer (`ptr addrspace(N) null` in IR presentation,
+    // `ConstantPointerNull` in LLVM class, not `nullptr` in C/C++) used to
+    // represent a zero-value pointer in the corresponding address space.
+    // Therefore, we can't simply fold an address space cast of a null pointer
+    // from one address space to another, because on some targets, the nullptr
+    // of an address space could be non-zero.
+    //
+    // Recently, the semantic of `ptr addrspace(N) null` is changed to represent
+    // the actual nullptr in the corresponding address space. It can be zero or
+    // non-zero, depending on the target. Therefore, we can fold an address
+    // space cast of a nullptr from one address space to another.
+
+    // If the input is a nullptr, we can fold it to the corresponding
+    // nullptr in the destination address space.
+    if (C->isNullValue())
----------------
arichardson wrote:

Sounds good

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


More information about the llvm-commits mailing list