Skip to content

Commit b5db1ab

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 919852c commit b5db1ab

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
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-inttoptr.ll

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
2+
; RUN: opt < %s -passes='instcombine<max-iterations=2>' -S | FileCheck %s
33

44
target datalayout = "e-p:64:64:64-p1:16:16:16-p2:32:32:32-p3:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
55

66
declare void @use_ptr(ptr)
77

88
define i1 @inttoptr(i64 %x, i64 %y) {
99
; CHECK-LABEL: @inttoptr(
10-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
11-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
12-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
10+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
1311
; CHECK-NEXT: ret i1 [[CMP]]
1412
;
1513
%xptr = inttoptr i64 %x to ptr
@@ -20,8 +18,7 @@ define i1 @inttoptr(i64 %x, i64 %y) {
2018

2119
define i1 @inttoptr_constant(i64 %x) {
2220
; CHECK-LABEL: @inttoptr_constant(
23-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
24-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], inttoptr (i64 42 to ptr)
21+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], 42
2522
; CHECK-NEXT: ret i1 [[CMP]]
2623
;
2724
%xptr = inttoptr i64 %x to ptr
@@ -31,9 +28,7 @@ define i1 @inttoptr_constant(i64 %x) {
3128

3229
define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
3330
; CHECK-LABEL: @inttoptr_vector(
34-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
35-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr <2 x i64> [[Y:%.*]] to <2 x ptr>
36-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], [[YPTR]]
31+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], [[Y:%.*]]
3732
; CHECK-NEXT: ret <2 x i1> [[CMP]]
3833
;
3934
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
@@ -44,8 +39,7 @@ define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
4439

4540
define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
4641
; CHECK-LABEL: @inttoptr_vector_constant(
47-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
48-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i64 42 to ptr), ptr inttoptr (i64 123 to ptr)>
42+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
4943
; CHECK-NEXT: ret <2 x i1> [[CMP]]
5044
;
5145
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
@@ -56,10 +50,8 @@ define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
5650
define i1 @inttoptr_size_mismatch(i200 %x, i9 %y) {
5751
; CHECK-LABEL: @inttoptr_size_mismatch(
5852
; CHECK-NEXT: [[TMP1:%.*]] = trunc i200 [[X:%.*]] to i64
59-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[TMP1]] to ptr
6053
; CHECK-NEXT: [[TMP2:%.*]] = zext i9 [[Y:%.*]] to i64
61-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[TMP2]] to ptr
62-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
54+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], [[TMP2]]
6355
; CHECK-NEXT: ret i1 [[CMP]]
6456
;
6557
%xptr = inttoptr i200 %x to ptr
@@ -71,8 +63,7 @@ define i1 @inttoptr_size_mismatch(i200 %x, i9 %y) {
7163
define <2 x i1> @inttoptr_vector_constant_size_mismatch(<2 x i200> %x) {
7264
; CHECK-LABEL: @inttoptr_vector_constant_size_mismatch(
7365
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i200> [[X:%.*]] to <2 x i64>
74-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[TMP1]] to <2 x ptr>
75-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i9 42 to ptr), ptr inttoptr (i9 123 to ptr)>
66+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[TMP1]], <i64 42, i64 123>
7667
; CHECK-NEXT: ret <2 x i1> [[CMP]]
7768
;
7869
%xptr = inttoptr <2 x i200> %x to <2 x ptr>
@@ -97,7 +88,7 @@ define i1 @inttoptr_used(i64 %x, i64 %y) {
9788
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
9889
; CHECK-NEXT: call void @use_ptr(ptr [[XPTR]])
9990
; CHECK-NEXT: call void @use_ptr(ptr [[YPTR]])
100-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[XPTR]], [[YPTR]]
91+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
10192
; CHECK-NEXT: ret i1 [[CMP]]
10293
;
10394
%xptr = inttoptr i64 %x to ptr

0 commit comments

Comments
 (0)