blob: 18e97cb018a29d16e8b79afc9224ab990bf7d0e5 [file] [log] [blame]
Chris Lattner4dabb2c2004-07-07 06:32:21 +00001//===- DataStructureStats.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//
Chris Lattner4dabb2c2004-07-07 06:32:21 +000010// This file defines a little pass that prints out statistics for DS Graphs.
11//
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000012//===----------------------------------------------------------------------===//
13
Chris Lattner4dabb2c2004-07-07 06:32:21 +000014#include "llvm/Analysis/DataStructure/DataStructure.h"
15#include "llvm/Analysis/DataStructure/DSGraph.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000016#include "llvm/Function.h"
17#include "llvm/iOther.h"
Chris Lattner78925492003-09-20 01:20:46 +000018#include "llvm/iMemory.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000019#include "llvm/Pass.h"
Chris Lattner78925492003-09-20 01:20:46 +000020#include "llvm/Support/InstVisitor.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000021#include "Support/Statistic.h"
22#include <vector>
Chris Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000025namespace {
Chris Lattnered806bf2002-11-17 22:17:12 +000026 Statistic<> TotalNumCallees("totalcallees",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000027 "Total number of callee functions at all indirect call sites");
28 Statistic<> NumIndirectCalls("numindirect",
29 "Total number of indirect call sites in the program");
30 Statistic<> NumPoolNodes("numpools",
31 "Number of allocation nodes that could be pool allocated");
32
Chris Lattner78925492003-09-20 01:20:46 +000033 // Typed/Untyped memory accesses: If DSA can infer that the types the loads
34 // and stores are accessing are correct (ie, the node has not been collapsed),
35 // increment the appropriate counter.
36 Statistic<> NumTypedMemAccesses("numtypedmemaccesses",
37 "Number of loads/stores which are fully typed");
38 Statistic<> NumUntypedMemAccesses("numuntypedmemaccesses",
39 "Number of loads/stores which are untyped");
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000040
Chris Lattner78925492003-09-20 01:20:46 +000041 class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> {
42 void countCallees(const Function &F);
43 const DSGraph *TDGraph;
44
45 DSNode *getNodeForValue(Value *V);
Chris Lattner192cd9c2003-09-20 16:12:57 +000046 bool isNodeForValueCollapsed(Value *V);
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000047 public:
48 /// Driver functions to compute the Load/Store Dep. Graph per function.
49 bool runOnFunction(Function& F);
50
51 /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
52 void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 AU.addRequired<TDDataStructures>();
55 }
56
Chris Lattner78925492003-09-20 01:20:46 +000057 void visitLoad(LoadInst &LI);
58 void visitStore(StoreInst &SI);
59
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000060 /// Debugging support methods
61 void print(std::ostream &O) const { }
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000062 };
63
64 static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
65}
66
Chris Lattnered806bf2002-11-17 22:17:12 +000067static bool isIndirectCallee(Value *V) {
68 if (isa<Function>(V)) return false;
69
70 if (CastInst *CI = dyn_cast<CastInst>(V))
71 return isIndirectCallee(CI->getOperand(0));
72 return true;
73}
74
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000075
Chris Lattner78925492003-09-20 01:20:46 +000076void DSGraphStats::countCallees(const Function& F) {
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000077 unsigned numIndirectCalls = 0, totalNumCallees = 0;
78
Chris Lattner78925492003-09-20 01:20:46 +000079 const std::vector<DSCallSite> &callSites = TDGraph->getFunctionCalls();
80 for (unsigned i = 0, N = callSites.size(); i != N; ++i)
Chris Lattner808a7ae2003-09-20 16:34:13 +000081 if (isIndirectCallee(callSites[i].getCallSite().getCalledValue())) {
Chris Lattner78925492003-09-20 01:20:46 +000082 // This is an indirect function call
83 const std::vector<GlobalValue*> &Callees =
84 callSites[i].getCalleeNode()->getGlobals();
85 if (Callees.size() > 0) {
86 totalNumCallees += Callees.size();
87 ++numIndirectCalls;
88 } else
Chris Lattner808a7ae2003-09-20 16:34:13 +000089 std::cerr << "WARNING: No callee in Function '" << F.getName()
90 << "' at call: \n"
91 << *callSites[i].getCallSite().getInstruction();
Chris Lattner78925492003-09-20 01:20:46 +000092 }
93
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000094 TotalNumCallees += totalNumCallees;
95 NumIndirectCalls += numIndirectCalls;
Chris Lattner78925492003-09-20 01:20:46 +000096
Chris Lattnered806bf2002-11-17 22:17:12 +000097 if (numIndirectCalls)
98 std::cout << " In function " << F.getName() << ": "
99 << (totalNumCallees / (double) numIndirectCalls)
100 << " average callees per indirect call\n";
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000101}
102
Chris Lattner78925492003-09-20 01:20:46 +0000103DSNode *DSGraphStats::getNodeForValue(Value *V) {
104 const DSGraph *G = TDGraph;
Reid Spencere8404342004-07-18 00:18:30 +0000105 if (isa<Constant>(V))
Chris Lattner78925492003-09-20 01:20:46 +0000106 G = TDGraph->getGlobalsGraph();
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000107
Chris Lattner192cd9c2003-09-20 16:12:57 +0000108 const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap();
109 DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V);
110 if (I != ScalarMap.end())
111 return I->second.getNode();
112 return 0;
113}
114
115bool DSGraphStats::isNodeForValueCollapsed(Value *V) {
116 if (DSNode *N = getNodeForValue(V))
Chris Lattner9970bf62003-09-20 21:48:01 +0000117 return N->isNodeCompletelyFolded() || N->isIncomplete();
Chris Lattner192cd9c2003-09-20 16:12:57 +0000118 return false;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000119}
120
Chris Lattner78925492003-09-20 01:20:46 +0000121void DSGraphStats::visitLoad(LoadInst &LI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000122 if (isNodeForValueCollapsed(LI.getOperand(0))) {
Chris Lattner78925492003-09-20 01:20:46 +0000123 NumUntypedMemAccesses++;
124 } else {
125 NumTypedMemAccesses++;
126 }
127}
128
129void DSGraphStats::visitStore(StoreInst &SI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000130 if (isNodeForValueCollapsed(SI.getOperand(1))) {
Chris Lattner78925492003-09-20 01:20:46 +0000131 NumUntypedMemAccesses++;
132 } else {
133 NumTypedMemAccesses++;
134 }
135}
136
137
138
139bool DSGraphStats::runOnFunction(Function& F) {
140 TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F);
141 countCallees(F);
142 visit(F);
143 return true;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000144}