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

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 6 11:50:08 PST 2025


================
@@ -1497,6 +1497,19 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
     llvm_unreachable("Missing case");
   case Instruction::PtrToAddr:
   case Instruction::PtrToInt:
+    // If the input is a nullptr, we can fold it to the corresponding nullptr
+    // value.
+    if (Opcode == Instruction::PtrToInt && C->isNullValue()) {
+      if (std::optional<APInt> NullPtrValue = DL.getNullPtrValue(
+              C->getType()->getScalarType()->getPointerAddressSpace())) {
+        if (NullPtrValue->isZero())
+          return Constant::getZeroValue(DestTy);
+        else if (NullPtrValue->isAllOnes())
+          return Constant::getAllOnesValue(DestTy);
----------------
shiltian wrote:

The LLVM LangRef says:

> If value is smaller than ty2 then a zero extension is done. If value is larger than ty2 then a truncation is done.

So I think this should be okay?

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


More information about the llvm-commits mailing list