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 {
|
|
|
|
class VISIBILITY_HIDDEN ProfileEstimatorPass :
|
|
|
|
public FunctionPass, public ProfileInfo {
|
|
|
|
double ExecCount;
|
|
|
|
LoopInfo *LI;
|
|
|
|
std::set<BasicBlock*> BBisVisited;
|
|
|
|
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-10 16:30:38 +00:00
|
|
|
BasicBlock *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-10 16:30:38 +00:00
|
|
|
static void inline printEdgeError(ProfileInfo::Edge e) {
|
|
|
|
DEBUG(errs() << "-- Edge " << e << " is not calculated, returning\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-10 16:30:38 +00:00
|
|
|
BasicBlock* 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-10 16:30:38 +00:00
|
|
|
if (BBisVisited.find(BB) != BBisVisited.end()) return 0;
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2009-08-26 15:13:44 +00:00
|
|
|
// Check if incoming edges are calculated already, if BB is header allow
|
|
|
|
// backedges that are uncalculated for now.
|
2009-08-08 18:44:18 +00:00
|
|
|
bool BBisHeader = LI->isLoopHeader(BB);
|
|
|
|
Loop* BBLoop = LI->getLoopFor(BB);
|
|
|
|
|
|
|
|
double BBWeight = 0;
|
|
|
|
std::set<BasicBlock*> ProcessedPreds;
|
|
|
|
for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
|
|
|
|
bbi != bbe; ++bbi ) {
|
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-10 16:30:38 +00:00
|
|
|
if (BBisHeader && BBLoop->contains(*bbi)) {
|
|
|
|
printEdgeError(edge);
|
2009-08-08 18:44:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-10 16:30:38 +00:00
|
|
|
if (w == MissingValue) {
|
|
|
|
printEdgeError(edge);
|
|
|
|
return BB;
|
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-08-26 15:13:44 +00:00
|
|
|
// If block is an loop header, first subtract all weights from edges that
|
2009-08-08 18:44:18 +00:00
|
|
|
// exit this loop, then distribute remaining weight on to the edges exiting
|
2009-08-26 15:13:44 +00:00
|
|
|
// this loop. Finally the weight of the block is increased, to simulate
|
|
|
|
// several executions of this loop.
|
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-09-10 16:30:38 +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-08-26 15:13:44 +00:00
|
|
|
// Remove from current flow of block all the successor edges that already
|
|
|
|
// have some flow on them.
|
2009-08-08 18:44:18 +00:00
|
|
|
Edges.clear();
|
|
|
|
std::set<BasicBlock*> ProcessedSuccs;
|
2009-08-26 15:13:44 +00:00
|
|
|
|
|
|
|
// Otherwise consider weight of outgoing edges and store them for
|
2009-09-01 10:06:05 +00:00
|
|
|
// distribution of remaining weight. In case the block has no successors
|
|
|
|
// create a (BB,0) edge.
|
|
|
|
succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
|
|
|
|
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-08-26 15:13:44 +00:00
|
|
|
// Distribute remaining flow onto the outgoing 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-08-26 15:13:44 +00:00
|
|
|
// Mark this Block visited and recurse into successors.
|
2009-08-08 18:44:18 +00:00
|
|
|
BBisVisited.insert(BB);
|
2009-09-10 16:30:38 +00:00
|
|
|
BasicBlock *Uncalculated = 0;
|
2009-08-08 18:44:18 +00:00
|
|
|
for ( succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
|
2009-09-10 16:30:38 +00:00
|
|
|
bbi != bbe; ++bbi ) {
|
|
|
|
BasicBlock* ret = recurseBasicBlock(*bbi);
|
|
|
|
if (!Uncalculated)
|
|
|
|
Uncalculated = ret;
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
2009-09-10 16:30:38 +00:00
|
|
|
if (BBisVisited.find(Uncalculated) != BBisVisited.end())
|
|
|
|
return 0;
|
|
|
|
return Uncalculated;
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileEstimatorPass::runOnFunction(Function &F) {
|
|
|
|
if (F.isDeclaration()) return false;
|
|
|
|
|
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
FunctionInformation.erase(&F);
|
|
|
|
BlockInformation[&F].clear();
|
|
|
|
EdgeInformation[&F].clear();
|
|
|
|
BBisVisited.clear();
|
|
|
|
|
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-08-26 15:13:44 +00:00
|
|
|
EdgeInformation[&F][edge] = 1; printEdgeWeight(edge);
|
2009-09-10 16:30:38 +00:00
|
|
|
BasicBlock *BB = entry;
|
|
|
|
while (BB) {
|
|
|
|
BB = recurseBasicBlock(BB);
|
|
|
|
if (BB) {
|
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;
|
|
|
|
errs() << "Assuming edge weight: ";
|
|
|
|
printEdgeWeight(e);
|
|
|
|
}
|
2009-08-08 18:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|