[llvm] [InferAS] Infer the address space of inttoptr (PR #173244)
Artem Belevich via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 9 11:30:40 PST 2026
================
@@ -369,15 +384,56 @@ getPointerOperands(const Value &V, const DataLayout &DL,
return {II.getArgOperand(0)};
}
case Instruction::IntToPtr: {
- assert(isNoopPtrIntCastPair(&Op, DL, TTI));
- auto *P2I = cast<Operator>(Op.getOperand(0));
- return {P2I->getOperand(0)};
+ if (isNoopPtrIntCastPair(&Op, DL, TTI)) {
+ auto *P2I = cast<Operator>(Op.getOperand(0));
+ return {P2I->getOperand(0)};
+ }
+ assert(isSafeToCastPtrIntPair(&Op, DL));
+ return {PtrIntCastPairs[&Op]};
}
default:
llvm_unreachable("Unexpected instruction type.");
}
}
+bool InferAddressSpacesImpl::isSafeToCastPtrIntPair(
+ const Operator *I2P, const DataLayout &DL) const {
+ assert(I2P->getOpcode() == Instruction::IntToPtr);
+ if (PtrIntCastPairs.count(I2P))
+ return true;
+
+ if (I2P->getType()->isVectorTy())
+ return false;
+
+ auto *Xor = dyn_cast<Operator>(I2P->getOperand(0));
+ if (!Xor || Xor->getOpcode() != Instruction::Xor)
+ return false;
----------------
Artem-B wrote:
We do not have to copy/paste the functionality. We just need to figure out how to get instcombine to do the job it already does.
E.g. we could replace the argument of the `inttoptr(ptrvalue)` which may be operating on a pointer in originally known AS with something like this:
```
as_inference_helper_intrinsic(
and(bitmask_for bits_that_must_remain_unchanged, // constant for the AS we operate on
xor(initial_ptrvalue, ptrvalue)
), // instcombine collapses `and` to constant zero if the upper bits are unchanged
ptrvalue,
)
```
Later on if AS inference pass sees `as_inference_helper_intrinsic` with a constant 0 first argument, it can proceed inferring the AS. Otherwise, it just replaces the intrinsic with the `ptrvalue` (and the and/xor if they are still there) and keeps the pointer in the generic AS.
https://github.com/llvm/llvm-project/pull/173244
More information about the llvm-commits
mailing list