[llvm] [NFCI][SROA] reduce calls to getAllocatedType() (PR #177437)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 22 11:03:11 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Jameson Nash (vtjnash)
<details>
<summary>Changes</summary>
Replace repeated calls with getAllocationSize() and cached NewAllocaTy. In AllocaSliceRewriter, the allocated type is already passed and stored in the NewAllocaTy member variable. This change replaces the remaining direct calls to `NewAI.getAllocatedType()` with the cached NewAllocaTy.
---
Full diff: https://github.com/llvm/llvm-project/pull/177437.diff
1 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/SROA.cpp (+35-38)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 9e1f695d4fd97..706cd7fcda9c5 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -252,7 +252,7 @@ class SROA {
friend class AllocaSliceRewriter;
bool presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS);
- AllocaInst *rewritePartition(AllocaInst &AI, AllocaSlices &AS, Partition &P);
+ std::pair<AllocaInst *, uint64_t> rewritePartition(AllocaInst &AI, AllocaSlices &AS, Partition &P);
bool splitAlloca(AllocaInst &AI, AllocaSlices &AS);
bool propagateStoredValuesToLoads(AllocaInst &AI, AllocaSlices &AS);
std::pair<bool /*Changed*/, bool /*CFGChanged*/> runOnAlloca(AllocaInst &AI);
@@ -1051,7 +1051,7 @@ class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
public:
SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS)
: PtrUseVisitor<SliceBuilder>(DL),
- AllocSize(DL.getTypeAllocSize(AI.getAllocatedType()).getFixedValue()),
+ AllocSize(AI.getAllocationSize(DL)->getFixedValue()),
AS(AS) {}
private:
@@ -2878,7 +2878,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
public:
AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass,
- AllocaInst &OldAI, AllocaInst &NewAI,
+ AllocaInst &OldAI, AllocaInst &NewAI, Type *NewAllocaTy,
uint64_t NewAllocaBeginOffset,
uint64_t NewAllocaEndOffset, bool IsIntegerPromotable,
VectorType *PromotableVecTy,
@@ -2887,11 +2887,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
: DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI),
NewAllocaBeginOffset(NewAllocaBeginOffset),
NewAllocaEndOffset(NewAllocaEndOffset),
- NewAllocaTy(NewAI.getAllocatedType()),
+ NewAllocaTy(NewAllocaTy),
IntTy(
IsIntegerPromotable
? Type::getIntNTy(NewAI.getContext(),
- DL.getTypeSizeInBits(NewAI.getAllocatedType())
+ DL.getTypeSizeInBits(NewAllocaTy)
.getFixedValue())
: nullptr),
VecTy(PromotableVecTy),
@@ -3009,8 +3009,8 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// If the new alloca is a fixed vector type, we use its element type as the
// allocated element type, otherwise we use i8 as the allocated element
Type *AllocatedEltTy =
- isa<FixedVectorType>(NewAI.getAllocatedType())
- ? cast<FixedVectorType>(NewAI.getAllocatedType())->getElementType()
+ isa<FixedVectorType>(NewAllocaTy)
+ ? cast<FixedVectorType>(NewAllocaTy)->getElementType()
: Type::getInt8Ty(NewAI.getContext());
unsigned AllocatedEltTySize = DL.getTypeSizeInBits(AllocatedEltTy);
@@ -3232,7 +3232,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
unsigned EndIndex = getIndex(NewEndOffset);
assert(EndIndex > BeginIndex && "Empty vector!");
- LoadInst *Load = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ LoadInst *Load = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "load");
Load->copyMetadata(LI, {LLVMContext::MD_mem_parallel_loop_access,
@@ -3243,7 +3243,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
Value *rewriteIntegerLoad(LoadInst &LI) {
assert(IntTy && "We cannot insert an integer to the alloca");
assert(!LI.isVolatile());
- Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *V = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "load");
V = convertValue(DL, IRB, V, IntTy);
assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
@@ -3289,7 +3289,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
!LI.isVolatile()))) {
Value *NewPtr =
getPtrToNewAI(LI.getPointerAddressSpace(), LI.isVolatile());
- LoadInst *NewLI = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), NewPtr,
+ LoadInst *NewLI = IRB.CreateAlignedLoad(NewAllocaTy, NewPtr,
NewAI.getAlign(), LI.isVolatile(),
LI.getName());
if (LI.isVolatile())
@@ -3397,7 +3397,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
V = convertValue(DL, IRB, V, SliceTy);
// Mix in the existing elements.
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "load");
V = insertVector(IRB, Old, V, BeginIndex, "vec");
}
@@ -3421,7 +3421,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
assert(!SI.isVolatile());
if (DL.getTypeSizeInBits(V->getType()).getFixedValue() !=
IntTy->getBitWidth()) {
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "oldload");
Old = convertValue(DL, IRB, Old, IntTy);
assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
@@ -3573,7 +3573,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// Record this instruction for deletion.
Pass.DeadInsts.push_back(&II);
- Type *AllocaTy = NewAI.getAllocatedType();
+ Type *AllocaTy = NewAllocaTy;
Type *ScalarTy = AllocaTy->getScalarType();
const bool CanContinue = [&]() {
@@ -3636,7 +3636,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
if (NumElements > 1)
Splat = getVectorSplat(Splat, NumElements);
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "oldload");
V = insertVector(IRB, Old, Splat, BeginIndex, "vec");
} else if (IntTy) {
@@ -3649,7 +3649,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
if (IntTy && (BeginOffset != NewAllocaBeginOffset ||
EndOffset != NewAllocaBeginOffset)) {
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "oldload");
Old = convertValue(DL, IRB, Old, IntTy);
uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
@@ -3741,9 +3741,9 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
!VecTy && !IntTy &&
(BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset ||
SliceSize !=
- DL.getTypeStoreSize(NewAI.getAllocatedType()).getFixedValue() ||
- !DL.typeSizeEqualsStoreSize(NewAI.getAllocatedType()) ||
- !NewAI.getAllocatedType()->isSingleValueType());
+ DL.getTypeStoreSize(NewAllocaTy).getFixedValue() ||
+ !DL.typeSizeEqualsStoreSize(NewAllocaTy) ||
+ !NewAllocaTy->isSingleValueType());
// If we're just going to emit a memcpy, the alloca hasn't changed, and the
// size hasn't been shrunk based on analysis of the viable range, this is
@@ -3867,11 +3867,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
Value *Src;
if (VecTy && !IsWholeAlloca && !IsDest) {
- Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Src = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "load");
Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec");
} else if (IntTy && !IsWholeAlloca && !IsDest) {
- Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Src = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "load");
Src = convertValue(DL, IRB, Src, IntTy);
uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
@@ -3888,11 +3888,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
}
if (VecTy && !IsWholeAlloca && IsDest) {
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "oldload");
Src = insertVector(IRB, Old, Src, BeginIndex, "vec");
} else if (IntTy && !IsWholeAlloca && IsDest) {
- Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
+ Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
NewAI.getAlign(), "oldload");
Old = convertValue(DL, IRB, Old, IntTy);
uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
@@ -5319,7 +5319,7 @@ selectPartitionType(Partition &P, const DataLayout &DL, AllocaInst &AI,
/// appropriate new offsets. It also evaluates how successful the rewrite was
/// at enabling promotion and if it was successful queues the alloca to be
/// promoted.
-AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
+std::pair<AllocaInst *, uint64_t> SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
Partition &P) {
const DataLayout &DL = AI.getDataLayout();
// Select the type for the new alloca that spans the partition.
@@ -5365,7 +5365,7 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
SmallSetVector<PHINode *, 8> PHIUsers;
SmallSetVector<SelectInst *, 8> SelectUsers;
- AllocaSliceRewriter Rewriter(DL, AS, *this, AI, *NewAI, P.beginOffset(),
+ AllocaSliceRewriter Rewriter(DL, AS, *this, AI, *NewAI, PartitionTy, P.beginOffset(),
P.endOffset(), IsIntegerWideningViable, VecTy,
PHIUsers, SelectUsers);
bool Promotable = true;
@@ -5446,7 +5446,7 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
// We couldn't promote and we didn't create a new partition, nothing
// happened.
if (NewAI == &AI)
- return nullptr;
+ return {nullptr, 0};
// If we can't promote the alloca, iterate on it to check for new
// refinements exposed by splitting the current alloca. Don't iterate on an
@@ -5454,7 +5454,7 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
Worklist.insert(NewAI);
}
- return NewAI;
+ return {NewAI, DL.getTypeSizeInBits(PartitionTy).getFixedValue()};
}
// There isn't a shared interface to get the "address" parts out of a
@@ -5633,8 +5633,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
// to be rewritten into a partition.
bool IsSorted = true;
- uint64_t AllocaSize =
- DL.getTypeAllocSize(AI.getAllocatedType()).getFixedValue();
+ uint64_t AllocaSize = AI.getAllocationSize(DL)->getFixedValue();
const uint64_t MaxBitVectorSize = 1024;
if (AllocaSize <= MaxBitVectorSize) {
// If a byte boundary is included in any load or store, a slice starting or
@@ -5693,14 +5692,13 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
// Rewrite each partition.
for (auto &P : AS.partitions()) {
- if (AllocaInst *NewAI = rewritePartition(AI, AS, P)) {
+ auto [NewAI, ActiveBits] = rewritePartition(AI, AS, P);
+ if (NewAI) {
Changed = true;
if (NewAI != &AI) {
uint64_t SizeOfByte = 8;
- uint64_t AllocaSize =
- DL.getTypeSizeInBits(NewAI->getAllocatedType()).getFixedValue();
// Don't include any padding.
- uint64_t Size = std::min(AllocaSize, P.size() * SizeOfByte);
+ uint64_t Size = std::min(ActiveBits, P.size() * SizeOfByte);
Fragments.push_back(
Fragment(NewAI, P.beginOffset() * SizeOfByte, Size));
}
@@ -5945,10 +5943,9 @@ SROA::runOnAlloca(AllocaInst &AI) {
const DataLayout &DL = AI.getDataLayout();
// Skip alloca forms that this analysis can't handle.
- auto *AT = AI.getAllocatedType();
- TypeSize Size = DL.getTypeAllocSize(AT);
- if (AI.isArrayAllocation() || !AT->isSized() || Size.isScalable() ||
- Size.getFixedValue() == 0)
+ std::optional<TypeSize> Size = AI.getAllocationSize(DL);
+ if (AI.isArrayAllocation() || !Size || Size->isScalable() ||
+ Size->isZero())
return {Changed, CFGChanged};
// First, split any FCA loads and stores touching this alloca to promote
@@ -6110,8 +6107,8 @@ std::pair<bool /*Changed*/, bool /*CFGChanged*/> SROA::runSROA(Function &F) {
for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end());
I != E; ++I) {
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
- if (DL.getTypeAllocSize(AI->getAllocatedType()).isScalable() &&
- isAllocaPromotable(AI))
+ std::optional<TypeSize> Size = AI->getAllocationSize(DL);
+ if (Size && Size->isScalable() && isAllocaPromotable(AI))
PromotableAllocas.insert(AI);
else
Worklist.insert(AI);
``````````
</details>
https://github.com/llvm/llvm-project/pull/177437
More information about the llvm-commits
mailing list