@@ -6081,12 +6081,12 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
6081
6081
6082
6082
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
6083
6083
// integer type is the same size as the pointer type.
6084
- auto CompatibleSizes = [&](Type *SrcTy , Type *DestTy ) {
6085
- if (isa<VectorType>(SrcTy )) {
6086
- SrcTy = cast<VectorType>(SrcTy )->getElementType ();
6087
- DestTy = cast<VectorType>(DestTy )->getElementType ();
6084
+ auto CompatibleSizes = [&](Type *PtrTy , Type *IntTy ) {
6085
+ if (isa<VectorType>(PtrTy )) {
6086
+ PtrTy = cast<VectorType>(PtrTy )->getElementType ();
6087
+ IntTy = cast<VectorType>(IntTy )->getElementType ();
6088
6088
}
6089
- return DL.getPointerTypeSizeInBits (SrcTy ) == DestTy ->getIntegerBitWidth ();
6089
+ return DL.getPointerTypeSizeInBits (PtrTy ) == IntTy ->getIntegerBitWidth ();
6090
6090
};
6091
6091
if (CastOp0->getOpcode () == Instruction::PtrToInt &&
6092
6092
CompatibleSizes (SrcTy, DestTy)) {
@@ -6103,6 +6103,22 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
6103
6103
return new ICmpInst (ICmp.getPredicate (), Op0Src, NewOp1);
6104
6104
}
6105
6105
6106
+ // Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6107
+ if (CastOp0->getOpcode () == Instruction::IntToPtr &&
6108
+ CompatibleSizes (DestTy, SrcTy)) {
6109
+ Value *NewOp1 = nullptr ;
6110
+ if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand (1 ))) {
6111
+ Value *IntSrc = IntToPtrOp1->getOperand (0 );
6112
+ if (IntSrc->getType () == Op0Src->getType ())
6113
+ NewOp1 = IntToPtrOp1->getOperand (0 );
6114
+ } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand (1 ))) {
6115
+ NewOp1 = ConstantExpr::getPtrToInt (RHSC, SrcTy);
6116
+ }
6117
+
6118
+ if (NewOp1)
6119
+ return new ICmpInst (ICmp.getPredicate (), Op0Src, NewOp1);
6120
+ }
6121
+
6106
6122
if (Instruction *R = foldICmpWithTrunc (ICmp))
6107
6123
return R;
6108
6124
0 commit comments