1 //===-- CFGMST.h - Minimum Spanning Tree for CFG ----------------*- C++ -*-===//
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 a Union-find algorithm to compute Minimum Spanning Tree
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H
16 #define LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Analysis/BlockFrequencyInfo.h"
21 #include "llvm/Analysis/BranchProbabilityInfo.h"
22 #include "llvm/Analysis/CFG.h"
23 #include "llvm/Support/BranchProbability.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
30 #define DEBUG_TYPE "cfgmst"
34 /// An union-find based Minimum Spanning Tree for CFG
36 /// Implements a Union-find algorithm to compute Minimum Spanning Tree
38 template <class Edge, class BBInfo> class CFGMST {
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;
46 // This map records the auxiliary information for each BB.
47 DenseMap<const BasicBlock *, std::unique_ptr<BBInfo>> BBInfos;
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;
53 // Find the root group of the G and compress the path from G to the root.
54 BBInfo *findAndCompressGroup(BBInfo *G) {
56 G->Group = findAndCompressGroup(static_cast<BBInfo *>(G->Group));
57 return static_cast<BBInfo *>(G->Group);
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));
69 // Make the smaller rank tree a direct child or the root of high rank tree.
70 if (BB1G->Rank < BB2G->Rank)
74 // If the ranks are the same, increment root of one tree by one.
75 if (BB1G->Rank == BB2G->Rank)
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();
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())
93 return It->second.get();
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.
100 LLVM_DEBUG(dbgs() << "Build Edge on " << F.getName() << "\n");
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;
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");
113 // Special handling for single BB functions.
114 if (succ_empty(Entry)) {
115 addEdge(Entry, nullptr, EntryWeight);
119 static const uint32_t CriticalEdgeMultiplier = 1000;
121 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
122 TerminatorInst *TI = BB->getTerminator();
124 (BFI != nullptr ? BFI->getBlockFreq(&*BB).getFrequency() : 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;
132 if (scaleFactor < UINT64_MAX / CriticalEdgeMultiplier)
133 scaleFactor *= CriticalEdgeMultiplier;
135 scaleFactor = UINT64_MAX;
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");
144 // Keep track of entry/exit edges:
146 if (Weight > MaxEntryOutWeight) {
147 MaxEntryOutWeight = Weight;
152 auto *TargetTI = TargetBB->getTerminator();
153 if (TargetTI && !TargetTI->getNumSuccessors()) {
154 if (Weight > MaxExitInWeight) {
155 MaxExitInWeight = Weight;
161 ExitBlockFound = true;
162 Edge *ExitO = &addEdge(&*BB, nullptr, BBWeight);
163 if (BBWeight > MaxExitOutWeight) {
164 MaxExitOutWeight = BBWeight;
165 ExitOutgoing = ExitO;
167 LLVM_DEBUG(dbgs() << " Edge: from " << BB->getName() << " to fake exit"
168 << " w = " << BBWeight << "\n");
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.
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
182 uint64_t EntryInWeight = EntryWeight;
184 if (EntryInWeight >= MaxExitOutWeight &&
185 EntryInWeight * 2 < MaxExitOutWeight * 3) {
186 EntryIncoming->Weight = MaxExitOutWeight;
187 ExitOutgoing->Weight = EntryInWeight + 1;
190 if (MaxEntryOutWeight >= MaxExitInWeight &&
191 MaxEntryOutWeight * 2 < MaxExitInWeight * 3) {
192 EntryOutgoing->Weight = MaxExitInWeight;
193 ExitIncoming->Weight = MaxEntryOutWeight + 1;
197 // Sort CFG edges based on its weight.
198 void sortEdgesByWeight() {
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;
206 // Traverse all the edges and compute the Minimum Weight Spanning Tree
207 // using union-find algorithm.
208 void computeMinimumSpanningTree() {
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) {
215 if (Ei->IsCritical) {
216 if (Ei->DestBB && Ei->DestBB->isLandingPad()) {
217 if (unionGroups(Ei->SrcBB, Ei->DestBB))
223 for (auto &Ei : AllEdges) {
226 // If we detect infinite loops, force
227 // instrumenting the entry edge:
228 if (!ExitBlockFound && Ei->SrcBB == nullptr)
230 if (unionGroups(Ei->SrcBB, Ei->DestBB))
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";
246 OS << " Number of Edges: " << AllEdges.size()
247 << " (*: Instrument, C: CriticalEdge, -: Removed)\n";
249 for (auto &EI : AllEdges)
250 OS << " Edge " << Count++ << ": " << getBBInfo(EI->SrcBB).Index << "-->"
251 << getBBInfo(EI->DestBB).Index << EI->infoString() << "\n";
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();
259 std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Src, nullptr));
261 // Newly inserted, update the real info.
262 Iter->second = std::move(llvm::make_unique<BBInfo>(Index));
265 std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Dest, nullptr));
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();
273 BranchProbabilityInfo *BPI;
274 BlockFrequencyInfo *BFI;
277 CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,
278 BlockFrequencyInfo *BFI_ = nullptr)
279 : F(Func), BPI(BPI_), BFI(BFI_) {
282 computeMinimumSpanningTree();
286 } // end namespace llvm
288 #undef DEBUG_TYPE // "cfgmst"
290 #endif // LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H