1 //===- DSGraphStats.cpp - Various statistics for DS Graphs -----*- C++ -*--===//
3 //===----------------------------------------------------------------------===//
5 #include "llvm/Analysis/DataStructure.h"
6 #include "llvm/Analysis/DSGraph.h"
7 #include "llvm/Function.h"
8 #include "llvm/iOther.h"
10 #include "Support/Statistic.h"
14 Statistic<> TotalNumCallees("totalcallees",
15 "Total number of callee functions at all indirect call sites");
16 Statistic<> NumIndirectCalls("numindirect",
17 "Total number of indirect call sites in the program");
18 Statistic<> NumPoolNodes("numpools",
19 "Number of allocation nodes that could be pool allocated");
21 class DSGraphStats: public FunctionPass {
22 void countCallees(const Function& F, const DSGraph& tdGraph);
25 /// Driver functions to compute the Load/Store Dep. Graph per function.
26 bool runOnFunction(Function& F);
28 /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
29 void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired<TDDataStructures>();
34 /// Debugging support methods
35 void print(std::ostream &O) const { }
39 static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
42 static bool isIndirectCallee(Value *V) {
43 if (isa<Function>(V)) return false;
45 if (CastInst *CI = dyn_cast<CastInst>(V))
46 return isIndirectCallee(CI->getOperand(0));
51 void DSGraphStats::countCallees(const Function& F,
52 const DSGraph& tdGraph)
54 unsigned numIndirectCalls = 0, totalNumCallees = 0;
56 const std::vector<DSCallSite>& callSites = tdGraph.getFunctionCalls();
57 for (unsigned i=0, N = callSites.size(); i < N; ++i)
58 if (isIndirectCallee(callSites[i].getCallInst().getCalledValue()))
59 { // This is an indirect function call
60 std::vector<GlobalValue*> Callees =
61 callSites[i].getCallee().getNode()->getGlobals();
62 if (Callees.size() > 0)
64 totalNumCallees += Callees.size();
69 std::cerr << "WARNING: No callee in Function " << F.getName()
70 << "at call:\n" << callSites[i].getCallInst();
74 TotalNumCallees += totalNumCallees;
75 NumIndirectCalls += numIndirectCalls;
78 std::cout << " In function " << F.getName() << ": "
79 << (totalNumCallees / (double) numIndirectCalls)
80 << " average callees per indirect call\n";
84 bool DSGraphStats::runOnFunction(Function& F)
86 const DSGraph& tdGraph = getAnalysis<TDDataStructures>().getDSGraph(F);
87 countCallees(F, tdGraph);
91 void DSGraphStats::dump() const
93 this->print(std::cerr);