[llvm] [InferAS] Infer the address space of inttoptr (PR #173244)

Luo Yuanke via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 03:32:49 PST 2026


================
@@ -375,15 +390,81 @@ 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.");
   }
 }
 
+// Return mask. The 1 in mask indicate the bit is changed.
+// This helper function is to compute the max know changed bits for ptr1 and
+// ptr2 after the operation `ptr2 = ptr1 Op Mask`.
+static APInt computeMaxChangedPtrBits(const Operator *Op, const Value *Mask,
+                                      const DataLayout &DL, AssumptionCache *AC,
+                                      const DominatorTree *DT) {
+  KnownBits Known = computeKnownBits(Mask, DL, AC, nullptr, DT);
+  switch (Op->getOpcode()) {
+  case Instruction::Xor:
+  case Instruction::Or:
+    return ~Known.Zero;
+  case Instruction::And:
+    return ~Known.One;
+  default:
----------------
LuoYuanke wrote:

For example "NewPtr = 7 or OldPtr", we need to know only lower 3 bits changes by comparing NewPtr with OldPtr. Compiler doesn't know the bits of OldPtr. Compiler can deduce the lower 3 bits is 1 for NewPtr, but it doesn't know the other bits of the NewPtr. To validate it is legal to perform address space cast, we need to know only the lower 3 bits changes which depends on operator `or` and operand `7`.

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


More information about the llvm-commits mailing list