1 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
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 implements the abstract ProfileInfo interface, and the default
11 // "no profile" implementation.
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "profile-info"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Analysis/ProfileInfo.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CFG.h"
21 #include "llvm/ADT/SmallSet.h"
27 // Register the ProfileInfo interface, providing a nice name to refer to.
28 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
33 ProfileInfoT<MachineFunction, MachineBasicBlock>::ProfileInfoT() {}
35 ProfileInfoT<MachineFunction, MachineBasicBlock>::~ProfileInfoT() {}
38 ProfileInfoT<Function, BasicBlock>::ProfileInfoT() {
42 ProfileInfoT<Function, BasicBlock>::~ProfileInfoT() {
43 if (MachineProfile) delete MachineProfile;
47 char ProfileInfoT<Function,BasicBlock>::ID = 0;
50 char ProfileInfoT<MachineFunction, MachineBasicBlock>::ID = 0;
53 const double ProfileInfoT<Function,BasicBlock>::MissingValue = -1;
56 double ProfileInfoT<MachineFunction, MachineBasicBlock>::MissingValue = -1;
59 ProfileInfoT<Function,BasicBlock>::getExecutionCount(const BasicBlock *BB) {
60 std::map<const Function*, BlockCounts>::iterator J =
61 BlockInformation.find(BB->getParent());
62 if (J != BlockInformation.end()) {
63 BlockCounts::iterator I = J->second.find(BB);
64 if (I != J->second.end())
68 double Count = MissingValue;
70 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
72 // Are there zero predecessors of this block?
74 Edge e = getEdge(0, BB);
75 Count = getEdgeWeight(e);
77 // Otherwise, if there are predecessors, the execution count of this block is
78 // the sum of the edge frequencies from the incoming edges.
79 std::set<const BasicBlock*> ProcessedPreds;
81 for (; PI != PE; ++PI) {
82 const BasicBlock *P = *PI;
83 if (ProcessedPreds.insert(P).second) {
84 double w = getEdgeWeight(getEdge(P, BB));
85 if (w == MissingValue) {
94 // If the predecessors did not suffice to get block weight, try successors.
95 if (Count == MissingValue) {
97 succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
99 // Are there zero successors of this block?
101 Edge e = getEdge(BB,0);
102 Count = getEdgeWeight(e);
104 std::set<const BasicBlock*> ProcessedSuccs;
106 for (; SI != SE; ++SI)
107 if (ProcessedSuccs.insert(*SI).second) {
108 double w = getEdgeWeight(getEdge(BB, *SI));
109 if (w == MissingValue) {
110 Count = MissingValue;
118 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
123 double ProfileInfoT<MachineFunction, MachineBasicBlock>::
124 getExecutionCount(const MachineBasicBlock *MBB) {
125 std::map<const MachineFunction*, BlockCounts>::iterator J =
126 BlockInformation.find(MBB->getParent());
127 if (J != BlockInformation.end()) {
128 BlockCounts::iterator I = J->second.find(MBB);
129 if (I != J->second.end())
137 double ProfileInfoT<Function,BasicBlock>::getExecutionCount(const Function *F) {
138 std::map<const Function*, double>::iterator J =
139 FunctionInformation.find(F);
140 if (J != FunctionInformation.end())
143 // isDeclaration() is checked here and not at start of function to allow
144 // functions without a body still to have a execution count.
145 if (F->isDeclaration()) return MissingValue;
147 double Count = getExecutionCount(&F->getEntryBlock());
148 if (Count != MissingValue) FunctionInformation[F] = Count;
153 double ProfileInfoT<MachineFunction, MachineBasicBlock>::
154 getExecutionCount(const MachineFunction *MF) {
155 std::map<const MachineFunction*, double>::iterator J =
156 FunctionInformation.find(MF);
157 if (J != FunctionInformation.end())
160 double Count = getExecutionCount(&MF->front());
161 if (Count != MissingValue) FunctionInformation[MF] = Count;
166 void ProfileInfoT<Function,BasicBlock>::
167 setExecutionCount(const BasicBlock *BB, double w) {
168 DEBUG(dbgs() << "Creating Block " << BB->getName()
169 << " (weight: " << format("%.20g",w) << ")\n");
170 BlockInformation[BB->getParent()][BB] = w;
174 void ProfileInfoT<MachineFunction, MachineBasicBlock>::
175 setExecutionCount(const MachineBasicBlock *MBB, double w) {
176 DEBUG(dbgs() << "Creating Block " << MBB->getBasicBlock()->getName()
177 << " (weight: " << format("%.20g",w) << ")\n");
178 BlockInformation[MBB->getParent()][MBB] = w;
182 void ProfileInfoT<Function,BasicBlock>::addEdgeWeight(Edge e, double w) {
183 double oldw = getEdgeWeight(e);
184 assert (oldw != MissingValue && "Adding weight to Edge with no previous weight");
185 DEBUG(dbgs() << "Adding to Edge " << e
186 << " (new weight: " << format("%.20g",oldw + w) << ")\n");
187 EdgeInformation[getFunction(e)][e] = oldw + w;
191 void ProfileInfoT<Function,BasicBlock>::
192 addExecutionCount(const BasicBlock *BB, double w) {
193 double oldw = getExecutionCount(BB);
194 assert (oldw != MissingValue && "Adding weight to Block with no previous weight");
195 DEBUG(dbgs() << "Adding to Block " << BB->getName()
196 << " (new weight: " << format("%.20g",oldw + w) << ")\n");
197 BlockInformation[BB->getParent()][BB] = oldw + w;
201 void ProfileInfoT<Function,BasicBlock>::removeBlock(const BasicBlock *BB) {
202 std::map<const Function*, BlockCounts>::iterator J =
203 BlockInformation.find(BB->getParent());
204 if (J == BlockInformation.end()) return;
206 DEBUG(dbgs() << "Deleting " << BB->getName() << "\n");
211 void ProfileInfoT<Function,BasicBlock>::removeEdge(Edge e) {
212 std::map<const Function*, EdgeWeights>::iterator J =
213 EdgeInformation.find(getFunction(e));
214 if (J == EdgeInformation.end()) return;
216 DEBUG(dbgs() << "Deleting" << e << "\n");
221 void ProfileInfoT<Function,BasicBlock>::
222 replaceEdge(const Edge &oldedge, const Edge &newedge) {
224 if ((w = getEdgeWeight(newedge)) == MissingValue) {
225 w = getEdgeWeight(oldedge);
226 DEBUG(dbgs() << "Replacing " << oldedge << " with " << newedge << "\n");
228 w += getEdgeWeight(oldedge);
229 DEBUG(dbgs() << "Adding " << oldedge << " to " << newedge << "\n");
231 setEdgeWeight(newedge,w);
236 const BasicBlock *ProfileInfoT<Function,BasicBlock>::
237 GetPath(const BasicBlock *Src, const BasicBlock *Dest,
238 Path &P, unsigned Mode) {
239 const BasicBlock *BB = 0;
240 bool hasFoundPath = false;
242 std::queue<const BasicBlock *> BFS;
245 while(BFS.size() && !hasFoundPath) {
249 succ_const_iterator Succ = succ_begin(BB), End = succ_end(BB);
252 if (Mode & GetPathToExit) {
257 for(;Succ != End; ++Succ) {
258 if (P.find(*Succ) != P.end()) continue;
259 Edge e = getEdge(BB,*Succ);
260 if ((Mode & GetPathWithNewEdges) && (getEdgeWeight(e) != MissingValue)) continue;
263 if ((Mode & GetPathToDest) && *Succ == Dest) {
268 if ((Mode & GetPathToValue) && (getExecutionCount(*Succ) != MissingValue)) {
280 void ProfileInfoT<Function,BasicBlock>::
281 divertFlow(const Edge &oldedge, const Edge &newedge) {
282 DEBUG(dbgs() << "Diverting " << oldedge << " via " << newedge );
284 // First check if the old edge was taken, if not, just delete it...
285 if (getEdgeWeight(oldedge) == 0) {
291 P[newedge.first] = 0;
292 P[newedge.second] = newedge.first;
293 const BasicBlock *BB = GetPath(newedge.second,oldedge.second,P,GetPathToExit | GetPathToDest);
295 double w = getEdgeWeight (oldedge);
296 DEBUG(dbgs() << ", Weight: " << format("%.20g",w) << "\n");
298 const BasicBlock *Parent = P.find(BB)->second;
299 Edge e = getEdge(Parent,BB);
300 double oldw = getEdgeWeight(e);
301 double oldc = getExecutionCount(e.first);
302 setEdgeWeight(e, w+oldw);
303 if (Parent != oldedge.first) {
304 setExecutionCount(e.first, w+oldc);
307 } while (BB != newedge.first);
311 /// Replaces all occurences of RmBB in the ProfilingInfo with DestBB.
312 /// This checks all edges of the function the blocks reside in and replaces the
313 /// occurences of RmBB with DestBB.
315 void ProfileInfoT<Function,BasicBlock>::
316 replaceAllUses(const BasicBlock *RmBB, const BasicBlock *DestBB) {
317 DEBUG(dbgs() << "Replacing " << RmBB->getName()
318 << " with " << DestBB->getName() << "\n");
319 const Function *F = DestBB->getParent();
320 std::map<const Function*, EdgeWeights>::iterator J =
321 EdgeInformation.find(F);
322 if (J == EdgeInformation.end()) return;
325 bool erasededge = false;
326 EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
329 bool foundedge = false; bool eraseedge = false;
330 if (e.first == RmBB) {
331 if (e.second == DestBB) {
334 newedge = getEdge(DestBB, e.second);
338 if (e.second == RmBB) {
339 if (e.first == DestBB) {
342 newedge = getEdge(e.first, DestBB);
347 replaceEdge(e, newedge);
351 Edge newedge = getEdge(DestBB, DestBB);
352 replaceEdge(e, newedge);
361 /// Splits an edge in the ProfileInfo and redirects flow over NewBB.
362 /// Since its possible that there is more than one edge in the CFG from FristBB
363 /// to SecondBB its necessary to redirect the flow proporionally.
365 void ProfileInfoT<Function,BasicBlock>::splitEdge(const BasicBlock *FirstBB,
366 const BasicBlock *SecondBB,
367 const BasicBlock *NewBB,
368 bool MergeIdenticalEdges) {
369 const Function *F = FirstBB->getParent();
370 std::map<const Function*, EdgeWeights>::iterator J =
371 EdgeInformation.find(F);
372 if (J == EdgeInformation.end()) return;
374 // Generate edges and read current weight.
375 Edge e = getEdge(FirstBB, SecondBB);
376 Edge n1 = getEdge(FirstBB, NewBB);
377 Edge n2 = getEdge(NewBB, SecondBB);
378 EdgeWeights &ECs = J->second;
382 if (!MergeIdenticalEdges) {
383 // First count the edges from FristBB to SecondBB, if there is more than
384 // one, only slice out a proporional part for NewBB.
385 for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
387 if (*BBI == SecondBB) succ_count++;
389 // When the NewBB is completely new, increment the count by one so that
390 // the counts are properly distributed.
391 if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
393 // When the edges are merged anyway, then redirect all flow.
397 // We know now how many edges there are from FirstBB to SecondBB, reroute a
398 // proportional part of the edge weight over NewBB.
399 double neww = floor(w / succ_count);
402 BlockInformation[F][NewBB] += neww;
403 if (succ_count == 1) {
411 void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *Old,
412 const BasicBlock* New) {
413 const Function *F = Old->getParent();
414 std::map<const Function*, EdgeWeights>::iterator J =
415 EdgeInformation.find(F);
416 if (J == EdgeInformation.end()) return;
418 DEBUG(dbgs() << "Splitting " << Old->getName() << " to " << New->getName() << "\n");
420 std::set<Edge> Edges;
421 for (EdgeWeights::iterator ewi = J->second.begin(), ewe = J->second.end();
423 Edge old = ewi->first;
424 if (old.first == Old) {
428 for (std::set<Edge>::iterator EI = Edges.begin(), EE = Edges.end();
430 Edge newedge = getEdge(New, EI->second);
431 replaceEdge(*EI, newedge);
434 double w = getExecutionCount(Old);
435 setEdgeWeight(getEdge(Old, New), w);
436 setExecutionCount(New, w);
440 void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *BB,
441 const BasicBlock* NewBB,
442 BasicBlock *const *Preds,
444 const Function *F = BB->getParent();
445 std::map<const Function*, EdgeWeights>::iterator J =
446 EdgeInformation.find(F);
447 if (J == EdgeInformation.end()) return;
449 DEBUG(dbgs() << "Splitting " << NumPreds << " Edges from " << BB->getName()
450 << " to " << NewBB->getName() << "\n");
452 // Collect weight that was redirected over NewBB.
453 double newweight = 0;
455 std::set<const BasicBlock *> ProcessedPreds;
456 // For all requestes Predecessors.
457 for (unsigned pred = 0; pred < NumPreds; ++pred) {
458 const BasicBlock * Pred = Preds[pred];
459 if (ProcessedPreds.insert(Pred).second) {
460 // Create edges and read old weight.
461 Edge oldedge = getEdge(Pred, BB);
462 Edge newedge = getEdge(Pred, NewBB);
464 // Remember how much weight was redirected.
465 newweight += getEdgeWeight(oldedge);
467 replaceEdge(oldedge,newedge);
471 Edge newedge = getEdge(NewBB,BB);
472 setEdgeWeight(newedge, newweight);
473 setExecutionCount(NewBB, newweight);
477 void ProfileInfoT<Function,BasicBlock>::transfer(const Function *Old,
478 const Function *New) {
479 DEBUG(dbgs() << "Replacing Function " << Old->getName() << " with "
480 << New->getName() << "\n");
481 std::map<const Function*, EdgeWeights>::iterator J =
482 EdgeInformation.find(Old);
483 if(J != EdgeInformation.end()) {
484 EdgeInformation[New] = J->second;
486 EdgeInformation.erase(Old);
487 BlockInformation.erase(Old);
488 FunctionInformation.erase(Old);
491 static double readEdgeOrRemember(ProfileInfo::Edge edge, double w,
492 ProfileInfo::Edge &tocalc, unsigned &uncalc) {
493 if (w == ProfileInfo::MissingValue) {
503 bool ProfileInfoT<Function,BasicBlock>::
504 CalculateMissingEdge(const BasicBlock *BB, Edge &removed,
505 bool assumeEmptySelf) {
507 unsigned uncalculated = 0;
509 // collect weights of all incoming and outgoing edges, rememer edges that
512 SmallSet<const BasicBlock*,8> pred_visited;
513 const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
515 Edge e = getEdge(0,BB);
516 incount += readEdgeOrRemember(e, getEdgeWeight(e) ,edgetocalc,uncalculated);
518 for (;bbi != bbe; ++bbi) {
519 if (pred_visited.insert(*bbi)) {
520 Edge e = getEdge(*bbi,BB);
521 incount += readEdgeOrRemember(e, getEdgeWeight(e) ,edgetocalc,uncalculated);
526 SmallSet<const BasicBlock*,8> succ_visited;
527 succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
529 Edge e = getEdge(BB,0);
530 if (getEdgeWeight(e) == MissingValue) {
531 double w = getExecutionCount(BB);
532 if (w != MissingValue) {
537 outcount += readEdgeOrRemember(e, getEdgeWeight(e), edgetocalc, uncalculated);
539 for (;sbbi != sbbe; ++sbbi) {
540 if (succ_visited.insert(*sbbi)) {
541 Edge e = getEdge(BB,*sbbi);
542 outcount += readEdgeOrRemember(e, getEdgeWeight(e), edgetocalc, uncalculated);
546 // if exactly one edge weight was missing, calculate it and remove it from
548 if (uncalculated == 0 ) {
551 if (uncalculated == 1) {
552 if (incount < outcount) {
553 EdgeInformation[BB->getParent()][edgetocalc] = outcount-incount;
555 EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
557 DEBUG(dbgs() << "--Calc Edge Counter for " << edgetocalc << ": "
558 << format("%.20g", getEdgeWeight(edgetocalc)) << "\n");
559 removed = edgetocalc;
562 if (uncalculated == 2 && assumeEmptySelf && edgetocalc.first == edgetocalc.second && incount == outcount) {
563 setEdgeWeight(edgetocalc, incount * 10);
564 removed = edgetocalc;
571 static void readEdge(ProfileInfo *PI, ProfileInfo::Edge e, double &calcw, std::set<ProfileInfo::Edge> &misscount) {
572 double w = PI->getEdgeWeight(e);
573 if (w != ProfileInfo::MissingValue) {
581 bool ProfileInfoT<Function,BasicBlock>::EstimateMissingEdges(const BasicBlock *BB) {
583 std::set<Edge> inMissing;
584 std::set<const BasicBlock*> ProcessedPreds;
585 const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
587 readEdge(this,getEdge(0,BB),inWeight,inMissing);
589 for( ; bbi != bbe; ++bbi ) {
590 if (ProcessedPreds.insert(*bbi).second) {
591 readEdge(this,getEdge(*bbi,BB),inWeight,inMissing);
595 double outWeight = 0;
596 std::set<Edge> outMissing;
597 std::set<const BasicBlock*> ProcessedSuccs;
598 succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
600 readEdge(this,getEdge(BB,0),outWeight,outMissing);
601 for ( ; sbbi != sbbe; ++sbbi ) {
602 if (ProcessedSuccs.insert(*sbbi).second) {
603 readEdge(this,getEdge(BB,*sbbi),outWeight,outMissing);
608 std::set<Edge>::iterator ei,ee;
609 if (inMissing.size() == 0 && outMissing.size() > 0) {
610 ei = outMissing.begin();
611 ee = outMissing.end();
612 share = inWeight/outMissing.size();
613 setExecutionCount(BB,inWeight);
615 if (inMissing.size() > 0 && outMissing.size() == 0 && outWeight == 0) {
616 ei = inMissing.begin();
617 ee = inMissing.end();
619 setExecutionCount(BB,0);
621 if (inMissing.size() == 0 && outMissing.size() == 0) {
622 setExecutionCount(BB,outWeight);
627 for ( ; ei != ee; ++ei ) {
628 setEdgeWeight(*ei,share);
634 void ProfileInfoT<Function,BasicBlock>::repair(const Function *F) {
635 // if (getExecutionCount(&(F->getEntryBlock())) == 0) {
636 // for (Function::const_iterator FI = F->begin(), FE = F->end();
638 // const BasicBlock* BB = &(*FI);
640 // const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
642 // setEdgeWeight(getEdge(0,BB),0);
644 // for(;NBB != End; ++NBB) {
645 // setEdgeWeight(getEdge(*NBB,BB),0);
649 // succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
651 // setEdgeWeight(getEdge(0,BB),0);
653 // for(;NBB != End; ++NBB) {
654 // setEdgeWeight(getEdge(*NBB,BB),0);
660 // The set of BasicBlocks that are still unvisited.
661 std::set<const BasicBlock*> Unvisited;
663 // The set of return edges (Edges with no successors).
664 std::set<Edge> ReturnEdges;
665 double ReturnWeight = 0;
667 // First iterate over the whole function and collect:
668 // 1) The blocks in this function in the Unvisited set.
669 // 2) The return edges in the ReturnEdges set.
670 // 3) The flow that is leaving the function already via return edges.
672 // Data structure for searching the function.
673 std::queue<const BasicBlock *> BFS;
674 const BasicBlock *BB = &(F->getEntryBlock());
676 Unvisited.insert(BB);
679 BB = BFS.front(); BFS.pop();
680 succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
682 Edge e = getEdge(BB,0);
683 double w = getEdgeWeight(e);
684 if (w == MissingValue) {
685 // If the return edge has no value, try to read value from block.
686 double bw = getExecutionCount(BB);
687 if (bw != MissingValue) {
691 // If both return edge and block provide no value, collect edge.
692 ReturnEdges.insert(e);
695 // If the return edge has a proper value, collect it.
699 for (;NBB != End; ++NBB) {
700 if (Unvisited.insert(*NBB).second) {
706 while (Unvisited.size() > 0) {
707 unsigned oldUnvisitedCount = Unvisited.size();
708 bool FoundPath = false;
710 // If there is only one edge left, calculate it.
711 if (ReturnEdges.size() == 1) {
712 ReturnWeight = getExecutionCount(&(F->getEntryBlock())) - ReturnWeight;
714 Edge e = *ReturnEdges.begin();
715 setEdgeWeight(e,ReturnWeight);
716 setExecutionCount(e.first,ReturnWeight);
718 Unvisited.erase(e.first);
719 ReturnEdges.erase(e);
723 // Calculate all blocks where only one edge is missing, this may also
724 // resolve furhter return edges.
725 std::set<const BasicBlock *>::iterator FI = Unvisited.begin(), FE = Unvisited.end();
727 const BasicBlock *BB = *FI; ++FI;
729 if(CalculateMissingEdge(BB,e,true)) {
730 if (BlockInformation[F].find(BB) == BlockInformation[F].end()) {
731 setExecutionCount(BB,getExecutionCount(BB));
734 if (e.first != 0 && e.second == 0) {
735 ReturnEdges.erase(e);
736 ReturnWeight += getEdgeWeight(e);
740 if (oldUnvisitedCount > Unvisited.size()) continue;
742 // Estimate edge weights by dividing the flow proportionally.
743 FI = Unvisited.begin(), FE = Unvisited.end();
745 const BasicBlock *BB = *FI; ++FI;
746 const BasicBlock *Dest = 0;
747 bool AllEdgesHaveSameReturn = true;
748 // Check each Successor, these must all end up in the same or an empty
749 // return block otherwise its dangerous to do an estimation on them.
750 for (succ_const_iterator Succ = succ_begin(BB), End = succ_end(BB);
751 Succ != End; ++Succ) {
753 GetPath(*Succ, 0, P, GetPathToExit);
754 if (Dest && Dest != P[0]) {
755 AllEdgesHaveSameReturn = false;
759 if (AllEdgesHaveSameReturn) {
760 if(EstimateMissingEdges(BB)) {
766 if (oldUnvisitedCount > Unvisited.size()) continue;
768 // Check if there is a path to an block that has a known value and redirect
770 FI = Unvisited.begin(), FE = Unvisited.end();
771 while(FI != FE && !FoundPath) {
773 const BasicBlock *BB = *FI; ++FI;
775 const BasicBlock *Dest = GetPath(BB, 0, P, GetPathToValue);
777 // Calculate incoming flow.
778 double iw = 0; unsigned inmissing = 0; unsigned incount = 0; unsigned invalid = 0;
779 std::set<const BasicBlock *> Processed;
780 for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
782 if (Processed.insert(*NBB).second) {
783 Edge e = getEdge(*NBB, BB);
784 double ew = getEdgeWeight(e);
785 if (ew != MissingValue) {
789 // If the path contains the successor, this means its a backedge,
790 // do not count as missing.
791 if (P.find(*NBB) == P.end())
797 if (inmissing == incount) continue;
798 if (invalid == 0) continue;
800 // Subtract (already) outgoing flow.
802 for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
804 if (Processed.insert(*NBB).second) {
805 Edge e = getEdge(BB, *NBB);
806 double ew = getEdgeWeight(e);
807 if (ew != MissingValue) {
812 if (iw < 0) continue;
814 // Check the recieving end of the path if it can handle the flow.
815 double ow = getExecutionCount(Dest);
817 for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
819 if (Processed.insert(*NBB).second) {
820 Edge e = getEdge(BB, *NBB);
821 double ew = getEdgeWeight(e);
822 if (ew != MissingValue) {
827 if (ow < 0) continue;
829 // Determine how much flow shall be used.
830 double ew = getEdgeWeight(getEdge(P[Dest],Dest));
831 if (ew != MissingValue) {
840 if (ew != MissingValue) {
842 Edge e = getEdge(P[Dest],Dest);
843 if (getEdgeWeight(e) == MissingValue) {
848 } while (Dest != BB);
851 if (FoundPath) continue;
853 // Calculate a block with self loop.
854 FI = Unvisited.begin(), FE = Unvisited.end();
855 while(FI != FE && !FoundPath) {
856 const BasicBlock *BB = *FI; ++FI;
857 bool SelfEdgeFound = false;
858 for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
861 SelfEdgeFound = true;
866 Edge e = getEdge(BB,BB);
867 if (getEdgeWeight(e) == MissingValue) {
869 std::set<const BasicBlock *> Processed;
870 for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
872 if (Processed.insert(*NBB).second) {
873 Edge e = getEdge(*NBB, BB);
874 double ew = getEdgeWeight(e);
875 if (ew != MissingValue) {
880 setEdgeWeight(e,iw * 10);
885 if (FoundPath) continue;
887 // Determine backedges, set them to zero.
888 FI = Unvisited.begin(), FE = Unvisited.end();
889 while(FI != FE && !FoundPath) {
890 const BasicBlock *BB = *FI; ++FI;
891 const BasicBlock *Dest;
893 bool BackEdgeFound = false;
894 for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
896 Dest = GetPath(BB, *NBB, P, GetPathToDest | GetPathWithNewEdges);
898 BackEdgeFound = true;
903 Edge e = getEdge(Dest,BB);
904 double w = getEdgeWeight(e);
905 if (w == MissingValue) {
910 Edge e = getEdge(P[Dest], Dest);
911 double w = getEdgeWeight(e);
912 if (w == MissingValue) {
917 } while (Dest != BB);
920 if (FoundPath) continue;
922 // Channel flow to return block.
923 FI = Unvisited.begin(), FE = Unvisited.end();
924 while(FI != FE && !FoundPath) {
925 const BasicBlock *BB = *FI; ++FI;
928 const BasicBlock *Dest = GetPath(BB, 0, P, GetPathToExit | GetPathWithNewEdges);
932 if (getEdgeWeight(getEdge(Dest,0)) == MissingValue) {
933 // Calculate incoming flow.
935 std::set<const BasicBlock *> Processed;
936 for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
938 if (Processed.insert(*NBB).second) {
939 Edge e = getEdge(*NBB, BB);
940 double ew = getEdgeWeight(e);
941 if (ew != MissingValue) {
947 Edge e = getEdge(P[Dest], Dest);
948 double w = getEdgeWeight(e);
949 if (w == MissingValue) {
953 assert(0 && "Edge should not have value already!");
956 } while (Dest != BB);
959 if (FoundPath) continue;
961 // Speculatively set edges to zero.
962 FI = Unvisited.begin(), FE = Unvisited.end();
963 while(FI != FE && !FoundPath) {
964 const BasicBlock *BB = *FI; ++FI;
966 for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
968 Edge e = getEdge(*NBB,BB);
969 double w = getEdgeWeight(e);
970 if (w == MissingValue) {
977 if (FoundPath) continue;
980 FI = Unvisited.begin(), FE = Unvisited.end();
982 const BasicBlock *BB = *FI; ++FI;
983 dbgs() << BB->getName();
989 errs() << "ASSERT: could not repair function";
990 assert(0 && "could not repair function");
993 EdgeWeights J = EdgeInformation[F];
994 for (EdgeWeights::iterator EI = J.begin(), EE = J.end(); EI != EE; ++EI) {
997 bool SuccFound = false;
999 succ_const_iterator NBB = succ_begin(e.first), End = succ_end(e.first);
1001 if (0 == e.second) {
1005 for (;NBB != End; ++NBB) {
1006 if (*NBB == e.second) {
1018 raw_ostream& operator<<(raw_ostream &O, const Function *F) {
1019 return O << F->getName();
1022 raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF) {
1023 return O << MF->getFunction()->getName() << "(MF)";
1026 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
1027 return O << BB->getName();
1030 raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB) {
1031 return O << MBB->getBasicBlock()->getName() << "(MB)";
1034 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E) {
1052 raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E) {
1072 //===----------------------------------------------------------------------===//
1073 // NoProfile ProfileInfo implementation
1077 struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
1078 static char ID; // Class identification, replacement for typeinfo
1079 NoProfileInfo() : ImmutablePass(ID) {}
1081 /// getAdjustedAnalysisPointer - This method is used when a pass implements
1082 /// an analysis interface through multiple inheritance. If needed, it
1083 /// should override this to adjust the this pointer as needed for the
1084 /// specified pass info.
1085 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
1086 if (PI == &ProfileInfo::ID)
1087 return (ProfileInfo*)this;
1091 virtual const char *getPassName() const {
1092 return "NoProfileInfo";
1095 } // End of anonymous namespace
1097 char NoProfileInfo::ID = 0;
1098 // Register this pass...
1099 INITIALIZE_AG_PASS(NoProfileInfo, ProfileInfo, "no-profile",
1100 "No Profile Information", false, true, true);
1102 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }