LLVM  8.0.0svn
CFGMST.h
Go to the documentation of this file.
1 //===-- CFGMST.h - Minimum Spanning Tree for CFG ----------------*- C++ -*-===//
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 file implements a Union-find algorithm to compute Minimum Spanning Tree
11 // for a given CFG.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H
16 #define LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H
17 
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Analysis/CFG.h"
24 #include "llvm/Support/Debug.h"
27 #include <utility>
28 #include <vector>
29 
30 #define DEBUG_TYPE "cfgmst"
31 
32 namespace llvm {
33 
34 /// An union-find based Minimum Spanning Tree for CFG
35 ///
36 /// Implements a Union-find algorithm to compute Minimum Spanning Tree
37 /// for a given CFG.
38 template <class Edge, class BBInfo> class CFGMST {
39 public:
41 
42  // Store all the edges in CFG. It may contain some stale edges
43  // when Removed is set.
44  std::vector<std::unique_ptr<Edge>> AllEdges;
45 
46  // This map records the auxiliary information for each BB.
48 
49  // Whehter the function has an exit block with no successors.
50  // (For function with an infinite loop, this block may be absent)
51  bool ExitBlockFound = false;
52 
53  // Find the root group of the G and compress the path from G to the root.
54  BBInfo *findAndCompressGroup(BBInfo *G) {
55  if (G->Group != G)
56  G->Group = findAndCompressGroup(static_cast<BBInfo *>(G->Group));
57  return static_cast<BBInfo *>(G->Group);
58  }
59 
60  // Union BB1 and BB2 into the same group and return true.
61  // Returns false if BB1 and BB2 are already in the same group.
62  bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2) {
63  BBInfo *BB1G = findAndCompressGroup(&getBBInfo(BB1));
64  BBInfo *BB2G = findAndCompressGroup(&getBBInfo(BB2));
65 
66  if (BB1G == BB2G)
67  return false;
68 
69  // Make the smaller rank tree a direct child or the root of high rank tree.
70  if (BB1G->Rank < BB2G->Rank)
71  BB1G->Group = BB2G;
72  else {
73  BB2G->Group = BB1G;
74  // If the ranks are the same, increment root of one tree by one.
75  if (BB1G->Rank == BB2G->Rank)
76  BB1G->Rank++;
77  }
78  return true;
79  }
80 
81  // Give BB, return the auxiliary information.
82  BBInfo &getBBInfo(const BasicBlock *BB) const {
83  auto It = BBInfos.find(BB);
84  assert(It->second.get() != nullptr);
85  return *It->second.get();
86  }
87 
88  // Give BB, return the auxiliary information if it's available.
89  BBInfo *findBBInfo(const BasicBlock *BB) const {
90  auto It = BBInfos.find(BB);
91  if (It == BBInfos.end())
92  return nullptr;
93  return It->second.get();
94  }
95 
96  // Traverse the CFG using a stack. Find all the edges and assign the weight.
97  // Edges with large weight will be put into MST first so they are less likely
98  // to be instrumented.
99  void buildEdges() {
100  LLVM_DEBUG(dbgs() << "Build Edge on " << F.getName() << "\n");
101 
102  const BasicBlock *Entry = &(F.getEntryBlock());
103  uint64_t EntryWeight = (BFI != nullptr ? BFI->getEntryFreq() : 2);
104  Edge *EntryIncoming = nullptr, *EntryOutgoing = nullptr,
105  *ExitOutgoing = nullptr, *ExitIncoming = nullptr;
106  uint64_t MaxEntryOutWeight = 0, MaxExitOutWeight = 0, MaxExitInWeight = 0;
107 
108  // Add a fake edge to the entry.
109  EntryIncoming = &addEdge(nullptr, Entry, EntryWeight);
110  LLVM_DEBUG(dbgs() << " Edge: from fake node to " << Entry->getName()
111  << " w = " << EntryWeight << "\n");
112 
113  // Special handling for single BB functions.
114  if (succ_empty(Entry)) {
115  addEdge(Entry, nullptr, EntryWeight);
116  return;
117  }
118 
119  static const uint32_t CriticalEdgeMultiplier = 1000;
120 
121  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
122  Instruction *TI = BB->getTerminator();
123  uint64_t BBWeight =
124  (BFI != nullptr ? BFI->getBlockFreq(&*BB).getFrequency() : 2);
125  uint64_t Weight = 2;
126  if (int successors = TI->getNumSuccessors()) {
127  for (int i = 0; i != successors; ++i) {
128  BasicBlock *TargetBB = TI->getSuccessor(i);
129  bool Critical = isCriticalEdge(TI, i);
130  uint64_t scaleFactor = BBWeight;
131  if (Critical) {
132  if (scaleFactor < UINT64_MAX / CriticalEdgeMultiplier)
133  scaleFactor *= CriticalEdgeMultiplier;
134  else
135  scaleFactor = UINT64_MAX;
136  }
137  if (BPI != nullptr)
138  Weight = BPI->getEdgeProbability(&*BB, TargetBB).scale(scaleFactor);
139  auto *E = &addEdge(&*BB, TargetBB, Weight);
140  E->IsCritical = Critical;
141  LLVM_DEBUG(dbgs() << " Edge: from " << BB->getName() << " to "
142  << TargetBB->getName() << " w=" << Weight << "\n");
143 
144  // Keep track of entry/exit edges:
145  if (&*BB == Entry) {
146  if (Weight > MaxEntryOutWeight) {
147  MaxEntryOutWeight = Weight;
148  EntryOutgoing = E;
149  }
150  }
151 
152  auto *TargetTI = TargetBB->getTerminator();
153  if (TargetTI && !TargetTI->getNumSuccessors()) {
154  if (Weight > MaxExitInWeight) {
155  MaxExitInWeight = Weight;
156  ExitIncoming = E;
157  }
158  }
159  }
160  } else {
161  ExitBlockFound = true;
162  Edge *ExitO = &addEdge(&*BB, nullptr, BBWeight);
163  if (BBWeight > MaxExitOutWeight) {
164  MaxExitOutWeight = BBWeight;
165  ExitOutgoing = ExitO;
166  }
167  LLVM_DEBUG(dbgs() << " Edge: from " << BB->getName() << " to fake exit"
168  << " w = " << BBWeight << "\n");
169  }
170  }
171 
172  // Entry/exit edge adjustment heurisitic:
173  // prefer instrumenting entry edge over exit edge
174  // if possible. Those exit edges may never have a chance to be
175  // executed (for instance the program is an event handling loop)
176  // before the profile is asynchronously dumped.
177  //
178  // If EntryIncoming and ExitOutgoing has similar weight, make sure
179  // ExitOutging is selected as the min-edge. Similarly, if EntryOutgoing
180  // and ExitIncoming has similar weight, make sure ExitIncoming becomes
181  // the min-edge.
182  uint64_t EntryInWeight = EntryWeight;
183 
184  if (EntryInWeight >= MaxExitOutWeight &&
185  EntryInWeight * 2 < MaxExitOutWeight * 3) {
186  EntryIncoming->Weight = MaxExitOutWeight;
187  ExitOutgoing->Weight = EntryInWeight + 1;
188  }
189 
190  if (MaxEntryOutWeight >= MaxExitInWeight &&
191  MaxEntryOutWeight * 2 < MaxExitInWeight * 3) {
192  EntryOutgoing->Weight = MaxExitInWeight;
193  ExitIncoming->Weight = MaxEntryOutWeight + 1;
194  }
195  }
196 
197  // Sort CFG edges based on its weight.
199  std::stable_sort(AllEdges.begin(), AllEdges.end(),
200  [](const std::unique_ptr<Edge> &Edge1,
201  const std::unique_ptr<Edge> &Edge2) {
202  return Edge1->Weight > Edge2->Weight;
203  });
204  }
205 
206  // Traverse all the edges and compute the Minimum Weight Spanning Tree
207  // using union-find algorithm.
209  // First, put all the critical edge with landing-pad as the Dest to MST.
210  // This works around the insufficient support of critical edges split
211  // when destination BB is a landing pad.
212  for (auto &Ei : AllEdges) {
213  if (Ei->Removed)
214  continue;
215  if (Ei->IsCritical) {
216  if (Ei->DestBB && Ei->DestBB->isLandingPad()) {
217  if (unionGroups(Ei->SrcBB, Ei->DestBB))
218  Ei->InMST = true;
219  }
220  }
221  }
222 
223  for (auto &Ei : AllEdges) {
224  if (Ei->Removed)
225  continue;
226  // If we detect infinite loops, force
227  // instrumenting the entry edge:
228  if (!ExitBlockFound && Ei->SrcBB == nullptr)
229  continue;
230  if (unionGroups(Ei->SrcBB, Ei->DestBB))
231  Ei->InMST = true;
232  }
233  }
234 
235  // Dump the Debug information about the instrumentation.
236  void dumpEdges(raw_ostream &OS, const Twine &Message) const {
237  if (!Message.str().empty())
238  OS << Message << "\n";
239  OS << " Number of Basic Blocks: " << BBInfos.size() << "\n";
240  for (auto &BI : BBInfos) {
241  const BasicBlock *BB = BI.first;
242  OS << " BB: " << (BB == nullptr ? "FakeNode" : BB->getName()) << " "
243  << BI.second->infoString() << "\n";
244  }
245 
246  OS << " Number of Edges: " << AllEdges.size()
247  << " (*: Instrument, C: CriticalEdge, -: Removed)\n";
248  uint32_t Count = 0;
249  for (auto &EI : AllEdges)
250  OS << " Edge " << Count++ << ": " << getBBInfo(EI->SrcBB).Index << "-->"
251  << getBBInfo(EI->DestBB).Index << EI->infoString() << "\n";
252  }
253 
254  // Add an edge to AllEdges with weight W.
255  Edge &addEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W) {
256  uint32_t Index = BBInfos.size();
257  auto Iter = BBInfos.end();
258  bool Inserted;
259  std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Src, nullptr));
260  if (Inserted) {
261  // Newly inserted, update the real info.
262  Iter->second = std::move(llvm::make_unique<BBInfo>(Index));
263  Index++;
264  }
265  std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Dest, nullptr));
266  if (Inserted)
267  // Newly inserted, update the real info.
268  Iter->second = std::move(llvm::make_unique<BBInfo>(Index));
269  AllEdges.emplace_back(new Edge(Src, Dest, W));
270  return *AllEdges.back();
271  }
272 
275 
276 public:
277  CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
278  BlockFrequencyInfo *BFI_ = nullptr)
279  : F(Func), BPI(BPI_), BFI(BFI_) {
280  buildEdges();
283  }
284 };
285 
286 } // end namespace llvm
287 
288 #undef DEBUG_TYPE // "cfgmst"
289 
290 #endif // LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
iterator end()
Definition: Function.h:658
std::vector< std::unique_ptr< Edge > > AllEdges
Definition: CFGMST.h:44
CFGMST(Function &Func, BranchProbabilityInfo *BPI_=nullptr, BlockFrequencyInfo *BFI_=nullptr)
Definition: CFGMST.h:277
void buildEdges()
Definition: CFGMST.h:99
Function & F
Definition: CFGMST.h:40
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:138
An union-find based Minimum Spanning Tree for CFG.
Definition: CFGMST.h:38
BlockFrequencyInfo * BFI
Definition: CFGMST.h:274
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:196
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2)
Definition: CFGMST.h:62
#define UINT64_MAX
Definition: DataTypes.h:83
iterator begin()
Definition: Function.h:656
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
bool ExitBlockFound
Definition: CFGMST.h:51
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void sortEdgesByWeight()
Definition: CFGMST.h:198
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition: CFG.cpp:88
DenseMap< const BasicBlock *, std::unique_ptr< BBInfo > > BBInfos
Definition: CFGMST.h:47
bool succ_empty(const Instruction *I)
Definition: CFG.h:256
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge&#39;s probability, relative to other out-edges of the Src.
BBInfo * findBBInfo(const BasicBlock *BB) const
Definition: CFGMST.h:89
void computeMinimumSpanningTree()
Definition: CFGMST.h:208
Iterator for intrusive lists based on ilist_node.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
BBInfo * findAndCompressGroup(BBInfo *G)
Definition: CFGMST.h:54
uint64_t scale(uint64_t Num) const
Scale a large integer.
void dumpEdges(raw_ostream &OS, const Twine &Message) const
Definition: CFGMST.h:236
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
BranchProbabilityInfo * BPI
Definition: CFGMST.h:273
BBInfo & getBBInfo(const BasicBlock *BB) const
Definition: CFGMST.h:82
Analysis providing branch probability information.
Edge & addEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W)
Definition: CFGMST.h:255
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:18
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
succ_range successors(Instruction *I)
Definition: CFG.h:262
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
#define LLVM_DEBUG(X)
Definition: Debug.h:123