blob: 3037cb3210e6967e91bc59a590db15965959fb1a [file] [log] [blame]
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +00001//===- lib/CodeGen/MachineTraceMetrics.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#define DEBUG_TYPE "early-ifcvt"
11#include "MachineTraceMetrics.h"
12#include "llvm/CodeGen/MachineBasicBlock.h"
13#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
14#include "llvm/CodeGen/MachineLoopInfo.h"
15#include "llvm/CodeGen/MachineRegisterInfo.h"
16#include "llvm/CodeGen/Passes.h"
17#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/Target/TargetRegisterInfo.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/ADT/PostOrderIterator.h"
22
23using namespace llvm;
24
25char MachineTraceMetrics::ID = 0;
26char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
27
28INITIALIZE_PASS_BEGIN(MachineTraceMetrics,
29 "machine-trace-metrics", "Machine Trace Metrics", false, true)
30INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
31INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
32INITIALIZE_PASS_END(MachineTraceMetrics,
33 "machine-trace-metrics", "Machine Trace Metrics", false, true)
34
35MachineTraceMetrics::MachineTraceMetrics()
36 : MachineFunctionPass(ID), TII(0), TRI(0), MRI(0), Loops(0) {
37 std::fill(Ensembles, array_endof(Ensembles), (Ensemble*)0);
38}
39
40void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesAll();
42 AU.addRequired<MachineBranchProbabilityInfo>();
43 AU.addRequired<MachineLoopInfo>();
44 MachineFunctionPass::getAnalysisUsage(AU);
45}
46
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +000047bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
48 MF = &Func;
49 TII = MF->getTarget().getInstrInfo();
50 TRI = MF->getTarget().getRegisterInfo();
51 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000052 Loops = &getAnalysis<MachineLoopInfo>();
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +000053 BlockInfo.resize(MF->getNumBlockIDs());
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000054 return false;
55}
56
57void MachineTraceMetrics::releaseMemory() {
58 BlockInfo.clear();
59 for (unsigned i = 0; i != TS_NumStrategies; ++i) {
60 delete Ensembles[i];
61 Ensembles[i] = 0;
62 }
63}
64
65//===----------------------------------------------------------------------===//
66// Fixed block information
67//===----------------------------------------------------------------------===//
68//
69// The number of instructions in a basic block and the CPU resources used by
70// those instructions don't depend on any given trace strategy.
71
72/// Is MI an instruction that should be considered free because it will likely
73/// be eliminated by later passes?
74static bool isFree(const MachineInstr *MI) {
75 switch(MI->getOpcode()) {
76 default: return false;
77 case TargetOpcode::PHI:
78 case TargetOpcode::PROLOG_LABEL:
79 case TargetOpcode::EH_LABEL:
80 case TargetOpcode::GC_LABEL:
81 case TargetOpcode::KILL:
82 case TargetOpcode::EXTRACT_SUBREG:
83 case TargetOpcode::INSERT_SUBREG:
84 case TargetOpcode::IMPLICIT_DEF:
85 case TargetOpcode::SUBREG_TO_REG:
86 case TargetOpcode::COPY_TO_REGCLASS:
87 case TargetOpcode::DBG_VALUE:
88 case TargetOpcode::REG_SEQUENCE:
89 case TargetOpcode::COPY:
90 return true;
91 }
92}
93
94/// Compute the resource usage in basic block MBB.
95const MachineTraceMetrics::FixedBlockInfo*
96MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
97 assert(MBB && "No basic block");
98 FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()];
99 if (FBI->hasResources())
100 return FBI;
101
102 // Compute resource usage in the block.
103 // FIXME: Compute per-functional unit counts.
104 FBI->HasCalls = false;
105 unsigned InstrCount = 0;
106 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
107 I != E; ++I) {
108 const MachineInstr *MI = I;
109 if (isFree(MI))
110 continue;
111 ++InstrCount;
112 if (MI->isCall())
113 FBI->HasCalls = true;
114 }
115 FBI->InstrCount = InstrCount;
116 return FBI;
117}
118
119//===----------------------------------------------------------------------===//
120// Ensemble utility functions
121//===----------------------------------------------------------------------===//
122
123MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
124 : CT(*ct) {
125 BlockInfo.resize(CT.BlockInfo.size());
126}
127
128// Virtual destructor serves as an anchor.
129MachineTraceMetrics::Ensemble::~Ensemble() {}
130
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000131const MachineLoop*
132MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) const {
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000133 return CT.Loops->getLoopFor(MBB);
134}
135
136// Update resource-related information in the TraceBlockInfo for MBB.
137// Only update resources related to the trace above MBB.
138void MachineTraceMetrics::Ensemble::
139computeDepthResources(const MachineBasicBlock *MBB) {
140 TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
141
142 // Compute resources from trace above. The top block is simple.
143 if (!TBI->Pred) {
144 TBI->InstrDepth = 0;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000145 TBI->Head = MBB->getNumber();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000146 return;
147 }
148
149 // Compute from the block above. A post-order traversal ensures the
150 // predecessor is always computed first.
151 TraceBlockInfo *PredTBI = &BlockInfo[TBI->Pred->getNumber()];
152 assert(PredTBI->hasValidDepth() && "Trace above has not been computed yet");
153 const FixedBlockInfo *PredFBI = CT.getResources(TBI->Pred);
154 TBI->InstrDepth = PredTBI->InstrDepth + PredFBI->InstrCount;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000155 TBI->Head = PredTBI->Head;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000156}
157
158// Update resource-related information in the TraceBlockInfo for MBB.
159// Only update resources related to the trace below MBB.
160void MachineTraceMetrics::Ensemble::
161computeHeightResources(const MachineBasicBlock *MBB) {
162 TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
163
164 // Compute resources for the current block.
165 TBI->InstrHeight = CT.getResources(MBB)->InstrCount;
166
167 // The trace tail is done.
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000168 if (!TBI->Succ) {
169 TBI->Tail = MBB->getNumber();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000170 return;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000171 }
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000172
173 // Compute from the block below. A post-order traversal ensures the
174 // predecessor is always computed first.
175 TraceBlockInfo *SuccTBI = &BlockInfo[TBI->Succ->getNumber()];
176 assert(SuccTBI->hasValidHeight() && "Trace below has not been computed yet");
177 TBI->InstrHeight += SuccTBI->InstrHeight;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000178 TBI->Tail = SuccTBI->Tail;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000179}
180
181// Check if depth resources for MBB are valid and return the TBI.
182// Return NULL if the resources have been invalidated.
183const MachineTraceMetrics::TraceBlockInfo*
184MachineTraceMetrics::Ensemble::
185getDepthResources(const MachineBasicBlock *MBB) const {
186 const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
187 return TBI->hasValidDepth() ? TBI : 0;
188}
189
190// Check if height resources for MBB are valid and return the TBI.
191// Return NULL if the resources have been invalidated.
192const MachineTraceMetrics::TraceBlockInfo*
193MachineTraceMetrics::Ensemble::
194getHeightResources(const MachineBasicBlock *MBB) const {
195 const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
196 return TBI->hasValidHeight() ? TBI : 0;
197}
198
199//===----------------------------------------------------------------------===//
200// Trace Selection Strategies
201//===----------------------------------------------------------------------===//
202//
203// A trace selection strategy is implemented as a sub-class of Ensemble. The
204// trace through a block B is computed by two DFS traversals of the CFG
205// starting from B. One upwards, and one downwards. During the upwards DFS,
206// pickTracePred() is called on the post-ordered blocks. During the downwards
207// DFS, pickTraceSucc() is called in a post-order.
208//
209
210// MinInstrCountEnsemble - Pick the trace that executes the least number of
211// instructions.
212namespace {
213class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000214 const char *getName() const { return "MinInstr"; }
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000215 const MachineBasicBlock *pickTracePred(const MachineBasicBlock*);
216 const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*);
217
218public:
219 MinInstrCountEnsemble(MachineTraceMetrics *ct)
220 : MachineTraceMetrics::Ensemble(ct) {}
221};
222}
223
224// Select the preferred predecessor for MBB.
225const MachineBasicBlock*
226MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) {
227 if (MBB->pred_empty())
228 return 0;
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000229 const MachineLoop *CurLoop = getLoopFor(MBB);
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000230 // Don't leave loops, and never follow back-edges.
231 if (CurLoop && MBB == CurLoop->getHeader())
232 return 0;
233 unsigned CurCount = CT.getResources(MBB)->InstrCount;
234 const MachineBasicBlock *Best = 0;
235 unsigned BestDepth = 0;
236 for (MachineBasicBlock::const_pred_iterator
237 I = MBB->pred_begin(), E = MBB->pred_end(); I != E; ++I) {
238 const MachineBasicBlock *Pred = *I;
239 const MachineTraceMetrics::TraceBlockInfo *PredTBI =
240 getDepthResources(Pred);
241 // Ignore invalidated predecessors. This never happens on the first scan,
242 // but if we rejected this predecessor earlier, it won't be revalidated.
243 if (!PredTBI)
244 continue;
245 // Don't consider predecessors in other loops.
246 if (getLoopFor(Pred) != CurLoop)
247 continue;
248 // Pick the predecessor that would give this block the smallest InstrDepth.
249 unsigned Depth = PredTBI->InstrDepth + CurCount;
250 if (!Best || Depth < BestDepth)
251 Best = Pred, BestDepth = Depth;
252 }
253 return Best;
254}
255
256// Select the preferred successor for MBB.
257const MachineBasicBlock*
258MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
259 if (MBB->pred_empty())
260 return 0;
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000261 const MachineLoop *CurLoop = getLoopFor(MBB);
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000262 const MachineBasicBlock *Best = 0;
263 unsigned BestHeight = 0;
264 for (MachineBasicBlock::const_succ_iterator
265 I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I) {
266 const MachineBasicBlock *Succ = *I;
267 const MachineTraceMetrics::TraceBlockInfo *SuccTBI =
268 getHeightResources(Succ);
269 // Ignore invalidated successors.
270 if (!SuccTBI)
271 continue;
272 // Don't consider back-edges.
273 if (CurLoop && Succ == CurLoop->getHeader())
274 continue;
275 // Don't consider successors in other loops.
276 if (getLoopFor(Succ) != CurLoop)
277 continue;
278 // Pick the successor that would give this block the smallest InstrHeight.
279 unsigned Height = SuccTBI->InstrHeight;
280 if (!Best || Height < BestHeight)
281 Best = Succ, BestHeight = Height;
282 }
283 return Best;
284}
285
286// Get an Ensemble sub-class for the requested trace strategy.
287MachineTraceMetrics::Ensemble *
288MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) {
289 assert(strategy < TS_NumStrategies && "Invalid trace strategy enum");
290 Ensemble *&E = Ensembles[strategy];
291 if (E)
292 return E;
293
294 // Allocate new Ensemble on demand.
295 switch (strategy) {
296 case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this));
297 default: llvm_unreachable("Invalid trace strategy enum");
298 }
299}
300
301void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
302 DEBUG(dbgs() << "Invalidate traces through BB#" << MBB->getNumber() << '\n');
303 BlockInfo[MBB->getNumber()].invalidate();
304 for (unsigned i = 0; i != TS_NumStrategies; ++i)
305 if (Ensembles[i])
306 Ensembles[i]->invalidate(MBB);
307}
308
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000309void MachineTraceMetrics::verify() const {
310#ifndef NDEBUG
311 assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
312 for (unsigned i = 0; i != TS_NumStrategies; ++i)
313 if (Ensembles[i])
314 Ensembles[i]->verify();
315#endif
316}
317
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000318//===----------------------------------------------------------------------===//
319// Trace building
320//===----------------------------------------------------------------------===//
321//
322// Traces are built by two CFG traversals. To avoid recomputing too much, use a
323// set abstraction that confines the search to the current loop, and doesn't
324// revisit blocks.
325
326namespace {
327struct LoopBounds {
328 MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks;
329 const MachineLoopInfo *Loops;
330 const MachineLoop *CurLoop;
331 bool Downward;
332 LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
333 const MachineLoopInfo *loops, const MachineLoop *curloop)
334 : Blocks(blocks), Loops(loops), CurLoop(curloop), Downward(false) {}
335};
336}
337
338// Specialize po_iterator_storage in order to prune the post-order traversal so
339// it is limited to the current loop and doesn't traverse the loop back edges.
340namespace llvm {
341template<>
342class po_iterator_storage<LoopBounds, true> {
343 LoopBounds &LB;
344public:
345 po_iterator_storage(LoopBounds &lb) : LB(lb) {}
346 void finishPostorder(const MachineBasicBlock*) {}
347
348 bool insertEdge(const MachineBasicBlock *From, const MachineBasicBlock *To) {
349 // Skip already visited To blocks.
350 MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()];
351 if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth())
352 return false;
353 // Don't follow CurLoop backedges.
354 if (LB.CurLoop && (LB.Downward ? To : From) == LB.CurLoop->getHeader())
355 return false;
356 // Don't leave CurLoop.
357 if (LB.Loops->getLoopFor(To) != LB.CurLoop)
358 return false;
359 // This is a new block. The PO traversal will compute height/depth
360 // resources, causing us to reject new edges to To. This only works because
361 // we reject back-edges, so the CFG is cycle-free.
362 return true;
363 }
364};
365}
366
367/// Compute the trace through MBB.
368void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
369 DEBUG(dbgs() << "Computing " << getName() << " trace through BB#"
370 << MBB->getNumber() << '\n');
371 // Set up loop bounds for the backwards post-order traversal.
372 LoopBounds Bounds(BlockInfo, CT.Loops, getLoopFor(MBB));
373
374 // Run an upwards post-order search for the trace start.
375 Bounds.Downward = false;
376 typedef ipo_ext_iterator<const MachineBasicBlock*, LoopBounds> UpwardPO;
377 for (UpwardPO I = ipo_ext_begin(MBB, Bounds), E = ipo_ext_end(MBB, Bounds);
378 I != E; ++I) {
379 DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": ");
380 TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
381 // All the predecessors have been visited, pick the preferred one.
382 TBI.Pred = pickTracePred(*I);
383 DEBUG({
384 if (TBI.Pred)
385 dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
386 else
387 dbgs() << "null\n";
388 });
389 // The trace leading to I is now known, compute the depth resources.
390 computeDepthResources(*I);
391 }
392
393 // Run a downwards post-order search for the trace end.
394 Bounds.Downward = true;
395 typedef po_ext_iterator<const MachineBasicBlock*, LoopBounds> DownwardPO;
396 for (DownwardPO I = po_ext_begin(MBB, Bounds), E = po_ext_end(MBB, Bounds);
397 I != E; ++I) {
398 DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": ");
399 TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
400 // All the successors have been visited, pick the preferred one.
Jakob Stoklund Olesen0fc44862012-07-26 19:42:56 +0000401 TBI.Succ = pickTraceSucc(*I);
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000402 DEBUG({
403 if (TBI.Pred)
404 dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
405 else
406 dbgs() << "null\n";
407 });
408 // The trace leaving I is now known, compute the height resources.
409 computeHeightResources(*I);
410 }
411}
412
413/// Invalidate traces through BadMBB.
414void
415MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
416 SmallVector<const MachineBasicBlock*, 16> WorkList;
417 TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()];
418
419 // Invalidate height resources of blocks above MBB.
420 if (BadTBI.hasValidHeight()) {
421 BadTBI.invalidateHeight();
422 WorkList.push_back(BadMBB);
423 do {
424 const MachineBasicBlock *MBB = WorkList.pop_back_val();
425 DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
426 << " height.\n");
427 // Find any MBB predecessors that have MBB as their preferred successor.
428 // They are the only ones that need to be invalidated.
429 for (MachineBasicBlock::const_pred_iterator
430 I = MBB->pred_begin(), E = MBB->pred_end(); I != E; ++I) {
431 TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000432 if (!TBI.hasValidHeight())
433 continue;
434 if (TBI.Succ == MBB) {
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000435 TBI.invalidateHeight();
436 WorkList.push_back(*I);
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000437 continue;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000438 }
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000439 // Verify that TBI.Succ is actually a *I successor.
440 assert((!TBI.Succ || (*I)->isSuccessor(TBI.Succ)) && "CFG changed");
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000441 }
442 } while (!WorkList.empty());
443 }
444
445 // Invalidate depth resources of blocks below MBB.
446 if (BadTBI.hasValidDepth()) {
447 BadTBI.invalidateDepth();
448 WorkList.push_back(BadMBB);
449 do {
450 const MachineBasicBlock *MBB = WorkList.pop_back_val();
451 DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
452 << " depth.\n");
453 // Find any MBB successors that have MBB as their preferred predecessor.
454 // They are the only ones that need to be invalidated.
455 for (MachineBasicBlock::const_succ_iterator
456 I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I) {
457 TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000458 if (!TBI.hasValidDepth())
459 continue;
460 if (TBI.Pred == MBB) {
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000461 TBI.invalidateDepth();
462 WorkList.push_back(*I);
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000463 continue;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000464 }
Jakob Stoklund Olesenee31ae12012-07-30 17:36:49 +0000465 // Verify that TBI.Pred is actually a *I predecessor.
466 assert((!TBI.Pred || (*I)->isPredecessor(TBI.Pred)) && "CFG changed");
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000467 }
468 } while (!WorkList.empty());
469 }
470}
471
Jakob Stoklund Olesena1b2bf72012-07-30 18:34:11 +0000472void MachineTraceMetrics::Ensemble::verify() const {
473#ifndef NDEBUG
474 assert(BlockInfo.size() == CT.MF->getNumBlockIDs() &&
475 "Outdated BlockInfo size");
476 for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
477 const TraceBlockInfo &TBI = BlockInfo[Num];
478 if (TBI.hasValidDepth() && TBI.Pred) {
479 const MachineBasicBlock *MBB = CT.MF->getBlockNumbered(Num);
480 assert(MBB->isPredecessor(TBI.Pred) && "CFG doesn't match trace");
481 assert(BlockInfo[TBI.Pred->getNumber()].hasValidDepth() &&
482 "Trace is broken, depth should have been invalidated.");
483 const MachineLoop *Loop = getLoopFor(MBB);
484 assert(!(Loop && MBB == Loop->getHeader()) && "Trace contains backedge");
485 }
486 if (TBI.hasValidHeight() && TBI.Succ) {
487 const MachineBasicBlock *MBB = CT.MF->getBlockNumbered(Num);
488 assert(MBB->isSuccessor(TBI.Succ) && "CFG doesn't match trace");
489 assert(BlockInfo[TBI.Succ->getNumber()].hasValidHeight() &&
490 "Trace is broken, height should have been invalidated.");
491 const MachineLoop *Loop = getLoopFor(MBB);
492 const MachineLoop *SuccLoop = getLoopFor(TBI.Succ);
493 assert(!(Loop && Loop == SuccLoop && TBI.Succ == Loop->getHeader()) &&
494 "Trace contains backedge");
495 }
496 }
497#endif
498}
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000499
500MachineTraceMetrics::Trace
501MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) {
502 // FIXME: Check cache tags, recompute as needed.
503 computeTrace(MBB);
504 return Trace(*this, BlockInfo[MBB->getNumber()]);
505}
506
Jakob Stoklund Olesen08f6ef62012-07-27 23:58:38 +0000507void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
508 OS << getName() << " ensemble:\n";
509 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
510 OS << " BB#" << i << '\t';
511 BlockInfo[i].print(OS);
512 OS << '\n';
513 }
514}
515
516void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const {
517 if (hasValidDepth()) {
518 OS << "depth=" << InstrDepth;
519 if (Pred)
520 OS << " pred=BB#" << Pred->getNumber();
521 else
522 OS << " pred=null";
523 OS << " head=BB#" << Head;
524 } else
525 OS << "depth invalid";
526 OS << ", ";
527 if (hasValidHeight()) {
528 OS << "height=" << InstrHeight;
529 if (Succ)
530 OS << " succ=BB#" << Succ->getNumber();
531 else
532 OS << " succ=null";
533 OS << " tail=BB#" << Tail;
534 } else
535 OS << "height invalid";
536}
537
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000538void MachineTraceMetrics::Trace::print(raw_ostream &OS) const {
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000539 unsigned MBBNum = &TBI - &TE.BlockInfo[0];
540
541 OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum
542 << " --> BB#" << TBI.Tail << ':';
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000543 if (TBI.hasValidHeight() && TBI.hasValidDepth())
544 OS << ' ' << getInstrCount() << " instrs.";
545
546 const MachineTraceMetrics::TraceBlockInfo *Block = &TBI;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000547 OS << "\nBB#" << MBBNum;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000548 while (Block->hasValidDepth() && Block->Pred) {
549 unsigned Num = Block->Pred->getNumber();
550 OS << " <- BB#" << Num;
551 Block = &TE.BlockInfo[Num];
552 }
553
554 Block = &TBI;
Jakob Stoklund Olesen0271a5f2012-07-27 23:58:36 +0000555 OS << "\n ";
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000556 while (Block->hasValidHeight() && Block->Succ) {
557 unsigned Num = Block->Succ->getNumber();
558 OS << " -> BB#" << Num;
559 Block = &TE.BlockInfo[Num];
560 }
561 OS << '\n';
562}