Skip to content

Commit 9ca8ba2

Browse files
committed
[InstCombine] Fold icmp of gep chain with base
Fold icmp between a chain of geps and its based 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.
1 parent df5e53f commit 9ca8ba2

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
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: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,9 @@ define i1 @gep_diff_base_same_indices_nuw_nusw(ptr %x, ptr %y, i64 %z) {
788788

789789
define i1 @gep_multiple_eq(ptr %base, i64 %idx, i64 %idx2) {
790790
; CHECK-LABEL: @gep_multiple_eq(
791-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
792-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
793-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[GEP2]], [[BASE]]
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
794794
; CHECK-NEXT: ret i1 [[CMP]]
795795
;
796796
%gep1 = getelementptr i32, ptr %base, i64 %idx
@@ -801,9 +801,9 @@ define i1 @gep_multiple_eq(ptr %base, i64 %idx, i64 %idx2) {
801801

802802
define i1 @gep_multiple_eq_commuted(ptr %base, i64 %idx, i64 %idx2) {
803803
; CHECK-LABEL: @gep_multiple_eq_commuted(
804-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
805-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
806-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[BASE]], [[GEP2]]
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
807807
; CHECK-NEXT: ret i1 [[CMP]]
808808
;
809809
%gep1 = getelementptr i32, ptr %base, i64 %idx
@@ -814,9 +814,8 @@ define i1 @gep_multiple_eq_commuted(ptr %base, i64 %idx, i64 %idx2) {
814814

815815
define i1 @gep_mugtiple_ugt_nuw(ptr %base, i64 %idx, i64 %idx2) {
816816
; CHECK-LABEL: @gep_mugtiple_ugt_nuw(
817-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr nuw i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
818-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr nuw i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
819-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[GEP2]], [[BASE]]
817+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = sub i64 0, [[IDX2:%.*]]
818+
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[IDX:%.*]], [[GEP1_IDX1]]
820819
; CHECK-NEXT: ret i1 [[CMP]]
821820
;
822821
%gep1 = getelementptr nuw i32, ptr %base, i64 %idx
@@ -840,9 +839,9 @@ define i1 @gep_mugtiple_ugt_not_all_nuw(ptr %base, i64 %idx, i64 %idx2) {
840839

841840
define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
842841
; CHECK-LABEL: @gep_mugtiple_ugt_inbounds_nusw(
843-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, ptr [[BASE:%.*]], i64 [[IDX:%.*]]
844-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr nusw i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
845-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt ptr [[GEP2]], [[BASE]]
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
846845
; CHECK-NEXT: ret i1 [[CMP]]
847846
;
848847
%gep1 = getelementptr inbounds i32, ptr %base, i64 %idx

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)