00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef LLVM_ANALYSIS_PROFILEINFO_H
00022 #define LLVM_ANALYSIS_PROFILEINFO_H
00023
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/Format.h"
00026 #include "llvm/Support/raw_ostream.h"
00027 #include <cassert>
00028 #include <string>
00029 #include <map>
00030 #include <set>
00031
00032 namespace llvm {
00033 class Pass;
00034 class raw_ostream;
00035
00036 class BasicBlock;
00037 class Function;
00038 class MachineBasicBlock;
00039 class MachineFunction;
00040
00041
00042 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E);
00043 raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E);
00044
00045 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
00046 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB);
00047
00048 raw_ostream& operator<<(raw_ostream &O, const Function *F);
00049 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF);
00050
00053 template<class FType, class BType>
00054 class ProfileInfoT {
00055 public:
00056
00057 typedef std::pair<const BType*, const BType*> Edge;
00058 typedef std::pair<Edge, double> EdgeWeight;
00059 typedef std::map<Edge, double> EdgeWeights;
00060 typedef std::map<const BType*, double> BlockCounts;
00061 typedef std::map<const BType*, const BType*> Path;
00062
00063 protected:
00064
00065
00066
00067
00068 std::map<const FType*, EdgeWeights> EdgeInformation;
00069
00070
00071 std::map<const FType*, BlockCounts> BlockInformation;
00072
00073
00074 std::map<const FType*, double> FunctionInformation;
00075
00076 ProfileInfoT<MachineFunction, MachineBasicBlock> *MachineProfile;
00077 public:
00078 static char ID;
00079 ProfileInfoT();
00080 ~ProfileInfoT();
00081
00082
00083
00084 static const double MissingValue;
00085
00086
00087 static const FType* getFunction(Edge e) {
00088 if (e.first) {
00089 return e.first->getParent();
00090 } else if (e.second) {
00091 return e.second->getParent();
00092 }
00093 assert(0 && "Invalid ProfileInfo::Edge");
00094 return (const FType*)0;
00095 }
00096
00097
00098 static Edge getEdge(const BType *Src, const BType *Dest) {
00099 return std::make_pair(Src, Dest);
00100 }
00101
00102
00105 double getExecutionCount(const FType *F);
00106
00107 double getExecutionCount(const BType *BB);
00108
00109 void setExecutionCount(const BType *BB, double w);
00110
00111 void addExecutionCount(const BType *BB, double w);
00112
00113 double getEdgeWeight(Edge e) const {
00114 typename std::map<const FType*, EdgeWeights>::const_iterator J =
00115 EdgeInformation.find(getFunction(e));
00116 if (J == EdgeInformation.end()) return MissingValue;
00117
00118 typename EdgeWeights::const_iterator I = J->second.find(e);
00119 if (I == J->second.end()) return MissingValue;
00120
00121 return I->second;
00122 }
00123
00124 void setEdgeWeight(Edge e, double w) {
00125 DEBUG_WITH_TYPE("profile-info",
00126 dbgs() << "Creating Edge " << e
00127 << " (weight: " << format("%.20g",w) << ")\n");
00128 EdgeInformation[getFunction(e)][e] = w;
00129 }
00130
00131 void addEdgeWeight(Edge e, double w);
00132
00133 EdgeWeights &getEdgeWeights (const FType *F) {
00134 return EdgeInformation[F];
00135 }
00136
00137
00140 void removeBlock(const BType *BB);
00141
00142 void removeEdge(Edge e);
00143
00144 void replaceEdge(const Edge &, const Edge &);
00145
00146 enum GetPathMode {
00147 GetPathToExit = 1,
00148 GetPathToValue = 2,
00149 GetPathToDest = 4,
00150 GetPathWithNewEdges = 8
00151 };
00152
00153 const BType *GetPath(const BType *Src, const BType *Dest,
00154 Path &P, unsigned Mode);
00155
00156 void divertFlow(const Edge &, const Edge &);
00157
00158 void splitEdge(const BType *FirstBB, const BType *SecondBB,
00159 const BType *NewBB, bool MergeIdenticalEdges = false);
00160
00161 void splitBlock(const BType *Old, const BType* New);
00162
00163 void splitBlock(const BType *BB, const BType* NewBB,
00164 BType *const *Preds, unsigned NumPreds);
00165
00166 void replaceAllUses(const BType *RmBB, const BType *DestBB);
00167
00168 void transfer(const FType *Old, const FType *New);
00169
00170 void repair(const FType *F);
00171
00172 void dump(FType *F = 0, bool real = true) {
00173 dbgs() << "**** This is ProfileInfo " << this << " speaking:\n";
00174 if (!real) {
00175 typename std::set<const FType*> Functions;
00176
00177 dbgs() << "Functions: \n";
00178 if (F) {
00179 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
00180 Functions.insert(F);
00181 } else {
00182 for (typename std::map<const FType*, double>::iterator fi = FunctionInformation.begin(),
00183 fe = FunctionInformation.end(); fi != fe; ++fi) {
00184 dbgs() << fi->first << "@" << format("%p",fi->first) << ": " << format("%.20g",fi->second) << "\n";
00185 Functions.insert(fi->first);
00186 }
00187 }
00188
00189 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
00190 FI != FE; ++FI) {
00191 const FType *F = *FI;
00192 typename std::map<const FType*, BlockCounts>::iterator bwi = BlockInformation.find(F);
00193 dbgs() << "BasicBlocks for Function " << F << ":\n";
00194 for (typename BlockCounts::const_iterator bi = bwi->second.begin(), be = bwi->second.end(); bi != be; ++bi) {
00195 dbgs() << bi->first << "@" << format("%p", bi->first) << ": " << format("%.20g",bi->second) << "\n";
00196 }
00197 }
00198
00199 for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
00200 FI != FE; ++FI) {
00201 typename std::map<const FType*, EdgeWeights>::iterator ei = EdgeInformation.find(*FI);
00202 dbgs() << "Edges for Function " << ei->first << ":\n";
00203 for (typename EdgeWeights::iterator ewi = ei->second.begin(), ewe = ei->second.end();
00204 ewi != ewe; ++ewi) {
00205 dbgs() << ewi->first << ": " << format("%.20g",ewi->second) << "\n";
00206 }
00207 }
00208 } else {
00209 assert(F && "No function given, this is not supported!");
00210 dbgs() << "Functions: \n";
00211 dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
00212
00213 dbgs() << "BasicBlocks for Function " << F << ":\n";
00214 for (typename FType::const_iterator BI = F->begin(), BE = F->end();
00215 BI != BE; ++BI) {
00216 const BType *BB = &(*BI);
00217 dbgs() << BB << "@" << format("%p", BB) << ": " << format("%.20g",getExecutionCount(BB)) << "\n";
00218 }
00219 }
00220 dbgs() << "**** ProfileInfo " << this << ", over and out.\n";
00221 }
00222
00223 bool CalculateMissingEdge(const BType *BB, Edge &removed, bool assumeEmptyExit = false);
00224
00225 bool EstimateMissingEdges(const BType *BB);
00226
00227 ProfileInfoT<MachineFunction, MachineBasicBlock> *MI() {
00228 if (MachineProfile == 0)
00229 MachineProfile = new ProfileInfoT<MachineFunction, MachineBasicBlock>();
00230 return MachineProfile;
00231 }
00232
00233 bool hasMI() const {
00234 return (MachineProfile != 0);
00235 }
00236 };
00237
00238 typedef ProfileInfoT<Function, BasicBlock> ProfileInfo;
00239 typedef ProfileInfoT<MachineFunction, MachineBasicBlock> MachineProfileInfo;
00240
00244 Pass *createProfileLoaderPass(const std::string &Filename);
00245
00246 }
00247
00248 #endif