Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 1 | //===- lib/CodeGen/MachineTraceMetrics.h - Super-scalar metrics -*- C++ -*-===// |
| 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 | // This file defines the interface for the MachineTraceMetrics analysis pass |
| 11 | // that estimates CPU resource usage and critical data dependency paths through |
| 12 | // preferred traces. This is useful for super-scalar CPUs where execution speed |
| 13 | // can be limited both by data dependencies and by limited execution resources. |
| 14 | // |
| 15 | // Out-of-order CPUs will often be executing instructions from multiple basic |
| 16 | // blocks at the same time. This makes it difficult to estimate the resource |
| 17 | // usage accurately in a single basic block. Resources can be estimated better |
| 18 | // by looking at a trace through the current basic block. |
| 19 | // |
| 20 | // For every block, the MachineTraceMetrics pass will pick a preferred trace |
| 21 | // that passes through the block. The trace is chosen based on loop structure, |
| 22 | // branch probabilities, and resource usage. The intention is to pick likely |
| 23 | // traces that would be the most affected by code transformations. |
| 24 | // |
| 25 | // It is expensive to compute a full arbitrary trace for every block, so to |
| 26 | // save some computations, traces are chosen to be convergent. This means that |
| 27 | // if the traces through basic blocks A and B ever cross when moving away from |
| 28 | // A and B, they never diverge again. This applies in both directions - If the |
| 29 | // traces meet above A and B, they won't diverge when going further back. |
| 30 | // |
| 31 | // Traces tend to align with loops. The trace through a block in an inner loop |
| 32 | // will begin at the loop entry block and end at a back edge. If there are |
| 33 | // nested loops, the trace may begin and end at those instead. |
| 34 | // |
| 35 | // For each trace, we compute the critical path length, which is the number of |
| 36 | // cycles required to execute the trace when execution is limited by data |
| 37 | // dependencies only. We also compute the resource height, which is the number |
| 38 | // of cycles required to execute all instructions in the trace when ignoring |
| 39 | // data dependencies. |
| 40 | // |
| 41 | // Every instruction in the current block has a slack - the number of cycles |
| 42 | // execution of the instruction can be delayed without extending the critical |
| 43 | // path. |
| 44 | // |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | #ifndef LLVM_CODEGEN_MACHINE_TRACE_METRICS_H |
| 48 | #define LLVM_CODEGEN_MACHINE_TRACE_METRICS_H |
| 49 | |
| 50 | #include "llvm/ADT/SmallVector.h" |
| 51 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 52 | |
| 53 | namespace llvm { |
| 54 | |
| 55 | class TargetInstrInfo; |
| 56 | class TargetRegisterInfo; |
| 57 | class MachineBasicBlock; |
| 58 | class MachineRegisterInfo; |
| 59 | class MachineLoopInfo; |
| 60 | class MachineLoop; |
| 61 | class raw_ostream; |
| 62 | |
| 63 | class MachineTraceMetrics : public MachineFunctionPass { |
| 64 | const TargetInstrInfo *TII; |
| 65 | const TargetRegisterInfo *TRI; |
| 66 | const MachineRegisterInfo *MRI; |
| 67 | const MachineLoopInfo *Loops; |
| 68 | |
| 69 | public: |
| 70 | class Ensemble; |
| 71 | class Trace; |
| 72 | static char ID; |
| 73 | MachineTraceMetrics(); |
| 74 | void getAnalysisUsage(AnalysisUsage&) const; |
| 75 | bool runOnMachineFunction(MachineFunction&); |
| 76 | void releaseMemory(); |
| 77 | |
| 78 | friend class Ensemble; |
| 79 | friend class Trace; |
| 80 | |
| 81 | /// Per-basic block information that doesn't depend on the trace through the |
| 82 | /// block. |
| 83 | struct FixedBlockInfo { |
| 84 | /// The number of non-trivial instructions in the block. |
| 85 | /// Doesn't count PHI and COPY instructions that are likely to be removed. |
| 86 | unsigned InstrCount; |
| 87 | |
| 88 | /// True when the block contains calls. |
| 89 | bool HasCalls; |
| 90 | |
| 91 | FixedBlockInfo() : InstrCount(~0u), HasCalls(false) {} |
| 92 | |
| 93 | /// Returns true when resource information for this block has been computed. |
| 94 | bool hasResources() const { return InstrCount != ~0u; } |
| 95 | |
| 96 | /// Invalidate resource information. |
| 97 | void invalidate() { InstrCount = ~0u; } |
| 98 | }; |
| 99 | |
| 100 | /// Get the fixed resource information about MBB. Compute it on demand. |
| 101 | const FixedBlockInfo *getResources(const MachineBasicBlock*); |
| 102 | |
| 103 | /// Per-basic block information that relates to a specific trace through the |
| 104 | /// block. Convergent traces means that only one of these is required per |
| 105 | /// block in a trace ensemble. |
| 106 | struct TraceBlockInfo { |
| 107 | /// Trace predecessor, or NULL for the first block in the trace. |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame^] | 108 | /// Valid when hasValidDepth(). |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 109 | const MachineBasicBlock *Pred; |
| 110 | |
| 111 | /// Trace successor, or NULL for the last block in the trace. |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame^] | 112 | /// Valid when hasValidHeight(). |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 113 | const MachineBasicBlock *Succ; |
| 114 | |
Jakob Stoklund Olesen | 0271a5f | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 115 | /// The block number of the head of the trace. (When hasValidDepth()). |
| 116 | unsigned Head; |
| 117 | |
| 118 | /// The block number of the tail of the trace. (When hasValidHeight()). |
| 119 | unsigned Tail; |
| 120 | |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 121 | /// Accumulated number of instructions in the trace above this block. |
| 122 | /// Does not include instructions in this block. |
| 123 | unsigned InstrDepth; |
| 124 | |
| 125 | /// Accumulated number of instructions in the trace below this block. |
| 126 | /// Includes instructions in this block. |
| 127 | unsigned InstrHeight; |
| 128 | |
| 129 | TraceBlockInfo() : Pred(0), Succ(0), InstrDepth(~0u), InstrHeight(~0u) {} |
| 130 | |
| 131 | /// Returns true if the depth resources have been computed from the trace |
| 132 | /// above this block. |
| 133 | bool hasValidDepth() const { return InstrDepth != ~0u; } |
| 134 | |
| 135 | /// Returns true if the height resources have been computed from the trace |
| 136 | /// below this block. |
| 137 | bool hasValidHeight() const { return InstrHeight != ~0u; } |
| 138 | |
| 139 | /// Invalidate depth resources when some block above this one has changed. |
| 140 | void invalidateDepth() { InstrDepth = ~0u; } |
| 141 | |
| 142 | /// Invalidate height resources when a block below this one has changed. |
| 143 | void invalidateHeight() { InstrHeight = ~0u; } |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame^] | 144 | |
| 145 | void print(raw_ostream&) const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /// A trace represents a plausible sequence of executed basic blocks that |
| 149 | /// passes through the current basic block one. The Trace class serves as a |
| 150 | /// handle to internal cached data structures. |
| 151 | class Trace { |
| 152 | Ensemble &TE; |
| 153 | TraceBlockInfo &TBI; |
| 154 | |
| 155 | public: |
| 156 | explicit Trace(Ensemble &te, TraceBlockInfo &tbi) : TE(te), TBI(tbi) {} |
| 157 | void print(raw_ostream&) const; |
| 158 | |
| 159 | /// Compute the total number of instructions in the trace. |
| 160 | unsigned getInstrCount() const { |
| 161 | return TBI.InstrDepth + TBI.InstrHeight; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | /// A trace ensemble is a collection of traces selected using the same |
| 166 | /// strategy, for example 'minimum resource height'. There is one trace for |
| 167 | /// every block in the function. |
| 168 | class Ensemble { |
| 169 | SmallVector<TraceBlockInfo, 4> BlockInfo; |
| 170 | friend class Trace; |
| 171 | |
| 172 | void computeTrace(const MachineBasicBlock*); |
| 173 | void computeDepthResources(const MachineBasicBlock*); |
| 174 | void computeHeightResources(const MachineBasicBlock*); |
| 175 | |
| 176 | protected: |
| 177 | MachineTraceMetrics &CT; |
| 178 | virtual const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) =0; |
| 179 | virtual const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) =0; |
| 180 | explicit Ensemble(MachineTraceMetrics*); |
| 181 | MachineLoop *getLoopFor(const MachineBasicBlock*); |
| 182 | const TraceBlockInfo *getDepthResources(const MachineBasicBlock*) const; |
| 183 | const TraceBlockInfo *getHeightResources(const MachineBasicBlock*) const; |
| 184 | |
| 185 | public: |
| 186 | virtual ~Ensemble(); |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame^] | 187 | virtual const char *getName() const =0; |
| 188 | void print(raw_ostream&) const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 189 | void invalidate(const MachineBasicBlock *MBB); |
| 190 | |
| 191 | /// Get the trace that passes through MBB. |
| 192 | /// The trace is computed on demand. |
| 193 | Trace getTrace(const MachineBasicBlock *MBB); |
| 194 | }; |
| 195 | |
| 196 | /// Strategies for selecting traces. |
| 197 | enum Strategy { |
| 198 | /// Select the trace through a block that has the fewest instructions. |
| 199 | TS_MinInstrCount, |
| 200 | |
| 201 | TS_NumStrategies |
| 202 | }; |
| 203 | |
| 204 | /// Get the trace ensemble representing the given trace selection strategy. |
| 205 | /// The returned Ensemble object is owned by the MachineTraceMetrics analysis, |
| 206 | /// and valid for the lifetime of the analysis pass. |
| 207 | Ensemble *getEnsemble(Strategy); |
| 208 | |
| 209 | /// Invalidate cached information about MBB. This must be called *before* MBB |
| 210 | /// is erased, or the CFG is otherwise changed. |
| 211 | void invalidate(const MachineBasicBlock *MBB); |
| 212 | |
| 213 | private: |
| 214 | // One entry per basic block, indexed by block number. |
| 215 | SmallVector<FixedBlockInfo, 4> BlockInfo; |
| 216 | |
| 217 | // One ensemble per strategy. |
| 218 | Ensemble* Ensembles[TS_NumStrategies]; |
| 219 | }; |
| 220 | |
| 221 | inline raw_ostream &operator<<(raw_ostream &OS, |
| 222 | const MachineTraceMetrics::Trace &Tr) { |
| 223 | Tr.print(OS); |
| 224 | return OS; |
| 225 | } |
| 226 | |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame^] | 227 | inline raw_ostream &operator<<(raw_ostream &OS, |
| 228 | const MachineTraceMetrics::Ensemble &En) { |
| 229 | En.print(OS); |
| 230 | return OS; |
| 231 | } |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 232 | } // end namespace llvm |
| 233 | |
| 234 | #endif |