Pass to compute various statisics related to DSGraphs.
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureStats.cpp
1 //===- DSGraphStats.cpp - Various statistics for DS Graphs -----*- C++ -*--===//
2 //
3 //===----------------------------------------------------------------------===//
4
5 #include "llvm/Analysis/DataStructure.h"
6 #include "llvm/Analysis/DSGraph.h"
7 #include "llvm/Function.h"
8 #include "llvm/iOther.h"
9 #include "llvm/Pass.h"
10 #include "Support/Statistic.h"
11 #include <vector>
12
13 namespace {
14   Statistic<double> 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");
20
21   class DSGraphStats: public FunctionPass {
22     void countCallees(const Function& F, const DSGraph& tdGraph);
23
24   public:
25     /// Driver functions to compute the Load/Store Dep. Graph per function.
26     bool runOnFunction(Function& F);
27
28     /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
29     void getAnalysisUsage(AnalysisUsage &AU) const {
30       AU.setPreservesAll();
31       AU.addRequired<TDDataStructures>();
32     }
33
34     /// Debugging support methods
35     void print(std::ostream &O) const { }
36     void dump() const;
37   };
38
39   static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
40 }
41
42
43 void DSGraphStats::countCallees(const Function& F,
44                                 const DSGraph& tdGraph)
45 {
46   unsigned numIndirectCalls = 0, totalNumCallees = 0;
47
48   const std::vector<DSCallSite>& callSites = tdGraph.getFunctionCalls();
49   for (unsigned i=0, N = callSites.size(); i < N; ++i)
50     if (callSites[i].getCallInst().getCalledFunction() == NULL)
51       { // This is an indirect function call
52         std::vector<GlobalValue*> Callees =
53           callSites[i].getCallee().getNode()->getGlobals();
54         if (Callees.size() > 0)
55           {
56             totalNumCallees  += Callees.size();
57             ++numIndirectCalls;
58           }
59 #ifndef NDEBUG
60         else
61           std::cerr << "WARNING: No callee in Function " << F.getName()
62                       << "at call:\n" << callSites[i].getCallInst();
63 #endif
64       }
65
66   TotalNumCallees  += totalNumCallees;
67   NumIndirectCalls += numIndirectCalls;
68
69   std::cout << "  In function " << F.getName() << " :  "
70             << (double) (numIndirectCalls == 0? 0.0
71                          : (totalNumCallees / (double) numIndirectCalls)) 
72             << " avg. callees per indirect call\n";
73 }
74
75
76 bool DSGraphStats::runOnFunction(Function& F)
77 {
78   const DSGraph& tdGraph = getAnalysis<TDDataStructures>().getDSGraph(F);
79   countCallees(F, tdGraph);
80   return true;
81 }
82
83 void DSGraphStats::dump() const
84 {
85   this->print(std::cerr);
86 }