Skip to content

Commit e540bc1

Browse files
committed
[InstCombine] Compare icmp inttoptr, inttoptr values directly (llvm#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`. (cherry picked from commit 5cd0900) Editor's note: two test cases were changed during the cherry-pick. They were of the form: -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], [[TMP1]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], [[Y:%.*]] I assume that between stable/20240723 and this landing, the canonical order of these icmps was changed.
1 parent 1815d07 commit e540bc1

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
@@ -6013,12 +6013,12 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
60136013

60146014
// Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
60156015
// integer type is the same size as the pointer type.
6016-
auto CompatibleSizes = [&](Type *SrcTy, Type *DestTy) {
6017-
if (isa<VectorType>(SrcTy)) {
6018-
SrcTy = cast<VectorType>(SrcTy)->getElementType();
6019-
DestTy = cast<VectorType>(DestTy)->getElementType();
6016+
auto CompatibleSizes = [&](Type *PtrTy, Type *IntTy) {
6017+
if (isa<VectorType>(PtrTy)) {
6018+
PtrTy = cast<VectorType>(PtrTy)->getElementType();
6019+
IntTy = cast<VectorType>(IntTy)->getElementType();
60206020
}
6021-
return DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth();
6021+
return DL.getPointerTypeSizeInBits(PtrTy) == IntTy->getIntegerBitWidth();
60226022
};
60236023
if (CastOp0->getOpcode() == Instruction::PtrToInt &&
60246024
CompatibleSizes(SrcTy, DestTy)) {
@@ -6035,6 +6035,22 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
60356035
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
60366036
}
60376037

6038+
// Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
6039+
if (CastOp0->getOpcode() == Instruction::IntToPtr &&
6040+
CompatibleSizes(DestTy, SrcTy)) {
6041+
Value *NewOp1 = nullptr;
6042+
if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
6043+
Value *IntSrc = IntToPtrOp1->getOperand(0);
6044+
if (IntSrc->getType() == Op0Src->getType())
6045+
NewOp1 = IntToPtrOp1->getOperand(0);
6046+
} else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
6047+
NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
6048+
}
6049+
6050+
if (NewOp1)
6051+
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
6052+
}
6053+
60386054
if (Instruction *R = foldICmpWithTrunc(ICmp))
60396055
return R;
60406056

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 [[TMP1]], [[Y:%.*]]
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 [[XPTR]], [[Y:%.*]]
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)