LLVM API Documentation

ProfileInfo.cpp
Go to the documentation of this file.
00001 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the abstract ProfileInfo interface, and the default
00011 // "no profile" implementation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #define DEBUG_TYPE "profile-info"
00015 #include "llvm/Analysis/ProfileInfo.h"
00016 #include "llvm/ADT/SmallSet.h"
00017 #include "llvm/Analysis/Passes.h"
00018 #include "llvm/CodeGen/MachineBasicBlock.h"
00019 #include "llvm/CodeGen/MachineFunction.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Support/CFG.h"
00022 #include <limits>
00023 #include <queue>
00024 #include <set>
00025 using namespace llvm;
00026 
00027 namespace llvm {
00028   template<> char ProfileInfoT<Function,BasicBlock>::ID = 0;
00029 }
00030 
00031 // Register the ProfileInfo interface, providing a nice name to refer to.
00032 INITIALIZE_ANALYSIS_GROUP(ProfileInfo, "Profile Information", NoProfileInfo)
00033 
00034 namespace llvm {
00035 
00036 template <>
00037 ProfileInfoT<MachineFunction, MachineBasicBlock>::ProfileInfoT() {}
00038 template <>
00039 ProfileInfoT<MachineFunction, MachineBasicBlock>::~ProfileInfoT() {}
00040 
00041 template <>
00042 ProfileInfoT<Function, BasicBlock>::ProfileInfoT() {
00043   MachineProfile = 0;
00044 }
00045 template <>
00046 ProfileInfoT<Function, BasicBlock>::~ProfileInfoT() {
00047   if (MachineProfile) delete MachineProfile;
00048 }
00049 
00050 template<>
00051 char ProfileInfoT<MachineFunction, MachineBasicBlock>::ID = 0;
00052 
00053 template<>
00054 const double ProfileInfoT<Function,BasicBlock>::MissingValue = -1;
00055 
00056 template<> const
00057 double ProfileInfoT<MachineFunction, MachineBasicBlock>::MissingValue = -1;
00058 
00059 template<> double
00060 ProfileInfoT<Function,BasicBlock>::getExecutionCount(const BasicBlock *BB) {
00061   std::map<const Function*, BlockCounts>::iterator J =
00062     BlockInformation.find(BB->getParent());
00063   if (J != BlockInformation.end()) {
00064     BlockCounts::iterator I = J->second.find(BB);
00065     if (I != J->second.end())
00066       return I->second;
00067   }
00068 
00069   double Count = MissingValue;
00070 
00071   const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
00072 
00073   // Are there zero predecessors of this block?
00074   if (PI == PE) {
00075     Edge e = getEdge(0, BB);
00076     Count = getEdgeWeight(e);
00077   } else {
00078     // Otherwise, if there are predecessors, the execution count of this block is
00079     // the sum of the edge frequencies from the incoming edges.
00080     std::set<const BasicBlock*> ProcessedPreds;
00081     Count = 0;
00082     for (; PI != PE; ++PI) {
00083       const BasicBlock *P = *PI;
00084       if (ProcessedPreds.insert(P).second) {
00085         double w = getEdgeWeight(getEdge(P, BB));
00086         if (w == MissingValue) {
00087           Count = MissingValue;
00088           break;
00089         }
00090         Count += w;
00091       }
00092     }
00093   }
00094 
00095   // If the predecessors did not suffice to get block weight, try successors.
00096   if (Count == MissingValue) {
00097 
00098     succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
00099 
00100     // Are there zero successors of this block?
00101     if (SI == SE) {
00102       Edge e = getEdge(BB,0);
00103       Count = getEdgeWeight(e);
00104     } else {
00105       std::set<const BasicBlock*> ProcessedSuccs;
00106       Count = 0;
00107       for (; SI != SE; ++SI)
00108         if (ProcessedSuccs.insert(*SI).second) {
00109           double w = getEdgeWeight(getEdge(BB, *SI));
00110           if (w == MissingValue) {
00111             Count = MissingValue;
00112             break;
00113           }
00114           Count += w;
00115         }
00116     }
00117   }
00118 
00119   if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
00120   return Count;
00121 }
00122 
00123 template<>
00124 double ProfileInfoT<MachineFunction, MachineBasicBlock>::
00125         getExecutionCount(const MachineBasicBlock *MBB) {
00126   std::map<const MachineFunction*, BlockCounts>::iterator J =
00127     BlockInformation.find(MBB->getParent());
00128   if (J != BlockInformation.end()) {
00129     BlockCounts::iterator I = J->second.find(MBB);
00130     if (I != J->second.end())
00131       return I->second;
00132   }
00133 
00134   return MissingValue;
00135 }
00136 
00137 template<>
00138 double ProfileInfoT<Function,BasicBlock>::getExecutionCount(const Function *F) {
00139   std::map<const Function*, double>::iterator J =
00140     FunctionInformation.find(F);
00141   if (J != FunctionInformation.end())
00142     return J->second;
00143 
00144   // isDeclaration() is checked here and not at start of function to allow
00145   // functions without a body still to have a execution count.
00146   if (F->isDeclaration()) return MissingValue;
00147 
00148   double Count = getExecutionCount(&F->getEntryBlock());
00149   if (Count != MissingValue) FunctionInformation[F] = Count;
00150   return Count;
00151 }
00152 
00153 template<>
00154 double ProfileInfoT<MachineFunction, MachineBasicBlock>::
00155         getExecutionCount(const MachineFunction *MF) {
00156   std::map<const MachineFunction*, double>::iterator J =
00157     FunctionInformation.find(MF);
00158   if (J != FunctionInformation.end())
00159     return J->second;
00160 
00161   double Count = getExecutionCount(&MF->front());
00162   if (Count != MissingValue) FunctionInformation[MF] = Count;
00163   return Count;
00164 }
00165 
00166 template<>
00167 void ProfileInfoT<Function,BasicBlock>::
00168         setExecutionCount(const BasicBlock *BB, double w) {
00169   DEBUG(dbgs() << "Creating Block " << BB->getName() 
00170                << " (weight: " << format("%.20g",w) << ")\n");
00171   BlockInformation[BB->getParent()][BB] = w;
00172 }
00173 
00174 template<>
00175 void ProfileInfoT<MachineFunction, MachineBasicBlock>::
00176         setExecutionCount(const MachineBasicBlock *MBB, double w) {
00177   DEBUG(dbgs() << "Creating Block " << MBB->getBasicBlock()->getName()
00178                << " (weight: " << format("%.20g",w) << ")\n");
00179   BlockInformation[MBB->getParent()][MBB] = w;
00180 }
00181 
00182 template<>
00183 void ProfileInfoT<Function,BasicBlock>::addEdgeWeight(Edge e, double w) {
00184   double oldw = getEdgeWeight(e);
00185   assert (oldw != MissingValue && "Adding weight to Edge with no previous weight");
00186   DEBUG(dbgs() << "Adding to Edge " << e
00187                << " (new weight: " << format("%.20g",oldw + w) << ")\n");
00188   EdgeInformation[getFunction(e)][e] = oldw + w;
00189 }
00190 
00191 template<>
00192 void ProfileInfoT<Function,BasicBlock>::
00193         addExecutionCount(const BasicBlock *BB, double w) {
00194   double oldw = getExecutionCount(BB);
00195   assert (oldw != MissingValue && "Adding weight to Block with no previous weight");
00196   DEBUG(dbgs() << "Adding to Block " << BB->getName()
00197                << " (new weight: " << format("%.20g",oldw + w) << ")\n");
00198   BlockInformation[BB->getParent()][BB] = oldw + w;
00199 }
00200 
00201 template<>
00202 void ProfileInfoT<Function,BasicBlock>::removeBlock(const BasicBlock *BB) {
00203   std::map<const Function*, BlockCounts>::iterator J =
00204     BlockInformation.find(BB->getParent());
00205   if (J == BlockInformation.end()) return;
00206 
00207   DEBUG(dbgs() << "Deleting " << BB->getName() << "\n");
00208   J->second.erase(BB);
00209 }
00210 
00211 template<>
00212 void ProfileInfoT<Function,BasicBlock>::removeEdge(Edge e) {
00213   std::map<const Function*, EdgeWeights>::iterator J =
00214     EdgeInformation.find(getFunction(e));
00215   if (J == EdgeInformation.end()) return;
00216 
00217   DEBUG(dbgs() << "Deleting" << e << "\n");
00218   J->second.erase(e);
00219 }
00220 
00221 template<>
00222 void ProfileInfoT<Function,BasicBlock>::
00223         replaceEdge(const Edge &oldedge, const Edge &newedge) {
00224   double w;
00225   if ((w = getEdgeWeight(newedge)) == MissingValue) {
00226     w = getEdgeWeight(oldedge);
00227     DEBUG(dbgs() << "Replacing " << oldedge << " with " << newedge  << "\n");
00228   } else {
00229     w += getEdgeWeight(oldedge);
00230     DEBUG(dbgs() << "Adding " << oldedge << " to " << newedge  << "\n");
00231   }
00232   setEdgeWeight(newedge,w);
00233   removeEdge(oldedge);
00234 }
00235 
00236 template<>
00237 const BasicBlock *ProfileInfoT<Function,BasicBlock>::
00238         GetPath(const BasicBlock *Src, const BasicBlock *Dest,
00239                 Path &P, unsigned Mode) {
00240   const BasicBlock *BB = 0;
00241   bool hasFoundPath = false;
00242 
00243   std::queue<const BasicBlock *> BFS;
00244   BFS.push(Src);
00245 
00246   while(BFS.size() && !hasFoundPath) {
00247     BB = BFS.front();
00248     BFS.pop();
00249 
00250     succ_const_iterator Succ = succ_begin(BB), End = succ_end(BB);
00251     if (Succ == End) {
00252       P[(const BasicBlock*)0] = BB;
00253       if (Mode & GetPathToExit) {
00254         hasFoundPath = true;
00255         BB = 0;
00256       }
00257     }
00258     for(;Succ != End; ++Succ) {
00259       if (P.find(*Succ) != P.end()) continue;
00260       Edge e = getEdge(BB,*Succ);
00261       if ((Mode & GetPathWithNewEdges) && (getEdgeWeight(e) != MissingValue)) continue;
00262       P[*Succ] = BB;
00263       BFS.push(*Succ);
00264       if ((Mode & GetPathToDest) && *Succ == Dest) {
00265         hasFoundPath = true;
00266         BB = *Succ;
00267         break;
00268       }
00269       if ((Mode & GetPathToValue) && (getExecutionCount(*Succ) != MissingValue)) {
00270         hasFoundPath = true;
00271         BB = *Succ;
00272         break;
00273       }
00274     }
00275   }
00276 
00277   return BB;
00278 }
00279 
00280 template<>
00281 void ProfileInfoT<Function,BasicBlock>::
00282         divertFlow(const Edge &oldedge, const Edge &newedge) {
00283   DEBUG(dbgs() << "Diverting " << oldedge << " via " << newedge );
00284 
00285   // First check if the old edge was taken, if not, just delete it...
00286   if (getEdgeWeight(oldedge) == 0) {
00287     removeEdge(oldedge);
00288     return;
00289   }
00290 
00291   Path P;
00292   P[newedge.first] = 0;
00293   P[newedge.second] = newedge.first;
00294   const BasicBlock *BB = GetPath(newedge.second,oldedge.second,P,GetPathToExit | GetPathToDest);
00295 
00296   double w = getEdgeWeight (oldedge);
00297   DEBUG(dbgs() << ", Weight: " << format("%.20g",w) << "\n");
00298   do {
00299     const BasicBlock *Parent = P.find(BB)->second;
00300     Edge e = getEdge(Parent,BB);
00301     double oldw = getEdgeWeight(e);
00302     double oldc = getExecutionCount(e.first);
00303     setEdgeWeight(e, w+oldw);
00304     if (Parent != oldedge.first) {
00305       setExecutionCount(e.first, w+oldc);
00306     }
00307     BB = Parent;
00308   } while (BB != newedge.first);
00309   removeEdge(oldedge);
00310 }
00311 
00312 /// Replaces all occurrences of RmBB in the ProfilingInfo with DestBB.
00313 /// This checks all edges of the function the blocks reside in and replaces the
00314 /// occurrences of RmBB with DestBB.
00315 template<>
00316 void ProfileInfoT<Function,BasicBlock>::
00317         replaceAllUses(const BasicBlock *RmBB, const BasicBlock *DestBB) {
00318   DEBUG(dbgs() << "Replacing " << RmBB->getName()
00319                << " with " << DestBB->getName() << "\n");
00320   const Function *F = DestBB->getParent();
00321   std::map<const Function*, EdgeWeights>::iterator J =
00322     EdgeInformation.find(F);
00323   if (J == EdgeInformation.end()) return;
00324 
00325   Edge e, newedge;
00326   bool erasededge = false;
00327   EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
00328   while(I != E) {
00329     e = (I++)->first;
00330     bool foundedge = false; bool eraseedge = false;
00331     if (e.first == RmBB) {
00332       if (e.second == DestBB) {
00333         eraseedge = true;
00334       } else {
00335         newedge = getEdge(DestBB, e.second);
00336         foundedge = true;
00337       }
00338     }
00339     if (e.second == RmBB) {
00340       if (e.first == DestBB) {
00341         eraseedge = true;
00342       } else {
00343         newedge = getEdge(e.first, DestBB);
00344         foundedge = true;
00345       }
00346     }
00347     if (foundedge) {
00348       replaceEdge(e, newedge);
00349     }
00350     if (eraseedge) {
00351       if (erasededge) {
00352         Edge newedge = getEdge(DestBB, DestBB);
00353         replaceEdge(e, newedge);
00354       } else {
00355         removeEdge(e);
00356         erasededge = true;
00357       }
00358     }
00359   }
00360 }
00361 
00362 /// Splits an edge in the ProfileInfo and redirects flow over NewBB.
00363 /// Since its possible that there is more than one edge in the CFG from FristBB
00364 /// to SecondBB its necessary to redirect the flow proporionally.
00365 template<>
00366 void ProfileInfoT<Function,BasicBlock>::splitEdge(const BasicBlock *FirstBB,
00367                                                   const BasicBlock *SecondBB,
00368                                                   const BasicBlock *NewBB,
00369                                                   bool MergeIdenticalEdges) {
00370   const Function *F = FirstBB->getParent();
00371   std::map<const Function*, EdgeWeights>::iterator J =
00372     EdgeInformation.find(F);
00373   if (J == EdgeInformation.end()) return;
00374 
00375   // Generate edges and read current weight.
00376   Edge e  = getEdge(FirstBB, SecondBB);
00377   Edge n1 = getEdge(FirstBB, NewBB);
00378   Edge n2 = getEdge(NewBB, SecondBB);
00379   EdgeWeights &ECs = J->second;
00380   double w = ECs[e];
00381 
00382   int succ_count = 0;
00383   if (!MergeIdenticalEdges) {
00384     // First count the edges from FristBB to SecondBB, if there is more than
00385     // one, only slice out a proporional part for NewBB.
00386     for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
00387         BBI != BBE; ++BBI) {
00388       if (*BBI == SecondBB) succ_count++;  
00389     }
00390     // When the NewBB is completely new, increment the count by one so that
00391     // the counts are properly distributed.
00392     if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
00393   } else {
00394     // When the edges are merged anyway, then redirect all flow.
00395     succ_count = 1;
00396   }
00397 
00398   // We know now how many edges there are from FirstBB to SecondBB, reroute a
00399   // proportional part of the edge weight over NewBB.
00400   double neww = floor(w / succ_count);
00401   ECs[n1] += neww;
00402   ECs[n2] += neww;
00403   BlockInformation[F][NewBB] += neww;
00404   if (succ_count == 1) {
00405     ECs.erase(e);
00406   } else {
00407     ECs[e] -= neww;
00408   }
00409 }
00410 
00411 template<>
00412 void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *Old,
00413                                                    const BasicBlock* New) {
00414   const Function *F = Old->getParent();
00415   std::map<const Function*, EdgeWeights>::iterator J =
00416     EdgeInformation.find(F);
00417   if (J == EdgeInformation.end()) return;
00418 
00419   DEBUG(dbgs() << "Splitting " << Old->getName() << " to " << New->getName() << "\n");
00420 
00421   std::set<Edge> Edges;
00422   for (EdgeWeights::iterator ewi = J->second.begin(), ewe = J->second.end(); 
00423        ewi != ewe; ++ewi) {
00424     Edge old = ewi->first;
00425     if (old.first == Old) {
00426       Edges.insert(old);
00427     }
00428   }
00429   for (std::set<Edge>::iterator EI = Edges.begin(), EE = Edges.end(); 
00430        EI != EE; ++EI) {
00431     Edge newedge = getEdge(New, EI->second);
00432     replaceEdge(*EI, newedge);
00433   }
00434 
00435   double w = getExecutionCount(Old);
00436   setEdgeWeight(getEdge(Old, New), w);
00437   setExecutionCount(New, w);
00438 }
00439 
00440 template<>
00441 void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *BB,
00442                                                    const BasicBlock* NewBB,
00443                                                    BasicBlock *const *Preds,
00444                                                    unsigned NumPreds) {
00445   const Function *F = BB->getParent();
00446   std::map<const Function*, EdgeWeights>::iterator J =
00447     EdgeInformation.find(F);
00448   if (J == EdgeInformation.end()) return;
00449 
00450   DEBUG(dbgs() << "Splitting " << NumPreds << " Edges from " << BB->getName() 
00451                << " to " << NewBB->getName() << "\n");
00452 
00453   // Collect weight that was redirected over NewBB.
00454   double newweight = 0;
00455   
00456   std::set<const BasicBlock *> ProcessedPreds;
00457   // For all requestes Predecessors.
00458   for (unsigned pred = 0; pred < NumPreds; ++pred) {
00459     const BasicBlock * Pred = Preds[pred];
00460     if (ProcessedPreds.insert(Pred).second) {
00461       // Create edges and read old weight.
00462       Edge oldedge = getEdge(Pred, BB);
00463       Edge newedge = getEdge(Pred, NewBB);
00464 
00465       // Remember how much weight was redirected.
00466       newweight += getEdgeWeight(oldedge);
00467     
00468       replaceEdge(oldedge,newedge);
00469     }
00470   }
00471 
00472   Edge newedge = getEdge(NewBB,BB);
00473   setEdgeWeight(newedge, newweight);
00474   setExecutionCount(NewBB, newweight);
00475 }
00476 
00477 template<>
00478 void ProfileInfoT<Function,BasicBlock>::transfer(const Function *Old,
00479                                                  const Function *New) {
00480   DEBUG(dbgs() << "Replacing Function " << Old->getName() << " with "
00481                << New->getName() << "\n");
00482   std::map<const Function*, EdgeWeights>::iterator J =
00483     EdgeInformation.find(Old);
00484   if(J != EdgeInformation.end()) {
00485     EdgeInformation[New] = J->second;
00486   }
00487   EdgeInformation.erase(Old);
00488   BlockInformation.erase(Old);
00489   FunctionInformation.erase(Old);
00490 }
00491 
00492 static double readEdgeOrRemember(ProfileInfo::Edge edge, double w,
00493                                  ProfileInfo::Edge &tocalc, unsigned &uncalc) {
00494   if (w == ProfileInfo::MissingValue) {
00495     tocalc = edge;
00496     uncalc++;
00497     return 0;
00498   } else {
00499     return w;
00500   }
00501 }
00502 
00503 template<>
00504 bool ProfileInfoT<Function,BasicBlock>::
00505         CalculateMissingEdge(const BasicBlock *BB, Edge &removed,
00506                              bool assumeEmptySelf) {
00507   Edge edgetocalc;
00508   unsigned uncalculated = 0;
00509 
00510   // collect weights of all incoming and outgoing edges, rememer edges that
00511   // have no value
00512   double incount = 0;
00513   SmallSet<const BasicBlock*,8> pred_visited;
00514   const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
00515   if (bbi==bbe) {
00516     Edge e = getEdge(0,BB);
00517     incount += readEdgeOrRemember(e, getEdgeWeight(e) ,edgetocalc,uncalculated);
00518   }
00519   for (;bbi != bbe; ++bbi) {
00520     if (pred_visited.insert(*bbi)) {
00521       Edge e = getEdge(*bbi,BB);
00522       incount += readEdgeOrRemember(e, getEdgeWeight(e) ,edgetocalc,uncalculated);
00523     }
00524   }
00525 
00526   double outcount = 0;
00527   SmallSet<const BasicBlock*,8> succ_visited;
00528   succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
00529   if (sbbi==sbbe) {
00530     Edge e = getEdge(BB,0);
00531     if (getEdgeWeight(e) == MissingValue) {
00532       double w = getExecutionCount(BB);
00533       if (w != MissingValue) {
00534         setEdgeWeight(e,w);
00535         removed = e;
00536       }
00537     }
00538     outcount += readEdgeOrRemember(e, getEdgeWeight(e), edgetocalc, uncalculated);
00539   }
00540   for (;sbbi != sbbe; ++sbbi) {
00541     if (succ_visited.insert(*sbbi)) {
00542       Edge e = getEdge(BB,*sbbi);
00543       outcount += readEdgeOrRemember(e, getEdgeWeight(e), edgetocalc, uncalculated);
00544     }
00545   }
00546 
00547   // if exactly one edge weight was missing, calculate it and remove it from
00548   // spanning tree
00549   if (uncalculated == 0 ) {
00550     return true;
00551   } else
00552   if (uncalculated == 1) {
00553     if (incount < outcount) {
00554       EdgeInformation[BB->getParent()][edgetocalc] = outcount-incount;
00555     } else {
00556       EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
00557     }
00558     DEBUG(dbgs() << "--Calc Edge Counter for " << edgetocalc << ": "
00559                  << format("%.20g", getEdgeWeight(edgetocalc)) << "\n");
00560     removed = edgetocalc;
00561     return true;
00562   } else 
00563   if (uncalculated == 2 && assumeEmptySelf && edgetocalc.first == edgetocalc.second && incount == outcount) {
00564     setEdgeWeight(edgetocalc, incount * 10);
00565     removed = edgetocalc;
00566     return true;
00567   } else {
00568     return false;
00569   }
00570 }
00571 
00572 static void readEdge(ProfileInfo *PI, ProfileInfo::Edge e, double &calcw, std::set<ProfileInfo::Edge> &misscount) {
00573   double w = PI->getEdgeWeight(e);
00574   if (w != ProfileInfo::MissingValue) {
00575     calcw += w;
00576   } else {
00577     misscount.insert(e);
00578   }
00579 }
00580 
00581 template<>
00582 bool ProfileInfoT<Function,BasicBlock>::EstimateMissingEdges(const BasicBlock *BB) {
00583   double inWeight = 0;
00584   std::set<Edge> inMissing;
00585   std::set<const BasicBlock*> ProcessedPreds;
00586   const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
00587   if (bbi == bbe) {
00588     readEdge(this,getEdge(0,BB),inWeight,inMissing);
00589   }
00590   for( ; bbi != bbe; ++bbi ) {
00591     if (ProcessedPreds.insert(*bbi).second) {
00592       readEdge(this,getEdge(*bbi,BB),inWeight,inMissing);
00593     }
00594   }
00595 
00596   double outWeight = 0;
00597   std::set<Edge> outMissing;
00598   std::set<const BasicBlock*> ProcessedSuccs;
00599   succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
00600   if (sbbi == sbbe)
00601     readEdge(this,getEdge(BB,0),outWeight,outMissing);
00602   for ( ; sbbi != sbbe; ++sbbi ) {
00603     if (ProcessedSuccs.insert(*sbbi).second) {
00604       readEdge(this,getEdge(BB,*sbbi),outWeight,outMissing);
00605     }
00606   }
00607 
00608   double share;
00609   std::set<Edge>::iterator ei,ee;
00610   if (inMissing.size() == 0 && outMissing.size() > 0) {
00611     ei = outMissing.begin();
00612     ee = outMissing.end();
00613     share = inWeight/outMissing.size();
00614     setExecutionCount(BB,inWeight);
00615   } else
00616   if (inMissing.size() > 0 && outMissing.size() == 0 && outWeight == 0) {
00617     ei = inMissing.begin();
00618     ee = inMissing.end();
00619     share = 0;
00620     setExecutionCount(BB,0);
00621   } else
00622   if (inMissing.size() == 0 && outMissing.size() == 0) {
00623     setExecutionCount(BB,outWeight);
00624     return true;
00625   } else {
00626     return false;
00627   }
00628   for ( ; ei != ee; ++ei ) {
00629     setEdgeWeight(*ei,share);
00630   }
00631   return true;
00632 }
00633 
00634 template<>
00635 void ProfileInfoT<Function,BasicBlock>::repair(const Function *F) {
00636 //  if (getExecutionCount(&(F->getEntryBlock())) == 0) {
00637 //    for (Function::const_iterator FI = F->begin(), FE = F->end();
00638 //         FI != FE; ++FI) {
00639 //      const BasicBlock* BB = &(*FI);
00640 //      {
00641 //        const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00642 //        if (NBB == End) {
00643 //          setEdgeWeight(getEdge(0,BB),0);
00644 //        }
00645 //        for(;NBB != End; ++NBB) {
00646 //          setEdgeWeight(getEdge(*NBB,BB),0);
00647 //        }
00648 //      }
00649 //      {
00650 //        succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
00651 //        if (NBB == End) {
00652 //          setEdgeWeight(getEdge(0,BB),0);
00653 //        }
00654 //        for(;NBB != End; ++NBB) {
00655 //          setEdgeWeight(getEdge(*NBB,BB),0);
00656 //        }
00657 //      }
00658 //    }
00659 //    return;
00660 //  }
00661   // The set of BasicBlocks that are still unvisited.
00662   std::set<const BasicBlock*> Unvisited;
00663 
00664   // The set of return edges (Edges with no successors).
00665   std::set<Edge> ReturnEdges;
00666   double ReturnWeight = 0;
00667   
00668   // First iterate over the whole function and collect:
00669   // 1) The blocks in this function in the Unvisited set.
00670   // 2) The return edges in the ReturnEdges set.
00671   // 3) The flow that is leaving the function already via return edges.
00672 
00673   // Data structure for searching the function.
00674   std::queue<const BasicBlock *> BFS;
00675   const BasicBlock *BB = &(F->getEntryBlock());
00676   BFS.push(BB);
00677   Unvisited.insert(BB);
00678 
00679   while (BFS.size()) {
00680     BB = BFS.front(); BFS.pop();
00681     succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
00682     if (NBB == End) {
00683       Edge e = getEdge(BB,0);
00684       double w = getEdgeWeight(e);
00685       if (w == MissingValue) {
00686         // If the return edge has no value, try to read value from block.
00687         double bw = getExecutionCount(BB);
00688         if (bw != MissingValue) {
00689           setEdgeWeight(e,bw);
00690           ReturnWeight += bw;
00691         } else {
00692           // If both return edge and block provide no value, collect edge.
00693           ReturnEdges.insert(e);
00694         }
00695       } else {
00696         // If the return edge has a proper value, collect it.
00697         ReturnWeight += w;
00698       }
00699     }
00700     for (;NBB != End; ++NBB) {
00701       if (Unvisited.insert(*NBB).second) {
00702         BFS.push(*NBB);
00703       }
00704     }
00705   }
00706 
00707   while (Unvisited.size() > 0) {
00708     unsigned oldUnvisitedCount = Unvisited.size();
00709     bool FoundPath = false;
00710 
00711     // If there is only one edge left, calculate it.
00712     if (ReturnEdges.size() == 1) {
00713       ReturnWeight = getExecutionCount(&(F->getEntryBlock())) - ReturnWeight;
00714 
00715       Edge e = *ReturnEdges.begin();
00716       setEdgeWeight(e,ReturnWeight);
00717       setExecutionCount(e.first,ReturnWeight);
00718 
00719       Unvisited.erase(e.first);
00720       ReturnEdges.erase(e);
00721       continue;
00722     }
00723 
00724     // Calculate all blocks where only one edge is missing, this may also
00725     // resolve furhter return edges.
00726     std::set<const BasicBlock *>::iterator FI = Unvisited.begin(), FE = Unvisited.end();
00727     while(FI != FE) {
00728       const BasicBlock *BB = *FI; ++FI;
00729       Edge e;
00730       if(CalculateMissingEdge(BB,e,true)) {
00731         if (BlockInformation[F].find(BB) == BlockInformation[F].end()) {
00732           setExecutionCount(BB,getExecutionCount(BB));
00733         }
00734         Unvisited.erase(BB);
00735         if (e.first != 0 && e.second == 0) {
00736           ReturnEdges.erase(e);
00737           ReturnWeight += getEdgeWeight(e);
00738         }
00739       }
00740     }
00741     if (oldUnvisitedCount > Unvisited.size()) continue;
00742 
00743     // Estimate edge weights by dividing the flow proportionally.
00744     FI = Unvisited.begin(), FE = Unvisited.end();
00745     while(FI != FE) {
00746       const BasicBlock *BB = *FI; ++FI;
00747       const BasicBlock *Dest = 0;
00748       bool AllEdgesHaveSameReturn = true;
00749       // Check each Successor, these must all end up in the same or an empty
00750       // return block otherwise its dangerous to do an estimation on them.
00751       for (succ_const_iterator Succ = succ_begin(BB), End = succ_end(BB);
00752            Succ != End; ++Succ) {
00753         Path P;
00754         GetPath(*Succ, 0, P, GetPathToExit);
00755         if (Dest && Dest != P[(const BasicBlock*)0]) {
00756           AllEdgesHaveSameReturn = false;
00757         }
00758         Dest = P[(const BasicBlock*)0];
00759       }
00760       if (AllEdgesHaveSameReturn) {
00761         if(EstimateMissingEdges(BB)) {
00762           Unvisited.erase(BB);
00763           break;
00764         }
00765       }
00766     }
00767     if (oldUnvisitedCount > Unvisited.size()) continue;
00768 
00769     // Check if there is a path to an block that has a known value and redirect
00770     // flow accordingly.
00771     FI = Unvisited.begin(), FE = Unvisited.end();
00772     while(FI != FE && !FoundPath) {
00773       // Fetch path.
00774       const BasicBlock *BB = *FI; ++FI;
00775       Path P;
00776       const BasicBlock *Dest = GetPath(BB, 0, P, GetPathToValue);
00777 
00778       // Calculate incoming flow.
00779       double iw = 0; unsigned inmissing = 0; unsigned incount = 0; unsigned invalid = 0;
00780       std::set<const BasicBlock *> Processed;
00781       for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00782            NBB != End; ++NBB) {
00783         if (Processed.insert(*NBB).second) {
00784           Edge e = getEdge(*NBB, BB);
00785           double ew = getEdgeWeight(e);
00786           if (ew != MissingValue) {
00787             iw += ew;
00788             invalid++;
00789           } else {
00790             // If the path contains the successor, this means its a backedge,
00791             // do not count as missing.
00792             if (P.find(*NBB) == P.end())
00793               inmissing++;
00794           }
00795           incount++;
00796         }
00797       }
00798       if (inmissing == incount) continue;
00799       if (invalid == 0) continue;
00800 
00801       // Subtract (already) outgoing flow.
00802       Processed.clear();
00803       for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
00804            NBB != End; ++NBB) {
00805         if (Processed.insert(*NBB).second) {
00806           Edge e = getEdge(BB, *NBB);
00807           double ew = getEdgeWeight(e);
00808           if (ew != MissingValue) {
00809             iw -= ew;
00810           }
00811         }
00812       }
00813       if (iw < 0) continue;
00814 
00815       // Check the receiving end of the path if it can handle the flow.
00816       double ow = getExecutionCount(Dest);
00817       Processed.clear();
00818       for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
00819            NBB != End; ++NBB) {
00820         if (Processed.insert(*NBB).second) {
00821           Edge e = getEdge(BB, *NBB);
00822           double ew = getEdgeWeight(e);
00823           if (ew != MissingValue) {
00824             ow -= ew;
00825           }
00826         }
00827       }
00828       if (ow < 0) continue;
00829 
00830       // Determine how much flow shall be used.
00831       double ew = getEdgeWeight(getEdge(P[Dest],Dest));
00832       if (ew != MissingValue) {
00833         ew = ew<ow?ew:ow;
00834         ew = ew<iw?ew:iw;
00835       } else {
00836         if (inmissing == 0)
00837           ew = iw;
00838       }
00839 
00840       // Create flow.
00841       if (ew != MissingValue) {
00842         do {
00843           Edge e = getEdge(P[Dest],Dest);
00844           if (getEdgeWeight(e) == MissingValue) {
00845             setEdgeWeight(e,ew);
00846             FoundPath = true;
00847           }
00848           Dest = P[Dest];
00849         } while (Dest != BB);
00850       }
00851     }
00852     if (FoundPath) continue;
00853 
00854     // Calculate a block with self loop.
00855     FI = Unvisited.begin(), FE = Unvisited.end();
00856     while(FI != FE && !FoundPath) {
00857       const BasicBlock *BB = *FI; ++FI;
00858       bool SelfEdgeFound = false;
00859       for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
00860            NBB != End; ++NBB) {
00861         if (*NBB == BB) {
00862           SelfEdgeFound = true;
00863           break;
00864         }
00865       }
00866       if (SelfEdgeFound) {
00867         Edge e = getEdge(BB,BB);
00868         if (getEdgeWeight(e) == MissingValue) {
00869           double iw = 0;
00870           std::set<const BasicBlock *> Processed;
00871           for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00872                NBB != End; ++NBB) {
00873             if (Processed.insert(*NBB).second) {
00874               Edge e = getEdge(*NBB, BB);
00875               double ew = getEdgeWeight(e);
00876               if (ew != MissingValue) {
00877                 iw += ew;
00878               }
00879             }
00880           }
00881           setEdgeWeight(e,iw * 10);
00882           FoundPath = true;
00883         }
00884       }
00885     }
00886     if (FoundPath) continue;
00887 
00888     // Determine backedges, set them to zero.
00889     FI = Unvisited.begin(), FE = Unvisited.end();
00890     while(FI != FE && !FoundPath) {
00891       const BasicBlock *BB = *FI; ++FI;
00892       const BasicBlock *Dest = 0;
00893       Path P;
00894       bool BackEdgeFound = false;
00895       for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00896            NBB != End; ++NBB) {
00897         Dest = GetPath(BB, *NBB, P, GetPathToDest | GetPathWithNewEdges);
00898         if (Dest == *NBB) {
00899           BackEdgeFound = true;
00900           break;
00901         }
00902       }
00903       if (BackEdgeFound) {
00904         Edge e = getEdge(Dest,BB);
00905         double w = getEdgeWeight(e);
00906         if (w == MissingValue) {
00907           setEdgeWeight(e,0);
00908           FoundPath = true;
00909         }
00910         do {
00911           Edge e = getEdge(P[Dest], Dest);
00912           double w = getEdgeWeight(e);
00913           if (w == MissingValue) {
00914             setEdgeWeight(e,0);
00915             FoundPath = true;
00916           }
00917           Dest = P[Dest];
00918         } while (Dest != BB);
00919       }
00920     }
00921     if (FoundPath) continue;
00922 
00923     // Channel flow to return block.
00924     FI = Unvisited.begin(), FE = Unvisited.end();
00925     while(FI != FE && !FoundPath) {
00926       const BasicBlock *BB = *FI; ++FI;
00927 
00928       Path P;
00929       const BasicBlock *Dest = GetPath(BB, 0, P, GetPathToExit | GetPathWithNewEdges);
00930       Dest = P[(const BasicBlock*)0];
00931       if (!Dest) continue;
00932 
00933       if (getEdgeWeight(getEdge(Dest,0)) == MissingValue) {
00934         // Calculate incoming flow.
00935         double iw = 0;
00936         std::set<const BasicBlock *> Processed;
00937         for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00938              NBB != End; ++NBB) {
00939           if (Processed.insert(*NBB).second) {
00940             Edge e = getEdge(*NBB, BB);
00941             double ew = getEdgeWeight(e);
00942             if (ew != MissingValue) {
00943               iw += ew;
00944             }
00945           }
00946         }
00947         do {
00948           Edge e = getEdge(P[Dest], Dest);
00949           double w = getEdgeWeight(e);
00950           if (w == MissingValue) {
00951             setEdgeWeight(e,iw);
00952             FoundPath = true;
00953           } else {
00954             assert(0 && "Edge should not have value already!");
00955           }
00956           Dest = P[Dest];
00957         } while (Dest != BB);
00958       }
00959     }
00960     if (FoundPath) continue;
00961 
00962     // Speculatively set edges to zero.
00963     FI = Unvisited.begin(), FE = Unvisited.end();
00964     while(FI != FE && !FoundPath) {
00965       const BasicBlock *BB = *FI; ++FI;
00966 
00967       for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
00968            NBB != End; ++NBB) {
00969         Edge e = getEdge(*NBB,BB);
00970         double w = getEdgeWeight(e);
00971         if (w == MissingValue) {
00972           setEdgeWeight(e,0);
00973           FoundPath = true;
00974           break;
00975         }
00976       }
00977     }
00978     if (FoundPath) continue;
00979 
00980     errs() << "{";
00981     FI = Unvisited.begin(), FE = Unvisited.end();
00982     while(FI != FE) {
00983       const BasicBlock *BB = *FI; ++FI;
00984       dbgs() << BB->getName();
00985       if (FI != FE)
00986         dbgs() << ",";
00987     }
00988     errs() << "}";
00989 
00990     errs() << "ASSERT: could not repair function";
00991     assert(0 && "could not repair function");
00992   }
00993 
00994   EdgeWeights J = EdgeInformation[F];
00995   for (EdgeWeights::iterator EI = J.begin(), EE = J.end(); EI != EE; ++EI) {
00996     Edge e = EI->first;
00997 
00998     bool SuccFound = false;
00999     if (e.first != 0) {
01000       succ_const_iterator NBB = succ_begin(e.first), End = succ_end(e.first);
01001       if (NBB == End) {
01002         if (0 == e.second) {
01003           SuccFound = true;
01004         }
01005       }
01006       for (;NBB != End; ++NBB) {
01007         if (*NBB == e.second) {
01008           SuccFound = true;
01009           break;
01010         }
01011       }
01012       if (!SuccFound) {
01013         removeEdge(e);
01014       }
01015     }
01016   }
01017 }
01018 
01019 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF) {
01020   return O << MF->getFunction()->getName() << "(MF)";
01021 }
01022 
01023 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB) {
01024   return O << MBB->getBasicBlock()->getName() << "(MB)";
01025 }
01026 
01027 raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E) {
01028   O << "(";
01029 
01030   if (E.first)
01031     O << E.first;
01032   else
01033     O << "0";
01034 
01035   O << ",";
01036 
01037   if (E.second)
01038     O << E.second;
01039   else
01040     O << "0";
01041 
01042   return O << ")";
01043 }
01044 
01045 } // namespace llvm
01046 
01047 //===----------------------------------------------------------------------===//
01048 //  NoProfile ProfileInfo implementation
01049 //
01050 
01051 namespace {
01052   struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
01053     static char ID; // Class identification, replacement for typeinfo
01054     NoProfileInfo() : ImmutablePass(ID) {
01055       initializeNoProfileInfoPass(*PassRegistry::getPassRegistry());
01056     }
01057     
01058     /// getAdjustedAnalysisPointer - This method is used when a pass implements
01059     /// an analysis interface through multiple inheritance.  If needed, it
01060     /// should override this to adjust the this pointer as needed for the
01061     /// specified pass info.
01062     virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
01063       if (PI == &ProfileInfo::ID)
01064         return (ProfileInfo*)this;
01065       return this;
01066     }
01067     
01068     virtual const char *getPassName() const {
01069       return "NoProfileInfo";
01070     }
01071   };
01072 }  // End of anonymous namespace
01073 
01074 char NoProfileInfo::ID = 0;
01075 // Register this pass...
01076 INITIALIZE_AG_PASS(NoProfileInfo, ProfileInfo, "no-profile",
01077                    "No Profile Information", false, true, true)
01078 
01079 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }