1 //===- ProfileDataLoader.h - Load & convert profile info ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H
17 #define LLVM_ANALYSIS_PROFILEDATALOADER_H
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
31 // Helpers for dumping edges to dbgs().
32 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
33 const BasicBlock *> E);
34 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
35 raw_ostream& operator<<(raw_ostream &O, const Function *F);
37 /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of
38 /// profiling data to control flow edges.
40 /// An edge is defined by its source and sink basic blocks.
41 template<class FType, class BType>
44 // The profiling information defines an Edge by its source and sink basic
46 typedef std::pair<const BType*, const BType*> Edge;
49 typedef DenseMap<Edge, unsigned> EdgeWeights;
51 /// \brief Count the number of times a transition between two blocks is
54 /// As a special case, we also hold an edge from the null BasicBlock to the
55 /// entry block to indicate how many times the function was entered.
56 DenseMap<const FType*, EdgeWeights> EdgeInformation;
59 static char ID; // Class identification, replacement for typeinfo
63 /// getFunction() - Returns the Function for an Edge.
64 static const FType *getFunction(Edge e) {
65 // e.first may be NULL
66 assert(((!e.first) || (e.first->getParent() == e.second->getParent()))
67 && "A ProfileData::Edge can not be between two functions");
68 assert(e.second && "A ProfileData::Edge must have a real sink");
69 return e.second->getParent();
72 /// getEdge() - Creates an Edge between two BasicBlocks.
73 static Edge getEdge(const BType *Src, const BType *Dest) {
74 return Edge(Src, Dest);
77 /// getEdgeWeight - Return the number of times that a given edge was
79 unsigned getEdgeWeight(Edge e) const {
80 const FType *f = getFunction(e);
81 assert((EdgeInformation.find(f) != EdgeInformation.end())
82 && "No profiling information for function");
83 EdgeWeights weights = EdgeInformation.find(f)->second;
85 assert((weights.find(e) != weights.end())
86 && "No profiling information for edge");
87 return weights.find(e)->second;
90 /// addEdgeWeight - Add 'weight' to the already stored execution count for
92 void addEdgeWeight(Edge e, unsigned weight) {
93 EdgeInformation[getFunction(e)][e] += weight;
97 typedef ProfileDataT<Function, BasicBlock> ProfileData;
98 //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData;
100 /// The ProfileDataLoader class is used to load raw profiling data from the
102 class ProfileDataLoader {
104 /// The name of the file where the raw profiling data is stored.
105 const std::string &Filename;
107 /// A vector of the command line arguments used when the target program was
108 /// run to generate profiling data. One entry per program run.
109 SmallVector<std::string, 1> CommandLines;
111 /// The raw values for how many times each edge was traversed, values from
112 /// multiple program runs are accumulated.
113 SmallVector<unsigned, 32> EdgeCounts;
116 /// ProfileDataLoader ctor - Read the specified profiling data file, exiting
117 /// the program if the file is invalid or broken.
118 ProfileDataLoader(const char *ToolName, const std::string &Filename);
120 /// A special value used to represent the weight of an edge which has not
121 /// been counted yet.
122 static const unsigned Uncounted;
124 /// The maximum value that can be stored in a profiling counter.
125 static const unsigned MaxCount;
127 /// getNumExecutions - Return the number of times the target program was run
128 /// to generate this profiling data.
129 unsigned getNumExecutions() const { return CommandLines.size(); }
131 /// getExecution - Return the command line parameters used to generate the
132 /// i'th set of profiling data.
133 const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
135 const std::string &getFileName() const { return Filename; }
137 /// getRawEdgeCounts - Return the raw profiling data, this is just a list of
138 /// numbers with no mappings to edges.
139 const SmallVector<unsigned, 32> &getRawEdgeCounts() const { return EdgeCounts; }
142 /// createProfileMetadataLoaderPass - This function returns a Pass that loads
143 /// the profiling information for the module from the specified filename.
144 ModulePass *createProfileMetadataLoaderPass(const std::string &Filename);
146 } // End llvm namespace