[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:23 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);
----------------
arichardson wrote:
I'm not sure off the top of my head if this is safe for an extending ptrtoint? Does it zero or sign extend?
https://github.com/llvm/llvm-project/pull/166667
More information about the llvm-commits
mailing list