[AllocaInst] Update `getAllocationSizeInBits` to return `TypeSize`.

Reviewed By: peterwaller-arm, sdesmalen

Differential Revision: https://reviews.llvm.org/D92020
This commit is contained in:
Francesco Petrogalli 2020-11-27 16:32:05 +00:00
parent 2ad2e91016
commit 8e0148dff7
4 changed files with 48 additions and 6 deletions

View File

@ -106,7 +106,7 @@ public:
/// Get allocation size in bits. Returns None if size can't be determined,
/// e.g. in case of a VLA.
Optional<uint64_t> getAllocationSizeInBits(const DataLayout &DL) const;
Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
/// Return the type that is being allocated by the instruction.
Type *getAllocatedType() const { return AllocatedType; }

View File

@ -49,13 +49,14 @@ using namespace llvm;
// AllocaInst Class
//===----------------------------------------------------------------------===//
Optional<uint64_t>
Optional<TypeSize>
AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
TypeSize Size = DL.getTypeAllocSizeInBits(getAllocatedType());
if (isArrayAllocation()) {
auto *C = dyn_cast<ConstantInt>(getArraySize());
if (!C)
return None;
assert(!Size.isScalable() && "Array elements cannot have a scalable size");
Size *= C->getZExtValue();
}
return Size;

View File

@ -576,9 +576,10 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
StackLifetimeAnalyzer.getLiveRange(AI2));
};
auto GetAllocaSize = [&](const AllocaInfo &A) {
Optional<uint64_t> RetSize = A.Alloca->getAllocationSizeInBits(DL);
assert(RetSize && "We can't handle scalable type now.\n");
return RetSize.getValue();
Optional<TypeSize> RetSize = A.Alloca->getAllocationSizeInBits(DL);
assert(RetSize && "Variable Length Arrays (VLA) are not supported.\n");
assert(!RetSize->isScalable() && "Scalable vectors are not yet supported");
return RetSize->getFixedSize();
};
// Put larger allocas in the front. So the larger allocas have higher
// priority to merge, which can save more space potentially. Also each

View File

@ -1400,5 +1400,45 @@ TEST(InstructionsTest, BranchWeightOverflow) {
ASSERT_EQ(ProfWeight, UINT32_MAX);
}
TEST(InstructionsTest, AllocaInst) {
LLVMContext Ctx;
std::unique_ptr<Module> M = parseIR(Ctx, R"(
%T = type { i64, [3 x i32]}
define void @f(i32 %n) {
entry:
%A = alloca i32, i32 1
%B = alloca i32, i32 4
%C = alloca i32, i32 %n
%D = alloca <8 x double>
%E = alloca <vscale x 8 x double>
%F = alloca [2 x half]
%G = alloca [2 x [3 x i128]]
%H = alloca %T
ret void
}
)");
const DataLayout &DL = M->getDataLayout();
ASSERT_TRUE(M);
Function *Fun = cast<Function>(M->getNamedValue("f"));
BasicBlock &BB = Fun->front();
auto It = BB.begin();
AllocaInst &A = cast<AllocaInst>(*It++);
AllocaInst &B = cast<AllocaInst>(*It++);
AllocaInst &C = cast<AllocaInst>(*It++);
AllocaInst &D = cast<AllocaInst>(*It++);
AllocaInst &E = cast<AllocaInst>(*It++);
AllocaInst &F = cast<AllocaInst>(*It++);
AllocaInst &G = cast<AllocaInst>(*It++);
AllocaInst &H = cast<AllocaInst>(*It++);
EXPECT_EQ(A.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
EXPECT_EQ(B.getAllocationSizeInBits(DL), TypeSize::getFixed(128));
EXPECT_FALSE(C.getAllocationSizeInBits(DL));
EXPECT_EQ(D.getAllocationSizeInBits(DL), TypeSize::getFixed(512));
EXPECT_EQ(E.getAllocationSizeInBits(DL), TypeSize::getScalable(512));
EXPECT_EQ(F.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
EXPECT_EQ(G.getAllocationSizeInBits(DL), TypeSize::getFixed(768));
EXPECT_EQ(H.getAllocationSizeInBits(DL), TypeSize::getFixed(160));
}
} // end anonymous namespace
} // end namespace llvm