[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
Thu Nov 6 11:20:20 PST 2025


================
@@ -1543,6 +1556,13 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
     }
     break;
   case Instruction::IntToPtr:
+    // We can fold it to a null pointer if the input is the nullptr value.
+    if (std::optional<APInt> NullPtrValue = DL.getNullPtrValue(
+            DestTy->getScalarType()->getPointerAddressSpace())) {
+      if ((NullPtrValue->isZero() && C->isZeroValue()) ||
+          (NullPtrValue->isAllOnes() && C->isAllOnesValue()))
+        return Constant::getNullValue(DestTy);
----------------
arichardson wrote:

I wonder if we should introduce a `getNullptrValue()` and eventually deprecate+remove the `getNullValue()` to avoid subtle bugs for address spaces with non-zero null patterns?

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


More information about the llvm-commits mailing list