[llvm] [DataLayout] Add isNullPointerAllZeroes (PR #165314)
Robert Imschweiler via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 27 14:21:10 PDT 2025
https://github.com/ro-i updated https://github.com/llvm/llvm-project/pull/165314
>From 1721831722e6614c4b7be6b5b66ab4131776facd Mon Sep 17 00:00:00 2001
From: Robert Imschweiler <robert.imschweiler at amd.com>
Date: Mon, 27 Oct 2025 15:41:10 -0500
Subject: [PATCH 1/2] [DataLayout] Add isNullPointerAllZeroes
Add a function to check for a given address space if its null pointer
has an all-zero bit representation.
---
llvm/include/llvm/IR/DataLayout.h | 6 ++++++
llvm/unittests/IR/DataLayoutTest.cpp | 9 +++++++++
2 files changed, 15 insertions(+)
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index 56fc749838ef9..b8dcd6647f877 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -398,6 +398,12 @@ class DataLayout {
PS.HasExternalState;
}
+ /// Returns if the null pointer for this address space has an all-zero bit
+ /// representation.
+ bool isNullPointerAllZeroes(unsigned AddrSpace) const {
+ return AddrSpace == 0;
+ }
+
/// Returns whether this address space has an "unstable" pointer
/// representation. The bitwise pattern of such pointers is allowed to change
/// in a target-specific way. For example, this could be used for copying
diff --git a/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp
index 9ca88141ca0eb..d5336a89c5859 100644
--- a/llvm/unittests/IR/DataLayoutTest.cpp
+++ b/llvm/unittests/IR/DataLayoutTest.cpp
@@ -700,6 +700,15 @@ TEST(DataLayout, NonIntegralHelpers) {
}
}
+TEST(DataLayoutTest, IsNullPointerAllZeroes) {
+ EXPECT_TRUE(DataLayout("").isNullPointerAllZeroes(0));
+ EXPECT_FALSE(DataLayout("").isNullPointerAllZeroes(1));
+ EXPECT_TRUE(DataLayout("p:32:32").isNullPointerAllZeroes(0));
+ EXPECT_FALSE(DataLayout("p:32:32").isNullPointerAllZeroes(1));
+ EXPECT_TRUE(DataLayout("p:64:64").isNullPointerAllZeroes(0));
+ EXPECT_FALSE(DataLayout("p:64:64").isNullPointerAllZeroes(1));
+}
+
TEST(DataLayoutTest, CopyAssignmentInvalidatesStructLayout) {
DataLayout DL1 = cantFail(DataLayout::parse("p:32:32"));
DataLayout DL2 = cantFail(DataLayout::parse("p:64:64"));
>From 9113b53de54d2c3f5f6ad9e9e7c0f8cc96231ed7 Mon Sep 17 00:00:00 2001
From: Robert Imschweiler <robert.imschweiler at amd.com>
Date: Mon, 27 Oct 2025 16:20:09 -0500
Subject: [PATCH 2/2] move to getNullPointerValue
Co-authored-by: Alexander Richardson <alexrichardson at google.com>
---
llvm/include/llvm/IR/DataLayout.h | 12 ++++++++----
llvm/unittests/IR/DataLayoutTest.cpp | 14 +++++++-------
2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index b8dcd6647f877..a2106dd7c4dd0 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -398,10 +398,14 @@ class DataLayout {
PS.HasExternalState;
}
- /// Returns if the null pointer for this address space has an all-zero bit
- /// representation.
- bool isNullPointerAllZeroes(unsigned AddrSpace) const {
- return AddrSpace == 0;
+ /// Return the bit value of the null pointer for the given address space.
+ std::optional<APInt> getNullPointerValue(unsigned AS) const {
+ // Address space zero is currently defined to always have an all-zero null
+ // pointer representation, the others are target-specific and will require a
+ // data layout property (work-in-progress).
+ if (AS == 0)
+ return APInt::getZero(getPointerSizeInBits(AS));
+ return std::nullopt;
}
/// Returns whether this address space has an "unstable" pointer
diff --git a/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp
index d5336a89c5859..d82e31c6dca6c 100644
--- a/llvm/unittests/IR/DataLayoutTest.cpp
+++ b/llvm/unittests/IR/DataLayoutTest.cpp
@@ -700,13 +700,13 @@ TEST(DataLayout, NonIntegralHelpers) {
}
}
-TEST(DataLayoutTest, IsNullPointerAllZeroes) {
- EXPECT_TRUE(DataLayout("").isNullPointerAllZeroes(0));
- EXPECT_FALSE(DataLayout("").isNullPointerAllZeroes(1));
- EXPECT_TRUE(DataLayout("p:32:32").isNullPointerAllZeroes(0));
- EXPECT_FALSE(DataLayout("p:32:32").isNullPointerAllZeroes(1));
- EXPECT_TRUE(DataLayout("p:64:64").isNullPointerAllZeroes(0));
- EXPECT_FALSE(DataLayout("p:64:64").isNullPointerAllZeroes(1));
+TEST(DataLayoutTest, GetNullPointerValue) {
+ EXPECT_EQ(DataLayout("").getNullPointerValue(0), APInt::getZero(64));
+ EXPECT_EQ(DataLayout("").getNullPointerValue(1), std::nullopt);
+ EXPECT_EQ(DataLayout("p:32:32").getNullPointerValue(0), APInt::getZero(32));
+ EXPECT_EQ(DataLayout("p:32:32").getNullPointerValue(1), std::nullopt);
+ EXPECT_EQ(DataLayout("p:64:64").getNullPointerValue(0), APInt::getZero(64));
+ EXPECT_EQ(DataLayout("p:64:64").getNullPointerValue(1), std::nullopt);
}
TEST(DataLayoutTest, CopyAssignmentInvalidatesStructLayout) {
More information about the llvm-commits
mailing list