Skip to content

Commit

Permalink
[InstCombine] Compare icmp inttoptr, inttoptr values directly
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
citymarina committed Sep 23, 2024
1 parent 19be41c commit d2ffe52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
26 changes: 21 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6087,12 +6087,12 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {

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

// Do the same in the other direction for icmp (inttoptr x), (inttoptr/c).
if (CastOp0->getOpcode() == Instruction::IntToPtr &&
CompatibleSizes(DestTy, SrcTy)) {
Value *NewOp1 = nullptr;
if (auto *IntToPtrOp1 = dyn_cast<IntToPtrInst>(ICmp.getOperand(1))) {
Value *IntSrc = IntToPtrOp1->getOperand(0);
if (IntSrc->getType() == Op0Src->getType())
NewOp1 = IntToPtrOp1->getOperand(0);
} else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
NewOp1 = ConstantFoldConstant(ConstantExpr::getPtrToInt(RHSC, SrcTy), DL);
}

if (NewOp1)
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
}

if (Instruction *R = foldICmpWithTrunc(ICmp))
return R;

Expand Down
23 changes: 7 additions & 16 deletions llvm/test/Transforms/InstCombine/icmp-inttoptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ declare void @use_ptr(ptr)

define i1 @inttoptr(i64 %x, i64 %y) {
; CHECK-LABEL: @inttoptr(
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%xptr = inttoptr i64 %x to ptr
Expand All @@ -18,8 +16,7 @@ define i1 @inttoptr(i64 %x, i64 %y) {

define i1 @inttoptr_constant(i64 %x) {
; CHECK-LABEL: @inttoptr_constant(
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[X:%.*]] to ptr
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], inttoptr (i64 42 to ptr)
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[X:%.*]], 42
; CHECK-NEXT: ret i1 [[CMP]]
;
%xptr = inttoptr i64 %x to ptr
Expand All @@ -29,9 +26,7 @@ define i1 @inttoptr_constant(i64 %x) {

define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {
; CHECK-LABEL: @inttoptr_vector(
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr <2 x i64> [[Y:%.*]] to <2 x ptr>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], [[YPTR]]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
Expand All @@ -42,8 +37,7 @@ define <2 x i1> @inttoptr_vector(<2 x i64> %x, <2 x i64> %y) {

define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
; CHECK-LABEL: @inttoptr_vector_constant(
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i64 42 to ptr), ptr inttoptr (i64 123 to ptr)>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
Expand All @@ -54,9 +48,7 @@ define <2 x i1> @inttoptr_vector_constant(<2 x i64> %x) {
define i1 @inttoptr_size_mismatch(i200 %x, i64 %y) {
; CHECK-LABEL: @inttoptr_size_mismatch(
; CHECK-NEXT: [[TMP1:%.*]] = trunc i200 [[X:%.*]] to i64
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr i64 [[TMP1]] to ptr
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[XPTR]], [[YPTR]]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], [[TMP1]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%xptr = inttoptr i200 %x to ptr
Expand All @@ -67,8 +59,7 @@ define i1 @inttoptr_size_mismatch(i200 %x, i64 %y) {

define <2 x i1> @inttoptr_vector_constant_size_mismatch(<2 x i64> %x) {
; CHECK-LABEL: @inttoptr_vector_constant_size_mismatch(
; CHECK-NEXT: [[XPTR:%.*]] = inttoptr <2 x i64> [[X:%.*]] to <2 x ptr>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x ptr> [[XPTR]], <ptr inttoptr (i9 42 to ptr), ptr inttoptr (i9 123 to ptr)>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 42, i64 123>
; CHECK-NEXT: ret <2 x i1> [[CMP]]
;
%xptr = inttoptr <2 x i64> %x to <2 x ptr>
Expand All @@ -93,7 +84,7 @@ define i1 @inttoptr_used(i64 %x, i64 %y) {
; CHECK-NEXT: [[YPTR:%.*]] = inttoptr i64 [[Y:%.*]] to ptr
; CHECK-NEXT: call void @use_ptr(ptr [[XPTR]])
; CHECK-NEXT: call void @use_ptr(ptr [[YPTR]])
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[XPTR]], [[YPTR]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%xptr = inttoptr i64 %x to ptr
Expand Down

0 comments on commit d2ffe52

Please sign in to comment.