blob: 8d2984845a26ca39dbcb25166d92ced2defcb598 [file] [log] [blame]
Chris Lattner78925492003-09-20 01:20:46 +00001//===- DSGraphStats.cpp - Various statistics for DS Graphs ----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Vikram S. Adve586aa3c2002-11-13 15:41:00 +00009//
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Analysis/DataStructure.h"
13#include "llvm/Analysis/DSGraph.h"
14#include "llvm/Function.h"
15#include "llvm/iOther.h"
Chris Lattner78925492003-09-20 01:20:46 +000016#include "llvm/iMemory.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000017#include "llvm/Pass.h"
Chris Lattner78925492003-09-20 01:20:46 +000018#include "llvm/Support/InstVisitor.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000019#include "Support/Statistic.h"
20#include <vector>
21
22namespace {
Chris Lattnered806bf2002-11-17 22:17:12 +000023 Statistic<> TotalNumCallees("totalcallees",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000024 "Total number of callee functions at all indirect call sites");
25 Statistic<> NumIndirectCalls("numindirect",
26 "Total number of indirect call sites in the program");
27 Statistic<> NumPoolNodes("numpools",
28 "Number of allocation nodes that could be pool allocated");
29
Chris Lattner78925492003-09-20 01:20:46 +000030 // Typed/Untyped memory accesses: If DSA can infer that the types the loads
31 // and stores are accessing are correct (ie, the node has not been collapsed),
32 // increment the appropriate counter.
33 Statistic<> NumTypedMemAccesses("numtypedmemaccesses",
34 "Number of loads/stores which are fully typed");
35 Statistic<> NumUntypedMemAccesses("numuntypedmemaccesses",
36 "Number of loads/stores which are untyped");
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000037
Chris Lattner78925492003-09-20 01:20:46 +000038 class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> {
39 void countCallees(const Function &F);
40 const DSGraph *TDGraph;
41
42 DSNode *getNodeForValue(Value *V);
Chris Lattner192cd9c2003-09-20 16:12:57 +000043 bool isNodeForValueCollapsed(Value *V);
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000044 public:
45 /// Driver functions to compute the Load/Store Dep. Graph per function.
46 bool runOnFunction(Function& F);
47
48 /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
49 void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 AU.addRequired<TDDataStructures>();
52 }
53
Chris Lattner78925492003-09-20 01:20:46 +000054 void visitLoad(LoadInst &LI);
55 void visitStore(StoreInst &SI);
56
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000057 /// Debugging support methods
58 void print(std::ostream &O) const { }
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000059 };
60
61 static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
62}
63
Chris Lattnered806bf2002-11-17 22:17:12 +000064static bool isIndirectCallee(Value *V) {
65 if (isa<Function>(V)) return false;
66
67 if (CastInst *CI = dyn_cast<CastInst>(V))
68 return isIndirectCallee(CI->getOperand(0));
69 return true;
70}
71
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000072
Chris Lattner78925492003-09-20 01:20:46 +000073void DSGraphStats::countCallees(const Function& F) {
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000074 unsigned numIndirectCalls = 0, totalNumCallees = 0;
75
Chris Lattner78925492003-09-20 01:20:46 +000076 const std::vector<DSCallSite> &callSites = TDGraph->getFunctionCalls();
77 for (unsigned i = 0, N = callSites.size(); i != N; ++i)
Chris Lattner808a7ae2003-09-20 16:34:13 +000078 if (isIndirectCallee(callSites[i].getCallSite().getCalledValue())) {
Chris Lattner78925492003-09-20 01:20:46 +000079 // This is an indirect function call
80 const std::vector<GlobalValue*> &Callees =
81 callSites[i].getCalleeNode()->getGlobals();
82 if (Callees.size() > 0) {
83 totalNumCallees += Callees.size();
84 ++numIndirectCalls;
85 } else
Chris Lattner808a7ae2003-09-20 16:34:13 +000086 std::cerr << "WARNING: No callee in Function '" << F.getName()
87 << "' at call: \n"
88 << *callSites[i].getCallSite().getInstruction();
Chris Lattner78925492003-09-20 01:20:46 +000089 }
90
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000091 TotalNumCallees += totalNumCallees;
92 NumIndirectCalls += numIndirectCalls;
Chris Lattner78925492003-09-20 01:20:46 +000093
Chris Lattnered806bf2002-11-17 22:17:12 +000094 if (numIndirectCalls)
95 std::cout << " In function " << F.getName() << ": "
96 << (totalNumCallees / (double) numIndirectCalls)
97 << " average callees per indirect call\n";
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000098}
99
Chris Lattner78925492003-09-20 01:20:46 +0000100DSNode *DSGraphStats::getNodeForValue(Value *V) {
101 const DSGraph *G = TDGraph;
Chris Lattner192cd9c2003-09-20 16:12:57 +0000102 if (isa<GlobalValue>(V) || isa<Constant>(V))
Chris Lattner78925492003-09-20 01:20:46 +0000103 G = TDGraph->getGlobalsGraph();
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000104
Chris Lattner192cd9c2003-09-20 16:12:57 +0000105 const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap();
106 DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V);
107 if (I != ScalarMap.end())
108 return I->second.getNode();
109 return 0;
110}
111
112bool DSGraphStats::isNodeForValueCollapsed(Value *V) {
113 if (DSNode *N = getNodeForValue(V))
Chris Lattner9970bf62003-09-20 21:48:01 +0000114 return N->isNodeCompletelyFolded() || N->isIncomplete();
Chris Lattner192cd9c2003-09-20 16:12:57 +0000115 return false;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000116}
117
Chris Lattner78925492003-09-20 01:20:46 +0000118void DSGraphStats::visitLoad(LoadInst &LI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000119 if (isNodeForValueCollapsed(LI.getOperand(0))) {
Chris Lattner78925492003-09-20 01:20:46 +0000120 NumUntypedMemAccesses++;
121 } else {
122 NumTypedMemAccesses++;
123 }
124}
125
126void DSGraphStats::visitStore(StoreInst &SI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000127 if (isNodeForValueCollapsed(SI.getOperand(1))) {
Chris Lattner78925492003-09-20 01:20:46 +0000128 NumUntypedMemAccesses++;
129 } else {
130 NumTypedMemAccesses++;
131 }
132}
133
134
135
136bool DSGraphStats::runOnFunction(Function& F) {
137 TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F);
138 countCallees(F);
139 visit(F);
140 return true;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000141}