@@ -39,47 +39,66 @@ using namespace llvm;
3939
4040#define DEBUG_TYPE " machine-trace-metrics"
4141
42- char MachineTraceMetrics::ID = 0 ;
42+ AnalysisKey MachineTraceMetricsAnalysis::Key ;
4343
44- char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
44+ MachineTraceMetricsAnalysis::Result
45+ MachineTraceMetricsAnalysis::run (MachineFunction &MF,
46+ MachineFunctionAnalysisManager &MFAM) {
47+ return Result (MF, MFAM.getResult <MachineLoopAnalysis>(MF));
48+ }
49+
50+ PreservedAnalyses
51+ MachineTraceMetricsVerifierPass::run (MachineFunction &MF,
52+ MachineFunctionAnalysisManager &MFAM) {
53+ MFAM.getResult <MachineTraceMetricsAnalysis>(MF).verifyAnalysis ();
54+ return PreservedAnalyses::all ();
55+ }
4556
46- INITIALIZE_PASS_BEGIN (MachineTraceMetrics, DEBUG_TYPE, " Machine Trace Metrics" ,
47- false , true )
57+ char MachineTraceMetricsWrapperPass::ID = 0 ;
58+
59+ char &llvm::MachineTraceMetricsID = MachineTraceMetricsWrapperPass::ID;
60+
61+ INITIALIZE_PASS_BEGIN (MachineTraceMetricsWrapperPass, DEBUG_TYPE,
62+ " Machine Trace Metrics" , false , true )
4863INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
49- INITIALIZE_PASS_END(MachineTraceMetrics , DEBUG_TYPE,
64+ INITIALIZE_PASS_END(MachineTraceMetricsWrapperPass , DEBUG_TYPE,
5065 " Machine Trace Metrics" , false , true )
5166
52- MachineTraceMetrics::MachineTraceMetrics() : MachineFunctionPass(ID) {
53- std::fill (std::begin (Ensembles), std::end (Ensembles), nullptr );
54- }
67+ MachineTraceMetricsWrapperPass::MachineTraceMetricsWrapperPass()
68+ : MachineFunctionPass(ID) {}
5569
56- void MachineTraceMetrics ::getAnalysisUsage (AnalysisUsage &AU) const {
70+ void MachineTraceMetricsWrapperPass ::getAnalysisUsage (AnalysisUsage &AU) const {
5771 AU.setPreservesAll ();
5872 AU.addRequired <MachineLoopInfoWrapperPass>();
5973 MachineFunctionPass::getAnalysisUsage (AU);
6074}
6175
62- bool MachineTraceMetrics::runOnMachineFunction (MachineFunction &Func) {
76+ void MachineTraceMetrics::init (MachineFunction &Func,
77+ const MachineLoopInfo &LI) {
6378 MF = &Func;
6479 const TargetSubtargetInfo &ST = MF->getSubtarget ();
6580 TII = ST.getInstrInfo ();
6681 TRI = ST.getRegisterInfo ();
6782 MRI = &MF->getRegInfo ();
68- Loops = &getAnalysis<MachineLoopInfoWrapperPass>(). getLI () ;
83+ Loops = &LI ;
6984 SchedModel.init (&ST);
7085 BlockInfo.resize (MF->getNumBlockIDs ());
7186 ProcReleaseAtCycles.resize (MF->getNumBlockIDs () *
7287 SchedModel.getNumProcResourceKinds ());
88+ }
89+
90+ bool MachineTraceMetricsWrapperPass::runOnMachineFunction (MachineFunction &MF) {
91+ MTM.init (MF, getAnalysis<MachineLoopInfoWrapperPass>().getLI ());
7392 return false ;
7493}
7594
76- void MachineTraceMetrics::releaseMemory () {
95+ MachineTraceMetrics::~MachineTraceMetrics () { clear (); }
96+
97+ void MachineTraceMetrics::clear () {
7798 MF = nullptr ;
7899 BlockInfo.clear ();
79- for (Ensemble *&E : Ensembles) {
80- delete E;
81- E = nullptr ;
82- }
100+ for (auto &E : Ensembles)
101+ E.reset ();
83102}
84103
85104// ===----------------------------------------------------------------------===//
@@ -395,35 +414,50 @@ MachineTraceMetrics::Ensemble *
395414MachineTraceMetrics::getEnsemble (MachineTraceStrategy strategy) {
396415 assert (strategy < MachineTraceStrategy::TS_NumStrategies &&
397416 " Invalid trace strategy enum" );
398- Ensemble *&E = Ensembles[static_cast <size_t >(strategy)];
417+ std::unique_ptr<MachineTraceMetrics::Ensemble> &E =
418+ Ensembles[static_cast <size_t >(strategy)];
399419 if (E)
400- return E;
420+ return E. get () ;
401421
402422 // Allocate new Ensemble on demand.
403423 switch (strategy) {
404424 case MachineTraceStrategy::TS_MinInstrCount:
405- return (E = new MinInstrCountEnsemble (this ));
425+ E = std::make_unique<MinInstrCountEnsemble>(MinInstrCountEnsemble (this ));
426+ break ;
406427 case MachineTraceStrategy::TS_Local:
407- return (E = new LocalEnsemble (this ));
428+ E = std::make_unique<LocalEnsemble>(LocalEnsemble (this ));
429+ break ;
408430 default : llvm_unreachable (" Invalid trace strategy enum" );
409431 }
432+ return E.get ();
410433}
411434
412435void MachineTraceMetrics::invalidate (const MachineBasicBlock *MBB) {
413436 LLVM_DEBUG (dbgs () << " Invalidate traces through " << printMBBReference (*MBB)
414437 << ' \n ' );
415438 BlockInfo[MBB->getNumber ()].invalidate ();
416- for (Ensemble * E : Ensembles)
439+ for (auto & E : Ensembles)
417440 if (E)
418441 E->invalidate (MBB);
419442}
420443
444+ bool MachineTraceMetrics::invalidate (
445+ MachineFunction &, const PreservedAnalyses &PA,
446+ MachineFunctionAnalysisManager::Invalidator &) {
447+ // Check whether the analysis, all analyses on machine functions, or the
448+ // machine function's CFG have been preserved.
449+ auto PAC = PA.getChecker <MachineTraceMetricsAnalysis>();
450+ return !PAC.preserved () &&
451+ !PAC.preservedSet <AllAnalysesOn<MachineFunction>>() &&
452+ !PAC.preservedSet <CFGAnalyses>();
453+ }
454+
421455void MachineTraceMetrics::verifyAnalysis () const {
422456 if (!MF)
423457 return ;
424458#ifndef NDEBUG
425459 assert (BlockInfo.size () == MF->getNumBlockIDs () && " Outdated BlockInfo size" );
426- for (Ensemble * E : Ensembles)
460+ for (auto & E : Ensembles)
427461 if (E)
428462 E->verify ();
429463#endif
0 commit comments