LLVM 20.0.0git
SpillPlacement.cpp
Go to the documentation of this file.
1//===- SpillPlacement.cpp - Optimal Spill Code Placement ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the spill code placement analysis.
10//
11// Each edge bundle corresponds to a node in a Hopfield network. Constraints on
12// basic blocks are weighted by the block frequency and added to become the node
13// bias.
14//
15// Transparent basic blocks have the variable live through, but don't care if it
16// is spilled or in a register. These blocks become connections in the Hopfield
17// network, again weighted by block frequency.
18//
19// The Hopfield network minimizes (possibly locally) its energy function:
20//
21// E = -sum_n V_n * ( B_n + sum_{n, m linked by b} V_m * F_b )
22//
23// The energy function represents the expected spill code execution frequency,
24// or the cost of spilling. This is a Lyapunov function which never increases
25// when a node is updated. It is guaranteed to converge to a local minimum.
26//
27//===----------------------------------------------------------------------===//
28
30#include "llvm/ADT/BitVector.h"
35#include "llvm/CodeGen/Passes.h"
37#include "llvm/Pass.h"
38#include <algorithm>
39#include <cassert>
40#include <cstdint>
41#include <utility>
42
43using namespace llvm;
44
45#define DEBUG_TYPE "spill-code-placement"
46
48
50
52 "Spill Code Placement Analysis", true, true)
55 "Spill Code Placement Analysis", true, true)
56
57void SpillPlacementWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesAll();
60 AU.addRequiredTransitive<EdgeBundlesWrapperLegacy>();
62}
63
64/// Node - Each edge bundle corresponds to a Hopfield node.
65///
66/// The node contains precomputed frequency data that only depends on the CFG,
67/// but Bias and Links are computed each time placeSpills is called.
68///
69/// The node Value is positive when the variable should be in a register. The
70/// value can change when linked nodes change, but convergence is very fast
71/// because all weights are positive.
73 /// BiasN - Sum of blocks that prefer a spill.
75
76 /// BiasP - Sum of blocks that prefer a register.
78
79 /// Value - Output value of this node computed from the Bias and links.
80 /// This is always on of the values {-1, 0, 1}. A positive number means the
81 /// variable should go in a register through this bundle.
82 int Value;
83
85
86 /// Links - (Weight, BundleNo) for all transparent blocks connecting to other
87 /// bundles. The weights are all positive block frequencies.
89
90 /// SumLinkWeights - Cached sum of the weights of all links + ThresHold.
92
93 /// preferReg - Return true when this node prefers to be in a register.
94 bool preferReg() const {
95 // Undecided nodes (Value==0) go on the stack.
96 return Value > 0;
97 }
98
99 /// mustSpill - Return True if this node is so biased that it must spill.
100 bool mustSpill() const {
101 // We must spill if Bias < -sum(weights) or the MustSpill flag was set.
102 // BiasN is saturated when MustSpill is set, make sure this still returns
103 // true when the RHS saturates. Note that SumLinkWeights includes Threshold.
104 return BiasN >= BiasP + SumLinkWeights;
105 }
106
107 /// clear - Reset per-query data, but preserve frequencies that only depend on
108 /// the CFG.
109 void clear(BlockFrequency Threshold) {
112 Value = 0;
113 SumLinkWeights = Threshold;
114 Links.clear();
115 }
116
117 /// addLink - Add a link to bundle b with weight w.
118 void addLink(unsigned b, BlockFrequency w) {
119 // Update cached sum.
120 SumLinkWeights += w;
121
122 // There can be multiple links to the same bundle, add them up.
123 for (std::pair<BlockFrequency, unsigned> &L : Links)
124 if (L.second == b) {
125 L.first += w;
126 return;
127 }
128 // This must be the first link to b.
129 Links.push_back(std::make_pair(w, b));
130 }
131
132 /// addBias - Bias this node.
134 switch (direction) {
135 default:
136 break;
137 case PrefReg:
138 BiasP += freq;
139 break;
140 case PrefSpill:
141 BiasN += freq;
142 break;
143 case MustSpill:
145 break;
146 }
147 }
148
149 /// update - Recompute Value from Bias and Links. Return true when node
150 /// preference changes.
151 bool update(const Node nodes[], BlockFrequency Threshold) {
152 // Compute the weighted sum of inputs.
153 BlockFrequency SumN = BiasN;
154 BlockFrequency SumP = BiasP;
155 for (std::pair<BlockFrequency, unsigned> &L : Links) {
156 if (nodes[L.second].Value == -1)
157 SumN += L.first;
158 else if (nodes[L.second].Value == 1)
159 SumP += L.first;
160 }
161
162 // Each weighted sum is going to be less than the total frequency of the
163 // bundle. Ideally, we should simply set Value = sign(SumP - SumN), but we
164 // will add a dead zone around 0 for two reasons:
165 //
166 // 1. It avoids arbitrary bias when all links are 0 as is possible during
167 // initial iterations.
168 // 2. It helps tame rounding errors when the links nominally sum to 0.
169 //
170 bool Before = preferReg();
171 if (SumN >= SumP + Threshold)
172 Value = -1;
173 else if (SumP >= SumN + Threshold)
174 Value = 1;
175 else
176 Value = 0;
177 return Before != preferReg();
178 }
179
181 const Node nodes[]) const {
182 for (const auto &Elt : Links) {
183 unsigned n = Elt.second;
184 // Neighbors that already have the same value are not going to
185 // change because of this node changing.
186 if (Value != nodes[n].Value)
187 List.insert(n);
188 }
189 }
190};
191
192bool SpillPlacementWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
193 auto *Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
194 auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
195
196 Impl.run(MF, Bundles, MBFI);
197 return false;
198}
199
200AnalysisKey SpillPlacementAnalysis::Key;
201
205 auto *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
206 auto *MBFI = &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
207 SpillPlacement Impl;
208 Impl.run(MF, Bundles, MBFI);
209 return Impl;
210}
211
213 MachineFunction &MF, const PreservedAnalyses &PA,
215 auto PAC = PA.getChecker<SpillPlacementAnalysis>();
216 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<MachineFunction>>())
217 return true;
218 // Check dependencies.
219 return Inv.invalidate<EdgeBundlesAnalysis>(MF, PA) ||
221}
222
223SpillPlacement::SpillPlacement() = default;
225SpillPlacement::SpillPlacement(SpillPlacement &&) = default;
226
227void SpillPlacement::releaseMemory() {
228 nodes.reset();
229 TodoList.clear();
230}
231
232void SpillPlacement::run(MachineFunction &mf, EdgeBundles *Bundles,
234 MF = &mf;
235 this->bundles = Bundles;
236 this->MBFI = MBFI;
237
238 assert(!nodes && "Leaking node array");
239 nodes.reset(new Node[bundles->getNumBundles()]);
240 TodoList.clear();
241 TodoList.setUniverse(bundles->getNumBundles());
242
243 // Compute total ingoing and outgoing block frequencies for all bundles.
244 BlockFrequencies.resize(mf.getNumBlockIDs());
245 setThreshold(MBFI->getEntryFreq());
246 for (auto &I : mf) {
247 unsigned Num = I.getNumber();
248 BlockFrequencies[Num] = MBFI->getBlockFreq(&I);
249 }
250}
251
252/// activate - mark node n as active if it wasn't already.
253void SpillPlacement::activate(unsigned n) {
254 TodoList.insert(n);
255 if (ActiveNodes->test(n))
256 return;
257 ActiveNodes->set(n);
258 nodes[n].clear(Threshold);
259
260 // Very large bundles usually come from big switches, indirect branches,
261 // landing pads, or loops with many 'continue' statements. It is difficult to
262 // allocate registers when so many different blocks are involved.
263 //
264 // Give a small negative bias to large bundles such that a substantial
265 // fraction of the connected blocks need to be interested before we consider
266 // expanding the region through the bundle. This helps compile time by
267 // limiting the number of blocks visited and the number of links in the
268 // Hopfield network.
269 if (bundles->getBlocks(n).size() > 100) {
270 nodes[n].BiasP = BlockFrequency(0);
271 BlockFrequency BiasN = MBFI->getEntryFreq();
272 BiasN >>= 4;
273 nodes[n].BiasN = BiasN;
274 }
275}
276
277/// Set the threshold for a given entry frequency.
278///
279/// Set the threshold relative to \c Entry. Since the threshold is used as a
280/// bound on the open interval (-Threshold;Threshold), 1 is the minimum
281/// threshold.
282void SpillPlacement::setThreshold(BlockFrequency Entry) {
283 // Apparently 2 is a good threshold when Entry==2^14, but we need to scale
284 // it. Divide by 2^13, rounding as appropriate.
285 uint64_t Freq = Entry.getFrequency();
286 uint64_t Scaled = (Freq >> 13) + bool(Freq & (1 << 12));
287 Threshold = BlockFrequency(std::max(UINT64_C(1), Scaled));
288}
289
290/// addConstraints - Compute node biases and weights from a set of constraints.
291/// Set a bit in NodeMask for each active node.
293 for (const BlockConstraint &LB : LiveBlocks) {
294 BlockFrequency Freq = BlockFrequencies[LB.Number];
295
296 // Live-in to block?
297 if (LB.Entry != DontCare) {
298 unsigned ib = bundles->getBundle(LB.Number, false);
299 activate(ib);
300 nodes[ib].addBias(Freq, LB.Entry);
301 }
302
303 // Live-out from block?
304 if (LB.Exit != DontCare) {
305 unsigned ob = bundles->getBundle(LB.Number, true);
306 activate(ob);
307 nodes[ob].addBias(Freq, LB.Exit);
308 }
309 }
310}
311
312/// addPrefSpill - Same as addConstraints(PrefSpill)
314 for (unsigned B : Blocks) {
315 BlockFrequency Freq = BlockFrequencies[B];
316 if (Strong)
317 Freq += Freq;
318 unsigned ib = bundles->getBundle(B, false);
319 unsigned ob = bundles->getBundle(B, true);
320 activate(ib);
321 activate(ob);
322 nodes[ib].addBias(Freq, PrefSpill);
323 nodes[ob].addBias(Freq, PrefSpill);
324 }
325}
326
328 for (unsigned Number : Links) {
329 unsigned ib = bundles->getBundle(Number, false);
330 unsigned ob = bundles->getBundle(Number, true);
331
332 // Ignore self-loops.
333 if (ib == ob)
334 continue;
335 activate(ib);
336 activate(ob);
337 BlockFrequency Freq = BlockFrequencies[Number];
338 nodes[ib].addLink(ob, Freq);
339 nodes[ob].addLink(ib, Freq);
340 }
341}
342
344 RecentPositive.clear();
345 for (unsigned n : ActiveNodes->set_bits()) {
346 update(n);
347 // A node that must spill, or a node without any links is not going to
348 // change its value ever again, so exclude it from iterations.
349 if (nodes[n].mustSpill())
350 continue;
351 if (nodes[n].preferReg())
352 RecentPositive.push_back(n);
353 }
354 return !RecentPositive.empty();
355}
356
357bool SpillPlacement::update(unsigned n) {
358 if (!nodes[n].update(nodes.get(), Threshold))
359 return false;
360 nodes[n].getDissentingNeighbors(TodoList, nodes.get());
361 return true;
362}
363
364/// iterate - Repeatedly update the Hopfield nodes until stability or the
365/// maximum number of iterations is reached.
367 // We do not need to push those node in the todolist.
368 // They are already been proceeded as part of the previous iteration.
369 RecentPositive.clear();
370
371 // Since the last iteration, the todolist have been augmented by calls
372 // to addConstraints, addLinks, and co.
373 // Update the network energy starting at this new frontier.
374 // The call to ::update will add the nodes that changed into the todolist.
375 unsigned Limit = bundles->getNumBundles() * 10;
376 while(Limit-- > 0 && !TodoList.empty()) {
377 unsigned n = TodoList.pop_back_val();
378 if (!update(n))
379 continue;
380 if (nodes[n].preferReg())
381 RecentPositive.push_back(n);
382 }
383}
384
386 RecentPositive.clear();
387 TodoList.clear();
388 // Reuse RegBundles as our ActiveNodes vector.
389 ActiveNodes = &RegBundles;
390 ActiveNodes->clear();
391 ActiveNodes->resize(bundles->getNumBundles());
392}
393
394bool
396 assert(ActiveNodes && "Call prepare() first");
397
398 // Write preferences back to ActiveNodes.
399 bool Perfect = true;
400 for (unsigned n : ActiveNodes->set_bits())
401 if (!nodes[n].preferReg()) {
402 ActiveNodes->reset(n);
403 Perfect = false;
404 }
405 ActiveNodes = nullptr;
406 return Perfect;
407}
408
410 auto toString = [](BorderConstraint C) -> StringRef {
411 switch(C) {
412 case DontCare: return "DontCare";
413 case PrefReg: return "PrefReg";
414 case PrefSpill: return "PrefSpill";
415 case PrefBoth: return "PrefBoth";
416 case MustSpill: return "MustSpill";
417 };
418 llvm_unreachable("uncovered switch");
419 };
420
421 dbgs() << "{" << Number << ", "
422 << toString(Entry) << ", "
423 << toString(Exit) << ", "
424 << (ChangesValue ? "changes" : "no change") << "}";
425}
426
428 print(dbgs());
429 dbgs() << "\n";
430}
Unify divergent function exit nodes
@ Scaled
This file implements the BitVector class.
block freq
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
#define I(x, y, z)
Definition: MD5.cpp:58
Branch Probability Basic Block Placement
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Spill Code Placement true
Spill Code Placement Analysis
#define DEBUG_TYPE
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:49
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:310
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
bool test(unsigned Idx) const
Definition: BitVector.h:461
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
iterator_range< const_set_bits_iterator > set_bits() const
Definition: BitVector.h:140
static BlockFrequency max()
Returns the maximum possible frequency, the saturation value.
ArrayRef< unsigned > getBlocks(unsigned Bundle) const
getBlocks - Return an array of blocks that are connected to Bundle.
Definition: EdgeBundles.h:53
unsigned getBundle(unsigned N, bool Out) const
getBundle - Return the ingoing (Out = false) or outgoing (Out = true) bundle number for basic block N
Definition: EdgeBundles.h:47
unsigned getNumBundles() const
getNumBundles - Return the total number of bundles in the CFG.
Definition: EdgeBundles.h:50
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
BlockFrequency getEntryFreq() const
Divide a block's BlockFrequency::getFrequency() value by this value to obtain the entry block - relat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
SparseSet - Fast set implementation for objects that can be identified by small unsigned keys.
Definition: SparseSet.h:124
bool empty() const
empty - Returns true if the set is empty.
Definition: SparseSet.h:187
ValueT pop_back_val()
Definition: SparseSet.h:274
void clear()
clear - Clears the set.
Definition: SparseSet.h:198
std::pair< iterator, bool > insert(const ValueT &Val)
insert - Attempts to insert a new element.
Definition: SparseSet.h:257
void setUniverse(unsigned U)
setUniverse - Set the universe size which determines the largest key the set can hold.
Definition: SparseSet.h:160
SpillPlacement run(MachineFunction &, MachineFunctionAnalysisManager &)
void addConstraints(ArrayRef< BlockConstraint > LiveBlocks)
addConstraints - Add constraints and biases.
bool finish()
finish - Compute the optimal spill code placement given the constraints.
void addPrefSpill(ArrayRef< unsigned > Blocks, bool Strong)
addPrefSpill - Add PrefSpill constraints to all blocks listed.
void prepare(BitVector &RegBundles)
prepare - Reset state and prepare for a new spill placement computation.
bool scanActiveBundles()
scanActiveBundles - Perform an initial scan of all bundles activated by addConstraints and addLinks,...
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
void addLinks(ArrayRef< unsigned > Links)
addLinks - Add transparent blocks with the given numbers.
void iterate()
iterate - Update the network iteratively until convergence, or new bundles are found.
BorderConstraint
BorderConstraint - A basic block has separate constraints for entry and exit.
@ MustSpill
A register is impossible, variable must be spilled.
@ DontCare
Block doesn't care / variable not live.
@ PrefBoth
Block entry prefers both register and stack.
@ PrefReg
Block entry/exit prefers a register.
@ PrefSpill
Block entry/exit prefers a stack slot.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition: COFF.h:844
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
char & SpillPlacementID
SpillPlacement analysis.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
const char * toString(DWARFSectionKind Kind)
Node - Each edge bundle corresponds to a Hopfield node.
void addBias(BlockFrequency freq, BorderConstraint direction)
addBias - Bias this node.
bool preferReg() const
preferReg - Return true when this node prefers to be in a register.
bool update(const Node nodes[], BlockFrequency Threshold)
update - Recompute Value from Bias and Links.
BlockFrequency SumLinkWeights
SumLinkWeights - Cached sum of the weights of all links + ThresHold.
BlockFrequency BiasN
BiasN - Sum of blocks that prefer a spill.
void addLink(unsigned b, BlockFrequency w)
addLink - Add a link to bundle b with weight w.
LinkVector Links
Links - (Weight, BundleNo) for all transparent blocks connecting to other bundles.
int Value
Value - Output value of this node computed from the Bias and links.
BlockFrequency BiasP
BiasP - Sum of blocks that prefer a register.
void clear(BlockFrequency Threshold)
clear - Reset per-query data, but preserve frequencies that only depend on the CFG.
bool mustSpill() const
mustSpill - Return True if this node is so biased that it must spill.
void getDissentingNeighbors(SparseSet< unsigned > &List, const Node nodes[]) const
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
BlockConstraint - Entry and exit constraints for a basic block.
BorderConstraint Exit
Constraint on block exit.
void print(raw_ostream &OS) const
bool ChangesValue
True when this block changes the value of the live range.
BorderConstraint Entry
Constraint on block entry.
unsigned Number
Basic block number (from MBB::getNumber()).