diff --git a/include/llvm/IR/PassManager.h b/include/llvm/IR/PassManager.h index b2bf73ffe8b..59855b5fc91 100644 --- a/include/llvm/IR/PassManager.h +++ b/include/llvm/IR/PassManager.h @@ -141,15 +141,19 @@ public: PreservedPassIDs.count(PassID); } + /// \brief Test whether all passes are preserved. + /// + /// This is used primarily to optimize for the case of no changes which will + /// common in many scenarios. + bool areAllPreserved() const { + return PreservedPassIDs.count((void *)AllPassesID); + } + private: // Note that this must not be -1 or -2 as those are already used by the // SmallPtrSet. static const uintptr_t AllPassesID = (intptr_t)(-3); - bool areAllPreserved() const { - return PreservedPassIDs.count((void *)AllPassesID); - } - SmallPtrSet PreservedPassIDs; }; diff --git a/lib/Analysis/CGSCCPassManager.cpp b/lib/Analysis/CGSCCPassManager.cpp index 4d569a972fe..ad7eea8efca 100644 --- a/lib/Analysis/CGSCCPassManager.cpp +++ b/lib/Analysis/CGSCCPassManager.cpp @@ -92,6 +92,10 @@ void CGSCCAnalysisManager::invalidateImpl(void *PassID, LazyCallGraph::SCC &C) { void CGSCCAnalysisManager::invalidateImpl(LazyCallGraph::SCC &C, const PreservedAnalyses &PA) { + // Short circuit for a common case of all analyses being preserved. + if (PA.areAllPreserved()) + return; + if (DebugPM) dbgs() << "Invalidating all non-preserved analyses for SCC: " << C.getName() << "\n"; diff --git a/lib/IR/PassManager.cpp b/lib/IR/PassManager.cpp index c7638bb7fce..1eab4ae19bf 100644 --- a/lib/IR/PassManager.cpp +++ b/lib/IR/PassManager.cpp @@ -78,6 +78,10 @@ void ModuleAnalysisManager::invalidateImpl(void *PassID, Module &M) { void ModuleAnalysisManager::invalidateImpl(Module &M, const PreservedAnalyses &PA) { + // Short circuit for a common case of all analyses being preserved. + if (PA.areAllPreserved()) + return; + if (DebugPM) dbgs() << "Invalidating all non-preserved analyses for module: " << M.getModuleIdentifier() << "\n"; @@ -176,6 +180,10 @@ void FunctionAnalysisManager::invalidateImpl(void *PassID, Function &F) { void FunctionAnalysisManager::invalidateImpl(Function &F, const PreservedAnalyses &PA) { + // Short circuit for a common case of all analyses being preserved. + if (PA.areAllPreserved()) + return; + if (DebugPM) dbgs() << "Invalidating all non-preserved analyses for function: " << F.getName() << "\n"; diff --git a/test/Other/new-pass-manager.ll b/test/Other/new-pass-manager.ll index f9fc444dbf1..bb338d42253 100644 --- a/test/Other/new-pass-manager.ll +++ b/test/Other/new-pass-manager.ll @@ -94,6 +94,13 @@ ; CHECK-LCG-ANALYSIS: Running module analysis: Lazy CallGraph Analysis ; CHECK-LCG-ANALYSIS: Starting CGSCC pass manager run. +; Make sure no-op passes that preserve all analyses don't even try to do any +; analysis invalidation. +; RUN: opt -disable-output -debug-pass-manager -debug-cgscc-pass-manager -passes='cgscc(function(no-op-function))' %s 2>&1 \ +; RUN: | FileCheck %s --check-prefix=CHECK-NO-OP-INVALIDATION +; CHECK-NO-OP-INVALIDATION: Starting module pass manager +; CHECK-NO-OP-INVALIDATION-NOT: Invalidating all non-preserved analyses + define void @foo() { ret void }