comparison lib/CodeGen/MachineBlockPlacement.cpp @ 77:54457678186b

LLVM 3.6
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 08 Sep 2014 22:06:00 +0900
parents e4204d083e25
children 60c9769439b8
comparison
equal deleted inserted replaced
34:e874dbf0ad9d 77:54457678186b
23 // first time it reaches a chain of basic blocks, it schedules them in the 23 // first time it reaches a chain of basic blocks, it schedules them in the
24 // function in-order. 24 // function in-order.
25 // 25 //
26 //===----------------------------------------------------------------------===// 26 //===----------------------------------------------------------------------===//
27 27
28 #define DEBUG_TYPE "block-placement2"
29 #include "llvm/CodeGen/Passes.h" 28 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/ADT/DenseMap.h" 29 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/SmallPtrSet.h" 30 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/Statistic.h" 32 #include "llvm/ADT/Statistic.h"
41 #include "llvm/Support/Allocator.h" 40 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/CommandLine.h" 41 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/Debug.h"
44 #include "llvm/Target/TargetInstrInfo.h" 43 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h" 44 #include "llvm/Target/TargetLowering.h"
45 #include "llvm/Target/TargetSubtargetInfo.h"
46 #include <algorithm> 46 #include <algorithm>
47 using namespace llvm; 47 using namespace llvm;
48
49 #define DEBUG_TYPE "block-placement2"
48 50
49 STATISTIC(NumCondBranches, "Number of conditional branches"); 51 STATISTIC(NumCondBranches, "Number of conditional branches");
50 STATISTIC(NumUncondBranches, "Number of uncondittional branches"); 52 STATISTIC(NumUncondBranches, "Number of uncondittional branches");
51 STATISTIC(CondBranchTakenFreq, 53 STATISTIC(CondBranchTakenFreq,
52 "Potential frequency of taking conditional branches"); 54 "Potential frequency of taking conditional branches");
150 } 152 }
151 } 153 }
152 154
153 #ifndef NDEBUG 155 #ifndef NDEBUG
154 /// \brief Dump the blocks in this chain. 156 /// \brief Dump the blocks in this chain.
155 void dump() LLVM_ATTRIBUTE_USED { 157 LLVM_DUMP_METHOD void dump() {
156 for (iterator I = begin(), E = end(); I != E; ++I) 158 for (iterator I = begin(), E = end(); I != E; ++I)
157 (*I)->dump(); 159 (*I)->dump();
158 } 160 }
159 #endif // NDEBUG 161 #endif // NDEBUG
160 162
204 DenseMap<MachineBasicBlock *, BlockChain *> BlockToChain; 206 DenseMap<MachineBasicBlock *, BlockChain *> BlockToChain;
205 207
206 void markChainSuccessors(BlockChain &Chain, 208 void markChainSuccessors(BlockChain &Chain,
207 MachineBasicBlock *LoopHeaderBB, 209 MachineBasicBlock *LoopHeaderBB,
208 SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, 210 SmallVectorImpl<MachineBasicBlock *> &BlockWorkList,
209 const BlockFilterSet *BlockFilter = 0); 211 const BlockFilterSet *BlockFilter = nullptr);
210 MachineBasicBlock *selectBestSuccessor(MachineBasicBlock *BB, 212 MachineBasicBlock *selectBestSuccessor(MachineBasicBlock *BB,
211 BlockChain &Chain, 213 BlockChain &Chain,
212 const BlockFilterSet *BlockFilter); 214 const BlockFilterSet *BlockFilter);
213 MachineBasicBlock *selectBestCandidateBlock( 215 MachineBasicBlock *selectBestCandidateBlock(
214 BlockChain &Chain, SmallVectorImpl<MachineBasicBlock *> &WorkList, 216 BlockChain &Chain, SmallVectorImpl<MachineBasicBlock *> &WorkList,
218 const BlockChain &PlacedChain, 220 const BlockChain &PlacedChain,
219 MachineFunction::iterator &PrevUnplacedBlockIt, 221 MachineFunction::iterator &PrevUnplacedBlockIt,
220 const BlockFilterSet *BlockFilter); 222 const BlockFilterSet *BlockFilter);
221 void buildChain(MachineBasicBlock *BB, BlockChain &Chain, 223 void buildChain(MachineBasicBlock *BB, BlockChain &Chain,
222 SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, 224 SmallVectorImpl<MachineBasicBlock *> &BlockWorkList,
223 const BlockFilterSet *BlockFilter = 0); 225 const BlockFilterSet *BlockFilter = nullptr);
224 MachineBasicBlock *findBestLoopTop(MachineLoop &L, 226 MachineBasicBlock *findBestLoopTop(MachineLoop &L,
225 const BlockFilterSet &LoopBlockSet); 227 const BlockFilterSet &LoopBlockSet);
226 MachineBasicBlock *findBestLoopExit(MachineFunction &F, 228 MachineBasicBlock *findBestLoopExit(MachineFunction &F,
227 MachineLoop &L, 229 MachineLoop &L,
228 const BlockFilterSet &LoopBlockSet); 230 const BlockFilterSet &LoopBlockSet);
235 static char ID; // Pass identification, replacement for typeid 237 static char ID; // Pass identification, replacement for typeid
236 MachineBlockPlacement() : MachineFunctionPass(ID) { 238 MachineBlockPlacement() : MachineFunctionPass(ID) {
237 initializeMachineBlockPlacementPass(*PassRegistry::getPassRegistry()); 239 initializeMachineBlockPlacementPass(*PassRegistry::getPassRegistry());
238 } 240 }
239 241
240 bool runOnMachineFunction(MachineFunction &F); 242 bool runOnMachineFunction(MachineFunction &F) override;
241 243
242 void getAnalysisUsage(AnalysisUsage &AU) const { 244 void getAnalysisUsage(AnalysisUsage &AU) const override {
243 AU.addRequired<MachineBranchProbabilityInfo>(); 245 AU.addRequired<MachineBranchProbabilityInfo>();
244 AU.addRequired<MachineBlockFrequencyInfo>(); 246 AU.addRequired<MachineBlockFrequencyInfo>();
245 AU.addRequired<MachineLoopInfo>(); 247 AU.addRequired<MachineLoopInfo>();
246 MachineFunctionPass::getAnalysisUsage(AU); 248 MachineFunctionPass::getAnalysisUsage(AU);
247 } 249 }
332 MachineBasicBlock *MachineBlockPlacement::selectBestSuccessor( 334 MachineBasicBlock *MachineBlockPlacement::selectBestSuccessor(
333 MachineBasicBlock *BB, BlockChain &Chain, 335 MachineBasicBlock *BB, BlockChain &Chain,
334 const BlockFilterSet *BlockFilter) { 336 const BlockFilterSet *BlockFilter) {
335 const BranchProbability HotProb(4, 5); // 80% 337 const BranchProbability HotProb(4, 5); // 80%
336 338
337 MachineBasicBlock *BestSucc = 0; 339 MachineBasicBlock *BestSucc = nullptr;
338 // FIXME: Due to the performance of the probability and weight routines in 340 // FIXME: Due to the performance of the probability and weight routines in
339 // the MBPI analysis, we manually compute probabilities using the edge 341 // the MBPI analysis, we manually compute probabilities using the edge
340 // weights. This is suboptimal as it means that the somewhat subtle 342 // weights. This is suboptimal as it means that the somewhat subtle
341 // definition of edge weight semantics is encoded here as well. We should 343 // definition of edge weight semantics is encoded here as well. We should
342 // improve the MBPI interface to efficiently support query patterns such as 344 // improve the MBPI interface to efficiently support query patterns such as
407 BestWeight = SuccWeight; 409 BestWeight = SuccWeight;
408 } 410 }
409 return BestSucc; 411 return BestSucc;
410 } 412 }
411 413
412 namespace {
413 /// \brief Predicate struct to detect blocks already placed.
414 class IsBlockPlaced {
415 const BlockChain &PlacedChain;
416 const BlockToChainMapType &BlockToChain;
417
418 public:
419 IsBlockPlaced(const BlockChain &PlacedChain,
420 const BlockToChainMapType &BlockToChain)
421 : PlacedChain(PlacedChain), BlockToChain(BlockToChain) {}
422
423 bool operator()(MachineBasicBlock *BB) const {
424 return BlockToChain.lookup(BB) == &PlacedChain;
425 }
426 };
427 }
428
429 /// \brief Select the best block from a worklist. 414 /// \brief Select the best block from a worklist.
430 /// 415 ///
431 /// This looks through the provided worklist as a list of candidate basic 416 /// This looks through the provided worklist as a list of candidate basic
432 /// blocks and select the most profitable one to place. The definition of 417 /// blocks and select the most profitable one to place. The definition of
433 /// profitable only really makes sense in the context of a loop. This returns 418 /// profitable only really makes sense in the context of a loop. This returns
442 // Once we need to walk the worklist looking for a candidate, cleanup the 427 // Once we need to walk the worklist looking for a candidate, cleanup the
443 // worklist of already placed entries. 428 // worklist of already placed entries.
444 // FIXME: If this shows up on profiles, it could be folded (at the cost of 429 // FIXME: If this shows up on profiles, it could be folded (at the cost of
445 // some code complexity) into the loop below. 430 // some code complexity) into the loop below.
446 WorkList.erase(std::remove_if(WorkList.begin(), WorkList.end(), 431 WorkList.erase(std::remove_if(WorkList.begin(), WorkList.end(),
447 IsBlockPlaced(Chain, BlockToChain)), 432 [&](MachineBasicBlock *BB) {
433 return BlockToChain.lookup(BB) == &Chain;
434 }),
448 WorkList.end()); 435 WorkList.end());
449 436
450 MachineBasicBlock *BestBlock = 0; 437 MachineBasicBlock *BestBlock = nullptr;
451 BlockFrequency BestFreq; 438 BlockFrequency BestFreq;
452 for (SmallVectorImpl<MachineBasicBlock *>::iterator WBI = WorkList.begin(), 439 for (SmallVectorImpl<MachineBasicBlock *>::iterator WBI = WorkList.begin(),
453 WBE = WorkList.end(); 440 WBE = WorkList.end();
454 WBI != WBE; ++WBI) { 441 WBI != WBE; ++WBI) {
455 BlockChain &SuccChain = *BlockToChain[*WBI]; 442 BlockChain &SuccChain = *BlockToChain[*WBI];
459 continue; 446 continue;
460 } 447 }
461 assert(SuccChain.LoopPredecessors == 0 && "Found CFG-violating block"); 448 assert(SuccChain.LoopPredecessors == 0 && "Found CFG-violating block");
462 449
463 BlockFrequency CandidateFreq = MBFI->getBlockFreq(*WBI); 450 BlockFrequency CandidateFreq = MBFI->getBlockFreq(*WBI);
464 DEBUG(dbgs() << " " << getBlockName(*WBI) << " -> " << CandidateFreq 451 DEBUG(dbgs() << " " << getBlockName(*WBI) << " -> ";
465 << " (freq)\n"); 452 MBFI->printBlockFreq(dbgs(), CandidateFreq) << " (freq)\n");
466 if (BestBlock && BestFreq >= CandidateFreq) 453 if (BestBlock && BestFreq >= CandidateFreq)
467 continue; 454 continue;
468 BestBlock = *WBI; 455 BestBlock = *WBI;
469 BestFreq = CandidateFreq; 456 BestFreq = CandidateFreq;
470 } 457 }
492 // as the block to place. This will force the entire chain to be placed, 479 // as the block to place. This will force the entire chain to be placed,
493 // and satisfies the requirements of merging chains. 480 // and satisfies the requirements of merging chains.
494 return *BlockToChain[I]->begin(); 481 return *BlockToChain[I]->begin();
495 } 482 }
496 } 483 }
497 return 0; 484 return nullptr;
498 } 485 }
499 486
500 void MachineBlockPlacement::buildChain( 487 void MachineBlockPlacement::buildChain(
501 MachineBasicBlock *BB, 488 MachineBasicBlock *BB,
502 BlockChain &Chain, 489 BlockChain &Chain,
507 MachineFunction &F = *BB->getParent(); 494 MachineFunction &F = *BB->getParent();
508 MachineFunction::iterator PrevUnplacedBlockIt = F.begin(); 495 MachineFunction::iterator PrevUnplacedBlockIt = F.begin();
509 496
510 MachineBasicBlock *LoopHeaderBB = BB; 497 MachineBasicBlock *LoopHeaderBB = BB;
511 markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter); 498 markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter);
512 BB = *llvm::prior(Chain.end()); 499 BB = *std::prev(Chain.end());
513 for (;;) { 500 for (;;) {
514 assert(BB); 501 assert(BB);
515 assert(BlockToChain[BB] == &Chain); 502 assert(BlockToChain[BB] == &Chain);
516 assert(*llvm::prior(Chain.end()) == BB); 503 assert(*std::prev(Chain.end()) == BB);
517 504
518 // Look for the best viable successor if there is one to place immediately 505 // Look for the best viable successor if there is one to place immediately
519 // after this block. 506 // after this block.
520 MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter); 507 MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter);
521 508
542 SuccChain.LoopPredecessors = 0; 529 SuccChain.LoopPredecessors = 0;
543 DEBUG(dbgs() << "Merging from " << getBlockNum(BB) 530 DEBUG(dbgs() << "Merging from " << getBlockNum(BB)
544 << " to " << getBlockNum(BestSucc) << "\n"); 531 << " to " << getBlockNum(BestSucc) << "\n");
545 markChainSuccessors(SuccChain, LoopHeaderBB, BlockWorkList, BlockFilter); 532 markChainSuccessors(SuccChain, LoopHeaderBB, BlockWorkList, BlockFilter);
546 Chain.merge(BestSucc, &SuccChain); 533 Chain.merge(BestSucc, &SuccChain);
547 BB = *llvm::prior(Chain.end()); 534 BB = *std::prev(Chain.end());
548 } 535 }
549 536
550 DEBUG(dbgs() << "Finished forming chain for header block " 537 DEBUG(dbgs() << "Finished forming chain for header block "
551 << getBlockNum(*Chain.begin()) << "\n"); 538 << getBlockNum(*Chain.begin()) << "\n");
552 } 539 }
573 560
574 DEBUG(dbgs() << "Finding best loop top for: " 561 DEBUG(dbgs() << "Finding best loop top for: "
575 << getBlockName(L.getHeader()) << "\n"); 562 << getBlockName(L.getHeader()) << "\n");
576 563
577 BlockFrequency BestPredFreq; 564 BlockFrequency BestPredFreq;
578 MachineBasicBlock *BestPred = 0; 565 MachineBasicBlock *BestPred = nullptr;
579 for (MachineBasicBlock::pred_iterator PI = L.getHeader()->pred_begin(), 566 for (MachineBasicBlock::pred_iterator PI = L.getHeader()->pred_begin(),
580 PE = L.getHeader()->pred_end(); 567 PE = L.getHeader()->pred_end();
581 PI != PE; ++PI) { 568 PI != PE; ++PI) {
582 MachineBasicBlock *Pred = *PI; 569 MachineBasicBlock *Pred = *PI;
583 if (!LoopBlockSet.count(Pred)) 570 if (!LoopBlockSet.count(Pred))
584 continue; 571 continue;
585 DEBUG(dbgs() << " header pred: " << getBlockName(Pred) << ", " 572 DEBUG(dbgs() << " header pred: " << getBlockName(Pred) << ", "
586 << Pred->succ_size() << " successors, " 573 << Pred->succ_size() << " successors, ";
587 << MBFI->getBlockFreq(Pred) << " freq\n"); 574 MBFI->printBlockFreq(dbgs(), Pred) << " freq\n");
588 if (Pred->succ_size() > 1) 575 if (Pred->succ_size() > 1)
589 continue; 576 continue;
590 577
591 BlockFrequency PredFreq = MBFI->getBlockFreq(Pred); 578 BlockFrequency PredFreq = MBFI->getBlockFreq(Pred);
592 if (!BestPred || PredFreq > BestPredFreq || 579 if (!BestPred || PredFreq > BestPredFreq ||
629 // of the loop, rotating the header into the middle of the loop will create 616 // of the loop, rotating the header into the middle of the loop will create
630 // a non-contiguous range of blocks which is Very Bad. So start with the 617 // a non-contiguous range of blocks which is Very Bad. So start with the
631 // header and only rotate if safe. 618 // header and only rotate if safe.
632 BlockChain &HeaderChain = *BlockToChain[L.getHeader()]; 619 BlockChain &HeaderChain = *BlockToChain[L.getHeader()];
633 if (!LoopBlockSet.count(*HeaderChain.begin())) 620 if (!LoopBlockSet.count(*HeaderChain.begin()))
634 return 0; 621 return nullptr;
635 622
636 BlockFrequency BestExitEdgeFreq; 623 BlockFrequency BestExitEdgeFreq;
637 unsigned BestExitLoopDepth = 0; 624 unsigned BestExitLoopDepth = 0;
638 MachineBasicBlock *ExitingBB = 0; 625 MachineBasicBlock *ExitingBB = nullptr;
639 // If there are exits to outer loops, loop rotation can severely limit 626 // If there are exits to outer loops, loop rotation can severely limit
640 // fallthrough opportunites unless it selects such an exit. Keep a set of 627 // fallthrough opportunites unless it selects such an exit. Keep a set of
641 // blocks where rotating to exit with that block will reach an outer loop. 628 // blocks where rotating to exit with that block will reach an outer loop.
642 SmallPtrSet<MachineBasicBlock *, 4> BlocksExitingToOuterLoop; 629 SmallPtrSet<MachineBasicBlock *, 4> BlocksExitingToOuterLoop;
643 630
647 E = L.block_end(); 634 E = L.block_end();
648 I != E; ++I) { 635 I != E; ++I) {
649 BlockChain &Chain = *BlockToChain[*I]; 636 BlockChain &Chain = *BlockToChain[*I];
650 // Ensure that this block is at the end of a chain; otherwise it could be 637 // Ensure that this block is at the end of a chain; otherwise it could be
651 // mid-way through an inner loop or a successor of an analyzable branch. 638 // mid-way through an inner loop or a successor of an analyzable branch.
652 if (*I != *llvm::prior(Chain.end())) 639 if (*I != *std::prev(Chain.end()))
653 continue; 640 continue;
654 641
655 // Now walk the successors. We need to establish whether this has a viable 642 // Now walk the successors. We need to establish whether this has a viable
656 // exiting successor and whether it has a viable non-exiting successor. 643 // exiting successor and whether it has a viable non-exiting successor.
657 // We store the old exiting state and restore it if a viable looping 644 // We store the old exiting state and restore it if a viable looping
696 683
697 BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight); 684 BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight);
698 BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(*I) * SuccProb; 685 BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(*I) * SuccProb;
699 DEBUG(dbgs() << " exiting: " << getBlockName(*I) << " -> " 686 DEBUG(dbgs() << " exiting: " << getBlockName(*I) << " -> "
700 << getBlockName(*SI) << " [L:" << SuccLoopDepth 687 << getBlockName(*SI) << " [L:" << SuccLoopDepth
701 << "] (" << ExitEdgeFreq << ")\n"); 688 << "] (";
689 MBFI->printBlockFreq(dbgs(), ExitEdgeFreq) << ")\n");
702 // Note that we bias this toward an existing layout successor to retain 690 // Note that we bias this toward an existing layout successor to retain
703 // incoming order in the absence of better information. The exit must have 691 // incoming order in the absence of better information. The exit must have
704 // a frequency higher than the current exit before we consider breaking 692 // a frequency higher than the current exit before we consider breaking
705 // the layout. 693 // the layout.
706 BranchProbability Bias(100 - ExitBlockBias, 100); 694 BranchProbability Bias(100 - ExitBlockBias, 100);
721 } 709 }
722 } 710 }
723 // Without a candidate exiting block or with only a single block in the 711 // Without a candidate exiting block or with only a single block in the
724 // loop, just use the loop header to layout the loop. 712 // loop, just use the loop header to layout the loop.
725 if (!ExitingBB || L.getNumBlocks() == 1) 713 if (!ExitingBB || L.getNumBlocks() == 1)
726 return 0; 714 return nullptr;
727 715
728 // Also, if we have exit blocks which lead to outer loops but didn't select 716 // Also, if we have exit blocks which lead to outer loops but didn't select
729 // one of them as the exiting block we are rotating toward, disable loop 717 // one of them as the exiting block we are rotating toward, disable loop
730 // rotation altogether. 718 // rotation altogether.
731 if (!BlocksExitingToOuterLoop.empty() && 719 if (!BlocksExitingToOuterLoop.empty() &&
732 !BlocksExitingToOuterLoop.count(ExitingBB)) 720 !BlocksExitingToOuterLoop.count(ExitingBB))
733 return 0; 721 return nullptr;
734 722
735 DEBUG(dbgs() << " Best exiting block: " << getBlockName(ExitingBB) << "\n"); 723 DEBUG(dbgs() << " Best exiting block: " << getBlockName(ExitingBB) << "\n");
736 return ExitingBB; 724 return ExitingBB;
737 } 725 }
738 726
753 for (MachineBasicBlock::pred_iterator PI = Top->pred_begin(), 741 for (MachineBasicBlock::pred_iterator PI = Top->pred_begin(),
754 PE = Top->pred_end(); 742 PE = Top->pred_end();
755 PI != PE; ++PI) { 743 PI != PE; ++PI) {
756 BlockChain *PredChain = BlockToChain[*PI]; 744 BlockChain *PredChain = BlockToChain[*PI];
757 if (!LoopBlockSet.count(*PI) && 745 if (!LoopBlockSet.count(*PI) &&
758 (!PredChain || *PI == *llvm::prior(PredChain->end()))) { 746 (!PredChain || *PI == *std::prev(PredChain->end()))) {
759 ViableTopFallthrough = true; 747 ViableTopFallthrough = true;
760 break; 748 break;
761 } 749 }
762 } 750 }
763 751
764 // If the header has viable fallthrough, check whether the current loop 752 // If the header has viable fallthrough, check whether the current loop
765 // bottom is a viable exiting block. If so, bail out as rotating will 753 // bottom is a viable exiting block. If so, bail out as rotating will
766 // introduce an unnecessary branch. 754 // introduce an unnecessary branch.
767 if (ViableTopFallthrough) { 755 if (ViableTopFallthrough) {
768 MachineBasicBlock *Bottom = *llvm::prior(LoopChain.end()); 756 MachineBasicBlock *Bottom = *std::prev(LoopChain.end());
769 for (MachineBasicBlock::succ_iterator SI = Bottom->succ_begin(), 757 for (MachineBasicBlock::succ_iterator SI = Bottom->succ_begin(),
770 SE = Bottom->succ_end(); 758 SE = Bottom->succ_end();
771 SI != SE; ++SI) { 759 SI != SE; ++SI) {
772 BlockChain *SuccChain = BlockToChain[*SI]; 760 BlockChain *SuccChain = BlockToChain[*SI];
773 if (!LoopBlockSet.count(*SI) && 761 if (!LoopBlockSet.count(*SI) &&
779 BlockChain::iterator ExitIt = std::find(LoopChain.begin(), LoopChain.end(), 767 BlockChain::iterator ExitIt = std::find(LoopChain.begin(), LoopChain.end(),
780 ExitingBB); 768 ExitingBB);
781 if (ExitIt == LoopChain.end()) 769 if (ExitIt == LoopChain.end())
782 return; 770 return;
783 771
784 std::rotate(LoopChain.begin(), llvm::next(ExitIt), LoopChain.end()); 772 std::rotate(LoopChain.begin(), std::next(ExitIt), LoopChain.end());
785 } 773 }
786 774
787 /// \brief Forms basic block chains from the natural loop structures. 775 /// \brief Forms basic block chains from the natural loop structures.
788 /// 776 ///
789 /// These chains are designed to preserve the existing *structure* of the code 777 /// These chains are designed to preserve the existing *structure* of the code
807 MachineBasicBlock *LoopTop = findBestLoopTop(L, LoopBlockSet); 795 MachineBasicBlock *LoopTop = findBestLoopTop(L, LoopBlockSet);
808 796
809 // If we selected just the header for the loop top, look for a potentially 797 // If we selected just the header for the loop top, look for a potentially
810 // profitable exit block in the event that rotating the loop can eliminate 798 // profitable exit block in the event that rotating the loop can eliminate
811 // branches by placing an exit edge at the bottom. 799 // branches by placing an exit edge at the bottom.
812 MachineBasicBlock *ExitingBB = 0; 800 MachineBasicBlock *ExitingBB = nullptr;
813 if (LoopTop == L.getHeader()) 801 if (LoopTop == L.getHeader())
814 ExitingBB = findBestLoopExit(F, L, LoopBlockSet); 802 ExitingBB = findBestLoopExit(F, L, LoopBlockSet);
815 803
816 BlockChain &LoopChain = *BlockToChain[LoopTop]; 804 BlockChain &LoopChain = *BlockToChain[LoopTop];
817 805
895 = new (ChainAllocator.Allocate()) BlockChain(BlockToChain, BB); 883 = new (ChainAllocator.Allocate()) BlockChain(BlockToChain, BB);
896 // Also, merge any blocks which we cannot reason about and must preserve 884 // Also, merge any blocks which we cannot reason about and must preserve
897 // the exact fallthrough behavior for. 885 // the exact fallthrough behavior for.
898 for (;;) { 886 for (;;) {
899 Cond.clear(); 887 Cond.clear();
900 MachineBasicBlock *TBB = 0, *FBB = 0; // For AnalyzeBranch. 888 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch.
901 if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond) || !FI->canFallThrough()) 889 if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond) || !FI->canFallThrough())
902 break; 890 break;
903 891
904 MachineFunction::iterator NextFI(llvm::next(FI)); 892 MachineFunction::iterator NextFI(std::next(FI));
905 MachineBasicBlock *NextBB = NextFI; 893 MachineBasicBlock *NextBB = NextFI;
906 // Ensure that the layout successor is a viable block, as we know that 894 // Ensure that the layout successor is a viable block, as we know that
907 // fallthrough is a possibility. 895 // fallthrough is a possibility.
908 assert(NextFI != FE && "Can't fallthrough past the last block."); 896 assert(NextFI != FE && "Can't fallthrough past the last block.");
909 DEBUG(dbgs() << "Pre-merging due to unanalyzable fallthrough: " 897 DEBUG(dbgs() << "Pre-merging due to unanalyzable fallthrough: "
910 << getBlockName(BB) << " -> " << getBlockName(NextBB) 898 << getBlockName(BB) << " -> " << getBlockName(NextBB)
911 << "\n"); 899 << "\n");
912 Chain->merge(NextBB, 0); 900 Chain->merge(NextBB, nullptr);
913 FI = NextFI; 901 FI = NextFI;
914 BB = NextBB; 902 BB = NextBB;
915 } 903 }
916 } 904 }
917 905
993 ++InsertPos; 981 ++InsertPos;
994 982
995 // Update the terminator of the previous block. 983 // Update the terminator of the previous block.
996 if (BI == FunctionChain.begin()) 984 if (BI == FunctionChain.begin())
997 continue; 985 continue;
998 MachineBasicBlock *PrevBB = llvm::prior(MachineFunction::iterator(*BI)); 986 MachineBasicBlock *PrevBB = std::prev(MachineFunction::iterator(*BI));
999 987
1000 // FIXME: It would be awesome of updateTerminator would just return rather 988 // FIXME: It would be awesome of updateTerminator would just return rather
1001 // than assert when the branch cannot be analyzed in order to remove this 989 // than assert when the branch cannot be analyzed in order to remove this
1002 // boiler plate. 990 // boiler plate.
1003 Cond.clear(); 991 Cond.clear();
1004 MachineBasicBlock *TBB = 0, *FBB = 0; // For AnalyzeBranch. 992 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch.
1005 if (!TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { 993 if (!TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) {
1006 // The "PrevBB" is not yet updated to reflect current code layout, so, 994 // The "PrevBB" is not yet updated to reflect current code layout, so,
1007 // o. it may fall-through to a block without explict "goto" instruction 995 // o. it may fall-through to a block without explict "goto" instruction
1008 // before layout, and no longer fall-through it after layout; or 996 // before layout, and no longer fall-through it after layout; or
1009 // o. just opposite. 997 // o. just opposite.
1016 bool needUpdateBr = true; 1004 bool needUpdateBr = true;
1017 if (!Cond.empty() && (!FBB || FBB == *BI)) { 1005 if (!Cond.empty() && (!FBB || FBB == *BI)) {
1018 PrevBB->updateTerminator(); 1006 PrevBB->updateTerminator();
1019 needUpdateBr = false; 1007 needUpdateBr = false;
1020 Cond.clear(); 1008 Cond.clear();
1021 TBB = FBB = 0; 1009 TBB = FBB = nullptr;
1022 if (TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { 1010 if (TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) {
1023 // FIXME: This should never take place. 1011 // FIXME: This should never take place.
1024 TBB = FBB = 0; 1012 TBB = FBB = nullptr;
1025 } 1013 }
1026 } 1014 }
1027 1015
1028 // If PrevBB has a two-way branch, try to re-order the branches 1016 // If PrevBB has a two-way branch, try to re-order the branches
1029 // such that we branch to the successor with higher weight first. 1017 // such that we branch to the successor with higher weight first.
1044 } 1032 }
1045 } 1033 }
1046 1034
1047 // Fixup the last block. 1035 // Fixup the last block.
1048 Cond.clear(); 1036 Cond.clear();
1049 MachineBasicBlock *TBB = 0, *FBB = 0; // For AnalyzeBranch. 1037 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch.
1050 if (!TII->AnalyzeBranch(F.back(), TBB, FBB, Cond)) 1038 if (!TII->AnalyzeBranch(F.back(), TBB, FBB, Cond))
1051 F.back().updateTerminator(); 1039 F.back().updateTerminator();
1052 1040
1053 // Walk through the backedges of the function now that we have fully laid out 1041 // Walk through the backedges of the function now that we have fully laid out
1054 // the basic blocks and align the destination of each backedge. We don't rely 1042 // the basic blocks and align the destination of each backedge. We don't rely
1065 return; // Empty chain. 1053 return; // Empty chain.
1066 1054
1067 const BranchProbability ColdProb(1, 5); // 20% 1055 const BranchProbability ColdProb(1, 5); // 20%
1068 BlockFrequency EntryFreq = MBFI->getBlockFreq(F.begin()); 1056 BlockFrequency EntryFreq = MBFI->getBlockFreq(F.begin());
1069 BlockFrequency WeightedEntryFreq = EntryFreq * ColdProb; 1057 BlockFrequency WeightedEntryFreq = EntryFreq * ColdProb;
1070 for (BlockChain::iterator BI = llvm::next(FunctionChain.begin()), 1058 for (BlockChain::iterator BI = std::next(FunctionChain.begin()),
1071 BE = FunctionChain.end(); 1059 BE = FunctionChain.end();
1072 BI != BE; ++BI) { 1060 BI != BE; ++BI) {
1073 // Don't align non-looping basic blocks. These are unlikely to execute 1061 // Don't align non-looping basic blocks. These are unlikely to execute
1074 // enough times to matter in practice. Note that we'll still handle 1062 // enough times to matter in practice. Note that we'll still handle
1075 // unnatural CFGs inside of a natural outer loop (the common case) and 1063 // unnatural CFGs inside of a natural outer loop (the common case) and
1091 if (Freq < (LoopHeaderFreq * ColdProb)) 1079 if (Freq < (LoopHeaderFreq * ColdProb))
1092 continue; 1080 continue;
1093 1081
1094 // Check for the existence of a non-layout predecessor which would benefit 1082 // Check for the existence of a non-layout predecessor which would benefit
1095 // from aligning this block. 1083 // from aligning this block.
1096 MachineBasicBlock *LayoutPred = *llvm::prior(BI); 1084 MachineBasicBlock *LayoutPred = *std::prev(BI);
1097 1085
1098 // Force alignment if all the predecessors are jumps. We already checked 1086 // Force alignment if all the predecessors are jumps. We already checked
1099 // that the block isn't cold above. 1087 // that the block isn't cold above.
1100 if (!LayoutPred->isSuccessor(*BI)) { 1088 if (!LayoutPred->isSuccessor(*BI)) {
1101 (*BI)->setAlignment(Align); 1089 (*BI)->setAlignment(Align);
1113 } 1101 }
1114 } 1102 }
1115 1103
1116 bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &F) { 1104 bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &F) {
1117 // Check for single-block functions and skip them. 1105 // Check for single-block functions and skip them.
1118 if (llvm::next(F.begin()) == F.end()) 1106 if (std::next(F.begin()) == F.end())
1107 return false;
1108
1109 if (skipOptnoneFunction(*F.getFunction()))
1119 return false; 1110 return false;
1120 1111
1121 MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 1112 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
1122 MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); 1113 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
1123 MLI = &getAnalysis<MachineLoopInfo>(); 1114 MLI = &getAnalysis<MachineLoopInfo>();
1124 TII = F.getTarget().getInstrInfo(); 1115 TII = F.getSubtarget().getInstrInfo();
1125 TLI = F.getTarget().getTargetLowering(); 1116 TLI = F.getSubtarget().getTargetLowering();
1126 assert(BlockToChain.empty()); 1117 assert(BlockToChain.empty());
1127 1118
1128 buildCFGChains(F); 1119 buildCFGChains(F);
1129 1120
1130 BlockToChain.clear(); 1121 BlockToChain.clear();
1159 static char ID; // Pass identification, replacement for typeid 1150 static char ID; // Pass identification, replacement for typeid
1160 MachineBlockPlacementStats() : MachineFunctionPass(ID) { 1151 MachineBlockPlacementStats() : MachineFunctionPass(ID) {
1161 initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry()); 1152 initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry());
1162 } 1153 }
1163 1154
1164 bool runOnMachineFunction(MachineFunction &F); 1155 bool runOnMachineFunction(MachineFunction &F) override;
1165 1156
1166 void getAnalysisUsage(AnalysisUsage &AU) const { 1157 void getAnalysisUsage(AnalysisUsage &AU) const override {
1167 AU.addRequired<MachineBranchProbabilityInfo>(); 1158 AU.addRequired<MachineBranchProbabilityInfo>();
1168 AU.addRequired<MachineBlockFrequencyInfo>(); 1159 AU.addRequired<MachineBlockFrequencyInfo>();
1169 AU.setPreservesAll(); 1160 AU.setPreservesAll();
1170 MachineFunctionPass::getAnalysisUsage(AU); 1161 MachineFunctionPass::getAnalysisUsage(AU);
1171 } 1162 }
1181 INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats", 1172 INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats",
1182 "Basic Block Placement Stats", false, false) 1173 "Basic Block Placement Stats", false, false)
1183 1174
1184 bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) { 1175 bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) {
1185 // Check for single-block functions and skip them. 1176 // Check for single-block functions and skip them.
1186 if (llvm::next(F.begin()) == F.end()) 1177 if (std::next(F.begin()) == F.end())
1187 return false; 1178 return false;
1188 1179
1189 MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 1180 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
1190 MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); 1181 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
1191 1182