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 | |
Jakob Stoklund Olesen | c7f44b8 | 2012-08-01 22:36:00 +0000 | [diff] [blame^] | 50 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/DenseMap.h" |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 53 | |
| 54 | namespace llvm { |
| 55 | |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 56 | class InstrItineraryData; |
| 57 | class MachineBasicBlock; |
| 58 | class MachineInstr; |
| 59 | class MachineLoop; |
| 60 | class MachineLoopInfo; |
| 61 | class MachineRegisterInfo; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 62 | class TargetInstrInfo; |
| 63 | class TargetRegisterInfo; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 64 | class raw_ostream; |
| 65 | |
| 66 | class MachineTraceMetrics : public MachineFunctionPass { |
Jakob Stoklund Olesen | a1b2bf7 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 67 | const MachineFunction *MF; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 68 | const TargetInstrInfo *TII; |
| 69 | const TargetRegisterInfo *TRI; |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 70 | const InstrItineraryData *ItinData; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 71 | const MachineRegisterInfo *MRI; |
| 72 | const MachineLoopInfo *Loops; |
| 73 | |
| 74 | public: |
| 75 | class Ensemble; |
| 76 | class Trace; |
| 77 | static char ID; |
| 78 | MachineTraceMetrics(); |
| 79 | void getAnalysisUsage(AnalysisUsage&) const; |
| 80 | bool runOnMachineFunction(MachineFunction&); |
| 81 | void releaseMemory(); |
Jakob Stoklund Olesen | ef6c76c | 2012-07-30 20:57:50 +0000 | [diff] [blame] | 82 | void verifyAnalysis() const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 83 | |
| 84 | friend class Ensemble; |
| 85 | friend class Trace; |
| 86 | |
| 87 | /// Per-basic block information that doesn't depend on the trace through the |
| 88 | /// block. |
| 89 | struct FixedBlockInfo { |
| 90 | /// The number of non-trivial instructions in the block. |
| 91 | /// Doesn't count PHI and COPY instructions that are likely to be removed. |
| 92 | unsigned InstrCount; |
| 93 | |
| 94 | /// True when the block contains calls. |
| 95 | bool HasCalls; |
| 96 | |
| 97 | FixedBlockInfo() : InstrCount(~0u), HasCalls(false) {} |
| 98 | |
| 99 | /// Returns true when resource information for this block has been computed. |
| 100 | bool hasResources() const { return InstrCount != ~0u; } |
| 101 | |
| 102 | /// Invalidate resource information. |
| 103 | void invalidate() { InstrCount = ~0u; } |
| 104 | }; |
| 105 | |
| 106 | /// Get the fixed resource information about MBB. Compute it on demand. |
| 107 | const FixedBlockInfo *getResources(const MachineBasicBlock*); |
| 108 | |
Jakob Stoklund Olesen | c7f44b8 | 2012-08-01 22:36:00 +0000 | [diff] [blame^] | 109 | /// A virtual register or regunit required by a basic block or its trace |
| 110 | /// successors. |
| 111 | struct LiveInReg { |
| 112 | /// The virtual register required, or a register unit. |
| 113 | unsigned Reg; |
| 114 | |
| 115 | /// For virtual registers: Minimum height of the defining instruction. |
| 116 | /// For regunits: Height of the highest user in the trace. |
| 117 | unsigned Height; |
| 118 | |
| 119 | LiveInReg(unsigned Reg, unsigned Height = 0) : Reg(Reg), Height(Height) {} |
| 120 | }; |
| 121 | |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 122 | /// Per-basic block information that relates to a specific trace through the |
| 123 | /// block. Convergent traces means that only one of these is required per |
| 124 | /// block in a trace ensemble. |
| 125 | struct TraceBlockInfo { |
| 126 | /// Trace predecessor, or NULL for the first block in the trace. |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 127 | /// Valid when hasValidDepth(). |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 128 | const MachineBasicBlock *Pred; |
| 129 | |
| 130 | /// Trace successor, or NULL for the last block in the trace. |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 131 | /// Valid when hasValidHeight(). |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 132 | const MachineBasicBlock *Succ; |
| 133 | |
Jakob Stoklund Olesen | 0271a5f | 2012-07-27 23:58:36 +0000 | [diff] [blame] | 134 | /// The block number of the head of the trace. (When hasValidDepth()). |
| 135 | unsigned Head; |
| 136 | |
| 137 | /// The block number of the tail of the trace. (When hasValidHeight()). |
| 138 | unsigned Tail; |
| 139 | |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 140 | /// Accumulated number of instructions in the trace above this block. |
| 141 | /// Does not include instructions in this block. |
| 142 | unsigned InstrDepth; |
| 143 | |
| 144 | /// Accumulated number of instructions in the trace below this block. |
| 145 | /// Includes instructions in this block. |
| 146 | unsigned InstrHeight; |
| 147 | |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 148 | TraceBlockInfo() : |
| 149 | Pred(0), Succ(0), |
| 150 | InstrDepth(~0u), InstrHeight(~0u), |
| 151 | HasValidInstrDepths(false), HasValidInstrHeights(false) {} |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 152 | |
| 153 | /// Returns true if the depth resources have been computed from the trace |
| 154 | /// above this block. |
| 155 | bool hasValidDepth() const { return InstrDepth != ~0u; } |
| 156 | |
| 157 | /// Returns true if the height resources have been computed from the trace |
| 158 | /// below this block. |
| 159 | bool hasValidHeight() const { return InstrHeight != ~0u; } |
| 160 | |
| 161 | /// Invalidate depth resources when some block above this one has changed. |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 162 | void invalidateDepth() { InstrDepth = ~0u; HasValidInstrDepths = false; } |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 163 | |
| 164 | /// Invalidate height resources when a block below this one has changed. |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 165 | void invalidateHeight() { InstrHeight = ~0u; HasValidInstrHeights = false; } |
| 166 | |
| 167 | // Data-dependency-related information. Per-instruction depth and height |
| 168 | // are computed from data dependencies in the current trace, using |
| 169 | // itinerary data. |
| 170 | |
| 171 | /// Instruction depths have been computed. This implies hasValidDepth(). |
| 172 | bool HasValidInstrDepths; |
| 173 | |
| 174 | /// Instruction heights have been computed. This implies hasValidHeight(). |
| 175 | bool HasValidInstrHeights; |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 176 | |
Jakob Stoklund Olesen | c7f44b8 | 2012-08-01 22:36:00 +0000 | [diff] [blame^] | 177 | /// Live-in registers. These registers are defined above the current block |
| 178 | /// and used by this block or a block below it. |
| 179 | /// This does not include PHI uses in the current block, but it does |
| 180 | /// include PHI uses in deeper blocks. |
| 181 | SmallVector<LiveInReg, 4> LiveIns; |
| 182 | |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 183 | void print(raw_ostream&) const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | /// A trace represents a plausible sequence of executed basic blocks that |
| 187 | /// passes through the current basic block one. The Trace class serves as a |
| 188 | /// handle to internal cached data structures. |
| 189 | class Trace { |
| 190 | Ensemble &TE; |
| 191 | TraceBlockInfo &TBI; |
| 192 | |
| 193 | public: |
| 194 | explicit Trace(Ensemble &te, TraceBlockInfo &tbi) : TE(te), TBI(tbi) {} |
| 195 | void print(raw_ostream&) const; |
| 196 | |
| 197 | /// Compute the total number of instructions in the trace. |
| 198 | unsigned getInstrCount() const { |
| 199 | return TBI.InstrDepth + TBI.InstrHeight; |
| 200 | } |
| 201 | }; |
| 202 | |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 203 | /// InstrCycles represents the cycle height and depth of an instruction in a |
| 204 | /// trace. |
| 205 | struct InstrCycles { |
| 206 | /// Earliest issue cycle as determined by data dependencies and instruction |
| 207 | /// latencies from the beginning of the trace. Data dependencies from |
| 208 | /// before the trace are not included. |
| 209 | unsigned Depth; |
| 210 | |
| 211 | /// Minimum number of cycles from this instruction is issued to the of the |
| 212 | /// trace, as determined by data dependencies and instruction latencies. |
| 213 | unsigned Height; |
| 214 | }; |
| 215 | |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 216 | /// A trace ensemble is a collection of traces selected using the same |
| 217 | /// strategy, for example 'minimum resource height'. There is one trace for |
| 218 | /// every block in the function. |
| 219 | class Ensemble { |
| 220 | SmallVector<TraceBlockInfo, 4> BlockInfo; |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 221 | DenseMap<const MachineInstr*, InstrCycles> Cycles; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 222 | friend class Trace; |
| 223 | |
| 224 | void computeTrace(const MachineBasicBlock*); |
| 225 | void computeDepthResources(const MachineBasicBlock*); |
| 226 | void computeHeightResources(const MachineBasicBlock*); |
Jakob Stoklund Olesen | 5f8e8bd | 2012-07-31 20:44:38 +0000 | [diff] [blame] | 227 | void computeInstrDepths(const MachineBasicBlock*); |
| 228 | void computeInstrHeights(const MachineBasicBlock*); |
Jakob Stoklund Olesen | c7f44b8 | 2012-08-01 22:36:00 +0000 | [diff] [blame^] | 229 | void addLiveIns(const MachineInstr *DefMI, |
| 230 | ArrayRef<const MachineBasicBlock*> Trace); |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 231 | |
| 232 | protected: |
Jakob Stoklund Olesen | 64e2973 | 2012-07-31 20:25:13 +0000 | [diff] [blame] | 233 | MachineTraceMetrics &MTM; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 234 | virtual const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) =0; |
| 235 | virtual const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) =0; |
| 236 | explicit Ensemble(MachineTraceMetrics*); |
Jakob Stoklund Olesen | a1b2bf7 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 237 | const MachineLoop *getLoopFor(const MachineBasicBlock*) const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 238 | const TraceBlockInfo *getDepthResources(const MachineBasicBlock*) const; |
| 239 | const TraceBlockInfo *getHeightResources(const MachineBasicBlock*) const; |
| 240 | |
| 241 | public: |
| 242 | virtual ~Ensemble(); |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 243 | virtual const char *getName() const =0; |
| 244 | void print(raw_ostream&) const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 245 | void invalidate(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | a1b2bf7 | 2012-07-30 18:34:11 +0000 | [diff] [blame] | 246 | void verify() const; |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 247 | |
| 248 | /// Get the trace that passes through MBB. |
| 249 | /// The trace is computed on demand. |
| 250 | Trace getTrace(const MachineBasicBlock *MBB); |
| 251 | }; |
| 252 | |
| 253 | /// Strategies for selecting traces. |
| 254 | enum Strategy { |
| 255 | /// Select the trace through a block that has the fewest instructions. |
| 256 | TS_MinInstrCount, |
| 257 | |
| 258 | TS_NumStrategies |
| 259 | }; |
| 260 | |
| 261 | /// Get the trace ensemble representing the given trace selection strategy. |
| 262 | /// The returned Ensemble object is owned by the MachineTraceMetrics analysis, |
| 263 | /// and valid for the lifetime of the analysis pass. |
| 264 | Ensemble *getEnsemble(Strategy); |
| 265 | |
| 266 | /// Invalidate cached information about MBB. This must be called *before* MBB |
| 267 | /// is erased, or the CFG is otherwise changed. |
Jakob Stoklund Olesen | 20f13c5 | 2012-07-30 21:16:22 +0000 | [diff] [blame] | 268 | /// |
| 269 | /// This invalidates per-block information about resource usage for MBB only, |
| 270 | /// and it invalidates per-trace information for any trace that passes |
| 271 | /// through MBB. |
| 272 | /// |
| 273 | /// Call Ensemble::getTrace() again to update any trace handles. |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 274 | void invalidate(const MachineBasicBlock *MBB); |
| 275 | |
| 276 | private: |
| 277 | // One entry per basic block, indexed by block number. |
| 278 | SmallVector<FixedBlockInfo, 4> BlockInfo; |
| 279 | |
| 280 | // One ensemble per strategy. |
| 281 | Ensemble* Ensembles[TS_NumStrategies]; |
| 282 | }; |
| 283 | |
| 284 | inline raw_ostream &operator<<(raw_ostream &OS, |
| 285 | const MachineTraceMetrics::Trace &Tr) { |
| 286 | Tr.print(OS); |
| 287 | return OS; |
| 288 | } |
| 289 | |
Jakob Stoklund Olesen | 08f6ef6 | 2012-07-27 23:58:38 +0000 | [diff] [blame] | 290 | inline raw_ostream &operator<<(raw_ostream &OS, |
| 291 | const MachineTraceMetrics::Ensemble &En) { |
| 292 | En.print(OS); |
| 293 | return OS; |
| 294 | } |
Jakob Stoklund Olesen | 9f63e10 | 2012-07-26 18:38:11 +0000 | [diff] [blame] | 295 | } // end namespace llvm |
| 296 | |
| 297 | #endif |