llvm.org GIT mirror llvm / e773e8c
Add a somewhat hacky heuristic to do something different from whole-loop rotation. When there is a loop backedge which is an unconditional branch, we will end up with a branch somewhere no matter what. Try placing this backedge in a fallthrough position above the loop header as that will definitely remove at least one branch from the loop iteration, where whole loop rotation may not. I haven't seen any benchmarks where this is important but loop-blocks.ll tests for it, and so this will be covered when I flip the default. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154812 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
2 changed file(s) with 83 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
210210 void buildChain(MachineBasicBlock *BB, BlockChain &Chain,
211211 SmallVectorImpl &BlockWorkList,
212212 const BlockFilterSet *BlockFilter = 0);
213 MachineBasicBlock *findBestLoopTop(MachineLoop &L,
214 const BlockFilterSet &LoopBlockSet);
213215 MachineBasicBlock *findBestLoopExit(MachineFunction &F,
214216 MachineLoop &L,
215217 const BlockFilterSet &LoopBlockSet);
540542
541543 /// \brief Find the best loop top block for layout.
542544 ///
545 /// Look for a block which is strictly better than the loop header for laying
546 /// out at the top of the loop. This looks for one and only one pattern:
547 /// a latch block with no conditional exit. This block will cause a conditional
548 /// jump around it or will be the bottom of the loop if we lay it out in place,
549 /// but if it it doesn't end up at the bottom of the loop for any reason,
550 /// rotation alone won't fix it. Because such a block will always result in an
551 /// unconditional jump (for the backedge) rotating it in front of the loop
552 /// header is always profitable.
553 MachineBasicBlock *
554 MachineBlockPlacement::findBestLoopTop(MachineLoop &L,
555 const BlockFilterSet &LoopBlockSet) {
556 // Check that the header hasn't been fused with a preheader block due to
557 // crazy branches. If it has, we need to start with the header at the top to
558 // prevent pulling the preheader into the loop body.
559 BlockChain &HeaderChain = *BlockToChain[L.getHeader()];
560 if (!LoopBlockSet.count(*HeaderChain.begin()))
561 return L.getHeader();
562
563 DEBUG(dbgs() << "Finding best loop top for: "
564 << getBlockName(L.getHeader()) << "\n");
565
566 BlockFrequency BestPredFreq;
567 MachineBasicBlock *BestPred = 0;
568 for (MachineBasicBlock::pred_iterator PI = L.getHeader()->pred_begin(),
569 PE = L.getHeader()->pred_end();
570 PI != PE; ++PI) {
571 MachineBasicBlock *Pred = *PI;
572 if (!LoopBlockSet.count(Pred))
573 continue;
574 DEBUG(dbgs() << " header pred: " << getBlockName(Pred) << ", "
575 << Pred->succ_size() << " successors, "
576 << MBFI->getBlockFreq(Pred) << " freq\n");
577 if (Pred->succ_size() > 1)
578 continue;
579
580 BlockFrequency PredFreq = MBFI->getBlockFreq(Pred);
581 if (!BestPred || PredFreq > BestPredFreq ||
582 (!(PredFreq < BestPredFreq) &&
583 Pred->isLayoutSuccessor(L.getHeader()))) {
584 BestPred = Pred;
585 BestPredFreq = PredFreq;
586 }
587 }
588
589 // If no direct predecessor is fine, just use the loop header.
590 if (!BestPred)
591 return L.getHeader();
592
593 // Walk backwards through any straight line of predecessors.
594 while (BestPred->pred_size() == 1 &&
595 (*BestPred->pred_begin())->succ_size() == 1 &&
596 *BestPred->pred_begin() != L.getHeader())
597 BestPred = *BestPred->pred_begin();
598
599 DEBUG(dbgs() << " final top: " << getBlockName(BestPred) << "\n");
600 return BestPred;
601 }
602
603
604 /// \brief Find the best loop exiting block for layout.
605 ///
543606 /// This routine implements the logic to analyze the loop looking for the best
544607 /// block to layout at the top of the loop. Typically this is done to maximize
545608 /// fallthrough opportunities.
724787 SmallVector BlockWorkList;
725788 BlockFilterSet LoopBlockSet(L.block_begin(), L.block_end());
726789
727 MachineBasicBlock *ExitingBB = findBestLoopExit(F, L, LoopBlockSet);
728 BlockChain &LoopChain = *BlockToChain[L.getHeader()];
790 // First check to see if there is an obviously preferable top block for the
791 // loop. This will default to the header, but may end up as one of the
792 // predecessors to the header if there is one which will result in strictly
793 // fewer branches in the loop body.
794 MachineBasicBlock *LoopTop = findBestLoopTop(L, LoopBlockSet);
795
796 // If we selected just the header for the loop top, look for a potentially
797 // profitable exit block in the event that rotating the loop can eliminate
798 // branches by placing an exit edge at the bottom.
799 MachineBasicBlock *ExitingBB = 0;
800 if (LoopTop == L.getHeader())
801 ExitingBB = findBestLoopExit(F, L, LoopBlockSet);
802
803 BlockChain &LoopChain = *BlockToChain[LoopTop];
729804
730805 // FIXME: This is a really lame way of walking the chains in the loop: we
731806 // walk the blocks, and use a set to prevent visiting a particular chain
757832 BlockWorkList.push_back(*Chain.begin());
758833 }
759834
760 buildChain(L.getHeader(), LoopChain, BlockWorkList, &LoopBlockSet);
835 buildChain(LoopTop, LoopChain, BlockWorkList, &LoopBlockSet);
761836 rotateLoop(LoopChain, ExitingBB, LoopBlockSet);
762837
763838 DEBUG({
973973 ; CHECK: %entry
974974 ; First rotated loop top.
975975 ; CHECK: .align
976 ; CHECK: %while.end
977 ; CHECK: %for.cond
978 ; CHECK: %if.then
979 ; CHECK: %if.else
976980 ; CHECK: %if.end10
977981 ; Second rotated loop top
978982 ; CHECK: .align
983 ; CHECK: %if.then24
979984 ; CHECK: %while.cond.outer
980985 ; Third rotated loop top
981986 ; CHECK: .align
984989 ; CHECK: %land.lhs.true
985990 ; CHECK: %if.then19
986991 ; CHECK: %if.then19
987 ; CHECK: %if.then24
988 ; CHECK: %while.end
989 ; CHECK: %for.cond
990 ; CHECK: %if.then
991 ; CHECK: %if.else
992992 ; CHECK: %if.then8
993993 ; CHECK: ret
994994