Skip to content

Commit 5d263ce

Browse files
nikicjrbyrnes
authored andcommitted
[SeparateConstOffsetFromGEP] Support multiple indices in reorderGEP (llvm#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. Change-Id: Icf2ccbd3efea42efd60b6ce47af1e253d104ae60
1 parent 7d33824 commit 5d263ce

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

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

978978
bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
979979
TargetTransformInfo &TTI) {
980-
if (GEP->getNumIndices() != 1)
981-
return false;
982-
983980
auto PtrGEP = dyn_cast<GetElementPtrInst>(GEP->getPointerOperand());
984981
if (!PtrGEP)
985982
return false;
986-
if (PtrGEP->getNumIndices() != 1)
987-
return false;
988983

989984
bool NestedNeedsExtraction;
990985
int64_t NestedByteOffset =
@@ -1002,14 +997,12 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
1002997
bool PtrGEPInBounds = PtrGEP->isInBounds();
1003998
bool IsChainInBounds = GEPInBounds && PtrGEPInBounds;
1004999
if (IsChainInBounds) {
1005-
auto GEPIdx = GEP->indices().begin();
1006-
auto KnownGEPIdx = computeKnownBits(GEPIdx->get(), *DL);
1007-
IsChainInBounds &= KnownGEPIdx.isNonNegative();
1008-
if (IsChainInBounds) {
1009-
auto PtrGEPIdx = PtrGEP->indices().begin();
1010-
auto KnownPtrGEPIdx = computeKnownBits(PtrGEPIdx->get(), *DL);
1011-
IsChainInBounds &= KnownPtrGEPIdx.isNonNegative();
1012-
}
1000+
auto IsKnownNonNegative = [this](Value *V) {
1001+
return isKnownNonNegative(V, *DL);
1002+
};
1003+
IsChainInBounds &= all_of(GEP->indices(), IsKnownNonNegative);
1004+
if (IsChainInBounds)
1005+
IsChainInBounds &= all_of(PtrGEP->indices(), IsKnownNonNegative);
10131006
}
10141007

10151008
IRBuilder<> Builder(GEP);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,29 @@ entry:
284284
%idx3 = getelementptr i8, ptr addrspace(3) %const3, i64 %in.idx2
285285
ret void
286286
}
287+
288+
define void @multiple_index_maybe_neg(ptr %in.ptr, i64 %in.idx1) {
289+
; CHECK-LABEL: define void @multiple_index_maybe_neg(
290+
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) #[[ATTR0]] {
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
293+
; CHECK-NEXT: ret void
294+
;
295+
%const1 = getelementptr inbounds [2 x <2 x i8>], ptr %in.ptr, i64 0, i64 1
296+
%idx1 = getelementptr inbounds [2 x <2 x i8>], ptr %const1, i64 0, i64 %in.idx1
297+
ret void
298+
}
299+
300+
define void @multiple_index_nonneg(ptr %in.ptr, i64 %in.idx1) {
301+
; CHECK-LABEL: define void @multiple_index_nonneg(
302+
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) #[[ATTR0]] {
303+
; CHECK-NEXT: [[IN_IDX1_NNEG:%.*]] = and i64 [[IN_IDX1]], 9223372036854775807
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
306+
; CHECK-NEXT: ret void
307+
;
308+
%in.idx1.nneg = and i64 %in.idx1, 9223372036854775807
309+
%const1 = getelementptr inbounds [2 x <2 x i8>], ptr %in.ptr, i64 0, i64 1
310+
%idx1 = getelementptr inbounds [2 x <2 x i8>], ptr %const1, i64 0, i64 %in.idx1.nneg
311+
ret void
312+
}

0 commit comments

Comments
 (0)