Include optional subclass flags, such as inbounds, nsw, etc., in the
[oota-llvm.git] / lib / Transforms / Instrumentation / MaximumSpanningTree.cpp
1 //===- MaximumSpanningTree.cpp - LLVM Pass to estimate profile info -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
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"
22 using namespace llvm;
23
24 namespace {
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;
31
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;
39
40       if (X.first.first->size() > Y.first.first->size()) return true;
41       if (X.first.first->size() < Y.first.first->size()) return false;
42
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;
46
47       if (X.first.second->size() > Y.first.second->size()) return true;
48       if (X.first.second->size() < Y.first.second->size()) return false;
49
50       return false;
51     }
52   };
53 }
54
55 static void inline printMSTEdge(ProfileInfo::EdgeWeight E, 
56                                 const char *M) {
57   DEBUG(errs() << "--Edge " << E.first
58                <<" (Weight "<< format("%g",E.second) << ") "
59                << (M) << "\n");
60 }
61
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>
66                                          &EdgeVector) {
67
68   std::sort(EdgeVector.begin(), EdgeVector.end(), EdgeWeightCompare());
69
70   // Create spanning tree, Forest contains a special data structure
71   // that makes checking if two nodes are already in a common (sub-)tree
72   // fast and cheap.
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);
78   }
79   Forest.insert(0);
80
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;
85
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.
90       MST.push_back(e);
91       printMSTEdge(*bbi,"in MST");
92     } else {
93       // This edge is either (0,entry) or (BB,0) or would create a circle in a
94       // subtree.
95       printMSTEdge(*bbi,"*not* in MST");
96     }
97   }
98
99   // Sort the MST edges.
100   std::stable_sort(MST.begin(),MST.end());
101 }
102
103 MaximumSpanningTree::MaxSpanTree::iterator MaximumSpanningTree::begin() {
104   return MST.begin();
105 }
106
107 MaximumSpanningTree::MaxSpanTree::iterator MaximumSpanningTree::end() {
108   return MST.end();
109 }
110
111 void MaximumSpanningTree::dump() {
112   errs()<<"{";
113   for ( MaxSpanTree::iterator ei = MST.begin(), ee = MST.end();
114         ei!=ee; ++ei ) {
115     errs()<<"("<<((*ei).first?(*ei).first->getNameStr():"0")<<",";
116     errs()<<(*ei).second->getNameStr()<<")";
117   }
118   errs()<<"}\n";
119 }