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/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"
32 // Helper for dumping edges to dbgs().
33 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
34 const BasicBlock *> E);
36 /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of
37 /// profiling data to control flow edges.
39 /// An edge is defined by its source and sink basic blocks.
40 template<class FType, class BType>
43 // The profiling information defines an Edge by its source and sink basic
45 typedef std::pair<const BType*, const BType*> Edge;
48 typedef DenseMap<Edge, unsigned> EdgeWeights;
50 /// \brief Count the number of times a transition between two blocks is
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;
58 /// getFunction() - Returns the Function for an Edge.
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();
67 /// getEdge() - Creates an Edge between two BasicBlocks.
68 static Edge getEdge(const BType *Src, const BType *Dest) {
69 return Edge(Src, Dest);
72 /// getEdgeWeight - Return the number of times that a given edge was
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;
80 assert((weights.find(e) != weights.end())
81 && "No profiling information for edge");
82 return weights.find(e)->second;
85 /// addEdgeWeight - Add 'weight' to the already stored execution count for
87 void addEdgeWeight(Edge e, unsigned weight) {
88 EdgeInformation[getFunction(e)][e] += weight;
92 typedef ProfileDataT<Function, BasicBlock> ProfileData;
93 //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData;
95 /// The ProfileDataLoader class is used to load raw profiling data from the
97 class ProfileDataLoader {
99 /// The name of the file where the raw profiling data is stored.
100 const std::string &Filename;
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;
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;
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);
115 /// A special value used to represent the weight of an edge which has not
116 /// been counted yet.
117 static const unsigned Uncounted;
119 /// getNumExecutions - Return the number of times the target program was run
120 /// to generate this profiling data.
121 unsigned getNumExecutions() const { return CommandLines.size(); }
123 /// getExecution - Return the command line parameters used to generate the
124 /// i'th set of profiling data.
125 const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
127 const std::string &getFileName() const { return Filename; }
129 /// getRawEdgeCounts - Return the raw profiling data, this is just a list of
130 /// numbers with no mappings to edges.
131 ArrayRef<unsigned> getRawEdgeCounts() const { return EdgeCounts; }
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);
138 } // End llvm namespace