1 //===- lib/CodeGen/MachineTraceMetrics.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 char MachineTraceMetrics::ID = 0;
26 char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
28 INITIALIZE_PASS_BEGIN(MachineTraceMetrics,
29 "machine-trace-metrics", "Machine Trace Metrics", false, true)
30 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
31 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
32 INITIALIZE_PASS_END(MachineTraceMetrics,
33 "machine-trace-metrics", "Machine Trace Metrics", false, true)
35 MachineTraceMetrics::MachineTraceMetrics()
36 : MachineFunctionPass(ID), TII(0), TRI(0), MRI(0), Loops(0) {
37 std::fill(Ensembles, array_endof(Ensembles), (Ensemble*)0);
40 void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<MachineBranchProbabilityInfo>();
43 AU.addRequired<MachineLoopInfo>();
44 MachineFunctionPass::getAnalysisUsage(AU);
47 bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &MF) {
48 TII = MF.getTarget().getInstrInfo();
49 TRI = MF.getTarget().getRegisterInfo();
50 MRI = &MF.getRegInfo();
51 Loops = &getAnalysis<MachineLoopInfo>();
52 unsigned NumBlocks = MF.getNumBlockIDs();
53 BlockInfo.resize(NumBlocks);
57 void MachineTraceMetrics::releaseMemory() {
59 for (unsigned i = 0; i != TS_NumStrategies; ++i) {
65 //===----------------------------------------------------------------------===//
66 // Fixed block information
67 //===----------------------------------------------------------------------===//
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.
72 /// Is MI an instruction that should be considered free because it will likely
73 /// be eliminated by later passes?
74 static 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:
94 /// Compute the resource usage in basic block MBB.
95 const MachineTraceMetrics::FixedBlockInfo*
96 MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
97 assert(MBB && "No basic block");
98 FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()];
99 if (FBI->hasResources())
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();
108 const MachineInstr *MI = I;
113 FBI->HasCalls = true;
115 FBI->InstrCount = InstrCount;
119 //===----------------------------------------------------------------------===//
120 // Ensemble utility functions
121 //===----------------------------------------------------------------------===//
123 MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
125 BlockInfo.resize(CT.BlockInfo.size());
128 // Virtual destructor serves as an anchor.
129 MachineTraceMetrics::Ensemble::~Ensemble() {}
132 MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) {
133 return CT.Loops->getLoopFor(MBB);
136 // Update resource-related information in the TraceBlockInfo for MBB.
137 // Only update resources related to the trace above MBB.
138 void MachineTraceMetrics::Ensemble::
139 computeDepthResources(const MachineBasicBlock *MBB) {
140 TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
142 // Compute resources from trace above. The top block is simple.
145 TBI->Head = MBB->getNumber();
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;
155 TBI->Head = PredTBI->Head;
158 // Update resource-related information in the TraceBlockInfo for MBB.
159 // Only update resources related to the trace below MBB.
160 void MachineTraceMetrics::Ensemble::
161 computeHeightResources(const MachineBasicBlock *MBB) {
162 TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
164 // Compute resources for the current block.
165 TBI->InstrHeight = CT.getResources(MBB)->InstrCount;
167 // The trace tail is done.
169 TBI->Tail = MBB->getNumber();
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;
178 TBI->Tail = SuccTBI->Tail;
181 // Check if depth resources for MBB are valid and return the TBI.
182 // Return NULL if the resources have been invalidated.
183 const MachineTraceMetrics::TraceBlockInfo*
184 MachineTraceMetrics::Ensemble::
185 getDepthResources(const MachineBasicBlock *MBB) const {
186 const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
187 return TBI->hasValidDepth() ? TBI : 0;
190 // Check if height resources for MBB are valid and return the TBI.
191 // Return NULL if the resources have been invalidated.
192 const MachineTraceMetrics::TraceBlockInfo*
193 MachineTraceMetrics::Ensemble::
194 getHeightResources(const MachineBasicBlock *MBB) const {
195 const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
196 return TBI->hasValidHeight() ? TBI : 0;
199 //===----------------------------------------------------------------------===//
200 // Trace Selection Strategies
201 //===----------------------------------------------------------------------===//
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.
210 // MinInstrCountEnsemble - Pick the trace that executes the least number of
213 class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
214 const char *getName() const { return "MinInstr"; }
215 const MachineBasicBlock *pickTracePred(const MachineBasicBlock*);
216 const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*);
219 MinInstrCountEnsemble(MachineTraceMetrics *ct)
220 : MachineTraceMetrics::Ensemble(ct) {}
224 // Select the preferred predecessor for MBB.
225 const MachineBasicBlock*
226 MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) {
227 if (MBB->pred_empty())
229 MachineLoop *CurLoop = getLoopFor(MBB);
230 // Don't leave loops, and never follow back-edges.
231 if (CurLoop && MBB == CurLoop->getHeader())
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.
245 // Don't consider predecessors in other loops.
246 if (getLoopFor(Pred) != CurLoop)
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;
256 // Select the preferred successor for MBB.
257 const MachineBasicBlock*
258 MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
259 if (MBB->pred_empty())
261 MachineLoop *CurLoop = getLoopFor(MBB);
262 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.
272 // Don't consider back-edges.
273 if (CurLoop && Succ == CurLoop->getHeader())
275 // Don't consider successors in other loops.
276 if (getLoopFor(Succ) != CurLoop)
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;
286 // Get an Ensemble sub-class for the requested trace strategy.
287 MachineTraceMetrics::Ensemble *
288 MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) {
289 assert(strategy < TS_NumStrategies && "Invalid trace strategy enum");
290 Ensemble *&E = Ensembles[strategy];
294 // Allocate new Ensemble on demand.
296 case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this));
297 default: llvm_unreachable("Invalid trace strategy enum");
301 void 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)
306 Ensembles[i]->invalidate(MBB);
309 //===----------------------------------------------------------------------===//
311 //===----------------------------------------------------------------------===//
313 // Traces are built by two CFG traversals. To avoid recomputing too much, use a
314 // set abstraction that confines the search to the current loop, and doesn't
319 MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks;
320 const MachineLoopInfo *Loops;
321 const MachineLoop *CurLoop;
323 LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
324 const MachineLoopInfo *loops, const MachineLoop *curloop)
325 : Blocks(blocks), Loops(loops), CurLoop(curloop), Downward(false) {}
329 // Specialize po_iterator_storage in order to prune the post-order traversal so
330 // it is limited to the current loop and doesn't traverse the loop back edges.
333 class po_iterator_storage<LoopBounds, true> {
336 po_iterator_storage(LoopBounds &lb) : LB(lb) {}
337 void finishPostorder(const MachineBasicBlock*) {}
339 bool insertEdge(const MachineBasicBlock *From, const MachineBasicBlock *To) {
340 // Skip already visited To blocks.
341 MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()];
342 if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth())
344 // Don't follow CurLoop backedges.
345 if (LB.CurLoop && (LB.Downward ? To : From) == LB.CurLoop->getHeader())
347 // Don't leave CurLoop.
348 if (LB.Loops->getLoopFor(To) != LB.CurLoop)
350 // This is a new block. The PO traversal will compute height/depth
351 // resources, causing us to reject new edges to To. This only works because
352 // we reject back-edges, so the CFG is cycle-free.
358 /// Compute the trace through MBB.
359 void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
360 DEBUG(dbgs() << "Computing " << getName() << " trace through BB#"
361 << MBB->getNumber() << '\n');
362 // Set up loop bounds for the backwards post-order traversal.
363 LoopBounds Bounds(BlockInfo, CT.Loops, getLoopFor(MBB));
365 // Run an upwards post-order search for the trace start.
366 Bounds.Downward = false;
367 typedef ipo_ext_iterator<const MachineBasicBlock*, LoopBounds> UpwardPO;
368 for (UpwardPO I = ipo_ext_begin(MBB, Bounds), E = ipo_ext_end(MBB, Bounds);
370 DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": ");
371 TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
372 // All the predecessors have been visited, pick the preferred one.
373 TBI.Pred = pickTracePred(*I);
376 dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
380 // The trace leading to I is now known, compute the depth resources.
381 computeDepthResources(*I);
384 // Run a downwards post-order search for the trace end.
385 Bounds.Downward = true;
386 typedef po_ext_iterator<const MachineBasicBlock*, LoopBounds> DownwardPO;
387 for (DownwardPO I = po_ext_begin(MBB, Bounds), E = po_ext_end(MBB, Bounds);
389 DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": ");
390 TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
391 // All the successors have been visited, pick the preferred one.
392 TBI.Succ = pickTraceSucc(*I);
395 dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
399 // The trace leaving I is now known, compute the height resources.
400 computeHeightResources(*I);
404 /// Invalidate traces through BadMBB.
406 MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
407 SmallVector<const MachineBasicBlock*, 16> WorkList;
408 TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()];
410 // Invalidate height resources of blocks above MBB.
411 if (BadTBI.hasValidHeight()) {
412 BadTBI.invalidateHeight();
413 WorkList.push_back(BadMBB);
415 const MachineBasicBlock *MBB = WorkList.pop_back_val();
416 DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
418 // Find any MBB predecessors that have MBB as their preferred successor.
419 // They are the only ones that need to be invalidated.
420 for (MachineBasicBlock::const_pred_iterator
421 I = MBB->pred_begin(), E = MBB->pred_end(); I != E; ++I) {
422 TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
423 if (!TBI.hasValidHeight())
425 if (TBI.Succ == MBB) {
426 TBI.invalidateHeight();
427 WorkList.push_back(*I);
430 // Verify that TBI.Succ is actually a *I successor.
431 assert((!TBI.Succ || (*I)->isSuccessor(TBI.Succ)) && "CFG changed");
433 } while (!WorkList.empty());
436 // Invalidate depth resources of blocks below MBB.
437 if (BadTBI.hasValidDepth()) {
438 BadTBI.invalidateDepth();
439 WorkList.push_back(BadMBB);
441 const MachineBasicBlock *MBB = WorkList.pop_back_val();
442 DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
444 // Find any MBB successors that have MBB as their preferred predecessor.
445 // They are the only ones that need to be invalidated.
446 for (MachineBasicBlock::const_succ_iterator
447 I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I) {
448 TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
449 if (!TBI.hasValidDepth())
451 if (TBI.Pred == MBB) {
452 TBI.invalidateDepth();
453 WorkList.push_back(*I);
456 // Verify that TBI.Pred is actually a *I predecessor.
457 assert((!TBI.Pred || (*I)->isPredecessor(TBI.Pred)) && "CFG changed");
459 } while (!WorkList.empty());
464 MachineTraceMetrics::Trace
465 MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) {
466 // FIXME: Check cache tags, recompute as needed.
468 return Trace(*this, BlockInfo[MBB->getNumber()]);
471 void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
472 OS << getName() << " ensemble:\n";
473 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
474 OS << " BB#" << i << '\t';
475 BlockInfo[i].print(OS);
480 void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const {
481 if (hasValidDepth()) {
482 OS << "depth=" << InstrDepth;
484 OS << " pred=BB#" << Pred->getNumber();
487 OS << " head=BB#" << Head;
489 OS << "depth invalid";
491 if (hasValidHeight()) {
492 OS << "height=" << InstrHeight;
494 OS << " succ=BB#" << Succ->getNumber();
497 OS << " tail=BB#" << Tail;
499 OS << "height invalid";
502 void MachineTraceMetrics::Trace::print(raw_ostream &OS) const {
503 unsigned MBBNum = &TBI - &TE.BlockInfo[0];
505 OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum
506 << " --> BB#" << TBI.Tail << ':';
507 if (TBI.hasValidHeight() && TBI.hasValidDepth())
508 OS << ' ' << getInstrCount() << " instrs.";
510 const MachineTraceMetrics::TraceBlockInfo *Block = &TBI;
511 OS << "\nBB#" << MBBNum;
512 while (Block->hasValidDepth() && Block->Pred) {
513 unsigned Num = Block->Pred->getNumber();
514 OS << " <- BB#" << Num;
515 Block = &TE.BlockInfo[Num];
520 while (Block->hasValidHeight() && Block->Succ) {
521 unsigned Num = Block->Succ->getNumber();
522 OS << " -> BB#" << Num;
523 Block = &TE.BlockInfo[Num];