1 //===- ProfileDataLoader.h - Load & convert profile info ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // The ProfileDataLoader class is used to load profiling data from a dump file. 11 // The ProfileDataT<FType, BType> class is used to store the mapping of this 12 // data to control flow edges. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H 17 #define LLVM_ANALYSIS_PROFILEDATALOADER_H 18 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include <string> 25 26 namespace llvm { 27 28 class ModulePass; 29 class Function; 30 class BasicBlock; 31 32 // Helper for dumping edges to dbgs(). 33 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, 34 const BasicBlock *> E); 35 36 /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of 37 /// profiling data to control flow edges. 38 /// 39 /// An edge is defined by its source and sink basic blocks. 40 template<class FType, class BType> 41 class ProfileDataT { 42 public: 43 // The profiling information defines an Edge by its source and sink basic 44 // blocks. 45 typedef std::pair<const BType*, const BType*> Edge; 46 47 private: 48 typedef DenseMap<Edge, unsigned> EdgeWeights; 49 50 /// \brief Count the number of times a transition between two blocks is 51 /// executed. 52 /// 53 /// As a special case, we also hold an edge from the null BasicBlock to the 54 /// entry block to indicate how many times the function was entered. 55 DenseMap<const FType*, EdgeWeights> EdgeInformation; 56 57 public: 58 /// getFunction() - Returns the Function for an Edge. getFunction(Edge e)59 static const FType *getFunction(Edge e) { 60 // e.first may be NULL 61 assert(((!e.first) || (e.first->getParent() == e.second->getParent())) 62 && "A ProfileData::Edge can not be between two functions"); 63 assert(e.second && "A ProfileData::Edge must have a real sink"); 64 return e.second->getParent(); 65 } 66 67 /// getEdge() - Creates an Edge between two BasicBlocks. getEdge(const BType * Src,const BType * Dest)68 static Edge getEdge(const BType *Src, const BType *Dest) { 69 return Edge(Src, Dest); 70 } 71 72 /// getEdgeWeight - Return the number of times that a given edge was 73 /// executed. getEdgeWeight(Edge e)74 unsigned getEdgeWeight(Edge e) const { 75 const FType *f = getFunction(e); 76 assert((EdgeInformation.find(f) != EdgeInformation.end()) 77 && "No profiling information for function"); 78 EdgeWeights weights = EdgeInformation.find(f)->second; 79 80 assert((weights.find(e) != weights.end()) 81 && "No profiling information for edge"); 82 return weights.find(e)->second; 83 } 84 85 /// addEdgeWeight - Add 'weight' to the already stored execution count for 86 /// this edge. addEdgeWeight(Edge e,unsigned weight)87 void addEdgeWeight(Edge e, unsigned weight) { 88 EdgeInformation[getFunction(e)][e] += weight; 89 } 90 }; 91 92 typedef ProfileDataT<Function, BasicBlock> ProfileData; 93 //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData; 94 95 /// The ProfileDataLoader class is used to load raw profiling data from the 96 /// dump file. 97 class ProfileDataLoader { 98 private: 99 /// The name of the file where the raw profiling data is stored. 100 const std::string &Filename; 101 102 /// A vector of the command line arguments used when the target program was 103 /// run to generate profiling data. One entry per program run. 104 SmallVector<std::string, 1> CommandLines; 105 106 /// The raw values for how many times each edge was traversed, values from 107 /// multiple program runs are accumulated. 108 SmallVector<unsigned, 32> EdgeCounts; 109 110 public: 111 /// ProfileDataLoader ctor - Read the specified profiling data file, exiting 112 /// the program if the file is invalid or broken. 113 ProfileDataLoader(const char *ToolName, const std::string &Filename); 114 115 /// A special value used to represent the weight of an edge which has not 116 /// been counted yet. 117 static const unsigned Uncounted; 118 119 /// getNumExecutions - Return the number of times the target program was run 120 /// to generate this profiling data. getNumExecutions()121 unsigned getNumExecutions() const { return CommandLines.size(); } 122 123 /// getExecution - Return the command line parameters used to generate the 124 /// i'th set of profiling data. getExecution(unsigned i)125 const std::string &getExecution(unsigned i) const { return CommandLines[i]; } 126 getFileName()127 const std::string &getFileName() const { return Filename; } 128 129 /// getRawEdgeCounts - Return the raw profiling data, this is just a list of 130 /// numbers with no mappings to edges. getRawEdgeCounts()131 ArrayRef<unsigned> getRawEdgeCounts() const { return EdgeCounts; } 132 }; 133 134 /// createProfileMetadataLoaderPass - This function returns a Pass that loads 135 /// the profiling information for the module from the specified filename. 136 ModulePass *createProfileMetadataLoaderPass(const std::string &Filename); 137 138 } // End llvm namespace 139 140 #endif 141