Skip to content

Commit

Permalink
Transforms/IPO: llvm::Optional => std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Dec 5, 2022
1 parent e92fe7a commit 75801e3
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 165 deletions.
70 changes: 36 additions & 34 deletions llvm/include/llvm/Transforms/IPO/Attributor.h
Expand Up @@ -206,9 +206,9 @@ Value *getWithType(Value &V, Type &Ty);
/// X + none => X
/// not_none + undef => not_none
/// V1 + V2 => nullptr
Optional<Value *>
combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
const Optional<Value *> &B, Type *Ty);
std::optional<Value *>
combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
const std::optional<Value *> &B, Type *Ty);

/// Helper to represent an access offset and size, with logic to deal with
/// uncertainty and check for overlapping accesses.
Expand Down Expand Up @@ -1745,27 +1745,27 @@ struct Attributor {

/// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
/// return std::nullopt, otherwise return `nullptr`.
Optional<Constant *> getAssumedConstant(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation);
Optional<Constant *> getAssumedConstant(const Value &V,
const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
std::optional<Constant *> getAssumedConstant(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation);
std::optional<Constant *> getAssumedConstant(const Value &V,
const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
}

/// If \p V is assumed simplified, return it, if it is unclear yet,
/// return std::nullopt, otherwise return `nullptr`.
Optional<Value *> getAssumedSimplified(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation,
AA::ValueScope S) {
std::optional<Value *> getAssumedSimplified(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation,
AA::ValueScope S) {
return getAssumedSimplified(IRP, &AA, UsedAssumedInformation, S);
}
Optional<Value *> getAssumedSimplified(const Value &V,
const AbstractAttribute &AA,
bool &UsedAssumedInformation,
AA::ValueScope S) {
std::optional<Value *> getAssumedSimplified(const Value &V,
const AbstractAttribute &AA,
bool &UsedAssumedInformation,
AA::ValueScope S) {
return getAssumedSimplified(IRPosition::value(V), AA,
UsedAssumedInformation, S);
}
Expand All @@ -1774,10 +1774,10 @@ struct Attributor {
/// return std::nullopt, otherwise return `nullptr`. Same as the public
/// version except that it can be used without recording dependences on any \p
/// AA.
Optional<Value *> getAssumedSimplified(const IRPosition &V,
const AbstractAttribute *AA,
bool &UsedAssumedInformation,
AA::ValueScope S);
std::optional<Value *> getAssumedSimplified(const IRPosition &V,
const AbstractAttribute *AA,
bool &UsedAssumedInformation,
AA::ValueScope S);

/// Try to simplify \p IRP and in the scope \p S. If successful, true is
/// returned and all potential values \p IRP can take are put into \p Values.
Expand All @@ -1793,7 +1793,7 @@ struct Attributor {
/// we it will ask `AAValueSimplify`. It is important to ensure this
/// is called before `identifyDefaultAbstractAttributes`, assuming the
/// latter is called at all.
using SimplifictionCallbackTy = std::function<Optional<Value *>(
using SimplifictionCallbackTy = std::function<std::optional<Value *>(
const IRPosition &, const AbstractAttribute *, bool &)>;
void registerSimplificationCallback(const IRPosition &IRP,
const SimplifictionCallbackTy &CB) {
Expand All @@ -1812,8 +1812,8 @@ struct Attributor {

public:
/// Translate \p V from the callee context into the call site context.
Optional<Value *>
translateArgumentToCallSiteContent(Optional<Value *> V, CallBase &CB,
std::optional<Value *>
translateArgumentToCallSiteContent(std::optional<Value *> V, CallBase &CB,
const AbstractAttribute &AA,
bool &UsedAssumedInformation);

Expand Down Expand Up @@ -3999,7 +3999,7 @@ struct ValueSimplifyStateType : public AbstractState {
Type *Ty;

/// Merge \p Other into the currently assumed simplified value
bool unionAssumed(Optional<Value *> Other);
bool unionAssumed(std::optional<Value *> Other);

/// Helper to track validity and fixpoint
BooleanState BS;
Expand All @@ -4008,7 +4008,7 @@ struct ValueSimplifyStateType : public AbstractState {
/// means that the value is not clear under current assumption. If in the
/// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
/// returns orignal associated value.
Optional<Value *> SimplifiedAssociatedValue;
std::optional<Value *> SimplifiedAssociatedValue;
};

/// An abstract interface for value simplify abstract attribute.
Expand Down Expand Up @@ -4043,7 +4043,8 @@ struct AAValueSimplify
/// the Optional::NoneType.
///
/// Use `Attributor::getAssumedSimplified` for value simplification.
virtual Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const = 0;
virtual std::optional<Value *>
getAssumedSimplifiedValue(Attributor &A) const = 0;

friend struct Attributor;
};
Expand Down Expand Up @@ -4100,7 +4101,7 @@ struct AAPrivatizablePtr

/// Return the type we can choose for a private copy of the underlying
/// value. None means it is not clear yet, nullptr means there is none.
virtual Optional<Type *> getPrivatizableType() const = 0;
virtual std::optional<Type *> getPrivatizableType() const = 0;

/// Create an abstract attribute view for the position \p IRP.
static AAPrivatizablePtr &createForPosition(const IRPosition &IRP,
Expand Down Expand Up @@ -4384,7 +4385,7 @@ struct AAValueConstantRange

/// Return an assumed constant for the associated value a program point \p
/// CtxI.
Optional<Constant *>
std::optional<Constant *>
getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
ConstantRange RangeV = getAssumedConstantRange(A, CtxI);
if (auto *C = RangeV.getSingleElement()) {
Expand Down Expand Up @@ -4635,7 +4636,7 @@ struct AAPotentialConstantValues
Attributor &A);

/// Return assumed constant for the associated value
Optional<Constant *>
std::optional<Constant *>
getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
if (!isValidState())
return nullptr;
Expand Down Expand Up @@ -5006,13 +5007,14 @@ struct AAPointerInfo : public AbstractAttribute {
/// An access description.
struct Access {
Access(Instruction *I, int64_t Offset, int64_t Size,
Optional<Value *> Content, AccessKind Kind, Type *Ty)
std::optional<Value *> Content, AccessKind Kind, Type *Ty)
: LocalI(I), RemoteI(I), Content(Content), OAS(Offset, Size),
Kind(Kind), Ty(Ty) {
verify();
}
Access(Instruction *LocalI, Instruction *RemoteI, int64_t Offset,
int64_t Size, Optional<Value *> Content, AccessKind Kind, Type *Ty)
int64_t Size, std::optional<Value *> Content, AccessKind Kind,
Type *Ty)
: LocalI(LocalI), RemoteI(RemoteI), Content(Content), OAS(Offset, Size),
Kind(Kind), Ty(Ty) {
verify();
Expand Down Expand Up @@ -5096,7 +5098,7 @@ struct AAPointerInfo : public AbstractAttribute {

/// Return the written value which can be `llvm::null` if it is not yet
/// determined.
Optional<Value *> getContent() const { return Content; }
std::optional<Value *> getContent() const { return Content; }

/// Return the offset for this access.
int64_t getOffset() const { return OAS.Offset; }
Expand All @@ -5114,7 +5116,7 @@ struct AAPointerInfo : public AbstractAttribute {

/// The value written, if any. `llvm::none` means "not known yet", `nullptr`
/// cannot be determined.
Optional<Value *> Content;
std::optional<Value *> Content;

/// The object accessed, in terms of an offset and size in bytes.
AA::OffsetAndSize OAS;
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Transforms/IPO/SampleContextTracker.h
Expand Up @@ -51,7 +51,7 @@ class ContextTrieNode {
StringRef getFuncName() const;
FunctionSamples *getFunctionSamples() const;
void setFunctionSamples(FunctionSamples *FSamples);
Optional<uint32_t> getFunctionSize() const;
std::optional<uint32_t> getFunctionSize() const;
void addFunctionSize(uint32_t FSize);
LineLocation getCallSiteLoc() const;
ContextTrieNode *getParentContext() const;
Expand All @@ -74,7 +74,7 @@ class ContextTrieNode {
FunctionSamples *FuncSamples;

// Function size for current context
Optional<uint32_t> FuncSize;
std::optional<uint32_t> FuncSize;

// Callsite location in parent context
LineLocation CallSiteLoc;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
Expand Up @@ -479,7 +479,7 @@ static bool findArgParts(Argument *Arg, const DataLayout &DL, AAResults &AAR,
// Returns std::nullopt if this load or store is not based on the argument.
// Return true if we can promote the instruction, false otherwise.
auto HandleEndUser = [&](auto *I, Type *Ty,
bool GuaranteedToExecute) -> Optional<bool> {
bool GuaranteedToExecute) -> std::optional<bool> {
// Don't promote volatile or atomic instructions.
if (!I->isSimple())
return false;
Expand Down Expand Up @@ -553,7 +553,7 @@ static bool findArgParts(Argument *Arg, const DataLayout &DL, AAResults &AAR,

// Look for loads and stores that are guaranteed to execute on entry.
for (Instruction &I : Arg->getParent()->getEntryBlock()) {
Optional<bool> Res{};
std::optional<bool> Res{};
if (LoadInst *LI = dyn_cast<LoadInst>(&I))
Res = HandleEndUser(LI, LI->getType(), /* GuaranteedToExecute */ true);
else if (StoreInst *SI = dyn_cast<StoreInst>(&I))
Expand Down
31 changes: 16 additions & 15 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Expand Up @@ -305,9 +305,10 @@ Value *AA::getWithType(Value &V, Type &Ty) {
return nullptr;
}

Optional<Value *>
AA::combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
const Optional<Value *> &B, Type *Ty) {
std::optional<Value *>
AA::combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
const std::optional<Value *> &B,
Type *Ty) {
if (A == B)
return A;
if (!B)
Expand Down Expand Up @@ -391,7 +392,8 @@ static bool getPotentialCopiesOfMemoryValue(

bool NullOnly = true;
bool NullRequired = false;
auto CheckForNullOnlyAndUndef = [&](Optional<Value *> V, bool IsExact) {
auto CheckForNullOnlyAndUndef = [&](std::optional<Value *> V,
bool IsExact) {
if (!V || *V == nullptr)
NullOnly = false;
else if (isa<UndefValue>(*V))
Expand Down Expand Up @@ -1069,15 +1071,15 @@ void IRPosition::verify() {
#endif
}

Optional<Constant *>
std::optional<Constant *>
Attributor::getAssumedConstant(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
// First check all callbacks provided by outside AAs. If any of them returns
// a non-null value that is different from the associated value, or
// std::nullopt, we assume it's simplified.
for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
Optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
std::optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
if (!SimplifiedV)
return std::nullopt;
if (isa_and_nonnull<Constant>(*SimplifiedV))
Expand All @@ -1099,10 +1101,9 @@ Attributor::getAssumedConstant(const IRPosition &IRP,
return nullptr;
}

Optional<Value *> Attributor::getAssumedSimplified(const IRPosition &IRP,
const AbstractAttribute *AA,
bool &UsedAssumedInformation,
AA::ValueScope S) {
std::optional<Value *> Attributor::getAssumedSimplified(
const IRPosition &IRP, const AbstractAttribute *AA,
bool &UsedAssumedInformation, AA::ValueScope S) {
// First check all callbacks provided by outside AAs. If any of them returns
// a non-null value that is different from the associated value, or
// std::nullopt, we assume it's simplified.
Expand Down Expand Up @@ -1132,7 +1133,7 @@ bool Attributor::getAssumedSimplifiedValues(
// std::nullopt, we assume it's simplified.
const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
for (const auto &CB : SimplificationCBs) {
Optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
std::optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
if (!CBResult.has_value())
continue;
Value *V = CBResult.value();
Expand All @@ -1156,8 +1157,8 @@ bool Attributor::getAssumedSimplifiedValues(
return true;
}

Optional<Value *> Attributor::translateArgumentToCallSiteContent(
Optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
std::optional<Value *> Attributor::translateArgumentToCallSiteContent(
std::optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
if (!V)
return V;
Expand Down Expand Up @@ -2779,7 +2780,7 @@ void InformationCache::initializeInformationCache(const Function &CF,
// queried by abstract attributes during their initialization or update.
// This has to happen before we create attributes.

DenseMap<const Value *, Optional<short>> AssumeUsesMap;
DenseMap<const Value *, std::optional<short>> AssumeUsesMap;

// Add \p V to the assume uses map which track the number of uses outside of
// "visited" assumes. If no outside uses are left the value is added to the
Expand All @@ -2790,7 +2791,7 @@ void InformationCache::initializeInformationCache(const Function &CF,
Worklist.push_back(I);
while (!Worklist.empty()) {
const Instruction *I = Worklist.pop_back_val();
Optional<short> &NumUses = AssumeUsesMap[I];
std::optional<short> &NumUses = AssumeUsesMap[I];
if (!NumUses)
NumUses = I->getNumUses();
NumUses = NumUses.value() - /* this assume */ 1;
Expand Down

0 comments on commit 75801e3

Please sign in to comment.