Skip to content

Commit fac4646

Browse files
authored
[InstCombine] Check no wrap flags before folding icmp of GEPs with same indices (#121628)
Alive2: https://alive2.llvm.org/ce/z/Dr3Sbe Closes #121581.
1 parent 95c5c5d commit fac4646

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
747747
ConstantExpr::getPointerBitCastOrAddrSpaceCast(
748748
cast<Constant>(RHS), Base->getType()));
749749
} else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
750+
GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
751+
750752
// If the base pointers are different, but the indices are the same, just
751753
// compare the base pointer.
752754
if (PtrBase != GEPRHS->getOperand(0)) {
@@ -764,7 +766,8 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
764766

765767
// If all indices are the same, just compare the base pointers.
766768
Type *BaseType = GEPLHS->getOperand(0)->getType();
767-
if (IndicesTheSame && CmpInst::makeCmpResultType(BaseType) == I.getType())
769+
if (IndicesTheSame &&
770+
CmpInst::makeCmpResultType(BaseType) == I.getType() && CanFold(NW))
768771
return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
769772

770773
// If we're comparing GEPs with two base pointers that only differ in type
@@ -804,7 +807,6 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
804807
return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
805808
}
806809

807-
GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags() & GEPRHS->getNoWrapFlags();
808810
if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
809811
GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType()) {
810812
// If the GEPs only differ by one index, compare it.

llvm/test/Transforms/InstCombine/icmp-gep.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,51 @@ define i1 @pointer_icmp_aligned_with_offset_negative(ptr align 8 %a, ptr align 8
709709
%cmp = icmp eq ptr %gep, %a2
710710
ret i1 %cmp
711711
}
712+
713+
define i1 @gep_diff_base_same_indices(ptr %x, ptr %y, i64 %z) {
714+
; CHECK-LABEL: @gep_diff_base_same_indices(
715+
; CHECK-NEXT: [[X:%.*]] = getelementptr i8, ptr [[X1:%.*]], i64 [[Z:%.*]]
716+
; CHECK-NEXT: [[Y:%.*]] = getelementptr i8, ptr [[Y1:%.*]], i64 [[Z]]
717+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[X]], [[Y]]
718+
; CHECK-NEXT: ret i1 [[CMP]]
719+
;
720+
%gep1 = getelementptr i8, ptr %x, i64 %z
721+
%gep2 = getelementptr i8, ptr %y, i64 %z
722+
%cmp = icmp ult ptr %gep1, %gep2
723+
ret i1 %cmp
724+
}
725+
726+
define i1 @gep_diff_base_same_indices_nuw(ptr %x, ptr %y, i64 %z) {
727+
; CHECK-LABEL: @gep_diff_base_same_indices_nuw(
728+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[X:%.*]], [[Y:%.*]]
729+
; CHECK-NEXT: ret i1 [[CMP]]
730+
;
731+
%gep1 = getelementptr nuw i8, ptr %x, i64 %z
732+
%gep2 = getelementptr nuw i8, ptr %y, i64 %z
733+
%cmp = icmp ult ptr %gep1, %gep2
734+
ret i1 %cmp
735+
}
736+
737+
define i1 @gep_diff_base_same_indices_nusw(ptr %x, ptr %y, i64 %z) {
738+
; CHECK-LABEL: @gep_diff_base_same_indices_nusw(
739+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[X:%.*]], [[Y:%.*]]
740+
; CHECK-NEXT: ret i1 [[CMP]]
741+
;
742+
%gep1 = getelementptr nusw i8, ptr %x, i64 %z
743+
%gep2 = getelementptr nusw i8, ptr %y, i64 %z
744+
%cmp = icmp ult ptr %gep1, %gep2
745+
ret i1 %cmp
746+
}
747+
748+
define i1 @gep_diff_base_same_indices_nuw_nusw(ptr %x, ptr %y, i64 %z) {
749+
; CHECK-LABEL: @gep_diff_base_same_indices_nuw_nusw(
750+
; CHECK-NEXT: [[X:%.*]] = getelementptr nuw i8, ptr [[X1:%.*]], i64 [[Z:%.*]]
751+
; CHECK-NEXT: [[Y:%.*]] = getelementptr nusw i8, ptr [[Y1:%.*]], i64 [[Z]]
752+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult ptr [[X]], [[Y]]
753+
; CHECK-NEXT: ret i1 [[CMP]]
754+
;
755+
%gep1 = getelementptr nuw i8, ptr %x, i64 %z
756+
%gep2 = getelementptr nusw i8, ptr %y, i64 %z
757+
%cmp = icmp ult ptr %gep1, %gep2
758+
ret i1 %cmp
759+
}

0 commit comments

Comments
 (0)