1*9880d681SAndroid Build Coastguard Worker //===- lib/CodeGen/MachineTraceMetrics.cpp ----------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineTraceMetrics.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/PostOrderIterator.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SparseSet.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSubtargetInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "machine-trace-metrics"
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker char MachineTraceMetrics::ID = 0;
31*9880d681SAndroid Build Coastguard Worker char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MachineTraceMetrics,
34*9880d681SAndroid Build Coastguard Worker "machine-trace-metrics", "Machine Trace Metrics", false, true)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)35*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
36*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
37*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MachineTraceMetrics,
38*9880d681SAndroid Build Coastguard Worker "machine-trace-metrics", "Machine Trace Metrics", false, true)
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::MachineTraceMetrics()
41*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), MF(nullptr), TII(nullptr), TRI(nullptr),
42*9880d681SAndroid Build Coastguard Worker MRI(nullptr), Loops(nullptr) {
43*9880d681SAndroid Build Coastguard Worker std::fill(std::begin(Ensembles), std::end(Ensembles), nullptr);
44*9880d681SAndroid Build Coastguard Worker }
45*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const46*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
47*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
48*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineBranchProbabilityInfo>();
49*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineLoopInfo>();
50*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & Func)53*9880d681SAndroid Build Coastguard Worker bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
54*9880d681SAndroid Build Coastguard Worker MF = &Func;
55*9880d681SAndroid Build Coastguard Worker const TargetSubtargetInfo &ST = MF->getSubtarget();
56*9880d681SAndroid Build Coastguard Worker TII = ST.getInstrInfo();
57*9880d681SAndroid Build Coastguard Worker TRI = ST.getRegisterInfo();
58*9880d681SAndroid Build Coastguard Worker MRI = &MF->getRegInfo();
59*9880d681SAndroid Build Coastguard Worker Loops = &getAnalysis<MachineLoopInfo>();
60*9880d681SAndroid Build Coastguard Worker SchedModel.init(ST.getSchedModel(), &ST, TII);
61*9880d681SAndroid Build Coastguard Worker BlockInfo.resize(MF->getNumBlockIDs());
62*9880d681SAndroid Build Coastguard Worker ProcResourceCycles.resize(MF->getNumBlockIDs() *
63*9880d681SAndroid Build Coastguard Worker SchedModel.getNumProcResourceKinds());
64*9880d681SAndroid Build Coastguard Worker return false;
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
releaseMemory()67*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::releaseMemory() {
68*9880d681SAndroid Build Coastguard Worker MF = nullptr;
69*9880d681SAndroid Build Coastguard Worker BlockInfo.clear();
70*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != TS_NumStrategies; ++i) {
71*9880d681SAndroid Build Coastguard Worker delete Ensembles[i];
72*9880d681SAndroid Build Coastguard Worker Ensembles[i] = nullptr;
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker }
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
77*9880d681SAndroid Build Coastguard Worker // Fixed block information
78*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
79*9880d681SAndroid Build Coastguard Worker //
80*9880d681SAndroid Build Coastguard Worker // The number of instructions in a basic block and the CPU resources used by
81*9880d681SAndroid Build Coastguard Worker // those instructions don't depend on any given trace strategy.
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker /// Compute the resource usage in basic block MBB.
84*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::FixedBlockInfo*
getResources(const MachineBasicBlock * MBB)85*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
86*9880d681SAndroid Build Coastguard Worker assert(MBB && "No basic block");
87*9880d681SAndroid Build Coastguard Worker FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()];
88*9880d681SAndroid Build Coastguard Worker if (FBI->hasResources())
89*9880d681SAndroid Build Coastguard Worker return FBI;
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker // Compute resource usage in the block.
92*9880d681SAndroid Build Coastguard Worker FBI->HasCalls = false;
93*9880d681SAndroid Build Coastguard Worker unsigned InstrCount = 0;
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker // Add up per-processor resource cycles as well.
96*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = SchedModel.getNumProcResourceKinds();
97*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 32> PRCycles(PRKinds);
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard Worker for (const auto &MI : *MBB) {
100*9880d681SAndroid Build Coastguard Worker if (MI.isTransient())
101*9880d681SAndroid Build Coastguard Worker continue;
102*9880d681SAndroid Build Coastguard Worker ++InstrCount;
103*9880d681SAndroid Build Coastguard Worker if (MI.isCall())
104*9880d681SAndroid Build Coastguard Worker FBI->HasCalls = true;
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker // Count processor resources used.
107*9880d681SAndroid Build Coastguard Worker if (!SchedModel.hasInstrSchedModel())
108*9880d681SAndroid Build Coastguard Worker continue;
109*9880d681SAndroid Build Coastguard Worker const MCSchedClassDesc *SC = SchedModel.resolveSchedClass(&MI);
110*9880d681SAndroid Build Coastguard Worker if (!SC->isValid())
111*9880d681SAndroid Build Coastguard Worker continue;
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker for (TargetSchedModel::ProcResIter
114*9880d681SAndroid Build Coastguard Worker PI = SchedModel.getWriteProcResBegin(SC),
115*9880d681SAndroid Build Coastguard Worker PE = SchedModel.getWriteProcResEnd(SC); PI != PE; ++PI) {
116*9880d681SAndroid Build Coastguard Worker assert(PI->ProcResourceIdx < PRKinds && "Bad processor resource kind");
117*9880d681SAndroid Build Coastguard Worker PRCycles[PI->ProcResourceIdx] += PI->Cycles;
118*9880d681SAndroid Build Coastguard Worker }
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker FBI->InstrCount = InstrCount;
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker // Scale the resource cycles so they are comparable.
123*9880d681SAndroid Build Coastguard Worker unsigned PROffset = MBB->getNumber() * PRKinds;
124*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRKinds; ++K)
125*9880d681SAndroid Build Coastguard Worker ProcResourceCycles[PROffset + K] =
126*9880d681SAndroid Build Coastguard Worker PRCycles[K] * SchedModel.getResourceFactor(K);
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker return FBI;
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned>
getProcResourceCycles(unsigned MBBNum) const132*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::getProcResourceCycles(unsigned MBBNum) const {
133*9880d681SAndroid Build Coastguard Worker assert(BlockInfo[MBBNum].hasResources() &&
134*9880d681SAndroid Build Coastguard Worker "getResources() must be called before getProcResourceCycles()");
135*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = SchedModel.getNumProcResourceKinds();
136*9880d681SAndroid Build Coastguard Worker assert((MBBNum+1) * PRKinds <= ProcResourceCycles.size());
137*9880d681SAndroid Build Coastguard Worker return makeArrayRef(ProcResourceCycles.data() + MBBNum * PRKinds, PRKinds);
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
142*9880d681SAndroid Build Coastguard Worker // Ensemble utility functions
143*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
144*9880d681SAndroid Build Coastguard Worker
Ensemble(MachineTraceMetrics * ct)145*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
146*9880d681SAndroid Build Coastguard Worker : MTM(*ct) {
147*9880d681SAndroid Build Coastguard Worker BlockInfo.resize(MTM.BlockInfo.size());
148*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
149*9880d681SAndroid Build Coastguard Worker ProcResourceDepths.resize(MTM.BlockInfo.size() * PRKinds);
150*9880d681SAndroid Build Coastguard Worker ProcResourceHeights.resize(MTM.BlockInfo.size() * PRKinds);
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker // Virtual destructor serves as an anchor.
~Ensemble()154*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::~Ensemble() {}
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker const MachineLoop*
getLoopFor(const MachineBasicBlock * MBB) const157*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) const {
158*9880d681SAndroid Build Coastguard Worker return MTM.Loops->getLoopFor(MBB);
159*9880d681SAndroid Build Coastguard Worker }
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker // Update resource-related information in the TraceBlockInfo for MBB.
162*9880d681SAndroid Build Coastguard Worker // Only update resources related to the trace above MBB.
163*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::
computeDepthResources(const MachineBasicBlock * MBB)164*9880d681SAndroid Build Coastguard Worker computeDepthResources(const MachineBasicBlock *MBB) {
165*9880d681SAndroid Build Coastguard Worker TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
166*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
167*9880d681SAndroid Build Coastguard Worker unsigned PROffset = MBB->getNumber() * PRKinds;
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker // Compute resources from trace above. The top block is simple.
170*9880d681SAndroid Build Coastguard Worker if (!TBI->Pred) {
171*9880d681SAndroid Build Coastguard Worker TBI->InstrDepth = 0;
172*9880d681SAndroid Build Coastguard Worker TBI->Head = MBB->getNumber();
173*9880d681SAndroid Build Coastguard Worker std::fill(ProcResourceDepths.begin() + PROffset,
174*9880d681SAndroid Build Coastguard Worker ProcResourceDepths.begin() + PROffset + PRKinds, 0);
175*9880d681SAndroid Build Coastguard Worker return;
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker // Compute from the block above. A post-order traversal ensures the
179*9880d681SAndroid Build Coastguard Worker // predecessor is always computed first.
180*9880d681SAndroid Build Coastguard Worker unsigned PredNum = TBI->Pred->getNumber();
181*9880d681SAndroid Build Coastguard Worker TraceBlockInfo *PredTBI = &BlockInfo[PredNum];
182*9880d681SAndroid Build Coastguard Worker assert(PredTBI->hasValidDepth() && "Trace above has not been computed yet");
183*9880d681SAndroid Build Coastguard Worker const FixedBlockInfo *PredFBI = MTM.getResources(TBI->Pred);
184*9880d681SAndroid Build Coastguard Worker TBI->InstrDepth = PredTBI->InstrDepth + PredFBI->InstrCount;
185*9880d681SAndroid Build Coastguard Worker TBI->Head = PredTBI->Head;
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker // Compute per-resource depths.
188*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PredPRDepths = getProcResourceDepths(PredNum);
189*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PredPRCycles = MTM.getProcResourceCycles(PredNum);
190*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRKinds; ++K)
191*9880d681SAndroid Build Coastguard Worker ProcResourceDepths[PROffset + K] = PredPRDepths[K] + PredPRCycles[K];
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // Update resource-related information in the TraceBlockInfo for MBB.
195*9880d681SAndroid Build Coastguard Worker // Only update resources related to the trace below MBB.
196*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::
computeHeightResources(const MachineBasicBlock * MBB)197*9880d681SAndroid Build Coastguard Worker computeHeightResources(const MachineBasicBlock *MBB) {
198*9880d681SAndroid Build Coastguard Worker TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
199*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
200*9880d681SAndroid Build Coastguard Worker unsigned PROffset = MBB->getNumber() * PRKinds;
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker // Compute resources for the current block.
203*9880d681SAndroid Build Coastguard Worker TBI->InstrHeight = MTM.getResources(MBB)->InstrCount;
204*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRCycles = MTM.getProcResourceCycles(MBB->getNumber());
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker // The trace tail is done.
207*9880d681SAndroid Build Coastguard Worker if (!TBI->Succ) {
208*9880d681SAndroid Build Coastguard Worker TBI->Tail = MBB->getNumber();
209*9880d681SAndroid Build Coastguard Worker std::copy(PRCycles.begin(), PRCycles.end(),
210*9880d681SAndroid Build Coastguard Worker ProcResourceHeights.begin() + PROffset);
211*9880d681SAndroid Build Coastguard Worker return;
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker // Compute from the block below. A post-order traversal ensures the
215*9880d681SAndroid Build Coastguard Worker // predecessor is always computed first.
216*9880d681SAndroid Build Coastguard Worker unsigned SuccNum = TBI->Succ->getNumber();
217*9880d681SAndroid Build Coastguard Worker TraceBlockInfo *SuccTBI = &BlockInfo[SuccNum];
218*9880d681SAndroid Build Coastguard Worker assert(SuccTBI->hasValidHeight() && "Trace below has not been computed yet");
219*9880d681SAndroid Build Coastguard Worker TBI->InstrHeight += SuccTBI->InstrHeight;
220*9880d681SAndroid Build Coastguard Worker TBI->Tail = SuccTBI->Tail;
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker // Compute per-resource heights.
223*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> SuccPRHeights = getProcResourceHeights(SuccNum);
224*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRKinds; ++K)
225*9880d681SAndroid Build Coastguard Worker ProcResourceHeights[PROffset + K] = SuccPRHeights[K] + PRCycles[K];
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker // Check if depth resources for MBB are valid and return the TBI.
229*9880d681SAndroid Build Coastguard Worker // Return NULL if the resources have been invalidated.
230*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::TraceBlockInfo*
231*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::
getDepthResources(const MachineBasicBlock * MBB) const232*9880d681SAndroid Build Coastguard Worker getDepthResources(const MachineBasicBlock *MBB) const {
233*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
234*9880d681SAndroid Build Coastguard Worker return TBI->hasValidDepth() ? TBI : nullptr;
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard Worker // Check if height resources for MBB are valid and return the TBI.
238*9880d681SAndroid Build Coastguard Worker // Return NULL if the resources have been invalidated.
239*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::TraceBlockInfo*
240*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::
getHeightResources(const MachineBasicBlock * MBB) const241*9880d681SAndroid Build Coastguard Worker getHeightResources(const MachineBasicBlock *MBB) const {
242*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
243*9880d681SAndroid Build Coastguard Worker return TBI->hasValidHeight() ? TBI : nullptr;
244*9880d681SAndroid Build Coastguard Worker }
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker /// Get an array of processor resource depths for MBB. Indexed by processor
247*9880d681SAndroid Build Coastguard Worker /// resource kind, this array contains the scaled processor resources consumed
248*9880d681SAndroid Build Coastguard Worker /// by all blocks preceding MBB in its trace. It does not include instructions
249*9880d681SAndroid Build Coastguard Worker /// in MBB.
250*9880d681SAndroid Build Coastguard Worker ///
251*9880d681SAndroid Build Coastguard Worker /// Compare TraceBlockInfo::InstrDepth.
252*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned>
253*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::
getProcResourceDepths(unsigned MBBNum) const254*9880d681SAndroid Build Coastguard Worker getProcResourceDepths(unsigned MBBNum) const {
255*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
256*9880d681SAndroid Build Coastguard Worker assert((MBBNum+1) * PRKinds <= ProcResourceDepths.size());
257*9880d681SAndroid Build Coastguard Worker return makeArrayRef(ProcResourceDepths.data() + MBBNum * PRKinds, PRKinds);
258*9880d681SAndroid Build Coastguard Worker }
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker /// Get an array of processor resource heights for MBB. Indexed by processor
261*9880d681SAndroid Build Coastguard Worker /// resource kind, this array contains the scaled processor resources consumed
262*9880d681SAndroid Build Coastguard Worker /// by this block and all blocks following it in its trace.
263*9880d681SAndroid Build Coastguard Worker ///
264*9880d681SAndroid Build Coastguard Worker /// Compare TraceBlockInfo::InstrHeight.
265*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned>
266*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::
getProcResourceHeights(unsigned MBBNum) const267*9880d681SAndroid Build Coastguard Worker getProcResourceHeights(unsigned MBBNum) const {
268*9880d681SAndroid Build Coastguard Worker unsigned PRKinds = MTM.SchedModel.getNumProcResourceKinds();
269*9880d681SAndroid Build Coastguard Worker assert((MBBNum+1) * PRKinds <= ProcResourceHeights.size());
270*9880d681SAndroid Build Coastguard Worker return makeArrayRef(ProcResourceHeights.data() + MBBNum * PRKinds, PRKinds);
271*9880d681SAndroid Build Coastguard Worker }
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
274*9880d681SAndroid Build Coastguard Worker // Trace Selection Strategies
275*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
276*9880d681SAndroid Build Coastguard Worker //
277*9880d681SAndroid Build Coastguard Worker // A trace selection strategy is implemented as a sub-class of Ensemble. The
278*9880d681SAndroid Build Coastguard Worker // trace through a block B is computed by two DFS traversals of the CFG
279*9880d681SAndroid Build Coastguard Worker // starting from B. One upwards, and one downwards. During the upwards DFS,
280*9880d681SAndroid Build Coastguard Worker // pickTracePred() is called on the post-ordered blocks. During the downwards
281*9880d681SAndroid Build Coastguard Worker // DFS, pickTraceSucc() is called in a post-order.
282*9880d681SAndroid Build Coastguard Worker //
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker // We never allow traces that leave loops, but we do allow traces to enter
285*9880d681SAndroid Build Coastguard Worker // nested loops. We also never allow traces to contain back-edges.
286*9880d681SAndroid Build Coastguard Worker //
287*9880d681SAndroid Build Coastguard Worker // This means that a loop header can never appear above the center block of a
288*9880d681SAndroid Build Coastguard Worker // trace, except as the trace head. Below the center block, loop exiting edges
289*9880d681SAndroid Build Coastguard Worker // are banned.
290*9880d681SAndroid Build Coastguard Worker //
291*9880d681SAndroid Build Coastguard Worker // Return true if an edge from the From loop to the To loop is leaving a loop.
292*9880d681SAndroid Build Coastguard Worker // Either of To and From can be null.
isExitingLoop(const MachineLoop * From,const MachineLoop * To)293*9880d681SAndroid Build Coastguard Worker static bool isExitingLoop(const MachineLoop *From, const MachineLoop *To) {
294*9880d681SAndroid Build Coastguard Worker return From && !From->contains(To);
295*9880d681SAndroid Build Coastguard Worker }
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard Worker // MinInstrCountEnsemble - Pick the trace that executes the least number of
298*9880d681SAndroid Build Coastguard Worker // instructions.
299*9880d681SAndroid Build Coastguard Worker namespace {
300*9880d681SAndroid Build Coastguard Worker class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
getName() const301*9880d681SAndroid Build Coastguard Worker const char *getName() const override { return "MinInstr"; }
302*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *pickTracePred(const MachineBasicBlock*) override;
303*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*) override;
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker public:
MinInstrCountEnsemble(MachineTraceMetrics * mtm)306*9880d681SAndroid Build Coastguard Worker MinInstrCountEnsemble(MachineTraceMetrics *mtm)
307*9880d681SAndroid Build Coastguard Worker : MachineTraceMetrics::Ensemble(mtm) {}
308*9880d681SAndroid Build Coastguard Worker };
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker // Select the preferred predecessor for MBB.
312*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock*
pickTracePred(const MachineBasicBlock * MBB)313*9880d681SAndroid Build Coastguard Worker MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) {
314*9880d681SAndroid Build Coastguard Worker if (MBB->pred_empty())
315*9880d681SAndroid Build Coastguard Worker return nullptr;
316*9880d681SAndroid Build Coastguard Worker const MachineLoop *CurLoop = getLoopFor(MBB);
317*9880d681SAndroid Build Coastguard Worker // Don't leave loops, and never follow back-edges.
318*9880d681SAndroid Build Coastguard Worker if (CurLoop && MBB == CurLoop->getHeader())
319*9880d681SAndroid Build Coastguard Worker return nullptr;
320*9880d681SAndroid Build Coastguard Worker unsigned CurCount = MTM.getResources(MBB)->InstrCount;
321*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Best = nullptr;
322*9880d681SAndroid Build Coastguard Worker unsigned BestDepth = 0;
323*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *Pred : MBB->predecessors()) {
324*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::TraceBlockInfo *PredTBI =
325*9880d681SAndroid Build Coastguard Worker getDepthResources(Pred);
326*9880d681SAndroid Build Coastguard Worker // Ignore cycles that aren't natural loops.
327*9880d681SAndroid Build Coastguard Worker if (!PredTBI)
328*9880d681SAndroid Build Coastguard Worker continue;
329*9880d681SAndroid Build Coastguard Worker // Pick the predecessor that would give this block the smallest InstrDepth.
330*9880d681SAndroid Build Coastguard Worker unsigned Depth = PredTBI->InstrDepth + CurCount;
331*9880d681SAndroid Build Coastguard Worker if (!Best || Depth < BestDepth) {
332*9880d681SAndroid Build Coastguard Worker Best = Pred;
333*9880d681SAndroid Build Coastguard Worker BestDepth = Depth;
334*9880d681SAndroid Build Coastguard Worker }
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker return Best;
337*9880d681SAndroid Build Coastguard Worker }
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard Worker // Select the preferred successor for MBB.
340*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock*
pickTraceSucc(const MachineBasicBlock * MBB)341*9880d681SAndroid Build Coastguard Worker MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
342*9880d681SAndroid Build Coastguard Worker if (MBB->pred_empty())
343*9880d681SAndroid Build Coastguard Worker return nullptr;
344*9880d681SAndroid Build Coastguard Worker const MachineLoop *CurLoop = getLoopFor(MBB);
345*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Best = nullptr;
346*9880d681SAndroid Build Coastguard Worker unsigned BestHeight = 0;
347*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *Succ : MBB->successors()) {
348*9880d681SAndroid Build Coastguard Worker // Don't consider back-edges.
349*9880d681SAndroid Build Coastguard Worker if (CurLoop && Succ == CurLoop->getHeader())
350*9880d681SAndroid Build Coastguard Worker continue;
351*9880d681SAndroid Build Coastguard Worker // Don't consider successors exiting CurLoop.
352*9880d681SAndroid Build Coastguard Worker if (isExitingLoop(CurLoop, getLoopFor(Succ)))
353*9880d681SAndroid Build Coastguard Worker continue;
354*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::TraceBlockInfo *SuccTBI =
355*9880d681SAndroid Build Coastguard Worker getHeightResources(Succ);
356*9880d681SAndroid Build Coastguard Worker // Ignore cycles that aren't natural loops.
357*9880d681SAndroid Build Coastguard Worker if (!SuccTBI)
358*9880d681SAndroid Build Coastguard Worker continue;
359*9880d681SAndroid Build Coastguard Worker // Pick the successor that would give this block the smallest InstrHeight.
360*9880d681SAndroid Build Coastguard Worker unsigned Height = SuccTBI->InstrHeight;
361*9880d681SAndroid Build Coastguard Worker if (!Best || Height < BestHeight) {
362*9880d681SAndroid Build Coastguard Worker Best = Succ;
363*9880d681SAndroid Build Coastguard Worker BestHeight = Height;
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker return Best;
367*9880d681SAndroid Build Coastguard Worker }
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker // Get an Ensemble sub-class for the requested trace strategy.
370*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble *
getEnsemble(MachineTraceMetrics::Strategy strategy)371*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) {
372*9880d681SAndroid Build Coastguard Worker assert(strategy < TS_NumStrategies && "Invalid trace strategy enum");
373*9880d681SAndroid Build Coastguard Worker Ensemble *&E = Ensembles[strategy];
374*9880d681SAndroid Build Coastguard Worker if (E)
375*9880d681SAndroid Build Coastguard Worker return E;
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Worker // Allocate new Ensemble on demand.
378*9880d681SAndroid Build Coastguard Worker switch (strategy) {
379*9880d681SAndroid Build Coastguard Worker case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this));
380*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Invalid trace strategy enum");
381*9880d681SAndroid Build Coastguard Worker }
382*9880d681SAndroid Build Coastguard Worker }
383*9880d681SAndroid Build Coastguard Worker
invalidate(const MachineBasicBlock * MBB)384*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
385*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Invalidate traces through BB#" << MBB->getNumber() << '\n');
386*9880d681SAndroid Build Coastguard Worker BlockInfo[MBB->getNumber()].invalidate();
387*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != TS_NumStrategies; ++i)
388*9880d681SAndroid Build Coastguard Worker if (Ensembles[i])
389*9880d681SAndroid Build Coastguard Worker Ensembles[i]->invalidate(MBB);
390*9880d681SAndroid Build Coastguard Worker }
391*9880d681SAndroid Build Coastguard Worker
verifyAnalysis() const392*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::verifyAnalysis() const {
393*9880d681SAndroid Build Coastguard Worker if (!MF)
394*9880d681SAndroid Build Coastguard Worker return;
395*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
396*9880d681SAndroid Build Coastguard Worker assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
397*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != TS_NumStrategies; ++i)
398*9880d681SAndroid Build Coastguard Worker if (Ensembles[i])
399*9880d681SAndroid Build Coastguard Worker Ensembles[i]->verify();
400*9880d681SAndroid Build Coastguard Worker #endif
401*9880d681SAndroid Build Coastguard Worker }
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
404*9880d681SAndroid Build Coastguard Worker // Trace building
405*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
406*9880d681SAndroid Build Coastguard Worker //
407*9880d681SAndroid Build Coastguard Worker // Traces are built by two CFG traversals. To avoid recomputing too much, use a
408*9880d681SAndroid Build Coastguard Worker // set abstraction that confines the search to the current loop, and doesn't
409*9880d681SAndroid Build Coastguard Worker // revisit blocks.
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker namespace {
412*9880d681SAndroid Build Coastguard Worker struct LoopBounds {
413*9880d681SAndroid Build Coastguard Worker MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks;
414*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> Visited;
415*9880d681SAndroid Build Coastguard Worker const MachineLoopInfo *Loops;
416*9880d681SAndroid Build Coastguard Worker bool Downward;
LoopBounds__anonf071fd070211::LoopBounds417*9880d681SAndroid Build Coastguard Worker LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
418*9880d681SAndroid Build Coastguard Worker const MachineLoopInfo *loops)
419*9880d681SAndroid Build Coastguard Worker : Blocks(blocks), Loops(loops), Downward(false) {}
420*9880d681SAndroid Build Coastguard Worker };
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard Worker // Specialize po_iterator_storage in order to prune the post-order traversal so
424*9880d681SAndroid Build Coastguard Worker // it is limited to the current loop and doesn't traverse the loop back edges.
425*9880d681SAndroid Build Coastguard Worker namespace llvm {
426*9880d681SAndroid Build Coastguard Worker template<>
427*9880d681SAndroid Build Coastguard Worker class po_iterator_storage<LoopBounds, true> {
428*9880d681SAndroid Build Coastguard Worker LoopBounds &LB;
429*9880d681SAndroid Build Coastguard Worker public:
po_iterator_storage(LoopBounds & lb)430*9880d681SAndroid Build Coastguard Worker po_iterator_storage(LoopBounds &lb) : LB(lb) {}
finishPostorder(const MachineBasicBlock *)431*9880d681SAndroid Build Coastguard Worker void finishPostorder(const MachineBasicBlock*) {}
432*9880d681SAndroid Build Coastguard Worker
insertEdge(const MachineBasicBlock * From,const MachineBasicBlock * To)433*9880d681SAndroid Build Coastguard Worker bool insertEdge(const MachineBasicBlock *From, const MachineBasicBlock *To) {
434*9880d681SAndroid Build Coastguard Worker // Skip already visited To blocks.
435*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()];
436*9880d681SAndroid Build Coastguard Worker if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth())
437*9880d681SAndroid Build Coastguard Worker return false;
438*9880d681SAndroid Build Coastguard Worker // From is null once when To is the trace center block.
439*9880d681SAndroid Build Coastguard Worker if (From) {
440*9880d681SAndroid Build Coastguard Worker if (const MachineLoop *FromLoop = LB.Loops->getLoopFor(From)) {
441*9880d681SAndroid Build Coastguard Worker // Don't follow backedges, don't leave FromLoop when going upwards.
442*9880d681SAndroid Build Coastguard Worker if ((LB.Downward ? To : From) == FromLoop->getHeader())
443*9880d681SAndroid Build Coastguard Worker return false;
444*9880d681SAndroid Build Coastguard Worker // Don't leave FromLoop.
445*9880d681SAndroid Build Coastguard Worker if (isExitingLoop(FromLoop, LB.Loops->getLoopFor(To)))
446*9880d681SAndroid Build Coastguard Worker return false;
447*9880d681SAndroid Build Coastguard Worker }
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker // To is a new block. Mark the block as visited in case the CFG has cycles
450*9880d681SAndroid Build Coastguard Worker // that MachineLoopInfo didn't recognize as a natural loop.
451*9880d681SAndroid Build Coastguard Worker return LB.Visited.insert(To).second;
452*9880d681SAndroid Build Coastguard Worker }
453*9880d681SAndroid Build Coastguard Worker };
454*9880d681SAndroid Build Coastguard Worker }
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker /// Compute the trace through MBB.
computeTrace(const MachineBasicBlock * MBB)457*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
458*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Computing " << getName() << " trace through BB#"
459*9880d681SAndroid Build Coastguard Worker << MBB->getNumber() << '\n');
460*9880d681SAndroid Build Coastguard Worker // Set up loop bounds for the backwards post-order traversal.
461*9880d681SAndroid Build Coastguard Worker LoopBounds Bounds(BlockInfo, MTM.Loops);
462*9880d681SAndroid Build Coastguard Worker
463*9880d681SAndroid Build Coastguard Worker // Run an upwards post-order search for the trace start.
464*9880d681SAndroid Build Coastguard Worker Bounds.Downward = false;
465*9880d681SAndroid Build Coastguard Worker Bounds.Visited.clear();
466*9880d681SAndroid Build Coastguard Worker for (auto I : inverse_post_order_ext(MBB, Bounds)) {
467*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": ");
468*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
469*9880d681SAndroid Build Coastguard Worker // All the predecessors have been visited, pick the preferred one.
470*9880d681SAndroid Build Coastguard Worker TBI.Pred = pickTracePred(I);
471*9880d681SAndroid Build Coastguard Worker DEBUG({
472*9880d681SAndroid Build Coastguard Worker if (TBI.Pred)
473*9880d681SAndroid Build Coastguard Worker dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
474*9880d681SAndroid Build Coastguard Worker else
475*9880d681SAndroid Build Coastguard Worker dbgs() << "null\n";
476*9880d681SAndroid Build Coastguard Worker });
477*9880d681SAndroid Build Coastguard Worker // The trace leading to I is now known, compute the depth resources.
478*9880d681SAndroid Build Coastguard Worker computeDepthResources(I);
479*9880d681SAndroid Build Coastguard Worker }
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker // Run a downwards post-order search for the trace end.
482*9880d681SAndroid Build Coastguard Worker Bounds.Downward = true;
483*9880d681SAndroid Build Coastguard Worker Bounds.Visited.clear();
484*9880d681SAndroid Build Coastguard Worker for (auto I : post_order_ext(MBB, Bounds)) {
485*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": ");
486*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
487*9880d681SAndroid Build Coastguard Worker // All the successors have been visited, pick the preferred one.
488*9880d681SAndroid Build Coastguard Worker TBI.Succ = pickTraceSucc(I);
489*9880d681SAndroid Build Coastguard Worker DEBUG({
490*9880d681SAndroid Build Coastguard Worker if (TBI.Succ)
491*9880d681SAndroid Build Coastguard Worker dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
492*9880d681SAndroid Build Coastguard Worker else
493*9880d681SAndroid Build Coastguard Worker dbgs() << "null\n";
494*9880d681SAndroid Build Coastguard Worker });
495*9880d681SAndroid Build Coastguard Worker // The trace leaving I is now known, compute the height resources.
496*9880d681SAndroid Build Coastguard Worker computeHeightResources(I);
497*9880d681SAndroid Build Coastguard Worker }
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker
500*9880d681SAndroid Build Coastguard Worker /// Invalidate traces through BadMBB.
501*9880d681SAndroid Build Coastguard Worker void
invalidate(const MachineBasicBlock * BadMBB)502*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
503*9880d681SAndroid Build Coastguard Worker SmallVector<const MachineBasicBlock*, 16> WorkList;
504*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()];
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard Worker // Invalidate height resources of blocks above MBB.
507*9880d681SAndroid Build Coastguard Worker if (BadTBI.hasValidHeight()) {
508*9880d681SAndroid Build Coastguard Worker BadTBI.invalidateHeight();
509*9880d681SAndroid Build Coastguard Worker WorkList.push_back(BadMBB);
510*9880d681SAndroid Build Coastguard Worker do {
511*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = WorkList.pop_back_val();
512*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
513*9880d681SAndroid Build Coastguard Worker << " height.\n");
514*9880d681SAndroid Build Coastguard Worker // Find any MBB predecessors that have MBB as their preferred successor.
515*9880d681SAndroid Build Coastguard Worker // They are the only ones that need to be invalidated.
516*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *Pred : MBB->predecessors()) {
517*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[Pred->getNumber()];
518*9880d681SAndroid Build Coastguard Worker if (!TBI.hasValidHeight())
519*9880d681SAndroid Build Coastguard Worker continue;
520*9880d681SAndroid Build Coastguard Worker if (TBI.Succ == MBB) {
521*9880d681SAndroid Build Coastguard Worker TBI.invalidateHeight();
522*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Pred);
523*9880d681SAndroid Build Coastguard Worker continue;
524*9880d681SAndroid Build Coastguard Worker }
525*9880d681SAndroid Build Coastguard Worker // Verify that TBI.Succ is actually a *I successor.
526*9880d681SAndroid Build Coastguard Worker assert((!TBI.Succ || Pred->isSuccessor(TBI.Succ)) && "CFG changed");
527*9880d681SAndroid Build Coastguard Worker }
528*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
529*9880d681SAndroid Build Coastguard Worker }
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker // Invalidate depth resources of blocks below MBB.
532*9880d681SAndroid Build Coastguard Worker if (BadTBI.hasValidDepth()) {
533*9880d681SAndroid Build Coastguard Worker BadTBI.invalidateDepth();
534*9880d681SAndroid Build Coastguard Worker WorkList.push_back(BadMBB);
535*9880d681SAndroid Build Coastguard Worker do {
536*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = WorkList.pop_back_val();
537*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
538*9880d681SAndroid Build Coastguard Worker << " depth.\n");
539*9880d681SAndroid Build Coastguard Worker // Find any MBB successors that have MBB as their preferred predecessor.
540*9880d681SAndroid Build Coastguard Worker // They are the only ones that need to be invalidated.
541*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *Succ : MBB->successors()) {
542*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[Succ->getNumber()];
543*9880d681SAndroid Build Coastguard Worker if (!TBI.hasValidDepth())
544*9880d681SAndroid Build Coastguard Worker continue;
545*9880d681SAndroid Build Coastguard Worker if (TBI.Pred == MBB) {
546*9880d681SAndroid Build Coastguard Worker TBI.invalidateDepth();
547*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Succ);
548*9880d681SAndroid Build Coastguard Worker continue;
549*9880d681SAndroid Build Coastguard Worker }
550*9880d681SAndroid Build Coastguard Worker // Verify that TBI.Pred is actually a *I predecessor.
551*9880d681SAndroid Build Coastguard Worker assert((!TBI.Pred || Succ->isPredecessor(TBI.Pred)) && "CFG changed");
552*9880d681SAndroid Build Coastguard Worker }
553*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
554*9880d681SAndroid Build Coastguard Worker }
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker // Clear any per-instruction data. We only have to do this for BadMBB itself
557*9880d681SAndroid Build Coastguard Worker // because the instructions in that block may change. Other blocks may be
558*9880d681SAndroid Build Coastguard Worker // invalidated, but their instructions will stay the same, so there is no
559*9880d681SAndroid Build Coastguard Worker // need to erase the Cycle entries. They will be overwritten when we
560*9880d681SAndroid Build Coastguard Worker // recompute.
561*9880d681SAndroid Build Coastguard Worker for (const auto &I : *BadMBB)
562*9880d681SAndroid Build Coastguard Worker Cycles.erase(&I);
563*9880d681SAndroid Build Coastguard Worker }
564*9880d681SAndroid Build Coastguard Worker
verify() const565*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::verify() const {
566*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
567*9880d681SAndroid Build Coastguard Worker assert(BlockInfo.size() == MTM.MF->getNumBlockIDs() &&
568*9880d681SAndroid Build Coastguard Worker "Outdated BlockInfo size");
569*9880d681SAndroid Build Coastguard Worker for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
570*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo &TBI = BlockInfo[Num];
571*9880d681SAndroid Build Coastguard Worker if (TBI.hasValidDepth() && TBI.Pred) {
572*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
573*9880d681SAndroid Build Coastguard Worker assert(MBB->isPredecessor(TBI.Pred) && "CFG doesn't match trace");
574*9880d681SAndroid Build Coastguard Worker assert(BlockInfo[TBI.Pred->getNumber()].hasValidDepth() &&
575*9880d681SAndroid Build Coastguard Worker "Trace is broken, depth should have been invalidated.");
576*9880d681SAndroid Build Coastguard Worker const MachineLoop *Loop = getLoopFor(MBB);
577*9880d681SAndroid Build Coastguard Worker assert(!(Loop && MBB == Loop->getHeader()) && "Trace contains backedge");
578*9880d681SAndroid Build Coastguard Worker }
579*9880d681SAndroid Build Coastguard Worker if (TBI.hasValidHeight() && TBI.Succ) {
580*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = MTM.MF->getBlockNumbered(Num);
581*9880d681SAndroid Build Coastguard Worker assert(MBB->isSuccessor(TBI.Succ) && "CFG doesn't match trace");
582*9880d681SAndroid Build Coastguard Worker assert(BlockInfo[TBI.Succ->getNumber()].hasValidHeight() &&
583*9880d681SAndroid Build Coastguard Worker "Trace is broken, height should have been invalidated.");
584*9880d681SAndroid Build Coastguard Worker const MachineLoop *Loop = getLoopFor(MBB);
585*9880d681SAndroid Build Coastguard Worker const MachineLoop *SuccLoop = getLoopFor(TBI.Succ);
586*9880d681SAndroid Build Coastguard Worker assert(!(Loop && Loop == SuccLoop && TBI.Succ == Loop->getHeader()) &&
587*9880d681SAndroid Build Coastguard Worker "Trace contains backedge");
588*9880d681SAndroid Build Coastguard Worker }
589*9880d681SAndroid Build Coastguard Worker }
590*9880d681SAndroid Build Coastguard Worker #endif
591*9880d681SAndroid Build Coastguard Worker }
592*9880d681SAndroid Build Coastguard Worker
593*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
594*9880d681SAndroid Build Coastguard Worker // Data Dependencies
595*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
596*9880d681SAndroid Build Coastguard Worker //
597*9880d681SAndroid Build Coastguard Worker // Compute the depth and height of each instruction based on data dependencies
598*9880d681SAndroid Build Coastguard Worker // and instruction latencies. These cycle numbers assume that the CPU can issue
599*9880d681SAndroid Build Coastguard Worker // an infinite number of instructions per cycle as long as their dependencies
600*9880d681SAndroid Build Coastguard Worker // are ready.
601*9880d681SAndroid Build Coastguard Worker
602*9880d681SAndroid Build Coastguard Worker // A data dependency is represented as a defining MI and operand numbers on the
603*9880d681SAndroid Build Coastguard Worker // defining and using MI.
604*9880d681SAndroid Build Coastguard Worker namespace {
605*9880d681SAndroid Build Coastguard Worker struct DataDep {
606*9880d681SAndroid Build Coastguard Worker const MachineInstr *DefMI;
607*9880d681SAndroid Build Coastguard Worker unsigned DefOp;
608*9880d681SAndroid Build Coastguard Worker unsigned UseOp;
609*9880d681SAndroid Build Coastguard Worker
DataDep__anonf071fd070311::DataDep610*9880d681SAndroid Build Coastguard Worker DataDep(const MachineInstr *DefMI, unsigned DefOp, unsigned UseOp)
611*9880d681SAndroid Build Coastguard Worker : DefMI(DefMI), DefOp(DefOp), UseOp(UseOp) {}
612*9880d681SAndroid Build Coastguard Worker
613*9880d681SAndroid Build Coastguard Worker /// Create a DataDep from an SSA form virtual register.
DataDep__anonf071fd070311::DataDep614*9880d681SAndroid Build Coastguard Worker DataDep(const MachineRegisterInfo *MRI, unsigned VirtReg, unsigned UseOp)
615*9880d681SAndroid Build Coastguard Worker : UseOp(UseOp) {
616*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(VirtReg));
617*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo::def_iterator DefI = MRI->def_begin(VirtReg);
618*9880d681SAndroid Build Coastguard Worker assert(!DefI.atEnd() && "Register has no defs");
619*9880d681SAndroid Build Coastguard Worker DefMI = DefI->getParent();
620*9880d681SAndroid Build Coastguard Worker DefOp = DefI.getOperandNo();
621*9880d681SAndroid Build Coastguard Worker assert((++DefI).atEnd() && "Register has multiple defs");
622*9880d681SAndroid Build Coastguard Worker }
623*9880d681SAndroid Build Coastguard Worker };
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard Worker // Get the input data dependencies that must be ready before UseMI can issue.
627*9880d681SAndroid Build Coastguard Worker // Return true if UseMI has any physreg operands.
getDataDeps(const MachineInstr & UseMI,SmallVectorImpl<DataDep> & Deps,const MachineRegisterInfo * MRI)628*9880d681SAndroid Build Coastguard Worker static bool getDataDeps(const MachineInstr &UseMI,
629*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<DataDep> &Deps,
630*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI) {
631*9880d681SAndroid Build Coastguard Worker // Debug values should not be included in any calculations.
632*9880d681SAndroid Build Coastguard Worker if (UseMI.isDebugValue())
633*9880d681SAndroid Build Coastguard Worker return false;
634*9880d681SAndroid Build Coastguard Worker
635*9880d681SAndroid Build Coastguard Worker bool HasPhysRegs = false;
636*9880d681SAndroid Build Coastguard Worker for (MachineInstr::const_mop_iterator I = UseMI.operands_begin(),
637*9880d681SAndroid Build Coastguard Worker E = UseMI.operands_end(); I != E; ++I) {
638*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = *I;
639*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
640*9880d681SAndroid Build Coastguard Worker continue;
641*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
642*9880d681SAndroid Build Coastguard Worker if (!Reg)
643*9880d681SAndroid Build Coastguard Worker continue;
644*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
645*9880d681SAndroid Build Coastguard Worker HasPhysRegs = true;
646*9880d681SAndroid Build Coastguard Worker continue;
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker // Collect virtual register reads.
649*9880d681SAndroid Build Coastguard Worker if (MO.readsReg())
650*9880d681SAndroid Build Coastguard Worker Deps.push_back(DataDep(MRI, Reg, UseMI.getOperandNo(I)));
651*9880d681SAndroid Build Coastguard Worker }
652*9880d681SAndroid Build Coastguard Worker return HasPhysRegs;
653*9880d681SAndroid Build Coastguard Worker }
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard Worker // Get the input data dependencies of a PHI instruction, using Pred as the
656*9880d681SAndroid Build Coastguard Worker // preferred predecessor.
657*9880d681SAndroid Build Coastguard Worker // This will add at most one dependency to Deps.
getPHIDeps(const MachineInstr & UseMI,SmallVectorImpl<DataDep> & Deps,const MachineBasicBlock * Pred,const MachineRegisterInfo * MRI)658*9880d681SAndroid Build Coastguard Worker static void getPHIDeps(const MachineInstr &UseMI,
659*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<DataDep> &Deps,
660*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Pred,
661*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI) {
662*9880d681SAndroid Build Coastguard Worker // No predecessor at the beginning of a trace. Ignore dependencies.
663*9880d681SAndroid Build Coastguard Worker if (!Pred)
664*9880d681SAndroid Build Coastguard Worker return;
665*9880d681SAndroid Build Coastguard Worker assert(UseMI.isPHI() && UseMI.getNumOperands() % 2 && "Bad PHI");
666*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i != UseMI.getNumOperands(); i += 2) {
667*9880d681SAndroid Build Coastguard Worker if (UseMI.getOperand(i + 1).getMBB() == Pred) {
668*9880d681SAndroid Build Coastguard Worker unsigned Reg = UseMI.getOperand(i).getReg();
669*9880d681SAndroid Build Coastguard Worker Deps.push_back(DataDep(MRI, Reg, i));
670*9880d681SAndroid Build Coastguard Worker return;
671*9880d681SAndroid Build Coastguard Worker }
672*9880d681SAndroid Build Coastguard Worker }
673*9880d681SAndroid Build Coastguard Worker }
674*9880d681SAndroid Build Coastguard Worker
675*9880d681SAndroid Build Coastguard Worker // Keep track of physreg data dependencies by recording each live register unit.
676*9880d681SAndroid Build Coastguard Worker // Associate each regunit with an instruction operand. Depending on the
677*9880d681SAndroid Build Coastguard Worker // direction instructions are scanned, it could be the operand that defined the
678*9880d681SAndroid Build Coastguard Worker // regunit, or the highest operand to read the regunit.
679*9880d681SAndroid Build Coastguard Worker namespace {
680*9880d681SAndroid Build Coastguard Worker struct LiveRegUnit {
681*9880d681SAndroid Build Coastguard Worker unsigned RegUnit;
682*9880d681SAndroid Build Coastguard Worker unsigned Cycle;
683*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI;
684*9880d681SAndroid Build Coastguard Worker unsigned Op;
685*9880d681SAndroid Build Coastguard Worker
getSparseSetIndex__anonf071fd070411::LiveRegUnit686*9880d681SAndroid Build Coastguard Worker unsigned getSparseSetIndex() const { return RegUnit; }
687*9880d681SAndroid Build Coastguard Worker
LiveRegUnit__anonf071fd070411::LiveRegUnit688*9880d681SAndroid Build Coastguard Worker LiveRegUnit(unsigned RU) : RegUnit(RU), Cycle(0), MI(nullptr), Op(0) {}
689*9880d681SAndroid Build Coastguard Worker };
690*9880d681SAndroid Build Coastguard Worker }
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard Worker // Identify physreg dependencies for UseMI, and update the live regunit
693*9880d681SAndroid Build Coastguard Worker // tracking set when scanning instructions downwards.
updatePhysDepsDownwards(const MachineInstr * UseMI,SmallVectorImpl<DataDep> & Deps,SparseSet<LiveRegUnit> & RegUnits,const TargetRegisterInfo * TRI)694*9880d681SAndroid Build Coastguard Worker static void updatePhysDepsDownwards(const MachineInstr *UseMI,
695*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<DataDep> &Deps,
696*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit> &RegUnits,
697*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) {
698*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> Kills;
699*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> LiveDefOps;
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker for (MachineInstr::const_mop_iterator MI = UseMI->operands_begin(),
702*9880d681SAndroid Build Coastguard Worker ME = UseMI->operands_end(); MI != ME; ++MI) {
703*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = *MI;
704*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
705*9880d681SAndroid Build Coastguard Worker continue;
706*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
707*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(Reg))
708*9880d681SAndroid Build Coastguard Worker continue;
709*9880d681SAndroid Build Coastguard Worker // Track live defs and kills for updating RegUnits.
710*9880d681SAndroid Build Coastguard Worker if (MO.isDef()) {
711*9880d681SAndroid Build Coastguard Worker if (MO.isDead())
712*9880d681SAndroid Build Coastguard Worker Kills.push_back(Reg);
713*9880d681SAndroid Build Coastguard Worker else
714*9880d681SAndroid Build Coastguard Worker LiveDefOps.push_back(UseMI->getOperandNo(MI));
715*9880d681SAndroid Build Coastguard Worker } else if (MO.isKill())
716*9880d681SAndroid Build Coastguard Worker Kills.push_back(Reg);
717*9880d681SAndroid Build Coastguard Worker // Identify dependencies.
718*9880d681SAndroid Build Coastguard Worker if (!MO.readsReg())
719*9880d681SAndroid Build Coastguard Worker continue;
720*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
721*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units);
722*9880d681SAndroid Build Coastguard Worker if (I == RegUnits.end())
723*9880d681SAndroid Build Coastguard Worker continue;
724*9880d681SAndroid Build Coastguard Worker Deps.push_back(DataDep(I->MI, I->Op, UseMI->getOperandNo(MI)));
725*9880d681SAndroid Build Coastguard Worker break;
726*9880d681SAndroid Build Coastguard Worker }
727*9880d681SAndroid Build Coastguard Worker }
728*9880d681SAndroid Build Coastguard Worker
729*9880d681SAndroid Build Coastguard Worker // Update RegUnits to reflect live registers after UseMI.
730*9880d681SAndroid Build Coastguard Worker // First kills.
731*9880d681SAndroid Build Coastguard Worker for (unsigned Kill : Kills)
732*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Kill, TRI); Units.isValid(); ++Units)
733*9880d681SAndroid Build Coastguard Worker RegUnits.erase(*Units);
734*9880d681SAndroid Build Coastguard Worker
735*9880d681SAndroid Build Coastguard Worker // Second, live defs.
736*9880d681SAndroid Build Coastguard Worker for (unsigned DefOp : LiveDefOps) {
737*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(UseMI->getOperand(DefOp).getReg(), TRI);
738*9880d681SAndroid Build Coastguard Worker Units.isValid(); ++Units) {
739*9880d681SAndroid Build Coastguard Worker LiveRegUnit &LRU = RegUnits[*Units];
740*9880d681SAndroid Build Coastguard Worker LRU.MI = UseMI;
741*9880d681SAndroid Build Coastguard Worker LRU.Op = DefOp;
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker }
744*9880d681SAndroid Build Coastguard Worker }
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker /// The length of the critical path through a trace is the maximum of two path
747*9880d681SAndroid Build Coastguard Worker /// lengths:
748*9880d681SAndroid Build Coastguard Worker ///
749*9880d681SAndroid Build Coastguard Worker /// 1. The maximum height+depth over all instructions in the trace center block.
750*9880d681SAndroid Build Coastguard Worker ///
751*9880d681SAndroid Build Coastguard Worker /// 2. The longest cross-block dependency chain. For small blocks, it is
752*9880d681SAndroid Build Coastguard Worker /// possible that the critical path through the trace doesn't include any
753*9880d681SAndroid Build Coastguard Worker /// instructions in the block.
754*9880d681SAndroid Build Coastguard Worker ///
755*9880d681SAndroid Build Coastguard Worker /// This function computes the second number from the live-in list of the
756*9880d681SAndroid Build Coastguard Worker /// center block.
757*9880d681SAndroid Build Coastguard Worker unsigned MachineTraceMetrics::Ensemble::
computeCrossBlockCriticalPath(const TraceBlockInfo & TBI)758*9880d681SAndroid Build Coastguard Worker computeCrossBlockCriticalPath(const TraceBlockInfo &TBI) {
759*9880d681SAndroid Build Coastguard Worker assert(TBI.HasValidInstrDepths && "Missing depth info");
760*9880d681SAndroid Build Coastguard Worker assert(TBI.HasValidInstrHeights && "Missing height info");
761*9880d681SAndroid Build Coastguard Worker unsigned MaxLen = 0;
762*9880d681SAndroid Build Coastguard Worker for (const LiveInReg &LIR : TBI.LiveIns) {
763*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(LIR.Reg))
764*9880d681SAndroid Build Coastguard Worker continue;
765*9880d681SAndroid Build Coastguard Worker const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg);
766*9880d681SAndroid Build Coastguard Worker // Ignore dependencies outside the current trace.
767*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo &DefTBI = BlockInfo[DefMI->getParent()->getNumber()];
768*9880d681SAndroid Build Coastguard Worker if (!DefTBI.isUsefulDominator(TBI))
769*9880d681SAndroid Build Coastguard Worker continue;
770*9880d681SAndroid Build Coastguard Worker unsigned Len = LIR.Height + Cycles[DefMI].Depth;
771*9880d681SAndroid Build Coastguard Worker MaxLen = std::max(MaxLen, Len);
772*9880d681SAndroid Build Coastguard Worker }
773*9880d681SAndroid Build Coastguard Worker return MaxLen;
774*9880d681SAndroid Build Coastguard Worker }
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker /// Compute instruction depths for all instructions above or in MBB in its
777*9880d681SAndroid Build Coastguard Worker /// trace. This assumes that the trace through MBB has already been computed.
778*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::
computeInstrDepths(const MachineBasicBlock * MBB)779*9880d681SAndroid Build Coastguard Worker computeInstrDepths(const MachineBasicBlock *MBB) {
780*9880d681SAndroid Build Coastguard Worker // The top of the trace may already be computed, and HasValidInstrDepths
781*9880d681SAndroid Build Coastguard Worker // implies Head->HasValidInstrDepths, so we only need to start from the first
782*9880d681SAndroid Build Coastguard Worker // block in the trace that needs to be recomputed.
783*9880d681SAndroid Build Coastguard Worker SmallVector<const MachineBasicBlock*, 8> Stack;
784*9880d681SAndroid Build Coastguard Worker do {
785*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
786*9880d681SAndroid Build Coastguard Worker assert(TBI.hasValidDepth() && "Incomplete trace");
787*9880d681SAndroid Build Coastguard Worker if (TBI.HasValidInstrDepths)
788*9880d681SAndroid Build Coastguard Worker break;
789*9880d681SAndroid Build Coastguard Worker Stack.push_back(MBB);
790*9880d681SAndroid Build Coastguard Worker MBB = TBI.Pred;
791*9880d681SAndroid Build Coastguard Worker } while (MBB);
792*9880d681SAndroid Build Coastguard Worker
793*9880d681SAndroid Build Coastguard Worker // FIXME: If MBB is non-null at this point, it is the last pre-computed block
794*9880d681SAndroid Build Coastguard Worker // in the trace. We should track any live-out physregs that were defined in
795*9880d681SAndroid Build Coastguard Worker // the trace. This is quite rare in SSA form, typically created by CSE
796*9880d681SAndroid Build Coastguard Worker // hoisting a compare.
797*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit> RegUnits;
798*9880d681SAndroid Build Coastguard Worker RegUnits.setUniverse(MTM.TRI->getNumRegUnits());
799*9880d681SAndroid Build Coastguard Worker
800*9880d681SAndroid Build Coastguard Worker // Go through trace blocks in top-down order, stopping after the center block.
801*9880d681SAndroid Build Coastguard Worker SmallVector<DataDep, 8> Deps;
802*9880d681SAndroid Build Coastguard Worker while (!Stack.empty()) {
803*9880d681SAndroid Build Coastguard Worker MBB = Stack.pop_back_val();
804*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\nDepths for BB#" << MBB->getNumber() << ":\n");
805*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
806*9880d681SAndroid Build Coastguard Worker TBI.HasValidInstrDepths = true;
807*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = 0;
808*9880d681SAndroid Build Coastguard Worker
809*9880d681SAndroid Build Coastguard Worker // Print out resource depths here as well.
810*9880d681SAndroid Build Coastguard Worker DEBUG({
811*9880d681SAndroid Build Coastguard Worker dbgs() << format("%7u Instructions\n", TBI.InstrDepth);
812*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRDepths = getProcResourceDepths(MBB->getNumber());
813*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRDepths.size(); ++K)
814*9880d681SAndroid Build Coastguard Worker if (PRDepths[K]) {
815*9880d681SAndroid Build Coastguard Worker unsigned Factor = MTM.SchedModel.getResourceFactor(K);
816*9880d681SAndroid Build Coastguard Worker dbgs() << format("%6uc @ ", MTM.getCycles(PRDepths[K]))
817*9880d681SAndroid Build Coastguard Worker << MTM.SchedModel.getProcResource(K)->Name << " ("
818*9880d681SAndroid Build Coastguard Worker << PRDepths[K]/Factor << " ops x" << Factor << ")\n";
819*9880d681SAndroid Build Coastguard Worker }
820*9880d681SAndroid Build Coastguard Worker });
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard Worker // Also compute the critical path length through MBB when possible.
823*9880d681SAndroid Build Coastguard Worker if (TBI.HasValidInstrHeights)
824*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = computeCrossBlockCriticalPath(TBI);
825*9880d681SAndroid Build Coastguard Worker
826*9880d681SAndroid Build Coastguard Worker for (const auto &UseMI : *MBB) {
827*9880d681SAndroid Build Coastguard Worker // Collect all data dependencies.
828*9880d681SAndroid Build Coastguard Worker Deps.clear();
829*9880d681SAndroid Build Coastguard Worker if (UseMI.isPHI())
830*9880d681SAndroid Build Coastguard Worker getPHIDeps(UseMI, Deps, TBI.Pred, MTM.MRI);
831*9880d681SAndroid Build Coastguard Worker else if (getDataDeps(UseMI, Deps, MTM.MRI))
832*9880d681SAndroid Build Coastguard Worker updatePhysDepsDownwards(&UseMI, Deps, RegUnits, MTM.TRI);
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker // Filter and process dependencies, computing the earliest issue cycle.
835*9880d681SAndroid Build Coastguard Worker unsigned Cycle = 0;
836*9880d681SAndroid Build Coastguard Worker for (const DataDep &Dep : Deps) {
837*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo&DepTBI =
838*9880d681SAndroid Build Coastguard Worker BlockInfo[Dep.DefMI->getParent()->getNumber()];
839*9880d681SAndroid Build Coastguard Worker // Ignore dependencies from outside the current trace.
840*9880d681SAndroid Build Coastguard Worker if (!DepTBI.isUsefulDominator(TBI))
841*9880d681SAndroid Build Coastguard Worker continue;
842*9880d681SAndroid Build Coastguard Worker assert(DepTBI.HasValidInstrDepths && "Inconsistent dependency");
843*9880d681SAndroid Build Coastguard Worker unsigned DepCycle = Cycles.lookup(Dep.DefMI).Depth;
844*9880d681SAndroid Build Coastguard Worker // Add latency if DefMI is a real instruction. Transients get latency 0.
845*9880d681SAndroid Build Coastguard Worker if (!Dep.DefMI->isTransient())
846*9880d681SAndroid Build Coastguard Worker DepCycle += MTM.SchedModel
847*9880d681SAndroid Build Coastguard Worker .computeOperandLatency(Dep.DefMI, Dep.DefOp, &UseMI, Dep.UseOp);
848*9880d681SAndroid Build Coastguard Worker Cycle = std::max(Cycle, DepCycle);
849*9880d681SAndroid Build Coastguard Worker }
850*9880d681SAndroid Build Coastguard Worker // Remember the instruction depth.
851*9880d681SAndroid Build Coastguard Worker InstrCycles &MICycles = Cycles[&UseMI];
852*9880d681SAndroid Build Coastguard Worker MICycles.Depth = Cycle;
853*9880d681SAndroid Build Coastguard Worker
854*9880d681SAndroid Build Coastguard Worker if (!TBI.HasValidInstrHeights) {
855*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << Cycle << '\t' << UseMI);
856*9880d681SAndroid Build Coastguard Worker continue;
857*9880d681SAndroid Build Coastguard Worker }
858*9880d681SAndroid Build Coastguard Worker // Update critical path length.
859*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Height);
860*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << UseMI);
861*9880d681SAndroid Build Coastguard Worker }
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker }
864*9880d681SAndroid Build Coastguard Worker
865*9880d681SAndroid Build Coastguard Worker // Identify physreg dependencies for MI when scanning instructions upwards.
866*9880d681SAndroid Build Coastguard Worker // Return the issue height of MI after considering any live regunits.
867*9880d681SAndroid Build Coastguard Worker // Height is the issue height computed from virtual register dependencies alone.
updatePhysDepsUpwards(const MachineInstr & MI,unsigned Height,SparseSet<LiveRegUnit> & RegUnits,const TargetSchedModel & SchedModel,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)868*9880d681SAndroid Build Coastguard Worker static unsigned updatePhysDepsUpwards(const MachineInstr &MI, unsigned Height,
869*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit> &RegUnits,
870*9880d681SAndroid Build Coastguard Worker const TargetSchedModel &SchedModel,
871*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII,
872*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) {
873*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> ReadOps;
874*9880d681SAndroid Build Coastguard Worker
875*9880d681SAndroid Build Coastguard Worker for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(),
876*9880d681SAndroid Build Coastguard Worker MOE = MI.operands_end();
877*9880d681SAndroid Build Coastguard Worker MOI != MOE; ++MOI) {
878*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = *MOI;
879*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
880*9880d681SAndroid Build Coastguard Worker continue;
881*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
882*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(Reg))
883*9880d681SAndroid Build Coastguard Worker continue;
884*9880d681SAndroid Build Coastguard Worker if (MO.readsReg())
885*9880d681SAndroid Build Coastguard Worker ReadOps.push_back(MI.getOperandNo(MOI));
886*9880d681SAndroid Build Coastguard Worker if (!MO.isDef())
887*9880d681SAndroid Build Coastguard Worker continue;
888*9880d681SAndroid Build Coastguard Worker // This is a def of Reg. Remove corresponding entries from RegUnits, and
889*9880d681SAndroid Build Coastguard Worker // update MI Height to consider the physreg dependencies.
890*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
891*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit>::iterator I = RegUnits.find(*Units);
892*9880d681SAndroid Build Coastguard Worker if (I == RegUnits.end())
893*9880d681SAndroid Build Coastguard Worker continue;
894*9880d681SAndroid Build Coastguard Worker unsigned DepHeight = I->Cycle;
895*9880d681SAndroid Build Coastguard Worker if (!MI.isTransient()) {
896*9880d681SAndroid Build Coastguard Worker // We may not know the UseMI of this dependency, if it came from the
897*9880d681SAndroid Build Coastguard Worker // live-in list. SchedModel can handle a NULL UseMI.
898*9880d681SAndroid Build Coastguard Worker DepHeight += SchedModel.computeOperandLatency(&MI, MI.getOperandNo(MOI),
899*9880d681SAndroid Build Coastguard Worker I->MI, I->Op);
900*9880d681SAndroid Build Coastguard Worker }
901*9880d681SAndroid Build Coastguard Worker Height = std::max(Height, DepHeight);
902*9880d681SAndroid Build Coastguard Worker // This regunit is dead above MI.
903*9880d681SAndroid Build Coastguard Worker RegUnits.erase(I);
904*9880d681SAndroid Build Coastguard Worker }
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker
907*9880d681SAndroid Build Coastguard Worker // Now we know the height of MI. Update any regunits read.
908*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = ReadOps.size(); i != e; ++i) {
909*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI.getOperand(ReadOps[i]).getReg();
910*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
911*9880d681SAndroid Build Coastguard Worker LiveRegUnit &LRU = RegUnits[*Units];
912*9880d681SAndroid Build Coastguard Worker // Set the height to the highest reader of the unit.
913*9880d681SAndroid Build Coastguard Worker if (LRU.Cycle <= Height && LRU.MI != &MI) {
914*9880d681SAndroid Build Coastguard Worker LRU.Cycle = Height;
915*9880d681SAndroid Build Coastguard Worker LRU.MI = &MI;
916*9880d681SAndroid Build Coastguard Worker LRU.Op = ReadOps[i];
917*9880d681SAndroid Build Coastguard Worker }
918*9880d681SAndroid Build Coastguard Worker }
919*9880d681SAndroid Build Coastguard Worker }
920*9880d681SAndroid Build Coastguard Worker
921*9880d681SAndroid Build Coastguard Worker return Height;
922*9880d681SAndroid Build Coastguard Worker }
923*9880d681SAndroid Build Coastguard Worker
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker typedef DenseMap<const MachineInstr *, unsigned> MIHeightMap;
926*9880d681SAndroid Build Coastguard Worker
927*9880d681SAndroid Build Coastguard Worker // Push the height of DefMI upwards if required to match UseMI.
928*9880d681SAndroid Build Coastguard Worker // Return true if this is the first time DefMI was seen.
pushDepHeight(const DataDep & Dep,const MachineInstr & UseMI,unsigned UseHeight,MIHeightMap & Heights,const TargetSchedModel & SchedModel,const TargetInstrInfo * TII)929*9880d681SAndroid Build Coastguard Worker static bool pushDepHeight(const DataDep &Dep, const MachineInstr &UseMI,
930*9880d681SAndroid Build Coastguard Worker unsigned UseHeight, MIHeightMap &Heights,
931*9880d681SAndroid Build Coastguard Worker const TargetSchedModel &SchedModel,
932*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII) {
933*9880d681SAndroid Build Coastguard Worker // Adjust height by Dep.DefMI latency.
934*9880d681SAndroid Build Coastguard Worker if (!Dep.DefMI->isTransient())
935*9880d681SAndroid Build Coastguard Worker UseHeight += SchedModel.computeOperandLatency(Dep.DefMI, Dep.DefOp, &UseMI,
936*9880d681SAndroid Build Coastguard Worker Dep.UseOp);
937*9880d681SAndroid Build Coastguard Worker
938*9880d681SAndroid Build Coastguard Worker // Update Heights[DefMI] to be the maximum height seen.
939*9880d681SAndroid Build Coastguard Worker MIHeightMap::iterator I;
940*9880d681SAndroid Build Coastguard Worker bool New;
941*9880d681SAndroid Build Coastguard Worker std::tie(I, New) = Heights.insert(std::make_pair(Dep.DefMI, UseHeight));
942*9880d681SAndroid Build Coastguard Worker if (New)
943*9880d681SAndroid Build Coastguard Worker return true;
944*9880d681SAndroid Build Coastguard Worker
945*9880d681SAndroid Build Coastguard Worker // DefMI has been pushed before. Give it the max height.
946*9880d681SAndroid Build Coastguard Worker if (I->second < UseHeight)
947*9880d681SAndroid Build Coastguard Worker I->second = UseHeight;
948*9880d681SAndroid Build Coastguard Worker return false;
949*9880d681SAndroid Build Coastguard Worker }
950*9880d681SAndroid Build Coastguard Worker
951*9880d681SAndroid Build Coastguard Worker /// Assuming that the virtual register defined by DefMI:DefOp was used by
952*9880d681SAndroid Build Coastguard Worker /// Trace.back(), add it to the live-in lists of all the blocks in Trace. Stop
953*9880d681SAndroid Build Coastguard Worker /// when reaching the block that contains DefMI.
954*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::
addLiveIns(const MachineInstr * DefMI,unsigned DefOp,ArrayRef<const MachineBasicBlock * > Trace)955*9880d681SAndroid Build Coastguard Worker addLiveIns(const MachineInstr *DefMI, unsigned DefOp,
956*9880d681SAndroid Build Coastguard Worker ArrayRef<const MachineBasicBlock*> Trace) {
957*9880d681SAndroid Build Coastguard Worker assert(!Trace.empty() && "Trace should contain at least one block");
958*9880d681SAndroid Build Coastguard Worker unsigned Reg = DefMI->getOperand(DefOp).getReg();
959*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(Reg));
960*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *DefMBB = DefMI->getParent();
961*9880d681SAndroid Build Coastguard Worker
962*9880d681SAndroid Build Coastguard Worker // Reg is live-in to all blocks in Trace that follow DefMBB.
963*9880d681SAndroid Build Coastguard Worker for (unsigned i = Trace.size(); i; --i) {
964*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = Trace[i-1];
965*9880d681SAndroid Build Coastguard Worker if (MBB == DefMBB)
966*9880d681SAndroid Build Coastguard Worker return;
967*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
968*9880d681SAndroid Build Coastguard Worker // Just add the register. The height will be updated later.
969*9880d681SAndroid Build Coastguard Worker TBI.LiveIns.push_back(Reg);
970*9880d681SAndroid Build Coastguard Worker }
971*9880d681SAndroid Build Coastguard Worker }
972*9880d681SAndroid Build Coastguard Worker
973*9880d681SAndroid Build Coastguard Worker /// Compute instruction heights in the trace through MBB. This updates MBB and
974*9880d681SAndroid Build Coastguard Worker /// the blocks below it in the trace. It is assumed that the trace has already
975*9880d681SAndroid Build Coastguard Worker /// been computed.
976*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::
computeInstrHeights(const MachineBasicBlock * MBB)977*9880d681SAndroid Build Coastguard Worker computeInstrHeights(const MachineBasicBlock *MBB) {
978*9880d681SAndroid Build Coastguard Worker // The bottom of the trace may already be computed.
979*9880d681SAndroid Build Coastguard Worker // Find the blocks that need updating.
980*9880d681SAndroid Build Coastguard Worker SmallVector<const MachineBasicBlock*, 8> Stack;
981*9880d681SAndroid Build Coastguard Worker do {
982*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
983*9880d681SAndroid Build Coastguard Worker assert(TBI.hasValidHeight() && "Incomplete trace");
984*9880d681SAndroid Build Coastguard Worker if (TBI.HasValidInstrHeights)
985*9880d681SAndroid Build Coastguard Worker break;
986*9880d681SAndroid Build Coastguard Worker Stack.push_back(MBB);
987*9880d681SAndroid Build Coastguard Worker TBI.LiveIns.clear();
988*9880d681SAndroid Build Coastguard Worker MBB = TBI.Succ;
989*9880d681SAndroid Build Coastguard Worker } while (MBB);
990*9880d681SAndroid Build Coastguard Worker
991*9880d681SAndroid Build Coastguard Worker // As we move upwards in the trace, keep track of instructions that are
992*9880d681SAndroid Build Coastguard Worker // required by deeper trace instructions. Map MI -> height required so far.
993*9880d681SAndroid Build Coastguard Worker MIHeightMap Heights;
994*9880d681SAndroid Build Coastguard Worker
995*9880d681SAndroid Build Coastguard Worker // For physregs, the def isn't known when we see the use.
996*9880d681SAndroid Build Coastguard Worker // Instead, keep track of the highest use of each regunit.
997*9880d681SAndroid Build Coastguard Worker SparseSet<LiveRegUnit> RegUnits;
998*9880d681SAndroid Build Coastguard Worker RegUnits.setUniverse(MTM.TRI->getNumRegUnits());
999*9880d681SAndroid Build Coastguard Worker
1000*9880d681SAndroid Build Coastguard Worker // If the bottom of the trace was already precomputed, initialize heights
1001*9880d681SAndroid Build Coastguard Worker // from its live-in list.
1002*9880d681SAndroid Build Coastguard Worker // MBB is the highest precomputed block in the trace.
1003*9880d681SAndroid Build Coastguard Worker if (MBB) {
1004*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
1005*9880d681SAndroid Build Coastguard Worker for (LiveInReg &LI : TBI.LiveIns) {
1006*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(LI.Reg)) {
1007*9880d681SAndroid Build Coastguard Worker // For virtual registers, the def latency is included.
1008*9880d681SAndroid Build Coastguard Worker unsigned &Height = Heights[MTM.MRI->getVRegDef(LI.Reg)];
1009*9880d681SAndroid Build Coastguard Worker if (Height < LI.Height)
1010*9880d681SAndroid Build Coastguard Worker Height = LI.Height;
1011*9880d681SAndroid Build Coastguard Worker } else {
1012*9880d681SAndroid Build Coastguard Worker // For register units, the def latency is not included because we don't
1013*9880d681SAndroid Build Coastguard Worker // know the def yet.
1014*9880d681SAndroid Build Coastguard Worker RegUnits[LI.Reg].Cycle = LI.Height;
1015*9880d681SAndroid Build Coastguard Worker }
1016*9880d681SAndroid Build Coastguard Worker }
1017*9880d681SAndroid Build Coastguard Worker }
1018*9880d681SAndroid Build Coastguard Worker
1019*9880d681SAndroid Build Coastguard Worker // Go through the trace blocks in bottom-up order.
1020*9880d681SAndroid Build Coastguard Worker SmallVector<DataDep, 8> Deps;
1021*9880d681SAndroid Build Coastguard Worker for (;!Stack.empty(); Stack.pop_back()) {
1022*9880d681SAndroid Build Coastguard Worker MBB = Stack.back();
1023*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Heights for BB#" << MBB->getNumber() << ":\n");
1024*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
1025*9880d681SAndroid Build Coastguard Worker TBI.HasValidInstrHeights = true;
1026*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = 0;
1027*9880d681SAndroid Build Coastguard Worker
1028*9880d681SAndroid Build Coastguard Worker DEBUG({
1029*9880d681SAndroid Build Coastguard Worker dbgs() << format("%7u Instructions\n", TBI.InstrHeight);
1030*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRHeights = getProcResourceHeights(MBB->getNumber());
1031*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRHeights.size(); ++K)
1032*9880d681SAndroid Build Coastguard Worker if (PRHeights[K]) {
1033*9880d681SAndroid Build Coastguard Worker unsigned Factor = MTM.SchedModel.getResourceFactor(K);
1034*9880d681SAndroid Build Coastguard Worker dbgs() << format("%6uc @ ", MTM.getCycles(PRHeights[K]))
1035*9880d681SAndroid Build Coastguard Worker << MTM.SchedModel.getProcResource(K)->Name << " ("
1036*9880d681SAndroid Build Coastguard Worker << PRHeights[K]/Factor << " ops x" << Factor << ")\n";
1037*9880d681SAndroid Build Coastguard Worker }
1038*9880d681SAndroid Build Coastguard Worker });
1039*9880d681SAndroid Build Coastguard Worker
1040*9880d681SAndroid Build Coastguard Worker // Get dependencies from PHIs in the trace successor.
1041*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Succ = TBI.Succ;
1042*9880d681SAndroid Build Coastguard Worker // If MBB is the last block in the trace, and it has a back-edge to the
1043*9880d681SAndroid Build Coastguard Worker // loop header, get loop-carried dependencies from PHIs in the header. For
1044*9880d681SAndroid Build Coastguard Worker // that purpose, pretend that all the loop header PHIs have height 0.
1045*9880d681SAndroid Build Coastguard Worker if (!Succ)
1046*9880d681SAndroid Build Coastguard Worker if (const MachineLoop *Loop = getLoopFor(MBB))
1047*9880d681SAndroid Build Coastguard Worker if (MBB->isSuccessor(Loop->getHeader()))
1048*9880d681SAndroid Build Coastguard Worker Succ = Loop->getHeader();
1049*9880d681SAndroid Build Coastguard Worker
1050*9880d681SAndroid Build Coastguard Worker if (Succ) {
1051*9880d681SAndroid Build Coastguard Worker for (const auto &PHI : *Succ) {
1052*9880d681SAndroid Build Coastguard Worker if (!PHI.isPHI())
1053*9880d681SAndroid Build Coastguard Worker break;
1054*9880d681SAndroid Build Coastguard Worker Deps.clear();
1055*9880d681SAndroid Build Coastguard Worker getPHIDeps(PHI, Deps, MBB, MTM.MRI);
1056*9880d681SAndroid Build Coastguard Worker if (!Deps.empty()) {
1057*9880d681SAndroid Build Coastguard Worker // Loop header PHI heights are all 0.
1058*9880d681SAndroid Build Coastguard Worker unsigned Height = TBI.Succ ? Cycles.lookup(&PHI).Height : 0;
1059*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "pred\t" << Height << '\t' << PHI);
1060*9880d681SAndroid Build Coastguard Worker if (pushDepHeight(Deps.front(), PHI, Height, Heights, MTM.SchedModel,
1061*9880d681SAndroid Build Coastguard Worker MTM.TII))
1062*9880d681SAndroid Build Coastguard Worker addLiveIns(Deps.front().DefMI, Deps.front().DefOp, Stack);
1063*9880d681SAndroid Build Coastguard Worker }
1064*9880d681SAndroid Build Coastguard Worker }
1065*9880d681SAndroid Build Coastguard Worker }
1066*9880d681SAndroid Build Coastguard Worker
1067*9880d681SAndroid Build Coastguard Worker // Go through the block backwards.
1068*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_iterator BI = MBB->end(), BB = MBB->begin();
1069*9880d681SAndroid Build Coastguard Worker BI != BB;) {
1070*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *--BI;
1071*9880d681SAndroid Build Coastguard Worker
1072*9880d681SAndroid Build Coastguard Worker // Find the MI height as determined by virtual register uses in the
1073*9880d681SAndroid Build Coastguard Worker // trace below.
1074*9880d681SAndroid Build Coastguard Worker unsigned Cycle = 0;
1075*9880d681SAndroid Build Coastguard Worker MIHeightMap::iterator HeightI = Heights.find(&MI);
1076*9880d681SAndroid Build Coastguard Worker if (HeightI != Heights.end()) {
1077*9880d681SAndroid Build Coastguard Worker Cycle = HeightI->second;
1078*9880d681SAndroid Build Coastguard Worker // We won't be seeing any more MI uses.
1079*9880d681SAndroid Build Coastguard Worker Heights.erase(HeightI);
1080*9880d681SAndroid Build Coastguard Worker }
1081*9880d681SAndroid Build Coastguard Worker
1082*9880d681SAndroid Build Coastguard Worker // Don't process PHI deps. They depend on the specific predecessor, and
1083*9880d681SAndroid Build Coastguard Worker // we'll get them when visiting the predecessor.
1084*9880d681SAndroid Build Coastguard Worker Deps.clear();
1085*9880d681SAndroid Build Coastguard Worker bool HasPhysRegs = !MI.isPHI() && getDataDeps(MI, Deps, MTM.MRI);
1086*9880d681SAndroid Build Coastguard Worker
1087*9880d681SAndroid Build Coastguard Worker // There may also be regunit dependencies to include in the height.
1088*9880d681SAndroid Build Coastguard Worker if (HasPhysRegs)
1089*9880d681SAndroid Build Coastguard Worker Cycle = updatePhysDepsUpwards(MI, Cycle, RegUnits, MTM.SchedModel,
1090*9880d681SAndroid Build Coastguard Worker MTM.TII, MTM.TRI);
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard Worker // Update the required height of any virtual registers read by MI.
1093*9880d681SAndroid Build Coastguard Worker for (const DataDep &Dep : Deps)
1094*9880d681SAndroid Build Coastguard Worker if (pushDepHeight(Dep, MI, Cycle, Heights, MTM.SchedModel, MTM.TII))
1095*9880d681SAndroid Build Coastguard Worker addLiveIns(Dep.DefMI, Dep.DefOp, Stack);
1096*9880d681SAndroid Build Coastguard Worker
1097*9880d681SAndroid Build Coastguard Worker InstrCycles &MICycles = Cycles[&MI];
1098*9880d681SAndroid Build Coastguard Worker MICycles.Height = Cycle;
1099*9880d681SAndroid Build Coastguard Worker if (!TBI.HasValidInstrDepths) {
1100*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << Cycle << '\t' << MI);
1101*9880d681SAndroid Build Coastguard Worker continue;
1102*9880d681SAndroid Build Coastguard Worker }
1103*9880d681SAndroid Build Coastguard Worker // Update critical path length.
1104*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = std::max(TBI.CriticalPath, Cycle + MICycles.Depth);
1105*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << TBI.CriticalPath << '\t' << Cycle << '\t' << MI);
1106*9880d681SAndroid Build Coastguard Worker }
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard Worker // Update virtual live-in heights. They were added by addLiveIns() with a 0
1109*9880d681SAndroid Build Coastguard Worker // height because the final height isn't known until now.
1110*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BB#" << MBB->getNumber() << " Live-ins:");
1111*9880d681SAndroid Build Coastguard Worker for (LiveInReg &LIR : TBI.LiveIns) {
1112*9880d681SAndroid Build Coastguard Worker const MachineInstr *DefMI = MTM.MRI->getVRegDef(LIR.Reg);
1113*9880d681SAndroid Build Coastguard Worker LIR.Height = Heights.lookup(DefMI);
1114*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << ' ' << PrintReg(LIR.Reg) << '@' << LIR.Height);
1115*9880d681SAndroid Build Coastguard Worker }
1116*9880d681SAndroid Build Coastguard Worker
1117*9880d681SAndroid Build Coastguard Worker // Transfer the live regunits to the live-in list.
1118*9880d681SAndroid Build Coastguard Worker for (SparseSet<LiveRegUnit>::const_iterator
1119*9880d681SAndroid Build Coastguard Worker RI = RegUnits.begin(), RE = RegUnits.end(); RI != RE; ++RI) {
1120*9880d681SAndroid Build Coastguard Worker TBI.LiveIns.push_back(LiveInReg(RI->RegUnit, RI->Cycle));
1121*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << ' ' << PrintRegUnit(RI->RegUnit, MTM.TRI)
1122*9880d681SAndroid Build Coastguard Worker << '@' << RI->Cycle);
1123*9880d681SAndroid Build Coastguard Worker }
1124*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << '\n');
1125*9880d681SAndroid Build Coastguard Worker
1126*9880d681SAndroid Build Coastguard Worker if (!TBI.HasValidInstrDepths)
1127*9880d681SAndroid Build Coastguard Worker continue;
1128*9880d681SAndroid Build Coastguard Worker // Add live-ins to the critical path length.
1129*9880d681SAndroid Build Coastguard Worker TBI.CriticalPath = std::max(TBI.CriticalPath,
1130*9880d681SAndroid Build Coastguard Worker computeCrossBlockCriticalPath(TBI));
1131*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Critical path: " << TBI.CriticalPath << '\n');
1132*9880d681SAndroid Build Coastguard Worker }
1133*9880d681SAndroid Build Coastguard Worker }
1134*9880d681SAndroid Build Coastguard Worker
1135*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Trace
getTrace(const MachineBasicBlock * MBB)1136*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) {
1137*9880d681SAndroid Build Coastguard Worker TraceBlockInfo &TBI = BlockInfo[MBB->getNumber()];
1138*9880d681SAndroid Build Coastguard Worker
1139*9880d681SAndroid Build Coastguard Worker if (!TBI.hasValidDepth() || !TBI.hasValidHeight())
1140*9880d681SAndroid Build Coastguard Worker computeTrace(MBB);
1141*9880d681SAndroid Build Coastguard Worker if (!TBI.HasValidInstrDepths)
1142*9880d681SAndroid Build Coastguard Worker computeInstrDepths(MBB);
1143*9880d681SAndroid Build Coastguard Worker if (!TBI.HasValidInstrHeights)
1144*9880d681SAndroid Build Coastguard Worker computeInstrHeights(MBB);
1145*9880d681SAndroid Build Coastguard Worker
1146*9880d681SAndroid Build Coastguard Worker return Trace(*this, TBI);
1147*9880d681SAndroid Build Coastguard Worker }
1148*9880d681SAndroid Build Coastguard Worker
1149*9880d681SAndroid Build Coastguard Worker unsigned
getInstrSlack(const MachineInstr & MI) const1150*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Trace::getInstrSlack(const MachineInstr &MI) const {
1151*9880d681SAndroid Build Coastguard Worker assert(getBlockNum() == unsigned(MI.getParent()->getNumber()) &&
1152*9880d681SAndroid Build Coastguard Worker "MI must be in the trace center block");
1153*9880d681SAndroid Build Coastguard Worker InstrCycles Cyc = getInstrCycles(MI);
1154*9880d681SAndroid Build Coastguard Worker return getCriticalPath() - (Cyc.Depth + Cyc.Height);
1155*9880d681SAndroid Build Coastguard Worker }
1156*9880d681SAndroid Build Coastguard Worker
1157*9880d681SAndroid Build Coastguard Worker unsigned
getPHIDepth(const MachineInstr & PHI) const1158*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Trace::getPHIDepth(const MachineInstr &PHI) const {
1159*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = TE.MTM.MF->getBlockNumbered(getBlockNum());
1160*9880d681SAndroid Build Coastguard Worker SmallVector<DataDep, 1> Deps;
1161*9880d681SAndroid Build Coastguard Worker getPHIDeps(PHI, Deps, MBB, TE.MTM.MRI);
1162*9880d681SAndroid Build Coastguard Worker assert(Deps.size() == 1 && "PHI doesn't have MBB as a predecessor");
1163*9880d681SAndroid Build Coastguard Worker DataDep &Dep = Deps.front();
1164*9880d681SAndroid Build Coastguard Worker unsigned DepCycle = getInstrCycles(*Dep.DefMI).Depth;
1165*9880d681SAndroid Build Coastguard Worker // Add latency if DefMI is a real instruction. Transients get latency 0.
1166*9880d681SAndroid Build Coastguard Worker if (!Dep.DefMI->isTransient())
1167*9880d681SAndroid Build Coastguard Worker DepCycle += TE.MTM.SchedModel.computeOperandLatency(Dep.DefMI, Dep.DefOp,
1168*9880d681SAndroid Build Coastguard Worker &PHI, Dep.UseOp);
1169*9880d681SAndroid Build Coastguard Worker return DepCycle;
1170*9880d681SAndroid Build Coastguard Worker }
1171*9880d681SAndroid Build Coastguard Worker
1172*9880d681SAndroid Build Coastguard Worker /// When bottom is set include instructions in current block in estimate.
getResourceDepth(bool Bottom) const1173*9880d681SAndroid Build Coastguard Worker unsigned MachineTraceMetrics::Trace::getResourceDepth(bool Bottom) const {
1174*9880d681SAndroid Build Coastguard Worker // Find the limiting processor resource.
1175*9880d681SAndroid Build Coastguard Worker // Numbers have been pre-scaled to be comparable.
1176*9880d681SAndroid Build Coastguard Worker unsigned PRMax = 0;
1177*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
1178*9880d681SAndroid Build Coastguard Worker if (Bottom) {
1179*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRCycles = TE.MTM.getProcResourceCycles(getBlockNum());
1180*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRDepths.size(); ++K)
1181*9880d681SAndroid Build Coastguard Worker PRMax = std::max(PRMax, PRDepths[K] + PRCycles[K]);
1182*9880d681SAndroid Build Coastguard Worker } else {
1183*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRDepths.size(); ++K)
1184*9880d681SAndroid Build Coastguard Worker PRMax = std::max(PRMax, PRDepths[K]);
1185*9880d681SAndroid Build Coastguard Worker }
1186*9880d681SAndroid Build Coastguard Worker // Convert to cycle count.
1187*9880d681SAndroid Build Coastguard Worker PRMax = TE.MTM.getCycles(PRMax);
1188*9880d681SAndroid Build Coastguard Worker
1189*9880d681SAndroid Build Coastguard Worker /// All instructions before current block
1190*9880d681SAndroid Build Coastguard Worker unsigned Instrs = TBI.InstrDepth;
1191*9880d681SAndroid Build Coastguard Worker // plus instructions in current block
1192*9880d681SAndroid Build Coastguard Worker if (Bottom)
1193*9880d681SAndroid Build Coastguard Worker Instrs += TE.MTM.BlockInfo[getBlockNum()].InstrCount;
1194*9880d681SAndroid Build Coastguard Worker if (unsigned IW = TE.MTM.SchedModel.getIssueWidth())
1195*9880d681SAndroid Build Coastguard Worker Instrs /= IW;
1196*9880d681SAndroid Build Coastguard Worker // Assume issue width 1 without a schedule model.
1197*9880d681SAndroid Build Coastguard Worker return std::max(Instrs, PRMax);
1198*9880d681SAndroid Build Coastguard Worker }
1199*9880d681SAndroid Build Coastguard Worker
getResourceLength(ArrayRef<const MachineBasicBlock * > Extrablocks,ArrayRef<const MCSchedClassDesc * > ExtraInstrs,ArrayRef<const MCSchedClassDesc * > RemoveInstrs) const1200*9880d681SAndroid Build Coastguard Worker unsigned MachineTraceMetrics::Trace::getResourceLength(
1201*9880d681SAndroid Build Coastguard Worker ArrayRef<const MachineBasicBlock *> Extrablocks,
1202*9880d681SAndroid Build Coastguard Worker ArrayRef<const MCSchedClassDesc *> ExtraInstrs,
1203*9880d681SAndroid Build Coastguard Worker ArrayRef<const MCSchedClassDesc *> RemoveInstrs) const {
1204*9880d681SAndroid Build Coastguard Worker // Add up resources above and below the center block.
1205*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRDepths = TE.getProcResourceDepths(getBlockNum());
1206*9880d681SAndroid Build Coastguard Worker ArrayRef<unsigned> PRHeights = TE.getProcResourceHeights(getBlockNum());
1207*9880d681SAndroid Build Coastguard Worker unsigned PRMax = 0;
1208*9880d681SAndroid Build Coastguard Worker
1209*9880d681SAndroid Build Coastguard Worker // Capture computing cycles from extra instructions
1210*9880d681SAndroid Build Coastguard Worker auto extraCycles = [this](ArrayRef<const MCSchedClassDesc *> Instrs,
1211*9880d681SAndroid Build Coastguard Worker unsigned ResourceIdx)
1212*9880d681SAndroid Build Coastguard Worker ->unsigned {
1213*9880d681SAndroid Build Coastguard Worker unsigned Cycles = 0;
1214*9880d681SAndroid Build Coastguard Worker for (const MCSchedClassDesc *SC : Instrs) {
1215*9880d681SAndroid Build Coastguard Worker if (!SC->isValid())
1216*9880d681SAndroid Build Coastguard Worker continue;
1217*9880d681SAndroid Build Coastguard Worker for (TargetSchedModel::ProcResIter
1218*9880d681SAndroid Build Coastguard Worker PI = TE.MTM.SchedModel.getWriteProcResBegin(SC),
1219*9880d681SAndroid Build Coastguard Worker PE = TE.MTM.SchedModel.getWriteProcResEnd(SC);
1220*9880d681SAndroid Build Coastguard Worker PI != PE; ++PI) {
1221*9880d681SAndroid Build Coastguard Worker if (PI->ProcResourceIdx != ResourceIdx)
1222*9880d681SAndroid Build Coastguard Worker continue;
1223*9880d681SAndroid Build Coastguard Worker Cycles +=
1224*9880d681SAndroid Build Coastguard Worker (PI->Cycles * TE.MTM.SchedModel.getResourceFactor(ResourceIdx));
1225*9880d681SAndroid Build Coastguard Worker }
1226*9880d681SAndroid Build Coastguard Worker }
1227*9880d681SAndroid Build Coastguard Worker return Cycles;
1228*9880d681SAndroid Build Coastguard Worker };
1229*9880d681SAndroid Build Coastguard Worker
1230*9880d681SAndroid Build Coastguard Worker for (unsigned K = 0; K != PRDepths.size(); ++K) {
1231*9880d681SAndroid Build Coastguard Worker unsigned PRCycles = PRDepths[K] + PRHeights[K];
1232*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *MBB : Extrablocks)
1233*9880d681SAndroid Build Coastguard Worker PRCycles += TE.MTM.getProcResourceCycles(MBB->getNumber())[K];
1234*9880d681SAndroid Build Coastguard Worker PRCycles += extraCycles(ExtraInstrs, K);
1235*9880d681SAndroid Build Coastguard Worker PRCycles -= extraCycles(RemoveInstrs, K);
1236*9880d681SAndroid Build Coastguard Worker PRMax = std::max(PRMax, PRCycles);
1237*9880d681SAndroid Build Coastguard Worker }
1238*9880d681SAndroid Build Coastguard Worker // Convert to cycle count.
1239*9880d681SAndroid Build Coastguard Worker PRMax = TE.MTM.getCycles(PRMax);
1240*9880d681SAndroid Build Coastguard Worker
1241*9880d681SAndroid Build Coastguard Worker // Instrs: #instructions in current trace outside current block.
1242*9880d681SAndroid Build Coastguard Worker unsigned Instrs = TBI.InstrDepth + TBI.InstrHeight;
1243*9880d681SAndroid Build Coastguard Worker // Add instruction count from the extra blocks.
1244*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *MBB : Extrablocks)
1245*9880d681SAndroid Build Coastguard Worker Instrs += TE.MTM.getResources(MBB)->InstrCount;
1246*9880d681SAndroid Build Coastguard Worker Instrs += ExtraInstrs.size();
1247*9880d681SAndroid Build Coastguard Worker Instrs -= RemoveInstrs.size();
1248*9880d681SAndroid Build Coastguard Worker if (unsigned IW = TE.MTM.SchedModel.getIssueWidth())
1249*9880d681SAndroid Build Coastguard Worker Instrs /= IW;
1250*9880d681SAndroid Build Coastguard Worker // Assume issue width 1 without a schedule model.
1251*9880d681SAndroid Build Coastguard Worker return std::max(Instrs, PRMax);
1252*9880d681SAndroid Build Coastguard Worker }
1253*9880d681SAndroid Build Coastguard Worker
isDepInTrace(const MachineInstr & DefMI,const MachineInstr & UseMI) const1254*9880d681SAndroid Build Coastguard Worker bool MachineTraceMetrics::Trace::isDepInTrace(const MachineInstr &DefMI,
1255*9880d681SAndroid Build Coastguard Worker const MachineInstr &UseMI) const {
1256*9880d681SAndroid Build Coastguard Worker if (DefMI.getParent() == UseMI.getParent())
1257*9880d681SAndroid Build Coastguard Worker return true;
1258*9880d681SAndroid Build Coastguard Worker
1259*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo &DepTBI = TE.BlockInfo[DefMI.getParent()->getNumber()];
1260*9880d681SAndroid Build Coastguard Worker const TraceBlockInfo &TBI = TE.BlockInfo[UseMI.getParent()->getNumber()];
1261*9880d681SAndroid Build Coastguard Worker
1262*9880d681SAndroid Build Coastguard Worker return DepTBI.isUsefulDominator(TBI);
1263*9880d681SAndroid Build Coastguard Worker }
1264*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const1265*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
1266*9880d681SAndroid Build Coastguard Worker OS << getName() << " ensemble:\n";
1267*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
1268*9880d681SAndroid Build Coastguard Worker OS << " BB#" << i << '\t';
1269*9880d681SAndroid Build Coastguard Worker BlockInfo[i].print(OS);
1270*9880d681SAndroid Build Coastguard Worker OS << '\n';
1271*9880d681SAndroid Build Coastguard Worker }
1272*9880d681SAndroid Build Coastguard Worker }
1273*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const1274*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const {
1275*9880d681SAndroid Build Coastguard Worker if (hasValidDepth()) {
1276*9880d681SAndroid Build Coastguard Worker OS << "depth=" << InstrDepth;
1277*9880d681SAndroid Build Coastguard Worker if (Pred)
1278*9880d681SAndroid Build Coastguard Worker OS << " pred=BB#" << Pred->getNumber();
1279*9880d681SAndroid Build Coastguard Worker else
1280*9880d681SAndroid Build Coastguard Worker OS << " pred=null";
1281*9880d681SAndroid Build Coastguard Worker OS << " head=BB#" << Head;
1282*9880d681SAndroid Build Coastguard Worker if (HasValidInstrDepths)
1283*9880d681SAndroid Build Coastguard Worker OS << " +instrs";
1284*9880d681SAndroid Build Coastguard Worker } else
1285*9880d681SAndroid Build Coastguard Worker OS << "depth invalid";
1286*9880d681SAndroid Build Coastguard Worker OS << ", ";
1287*9880d681SAndroid Build Coastguard Worker if (hasValidHeight()) {
1288*9880d681SAndroid Build Coastguard Worker OS << "height=" << InstrHeight;
1289*9880d681SAndroid Build Coastguard Worker if (Succ)
1290*9880d681SAndroid Build Coastguard Worker OS << " succ=BB#" << Succ->getNumber();
1291*9880d681SAndroid Build Coastguard Worker else
1292*9880d681SAndroid Build Coastguard Worker OS << " succ=null";
1293*9880d681SAndroid Build Coastguard Worker OS << " tail=BB#" << Tail;
1294*9880d681SAndroid Build Coastguard Worker if (HasValidInstrHeights)
1295*9880d681SAndroid Build Coastguard Worker OS << " +instrs";
1296*9880d681SAndroid Build Coastguard Worker } else
1297*9880d681SAndroid Build Coastguard Worker OS << "height invalid";
1298*9880d681SAndroid Build Coastguard Worker if (HasValidInstrDepths && HasValidInstrHeights)
1299*9880d681SAndroid Build Coastguard Worker OS << ", crit=" << CriticalPath;
1300*9880d681SAndroid Build Coastguard Worker }
1301*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const1302*9880d681SAndroid Build Coastguard Worker void MachineTraceMetrics::Trace::print(raw_ostream &OS) const {
1303*9880d681SAndroid Build Coastguard Worker unsigned MBBNum = &TBI - &TE.BlockInfo[0];
1304*9880d681SAndroid Build Coastguard Worker
1305*9880d681SAndroid Build Coastguard Worker OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum
1306*9880d681SAndroid Build Coastguard Worker << " --> BB#" << TBI.Tail << ':';
1307*9880d681SAndroid Build Coastguard Worker if (TBI.hasValidHeight() && TBI.hasValidDepth())
1308*9880d681SAndroid Build Coastguard Worker OS << ' ' << getInstrCount() << " instrs.";
1309*9880d681SAndroid Build Coastguard Worker if (TBI.HasValidInstrDepths && TBI.HasValidInstrHeights)
1310*9880d681SAndroid Build Coastguard Worker OS << ' ' << TBI.CriticalPath << " cycles.";
1311*9880d681SAndroid Build Coastguard Worker
1312*9880d681SAndroid Build Coastguard Worker const MachineTraceMetrics::TraceBlockInfo *Block = &TBI;
1313*9880d681SAndroid Build Coastguard Worker OS << "\nBB#" << MBBNum;
1314*9880d681SAndroid Build Coastguard Worker while (Block->hasValidDepth() && Block->Pred) {
1315*9880d681SAndroid Build Coastguard Worker unsigned Num = Block->Pred->getNumber();
1316*9880d681SAndroid Build Coastguard Worker OS << " <- BB#" << Num;
1317*9880d681SAndroid Build Coastguard Worker Block = &TE.BlockInfo[Num];
1318*9880d681SAndroid Build Coastguard Worker }
1319*9880d681SAndroid Build Coastguard Worker
1320*9880d681SAndroid Build Coastguard Worker Block = &TBI;
1321*9880d681SAndroid Build Coastguard Worker OS << "\n ";
1322*9880d681SAndroid Build Coastguard Worker while (Block->hasValidHeight() && Block->Succ) {
1323*9880d681SAndroid Build Coastguard Worker unsigned Num = Block->Succ->getNumber();
1324*9880d681SAndroid Build Coastguard Worker OS << " -> BB#" << Num;
1325*9880d681SAndroid Build Coastguard Worker Block = &TE.BlockInfo[Num];
1326*9880d681SAndroid Build Coastguard Worker }
1327*9880d681SAndroid Build Coastguard Worker OS << '\n';
1328*9880d681SAndroid Build Coastguard Worker }
1329