[llvm] a6a526e - [IR] Add AllocaInst::getAllocationSize() (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 6 06:36:25 PST 2023
Author: Nikita Popov
Date: 2023-01-06T15:36:16+01:00
New Revision: a6a526ec5465d712db11fdbf5ed5fce8da0722cf
URL: https://github.com/llvm/llvm-project/commit/a6a526ec5465d712db11fdbf5ed5fce8da0722cf
DIFF: https://github.com/llvm/llvm-project/commit/a6a526ec5465d712db11fdbf5ed5fce8da0722cf.diff
LOG: [IR] Add AllocaInst::getAllocationSize() (NFC)
When fetching allocation sizes, we almost always want to have the
size in bytes, but we were only providing an InBits API. Also add
the corresponding byte-based conjugate to save some *8 and /8
juggling everywhere.
Added:
Modified:
llvm/include/llvm/IR/Instructions.h
llvm/lib/Analysis/StackLifetime.cpp
llvm/lib/IR/Instructions.cpp
llvm/lib/Target/AArch64/AArch64StackTagging.cpp
llvm/lib/Transforms/Coroutines/CoroFrame.cpp
llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index 3177cfe0a6a01..9f82ac91e92d0 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -106,6 +106,10 @@ class AllocaInst : public UnaryInstruction {
return getType()->getAddressSpace();
}
+ /// Get allocation size in bytes. Returns std::nullopt if size can't be
+ /// determined, e.g. in case of a VLA.
+ std::optional<TypeSize> getAllocationSize(const DataLayout &DL) const;
+
/// Get allocation size in bits. Returns std::nullopt if size can't be
/// determined, e.g. in case of a VLA.
std::optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
diff --git a/llvm/lib/Analysis/StackLifetime.cpp b/llvm/lib/Analysis/StackLifetime.cpp
index 72a1ac7b4eff5..9fc78fad59aa0 100644
--- a/llvm/lib/Analysis/StackLifetime.cpp
+++ b/llvm/lib/Analysis/StackLifetime.cpp
@@ -67,17 +67,16 @@ static const AllocaInst *findMatchingAlloca(const IntrinsicInst &II,
if (!AI)
return nullptr;
- auto AllocaSizeInBits = AI->getAllocationSizeInBits(DL);
- if (!AllocaSizeInBits)
+ auto AllocaSize = AI->getAllocationSize(DL);
+ if (!AllocaSize)
return nullptr;
- int64_t AllocaSize = *AllocaSizeInBits / 8;
auto *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
if (!Size)
return nullptr;
int64_t LifetimeSize = Size->getSExtValue();
- if (LifetimeSize != -1 && LifetimeSize != AllocaSize)
+ if (LifetimeSize != -1 && LifetimeSize != *AllocaSize)
return nullptr;
return AI;
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index cdd2770bf29f9..d1d7e14454a47 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -56,8 +56,8 @@ static cl::opt<bool> DisableI2pP2iOpt(
//===----------------------------------------------------------------------===//
std::optional<TypeSize>
-AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
- TypeSize Size = DL.getTypeAllocSizeInBits(getAllocatedType());
+AllocaInst::getAllocationSize(const DataLayout &DL) const {
+ TypeSize Size = DL.getTypeAllocSize(getAllocatedType());
if (isArrayAllocation()) {
auto *C = dyn_cast<ConstantInt>(getArraySize());
if (!C)
@@ -68,6 +68,14 @@ AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
return Size;
}
+std::optional<TypeSize>
+AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
+ std::optional<TypeSize> Size = getAllocationSize(DL);
+ if (Size)
+ return *Size * 8;
+ return std::nullopt;
+}
+
//===----------------------------------------------------------------------===//
// SelectInst Class
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 93afbad574b21..97afb66ed9ab5 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -563,7 +563,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
End->eraseFromParent();
}
} else {
- uint64_t Size = *Info.AI->getAllocationSizeInBits(*DL) / 8;
+ uint64_t Size = *Info.AI->getAllocationSize(*DL);
Value *Ptr = IRB.CreatePointerCast(TagPCall, IRB.getInt8PtrTy());
tagAlloca(AI, &*IRB.GetInsertPoint(), Ptr, Size);
for (auto *RI : SInfo.RetVec) {
diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
index b650d4be47824..0cac8f560088c 100644
--- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
@@ -675,7 +675,7 @@ void FrameTypeBuilder::addFieldForAllocas(const Function &F,
StackLifetimeAnalyzer.getLiveRange(AI2));
};
auto GetAllocaSize = [&](const AllocaInfo &A) {
- std::optional<TypeSize> RetSize = A.Alloca->getAllocationSizeInBits(DL);
+ std::optional<TypeSize> RetSize = A.Alloca->getAllocationSize(DL);
assert(RetSize && "Variable Length Arrays (VLA) are not supported.\n");
assert(!RetSize->isScalable() && "Scalable vectors are not yet supported");
return RetSize->getFixedSize();
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 288ee5ceac5de..8d0a7ed3a91b4 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1354,8 +1354,8 @@ static bool hasUndefContents(MemorySSA *MSSA, BatchAAResults &AA, Value *V,
if (getUnderlyingObject(II->getArgOperand(1)) == Alloca) {
const DataLayout &DL = Alloca->getModule()->getDataLayout();
if (std::optional<TypeSize> AllocaSize =
- Alloca->getAllocationSizeInBits(DL))
- if (*AllocaSize == LTSize->getValue() * 8)
+ Alloca->getAllocationSize(DL))
+ if (*AllocaSize == LTSize->getValue())
return true;
}
}
diff --git a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
index 2a303cd8a170a..9341291a72d2d 100644
--- a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
@@ -341,9 +341,9 @@ void MemoryOpRemark::visitVariable(const Value *V,
return;
// If not, get it from the alloca.
- std::optional<TypeSize> TySize = AI->getAllocationSizeInBits(DL);
+ std::optional<TypeSize> TySize = AI->getAllocationSize(DL);
std::optional<uint64_t> Size =
- TySize ? getSizeInBytes(TySize->getFixedSize()) : std::nullopt;
+ TySize ? std::optional(TySize->getFixedSize()) : std::nullopt;
VariableInfo Var{nameOrNone(AI), Size};
if (!Var.isEmpty())
Result.push_back(std::move(Var));
diff --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
index cab85cbbce2ad..1e42d7491676d 100644
--- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
@@ -174,7 +174,7 @@ bool StackInfoBuilder::isInterestingAlloca(const AllocaInst &AI) {
uint64_t getAllocaSizeInBytes(const AllocaInst &AI) {
auto DL = AI.getModule()->getDataLayout();
- return *AI.getAllocationSizeInBits(DL) / 8;
+ return *AI.getAllocationSize(DL);
}
void alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Alignment) {
More information about the llvm-commits
mailing list