1 //===- MaximumSpanningTree.cpp - LLVM Pass to estimate profile info -------===//
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 module privides means for calculating a maximum spanning tree for the
11 // CFG of a function according to a given profile. The tree does not contain
12 // leaf edges, since they are needed for optimal edge profiling.
14 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "maximum-spanning-tree"
16 #include "MaximumSpanningTree.h"
17 #include "llvm/ADT/EquivalenceClasses.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/CFG.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
25 // compare two weighted edges
26 struct VISIBILITY_HIDDEN EdgeWeightCompare {
27 bool operator()(const ProfileInfo::EdgeWeight X,
28 const ProfileInfo::EdgeWeight Y) const {
29 if (X.second > Y.second) return true;
30 if (X.second < Y.second) return false;
32 // It would be enough to just compare the weights of the edges and be
33 // done. With edges of the same weight this may lead to a different MST
34 // each time the MST is created. To have more stable sorting (and thus
35 // more stable MSTs) furhter sort the edges.
36 if (X.first.first != 0 && Y.first.first == 0) return true;
37 if (X.first.first == 0 && Y.first.first != 0) return false;
38 if (X.first.first == 0 && Y.first.first == 0) return false;
40 if (X.first.first->size() > Y.first.first->size()) return true;
41 if (X.first.first->size() < Y.first.first->size()) return false;
43 if (X.first.second != 0 && Y.first.second == 0) return true;
44 if (X.first.second == 0 && Y.first.second != 0) return false;
45 if (X.first.second == 0 && Y.first.second == 0) return false;
47 if (X.first.second->size() > Y.first.second->size()) return true;
48 if (X.first.second->size() < Y.first.second->size()) return false;
55 static void inline printMSTEdge(ProfileInfo::EdgeWeight E,
57 DEBUG(errs() << "--Edge " << E.first
58 <<" (Weight "<< format("%g",E.second) << ") "
62 // MaximumSpanningTree() - Takes a function and returns a spanning tree
63 // according to the currently active profiling information, the leaf edges are
64 // NOT in the MST. MaximumSpanningTree uses the algorithm of Kruskal.
65 MaximumSpanningTree::MaximumSpanningTree(std::vector<ProfileInfo::EdgeWeight>
68 std::sort(EdgeVector.begin(), EdgeVector.end(), EdgeWeightCompare());
70 // Create spanning tree, Forest contains a special data structure
71 // that makes checking if two nodes are already in a common (sub-)tree
73 EquivalenceClasses<const BasicBlock*> Forest;
74 for (std::vector<ProfileInfo::EdgeWeight>::iterator bbi = EdgeVector.begin(),
75 bbe = EdgeVector.end(); bbi != bbe; ++bbi) {
76 Forest.insert(bbi->first.first);
77 Forest.insert(bbi->first.second);
81 // Iterate over the sorted edges, biggest first.
82 for (std::vector<ProfileInfo::EdgeWeight>::iterator bbi = EdgeVector.begin(),
83 bbe = EdgeVector.end(); bbi != bbe; ++bbi) {
84 ProfileInfo::Edge e = (*bbi).first;
86 if (Forest.findLeader(e.first) != Forest.findLeader(e.second)) {
87 Forest.unionSets(e.first, e.second);
88 // So we know now that the edge is not already in a subtree (and not
89 // (0,entry)), so we push the edge to the MST if it has some successors.
91 printMSTEdge(*bbi,"in MST");
93 // This edge is either (0,entry) or (BB,0) or would create a circle in a
95 printMSTEdge(*bbi,"*not* in MST");
99 // Sort the MST edges.
100 std::stable_sort(MST.begin(),MST.end());
103 MaximumSpanningTree::MaxSpanTree::iterator MaximumSpanningTree::begin() {
107 MaximumSpanningTree::MaxSpanTree::iterator MaximumSpanningTree::end() {
111 void MaximumSpanningTree::dump() {
113 for ( MaxSpanTree::iterator ei = MST.begin(), ee = MST.end();
115 errs()<<"("<<((*ei).first?(*ei).first->getNameStr():"0")<<",";
116 errs()<<(*ei).second->getNameStr()<<")";