xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/CodeGen/MachineTraceMetrics.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- lib/CodeGen/MachineTraceMetrics.h - Super-scalar metrics -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the interface for the MachineTraceMetrics analysis pass
100b57cec5SDimitry Andric // that estimates CPU resource usage and critical data dependency paths through
110b57cec5SDimitry Andric // preferred traces. This is useful for super-scalar CPUs where execution speed
120b57cec5SDimitry Andric // can be limited both by data dependencies and by limited execution resources.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric // Out-of-order CPUs will often be executing instructions from multiple basic
150b57cec5SDimitry Andric // blocks at the same time. This makes it difficult to estimate the resource
160b57cec5SDimitry Andric // usage accurately in a single basic block. Resources can be estimated better
170b57cec5SDimitry Andric // by looking at a trace through the current basic block.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // For every block, the MachineTraceMetrics pass will pick a preferred trace
200b57cec5SDimitry Andric // that passes through the block. The trace is chosen based on loop structure,
210b57cec5SDimitry Andric // branch probabilities, and resource usage. The intention is to pick likely
220b57cec5SDimitry Andric // traces that would be the most affected by code transformations.
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric // It is expensive to compute a full arbitrary trace for every block, so to
250b57cec5SDimitry Andric // save some computations, traces are chosen to be convergent. This means that
260b57cec5SDimitry Andric // if the traces through basic blocks A and B ever cross when moving away from
270b57cec5SDimitry Andric // A and B, they never diverge again. This applies in both directions - If the
280b57cec5SDimitry Andric // traces meet above A and B, they won't diverge when going further back.
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric // Traces tend to align with loops. The trace through a block in an inner loop
310b57cec5SDimitry Andric // will begin at the loop entry block and end at a back edge. If there are
320b57cec5SDimitry Andric // nested loops, the trace may begin and end at those instead.
330b57cec5SDimitry Andric //
340b57cec5SDimitry Andric // For each trace, we compute the critical path length, which is the number of
350b57cec5SDimitry Andric // cycles required to execute the trace when execution is limited by data
360b57cec5SDimitry Andric // dependencies only. We also compute the resource height, which is the number
370b57cec5SDimitry Andric // of cycles required to execute all instructions in the trace when ignoring
380b57cec5SDimitry Andric // data dependencies.
390b57cec5SDimitry Andric //
400b57cec5SDimitry Andric // Every instruction in the current block has a slack - the number of cycles
410b57cec5SDimitry Andric // execution of the instruction can be delayed without extending the critical
420b57cec5SDimitry Andric // path.
430b57cec5SDimitry Andric //
440b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #ifndef LLVM_CODEGEN_MACHINETRACEMETRICS_H
470b57cec5SDimitry Andric #define LLVM_CODEGEN_MACHINETRACEMETRICS_H
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric #include "llvm/ADT/SparseSet.h"
500b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
510b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
520b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
530b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
540b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
550b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSchedule.h"
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric namespace llvm {
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric class AnalysisUsage;
600b57cec5SDimitry Andric class MachineFunction;
610b57cec5SDimitry Andric class MachineInstr;
620b57cec5SDimitry Andric class MachineLoop;
630b57cec5SDimitry Andric class MachineLoopInfo;
640b57cec5SDimitry Andric class MachineRegisterInfo;
650b57cec5SDimitry Andric struct MCSchedClassDesc;
660b57cec5SDimitry Andric class raw_ostream;
670b57cec5SDimitry Andric class TargetInstrInfo;
680b57cec5SDimitry Andric class TargetRegisterInfo;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric // Keep track of physreg data dependencies by recording each live register unit.
710b57cec5SDimitry Andric // Associate each regunit with an instruction operand. Depending on the
720b57cec5SDimitry Andric // direction instructions are scanned, it could be the operand that defined the
730b57cec5SDimitry Andric // regunit, or the highest operand to read the regunit.
740b57cec5SDimitry Andric struct LiveRegUnit {
750b57cec5SDimitry Andric   unsigned RegUnit;
760b57cec5SDimitry Andric   unsigned Cycle = 0;
770b57cec5SDimitry Andric   const MachineInstr *MI = nullptr;
780b57cec5SDimitry Andric   unsigned Op = 0;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   unsigned getSparseSetIndex() const { return RegUnit; }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   LiveRegUnit(unsigned RU) : RegUnit(RU) {}
830b57cec5SDimitry Andric };
840b57cec5SDimitry Andric 
8506c3fb27SDimitry Andric /// Strategies for selecting traces.
8606c3fb27SDimitry Andric enum class MachineTraceStrategy {
8706c3fb27SDimitry Andric   /// Select the trace through a block that has the fewest instructions.
8806c3fb27SDimitry Andric   TS_MinInstrCount,
8906c3fb27SDimitry Andric   /// Select the trace that contains only the current basic block. For instance,
9006c3fb27SDimitry Andric   /// this strategy can be used by MachineCombiner to make better decisions when
9106c3fb27SDimitry Andric   /// we estimate critical path for in-order cores.
9206c3fb27SDimitry Andric   TS_Local,
9306c3fb27SDimitry Andric   TS_NumStrategies
9406c3fb27SDimitry Andric };
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric class MachineTraceMetrics : public MachineFunctionPass {
970b57cec5SDimitry Andric   const MachineFunction *MF = nullptr;
980b57cec5SDimitry Andric   const TargetInstrInfo *TII = nullptr;
990b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = nullptr;
1000b57cec5SDimitry Andric   const MachineRegisterInfo *MRI = nullptr;
1010b57cec5SDimitry Andric   const MachineLoopInfo *Loops = nullptr;
1020b57cec5SDimitry Andric   TargetSchedModel SchedModel;
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric public:
1050b57cec5SDimitry Andric   friend class Ensemble;
1060b57cec5SDimitry Andric   friend class Trace;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   class Ensemble;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   static char ID;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   MachineTraceMetrics();
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage&) const override;
1150b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction&) override;
1160b57cec5SDimitry Andric   void releaseMemory() override;
1170b57cec5SDimitry Andric   void verifyAnalysis() const override;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   /// Per-basic block information that doesn't depend on the trace through the
1200b57cec5SDimitry Andric   /// block.
1210b57cec5SDimitry Andric   struct FixedBlockInfo {
1220b57cec5SDimitry Andric     /// The number of non-trivial instructions in the block.
1230b57cec5SDimitry Andric     /// Doesn't count PHI and COPY instructions that are likely to be removed.
1240b57cec5SDimitry Andric     unsigned InstrCount = ~0u;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     /// True when the block contains calls.
1270b57cec5SDimitry Andric     bool HasCalls = false;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     FixedBlockInfo() = default;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric     /// Returns true when resource information for this block has been computed.
1320b57cec5SDimitry Andric     bool hasResources() const { return InstrCount != ~0u; }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     /// Invalidate resource information.
1350b57cec5SDimitry Andric     void invalidate() { InstrCount = ~0u; }
1360b57cec5SDimitry Andric   };
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   /// Get the fixed resource information about MBB. Compute it on demand.
1390b57cec5SDimitry Andric   const FixedBlockInfo *getResources(const MachineBasicBlock*);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   /// Get the scaled number of cycles used per processor resource in MBB.
1420b57cec5SDimitry Andric   /// This is an array with SchedModel.getNumProcResourceKinds() entries.
1430b57cec5SDimitry Andric   /// The getResources() function above must have been called first.
1440b57cec5SDimitry Andric   ///
1450b57cec5SDimitry Andric   /// These numbers have already been scaled by SchedModel.getResourceFactor().
1465f757f3fSDimitry Andric   ArrayRef<unsigned> getProcReleaseAtCycles(unsigned MBBNum) const;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   /// A virtual register or regunit required by a basic block or its trace
1490b57cec5SDimitry Andric   /// successors.
1500b57cec5SDimitry Andric   struct LiveInReg {
1510b57cec5SDimitry Andric     /// The virtual register required, or a register unit.
152e8d8bef9SDimitry Andric     Register Reg;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     /// For virtual registers: Minimum height of the defining instruction.
1550b57cec5SDimitry Andric     /// For regunits: Height of the highest user in the trace.
1560b57cec5SDimitry Andric     unsigned Height;
1570b57cec5SDimitry Andric 
158e8d8bef9SDimitry Andric     LiveInReg(Register Reg, unsigned Height = 0) : Reg(Reg), Height(Height) {}
1590b57cec5SDimitry Andric   };
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   /// Per-basic block information that relates to a specific trace through the
1620b57cec5SDimitry Andric   /// block. Convergent traces means that only one of these is required per
1630b57cec5SDimitry Andric   /// block in a trace ensemble.
1640b57cec5SDimitry Andric   struct TraceBlockInfo {
1650b57cec5SDimitry Andric     /// Trace predecessor, or NULL for the first block in the trace.
1660b57cec5SDimitry Andric     /// Valid when hasValidDepth().
1670b57cec5SDimitry Andric     const MachineBasicBlock *Pred = nullptr;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric     /// Trace successor, or NULL for the last block in the trace.
1700b57cec5SDimitry Andric     /// Valid when hasValidHeight().
1710b57cec5SDimitry Andric     const MachineBasicBlock *Succ = nullptr;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric     /// The block number of the head of the trace. (When hasValidDepth()).
1740b57cec5SDimitry Andric     unsigned Head;
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     /// The block number of the tail of the trace. (When hasValidHeight()).
1770b57cec5SDimitry Andric     unsigned Tail;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric     /// Accumulated number of instructions in the trace above this block.
1800b57cec5SDimitry Andric     /// Does not include instructions in this block.
1810b57cec5SDimitry Andric     unsigned InstrDepth = ~0u;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric     /// Accumulated number of instructions in the trace below this block.
1840b57cec5SDimitry Andric     /// Includes instructions in this block.
1850b57cec5SDimitry Andric     unsigned InstrHeight = ~0u;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric     TraceBlockInfo() = default;
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric     /// Returns true if the depth resources have been computed from the trace
1900b57cec5SDimitry Andric     /// above this block.
1910b57cec5SDimitry Andric     bool hasValidDepth() const { return InstrDepth != ~0u; }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric     /// Returns true if the height resources have been computed from the trace
1940b57cec5SDimitry Andric     /// below this block.
1950b57cec5SDimitry Andric     bool hasValidHeight() const { return InstrHeight != ~0u; }
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric     /// Invalidate depth resources when some block above this one has changed.
1980b57cec5SDimitry Andric     void invalidateDepth() { InstrDepth = ~0u; HasValidInstrDepths = false; }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     /// Invalidate height resources when a block below this one has changed.
2010b57cec5SDimitry Andric     void invalidateHeight() { InstrHeight = ~0u; HasValidInstrHeights = false; }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     /// Assuming that this is a dominator of TBI, determine if it contains
2040b57cec5SDimitry Andric     /// useful instruction depths. A dominating block can be above the current
2050b57cec5SDimitry Andric     /// trace head, and any dependencies from such a far away dominator are not
2060b57cec5SDimitry Andric     /// expected to affect the critical path.
2070b57cec5SDimitry Andric     ///
2080b57cec5SDimitry Andric     /// Also returns true when TBI == this.
2090b57cec5SDimitry Andric     bool isUsefulDominator(const TraceBlockInfo &TBI) const {
2100b57cec5SDimitry Andric       // The trace for TBI may not even be calculated yet.
2110b57cec5SDimitry Andric       if (!hasValidDepth() || !TBI.hasValidDepth())
2120b57cec5SDimitry Andric         return false;
2130b57cec5SDimitry Andric       // Instruction depths are only comparable if the traces share a head.
2140b57cec5SDimitry Andric       if (Head != TBI.Head)
2150b57cec5SDimitry Andric         return false;
2160b57cec5SDimitry Andric       // It is almost always the case that TBI belongs to the same trace as
2170b57cec5SDimitry Andric       // this block, but rare convoluted cases involving irreducible control
2180b57cec5SDimitry Andric       // flow, a dominator may share a trace head without actually being on the
2190b57cec5SDimitry Andric       // same trace as TBI. This is not a big problem as long as it doesn't
2200b57cec5SDimitry Andric       // increase the instruction depth.
2210b57cec5SDimitry Andric       return HasValidInstrDepths && InstrDepth <= TBI.InstrDepth;
2220b57cec5SDimitry Andric     }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric     // Data-dependency-related information. Per-instruction depth and height
2250b57cec5SDimitry Andric     // are computed from data dependencies in the current trace, using
2260b57cec5SDimitry Andric     // itinerary data.
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     /// Instruction depths have been computed. This implies hasValidDepth().
2290b57cec5SDimitry Andric     bool HasValidInstrDepths = false;
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric     /// Instruction heights have been computed. This implies hasValidHeight().
2320b57cec5SDimitry Andric     bool HasValidInstrHeights = false;
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     /// Critical path length. This is the number of cycles in the longest data
2350b57cec5SDimitry Andric     /// dependency chain through the trace. This is only valid when both
2360b57cec5SDimitry Andric     /// HasValidInstrDepths and HasValidInstrHeights are set.
2370b57cec5SDimitry Andric     unsigned CriticalPath;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     /// Live-in registers. These registers are defined above the current block
2400b57cec5SDimitry Andric     /// and used by this block or a block below it.
2410b57cec5SDimitry Andric     /// This does not include PHI uses in the current block, but it does
2420b57cec5SDimitry Andric     /// include PHI uses in deeper blocks.
2430b57cec5SDimitry Andric     SmallVector<LiveInReg, 4> LiveIns;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric     void print(raw_ostream&) const;
246*0fca6ea1SDimitry Andric     void dump() const { print(dbgs()); }
2470b57cec5SDimitry Andric   };
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric   /// InstrCycles represents the cycle height and depth of an instruction in a
2500b57cec5SDimitry Andric   /// trace.
2510b57cec5SDimitry Andric   struct InstrCycles {
2520b57cec5SDimitry Andric     /// Earliest issue cycle as determined by data dependencies and instruction
2530b57cec5SDimitry Andric     /// latencies from the beginning of the trace. Data dependencies from
2540b57cec5SDimitry Andric     /// before the trace are not included.
2550b57cec5SDimitry Andric     unsigned Depth;
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric     /// Minimum number of cycles from this instruction is issued to the of the
2580b57cec5SDimitry Andric     /// trace, as determined by data dependencies and instruction latencies.
2590b57cec5SDimitry Andric     unsigned Height;
2600b57cec5SDimitry Andric   };
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   /// A trace represents a plausible sequence of executed basic blocks that
2630b57cec5SDimitry Andric   /// passes through the current basic block one. The Trace class serves as a
2640b57cec5SDimitry Andric   /// handle to internal cached data structures.
2650b57cec5SDimitry Andric   class Trace {
2660b57cec5SDimitry Andric     Ensemble &TE;
2670b57cec5SDimitry Andric     TraceBlockInfo &TBI;
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric     unsigned getBlockNum() const { return &TBI - &TE.BlockInfo[0]; }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   public:
2720b57cec5SDimitry Andric     explicit Trace(Ensemble &te, TraceBlockInfo &tbi) : TE(te), TBI(tbi) {}
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     void print(raw_ostream&) const;
275*0fca6ea1SDimitry Andric     void dump() const { print(dbgs()); }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric     /// Compute the total number of instructions in the trace.
2780b57cec5SDimitry Andric     unsigned getInstrCount() const {
2790b57cec5SDimitry Andric       return TBI.InstrDepth + TBI.InstrHeight;
2800b57cec5SDimitry Andric     }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric     /// Return the resource depth of the top/bottom of the trace center block.
2830b57cec5SDimitry Andric     /// This is the number of cycles required to execute all instructions from
2840b57cec5SDimitry Andric     /// the trace head to the trace center block. The resource depth only
2850b57cec5SDimitry Andric     /// considers execution resources, it ignores data dependencies.
2860b57cec5SDimitry Andric     /// When Bottom is set, instructions in the trace center block are included.
2870b57cec5SDimitry Andric     unsigned getResourceDepth(bool Bottom) const;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric     /// Return the resource length of the trace. This is the number of cycles
2900b57cec5SDimitry Andric     /// required to execute the instructions in the trace if they were all
2910b57cec5SDimitry Andric     /// independent, exposing the maximum instruction-level parallelism.
2920b57cec5SDimitry Andric     ///
2930b57cec5SDimitry Andric     /// Any blocks in Extrablocks are included as if they were part of the
2940b57cec5SDimitry Andric     /// trace. Likewise, extra resources required by the specified scheduling
2950b57cec5SDimitry Andric     /// classes are included. For the caller to account for extra machine
2960b57cec5SDimitry Andric     /// instructions, it must first resolve each instruction's scheduling class.
2970b57cec5SDimitry Andric     unsigned getResourceLength(
298bdd1243dSDimitry Andric         ArrayRef<const MachineBasicBlock *> Extrablocks = std::nullopt,
299bdd1243dSDimitry Andric         ArrayRef<const MCSchedClassDesc *> ExtraInstrs = std::nullopt,
300bdd1243dSDimitry Andric         ArrayRef<const MCSchedClassDesc *> RemoveInstrs = std::nullopt) const;
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric     /// Return the length of the (data dependency) critical path through the
3030b57cec5SDimitry Andric     /// trace.
3040b57cec5SDimitry Andric     unsigned getCriticalPath() const { return TBI.CriticalPath; }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric     /// Return the depth and height of MI. The depth is only valid for
3070b57cec5SDimitry Andric     /// instructions in or above the trace center block. The height is only
3080b57cec5SDimitry Andric     /// valid for instructions in or below the trace center block.
3090b57cec5SDimitry Andric     InstrCycles getInstrCycles(const MachineInstr &MI) const {
3100b57cec5SDimitry Andric       return TE.Cycles.lookup(&MI);
3110b57cec5SDimitry Andric     }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric     /// Return the slack of MI. This is the number of cycles MI can be delayed
3140b57cec5SDimitry Andric     /// before the critical path becomes longer.
3150b57cec5SDimitry Andric     /// MI must be an instruction in the trace center block.
3160b57cec5SDimitry Andric     unsigned getInstrSlack(const MachineInstr &MI) const;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric     /// Return the Depth of a PHI instruction in a trace center block successor.
3190b57cec5SDimitry Andric     /// The PHI does not have to be part of the trace.
3200b57cec5SDimitry Andric     unsigned getPHIDepth(const MachineInstr &PHI) const;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric     /// A dependence is useful if the basic block of the defining instruction
3230b57cec5SDimitry Andric     /// is part of the trace of the user instruction. It is assumed that DefMI
3240b57cec5SDimitry Andric     /// dominates UseMI (see also isUsefulDominator).
3250b57cec5SDimitry Andric     bool isDepInTrace(const MachineInstr &DefMI,
3260b57cec5SDimitry Andric                       const MachineInstr &UseMI) const;
3270b57cec5SDimitry Andric   };
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   /// A trace ensemble is a collection of traces selected using the same
3300b57cec5SDimitry Andric   /// strategy, for example 'minimum resource height'. There is one trace for
3310b57cec5SDimitry Andric   /// every block in the function.
3320b57cec5SDimitry Andric   class Ensemble {
3330b57cec5SDimitry Andric     friend class Trace;
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric     SmallVector<TraceBlockInfo, 4> BlockInfo;
3360b57cec5SDimitry Andric     DenseMap<const MachineInstr*, InstrCycles> Cycles;
3370b57cec5SDimitry Andric     SmallVector<unsigned, 0> ProcResourceDepths;
3380b57cec5SDimitry Andric     SmallVector<unsigned, 0> ProcResourceHeights;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric     void computeTrace(const MachineBasicBlock*);
3410b57cec5SDimitry Andric     void computeDepthResources(const MachineBasicBlock*);
3420b57cec5SDimitry Andric     void computeHeightResources(const MachineBasicBlock*);
3430b57cec5SDimitry Andric     unsigned computeCrossBlockCriticalPath(const TraceBlockInfo&);
3440b57cec5SDimitry Andric     void computeInstrDepths(const MachineBasicBlock*);
3450b57cec5SDimitry Andric     void computeInstrHeights(const MachineBasicBlock*);
3460b57cec5SDimitry Andric     void addLiveIns(const MachineInstr *DefMI, unsigned DefOp,
3470b57cec5SDimitry Andric                     ArrayRef<const MachineBasicBlock*> Trace);
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   protected:
3500b57cec5SDimitry Andric     MachineTraceMetrics &MTM;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric     explicit Ensemble(MachineTraceMetrics*);
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric     virtual const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) =0;
3550b57cec5SDimitry Andric     virtual const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) =0;
3560b57cec5SDimitry Andric     const MachineLoop *getLoopFor(const MachineBasicBlock*) const;
3570b57cec5SDimitry Andric     const TraceBlockInfo *getDepthResources(const MachineBasicBlock*) const;
3580b57cec5SDimitry Andric     const TraceBlockInfo *getHeightResources(const MachineBasicBlock*) const;
3590b57cec5SDimitry Andric     ArrayRef<unsigned> getProcResourceDepths(unsigned MBBNum) const;
3600b57cec5SDimitry Andric     ArrayRef<unsigned> getProcResourceHeights(unsigned MBBNum) const;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   public:
3630b57cec5SDimitry Andric     virtual ~Ensemble();
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric     virtual const char *getName() const = 0;
3660b57cec5SDimitry Andric     void print(raw_ostream &) const;
367*0fca6ea1SDimitry Andric     void dump() const { print(dbgs()); }
3680b57cec5SDimitry Andric     void invalidate(const MachineBasicBlock *MBB);
3690b57cec5SDimitry Andric     void verify() const;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric     /// Get the trace that passes through MBB.
3720b57cec5SDimitry Andric     /// The trace is computed on demand.
3730b57cec5SDimitry Andric     Trace getTrace(const MachineBasicBlock *MBB);
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric     /// Updates the depth of an machine instruction, given RegUnits.
3760b57cec5SDimitry Andric     void updateDepth(TraceBlockInfo &TBI, const MachineInstr&,
3770b57cec5SDimitry Andric                      SparseSet<LiveRegUnit> &RegUnits);
3780b57cec5SDimitry Andric     void updateDepth(const MachineBasicBlock *, const MachineInstr&,
3790b57cec5SDimitry Andric                      SparseSet<LiveRegUnit> &RegUnits);
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric     /// Updates the depth of the instructions from Start to End.
3820b57cec5SDimitry Andric     void updateDepths(MachineBasicBlock::iterator Start,
3830b57cec5SDimitry Andric                       MachineBasicBlock::iterator End,
3840b57cec5SDimitry Andric                       SparseSet<LiveRegUnit> &RegUnits);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   };
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   /// Get the trace ensemble representing the given trace selection strategy.
3890b57cec5SDimitry Andric   /// The returned Ensemble object is owned by the MachineTraceMetrics analysis,
3900b57cec5SDimitry Andric   /// and valid for the lifetime of the analysis pass.
39106c3fb27SDimitry Andric   Ensemble *getEnsemble(MachineTraceStrategy);
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   /// Invalidate cached information about MBB. This must be called *before* MBB
3940b57cec5SDimitry Andric   /// is erased, or the CFG is otherwise changed.
3950b57cec5SDimitry Andric   ///
3960b57cec5SDimitry Andric   /// This invalidates per-block information about resource usage for MBB only,
3970b57cec5SDimitry Andric   /// and it invalidates per-trace information for any trace that passes
3980b57cec5SDimitry Andric   /// through MBB.
3990b57cec5SDimitry Andric   ///
4000b57cec5SDimitry Andric   /// Call Ensemble::getTrace() again to update any trace handles.
4010b57cec5SDimitry Andric   void invalidate(const MachineBasicBlock *MBB);
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric private:
4040b57cec5SDimitry Andric   // One entry per basic block, indexed by block number.
4050b57cec5SDimitry Andric   SmallVector<FixedBlockInfo, 4> BlockInfo;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   // Cycles consumed on each processor resource per block.
4080b57cec5SDimitry Andric   // The number of processor resource kinds is constant for a given subtarget,
4090b57cec5SDimitry Andric   // but it is not known at compile time. The number of cycles consumed by
4105f757f3fSDimitry Andric   // block B on processor resource R is at ProcReleaseAtCycles[B*Kinds + R]
4110b57cec5SDimitry Andric   // where Kinds = SchedModel.getNumProcResourceKinds().
4125f757f3fSDimitry Andric   SmallVector<unsigned, 0> ProcReleaseAtCycles;
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   // One ensemble per strategy.
41506c3fb27SDimitry Andric   Ensemble
41606c3fb27SDimitry Andric       *Ensembles[static_cast<size_t>(MachineTraceStrategy::TS_NumStrategies)];
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   // Convert scaled resource usage to a cycle count that can be compared with
4190b57cec5SDimitry Andric   // latencies.
4200b57cec5SDimitry Andric   unsigned getCycles(unsigned Scaled) {
4210b57cec5SDimitry Andric     unsigned Factor = SchedModel.getLatencyFactor();
4220b57cec5SDimitry Andric     return (Scaled + Factor - 1) / Factor;
4230b57cec5SDimitry Andric   }
4240b57cec5SDimitry Andric };
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric inline raw_ostream &operator<<(raw_ostream &OS,
4270b57cec5SDimitry Andric                                const MachineTraceMetrics::Trace &Tr) {
4280b57cec5SDimitry Andric   Tr.print(OS);
4290b57cec5SDimitry Andric   return OS;
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric inline raw_ostream &operator<<(raw_ostream &OS,
4330b57cec5SDimitry Andric                                const MachineTraceMetrics::Ensemble &En) {
4340b57cec5SDimitry Andric   En.print(OS);
4350b57cec5SDimitry Andric   return OS;
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric } // end namespace llvm
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric #endif // LLVM_CODEGEN_MACHINETRACEMETRICS_H
441