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 // Helper for dumping edges to dbgs().
32 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
33 const BasicBlock *> E);
35 /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of
36 /// profiling data to control flow edges.
38 /// An edge is defined by its source and sink basic blocks.
39 template<class FType, class BType>
42 // The profiling information defines an Edge by its source and sink basic
44 typedef std::pair<const BType*, const BType*> Edge;
47 typedef DenseMap<Edge, unsigned> EdgeWeights;
49 /// \brief Count the number of times a transition between two blocks is
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;
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();
66 /// getEdge() - Creates an Edge between two BasicBlocks.
67 static Edge getEdge(const BType *Src, const BType *Dest) {
68 return Edge(Src, Dest);
71 /// getEdgeWeight - Return the number of times that a given edge was
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;
79 assert((weights.find(e) != weights.end())
80 && "No profiling information for edge");
81 return weights.find(e)->second;
84 /// addEdgeWeight - Add 'weight' to the already stored execution count for
86 void addEdgeWeight(Edge e, unsigned weight) {
87 EdgeInformation[getFunction(e)][e] += weight;
91 typedef ProfileDataT<Function, BasicBlock> ProfileData;
92 //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData;
94 /// The ProfileDataLoader class is used to load raw profiling data from the
96 class ProfileDataLoader {
98 /// The name of the file where the raw profiling data is stored.
99 const std::string &Filename;
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;
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;
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);
114 /// A special value used to represent the weight of an edge which has not
115 /// been counted yet.
116 static const unsigned Uncounted;
118 /// The maximum value that can be stored in a profiling counter.
119 static const unsigned MaxCount;
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(); }
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]; }
129 const std::string &getFileName() const { return Filename; }
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; }
136 /// createProfileMetadataLoaderPass - This function returns a Pass that loads
137 /// the profiling information for the module from the specified filename.
138 ModulePass *createProfileMetadataLoaderPass(const std::string &Filename);
140 } // End llvm namespace