xref: /aosp_15_r20/external/llvm/lib/CodeGen/SpillPlacement.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- SpillPlacement.cpp - Optimal Spill Code Placement -----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the spill code placement analysis.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // Each edge bundle corresponds to a node in a Hopfield network. Constraints on
13*9880d681SAndroid Build Coastguard Worker // basic blocks are weighted by the block frequency and added to become the node
14*9880d681SAndroid Build Coastguard Worker // bias.
15*9880d681SAndroid Build Coastguard Worker //
16*9880d681SAndroid Build Coastguard Worker // Transparent basic blocks have the variable live through, but don't care if it
17*9880d681SAndroid Build Coastguard Worker // is spilled or in a register. These blocks become connections in the Hopfield
18*9880d681SAndroid Build Coastguard Worker // network, again weighted by block frequency.
19*9880d681SAndroid Build Coastguard Worker //
20*9880d681SAndroid Build Coastguard Worker // The Hopfield network minimizes (possibly locally) its energy function:
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker //   E = -sum_n V_n * ( B_n + sum_{n, m linked by b} V_m * F_b )
23*9880d681SAndroid Build Coastguard Worker //
24*9880d681SAndroid Build Coastguard Worker // The energy function represents the expected spill code execution frequency,
25*9880d681SAndroid Build Coastguard Worker // or the cost of spilling. This is a Lyapunov function which never increases
26*9880d681SAndroid Build Coastguard Worker // when a node is updated. It is guaranteed to converge to a local minimum.
27*9880d681SAndroid Build Coastguard Worker //
28*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker #include "SpillPlacement.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/BitVector.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/EdgeBundles.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker using namespace llvm;
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "spillplacement"
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker char SpillPlacement::ID = 0;
46*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(SpillPlacement, "spill-code-placement",
47*9880d681SAndroid Build Coastguard Worker                       "Spill Code Placement Analysis", true, true)
48*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
49*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
50*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(SpillPlacement, "spill-code-placement",
51*9880d681SAndroid Build Coastguard Worker                     "Spill Code Placement Analysis", true, true)
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker char &llvm::SpillPlacementID = SpillPlacement::ID;
54*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const55*9880d681SAndroid Build Coastguard Worker void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
56*9880d681SAndroid Build Coastguard Worker   AU.setPreservesAll();
57*9880d681SAndroid Build Coastguard Worker   AU.addRequired<MachineBlockFrequencyInfo>();
58*9880d681SAndroid Build Coastguard Worker   AU.addRequiredTransitive<EdgeBundles>();
59*9880d681SAndroid Build Coastguard Worker   AU.addRequiredTransitive<MachineLoopInfo>();
60*9880d681SAndroid Build Coastguard Worker   MachineFunctionPass::getAnalysisUsage(AU);
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker /// Node - Each edge bundle corresponds to a Hopfield node.
64*9880d681SAndroid Build Coastguard Worker ///
65*9880d681SAndroid Build Coastguard Worker /// The node contains precomputed frequency data that only depends on the CFG,
66*9880d681SAndroid Build Coastguard Worker /// but Bias and Links are computed each time placeSpills is called.
67*9880d681SAndroid Build Coastguard Worker ///
68*9880d681SAndroid Build Coastguard Worker /// The node Value is positive when the variable should be in a register. The
69*9880d681SAndroid Build Coastguard Worker /// value can change when linked nodes change, but convergence is very fast
70*9880d681SAndroid Build Coastguard Worker /// because all weights are positive.
71*9880d681SAndroid Build Coastguard Worker ///
72*9880d681SAndroid Build Coastguard Worker struct SpillPlacement::Node {
73*9880d681SAndroid Build Coastguard Worker   /// BiasN - Sum of blocks that prefer a spill.
74*9880d681SAndroid Build Coastguard Worker   BlockFrequency BiasN;
75*9880d681SAndroid Build Coastguard Worker   /// BiasP - Sum of blocks that prefer a register.
76*9880d681SAndroid Build Coastguard Worker   BlockFrequency BiasP;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   /// Value - Output value of this node computed from the Bias and links.
79*9880d681SAndroid Build Coastguard Worker   /// This is always on of the values {-1, 0, 1}. A positive number means the
80*9880d681SAndroid Build Coastguard Worker   /// variable should go in a register through this bundle.
81*9880d681SAndroid Build Coastguard Worker   int Value;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   typedef SmallVector<std::pair<BlockFrequency, unsigned>, 4> LinkVector;
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker   /// Links - (Weight, BundleNo) for all transparent blocks connecting to other
86*9880d681SAndroid Build Coastguard Worker   /// bundles. The weights are all positive block frequencies.
87*9880d681SAndroid Build Coastguard Worker   LinkVector Links;
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker   /// SumLinkWeights - Cached sum of the weights of all links + ThresHold.
90*9880d681SAndroid Build Coastguard Worker   BlockFrequency SumLinkWeights;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   /// preferReg - Return true when this node prefers to be in a register.
preferRegSpillPlacement::Node93*9880d681SAndroid Build Coastguard Worker   bool preferReg() const {
94*9880d681SAndroid Build Coastguard Worker     // Undecided nodes (Value==0) go on the stack.
95*9880d681SAndroid Build Coastguard Worker     return Value > 0;
96*9880d681SAndroid Build Coastguard Worker   }
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   /// mustSpill - Return True if this node is so biased that it must spill.
mustSpillSpillPlacement::Node99*9880d681SAndroid Build Coastguard Worker   bool mustSpill() const {
100*9880d681SAndroid Build Coastguard Worker     // We must spill if Bias < -sum(weights) or the MustSpill flag was set.
101*9880d681SAndroid Build Coastguard Worker     // BiasN is saturated when MustSpill is set, make sure this still returns
102*9880d681SAndroid Build Coastguard Worker     // true when the RHS saturates. Note that SumLinkWeights includes Threshold.
103*9880d681SAndroid Build Coastguard Worker     return BiasN >= BiasP + SumLinkWeights;
104*9880d681SAndroid Build Coastguard Worker   }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   /// clear - Reset per-query data, but preserve frequencies that only depend on
107*9880d681SAndroid Build Coastguard Worker   // the CFG.
clearSpillPlacement::Node108*9880d681SAndroid Build Coastguard Worker   void clear(const BlockFrequency &Threshold) {
109*9880d681SAndroid Build Coastguard Worker     BiasN = BiasP = Value = 0;
110*9880d681SAndroid Build Coastguard Worker     SumLinkWeights = Threshold;
111*9880d681SAndroid Build Coastguard Worker     Links.clear();
112*9880d681SAndroid Build Coastguard Worker   }
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker   /// addLink - Add a link to bundle b with weight w.
addLinkSpillPlacement::Node115*9880d681SAndroid Build Coastguard Worker   void addLink(unsigned b, BlockFrequency w) {
116*9880d681SAndroid Build Coastguard Worker     // Update cached sum.
117*9880d681SAndroid Build Coastguard Worker     SumLinkWeights += w;
118*9880d681SAndroid Build Coastguard Worker 
119*9880d681SAndroid Build Coastguard Worker     // There can be multiple links to the same bundle, add them up.
120*9880d681SAndroid Build Coastguard Worker     for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I)
121*9880d681SAndroid Build Coastguard Worker       if (I->second == b) {
122*9880d681SAndroid Build Coastguard Worker         I->first += w;
123*9880d681SAndroid Build Coastguard Worker         return;
124*9880d681SAndroid Build Coastguard Worker       }
125*9880d681SAndroid Build Coastguard Worker     // This must be the first link to b.
126*9880d681SAndroid Build Coastguard Worker     Links.push_back(std::make_pair(w, b));
127*9880d681SAndroid Build Coastguard Worker   }
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker   /// addBias - Bias this node.
addBiasSpillPlacement::Node130*9880d681SAndroid Build Coastguard Worker   void addBias(BlockFrequency freq, BorderConstraint direction) {
131*9880d681SAndroid Build Coastguard Worker     switch (direction) {
132*9880d681SAndroid Build Coastguard Worker     default:
133*9880d681SAndroid Build Coastguard Worker       break;
134*9880d681SAndroid Build Coastguard Worker     case PrefReg:
135*9880d681SAndroid Build Coastguard Worker       BiasP += freq;
136*9880d681SAndroid Build Coastguard Worker       break;
137*9880d681SAndroid Build Coastguard Worker     case PrefSpill:
138*9880d681SAndroid Build Coastguard Worker       BiasN += freq;
139*9880d681SAndroid Build Coastguard Worker       break;
140*9880d681SAndroid Build Coastguard Worker     case MustSpill:
141*9880d681SAndroid Build Coastguard Worker       BiasN = BlockFrequency::getMaxFrequency();
142*9880d681SAndroid Build Coastguard Worker       break;
143*9880d681SAndroid Build Coastguard Worker     }
144*9880d681SAndroid Build Coastguard Worker   }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker   /// update - Recompute Value from Bias and Links. Return true when node
147*9880d681SAndroid Build Coastguard Worker   /// preference changes.
updateSpillPlacement::Node148*9880d681SAndroid Build Coastguard Worker   bool update(const Node nodes[], const BlockFrequency &Threshold) {
149*9880d681SAndroid Build Coastguard Worker     // Compute the weighted sum of inputs.
150*9880d681SAndroid Build Coastguard Worker     BlockFrequency SumN = BiasN;
151*9880d681SAndroid Build Coastguard Worker     BlockFrequency SumP = BiasP;
152*9880d681SAndroid Build Coastguard Worker     for (LinkVector::iterator I = Links.begin(), E = Links.end(); I != E; ++I) {
153*9880d681SAndroid Build Coastguard Worker       if (nodes[I->second].Value == -1)
154*9880d681SAndroid Build Coastguard Worker         SumN += I->first;
155*9880d681SAndroid Build Coastguard Worker       else if (nodes[I->second].Value == 1)
156*9880d681SAndroid Build Coastguard Worker         SumP += I->first;
157*9880d681SAndroid Build Coastguard Worker     }
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker     // Each weighted sum is going to be less than the total frequency of the
160*9880d681SAndroid Build Coastguard Worker     // bundle. Ideally, we should simply set Value = sign(SumP - SumN), but we
161*9880d681SAndroid Build Coastguard Worker     // will add a dead zone around 0 for two reasons:
162*9880d681SAndroid Build Coastguard Worker     //
163*9880d681SAndroid Build Coastguard Worker     //  1. It avoids arbitrary bias when all links are 0 as is possible during
164*9880d681SAndroid Build Coastguard Worker     //     initial iterations.
165*9880d681SAndroid Build Coastguard Worker     //  2. It helps tame rounding errors when the links nominally sum to 0.
166*9880d681SAndroid Build Coastguard Worker     //
167*9880d681SAndroid Build Coastguard Worker     bool Before = preferReg();
168*9880d681SAndroid Build Coastguard Worker     if (SumN >= SumP + Threshold)
169*9880d681SAndroid Build Coastguard Worker       Value = -1;
170*9880d681SAndroid Build Coastguard Worker     else if (SumP >= SumN + Threshold)
171*9880d681SAndroid Build Coastguard Worker       Value = 1;
172*9880d681SAndroid Build Coastguard Worker     else
173*9880d681SAndroid Build Coastguard Worker       Value = 0;
174*9880d681SAndroid Build Coastguard Worker     return Before != preferReg();
175*9880d681SAndroid Build Coastguard Worker   }
176*9880d681SAndroid Build Coastguard Worker 
getDissentingNeighborsSpillPlacement::Node177*9880d681SAndroid Build Coastguard Worker   void getDissentingNeighbors(SparseSet<unsigned> &List,
178*9880d681SAndroid Build Coastguard Worker                               const Node nodes[]) const {
179*9880d681SAndroid Build Coastguard Worker     for (const auto &Elt : Links) {
180*9880d681SAndroid Build Coastguard Worker       unsigned n = Elt.second;
181*9880d681SAndroid Build Coastguard Worker       // Neighbors that already have the same value are not going to
182*9880d681SAndroid Build Coastguard Worker       // change because of this node changing.
183*9880d681SAndroid Build Coastguard Worker       if (Value != nodes[n].Value)
184*9880d681SAndroid Build Coastguard Worker         List.insert(n);
185*9880d681SAndroid Build Coastguard Worker     }
186*9880d681SAndroid Build Coastguard Worker   }
187*9880d681SAndroid Build Coastguard Worker };
188*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & mf)189*9880d681SAndroid Build Coastguard Worker bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
190*9880d681SAndroid Build Coastguard Worker   MF = &mf;
191*9880d681SAndroid Build Coastguard Worker   bundles = &getAnalysis<EdgeBundles>();
192*9880d681SAndroid Build Coastguard Worker   loops = &getAnalysis<MachineLoopInfo>();
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker   assert(!nodes && "Leaking node array");
195*9880d681SAndroid Build Coastguard Worker   nodes = new Node[bundles->getNumBundles()];
196*9880d681SAndroid Build Coastguard Worker   TodoList.clear();
197*9880d681SAndroid Build Coastguard Worker   TodoList.setUniverse(bundles->getNumBundles());
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker   // Compute total ingoing and outgoing block frequencies for all bundles.
200*9880d681SAndroid Build Coastguard Worker   BlockFrequencies.resize(mf.getNumBlockIDs());
201*9880d681SAndroid Build Coastguard Worker   MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
202*9880d681SAndroid Build Coastguard Worker   setThreshold(MBFI->getEntryFreq());
203*9880d681SAndroid Build Coastguard Worker   for (auto &I : mf) {
204*9880d681SAndroid Build Coastguard Worker     unsigned Num = I.getNumber();
205*9880d681SAndroid Build Coastguard Worker     BlockFrequencies[Num] = MBFI->getBlockFreq(&I);
206*9880d681SAndroid Build Coastguard Worker   }
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker   // We never change the function.
209*9880d681SAndroid Build Coastguard Worker   return false;
210*9880d681SAndroid Build Coastguard Worker }
211*9880d681SAndroid Build Coastguard Worker 
releaseMemory()212*9880d681SAndroid Build Coastguard Worker void SpillPlacement::releaseMemory() {
213*9880d681SAndroid Build Coastguard Worker   delete[] nodes;
214*9880d681SAndroid Build Coastguard Worker   nodes = nullptr;
215*9880d681SAndroid Build Coastguard Worker   TodoList.clear();
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker 
218*9880d681SAndroid Build Coastguard Worker /// activate - mark node n as active if it wasn't already.
activate(unsigned n)219*9880d681SAndroid Build Coastguard Worker void SpillPlacement::activate(unsigned n) {
220*9880d681SAndroid Build Coastguard Worker   TodoList.insert(n);
221*9880d681SAndroid Build Coastguard Worker   if (ActiveNodes->test(n))
222*9880d681SAndroid Build Coastguard Worker     return;
223*9880d681SAndroid Build Coastguard Worker   ActiveNodes->set(n);
224*9880d681SAndroid Build Coastguard Worker   nodes[n].clear(Threshold);
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker   // Very large bundles usually come from big switches, indirect branches,
227*9880d681SAndroid Build Coastguard Worker   // landing pads, or loops with many 'continue' statements. It is difficult to
228*9880d681SAndroid Build Coastguard Worker   // allocate registers when so many different blocks are involved.
229*9880d681SAndroid Build Coastguard Worker   //
230*9880d681SAndroid Build Coastguard Worker   // Give a small negative bias to large bundles such that a substantial
231*9880d681SAndroid Build Coastguard Worker   // fraction of the connected blocks need to be interested before we consider
232*9880d681SAndroid Build Coastguard Worker   // expanding the region through the bundle. This helps compile time by
233*9880d681SAndroid Build Coastguard Worker   // limiting the number of blocks visited and the number of links in the
234*9880d681SAndroid Build Coastguard Worker   // Hopfield network.
235*9880d681SAndroid Build Coastguard Worker   if (bundles->getBlocks(n).size() > 100) {
236*9880d681SAndroid Build Coastguard Worker     nodes[n].BiasP = 0;
237*9880d681SAndroid Build Coastguard Worker     nodes[n].BiasN = (MBFI->getEntryFreq() / 16);
238*9880d681SAndroid Build Coastguard Worker   }
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker /// \brief Set the threshold for a given entry frequency.
242*9880d681SAndroid Build Coastguard Worker ///
243*9880d681SAndroid Build Coastguard Worker /// Set the threshold relative to \c Entry.  Since the threshold is used as a
244*9880d681SAndroid Build Coastguard Worker /// bound on the open interval (-Threshold;Threshold), 1 is the minimum
245*9880d681SAndroid Build Coastguard Worker /// threshold.
setThreshold(const BlockFrequency & Entry)246*9880d681SAndroid Build Coastguard Worker void SpillPlacement::setThreshold(const BlockFrequency &Entry) {
247*9880d681SAndroid Build Coastguard Worker   // Apparently 2 is a good threshold when Entry==2^14, but we need to scale
248*9880d681SAndroid Build Coastguard Worker   // it.  Divide by 2^13, rounding as appropriate.
249*9880d681SAndroid Build Coastguard Worker   uint64_t Freq = Entry.getFrequency();
250*9880d681SAndroid Build Coastguard Worker   uint64_t Scaled = (Freq >> 13) + bool(Freq & (1 << 12));
251*9880d681SAndroid Build Coastguard Worker   Threshold = std::max(UINT64_C(1), Scaled);
252*9880d681SAndroid Build Coastguard Worker }
253*9880d681SAndroid Build Coastguard Worker 
254*9880d681SAndroid Build Coastguard Worker /// addConstraints - Compute node biases and weights from a set of constraints.
255*9880d681SAndroid Build Coastguard Worker /// Set a bit in NodeMask for each active node.
addConstraints(ArrayRef<BlockConstraint> LiveBlocks)256*9880d681SAndroid Build Coastguard Worker void SpillPlacement::addConstraints(ArrayRef<BlockConstraint> LiveBlocks) {
257*9880d681SAndroid Build Coastguard Worker   for (ArrayRef<BlockConstraint>::iterator I = LiveBlocks.begin(),
258*9880d681SAndroid Build Coastguard Worker        E = LiveBlocks.end(); I != E; ++I) {
259*9880d681SAndroid Build Coastguard Worker     BlockFrequency Freq = BlockFrequencies[I->Number];
260*9880d681SAndroid Build Coastguard Worker 
261*9880d681SAndroid Build Coastguard Worker     // Live-in to block?
262*9880d681SAndroid Build Coastguard Worker     if (I->Entry != DontCare) {
263*9880d681SAndroid Build Coastguard Worker       unsigned ib = bundles->getBundle(I->Number, 0);
264*9880d681SAndroid Build Coastguard Worker       activate(ib);
265*9880d681SAndroid Build Coastguard Worker       nodes[ib].addBias(Freq, I->Entry);
266*9880d681SAndroid Build Coastguard Worker     }
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker     // Live-out from block?
269*9880d681SAndroid Build Coastguard Worker     if (I->Exit != DontCare) {
270*9880d681SAndroid Build Coastguard Worker       unsigned ob = bundles->getBundle(I->Number, 1);
271*9880d681SAndroid Build Coastguard Worker       activate(ob);
272*9880d681SAndroid Build Coastguard Worker       nodes[ob].addBias(Freq, I->Exit);
273*9880d681SAndroid Build Coastguard Worker     }
274*9880d681SAndroid Build Coastguard Worker   }
275*9880d681SAndroid Build Coastguard Worker }
276*9880d681SAndroid Build Coastguard Worker 
277*9880d681SAndroid Build Coastguard Worker /// addPrefSpill - Same as addConstraints(PrefSpill)
addPrefSpill(ArrayRef<unsigned> Blocks,bool Strong)278*9880d681SAndroid Build Coastguard Worker void SpillPlacement::addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong) {
279*9880d681SAndroid Build Coastguard Worker   for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
280*9880d681SAndroid Build Coastguard Worker        I != E; ++I) {
281*9880d681SAndroid Build Coastguard Worker     BlockFrequency Freq = BlockFrequencies[*I];
282*9880d681SAndroid Build Coastguard Worker     if (Strong)
283*9880d681SAndroid Build Coastguard Worker       Freq += Freq;
284*9880d681SAndroid Build Coastguard Worker     unsigned ib = bundles->getBundle(*I, 0);
285*9880d681SAndroid Build Coastguard Worker     unsigned ob = bundles->getBundle(*I, 1);
286*9880d681SAndroid Build Coastguard Worker     activate(ib);
287*9880d681SAndroid Build Coastguard Worker     activate(ob);
288*9880d681SAndroid Build Coastguard Worker     nodes[ib].addBias(Freq, PrefSpill);
289*9880d681SAndroid Build Coastguard Worker     nodes[ob].addBias(Freq, PrefSpill);
290*9880d681SAndroid Build Coastguard Worker   }
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker 
addLinks(ArrayRef<unsigned> Links)293*9880d681SAndroid Build Coastguard Worker void SpillPlacement::addLinks(ArrayRef<unsigned> Links) {
294*9880d681SAndroid Build Coastguard Worker   for (ArrayRef<unsigned>::iterator I = Links.begin(), E = Links.end(); I != E;
295*9880d681SAndroid Build Coastguard Worker        ++I) {
296*9880d681SAndroid Build Coastguard Worker     unsigned Number = *I;
297*9880d681SAndroid Build Coastguard Worker     unsigned ib = bundles->getBundle(Number, 0);
298*9880d681SAndroid Build Coastguard Worker     unsigned ob = bundles->getBundle(Number, 1);
299*9880d681SAndroid Build Coastguard Worker 
300*9880d681SAndroid Build Coastguard Worker     // Ignore self-loops.
301*9880d681SAndroid Build Coastguard Worker     if (ib == ob)
302*9880d681SAndroid Build Coastguard Worker       continue;
303*9880d681SAndroid Build Coastguard Worker     activate(ib);
304*9880d681SAndroid Build Coastguard Worker     activate(ob);
305*9880d681SAndroid Build Coastguard Worker     BlockFrequency Freq = BlockFrequencies[Number];
306*9880d681SAndroid Build Coastguard Worker     nodes[ib].addLink(ob, Freq);
307*9880d681SAndroid Build Coastguard Worker     nodes[ob].addLink(ib, Freq);
308*9880d681SAndroid Build Coastguard Worker   }
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker 
scanActiveBundles()311*9880d681SAndroid Build Coastguard Worker bool SpillPlacement::scanActiveBundles() {
312*9880d681SAndroid Build Coastguard Worker   RecentPositive.clear();
313*9880d681SAndroid Build Coastguard Worker   for (int n = ActiveNodes->find_first(); n>=0; n = ActiveNodes->find_next(n)) {
314*9880d681SAndroid Build Coastguard Worker     update(n);
315*9880d681SAndroid Build Coastguard Worker     // A node that must spill, or a node without any links is not going to
316*9880d681SAndroid Build Coastguard Worker     // change its value ever again, so exclude it from iterations.
317*9880d681SAndroid Build Coastguard Worker     if (nodes[n].mustSpill())
318*9880d681SAndroid Build Coastguard Worker       continue;
319*9880d681SAndroid Build Coastguard Worker     if (nodes[n].preferReg())
320*9880d681SAndroid Build Coastguard Worker       RecentPositive.push_back(n);
321*9880d681SAndroid Build Coastguard Worker   }
322*9880d681SAndroid Build Coastguard Worker   return !RecentPositive.empty();
323*9880d681SAndroid Build Coastguard Worker }
324*9880d681SAndroid Build Coastguard Worker 
update(unsigned n)325*9880d681SAndroid Build Coastguard Worker bool SpillPlacement::update(unsigned n) {
326*9880d681SAndroid Build Coastguard Worker   if (!nodes[n].update(nodes, Threshold))
327*9880d681SAndroid Build Coastguard Worker     return false;
328*9880d681SAndroid Build Coastguard Worker   nodes[n].getDissentingNeighbors(TodoList, nodes);
329*9880d681SAndroid Build Coastguard Worker   return true;
330*9880d681SAndroid Build Coastguard Worker }
331*9880d681SAndroid Build Coastguard Worker 
332*9880d681SAndroid Build Coastguard Worker /// iterate - Repeatedly update the Hopfield nodes until stability or the
333*9880d681SAndroid Build Coastguard Worker /// maximum number of iterations is reached.
iterate()334*9880d681SAndroid Build Coastguard Worker void SpillPlacement::iterate() {
335*9880d681SAndroid Build Coastguard Worker   // We do not need to push those node in the todolist.
336*9880d681SAndroid Build Coastguard Worker   // They are already been proceeded as part of the previous iteration.
337*9880d681SAndroid Build Coastguard Worker   RecentPositive.clear();
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker   // Since the last iteration, the todolist have been augmented by calls
340*9880d681SAndroid Build Coastguard Worker   // to addConstraints, addLinks, and co.
341*9880d681SAndroid Build Coastguard Worker   // Update the network energy starting at this new frontier.
342*9880d681SAndroid Build Coastguard Worker   // The call to ::update will add the nodes that changed into the todolist.
343*9880d681SAndroid Build Coastguard Worker   unsigned Limit = bundles->getNumBundles() * 10;
344*9880d681SAndroid Build Coastguard Worker   while(Limit-- > 0 && !TodoList.empty()) {
345*9880d681SAndroid Build Coastguard Worker     unsigned n = TodoList.pop_back_val();
346*9880d681SAndroid Build Coastguard Worker     if (!update(n))
347*9880d681SAndroid Build Coastguard Worker       continue;
348*9880d681SAndroid Build Coastguard Worker     if (nodes[n].preferReg())
349*9880d681SAndroid Build Coastguard Worker       RecentPositive.push_back(n);
350*9880d681SAndroid Build Coastguard Worker   }
351*9880d681SAndroid Build Coastguard Worker }
352*9880d681SAndroid Build Coastguard Worker 
prepare(BitVector & RegBundles)353*9880d681SAndroid Build Coastguard Worker void SpillPlacement::prepare(BitVector &RegBundles) {
354*9880d681SAndroid Build Coastguard Worker   RecentPositive.clear();
355*9880d681SAndroid Build Coastguard Worker   TodoList.clear();
356*9880d681SAndroid Build Coastguard Worker   // Reuse RegBundles as our ActiveNodes vector.
357*9880d681SAndroid Build Coastguard Worker   ActiveNodes = &RegBundles;
358*9880d681SAndroid Build Coastguard Worker   ActiveNodes->clear();
359*9880d681SAndroid Build Coastguard Worker   ActiveNodes->resize(bundles->getNumBundles());
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker 
362*9880d681SAndroid Build Coastguard Worker bool
finish()363*9880d681SAndroid Build Coastguard Worker SpillPlacement::finish() {
364*9880d681SAndroid Build Coastguard Worker   assert(ActiveNodes && "Call prepare() first");
365*9880d681SAndroid Build Coastguard Worker 
366*9880d681SAndroid Build Coastguard Worker   // Write preferences back to ActiveNodes.
367*9880d681SAndroid Build Coastguard Worker   bool Perfect = true;
368*9880d681SAndroid Build Coastguard Worker   for (int n = ActiveNodes->find_first(); n>=0; n = ActiveNodes->find_next(n))
369*9880d681SAndroid Build Coastguard Worker     if (!nodes[n].preferReg()) {
370*9880d681SAndroid Build Coastguard Worker       ActiveNodes->reset(n);
371*9880d681SAndroid Build Coastguard Worker       Perfect = false;
372*9880d681SAndroid Build Coastguard Worker     }
373*9880d681SAndroid Build Coastguard Worker   ActiveNodes = nullptr;
374*9880d681SAndroid Build Coastguard Worker   return Perfect;
375*9880d681SAndroid Build Coastguard Worker }
376