1 //===- llvm/Analysis/ProfileInfo.h - Profile Info Interface -----*- 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 // This file defines the generic ProfileInfo interface, which is used as the
11 // common interface used by all clients of profiling information, and
12 // implemented either by making static guestimations, or by actually reading in
13 // profiling information gathered by running the program.
15 // Note that to be useful, all profile-based optimizations should preserve
16 // ProfileInfo, which requires that they notify it when changes to the CFG are
17 // made. (This is not implemented yet.)
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_ANALYSIS_PROFILEINFO_H
22 #define LLVM_ANALYSIS_PROFILEINFO_H
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/raw_ostream.h"
38 class MachineBasicBlock;
39 class MachineFunction;
41 // Helper for dumping edges to dbgs().
42 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E);
43 raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E);
45 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
46 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB);
48 raw_ostream& operator<<(raw_ostream &O, const Function *F);
49 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF);
51 /// ProfileInfo Class - This class holds and maintains profiling
52 /// information for some unit of code.
53 template<class FType, class BType>
56 // Types for handling profiling information.
57 typedef std::pair<const BType*, const BType*> Edge;
58 typedef std::pair<Edge, double> EdgeWeight;
59 typedef std::map<Edge, double> EdgeWeights;
60 typedef std::map<const BType*, double> BlockCounts;
61 typedef std::map<const BType*, const BType*> Path;
64 // EdgeInformation - Count the number of times a transition between two
65 // blocks is executed. As a special case, we also hold an edge from the
66 // null BasicBlock to the entry block to indicate how many times the
67 // function was entered.
68 std::map<const FType*, EdgeWeights> EdgeInformation;
70 // BlockInformation - Count the number of times a block is executed.
71 std::map<const FType*, BlockCounts> BlockInformation;
73 // FunctionInformation - Count the number of times a function is executed.
74 std::map<const FType*, double> FunctionInformation;
76 ProfileInfoT<MachineFunction, MachineBasicBlock> *MachineProfile;
78 static char ID; // Class identification, replacement for typeinfo
80 ~ProfileInfoT(); // We want to be subclassed
82 // MissingValue - The value that is returned for execution counts in case
83 // no value is available.
84 static const double MissingValue;
86 // getFunction() - Returns the Function for an Edge, checking for validity.
87 static const FType* getFunction(Edge e) {
89 return e.first->getParent();
90 } else if (e.second) {
91 return e.second->getParent();
93 assert(0 && "Invalid ProfileInfo::Edge");
94 return (const FType*)0;
97 // getEdge() - Creates an Edge from two BasicBlocks.
98 static Edge getEdge(const BType *Src, const BType *Dest) {
99 return std::make_pair(Src, Dest);
102 //===------------------------------------------------------------------===//
103 /// Profile Information Queries
105 double getExecutionCount(const FType *F);
107 double getExecutionCount(const BType *BB);
109 void setExecutionCount(const BType *BB, double w);
111 void addExecutionCount(const BType *BB, double w);
113 double getEdgeWeight(Edge e) const {
114 typename std::map<const FType*, EdgeWeights>::const_iterator J =
115 EdgeInformation.find(getFunction(e));
116 if (J == EdgeInformation.end()) return MissingValue;
118 typename EdgeWeights::const_iterator I = J->second.find(e);
119 if (I == J->second.end()) return MissingValue;
124 void setEdgeWeight(Edge e, double w) {
125 DEBUG_WITH_TYPE("profile-info",
126 dbgs() << "Creating Edge " << e
127 << " (weight: " << format("%.20g",w) << ")\n");
128 EdgeInformation[getFunction(e)][e] = w;
131 void addEdgeWeight(Edge e, double w);
133 EdgeWeights &getEdgeWeights (const FType *F) {
134 return EdgeInformation[F];
137 //===------------------------------------------------------------------===//
138 /// Analysis Update Methods
140 void removeBlock(const BType *BB);
142 void removeEdge(Edge e);
144 void replaceEdge(const Edge &, const Edge &);
150 GetPathWithNewEdges = 8
153 const BType *GetPath(const BType *Src, const BType *Dest,
154 Path &P, unsigned Mode);
156 void divertFlow(const Edge &, const Edge &);
158 void splitEdge(const BType *FirstBB, const BType *SecondBB,
159 const BType *NewBB, bool MergeIdenticalEdges = false);
161 void splitBlock(const BType *Old, const BType* New);
163 void splitBlock(const BType *BB, const BType* NewBB,
164 BType *const *Preds, unsigned NumPreds);
166 void replaceAllUses(const BType *RmBB, const BType *DestBB);
168 void transfer(const FType *Old, const FType *New);
170 void repair(const FType *F);
172 void dump(FType *F = 0, bool real = true) {
173 dbgs() << "**** This is ProfileInfo " << this << " speaking:\n";
175 typename std::set<const FType*> Functions;
177 dbgs() << "Functions: \n";
179 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
182 for (typename std::map<const FType*, double>::iterator fi = FunctionInformation.begin(),
183 fe = FunctionInformation.end(); fi != fe; ++fi) {
184 dbgs() << fi->first << "@" << format("%p",fi->first) << ": " << format("%.20g",fi->second) << "\n";
185 Functions.insert(fi->first);
189 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
191 const FType *F = *FI;
192 typename std::map<const FType*, BlockCounts>::iterator bwi = BlockInformation.find(F);
193 dbgs() << "BasicBlocks for Function " << F << ":\n";
194 for (typename BlockCounts::const_iterator bi = bwi->second.begin(), be = bwi->second.end(); bi != be; ++bi) {
195 dbgs() << bi->first << "@" << format("%p", bi->first) << ": " << format("%.20g",bi->second) << "\n";
199 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
201 typename std::map<const FType*, EdgeWeights>::iterator ei = EdgeInformation.find(*FI);
202 dbgs() << "Edges for Function " << ei->first << ":\n";
203 for (typename EdgeWeights::iterator ewi = ei->second.begin(), ewe = ei->second.end();
205 dbgs() << ewi->first << ": " << format("%.20g",ewi->second) << "\n";
209 assert(F && "No function given, this is not supported!");
210 dbgs() << "Functions: \n";
211 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
213 dbgs() << "BasicBlocks for Function " << F << ":\n";
214 for (typename FType::const_iterator BI = F->begin(), BE = F->end();
216 const BType *BB = &(*BI);
217 dbgs() << BB << "@" << format("%p", BB) << ": " << format("%.20g",getExecutionCount(BB)) << "\n";
220 dbgs() << "**** ProfileInfo " << this << ", over and out.\n";
223 bool CalculateMissingEdge(const BType *BB, Edge &removed, bool assumeEmptyExit = false);
225 bool EstimateMissingEdges(const BType *BB);
227 ProfileInfoT<MachineFunction, MachineBasicBlock> *MI() {
228 if (MachineProfile == 0)
229 MachineProfile = new ProfileInfoT<MachineFunction, MachineBasicBlock>();
230 return MachineProfile;
234 return (MachineProfile != 0);
238 typedef ProfileInfoT<Function, BasicBlock> ProfileInfo;
239 typedef ProfileInfoT<MachineFunction, MachineBasicBlock> MachineProfileInfo;
241 /// createProfileLoaderPass - This function returns a Pass that loads the
242 /// profiling information for the module from the specified filename, making
243 /// it available to the optimizers.
244 Pass *createProfileLoaderPass(const std::string &Filename);
246 } // End llvm namespace