Skip to content

Commit 3412950

Browse files
committed
[InstCombine] Compare icmp inttoptr, inttoptr values directly
InstCombine already has some rules for `icmp ptrtoint, ptrtoint` to drop the casts and compare the source values. This change adds the same for the reverse case with `inttoptr`.
1 parent 55eb93b commit 3412950

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6081,12 +6081,12 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
60816081

60826082
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
60836083
// 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();
60886088
}
6089-
return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
6089+
return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
60906090
};
60916091
if (CastOp0->getOpcode() == Instruction::PtrToInt &&
60926092
CompatibleSizes(SrcTy, DestTy)) {
@@ -6103,6 +6103,22 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
61036103
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
61046104
}
61056105

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+
61066122
if (Instruction *R = foldICmpWithTrunc(ICmp))
61076123
return R;
61086124

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,3 +5326,22 @@ define i1 @pr94897(i32 range(i32 -2147483648, 0) %x) {
53265326
%cmp = icmp ugt i32 %shl, -50331648
53275327
ret i1 %cmp
53285328
}
5329+
5330+
define i1 @test_icmp_inttoptr(i64 %x, i64 %y) {
5331+
; CHECK-LABEL: @test_icmp_inttoptr(
5332+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
5333+
; CHECK-NEXT: ret i1 [[CMP]]
5334+
%xptr = inttoptr i64 %x to ptr
5335+
%yptr = inttoptr i64 %y to ptr
5336+
%cmp = icmp eq ptr %xptr, %yptr
5337+
ret i1 %cmp
5338+
}
5339+
5340+
define i1 @test_icmp_inttoptr_constant(i64 %x) {
5341+
; CHECK-LABEL: @test_icmp_inttoptr_constant(
5342+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], 42
5343+
; CHECK-NEXT: ret i1 [[CMP]]
5344+
%xptr = inttoptr i64 %x to ptr
5345+
%cmp = icmp eq ptr %xptr, inttoptr (i64 42 to ptr)
5346+
ret i1 %cmp
5347+
}

0 commit comments

Comments
 (0)