BasicBlockPlacement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===-- BasicBlockPlacement.cpp - Basic Block Code Layout optimization ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a very simple profile guided basic block placement
  11. // algorithm. The idea is to put frequently executed blocks together at the
  12. // start of the function, and hopefully increase the number of fall-through
  13. // conditional branches. If there is no profile information for a particular
  14. // function, this pass basically orders blocks in depth-first order
  15. //
  16. // The algorithm implemented here is basically "Algo1" from "Profile Guided Code
  17. // Positioning" by Pettis and Hansen, except that it uses basic block counts
  18. // instead of edge counts. This should be improved in many ways, but is very
  19. // simple for now.
  20. //
  21. // Basically we "place" the entry block, then loop over all successors in a DFO,
  22. // placing the most frequently executed successor until we run out of blocks. I
  23. // told you this was _extremely_ simplistic. :) This is also much slower than it
  24. // could be. When it becomes important, this pass will be rewritten to use a
  25. // better algorithm, and then we can worry about efficiency.
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #define DEBUG_TYPE "block-placement"
  29. #include "llvm/Analysis/ProfileInfo.h"
  30. #include "llvm/Function.h"
  31. #include "llvm/Pass.h"
  32. #include "llvm/Support/CFG.h"
  33. #include "llvm/Support/Compiler.h"
  34. #include "llvm/ADT/Statistic.h"
  35. #include "llvm/Transforms/Scalar.h"
  36. #include <set>
  37. using namespace llvm;
  38. STATISTIC(NumMoved, "Number of basic blocks moved");
  39. namespace {
  40. struct VISIBILITY_HIDDEN BlockPlacement : public FunctionPass {
  41. static char ID; // Pass identification, replacement for typeid
  42. BlockPlacement() : FunctionPass(&ID) {}
  43. virtual bool runOnFunction(Function &F);
  44. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  45. AU.setPreservesCFG();
  46. AU.addRequired<ProfileInfo>();
  47. //AU.addPreserved<ProfileInfo>(); // Does this work?
  48. }
  49. private:
  50. /// PI - The profile information that is guiding us.
  51. ///
  52. ProfileInfo *PI;
  53. /// NumMovedBlocks - Every time we move a block, increment this counter.
  54. ///
  55. unsigned NumMovedBlocks;
  56. /// PlacedBlocks - Every time we place a block, remember it so we don't get
  57. /// into infinite loops.
  58. std::set<BasicBlock*> PlacedBlocks;
  59. /// InsertPos - This an iterator to the next place we want to insert a
  60. /// block.
  61. Function::iterator InsertPos;
  62. /// PlaceBlocks - Recursively place the specified blocks and any unplaced
  63. /// successors.
  64. void PlaceBlocks(BasicBlock *BB);
  65. };
  66. }
  67. char BlockPlacement::ID = 0;
  68. static RegisterPass<BlockPlacement>
  69. X("block-placement", "Profile Guided Basic Block Placement");
  70. FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); }
  71. bool BlockPlacement::runOnFunction(Function &F) {
  72. PI = &getAnalysis<ProfileInfo>();
  73. NumMovedBlocks = 0;
  74. InsertPos = F.begin();
  75. // Recursively place all blocks.
  76. PlaceBlocks(F.begin());
  77. PlacedBlocks.clear();
  78. NumMoved += NumMovedBlocks;
  79. return NumMovedBlocks != 0;
  80. }
  81. /// PlaceBlocks - Recursively place the specified blocks and any unplaced
  82. /// successors.
  83. void BlockPlacement::PlaceBlocks(BasicBlock *BB) {
  84. assert(!PlacedBlocks.count(BB) && "Already placed this block!");
  85. PlacedBlocks.insert(BB);
  86. // Place the specified block.
  87. if (&*InsertPos != BB) {
  88. // Use splice to move the block into the right place. This avoids having to
  89. // remove the block from the function then readd it, which causes a bunch of
  90. // symbol table traffic that is entirely pointless.
  91. Function::BasicBlockListType &Blocks = BB->getParent()->getBasicBlockList();
  92. Blocks.splice(InsertPos, Blocks, BB);
  93. ++NumMovedBlocks;
  94. } else {
  95. // This block is already in the right place, we don't have to do anything.
  96. ++InsertPos;
  97. }
  98. // Keep placing successors until we run out of ones to place. Note that this
  99. // loop is very inefficient (N^2) for blocks with many successors, like switch
  100. // statements. FIXME!
  101. while (1) {
  102. // Okay, now place any unplaced successors.
  103. succ_iterator SI = succ_begin(BB), E = succ_end(BB);
  104. // Scan for the first unplaced successor.
  105. for (; SI != E && PlacedBlocks.count(*SI); ++SI)
  106. /*empty*/;
  107. if (SI == E) return; // No more successors to place.
  108. unsigned MaxExecutionCount = PI->getExecutionCount(*SI);
  109. BasicBlock *MaxSuccessor = *SI;
  110. // Scan for more frequently executed successors
  111. for (; SI != E; ++SI)
  112. if (!PlacedBlocks.count(*SI)) {
  113. unsigned Count = PI->getExecutionCount(*SI);
  114. if (Count > MaxExecutionCount ||
  115. // Prefer to not disturb the code.
  116. (Count == MaxExecutionCount && *SI == &*InsertPos)) {
  117. MaxExecutionCount = Count;
  118. MaxSuccessor = *SI;
  119. }
  120. }
  121. // Now that we picked the maximally executed successor, place it.
  122. PlaceBlocks(MaxSuccessor);
  123. }
  124. }