Cleanups due to feedback. No functionality change. Patch by Alistair.
[oota-llvm.git] / include / llvm / Analysis / ProfileDataLoader.h
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
25 namespace llvm {
26
27 class ModulePass;
28 class Function;
29 class BasicBlock;
30
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);
36
37 /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of
38 /// profiling data to control flow edges.
39 ///
40 /// An edge is defined by its source and sink basic blocks.
41 template<class FType, class BType>
42 class ProfileDataT {
43   public:
44   // The profiling information defines an Edge by its source and sink basic
45   // blocks.
46   typedef std::pair<const BType*, const BType*> Edge;
47
48   private:
49   typedef DenseMap<Edge, unsigned> EdgeWeights;
50
51   /// \brief Count the number of times a transition between two blocks is
52   /// executed.
53   ///
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;
57
58   public:
59   static char ID; // Class identification, replacement for typeinfo
60   ProfileDataT() {};
61   ~ProfileDataT() {};
62
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();
70   }
71
72   /// getEdge() - Creates an Edge between two BasicBlocks.
73   static Edge getEdge(const BType *Src, const BType *Dest) {
74     return Edge(Src, Dest);
75   }
76
77   /// getEdgeWeight - Return the number of times that a given edge was
78   /// executed.
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;
84
85     assert((weights.find(e) != weights.end())
86            && "No profiling information for edge");
87     return weights.find(e)->second;
88   }
89
90   /// addEdgeWeight - Add 'weight' to the already stored execution count for
91   /// this edge.
92   void addEdgeWeight(Edge e, unsigned weight) {
93       EdgeInformation[getFunction(e)][e] += weight;
94   }
95 };
96
97 typedef ProfileDataT<Function, BasicBlock> ProfileData;
98 //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData;
99
100 /// The ProfileDataLoader class is used to load raw profiling data from the
101 /// dump file.
102 class ProfileDataLoader {
103 private:
104   /// The name of the file where the raw profiling data is stored.
105   const std::string &Filename;
106
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;
110
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;
114
115 public:
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);
119
120   /// A special value used to represent the weight of an edge which has not
121   /// been counted yet.
122   static const unsigned Uncounted;
123
124   /// The maximum value that can be stored in a profiling counter.
125   static const unsigned MaxCount;
126
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(); }
130
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]; }
134
135   const std::string &getFileName() const { return Filename; }
136
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; }
140 };
141
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);
145
146 } // End llvm namespace
147
148 #endif