Skip to content

Commit 9c78481

Browse files
authored
[SeparateConstOffsetFromGEP] Support multiple indices in reorderGEP (#92339)
The code was essentially already ready to handle multiple indices -- we only need to adjust the non-negative index check to check all indices, instead of only the first one.
1 parent f2e787e commit 9c78481

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -972,14 +972,9 @@ SeparateConstOffsetFromGEP::lowerToArithmetics(GetElementPtrInst *Variadic,
972972

973973
bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
974974
TargetTransformInfo &TTI) {
975-
if (GEP->getNumIndices() != 1)
976-
return false;
977-
978975
auto PtrGEP = dyn_cast<GetElementPtrInst>(GEP->getPointerOperand());
979976
if (!PtrGEP)
980977
return false;
981-
if (PtrGEP->getNumIndices() != 1)
982-
return false;
983978

984979
bool NestedNeedsExtraction;
985980
int64_t NestedByteOffset =
@@ -997,14 +992,12 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
997992
bool PtrGEPInBounds = PtrGEP->isInBounds();
998993
bool IsChainInBounds = GEPInBounds && PtrGEPInBounds;
999994
if (IsChainInBounds) {
1000-
auto GEPIdx = GEP->indices().begin();
1001-
auto KnownGEPIdx = computeKnownBits(GEPIdx->get(), *DL);
1002-
IsChainInBounds &= KnownGEPIdx.isNonNegative();
1003-
if (IsChainInBounds) {
1004-
auto PtrGEPIdx = PtrGEP->indices().begin();
1005-
auto KnownPtrGEPIdx = computeKnownBits(PtrGEPIdx->get(), *DL);
1006-
IsChainInBounds &= KnownPtrGEPIdx.isNonNegative();
1007-
}
995+
auto IsKnownNonNegative = [this](Value *V) {
996+
return isKnownNonNegative(V, *DL);
997+
};
998+
IsChainInBounds &= all_of(GEP->indices(), IsKnownNonNegative);
999+
if (IsChainInBounds)
1000+
IsChainInBounds &= all_of(PtrGEP->indices(), IsKnownNonNegative);
10081001
}
10091002

10101003
IRBuilder<> Builder(GEP);

llvm/test/Transforms/SeparateConstOffsetFromGEP/AMDGPU/reorder-gep.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ entry:
288288
define void @multiple_index_maybe_neg(ptr %in.ptr, i64 %in.idx1) {
289289
; CHECK-LABEL: define void @multiple_index_maybe_neg(
290290
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) #[[ATTR0]] {
291-
; CHECK-NEXT: [[CONST1:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[IN_PTR]], i64 0, i64 1
292-
; CHECK-NEXT: [[IDX1:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[CONST1]], i64 0, i64 [[IN_IDX1]]
291+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [2 x <2 x i8>], ptr [[IN_PTR]], i64 0, i64 [[IN_IDX1]]
292+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr [2 x <2 x i8>], ptr [[TMP1]], i64 0, i64 1
293293
; CHECK-NEXT: ret void
294294
;
295295
%const1 = getelementptr inbounds [2 x <2 x i8>], ptr %in.ptr, i64 0, i64 1
@@ -301,8 +301,8 @@ define void @multiple_index_nonneg(ptr %in.ptr, i64 %in.idx1) {
301301
; CHECK-LABEL: define void @multiple_index_nonneg(
302302
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) #[[ATTR0]] {
303303
; CHECK-NEXT: [[IN_IDX1_NNEG:%.*]] = and i64 [[IN_IDX1]], 9223372036854775807
304-
; CHECK-NEXT: [[CONST1:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[IN_PTR]], i64 0, i64 1
305-
; CHECK-NEXT: [[IDX1:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[CONST1]], i64 0, i64 [[IN_IDX1_NNEG]]
304+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[IN_PTR]], i64 0, i64 [[IN_IDX1_NNEG]]
305+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds [2 x <2 x i8>], ptr [[TMP1]], i64 0, i64 1
306306
; CHECK-NEXT: ret void
307307
;
308308
%in.idx1.nneg = and i64 %in.idx1, 9223372036854775807

0 commit comments

Comments
 (0)