Skip to content

[InstCombine] Check nowrap flags when folding comparison of GEPs with the same base pointer #121892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,9 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
return replaceInstUsesWith(
I, // No comparison is needed here.
ConstantInt::get(I.getType(), ICmpInst::isTrueWhenEqual(Cond)));

else if (NumDifferences == 1 && CanFold(NW)) {
// If two GEPs only differ by an index, compare them.
// Note that nowrap flags are always needed when comparing two indices.
else if (NumDifferences == 1 && NW != GEPNoWrapFlags::none()) {
Value *LHSV = GEPLHS->getOperand(DiffOperand);
Value *RHSV = GEPRHS->getOperand(DiffOperand);
return NewICmp(NW, LHSV, RHSV);
Expand Down
35 changes: 35 additions & 0 deletions llvm/test/Transforms/InstCombine/opaque-ptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,41 @@ define i1 @cmp_gep_same_base_same_type(ptr %ptr, i64 %idx1, i64 %idx2) {
ret i1 %cmp
}

define i1 @cmp_gep_same_base_same_type_maywrap(ptr %ptr, i64 %idx1, i64 %idx2) {
; CHECK-LABEL: @cmp_gep_same_base_same_type_maywrap(
; CHECK-NEXT: [[CMP_UNSHIFTED:%.*]] = xor i64 [[IDX1:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: [[CMP_MASK:%.*]] = and i64 [[CMP_UNSHIFTED]], 4611686018427387903
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[CMP_MASK]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%gep1 = getelementptr i32, ptr %ptr, i64 %idx1
%gep2 = getelementptr i32, ptr %ptr, i64 %idx2
%cmp = icmp eq ptr %gep1, %gep2
ret i1 %cmp
}

define i1 @cmp_gep_same_base_same_type_nuw(ptr %ptr, i64 %idx1, i64 %idx2) {
; CHECK-LABEL: @cmp_gep_same_base_same_type_nuw(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[IDX1:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%gep1 = getelementptr nuw i32, ptr %ptr, i64 %idx1
%gep2 = getelementptr nuw i32, ptr %ptr, i64 %idx2
%cmp = icmp eq ptr %gep1, %gep2
ret i1 %cmp
}

define i1 @cmp_gep_same_base_same_type_nusw(ptr %ptr, i64 %idx1, i64 %idx2) {
; CHECK-LABEL: @cmp_gep_same_base_same_type_nusw(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[IDX1:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%gep1 = getelementptr nusw i32, ptr %ptr, i64 %idx1
%gep2 = getelementptr nusw i32, ptr %ptr, i64 %idx2
%cmp = icmp eq ptr %gep1, %gep2
ret i1 %cmp
}

define i1 @cmp_gep_same_base_different_type(ptr %ptr, i64 %idx1, i64 %idx2) {
; CHECK-LABEL: @cmp_gep_same_base_different_type(
; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl nsw i64 [[IDX1:%.*]], 2
Expand Down