[llvm] [Value] Look through inttoptr (add ..) in accumulateConstantOffsets (PR #124981)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 12:48:03 PST 2025


================
@@ -775,6 +775,25 @@ const Value *Value::stripAndAccumulateConstantOffsets(
           V = RV;
         if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
           V = Call->getArgOperand(0);
+    } else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
+      // Try to accumulate across (inttoptr (add (ptrtoint p), off)).
+      if (!Int2Ptr || Int2Ptr->getOpcode() != Instruction::IntToPtr ||
+          Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
+        return V;
+      auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
+      if (!AllowNonInbounds || !Add)
+        return V;
+      auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
+      auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
+      if (!Ptr2Int || !CI)
+        return V;
+
+      APInt AddOffset = CI->getValue();
----------------
nikic wrote:

```suggestion
      const APInt &AddOffset = CI->getValue();
```

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


More information about the llvm-commits mailing list