blob: f7fbe3b33cdfdd1f75d2a6c2a416c407a2b8af8e [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"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000020#include "llvm/Support/Streams.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000022#include <ostream>
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 Lattnerac0b6ae2006-12-06 17:46:33 +000026 Statistic TotalNumCallees("totalcallees",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000027 "Total number of callee functions at all indirect call sites");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000028 Statistic NumIndirectCalls("numindirect",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000029 "Total number of indirect call sites in the program");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030 Statistic NumPoolNodes("numpools",
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000031 "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.
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000036 Statistic NumTypedMemAccesses("numtypedmemaccesses",
Chris Lattner78925492003-09-20 01:20:46 +000037 "Number of loads/stores which are fully typed");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000038 Statistic NumUntypedMemAccesses("numuntypedmemaccesses",
Chris Lattner78925492003-09-20 01:20:46 +000039 "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
Reid Spencerce9653c2004-12-07 04:03:45 +000061 void print(std::ostream &O, const Module* = 0) const { }
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000062 };
63
Chris Lattner5d8925c2006-08-27 22:30:17 +000064 static RegisterPass<DSGraphStats> Z("dsstats", "DS Graph Statistics");
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000065}
66
Chris Lattner6d796232005-10-24 00:38:38 +000067FunctionPass *llvm::createDataStructureStatsPass() {
68 return new DSGraphStats();
69}
70
71
Chris Lattnered806bf2002-11-17 22:17:12 +000072static bool isIndirectCallee(Value *V) {
73 if (isa<Function>(V)) return false;
74
75 if (CastInst *CI = dyn_cast<CastInst>(V))
76 return isIndirectCallee(CI->getOperand(0));
77 return true;
78}
79
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000080
Chris Lattner78925492003-09-20 01:20:46 +000081void DSGraphStats::countCallees(const Function& F) {
Vikram S. Adve586aa3c2002-11-13 15:41:00 +000082 unsigned numIndirectCalls = 0, totalNumCallees = 0;
83
Chris Lattnera9548d92005-01-30 23:51:02 +000084 for (DSGraph::fc_iterator I = TDGraph->fc_begin(), E = TDGraph->fc_end();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000085 I != E; ++I)
Chris Lattnera9548d92005-01-30 23:51:02 +000086 if (isIndirectCallee(I->getCallSite().getCalledValue())) {
Chris Lattner78925492003-09-20 01:20:46 +000087 // This is an indirect function call
Chris Lattner977b7052005-03-20 02:40:27 +000088 std::vector<Function*> Callees;
89 I->getCalleeNode()->addFullFunctionList(Callees);
90
Chris Lattner78925492003-09-20 01:20:46 +000091 if (Callees.size() > 0) {
92 totalNumCallees += Callees.size();
93 ++numIndirectCalls;
94 } else
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000095 llvm_cerr << "WARNING: No callee in Function '" << F.getName()
Chris Lattner808a7ae2003-09-20 16:34:13 +000096 << "' at call: \n"
Chris Lattnera9548d92005-01-30 23:51:02 +000097 << *I->getCallSite().getInstruction();
Chris Lattner78925492003-09-20 01:20:46 +000098 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000099
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000100 TotalNumCallees += totalNumCallees;
101 NumIndirectCalls += numIndirectCalls;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000102
Chris Lattnered806bf2002-11-17 22:17:12 +0000103 if (numIndirectCalls)
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000104 llvm_cout << " In function " << F.getName() << ": "
Chris Lattnered806bf2002-11-17 22:17:12 +0000105 << (totalNumCallees / (double) numIndirectCalls)
106 << " average callees per indirect call\n";
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000107}
108
Chris Lattner78925492003-09-20 01:20:46 +0000109DSNode *DSGraphStats::getNodeForValue(Value *V) {
110 const DSGraph *G = TDGraph;
Reid Spencere8404342004-07-18 00:18:30 +0000111 if (isa<Constant>(V))
Chris Lattner78925492003-09-20 01:20:46 +0000112 G = TDGraph->getGlobalsGraph();
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000113
Chris Lattner192cd9c2003-09-20 16:12:57 +0000114 const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap();
115 DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V);
116 if (I != ScalarMap.end())
117 return I->second.getNode();
118 return 0;
119}
120
121bool DSGraphStats::isNodeForValueCollapsed(Value *V) {
122 if (DSNode *N = getNodeForValue(V))
Chris Lattner9970bf62003-09-20 21:48:01 +0000123 return N->isNodeCompletelyFolded() || N->isIncomplete();
Chris Lattner192cd9c2003-09-20 16:12:57 +0000124 return false;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000125}
126
Chris Lattner78925492003-09-20 01:20:46 +0000127void DSGraphStats::visitLoad(LoadInst &LI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000128 if (isNodeForValueCollapsed(LI.getOperand(0))) {
Chris Lattner78925492003-09-20 01:20:46 +0000129 NumUntypedMemAccesses++;
130 } else {
131 NumTypedMemAccesses++;
132 }
133}
134
135void DSGraphStats::visitStore(StoreInst &SI) {
Chris Lattner192cd9c2003-09-20 16:12:57 +0000136 if (isNodeForValueCollapsed(SI.getOperand(1))) {
Chris Lattner78925492003-09-20 01:20:46 +0000137 NumUntypedMemAccesses++;
138 } else {
139 NumTypedMemAccesses++;
140 }
141}
142
143
144
145bool DSGraphStats::runOnFunction(Function& F) {
146 TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F);
147 countCallees(F);
148 visit(F);
149 return true;
Vikram S. Adve586aa3c2002-11-13 15:41:00 +0000150}