[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:18:14 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:
```
SrcSize < MidSize < DstSize -> zext
DstSize < MidSize < SrcSize -> trunc
```
These two cases are also valid without additional masking op.
https://alive2.llvm.org/ce/z/XWCoaJ
https://github.com/llvm/llvm-project/pull/162842
More information about the llvm-commits
mailing list