Skip to content

Commit f2da011

Browse files
committed
Update for new one-use GEP expansion
1 parent 90ddc85 commit f2da011

File tree

3 files changed

+44
-55
lines changed

3 files changed

+44
-55
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,14 +2152,17 @@ bool CommonPointerBase::isExpensive() const {
21522152
auto ProcessGEPs = [&SeenConst, &NumGEPs](ArrayRef<GEPOperator *> GEPs) {
21532153
bool SeenMultiUse = false;
21542154
for (GEPOperator *GEP : GEPs) {
2155-
// Only count GEPs after the first multi-use GEP. For the first one,
2156-
// we will directly reuse the offset.
2157-
if (SeenMultiUse) {
2158-
bool IsConst = GEP->hasAllConstantIndices();
2159-
SeenConst |= IsConst;
2160-
NumGEPs += !IsConst;
2155+
// Only count multi-use GEPs, excluding the first one. For the first one,
2156+
// we will directly reuse the offset. For one-use GEPs, their offset will
2157+
// be folded into a multi-use GEP.
2158+
if (!GEP->hasOneUse()) {
2159+
if (SeenMultiUse) {
2160+
bool IsConst = GEP->hasAllConstantIndices();
2161+
SeenConst |= IsConst;
2162+
NumGEPs += !IsConst;
2163+
}
2164+
SeenMultiUse = true;
21612165
}
2162-
SeenMultiUse |= !GEP->hasOneUse();
21632166
}
21642167
};
21652168
ProcessGEPs(LHSGEPs);

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

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -852,16 +852,12 @@ define i1 @gep_mugtiple_ugt_inbounds_nusw(ptr %base, i64 %idx, i64 %idx2) {
852852

853853
define i1 @gep_multiple_multi_use_below_limit(ptr %base, i64 %idx1, i64 %idx2, i64 %idx3) {
854854
; CHECK-LABEL: @gep_multiple_multi_use_below_limit(
855-
; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl i64 [[IDX1:%.*]], 2
856-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i8, ptr [[BASE:%.*]], i64 [[GEP1_IDX]]
857-
; CHECK-NEXT: [[GEP2_IDX:%.*]] = shl i64 [[IDX2:%.*]], 2
858-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, ptr [[GEP1]], i64 [[GEP2_IDX]]
859-
; CHECK-NEXT: [[GEP3_IDX:%.*]] = shl i64 [[IDX3:%.*]], 2
860-
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i8, ptr [[GEP2]], i64 [[GEP3_IDX]]
855+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX1:%.*]], [[IDX2:%.*]]
856+
; CHECK-NEXT: [[IDX3:%.*]] = add i64 [[GEP1_IDX1]], [[IDX4:%.*]]
857+
; CHECK-NEXT: [[GEP3_IDX:%.*]] = shl i64 [[IDX3]], 2
858+
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i8, ptr [[GEP2:%.*]], i64 [[GEP3_IDX]]
861859
; CHECK-NEXT: call void @use(ptr [[GEP3]])
862-
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[GEP1_IDX]], [[GEP2_IDX]]
863-
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 0, [[GEP3_IDX]]
864-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], [[TMP2]]
860+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[GEP3_IDX]], 0
865861
; CHECK-NEXT: ret i1 [[CMP]]
866862
;
867863
%gep1 = getelementptr i32, ptr %base, i64 %idx1
@@ -874,17 +870,14 @@ define i1 @gep_multiple_multi_use_below_limit(ptr %base, i64 %idx1, i64 %idx2, i
874870

875871
define i1 @gep_multiple_multi_use_below_limit_extra_one_use_gep(ptr %base, i64 %idx1, i64 %idx2, i64 %idx3, i64 %idx4) {
876872
; CHECK-LABEL: @gep_multiple_multi_use_below_limit_extra_one_use_gep(
877-
; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl i64 [[IDX1:%.*]], 2
878-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i8, ptr [[BASE:%.*]], i64 [[GEP1_IDX]]
879-
; CHECK-NEXT: [[GEP2_IDX:%.*]] = shl i64 [[IDX2:%.*]], 2
880-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, ptr [[GEP1]], i64 [[GEP2_IDX]]
881-
; CHECK-NEXT: [[GEP3_IDX:%.*]] = shl i64 [[IDX3:%.*]], 2
882-
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i8, ptr [[GEP2]], i64 [[GEP3_IDX]]
883-
; CHECK-NEXT: [[GEP4_IDX_NEG:%.*]] = mul i64 [[IDX4:%.*]], -4
873+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX1:%.*]], [[IDX2:%.*]]
874+
; CHECK-NEXT: [[IDX3:%.*]] = add i64 [[GEP1_IDX1]], [[IDX5:%.*]]
875+
; CHECK-NEXT: [[GEP3_IDX:%.*]] = shl i64 [[IDX3]], 2
876+
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i8, ptr [[GEP2:%.*]], i64 [[GEP3_IDX]]
884877
; CHECK-NEXT: call void @use(ptr [[GEP3]])
885-
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[GEP1_IDX]], [[GEP2_IDX]]
886-
; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[TMP1]], [[GEP3_IDX]]
887-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP2]], [[GEP4_IDX_NEG]]
878+
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[IDX3]], [[IDX4:%.*]]
879+
; CHECK-NEXT: [[DOTMASK:%.*]] = and i64 [[TMP3]], 4611686018427387903
880+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[DOTMASK]], 0
888881
; CHECK-NEXT: ret i1 [[CMP]]
889882
;
890883
%gep1 = getelementptr i32, ptr %base, i64 %idx1
@@ -898,16 +891,12 @@ define i1 @gep_multiple_multi_use_below_limit_extra_one_use_gep(ptr %base, i64 %
898891

899892
define i1 @gep_multiple_multi_use_below_limit_consts(ptr %base, i64 %idx1, i64 %idx2) {
900893
; CHECK-LABEL: @gep_multiple_multi_use_below_limit_consts(
901-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i8, ptr [[BASE:%.*]], i64 16
902-
; CHECK-NEXT: [[GEP2_IDX:%.*]] = shl i64 [[IDX1:%.*]], 2
903-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, ptr [[GEP1]], i64 [[GEP2_IDX]]
904-
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i8, ptr [[GEP2]], i64 16
905-
; CHECK-NEXT: [[GEP4_IDX:%.*]] = shl i64 [[IDX2:%.*]], 2
906-
; CHECK-NEXT: [[GEP4:%.*]] = getelementptr i8, ptr [[GEP3]], i64 [[GEP4_IDX]]
894+
; CHECK-NEXT: [[IDX2:%.*]] = add i64 [[IDX1:%.*]], [[IDX3:%.*]]
895+
; CHECK-NEXT: [[GEP4_IDX:%.*]] = shl i64 [[IDX2]], 2
896+
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[GEP4_IDX]], 32
897+
; CHECK-NEXT: [[GEP4:%.*]] = getelementptr i8, ptr [[BASE:%.*]], i64 [[TMP3]]
907898
; CHECK-NEXT: call void @use(ptr [[GEP4]])
908-
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[GEP2_IDX]], 32
909-
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 0, [[GEP4_IDX]]
910-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], [[TMP2]]
899+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP3]], 0
911900
; CHECK-NEXT: ret i1 [[CMP]]
912901
;
913902
%gep1 = getelementptr i32, ptr %base, i64 4
@@ -921,12 +910,13 @@ define i1 @gep_multiple_multi_use_below_limit_consts(ptr %base, i64 %idx1, i64 %
921910

922911
define i1 @gep_multiple_multi_use_above_limit(ptr %base, i64 %idx1, i64 %idx2, i64 %idx3, i64 %idx4) {
923912
; CHECK-LABEL: @gep_multiple_multi_use_above_limit(
924-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, ptr [[BASE:%.*]], i64 [[IDX1:%.*]]
925-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, ptr [[GEP1]], i64 [[IDX2:%.*]]
926-
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr i32, ptr [[GEP2]], i64 [[IDX3:%.*]]
927-
; CHECK-NEXT: [[GEP4:%.*]] = getelementptr i32, ptr [[GEP3]], i64 [[IDX4:%.*]]
913+
; CHECK-NEXT: [[GEP1_IDX1:%.*]] = add i64 [[IDX1:%.*]], [[IDX2:%.*]]
914+
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[GEP1_IDX1]], [[IDX3:%.*]]
915+
; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[TMP1]], [[IDX4:%.*]]
916+
; CHECK-NEXT: [[TMP3:%.*]] = shl i64 [[TMP2]], 2
917+
; CHECK-NEXT: [[GEP4:%.*]] = getelementptr i8, ptr [[BASE:%.*]], i64 [[TMP3]]
928918
; CHECK-NEXT: call void @use(ptr [[GEP4]])
929-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[GEP4]], [[BASE]]
919+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP3]], 0
930920
; CHECK-NEXT: ret i1 [[CMP]]
931921
;
932922
%gep1 = getelementptr i32, ptr %base, i64 %idx1

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,15 +1175,13 @@ define i64 @nuw_ptrdiff_mul_nsw_nneg_scale_multiuse(ptr %base, i64 %idx) {
11751175

11761176
define i64 @multiple_geps_multi_use_below_limit(ptr %base, i64 %idx1, i64 %idx2, i64 %idx3, i64 %idx4) {
11771177
; CHECK-LABEL: @multiple_geps_multi_use_below_limit(
1178-
; CHECK-NEXT: [[P1:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE:%.*]], i64 [[IDX1:%.*]]
1179-
; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds nuw i8, ptr [[P1]], i64 [[IDX2:%.*]]
1178+
; CHECK-NEXT: [[IDX2:%.*]] = add nuw nsw i64 [[IDX1:%.*]], [[IDX5:%.*]]
1179+
; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds nuw i8, ptr [[P1:%.*]], i64 [[IDX2]]
11801180
; CHECK-NEXT: call void @use(ptr [[P2]])
1181-
; CHECK-NEXT: [[P3:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE]], i64 [[IDX3:%.*]]
1182-
; CHECK-NEXT: [[P4:%.*]] = getelementptr inbounds nuw i8, ptr [[P3]], i64 [[IDX4:%.*]]
1181+
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i64 [[IDX3:%.*]], [[IDX4:%.*]]
1182+
; CHECK-NEXT: [[P4:%.*]] = getelementptr inbounds nuw i8, ptr [[P1]], i64 [[TMP2]]
11831183
; CHECK-NEXT: call void @use(ptr [[P4]])
1184-
; CHECK-NEXT: [[TMP1:%.*]] = add nuw nsw i64 [[IDX1]], [[IDX2]]
1185-
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i64 [[IDX3]], [[IDX4]]
1186-
; CHECK-NEXT: [[GEPDIFF:%.*]] = sub nsw i64 [[TMP1]], [[TMP2]]
1184+
; CHECK-NEXT: [[GEPDIFF:%.*]] = sub nsw i64 [[IDX2]], [[TMP2]]
11871185
; CHECK-NEXT: ret i64 [[GEPDIFF]]
11881186
;
11891187
%p1 = getelementptr inbounds nuw i8, ptr %base, i64 %idx1
@@ -1200,16 +1198,14 @@ define i64 @multiple_geps_multi_use_below_limit(ptr %base, i64 %idx1, i64 %idx2,
12001198

12011199
define i64 @multiple_geps_multi_use_above_limit(ptr %base, i64 %idx1, i64 %idx2, i64 %idx3, i64 %idx4, i64 %idx5) {
12021200
; CHECK-LABEL: @multiple_geps_multi_use_above_limit(
1203-
; CHECK-NEXT: [[P1:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE:%.*]], i64 [[IDX1:%.*]]
1204-
; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds nuw i8, ptr [[P1]], i64 [[IDX2:%.*]]
1201+
; CHECK-NEXT: [[IDX2:%.*]] = add nuw nsw i64 [[IDX1:%.*]], [[IDX6:%.*]]
1202+
; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds nuw i8, ptr [[P1:%.*]], i64 [[IDX2]]
12051203
; CHECK-NEXT: call void @use(ptr [[P2]])
1206-
; CHECK-NEXT: [[P3:%.*]] = getelementptr inbounds nuw i8, ptr [[BASE]], i64 [[IDX3:%.*]]
1207-
; CHECK-NEXT: [[P4:%.*]] = getelementptr inbounds nuw i8, ptr [[P3]], i64 [[IDX4:%.*]]
1208-
; CHECK-NEXT: [[P5:%.*]] = getelementptr inbounds nuw i8, ptr [[P4]], i64 [[IDX5:%.*]]
1204+
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i64 [[IDX3:%.*]], [[IDX4:%.*]]
1205+
; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i64 [[TMP2]], [[IDX5:%.*]]
1206+
; CHECK-NEXT: [[P5:%.*]] = getelementptr inbounds nuw i8, ptr [[P1]], i64 [[TMP3]]
12091207
; CHECK-NEXT: call void @use(ptr [[P5]])
1210-
; CHECK-NEXT: [[I1:%.*]] = ptrtoint ptr [[P5]] to i64
1211-
; CHECK-NEXT: [[I2:%.*]] = ptrtoint ptr [[P2]] to i64
1212-
; CHECK-NEXT: [[D:%.*]] = sub i64 [[I2]], [[I1]]
1208+
; CHECK-NEXT: [[D:%.*]] = sub nsw i64 [[IDX2]], [[TMP3]]
12131209
; CHECK-NEXT: ret i64 [[D]]
12141210
;
12151211
%p1 = getelementptr inbounds nuw i8, ptr %base, i64 %idx1

0 commit comments

Comments
 (0)