blob: a48223e0edfc43bbd45a32f6e03784654749326d [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>
Chris Lattner9a927292003-11-12 23:11:14 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000023namespace {
Chris Lattnered806bf2002-11-17 22:17:12 +000024 Statistic<> TotalNumCallees("totalcallees",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000025 "Total number of callee functions at all indirect call sites");
26 Statistic<> NumIndirectCalls("numindirect",
27 "Total number of indirect call sites in the program");
28 Statistic<> NumPoolNodes("numpools",
29 "Number of allocation nodes that could be pool allocated");
30
Chris Lattner78925492003-09-20 01:20:46 +000031 // Typed/Untyped memory accesses: If DSA can infer that the types the loads
32 // and stores are accessing are correct (ie, the node has not been collapsed),
33 // increment the appropriate counter.
34 Statistic<> NumTypedMemAccesses("numtypedmemaccesses",
35 "Number of loads/stores which are fully typed");
36 Statistic<> NumUntypedMemAccesses("numuntypedmemaccesses",
37 "Number of loads/stores which are untyped");
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000038
Chris Lattner78925492003-09-20 01:20:46 +000039 class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> {
40 void countCallees(const Function &F);
41 const DSGraph *TDGraph;
42
43 DSNode *getNodeForValue(Value *V);
Chris Lattner192cd9c2003-09-20 16:12:57 +000044 bool isNodeForValueCollapsed(Value *V);
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000045 public:
46 /// Driver functions to compute the Load/Store Dep. Graph per function.
47 bool runOnFunction(Function& F);
48
49 /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
50 void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.setPreservesAll();
52 AU.addRequired<TDDataStructures>();
53 }
54
Chris Lattner78925492003-09-20 01:20:46 +000055 void visitLoad(LoadInst &LI);
56 void visitStore(StoreInst &SI);
57
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000058 /// Debugging support methods
59 void print(std::ostream &O) const { }
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000060 };
61
62 static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
63}
64
Chris Lattnered806bf2002-11-17 22:17:12 +000065static bool isIndirectCallee(Value *V) {
66 if (isa<Function>(V)) return false;
67
68 if (CastInst *CI = dyn_cast<CastInst>(V))
69 return isIndirectCallee(CI->getOperand(0));
70 return true;
71}
72
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000073
Chris Lattner78925492003-09-20 01:20:46 +000074void DSGraphStats::countCallees(const Function& F) {
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000075 unsigned numIndirectCalls = 0, totalNumCallees = 0;
76
Chris Lattner78925492003-09-20 01:20:46 +000077 const std::vector<DSCallSite> &callSites = TDGraph->getFunctionCalls();
78 for (unsigned i = 0, N = callSites.size(); i != N; ++i)
Chris Lattner808a7ae2003-09-20 16:34:13 +000079 if (isIndirectCallee(callSites[i].getCallSite().getCalledValue())) {
Chris Lattner78925492003-09-20 01:20:46 +000080 // This is an indirect function call
81 const std::vector<GlobalValue*> &Callees =
82 callSites[i].getCalleeNode()->getGlobals();
83 if (Callees.size() > 0) {
84 totalNumCallees += Callees.size();
85 ++numIndirectCalls;
86 } else
Chris Lattner808a7ae2003-09-20 16:34:13 +000087 std::cerr << "WARNING: No callee in Function '" << F.getName()
88 << "' at call: \n"
89 << *callSites[i].getCallSite().getInstruction();
Chris Lattner78925492003-09-20 01:20:46 +000090 }
91
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000092 TotalNumCallees += totalNumCallees;
93 NumIndirectCalls += numIndirectCalls;
Chris Lattner78925492003-09-20 01:20:46 +000094
Chris Lattnered806bf2002-11-17 22:17:12 +000095 if (numIndirectCalls)
96 std::cout << " In function " << F.getName() << ": "
97 << (totalNumCallees / (double) numIndirectCalls)
98 << " average callees per indirect call\n";
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000099}
100
Chris Lattner78925492003-09-20 01:20:46 +0000101DSNode *DSGraphStats::getNodeForValue(Value *V) {
102 const DSGraph *G = TDGraph;
Chris Lattner192cd9c2003-09-20 16:12:57 +0000103 if (isa<GlobalValue>(V) || isa<Constant>(V))
Chris Lattner78925492003-09-20 01:20:46 +0000104 G = TDGraph->getGlobalsGraph();
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000105
Chris Lattner192cd9c2003-09-20 16:12:57 +0000106 const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap();
107 DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V);
108 if (I != ScalarMap.end())
109 return I->second.getNode();
110 return 0;
111}
112
113bool DSGraphStats::isNodeForValueCollapsed(Value *V) {
114 if (DSNode *N = getNodeForValue(V))
Chris Lattner9970bf62003-09-20 21:48:01 +0000115 return N->isNodeCompletelyFolded() || N->isIncomplete();
Chris Lattner192cd9c2003-09-20 16:12:57 +0000116 return false;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000117}
118
Chris Lattner78925492003-09-20 01:20:46 +0000119void DSGraphStats::visitLoad(LoadInst &LI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000120 if (isNodeForValueCollapsed(LI.getOperand(0))) {
Chris Lattner78925492003-09-20 01:20:46 +0000121 NumUntypedMemAccesses++;
122 } else {
123 NumTypedMemAccesses++;
124 }
125}
126
127void DSGraphStats::visitStore(StoreInst &SI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000128 if (isNodeForValueCollapsed(SI.getOperand(1))) {
Chris Lattner78925492003-09-20 01:20:46 +0000129 NumUntypedMemAccesses++;
130 } else {
131 NumTypedMemAccesses++;
132 }
133}
134
135
136
137bool DSGraphStats::runOnFunction(Function& F) {
138 TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F);
139 countCallees(F);
140 visit(F);
141 return true;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000142}