2009-08-08 18:44:18 +00:00
|
|
|
//===- ProfileEstimatorPass.cpp - LLVM Pass to estimate profile info ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a concrete implementation of profiling information that
|
|
|
|
// estimates the profiling information in a very crude and unimaginative way.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "profile-estimator"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-08-26 15:13:44 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-08-08 18:44:18 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<double>
|
2009-08-26 15:13:44 +00:00
|
|
|
LoopWeight(
|
2009-08-08 18:44:18 +00:00
|
|
|
"profile-estimator-loop-weight", cl::init(10),
|
|
|
|
cl::value_desc("loop-weight"),
|
|
|
|
cl::desc("Number of loop executions used for profile-estimator")
|
|
|
|
);
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class ProfileEstimatorPass : public FunctionPass, public ProfileInfo {
|
2009-08-08 18:44:18 +00:00
|
|
|
double ExecCount;
|
|
|
|
LoopInfo *LI;
|
2009-09-11 08:39:33 +00:00
|
|
|
std::set<BasicBlock*> BBToVisit;
|
2009-08-08 18:44:18 +00:00
|
|
|
std::map<Loop*,double> LoopExitWeights;
|
|
|
|
public:
|
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
|
|
|
explicit ProfileEstimatorPass(const double execcount = 0)
|
|
|
|
: FunctionPass(&ID), ExecCount(execcount) {
|
2009-08-26 15:13:44 +00:00
|
|
|
if (execcount == 0) ExecCount = LoopWeight;
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Profiling information estimator";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// run - Estimate the profile information from the specified file.
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
virtual void recurseBasicBlock(BasicBlock *BB);
|
2009-08-26 15:13:44 +00:00
|
|
|
|
|
|
|
void inline printEdgeWeight(Edge);
|
2009-08-08 18:44:18 +00:00
|
|
|
};
|
|
|
|
} // End of anonymous namespace
|
|
|
|
|
|
|
|
char ProfileEstimatorPass::ID = 0;
|
|
|
|
static RegisterPass<ProfileEstimatorPass>
|
|
|
|
X("profile-estimator", "Estimate profiling information", false, true);
|
|
|
|
|
|
|
|
static RegisterAnalysisGroup<ProfileInfo> Y(X);
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
const PassInfo *ProfileEstimatorPassID = &X;
|
|
|
|
|
|
|
|
FunctionPass *createProfileEstimatorPass() {
|
|
|
|
return new ProfileEstimatorPass();
|
|
|
|
}
|
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
/// createProfileEstimatorPass - This function returns a Pass that estimates
|
|
|
|
/// profiling information using the given loop execution count.
|
2009-08-08 18:44:18 +00:00
|
|
|
Pass *createProfileEstimatorPass(const unsigned execcount) {
|
|
|
|
return new ProfileEstimatorPass(execcount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static double ignoreMissing(double w) {
|
|
|
|
if (w == ProfileInfo::MissingValue) return 0;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
static void inline printEdgeError(ProfileInfo::Edge e, const char *M) {
|
|
|
|
DEBUG(errs() << "-- Edge " << e << " is not calculated, " << M << "\n");
|
2009-08-26 15:13:44 +00:00
|
|
|
}
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
void inline ProfileEstimatorPass::printEdgeWeight(Edge E) {
|
|
|
|
DEBUG(errs() << "-- Weight of Edge " << E << ":"
|
|
|
|
<< format("%g", getEdgeWeight(E)) << "\n");
|
|
|
|
}
|
2009-08-08 18:44:18 +00:00
|
|
|
|
|
|
|
// recurseBasicBlock() - This calculates the ProfileInfo estimation for a
|
|
|
|
// single block and then recurses into the successors.
|
2009-09-11 08:39:33 +00:00
|
|
|
// The algorithm preserves the flow condition, meaning that the sum of the
|
|
|
|
// weight of the incoming edges must be equal the block weight which must in
|
|
|
|
// turn be equal to the sume of the weights of the outgoing edges.
|
|
|
|
// Since the flow of an block is deterimined from the current state of the
|
|
|
|
// flow, once an edge has a flow assigned this flow is never changed again,
|
|
|
|
// otherwise it would be possible to violate the flow condition in another
|
|
|
|
// block.
|
|
|
|
void ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) {
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
// Break the recursion if this BasicBlock was already visited.
|
2009-09-11 08:39:33 +00:00
|
|
|
if (BBToVisit.find(BB) == BBToVisit.end()) return;
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// Read the LoopInfo for this block.
|
2009-08-08 18:44:18 +00:00
|
|
|
bool BBisHeader = LI->isLoopHeader(BB);
|
|
|
|
Loop* BBLoop = LI->getLoopFor(BB);
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// To get the block weight, read all incoming edges.
|
2009-08-08 18:44:18 +00:00
|
|
|
double BBWeight = 0;
|
|
|
|
std::set<BasicBlock*> ProcessedPreds;
|
|
|
|
for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
|
|
|
|
bbi != bbe; ++bbi ) {
|
2009-09-11 08:39:33 +00:00
|
|
|
// If this block was not considered already, add weight.
|
2009-09-10 16:30:38 +00:00
|
|
|
Edge edge = getEdge(*bbi,BB);
|
|
|
|
double w = getEdgeWeight(edge);
|
2009-08-08 18:44:18 +00:00
|
|
|
if (ProcessedPreds.insert(*bbi).second) {
|
2009-09-10 16:30:38 +00:00
|
|
|
BBWeight += ignoreMissing(w);
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
2009-09-11 08:39:33 +00:00
|
|
|
// If this block is a loop header and the predecessor is contained in this
|
|
|
|
// loop, thus the edge is a backedge, continue and do not check if the
|
|
|
|
// value is valid.
|
2009-09-10 16:30:38 +00:00
|
|
|
if (BBisHeader && BBLoop->contains(*bbi)) {
|
2009-09-11 08:39:33 +00:00
|
|
|
printEdgeError(edge, "but is backedge, continueing");
|
2009-08-08 18:44:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-11 08:39:33 +00:00
|
|
|
// If the edges value is missing (and this is no loop header, and this is
|
|
|
|
// no backedge) return, this block is currently non estimatable.
|
2009-09-10 16:30:38 +00:00
|
|
|
if (w == MissingValue) {
|
2009-09-11 08:39:33 +00:00
|
|
|
printEdgeError(edge, "returning");
|
|
|
|
return;
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getExecutionCount(BB) != MissingValue) {
|
|
|
|
BBWeight = getExecutionCount(BB);
|
|
|
|
}
|
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
// Fetch all necessary information for current block.
|
2009-08-08 18:44:18 +00:00
|
|
|
SmallVector<Edge, 8> ExitEdges;
|
|
|
|
SmallVector<Edge, 8> Edges;
|
|
|
|
if (BBLoop) {
|
|
|
|
BBLoop->getExitEdges(ExitEdges);
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// If this is a loop header, consider the following:
|
|
|
|
// Exactly the flow that is entering this block, must exit this block too. So
|
|
|
|
// do the following:
|
|
|
|
// *) get all the exit edges, read the flow that is already leaving this
|
|
|
|
// loop, remember the edges that do not have any flow on them right now.
|
|
|
|
// (The edges that have already flow on them are most likely exiting edges of
|
|
|
|
// other loops, do not touch those flows because the previously caclulated
|
|
|
|
// loopheaders would not be exact anymore.)
|
|
|
|
// *) In case there is not a single exiting edge left, create one at the loop
|
|
|
|
// latch to prevent the flow from building up in the loop.
|
|
|
|
// *) Take the flow that is not leaving the loop already and distribute it on
|
|
|
|
// the remaining exiting edges.
|
|
|
|
// (This ensures that all flow that enters the loop also leaves it.)
|
|
|
|
// *) Increase the flow into the loop by increasing the weight of this block.
|
|
|
|
// There is at least one incoming backedge that will bring us this flow later
|
|
|
|
// on. (So that the flow condition in this node is valid again.)
|
2009-08-08 18:44:18 +00:00
|
|
|
if (BBisHeader) {
|
|
|
|
double incoming = BBWeight;
|
2009-08-26 15:13:44 +00:00
|
|
|
// Subtract the flow leaving the loop.
|
2009-09-10 16:30:38 +00:00
|
|
|
std::set<Edge> ProcessedExits;
|
2009-08-08 18:44:18 +00:00
|
|
|
for (SmallVector<Edge, 8>::iterator ei = ExitEdges.begin(),
|
|
|
|
ee = ExitEdges.end(); ei != ee; ++ei) {
|
2009-09-10 16:30:38 +00:00
|
|
|
if (ProcessedExits.insert(*ei).second) {
|
|
|
|
double w = getEdgeWeight(*ei);
|
|
|
|
if (w == MissingValue) {
|
|
|
|
Edges.push_back(*ei);
|
|
|
|
} else {
|
|
|
|
incoming -= w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no exit edges, create one:
|
|
|
|
if (Edges.size() == 0) {
|
|
|
|
BasicBlock *Latch = BBLoop->getLoopLatch();
|
|
|
|
if (Latch) {
|
|
|
|
Edge edge = getEdge(Latch,0);
|
|
|
|
EdgeInformation[BB->getParent()][edge] = BBWeight;
|
|
|
|
printEdgeWeight(edge);
|
|
|
|
edge = getEdge(Latch, BB);
|
|
|
|
EdgeInformation[BB->getParent()][edge] = BBWeight * ExecCount;
|
|
|
|
printEdgeWeight(edge);
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-26 15:13:44 +00:00
|
|
|
// Distribute remaining weight onto the exit edges.
|
2009-08-08 18:44:18 +00:00
|
|
|
for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
|
|
|
|
ei != ee; ++ei) {
|
|
|
|
EdgeInformation[BB->getParent()][*ei] += incoming/Edges.size();
|
2009-08-26 15:13:44 +00:00
|
|
|
printEdgeWeight(*ei);
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
2009-08-26 15:13:44 +00:00
|
|
|
// Increase flow into the loop.
|
2009-08-08 18:44:18 +00:00
|
|
|
BBWeight *= (ExecCount+1);
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
BlockInformation[BB->getParent()][BB] = BBWeight;
|
|
|
|
// Up until now we considered only the loop exiting edges, now we have a
|
|
|
|
// definite block weight and must ditribute this onto the outgoing edges.
|
|
|
|
// Since there may be already flow attached to some of the edges, read this
|
|
|
|
// flow first and remember the edges that have still now flow attached.
|
2009-08-08 18:44:18 +00:00
|
|
|
Edges.clear();
|
|
|
|
std::set<BasicBlock*> ProcessedSuccs;
|
2009-08-26 15:13:44 +00:00
|
|
|
|
2009-09-01 10:06:05 +00:00
|
|
|
succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
|
2009-09-11 08:39:33 +00:00
|
|
|
// Also check for (BB,0) edges that may already contain some flow. (But only
|
|
|
|
// in case there are no successors.)
|
2009-09-01 10:06:05 +00:00
|
|
|
if (bbi == bbe) {
|
|
|
|
Edge edge = getEdge(BB,0);
|
|
|
|
EdgeInformation[BB->getParent()][edge] = BBWeight;
|
|
|
|
printEdgeWeight(edge);
|
|
|
|
}
|
2009-09-01 19:01:59 +00:00
|
|
|
for ( ; bbi != bbe; ++bbi ) {
|
2009-08-08 18:44:18 +00:00
|
|
|
if (ProcessedSuccs.insert(*bbi).second) {
|
|
|
|
Edge edge = getEdge(BB,*bbi);
|
|
|
|
double w = getEdgeWeight(edge);
|
|
|
|
if (w != MissingValue) {
|
|
|
|
BBWeight -= getEdgeWeight(edge);
|
|
|
|
} else {
|
|
|
|
Edges.push_back(edge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// Finally we know what flow is still not leaving the block, distribute this
|
|
|
|
// flow onto the empty edges.
|
2009-08-08 18:44:18 +00:00
|
|
|
for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
|
|
|
|
ei != ee; ++ei) {
|
|
|
|
EdgeInformation[BB->getParent()][*ei] += BBWeight/Edges.size();
|
2009-08-26 15:13:44 +00:00
|
|
|
printEdgeWeight(*ei);
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// This block is visited, mark this before the recursion.
|
|
|
|
BBToVisit.erase(BB);
|
|
|
|
|
|
|
|
// Recurse into successors.
|
|
|
|
for (succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
|
|
|
|
bbi != bbe; ++bbi) {
|
|
|
|
recurseBasicBlock(*bbi);
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileEstimatorPass::runOnFunction(Function &F) {
|
|
|
|
if (F.isDeclaration()) return false;
|
|
|
|
|
2009-09-11 08:39:33 +00:00
|
|
|
// Fetch LoopInfo and clear ProfileInfo for this function.
|
2009-08-08 18:44:18 +00:00
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
FunctionInformation.erase(&F);
|
|
|
|
BlockInformation[&F].clear();
|
|
|
|
EdgeInformation[&F].clear();
|
2009-09-11 08:39:33 +00:00
|
|
|
|
|
|
|
// Mark all blocks as to visit.
|
|
|
|
for (Function::iterator bi = F.begin(), be = F.end(); bi != be; ++bi)
|
|
|
|
BBToVisit.insert(bi);
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
DEBUG(errs() << "Working on function " << F.getNameStr() << "\n");
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
// Since the entry block is the first one and has no predecessors, the edge
|
|
|
|
// (0,entry) is inserted with the starting weight of 1.
|
2009-08-08 18:44:18 +00:00
|
|
|
BasicBlock *entry = &F.getEntryBlock();
|
|
|
|
BlockInformation[&F][entry] = 1;
|
|
|
|
Edge edge = getEdge(0,entry);
|
2009-09-11 08:39:33 +00:00
|
|
|
EdgeInformation[&F][edge] = 1;
|
|
|
|
printEdgeWeight(edge);
|
|
|
|
|
|
|
|
// Since recurseBasicBlock() maybe returns with a block which was not fully
|
|
|
|
// estimated, use recurseBasicBlock() until everything is calculated.
|
|
|
|
recurseBasicBlock(entry);
|
|
|
|
while (BBToVisit.size() > 0) {
|
|
|
|
// Remember number of open blocks, this is later used to check if progress
|
|
|
|
// was made.
|
|
|
|
unsigned size = BBToVisit.size();
|
|
|
|
|
|
|
|
// Try to calculate all blocks in turn.
|
|
|
|
for (std::set<BasicBlock*>::iterator bi = BBToVisit.begin(),
|
|
|
|
be = BBToVisit.end(); bi != be; ++bi) {
|
|
|
|
recurseBasicBlock(*bi);
|
|
|
|
// If at least one block was finished, break because iterator may be
|
|
|
|
// invalid.
|
|
|
|
if (BBToVisit.size() < size) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was not a single block resovled, make some assumptions.
|
|
|
|
if (BBToVisit.size() == size) {
|
|
|
|
BasicBlock *BB = *(BBToVisit.begin());
|
|
|
|
// Since this BB was not calculated because of missing incoming edges,
|
|
|
|
// set these edges to zero.
|
2009-08-08 18:44:18 +00:00
|
|
|
for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
|
|
|
|
bbi != bbe; ++bbi) {
|
|
|
|
Edge e = getEdge(*bbi,BB);
|
2009-09-10 16:30:38 +00:00
|
|
|
double w = getEdgeWeight(e);
|
|
|
|
if (w == MissingValue) {
|
|
|
|
EdgeInformation[&F][e] = 0;
|
2009-09-11 08:39:33 +00:00
|
|
|
DEBUG(errs() << "Assuming edge weight: ");
|
2009-09-10 16:30:38 +00:00
|
|
|
printEdgeWeight(e);
|
|
|
|
}
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|