diff --git a/llvm/include/llvm/CodeGen/EdgeBundles.h b/llvm/include/llvm/CodeGen/EdgeBundles.h index b844bd307c197..6e0c301a651e3 100644 --- a/llvm/include/llvm/CodeGen/EdgeBundles.h +++ b/llvm/include/llvm/CodeGen/EdgeBundles.h @@ -18,10 +18,16 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/IntEqClasses.h" #include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/IR/PassManager.h" namespace llvm { +class EdgeBundlesWrapperLegacy; +class EdgeBundlesAnalysis; + +class EdgeBundles { + friend class EdgeBundlesWrapperLegacy; + friend class EdgeBundlesAnalysis; -class EdgeBundles : public MachineFunctionPass { const MachineFunction *MF = nullptr; /// EC - Each edge bundle is an equivalence class. The keys are: @@ -32,10 +38,10 @@ class EdgeBundles : public MachineFunctionPass { /// Blocks - Map each bundle to a list of basic block numbers. SmallVector, 4> Blocks; -public: - static char ID; - EdgeBundles() : MachineFunctionPass(ID) {} + void init(); + EdgeBundles(MachineFunction &MF); +public: /// getBundle - Return the ingoing (Out = false) or outgoing (Out = true) /// bundle number for basic block #N unsigned getBundle(unsigned N, bool Out) const { return EC[2 * N + Out]; } @@ -52,11 +58,34 @@ class EdgeBundles : public MachineFunctionPass { /// view - Visualize the annotated bipartite CFG with Graphviz. void view() const; + // Handle invalidation for the new pass manager + bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &Inv); +}; + +class EdgeBundlesWrapperLegacy : public MachineFunctionPass { +public: + static char ID; + EdgeBundlesWrapperLegacy() : MachineFunctionPass(ID) {} + + EdgeBundles &getEdgeBundles() { return *Impl; } + const EdgeBundles &getEdgeBundles() const { return *Impl; } + private: - bool runOnMachineFunction(MachineFunction&) override; + std::unique_ptr Impl; + bool runOnMachineFunction(MachineFunction &MF) override; void getAnalysisUsage(AnalysisUsage&) const override; }; +class EdgeBundlesAnalysis : public AnalysisInfoMixin { + friend AnalysisInfoMixin; + static AnalysisKey Key; + +public: + using Result = EdgeBundles; + EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM); +}; + } // end namespace llvm #endif diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h index 7698f557c58a0..d1fac4a304cff 100644 --- a/llvm/include/llvm/CodeGen/Passes.h +++ b/llvm/include/llvm/CodeGen/Passes.h @@ -118,7 +118,7 @@ namespace llvm { extern char &MachineRegionInfoPassID; /// EdgeBundles analysis - Bundle machine CFG edges. - extern char &EdgeBundlesID; + extern char &EdgeBundlesWrapperLegacyID; /// LiveVariables pass - This pass computes the set of blocks in which each /// variable is life and sets machine operand kill flags. diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index af93d5e989f65..e883aae275868 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -102,7 +102,7 @@ void initializeEarlyIfConverterLegacyPass(PassRegistry &); void initializeEarlyIfPredicatorPass(PassRegistry &); void initializeEarlyMachineLICMPass(PassRegistry &); void initializeEarlyTailDuplicateLegacyPass(PassRegistry &); -void initializeEdgeBundlesPass(PassRegistry &); +void initializeEdgeBundlesWrapperLegacyPass(PassRegistry &); void initializeEHContGuardCatchretPass(PassRegistry &); void initializeExpandLargeFpConvertLegacyPassPass(PassRegistry &); void initializeExpandLargeDivRemLegacyPassPass(PassRegistry &); diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def index 851561f6b769b..cb1c295d82478 100644 --- a/llvm/include/llvm/Passes/MachinePassRegistry.def +++ b/llvm/include/llvm/Passes/MachinePassRegistry.def @@ -97,6 +97,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass()) // LiveVariables can be removed completely, and LiveIntervals can be directly // computed. (We still either need to regenerate kill flags after regalloc, or // preferably fix the scavenger to not depend on them). +MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis()) MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis()) @@ -114,7 +115,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis()) MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis()) // MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass()) -// MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis()) // MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi", // LazyMachineBlockFrequencyInfoAnalysis()) // MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis()) diff --git a/llvm/lib/CodeGen/EdgeBundles.cpp b/llvm/lib/CodeGen/EdgeBundles.cpp index d3d2bfc616eb5..f433539698349 100644 --- a/llvm/lib/CodeGen/EdgeBundles.cpp +++ b/llvm/lib/CodeGen/EdgeBundles.cpp @@ -26,20 +26,35 @@ static cl::opt ViewEdgeBundles("view-edge-bundles", cl::Hidden, cl::desc("Pop up a window to show edge bundle graphs")); -char EdgeBundles::ID = 0; +char EdgeBundlesWrapperLegacy::ID = 0; -INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges", - /* cfg = */true, /* is_analysis = */ true) +INITIALIZE_PASS(EdgeBundlesWrapperLegacy, "edge-bundles", + "Bundle Machine CFG Edges", + /* cfg = */ true, /* is_analysis = */ true) -char &llvm::EdgeBundlesID = EdgeBundles::ID; +char &llvm::EdgeBundlesWrapperLegacyID = EdgeBundlesWrapperLegacy::ID; -void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const { +void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); } -bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) { - MF = &mf; +AnalysisKey EdgeBundlesAnalysis::Key; + +EdgeBundles EdgeBundlesAnalysis::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + EdgeBundles Impl(MF); + return Impl; +} + +bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) { + Impl.reset(new EdgeBundles(MF)); + return false; +} + +EdgeBundles::EdgeBundles(MachineFunction &MF) : MF(&MF) { init(); } + +void EdgeBundles::init() { EC.clear(); EC.grow(2 * MF->getNumBlockIDs()); @@ -64,8 +79,6 @@ bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) { if (b1 != b0) Blocks[b1].push_back(i); } - - return false; } namespace llvm { @@ -100,3 +113,11 @@ raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G, void EdgeBundles::view() const { ViewGraph(*this, "EdgeBundles"); } + +bool EdgeBundles::invalidate(MachineFunction &MF, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &Inv) { + // Invalidated when CFG is not preserved + auto PAC = PA.getChecker(); + return !PAC.preserved() && !PAC.preservedSet() && + !PAC.preservedSet>(); +} diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp index 80f7af1eaebbe..3542bfe18af46 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.cpp +++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -161,7 +161,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(EdgeBundles) +INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(SpillPlacement) INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis) @@ -216,7 +216,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { AU.addPreserved(); AU.addRequired(); AU.addPreserved(); - AU.addRequired(); + AU.addRequired(); AU.addRequired(); AU.addRequired(); AU.addRequired(); @@ -2730,7 +2730,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { DomTree = &getAnalysis().getDomTree(); ORE = &getAnalysis().getORE(); Loops = &getAnalysis().getLI(); - Bundles = &getAnalysis(); + Bundles = &getAnalysis().getEdgeBundles(); SpillPlacer = &getAnalysis(); DebugVars = &getAnalysis(); diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp index 9f91ee4934159..318e2b19322bb 100644 --- a/llvm/lib/CodeGen/SpillPlacement.cpp +++ b/llvm/lib/CodeGen/SpillPlacement.cpp @@ -50,14 +50,14 @@ char &llvm::SpillPlacementID = SpillPlacement::ID; INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) -INITIALIZE_PASS_DEPENDENCY(EdgeBundles) +INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy) INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequired(); - AU.addRequiredTransitive(); + AU.addRequiredTransitive(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -191,7 +191,7 @@ struct SpillPlacement::Node { bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) { MF = &mf; - bundles = &getAnalysis(); + bundles = &getAnalysis().getEdgeBundles(); assert(!nodes && "Leaking node array"); nodes = new Node[bundles->getNumBundles()]; diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index bc6b449d22abe..cf7ceed63607a 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -85,6 +85,7 @@ #include "llvm/CodeGen/DeadMachineInstructionElim.h" #include "llvm/CodeGen/DwarfEHPrepare.h" #include "llvm/CodeGen/EarlyIfConversion.h" +#include "llvm/CodeGen/EdgeBundles.h" #include "llvm/CodeGen/ExpandLargeDivRem.h" #include "llvm/CodeGen/ExpandLargeFpConvert.h" #include "llvm/CodeGen/ExpandMemCmp.h" diff --git a/llvm/lib/Target/X86/X86FloatingPoint.cpp b/llvm/lib/Target/X86/X86FloatingPoint.cpp index ea94a4be32b2f..34d8b774a186a 100644 --- a/llvm/lib/Target/X86/X86FloatingPoint.cpp +++ b/llvm/lib/Target/X86/X86FloatingPoint.cpp @@ -67,7 +67,7 @@ namespace { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); - AU.addRequired(); + AU.addRequired(); AU.addPreservedID(MachineLoopInfoID); AU.addPreservedID(MachineDominatorsID); MachineFunctionPass::getAnalysisUsage(AU); @@ -303,7 +303,7 @@ char FPS::ID = 0; INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false) -INITIALIZE_PASS_DEPENDENCY(EdgeBundles) +INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy) INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false) @@ -337,7 +337,7 @@ bool FPS::runOnMachineFunction(MachineFunction &MF) { // Early exit. if (!FPIsUsed) return false; - Bundles = &getAnalysis(); + Bundles = &getAnalysis().getEdgeBundles(); TII = MF.getSubtarget().getInstrInfo(); // Prepare cross-MBB liveness.