Index: lib/CodeGen/MachineBlockPlacement.cpp =================================================================== --- lib/CodeGen/MachineBlockPlacement.cpp +++ lib/CodeGen/MachineBlockPlacement.cpp @@ -117,6 +117,7 @@ cl::init(1), cl::Hidden); extern cl::opt StaticLikelyProb; +extern cl::opt ProfileLikelyProb; namespace { class BlockChain; @@ -407,7 +408,10 @@ MachineBlockPlacement::selectBestSuccessor(MachineBasicBlock *BB, BlockChain &Chain, const BlockFilterSet *BlockFilter) { - const BranchProbability HotProb(StaticLikelyProb, 100); + const BranchProbability HotProb( + BB->getParent()->getFunction()->getEntryCount() ? ProfileLikelyProb + : StaticLikelyProb, + 100); MachineBasicBlock *BestSucc = nullptr; auto BestProb = BranchProbability::getZero(); @@ -454,6 +458,28 @@ BranchProbability SuccProb; uint32_t SuccProbN = MBPI->getEdgeProbability(BB, Succ).getNumerator(); uint32_t SuccProbD = AdjustedSumProb.getNumerator(); + + // When evaluating cost for the following CFG: + // A + // | \ + // | B + // | / + // C + // Assume that the only cost is the taken jump instruction + // The cost of layout ABC = P(AC) + // The cost of layout ACB = P(AB) * 2 + // We compare P(AB)*2 with P(AC) to find the best successor of A. + if (Successors.size() == 2) { + MachineBasicBlock *OtherSucc = NULL; + for (MachineBasicBlock *S : Successors) + if (S != Succ) { + OtherSucc = S; + break; + } + if (Succ->succ_size() == 1 && *Succ->succ_begin() == OtherSucc) + SuccProbN *= 2; + } + if (SuccProbN >= SuccProbD) SuccProb = BranchProbability::getOne(); else Index: lib/CodeGen/MachineBranchProbabilityInfo.cpp =================================================================== --- lib/CodeGen/MachineBranchProbabilityInfo.cpp +++ lib/CodeGen/MachineBranchProbabilityInfo.cpp @@ -29,6 +29,12 @@ cl::desc("branch probability threshold to be considered very likely"), cl::init(80), cl::Hidden); +cl::opt ProfileLikelyProb( + "profile-likely-prob", + cl::desc("branch probability threshold to be considered very likely " + "when profile is available"), + cl::init(51), cl::Hidden); + char MachineBranchProbabilityInfo::ID = 0; void MachineBranchProbabilityInfo::anchor() {}