[clang] [lld] [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
Mon Nov 17 06:19:22 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())
----------------
shiltian wrote:
> Is this actually true? Are null values in one address space always null in all others? I imagine this is almost always true, but maybe you could have some where this conversion is not valid?
This is actually the whole point of this PR. The semantic would be, convert the `nullptr` in AS X to AS Y, so it would still be a `nullptr` in AS Y. I don't think the semantic should be convert the value of `nullptr` in AS X to the corresponding pointer value in AS Y.
I'll clarify this in the LangRef.
https://github.com/llvm/llvm-project/pull/166667
More information about the llvm-commits
mailing list