[llvm-branch-commits] [llvm] [NFCI][IR] Add DataLayout-aware `isZeroValue`/`getNullValue` and `getZeroValue` APIs (PR #183208)

Alexander Richardson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Apr 27 14:55:29 PDT 2026


================
@@ -71,6 +72,27 @@ bool Constant::isNegativeZeroValue() const {
   return isNullValue();
 }
 
+// Return true iff this constant has an all-zero bit pattern.
+bool Constant::isZeroValue(const DataLayout *DL) const {
+  // When DataLayout is available, check if ConstantPointerNull actually has
+  // a zero bit pattern (it might not for non-zero-null address spaces).
+  if (DL) {
+    if (const auto *CPN = dyn_cast<ConstantPointerNull>(this))
+      return DL->isNullPointerAllZeroes(CPN->getType()->getAddressSpace());
+
+    // Check for vector splats of ConstantPointerNull.
+    if (getType()->isVectorTy()) {
+      if (const auto *SplatCPN =
+              dyn_cast_or_null<ConstantPointerNull>(getSplatValue())) {
+        return DL->isNullPointerAllZeroes(
+            SplatCPN->getType()->getAddressSpace());
+      }
+    }
+  }
+
+  return this == getZeroValue(getType());
----------------
arichardson wrote:

Could we just factor out the non-pointer parts of `bool Constant::isNullValue() const` and call that here?

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


More information about the llvm-branch-commits mailing list