Skip to content

Commit 83272a4

Browse files
authored
[InstCombine] Fold icmp of gep chain with base (#144065)
Fold icmp between a chain of geps and its base pointer. Previously only a single gep was supported. This will be extended to handle the case of two gep chains with a common base in a followup. This helps to avoid regressions after #137297.
1 parent 0588e81 commit 83272a4

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -708,14 +708,14 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
708708
return I;
709709
};
710710

711-
Value *PtrBase = GEPLHS->getOperand(0);
712-
if (PtrBase == RHS && CanFold(GEPLHS->getNoWrapFlags())) {
711+
CommonPointerBase Base = CommonPointerBase::compute(GEPLHS, RHS);
712+
if (Base.Ptr == RHS && CanFold(Base.LHSNW)) {
713713
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
714-
GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
715-
// Do not access GEPLHS after EmitGEPOffset, as the instruction may be
716-
// destroyed.
717-
Value *Offset = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
718-
return NewICmp(NW, Offset, Constant::getNullValue(Offset->getType()));
714+
Type *IdxTy = DL.getIndexType(GEPLHS->getType());
715+
Value *Offset =
716+
EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
717+
return NewICmp(Base.LHSNW, Offset,
718+
Constant::getNullValue(Offset->getType()));
719719
}
720720

721721
if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
@@ -752,6 +752,7 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
752752

753753
// If the base pointers are different, but the indices are the same, just
754754
// compare the base pointer.
755+
Value *PtrBase = GEPLHS->getOperand(0);
755756
if (PtrBase != GEPRHS->getOperand(0)) {
756757
bool IndicesTheSame =
757758
GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,9 @@ define i32 @test28() nounwind {
688688
; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[BB10]] ]
689689
; CHECK-NEXT: [[T12_REC:%.*]] = xor i32 [[INDVAR]], -1
690690
; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[T12_REC]] to i64
691-
; CHECK-NEXT: [[T12:%.*]] = getelementptr inbounds [[STRUCT_X:%.*]], ptr [[T45]], i64 [[TMP0]]
691+
; CHECK-NEXT: [[T12:%.*]] = getelementptr inbounds i8, ptr [[T45]], i64 [[TMP0]]
692692
; CHECK-NEXT: [[T16:%.*]] = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str1, ptr nonnull [[T12]]) #[[ATTR0]]
693-
; CHECK-NEXT: [[T84:%.*]] = icmp eq ptr [[T12]], [[ORIENTATIONS]]
693+
; CHECK-NEXT: [[T84:%.*]] = icmp eq i32 [[INDVAR]], 0
694694
; CHECK-NEXT: [[INDVAR_NEXT]] = add i32 [[INDVAR]], 1
695695
; CHECK-NEXT: br i1 [[T84]], label [[BB17:%.*]], label [[BB10]]
696696
; CHECK: bb17:

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,67 @@ define i1 @gep_diff_base_same_indices_nuw_nusw(ptr %x, ptr %y, i64 %z) {
785785
%cmp = icmp ult ptr %gep1, %gep2
786786
ret i1 %cmp
787787
}
788+
789+
define i1 @gep_multiple_eq(ptr %base, i64 %idx, i64 %idx2) {
790+
; CHECK-LABEL: @gep_multiple_eq(
791+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
792+
; CHECK-NEXT: [[DOTMASK:%.*]] = and i64 [[GEP1_IDX1]], 4611686018427387903
793+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[DOTMASK]], 0
794+
; CHECK-NEXT: ret i1 [[CMP]]
795+
;
796+
%gep1 = getelementptr i32, ptr %base, i64 %idx
797+
%gep2 = getelementptr i32, ptr %gep1, i64 %idx2
798+
%cmp = icmp eq ptr %gep2, %base
799+
ret i1 %cmp
800+
}
801+
802+
define i1 @gep_multiple_eq_commuted(ptr %base, i64 %idx, i64 %idx2) {
803+
; CHECK-LABEL: @gep_multiple_eq_commuted(
804+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
805+
; CHECK-NEXT: [[DOTMASK:%.*]] = and i64 [[GEP1_IDX1]], 4611686018427387903
806+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[DOTMASK]], 0
807+
; CHECK-NEXT: ret i1 [[CMP]]
808+
;
809+
%gep1 = getelementptr i32, ptr %base, i64 %idx
810+
%gep2 = getelementptr i32, ptr %gep1, i64 %idx2
811+
%cmp = icmp eq ptr %base, %gep2
812+
ret i1 %cmp
813+
}
814+
815+
define i1 @gep_mugtiple_ugt_nuw(ptr %base, i64 %idx, i64 %idx2) {
816+
; CHECK-LABEL: @gep_mugtiple_ugt_nuw(
817+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = sub i64 0, [[IDX2:%.*]]
818+
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[IDX:%.*]], [[GEP1_IDX1]]
819+
; CHECK-NEXT: ret i1 [[CMP]]
820+
;
821+
%gep1 = getelementptr nuw i32, ptr %base, i64 %idx
822+
%gep2 = getelementptr nuw i32, ptr %gep1, i64 %idx2
823+
%cmp = icmp ugt ptr %gep2, %base
824+
ret i1 %cmp
825+
}
826+
827+
define i1 @gep_mugtiple_ugt_not_all_nuw(ptr %base, i64 %idx, i64 %idx2) {
828+
; CHECK-LABEL: @gep_mugtiple_ugt_not_all_nuw(
829+
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr nuw i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
830+
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
831+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[GEP2]], [[BASE]]
832+
; CHECK-NEXT: ret i1 [[CMP]]
833+
;
834+
%gep1 = getelementptr nuw i32, ptr %base, i64 %idx
835+
%gep2 = getelementptr i32, ptr %gep1, i64 %idx2
836+
%cmp = icmp ugt ptr %gep2, %base
837+
ret i1 %cmp
838+
}
839+
840+
define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
841+
; CHECK-LABEL: @gep_mugtiple_ugt_inbounds_nusw(
842+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
843+
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[GEP1_IDX1]], 2
844+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[TMP1]], 0
845+
; CHECK-NEXT: ret i1 [[CMP]]
846+
;
847+
%gep1 = getelementptr inbounds i32, ptr %base, i64 %idx
848+
%gep2 = getelementptr nusw i32, ptr %gep1, i64 %idx2
849+
%cmp = icmp ugt ptr %gep2, %base
850+
ret i1 %cmp
851+
}

