Skip to content

Commit d2ffe52

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 19be41c commit d2ffe52

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
@@ -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

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,9 +48,7 @@ define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
5448
define i1 @inttoptr_size_mismatch(i200 %x, i64 %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
58-
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
59-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
51+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], [[TMP1]]
6052
; CHECK-NEXT: ret i1 [[CMP]]
6153
;
6254
%xptr = inttoptr i200 %x to ptr
@@ -67,8 +59,7 @@ define i1 @inttoptr_size_mismatch(i200 %x, i64 %y) {
6759

6860
define <2 x i1> @inttoptr_vector_constant_size_mismatch(<2 x i64> %x) {
6961
; CHECK-LABEL: @inttoptr_vector_constant_size_mismatch(
70-
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
71-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i9 42 to ptr), ptr inttoptr (i9 123 to ptr)>
62+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
7263
; CHECK-NEXT: ret <2 x i1> [[CMP]]
7364
;
7465
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
@@ -93,7 +84,7 @@ define i1 @inttoptr_used(i64 %x, i64 %y) {
9384
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
9485
; CHECK-NEXT: call void @use_ptr(ptr [[XPTR]])
9586
; CHECK-NEXT: call void @use_ptr(ptr [[YPTR]])
96-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[XPTR]], [[YPTR]]
87+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
9788
; CHECK-NEXT: ret i1 [[CMP]]
9889
;
9990
%xptr = inttoptr i64 %x to ptr

0 commit comments

Comments
 (0)