LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineTraceMetrics.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 529 587 90.1 %
Date: 2017-09-14 15:23:50 Functions: 44 50 88.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/CodeGen/MachineTraceMetrics.cpp --------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/CodeGen/MachineTraceMetrics.h"
      11             : #include "llvm/ADT/ArrayRef.h"
      12             : #include "llvm/ADT/DenseMap.h"
      13             : #include "llvm/ADT/Optional.h"
      14             : #include "llvm/ADT/PostOrderIterator.h"
      15             : #include "llvm/ADT/SmallPtrSet.h"
      16             : #include "llvm/ADT/SmallVector.h"
      17             : #include "llvm/ADT/SparseSet.h"
      18             : #include "llvm/CodeGen/MachineBasicBlock.h"
      19             : #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
      20             : #include "llvm/CodeGen/MachineFunction.h"
      21             : #include "llvm/CodeGen/MachineInstr.h"
      22             : #include "llvm/CodeGen/MachineLoopInfo.h"
      23             : #include "llvm/CodeGen/MachineOperand.h"
      24             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      25             : #include "llvm/CodeGen/TargetSchedule.h"
      26             : #include "llvm/MC/MCRegisterInfo.h"
      27             : #include "llvm/Pass.h"
      28             : #include "llvm/Support/Debug.h"
      29             : #include "llvm/Support/ErrorHandling.h"
      30             : #include "llvm/Support/Format.h"
      31             : #include "llvm/Support/raw_ostream.h"
      32             : #include "llvm/Target/TargetRegisterInfo.h"
      33             : #include "llvm/Target/TargetSubtargetInfo.h"
      34             : #include <algorithm>
      35             : #include <cassert>
      36             : #include <iterator>
      37             : #include <tuple>
      38             : #include <utility>
      39             : 
      40             : using namespace llvm;
      41             : 
      42             : #define DEBUG_TYPE "machine-trace-metrics"
      43             : 
      44             : char MachineTraceMetrics::ID = 0;
      45             : 
      46             : char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
      47             : 
      48       53257 : INITIALIZE_PASS_BEGIN(MachineTraceMetrics, DEBUG_TYPE,
      49             :                       "Machine Trace Metrics", false, true)
      50       53257 : INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
      51       53257 : INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
      52      386921 : INITIALIZE_PASS_END(MachineTraceMetrics, DEBUG_TYPE,
      53             :                     "Machine Trace Metrics", false, true)
      54             : 
      55       29910 : MachineTraceMetrics::MachineTraceMetrics() : MachineFunctionPass(ID) {
      56       29910 :   std::fill(std::begin(Ensembles), std::end(Ensembles), nullptr);
      57        9970 : }
      58             : 
      59        9970 : void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
      60       19940 :   AU.setPreservesAll();
      61        9970 :   AU.addRequired<MachineBranchProbabilityInfo>();
      62        9970 :   AU.addRequired<MachineLoopInfo>();
      63        9970 :   MachineFunctionPass::getAnalysisUsage(AU);
      64        9970 : }
      65             : 
      66      104216 : bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
      67      104216 :   MF = &Func;
      68      104216 :   const TargetSubtargetInfo &ST = MF->getSubtarget();
      69      104216 :   TII = ST.getInstrInfo();
      70      104216 :   TRI = ST.getRegisterInfo();
      71      104216 :   MRI = &MF->getRegInfo();
      72      104216 :   Loops = &getAnalysis<MachineLoopInfo>();
      73      104216 :   SchedModel.init(ST.getSchedModel(), &ST, TII);
      74      208432 :   BlockInfo.resize(MF->getNumBlockIDs());
      75      312648 :   ProcResourceCycles.resize(MF->getNumBlockIDs() *
      76      208432 :                             SchedModel.getNumProcResourceKinds());
      77      104216 :   return false;
      78             : }
      79             : 
      80      104240 : void MachineTraceMetrics::releaseMemory() {
      81      104240 :   MF = nullptr;
      82      208480 :   BlockInfo.clear();
      83      208480 :   for (unsigned i = 0; i != TS_NumStrategies; ++i) {
      84      104240 :     delete Ensembles[i];
      85      104240 :     Ensembles[i] = nullptr;
      86             :   }
      87      104240 : }
      88             : 
      89             : //===----------------------------------------------------------------------===//
      90             : //                          Fixed block information
      91             : //===----------------------------------------------------------------------===//
      92             : //
      93             : // The number of instructions in a basic block and the CPU resources used by
      94             : // those instructions don't depend on any given trace strategy.
      95             : 
      96             : /// Compute the resource usage in basic block MBB.
      97             : const MachineTraceMetrics::FixedBlockInfo*
      98        1927 : MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
      99             :   assert(MBB && "No basic block");
     100        3854 :   FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()];
     101        1927 :   if (FBI->hasResources())
     102             :     return FBI;
     103             : 
     104             :   // Compute resource usage in the block.
     105        1007 :   FBI->HasCalls = false;
     106        1007 :   unsigned InstrCount = 0;
     107             : 
     108             :   // Add up per-processor resource cycles as well.
     109        2014 :   unsigned PRKinds = SchedModel.getNumProcResourceKinds();
     110        2014 :   SmallVector<unsigned, 32> PRCycles(PRKinds);
     111             : 
     112       25562 :   for (const auto &MI : *MBB) {
     113        5124 :     if (MI.isTransient())
     114        5124 :       continue;
     115        5643 :     ++InstrCount;
     116        5643 :     if (MI.isCall())
     117         141 :       FBI->HasCalls = true;
     118             : 
     119             :     // Count processor resources used.
     120        5643 :     if (!SchedModel.hasInstrSchedModel())
     121         927 :       continue;
     122        4716 :     const MCSchedClassDesc *SC = SchedModel.resolveSchedClass(&MI);
     123        4716 :     if (!SC->isValid())
     124         816 :       continue;
     125             : 
     126        9674 :     for (TargetSchedModel::ProcResIter
     127        7800 :          PI = SchedModel.getWriteProcResBegin(SC),
     128        7800 :          PE = SchedModel.getWriteProcResEnd(SC); PI != PE; ++PI) {
     129             :       assert(PI->ProcResourceIdx < PRKinds && "Bad processor resource kind");
     130       19348 :       PRCycles[PI->ProcResourceIdx] += PI->Cycles;
     131             :     }
     132             :   }
     133        1007 :   FBI->InstrCount = InstrCount;
     134             : 
     135             :   // Scale the resource cycles so they are comparable.
     136        1007 :   unsigned PROffset = MBB->getNumber() * PRKinds;
     137       12116 :   for (unsigned K = 0; K != PRKinds; ++K)
     138       33327 :     ProcResourceCycles[PROffset + K] =
     139       44436 :       PRCycles[K] * SchedModel.getResourceFactor(K);
     140             : 
     141        1007 :   return FBI;
     142             : }
     143             : 
     144             : ArrayRef<unsigned>
     145        8093 : MachineTraceMetrics::getProcResourceCycles(unsigned MBBNum) const {
     146             :   assert(BlockInfo[MBBNum].hasResources() &&
     147             :          "getResources() must be called before getProcResourceCycles()");
     148       16186 :   unsigned PRKinds = SchedModel.getNumProcResourceKinds();
     149             :   assert((MBBNum+1) * PRKinds <= ProcResourceCycles.size());
     150       24279 :   return makeArrayRef(ProcResourceCycles.data() + MBBNum * PRKinds, PRKinds);
     151             : }
     152             : 
     153             : //===----------------------------------------------------------------------===//
     154             : //                         Ensemble utility functions
     155             : //===----------------------------------------------------------------------===//
     156             : 
     157       86718 : MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
     158      433590 :   : MTM(*ct) {
     159      173436 :   BlockInfo.resize(MTM.BlockInfo.size());
     160      173436 :   unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
     161      173436 :   ProcResourceDepths.resize(MTM.BlockInfo.size() * PRKinds);
     162      173436 :   ProcResourceHeights.resize(MTM.BlockInfo.size() * PRKinds);
     163       86718 : }
     164             : 
     165             : // Virtual destructor serves as an anchor.
     166             : MachineTraceMetrics::Ensemble::~Ensemble() = default;
     167             : 
     168             : const MachineLoop*
     169        1634 : MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) const {
     170        3268 :   return MTM.Loops->getLoopFor(MBB);
     171             : }
     172             : 
     173             : // Update resource-related information in the TraceBlockInfo for MBB.
     174             : // Only update resources related to the trace above MBB.
     175         879 : void MachineTraceMetrics::Ensemble::
     176             : computeDepthResources(const MachineBasicBlock *MBB) {
     177        1758 :   TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
     178        1758 :   unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
     179         879 :   unsigned PROffset = MBB->getNumber() * PRKinds;
     180             : 
     181             :   // Compute resources from trace above. The top block is simple.
     182         879 :   if (!TBI->Pred) {
     183         681 :     TBI->InstrDepth = 0;
     184         681 :     TBI->Head = MBB->getNumber();
     185        1362 :     std::fill(ProcResourceDepths.begin() + PROffset,
     186        1362 :               ProcResourceDepths.begin() + PROffset + PRKinds, 0);
     187         681 :     return;
     188             :   }
     189             : 
     190             :   // Compute from the block above. A post-order traversal ensures the
     191             :   // predecessor is always computed first.
     192         198 :   unsigned PredNum = TBI->Pred->getNumber();
     193         396 :   TraceBlockInfo *PredTBI = &BlockInfo[PredNum];
     194             :   assert(PredTBI->hasValidDepth() && "Trace above has not been computed yet");
     195         198 :   const FixedBlockInfo *PredFBI = MTM.getResources(TBI->Pred);
     196         198 :   TBI->InstrDepth = PredTBI->InstrDepth + PredFBI->InstrCount;
     197         198 :   TBI->Head = PredTBI->Head;
     198             : 
     199             :   // Compute per-resource depths.
     200         198 :   ArrayRef<unsigned> PredPRDepths = getProcResourceDepths(PredNum);
     201         198 :   ArrayRef<unsigned> PredPRCycles = MTM.getProcResourceCycles(PredNum);
     202         901 :   for (unsigned K = 0; K != PRKinds; ++K)
     203        2812 :     ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K];
     204             : }
     205             : 
     206             : // Update resource-related information in the TraceBlockInfo for MBB.
     207             : // Only update resources related to the trace below MBB.
     208         971 : void MachineTraceMetrics::Ensemble::
     209             : computeHeightResources(const MachineBasicBlock *MBB) {
     210        1942 :   TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
     211        1942 :   unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
     212         971 :   unsigned PROffset = MBB->getNumber() * PRKinds;
     213             : 
     214             :   // Compute resources for the current block.
     215         971 :   TBI->InstrHeight = MTM.getResources(MBB)->InstrCount;
     216         971 :   ArrayRef<unsigned> PRCycles = MTM.getProcResourceCycles(MBB->getNumber());
     217             : 
     218             :   // The trace tail is done.
     219         971 :   if (!TBI->Succ) {
     220         749 :     TBI->Tail = MBB->getNumber();
     221        2247 :     std::copy(PRCycles.begin(), PRCycles.end(),
     222        1498 :               ProcResourceHeights.begin() + PROffset);
     223         749 :     return;
     224             :   }
     225             : 
     226             :   // Compute from the block below. A post-order traversal ensures the
     227             :   // predecessor is always computed first.
     228         222 :   unsigned SuccNum = TBI->Succ->getNumber();
     229         444 :   TraceBlockInfo *SuccTBI = &BlockInfo[SuccNum];
     230             :   assert(SuccTBI->hasValidHeight() && "Trace below has not been computed yet");
     231         222 :   TBI->InstrHeight += SuccTBI->InstrHeight;
     232         222 :   TBI->Tail = SuccTBI->Tail;
     233             : 
     234             :   // Compute per-resource heights.
     235         222 :   ArrayRef<unsigned> SuccPRHeights = getProcResourceHeights(SuccNum);
     236        1591 :   for (unsigned K = 0; K != PRKinds; ++K)
     237        5476 :     ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K];
     238             : }
     239             : 
     240             : // Check if depth resources for MBB are valid and return the TBI.
     241             : // Return NULL if the resources have been invalidated.
     242             : const MachineTraceMetrics::TraceBlockInfo*
     243         261 : MachineTraceMetrics::Ensemble::
     244             : getDepthResources(const MachineBasicBlock *MBB) const {
     245         522 :   const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
     246         261 :   return TBI->hasValidDepth() ? TBI : nullptr;
     247             : }
     248             : 
     249             : // Check if height resources for MBB are valid and return the TBI.
     250             : // Return NULL if the resources have been invalidated.
     251             : const MachineTraceMetrics::TraceBlockInfo*
     252         319 : MachineTraceMetrics::Ensemble::
     253             : getHeightResources(const MachineBasicBlock *MBB) const {
     254         638 :   const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
     255         319 :   return TBI->hasValidHeight() ? TBI : nullptr;
     256             : }
     257             : 
     258             : /// Get an array of processor resource depths for MBB. Indexed by processor
     259             : /// resource kind, this array contains the scaled processor resources consumed
     260             : /// by all blocks preceding MBB in its trace. It does not include instructions
     261             : /// in MBB.
     262             : ///
     263             : /// Compare TraceBlockInfo::InstrDepth.
     264             : ArrayRef<unsigned>
     265         854 : MachineTraceMetrics::Ensemble::
     266             : getProcResourceDepths(unsigned MBBNum) const {
     267        1708 :   unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
     268             :   assert((MBBNum+1) * PRKinds <= ProcResourceDepths.size());
     269        2562 :   return makeArrayRef(ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds);
     270             : }
     271             : 
     272             : /// Get an array of processor resource heights for MBB. Indexed by processor
     273             : /// resource kind, this array contains the scaled processor resources consumed
     274             : /// by this block and all blocks following it in its trace.
     275             : ///
     276             : /// Compare TraceBlockInfo::InstrHeight.
     277             : ArrayRef<unsigned>
     278         859 : MachineTraceMetrics::Ensemble::
     279             : getProcResourceHeights(unsigned MBBNum) const {
     280        1718 :   unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
     281             :   assert((MBBNum+1) * PRKinds <= ProcResourceHeights.size());
     282        2577 :   return makeArrayRef(ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds);
     283             : }
     284             : 
     285             : //===----------------------------------------------------------------------===//
     286             : //                         Trace Selection Strategies
     287             : //===----------------------------------------------------------------------===//
     288             : //
     289             : // A trace selection strategy is implemented as a sub-class of Ensemble. The
     290             : // trace through a block B is computed by two DFS traversals of the CFG
     291             : // starting from B. One upwards, and one downwards. During the upwards DFS,
     292             : // pickTracePred() is called on the post-ordered blocks. During the downwards
     293             : // DFS, pickTraceSucc() is called in a post-order.
     294             : //
     295             : 
     296             : // We never allow traces that leave loops, but we do allow traces to enter
     297             : // nested loops. We also never allow traces to contain back-edges.
     298             : //
     299             : // This means that a loop header can never appear above the center block of a
     300             : // trace, except as the trace head. Below the center block, loop exiting edges
     301             : // are banned.
     302             : //
     303             : // Return true if an edge from the From loop to the To loop is leaving a loop.
     304             : // Either of To and From can be null.
     305             : static bool isExitingLoop(const MachineLoop *From, const MachineLoop *To) {
     306         815 :   return From && !From->contains(To);
     307             : }
     308             : 
     309             : // MinInstrCountEnsemble - Pick the trace that executes the least number of
     310             : // instructions.
     311             : namespace {
     312             : 
     313       86718 : class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
     314           0 :   const char *getName() const override { return "MinInstr"; }
     315             :   const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) override;
     316             :   const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) override;
     317             : 
     318             : public:
     319             :   MinInstrCountEnsemble(MachineTraceMetrics *mtm)
     320       86718 :     : MachineTraceMetrics::Ensemble(mtm) {}
     321             : };
     322             : 
     323             : } // end anonymous namespace
     324             : 
     325             : // Select the preferred predecessor for MBB.
     326             : const MachineBasicBlock*
     327         879 : MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) {
     328         879 :   if (MBB->pred_empty())
     329             :     return nullptr;
     330         217 :   const MachineLoop *CurLoop = getLoopFor(MBB);
     331             :   // Don't leave loops, and never follow back-edges.
     332         280 :   if (CurLoop && MBB == CurLoop->getHeader())
     333             :     return nullptr;
     334         198 :   unsigned CurCount = MTM.getResources(MBB)->InstrCount;
     335         198 :   const MachineBasicBlock *Best = nullptr;
     336         198 :   unsigned BestDepth = 0;
     337         657 :   for (const MachineBasicBlock *Pred : MBB->predecessors()) {
     338             :     const MachineTraceMetrics::TraceBlockInfo *PredTBI =
     339         261 :       getDepthResources(Pred);
     340             :     // Ignore cycles that aren't natural loops.
     341         261 :     if (!PredTBI)
     342           3 :       continue;
     343             :     // Pick the predecessor that would give this block the smallest InstrDepth.
     344         258 :     unsigned Depth = PredTBI->InstrDepth + CurCount;
     345         258 :     if (!Best || Depth < BestDepth) {
     346         198 :       Best = Pred;
     347         198 :       BestDepth = Depth;
     348             :     }
     349             :   }
     350             :   return Best;
     351             : }
     352             : 
     353             : // Select the preferred successor for MBB.
     354             : const MachineBasicBlock*
     355         971 : MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
     356         971 :   if (MBB->pred_empty())
     357             :     return nullptr;
     358         333 :   const MachineLoop *CurLoop = getLoopFor(MBB);
     359         333 :   const MachineBasicBlock *Best = nullptr;
     360         333 :   unsigned BestHeight = 0;
     361        1064 :   for (const MachineBasicBlock *Succ : MBB->successors()) {
     362             :     // Don't consider back-edges.
     363         591 :     if (CurLoop && Succ == CurLoop->getHeader())
     364          34 :       continue;
     365             :     // Don't consider successors exiting CurLoop.
     366         409 :     if (isExitingLoop(CurLoop, getLoopFor(Succ)))
     367          45 :       continue;
     368             :     const MachineTraceMetrics::TraceBlockInfo *SuccTBI =
     369         319 :       getHeightResources(Succ);
     370             :     // Ignore cycles that aren't natural loops.
     371         319 :     if (!SuccTBI)
     372           3 :       continue;
     373             :     // Pick the successor that would give this block the smallest InstrHeight.
     374         316 :     unsigned Height = SuccTBI->InstrHeight;
     375         316 :     if (!Best || Height < BestHeight) {
     376         251 :       Best = Succ;
     377         251 :       BestHeight = Height;
     378             :     }
     379             :   }
     380             :   return Best;
     381             : }
     382             : 
     383             : // Get an Ensemble sub-class for the requested trace strategy.
     384             : MachineTraceMetrics::Ensemble *
     385       86743 : MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) {
     386             :   assert(strategy < TS_NumStrategies && "Invalid trace strategy enum");
     387       86743 :   Ensemble *&E = Ensembles[strategy];
     388       86743 :   if (E)
     389             :     return E;
     390             : 
     391             :   // Allocate new Ensemble on demand.
     392       86718 :   switch (strategy) {
     393      173436 :   case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this));
     394           0 :   default: llvm_unreachable("Invalid trace strategy enum");
     395             :   }
     396             : }
     397             : 
     398         387 : void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
     399             :   DEBUG(dbgs() << "Invalidate traces through BB#" << MBB->getNumber() << '\n');
     400        1161 :   BlockInfo[MBB->getNumber()].invalidate();
     401         387 :   for (unsigned i = 0; i != TS_NumStrategies; ++i)
     402         387 :     if (Ensembles[i])
     403         247 :       Ensembles[i]->invalidate(MBB);
     404         387 : }
     405             : 
     406        1503 : void MachineTraceMetrics::verifyAnalysis() const {
     407             :   if (!MF)
     408             :     return;
     409             : #ifndef NDEBUG
     410             :   assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
     411             :   for (unsigned i = 0; i != TS_NumStrategies; ++i)
     412             :     if (Ensembles[i])
     413             :       Ensembles[i]->verify();
     414             : #endif
     415             : }
     416             : 
     417             : //===----------------------------------------------------------------------===//
     418             : //                               Trace building
     419             : //===----------------------------------------------------------------------===//
     420             : //
     421             : // Traces are built by two CFG traversals. To avoid recomputing too much, use a
     422             : // set abstraction that confines the search to the current loop, and doesn't
     423             : // revisit blocks.
     424             : 
     425             : namespace {
     426             : 
     427        1574 : struct LoopBounds {
     428             :   MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks;
     429             :   SmallPtrSet<const MachineBasicBlock*, 8> Visited;
     430             :   const MachineLoopInfo *Loops;
     431             :   bool Downward = false;
     432             : 
     433             :   LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
     434        1574 :              const MachineLoopInfo *loops) : Blocks(blocks), Loops(loops) {}
     435             : };
     436             : 
     437             : } // end anonymous namespace
     438             : 
     439             : // Specialize po_iterator_storage in order to prune the post-order traversal so
     440             : // it is limited to the current loop and doesn't traverse the loop back edges.
     441             : namespace llvm {
     442             : 
     443             : template<>
     444             : class po_iterator_storage<LoopBounds, true> {
     445             :   LoopBounds &LB;
     446             : 
     447             : public:
     448        3148 :   po_iterator_storage(LoopBounds &lb) : LB(lb) {}
     449             : 
     450             :   void finishPostorder(const MachineBasicBlock*) {}
     451             : 
     452        2359 :   bool insertEdge(Optional<const MachineBasicBlock *> From,
     453             :                   const MachineBasicBlock *To) {
     454             :     // Skip already visited To blocks.
     455        4718 :     MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()];
     456        4718 :     if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth())
     457             :       return false;
     458             :     // From is null once when To is the trace center block.
     459        1961 :     if (From) {
     460        1461 :       if (const MachineLoop *FromLoop = LB.Loops->getLoopFor(*From)) {
     461             :         // Don't follow backedges, don't leave FromLoop when going upwards.
     462         522 :         if ((LB.Downward ? To : *From) == FromLoop->getHeader())
     463             :           return false;
     464             :         // Don't leave FromLoop.
     465         292 :         if (isExitingLoop(FromLoop, LB.Loops->getLoopFor(To)))
     466             :           return false;
     467             :       }
     468             :     }
     469             :     // To is a new block. Mark the block as visited in case the CFG has cycles
     470             :     // that MachineLoopInfo didn't recognize as a natural loop.
     471        1856 :     return LB.Visited.insert(To).second;
     472             :   }
     473             : };
     474             : 
     475             : } // end namespace llvm
     476             : 
     477             : /// Compute the trace through MBB.
     478         787 : void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
     479             :   DEBUG(dbgs() << "Computing " << getName() << " trace through BB#"
     480             :                << MBB->getNumber() << '\n');
     481             :   // Set up loop bounds for the backwards post-order traversal.
     482        3148 :   LoopBounds Bounds(BlockInfo, MTM.Loops);
     483             : 
     484             :   // Run an upwards post-order search for the trace start.
     485             :   Bounds.Downward = false;
     486         787 :   Bounds.Visited.clear();
     487        5693 :   for (auto I : inverse_post_order_ext(MBB, Bounds)) {
     488             :     DEBUG(dbgs() << "  pred for BB#" << I->getNumber() << ": ");
     489        1758 :     TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
     490             :     // All the predecessors have been visited, pick the preferred one.
     491         879 :     TBI.Pred = pickTracePred(I);
     492             :     DEBUG({
     493             :       if (TBI.Pred)
     494             :         dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
     495             :       else
     496             :         dbgs() << "null\n";
     497             :     });
     498             :     // The trace leading to I is now known, compute the depth resources.
     499         879 :     computeDepthResources(I);
     500             :   }
     501             : 
     502             :   // Run a downwards post-order search for the trace end.
     503         787 :   Bounds.Downward = true;
     504         787 :   Bounds.Visited.clear();
     505        5877 :   for (auto I : post_order_ext(MBB, Bounds)) {
     506             :     DEBUG(dbgs() << "  succ for BB#" << I->getNumber() << ": ");
     507        1942 :     TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
     508             :     // All the successors have been visited, pick the preferred one.
     509         971 :     TBI.Succ = pickTraceSucc(I);
     510             :     DEBUG({
     511             :       if (TBI.Succ)
     512             :         dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
     513             :       else
     514             :         dbgs() << "null\n";
     515             :     });
     516             :     // The trace leaving I is now known, compute the height resources.
     517         971 :     computeHeightResources(I);
     518             :   }
     519         787 : }
     520             : 
     521             : /// Invalidate traces through BadMBB.
     522             : void
     523         808 : MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
     524        1616 :   SmallVector<const MachineBasicBlock*, 16> WorkList;
     525        1616 :   TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()];
     526             : 
     527             :   // Invalidate height resources of blocks above MBB.
     528         808 :   if (BadTBI.hasValidHeight()) {
     529             :     BadTBI.invalidateHeight();
     530         358 :     WorkList.push_back(BadMBB);
     531             :     do {
     532         405 :       const MachineBasicBlock *MBB = WorkList.pop_back_val();
     533             :       DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
     534             :             << " height.\n");
     535             :       // Find any MBB predecessors that have MBB as their preferred successor.
     536             :       // They are the only ones that need to be invalidated.
     537         972 :       for (const MachineBasicBlock *Pred : MBB->predecessors()) {
     538         324 :         TraceBlockInfo &TBI = BlockInfo[Pred->getNumber()];
     539         162 :         if (!TBI.hasValidHeight())
     540         113 :           continue;
     541          96 :         if (TBI.Succ == MBB) {
     542             :           TBI.invalidateHeight();
     543          47 :           WorkList.push_back(Pred);
     544          47 :           continue;
     545             :         }
     546             :         // Verify that TBI.Succ is actually a *I successor.
     547             :         assert((!TBI.Succ || Pred->isSuccessor(TBI.Succ)) && "CFG changed");
     548             :       }
     549         405 :     } while (!WorkList.empty());
     550             :   }
     551             : 
     552             :   // Invalidate depth resources of blocks below MBB.
     553         808 :   if (BadTBI.hasValidDepth()) {
     554             :     BadTBI.invalidateDepth();
     555         316 :     WorkList.push_back(BadMBB);
     556             :     do {
     557         405 :       const MachineBasicBlock *MBB = WorkList.pop_back_val();
     558             :       DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
     559             :             << " depth.\n");
     560             :       // Find any MBB successors that have MBB as their preferred predecessor.
     561             :       // They are the only ones that need to be invalidated.
     562         977 :       for (const MachineBasicBlock *Succ : MBB->successors()) {
     563         334 :         TraceBlockInfo &TBI = BlockInfo[Succ->getNumber()];
     564         167 :         if (!TBI.hasValidDepth())
     565          72 :           continue;
     566         184 :         if (TBI.Pred == MBB) {
     567             :           TBI.invalidateDepth();
     568          89 :           WorkList.push_back(Succ);
     569          89 :           continue;
     570             :         }
     571             :         // Verify that TBI.Pred is actually a *I predecessor.
     572             :         assert((!TBI.Pred || Succ->isPredecessor(TBI.Pred)) && "CFG changed");
     573             :       }
     574         405 :     } while (!WorkList.empty());
     575             :   }
     576             : 
     577             :   // Clear any per-instruction data. We only have to do this for BadMBB itself
     578             :   // because the instructions in that block may change. Other blocks may be
     579             :   // invalidated, but their instructions will stay the same, so there is no
     580             :   // need to erase the Cycle entries. They will be overwritten when we
     581             :   // recompute.
     582       53042 :   for (const auto &I : *BadMBB)
     583       24905 :     Cycles.erase(&I);
     584         808 : }
     585             : 
     586           0 : void MachineTraceMetrics::Ensemble::verify() const {
     587             : #ifndef NDEBUG
     588             :   assert(BlockInfo.size() == MTM.MF->getNumBlockIDs() &&
     589             :          "Outdated BlockInfo size");
     590             :   for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
     591             :     const TraceBlockInfo &TBI = BlockInfo[Num];
     592             :     if (TBI.hasValidDepth() && TBI.Pred) {
     593             :       const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
     594             :       assert(MBB->isPredecessor(TBI.Pred) && "CFG doesn't match trace");
     595             :       assert(BlockInfo[TBI.Pred->getNumber()].hasValidDepth() &&
     596             :              "Trace is broken, depth should have been invalidated.");
     597             :       const MachineLoop *Loop = getLoopFor(MBB);
     598             :       assert(!(Loop && MBB == Loop->getHeader()) && "Trace contains backedge");
     599             :     }
     600             :     if (TBI.hasValidHeight() && TBI.Succ) {
     601             :       const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
     602             :       assert(MBB->isSuccessor(TBI.Succ) && "CFG doesn't match trace");
     603             :       assert(BlockInfo[TBI.Succ->getNumber()].hasValidHeight() &&
     604             :              "Trace is broken, height should have been invalidated.");
     605             :       const MachineLoop *Loop = getLoopFor(MBB);
     606             :       const MachineLoop *SuccLoop = getLoopFor(TBI.Succ);
     607             :       assert(!(Loop && Loop == SuccLoop && TBI.Succ == Loop->getHeader()) &&
     608             :              "Trace contains backedge");
     609             :     }
     610             :   }
     611             : #endif
     612           0 : }
     613             : 
     614             : //===----------------------------------------------------------------------===//
     615             : //                             Data Dependencies
     616             : //===----------------------------------------------------------------------===//
     617             : //
     618             : // Compute the depth and height of each instruction based on data dependencies
     619             : // and instruction latencies. These cycle numbers assume that the CPU can issue
     620             : // an infinite number of instructions per cycle as long as their dependencies
     621             : // are ready.
     622             : 
     623             : // A data dependency is represented as a defining MI and operand numbers on the
     624             : // defining and using MI.
     625             : namespace {
     626             : 
     627             : struct DataDep {
     628             :   const MachineInstr *DefMI;
     629             :   unsigned DefOp;
     630             :   unsigned UseOp;
     631             : 
     632             :   DataDep(const MachineInstr *DefMI, unsigned DefOp, unsigned UseOp)
     633        1142 :     : DefMI(DefMI), DefOp(DefOp), UseOp(UseOp) {}
     634             : 
     635             :   /// Create a DataDep from an SSA form virtual register.
     636             :   DataDep(const MachineRegisterInfo *MRI, unsigned VirtReg, unsigned UseOp)
     637       21116 :     : UseOp(UseOp) {
     638             :     assert(TargetRegisterInfo::isVirtualRegister(VirtReg));
     639       21116 :     MachineRegisterInfo::def_iterator DefI = MRI->def_begin(VirtReg);
     640             :     assert(!DefI.atEnd() && "Register has no defs");
     641       21116 :     DefMI = DefI->getParent();
     642       21116 :     DefOp = DefI.getOperandNo();
     643             :     assert((++DefI).atEnd() && "Register has multiple defs");
     644             :   }
     645             : };
     646             : 
     647             : } // end anonymous namespace
     648             : 
     649             : // Get the input data dependencies that must be ready before UseMI can issue.
     650             : // Return true if UseMI has any physreg operands.
     651       20934 : static bool getDataDeps(const MachineInstr &UseMI,
     652             :                         SmallVectorImpl<DataDep> &Deps,
     653             :                         const MachineRegisterInfo *MRI) {
     654             :   // Debug values should not be included in any calculations.
     655       20934 :   if (UseMI.isDebugValue())
     656             :     return false;
     657             :   
     658       20866 :   bool HasPhysRegs = false;
     659       77685 :   for (MachineInstr::const_mop_iterator I = UseMI.operands_begin(),
     660       41732 :        E = UseMI.operands_end(); I != E; ++I) {
     661       56819 :     const MachineOperand &MO = *I;
     662       56819 :     if (!MO.isReg())
     663        7281 :       continue;
     664       49538 :     unsigned Reg = MO.getReg();
     665       49538 :     if (!Reg)
     666         377 :       continue;
     667       60919 :     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
     668       11758 :       HasPhysRegs = true;
     669       11758 :       continue;
     670             :     }
     671             :     // Collect virtual register reads.
     672             :     if (MO.readsReg())
     673       62490 :       Deps.push_back(DataDep(MRI, Reg, UseMI.getOperandNo(I)));
     674             :   }
     675             :   return HasPhysRegs;
     676             : }
     677             : 
     678             : // Get the input data dependencies of a PHI instruction, using Pred as the
     679             : // preferred predecessor.
     680             : // This will add at most one dependency to Deps.
     681         322 : static void getPHIDeps(const MachineInstr &UseMI,
     682             :                        SmallVectorImpl<DataDep> &Deps,
     683             :                        const MachineBasicBlock *Pred,
     684             :                        const MachineRegisterInfo *MRI) {
     685             :   // No predecessor at the beginning of a trace. Ignore dependencies.
     686         322 :   if (!Pred)
     687             :     return;
     688             :   assert(UseMI.isPHI() && UseMI.getNumOperands() % 2 && "Bad PHI");
     689         718 :   for (unsigned i = 1; i != UseMI.getNumOperands(); i += 2) {
     690        1004 :     if (UseMI.getOperand(i + 1).getMBB() == Pred) {
     691         572 :       unsigned Reg = UseMI.getOperand(i).getReg();
     692         572 :       Deps.push_back(DataDep(MRI, Reg, i));
     693         286 :       return;
     694             :     }
     695             :   }
     696             : }
     697             : 
     698             : // Identify physreg dependencies for UseMI, and update the live regunit
     699             : // tracking set when scanning instructions downwards.
     700        5269 : static void updatePhysDepsDownwards(const MachineInstr *UseMI,
     701             :                                     SmallVectorImpl<DataDep> &Deps,
     702             :                                     SparseSet<LiveRegUnit> &RegUnits,
     703             :                                     const TargetRegisterInfo *TRI) {
     704       10538 :   SmallVector<unsigned, 8> Kills;
     705       10538 :   SmallVector<unsigned, 8> LiveDefOps;
     706             : 
     707       19056 :   for (MachineInstr::const_mop_iterator MI = UseMI->operands_begin(),
     708       10538 :        ME = UseMI->operands_end(); MI != ME; ++MI) {
     709       13787 :     const MachineOperand &MO = *MI;
     710       13787 :     if (!MO.isReg())
     711       11589 :       continue;
     712       11716 :     unsigned Reg = MO.getReg();
     713       23432 :     if (!TargetRegisterInfo::isPhysicalRegister(Reg))
     714        5414 :       continue;
     715             :     // Track live defs and kills for updating RegUnits.
     716        6302 :     if (MO.isDef()) {
     717        2032 :       if (MO.isDead())
     718         863 :         Kills.push_back(Reg);
     719             :       else
     720        1169 :         LiveDefOps.push_back(UseMI->getOperandNo(MI));
     721        4270 :     } else if (MO.isKill())
     722           1 :       Kills.push_back(Reg);
     723             :     // Identify dependencies.
     724        2033 :     if (!MO.readsReg())
     725        2033 :       continue;
     726       11814 :     for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
     727       13254 :       SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units);
     728        4418 :       if (I == RegUnits.end())
     729             :         continue;
     730        3426 :       Deps.push_back(DataDep(I->MI, I->Op, UseMI->getOperandNo(MI)));
     731        1142 :       break;
     732             :     }
     733             :   }
     734             : 
     735             :   // Update RegUnits to reflect live registers after UseMI.
     736             :   // First kills.
     737       16671 :   for (unsigned Kill : Kills)
     738        2592 :     for (MCRegUnitIterator Units(Kill, TRI); Units.isValid(); ++Units)
     739        1728 :       RegUnits.erase(*Units);
     740             : 
     741             :   // Second, live defs.
     742       16976 :   for (unsigned DefOp : LiveDefOps) {
     743        2338 :     for (MCRegUnitIterator Units(UseMI->getOperand(DefOp).getReg(), TRI);
     744        2456 :          Units.isValid(); ++Units) {
     745        3861 :       LiveRegUnit &LRU = RegUnits[*Units];
     746        1287 :       LRU.MI = UseMI;
     747        1287 :       LRU.Op = DefOp;
     748             :     }
     749             :   }
     750        5269 : }
     751             : 
     752             : /// The length of the critical path through a trace is the maximum of two path
     753             : /// lengths:
     754             : ///
     755             : /// 1. The maximum height+depth over all instructions in the trace center block.
     756             : ///
     757             : /// 2. The longest cross-block dependency chain. For small blocks, it is
     758             : ///    possible that the critical path through the trace doesn't include any
     759             : ///    instructions in the block.
     760             : ///
     761             : /// This function computes the second number from the live-in list of the
     762             : /// center block.
     763         789 : unsigned MachineTraceMetrics::Ensemble::
     764             : computeCrossBlockCriticalPath(const TraceBlockInfo &TBI) {
     765             :   assert(TBI.HasValidInstrDepths && "Missing depth info");
     766             :   assert(TBI.HasValidInstrHeights && "Missing height info");
     767         789 :   unsigned MaxLen = 0;
     768        5197 :   for (const LiveInReg &LIR : TBI.LiveIns) {
     769        5660 :     if (!TargetRegisterInfo::isVirtualRegister(LIR.Reg))
     770        5350 :       continue;
     771         162 :     const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg);
     772             :     // Ignore dependencies outside the current trace.
     773         324 :     const TraceBlockInfo &DefTBI = BlockInfo[DefMI->getParent()->getNumber()];
     774         162 :     if (!DefTBI.isUsefulDominator(TBI))
     775          14 :       continue;
     776         296 :     unsigned Len = LIR.Height + Cycles[DefMI].Depth;
     777         148 :     MaxLen = std::max(MaxLen, Len);
     778             :   }
     779         789 :   return MaxLen;
     780             : }
     781             : 
     782       11117 : void MachineTraceMetrics::Ensemble::
     783             : updateDepth(MachineTraceMetrics::TraceBlockInfo &TBI, const MachineInstr &UseMI,
     784             :             SparseSet<LiveRegUnit> &RegUnits) {
     785       22234 :   SmallVector<DataDep, 8> Deps;
     786             :   // Collect all data dependencies.
     787       11022 :   if (UseMI.isPHI())
     788          95 :     getPHIDeps(UseMI, Deps, TBI.Pred, MTM.MRI);
     789       11022 :   else if (getDataDeps(UseMI, Deps, MTM.MRI))
     790        5269 :     updatePhysDepsDownwards(&UseMI, Deps, RegUnits, MTM.TRI);
     791             : 
     792             :   // Filter and process dependencies, computing the earliest issue cycle.
     793       11117 :   unsigned Cycle = 0;
     794       45500 :   for (const DataDep &Dep : Deps) {
     795             :     const TraceBlockInfo&DepTBI =
     796       24298 :       BlockInfo[Dep.DefMI->getParent()->getNumber()];
     797             :     // Ignore dependencies from outside the current trace.
     798       12149 :     if (!DepTBI.isUsefulDominator(TBI))
     799          28 :       continue;
     800             :     assert(DepTBI.HasValidInstrDepths && "Inconsistent dependency");
     801       12121 :     unsigned DepCycle = Cycles.lookup(Dep.DefMI).Depth;
     802             :     // Add latency if DefMI is a real instruction. Transients get latency 0.
     803       12121 :     if (!Dep.DefMI->isTransient())
     804       11076 :       DepCycle += MTM.SchedModel
     805        5538 :         .computeOperandLatency(Dep.DefMI, Dep.DefOp, &UseMI, Dep.UseOp);
     806       12121 :     Cycle = std::max(Cycle, DepCycle);
     807             :   }
     808             :   // Remember the instruction depth.
     809       22234 :   InstrCycles &MICycles = Cycles[&UseMI];
     810       11117 :   MICycles.Depth = Cycle;
     811             : 
     812       11117 :   if (TBI.HasValidInstrHeights) {
     813             :     // Update critical path length.
     814        1994 :     TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Height);
     815             :     DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << UseMI);
     816             :   } else {
     817             :     DEBUG(dbgs() << Cycle << '\t' << UseMI);
     818             :   }
     819       11117 : }
     820             : 
     821         727 : void MachineTraceMetrics::Ensemble::
     822             : updateDepth(const MachineBasicBlock *MBB, const MachineInstr &UseMI,
     823             :             SparseSet<LiveRegUnit> &RegUnits) {
     824        1454 :   updateDepth(BlockInfo[MBB->getNumber()], UseMI, RegUnits);
     825         727 : }
     826             : 
     827          67 : void MachineTraceMetrics::Ensemble::
     828             : updateDepths(MachineBasicBlock::iterator Start,
     829             :              MachineBasicBlock::iterator End,
     830             :              SparseSet<LiveRegUnit> &RegUnits) {
     831        1073 :     for (; Start != End; Start++)
     832        1006 :       updateDepth(Start->getParent(), *Start, RegUnits);
     833          67 : }
     834             : 
     835             : /// Compute instruction depths for all instructions above or in MBB in its
     836             : /// trace. This assumes that the trace through MBB has already been computed.
     837         778 : void MachineTraceMetrics::Ensemble::
     838             : computeInstrDepths(const MachineBasicBlock *MBB) {
     839             :   // The top of the trace may already be computed, and HasValidInstrDepths
     840             :   // implies Head->HasValidInstrDepths, so we only need to start from the first
     841             :   // block in the trace that needs to be recomputed.
     842         778 :   SmallVector<const MachineBasicBlock*, 8> Stack;
     843             :   do {
     844        1910 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
     845             :     assert(TBI.hasValidDepth() && "Incomplete trace");
     846         955 :     if (TBI.HasValidInstrDepths)
     847             :       break;
     848         856 :     Stack.push_back(MBB);
     849         856 :     MBB = TBI.Pred;
     850         856 :   } while (MBB);
     851             : 
     852             :   // FIXME: If MBB is non-null at this point, it is the last pre-computed block
     853             :   // in the trace. We should track any live-out physregs that were defined in
     854             :   // the trace. This is quite rare in SSA form, typically created by CSE
     855             :   // hoisting a compare.
     856        1556 :   SparseSet<LiveRegUnit> RegUnits;
     857         778 :   RegUnits.setUniverse(MTM.TRI->getNumRegUnits());
     858             : 
     859             :   // Go through trace blocks in top-down order, stopping after the center block.
     860        2490 :   while (!Stack.empty()) {
     861         856 :     MBB = Stack.pop_back_val();
     862             :     DEBUG(dbgs() << "\nDepths for BB#" << MBB->getNumber() << ":\n");
     863        1712 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
     864         856 :     TBI.HasValidInstrDepths = true;
     865         856 :     TBI.CriticalPath = 0;
     866             : 
     867             :     // Print out resource depths here as well.
     868             :     DEBUG({
     869             :       dbgs() << format("%7u Instructions\n", TBI.InstrDepth);
     870             :       ArrayRef<unsigned> PRDepths = getProcResourceDepths(MBB->getNumber());
     871             :       for (unsigned K = 0; K != PRDepths.size(); ++K)
     872             :         if (PRDepths[K]) {
     873             :           unsigned Factor = MTM.SchedModel.getResourceFactor(K);
     874             :           dbgs() << format("%6uc @ ", MTM.getCycles(PRDepths[K]))
     875             :                  << MTM.SchedModel.getProcResource(K)->Name << " ("
     876             :                  << PRDepths[K]/Factor << " ops x" << Factor << ")\n";
     877             :         }
     878             :     });
     879             : 
     880             :     // Also compute the critical path length through MBB when possible.
     881         856 :     if (TBI.HasValidInstrHeights)
     882          48 :       TBI.CriticalPath = computeCrossBlockCriticalPath(TBI);
     883             : 
     884       24204 :     for (const auto &UseMI : *MBB) {
     885       10390 :       updateDepth(TBI, UseMI, RegUnits);
     886             :     }
     887             :   }
     888         778 : }
     889             : 
     890             : // Identify physreg dependencies for MI when scanning instructions upwards.
     891             : // Return the issue height of MI after considering any live regunits.
     892             : // Height is the issue height computed from virtual register dependencies alone.
     893        4744 : static unsigned updatePhysDepsUpwards(const MachineInstr &MI, unsigned Height,
     894             :                                       SparseSet<LiveRegUnit> &RegUnits,
     895             :                                       const TargetSchedModel &SchedModel,
     896             :                                       const TargetInstrInfo *TII,
     897             :                                       const TargetRegisterInfo *TRI) {
     898        9488 :   SmallVector<unsigned, 8> ReadOps;
     899             : 
     900       16648 :   for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(),
     901        9488 :                                         MOE = MI.operands_end();
     902       16648 :        MOI != MOE; ++MOI) {
     903       11904 :     const MachineOperand &MO = *MOI;
     904       11904 :     if (!MO.isReg())
     905             :       continue;
     906       10266 :     unsigned Reg = MO.getReg();
     907       10266 :     if (!TargetRegisterInfo::isPhysicalRegister(Reg))
     908             :       continue;
     909             :     if (MO.readsReg())
     910        3894 :       ReadOps.push_back(MI.getOperandNo(MOI));
     911        5456 :     if (!MO.isDef())
     912             :       continue;
     913             :     // This is a def of Reg. Remove corresponding entries from RegUnits, and
     914             :     // update MI Height to consider the physreg dependencies.
     915        4773 :     for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
     916        4953 :       SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units);
     917        1651 :       if (I == RegUnits.end())
     918         414 :         continue;
     919        1237 :       unsigned DepHeight = I->Cycle;
     920             :       if (!MI.isTransient()) {
     921             :         // We may not know the UseMI of this dependency, if it came from the
     922             :         // live-in list. SchedModel can handle a NULL UseMI.
     923         888 :         DepHeight += SchedModel.computeOperandLatency(&MI, MI.getOperandNo(MOI),
     924             :                                                       I->MI, I->Op);
     925             :       }
     926        1237 :       Height = std::max(Height, DepHeight);
     927             :       // This regunit is dead above MI.
     928        1237 :       RegUnits.erase(I);
     929             :     }
     930             :   }
     931             : 
     932             :   // Now we know the height of MI. Update any regunits read.
     933       13382 :   for (unsigned i = 0, e = ReadOps.size(); i != e; ++i) {
     934       11682 :     unsigned Reg = MI.getOperand(ReadOps[i]).getReg();
     935       11913 :     for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
     936       12375 :       LiveRegUnit &LRU = RegUnits[*Units];
     937             :       // Set the height to the highest reader of the unit.
     938        4125 :       if (LRU.Cycle <= Height && LRU.MI != &MI) {
     939        4118 :         LRU.Cycle = Height;
     940        4118 :         LRU.MI = &MI;
     941        8236 :         LRU.Op = ReadOps[i];
     942             :       }
     943             :     }
     944             :   }
     945             : 
     946        9488 :   return Height;
     947             : }
     948             : 
     949             : using MIHeightMap = DenseMap<const MachineInstr *, unsigned>;
     950             : 
     951             : // Push the height of DefMI upwards if required to match UseMI.
     952             : // Return true if this is the first time DefMI was seen.
     953       10010 : static bool pushDepHeight(const DataDep &Dep, const MachineInstr &UseMI,
     954             :                           unsigned UseHeight, MIHeightMap &Heights,
     955             :                           const TargetSchedModel &SchedModel,
     956             :                           const TargetInstrInfo *TII) {
     957             :   // Adjust height by Dep.DefMI latency.
     958       10010 :   if (!Dep.DefMI->isTransient())
     959        4571 :     UseHeight += SchedModel.computeOperandLatency(Dep.DefMI, Dep.DefOp, &UseMI,
     960        4571 :                                                   Dep.UseOp);
     961             : 
     962             :   // Update Heights[DefMI] to be the maximum height seen.
     963             :   MIHeightMap::iterator I;
     964             :   bool New;
     965       50050 :   std::tie(I, New) = Heights.insert(std::make_pair(Dep.DefMI, UseHeight));
     966       10010 :   if (New)
     967             :     return true;
     968             : 
     969             :   // DefMI has been pushed before. Give it the max height.
     970        2024 :   if (I->second < UseHeight)
     971         497 :     I->second = UseHeight;
     972             :   return false;
     973             : }
     974             : 
     975             : /// Assuming that the virtual register defined by DefMI:DefOp was used by
     976             : /// Trace.back(), add it to the live-in lists of all the blocks in Trace. Stop
     977             : /// when reaching the block that contains DefMI.
     978        7986 : void MachineTraceMetrics::Ensemble::
     979             : addLiveIns(const MachineInstr *DefMI, unsigned DefOp,
     980             :            ArrayRef<const MachineBasicBlock*> Trace) {
     981             :   assert(!Trace.empty() && "Trace should contain at least one block");
     982       15972 :   unsigned Reg = DefMI->getOperand(DefOp).getReg();
     983             :   assert(TargetRegisterInfo::isVirtualRegister(Reg));
     984        7986 :   const MachineBasicBlock *DefMBB = DefMI->getParent();
     985             : 
     986             :   // Reg is live-in to all blocks in Trace that follow DefMBB.
     987        8171 :   for (unsigned i = Trace.size(); i; --i) {
     988       16062 :     const MachineBasicBlock *MBB = Trace[i-1];
     989        8031 :     if (MBB == DefMBB)
     990             :       return;
     991         370 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
     992             :     // Just add the register. The height will be updated later.
     993         370 :     TBI.LiveIns.push_back(Reg);
     994             :   }
     995             : }
     996             : 
     997             : /// Compute instruction heights in the trace through MBB. This updates MBB and
     998             : /// the blocks below it in the trace. It is assumed that the trace has already
     999             : /// been computed.
    1000         741 : void MachineTraceMetrics::Ensemble::
    1001             : computeInstrHeights(const MachineBasicBlock *MBB) {
    1002             :   // The bottom of the trace may already be computed.
    1003             :   // Find the blocks that need updating.
    1004         741 :   SmallVector<const MachineBasicBlock*, 8> Stack;
    1005             :   do {
    1006        1698 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
    1007             :     assert(TBI.hasValidHeight() && "Incomplete trace");
    1008         849 :     if (TBI.HasValidInstrHeights)
    1009             :       break;
    1010         828 :     Stack.push_back(MBB);
    1011        1656 :     TBI.LiveIns.clear();
    1012         828 :     MBB = TBI.Succ;
    1013         828 :   } while (MBB);
    1014             : 
    1015             :   // As we move upwards in the trace, keep track of instructions that are
    1016             :   // required by deeper trace instructions. Map MI -> height required so far.
    1017        1482 :   MIHeightMap Heights;
    1018             : 
    1019             :   // For physregs, the def isn't known when we see the use.
    1020             :   // Instead, keep track of the highest use of each regunit.
    1021        1482 :   SparseSet<LiveRegUnit> RegUnits;
    1022         741 :   RegUnits.setUniverse(MTM.TRI->getNumRegUnits());
    1023             : 
    1024             :   // If the bottom of the trace was already precomputed, initialize heights
    1025             :   // from its live-in list.
    1026             :   // MBB is the highest precomputed block in the trace.
    1027         741 :   if (MBB) {
    1028          42 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
    1029          97 :     for (LiveInReg &LI : TBI.LiveIns) {
    1030          68 :       if (TargetRegisterInfo::isVirtualRegister(LI.Reg)) {
    1031             :         // For virtual registers, the def latency is included.
    1032          36 :         unsigned &Height = Heights[MTM.MRI->getVRegDef(LI.Reg)];
    1033          18 :         if (Height < LI.Height)
    1034          14 :           Height = LI.Height;
    1035             :       } else {
    1036             :         // For register units, the def latency is not included because we don't
    1037             :         // know the def yet.
    1038          32 :         RegUnits[LI.Reg].Cycle = LI.Height;
    1039             :       }
    1040             :     }
    1041             :   }
    1042             : 
    1043             :   // Go through the trace blocks in bottom-up order.
    1044         741 :   SmallVector<DataDep, 8> Deps;
    1045        1569 :   for (;!Stack.empty(); Stack.pop_back()) {
    1046        1656 :     MBB = Stack.back();
    1047             :     DEBUG(dbgs() << "Heights for BB#" << MBB->getNumber() << ":\n");
    1048        1656 :     TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
    1049         828 :     TBI.HasValidInstrHeights = true;
    1050         828 :     TBI.CriticalPath = 0;
    1051             : 
    1052             :     DEBUG({
    1053             :       dbgs() << format("%7u Instructions\n", TBI.InstrHeight);
    1054             :       ArrayRef<unsigned> PRHeights = getProcResourceHeights(MBB->getNumber());
    1055             :       for (unsigned K = 0; K != PRHeights.size(); ++K)
    1056             :         if (PRHeights[K]) {
    1057             :           unsigned Factor = MTM.SchedModel.getResourceFactor(K);
    1058             :           dbgs() << format("%6uc @ ", MTM.getCycles(PRHeights[K]))
    1059             :                  << MTM.SchedModel.getProcResource(K)->Name << " ("
    1060             :                  << PRHeights[K]/Factor << " ops x" << Factor << ")\n";
    1061             :         }
    1062             :     });
    1063             : 
    1064             :     // Get dependencies from PHIs in the trace successor.
    1065         828 :     const MachineBasicBlock *Succ = TBI.Succ;
    1066             :     // If MBB is the last block in the trace, and it has a back-edge to the
    1067             :     // loop header, get loop-carried dependencies from PHIs in the header. For
    1068             :     // that purpose, pretend that all the loop header PHIs have height 0.
    1069         828 :     if (!Succ)
    1070         720 :       if (const MachineLoop *Loop = getLoopFor(MBB))
    1071          40 :         if (MBB->isSuccessor(Loop->getHeader()))
    1072          40 :           Succ = Loop->getHeader();
    1073             : 
    1074         828 :     if (Succ) {
    1075         892 :       for (const auto &PHI : *Succ) {
    1076             :         if (!PHI.isPHI())
    1077             :           break;
    1078         128 :         Deps.clear();
    1079         128 :         getPHIDeps(PHI, Deps, MBB, MTM.MRI);
    1080         128 :         if (!Deps.empty()) {
    1081             :           // Loop header PHI heights are all 0.
    1082         128 :           unsigned Height = TBI.Succ ? Cycles.lookup(&PHI).Height : 0;
    1083             :           DEBUG(dbgs() << "pred\t" << Height << '\t' << PHI);
    1084         256 :           if (pushDepHeight(Deps.front(), PHI, Height, Heights, MTM.SchedModel,
    1085         128 :                             MTM.TII))
    1086         360 :             addLiveIns(Deps.front().DefMI, Deps.front().DefOp, Stack);
    1087             :         }
    1088             :       }
    1089             :     }
    1090             : 
    1091             :     // Go through the block backwards.
    1092        2484 :     for (MachineBasicBlock::const_iterator BI = MBB->end(), BB = MBB->begin();
    1093       10838 :          BI != BB;) {
    1094       20020 :       const MachineInstr &MI = *--BI;
    1095             : 
    1096             :       // Find the MI height as determined by virtual register uses in the
    1097             :       // trace below.
    1098       10010 :       unsigned Cycle = 0;
    1099       10010 :       MIHeightMap::iterator HeightI = Heights.find(&MI);
    1100       20020 :       if (HeightI != Heights.end()) {
    1101        7848 :         Cycle = HeightI->second;
    1102             :         // We won't be seeing any more MI uses.
    1103        7848 :         Heights.erase(HeightI);
    1104             :       }
    1105             : 
    1106             :       // Don't process PHI deps. They depend on the specific predecessor, and
    1107             :       // we'll get them when visiting the predecessor.
    1108       10010 :       Deps.clear();
    1109        9912 :       bool HasPhysRegs = !MI.isPHI() && getDataDeps(MI, Deps, MTM.MRI);
    1110             : 
    1111             :       // There may also be regunit dependencies to include in the height.
    1112             :       if (HasPhysRegs)
    1113        4744 :         Cycle = updatePhysDepsUpwards(MI, Cycle, RegUnits, MTM.SchedModel,
    1114        4744 :                                       MTM.TII, MTM.TRI);
    1115             : 
    1116             :       // Update the required height of any virtual registers read by MI.
    1117       39912 :       for (const DataDep &Dep : Deps)
    1118        9882 :         if (pushDepHeight(Dep, MI, Cycle, Heights, MTM.SchedModel, MTM.TII))
    1119        7866 :           addLiveIns(Dep.DefMI, Dep.DefOp, Stack);
    1120             : 
    1121       20020 :       InstrCycles &MICycles = Cycles[&MI];
    1122       10010 :       MICycles.Height = Cycle;
    1123       10010 :       if (!TBI.HasValidInstrDepths) {
    1124             :         DEBUG(dbgs() << Cycle << '\t' << MI);
    1125         407 :         continue;
    1126             :       }
    1127             :       // Update critical path length.
    1128       19206 :       TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Depth);
    1129             :       DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << MI);
    1130             :     }
    1131             : 
    1132             :     // Update virtual live-in heights. They were added by addLiveIns() with a 0
    1133             :     // height because the final height isn't known until now.
    1134             :     DEBUG(dbgs() << "BB#" << MBB->getNumber() <<  " Live-ins:");
    1135        2669 :     for (LiveInReg &LIR : TBI.LiveIns) {
    1136         185 :       const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg);
    1137         185 :       LIR.Height = Heights.lookup(DefMI);
    1138             :       DEBUG(dbgs() << ' ' << PrintReg(LIR.Reg) << '@' << LIR.Height);
    1139             :     }
    1140             : 
    1141             :     // Transfer the live regunits to the live-in list.
    1142        2687 :     for (SparseSet<LiveRegUnit>::const_iterator
    1143        1656 :          RI = RegUnits.begin(), RE = RegUnits.end(); RI != RE; ++RI) {
    1144        5374 :       TBI.LiveIns.push_back(LiveInReg(RI->RegUnit, RI->Cycle));
    1145             :       DEBUG(dbgs() << ' ' << PrintRegUnit(RI->RegUnit, MTM.TRI)
    1146             :                    << '@' << RI->Cycle);
    1147             :     }
    1148             :     DEBUG(dbgs() << '\n');
    1149             : 
    1150         828 :     if (!TBI.HasValidInstrDepths)
    1151          87 :       continue;
    1152             :     // Add live-ins to the critical path length.
    1153         741 :     TBI.CriticalPath = std::max(TBI.CriticalPath,
    1154        2223 :                                 computeCrossBlockCriticalPath(TBI));
    1155             :     DEBUG(dbgs() << "Critical path: " << TBI.CriticalPath << '\n');
    1156             :   }
    1157         741 : }
    1158             : 
    1159             : MachineTraceMetrics::Trace
    1160        1587 : MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) {
    1161        3174 :   TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
    1162             : 
    1163        1587 :   if (!TBI.hasValidDepth() || !TBI.hasValidHeight())
    1164         787 :     computeTrace(MBB);
    1165        1587 :   if (!TBI.HasValidInstrDepths)
    1166         778 :     computeInstrDepths(MBB);
    1167        1587 :   if (!TBI.HasValidInstrHeights)
    1168         741 :     computeInstrHeights(MBB);
    1169             :   
    1170        1587 :   return Trace(*this, TBI);
    1171             : }
    1172             : 
    1173             : unsigned
    1174          65 : MachineTraceMetrics::Trace::getInstrSlack(const MachineInstr &MI) const {
    1175             :   assert(getBlockNum() == unsigned(MI.getParent()->getNumber()) &&
    1176             :          "MI must be in the trace center block");
    1177         130 :   InstrCycles Cyc = getInstrCycles(MI);
    1178         130 :   return getCriticalPath() - (Cyc.Depth + Cyc.Height);
    1179             : }
    1180             : 
    1181             : unsigned
    1182          99 : MachineTraceMetrics::Trace::getPHIDepth(const MachineInstr &PHI) const {
    1183         297 :   const MachineBasicBlock *MBB = TE.MTM.MF->getBlockNumbered(getBlockNum());
    1184         198 :   SmallVector<DataDep, 1> Deps;
    1185          99 :   getPHIDeps(PHI, Deps, MBB, TE.MTM.MRI);
    1186             :   assert(Deps.size() == 1 && "PHI doesn't have MBB as a predecessor");
    1187         198 :   DataDep &Dep = Deps.front();
    1188         198 :   unsigned DepCycle = getInstrCycles(*Dep.DefMI).Depth;
    1189             :   // Add latency if DefMI is a real instruction. Transients get latency 0.
    1190          99 :   if (!Dep.DefMI->isTransient())
    1191          41 :     DepCycle += TE.MTM.SchedModel.computeOperandLatency(Dep.DefMI, Dep.DefOp,
    1192             :                                                         &PHI, Dep.UseOp);
    1193         198 :   return DepCycle;
    1194             : }
    1195             : 
    1196             : /// When bottom is set include instructions in current block in estimate.
    1197          19 : unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const {
    1198             :   // Find the limiting processor resource.
    1199             :   // Numbers have been pre-scaled to be comparable.
    1200          19 :   unsigned PRMax = 0;
    1201          38 :   ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
    1202          19 :   if (Bottom) {
    1203          38 :     ArrayRef<unsigned> PRCycles = TE.MTM.getProcResourceCycles(getBlockNum());
    1204          35 :     for (unsigned K = 0; K != PRDepths.size(); ++K)
    1205          64 :       PRMax = std::max(PRMax, PRDepths[K] + PRCycles[K]);
    1206             :   } else {
    1207           0 :     for (unsigned K = 0; K != PRDepths.size(); ++K)
    1208           0 :       PRMax = std::max(PRMax, PRDepths[K]);
    1209             :   }
    1210             :   // Convert to cycle count.
    1211          38 :   PRMax = TE.MTM.getCycles(PRMax);
    1212             : 
    1213             :   /// All instructions before current block
    1214          19 :   unsigned Instrs = TBI.InstrDepth;
    1215             :   // plus instructions in current block
    1216          19 :   if (Bottom)
    1217          38 :     Instrs += TE.MTM.BlockInfo[getBlockNum()].InstrCount;
    1218          19 :   if (unsigned IW = TE.MTM.SchedModel.getIssueWidth())
    1219          19 :     Instrs /= IW;
    1220             :   // Assume issue width 1 without a schedule model.
    1221          19 :   return std::max(Instrs, PRMax);
    1222             : }
    1223             : 
    1224         637 : unsigned MachineTraceMetrics::Trace::getResourceLength(
    1225             :     ArrayRef<const MachineBasicBlock *> Extrablocks,
    1226             :     ArrayRef<const MCSchedClassDesc *> ExtraInstrs,
    1227             :     ArrayRef<const MCSchedClassDesc *> RemoveInstrs) const {
    1228             :   // Add up resources above and below the center block.
    1229        1274 :   ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
    1230        1274 :   ArrayRef<unsigned> PRHeights = TE.getProcResourceHeights(getBlockNum());
    1231         637 :   unsigned PRMax = 0;
    1232             : 
    1233             :   // Capture computing cycles from extra instructions
    1234             :   auto extraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
    1235             :                             unsigned ResourceIdx)
    1236       28372 :                          ->unsigned {
    1237       14936 :     unsigned Cycles = 0;
    1238       43628 :     for (const MCSchedClassDesc *SC : Instrs) {
    1239       13756 :       if (!SC->isValid())
    1240         320 :         continue;
    1241       47337 :       for (TargetSchedModel::ProcResIter
    1242       26872 :                PI = TE.MTM.SchedModel.getWriteProcResBegin(SC),
    1243       26872 :                PE = TE.MTM.SchedModel.getWriteProcResEnd(SC);
    1244       60773 :            PI != PE; ++PI) {
    1245       47337 :         if (PI->ProcResourceIdx != ResourceIdx)
    1246       43485 :           continue;
    1247        3852 :         Cycles +=
    1248        7704 :             (PI->Cycles * TE.MTM.SchedModel.getResourceFactor(ResourceIdx));
    1249             :       }
    1250             :     }
    1251       14936 :     return Cycles;
    1252         637 :   };
    1253             : 
    1254        8105 :   for (unsigned K = 0; K != PRDepths.size(); ++K) {
    1255       22404 :     unsigned PRCycles = PRDepths[K] + PRHeights[K];
    1256       21841 :     for (const MachineBasicBlock *MBB : Extrablocks)
    1257       13810 :       PRCycles += TE.MTM.getProcResourceCycles(MBB->getNumber())[K];
    1258        7468 :     PRCycles += extraCycles(ExtraInstrs, K);
    1259        7468 :     PRCycles -= extraCycles(RemoveInstrs, K);
    1260        7468 :     PRMax = std::max(PRMax, PRCycles);
    1261             :   }
    1262             :   // Convert to cycle count.
    1263        1274 :   PRMax = TE.MTM.getCycles(PRMax);
    1264             : 
    1265             :   // Instrs: #instructions in current trace outside current block.
    1266         637 :   unsigned Instrs = TBI.InstrDepth + TBI.InstrHeight;
    1267             :   // Add instruction count from the extra blocks.
    1268        1834 :   for (const MachineBasicBlock *MBB : Extrablocks)
    1269         560 :     Instrs += TE.MTM.getResources(MBB)->InstrCount;
    1270         637 :   Instrs += ExtraInstrs.size();
    1271         637 :   Instrs -= RemoveInstrs.size();
    1272         637 :   if (unsigned IW = TE.MTM.SchedModel.getIssueWidth())
    1273         637 :     Instrs /= IW;
    1274             :   // Assume issue width 1 without a schedule model.
    1275         637 :   return std::max(Instrs, PRMax);
    1276             : }
    1277             : 
    1278          15 : bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr &DefMI,
    1279             :                                               const MachineInstr &UseMI) const {
    1280          15 :   if (DefMI.getParent() == UseMI.getParent())
    1281             :     return true;
    1282             : 
    1283           2 :   const TraceBlockInfo &DepTBI = TE.BlockInfo[DefMI.getParent()->getNumber()];
    1284           2 :   const TraceBlockInfo &TBI = TE.BlockInfo[UseMI.getParent()->getNumber()];
    1285             : 
    1286             :   return DepTBI.isUsefulDominator(TBI);
    1287             : }
    1288             : 
    1289           0 : void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
    1290           0 :   OS << getName() << " ensemble:\n";
    1291           0 :   for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
    1292           0 :     OS << "  BB#" << i << '\t';
    1293           0 :     BlockInfo[i].print(OS);
    1294           0 :     OS << '\n';
    1295             :   }
    1296           0 : }
    1297             : 
    1298           0 : void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const {
    1299           0 :   if (hasValidDepth()) {
    1300           0 :     OS << "depth=" << InstrDepth;
    1301           0 :     if (Pred)
    1302           0 :       OS << " pred=BB#" << Pred->getNumber();
    1303             :     else
    1304           0 :       OS << " pred=null";
    1305           0 :     OS << " head=BB#" << Head;
    1306           0 :     if (HasValidInstrDepths)
    1307           0 :       OS << " +instrs";
    1308             :   } else
    1309           0 :     OS << "depth invalid";
    1310           0 :   OS << ", ";
    1311           0 :   if (hasValidHeight()) {
    1312           0 :     OS << "height=" << InstrHeight;
    1313           0 :     if (Succ)
    1314           0 :       OS << " succ=BB#" << Succ->getNumber();
    1315             :     else
    1316           0 :       OS << " succ=null";
    1317           0 :     OS << " tail=BB#" << Tail;
    1318           0 :     if (HasValidInstrHeights)
    1319           0 :       OS << " +instrs";
    1320             :   } else
    1321           0 :     OS << "height invalid";
    1322           0 :   if (HasValidInstrDepths && HasValidInstrHeights)
    1323           0 :     OS << ", crit=" << CriticalPath;
    1324           0 : }
    1325             : 
    1326           0 : void MachineTraceMetrics::Trace::print(raw_ostream &OS) const {
    1327           0 :   unsigned MBBNum = &TBI - &TE.BlockInfo[0];
    1328             : 
    1329           0 :   OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum
    1330           0 :      << " --> BB#" << TBI.Tail << ':';
    1331           0 :   if (TBI.hasValidHeight() && TBI.hasValidDepth())
    1332           0 :     OS << ' ' << getInstrCount() << " instrs.";
    1333           0 :   if (TBI.HasValidInstrDepths && TBI.HasValidInstrHeights)
    1334           0 :     OS << ' ' << TBI.CriticalPath << " cycles.";
    1335             : 
    1336           0 :   const MachineTraceMetrics::TraceBlockInfo *Block = &TBI;
    1337           0 :   OS << "\nBB#" << MBBNum;
    1338           0 :   while (Block->hasValidDepth() && Block->Pred) {
    1339           0 :     unsigned Num = Block->Pred->getNumber();
    1340           0 :     OS << " <- BB#" << Num;
    1341           0 :     Block = &TE.BlockInfo[Num];
    1342             :   }
    1343             : 
    1344           0 :   Block = &TBI;
    1345           0 :   OS << "\n    ";
    1346           0 :   while (Block->hasValidHeight() && Block->Succ) {
    1347           0 :     unsigned Num = Block->Succ->getNumber();
    1348           0 :     OS << " -> BB#" << Num;
    1349           0 :     Block = &TE.BlockInfo[Num];
    1350             :   }
    1351           0 :   OS << '\n';
    1352           0 : }

Generated by: LCOV version 1.13