blob: c926c561d8216891f54a3c9e76e7ae469ca95e51 [file] [log] [blame]
Chris Lattner206805e2004-02-11 04:53:20 +00001//===-- BasicBlockPlacement.cpp - Basic Block Code Layout optimization ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattner206805e2004-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattner206805e2004-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
29#include "llvm/Analysis/ProfileInfo.h"
30#include "llvm/Function.h"
31#include "llvm/Pass.h"
32#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/Statistic.h"
Jeff Cohenbf652682005-01-08 17:21:40 +000034#include "llvm/Transforms/Scalar.h"
Chris Lattner206805e2004-02-11 04:53:20 +000035#include <set>
36using namespace llvm;
37
38namespace {
39 Statistic<> NumMoved("block-placement", "Number of basic blocks moved");
Misha Brukmanfd939082005-04-21 23:48:37 +000040
Chris Lattner206805e2004-02-11 04:53:20 +000041 struct BlockPlacement : public FunctionPass {
42 virtual bool runOnFunction(Function &F);
43
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
54 /// NumMovedBlocks - Every time we move a block, increment this counter.
55 ///
56 unsigned NumMovedBlocks;
57
58 /// PlacedBlocks - Every time we place a block, remember it so we don't get
59 /// into infinite loops.
60 std::set<BasicBlock*> PlacedBlocks;
61
62 /// InsertPos - This an iterator to the next place we want to insert a
63 /// block.
64 Function::iterator InsertPos;
65
66 /// PlaceBlocks - Recursively place the specified blocks and any unplaced
67 /// successors.
68 void PlaceBlocks(BasicBlock *BB);
69 };
70
71 RegisterOpt<BlockPlacement> X("block-placement",
72 "Profile Guided Basic Block Placement");
73}
74
Jeff Cohenbf652682005-01-08 17:21:40 +000075FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); }
76
Chris Lattner206805e2004-02-11 04:53:20 +000077bool BlockPlacement::runOnFunction(Function &F) {
78 PI = &getAnalysis<ProfileInfo>();
79
80 NumMovedBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000081 InsertPos = F.begin();
Chris Lattner206805e2004-02-11 04:53:20 +000082
83 // Recursively place all blocks.
84 PlaceBlocks(F.begin());
Misha Brukmanfd939082005-04-21 23:48:37 +000085
Chris Lattner206805e2004-02-11 04:53:20 +000086 PlacedBlocks.clear();
87 NumMoved += NumMovedBlocks;
88 return NumMovedBlocks != 0;
89}
90
91
92/// PlaceBlocks - Recursively place the specified blocks and any unplaced
93/// successors.
94void BlockPlacement::PlaceBlocks(BasicBlock *BB) {
95 assert(!PlacedBlocks.count(BB) && "Already placed this block!");
96 PlacedBlocks.insert(BB);
97
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);
105
106 ++NumMovedBlocks;
107 } else {
108 // This block is already in the right place, we don't have to do anything.
109 ++InsertPos;
110 }
111
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!
115 while (1) {
116 // Okay, now place any unplaced successors.
117 succ_iterator SI = succ_begin(BB), E = succ_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +0000118
Chris Lattner206805e2004-02-11 04:53:20 +0000119 // Scan for the first unplaced successor.
120 for (; SI != E && PlacedBlocks.count(*SI); ++SI)
121 /*empty*/;
122 if (SI == E) return; // No more successors to place.
Misha Brukmanfd939082005-04-21 23:48:37 +0000123
Chris Lattner206805e2004-02-11 04:53:20 +0000124 unsigned MaxExecutionCount = PI->getExecutionCount(*SI);
125 BasicBlock *MaxSuccessor = *SI;
126
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;
135 MaxSuccessor = *SI;
136 }
137 }
138
139 // Now that we picked the maximally executed successor, place it.
140 PlaceBlocks(MaxSuccessor);
141 }
142}