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"
28 template<> char ProfileInfoT
<Function
,BasicBlock
>::ID
= 0;
31 // Register the ProfileInfo interface, providing a nice name to refer to.
32 INITIALIZE_ANALYSIS_GROUP(ProfileInfo
, "Profile Information", NoProfileInfo
)
37 ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::ProfileInfoT() {}
39 ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::~ProfileInfoT() {}
42 ProfileInfoT
<Function
, BasicBlock
>::ProfileInfoT() {
46 ProfileInfoT
<Function
, BasicBlock
>::~ProfileInfoT() {
47 if (MachineProfile
) delete MachineProfile
;
51 char ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::ID
= 0;
54 const double ProfileInfoT
<Function
,BasicBlock
>::MissingValue
= -1;
57 double ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::MissingValue
= -1;
60 ProfileInfoT
<Function
,BasicBlock
>::getExecutionCount(const BasicBlock
*BB
) {
61 std::map
<const Function
*, BlockCounts
>::iterator J
=
62 BlockInformation
.find(BB
->getParent());
63 if (J
!= BlockInformation
.end()) {
64 BlockCounts::iterator I
= J
->second
.find(BB
);
65 if (I
!= J
->second
.end())
69 double Count
= MissingValue
;
71 const_pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
);
73 // Are there zero predecessors of this block?
75 Edge e
= getEdge(0, BB
);
76 Count
= getEdgeWeight(e
);
78 // Otherwise, if there are predecessors, the execution count of this block is
79 // the sum of the edge frequencies from the incoming edges.
80 std::set
<const BasicBlock
*> ProcessedPreds
;
82 for (; PI
!= PE
; ++PI
) {
83 const BasicBlock
*P
= *PI
;
84 if (ProcessedPreds
.insert(P
).second
) {
85 double w
= getEdgeWeight(getEdge(P
, BB
));
86 if (w
== MissingValue
) {
95 // If the predecessors did not suffice to get block weight, try successors.
96 if (Count
== MissingValue
) {
98 succ_const_iterator SI
= succ_begin(BB
), SE
= succ_end(BB
);
100 // Are there zero successors of this block?
102 Edge e
= getEdge(BB
,0);
103 Count
= getEdgeWeight(e
);
105 std::set
<const BasicBlock
*> ProcessedSuccs
;
107 for (; SI
!= SE
; ++SI
)
108 if (ProcessedSuccs
.insert(*SI
).second
) {
109 double w
= getEdgeWeight(getEdge(BB
, *SI
));
110 if (w
== MissingValue
) {
111 Count
= MissingValue
;
119 if (Count
!= MissingValue
) BlockInformation
[BB
->getParent()][BB
] = Count
;
124 double ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::
125 getExecutionCount(const MachineBasicBlock
*MBB
) {
126 std::map
<const MachineFunction
*, BlockCounts
>::iterator J
=
127 BlockInformation
.find(MBB
->getParent());
128 if (J
!= BlockInformation
.end()) {
129 BlockCounts::iterator I
= J
->second
.find(MBB
);
130 if (I
!= J
->second
.end())
138 double ProfileInfoT
<Function
,BasicBlock
>::getExecutionCount(const Function
*F
) {
139 std::map
<const Function
*, double>::iterator J
=
140 FunctionInformation
.find(F
);
141 if (J
!= FunctionInformation
.end())
144 // isDeclaration() is checked here and not at start of function to allow
145 // functions without a body still to have a execution count.
146 if (F
->isDeclaration()) return MissingValue
;
148 double Count
= getExecutionCount(&F
->getEntryBlock());
149 if (Count
!= MissingValue
) FunctionInformation
[F
] = Count
;
154 double ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::
155 getExecutionCount(const MachineFunction
*MF
) {
156 std::map
<const MachineFunction
*, double>::iterator J
=
157 FunctionInformation
.find(MF
);
158 if (J
!= FunctionInformation
.end())
161 double Count
= getExecutionCount(&MF
->front());
162 if (Count
!= MissingValue
) FunctionInformation
[MF
] = Count
;
167 void ProfileInfoT
<Function
,BasicBlock
>::
168 setExecutionCount(const BasicBlock
*BB
, double w
) {
169 DEBUG(dbgs() << "Creating Block " << BB
->getName()
170 << " (weight: " << format("%.20g",w
) << ")\n");
171 BlockInformation
[BB
->getParent()][BB
] = w
;
175 void ProfileInfoT
<MachineFunction
, MachineBasicBlock
>::
176 setExecutionCount(const MachineBasicBlock
*MBB
, double w
) {
177 DEBUG(dbgs() << "Creating Block " << MBB
->getBasicBlock()->getName()
178 << " (weight: " << format("%.20g",w
) << ")\n");
179 BlockInformation
[MBB
->getParent()][MBB
] = w
;
183 void ProfileInfoT
<Function
,BasicBlock
>::addEdgeWeight(Edge e
, double w
) {
184 double oldw
= getEdgeWeight(e
);
185 assert (oldw
!= MissingValue
&& "Adding weight to Edge with no previous weight");
186 DEBUG(dbgs() << "Adding to Edge " << e
187 << " (new weight: " << format("%.20g",oldw
+ w
) << ")\n");
188 EdgeInformation
[getFunction(e
)][e
] = oldw
+ w
;
192 void ProfileInfoT
<Function
,BasicBlock
>::
193 addExecutionCount(const BasicBlock
*BB
, double w
) {
194 double oldw
= getExecutionCount(BB
);
195 assert (oldw
!= MissingValue
&& "Adding weight to Block with no previous weight");
196 DEBUG(dbgs() << "Adding to Block " << BB
->getName()
197 << " (new weight: " << format("%.20g",oldw
+ w
) << ")\n");
198 BlockInformation
[BB
->getParent()][BB
] = oldw
+ w
;
202 void ProfileInfoT
<Function
,BasicBlock
>::removeBlock(const BasicBlock
*BB
) {
203 std::map
<const Function
*, BlockCounts
>::iterator J
=
204 BlockInformation
.find(BB
->getParent());
205 if (J
== BlockInformation
.end()) return;
207 DEBUG(dbgs() << "Deleting " << BB
->getName() << "\n");
212 void ProfileInfoT
<Function
,BasicBlock
>::removeEdge(Edge e
) {
213 std::map
<const Function
*, EdgeWeights
>::iterator J
=
214 EdgeInformation
.find(getFunction(e
));
215 if (J
== EdgeInformation
.end()) return;
217 DEBUG(dbgs() << "Deleting" << e
<< "\n");
222 void ProfileInfoT
<Function
,BasicBlock
>::
223 replaceEdge(const Edge
&oldedge
, const Edge
&newedge
) {
225 if ((w
= getEdgeWeight(newedge
)) == MissingValue
) {
226 w
= getEdgeWeight(oldedge
);
227 DEBUG(dbgs() << "Replacing " << oldedge
<< " with " << newedge
<< "\n");
229 w
+= getEdgeWeight(oldedge
);
230 DEBUG(dbgs() << "Adding " << oldedge
<< " to " << newedge
<< "\n");
232 setEdgeWeight(newedge
,w
);
237 const BasicBlock
*ProfileInfoT
<Function
,BasicBlock
>::
238 GetPath(const BasicBlock
*Src
, const BasicBlock
*Dest
,
239 Path
&P
, unsigned Mode
) {
240 const BasicBlock
*BB
= 0;
241 bool hasFoundPath
= false;
243 std::queue
<const BasicBlock
*> BFS
;
246 while(BFS
.size() && !hasFoundPath
) {
250 succ_const_iterator Succ
= succ_begin(BB
), End
= succ_end(BB
);
253 if (Mode
& GetPathToExit
) {
258 for(;Succ
!= End
; ++Succ
) {
259 if (P
.find(*Succ
) != P
.end()) continue;
260 Edge e
= getEdge(BB
,*Succ
);
261 if ((Mode
& GetPathWithNewEdges
) && (getEdgeWeight(e
) != MissingValue
)) continue;
264 if ((Mode
& GetPathToDest
) && *Succ
== Dest
) {
269 if ((Mode
& GetPathToValue
) && (getExecutionCount(*Succ
) != MissingValue
)) {
281 void ProfileInfoT
<Function
,BasicBlock
>::
282 divertFlow(const Edge
&oldedge
, const Edge
&newedge
) {
283 DEBUG(dbgs() << "Diverting " << oldedge
<< " via " << newedge
);
285 // First check if the old edge was taken, if not, just delete it...
286 if (getEdgeWeight(oldedge
) == 0) {
292 P
[newedge
.first
] = 0;
293 P
[newedge
.second
] = newedge
.first
;
294 const BasicBlock
*BB
= GetPath(newedge
.second
,oldedge
.second
,P
,GetPathToExit
| GetPathToDest
);
296 double w
= getEdgeWeight (oldedge
);
297 DEBUG(dbgs() << ", Weight: " << format("%.20g",w
) << "\n");
299 const BasicBlock
*Parent
= P
.find(BB
)->second
;
300 Edge e
= getEdge(Parent
,BB
);
301 double oldw
= getEdgeWeight(e
);
302 double oldc
= getExecutionCount(e
.first
);
303 setEdgeWeight(e
, w
+oldw
);
304 if (Parent
!= oldedge
.first
) {
305 setExecutionCount(e
.first
, w
+oldc
);
308 } while (BB
!= newedge
.first
);
312 /// Replaces all occurrences of RmBB in the ProfilingInfo with DestBB.
313 /// This checks all edges of the function the blocks reside in and replaces the
314 /// occurrences of RmBB with DestBB.
316 void ProfileInfoT
<Function
,BasicBlock
>::
317 replaceAllUses(const BasicBlock
*RmBB
, const BasicBlock
*DestBB
) {
318 DEBUG(dbgs() << "Replacing " << RmBB
->getName()
319 << " with " << DestBB
->getName() << "\n");
320 const Function
*F
= DestBB
->getParent();
321 std::map
<const Function
*, EdgeWeights
>::iterator J
=
322 EdgeInformation
.find(F
);
323 if (J
== EdgeInformation
.end()) return;
326 bool erasededge
= false;
327 EdgeWeights::iterator I
= J
->second
.begin(), E
= J
->second
.end();
330 bool foundedge
= false; bool eraseedge
= false;
331 if (e
.first
== RmBB
) {
332 if (e
.second
== DestBB
) {
335 newedge
= getEdge(DestBB
, e
.second
);
339 if (e
.second
== RmBB
) {
340 if (e
.first
== DestBB
) {
343 newedge
= getEdge(e
.first
, DestBB
);
348 replaceEdge(e
, newedge
);
352 Edge newedge
= getEdge(DestBB
, DestBB
);
353 replaceEdge(e
, newedge
);
362 /// Splits an edge in the ProfileInfo and redirects flow over NewBB.
363 /// Since its possible that there is more than one edge in the CFG from FristBB
364 /// to SecondBB its necessary to redirect the flow proporionally.
366 void ProfileInfoT
<Function
,BasicBlock
>::splitEdge(const BasicBlock
*FirstBB
,
367 const BasicBlock
*SecondBB
,
368 const BasicBlock
*NewBB
,
369 bool MergeIdenticalEdges
) {
370 const Function
*F
= FirstBB
->getParent();
371 std::map
<const Function
*, EdgeWeights
>::iterator J
=
372 EdgeInformation
.find(F
);
373 if (J
== EdgeInformation
.end()) return;
375 // Generate edges and read current weight.
376 Edge e
= getEdge(FirstBB
, SecondBB
);
377 Edge n1
= getEdge(FirstBB
, NewBB
);
378 Edge n2
= getEdge(NewBB
, SecondBB
);
379 EdgeWeights
&ECs
= J
->second
;
383 if (!MergeIdenticalEdges
) {
384 // First count the edges from FristBB to SecondBB, if there is more than
385 // one, only slice out a proporional part for NewBB.
386 for(succ_const_iterator BBI
= succ_begin(FirstBB
), BBE
= succ_end(FirstBB
);
388 if (*BBI
== SecondBB
) succ_count
++;
390 // When the NewBB is completely new, increment the count by one so that
391 // the counts are properly distributed.
392 if (getExecutionCount(NewBB
) == ProfileInfo::MissingValue
) succ_count
++;
394 // When the edges are merged anyway, then redirect all flow.
398 // We know now how many edges there are from FirstBB to SecondBB, reroute a
399 // proportional part of the edge weight over NewBB.
400 double neww
= floor(w
/ succ_count
);
403 BlockInformation
[F
][NewBB
] += neww
;
404 if (succ_count
== 1) {
412 void ProfileInfoT
<Function
,BasicBlock
>::splitBlock(const BasicBlock
*Old
,
413 const BasicBlock
* New
) {
414 const Function
*F
= Old
->getParent();
415 std::map
<const Function
*, EdgeWeights
>::iterator J
=
416 EdgeInformation
.find(F
);
417 if (J
== EdgeInformation
.end()) return;
419 DEBUG(dbgs() << "Splitting " << Old
->getName() << " to " << New
->getName() << "\n");
421 std::set
<Edge
> Edges
;
422 for (EdgeWeights::iterator ewi
= J
->second
.begin(), ewe
= J
->second
.end();
424 Edge old
= ewi
->first
;
425 if (old
.first
== Old
) {
429 for (std::set
<Edge
>::iterator EI
= Edges
.begin(), EE
= Edges
.end();
431 Edge newedge
= getEdge(New
, EI
->second
);
432 replaceEdge(*EI
, newedge
);
435 double w
= getExecutionCount(Old
);
436 setEdgeWeight(getEdge(Old
, New
), w
);
437 setExecutionCount(New
, w
);
441 void ProfileInfoT
<Function
,BasicBlock
>::splitBlock(const BasicBlock
*BB
,
442 const BasicBlock
* NewBB
,
443 BasicBlock
*const *Preds
,
445 const Function
*F
= BB
->getParent();
446 std::map
<const Function
*, EdgeWeights
>::iterator J
=
447 EdgeInformation
.find(F
);
448 if (J
== EdgeInformation
.end()) return;
450 DEBUG(dbgs() << "Splitting " << NumPreds
<< " Edges from " << BB
->getName()
451 << " to " << NewBB
->getName() << "\n");
453 // Collect weight that was redirected over NewBB.
454 double newweight
= 0;
456 std::set
<const BasicBlock
*> ProcessedPreds
;
457 // For all requestes Predecessors.
458 for (unsigned pred
= 0; pred
< NumPreds
; ++pred
) {
459 const BasicBlock
* Pred
= Preds
[pred
];
460 if (ProcessedPreds
.insert(Pred
).second
) {
461 // Create edges and read old weight.
462 Edge oldedge
= getEdge(Pred
, BB
);
463 Edge newedge
= getEdge(Pred
, NewBB
);
465 // Remember how much weight was redirected.
466 newweight
+= getEdgeWeight(oldedge
);
468 replaceEdge(oldedge
,newedge
);
472 Edge newedge
= getEdge(NewBB
,BB
);
473 setEdgeWeight(newedge
, newweight
);
474 setExecutionCount(NewBB
, newweight
);
478 void ProfileInfoT
<Function
,BasicBlock
>::transfer(const Function
*Old
,
479 const Function
*New
) {
480 DEBUG(dbgs() << "Replacing Function " << Old
->getName() << " with "
481 << New
->getName() << "\n");
482 std::map
<const Function
*, EdgeWeights
>::iterator J
=
483 EdgeInformation
.find(Old
);
484 if(J
!= EdgeInformation
.end()) {
485 EdgeInformation
[New
] = J
->second
;
487 EdgeInformation
.erase(Old
);
488 BlockInformation
.erase(Old
);
489 FunctionInformation
.erase(Old
);
492 static double readEdgeOrRemember(ProfileInfo::Edge edge
, double w
,
493 ProfileInfo::Edge
&tocalc
, unsigned &uncalc
) {
494 if (w
== ProfileInfo::MissingValue
) {
504 bool ProfileInfoT
<Function
,BasicBlock
>::
505 CalculateMissingEdge(const BasicBlock
*BB
, Edge
&removed
,
506 bool assumeEmptySelf
) {
508 unsigned uncalculated
= 0;
510 // collect weights of all incoming and outgoing edges, rememer edges that
513 SmallSet
<const BasicBlock
*,8> pred_visited
;
514 const_pred_iterator bbi
= pred_begin(BB
), bbe
= pred_end(BB
);
516 Edge e
= getEdge(0,BB
);
517 incount
+= readEdgeOrRemember(e
, getEdgeWeight(e
) ,edgetocalc
,uncalculated
);
519 for (;bbi
!= bbe
; ++bbi
) {
520 if (pred_visited
.insert(*bbi
)) {
521 Edge e
= getEdge(*bbi
,BB
);
522 incount
+= readEdgeOrRemember(e
, getEdgeWeight(e
) ,edgetocalc
,uncalculated
);
527 SmallSet
<const BasicBlock
*,8> succ_visited
;
528 succ_const_iterator sbbi
= succ_begin(BB
), sbbe
= succ_end(BB
);
530 Edge e
= getEdge(BB
,0);
531 if (getEdgeWeight(e
) == MissingValue
) {
532 double w
= getExecutionCount(BB
);
533 if (w
!= MissingValue
) {
538 outcount
+= readEdgeOrRemember(e
, getEdgeWeight(e
), edgetocalc
, uncalculated
);
540 for (;sbbi
!= sbbe
; ++sbbi
) {
541 if (succ_visited
.insert(*sbbi
)) {
542 Edge e
= getEdge(BB
,*sbbi
);
543 outcount
+= readEdgeOrRemember(e
, getEdgeWeight(e
), edgetocalc
, uncalculated
);
547 // if exactly one edge weight was missing, calculate it and remove it from
549 if (uncalculated
== 0 ) {
552 if (uncalculated
== 1) {
553 if (incount
< outcount
) {
554 EdgeInformation
[BB
->getParent()][edgetocalc
] = outcount
-incount
;
556 EdgeInformation
[BB
->getParent()][edgetocalc
] = incount
-outcount
;
558 DEBUG(dbgs() << "--Calc Edge Counter for " << edgetocalc
<< ": "
559 << format("%.20g", getEdgeWeight(edgetocalc
)) << "\n");
560 removed
= edgetocalc
;
563 if (uncalculated
== 2 && assumeEmptySelf
&& edgetocalc
.first
== edgetocalc
.second
&& incount
== outcount
) {
564 setEdgeWeight(edgetocalc
, incount
* 10);
565 removed
= edgetocalc
;
572 static void readEdge(ProfileInfo
*PI
, ProfileInfo::Edge e
, double &calcw
, std::set
<ProfileInfo::Edge
> &misscount
) {
573 double w
= PI
->getEdgeWeight(e
);
574 if (w
!= ProfileInfo::MissingValue
) {
582 bool ProfileInfoT
<Function
,BasicBlock
>::EstimateMissingEdges(const BasicBlock
*BB
) {
584 std::set
<Edge
> inMissing
;
585 std::set
<const BasicBlock
*> ProcessedPreds
;
586 const_pred_iterator bbi
= pred_begin(BB
), bbe
= pred_end(BB
);
588 readEdge(this,getEdge(0,BB
),inWeight
,inMissing
);
590 for( ; bbi
!= bbe
; ++bbi
) {
591 if (ProcessedPreds
.insert(*bbi
).second
) {
592 readEdge(this,getEdge(*bbi
,BB
),inWeight
,inMissing
);
596 double outWeight
= 0;
597 std::set
<Edge
> outMissing
;
598 std::set
<const BasicBlock
*> ProcessedSuccs
;
599 succ_const_iterator sbbi
= succ_begin(BB
), sbbe
= succ_end(BB
);
601 readEdge(this,getEdge(BB
,0),outWeight
,outMissing
);
602 for ( ; sbbi
!= sbbe
; ++sbbi
) {
603 if (ProcessedSuccs
.insert(*sbbi
).second
) {
604 readEdge(this,getEdge(BB
,*sbbi
),outWeight
,outMissing
);
609 std::set
<Edge
>::iterator ei
,ee
;
610 if (inMissing
.size() == 0 && outMissing
.size() > 0) {
611 ei
= outMissing
.begin();
612 ee
= outMissing
.end();
613 share
= inWeight
/outMissing
.size();
614 setExecutionCount(BB
,inWeight
);
616 if (inMissing
.size() > 0 && outMissing
.size() == 0 && outWeight
== 0) {
617 ei
= inMissing
.begin();
618 ee
= inMissing
.end();
620 setExecutionCount(BB
,0);
622 if (inMissing
.size() == 0 && outMissing
.size() == 0) {
623 setExecutionCount(BB
,outWeight
);
628 for ( ; ei
!= ee
; ++ei
) {
629 setEdgeWeight(*ei
,share
);
635 void ProfileInfoT
<Function
,BasicBlock
>::repair(const Function
*F
) {
636 // if (getExecutionCount(&(F->getEntryBlock())) == 0) {
637 // for (Function::const_iterator FI = F->begin(), FE = F->end();
639 // const BasicBlock* BB = &(*FI);
641 // const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
643 // setEdgeWeight(getEdge(0,BB),0);
645 // for(;NBB != End; ++NBB) {
646 // setEdgeWeight(getEdge(*NBB,BB),0);
650 // succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
652 // setEdgeWeight(getEdge(0,BB),0);
654 // for(;NBB != End; ++NBB) {
655 // setEdgeWeight(getEdge(*NBB,BB),0);
661 // The set of BasicBlocks that are still unvisited.
662 std::set
<const BasicBlock
*> Unvisited
;
664 // The set of return edges (Edges with no successors).
665 std::set
<Edge
> ReturnEdges
;
666 double ReturnWeight
= 0;
668 // First iterate over the whole function and collect:
669 // 1) The blocks in this function in the Unvisited set.
670 // 2) The return edges in the ReturnEdges set.
671 // 3) The flow that is leaving the function already via return edges.
673 // Data structure for searching the function.
674 std::queue
<const BasicBlock
*> BFS
;
675 const BasicBlock
*BB
= &(F
->getEntryBlock());
677 Unvisited
.insert(BB
);
680 BB
= BFS
.front(); BFS
.pop();
681 succ_const_iterator NBB
= succ_begin(BB
), End
= succ_end(BB
);
683 Edge e
= getEdge(BB
,0);
684 double w
= getEdgeWeight(e
);
685 if (w
== MissingValue
) {
686 // If the return edge has no value, try to read value from block.
687 double bw
= getExecutionCount(BB
);
688 if (bw
!= MissingValue
) {
692 // If both return edge and block provide no value, collect edge.
693 ReturnEdges
.insert(e
);
696 // If the return edge has a proper value, collect it.
700 for (;NBB
!= End
; ++NBB
) {
701 if (Unvisited
.insert(*NBB
).second
) {
707 while (Unvisited
.size() > 0) {
708 unsigned oldUnvisitedCount
= Unvisited
.size();
709 bool FoundPath
= false;
711 // If there is only one edge left, calculate it.
712 if (ReturnEdges
.size() == 1) {
713 ReturnWeight
= getExecutionCount(&(F
->getEntryBlock())) - ReturnWeight
;
715 Edge e
= *ReturnEdges
.begin();
716 setEdgeWeight(e
,ReturnWeight
);
717 setExecutionCount(e
.first
,ReturnWeight
);
719 Unvisited
.erase(e
.first
);
720 ReturnEdges
.erase(e
);
724 // Calculate all blocks where only one edge is missing, this may also
725 // resolve furhter return edges.
726 std::set
<const BasicBlock
*>::iterator FI
= Unvisited
.begin(), FE
= Unvisited
.end();
728 const BasicBlock
*BB
= *FI
; ++FI
;
730 if(CalculateMissingEdge(BB
,e
,true)) {
731 if (BlockInformation
[F
].find(BB
) == BlockInformation
[F
].end()) {
732 setExecutionCount(BB
,getExecutionCount(BB
));
735 if (e
.first
!= 0 && e
.second
== 0) {
736 ReturnEdges
.erase(e
);
737 ReturnWeight
+= getEdgeWeight(e
);
741 if (oldUnvisitedCount
> Unvisited
.size()) continue;
743 // Estimate edge weights by dividing the flow proportionally.
744 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
746 const BasicBlock
*BB
= *FI
; ++FI
;
747 const BasicBlock
*Dest
= 0;
748 bool AllEdgesHaveSameReturn
= true;
749 // Check each Successor, these must all end up in the same or an empty
750 // return block otherwise its dangerous to do an estimation on them.
751 for (succ_const_iterator Succ
= succ_begin(BB
), End
= succ_end(BB
);
752 Succ
!= End
; ++Succ
) {
754 GetPath(*Succ
, 0, P
, GetPathToExit
);
755 if (Dest
&& Dest
!= P
[0]) {
756 AllEdgesHaveSameReturn
= false;
760 if (AllEdgesHaveSameReturn
) {
761 if(EstimateMissingEdges(BB
)) {
767 if (oldUnvisitedCount
> Unvisited
.size()) continue;
769 // Check if there is a path to an block that has a known value and redirect
771 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
772 while(FI
!= FE
&& !FoundPath
) {
774 const BasicBlock
*BB
= *FI
; ++FI
;
776 const BasicBlock
*Dest
= GetPath(BB
, 0, P
, GetPathToValue
);
778 // Calculate incoming flow.
779 double iw
= 0; unsigned inmissing
= 0; unsigned incount
= 0; unsigned invalid
= 0;
780 std::set
<const BasicBlock
*> Processed
;
781 for (const_pred_iterator NBB
= pred_begin(BB
), End
= pred_end(BB
);
783 if (Processed
.insert(*NBB
).second
) {
784 Edge e
= getEdge(*NBB
, BB
);
785 double ew
= getEdgeWeight(e
);
786 if (ew
!= MissingValue
) {
790 // If the path contains the successor, this means its a backedge,
791 // do not count as missing.
792 if (P
.find(*NBB
) == P
.end())
798 if (inmissing
== incount
) continue;
799 if (invalid
== 0) continue;
801 // Subtract (already) outgoing flow.
803 for (succ_const_iterator NBB
= succ_begin(BB
), End
= succ_end(BB
);
805 if (Processed
.insert(*NBB
).second
) {
806 Edge e
= getEdge(BB
, *NBB
);
807 double ew
= getEdgeWeight(e
);
808 if (ew
!= MissingValue
) {
813 if (iw
< 0) continue;
815 // Check the receiving end of the path if it can handle the flow.
816 double ow
= getExecutionCount(Dest
);
818 for (succ_const_iterator NBB
= succ_begin(BB
), End
= succ_end(BB
);
820 if (Processed
.insert(*NBB
).second
) {
821 Edge e
= getEdge(BB
, *NBB
);
822 double ew
= getEdgeWeight(e
);
823 if (ew
!= MissingValue
) {
828 if (ow
< 0) continue;
830 // Determine how much flow shall be used.
831 double ew
= getEdgeWeight(getEdge(P
[Dest
],Dest
));
832 if (ew
!= MissingValue
) {
841 if (ew
!= MissingValue
) {
843 Edge e
= getEdge(P
[Dest
],Dest
);
844 if (getEdgeWeight(e
) == MissingValue
) {
849 } while (Dest
!= BB
);
852 if (FoundPath
) continue;
854 // Calculate a block with self loop.
855 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
856 while(FI
!= FE
&& !FoundPath
) {
857 const BasicBlock
*BB
= *FI
; ++FI
;
858 bool SelfEdgeFound
= false;
859 for (succ_const_iterator NBB
= succ_begin(BB
), End
= succ_end(BB
);
862 SelfEdgeFound
= true;
867 Edge e
= getEdge(BB
,BB
);
868 if (getEdgeWeight(e
) == MissingValue
) {
870 std::set
<const BasicBlock
*> Processed
;
871 for (const_pred_iterator NBB
= pred_begin(BB
), End
= pred_end(BB
);
873 if (Processed
.insert(*NBB
).second
) {
874 Edge e
= getEdge(*NBB
, BB
);
875 double ew
= getEdgeWeight(e
);
876 if (ew
!= MissingValue
) {
881 setEdgeWeight(e
,iw
* 10);
886 if (FoundPath
) continue;
888 // Determine backedges, set them to zero.
889 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
890 while(FI
!= FE
&& !FoundPath
) {
891 const BasicBlock
*BB
= *FI
; ++FI
;
892 const BasicBlock
*Dest
= 0;
894 bool BackEdgeFound
= false;
895 for (const_pred_iterator NBB
= pred_begin(BB
), End
= pred_end(BB
);
897 Dest
= GetPath(BB
, *NBB
, P
, GetPathToDest
| GetPathWithNewEdges
);
899 BackEdgeFound
= true;
904 Edge e
= getEdge(Dest
,BB
);
905 double w
= getEdgeWeight(e
);
906 if (w
== MissingValue
) {
911 Edge e
= getEdge(P
[Dest
], Dest
);
912 double w
= getEdgeWeight(e
);
913 if (w
== MissingValue
) {
918 } while (Dest
!= BB
);
921 if (FoundPath
) continue;
923 // Channel flow to return block.
924 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
925 while(FI
!= FE
&& !FoundPath
) {
926 const BasicBlock
*BB
= *FI
; ++FI
;
929 const BasicBlock
*Dest
= GetPath(BB
, 0, P
, GetPathToExit
| GetPathWithNewEdges
);
933 if (getEdgeWeight(getEdge(Dest
,0)) == MissingValue
) {
934 // Calculate incoming flow.
936 std::set
<const BasicBlock
*> Processed
;
937 for (const_pred_iterator NBB
= pred_begin(BB
), End
= pred_end(BB
);
939 if (Processed
.insert(*NBB
).second
) {
940 Edge e
= getEdge(*NBB
, BB
);
941 double ew
= getEdgeWeight(e
);
942 if (ew
!= MissingValue
) {
948 Edge e
= getEdge(P
[Dest
], Dest
);
949 double w
= getEdgeWeight(e
);
950 if (w
== MissingValue
) {
954 assert(0 && "Edge should not have value already!");
957 } while (Dest
!= BB
);
960 if (FoundPath
) continue;
962 // Speculatively set edges to zero.
963 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
964 while(FI
!= FE
&& !FoundPath
) {
965 const BasicBlock
*BB
= *FI
; ++FI
;
967 for (const_pred_iterator NBB
= pred_begin(BB
), End
= pred_end(BB
);
969 Edge e
= getEdge(*NBB
,BB
);
970 double w
= getEdgeWeight(e
);
971 if (w
== MissingValue
) {
978 if (FoundPath
) continue;
981 FI
= Unvisited
.begin(), FE
= Unvisited
.end();
983 const BasicBlock
*BB
= *FI
; ++FI
;
984 dbgs() << BB
->getName();
990 errs() << "ASSERT: could not repair function";
991 assert(0 && "could not repair function");
994 EdgeWeights J
= EdgeInformation
[F
];
995 for (EdgeWeights::iterator EI
= J
.begin(), EE
= J
.end(); EI
!= EE
; ++EI
) {
998 bool SuccFound
= false;
1000 succ_const_iterator NBB
= succ_begin(e
.first
), End
= succ_end(e
.first
);
1002 if (0 == e
.second
) {
1006 for (;NBB
!= End
; ++NBB
) {
1007 if (*NBB
== e
.second
) {
1019 raw_ostream
& operator<<(raw_ostream
&O
, const Function
*F
) {
1020 return O
<< F
->getName();
1023 raw_ostream
& operator<<(raw_ostream
&O
, const MachineFunction
*MF
) {
1024 return O
<< MF
->getFunction()->getName() << "(MF)";
1027 raw_ostream
& operator<<(raw_ostream
&O
, const BasicBlock
*BB
) {
1028 return O
<< BB
->getName();
1031 raw_ostream
& operator<<(raw_ostream
&O
, const MachineBasicBlock
*MBB
) {
1032 return O
<< MBB
->getBasicBlock()->getName() << "(MB)";
1035 raw_ostream
& operator<<(raw_ostream
&O
, std::pair
<const BasicBlock
*, const BasicBlock
*> E
) {
1053 raw_ostream
& operator<<(raw_ostream
&O
, std::pair
<const MachineBasicBlock
*, const MachineBasicBlock
*> E
) {
1073 //===----------------------------------------------------------------------===//
1074 // NoProfile ProfileInfo implementation
1078 struct NoProfileInfo
: public ImmutablePass
, public ProfileInfo
{
1079 static char ID
; // Class identification, replacement for typeinfo
1080 NoProfileInfo() : ImmutablePass(ID
) {
1081 initializeNoProfileInfoPass(*PassRegistry::getPassRegistry());
1084 /// getAdjustedAnalysisPointer - This method is used when a pass implements
1085 /// an analysis interface through multiple inheritance. If needed, it
1086 /// should override this to adjust the this pointer as needed for the
1087 /// specified pass info.
1088 virtual void *getAdjustedAnalysisPointer(AnalysisID PI
) {
1089 if (PI
== &ProfileInfo::ID
)
1090 return (ProfileInfo
*)this;
1094 virtual const char *getPassName() const {
1095 return "NoProfileInfo";
1098 } // End of anonymous namespace
1100 char NoProfileInfo::ID
= 0;
1101 // Register this pass...
1102 INITIALIZE_AG_PASS(NoProfileInfo
, ProfileInfo
, "no-profile",
1103 "No Profile Information", false, true, true)
1105 ImmutablePass
*llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }