Index: lib/CodeGen/MachineSink.cpp =================================================================== --- lib/CodeGen/MachineSink.cpp +++ lib/CodeGen/MachineSink.cpp @@ -21,6 +21,7 @@ #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/CodeGen/MachineBlockFrequencyInfo.h" #include "llvm/CodeGen/MachineDominators.h" #include "llvm/CodeGen/MachineLoopInfo.h" #include "llvm/CodeGen/MachinePostDominators.h" @@ -53,6 +54,7 @@ MachineDominatorTree *DT; // Machine dominator tree MachinePostDominatorTree *PDT; // Machine post dominator tree MachineLoopInfo *LI; + const MachineBlockFrequencyInfo *MBFI; AliasAnalysis *AA; // Remember which edges have been considered for breaking. @@ -81,6 +83,7 @@ AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); + AU.addRequired(); } void releaseMemory() override { @@ -247,6 +250,7 @@ DT = &getAnalysis(); PDT = &getAnalysis(); LI = &getAnalysis(); + MBFI = &getAnalysis(); AA = &getAnalysis(); bool EverMadeChange = false; @@ -566,14 +570,19 @@ } // Otherwise, we should look at all the successors and decide which one - // we should sink to. - // We give successors with smaller loop depth higher priority. + // we should sink to. If we have reliable block frequency information + // (frequency != 0) available, give successors with smaller frequencies + // higher priority, otherwise prioritize smaller loop depths. SmallVector Succs(MBB->succ_begin(), MBB->succ_end()); - // Sort Successors according to their loop depth. + // Sort Successors according to their loop depth or block frequency info. std::stable_sort( Succs.begin(), Succs.end(), [this](const MachineBasicBlock *LHS, const MachineBasicBlock *RHS) { - return LI->getLoopDepth(LHS) < LI->getLoopDepth(RHS); + uint64_t LHSFreq = MBFI->getBlockFreq(LHS).getFrequency(); + uint64_t RHSFreq = MBFI->getBlockFreq(RHS).getFrequency(); + bool UseBlockFreq = LHSFreq != 0 && RHSFreq != 0; + return UseBlockFreq ? LHSFreq < RHSFreq : + LI->getLoopDepth(LHS) < LI->getLoopDepth(RHS); }); for (SmallVectorImpl::iterator SI = Succs.begin(), E = Succs.end(); SI != E; ++SI) {