Skip to content

Commit 5cd0900

Browse files
authored
[InstCombine] Compare icmp inttoptr, inttoptr values directly (#107012)
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 debc325 commit 5cd0900

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

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

60886088
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
60896089
// integer type is the same size as the pointer type.
6090-
auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
6091-
if (isa<VectorType>(SrcTy)) {
6092-
SrcTy = cast<VectorType>(SrcTy)->getElementType();
6093-
DestTy = cast<VectorType>(DestTy)->getElementType();
6090+
auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6091+
if (isa<VectorType>(PtrTy)) {
6092+
PtrTy = cast<VectorType>(PtrTy)->getElementType();
6093+
IntTy = cast<VectorType>(IntTy)->getElementType();
60946094
}
6095-
return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
6095+
return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
60966096
};
60976097
if (CastOp0->getOpcode() == Instruction::PtrToInt &&
60986098
CompatibleSizes(SrcTy, DestTy)) {
@@ -6109,6 +6109,22 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
61096109
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
61106110
}
61116111

6112+
// Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6113+
if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6114+
CompatibleSizes(DestTy, SrcTy)) {
6115+
Value *NewOp1 = nullptr;
6116+
if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6117+
Value *IntSrc = IntToPtrOp1->getOperand(0);
6118+
if (IntSrc->getType() == Op0Src->getType())
6119+
NewOp1 = IntToPtrOp1->getOperand(0);
6120+
} else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6121+
NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6122+
}
6123+
6124+
if (NewOp1)
6125+
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6126+
}
6127+
61126128
if (Instruction *R = foldICmpWithTrunc(ICmp))
61136129
return R;
61146130

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
declare void @use_ptr(ptr)
5+
6+
define i1 @inttoptr(i64 %x, i64 %y) {
7+
; CHECK-LABEL: @inttoptr(
8+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
9+
; CHECK-NEXT: ret i1 [[CMP]]
10+
;
11+
%xptr = inttoptr i64 %x to ptr
12+
%yptr = inttoptr i64 %y to ptr
13+
%cmp = icmp eq ptr %xptr, %yptr
14+
ret i1 %cmp
15+
}
16+
17+
define i1 @inttoptr_constant(i64 %x) {
18+
; CHECK-LABEL: @inttoptr_constant(
19+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], 42
20+
; CHECK-NEXT: ret i1 [[CMP]]
21+
;
22+
%xptr = inttoptr i64 %x to ptr
23+
%cmp = icmp eq ptr %xptr, inttoptr (i64 42 to ptr)
24+
ret i1 %cmp
25+
}
26+
27+
define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
28+
; CHECK-LABEL: @inttoptr_vector(
29+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], [[Y:%.*]]
30+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
31+
;
32+
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
33+
%yptr = inttoptr <2 x i64> %y to <2 x ptr>
34+
%cmp = icmp eq <2 x ptr> %xptr, %yptr
35+
ret <2 x i1> %cmp
36+
}
37+
38+
define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
39+
; CHECK-LABEL: @inttoptr_vector_constant(
40+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
41+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
42+
;
43+
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
44+
%cmp = icmp eq <2 x ptr> %xptr, inttoptr (<2 x i64> <i64 42, i64 123> to <2 x ptr>)
45+
ret <2 x i1> %cmp
46+
}
47+
48+
define i1 @inttoptr_size_mismatch(i200 %x, i64 %y) {
49+
; CHECK-LABEL: @inttoptr_size_mismatch(
50+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i200 [[X:%.*]] to i64
51+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], [[TMP1]]
52+
; CHECK-NEXT: ret i1 [[CMP]]
53+
;
54+
%xptr = inttoptr i200 %x to ptr
55+
%yptr = inttoptr i64 %y to ptr
56+
%cmp = icmp eq ptr %xptr, %yptr
57+
ret i1 %cmp
58+
}
59+
60+
define <2 x i1> @inttoptr_vector_constant_size_mismatch(<2 x i64> %x) {
61+
; CHECK-LABEL: @inttoptr_vector_constant_size_mismatch(
62+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
63+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
64+
;
65+
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
66+
%cmp = icmp eq <2 x ptr> %xptr, inttoptr (<2x i9> <i9 42, i9 123> to <2 x ptr>)
67+
ret <2 x i1> %cmp
68+
}
69+
70+
define i1 @inttoptr_oneside(i64 %x, ptr %y) {
71+
; CHECK-LABEL: @inttoptr_oneside(
72+
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
73+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[Y:%.*]], [[XPTR]]
74+
; CHECK-NEXT: ret i1 [[CMP]]
75+
;
76+
%xptr = inttoptr i64 %x to ptr
77+
%cmp = icmp eq ptr %xptr, %y
78+
ret i1 %cmp
79+
}
80+
81+
define i1 @inttoptr_used(i64 %x, i64 %y) {
82+
; CHECK-LABEL: @inttoptr_used(
83+
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
84+
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
85+
; CHECK-NEXT: call void @use_ptr(ptr [[XPTR]])
86+
; CHECK-NEXT: call void @use_ptr(ptr [[YPTR]])
87+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
88+
; CHECK-NEXT: ret i1 [[CMP]]
89+
;
90+
%xptr = inttoptr i64 %x to ptr
91+
%yptr = inttoptr i64 %y to ptr
92+
call void @use_ptr(ptr %xptr)
93+
call void @use_ptr(ptr %yptr)
94+
%cmp = icmp ugt ptr %xptr, %yptr
95+
ret i1 %cmp
96+
}
97+

0 commit comments

Comments
 (0)