Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 1 | //===- DataStructureStats.cpp - Various statistics for DS Graphs ----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 10 | // This file defines a little pass that prints out statistics for DS Graphs. |
| 11 | // |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
| 15 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/iOther.h" |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 18 | #include "llvm/iMemory.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/InstVisitor.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 21 | #include "Support/Statistic.h" |
| 22 | #include <vector> |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 25 | namespace { |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 26 | Statistic<> TotalNumCallees("totalcallees", |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 27 | "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 Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 33 | // 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. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 41 | class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> { |
| 42 | void countCallees(const Function &F); |
| 43 | const DSGraph *TDGraph; |
| 44 | |
| 45 | DSNode *getNodeForValue(Value *V); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 46 | bool isNodeForValueCollapsed(Value *V); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 47 | 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 Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 57 | void visitLoad(LoadInst &LI); |
| 58 | void visitStore(StoreInst &SI); |
| 59 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 60 | /// Debugging support methods |
| 61 | void print(std::ostream &O) const { } |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics"); |
| 65 | } |
| 66 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 67 | static 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. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 76 | void DSGraphStats::countCallees(const Function& F) { |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 77 | unsigned numIndirectCalls = 0, totalNumCallees = 0; |
| 78 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 79 | const std::vector<DSCallSite> &callSites = TDGraph->getFunctionCalls(); |
| 80 | for (unsigned i = 0, N = callSites.size(); i != N; ++i) |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 81 | if (isIndirectCallee(callSites[i].getCallSite().getCalledValue())) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 82 | // 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 Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 89 | std::cerr << "WARNING: No callee in Function '" << F.getName() |
| 90 | << "' at call: \n" |
| 91 | << *callSites[i].getCallSite().getInstruction(); |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 94 | TotalNumCallees += totalNumCallees; |
| 95 | NumIndirectCalls += numIndirectCalls; |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 96 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 97 | if (numIndirectCalls) |
| 98 | std::cout << " In function " << F.getName() << ": " |
| 99 | << (totalNumCallees / (double) numIndirectCalls) |
| 100 | << " average callees per indirect call\n"; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 103 | DSNode *DSGraphStats::getNodeForValue(Value *V) { |
| 104 | const DSGraph *G = TDGraph; |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 105 | if (isa<GlobalValue>(V) || isa<Constant>(V)) |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 106 | G = TDGraph->getGlobalsGraph(); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 108 | 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 | |
| 115 | bool DSGraphStats::isNodeForValueCollapsed(Value *V) { |
| 116 | if (DSNode *N = getNodeForValue(V)) |
Chris Lattner | 9970bf6 | 2003-09-20 21:48:01 +0000 | [diff] [blame] | 117 | return N->isNodeCompletelyFolded() || N->isIncomplete(); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 118 | return false; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 121 | void DSGraphStats::visitLoad(LoadInst &LI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 122 | if (isNodeForValueCollapsed(LI.getOperand(0))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 123 | NumUntypedMemAccesses++; |
| 124 | } else { |
| 125 | NumTypedMemAccesses++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void DSGraphStats::visitStore(StoreInst &SI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 130 | if (isNodeForValueCollapsed(SI.getOperand(1))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 131 | NumUntypedMemAccesses++; |
| 132 | } else { |
| 133 | NumTypedMemAccesses++; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | bool DSGraphStats::runOnFunction(Function& F) { |
| 140 | TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F); |
| 141 | countCallees(F); |
| 142 | visit(F); |
| 143 | return true; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 144 | } |