[llvm] [SeparateConstOffsetFromGEP] Support GEP reordering for different types (PR #90802)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue May 7 07:47:59 PDT 2024
================
@@ -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));
----------------
arsenm wrote:
Probably shouldn't be using indices() if you aren't iterating over them. You shouldn't have to do this much work just to get the integer size?
https://github.com/llvm/llvm-project/pull/90802
More information about the llvm-commits
mailing list