OptimalEdgeProfiling.cpp [plain text]
#define DEBUG_TYPE "insert-optimal-edge-profiling"
#include "ProfilingUtils.h"
#include "llvm/Constants.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Analysis/ProfileInfoLoader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Statistic.h"
#include "MaximumSpanningTree.h"
using namespace llvm;
STATISTIC(NumEdgesInserted, "The # of edges inserted.");
namespace {
class OptimalEdgeProfiler : public ModulePass {
bool runOnModule(Module &M);
public:
static char ID; OptimalEdgeProfiler() : ModulePass(ID) {
initializeOptimalEdgeProfilerPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequiredID(ProfileEstimatorPassID);
AU.addRequired<ProfileInfo>();
}
virtual const char *getPassName() const {
return "Optimal Edge Profiler";
}
};
}
char OptimalEdgeProfiler::ID = 0;
INITIALIZE_PASS_BEGIN(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
"Insert optimal instrumentation for edge profiling",
false, false)
INITIALIZE_PASS_DEPENDENCY(ProfileEstimatorPass)
INITIALIZE_AG_DEPENDENCY(ProfileInfo)
INITIALIZE_PASS_END(OptimalEdgeProfiler, "insert-optimal-edge-profiling",
"Insert optimal instrumentation for edge profiling",
false, false)
ModulePass *llvm::createOptimalEdgeProfilerPass() {
return new OptimalEdgeProfiler();
}
inline static void printEdgeCounter(ProfileInfo::Edge e,
BasicBlock* b,
unsigned i) {
DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \
<< ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
}
bool OptimalEdgeProfiler::runOnModule(Module &M) {
Function *Main = M.getFunction("main");
if (Main == 0) {
errs() << "WARNING: cannot insert edge profiling into a module"
<< " with no main function!\n";
return false; }
unsigned NumEdges = 0;
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
if (F->isDeclaration()) continue;
++NumEdges;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
if (BB->getTerminator()->getNumSuccessors() == 0) {
++NumEdges;
} else {
NumEdges += BB->getTerminator()->getNumSuccessors();
}
}
}
const Type *Int32 = Type::getInt32Ty(M.getContext());
const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
GlobalVariable *Counters =
new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Constant::getNullValue(ATy), "OptEdgeProfCounters");
NumEdgesInserted = 0;
std::vector<Constant*> Initializer(NumEdges);
Constant *Zero = ConstantInt::get(Int32, 0);
Constant *Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
unsigned i = 0;
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
if (F->isDeclaration()) continue;
DEBUG(dbgs() << "Working on " << F->getNameStr() << "\n");
ProfileInfo::EdgeWeights ECs =
getAnalysis<ProfileInfo>(*F).getEdgeWeights(F);
std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
MaximumSpanningTree<BasicBlock> MST(EdgeVector);
std::stable_sort(MST.begin(), MST.end());
BasicBlock *entry = &(F->getEntryBlock());
ProfileInfo::Edge edge = ProfileInfo::getEdge(0, entry);
if (!std::binary_search(MST.begin(), MST.end(), edge)) {
printEdgeCounter(edge, entry, i);
IncrementCounterInBlock(entry, i, Counters); ++NumEdgesInserted;
Initializer[i++] = (Zero);
} else{
Initializer[i++] = (Uncounted);
}
DenseSet<BasicBlock*> InsertedBlocks;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
if (InsertedBlocks.count(BB)) continue;
TerminatorInst *TI = BB->getTerminator();
if (TI->getNumSuccessors() == 0) {
ProfileInfo::Edge edge = ProfileInfo::getEdge(BB, 0);
if (!std::binary_search(MST.begin(), MST.end(), edge)) {
printEdgeCounter(edge, BB, i);
IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
Initializer[i++] = (Zero);
} else{
Initializer[i++] = (Uncounted);
}
}
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
BasicBlock *Succ = TI->getSuccessor(s);
ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
if (!std::binary_search(MST.begin(), MST.end(), edge)) {
bool wasInserted = SplitCriticalEdge(TI, s, this);
Succ = TI->getSuccessor(s);
if (wasInserted)
InsertedBlocks.insert(Succ);
if (TI->getNumSuccessors() == 1) {
printEdgeCounter(edge, BB, i);
IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted;
} else {
printEdgeCounter(edge, Succ, i);
IncrementCounterInBlock(Succ, i, Counters); ++NumEdgesInserted;
}
Initializer[i++] = (Zero);
} else {
Initializer[i++] = (Uncounted);
}
}
}
}
assert(i == NumEdges && "the number of edges in counting array is wrong");
Constant *init = ConstantArray::get(ATy, Initializer);
Counters->setInitializer(init);
InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
return true;
}