Skip to content

Commit 0204fad

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 33b53c0 commit 0204fad

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
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 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
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: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ declare void @use_ptr(ptr)
55

66
define i1 @inttoptr(i64 %x, i64 %y) {
77
; CHECK-LABEL: @inttoptr(
8-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
9-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
10-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
8+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
119
; CHECK-NEXT: ret i1 [[CMP]]
1210
;
1311
%xptr = inttoptr i64 %x to ptr
@@ -18,8 +16,7 @@ define i1 @inttoptr(i64 %x, i64 %y) {
1816

1917
define i1 @inttoptr_constant(i64 %x) {
2018
; CHECK-LABEL: @inttoptr_constant(
21-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
22-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], inttoptr (i64 42 to ptr)
19+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], 42
2320
; CHECK-NEXT: ret i1 [[CMP]]
2421
;
2522
%xptr = inttoptr i64 %x to ptr
@@ -29,9 +26,7 @@ define i1 @inttoptr_constant(i64 %x) {
2926

3027
define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
3128
; CHECK-LABEL: @inttoptr_vector(
32-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
33-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr <2 x i64> [[Y:%.*]] to <2 x ptr>
34-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], [[YPTR]]
29+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], [[Y:%.*]]
3530
; CHECK-NEXT: ret <2 x i1> [[CMP]]
3631
;
3732
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
@@ -42,8 +37,7 @@ define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
4237

4338
define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
4439
; CHECK-LABEL: @inttoptr_vector_constant(
45-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
46-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i64 42 to ptr), ptr inttoptr (i64 123 to ptr)>
40+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
4741
; CHECK-NEXT: ret <2 x i1> [[CMP]]
4842
;
4943
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
@@ -54,10 +48,8 @@ define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
5448
define i1 @inttoptr_size_mismatch(i200 %x, i9 %y) {
5549
; CHECK-LABEL: @inttoptr_size_mismatch(
5650
; CHECK-NEXT: [[TMP1:%.*]] = trunc i200 [[X:%.*]] to i64
57-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[TMP1]] to ptr
5851
; CHECK-NEXT: [[TMP2:%.*]] = zext i9 [[Y:%.*]] to i64
59-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[TMP2]] to ptr
60-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
52+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], [[TMP2]]
6153
; CHECK-NEXT: ret i1 [[CMP]]
6254
;
6355
%xptr = inttoptr i200 %x to ptr
@@ -69,8 +61,7 @@ define i1 @inttoptr_size_mismatch(i200 %x, i9 %y) {
6961
define <2 x i1> @inttoptr_vector_constant_size_mismatch(<2 x i200> %x) {
7062
; CHECK-LABEL: @inttoptr_vector_constant_size_mismatch(
7163
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i200> [[X:%.*]] to <2 x i64>
72-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[TMP1]] to <2 x ptr>
73-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i9 42 to ptr), ptr inttoptr (i9 123 to ptr)>
64+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[TMP1]], <i64 42, i64 123>
7465
; CHECK-NEXT: ret <2 x i1> [[CMP]]
7566
;
7667
%xptr = inttoptr <2 x i200> %x to <2 x ptr>
@@ -95,7 +86,7 @@ define i1 @inttoptr_used(i64 %x, i64 %y) {
9586
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
9687
; CHECK-NEXT: call void @use_ptr(ptr [[XPTR]])
9788
; CHECK-NEXT: call void @use_ptr(ptr [[YPTR]])
98-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[XPTR]], [[YPTR]]
89+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
9990
; CHECK-NEXT: ret i1 [[CMP]]
10091
;
10192
%xptr = inttoptr i64 %x to ptr

0 commit comments

Comments
 (0)