blob: 020b2b2c52e543261522240660cfd62109febff0 [file] [log] [blame]
Chris Lattner5add0512004-02-11 04:53:20 +00001//===-- BasicBlockPlacement.cpp - Basic Block Code Layout optimization ----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner5add0512004-02-11 04:53:20 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner5add0512004-02-11 04:53:20 +00008//===----------------------------------------------------------------------===//
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 Lattner79a42ac2006-12-19 21:40:18 +000029#define DEBUG_TYPE "block-placement"
Chris Lattner5add0512004-02-11 04:53:20 +000030#include "llvm/Analysis/ProfileInfo.h"
31#include "llvm/Function.h"
32#include "llvm/Pass.h"
33#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/ADT/Statistic.h"
Jeff Cohen677babc2005-01-08 17:21:40 +000035#include "llvm/Transforms/Scalar.h"
Chris Lattner5add0512004-02-11 04:53:20 +000036#include <set>
37using namespace llvm;
38
Chris Lattner79a42ac2006-12-19 21:40:18 +000039STATISTIC(NumMoved, "Number of basic blocks moved");
Misha Brukmanb1c93172005-04-21 23:48:37 +000040
Chris Lattner79a42ac2006-12-19 21:40:18 +000041namespace {
Chris Lattner5add0512004-02-11 04:53:20 +000042 struct BlockPlacement : public FunctionPass {
43 virtual bool runOnFunction(Function &F);
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesCFG();
47 AU.addRequired<ProfileInfo>();
48 //AU.addPreserved<ProfileInfo>(); // Does this work?
49 }
50 private:
51 /// PI - The profile information that is guiding us.
52 ///
53 ProfileInfo *PI;
54
55 /// NumMovedBlocks - Every time we move a block, increment this counter.
56 ///
57 unsigned NumMovedBlocks;
58
59 /// PlacedBlocks - Every time we place a block, remember it so we don't get
60 /// into infinite loops.
61 std::set<BasicBlock*> PlacedBlocks;
62
63 /// InsertPos - This an iterator to the next place we want to insert a
64 /// block.
65 Function::iterator InsertPos;
66
67 /// PlaceBlocks - Recursively place the specified blocks and any unplaced
68 /// successors.
69 void PlaceBlocks(BasicBlock *BB);
70 };
71
Chris Lattnerc2d3d312006-08-27 22:42:52 +000072 RegisterPass<BlockPlacement> X("block-placement",
73 "Profile Guided Basic Block Placement");
Chris Lattner5add0512004-02-11 04:53:20 +000074}
75
Jeff Cohen677babc2005-01-08 17:21:40 +000076FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); }
77
Chris Lattner5add0512004-02-11 04:53:20 +000078bool BlockPlacement::runOnFunction(Function &F) {
79 PI = &getAnalysis<ProfileInfo>();
80
81 NumMovedBlocks = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000082 InsertPos = F.begin();
Chris Lattner5add0512004-02-11 04:53:20 +000083
84 // Recursively place all blocks.
85 PlaceBlocks(F.begin());
Misha Brukmanb1c93172005-04-21 23:48:37 +000086
Chris Lattner5add0512004-02-11 04:53:20 +000087 PlacedBlocks.clear();
88 NumMoved += NumMovedBlocks;
89 return NumMovedBlocks != 0;
90}
91
92
93/// PlaceBlocks - Recursively place the specified blocks and any unplaced
94/// successors.
95void BlockPlacement::PlaceBlocks(BasicBlock *BB) {
96 assert(!PlacedBlocks.count(BB) && "Already placed this block!");
97 PlacedBlocks.insert(BB);
98
99 // Place the specified block.
100 if (&*InsertPos != BB) {
101 // Use splice to move the block into the right place. This avoids having to
102 // remove the block from the function then readd it, which causes a bunch of
103 // symbol table traffic that is entirely pointless.
104 Function::BasicBlockListType &Blocks = BB->getParent()->getBasicBlockList();
105 Blocks.splice(InsertPos, Blocks, BB);
106
107 ++NumMovedBlocks;
108 } else {
109 // This block is already in the right place, we don't have to do anything.
110 ++InsertPos;
111 }
112
113 // Keep placing successors until we run out of ones to place. Note that this
114 // loop is very inefficient (N^2) for blocks with many successors, like switch
115 // statements. FIXME!
116 while (1) {
117 // Okay, now place any unplaced successors.
118 succ_iterator SI = succ_begin(BB), E = succ_end(BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000119
Chris Lattner5add0512004-02-11 04:53:20 +0000120 // Scan for the first unplaced successor.
121 for (; SI != E && PlacedBlocks.count(*SI); ++SI)
122 /*empty*/;
123 if (SI == E) return; // No more successors to place.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000124
Chris Lattner5add0512004-02-11 04:53:20 +0000125 unsigned MaxExecutionCount = PI->getExecutionCount(*SI);
126 BasicBlock *MaxSuccessor = *SI;
127
128 // Scan for more frequently executed successors
129 for (; SI != E; ++SI)
130 if (!PlacedBlocks.count(*SI)) {
131 unsigned Count = PI->getExecutionCount(*SI);
132 if (Count > MaxExecutionCount ||
133 // Prefer to not disturb the code.
134 (Count == MaxExecutionCount && *SI == &*InsertPos)) {
135 MaxExecutionCount = Count;
136 MaxSuccessor = *SI;
137 }
138 }
139
140 // Now that we picked the maximally executed successor, place it.
141 PlaceBlocks(MaxSuccessor);
142 }
143}