From eaebb0f61dadd7243bcebad841213c538a25a0a2 Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Sun, 4 Aug 2019 18:37:38 +0000 Subject: [PATCH] [Attributor][NFC] Simplify common pattern wrt. fixpoints When a fixpoint is indicated the change status is known due to the fixpoint kind. This simplifies a common code pattern by making the connection explicit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367790 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/IPO/Attributor.h | 18 +++- lib/Transforms/IPO/Attributor.cpp | 111 +++++++++-------------- 2 files changed, 55 insertions(+), 74 deletions(-) diff --git a/include/llvm/Transforms/IPO/Attributor.h b/include/llvm/Transforms/IPO/Attributor.h index 3c6a9cf0007..6c9c8151d5a 100644 --- a/include/llvm/Transforms/IPO/Attributor.h +++ b/include/llvm/Transforms/IPO/Attributor.h @@ -375,13 +375,17 @@ struct AbstractState { /// /// This will usually make the optimistically assumed state the known to be /// true state. - virtual void indicateOptimisticFixpoint() = 0; + /// + /// \returns ChangeStatus::UNCHANGED as the assumed value should not change. + virtual ChangeStatus indicateOptimisticFixpoint() = 0; /// Indicate that the abstract state should converge to the pessimistic state. /// /// This will usually revert the optimistically assumed state to the known to /// be true state. - virtual void indicatePessimisticFixpoint() = 0; + /// + /// \returns ChangeStatus::CHANGED as the assumed value may change. + virtual ChangeStatus indicatePessimisticFixpoint() = 0; }; /// Simple state with integers encoding. @@ -412,10 +416,16 @@ struct IntegerState : public AbstractState { bool isAtFixpoint() const override { return Assumed == Known; } /// See AbstractState::indicateOptimisticFixpoint(...) - void indicateOptimisticFixpoint() override { Known = Assumed; } + ChangeStatus indicateOptimisticFixpoint() override { + Known = Assumed; + return ChangeStatus::UNCHANGED; + } /// See AbstractState::indicatePessimisticFixpoint(...) - void indicatePessimisticFixpoint() override { Assumed = Known; } + ChangeStatus indicatePessimisticFixpoint() override { + Assumed = Known; + return ChangeStatus::CHANGED; + } /// Return the known state encoding base_t getKnown() const { return Known; } diff --git a/lib/Transforms/IPO/Attributor.cpp b/lib/Transforms/IPO/Attributor.cpp index fabb50fb38a..87bdc0d0aa5 100644 --- a/lib/Transforms/IPO/Attributor.cpp +++ b/lib/Transforms/IPO/Attributor.cpp @@ -488,10 +488,8 @@ ChangeStatus AANoUnwindFunction::updateImpl(Attributor &A) { auto *NoUnwindAA = A.getAAFor(*this, *I); - if (!NoUnwindAA || !NoUnwindAA->isAssumedNoUnwind()) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!NoUnwindAA || !NoUnwindAA->isAssumedNoUnwind()) + return indicatePessimisticFixpoint(); } } return ChangeStatus::UNCHANGED; @@ -625,14 +623,16 @@ public: bool isValidState() const override { return IsValidState; } /// See AbstractState::indicateOptimisticFixpoint(...). - void indicateOptimisticFixpoint() override { + ChangeStatus indicateOptimisticFixpoint() override { IsFixed = true; IsValidState &= true; + return ChangeStatus::UNCHANGED; } - void indicatePessimisticFixpoint() override { + ChangeStatus indicatePessimisticFixpoint() override { IsFixed = true; IsValidState = false; + return ChangeStatus::CHANGED; } }; @@ -1000,10 +1000,8 @@ ChangeStatus AANoSyncFunction::updateImpl(Attributor &A) { continue; if (ICS && (!NoSyncAA || !NoSyncAA->isAssumedNoSync()) && - !ICS.hasFnAttr(Attribute::NoSync)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + !ICS.hasFnAttr(Attribute::NoSync)) + return indicatePessimisticFixpoint(); if (ICS) continue; @@ -1011,8 +1009,7 @@ ChangeStatus AANoSyncFunction::updateImpl(Attributor &A) { if (!isVolatile(I) && !isNonRelaxedAtomic(I)) continue; - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; + return indicatePessimisticFixpoint(); } auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); @@ -1035,8 +1032,7 @@ ChangeStatus AANoSyncFunction::updateImpl(Attributor &A) { if (!ICS.isConvergent()) continue; - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; + return indicatePessimisticFixpoint(); } } @@ -1100,10 +1096,8 @@ ChangeStatus AANoFreeFunction::updateImpl(Attributor &A) { auto *NoFreeAA = A.getAAFor(*this, *I); if ((!NoFreeAA || !NoFreeAA->isAssumedNoFree()) && - !ICS.hasFnAttr(Attribute::NoFree)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + !ICS.hasFnAttr(Attribute::NoFree)) + return indicatePessimisticFixpoint(); } } return ChangeStatus::UNCHANGED; @@ -1203,18 +1197,14 @@ ChangeStatus AANonNullReturned::updateImpl(Attributor &A) { Function &F = getAnchorScope(); auto *AARetVal = A.getAAFor(*this, F); - if (!AARetVal) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetVal) + return indicatePessimisticFixpoint(); std::function &)> Pred = this->generatePredicate(A); - if (!AARetVal->checkForallReturnedValues(Pred)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetVal->checkForallReturnedValues(Pred)) + return indicatePessimisticFixpoint(); return ChangeStatus::UNCHANGED; } @@ -1300,10 +1290,8 @@ ChangeStatus AANonNullArgument::updateImpl(Attributor &A) { return false; }; - if (!A.checkForAllCallSites(F, CallSiteCheck, true, *this)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!A.checkForAllCallSites(F, CallSiteCheck, true, *this)) + return indicatePessimisticFixpoint(); return ChangeStatus::UNCHANGED; } @@ -1315,10 +1303,8 @@ ChangeStatus AANonNullCallSiteArgument::updateImpl(Attributor &A) { auto *NonNullAA = A.getAAFor(*this, V); - if (!NonNullAA || !NonNullAA->isAssumedNonNull()) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!NonNullAA || !NonNullAA->isAssumedNonNull()) + return indicatePessimisticFixpoint(); return ChangeStatus::UNCHANGED; } @@ -1416,10 +1402,8 @@ ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A) { continue; auto *WillReturnAA = A.getAAFor(*this, *I); - if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn()) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn()) + return indicatePessimisticFixpoint(); auto *NoRecurseAA = A.getAAFor(*this, *I); @@ -1428,10 +1412,8 @@ ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A) { // regarded as having recursion. // Code below should be // if ((!NoRecurseAA || !NoRecurseAA->isAssumedNoRecurse()) && - if (!NoRecurseAA && !ICS.hasFnAttr(Attribute::NoRecurse)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!NoRecurseAA && !ICS.hasFnAttr(Attribute::NoRecurse)) + return indicatePessimisticFixpoint(); } } @@ -1492,10 +1474,8 @@ ChangeStatus AANoAliasReturned::updateImpl(Attributor &A) { Function &F = getAnchorScope(); auto *AARetValImpl = A.getAAFor(*this, F); - if (!AARetValImpl) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetValImpl) + return indicatePessimisticFixpoint(); std::function &)> Pred = [&](Value &RV, const SmallPtrSetImpl &RetInsts) -> bool { @@ -1525,10 +1505,8 @@ ChangeStatus AANoAliasReturned::updateImpl(Attributor &A) { return true; }; - if (!AARetValImpl->checkForallReturnedValues(Pred)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetValImpl->checkForallReturnedValues(Pred)) + return indicatePessimisticFixpoint(); return ChangeStatus::UNCHANGED; } @@ -1764,15 +1742,17 @@ struct DerefState : AbstractState { } /// See AbstractState::indicateOptimisticFixpoint(...) - void indicateOptimisticFixpoint() override { + ChangeStatus indicateOptimisticFixpoint() override { DerefBytesState.indicateOptimisticFixpoint(); NonNullGlobalState.indicateOptimisticFixpoint(); + return ChangeStatus::UNCHANGED; } /// See AbstractState::indicatePessimisticFixpoint(...) - void indicatePessimisticFixpoint() override { + ChangeStatus indicatePessimisticFixpoint() override { DerefBytesState.indicatePessimisticFixpoint(); NonNullGlobalState.indicatePessimisticFixpoint(); + return ChangeStatus::CHANGED; } /// Update known dereferenceable bytes. @@ -1958,10 +1938,8 @@ ChangeStatus AADereferenceableReturned::updateImpl(Attributor &A) { syncNonNull(A.getAAFor(*this, F)); auto *AARetVal = A.getAAFor(*this, F); - if (!AARetVal) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetVal) + return indicatePessimisticFixpoint(); bool IsNonNull = isAssumedNonNull(); bool IsGlobal = isAssumedGlobal(); @@ -1979,8 +1957,7 @@ ChangeStatus AADereferenceableReturned::updateImpl(Attributor &A) { ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED; } - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; + return indicatePessimisticFixpoint(); } struct AADereferenceableArgument : AADereferenceableImpl { @@ -2030,10 +2007,8 @@ ChangeStatus AADereferenceableArgument::updateImpl(Attributor &A) { return isValidState(); }; - if (!A.checkForAllCallSites(F, CallSiteCheck, true, *this)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!A.checkForAllCallSites(F, CallSiteCheck, true, *this)) + return indicatePessimisticFixpoint(); updateAssumedNonNullGlobalState(IsNonNull, IsGlobal); @@ -2171,10 +2146,8 @@ struct AAAlignReturned : AAAlignImpl { ChangeStatus AAAlignReturned::updateImpl(Attributor &A) { Function &F = getAnchorScope(); auto *AARetValImpl = A.getAAFor(*this, F); - if (!AARetValImpl) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetValImpl) + return indicatePessimisticFixpoint(); // Currently, align is deduced if alignments in return values are assumed // as greater than n. We reach pessimistic fixpoint if any of the return value @@ -2196,10 +2169,8 @@ ChangeStatus AAAlignReturned::updateImpl(Attributor &A) { return isValidState(); }; - if (!AARetValImpl->checkForallReturnedValues(Pred)) { - indicatePessimisticFixpoint(); - return ChangeStatus::CHANGED; - } + if (!AARetValImpl->checkForallReturnedValues(Pred)) + return indicatePessimisticFixpoint(); return (getAssumed() != BeforeState) ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED; -- 2.40.0