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/ErrorHandling.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/raw_ostream.h"
39 class MachineBasicBlock;
40 class MachineFunction;
42 // Helper for dumping edges to dbgs().
43 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E);
44 raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E);
46 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
47 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB);
49 raw_ostream& operator<<(raw_ostream &O, const Function *F);
50 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF);
52 /// ProfileInfo Class - This class holds and maintains profiling
53 /// information for some unit of code.
54 template<class FType, class BType>
57 // Types for handling profiling information.
58 typedef std::pair<const BType*, const BType*> Edge;
59 typedef std::pair<Edge, double> EdgeWeight;
60 typedef std::map<Edge, double> EdgeWeights;
61 typedef std::map<const BType*, double> BlockCounts;
62 typedef std::map<const BType*, const BType*> Path;
65 // EdgeInformation - Count the number of times a transition between two
66 // blocks is executed. As a special case, we also hold an edge from the
67 // null BasicBlock to the entry block to indicate how many times the
68 // function was entered.
69 std::map<const FType*, EdgeWeights> EdgeInformation;
71 // BlockInformation - Count the number of times a block is executed.
72 std::map<const FType*, BlockCounts> BlockInformation;
74 // FunctionInformation - Count the number of times a function is executed.
75 std::map<const FType*, double> FunctionInformation;
77 ProfileInfoT<MachineFunction, MachineBasicBlock> *MachineProfile;
79 static char ID; // Class identification, replacement for typeinfo
81 ~ProfileInfoT(); // We want to be subclassed
83 // MissingValue - The value that is returned for execution counts in case
84 // no value is available.
85 static const double MissingValue;
87 // getFunction() - Returns the Function for an Edge, checking for validity.
88 static const FType* getFunction(Edge e) {
90 return e.first->getParent();
92 return e.second->getParent();
93 llvm_unreachable("Invalid ProfileInfo::Edge");
96 // getEdge() - Creates an Edge from two BasicBlocks.
97 static Edge getEdge(const BType *Src, const BType *Dest) {
98 return std::make_pair(Src, Dest);
101 //===------------------------------------------------------------------===//
102 /// Profile Information Queries
104 double getExecutionCount(const FType *F);
106 double getExecutionCount(const BType *BB);
108 void setExecutionCount(const BType *BB, double w);
110 void addExecutionCount(const BType *BB, double w);
112 double getEdgeWeight(Edge e) const {
113 typename std::map<const FType*, EdgeWeights>::const_iterator J =
114 EdgeInformation.find(getFunction(e));
115 if (J == EdgeInformation.end()) return MissingValue;
117 typename EdgeWeights::const_iterator I = J->second.find(e);
118 if (I == J->second.end()) return MissingValue;
123 void setEdgeWeight(Edge e, double w) {
124 DEBUG_WITH_TYPE("profile-info",
125 dbgs() << "Creating Edge " << e
126 << " (weight: " << format("%.20g",w) << ")\n");
127 EdgeInformation[getFunction(e)][e] = w;
130 void addEdgeWeight(Edge e, double w);
132 EdgeWeights &getEdgeWeights (const FType *F) {
133 return EdgeInformation[F];
136 //===------------------------------------------------------------------===//
137 /// Analysis Update Methods
139 void removeBlock(const BType *BB);
141 void removeEdge(Edge e);
143 void replaceEdge(const Edge &, const Edge &);
149 GetPathWithNewEdges = 8
152 const BType *GetPath(const BType *Src, const BType *Dest,
153 Path &P, unsigned Mode);
155 void divertFlow(const Edge &, const Edge &);
157 void splitEdge(const BType *FirstBB, const BType *SecondBB,
158 const BType *NewBB, bool MergeIdenticalEdges = false);
160 void splitBlock(const BType *Old, const BType* New);
162 void splitBlock(const BType *BB, const BType* NewBB,
163 BType *const *Preds, unsigned NumPreds);
165 void replaceAllUses(const BType *RmBB, const BType *DestBB);
167 void transfer(const FType *Old, const FType *New);
169 void repair(const FType *F);
171 void dump(FType *F = 0, bool real = true) {
172 dbgs() << "**** This is ProfileInfo " << this << " speaking:\n";
174 typename std::set<const FType*> Functions;
176 dbgs() << "Functions: \n";
178 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
181 for (typename std::map<const FType*, double>::iterator fi = FunctionInformation.begin(),
182 fe = FunctionInformation.end(); fi != fe; ++fi) {
183 dbgs() << fi->first << "@" << format("%p",fi->first) << ": " << format("%.20g",fi->second) << "\n";
184 Functions.insert(fi->first);
188 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
190 const FType *F = *FI;
191 typename std::map<const FType*, BlockCounts>::iterator bwi = BlockInformation.find(F);
192 dbgs() << "BasicBlocks for Function " << F << ":\n";
193 for (typename BlockCounts::const_iterator bi = bwi->second.begin(), be = bwi->second.end(); bi != be; ++bi) {
194 dbgs() << bi->first << "@" << format("%p", bi->first) << ": " << format("%.20g",bi->second) << "\n";
198 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
200 typename std::map<const FType*, EdgeWeights>::iterator ei = EdgeInformation.find(*FI);
201 dbgs() << "Edges for Function " << ei->first << ":\n";
202 for (typename EdgeWeights::iterator ewi = ei->second.begin(), ewe = ei->second.end();
204 dbgs() << ewi->first << ": " << format("%.20g",ewi->second) << "\n";
208 assert(F && "No function given, this is not supported!");
209 dbgs() << "Functions: \n";
210 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
212 dbgs() << "BasicBlocks for Function " << F << ":\n";
213 for (typename FType::const_iterator BI = F->begin(), BE = F->end();
215 const BType *BB = &(*BI);
216 dbgs() << BB << "@" << format("%p", BB) << ": " << format("%.20g",getExecutionCount(BB)) << "\n";
219 dbgs() << "**** ProfileInfo " << this << ", over and out.\n";
222 bool CalculateMissingEdge(const BType *BB, Edge &removed, bool assumeEmptyExit = false);
224 bool EstimateMissingEdges(const BType *BB);
226 ProfileInfoT<MachineFunction, MachineBasicBlock> *MI() {
227 if (MachineProfile == 0)
228 MachineProfile = new ProfileInfoT<MachineFunction, MachineBasicBlock>();
229 return MachineProfile;
233 return (MachineProfile != 0);
237 typedef ProfileInfoT<Function, BasicBlock> ProfileInfo;
238 typedef ProfileInfoT<MachineFunction, MachineBasicBlock> MachineProfileInfo;
240 /// createProfileLoaderPass - This function returns a Pass that loads the
241 /// profiling information for the module from the specified filename, making
242 /// it available to the optimizers.
243 Pass *createProfileLoaderPass(const std::string &Filename);
245 } // End llvm namespace