blob: 1030407b766de43f748a869488dcf282eac13d26 [file] [log] [blame]
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +00001//===- BlockFrequencyImplInfo.cpp - Block Frequency Info Implementation ---===//
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// Loops should be simplified before this analysis.
11//
12//===----------------------------------------------------------------------===//
13
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000014#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000015#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/GraphTraits.h"
18#include "llvm/ADT/None.h"
Duncan P. N. Exon Smith87c40fd2014-05-06 01:57:42 +000019#include "llvm/ADT/SCCIterator.h"
Xinliang David Lib12b3532016-06-22 17:12:12 +000020#include "llvm/IR/Function.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000021#include "llvm/Support/BlockFrequency.h"
22#include "llvm/Support/BranchProbability.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ScaledNumber.h"
26#include "llvm/Support/MathExtras.h"
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000027#include "llvm/Support/raw_ostream.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000028#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <iterator>
33#include <list>
Duncan P. N. Exon Smith57cbdfc2014-12-05 19:13:42 +000034#include <numeric>
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000035#include <utility>
36#include <vector>
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000037
38using namespace llvm;
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +000039using namespace llvm::bfi_detail;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000040
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "block-freq"
42
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +000043ScaledNumber<uint64_t> BlockMass::toScaled() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000044 if (isFull())
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +000045 return ScaledNumber<uint64_t>(1, 0);
46 return ScaledNumber<uint64_t>(getMass() + 1, -64);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000047}
48
Aaron Ballman615eb472017-10-15 14:32:27 +000049#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000050LLVM_DUMP_METHOD void BlockMass::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000051#endif
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000052
53static char getHexDigit(int N) {
54 assert(N < 16);
55 if (N < 10)
56 return '0' + N;
57 return 'a' + N - 10;
58}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000059
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000060raw_ostream &BlockMass::print(raw_ostream &OS) const {
61 for (int Digits = 0; Digits < 16; ++Digits)
62 OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf);
63 return OS;
64}
65
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000066namespace {
67
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000068using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
69using Distribution = BlockFrequencyInfoImplBase::Distribution;
70using WeightList = BlockFrequencyInfoImplBase::Distribution::WeightList;
71using Scaled64 = BlockFrequencyInfoImplBase::Scaled64;
72using LoopData = BlockFrequencyInfoImplBase::LoopData;
73using Weight = BlockFrequencyInfoImplBase::Weight;
74using FrequencyData = BlockFrequencyInfoImplBase::FrequencyData;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000075
76/// \brief Dithering mass distributer.
77///
78/// This class splits up a single mass into portions by weight, dithering to
79/// spread out error. No mass is lost. The dithering precision depends on the
80/// precision of the product of \a BlockMass and \a BranchProbability.
81///
82/// The distribution algorithm follows.
83///
84/// 1. Initialize by saving the sum of the weights in \a RemWeight and the
85/// mass to distribute in \a RemMass.
86///
87/// 2. For each portion:
88///
89/// 1. Construct a branch probability, P, as the portion's weight divided
90/// by the current value of \a RemWeight.
91/// 2. Calculate the portion's mass as \a RemMass times P.
92/// 3. Update \a RemWeight and \a RemMass at each portion by subtracting
93/// the current portion's weight and mass.
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000094struct DitheringDistributer {
95 uint32_t RemWeight;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000096 BlockMass RemMass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000097
98 DitheringDistributer(Distribution &Dist, const BlockMass &Mass);
99
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000100 BlockMass takeMass(uint32_t Weight);
101};
Duncan P. N. Exon Smithb5650e52014-07-11 23:56:50 +0000102
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000103} // end anonymous namespace
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000104
105DitheringDistributer::DitheringDistributer(Distribution &Dist,
106 const BlockMass &Mass) {
107 Dist.normalize();
108 RemWeight = Dist.Total;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000109 RemMass = Mass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000110}
111
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000112BlockMass DitheringDistributer::takeMass(uint32_t Weight) {
113 assert(Weight && "invalid weight");
114 assert(Weight <= RemWeight);
115 BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight);
116
117 // Decrement totals (dither).
118 RemWeight -= Weight;
119 RemMass -= Mass;
120 return Mass;
121}
122
123void Distribution::add(const BlockNode &Node, uint64_t Amount,
124 Weight::DistType Type) {
125 assert(Amount && "invalid weight of 0");
126 uint64_t NewTotal = Total + Amount;
127
128 // Check for overflow. It should be impossible to overflow twice.
129 bool IsOverflow = NewTotal < Total;
130 assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow");
131 DidOverflow |= IsOverflow;
132
133 // Update the total.
134 Total = NewTotal;
135
136 // Save the weight.
Duncan P. N. Exon Smith60755102014-07-12 00:26:00 +0000137 Weights.push_back(Weight(Type, Node, Amount));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000138}
139
140static void combineWeight(Weight &W, const Weight &OtherW) {
141 assert(OtherW.TargetNode.isValid());
142 if (!W.Amount) {
143 W = OtherW;
144 return;
145 }
146 assert(W.Type == OtherW.Type);
147 assert(W.TargetNode == OtherW.TargetNode);
Duncan P. N. Exon Smith57cbdfc2014-12-05 19:13:42 +0000148 assert(OtherW.Amount && "Expected non-zero weight");
149 if (W.Amount > W.Amount + OtherW.Amount)
150 // Saturate on overflow.
151 W.Amount = UINT64_MAX;
152 else
153 W.Amount += OtherW.Amount;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000154}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000155
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000156static void combineWeightsBySorting(WeightList &Weights) {
157 // Sort so edges to the same node are adjacent.
158 std::sort(Weights.begin(), Weights.end(),
159 [](const Weight &L,
160 const Weight &R) { return L.TargetNode < R.TargetNode; });
161
162 // Combine adjacent edges.
163 WeightList::iterator O = Weights.begin();
164 for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E;
165 ++O, (I = L)) {
166 *O = *I;
167
168 // Find the adjacent weights to the same node.
169 for (++L; L != E && I->TargetNode == L->TargetNode; ++L)
170 combineWeight(*O, *L);
171 }
172
173 // Erase extra entries.
174 Weights.erase(O, Weights.end());
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000175}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000176
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000177static void combineWeightsByHashing(WeightList &Weights) {
178 // Collect weights into a DenseMap.
Eugene Zelenko38c02bc2017-07-21 21:37:46 +0000179 using HashTable = DenseMap<BlockNode::IndexType, Weight>;
180
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000181 HashTable Combined(NextPowerOf2(2 * Weights.size()));
182 for (const Weight &W : Weights)
183 combineWeight(Combined[W.TargetNode.Index], W);
184
185 // Check whether anything changed.
186 if (Weights.size() == Combined.size())
187 return;
188
189 // Fill in the new weights.
190 Weights.clear();
191 Weights.reserve(Combined.size());
192 for (const auto &I : Combined)
193 Weights.push_back(I.second);
194}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000195
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000196static void combineWeights(WeightList &Weights) {
197 // Use a hash table for many successors to keep this linear.
198 if (Weights.size() > 128) {
199 combineWeightsByHashing(Weights);
200 return;
201 }
202
203 combineWeightsBySorting(Weights);
204}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000205
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000206static uint64_t shiftRightAndRound(uint64_t N, int Shift) {
207 assert(Shift >= 0);
208 assert(Shift < 64);
209 if (!Shift)
210 return N;
211 return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1));
212}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000213
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000214void Distribution::normalize() {
215 // Early exit for termination nodes.
216 if (Weights.empty())
217 return;
218
219 // Only bother if there are multiple successors.
220 if (Weights.size() > 1)
221 combineWeights(Weights);
222
223 // Early exit when combined into a single successor.
224 if (Weights.size() == 1) {
225 Total = 1;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000226 Weights.front().Amount = 1;
227 return;
228 }
229
230 // Determine how much to shift right so that the total fits into 32-bits.
231 //
232 // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1
233 // for each weight can cause a 32-bit overflow.
234 int Shift = 0;
235 if (DidOverflow)
236 Shift = 33;
237 else if (Total > UINT32_MAX)
238 Shift = 33 - countLeadingZeros(Total);
239
240 // Early exit if nothing needs to be scaled.
Duncan P. N. Exon Smith57cbdfc2014-12-05 19:13:42 +0000241 if (!Shift) {
242 // If we didn't overflow then combineWeights() shouldn't have changed the
243 // sum of the weights, but let's double-check.
244 assert(Total == std::accumulate(Weights.begin(), Weights.end(), UINT64_C(0),
245 [](uint64_t Sum, const Weight &W) {
246 return Sum + W.Amount;
247 }) &&
248 "Expected total to be correct");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000249 return;
Duncan P. N. Exon Smith57cbdfc2014-12-05 19:13:42 +0000250 }
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000251
252 // Recompute the total through accumulation (rather than shifting it) so that
Duncan P. N. Exon Smith57cbdfc2014-12-05 19:13:42 +0000253 // it's accurate after shifting and any changes combineWeights() made above.
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000254 Total = 0;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000255
256 // Sum the weights to each node and shift right if necessary.
257 for (Weight &W : Weights) {
258 // Scale down below UINT32_MAX. Since Shift is larger than necessary, we
259 // can round here without concern about overflow.
260 assert(W.TargetNode.isValid());
261 W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift));
262 assert(W.Amount <= UINT32_MAX);
263
264 // Update the total.
265 Total += W.Amount;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000266 }
267 assert(Total <= UINT32_MAX);
268}
269
270void BlockFrequencyInfoImplBase::clear() {
Duncan P. N. Exon Smithdc2d66e2014-04-22 03:31:34 +0000271 // Swap with a default-constructed std::vector, since std::vector<>::clear()
272 // does not actually clear heap storage.
273 std::vector<FrequencyData>().swap(Freqs);
274 std::vector<WorkingData>().swap(Working);
Duncan P. N. Exon Smithfc7dc932014-04-25 04:30:06 +0000275 Loops.clear();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000276}
277
278/// \brief Clear all memory not needed downstream.
279///
280/// Releases all memory not used downstream. In particular, saves Freqs.
281static void cleanup(BlockFrequencyInfoImplBase &BFI) {
282 std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs));
283 BFI.clear();
284 BFI.Freqs = std::move(SavedFreqs);
285}
286
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000287bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist,
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000288 const LoopData *OuterLoop,
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000289 const BlockNode &Pred,
290 const BlockNode &Succ,
291 uint64_t Weight) {
292 if (!Weight)
293 Weight = 1;
294
Duncan P. N. Exon Smith39cc6482014-04-25 04:38:06 +0000295 auto isLoopHeader = [&OuterLoop](const BlockNode &Node) {
296 return OuterLoop && OuterLoop->isHeader(Node);
297 };
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000298
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000299 BlockNode Resolved = Working[Succ.Index].getResolvedNode();
300
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000301#ifndef NDEBUG
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000302 auto debugSuccessor = [&](const char *Type) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000303 dbgs() << " =>"
304 << " [" << Type << "] weight = " << Weight;
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000305 if (!isLoopHeader(Resolved))
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000306 dbgs() << ", succ = " << getBlockName(Succ);
307 if (Resolved != Succ)
308 dbgs() << ", resolved = " << getBlockName(Resolved);
309 dbgs() << "\n";
310 };
311 (void)debugSuccessor;
312#endif
313
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000314 if (isLoopHeader(Resolved)) {
315 DEBUG(debugSuccessor("backedge"));
Diego Novillo9a779622015-06-16 19:10:58 +0000316 Dist.addBackedge(Resolved, Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000317 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000318 }
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000319
Duncan P. N. Exon Smith39cc6482014-04-25 04:38:06 +0000320 if (Working[Resolved.Index].getContainingLoop() != OuterLoop) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000321 DEBUG(debugSuccessor(" exit "));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000322 Dist.addExit(Resolved, Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000323 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000324 }
325
Duncan P. N. Exon Smithb3380ea2014-04-22 03:31:53 +0000326 if (Resolved < Pred) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000327 if (!isLoopHeader(Pred)) {
328 // If OuterLoop is an irreducible loop, we can't actually handle this.
329 assert((!OuterLoop || !OuterLoop->isIrreducible()) &&
330 "unhandled irreducible control flow");
331
332 // Irreducible backedge. Abort.
333 DEBUG(debugSuccessor("abort!!!"));
334 return false;
335 }
336
337 // If "Pred" is a loop header, then this isn't really a backedge; rather,
338 // OuterLoop must be irreducible. These false backedges can come only from
339 // secondary loop headers.
340 assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) &&
341 "unhandled irreducible control flow");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000342 }
343
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000344 DEBUG(debugSuccessor(" local "));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000345 Dist.addLocal(Resolved, Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000346 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000347}
348
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000349bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist(
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000350 const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000351 // Copy the exit map into Dist.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000352 for (const auto &I : Loop.Exits)
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000353 if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first,
354 I.second.getMass()))
355 // Irreducible backedge.
356 return false;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000357
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000358 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000359}
360
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000361/// \brief Compute the loop scale for a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000362void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000363 // Compute loop scale.
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000364 DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000365
Diego Novilloa354f482015-04-01 17:42:27 +0000366 // Infinite loops need special handling. If we give the back edge an infinite
367 // mass, they may saturate all the other scales in the function down to 1,
368 // making all the other region temperatures look exactly the same. Choose an
369 // arbitrary scale to avoid these issues.
370 //
371 // FIXME: An alternate way would be to select a symbolic scale which is later
372 // replaced to be the maximum of all computed scales plus 1. This would
373 // appropriately describe the loop as having a large scale, without skewing
374 // the final frequency computation.
Sanjay Patel0fb98802016-05-09 16:07:45 +0000375 const Scaled64 InfiniteLoopScale(1, 12);
Diego Novilloa354f482015-04-01 17:42:27 +0000376
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000377 // LoopScale == 1 / ExitMass
378 // ExitMass == HeadMass - BackedgeMass
Diego Novillo9a779622015-06-16 19:10:58 +0000379 BlockMass TotalBackedgeMass;
380 for (auto &Mass : Loop.BackedgeMass)
381 TotalBackedgeMass += Mass;
382 BlockMass ExitMass = BlockMass::getFull() - TotalBackedgeMass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000383
Diego Novilloa354f482015-04-01 17:42:27 +0000384 // Block scale stores the inverse of the scale. If this is an infinite loop,
385 // its exit mass will be zero. In this case, use an arbitrary scale for the
386 // loop scale.
387 Loop.Scale =
Sanjay Patel0fb98802016-05-09 16:07:45 +0000388 ExitMass.isEmpty() ? InfiniteLoopScale : ExitMass.toScaled().inverse();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000389
390 DEBUG(dbgs() << " - exit-mass = " << ExitMass << " (" << BlockMass::getFull()
Diego Novillo9a779622015-06-16 19:10:58 +0000391 << " - " << TotalBackedgeMass << ")\n"
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000392 << " - scale = " << Loop.Scale << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000393}
394
395/// \brief Package up a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000396void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000397 DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n");
398
399 // Clear the subloop exits to prevent quadratic memory usage.
400 for (const BlockNode &M : Loop.Nodes) {
401 if (auto *Loop = Working[M.Index].getPackagedLoop())
402 Loop->Exits.clear();
403 DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n");
404 }
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000405 Loop.IsPackaged = true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000406}
407
Diego Novillo9a779622015-06-16 19:10:58 +0000408#ifndef NDEBUG
409static void debugAssign(const BlockFrequencyInfoImplBase &BFI,
410 const DitheringDistributer &D, const BlockNode &T,
411 const BlockMass &M, const char *Desc) {
412 dbgs() << " => assign " << M << " (" << D.RemMass << ")";
413 if (Desc)
414 dbgs() << " [" << Desc << "]";
415 if (T.isValid())
416 dbgs() << " to " << BFI.getBlockName(T);
417 dbgs() << "\n";
418}
419#endif
420
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000421void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source,
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000422 LoopData *OuterLoop,
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000423 Distribution &Dist) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000424 BlockMass Mass = Working[Source.Index].getMass();
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000425 DEBUG(dbgs() << " => mass: " << Mass << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000426
427 // Distribute mass to successors as laid out in Dist.
428 DitheringDistributer D(Dist, Mass);
429
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000430 for (const Weight &W : Dist.Weights) {
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000431 // Check for a local edge (non-backedge and non-exit).
432 BlockMass Taken = D.takeMass(W.Amount);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000433 if (W.Type == Weight::Local) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000434 Working[W.TargetNode.Index].getMass() += Taken;
Diego Novillo9a779622015-06-16 19:10:58 +0000435 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000436 continue;
437 }
438
439 // Backedges and exits only make sense if we're processing a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000440 assert(OuterLoop && "backedge or exit outside of loop");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000441
442 // Check for a backedge.
443 if (W.Type == Weight::Backedge) {
Diego Novillo8c49a572015-06-17 16:28:22 +0000444 OuterLoop->BackedgeMass[OuterLoop->getHeaderIndex(W.TargetNode)] += Taken;
Diego Novillo9a779622015-06-16 19:10:58 +0000445 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "back"));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000446 continue;
447 }
448
449 // This must be an exit.
450 assert(W.Type == Weight::Exit);
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000451 OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken));
Diego Novillo9a779622015-06-16 19:10:58 +0000452 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "exit"));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000453 }
454}
455
456static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI,
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000457 const Scaled64 &Min, const Scaled64 &Max) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000458 // Scale the Factor to a size that creates integers. Ideally, integers would
459 // be scaled so that Max == UINT64_MAX so that they can be best
Diego Novilloa354f482015-04-01 17:42:27 +0000460 // differentiated. However, in the presence of large frequency values, small
461 // frequencies are scaled down to 1, making it impossible to differentiate
462 // small, unequal numbers. When the spread between Min and Max frequencies
463 // fits well within MaxBits, we make the scale be at least 8.
464 const unsigned MaxBits = 64;
465 const unsigned SpreadBits = (Max / Min).lg();
466 Scaled64 ScalingFactor;
467 if (SpreadBits <= MaxBits - 3) {
468 // If the values are small enough, make the scaling factor at least 8 to
469 // allow distinguishing small values.
470 ScalingFactor = Min.inverse();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000471 ScalingFactor <<= 3;
Diego Novilloa354f482015-04-01 17:42:27 +0000472 } else {
473 // If the values need more than MaxBits to be represented, saturate small
474 // frequency values down to 1 by using a scaling factor that benefits large
475 // frequency values.
476 ScalingFactor = Scaled64(1, MaxBits) / Max;
477 }
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000478
479 // Translate the floats to integers.
480 DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max
481 << ", factor = " << ScalingFactor << "\n");
482 for (size_t Index = 0; Index < BFI.Freqs.size(); ++Index) {
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000483 Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000484 BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>());
485 DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = "
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000486 << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000487 << ", int = " << BFI.Freqs[Index].Integer << "\n");
488 }
489}
490
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000491/// \brief Unwrap a loop package.
492///
493/// Visits all the members of a loop, adjusting their BlockData according to
494/// the loop's pseudo-node.
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000495static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000496 DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop)
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000497 << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale
498 << "\n");
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000499 Loop.Scale *= Loop.Mass.toScaled();
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000500 Loop.IsPackaged = false;
Duncan P. N. Exon Smith3f086782014-04-25 04:38:32 +0000501 DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000502
503 // Propagate the head scale through the loop. Since members are visited in
504 // RPO, the head scale will be updated by the loop scale first, and then the
505 // final head scale will be used for updated the rest of the members.
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000506 for (const BlockNode &N : Loop.Nodes) {
507 const auto &Working = BFI.Working[N.Index];
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000508 Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale
509 : BFI.Freqs[N.Index].Scaled;
510 Scaled64 New = Loop.Scale * F;
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000511 DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => " << New
512 << "\n");
513 F = New;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000514 }
515}
516
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000517void BlockFrequencyInfoImplBase::unwrapLoops() {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000518 // Set initial frequencies from loop-local masses.
519 for (size_t Index = 0; Index < Working.size(); ++Index)
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000520 Freqs[Index].Scaled = Working[Index].Mass.toScaled();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000521
Duncan P. N. Exon Smithda0b21c2014-04-25 04:38:23 +0000522 for (LoopData &Loop : Loops)
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000523 unwrapLoop(*this, Loop);
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000524}
525
526void BlockFrequencyInfoImplBase::finalizeMetrics() {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000527 // Unwrap loop packages in reverse post-order, tracking min and max
528 // frequencies.
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000529 auto Min = Scaled64::getLargest();
530 auto Max = Scaled64::getZero();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000531 for (size_t Index = 0; Index < Working.size(); ++Index) {
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000532 // Update min/max scale.
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000533 Min = std::min(Min, Freqs[Index].Scaled);
534 Max = std::max(Max, Freqs[Index].Scaled);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000535 }
536
537 // Convert to integers.
538 convertFloatingToInteger(*this, Min, Max);
539
540 // Clean up data structures.
541 cleanup(*this);
542
543 // Print out the final stats.
544 DEBUG(dump());
545}
546
547BlockFrequency
548BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const {
549 if (!Node.isValid())
550 return 0;
551 return Freqs[Node.Index].Integer;
552}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000553
Xinliang David Lib12b3532016-06-22 17:12:12 +0000554Optional<uint64_t>
555BlockFrequencyInfoImplBase::getBlockProfileCount(const Function &F,
556 const BlockNode &Node) const {
Sean Silvaf8015752016-08-02 02:15:45 +0000557 return getProfileCountFromFreq(F, getBlockFreq(Node).getFrequency());
558}
559
560Optional<uint64_t>
561BlockFrequencyInfoImplBase::getProfileCountFromFreq(const Function &F,
562 uint64_t Freq) const {
Xinliang David Lib12b3532016-06-22 17:12:12 +0000563 auto EntryCount = F.getEntryCount();
564 if (!EntryCount)
565 return None;
566 // Use 128 bit APInt to do the arithmetic to avoid overflow.
567 APInt BlockCount(128, EntryCount.getValue());
Sean Silvaf8015752016-08-02 02:15:45 +0000568 APInt BlockFreq(128, Freq);
Xinliang David Lib12b3532016-06-22 17:12:12 +0000569 APInt EntryFreq(128, getEntryFreq());
570 BlockCount *= BlockFreq;
571 BlockCount = BlockCount.udiv(EntryFreq);
572 return BlockCount.getLimitedValue();
573}
574
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000575Scaled64
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000576BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const {
577 if (!Node.isValid())
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000578 return Scaled64::getZero();
579 return Freqs[Node.Index].Scaled;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000580}
581
Manman Ren72d44b12015-10-15 14:59:40 +0000582void BlockFrequencyInfoImplBase::setBlockFreq(const BlockNode &Node,
583 uint64_t Freq) {
584 assert(Node.isValid() && "Expected valid node");
585 assert(Node.Index < Freqs.size() && "Expected legal index");
586 Freqs[Node.Index].Integer = Freq;
587}
588
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000589std::string
590BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const {
Eugene Zelenko38c02bc2017-07-21 21:37:46 +0000591 return {};
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000592}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000593
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000594std::string
595BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
596 return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
597}
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000598
599raw_ostream &
600BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
601 const BlockNode &Node) const {
602 return OS << getFloatingBlockFreq(Node);
603}
604
605raw_ostream &
606BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
607 const BlockFrequency &Freq) const {
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000608 Scaled64 Block(Freq.getFrequency(), 0);
609 Scaled64 Entry(getEntryFreq(), 0);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000610
611 return OS << Block / Entry;
612}
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000613
614void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {
615 Start = OuterLoop.getHeader();
616 Nodes.reserve(OuterLoop.Nodes.size());
617 for (auto N : OuterLoop.Nodes)
618 addNode(N);
619 indexNodes();
620}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000621
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000622void IrreducibleGraph::addNodesInFunction() {
623 Start = 0;
624 for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index)
625 if (!BFI.Working[Index].isPackaged())
626 addNode(Index);
627 indexNodes();
628}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000629
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000630void IrreducibleGraph::indexNodes() {
631 for (auto &I : Nodes)
632 Lookup[I.Node.Index] = &I;
633}
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000634
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000635void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ,
636 const BFIBase::LoopData *OuterLoop) {
637 if (OuterLoop && OuterLoop->isHeader(Succ))
638 return;
639 auto L = Lookup.find(Succ.Index);
640 if (L == Lookup.end())
641 return;
642 IrrNode &SuccIrr = *L->second;
643 Irr.Edges.push_back(&SuccIrr);
644 SuccIrr.Edges.push_front(&Irr);
645 ++SuccIrr.NumIn;
646}
647
648namespace llvm {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000649
Eugene Zelenko38c02bc2017-07-21 21:37:46 +0000650template <> struct GraphTraits<IrreducibleGraph> {
651 using GraphT = bfi_detail::IrreducibleGraph;
652 using NodeRef = const GraphT::IrrNode *;
653 using ChildIteratorType = GraphT::IrrNode::iterator;
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000654
Tim Shenf2187ed2016-08-22 21:09:30 +0000655 static NodeRef getEntryNode(const GraphT &G) { return G.StartIrr; }
656 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
657 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000658};
Eugene Zelenko38c02bc2017-07-21 21:37:46 +0000659
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +0000660} // end namespace llvm
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000661
662/// \brief Find extra irreducible headers.
663///
664/// Find entry blocks and other blocks with backedges, which exist when \c G
665/// contains irreducible sub-SCCs.
666static void findIrreducibleHeaders(
667 const BlockFrequencyInfoImplBase &BFI,
668 const IrreducibleGraph &G,
669 const std::vector<const IrreducibleGraph::IrrNode *> &SCC,
670 LoopData::NodeList &Headers, LoopData::NodeList &Others) {
671 // Map from nodes in the SCC to whether it's an entry block.
672 SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC;
673
674 // InSCC also acts the set of nodes in the graph. Seed it.
675 for (const auto *I : SCC)
676 InSCC[I] = false;
677
678 for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) {
679 auto &Irr = *I->first;
680 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
681 if (InSCC.count(P))
682 continue;
683
684 // This is an entry block.
685 I->second = true;
686 Headers.push_back(Irr.Node);
687 DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node) << "\n");
688 break;
689 }
690 }
Duncan P. N. Exon Smitha7a90a22014-10-06 17:42:00 +0000691 assert(Headers.size() >= 2 &&
692 "Expected irreducible CFG; -loop-info is likely invalid");
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000693 if (Headers.size() == InSCC.size()) {
694 // Every block is a header.
695 std::sort(Headers.begin(), Headers.end());
696 return;
697 }
698
699 // Look for extra headers from irreducible sub-SCCs.
700 for (const auto &I : InSCC) {
701 // Entry blocks are already headers.
702 if (I.second)
703 continue;
704
705 auto &Irr = *I.first;
706 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
707 // Skip forward edges.
708 if (P->Node < Irr.Node)
709 continue;
710
711 // Skip predecessors from entry blocks. These can have inverted
712 // ordering.
713 if (InSCC.lookup(P))
714 continue;
715
716 // Store the extra header.
717 Headers.push_back(Irr.Node);
718 DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node) << "\n");
719 break;
720 }
721 if (Headers.back() == Irr.Node)
722 // Added this as a header.
723 continue;
724
725 // This is not a header.
726 Others.push_back(Irr.Node);
727 DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n");
728 }
729 std::sort(Headers.begin(), Headers.end());
730 std::sort(Others.begin(), Others.end());
731}
732
733static void createIrreducibleLoop(
734 BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G,
735 LoopData *OuterLoop, std::list<LoopData>::iterator Insert,
736 const std::vector<const IrreducibleGraph::IrrNode *> &SCC) {
737 // Translate the SCC into RPO.
738 DEBUG(dbgs() << " - found-scc\n");
739
740 LoopData::NodeList Headers;
741 LoopData::NodeList Others;
742 findIrreducibleHeaders(BFI, G, SCC, Headers, Others);
743
744 auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(),
745 Headers.end(), Others.begin(), Others.end());
746
747 // Update loop hierarchy.
748 for (const auto &N : Loop->Nodes)
749 if (BFI.Working[N.Index].isLoopHeader())
750 BFI.Working[N.Index].Loop->Parent = &*Loop;
751 else
752 BFI.Working[N.Index].Loop = &*Loop;
753}
754
755iterator_range<std::list<LoopData>::iterator>
756BlockFrequencyInfoImplBase::analyzeIrreducible(
757 const IrreducibleGraph &G, LoopData *OuterLoop,
758 std::list<LoopData>::iterator Insert) {
759 assert((OuterLoop == nullptr) == (Insert == Loops.begin()));
760 auto Prev = OuterLoop ? std::prev(Insert) : Loops.end();
761
762 for (auto I = scc_begin(G); !I.isAtEnd(); ++I) {
763 if (I->size() < 2)
764 continue;
765
766 // Translate the SCC into RPO.
767 createIrreducibleLoop(*this, G, OuterLoop, Insert, *I);
768 }
769
770 if (OuterLoop)
771 return make_range(std::next(Prev), Insert);
772 return make_range(Loops.begin(), Insert);
773}
774
775void
776BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) {
777 OuterLoop.Exits.clear();
Diego Novillo9a779622015-06-16 19:10:58 +0000778 for (auto &Mass : OuterLoop.BackedgeMass)
779 Mass = BlockMass::getEmpty();
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000780 auto O = OuterLoop.Nodes.begin() + 1;
781 for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I)
782 if (!Working[I->Index].isPackaged())
783 *O++ = *I;
784 OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end());
785}
Diego Novillo9a779622015-06-16 19:10:58 +0000786
787void BlockFrequencyInfoImplBase::adjustLoopHeaderMass(LoopData &Loop) {
788 assert(Loop.isIrreducible() && "this only makes sense on irreducible loops");
789
790 // Since the loop has more than one header block, the mass flowing back into
791 // each header will be different. Adjust the mass in each header loop to
792 // reflect the masses flowing through back edges.
793 //
794 // To do this, we distribute the initial mass using the backedge masses
795 // as weights for the distribution.
796 BlockMass LoopMass = BlockMass::getFull();
797 Distribution Dist;
798
799 DEBUG(dbgs() << "adjust-loop-header-mass:\n");
800 for (uint32_t H = 0; H < Loop.NumHeaders; ++H) {
801 auto &HeaderNode = Loop.Nodes[H];
Diego Novillo8c49a572015-06-17 16:28:22 +0000802 auto &BackedgeMass = Loop.BackedgeMass[Loop.getHeaderIndex(HeaderNode)];
Diego Novillo9a779622015-06-16 19:10:58 +0000803 DEBUG(dbgs() << " - Add back edge mass for node "
804 << getBlockName(HeaderNode) << ": " << BackedgeMass << "\n");
Diego Novillof9aa39b2015-09-08 19:22:17 +0000805 if (BackedgeMass.getMass() > 0)
806 Dist.addLocal(HeaderNode, BackedgeMass.getMass());
807 else
808 DEBUG(dbgs() << " Nothing added. Back edge mass is zero\n");
Diego Novillo9a779622015-06-16 19:10:58 +0000809 }
810
811 DitheringDistributer D(Dist, LoopMass);
812
813 DEBUG(dbgs() << " Distribute loop mass " << LoopMass
814 << " to headers using above weights\n");
815 for (const Weight &W : Dist.Weights) {
816 BlockMass Taken = D.takeMass(W.Amount);
817 assert(W.Type == Weight::Local && "all weights should be local");
818 Working[W.TargetNode.Index].getMass() = Taken;
819 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
820 }
821}