blob: 82079c66f51a9c04f43657b6087f3230485ff7a4 [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
50#include "llvm/ADT/SmallVector.h"
51#include "llvm/CodeGen/MachineFunctionPass.h"
52
53namespace llvm {
54
55class TargetInstrInfo;
56class TargetRegisterInfo;
57class MachineBasicBlock;
58class MachineRegisterInfo;
59class MachineLoopInfo;
60class MachineLoop;
61class raw_ostream;
62
63class MachineTraceMetrics : public MachineFunctionPass {
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +000064 const MachineFunction *MF;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000065 const TargetInstrInfo *TII;
66 const TargetRegisterInfo *TRI;
67 const MachineRegisterInfo *MRI;
68 const MachineLoopInfo *Loops;
69
70public:
71 class Ensemble;
72 class Trace;
73 static char ID;
74 MachineTraceMetrics();
75 void getAnalysisUsage(AnalysisUsage&) const;
76 bool runOnMachineFunction(MachineFunction&);
77 void releaseMemory();
Jakob Stoklund Olesenef6c76c2012-07-30 20:57:50 +000078 void verifyAnalysis() const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000079
80 friend class Ensemble;
81 friend class Trace;
82
83 /// Per-basic block information that doesn't depend on the trace through the
84 /// block.
85 struct FixedBlockInfo {
86 /// The number of non-trivial instructions in the block.
87 /// Doesn't count PHI and COPY instructions that are likely to be removed.
88 unsigned InstrCount;
89
90 /// True when the block contains calls.
91 bool HasCalls;
92
93 FixedBlockInfo() : InstrCount(~0u), HasCalls(false) {}
94
95 /// Returns true when resource information for this block has been computed.
96 bool hasResources() const { return InstrCount != ~0u; }
97
98 /// Invalidate resource information.
99 void invalidate() { InstrCount = ~0u; }
100 };
101
102 /// Get the fixed resource information about MBB. Compute it on demand.
103 const FixedBlockInfo *getResources(const MachineBasicBlock*);
104
105 /// Per-basic block information that relates to a specific trace through the
106 /// block. Convergent traces means that only one of these is required per
107 /// block in a trace ensemble.
108 struct TraceBlockInfo {
109 /// Trace predecessor, or NULL for the first block in the trace.
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000110 /// Valid when hasValidDepth().
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000111 const MachineBasicBlock *Pred;
112
113 /// Trace successor, or NULL for the last block in the trace.
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000114 /// Valid when hasValidHeight().
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000115 const MachineBasicBlock *Succ;
116
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000117 /// The block number of the head of the trace. (When hasValidDepth()).
118 unsigned Head;
119
120 /// The block number of the tail of the trace. (When hasValidHeight()).
121 unsigned Tail;
122
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000123 /// Accumulated number of instructions in the trace above this block.
124 /// Does not include instructions in this block.
125 unsigned InstrDepth;
126
127 /// Accumulated number of instructions in the trace below this block.
128 /// Includes instructions in this block.
129 unsigned InstrHeight;
130
131 TraceBlockInfo() : Pred(0), Succ(0), InstrDepth(~0u), InstrHeight(~0u) {}
132
133 /// Returns true if the depth resources have been computed from the trace
134 /// above this block.
135 bool hasValidDepth() const { return InstrDepth != ~0u; }
136
137 /// Returns true if the height resources have been computed from the trace
138 /// below this block.
139 bool hasValidHeight() const { return InstrHeight != ~0u; }
140
141 /// Invalidate depth resources when some block above this one has changed.
142 void invalidateDepth() { InstrDepth = ~0u; }
143
144 /// Invalidate height resources when a block below this one has changed.
145 void invalidateHeight() { InstrHeight = ~0u; }
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000146
147 void print(raw_ostream&) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000148 };
149
150 /// A trace represents a plausible sequence of executed basic blocks that
151 /// passes through the current basic block one. The Trace class serves as a
152 /// handle to internal cached data structures.
153 class Trace {
154 Ensemble &TE;
155 TraceBlockInfo &TBI;
156
157 public:
158 explicit Trace(Ensemble &te, TraceBlockInfo &tbi) : TE(te), TBI(tbi) {}
159 void print(raw_ostream&) const;
160
161 /// Compute the total number of instructions in the trace.
162 unsigned getInstrCount() const {
163 return TBI.InstrDepth + TBI.InstrHeight;
164 }
165 };
166
167 /// A trace ensemble is a collection of traces selected using the same
168 /// strategy, for example 'minimum resource height'. There is one trace for
169 /// every block in the function.
170 class Ensemble {
171 SmallVector<TraceBlockInfo, 4> BlockInfo;
172 friend class Trace;
173
174 void computeTrace(const MachineBasicBlock*);
175 void computeDepthResources(const MachineBasicBlock*);
176 void computeHeightResources(const MachineBasicBlock*);
177
178 protected:
Jakob Stoklund Olesen64e29732012-07-31 20:25:13 +0000179 MachineTraceMetrics &MTM;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000180 virtual const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) =0;
181 virtual const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) =0;
182 explicit Ensemble(MachineTraceMetrics*);
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000183 const MachineLoop *getLoopFor(const MachineBasicBlock*) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000184 const TraceBlockInfo *getDepthResources(const MachineBasicBlock*) const;
185 const TraceBlockInfo *getHeightResources(const MachineBasicBlock*) const;
186
187 public:
188 virtual ~Ensemble();
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000189 virtual const char *getName() const =0;
190 void print(raw_ostream&) const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000191 void invalidate(const MachineBasicBlock *MBB);
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000192 void verify() const;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000193
194 /// Get the trace that passes through MBB.
195 /// The trace is computed on demand.
196 Trace getTrace(const MachineBasicBlock *MBB);
197 };
198
199 /// Strategies for selecting traces.
200 enum Strategy {
201 /// Select the trace through a block that has the fewest instructions.
202 TS_MinInstrCount,
203
204 TS_NumStrategies
205 };
206
207 /// Get the trace ensemble representing the given trace selection strategy.
208 /// The returned Ensemble object is owned by the MachineTraceMetrics analysis,
209 /// and valid for the lifetime of the analysis pass.
210 Ensemble *getEnsemble(Strategy);
211
212 /// Invalidate cached information about MBB. This must be called *before* MBB
213 /// is erased, or the CFG is otherwise changed.
Jakob Stoklund Olesen20f13c52012-07-30 21:16:22 +0000214 ///
215 /// This invalidates per-block information about resource usage for MBB only,
216 /// and it invalidates per-trace information for any trace that passes
217 /// through MBB.
218 ///
219 /// Call Ensemble::getTrace() again to update any trace handles.
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000220 void invalidate(const MachineBasicBlock *MBB);
221
222private:
223 // One entry per basic block, indexed by block number.
224 SmallVector<FixedBlockInfo, 4> BlockInfo;
225
226 // One ensemble per strategy.
227 Ensemble* Ensembles[TS_NumStrategies];
228};
229
230inline raw_ostream &operator<<(raw_ostream &OS,
231 const MachineTraceMetrics::Trace &Tr) {
232 Tr.print(OS);
233 return OS;
234}
235
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000236inline raw_ostream &operator<<(raw_ostream &OS,
237 const MachineTraceMetrics::Ensemble &En) {
238 En.print(OS);
239 return OS;
240}
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000241} // end namespace llvm
242
243#endif