Skip to content

Commit 5d5d4d9

Browse files
committed
[Attributor] Generalize heap to stack to any allocator with relevant properties
This completes removal of the isXLike queries, and depends on a whole series of earlier patches which have already landed. Differential Revision: https://reviews.llvm.org/D117242
1 parent cf66f01 commit 5d5d4d9

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5771,13 +5771,6 @@ struct AAHeapToStackFunction final : public AAHeapToStack {
57715771
/// The call that allocates the memory.
57725772
CallBase *const CB;
57735773

5774-
/// The kind of allocation.
5775-
const enum class AllocationKind {
5776-
MALLOC,
5777-
CALLOC,
5778-
ALIGNED_ALLOC,
5779-
} Kind;
5780-
57815774
/// The library function id for the allocation.
57825775
LibFunc LibraryFunctionId = NotLibFunc;
57835776

@@ -5834,20 +5827,17 @@ struct AAHeapToStackFunction final : public AAHeapToStack {
58345827
DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
58355828
return true;
58365829
}
5837-
bool IsMalloc = isMallocLikeFn(CB, TLI);
5838-
bool IsAlignedAllocLike = !IsMalloc && isAlignedAllocLikeFn(CB, TLI);
5839-
bool IsCalloc =
5840-
!IsMalloc && !IsAlignedAllocLike && isCallocLikeFn(CB, TLI);
5841-
if (!IsMalloc && !IsAlignedAllocLike && !IsCalloc)
5842-
return true;
5843-
auto Kind =
5844-
IsMalloc ? AllocationInfo::AllocationKind::MALLOC
5845-
: (IsCalloc ? AllocationInfo::AllocationKind::CALLOC
5846-
: AllocationInfo::AllocationKind::ALIGNED_ALLOC);
5847-
5848-
AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB, Kind};
5849-
AllocationInfos[CB] = AI;
5850-
TLI->getLibFunc(*CB, AI->LibraryFunctionId);
5830+
// To do heap to stack, we need to know that the allocation itself is
5831+
// removable once uses are rewritten, and that we can initialize the
5832+
// alloca to the same pattern as the original allocation result.
5833+
if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) {
5834+
auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
5835+
if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
5836+
AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
5837+
AllocationInfos[CB] = AI;
5838+
TLI->getLibFunc(*CB, AI->LibraryFunctionId);
5839+
}
5840+
}
58515841
return true;
58525842
};
58535843

0 commit comments

Comments
 (0)