1 //===-- BasicBlockPlacement.cpp - Basic Block Code Layout optimization ----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
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
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.
27 //===----------------------------------------------------------------------===//
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/ADT/Statistic.h"
34 #include "llvm/Transforms/Scalar.h"
39 Statistic<> NumMoved("block-placement", "Number of basic blocks moved");
41 struct BlockPlacement : public FunctionPass {
42 virtual bool runOnFunction(Function &F);
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<ProfileInfo>();
47 //AU.addPreserved<ProfileInfo>(); // Does this work?
50 /// PI - The profile information that is guiding us.
54 /// NumMovedBlocks - Every time we move a block, increment this counter.
56 unsigned NumMovedBlocks;
58 /// PlacedBlocks - Every time we place a block, remember it so we don't get
59 /// into infinite loops.
60 std::set<BasicBlock*> PlacedBlocks;
62 /// InsertPos - This an iterator to the next place we want to insert a
64 Function::iterator InsertPos;
66 /// PlaceBlocks - Recursively place the specified blocks and any unplaced
68 void PlaceBlocks(BasicBlock *BB);
71 RegisterOpt<BlockPlacement> X("block-placement",
72 "Profile Guided Basic Block Placement");
75 FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); }
77 bool BlockPlacement::runOnFunction(Function &F) {
78 PI = &getAnalysis<ProfileInfo>();
81 InsertPos = F.begin();
83 // Recursively place all blocks.
84 PlaceBlocks(F.begin());
87 NumMoved += NumMovedBlocks;
88 return NumMovedBlocks != 0;
92 /// PlaceBlocks - Recursively place the specified blocks and any unplaced
94 void BlockPlacement::PlaceBlocks(BasicBlock *BB) {
95 assert(!PlacedBlocks.count(BB) && "Already placed this block!");
96 PlacedBlocks.insert(BB);
98 // Place the specified block.
99 if (&*InsertPos != BB) {
100 // Use splice to move the block into the right place. This avoids having to
101 // remove the block from the function then readd it, which causes a bunch of
102 // symbol table traffic that is entirely pointless.
103 Function::BasicBlockListType &Blocks = BB->getParent()->getBasicBlockList();
104 Blocks.splice(InsertPos, Blocks, BB);
108 // This block is already in the right place, we don't have to do anything.
112 // Keep placing successors until we run out of ones to place. Note that this
113 // loop is very inefficient (N^2) for blocks with many successors, like switch
114 // statements. FIXME!
116 // Okay, now place any unplaced successors.
117 succ_iterator SI = succ_begin(BB), E = succ_end(BB);
119 // Scan for the first unplaced successor.
120 for (; SI != E && PlacedBlocks.count(*SI); ++SI)
122 if (SI == E) return; // No more successors to place.
124 unsigned MaxExecutionCount = PI->getExecutionCount(*SI);
125 BasicBlock *MaxSuccessor = *SI;
127 // Scan for more frequently executed successors
128 for (; SI != E; ++SI)
129 if (!PlacedBlocks.count(*SI)) {
130 unsigned Count = PI->getExecutionCount(*SI);
131 if (Count > MaxExecutionCount ||
132 // Prefer to not disturb the code.
133 (Count == MaxExecutionCount && *SI == &*InsertPos)) {
134 MaxExecutionCount = Count;
139 // Now that we picked the maximally executed successor, place it.
140 PlaceBlocks(MaxSuccessor);