Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 1 | //===-- BasicBlockPlacement.cpp - Basic Block Code Layout optimization ----===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "block-placement" |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ProfileInfo.h" |
| 31 | #include "llvm/Function.h" |
| 32 | #include "llvm/Pass.h" |
| 33 | #include "llvm/Support/CFG.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Jeff Cohen | bf65268 | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 37 | #include <set> |
| 38 | using namespace llvm; |
| 39 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 40 | STATISTIC(NumMoved, "Number of basic blocks moved"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 42 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 43 | struct VISIBILITY_HIDDEN BlockPlacement : public FunctionPass { |
Devang Patel | 3e15bf3 | 2007-05-02 21:39:20 +0000 | [diff] [blame] | 44 | static const char ID; // Pass identifcation, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 45 | BlockPlacement() : FunctionPass((intptr_t)&ID) {} |
| 46 | |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 47 | virtual bool runOnFunction(Function &F); |
| 48 | |
| 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 50 | AU.setPreservesCFG(); |
| 51 | AU.addRequired<ProfileInfo>(); |
| 52 | //AU.addPreserved<ProfileInfo>(); // Does this work? |
| 53 | } |
| 54 | private: |
| 55 | /// PI - The profile information that is guiding us. |
| 56 | /// |
| 57 | ProfileInfo *PI; |
| 58 | |
| 59 | /// NumMovedBlocks - Every time we move a block, increment this counter. |
| 60 | /// |
| 61 | unsigned NumMovedBlocks; |
| 62 | |
| 63 | /// PlacedBlocks - Every time we place a block, remember it so we don't get |
| 64 | /// into infinite loops. |
| 65 | std::set<BasicBlock*> PlacedBlocks; |
| 66 | |
| 67 | /// InsertPos - This an iterator to the next place we want to insert a |
| 68 | /// block. |
| 69 | Function::iterator InsertPos; |
| 70 | |
| 71 | /// PlaceBlocks - Recursively place the specified blocks and any unplaced |
| 72 | /// successors. |
| 73 | void PlaceBlocks(BasicBlock *BB); |
| 74 | }; |
| 75 | |
Devang Patel | 3e15bf3 | 2007-05-02 21:39:20 +0000 | [diff] [blame] | 76 | const char BlockPlacement::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 77 | RegisterPass<BlockPlacement> X("block-placement", |
| 78 | "Profile Guided Basic Block Placement"); |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Cohen | bf65268 | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 81 | FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); } |
| 82 | |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 83 | bool BlockPlacement::runOnFunction(Function &F) { |
| 84 | PI = &getAnalysis<ProfileInfo>(); |
| 85 | |
| 86 | NumMovedBlocks = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 87 | InsertPos = F.begin(); |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 88 | |
| 89 | // Recursively place all blocks. |
| 90 | PlaceBlocks(F.begin()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 92 | PlacedBlocks.clear(); |
| 93 | NumMoved += NumMovedBlocks; |
| 94 | return NumMovedBlocks != 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /// PlaceBlocks - Recursively place the specified blocks and any unplaced |
| 99 | /// successors. |
| 100 | void BlockPlacement::PlaceBlocks(BasicBlock *BB) { |
| 101 | assert(!PlacedBlocks.count(BB) && "Already placed this block!"); |
| 102 | PlacedBlocks.insert(BB); |
| 103 | |
| 104 | // Place the specified block. |
| 105 | if (&*InsertPos != BB) { |
| 106 | // Use splice to move the block into the right place. This avoids having to |
| 107 | // remove the block from the function then readd it, which causes a bunch of |
| 108 | // symbol table traffic that is entirely pointless. |
| 109 | Function::BasicBlockListType &Blocks = BB->getParent()->getBasicBlockList(); |
| 110 | Blocks.splice(InsertPos, Blocks, BB); |
| 111 | |
| 112 | ++NumMovedBlocks; |
| 113 | } else { |
| 114 | // This block is already in the right place, we don't have to do anything. |
| 115 | ++InsertPos; |
| 116 | } |
| 117 | |
| 118 | // Keep placing successors until we run out of ones to place. Note that this |
| 119 | // loop is very inefficient (N^2) for blocks with many successors, like switch |
| 120 | // statements. FIXME! |
| 121 | while (1) { |
| 122 | // Okay, now place any unplaced successors. |
| 123 | succ_iterator SI = succ_begin(BB), E = succ_end(BB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 125 | // Scan for the first unplaced successor. |
| 126 | for (; SI != E && PlacedBlocks.count(*SI); ++SI) |
| 127 | /*empty*/; |
| 128 | if (SI == E) return; // No more successors to place. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 206805e | 2004-02-11 04:53:20 +0000 | [diff] [blame] | 130 | unsigned MaxExecutionCount = PI->getExecutionCount(*SI); |
| 131 | BasicBlock *MaxSuccessor = *SI; |
| 132 | |
| 133 | // Scan for more frequently executed successors |
| 134 | for (; SI != E; ++SI) |
| 135 | if (!PlacedBlocks.count(*SI)) { |
| 136 | unsigned Count = PI->getExecutionCount(*SI); |
| 137 | if (Count > MaxExecutionCount || |
| 138 | // Prefer to not disturb the code. |
| 139 | (Count == MaxExecutionCount && *SI == &*InsertPos)) { |
| 140 | MaxExecutionCount = Count; |
| 141 | MaxSuccessor = *SI; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Now that we picked the maximally executed successor, place it. |
| 146 | PlaceBlocks(MaxSuccessor); |
| 147 | } |
| 148 | } |