Skip to content

Commit

Permalink
[PM] Introduce an analysis set used to preserve all analyses over
Browse files Browse the repository at this point in the history
a function's CFG when that CFG is unchanged.

This allows transformation passes to simply claim they preserve the CFG
and analysis passes to check for the CFG being preserved to remove the
fanout of all analyses being listed in all passes.

I've gone through and removed or cleaned up as many of the comments
reminding us to do this as I could.

Differential Revision: https://reviews.llvm.org/D28627

llvm-svn: 292054
  • Loading branch information
chandlerc committed Jan 15, 2017
1 parent f1388ef commit ca68a3e
Show file tree
Hide file tree
Showing 40 changed files with 182 additions and 73 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/BlockFrequencyInfo.h
Expand Up @@ -45,6 +45,10 @@ class BlockFrequencyInfo {

~BlockFrequencyInfo();

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);

const Function *getFunction() const;
const BranchProbabilityInfo *getBPI() const;
void view() const;
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/DominanceFrontier.h
Expand Up @@ -141,6 +141,10 @@ class DominanceFrontier : public ForwardDominanceFrontierBase<BasicBlock> {
typedef DominanceFrontierBase<BasicBlock>::DomSetType DomSetType;
typedef DominanceFrontierBase<BasicBlock>::iterator iterator;
typedef DominanceFrontierBase<BasicBlock>::const_iterator const_iterator;

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);
};

class DominanceFrontierWrapperPass : public FunctionPass {
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/LoopInfo.h
Expand Up @@ -682,6 +682,10 @@ class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
return *this;
}

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);

// Most of the public interface is provided via LoopInfoBase.

/// Update LoopInfo after removing the last backedge from a loop. This updates
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/PostDominators.h
Expand Up @@ -26,6 +26,10 @@ struct PostDominatorTree : public DominatorTreeBase<BasicBlock> {
typedef DominatorTreeBase<BasicBlock> Base;

PostDominatorTree() : DominatorTreeBase<BasicBlock>(true) {}

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);
};

/// \brief Analysis pass which computes a \c PostDominatorTree.
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Analysis/RegionInfo.h
Expand Up @@ -886,6 +886,10 @@ class RegionInfo : public RegionInfoBase<RegionTraits<Function>> {
return *this;
}

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);

// updateStatistics - Update statistic about created regions.
void updateStatistics(Region *R) final;

Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/Dominators.h
Expand Up @@ -102,6 +102,10 @@ class DominatorTree : public DominatorTreeBase<BasicBlock> {
recalculate(F);
}

/// Handle invalidation explicitly.
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &);

/// \brief Returns *false* if the other dominator tree matches this dominator
/// tree.
inline bool compare(const DominatorTree &Other) const {
Expand Down
63 changes: 40 additions & 23 deletions llvm/include/llvm/IR/PassManager.h
Expand Up @@ -73,6 +73,46 @@ struct alignas(8) AnalysisKey {};
/// if it is, the analysis knows that it itself is preserved.
struct alignas(8) AnalysisSetKey {};

/// This templated class represents "all analyses that operate over \<a
/// particular IR unit\>" (e.g. a Function or a Module) in instances of
/// PreservedAnalysis.
///
/// This lets a transformation say e.g. "I preserved all function analyses".
///
/// Note that you must provide an explicit instantiation declaration and
/// definition for this template in order to get the correct behavior on
/// Windows. Otherwise, the address of SetKey will not be stable.
template <typename IRUnitT> class AllAnalysesOn {
public:
static AnalysisSetKey *ID() { return &SetKey; }

private:
static AnalysisSetKey SetKey;
};

template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;

extern template class AllAnalysesOn<Module>;
extern template class AllAnalysesOn<Function>;

/// Represents analyses that only rely on functions' control flow.
///
/// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
/// to query whether it has been preserved.
///
/// The CFG of a function is defined as the set of basic blocks and the edges
/// between them. Changing the set of basic blocks in a function is enough to
/// mutate the CFG. Mutating the condition of a branch or argument of an
/// invoked function does not mutate the CFG, but changing the successor labels
/// of those instructions does.
class CFGAnalyses {
public:
static AnalysisSetKey *ID() { return &SetKey; }

private:
static AnalysisSetKey SetKey;
};

/// A set of analyses that are preserved following a run of a transformation
/// pass.
///
Expand Down Expand Up @@ -342,29 +382,6 @@ struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
static AnalysisKey *ID() { return &DerivedT::Key; }
};

