[llvm-branch-commits] [llvm] [mlir] [WIP][IR][Constants] Change the semantic of `ConstantPointerNull` to represent an actual `nullptr` instead of a zero-value pointer (PR #183215)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Feb 25 00:49:18 PST 2026


================
@@ -146,19 +146,37 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
     return UndefValue::get(DestTy);
   }
 
-  if (V->isNullValue() && !DestTy->isX86_AMXTy() &&
-      opc != Instruction::AddrSpaceCast) {
-    // If the source or destination involves pointers and DL tells us that
-    // null is not zero for the relevant address space, we cannot fold here.
-    // Defer to the DL-aware folding in Analysis/ConstantFolding.cpp.
-    if (DL) {
-      bool SrcIsPtr = V->getType()->isPtrOrPtrVectorTy();
-      bool DstIsPtr = DestTy->isPtrOrPtrVectorTy();
-      if (SrcIsPtr || DstIsPtr) {
-        unsigned AS = SrcIsPtr ? V->getType()->getPointerAddressSpace()
-                               : DestTy->getPointerAddressSpace();
-        if (!DL->isNullPointerAllZeroes(AS))
+  if (V->isNullValue() && !DestTy->isX86_AMXTy()) {
+    bool SrcIsPtr = V->getType()->isPtrOrPtrVectorTy();
+    bool DstIsPtr = DestTy->isPtrOrPtrVectorTy();
+    if (SrcIsPtr || DstIsPtr) {
+      // addrspacecast of semantic null -> semantic null in target AS.
+      // This is always valid regardless of bit patterns.
+      if (opc == Instruction::AddrSpaceCast)
+        return Constant::getNullValue(DestTy);
+      // For other pointer casts (inttoptr, ptrtoint), the bit representation
+      // of null matters. Without DataLayout we cannot determine whether null
+      // is zero for this address space. Defer to the DL-aware fold in
+      // ConstantFoldCastOperand.
+      if (!DL)
+        return nullptr;
+      unsigned AS = SrcIsPtr ? V->getType()->getPointerAddressSpace()
+                             : DestTy->getPointerAddressSpace();
+      if (!DL->isNullPointerAllZeroes(AS)) {
+        // ptrtoint(ConstantPointerNull): fold to the null pointer's actual
+        // integer bit pattern (non-zero for this AS).
+        if (isa<ConstantPointerNull>(V)) {
+          const APInt &NullVal = DL->getNullPtrValue(AS);
+          return ConstantInt::get(
+              DestTy, NullVal.zextOrTrunc(DestTy->getIntegerBitWidth()));
----------------
arsenm wrote:

Ditto, why is this zext? 

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


More information about the llvm-branch-commits mailing list