|
19 | 19 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
20 | 20 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
21 | 21 | #include "llvm/Analysis/CFG.h" |
| 22 | +#include "llvm/Analysis/LoopInfo.h" |
22 | 23 | #include "llvm/IR/Instructions.h" |
23 | 24 | #include "llvm/IR/IntrinsicInst.h" |
24 | 25 | #include "llvm/Support/BranchProbability.h" |
@@ -52,10 +53,14 @@ template <class Edge, class BBInfo> class CFGMST { |
52 | 53 |
|
53 | 54 | BranchProbabilityInfo *const BPI; |
54 | 55 | BlockFrequencyInfo *const BFI; |
| 56 | + LoopInfo *const LI; |
55 | 57 |
|
56 | 58 | // If function entry will be always instrumented. |
57 | 59 | const bool InstrumentFuncEntry; |
58 | 60 |
|
| 61 | + // If true loop entries will be always instrumented. |
| 62 | + const bool InstrumentLoopEntries; |
| 63 | + |
59 | 64 | // Find the root group of the G and compress the path from G to the root. |
60 | 65 | BBInfo *findAndCompressGroup(BBInfo *G) { |
61 | 66 | if (G->Group != G) |
@@ -154,6 +159,11 @@ template <class Edge, class BBInfo> class CFGMST { |
154 | 159 | } |
155 | 160 | if (BPI != nullptr) |
156 | 161 | Weight = BPI->getEdgeProbability(&BB, TargetBB).scale(scaleFactor); |
| 162 | + // If InstrumentLoopEntries is on and TargetBB is a loop head (i.e., |
| 163 | + // the current edge leads to a loop), set Weight to be minimal, so |
| 164 | + // that the edge won't be chosen for the MST and will be instrumented. |
| 165 | + if (InstrumentLoopEntries && LI->isLoopHeader(TargetBB)) |
| 166 | + Weight = 0; |
157 | 167 | if (Weight == 0) |
158 | 168 | Weight++; |
159 | 169 | auto *E = &addEdge(&BB, TargetBB, Weight); |
@@ -291,10 +301,14 @@ template <class Edge, class BBInfo> class CFGMST { |
291 | 301 | return *AllEdges.back(); |
292 | 302 | } |
293 | 303 |
|
294 | | - CFGMST(Function &Func, bool InstrumentFuncEntry, |
| 304 | + CFGMST(Function &Func, bool InstrumentFuncEntry, bool InstrumentLoopEntries, |
295 | 305 | BranchProbabilityInfo *BPI = nullptr, |
296 | | - BlockFrequencyInfo *BFI = nullptr) |
297 | | - : F(Func), BPI(BPI), BFI(BFI), InstrumentFuncEntry(InstrumentFuncEntry) { |
| 306 | + BlockFrequencyInfo *BFI = nullptr, LoopInfo *LI = nullptr) |
| 307 | + : F(Func), BPI(BPI), BFI(BFI), LI(LI), |
| 308 | + InstrumentFuncEntry(InstrumentFuncEntry), |
| 309 | + InstrumentLoopEntries(InstrumentLoopEntries) { |
| 310 | + assert(!(InstrumentLoopEntries && !LI) && |
| 311 | + "expected a LoopInfo to instrumenting loop entries"); |
298 | 312 | buildEdges(); |
299 | 313 | sortEdgesByWeight(); |
300 | 314 | computeMinimumSpanningTree(); |
|
0 commit comments