21
21
#include " llvm/ADT/SmallSet.h"
22
22
#include " llvm/ADT/Statistic.h"
23
23
#include " llvm/Analysis/AliasAnalysis.h"
24
+ #include " llvm/CodeGen/MachineBlockFrequencyInfo.h"
24
25
#include " llvm/CodeGen/MachineDominators.h"
25
26
#include " llvm/CodeGen/MachineLoopInfo.h"
26
27
#include " llvm/CodeGen/MachinePostDominators.h"
@@ -41,6 +42,12 @@ SplitEdges("machine-sink-split",
41
42
cl::desc (" Split critical edges during machine sinking" ),
42
43
cl::init(true ), cl::Hidden);
43
44
45
+ static cl::opt<bool >
46
+ UseBlockFreqInfo (" machine-sink-bfi" ,
47
+ cl::desc (" Use block frequency info to find successors to sink" ),
48
+ cl::init(true ), cl::Hidden);
49
+
50
+
44
51
STATISTIC (NumSunk, " Number of machine instructions sunk" );
45
52
STATISTIC (NumSplit, " Number of critical edges split" );
46
53
STATISTIC (NumCoalesces, " Number of copies coalesced" );
@@ -53,6 +60,7 @@ namespace {
53
60
MachineDominatorTree *DT; // Machine dominator tree
54
61
MachinePostDominatorTree *PDT; // Machine post dominator tree
55
62
MachineLoopInfo *LI;
63
+ const MachineBlockFrequencyInfo *MBFI;
56
64
AliasAnalysis *AA;
57
65
58
66
// Remember which edges have been considered for breaking.
@@ -81,6 +89,8 @@ namespace {
81
89
AU.addPreserved <MachineDominatorTree>();
82
90
AU.addPreserved <MachinePostDominatorTree>();
83
91
AU.addPreserved <MachineLoopInfo>();
92
+ if (UseBlockFreqInfo)
93
+ AU.addRequired <MachineBlockFrequencyInfo>();
84
94
}
85
95
86
96
void releaseMemory () override {
@@ -247,6 +257,7 @@ bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
247
257
DT = &getAnalysis<MachineDominatorTree>();
248
258
PDT = &getAnalysis<MachinePostDominatorTree>();
249
259
LI = &getAnalysis<MachineLoopInfo>();
260
+ MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr ;
250
261
AA = &getAnalysis<AliasAnalysis>();
251
262
252
263
bool EverMadeChange = false ;
@@ -566,14 +577,20 @@ MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI,
566
577
}
567
578
568
579
// Otherwise, we should look at all the successors and decide which one
569
- // we should sink to.
570
- // We give successors with smaller loop depth higher priority.
571
- SmallVector<MachineBasicBlock*, 4 > Succs (MBB->succ_begin (), MBB->succ_end ());
572
- // Sort Successors according to their loop depth.
580
+ // we should sink to. If we have reliable block frequency information
581
+ // (frequency != 0) available, give successors with smaller frequencies
582
+ // higher priority, otherwise prioritize smaller loop depths.
583
+ SmallVector<MachineBasicBlock*, 4 > Succs (MBB->succ_begin (),
584
+ MBB->succ_end ());
585
+ // Sort Successors according to their loop depth or block frequency info.
573
586
std::stable_sort (
574
587
Succs.begin (), Succs.end (),
575
- [this ](const MachineBasicBlock *LHS, const MachineBasicBlock *RHS) {
576
- return LI->getLoopDepth (LHS) < LI->getLoopDepth (RHS);
588
+ [this ](const MachineBasicBlock *L, const MachineBasicBlock *R) {
589
+ uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq (L).getFrequency () : 0 ;
590
+ uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq (R).getFrequency () : 0 ;
591
+ bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0 ;
592
+ return HasBlockFreq ? LHSFreq < RHSFreq
593
+ : LI->getLoopDepth (L) < LI->getLoopDepth (R);
577
594
});
578
595
for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin (),
579
596
E = Succs.end (); SI != E; ++SI) {
0 commit comments