llvm/test/Transforms/InstCombine/pr39908.ll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ target datalayout = "p:32:32"
77

88
define i1 @test(ptr %p, i32 %n) {
99
; CHECK-LABEL: @test(
10-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[N:%.*]], i32 0, i32 0
11-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
12-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
10+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[N:%.*]], 1
1311
; CHECK-NEXT: ret i1 [[CMP]]
1412
;
1513
%end = getelementptr inbounds [0 x %S], ptr %p, i32 0, i32 %n, i32 0, i32 0
@@ -22,9 +20,7 @@ define i1 @test(ptr %p, i32 %n) {
2220
define i1 @test64(ptr %p, i64 %n) {
2321
; CHECK-LABEL: @test64(
2422
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
25-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[TMP1]], i32 0, i32 0
26-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
27-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
23+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
2824
; CHECK-NEXT: ret i1 [[CMP]]
2925
;
3026
%end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 0
@@ -37,9 +33,7 @@ define i1 @test64(ptr %p, i64 %n) {
3733
define i1 @test64_overflow(ptr %p, i64 %n) {
3834
; CHECK-LABEL: @test64_overflow(
3935
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
40-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[TMP1]], i32 0, i32 0
41-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
42-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
36+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
4337
; CHECK-NEXT: ret i1 [[CMP]]
4438
;
4539
%end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 8589934592

0 commit comments

Comments
 (0)