[llvm] [InferAddressSpaces] Handle unconverted ptrmask (PR #140802)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 4 18:28:29 PDT 2025


================
@@ -153,23 +153,63 @@ class TargetTransformInfoImplBase {
 
   virtual bool isNoopAddrSpaceCast(unsigned, unsigned) const { return false; }
 
-  // Assuming that the cast between the two given addrspaces is not a noop,
-  // calculate the known bits of the resulting ptr in the destination addrspace.
-  // The default implementation returns 0 known bits in case either one of the
-  // addrspaces is not integral or the bit size of the source addrspace is
-  // smaller than the bit size of the destination addrspace.
-  virtual KnownBits computeKnownBitsAddrSpaceCast(unsigned FromAS,
-                                                  unsigned ToAS) const {
-    if (DL.isNonIntegralAddressSpace(FromAS) ||
-        DL.isNonIntegralAddressSpace(ToAS))
-      return 0;
-    unsigned FromASBitSize = DL.getPointerSizeInBits(FromAS);
+  // Given an address space cast of the given pointer value, calculate the known
+  // bits of the source pointer in the source addrspace and the destination
+  // pointer in the destination addrspace.
+  // The default implementation returns an empty optional in case one of the
+  // addrspaces is not integral.
+  virtual std::optional<std::pair<KnownBits, KnownBits>>
+  computeKnownBitsAddrSpaceCast(unsigned FromAS, unsigned ToAS,
+                                const Value &PtrOp) const {
+    if (DL.isNonIntegralAddressSpace(FromAS))
+      return std::nullopt;
+
+    KnownBits FromPtrBits;
+    if (const AddrSpaceCastInst *CastI = dyn_cast<AddrSpaceCastInst>(&PtrOp)) {
+      std::optional<std::pair<KnownBits, KnownBits>> KB =
+          computeKnownBitsAddrSpaceCast(CastI->getSrcAddressSpace(),
+                                        CastI->getDestAddressSpace(),
+                                        *CastI->getPointerOperand());
+      if (!KB)
+        return std::nullopt;
+      FromPtrBits = KB->second;
+    } else if (isa<ConstantPointerNull>(PtrOp) && !FromAS) {
+      // For addrspace 0, we know that a null pointer has the value 0.
+      FromPtrBits = KnownBits::makeConstant(
+          APInt::getZero(DL.getPointerSizeInBits(FromAS)));
+    } else {
+      FromPtrBits = computeKnownBits(&PtrOp, DL, nullptr);
+    }
+
+    std::optional<KnownBits> ToPtrBits =
+        computeKnownBitsAddrSpaceCast(FromAS, ToAS, FromPtrBits);
+    if (!ToPtrBits)
+      return std::nullopt;
+
+    return std::pair(FromPtrBits, *ToPtrBits);
+  }
+
+  // Given an address space cast, calculate the known bits of the resulting ptr
+  // in the destination addrspace using the known bits of the source pointer in
+  // the source addrspace.
+  // The default implementation returns an empty optional in case the source
+  // addrspace is not an integral addrspace.
+  virtual std::optional<KnownBits>
----------------
arsenm wrote:

ditto, just use KnownBits's conservative default 

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


More information about the llvm-commits mailing list