blob: d86c2a24b0bd7547a26d29aa7f2a67664cb09a02 [file] [log] [blame]
Chris Lattner4dabb2c2004-07-07 06:32:21 +00001//===- DataStructureStats.cpp - Various statistics for DS Graphs ----------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Misha Brukmand8e1eea2004-07-29 17:05:13 +000017#include "llvm/Instructions.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000018#include "llvm/Pass.h"
Chris Lattner78925492003-09-20 01:20:46 +000019#include "llvm/Support/InstVisitor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/Statistic.h"
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000021#include <vector>
Chris Lattner9a927292003-11-12 23:11:14 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000024namespace {
Chris Lattnered806bf2002-11-17 22:17:12 +000025 Statistic<> TotalNumCallees("totalcallees",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000026 "Total number of callee functions at all indirect call sites");
27 Statistic<> NumIndirectCalls("numindirect",
28 "Total number of indirect call sites in the program");
29 Statistic<> NumPoolNodes("numpools",
30 "Number of allocation nodes that could be pool allocated");
31
Chris Lattner78925492003-09-20 01:20:46 +000032 // Typed/Untyped memory accesses: If DSA can infer that the types the loads
33 // and stores are accessing are correct (ie, the node has not been collapsed),
34 // increment the appropriate counter.
35 Statistic<> NumTypedMemAccesses("numtypedmemaccesses",
36 "Number of loads/stores which are fully typed");
37 Statistic<> NumUntypedMemAccesses("numuntypedmemaccesses",
38 "Number of loads/stores which are untyped");
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000039
Chris Lattner78925492003-09-20 01:20:46 +000040 class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> {
41 void countCallees(const Function &F);
42 const DSGraph *TDGraph;
43
44 DSNode *getNodeForValue(Value *V);
Chris Lattner192cd9c2003-09-20 16:12:57 +000045 bool isNodeForValueCollapsed(Value *V);
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000046 public:
47 /// Driver functions to compute the Load/Store Dep. Graph per function.
48 bool runOnFunction(Function& F);
49
50 /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
51 void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
53 AU.addRequired<TDDataStructures>();
54 }
55
Chris Lattner78925492003-09-20 01:20:46 +000056 void visitLoad(LoadInst &LI);
57 void visitStore(StoreInst &SI);
58
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000059 /// Debugging support methods
Reid Spencerce9653c2004-12-07 04:03:45 +000060 void print(std::ostream &O, const Module* = 0) const { }
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000061 };
62
63 static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics");
64}
65
Chris Lattnered806bf2002-11-17 22:17:12 +000066static bool isIndirectCallee(Value *V) {
67 if (isa<Function>(V)) return false;
68
69 if (CastInst *CI = dyn_cast<CastInst>(V))
70 return isIndirectCallee(CI->getOperand(0));
71 return true;
72}
73
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000074
Chris Lattner78925492003-09-20 01:20:46 +000075void DSGraphStats::countCallees(const Function& F) {
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000076 unsigned numIndirectCalls = 0, totalNumCallees = 0;
77
Chris Lattnera9548d92005-01-30 23:51:02 +000078 for (DSGraph::fc_iterator I = TDGraph->fc_begin(), E = TDGraph->fc_end();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000079 I != E; ++I)
Chris Lattnera9548d92005-01-30 23:51:02 +000080 if (isIndirectCallee(I->getCallSite().getCalledValue())) {
Chris Lattner78925492003-09-20 01:20:46 +000081 // This is an indirect function call
Chris Lattner977b7052005-03-20 02:40:27 +000082 std::vector<Function*> Callees;
83 I->getCalleeNode()->addFullFunctionList(Callees);
84
Chris Lattner78925492003-09-20 01:20:46 +000085 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"
Chris Lattnera9548d92005-01-30 23:51:02 +000091 << *I->getCallSite().getInstruction();
Chris Lattner78925492003-09-20 01:20:46 +000092 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000093
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000094 TotalNumCallees += totalNumCallees;
95 NumIndirectCalls += numIndirectCalls;
Misha Brukman2b37d7c2005-04-21 21:13:18 +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}