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

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 5 09:52:25 PDT 2025


================
@@ -649,6 +655,69 @@ static Value *operandWithNewAddressSpaceOrCreatePoison(
   return PoisonValue::get(NewPtrTy);
 }
 
+// A helper function for cloneInstructionWithNewAddressSpace. Handles the
+// conversion of a ptrmask intrinsic instruction.
+Value *InferAddressSpacesImpl::clonePtrMaskWithNewAddressSpace(
+    IntrinsicInst *I, unsigned NewAddrSpace,
+    const ValueToValueMapTy &ValueWithNewAddrSpace,
+    const PredicatedAddrSpaceMapTy &PredicatedAS,
+    SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
+  const Use &PtrOpUse = I->getArgOperandUse(0);
+  unsigned OldAddrSpace = PtrOpUse->getType()->getPointerAddressSpace();
+  Value *MaskOp = I->getArgOperand(1);
+  Type *MaskTy = MaskOp->getType();
+
+  std::optional<KnownBits> OldPtrBits;
+  std::optional<KnownBits> NewPtrBits;
+  if (!TTI->isNoopAddrSpaceCast(OldAddrSpace, NewAddrSpace)) {
+    if (std::optional<std::pair<KnownBits, KnownBits>> KB =
+            TTI->computeKnownBitsAddrSpaceCast(OldAddrSpace, NewAddrSpace,
+                                               *PtrOpUse.get())) {
+      OldPtrBits = KB->first;
+      NewPtrBits = KB->second;
+    }
+  }
+
+  // If the pointers in both addrspaces have a bitwise representation and if the
+  // representation of the new pointer is smaller (fewer bits) than the old one,
+  // check if the mask is applicable to the ptr in the new addrspace. Any
+  // masking only clearing the low bits will also apply in the new addrspace
+  // Note: checking if the mask clears high bits is not sufficient as those
+  // might have already been 0 in the old ptr.
+  if (NewPtrBits && OldPtrBits->getBitWidth() > NewPtrBits->getBitWidth()) {
+    KnownBits MaskBits = computeKnownBits(MaskOp, *DL, nullptr, I);
----------------
shiltian wrote:


```suggestion
    KnownBits MaskBits = computeKnownBits(MaskOp, *DL, /*AssumptionCache=*/nullptr, I);
```

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


More information about the llvm-commits mailing list