[llvm] [IR] Handle trunc for ptrtoaddr(inttoptr) cast pair (PR #162842)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 08:46:42 PDT 2025


================
@@ -2965,19 +2965,23 @@ unsigned CastInst::isEliminableCastPair(Instruction::CastOps firstOp,
       // zext, sext -> zext, because sext can't sign extend after zext
       return Instruction::ZExt;
     case 11: {
-      // inttoptr, ptrtoint/ptrtoaddr -> bitcast if SrcSize<=PtrSize/AddrSize
-      // and SrcSize==DstSize
+      // inttoptr, ptrtoint/ptrtoaddr -> integer cast
       if (!DL)
         return 0;
       unsigned MidSize = secondOp == Instruction::PtrToAddr
                              ? DL->getAddressSizeInBits(MidTy)
                              : DL->getPointerTypeSizeInBits(MidTy);
       unsigned SrcSize = SrcTy->getScalarSizeInBits();
       unsigned DstSize = DstTy->getScalarSizeInBits();
-      // TODO: Could also produce zext or trunc here.
-      if (SrcSize <= MidSize && SrcSize == DstSize)
-        return Instruction::BitCast;
-      return 0;
+      // If the middle size is smaller than both source and destination,
+      // an additional masking operation would be required.
+      if (MidSize < SrcSize && MidSize < DstSize)
----------------
dtcxzyw wrote:

> Looks like these cases are covered by code below?

It is filtered out by `MidSize < SrcSize && MidSize < DstSize`.

> IIUC, it is filtering cases like https://alive2.llvm.org/ce/z/CL_PmK?

In this case, we need a masking operation because `MidSize < SrcSize < DstSize`.


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


More information about the llvm-commits mailing list