blob: d8e633c47cef573a819510dbfa5c3a26475fa2ca [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"
15#include "llvm/ADT/APFloat.h"
Duncan P. N. Exon Smith87c40fd2014-05-06 01:57:42 +000016#include "llvm/ADT/SCCIterator.h"
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000017#include "llvm/Support/raw_ostream.h"
18#include <deque>
19
20using namespace llvm;
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +000021using namespace llvm::bfi_detail;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000022
Chandler Carruth1b9dde02014-04-22 02:02:50 +000023#define DEBUG_TYPE "block-freq"
24
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000025//===----------------------------------------------------------------------===//
26//
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +000027// ScaledNumber implementation.
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000028//
29//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000030static void appendDigit(std::string &Str, unsigned D) {
31 assert(D < 10);
32 Str += '0' + D % 10;
33}
34
35static void appendNumber(std::string &Str, uint64_t N) {
36 while (N) {
37 appendDigit(Str, N % 10);
38 N /= 10;
39 }
40}
41
42static bool doesRoundUp(char Digit) {
43 switch (Digit) {
44 case '5':
45 case '6':
46 case '7':
47 case '8':
48 case '9':
49 return true;
50 default:
51 return false;
52 }
53}
54
55static std::string toStringAPFloat(uint64_t D, int E, unsigned Precision) {
Duncan P. N. Exon Smithe488c4a2014-06-24 00:15:19 +000056 assert(E >= ScaledNumbers::MinScale);
57 assert(E <= ScaledNumbers::MaxScale);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000058
Duncan P. N. Exon Smithb6bbd3f2014-06-23 23:57:12 +000059 // Find a new E, but don't let it increase past MaxScale.
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +000060 int LeadingZeros = ScaledNumberBase::countLeadingZeros64(D);
Duncan P. N. Exon Smithe488c4a2014-06-24 00:15:19 +000061 int NewE = std::min(ScaledNumbers::MaxScale, E + 63 - LeadingZeros);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000062 int Shift = 63 - (NewE - E);
63 assert(Shift <= LeadingZeros);
Duncan P. N. Exon Smithe488c4a2014-06-24 00:15:19 +000064 assert(Shift == LeadingZeros || NewE == ScaledNumbers::MaxScale);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000065 D <<= Shift;
66 E = NewE;
67
68 // Check for a denormal.
69 unsigned AdjustedE = E + 16383;
70 if (!(D >> 63)) {
Duncan P. N. Exon Smithe488c4a2014-06-24 00:15:19 +000071 assert(E == ScaledNumbers::MaxScale);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000072 AdjustedE = 0;
73 }
74
75 // Build the float and print it.
76 uint64_t RawBits[2] = {D, AdjustedE};
77 APFloat Float(APFloat::x87DoubleExtended, APInt(80, RawBits));
78 SmallVector<char, 24> Chars;
79 Float.toString(Chars, Precision, 0);
80 return std::string(Chars.begin(), Chars.end());
81}
82
83static std::string stripTrailingZeros(const std::string &Float) {
84 size_t NonZero = Float.find_last_not_of('0');
85 assert(NonZero != std::string::npos && "no . in floating point string");
86
87 if (Float[NonZero] == '.')
88 ++NonZero;
89
90 return Float.substr(0, NonZero + 1);
91}
92
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +000093std::string ScaledNumberBase::toString(uint64_t D, int16_t E, int Width,
94 unsigned Precision) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000095 if (!D)
96 return "0.0";
97
98 // Canonicalize exponent and digits.
99 uint64_t Above0 = 0;
100 uint64_t Below0 = 0;
101 uint64_t Extra = 0;
102 int ExtraShift = 0;
103 if (E == 0) {
104 Above0 = D;
105 } else if (E > 0) {
106 if (int Shift = std::min(int16_t(countLeadingZeros64(D)), E)) {
107 D <<= Shift;
108 E -= Shift;
109
110 if (!E)
111 Above0 = D;
112 }
113 } else if (E > -64) {
114 Above0 = D >> -E;
115 Below0 = D << (64 + E);
116 } else if (E > -120) {
117 Below0 = D >> (-E - 64);
118 Extra = D << (128 + E);
119 ExtraShift = -64 - E;
120 }
121
122 // Fall back on APFloat for very small and very large numbers.
123 if (!Above0 && !Below0)
124 return toStringAPFloat(D, E, Precision);
125
126 // Append the digits before the decimal.
127 std::string Str;
128 size_t DigitsOut = 0;
129 if (Above0) {
130 appendNumber(Str, Above0);
131 DigitsOut = Str.size();
132 } else
133 appendDigit(Str, 0);
134 std::reverse(Str.begin(), Str.end());
135
136 // Return early if there's nothing after the decimal.
137 if (!Below0)
138 return Str + ".0";
139
140 // Append the decimal and beyond.
141 Str += '.';
142 uint64_t Error = UINT64_C(1) << (64 - Width);
143
144 // We need to shift Below0 to the right to make space for calculating
145 // digits. Save the precision we're losing in Extra.
146 Extra = (Below0 & 0xf) << 56 | (Extra >> 8);
147 Below0 >>= 4;
148 size_t SinceDot = 0;
149 size_t AfterDot = Str.size();
150 do {
151 if (ExtraShift) {
152 --ExtraShift;
153 Error *= 5;
154 } else
155 Error *= 10;
156
157 Below0 *= 10;
158 Extra *= 10;
159 Below0 += (Extra >> 60);
160 Extra = Extra & (UINT64_MAX >> 4);
161 appendDigit(Str, Below0 >> 60);
162 Below0 = Below0 & (UINT64_MAX >> 4);
163 if (DigitsOut || Str.back() != '0')
164 ++DigitsOut;
165 ++SinceDot;
166 } while (Error && (Below0 << 4 | Extra >> 60) >= Error / 2 &&
167 (!Precision || DigitsOut <= Precision || SinceDot < 2));
168
169 // Return early for maximum precision.
170 if (!Precision || DigitsOut <= Precision)
171 return stripTrailingZeros(Str);
172
173 // Find where to truncate.
174 size_t Truncate =
175 std::max(Str.size() - (DigitsOut - Precision), AfterDot + 1);
176
177 // Check if there's anything to truncate.
178 if (Truncate >= Str.size())
179 return stripTrailingZeros(Str);
180
181 bool Carry = doesRoundUp(Str[Truncate]);
182 if (!Carry)
183 return stripTrailingZeros(Str.substr(0, Truncate));
184
185 // Round with the first truncated digit.
186 for (std::string::reverse_iterator I(Str.begin() + Truncate), E = Str.rend();
187 I != E; ++I) {
188 if (*I == '.')
189 continue;
190 if (*I == '9') {
191 *I = '0';
192 continue;
193 }
194
195 ++*I;
196 Carry = false;
197 break;
198 }
199
200 // Add "1" in front if we still need to carry.
201 return stripTrailingZeros(std::string(Carry, '1') + Str.substr(0, Truncate));
202}
203
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +0000204raw_ostream &ScaledNumberBase::print(raw_ostream &OS, uint64_t D, int16_t E,
205 int Width, unsigned Precision) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000206 return OS << toString(D, E, Width, Precision);
207}
208
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +0000209void ScaledNumberBase::dump(uint64_t D, int16_t E, int Width) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000210 print(dbgs(), D, E, Width, 0) << "[" << Width << ":" << D << "*2^" << E
211 << "]";
212}
213
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000214//===----------------------------------------------------------------------===//
215//
216// BlockMass implementation.
217//
218//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000219ScaledNumber<uint64_t> BlockMass::toScaled() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000220 if (isFull())
Duncan P. N. Exon Smithc379c872014-06-23 23:36:17 +0000221 return ScaledNumber<uint64_t>(1, 0);
222 return ScaledNumber<uint64_t>(getMass() + 1, -64);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000223}
224
225void BlockMass::dump() const { print(dbgs()); }
226
227static char getHexDigit(int N) {
228 assert(N < 16);
229 if (N < 10)
230 return '0' + N;
231 return 'a' + N - 10;
232}
233raw_ostream &BlockMass::print(raw_ostream &OS) const {
234 for (int Digits = 0; Digits < 16; ++Digits)
235 OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf);
236 return OS;
237}
238
239//===----------------------------------------------------------------------===//
240//
241// BlockFrequencyInfoImpl implementation.
242//
243//===----------------------------------------------------------------------===//
244namespace {
245
246typedef BlockFrequencyInfoImplBase::BlockNode BlockNode;
247typedef BlockFrequencyInfoImplBase::Distribution Distribution;
248typedef BlockFrequencyInfoImplBase::Distribution::WeightList WeightList;
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000249typedef BlockFrequencyInfoImplBase::Scaled64 Scaled64;
Duncan P. N. Exon Smithcc88ebf2014-04-22 03:31:31 +0000250typedef BlockFrequencyInfoImplBase::LoopData LoopData;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000251typedef BlockFrequencyInfoImplBase::Weight Weight;
252typedef BlockFrequencyInfoImplBase::FrequencyData FrequencyData;
253
254/// \brief Dithering mass distributer.
255///
256/// This class splits up a single mass into portions by weight, dithering to
257/// spread out error. No mass is lost. The dithering precision depends on the
258/// precision of the product of \a BlockMass and \a BranchProbability.
259///
260/// The distribution algorithm follows.
261///
262/// 1. Initialize by saving the sum of the weights in \a RemWeight and the
263/// mass to distribute in \a RemMass.
264///
265/// 2. For each portion:
266///
267/// 1. Construct a branch probability, P, as the portion's weight divided
268/// by the current value of \a RemWeight.
269/// 2. Calculate the portion's mass as \a RemMass times P.
270/// 3. Update \a RemWeight and \a RemMass at each portion by subtracting
271/// the current portion's weight and mass.
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000272struct DitheringDistributer {
273 uint32_t RemWeight;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000274 BlockMass RemMass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000275
276 DitheringDistributer(Distribution &Dist, const BlockMass &Mass);
277
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000278 BlockMass takeMass(uint32_t Weight);
279};
280}
281
282DitheringDistributer::DitheringDistributer(Distribution &Dist,
283 const BlockMass &Mass) {
284 Dist.normalize();
285 RemWeight = Dist.Total;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000286 RemMass = Mass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000287}
288
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000289BlockMass DitheringDistributer::takeMass(uint32_t Weight) {
290 assert(Weight && "invalid weight");
291 assert(Weight <= RemWeight);
292 BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight);
293
294 // Decrement totals (dither).
295 RemWeight -= Weight;
296 RemMass -= Mass;
297 return Mass;
298}
299
300void Distribution::add(const BlockNode &Node, uint64_t Amount,
301 Weight::DistType Type) {
302 assert(Amount && "invalid weight of 0");
303 uint64_t NewTotal = Total + Amount;
304
305 // Check for overflow. It should be impossible to overflow twice.
306 bool IsOverflow = NewTotal < Total;
307 assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow");
308 DidOverflow |= IsOverflow;
309
310 // Update the total.
311 Total = NewTotal;
312
313 // Save the weight.
314 Weight W;
315 W.TargetNode = Node;
316 W.Amount = Amount;
317 W.Type = Type;
318 Weights.push_back(W);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000319}
320
321static void combineWeight(Weight &W, const Weight &OtherW) {
322 assert(OtherW.TargetNode.isValid());
323 if (!W.Amount) {
324 W = OtherW;
325 return;
326 }
327 assert(W.Type == OtherW.Type);
328 assert(W.TargetNode == OtherW.TargetNode);
Duncan P. N. Exon Smithebf76262014-04-25 04:38:40 +0000329 assert(W.Amount < W.Amount + OtherW.Amount && "Unexpected overflow");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000330 W.Amount += OtherW.Amount;
331}
332static void combineWeightsBySorting(WeightList &Weights) {
333 // Sort so edges to the same node are adjacent.
334 std::sort(Weights.begin(), Weights.end(),
335 [](const Weight &L,
336 const Weight &R) { return L.TargetNode < R.TargetNode; });
337
338 // Combine adjacent edges.
339 WeightList::iterator O = Weights.begin();
340 for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E;
341 ++O, (I = L)) {
342 *O = *I;
343
344 // Find the adjacent weights to the same node.
345 for (++L; L != E && I->TargetNode == L->TargetNode; ++L)
346 combineWeight(*O, *L);
347 }
348
349 // Erase extra entries.
350 Weights.erase(O, Weights.end());
351 return;
352}
353static void combineWeightsByHashing(WeightList &Weights) {
354 // Collect weights into a DenseMap.
355 typedef DenseMap<BlockNode::IndexType, Weight> HashTable;
356 HashTable Combined(NextPowerOf2(2 * Weights.size()));
357 for (const Weight &W : Weights)
358 combineWeight(Combined[W.TargetNode.Index], W);
359
360 // Check whether anything changed.
361 if (Weights.size() == Combined.size())
362 return;
363
364 // Fill in the new weights.
365 Weights.clear();
366 Weights.reserve(Combined.size());
367 for (const auto &I : Combined)
368 Weights.push_back(I.second);
369}
370static void combineWeights(WeightList &Weights) {
371 // Use a hash table for many successors to keep this linear.
372 if (Weights.size() > 128) {
373 combineWeightsByHashing(Weights);
374 return;
375 }
376
377 combineWeightsBySorting(Weights);
378}
379static uint64_t shiftRightAndRound(uint64_t N, int Shift) {
380 assert(Shift >= 0);
381 assert(Shift < 64);
382 if (!Shift)
383 return N;
384 return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1));
385}
386void Distribution::normalize() {
387 // Early exit for termination nodes.
388 if (Weights.empty())
389 return;
390
391 // Only bother if there are multiple successors.
392 if (Weights.size() > 1)
393 combineWeights(Weights);
394
395 // Early exit when combined into a single successor.
396 if (Weights.size() == 1) {
397 Total = 1;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000398 Weights.front().Amount = 1;
399 return;
400 }
401
402 // Determine how much to shift right so that the total fits into 32-bits.
403 //
404 // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1
405 // for each weight can cause a 32-bit overflow.
406 int Shift = 0;
407 if (DidOverflow)
408 Shift = 33;
409 else if (Total > UINT32_MAX)
410 Shift = 33 - countLeadingZeros(Total);
411
412 // Early exit if nothing needs to be scaled.
413 if (!Shift)
414 return;
415
416 // Recompute the total through accumulation (rather than shifting it) so that
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000417 // it's accurate after shifting.
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000418 Total = 0;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000419
420 // Sum the weights to each node and shift right if necessary.
421 for (Weight &W : Weights) {
422 // Scale down below UINT32_MAX. Since Shift is larger than necessary, we
423 // can round here without concern about overflow.
424 assert(W.TargetNode.isValid());
425 W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift));
426 assert(W.Amount <= UINT32_MAX);
427
428 // Update the total.
429 Total += W.Amount;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000430 }
431 assert(Total <= UINT32_MAX);
432}
433
434void BlockFrequencyInfoImplBase::clear() {
Duncan P. N. Exon Smithdc2d66e2014-04-22 03:31:34 +0000435 // Swap with a default-constructed std::vector, since std::vector<>::clear()
436 // does not actually clear heap storage.
437 std::vector<FrequencyData>().swap(Freqs);
438 std::vector<WorkingData>().swap(Working);
Duncan P. N. Exon Smithfc7dc932014-04-25 04:30:06 +0000439 Loops.clear();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000440}
441
442/// \brief Clear all memory not needed downstream.
443///
444/// Releases all memory not used downstream. In particular, saves Freqs.
445static void cleanup(BlockFrequencyInfoImplBase &BFI) {
446 std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs));
447 BFI.clear();
448 BFI.Freqs = std::move(SavedFreqs);
449}
450
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000451bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist,
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000452 const LoopData *OuterLoop,
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000453 const BlockNode &Pred,
454 const BlockNode &Succ,
455 uint64_t Weight) {
456 if (!Weight)
457 Weight = 1;
458
Duncan P. N. Exon Smith39cc6482014-04-25 04:38:06 +0000459 auto isLoopHeader = [&OuterLoop](const BlockNode &Node) {
460 return OuterLoop && OuterLoop->isHeader(Node);
461 };
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000462
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000463 BlockNode Resolved = Working[Succ.Index].getResolvedNode();
464
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000465#ifndef NDEBUG
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000466 auto debugSuccessor = [&](const char *Type) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000467 dbgs() << " =>"
468 << " [" << Type << "] weight = " << Weight;
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000469 if (!isLoopHeader(Resolved))
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000470 dbgs() << ", succ = " << getBlockName(Succ);
471 if (Resolved != Succ)
472 dbgs() << ", resolved = " << getBlockName(Resolved);
473 dbgs() << "\n";
474 };
475 (void)debugSuccessor;
476#endif
477
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000478 if (isLoopHeader(Resolved)) {
479 DEBUG(debugSuccessor("backedge"));
Duncan P. N. Exon Smith39cc6482014-04-25 04:38:06 +0000480 Dist.addBackedge(OuterLoop->getHeader(), Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000481 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000482 }
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000483
Duncan P. N. Exon Smith39cc6482014-04-25 04:38:06 +0000484 if (Working[Resolved.Index].getContainingLoop() != OuterLoop) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000485 DEBUG(debugSuccessor(" exit "));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000486 Dist.addExit(Resolved, Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000487 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000488 }
489
Duncan P. N. Exon Smithb3380ea2014-04-22 03:31:53 +0000490 if (Resolved < Pred) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000491 if (!isLoopHeader(Pred)) {
492 // If OuterLoop is an irreducible loop, we can't actually handle this.
493 assert((!OuterLoop || !OuterLoop->isIrreducible()) &&
494 "unhandled irreducible control flow");
495
496 // Irreducible backedge. Abort.
497 DEBUG(debugSuccessor("abort!!!"));
498 return false;
499 }
500
501 // If "Pred" is a loop header, then this isn't really a backedge; rather,
502 // OuterLoop must be irreducible. These false backedges can come only from
503 // secondary loop headers.
504 assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) &&
505 "unhandled irreducible control flow");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000506 }
507
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000508 DEBUG(debugSuccessor(" local "));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000509 Dist.addLocal(Resolved, Weight);
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000510 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000511}
512
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000513bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist(
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000514 const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000515 // Copy the exit map into Dist.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000516 for (const auto &I : Loop.Exits)
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000517 if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first,
518 I.second.getMass()))
519 // Irreducible backedge.
520 return false;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000521
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000522 return true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000523}
524
525/// \brief Get the maximum allowed loop scale.
526///
Duncan P. N. Exon Smith254689f2014-04-21 18:31:58 +0000527/// Gives the maximum number of estimated iterations allowed for a loop. Very
528/// large numbers cause problems downstream (even within 64-bits).
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000529static Scaled64 getMaxLoopScale() { return Scaled64(1, 12); }
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000530
531/// \brief Compute the loop scale for a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000532void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000533 // Compute loop scale.
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000534 DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000535
536 // LoopScale == 1 / ExitMass
537 // ExitMass == HeadMass - BackedgeMass
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000538 BlockMass ExitMass = BlockMass::getFull() - Loop.BackedgeMass;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000539
540 // Block scale stores the inverse of the scale.
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000541 Loop.Scale = ExitMass.toScaled().inverse();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000542
543 DEBUG(dbgs() << " - exit-mass = " << ExitMass << " (" << BlockMass::getFull()
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000544 << " - " << Loop.BackedgeMass << ")\n"
545 << " - scale = " << Loop.Scale << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000546
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000547 if (Loop.Scale > getMaxLoopScale()) {
548 Loop.Scale = getMaxLoopScale();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000549 DEBUG(dbgs() << " - reduced-to-max-scale: " << getMaxLoopScale() << "\n");
550 }
551}
552
553/// \brief Package up a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000554void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000555 DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n");
556
557 // Clear the subloop exits to prevent quadratic memory usage.
558 for (const BlockNode &M : Loop.Nodes) {
559 if (auto *Loop = Working[M.Index].getPackagedLoop())
560 Loop->Exits.clear();
561 DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n");
562 }
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000563 Loop.IsPackaged = true;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000564}
565
566void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source,
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000567 LoopData *OuterLoop,
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000568 Distribution &Dist) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000569 BlockMass Mass = Working[Source.Index].getMass();
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000570 DEBUG(dbgs() << " => mass: " << Mass << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000571
572 // Distribute mass to successors as laid out in Dist.
573 DitheringDistributer D(Dist, Mass);
574
575#ifndef NDEBUG
576 auto debugAssign = [&](const BlockNode &T, const BlockMass &M,
577 const char *Desc) {
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000578 dbgs() << " => assign " << M << " (" << D.RemMass << ")";
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000579 if (Desc)
580 dbgs() << " [" << Desc << "]";
581 if (T.isValid())
582 dbgs() << " to " << getBlockName(T);
583 dbgs() << "\n";
584 };
585 (void)debugAssign;
586#endif
587
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000588 for (const Weight &W : Dist.Weights) {
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000589 // Check for a local edge (non-backedge and non-exit).
590 BlockMass Taken = D.takeMass(W.Amount);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000591 if (W.Type == Weight::Local) {
Duncan P. N. Exon Smithda5eaed2014-04-25 18:47:04 +0000592 Working[W.TargetNode.Index].getMass() += Taken;
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000593 DEBUG(debugAssign(W.TargetNode, Taken, nullptr));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000594 continue;
595 }
596
597 // Backedges and exits only make sense if we're processing a loop.
Duncan P. N. Exon Smithd1320402014-04-25 04:38:01 +0000598 assert(OuterLoop && "backedge or exit outside of loop");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000599
600 // Check for a backedge.
601 if (W.Type == Weight::Backedge) {
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000602 OuterLoop->BackedgeMass += Taken;
603 DEBUG(debugAssign(BlockNode(), Taken, "back"));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000604 continue;
605 }
606
607 // This must be an exit.
608 assert(W.Type == Weight::Exit);
Duncan P. N. Exon Smithcb7d29d2014-04-25 04:38:43 +0000609 OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken));
610 DEBUG(debugAssign(W.TargetNode, Taken, "exit"));
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000611 }
612}
613
614static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI,
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000615 const Scaled64 &Min, const Scaled64 &Max) {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000616 // Scale the Factor to a size that creates integers. Ideally, integers would
617 // be scaled so that Max == UINT64_MAX so that they can be best
618 // differentiated. However, the register allocator currently deals poorly
619 // with large numbers. Instead, push Min up a little from 1 to give some
620 // room to differentiate small, unequal numbers.
621 //
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000622 // TODO: fix issues downstream so that ScalingFactor can be
623 // Scaled64(1,64)/Max.
624 Scaled64 ScalingFactor = Min.inverse();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000625 if ((Max / Min).lg() < 60)
626 ScalingFactor <<= 3;
627
628 // Translate the floats to integers.
629 DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max
630 << ", factor = " << ScalingFactor << "\n");
631 for (size_t Index = 0; Index < BFI.Freqs.size(); ++Index) {
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000632 Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000633 BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>());
634 DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = "
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000635 << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000636 << ", int = " << BFI.Freqs[Index].Integer << "\n");
637 }
638}
639
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000640/// \brief Unwrap a loop package.
641///
642/// Visits all the members of a loop, adjusting their BlockData according to
643/// the loop's pseudo-node.
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000644static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) {
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000645 DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop)
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000646 << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale
647 << "\n");
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000648 Loop.Scale *= Loop.Mass.toScaled();
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000649 Loop.IsPackaged = false;
Duncan P. N. Exon Smith3f086782014-04-25 04:38:32 +0000650 DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n");
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000651
652 // Propagate the head scale through the loop. Since members are visited in
653 // RPO, the head scale will be updated by the loop scale first, and then the
654 // final head scale will be used for updated the rest of the members.
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000655 for (const BlockNode &N : Loop.Nodes) {
656 const auto &Working = BFI.Working[N.Index];
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000657 Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale
658 : BFI.Freqs[N.Index].Scaled;
659 Scaled64 New = Loop.Scale * F;
Duncan P. N. Exon Smith5291d2a2014-04-25 04:38:27 +0000660 DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => " << New
661 << "\n");
662 F = New;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000663 }
664}
665
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000666void BlockFrequencyInfoImplBase::unwrapLoops() {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000667 // Set initial frequencies from loop-local masses.
668 for (size_t Index = 0; Index < Working.size(); ++Index)
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000669 Freqs[Index].Scaled = Working[Index].Mass.toScaled();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000670
Duncan P. N. Exon Smithda0b21c2014-04-25 04:38:23 +0000671 for (LoopData &Loop : Loops)
Duncan P. N. Exon Smith0633f0e2014-04-25 04:38:25 +0000672 unwrapLoop(*this, Loop);
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000673}
674
675void BlockFrequencyInfoImplBase::finalizeMetrics() {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000676 // Unwrap loop packages in reverse post-order, tracking min and max
677 // frequencies.
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000678 auto Min = Scaled64::getLargest();
679 auto Max = Scaled64::getZero();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000680 for (size_t Index = 0; Index < Working.size(); ++Index) {
Duncan P. N. Exon Smith46d9a562014-04-25 04:38:17 +0000681 // Update min/max scale.
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000682 Min = std::min(Min, Freqs[Index].Scaled);
683 Max = std::max(Max, Freqs[Index].Scaled);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000684 }
685
686 // Convert to integers.
687 convertFloatingToInteger(*this, Min, Max);
688
689 // Clean up data structures.
690 cleanup(*this);
691
692 // Print out the final stats.
693 DEBUG(dump());
694}
695
696BlockFrequency
697BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const {
698 if (!Node.isValid())
699 return 0;
700 return Freqs[Node.Index].Integer;
701}
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000702Scaled64
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000703BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const {
704 if (!Node.isValid())
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000705 return Scaled64::getZero();
706 return Freqs[Node.Index].Scaled;
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000707}
708
709std::string
710BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const {
711 return std::string();
712}
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000713std::string
714BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
715 return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
716}
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000717
718raw_ostream &
719BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
720 const BlockNode &Node) const {
721 return OS << getFloatingBlockFreq(Node);
722}
723
724raw_ostream &
725BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
726 const BlockFrequency &Freq) const {
Duncan P. N. Exon Smithbeaf8132014-06-24 00:26:13 +0000727 Scaled64 Block(Freq.getFrequency(), 0);
728 Scaled64 Entry(getEntryFreq(), 0);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000729
730 return OS << Block / Entry;
731}
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000732
733void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {
734 Start = OuterLoop.getHeader();
735 Nodes.reserve(OuterLoop.Nodes.size());
736 for (auto N : OuterLoop.Nodes)
737 addNode(N);
738 indexNodes();
739}
740void IrreducibleGraph::addNodesInFunction() {
741 Start = 0;
742 for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index)
743 if (!BFI.Working[Index].isPackaged())
744 addNode(Index);
745 indexNodes();
746}
747void IrreducibleGraph::indexNodes() {
748 for (auto &I : Nodes)
749 Lookup[I.Node.Index] = &I;
750}
751void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ,
752 const BFIBase::LoopData *OuterLoop) {
753 if (OuterLoop && OuterLoop->isHeader(Succ))
754 return;
755 auto L = Lookup.find(Succ.Index);
756 if (L == Lookup.end())
757 return;
758 IrrNode &SuccIrr = *L->second;
759 Irr.Edges.push_back(&SuccIrr);
760 SuccIrr.Edges.push_front(&Irr);
761 ++SuccIrr.NumIn;
762}
763
764namespace llvm {
765template <> struct GraphTraits<IrreducibleGraph> {
766 typedef bfi_detail::IrreducibleGraph GraphT;
767
Duncan P. N. Exon Smith295b5e72014-04-28 20:22:29 +0000768 typedef const GraphT::IrrNode NodeType;
769 typedef GraphT::IrrNode::iterator ChildIteratorType;
Duncan P. N. Exon Smithc5a31392014-04-28 20:02:29 +0000770
771 static const NodeType *getEntryNode(const GraphT &G) {
772 return G.StartIrr;
773 }
774 static ChildIteratorType child_begin(NodeType *N) { return N->succ_begin(); }
775 static ChildIteratorType child_end(NodeType *N) { return N->succ_end(); }
776};
777}
778
779/// \brief Find extra irreducible headers.
780///
781/// Find entry blocks and other blocks with backedges, which exist when \c G
782/// contains irreducible sub-SCCs.
783static void findIrreducibleHeaders(
784 const BlockFrequencyInfoImplBase &BFI,
785 const IrreducibleGraph &G,
786 const std::vector<const IrreducibleGraph::IrrNode *> &SCC,
787 LoopData::NodeList &Headers, LoopData::NodeList &Others) {
788 // Map from nodes in the SCC to whether it's an entry block.
789 SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC;
790
791 // InSCC also acts the set of nodes in the graph. Seed it.
792 for (const auto *I : SCC)
793 InSCC[I] = false;
794
795 for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) {
796 auto &Irr = *I->first;
797 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
798 if (InSCC.count(P))
799 continue;
800
801 // This is an entry block.
802 I->second = true;
803 Headers.push_back(Irr.Node);
804 DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node) << "\n");
805 break;
806 }
807 }
808 assert(Headers.size() >= 2 && "Should be irreducible");
809 if (Headers.size() == InSCC.size()) {
810 // Every block is a header.
811 std::sort(Headers.begin(), Headers.end());
812 return;
813 }
814
815 // Look for extra headers from irreducible sub-SCCs.
816 for (const auto &I : InSCC) {
817 // Entry blocks are already headers.
818 if (I.second)
819 continue;
820
821 auto &Irr = *I.first;
822 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
823 // Skip forward edges.
824 if (P->Node < Irr.Node)
825 continue;
826
827 // Skip predecessors from entry blocks. These can have inverted
828 // ordering.
829 if (InSCC.lookup(P))
830 continue;
831
832 // Store the extra header.
833 Headers.push_back(Irr.Node);
834 DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node) << "\n");
835 break;
836 }
837 if (Headers.back() == Irr.Node)
838 // Added this as a header.
839 continue;
840
841 // This is not a header.
842 Others.push_back(Irr.Node);
843 DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n");
844 }
845 std::sort(Headers.begin(), Headers.end());
846 std::sort(Others.begin(), Others.end());
847}
848
849static void createIrreducibleLoop(
850 BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G,
851 LoopData *OuterLoop, std::list<LoopData>::iterator Insert,
852 const std::vector<const IrreducibleGraph::IrrNode *> &SCC) {
853 // Translate the SCC into RPO.
854 DEBUG(dbgs() << " - found-scc\n");
855
856 LoopData::NodeList Headers;
857 LoopData::NodeList Others;
858 findIrreducibleHeaders(BFI, G, SCC, Headers, Others);
859
860 auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(),
861 Headers.end(), Others.begin(), Others.end());
862
863 // Update loop hierarchy.
864 for (const auto &N : Loop->Nodes)
865 if (BFI.Working[N.Index].isLoopHeader())
866 BFI.Working[N.Index].Loop->Parent = &*Loop;
867 else
868 BFI.Working[N.Index].Loop = &*Loop;
869}
870
871iterator_range<std::list<LoopData>::iterator>
872BlockFrequencyInfoImplBase::analyzeIrreducible(
873 const IrreducibleGraph &G, LoopData *OuterLoop,
874 std::list<LoopData>::iterator Insert) {
875 assert((OuterLoop == nullptr) == (Insert == Loops.begin()));
876 auto Prev = OuterLoop ? std::prev(Insert) : Loops.end();
877
878 for (auto I = scc_begin(G); !I.isAtEnd(); ++I) {
879 if (I->size() < 2)
880 continue;
881
882 // Translate the SCC into RPO.
883 createIrreducibleLoop(*this, G, OuterLoop, Insert, *I);
884 }
885
886 if (OuterLoop)
887 return make_range(std::next(Prev), Insert);
888 return make_range(Loops.begin(), Insert);
889}
890
891void
892BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) {
893 OuterLoop.Exits.clear();
894 OuterLoop.BackedgeMass = BlockMass::getEmpty();
895 auto O = OuterLoop.Nodes.begin() + 1;
896 for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I)
897 if (!Working[I->Index].isPackaged())
898 *O++ = *I;
899 OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end());
900}