2012-07-26 18:38:11 +00:00
|
|
|
//===- lib/CodeGen/MachineTraceMetrics.cpp ----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "early-ifcvt"
|
|
|
|
#include "MachineTraceMetrics.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char MachineTraceMetrics::ID = 0;
|
|
|
|
char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(MachineTraceMetrics,
|
|
|
|
"machine-trace-metrics", "Machine Trace Metrics", false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_END(MachineTraceMetrics,
|
|
|
|
"machine-trace-metrics", "Machine Trace Metrics", false, true)
|
|
|
|
|
|
|
|
MachineTraceMetrics::MachineTraceMetrics()
|
2012-07-30 23:15:12 +00:00
|
|
|
: MachineFunctionPass(ID), MF(0), TII(0), TRI(0), MRI(0), Loops(0) {
|
2012-07-26 18:38:11 +00:00
|
|
|
std::fill(Ensembles, array_endof(Ensembles), (Ensemble*)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:34:11 +00:00
|
|
|
bool MachineTraceMetrics::runOnMachineFunction(MachineFunction &Func) {
|
|
|
|
MF = &Func;
|
|
|
|
TII = MF->getTarget().getInstrInfo();
|
|
|
|
TRI = MF->getTarget().getRegisterInfo();
|
|
|
|
MRI = &MF->getRegInfo();
|
2012-07-26 18:38:11 +00:00
|
|
|
Loops = &getAnalysis<MachineLoopInfo>();
|
2012-07-30 18:34:11 +00:00
|
|
|
BlockInfo.resize(MF->getNumBlockIDs());
|
2012-07-26 18:38:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineTraceMetrics::releaseMemory() {
|
2012-07-30 23:15:12 +00:00
|
|
|
MF = 0;
|
2012-07-26 18:38:11 +00:00
|
|
|
BlockInfo.clear();
|
|
|
|
for (unsigned i = 0; i != TS_NumStrategies; ++i) {
|
|
|
|
delete Ensembles[i];
|
|
|
|
Ensembles[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Fixed block information
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The number of instructions in a basic block and the CPU resources used by
|
|
|
|
// those instructions don't depend on any given trace strategy.
|
|
|
|
|
|
|
|
/// Compute the resource usage in basic block MBB.
|
|
|
|
const MachineTraceMetrics::FixedBlockInfo*
|
|
|
|
MachineTraceMetrics::getResources(const MachineBasicBlock *MBB) {
|
|
|
|
assert(MBB && "No basic block");
|
|
|
|
FixedBlockInfo *FBI = &BlockInfo[MBB->getNumber()];
|
|
|
|
if (FBI->hasResources())
|
|
|
|
return FBI;
|
|
|
|
|
|
|
|
// Compute resource usage in the block.
|
|
|
|
// FIXME: Compute per-functional unit counts.
|
|
|
|
FBI->HasCalls = false;
|
|
|
|
unsigned InstrCount = 0;
|
|
|
|
for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const MachineInstr *MI = I;
|
2012-07-30 18:34:14 +00:00
|
|
|
if (MI->isTransient())
|
2012-07-26 18:38:11 +00:00
|
|
|
continue;
|
|
|
|
++InstrCount;
|
|
|
|
if (MI->isCall())
|
|
|
|
FBI->HasCalls = true;
|
|
|
|
}
|
|
|
|
FBI->InstrCount = InstrCount;
|
|
|
|
return FBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Ensemble utility functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
MachineTraceMetrics::Ensemble::Ensemble(MachineTraceMetrics *ct)
|
|
|
|
: CT(*ct) {
|
|
|
|
BlockInfo.resize(CT.BlockInfo.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Virtual destructor serves as an anchor.
|
|
|
|
MachineTraceMetrics::Ensemble::~Ensemble() {}
|
|
|
|
|
2012-07-30 18:34:11 +00:00
|
|
|
const MachineLoop*
|
|
|
|
MachineTraceMetrics::Ensemble::getLoopFor(const MachineBasicBlock *MBB) const {
|
2012-07-26 18:38:11 +00:00
|
|
|
return CT.Loops->getLoopFor(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update resource-related information in the TraceBlockInfo for MBB.
|
|
|
|
// Only update resources related to the trace above MBB.
|
|
|
|
void MachineTraceMetrics::Ensemble::
|
|
|
|
computeDepthResources(const MachineBasicBlock *MBB) {
|
|
|
|
TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
|
|
|
|
|
|
|
|
// Compute resources from trace above. The top block is simple.
|
|
|
|
if (!TBI->Pred) {
|
|
|
|
TBI->InstrDepth = 0;
|
2012-07-27 23:58:36 +00:00
|
|
|
TBI->Head = MBB->getNumber();
|
2012-07-26 18:38:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute from the block above. A post-order traversal ensures the
|
|
|
|
// predecessor is always computed first.
|
|
|
|
TraceBlockInfo *PredTBI = &BlockInfo[TBI->Pred->getNumber()];
|
|
|
|
assert(PredTBI->hasValidDepth() && "Trace above has not been computed yet");
|
|
|
|
const FixedBlockInfo *PredFBI = CT.getResources(TBI->Pred);
|
|
|
|
TBI->InstrDepth = PredTBI->InstrDepth + PredFBI->InstrCount;
|
2012-07-27 23:58:36 +00:00
|
|
|
TBI->Head = PredTBI->Head;
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update resource-related information in the TraceBlockInfo for MBB.
|
|
|
|
// Only update resources related to the trace below MBB.
|
|
|
|
void MachineTraceMetrics::Ensemble::
|
|
|
|
computeHeightResources(const MachineBasicBlock *MBB) {
|
|
|
|
TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
|
|
|
|
|
|
|
|
// Compute resources for the current block.
|
|
|
|
TBI->InstrHeight = CT.getResources(MBB)->InstrCount;
|
|
|
|
|
|
|
|
// The trace tail is done.
|
2012-07-27 23:58:36 +00:00
|
|
|
if (!TBI->Succ) {
|
|
|
|
TBI->Tail = MBB->getNumber();
|
2012-07-26 18:38:11 +00:00
|
|
|
return;
|
2012-07-27 23:58:36 +00:00
|
|
|
}
|
2012-07-26 18:38:11 +00:00
|
|
|
|
|
|
|
// Compute from the block below. A post-order traversal ensures the
|
|
|
|
// predecessor is always computed first.
|
|
|
|
TraceBlockInfo *SuccTBI = &BlockInfo[TBI->Succ->getNumber()];
|
|
|
|
assert(SuccTBI->hasValidHeight() && "Trace below has not been computed yet");
|
|
|
|
TBI->InstrHeight += SuccTBI->InstrHeight;
|
2012-07-27 23:58:36 +00:00
|
|
|
TBI->Tail = SuccTBI->Tail;
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if depth resources for MBB are valid and return the TBI.
|
|
|
|
// Return NULL if the resources have been invalidated.
|
|
|
|
const MachineTraceMetrics::TraceBlockInfo*
|
|
|
|
MachineTraceMetrics::Ensemble::
|
|
|
|
getDepthResources(const MachineBasicBlock *MBB) const {
|
|
|
|
const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
|
|
|
|
return TBI->hasValidDepth() ? TBI : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if height resources for MBB are valid and return the TBI.
|
|
|
|
// Return NULL if the resources have been invalidated.
|
|
|
|
const MachineTraceMetrics::TraceBlockInfo*
|
|
|
|
MachineTraceMetrics::Ensemble::
|
|
|
|
getHeightResources(const MachineBasicBlock *MBB) const {
|
|
|
|
const TraceBlockInfo *TBI = &BlockInfo[MBB->getNumber()];
|
|
|
|
return TBI->hasValidHeight() ? TBI : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Trace Selection Strategies
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A trace selection strategy is implemented as a sub-class of Ensemble. The
|
|
|
|
// trace through a block B is computed by two DFS traversals of the CFG
|
|
|
|
// starting from B. One upwards, and one downwards. During the upwards DFS,
|
|
|
|
// pickTracePred() is called on the post-ordered blocks. During the downwards
|
|
|
|
// DFS, pickTraceSucc() is called in a post-order.
|
|
|
|
//
|
|
|
|
|
2012-07-30 23:15:10 +00:00
|
|
|
// We never allow traces that leave loops, but we do allow traces to enter
|
|
|
|
// nested loops. We also never allow traces to contain back-edges.
|
|
|
|
//
|
|
|
|
// This means that a loop header can never appear above the center block of a
|
|
|
|
// trace, except as the trace head. Below the center block, loop exiting edges
|
|
|
|
// are banned.
|
|
|
|
//
|
|
|
|
// Return true if an edge from the From loop to the To loop is leaving a loop.
|
|
|
|
// Either of To and From can be null.
|
|
|
|
static bool isExitingLoop(const MachineLoop *From, const MachineLoop *To) {
|
|
|
|
return From && !From->contains(To);
|
|
|
|
}
|
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
// MinInstrCountEnsemble - Pick the trace that executes the least number of
|
|
|
|
// instructions.
|
|
|
|
namespace {
|
|
|
|
class MinInstrCountEnsemble : public MachineTraceMetrics::Ensemble {
|
2012-07-27 23:58:38 +00:00
|
|
|
const char *getName() const { return "MinInstr"; }
|
2012-07-26 18:38:11 +00:00
|
|
|
const MachineBasicBlock *pickTracePred(const MachineBasicBlock*);
|
|
|
|
const MachineBasicBlock *pickTraceSucc(const MachineBasicBlock*);
|
|
|
|
|
|
|
|
public:
|
|
|
|
MinInstrCountEnsemble(MachineTraceMetrics *ct)
|
|
|
|
: MachineTraceMetrics::Ensemble(ct) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the preferred predecessor for MBB.
|
|
|
|
const MachineBasicBlock*
|
|
|
|
MinInstrCountEnsemble::pickTracePred(const MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->pred_empty())
|
|
|
|
return 0;
|
2012-07-30 18:34:11 +00:00
|
|
|
const MachineLoop *CurLoop = getLoopFor(MBB);
|
2012-07-26 18:38:11 +00:00
|
|
|
// Don't leave loops, and never follow back-edges.
|
|
|
|
if (CurLoop && MBB == CurLoop->getHeader())
|
|
|
|
return 0;
|
|
|
|
unsigned CurCount = CT.getResources(MBB)->InstrCount;
|
|
|
|
const MachineBasicBlock *Best = 0;
|
|
|
|
unsigned BestDepth = 0;
|
|
|
|
for (MachineBasicBlock::const_pred_iterator
|
|
|
|
I = MBB->pred_begin(), E = MBB->pred_end(); I != E; ++I) {
|
|
|
|
const MachineBasicBlock *Pred = *I;
|
2012-07-30 21:10:27 +00:00
|
|
|
const MachineTraceMetrics::TraceBlockInfo *PredTBI =
|
|
|
|
getDepthResources(Pred);
|
|
|
|
assert(PredTBI && "Predecessor must be visited first");
|
2012-07-26 18:38:11 +00:00
|
|
|
// Pick the predecessor that would give this block the smallest InstrDepth.
|
|
|
|
unsigned Depth = PredTBI->InstrDepth + CurCount;
|
|
|
|
if (!Best || Depth < BestDepth)
|
|
|
|
Best = Pred, BestDepth = Depth;
|
|
|
|
}
|
|
|
|
return Best;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the preferred successor for MBB.
|
|
|
|
const MachineBasicBlock*
|
|
|
|
MinInstrCountEnsemble::pickTraceSucc(const MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->pred_empty())
|
|
|
|
return 0;
|
2012-07-30 18:34:11 +00:00
|
|
|
const MachineLoop *CurLoop = getLoopFor(MBB);
|
2012-07-26 18:38:11 +00:00
|
|
|
const MachineBasicBlock *Best = 0;
|
|
|
|
unsigned BestHeight = 0;
|
|
|
|
for (MachineBasicBlock::const_succ_iterator
|
|
|
|
I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I) {
|
|
|
|
const MachineBasicBlock *Succ = *I;
|
|
|
|
// Don't consider back-edges.
|
|
|
|
if (CurLoop && Succ == CurLoop->getHeader())
|
|
|
|
continue;
|
2012-07-30 23:15:10 +00:00
|
|
|
// Don't consider successors exiting CurLoop.
|
|
|
|
if (isExitingLoop(CurLoop, getLoopFor(Succ)))
|
2012-07-26 18:38:11 +00:00
|
|
|
continue;
|
2012-07-30 21:10:27 +00:00
|
|
|
const MachineTraceMetrics::TraceBlockInfo *SuccTBI =
|
|
|
|
getHeightResources(Succ);
|
|
|
|
assert(SuccTBI && "Successor must be visited first");
|
2012-07-26 18:38:11 +00:00
|
|
|
// Pick the successor that would give this block the smallest InstrHeight.
|
|
|
|
unsigned Height = SuccTBI->InstrHeight;
|
|
|
|
if (!Best || Height < BestHeight)
|
|
|
|
Best = Succ, BestHeight = Height;
|
|
|
|
}
|
|
|
|
return Best;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an Ensemble sub-class for the requested trace strategy.
|
|
|
|
MachineTraceMetrics::Ensemble *
|
|
|
|
MachineTraceMetrics::getEnsemble(MachineTraceMetrics::Strategy strategy) {
|
|
|
|
assert(strategy < TS_NumStrategies && "Invalid trace strategy enum");
|
|
|
|
Ensemble *&E = Ensembles[strategy];
|
|
|
|
if (E)
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Allocate new Ensemble on demand.
|
|
|
|
switch (strategy) {
|
|
|
|
case TS_MinInstrCount: return (E = new MinInstrCountEnsemble(this));
|
|
|
|
default: llvm_unreachable("Invalid trace strategy enum");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineTraceMetrics::invalidate(const MachineBasicBlock *MBB) {
|
|
|
|
DEBUG(dbgs() << "Invalidate traces through BB#" << MBB->getNumber() << '\n');
|
|
|
|
BlockInfo[MBB->getNumber()].invalidate();
|
|
|
|
for (unsigned i = 0; i != TS_NumStrategies; ++i)
|
|
|
|
if (Ensembles[i])
|
|
|
|
Ensembles[i]->invalidate(MBB);
|
|
|
|
}
|
|
|
|
|
2012-07-30 20:57:50 +00:00
|
|
|
void MachineTraceMetrics::verifyAnalysis() const {
|
2012-07-30 23:15:12 +00:00
|
|
|
if (!MF)
|
|
|
|
return;
|
2012-07-30 18:34:11 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(BlockInfo.size() == MF->getNumBlockIDs() && "Outdated BlockInfo size");
|
|
|
|
for (unsigned i = 0; i != TS_NumStrategies; ++i)
|
|
|
|
if (Ensembles[i])
|
|
|
|
Ensembles[i]->verify();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Trace building
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Traces are built by two CFG traversals. To avoid recomputing too much, use a
|
|
|
|
// set abstraction that confines the search to the current loop, and doesn't
|
|
|
|
// revisit blocks.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct LoopBounds {
|
|
|
|
MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> Blocks;
|
|
|
|
const MachineLoopInfo *Loops;
|
|
|
|
bool Downward;
|
|
|
|
LoopBounds(MutableArrayRef<MachineTraceMetrics::TraceBlockInfo> blocks,
|
2012-07-30 23:15:10 +00:00
|
|
|
const MachineLoopInfo *loops)
|
|
|
|
: Blocks(blocks), Loops(loops), Downward(false) {}
|
2012-07-26 18:38:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize po_iterator_storage in order to prune the post-order traversal so
|
|
|
|
// it is limited to the current loop and doesn't traverse the loop back edges.
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
class po_iterator_storage<LoopBounds, true> {
|
|
|
|
LoopBounds &LB;
|
|
|
|
public:
|
|
|
|
po_iterator_storage(LoopBounds &lb) : LB(lb) {}
|
|
|
|
void finishPostorder(const MachineBasicBlock*) {}
|
|
|
|
|
|
|
|
bool insertEdge(const MachineBasicBlock *From, const MachineBasicBlock *To) {
|
|
|
|
// Skip already visited To blocks.
|
|
|
|
MachineTraceMetrics::TraceBlockInfo &TBI = LB.Blocks[To->getNumber()];
|
|
|
|
if (LB.Downward ? TBI.hasValidHeight() : TBI.hasValidDepth())
|
|
|
|
return false;
|
2012-07-30 23:15:10 +00:00
|
|
|
// From is null once when To is the trace center block.
|
|
|
|
if (!From)
|
|
|
|
return true;
|
|
|
|
const MachineLoop *FromLoop = LB.Loops->getLoopFor(From);
|
|
|
|
if (!FromLoop)
|
|
|
|
return true;
|
|
|
|
// Don't follow backedges, don't leave FromLoop when going upwards.
|
|
|
|
if ((LB.Downward ? To : From) == FromLoop->getHeader())
|
2012-07-26 18:38:11 +00:00
|
|
|
return false;
|
2012-07-30 23:15:10 +00:00
|
|
|
// Don't leave FromLoop.
|
|
|
|
if (isExitingLoop(FromLoop, LB.Loops->getLoopFor(To)))
|
2012-07-26 18:38:11 +00:00
|
|
|
return false;
|
|
|
|
// This is a new block. The PO traversal will compute height/depth
|
|
|
|
// resources, causing us to reject new edges to To. This only works because
|
|
|
|
// we reject back-edges, so the CFG is cycle-free.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the trace through MBB.
|
|
|
|
void MachineTraceMetrics::Ensemble::computeTrace(const MachineBasicBlock *MBB) {
|
|
|
|
DEBUG(dbgs() << "Computing " << getName() << " trace through BB#"
|
|
|
|
<< MBB->getNumber() << '\n');
|
|
|
|
// Set up loop bounds for the backwards post-order traversal.
|
2012-07-30 23:15:10 +00:00
|
|
|
LoopBounds Bounds(BlockInfo, CT.Loops);
|
2012-07-26 18:38:11 +00:00
|
|
|
|
|
|
|
// Run an upwards post-order search for the trace start.
|
|
|
|
Bounds.Downward = false;
|
|
|
|
typedef ipo_ext_iterator<const MachineBasicBlock*, LoopBounds> UpwardPO;
|
|
|
|
for (UpwardPO I = ipo_ext_begin(MBB, Bounds), E = ipo_ext_end(MBB, Bounds);
|
|
|
|
I != E; ++I) {
|
|
|
|
DEBUG(dbgs() << " pred for BB#" << I->getNumber() << ": ");
|
|
|
|
TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
|
|
|
|
// All the predecessors have been visited, pick the preferred one.
|
|
|
|
TBI.Pred = pickTracePred(*I);
|
|
|
|
DEBUG({
|
|
|
|
if (TBI.Pred)
|
|
|
|
dbgs() << "BB#" << TBI.Pred->getNumber() << '\n';
|
|
|
|
else
|
|
|
|
dbgs() << "null\n";
|
|
|
|
});
|
|
|
|
// The trace leading to I is now known, compute the depth resources.
|
|
|
|
computeDepthResources(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a downwards post-order search for the trace end.
|
|
|
|
Bounds.Downward = true;
|
|
|
|
typedef po_ext_iterator<const MachineBasicBlock*, LoopBounds> DownwardPO;
|
|
|
|
for (DownwardPO I = po_ext_begin(MBB, Bounds), E = po_ext_end(MBB, Bounds);
|
|
|
|
I != E; ++I) {
|
|
|
|
DEBUG(dbgs() << " succ for BB#" << I->getNumber() << ": ");
|
|
|
|
TraceBlockInfo &TBI = BlockInfo[I->getNumber()];
|
|
|
|
// All the successors have been visited, pick the preferred one.
|
2012-07-26 19:42:56 +00:00
|
|
|
TBI.Succ = pickTraceSucc(*I);
|
2012-07-26 18:38:11 +00:00
|
|
|
DEBUG({
|
2012-07-30 23:15:10 +00:00
|
|
|
if (TBI.Succ)
|
2012-07-26 18:38:11 +00:00
|
|
|
dbgs() << "BB#" << TBI.Succ->getNumber() << '\n';
|
|
|
|
else
|
|
|
|
dbgs() << "null\n";
|
|
|
|
});
|
|
|
|
// The trace leaving I is now known, compute the height resources.
|
|
|
|
computeHeightResources(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidate traces through BadMBB.
|
|
|
|
void
|
|
|
|
MachineTraceMetrics::Ensemble::invalidate(const MachineBasicBlock *BadMBB) {
|
|
|
|
SmallVector<const MachineBasicBlock*, 16> WorkList;
|
|
|
|
TraceBlockInfo &BadTBI = BlockInfo[BadMBB->getNumber()];
|
|
|
|
|
|
|
|
// Invalidate height resources of blocks above MBB.
|
|
|
|
if (BadTBI.hasValidHeight()) {
|
|
|
|
BadTBI.invalidateHeight();
|
|
|
|
WorkList.push_back(BadMBB);
|
|
|
|
do {
|
|
|
|
const MachineBasicBlock *MBB = WorkList.pop_back_val();
|
|
|
|
DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
|
|
|
|
<< " height.\n");
|
|
|
|
// Find any MBB predecessors that have MBB as their preferred successor.
|
|
|
|
// They are the only ones that need to be invalidated.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator
|
|
|
|
I = MBB->pred_begin(), E = MBB->pred_end(); I != E; ++I) {
|
|
|
|
TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
|
2012-07-30 17:36:49 +00:00
|
|
|
if (!TBI.hasValidHeight())
|
|
|
|
continue;
|
|
|
|
if (TBI.Succ == MBB) {
|
2012-07-26 18:38:11 +00:00
|
|
|
TBI.invalidateHeight();
|
|
|
|
WorkList.push_back(*I);
|
2012-07-30 17:36:49 +00:00
|
|
|
continue;
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
2012-07-30 17:36:49 +00:00
|
|
|
// Verify that TBI.Succ is actually a *I successor.
|
|
|
|
assert((!TBI.Succ || (*I)->isSuccessor(TBI.Succ)) && "CFG changed");
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
} while (!WorkList.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate depth resources of blocks below MBB.
|
|
|
|
if (BadTBI.hasValidDepth()) {
|
|
|
|
BadTBI.invalidateDepth();
|
|
|
|
WorkList.push_back(BadMBB);
|
|
|
|
do {
|
|
|
|
const MachineBasicBlock *MBB = WorkList.pop_back_val();
|
|
|
|
DEBUG(dbgs() << "Invalidate BB#" << MBB->getNumber() << ' ' << getName()
|
|
|
|
<< " depth.\n");
|
|
|
|
// Find any MBB successors that have MBB as their preferred predecessor.
|
|
|
|
// They are the only ones that need to be invalidated.
|
|
|
|
for (MachineBasicBlock::const_succ_iterator
|
|
|
|
I = MBB->succ_begin(), E = MBB->succ_end(); I != E; ++I) {
|
|
|
|
TraceBlockInfo &TBI = BlockInfo[(*I)->getNumber()];
|
2012-07-30 17:36:49 +00:00
|
|
|
if (!TBI.hasValidDepth())
|
|
|
|
continue;
|
|
|
|
if (TBI.Pred == MBB) {
|
2012-07-26 18:38:11 +00:00
|
|
|
TBI.invalidateDepth();
|
|
|
|
WorkList.push_back(*I);
|
2012-07-30 17:36:49 +00:00
|
|
|
continue;
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
2012-07-30 17:36:49 +00:00
|
|
|
// Verify that TBI.Pred is actually a *I predecessor.
|
|
|
|
assert((!TBI.Pred || (*I)->isPredecessor(TBI.Pred)) && "CFG changed");
|
2012-07-26 18:38:11 +00:00
|
|
|
}
|
|
|
|
} while (!WorkList.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:34:11 +00:00
|
|
|
void MachineTraceMetrics::Ensemble::verify() const {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(BlockInfo.size() == CT.MF->getNumBlockIDs() &&
|
|
|
|
"Outdated BlockInfo size");
|
|
|
|
for (unsigned Num = 0, e = BlockInfo.size(); Num != e; ++Num) {
|
|
|
|
const TraceBlockInfo &TBI = BlockInfo[Num];
|
|
|
|
if (TBI.hasValidDepth() && TBI.Pred) {
|
|
|
|
const MachineBasicBlock *MBB = CT.MF->getBlockNumbered(Num);
|
|
|
|
assert(MBB->isPredecessor(TBI.Pred) && "CFG doesn't match trace");
|
|
|
|
assert(BlockInfo[TBI.Pred->getNumber()].hasValidDepth() &&
|
|
|
|
"Trace is broken, depth should have been invalidated.");
|
|
|
|
const MachineLoop *Loop = getLoopFor(MBB);
|
|
|
|
assert(!(Loop && MBB == Loop->getHeader()) && "Trace contains backedge");
|
|
|
|
}
|
|
|
|
if (TBI.hasValidHeight() && TBI.Succ) {
|
|
|
|
const MachineBasicBlock *MBB = CT.MF->getBlockNumbered(Num);
|
|
|
|
assert(MBB->isSuccessor(TBI.Succ) && "CFG doesn't match trace");
|
|
|
|
assert(BlockInfo[TBI.Succ->getNumber()].hasValidHeight() &&
|
|
|
|
"Trace is broken, height should have been invalidated.");
|
|
|
|
const MachineLoop *Loop = getLoopFor(MBB);
|
|
|
|
const MachineLoop *SuccLoop = getLoopFor(TBI.Succ);
|
|
|
|
assert(!(Loop && Loop == SuccLoop && TBI.Succ == Loop->getHeader()) &&
|
|
|
|
"Trace contains backedge");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2012-07-26 18:38:11 +00:00
|
|
|
|
|
|
|
MachineTraceMetrics::Trace
|
|
|
|
MachineTraceMetrics::Ensemble::getTrace(const MachineBasicBlock *MBB) {
|
|
|
|
// FIXME: Check cache tags, recompute as needed.
|
|
|
|
computeTrace(MBB);
|
|
|
|
return Trace(*this, BlockInfo[MBB->getNumber()]);
|
|
|
|
}
|
|
|
|
|
2012-07-27 23:58:38 +00:00
|
|
|
void MachineTraceMetrics::Ensemble::print(raw_ostream &OS) const {
|
|
|
|
OS << getName() << " ensemble:\n";
|
|
|
|
for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
|
|
|
|
OS << " BB#" << i << '\t';
|
|
|
|
BlockInfo[i].print(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineTraceMetrics::TraceBlockInfo::print(raw_ostream &OS) const {
|
|
|
|
if (hasValidDepth()) {
|
|
|
|
OS << "depth=" << InstrDepth;
|
|
|
|
if (Pred)
|
|
|
|
OS << " pred=BB#" << Pred->getNumber();
|
|
|
|
else
|
|
|
|
OS << " pred=null";
|
|
|
|
OS << " head=BB#" << Head;
|
|
|
|
} else
|
|
|
|
OS << "depth invalid";
|
|
|
|
OS << ", ";
|
|
|
|
if (hasValidHeight()) {
|
|
|
|
OS << "height=" << InstrHeight;
|
|
|
|
if (Succ)
|
|
|
|
OS << " succ=BB#" << Succ->getNumber();
|
|
|
|
else
|
|
|
|
OS << " succ=null";
|
|
|
|
OS << " tail=BB#" << Tail;
|
|
|
|
} else
|
|
|
|
OS << "height invalid";
|
|
|
|
}
|
|
|
|
|
2012-07-26 18:38:11 +00:00
|
|
|
void MachineTraceMetrics::Trace::print(raw_ostream &OS) const {
|
2012-07-27 23:58:36 +00:00
|
|
|
unsigned MBBNum = &TBI - &TE.BlockInfo[0];
|
|
|
|
|
|
|
|
OS << TE.getName() << " trace BB#" << TBI.Head << " --> BB#" << MBBNum
|
|
|
|
<< " --> BB#" << TBI.Tail << ':';
|
2012-07-26 18:38:11 +00:00
|
|
|
if (TBI.hasValidHeight() && TBI.hasValidDepth())
|
|
|
|
OS << ' ' << getInstrCount() << " instrs.";
|
|
|
|
|
|
|
|
const MachineTraceMetrics::TraceBlockInfo *Block = &TBI;
|
2012-07-27 23:58:36 +00:00
|
|
|
OS << "\nBB#" << MBBNum;
|
2012-07-26 18:38:11 +00:00
|
|
|
while (Block->hasValidDepth() && Block->Pred) {
|
|
|
|
unsigned Num = Block->Pred->getNumber();
|
|
|
|
OS << " <- BB#" << Num;
|
|
|
|
Block = &TE.BlockInfo[Num];
|
|
|
|
}
|
|
|
|
|
|
|
|
Block = &TBI;
|
2012-07-27 23:58:36 +00:00
|
|
|
OS << "\n ";
|
2012-07-26 18:38:11 +00:00
|
|
|
while (Block->hasValidHeight() && Block->Succ) {
|
|
|
|
unsigned Num = Block->Succ->getNumber();
|
|
|
|
OS << " -> BB#" << Num;
|
|
|
|
Block = &TE.BlockInfo[Num];
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|