Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 1 | //===- DataStructureStats.cpp - Various statistics for DS Graphs ----------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/InstVisitor.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 21 | #include <vector> |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 24 | namespace { |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 25 | Statistic<> TotalNumCallees("totalcallees", |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 26 | "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 Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 32 | // 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. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 40 | class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> { |
| 41 | void countCallees(const Function &F); |
| 42 | const DSGraph *TDGraph; |
| 43 | |
| 44 | DSNode *getNodeForValue(Value *V); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 45 | bool isNodeForValueCollapsed(Value *V); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 46 | 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 Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 56 | void visitLoad(LoadInst &LI); |
| 57 | void visitStore(StoreInst &SI); |
| 58 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 59 | /// Debugging support methods |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 60 | void print(std::ostream &O, const Module* = 0) const { } |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics"); |
| 64 | } |
| 65 | |
Chris Lattner | 6d79623 | 2005-10-24 00:38:38 +0000 | [diff] [blame^] | 66 | FunctionPass *llvm::createDataStructureStatsPass() { |
| 67 | return new DSGraphStats(); |
| 68 | } |
| 69 | |
| 70 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 71 | static bool isIndirectCallee(Value *V) { |
| 72 | if (isa<Function>(V)) return false; |
| 73 | |
| 74 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 75 | return isIndirectCallee(CI->getOperand(0)); |
| 76 | return true; |
| 77 | } |
| 78 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 80 | void DSGraphStats::countCallees(const Function& F) { |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 81 | unsigned numIndirectCalls = 0, totalNumCallees = 0; |
| 82 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 83 | for (DSGraph::fc_iterator I = TDGraph->fc_begin(), E = TDGraph->fc_end(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 84 | I != E; ++I) |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 85 | if (isIndirectCallee(I->getCallSite().getCalledValue())) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 86 | // This is an indirect function call |
Chris Lattner | 977b705 | 2005-03-20 02:40:27 +0000 | [diff] [blame] | 87 | std::vector<Function*> Callees; |
| 88 | I->getCalleeNode()->addFullFunctionList(Callees); |
| 89 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 90 | if (Callees.size() > 0) { |
| 91 | totalNumCallees += Callees.size(); |
| 92 | ++numIndirectCalls; |
| 93 | } else |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 94 | std::cerr << "WARNING: No callee in Function '" << F.getName() |
| 95 | << "' at call: \n" |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 96 | << *I->getCallSite().getInstruction(); |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 97 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 98 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 99 | TotalNumCallees += totalNumCallees; |
| 100 | NumIndirectCalls += numIndirectCalls; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 101 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 102 | if (numIndirectCalls) |
| 103 | std::cout << " In function " << F.getName() << ": " |
| 104 | << (totalNumCallees / (double) numIndirectCalls) |
| 105 | << " average callees per indirect call\n"; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 108 | DSNode *DSGraphStats::getNodeForValue(Value *V) { |
| 109 | const DSGraph *G = TDGraph; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 110 | if (isa<Constant>(V)) |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 111 | G = TDGraph->getGlobalsGraph(); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 113 | const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap(); |
| 114 | DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V); |
| 115 | if (I != ScalarMap.end()) |
| 116 | return I->second.getNode(); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | bool DSGraphStats::isNodeForValueCollapsed(Value *V) { |
| 121 | if (DSNode *N = getNodeForValue(V)) |
Chris Lattner | 9970bf6 | 2003-09-20 21:48:01 +0000 | [diff] [blame] | 122 | return N->isNodeCompletelyFolded() || N->isIncomplete(); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 123 | return false; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 126 | void DSGraphStats::visitLoad(LoadInst &LI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 127 | if (isNodeForValueCollapsed(LI.getOperand(0))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 128 | NumUntypedMemAccesses++; |
| 129 | } else { |
| 130 | NumTypedMemAccesses++; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void DSGraphStats::visitStore(StoreInst &SI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 135 | if (isNodeForValueCollapsed(SI.getOperand(1))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 136 | NumUntypedMemAccesses++; |
| 137 | } else { |
| 138 | NumTypedMemAccesses++; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | bool DSGraphStats::runOnFunction(Function& F) { |
| 145 | TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F); |
| 146 | countCallees(F); |
| 147 | visit(F); |
| 148 | return true; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 149 | } |