/// This templated class represents "all analyses that operate over \<a
/// particular IR unit\>" (e.g. a Function or a Module) in instances of
/// PreservedAnalysis.
///
/// This lets a transformation say e.g. "I preserved all function analyses".
///
/// Note that you must provide an explicit instantiation declaration and
/// definition for this template in order to get the correct behavior on
/// Windows. Otherwise, the address of SetKey will not be stable.
template <typename IRUnitT>
class AllAnalysesOn {
public:
static AnalysisSetKey *ID() { return &SetKey; }

private:
static AnalysisSetKey SetKey;
};

template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;

extern template class AllAnalysesOn<Module>;
extern template class AllAnalysesOn<Function>;

/// \brief Manages a sequence of passes over a particular unit of IR.
///
/// A pass manager contains a sequence of passes to run over a particular unit
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/BlockFrequencyInfo.cpp
Expand Up @@ -132,6 +132,15 @@ BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
// template instantiated which is not available in the header.
BlockFrequencyInfo::~BlockFrequencyInfo() {}

bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<BlockFrequencyAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

void BlockFrequencyInfo::calculate(const Function &F,
const BranchProbabilityInfo &BPI,
const LoopInfo &LI) {
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Analysis/DominanceFrontier.cpp
Expand Up @@ -56,6 +56,16 @@ LLVM_DUMP_METHOD void DominanceFrontierWrapperPass::dump() const {
}
#endif

/// Handle invalidation explicitly.
bool DominanceFrontier::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<DominanceFrontierAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

AnalysisKey DominanceFrontierAnalysis::Key;

DominanceFrontier DominanceFrontierAnalysis::run(Function &F,
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/LoopInfo.cpp
Expand Up @@ -610,6 +610,15 @@ LoopInfo::LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree) {
analyze(DomTree);
}

bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<LoopAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

void LoopInfo::markAsRemoved(Loop *Unloop) {
assert(!Unloop->isInvalid() && "Loop has already been removed");
Unloop->invalidate();
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/PostDominators.cpp
Expand Up @@ -31,6 +31,15 @@ char PostDominatorTreeWrapperPass::ID = 0;
INITIALIZE_PASS(PostDominatorTreeWrapperPass, "postdomtree",
"Post-Dominator Tree Construction", true, true)

bool PostDominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<PostDominatorTreeAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
DT.recalculate(F);
return false;
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/RegionInfo.cpp
Expand Up @@ -83,6 +83,15 @@ RegionInfo::~RegionInfo() {

}

bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<RegionInfoAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

void RegionInfo::updateStatistics(Region *R) {
++numRegions;

Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/IR/Dominators.cpp
Expand Up @@ -73,6 +73,15 @@ template void llvm::Calculate<Function, Inverse<BasicBlock *>>(
GraphTraits<Inverse<BasicBlock *>>::NodeRef>::type> &DT,
Function &F);

bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
// Check whether the analysis, all analyses on functions, or the function's
// CFG have been preserved.
auto PAC = PA.getChecker<DominatorTreeAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}

// dominates - Return true if Def dominates a use in User. This performs
// the special checks necessary if Def and User are in the same basic block.
// Note that Def doesn't dominate a use in Def itself!
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/PassManager.cpp
Expand Up @@ -91,4 +91,6 @@ bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
}
}

AnalysisSetKey CFGAnalyses::SetKey;

AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -3176,10 +3176,9 @@ PreservedAnalyses InstCombinePass::run(Function &F,
return PreservedAnalyses::all();

// Mark all the analyses that instcombine updates as preserved.
// FIXME: This should also 'preserve the CFG'.
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
PA.preserve<AAManager>();
PA.preserve<DominatorTreeAnalysis>();
PA.preserve<GlobalsAA>();
return PA;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/ADCE.cpp
Expand Up @@ -644,8 +644,8 @@ PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
if (!AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination())
return PreservedAnalyses::all();

// FIXME: This should also 'preserve the CFG'.
auto PA = PreservedAnalyses();
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
PA.preserve<GlobalsAA>();
return PA;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/AlignmentFromAssumptions.cpp
Expand Up @@ -446,11 +446,11 @@ AlignmentFromAssumptionsPass::run(Function &F, FunctionAnalysisManager &AM) {

if (!Changed)
return PreservedAnalyses::all();

PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
PA.preserve<AAManager>();
PA.preserve<ScalarEvolutionAnalysis>();
PA.preserve<GlobalsAA>();
PA.preserve<LoopAnalysis>();
PA.preserve<DominatorTreeAnalysis>();
return PA;
}
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/BDCE.cpp
Expand Up @@ -80,8 +80,8 @@ PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
if (!bitTrackingDCE(F, DB))
return PreservedAnalyses::all();

// FIXME: This should also 'preserve the CFG'.
auto PA = PreservedAnalyses();
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
PA.preserve<GlobalsAA>();
return PA;
}
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
Expand Up @@ -623,6 +623,7 @@ PreservedAnalyses ConstantHoistingPass::run(Function &F,
if (!runImpl(F, TTI, DT, F.getEntryBlock()))
return PreservedAnalyses::all();

// FIXME: This should also 'preserve the CFG'.
return PreservedAnalyses::none();
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}
9 changes: 6 additions & 3 deletions llvm/lib/Transforms/Scalar/DCE.cpp
Expand Up @@ -124,9 +124,12 @@ static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
}

PreservedAnalyses DCEPass::run(Function &F, FunctionAnalysisManager &AM) {
if (eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
return PreservedAnalyses::none();
return PreservedAnalyses::all();
if (!eliminateDeadCode(F, AM.getCachedResult<TargetLibraryAnalysis>(F)))
return PreservedAnalyses::all();

PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}

namespace {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Expand Up @@ -1186,8 +1186,9 @@ PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) {

if (!eliminateDeadStores(F, AA, MD, DT, TLI))
return PreservedAnalyses::all();

PreservedAnalyses PA;
PA.preserve<DominatorTreeAnalysis>();
PA.preserveSet<CFGAnalyses>();
PA.preserve<GlobalsAA>();
PA.preserve<MemoryDependenceAnalysis>();
return PA;
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Expand Up @@ -967,10 +967,8 @@ PreservedAnalyses EarlyCSEPass::run(Function &F,
if (!CSE.run())
return PreservedAnalyses::all();

// CSE preserves the dominator tree because it doesn't mutate the CFG.
// FIXME: Bundle this with other CFG-preservation.
PreservedAnalyses PA;
PA.preserve<DominatorTreeAnalysis>();
PA.preserveSet<CFGAnalyses>();
PA.preserve<GlobalsAA>();
if (UseMemorySSA)
PA.preserve<MemorySSAAnalysis>();
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/Transforms/Scalar/Float2Int.cpp
Expand Up @@ -516,11 +516,10 @@ FunctionPass *createFloat2IntPass() { return new Float2IntLegacyPass(); }
PreservedAnalyses Float2IntPass::run(Function &F, FunctionAnalysisManager &) {
if (!runImpl(F))
return PreservedAnalyses::all();
else {
// FIXME: This should also 'preserve the CFG'.
PreservedAnalyses PA;
PA.preserve<GlobalsAA>();
return PA;
}

PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
PA.preserve<GlobalsAA>();
return PA;
}
} // End namespace llvm
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/Scalar/GuardWidening.cpp
Expand Up @@ -658,8 +658,12 @@ PreservedAnalyses GuardWideningPass::run(Function &F,
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
auto &LI = AM.getResult<LoopAnalysis>(F);
auto &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
bool Changed = GuardWideningImpl(DT, PDT, LI).run();
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
if (!GuardWideningImpl(DT, PDT, LI).run())
return PreservedAnalyses::all();

PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}

StringRef GuardWideningImpl::scoreTypeToString(WideningScore WS) {
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
Expand Up @@ -2492,8 +2492,9 @@ PreservedAnalyses IndVarSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
if (!IVS.run(&L))
return PreservedAnalyses::all();

// FIXME: This should also 'preserve the CFG'.
return getLoopPassPreservedAnalyses();
auto PA = getLoopPassPreservedAnalyses();
PA.preserveSet<CFGAnalyses>();
return PA;
}

namespace {
Expand Down

0 comments on commit ca68a3e

Please sign in to comment.