Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 1 | //===- DSGraphStats.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 | // |
| 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 Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 16 | #include "llvm/iMemory.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/InstVisitor.h" |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 19 | #include "Support/Statistic.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace { |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 23 | Statistic<> TotalNumCallees("totalcallees", |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 24 | "Total number of callee functions at all indirect call sites"); |
| 25 | Statistic<> NumIndirectCalls("numindirect", |
| 26 | "Total number of indirect call sites in the program"); |
| 27 | Statistic<> NumPoolNodes("numpools", |
| 28 | "Number of allocation nodes that could be pool allocated"); |
| 29 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 30 | // Typed/Untyped memory accesses: If DSA can infer that the types the loads |
| 31 | // and stores are accessing are correct (ie, the node has not been collapsed), |
| 32 | // increment the appropriate counter. |
| 33 | Statistic<> NumTypedMemAccesses("numtypedmemaccesses", |
| 34 | "Number of loads/stores which are fully typed"); |
| 35 | Statistic<> NumUntypedMemAccesses("numuntypedmemaccesses", |
| 36 | "Number of loads/stores which are untyped"); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 38 | class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> { |
| 39 | void countCallees(const Function &F); |
| 40 | const DSGraph *TDGraph; |
| 41 | |
| 42 | DSNode *getNodeForValue(Value *V); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 43 | bool isNodeForValueCollapsed(Value *V); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 44 | public: |
| 45 | /// Driver functions to compute the Load/Store Dep. Graph per function. |
| 46 | bool runOnFunction(Function& F); |
| 47 | |
| 48 | /// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph. |
| 49 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 50 | AU.setPreservesAll(); |
| 51 | AU.addRequired<TDDataStructures>(); |
| 52 | } |
| 53 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 54 | void visitLoad(LoadInst &LI); |
| 55 | void visitStore(StoreInst &SI); |
| 56 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 57 | /// Debugging support methods |
| 58 | void print(std::ostream &O) const { } |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static RegisterAnalysis<DSGraphStats> Z("dsstats", "DS Graph Statistics"); |
| 62 | } |
| 63 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 64 | static bool isIndirectCallee(Value *V) { |
| 65 | if (isa<Function>(V)) return false; |
| 66 | |
| 67 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 68 | return isIndirectCallee(CI->getOperand(0)); |
| 69 | return true; |
| 70 | } |
| 71 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 73 | void DSGraphStats::countCallees(const Function& F) { |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 74 | unsigned numIndirectCalls = 0, totalNumCallees = 0; |
| 75 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 76 | const std::vector<DSCallSite> &callSites = TDGraph->getFunctionCalls(); |
| 77 | for (unsigned i = 0, N = callSites.size(); i != N; ++i) |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 78 | if (isIndirectCallee(callSites[i].getCallSite().getCalledValue())) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 79 | // This is an indirect function call |
| 80 | const std::vector<GlobalValue*> &Callees = |
| 81 | callSites[i].getCalleeNode()->getGlobals(); |
| 82 | if (Callees.size() > 0) { |
| 83 | totalNumCallees += Callees.size(); |
| 84 | ++numIndirectCalls; |
| 85 | } else |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 86 | std::cerr << "WARNING: No callee in Function '" << F.getName() |
| 87 | << "' at call: \n" |
| 88 | << *callSites[i].getCallSite().getInstruction(); |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 91 | TotalNumCallees += totalNumCallees; |
| 92 | NumIndirectCalls += numIndirectCalls; |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 93 | |
Chris Lattner | ed806bf | 2002-11-17 22:17:12 +0000 | [diff] [blame] | 94 | if (numIndirectCalls) |
| 95 | std::cout << " In function " << F.getName() << ": " |
| 96 | << (totalNumCallees / (double) numIndirectCalls) |
| 97 | << " average callees per indirect call\n"; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 100 | DSNode *DSGraphStats::getNodeForValue(Value *V) { |
| 101 | const DSGraph *G = TDGraph; |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 102 | if (isa<GlobalValue>(V) || isa<Constant>(V)) |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 103 | G = TDGraph->getGlobalsGraph(); |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 105 | const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap(); |
| 106 | DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V); |
| 107 | if (I != ScalarMap.end()) |
| 108 | return I->second.getNode(); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | bool DSGraphStats::isNodeForValueCollapsed(Value *V) { |
| 113 | if (DSNode *N = getNodeForValue(V)) |
Chris Lattner | 9970bf6 | 2003-09-20 21:48:01 +0000 | [diff] [blame] | 114 | return N->isNodeCompletelyFolded() || N->isIncomplete(); |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 115 | return false; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 118 | void DSGraphStats::visitLoad(LoadInst &LI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 119 | if (isNodeForValueCollapsed(LI.getOperand(0))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 120 | NumUntypedMemAccesses++; |
| 121 | } else { |
| 122 | NumTypedMemAccesses++; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void DSGraphStats::visitStore(StoreInst &SI) { |
Chris Lattner | 192cd9c | 2003-09-20 16:12:57 +0000 | [diff] [blame] | 127 | if (isNodeForValueCollapsed(SI.getOperand(1))) { |
Chris Lattner | 7892549 | 2003-09-20 01:20:46 +0000 | [diff] [blame] | 128 | NumUntypedMemAccesses++; |
| 129 | } else { |
| 130 | NumTypedMemAccesses++; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | bool DSGraphStats::runOnFunction(Function& F) { |
| 137 | TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F); |
| 138 | countCallees(F); |
| 139 | visit(F); |
| 140 | return true; |
Vikram S. Adve | 586aa3c | 2002-11-13 15:41:00 +0000 | [diff] [blame] | 141 | } |