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