diff --git a/llvm/lib/CodeGen/SpillPlacement.h b/llvm/include/llvm/CodeGen/SpillPlacement.h similarity index 83% rename from llvm/lib/CodeGen/SpillPlacement.h rename to llvm/include/llvm/CodeGen/SpillPlacement.h index 5fd9b085259d1..1ef37f2718a65 100644 --- a/llvm/lib/CodeGen/SpillPlacement.h +++ b/llvm/include/llvm/CodeGen/SpillPlacement.h @@ -29,6 +29,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SparseSet.h" +#include "llvm/CodeGen/MachineFunctionAnalysis.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/Support/BlockFrequency.h" @@ -38,13 +39,20 @@ class BitVector; class EdgeBundles; class MachineBlockFrequencyInfo; class MachineFunction; +class SpillPlacementWrapperLegacy; +class SpillPlacementAnalysis; + +class SpillPlacement { + friend class SpillPlacementWrapperLegacy; + friend class SpillPlacementAnalysis; -class SpillPlacement : public MachineFunctionPass { struct Node; + const MachineFunction *MF = nullptr; const EdgeBundles *bundles = nullptr; const MachineBlockFrequencyInfo *MBFI = nullptr; - Node *nodes = nullptr; + + std::unique_ptr nodes; // Nodes that are active in the current computation. Owned by the prepare() // caller. @@ -68,11 +76,6 @@ class SpillPlacement : public MachineFunctionPass { SparseSet TodoList; public: - static char ID; // Pass identification, replacement for typeid. - - SpillPlacement() : MachineFunctionPass(ID) {} - ~SpillPlacement() override { releaseMemory(); } - /// BorderConstraint - A basic block has separate constraints for entry and /// exit. enum BorderConstraint { @@ -154,17 +157,50 @@ class SpillPlacement : public MachineFunctionPass { return BlockFrequencies[Number]; } + bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &Inv); + + SpillPlacement(SpillPlacement &&); + ~SpillPlacement(); + private: - bool runOnMachineFunction(MachineFunction &mf) override; - void getAnalysisUsage(AnalysisUsage &AU) const override; - void releaseMemory() override; + SpillPlacement(); + void releaseMemory(); + + void run(MachineFunction &MF, EdgeBundles *Bundles, + MachineBlockFrequencyInfo *MBFI); void activate(unsigned n); void setThreshold(BlockFrequency Entry); bool update(unsigned n); }; +class SpillPlacementWrapperLegacy : public MachineFunctionPass { +public: + static char ID; + SpillPlacementWrapperLegacy() : MachineFunctionPass(ID) {} + + SpillPlacement &getResult() { return Impl; } + const SpillPlacement &getResult() const { return Impl; } + +private: + SpillPlacement Impl; + bool runOnMachineFunction(MachineFunction &MF) override; + void getAnalysisUsage(AnalysisUsage &AU) const override; + void releaseMemory() override { Impl.releaseMemory(); } +}; + +class SpillPlacementAnalysis + : public AnalysisInfoMixin { + friend AnalysisInfoMixin; + static AnalysisKey Key; + +public: + using Result = SpillPlacement; + SpillPlacement run(MachineFunction &, MachineFunctionAnalysisManager &); +}; + } // end namespace llvm #endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index e883aae275868..88bca2c75c949 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -289,7 +289,7 @@ void initializeSinkingLegacyPassPass(PassRegistry &); void initializeSjLjEHPreparePass(PassRegistry &); void initializeSlotIndexesWrapperPassPass(PassRegistry &); void initializeSpeculativeExecutionLegacyPassPass(PassRegistry &); -void initializeSpillPlacementPass(PassRegistry &); +void initializeSpillPlacementWrapperLegacyPass(PassRegistry &); void initializeStackColoringLegacyPass(PassRegistry &); void initializeStackFrameLayoutAnalysisPassPass(PassRegistry &); void initializeStackMapLivenessPass(PassRegistry &); diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def index cb1c295d82478..437ec39beb040 100644 --- a/llvm/include/llvm/Passes/MachinePassRegistry.def +++ b/llvm/include/llvm/Passes/MachinePassRegistry.def @@ -113,6 +113,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree", MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics", MachineTraceMetricsAnalysis()) MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC)) MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis()) +MACHINE_FUNCTION_ANALYSIS("spill-code-placement", SpillPlacementAnalysis()) MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis()) // MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass()) // MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi", diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp index 3542bfe18af46..d0d2c585f0b54 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.cpp +++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp @@ -17,7 +17,6 @@ #include "RegAllocBase.h" #include "RegAllocEvictionAdvisor.h" #include "RegAllocPriorityAdvisor.h" -#include "SpillPlacement.h" #include "SplitKit.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitVector.h" @@ -50,6 +49,7 @@ #include "llvm/CodeGen/RegAllocRegistry.h" #include "llvm/CodeGen/RegisterClassInfo.h" #include "llvm/CodeGen/SlotIndexes.h" +#include "llvm/CodeGen/SpillPlacement.h" #include "llvm/CodeGen/Spiller.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/CodeGen/TargetRegisterInfo.h" @@ -162,7 +162,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy) -INITIALIZE_PASS_DEPENDENCY(SpillPlacement) +INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy) INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis) INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysis) @@ -217,7 +217,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired(); AU.addPreserved(); AU.addRequired(); - AU.addRequired(); + AU.addRequired(); AU.addRequired(); AU.addRequired(); AU.addRequired(); @@ -2731,7 +2731,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { ORE = &getAnalysis().getORE(); Loops = &getAnalysis().getLI(); Bundles = &getAnalysis().getEdgeBundles(); - SpillPlacer = &getAnalysis(); + SpillPlacer = &getAnalysis().getResult(); DebugVars = &getAnalysis(); initializeCSRCost(); diff --git a/llvm/lib/CodeGen/RegAllocGreedy.h b/llvm/lib/CodeGen/RegAllocGreedy.h index 2e7608a53e9ce..9578b8d3bef87 100644 --- a/llvm/lib/CodeGen/RegAllocGreedy.h +++ b/llvm/lib/CodeGen/RegAllocGreedy.h @@ -16,7 +16,6 @@ #include "RegAllocBase.h" #include "RegAllocEvictionAdvisor.h" #include "RegAllocPriorityAdvisor.h" -#include "SpillPlacement.h" #include "SplitKit.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitVector.h" @@ -30,6 +29,7 @@ #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/RegisterClassInfo.h" +#include "llvm/CodeGen/SpillPlacement.h" #include "llvm/CodeGen/Spiller.h" #include "llvm/CodeGen/TargetRegisterInfo.h" #include diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp index 318e2b19322bb..55a96a22a00ec 100644 --- a/llvm/lib/CodeGen/SpillPlacement.cpp +++ b/llvm/lib/CodeGen/SpillPlacement.cpp @@ -26,7 +26,7 @@ // //===----------------------------------------------------------------------===// -#include "SpillPlacement.h" +#include "llvm/CodeGen/SpillPlacement.h" #include "llvm/ADT/BitVector.h" #include "llvm/CodeGen/EdgeBundles.h" #include "llvm/CodeGen/MachineBasicBlock.h" @@ -44,17 +44,17 @@ using namespace llvm; #define DEBUG_TYPE "spill-code-placement" -char SpillPlacement::ID = 0; +char SpillPlacementWrapperLegacy::ID = 0; -char &llvm::SpillPlacementID = SpillPlacement::ID; +char &llvm::SpillPlacementID = SpillPlacementWrapperLegacy::ID; -INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE, +INITIALIZE_PASS_BEGIN(SpillPlacementWrapperLegacy, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy) -INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE, +INITIALIZE_PASS_END(SpillPlacementWrapperLegacy, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) -void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const { +void SpillPlacementWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequired(); AU.addRequiredTransitive(); @@ -189,32 +189,64 @@ struct SpillPlacement::Node { } }; -bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) { +bool SpillPlacementWrapperLegacy::runOnMachineFunction(MachineFunction &MF) { + auto *Bundles = &getAnalysis().getEdgeBundles(); + auto *MBFI = &getAnalysis().getMBFI(); + + Impl.run(MF, Bundles, MBFI); + return false; +} + +AnalysisKey SpillPlacementAnalysis::Key; + +SpillPlacement +SpillPlacementAnalysis::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + auto *Bundles = &MFAM.getResult(MF); + auto *MBFI = &MFAM.getResult(MF); + SpillPlacement Impl; + Impl.run(MF, Bundles, MBFI); + return Impl; +} + +bool SpillPlacementAnalysis::Result::invalidate( + MachineFunction &MF, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &Inv) { + auto PAC = PA.getChecker(); + if (!PAC.preserved() && !PAC.preservedSet>()) + return true; + // Check dependencies. + return Inv.invalidate(MF, PA) || + Inv.invalidate(MF, PA); +} + +SpillPlacement::SpillPlacement() = default; +SpillPlacement::~SpillPlacement() = default; +SpillPlacement::SpillPlacement(SpillPlacement &&) = default; + +void SpillPlacement::releaseMemory() { + nodes.reset(); + TodoList.clear(); +} + +void SpillPlacement::run(MachineFunction &mf, EdgeBundles *Bundles, + MachineBlockFrequencyInfo *MBFI) { MF = &mf; - bundles = &getAnalysis().getEdgeBundles(); + this->bundles = Bundles; + this->MBFI = MBFI; assert(!nodes && "Leaking node array"); - nodes = new Node[bundles->getNumBundles()]; + nodes.reset(new Node[bundles->getNumBundles()]); TodoList.clear(); TodoList.setUniverse(bundles->getNumBundles()); // Compute total ingoing and outgoing block frequencies for all bundles. BlockFrequencies.resize(mf.getNumBlockIDs()); - MBFI = &getAnalysis().getMBFI(); setThreshold(MBFI->getEntryFreq()); for (auto &I : mf) { unsigned Num = I.getNumber(); BlockFrequencies[Num] = MBFI->getBlockFreq(&I); } - - // We never change the function. - return false; -} - -void SpillPlacement::releaseMemory() { - delete[] nodes; - nodes = nullptr; - TodoList.clear(); } /// activate - mark node n as active if it wasn't already. @@ -323,9 +355,9 @@ bool SpillPlacement::scanActiveBundles() { } bool SpillPlacement::update(unsigned n) { - if (!nodes[n].update(nodes, Threshold)) + if (!nodes[n].update(nodes.get(), Threshold)) return false; - nodes[n].getDissentingNeighbors(TodoList, nodes); + nodes[n].getDissentingNeighbors(TodoList, nodes.get()); return true; } diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index cf7ceed63607a..ba52a37df9c25 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -130,6 +130,7 @@ #include "llvm/CodeGen/ShadowStackGCLowering.h" #include "llvm/CodeGen/SjLjEHPrepare.h" #include "llvm/CodeGen/SlotIndexes.h" +#include "llvm/CodeGen/SpillPlacement.h" #include "llvm/CodeGen/StackColoring.h" #include "llvm/CodeGen/StackProtector.h" #include "llvm/CodeGen/TailDuplication.h"