@@ -3566,10 +3566,10 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
35663566 if (EnableExtTspBlockPlacement &&
35673567 (ApplyExtTspWithoutProfile || MF.getFunction ().hasProfileData ()) &&
35683568 MF.size () <= ExtTspBlockPlacementMaxBlocks) {
3569- applyExtTsp (false );
3569+ applyExtTsp (/* OptForSize= */ false );
35703570 createCFGChainExtTsp ();
35713571 } else if (UseExtTspForSize) {
3572- applyExtTsp (true );
3572+ applyExtTsp (/* OptForSize= */ true );
35733573 createCFGChainExtTsp ();
35743574 }
35753575 }
@@ -3607,9 +3607,9 @@ void MachineBlockPlacement::applyExtTsp(bool OptForSize) {
36073607 CurrentBlockOrder.push_back (&MBB);
36083608 }
36093609
3610- std::vector <uint64_t > BlockCounts (F->size ());
3611- std::vector <uint64_t > BlockSizes (F->size ());
3612- std::vector <codelayout::EdgeCount> JumpCounts;
3610+ SmallVector <uint64_t , 0 > BlockCounts (F->size ());
3611+ SmallVector <uint64_t , 0 > BlockSizes (F->size ());
3612+ SmallVector <codelayout::EdgeCount, 0 > JumpCounts;
36133613 SmallVector<MachineOperand, 4 > Cond; // For analyzeBranch.
36143614 SmallVector<const MachineBasicBlock *, 4 > Succs;
36153615 for (MachineBasicBlock &MBB : *F) {
@@ -3626,23 +3626,18 @@ void MachineBlockPlacement::applyExtTsp(bool OptForSize) {
36263626 instructionsWithoutDebug (MBB.instr_begin (), MBB.instr_end ());
36273627 size_t NumInsts = std::distance (NonDbgInsts.begin (), NonDbgInsts.end ());
36283628 BlockSizes[BlockIndex[&MBB]] = 4 * NumInsts;
3629- // Getting jump frequencies.
36303629
3631- if (!OptForSize) {
3632- for (MachineBasicBlock *Succ : MBB.successors ()) {
3633- auto EP = MBPI->getEdgeProbability (&MBB, Succ);
3634- BlockFrequency JumpFreq = BlockFreq * EP;
3635- JumpCounts.push_back (
3636- {BlockIndex[&MBB], BlockIndex[Succ], JumpFreq.getFrequency ()});
3637- }
3638- } else {
3630+ // Getting jump frequencies.
3631+ if (OptForSize) {
36393632 Cond.clear ();
36403633 MachineBasicBlock *TBB = nullptr , *FBB = nullptr ; // For analyzeBranch.
36413634 if (TII->analyzeBranch (MBB, TBB, FBB, Cond))
36423635 continue ;
36433636
36443637 const MachineBasicBlock *FTB = MBB.getFallThrough ();
3645-
3638+ // Succs is a collection of distinct destinations of the block reachable
3639+ // from MBB via a jump instruction; initialize the list using the three
3640+ // (non-necessarily distinct) blocks, FTB, TBB, and FBB.
36463641 Succs.clear ();
36473642 if (TBB && TBB != FTB)
36483643 Succs.push_back (TBB);
@@ -3654,17 +3649,23 @@ void MachineBlockPlacement::applyExtTsp(bool OptForSize) {
36543649 // optimization; prioritize slightly jumps with a single successor, since
36553650 // the corresponding jump instruction will be removed from the binary.
36563651 const uint64_t Freq = Succs.size () == 1 ? 110 : 100 ;
3657- for (const MachineBasicBlock *Succ : Succs) {
3652+ for (const MachineBasicBlock *Succ : Succs)
36583653 JumpCounts.push_back ({BlockIndex[&MBB], BlockIndex[Succ], Freq});
3654+ } else {
3655+ for (MachineBasicBlock *Succ : MBB.successors ()) {
3656+ auto EP = MBPI->getEdgeProbability (&MBB, Succ);
3657+ BlockFrequency JumpFreq = BlockFreq * EP;
3658+ JumpCounts.push_back (
3659+ {BlockIndex[&MBB], BlockIndex[Succ], JumpFreq.getFrequency ()});
36593660 }
36603661 }
36613662 }
36623663
36633664 LLVM_DEBUG (dbgs () << " Applying ext-tsp layout for |V| = " << F->size ()
36643665 << " with profile = " << F->getFunction ().hasProfileData ()
3665- << " (" << F->getName (). str () << " )" << " \n " );
3666+ << " (" << F->getName () << " )" << " \n " );
36663667
3667- const double OrgScore = calcExtTspScore (BlockSizes, BlockCounts, JumpCounts);
3668+ const double OrgScore = calcExtTspScore (BlockSizes, JumpCounts);
36683669 LLVM_DEBUG (dbgs () << format (" original layout score: %0.2f\n " , OrgScore));
36693670
36703671 // Run the layout algorithm.
@@ -3674,8 +3675,7 @@ void MachineBlockPlacement::applyExtTsp(bool OptForSize) {
36743675 for (uint64_t Node : NewOrder) {
36753676 NewBlockOrder.push_back (CurrentBlockOrder[Node]);
36763677 }
3677- const double OptScore =
3678- calcExtTspScore (NewOrder, BlockSizes, BlockCounts, JumpCounts);
3678+ const double OptScore = calcExtTspScore (NewOrder, BlockSizes, JumpCounts);
36793679 LLVM_DEBUG (dbgs () << format (" optimized layout score: %0.2f\n " , OptScore));
36803680
36813681 // If the optimization is unsuccessful, fall back to the original block order.
0 commit comments