blob: 24e94d11f88881b78b3c8211ef95a8c2b59d89d1 [file] [log] [blame]
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +00001//===-- SpillPlacement.cpp - Optimal Spill Code Placement -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the spill code placement analysis.
11//
12// Each edge bundle corresponds to a node in a Hopfield network. Constraints on
13// basic blocks are weighted by the block frequency and added to become the node
14// bias.
15//
16// Transparent basic blocks have the variable live through, but don't care if it
17// is spilled or in a register. These blocks become connections in the Hopfield
18// network, again weighted by block frequency.
19//
20// The Hopfield network minimizes (possibly locally) its energy function:
21//
22// E = -sum_n V_n * ( B_n + sum_{n, m linked by b} V_m * F_b )
23//
24// The energy function represents the expected spill code execution frequency,
25// or the cost of spilling. This is a Lyapunov function which never increases
26// when a node is updated. It is guaranteed to converge to a local minimum.
27//
28//===----------------------------------------------------------------------===//
29
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000030#include "SpillPlacement.h"
Jakub Staszakf31034d2013-03-18 23:45:45 +000031#include "llvm/ADT/BitVector.h"
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000032#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000034#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000035#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineLoopInfo.h"
37#include "llvm/CodeGen/Passes.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/Format.h"
40
41using namespace llvm;
42
Stephen Hinesdce4a402014-05-29 02:49:00 -070043#define DEBUG_TYPE "spillplacement"
44
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000045char SpillPlacement::ID = 0;
46INITIALIZE_PASS_BEGIN(SpillPlacement, "spill-code-placement",
47 "Spill Code Placement Analysis", true, true)
48INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
49INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
50INITIALIZE_PASS_END(SpillPlacement, "spill-code-placement",
51 "Spill Code Placement Analysis", true, true)
52
53char &llvm::SpillPlacementID = SpillPlacement::ID;
54
55void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
Benjamin Kramer4eed7562013-06-17 19:00:36 +000057 AU.addRequired<MachineBlockFrequencyInfo>();
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000058 AU.addRequiredTransitive<EdgeBundles>();
59 AU.addRequiredTransitive<MachineLoopInfo>();
60 MachineFunctionPass::getAnalysisUsage(AU);
61}
62
Stephen Hinesdce4a402014-05-29 02:49:00 -070063namespace {
64static BlockFrequency Threshold;
65}
66
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +000067/// Decision threshold. A node gets the output value 0 if the weighted sum of
68/// its inputs falls in the open interval (-Threshold;Threshold).
Stephen Hinesdce4a402014-05-29 02:49:00 -070069static BlockFrequency getThreshold() { return Threshold; }
70
71/// \brief Set the threshold for a given entry frequency.
72///
73/// Set the threshold relative to \c Entry. Since the threshold is used as a
74/// bound on the open interval (-Threshold;Threshold), 1 is the minimum
75/// threshold.
76static void setThreshold(const BlockFrequency &Entry) {
77 // Apparently 2 is a good threshold when Entry==2^14, but we need to scale
78 // it. Divide by 2^13, rounding as appropriate.
79 uint64_t Freq = Entry.getFrequency();
80 uint64_t Scaled = (Freq >> 13) + bool(Freq & (1 << 12));
81 Threshold = std::max(UINT64_C(1), Scaled);
82}
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +000083
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000084/// Node - Each edge bundle corresponds to a Hopfield node.
85///
86/// The node contains precomputed frequency data that only depends on the CFG,
87/// but Bias and Links are computed each time placeSpills is called.
88///
89/// The node Value is positive when the variable should be in a register. The
90/// value can change when linked nodes change, but convergence is very fast
91/// because all weights are positive.
92///
93struct SpillPlacement::Node {
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +000094 /// BiasN - Sum of blocks that prefer a spill.
95 BlockFrequency BiasN;
96 /// BiasP - Sum of blocks that prefer a register.
97 BlockFrequency BiasP;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +000098
99 /// Value - Output value of this node computed from the Bias and links.
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000100 /// This is always on of the values {-1, 0, 1}. A positive number means the
101 /// variable should go in a register through this bundle.
102 int Value;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000103
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000104 typedef SmallVector<std::pair<BlockFrequency, unsigned>, 4> LinkVector;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000105
106 /// Links - (Weight, BundleNo) for all transparent blocks connecting to other
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000107 /// bundles. The weights are all positive block frequencies.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000108 LinkVector Links;
109
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000110 /// SumLinkWeights - Cached sum of the weights of all links + ThresHold.
111 BlockFrequency SumLinkWeights;
112
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000113 /// preferReg - Return true when this node prefers to be in a register.
114 bool preferReg() const {
115 // Undecided nodes (Value==0) go on the stack.
116 return Value > 0;
117 }
118
119 /// mustSpill - Return True if this node is so biased that it must spill.
120 bool mustSpill() const {
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000121 // We must spill if Bias < -sum(weights) or the MustSpill flag was set.
122 // BiasN is saturated when MustSpill is set, make sure this still returns
123 // true when the RHS saturates. Note that SumLinkWeights includes Threshold.
124 return BiasN >= BiasP + SumLinkWeights;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000125 }
126
127 /// clear - Reset per-query data, but preserve frequencies that only depend on
128 // the CFG.
129 void clear() {
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000130 BiasN = BiasP = Value = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700131 SumLinkWeights = getThreshold();
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000132 Links.clear();
133 }
134
135 /// addLink - Add a link to bundle b with weight w.
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000136 void addLink(unsigned b, BlockFrequency w) {
137 // Update cached sum.
138 SumLinkWeights += w;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000139
140 // There can be multiple links to the same bundle, add them up.
141 for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I)
142 if (I->second == b) {
143 I->first += w;
144 return;
145 }
146 // This must be the first link to b.
147 Links.push_back(std::make_pair(w, b));
148 }
149
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000150 /// addBias - Bias this node.
151 void addBias(BlockFrequency freq, BorderConstraint direction) {
152 switch (direction) {
153 default:
154 break;
155 case PrefReg:
156 BiasP += freq;
157 break;
158 case PrefSpill:
159 BiasN += freq;
160 break;
161 case MustSpill:
162 BiasN = BlockFrequency::getMaxFrequency();
163 break;
164 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000165 }
166
167 /// update - Recompute Value from Bias and Links. Return true when node
168 /// preference changes.
169 bool update(const Node nodes[]) {
170 // Compute the weighted sum of inputs.
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000171 BlockFrequency SumN = BiasN;
172 BlockFrequency SumP = BiasP;
173 for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I) {
174 if (nodes[I->second].Value == -1)
175 SumN += I->first;
176 else if (nodes[I->second].Value == 1)
177 SumP += I->first;
178 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000179
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000180 // Each weighted sum is going to be less than the total frequency of the
181 // bundle. Ideally, we should simply set Value = sign(SumP - SumN), but we
182 // will add a dead zone around 0 for two reasons:
183 //
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000184 // 1. It avoids arbitrary bias when all links are 0 as is possible during
185 // initial iterations.
186 // 2. It helps tame rounding errors when the links nominally sum to 0.
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000187 //
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000188 bool Before = preferReg();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700189 if (SumN >= SumP + getThreshold())
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000190 Value = -1;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700191 else if (SumP >= SumN + getThreshold())
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000192 Value = 1;
193 else
194 Value = 0;
195 return Before != preferReg();
196 }
197};
198
199bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
200 MF = &mf;
201 bundles = &getAnalysis<EdgeBundles>();
202 loops = &getAnalysis<MachineLoopInfo>();
203
204 assert(!nodes && "Leaking node array");
205 nodes = new Node[bundles->getNumBundles()];
206
207 // Compute total ingoing and outgoing block frequencies for all bundles.
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000208 BlockFrequencies.resize(mf.getNumBlockIDs());
Stephen Hines36b56882014-04-23 16:57:46 -0700209 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700210 setThreshold(MBFI->getEntryFreq());
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000211 for (MachineFunction::iterator I = mf.begin(), E = mf.end(); I != E; ++I) {
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000212 unsigned Num = I->getNumber();
Stephen Hines36b56882014-04-23 16:57:46 -0700213 BlockFrequencies[Num] = MBFI->getBlockFreq(I);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000214 }
215
216 // We never change the function.
217 return false;
218}
219
220void SpillPlacement::releaseMemory() {
221 delete[] nodes;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700222 nodes = nullptr;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000223}
224
225/// activate - mark node n as active if it wasn't already.
226void SpillPlacement::activate(unsigned n) {
227 if (ActiveNodes->test(n))
228 return;
229 ActiveNodes->set(n);
230 nodes[n].clear();
Jakob Stoklund Olesen1dc12aa2012-05-21 03:11:23 +0000231
232 // Very large bundles usually come from big switches, indirect branches,
233 // landing pads, or loops with many 'continue' statements. It is difficult to
234 // allocate registers when so many different blocks are involved.
235 //
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000236 // Give a small negative bias to large bundles such that a substantial
237 // fraction of the connected blocks need to be interested before we consider
238 // expanding the region through the bundle. This helps compile time by
239 // limiting the number of blocks visited and the number of links in the
240 // Hopfield network.
241 if (bundles->getBlocks(n).size() > 100) {
242 nodes[n].BiasP = 0;
Stephen Hines36b56882014-04-23 16:57:46 -0700243 nodes[n].BiasN = (MBFI->getEntryFreq() / 16);
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000244 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000245}
246
247
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000248/// addConstraints - Compute node biases and weights from a set of constraints.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000249/// Set a bit in NodeMask for each active node.
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000250void SpillPlacement::addConstraints(ArrayRef<BlockConstraint> LiveBlocks) {
251 for (ArrayRef<BlockConstraint>::iterator I = LiveBlocks.begin(),
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000252 E = LiveBlocks.end(); I != E; ++I) {
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000253 BlockFrequency Freq = BlockFrequencies[I->Number];
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000254
255 // Live-in to block?
256 if (I->Entry != DontCare) {
257 unsigned ib = bundles->getBundle(I->Number, 0);
258 activate(ib);
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000259 nodes[ib].addBias(Freq, I->Entry);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000260 }
261
262 // Live-out from block?
263 if (I->Exit != DontCare) {
264 unsigned ob = bundles->getBundle(I->Number, 1);
265 activate(ob);
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000266 nodes[ob].addBias(Freq, I->Exit);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000267 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000268 }
269}
270
Jakob Stoklund Olesene60f1032011-07-23 03:10:19 +0000271/// addPrefSpill - Same as addConstraints(PrefSpill)
Jakob Stoklund Olesenb87f91b2011-08-03 23:09:38 +0000272void SpillPlacement::addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong) {
Jakob Stoklund Olesene60f1032011-07-23 03:10:19 +0000273 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
274 I != E; ++I) {
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000275 BlockFrequency Freq = BlockFrequencies[*I];
Jakob Stoklund Olesenb87f91b2011-08-03 23:09:38 +0000276 if (Strong)
277 Freq += Freq;
Jakob Stoklund Olesene60f1032011-07-23 03:10:19 +0000278 unsigned ib = bundles->getBundle(*I, 0);
279 unsigned ob = bundles->getBundle(*I, 1);
280 activate(ib);
281 activate(ob);
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000282 nodes[ib].addBias(Freq, PrefSpill);
283 nodes[ob].addBias(Freq, PrefSpill);
Jakob Stoklund Olesene60f1032011-07-23 03:10:19 +0000284 }
285}
286
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000287void SpillPlacement::addLinks(ArrayRef<unsigned> Links) {
288 for (ArrayRef<unsigned>::iterator I = Links.begin(), E = Links.end(); I != E;
289 ++I) {
290 unsigned Number = *I;
291 unsigned ib = bundles->getBundle(Number, 0);
292 unsigned ob = bundles->getBundle(Number, 1);
293
294 // Ignore self-loops.
295 if (ib == ob)
296 continue;
297 activate(ib);
298 activate(ob);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000299 if (nodes[ib].Links.empty() && !nodes[ib].mustSpill())
300 Linked.push_back(ib);
301 if (nodes[ob].Links.empty() && !nodes[ob].mustSpill())
302 Linked.push_back(ob);
Jakob Stoklund Olesen6d9fe792013-07-16 18:26:15 +0000303 BlockFrequency Freq = BlockFrequencies[Number];
304 nodes[ib].addLink(ob, Freq);
305 nodes[ob].addLink(ib, Freq);
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000306 }
307}
308
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000309bool SpillPlacement::scanActiveBundles() {
310 Linked.clear();
311 RecentPositive.clear();
312 for (int n = ActiveNodes->find_first(); n>=0; n = ActiveNodes->find_next(n)) {
313 nodes[n].update(nodes);
314 // A node that must spill, or a node without any links is not going to
315 // change its value ever again, so exclude it from iterations.
316 if (nodes[n].mustSpill())
317 continue;
318 if (!nodes[n].Links.empty())
319 Linked.push_back(n);
320 if (nodes[n].preferReg())
321 RecentPositive.push_back(n);
322 }
323 return !RecentPositive.empty();
324}
325
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000326/// iterate - Repeatedly update the Hopfield nodes until stability or the
327/// maximum number of iterations is reached.
328/// @param Linked - Numbers of linked nodes that need updating.
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000329void SpillPlacement::iterate() {
330 // First update the recently positive nodes. They have likely received new
331 // negative bias that will turn them off.
332 while (!RecentPositive.empty())
333 nodes[RecentPositive.pop_back_val()].update(nodes);
334
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000335 if (Linked.empty())
336 return;
337
338 // Run up to 10 iterations. The edge bundle numbering is closely related to
339 // basic block numbering, so there is a strong tendency towards chains of
340 // linked nodes with sequential numbers. By scanning the linked nodes
341 // backwards and forwards, we make it very likely that a single node can
342 // affect the entire network in a single iteration. That means very fast
343 // convergence, usually in a single iteration.
344 for (unsigned iteration = 0; iteration != 10; ++iteration) {
Stephen Hines36b56882014-04-23 16:57:46 -0700345 // Scan backwards, skipping the last node when iteration is not zero. When
346 // iteration is not zero, the last node was just updated.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000347 bool Changed = false;
348 for (SmallVectorImpl<unsigned>::const_reverse_iterator I =
Stephen Hines36b56882014-04-23 16:57:46 -0700349 iteration == 0 ? Linked.rbegin() : std::next(Linked.rbegin()),
350 E = Linked.rend(); I != E; ++I) {
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000351 unsigned n = *I;
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000352 if (nodes[n].update(nodes)) {
353 Changed = true;
354 if (nodes[n].preferReg())
355 RecentPositive.push_back(n);
356 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000357 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000358 if (!Changed || !RecentPositive.empty())
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000359 return;
360
361 // Scan forwards, skipping the first node which was just updated.
362 Changed = false;
363 for (SmallVectorImpl<unsigned>::const_iterator I =
Stephen Hines36b56882014-04-23 16:57:46 -0700364 std::next(Linked.begin()), E = Linked.end(); I != E; ++I) {
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000365 unsigned n = *I;
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000366 if (nodes[n].update(nodes)) {
367 Changed = true;
368 if (nodes[n].preferReg())
369 RecentPositive.push_back(n);
370 }
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000371 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000372 if (!Changed || !RecentPositive.empty())
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000373 return;
374 }
375}
376
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000377void SpillPlacement::prepare(BitVector &RegBundles) {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000378 Linked.clear();
379 RecentPositive.clear();
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000380 // Reuse RegBundles as our ActiveNodes vector.
381 ActiveNodes = &RegBundles;
382 ActiveNodes->clear();
383 ActiveNodes->resize(bundles->getNumBundles());
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000384}
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000385
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000386bool
387SpillPlacement::finish() {
388 assert(ActiveNodes && "Call prepare() first");
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000389
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000390 // Write preferences back to ActiveNodes.
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000391 bool Perfect = true;
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000392 for (int n = ActiveNodes->find_first(); n>=0; n = ActiveNodes->find_next(n))
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000393 if (!nodes[n].preferReg()) {
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +0000394 ActiveNodes->reset(n);
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000395 Perfect = false;
396 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700397 ActiveNodes = nullptr;
Jakob Stoklund Olesen8bfe5082011-01-06 01:21:53 +0000398 return Perfect;
399}