Skip to content

[SeparateConstOffsetFromGEP] Support GEP reordering for different types #90802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,22 +972,23 @@ SeparateConstOffsetFromGEP::lowerToArithmetics(GetElementPtrInst *Variadic,

bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
TargetTransformInfo &TTI) {
Type *GEPType = GEP->getResultElementType();
Type *GEPType = GEP->getSourceElementType();
// TODO: support reordering for non-trivial GEP chains
if (GEPType->isAggregateType() || GEP->getNumIndices() != 1)
return false;

auto PtrGEP = dyn_cast<GetElementPtrInst>(GEP->getPointerOperand());
if (!PtrGEP)
return false;
Type *PtrGEPType = PtrGEP->getResultElementType();
Type *PtrGEPType = PtrGEP->getSourceElementType();
// TODO: support reordering for non-trivial GEP chains
if (PtrGEPType->isAggregateType() || PtrGEP->getNumIndices() != 1)
return false;

// TODO: support reordering for non-trivial GEP chains
if (PtrGEPType != GEPType ||
PtrGEP->getSourceElementType() != GEP->getSourceElementType())
bool GEPIsPtr = GEPType->getScalarType()->isPtrOrPtrVectorTy();
bool PtrGEPIsPtr = PtrGEPType->getScalarType()->isPtrOrPtrVectorTy();

if (GEPIsPtr != PtrGEPIsPtr)
return false;

bool NestedNeedsExtraction;
Expand All @@ -1002,8 +1003,6 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
/*HasBaseReg=*/true, /*Scale=*/0, AddrSpace))
return false;

IRBuilder<> Builder(GEP);
Builder.SetCurrentDebugLocation(GEP->getDebugLoc());
bool GEPInBounds = GEP->isInBounds();
bool PtrGEPInBounds = PtrGEP->isInBounds();
bool IsChainInBounds = GEPInBounds && PtrGEPInBounds;
Expand All @@ -1017,6 +1016,54 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
IsChainInBounds &= KnownPtrGEPIdx.isNonNegative();
}
}
TypeSize GEPSize = DL->getTypeSizeInBits(GEP->getIndexedType(
GEP->getSourceElementType(), GEP->indices().begin()->get()));
TypeSize PtrGEPSize = DL->getTypeSizeInBits(PtrGEP->getIndexedType(
PtrGEP->getSourceElementType(), PtrGEP->indices().begin()->get()));

IRBuilder<> Builder(GEP);
Builder.SetCurrentDebugLocation(GEP->getDebugLoc());
if (GEPSize > PtrGEPSize) {
if (GEPSize % PtrGEPSize)
return false;
unsigned Ratio = GEPSize / PtrGEPSize;
if (NestedByteOffset % Ratio)
return false;

auto NewGEPOffset = Builder.CreateUDiv(
*PtrGEP->indices().begin(),
Builder.getIntN(
PtrGEP->indices().begin()->get()->getType()->getScalarSizeInBits(),
Ratio));
auto NewSrc = Builder.CreateGEP(GEPType, PtrGEP->getPointerOperand(),
SmallVector<Value *, 4>(GEP->indices()));
cast<GetElementPtrInst>(NewSrc)->setIsInBounds(IsChainInBounds);
auto NewGEP = Builder.CreateGEP(GEPType, NewSrc, NewGEPOffset);
cast<GetElementPtrInst>(NewGEP)->setIsInBounds(IsChainInBounds);
GEP->replaceAllUsesWith(NewGEP);
RecursivelyDeleteTriviallyDeadInstructions(GEP);
return true;
}

if (GEPSize < PtrGEPSize) {
if (PtrGEPSize % GEPSize)
return false;
unsigned Ratio = PtrGEPSize / GEPSize;

auto NewGEPOffset = Builder.CreateMul(
*PtrGEP->indices().begin(),
Builder.getIntN(
PtrGEP->indices().begin()->get()->getType()->getScalarSizeInBits(),
Ratio));
auto NewSrc = Builder.CreateGEP(GEPType, PtrGEP->getPointerOperand(),
SmallVector<Value *, 4>(GEP->indices()));
cast<GetElementPtrInst>(NewSrc)->setIsInBounds(IsChainInBounds);
auto NewGEP = Builder.CreateGEP(GEPType, NewSrc, NewGEPOffset);
cast<GetElementPtrInst>(NewGEP)->setIsInBounds(IsChainInBounds);
GEP->replaceAllUsesWith(NewGEP);
RecursivelyDeleteTriviallyDeadInstructions(GEP);
return true;
}

// For trivial GEP chains, we can swap the indicies.
auto NewSrc = Builder.CreateGEP(PtrGEPType, PtrGEP->getPointerOperand(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,37 @@ entry:
%idx1 = getelementptr <2 x i8>, ptr %const1, i32 %in.idx1.nneg
ret void
}

define void @inboundsNonNegativeTypeShrink(ptr %in.ptr, i32 %in.idx1) {
; CHECK-LABEL: define void @inboundsNonNegativeTypeShrink(
; CHECK-SAME: ptr [[IN_PTR:%.*]], i32 [[IN_IDX1:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[IN_IDX1_NNEG:%.*]] = and i32 [[IN_IDX1]], 2147483647
; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[IN_IDX1_NNEG]] to i64
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i8, ptr [[IN_PTR]], i64 [[IDXPROM]]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i32 2048
; CHECK-NEXT: ret void
;
entry:
%in.idx1.nneg = and i32 %in.idx1, 2147483647
%const1 = getelementptr inbounds i16, ptr %in.ptr, i32 1024
%idx1 = getelementptr inbounds i8, ptr %const1, i32 %in.idx1.nneg
ret void
}

define void @inboundsNonNegativeTypeExpand(ptr %in.ptr, i32 %in.idx1) {
; CHECK-LABEL: define void @inboundsNonNegativeTypeExpand(
; CHECK-SAME: ptr [[IN_PTR:%.*]], i32 [[IN_IDX1:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[IN_IDX1_NNEG:%.*]] = and i32 [[IN_IDX1]], 2147483647
; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[IN_IDX1_NNEG]] to i64
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i16, ptr [[IN_PTR]], i64 [[IDXPROM]]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr [[TMP0]], i32 512
; CHECK-NEXT: ret void
;
entry:
%in.idx1.nneg = and i32 %in.idx1, 2147483647
%const1 = getelementptr inbounds i8, ptr %in.ptr, i32 1024
%idx1 = getelementptr inbounds i16, ptr %const1, i32 %in.idx1.nneg
ret void
}
Loading
Loading