[llvm] [InstCombine] Add support for ptrtoaddr in pointer difference folds (PR #164428)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 21 07:51:40 PDT 2025


================
@@ -2111,6 +2111,28 @@ template <typename Op_t> struct PtrToIntSameSize_match {
   }
 };
 
+template <typename Op_t> struct PtrToIntOrAddr_GEAddrSize_match {
+  const DataLayout &DL;
+  Op_t Op;
+
+  PtrToIntOrAddr_GEAddrSize_match(const DataLayout &DL, const Op_t &OpMatch)
+      : DL(DL), Op(OpMatch) {}
+
+  template <typename OpTy> bool match(OpTy *V) const {
+    if (auto *O = dyn_cast<Operator>(V)) {
+      unsigned Opcode = O->getOpcode();
+      // The ptrtoaddr result type always matches the address size.
+      // For ptrtoint we have to explicitly check it.
+      return (Opcode == Instruction::PtrToAddr ||
+              (Opcode == Instruction::PtrToInt &&
+               O->getType()->getScalarSizeInBits() ==
----------------
nikic wrote:

Ooops, fixed. I've also added an additional ptrtoint test that covers this.

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


More information about the llvm-commits mailing list