blob: 1eb98c196825bc8724f8f4844e8123d3d5c1ea95 [file] [log] [blame]
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +00001//===- 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 Olesen5f8e8bd2012-07-31 20:44:38 +000050#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000051#include "llvm/ADT/SmallVector.h"
52#include "llvm/CodeGen/MachineFunctionPass.h"
53
54namespace llvm {
55
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +000056class InstrItineraryData;
57class MachineBasicBlock;
58class MachineInstr;
59class MachineLoop;
60class MachineLoopInfo;
61class MachineRegisterInfo;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000062class TargetInstrInfo;
63class TargetRegisterInfo;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000064class raw_ostream;
65
66class MachineTraceMetrics : public MachineFunctionPass {
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +000067 const MachineFunction *MF;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000068 const TargetInstrInfo *TII;
69 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +000070 const InstrItineraryData *ItinData;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000071 const MachineRegisterInfo *MRI;
72 const MachineLoopInfo *Loops;
73
74public:
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 Olesenef6c76c2012-07-30 20:57:50 +000082 void verifyAnalysis() const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000083
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
109 /// Per-basic block information that relates to a specific trace through the
110 /// block. Convergent traces means that only one of these is required per
111 /// block in a trace ensemble.
112 struct TraceBlockInfo {
113 /// Trace predecessor, or NULL for the first block in the trace.
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000114 /// Valid when hasValidDepth().
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000115 const MachineBasicBlock *Pred;
116
117 /// Trace successor, or NULL for the last block in the trace.
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000118 /// Valid when hasValidHeight().
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000119 const MachineBasicBlock *Succ;
120
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000121 /// The block number of the head of the trace. (When hasValidDepth()).
122 unsigned Head;
123
124 /// The block number of the tail of the trace. (When hasValidHeight()).
125 unsigned Tail;
126
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000127 /// Accumulated number of instructions in the trace above this block.
128 /// Does not include instructions in this block.
129 unsigned InstrDepth;
130
131 /// Accumulated number of instructions in the trace below this block.
132 /// Includes instructions in this block.
133 unsigned InstrHeight;
134
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000135 TraceBlockInfo() :
136 Pred(0), Succ(0),
137 InstrDepth(~0u), InstrHeight(~0u),
138 HasValidInstrDepths(false), HasValidInstrHeights(false) {}
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000139
140 /// Returns true if the depth resources have been computed from the trace
141 /// above this block.
142 bool hasValidDepth() const { return InstrDepth != ~0u; }
143
144 /// Returns true if the height resources have been computed from the trace
145 /// below this block.
146 bool hasValidHeight() const { return InstrHeight != ~0u; }
147
148 /// Invalidate depth resources when some block above this one has changed.
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000149 void invalidateDepth() { InstrDepth = ~0u; HasValidInstrDepths = false; }
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000150
151 /// Invalidate height resources when a block below this one has changed.
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000152 void invalidateHeight() { InstrHeight = ~0u; HasValidInstrHeights = false; }
153
154 // Data-dependency-related information. Per-instruction depth and height
155 // are computed from data dependencies in the current trace, using
156 // itinerary data.
157
158 /// Instruction depths have been computed. This implies hasValidDepth().
159 bool HasValidInstrDepths;
160
161 /// Instruction heights have been computed. This implies hasValidHeight().
162 bool HasValidInstrHeights;
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000163
164 void print(raw_ostream&) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000165 };
166
167 /// A trace represents a plausible sequence of executed basic blocks that
168 /// passes through the current basic block one. The Trace class serves as a
169 /// handle to internal cached data structures.
170 class Trace {
171 Ensemble &TE;
172 TraceBlockInfo &TBI;
173
174 public:
175 explicit Trace(Ensemble &te, TraceBlockInfo &tbi) : TE(te), TBI(tbi) {}
176 void print(raw_ostream&) const;
177
178 /// Compute the total number of instructions in the trace.
179 unsigned getInstrCount() const {
180 return TBI.InstrDepth + TBI.InstrHeight;
181 }
182 };
183
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000184 /// InstrCycles represents the cycle height and depth of an instruction in a
185 /// trace.
186 struct InstrCycles {
187 /// Earliest issue cycle as determined by data dependencies and instruction
188 /// latencies from the beginning of the trace. Data dependencies from
189 /// before the trace are not included.
190 unsigned Depth;
191
192 /// Minimum number of cycles from this instruction is issued to the of the
193 /// trace, as determined by data dependencies and instruction latencies.
194 unsigned Height;
195 };
196
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000197 /// A trace ensemble is a collection of traces selected using the same
198 /// strategy, for example 'minimum resource height'. There is one trace for
199 /// every block in the function.
200 class Ensemble {
201 SmallVector<TraceBlockInfo, 4> BlockInfo;
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000202 DenseMap<const MachineInstr*, InstrCycles> Cycles;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000203 friend class Trace;
204
205 void computeTrace(const MachineBasicBlock*);
206 void computeDepthResources(const MachineBasicBlock*);
207 void computeHeightResources(const MachineBasicBlock*);
Jakob Stoklund Olesen5f8e8bd2012-07-31 20:44:38 +0000208 void computeInstrDepths(const MachineBasicBlock*);
209 void computeInstrHeights(const MachineBasicBlock*);
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000210
211 protected:
Jakob Stoklund Olesen64e29732012-07-31 20:25:13 +0000212 MachineTraceMetrics &MTM;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000213 virtual const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) =0;
214 virtual const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) =0;
215 explicit Ensemble(MachineTraceMetrics*);
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000216 const MachineLoop *getLoopFor(const MachineBasicBlock*) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000217 const TraceBlockInfo *getDepthResources(const MachineBasicBlock*) const;
218 const TraceBlockInfo *getHeightResources(const MachineBasicBlock*) const;
219
220 public:
221 virtual ~Ensemble();
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000222 virtual const char *getName() const =0;
223 void print(raw_ostream&) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000224 void invalidate(const MachineBasicBlock *MBB);
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000225 void verify() const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000226
227 /// Get the trace that passes through MBB.
228 /// The trace is computed on demand.
229 Trace getTrace(const MachineBasicBlock *MBB);
230 };
231
232 /// Strategies for selecting traces.
233 enum Strategy {
234 /// Select the trace through a block that has the fewest instructions.
235 TS_MinInstrCount,
236
237 TS_NumStrategies
238 };
239
240 /// Get the trace ensemble representing the given trace selection strategy.
241 /// The returned Ensemble object is owned by the MachineTraceMetrics analysis,
242 /// and valid for the lifetime of the analysis pass.
243 Ensemble *getEnsemble(Strategy);
244
245 /// Invalidate cached information about MBB. This must be called *before* MBB
246 /// is erased, or the CFG is otherwise changed.
Jakob Stoklund Olesen20f13c52012-07-30 21:16:22 +0000247 ///
248 /// This invalidates per-block information about resource usage for MBB only,
249 /// and it invalidates per-trace information for any trace that passes
250 /// through MBB.
251 ///
252 /// Call Ensemble::getTrace() again to update any trace handles.
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000253 void invalidate(const MachineBasicBlock *MBB);
254
255private:
256 // One entry per basic block, indexed by block number.
257 SmallVector<FixedBlockInfo, 4> BlockInfo;
258
259 // One ensemble per strategy.
260 Ensemble* Ensembles[TS_NumStrategies];
261};
262
263inline raw_ostream &operator<<(raw_ostream &OS,
264 const MachineTraceMetrics::Trace &Tr) {
265 Tr.print(OS);
266 return OS;
267}
268
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000269inline raw_ostream &operator<<(raw_ostream &OS,
270 const MachineTraceMetrics::Ensemble &En) {
271 En.print(OS);
272 return OS;
273}
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000274} // end namespace llvm
275
276#endif