[llvm] e5ef6ac - [Analysis] Use std::optional in MemoryBuiltins.cpp (NFC)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 11 01:32:33 PST 2022
Author: Kazu Hirata
Date: 2022-12-11T01:32:26-08:00
New Revision: e5ef6aced2e4e84069a9de45daed6ff8b2598e6f
URL: https://github.com/llvm/llvm-project/commit/e5ef6aced2e4e84069a9de45daed6ff8b2598e6f
DIFF: https://github.com/llvm/llvm-project/commit/e5ef6aced2e4e84069a9de45daed6ff8b2598e6f.diff
LOG: [Analysis] Use std::optional in MemoryBuiltins.cpp (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
Added:
Modified:
llvm/include/llvm/Analysis/MemoryBuiltins.h
llvm/lib/Analysis/MemoryBuiltins.cpp
llvm/lib/Transforms/IPO/AttributorAttributes.cpp
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/MemoryBuiltins.h b/llvm/include/llvm/Analysis/MemoryBuiltins.h
index bed4565fd8853..146781515abaa 100644
--- a/llvm/include/llvm/Analysis/MemoryBuiltins.h
+++ b/llvm/include/llvm/Analysis/MemoryBuiltins.h
@@ -23,6 +23,7 @@
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/ValueHandle.h"
#include <cstdint>
+#include <optional>
#include <utility>
namespace llvm {
@@ -111,7 +112,7 @@ Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI);
/// calls that return their argument. A mapper function can be used to replace
/// one Value* (operand to the allocation) with another. This is useful when
/// doing abstract interpretation.
-Optional<APInt> getAllocSize(
+std::optional<APInt> getAllocSize(
const CallBase *CB, const TargetLibraryInfo *TLI,
function_ref<const Value *(const Value *)> Mapper = [](const Value *V) {
return V;
@@ -127,8 +128,8 @@ Constant *getInitialValueOfAllocation(const Value *V,
/// If a function is part of an allocation family (e.g.
/// malloc/realloc/calloc/free), return the identifier for its family
/// of functions.
-Optional<StringRef> getAllocationFamily(const Value *I,
- const TargetLibraryInfo *TLI);
+std::optional<StringRef> getAllocationFamily(const Value *I,
+ const TargetLibraryInfo *TLI);
//===----------------------------------------------------------------------===//
// Utility functions to compute size of objects.
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 51517833db9bd..679dd08406780 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -162,7 +162,7 @@ static const Function *getCalledFunction(const Value *V,
/// Returns the allocation data for the given value if it's a call to a known
/// allocation function.
-static Optional<AllocFnsTy>
+static std::optional<AllocFnsTy>
getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
const TargetLibraryInfo *TLI) {
// Don't perform a slow TLI lookup, if this function doesn't return a pointer
@@ -204,8 +204,9 @@ getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
return std::nullopt;
}
-static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
- const TargetLibraryInfo *TLI) {
+static std::optional<AllocFnsTy>
+getAllocationData(const Value *V, AllocType AllocTy,
+ const TargetLibraryInfo *TLI) {
bool IsNoBuiltinCall;
if (const Function *Callee = getCalledFunction(V, IsNoBuiltinCall))
if (!IsNoBuiltinCall)
@@ -213,7 +214,7 @@ static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
return std::nullopt;
}
-static Optional<AllocFnsTy>
+static std::optional<AllocFnsTy>
getAllocationData(const Value *V, AllocType AllocTy,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
bool IsNoBuiltinCall;
@@ -224,8 +225,8 @@ getAllocationData(const Value *V, AllocType AllocTy,
return std::nullopt;
}
-static Optional<AllocFnsTy> getAllocationSize(const Value *V,
- const TargetLibraryInfo *TLI) {
+static std::optional<AllocFnsTy>
+getAllocationSize(const Value *V, const TargetLibraryInfo *TLI) {
bool IsNoBuiltinCall;
const Function *Callee =
getCalledFunction(V, IsNoBuiltinCall);
@@ -235,7 +236,7 @@ static Optional<AllocFnsTy> getAllocationSize(const Value *V,
// Prefer to use existing information over allocsize. This will give us an
// accurate AllocTy.
if (!IsNoBuiltinCall)
- if (Optional<AllocFnsTy> Data =
+ if (std::optional<AllocFnsTy> Data =
getAllocationDataForFunction(Callee, AnyAlloc, TLI))
return Data;
@@ -340,7 +341,7 @@ bool llvm::isRemovableAlloc(const CallBase *CB, const TargetLibraryInfo *TLI) {
Value *llvm::getAllocAlignment(const CallBase *V,
const TargetLibraryInfo *TLI) {
- const Optional<AllocFnsTy> FnData = getAllocationData(V, AnyAlloc, TLI);
+ const std::optional<AllocFnsTy> FnData = getAllocationData(V, AnyAlloc, TLI);
if (FnData && FnData->AlignParam >= 0) {
return V->getOperand(FnData->AlignParam);
}
@@ -363,12 +364,12 @@ static bool CheckedZextOrTrunc(APInt &I, unsigned IntTyBits) {
return true;
}
-Optional<APInt>
+std::optional<APInt>
llvm::getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI,
function_ref<const Value *(const Value *)> Mapper) {
// Note: This handles both explicitly listed allocation functions and
// allocsize. The code structure could stand to be cleaned up a bit.
- Optional<AllocFnsTy> FnData = getAllocationSize(CB, TLI);
+ std::optional<AllocFnsTy> FnData = getAllocationSize(CB, TLI);
if (!FnData)
return std::nullopt;
@@ -485,8 +486,8 @@ static const std::pair<LibFunc, FreeFnsTy> FreeFnData[] = {
};
// clang-format on
-Optional<FreeFnsTy> getFreeFunctionDataForFunction(const Function *Callee,
- const LibFunc TLIFn) {
+std::optional<FreeFnsTy> getFreeFunctionDataForFunction(const Function *Callee,
+ const LibFunc TLIFn) {
const auto *Iter =
find_if(FreeFnData, [TLIFn](const std::pair<LibFunc, FreeFnsTy> &P) {
return P.first == TLIFn;
@@ -496,8 +497,8 @@ Optional<FreeFnsTy> getFreeFunctionDataForFunction(const Function *Callee,
return Iter->second;
}
-Optional<StringRef> llvm::getAllocationFamily(const Value *I,
- const TargetLibraryInfo *TLI) {
+std::optional<StringRef>
+llvm::getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI) {
bool IsNoBuiltin;
const Function *Callee = getCalledFunction(I, IsNoBuiltin);
if (Callee == nullptr || IsNoBuiltin)
@@ -525,7 +526,7 @@ Optional<StringRef> llvm::getAllocationFamily(const Value *I,
/// isLibFreeFunction - Returns true if the function is a builtin free()
bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
- Optional<FreeFnsTy> FnData = getFreeFunctionDataForFunction(F, TLIFn);
+ std::optional<FreeFnsTy> FnData = getFreeFunctionDataForFunction(F, TLIFn);
if (!FnData)
return checkFnAllocKind(F, AllocFnKind::Free);
@@ -781,7 +782,7 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
}
SizeOffsetType ObjectSizeOffsetVisitor::visitCallBase(CallBase &CB) {
- if (Optional<APInt> Size = getAllocSize(&CB, TLI))
+ if (std::optional<APInt> Size = getAllocSize(&CB, TLI))
return std::make_pair(*Size, Zero);
return unknown();
}
@@ -1113,7 +1114,7 @@ SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
}
SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallBase(CallBase &CB) {
- Optional<AllocFnsTy> FnData = getAllocationSize(&CB, TLI);
+ std::optional<AllocFnsTy> FnData = getAllocationSize(&CB, TLI);
if (!FnData)
return unknown();
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index 047ee7a2fc1cb..b5ca98d811a1b 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -6156,7 +6156,7 @@ struct AAHeapToStackFunction final : public AAHeapToStack {
const DataLayout &DL = A.getInfoCache().getDL();
Value *Size;
- Optional<APInt> SizeAPI = getSize(A, *this, AI);
+ std::optional<APInt> SizeAPI = getSize(A, *this, AI);
if (SizeAPI) {
Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
} else {
@@ -6234,8 +6234,8 @@ struct AAHeapToStackFunction final : public AAHeapToStack {
return std::nullopt;
}
- Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
- AllocationInfo &AI) {
+ std::optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
+ AllocationInfo &AI) {
auto Mapper = [&](const Value *V) -> const Value * {
bool UsedAssumedInformation = false;
if (std::optional<Constant *> SimpleV =
@@ -6510,7 +6510,7 @@ ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
}
}
- Optional<APInt> Size = getSize(A, *this, AI);
+ std::optional<APInt> Size = getSize(A, *this, AI);
if (MaxHeapToStackSize != -1) {
if (!Size || Size.value().ugt(MaxHeapToStackSize)) {
LLVM_DEBUG({
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 11e5d2a2089e4..d157737771210 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3039,7 +3039,7 @@ bool InstCombinerImpl::annotateAnyAllocSite(CallBase &Call,
if (!Call.getType()->isPointerTy())
return Changed;
- Optional<APInt> Size = getAllocSize(&Call, TLI);
+ std::optional<APInt> Size = getAllocSize(&Call, TLI);
if (Size && *Size != 0) {
// TODO: We really should just emit deref_or_null here and then
// let the generic inference code combine that with nonnull.
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index e9cd4a65b06a7..78c74538fe2d9 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2717,7 +2717,7 @@ static bool isAllocSiteRemovable(Instruction *AI,
SmallVectorImpl<WeakTrackingVH> &Users,
const TargetLibraryInfo &TLI) {
SmallVector<Instruction*, 4> Worklist;
- const Optional<StringRef> Family = getAllocationFamily(AI, &TLI);
+ const std::optional<StringRef> Family = getAllocationFamily(AI, &TLI);
Worklist.push_back(AI);
do {
More information about the llvm-commits
mailing list