diff options
| author | Mingming Liu <mingmingl@google.com> | 2025-09-10 15:25:31 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-10 15:25:31 -0700 |
| commit | 1417dafa1db9cb1b2b09438aa9f53ea5ab6e36e2 (patch) | |
| tree | 57f4b1f313c8cf74eed8819870f39c36ea263c68 /llvm/lib/Analysis/MemoryBuiltins.cpp | |
| parent | 898b813bc8a6d0276bf0f4769f5f2f64b34e632d (diff) | |
| parent | b8cefcb601ddaa18482555c4ff363c01a270c2fe (diff) | |
Merge branch 'main' into users/mingmingl-llvm/samplefdo-profile-formatusers/mingmingl-llvm/samplefdo-profile-format
Diffstat (limited to 'llvm/lib/Analysis/MemoryBuiltins.cpp')
| -rw-r--r-- | llvm/lib/Analysis/MemoryBuiltins.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp index e0b7f65d18a3..1df4eda2580d 100644 --- a/llvm/lib/Analysis/MemoryBuiltins.cpp +++ b/llvm/lib/Analysis/MemoryBuiltins.cpp @@ -589,6 +589,59 @@ bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, return true; } +std::optional<TypeSize> llvm::getBaseObjectSize(const Value *Ptr, + const DataLayout &DL, + const TargetLibraryInfo *TLI, + ObjectSizeOpts Opts) { + assert(Opts.EvalMode == ObjectSizeOpts::Mode::ExactSizeFromOffset && + "Other modes are currently not supported"); + + auto Align = [&](TypeSize Size, MaybeAlign Alignment) { + if (Opts.RoundToAlign && Alignment && !Size.isScalable()) + return TypeSize::getFixed(alignTo(Size.getFixedValue(), *Alignment)); + return Size; + }; + + if (isa<UndefValue>(Ptr)) + return TypeSize::getZero(); + + if (isa<ConstantPointerNull>(Ptr)) { + if (Opts.NullIsUnknownSize || Ptr->getType()->getPointerAddressSpace()) + return std::nullopt; + return TypeSize::getZero(); + } + + if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) { + if (!GV->getValueType()->isSized() || GV->hasExternalWeakLinkage() || + !GV->hasInitializer() || GV->isInterposable()) + return std::nullopt; + return Align(DL.getTypeAllocSize(GV->getValueType()), GV->getAlign()); + } + + if (auto *A = dyn_cast<Argument>(Ptr)) { + Type *MemoryTy = A->getPointeeInMemoryValueType(); + if (!MemoryTy || !MemoryTy->isSized()) + return std::nullopt; + return Align(DL.getTypeAllocSize(MemoryTy), A->getParamAlign()); + } + + if (auto *AI = dyn_cast<AllocaInst>(Ptr)) { + if (std::optional<TypeSize> Size = AI->getAllocationSize(DL)) + return Align(*Size, AI->getAlign()); + return std::nullopt; + } + + if (auto *CB = dyn_cast<CallBase>(Ptr)) { + if (std::optional<APInt> Size = getAllocSize(CB, TLI)) { + if (std::optional<uint64_t> ZExtSize = Size->tryZExtValue()) + return TypeSize::getFixed(*ZExtSize); + } + return std::nullopt; + } + + return std::nullopt; +} + Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, |
