blob: 1ba81598f05d22ac144ec20fdca84d6388ccc54c [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Chris Lattner0e5f4992006-12-19 21:40:18 +000029#define DEBUG_TYPE "block-placement"
Chris Lattner206805e2004-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 Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/Statistic.h"
Jeff Cohenbf652682005-01-08 17:21:40 +000035#include "llvm/Transforms/Scalar.h"
Chris Lattner206805e2004-02-11 04:53:20 +000036#include <set>
37using namespace llvm;
38
Chris Lattner0e5f4992006-12-19 21:40:18 +000039STATISTIC(NumMoved, "Number of basic blocks moved");
Misha Brukmanfd939082005-04-21 23:48:37 +000040
Chris Lattner0e5f4992006-12-19 21:40:18 +000041namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000042 struct BlockPlacement : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000043 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000044 BlockPlacement() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000045
Chris Lattner206805e2004-02-11 04:53:20 +000046 virtual bool runOnFunction(Function &F);
47
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesCFG();
50 AU.addRequired<ProfileInfo>();
51 //AU.addPreserved<ProfileInfo>(); // Does this work?
52 }
53 private:
54 /// PI - The profile information that is guiding us.
55 ///
56 ProfileInfo *PI;
57
58 /// NumMovedBlocks - Every time we move a block, increment this counter.
59 ///
60 unsigned NumMovedBlocks;
61
62 /// PlacedBlocks - Every time we place a block, remember it so we don't get
63 /// into infinite loops.
64 std::set<BasicBlock*> PlacedBlocks;
65
66 /// InsertPos - This an iterator to the next place we want to insert a
67 /// block.
68 Function::iterator InsertPos;
69
70 /// PlaceBlocks - Recursively place the specified blocks and any unplaced
71 /// successors.
72 void PlaceBlocks(BasicBlock *BB);
73 };
Chris Lattner206805e2004-02-11 04:53:20 +000074}
75
Dan Gohman844731a2008-05-13 00:00:25 +000076char BlockPlacement::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000077INITIALIZE_PASS_BEGIN(BlockPlacement, "block-placement",
78 "Profile Guided Basic Block Placement", false, false)
79INITIALIZE_AG_DEPENDENCY(ProfileInfo)
80INITIALIZE_PASS_END(BlockPlacement, "block-placement",
Owen Andersonce665bd2010-10-07 22:25:06 +000081 "Profile Guided Basic Block Placement", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000082
Jeff Cohenbf652682005-01-08 17:21:40 +000083FunctionPass *llvm::createBlockPlacementPass() { return new BlockPlacement(); }
84
Chris Lattner206805e2004-02-11 04:53:20 +000085bool BlockPlacement::runOnFunction(Function &F) {
86 PI = &getAnalysis<ProfileInfo>();
87
88 NumMovedBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000089 InsertPos = F.begin();
Chris Lattner206805e2004-02-11 04:53:20 +000090
91 // Recursively place all blocks.
92 PlaceBlocks(F.begin());
Misha Brukmanfd939082005-04-21 23:48:37 +000093
Chris Lattner206805e2004-02-11 04:53:20 +000094 PlacedBlocks.clear();
95 NumMoved += NumMovedBlocks;
96 return NumMovedBlocks != 0;
97}
98
99
100/// PlaceBlocks - Recursively place the specified blocks and any unplaced
101/// successors.
102void BlockPlacement::PlaceBlocks(BasicBlock *BB) {
103 assert(!PlacedBlocks.count(BB) && "Already placed this block!");
104 PlacedBlocks.insert(BB);
105
106 // Place the specified block.
107 if (&*InsertPos != BB) {
108 // Use splice to move the block into the right place. This avoids having to
109 // remove the block from the function then readd it, which causes a bunch of
110 // symbol table traffic that is entirely pointless.
111 Function::BasicBlockListType &Blocks = BB->getParent()->getBasicBlockList();
112 Blocks.splice(InsertPos, Blocks, BB);
113
114 ++NumMovedBlocks;
115 } else {
116 // This block is already in the right place, we don't have to do anything.
117 ++InsertPos;
118 }
119
120 // Keep placing successors until we run out of ones to place. Note that this
121 // loop is very inefficient (N^2) for blocks with many successors, like switch
122 // statements. FIXME!
123 while (1) {
124 // Okay, now place any unplaced successors.
125 succ_iterator SI = succ_begin(BB), E = succ_end(BB);
Misha Brukmanfd939082005-04-21 23:48:37 +0000126
Chris Lattner206805e2004-02-11 04:53:20 +0000127 // Scan for the first unplaced successor.
128 for (; SI != E && PlacedBlocks.count(*SI); ++SI)
129 /*empty*/;
130 if (SI == E) return; // No more successors to place.
Misha Brukmanfd939082005-04-21 23:48:37 +0000131
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000132 double MaxExecutionCount = PI->getExecutionCount(*SI);
Chris Lattner206805e2004-02-11 04:53:20 +0000133 BasicBlock *MaxSuccessor = *SI;
134
135 // Scan for more frequently executed successors
136 for (; SI != E; ++SI)
137 if (!PlacedBlocks.count(*SI)) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000138 double Count = PI->getExecutionCount(*SI);
Chris Lattner206805e2004-02-11 04:53:20 +0000139 if (Count > MaxExecutionCount ||
140 // Prefer to not disturb the code.
141 (Count == MaxExecutionCount && *SI == &*InsertPos)) {
142 MaxExecutionCount = Count;
143 MaxSuccessor = *SI;
144 }
145 }
146
147 // Now that we picked the maximally executed successor, place it.
148 PlaceBlocks(MaxSuccessor);
149 }
150}