110be9a88SDuncan P. N. Exon Smith //===- BlockFrequencyImplInfo.cpp - Block Frequency Info Implementation ---===//
210be9a88SDuncan P. N. Exon Smith //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610be9a88SDuncan P. N. Exon Smith //
710be9a88SDuncan P. N. Exon Smith //===----------------------------------------------------------------------===//
810be9a88SDuncan P. N. Exon Smith //
910be9a88SDuncan P. N. Exon Smith // Loops should be simplified before this analysis.
1010be9a88SDuncan P. N. Exon Smith //
1110be9a88SDuncan P. N. Exon Smith //===----------------------------------------------------------------------===//
1210be9a88SDuncan P. N. Exon Smith
1310be9a88SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
1438c02bc7SEugene Zelenko #include "llvm/ADT/APInt.h"
1538c02bc7SEugene Zelenko #include "llvm/ADT/DenseMap.h"
1638c02bc7SEugene Zelenko #include "llvm/ADT/None.h"
1787c40fdfSDuncan P. N. Exon Smith #include "llvm/ADT/SCCIterator.h"
18432a3883SNico Weber #include "llvm/Config/llvm-config.h"
19b12b353aSXinliang David Li #include "llvm/IR/Function.h"
2038c02bc7SEugene Zelenko #include "llvm/Support/BlockFrequency.h"
2138c02bc7SEugene Zelenko #include "llvm/Support/BranchProbability.h"
2238c02bc7SEugene Zelenko #include "llvm/Support/Compiler.h"
2338c02bc7SEugene Zelenko #include "llvm/Support/Debug.h"
2438c02bc7SEugene Zelenko #include "llvm/Support/MathExtras.h"
2571c3a551Sserge-sans-paille #include "llvm/Support/ScaledNumber.h"
2610be9a88SDuncan P. N. Exon Smith #include "llvm/Support/raw_ostream.h"
2738c02bc7SEugene Zelenko #include <algorithm>
2838c02bc7SEugene Zelenko #include <cassert>
2938c02bc7SEugene Zelenko #include <cstddef>
3038c02bc7SEugene Zelenko #include <cstdint>
3138c02bc7SEugene Zelenko #include <iterator>
3238c02bc7SEugene Zelenko #include <list>
3357cbdfc9SDuncan P. N. Exon Smith #include <numeric>
3438c02bc7SEugene Zelenko #include <utility>
3538c02bc7SEugene Zelenko #include <vector>
3610be9a88SDuncan P. N. Exon Smith
3710be9a88SDuncan P. N. Exon Smith using namespace llvm;
38c5a3139eSDuncan P. N. Exon Smith using namespace llvm::bfi_detail;
3910be9a88SDuncan P. N. Exon Smith
401b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq"
411b9dde08SChandler Carruth
42d8aba75aSFangrui Song namespace llvm {
43803dd6feSHiroshi Yamauchi cl::opt<bool> CheckBFIUnknownBlockQueries(
44803dd6feSHiroshi Yamauchi "check-bfi-unknown-block-queries",
45803dd6feSHiroshi Yamauchi cl::init(false), cl::Hidden,
46803dd6feSHiroshi Yamauchi cl::desc("Check if block frequency is queried for an unknown block "
47803dd6feSHiroshi Yamauchi "for debugging missed BFI updates"));
480a0800c4Sspupyrev
490a0800c4Sspupyrev cl::opt<bool> UseIterativeBFIInference(
50*557efc9aSFangrui Song "use-iterative-bfi-inference", cl::Hidden,
510a0800c4Sspupyrev cl::desc("Apply an iterative post-processing to infer correct BFI counts"));
520a0800c4Sspupyrev
530a0800c4Sspupyrev cl::opt<unsigned> IterativeBFIMaxIterationsPerBlock(
540a0800c4Sspupyrev "iterative-bfi-max-iterations-per-block", cl::init(1000), cl::Hidden,
550a0800c4Sspupyrev cl::desc("Iterative inference: maximum number of update iterations "
560a0800c4Sspupyrev "per block"));
570a0800c4Sspupyrev
580a0800c4Sspupyrev cl::opt<double> IterativeBFIPrecision(
590a0800c4Sspupyrev "iterative-bfi-precision", cl::init(1e-12), cl::Hidden,
600a0800c4Sspupyrev cl::desc("Iterative inference: delta convergence precision; smaller values "
610a0800c4Sspupyrev "typically lead to better results at the cost of worsen runtime"));
62d8aba75aSFangrui Song }
63803dd6feSHiroshi Yamauchi
toScaled() const64beaf813dSDuncan P. N. Exon Smith ScaledNumber<uint64_t> BlockMass::toScaled() const {
6510be9a88SDuncan P. N. Exon Smith if (isFull())
66c379c87aSDuncan P. N. Exon Smith return ScaledNumber<uint64_t>(1, 0);
67c379c87aSDuncan P. N. Exon Smith return ScaledNumber<uint64_t>(getMass() + 1, -64);
6810be9a88SDuncan P. N. Exon Smith }
6910be9a88SDuncan P. N. Exon Smith
70615eb470SAaron Ballman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const71eb2a2546SYaron Keren LLVM_DUMP_METHOD void BlockMass::dump() const { print(dbgs()); }
728c209aa8SMatthias Braun #endif
7310be9a88SDuncan P. N. Exon Smith
getHexDigit(int N)7410be9a88SDuncan P. N. Exon Smith static char getHexDigit(int N) {
7510be9a88SDuncan P. N. Exon Smith assert(N < 16);
7610be9a88SDuncan P. N. Exon Smith if (N < 10)
7710be9a88SDuncan P. N. Exon Smith return '0' + N;
7810be9a88SDuncan P. N. Exon Smith return 'a' + N - 10;
7910be9a88SDuncan P. N. Exon Smith }
80ecefe5a8SEugene Zelenko
print(raw_ostream & OS) const8110be9a88SDuncan P. N. Exon Smith raw_ostream &BlockMass::print(raw_ostream &OS) const {
8210be9a88SDuncan P. N. Exon Smith for (int Digits = 0; Digits < 16; ++Digits)
8310be9a88SDuncan P. N. Exon Smith OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf);
8410be9a88SDuncan P. N. Exon Smith return OS;
8510be9a88SDuncan P. N. Exon Smith }
8610be9a88SDuncan P. N. Exon Smith
8710be9a88SDuncan P. N. Exon Smith namespace {
8810be9a88SDuncan P. N. Exon Smith
8938c02bc7SEugene Zelenko using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
9038c02bc7SEugene Zelenko using Distribution = BlockFrequencyInfoImplBase::Distribution;
9138c02bc7SEugene Zelenko using WeightList = BlockFrequencyInfoImplBase::Distribution::WeightList;
9238c02bc7SEugene Zelenko using Scaled64 = BlockFrequencyInfoImplBase::Scaled64;
9338c02bc7SEugene Zelenko using LoopData = BlockFrequencyInfoImplBase::LoopData;
9438c02bc7SEugene Zelenko using Weight = BlockFrequencyInfoImplBase::Weight;
9538c02bc7SEugene Zelenko using FrequencyData = BlockFrequencyInfoImplBase::FrequencyData;
9610be9a88SDuncan P. N. Exon Smith
975f8f34e4SAdrian Prantl /// Dithering mass distributer.
9810be9a88SDuncan P. N. Exon Smith ///
9910be9a88SDuncan P. N. Exon Smith /// This class splits up a single mass into portions by weight, dithering to
10010be9a88SDuncan P. N. Exon Smith /// spread out error. No mass is lost. The dithering precision depends on the
10110be9a88SDuncan P. N. Exon Smith /// precision of the product of \a BlockMass and \a BranchProbability.
10210be9a88SDuncan P. N. Exon Smith ///
10310be9a88SDuncan P. N. Exon Smith /// The distribution algorithm follows.
10410be9a88SDuncan P. N. Exon Smith ///
10510be9a88SDuncan P. N. Exon Smith /// 1. Initialize by saving the sum of the weights in \a RemWeight and the
10610be9a88SDuncan P. N. Exon Smith /// mass to distribute in \a RemMass.
10710be9a88SDuncan P. N. Exon Smith ///
10810be9a88SDuncan P. N. Exon Smith /// 2. For each portion:
10910be9a88SDuncan P. N. Exon Smith ///
11010be9a88SDuncan P. N. Exon Smith /// 1. Construct a branch probability, P, as the portion's weight divided
11110be9a88SDuncan P. N. Exon Smith /// by the current value of \a RemWeight.
11210be9a88SDuncan P. N. Exon Smith /// 2. Calculate the portion's mass as \a RemMass times P.
11310be9a88SDuncan P. N. Exon Smith /// 3. Update \a RemWeight and \a RemMass at each portion by subtracting
11410be9a88SDuncan P. N. Exon Smith /// the current portion's weight and mass.
11510be9a88SDuncan P. N. Exon Smith struct DitheringDistributer {
11610be9a88SDuncan P. N. Exon Smith uint32_t RemWeight;
11710be9a88SDuncan P. N. Exon Smith BlockMass RemMass;
11810be9a88SDuncan P. N. Exon Smith
11910be9a88SDuncan P. N. Exon Smith DitheringDistributer(Distribution &Dist, const BlockMass &Mass);
12010be9a88SDuncan P. N. Exon Smith
12110be9a88SDuncan P. N. Exon Smith BlockMass takeMass(uint32_t Weight);
12210be9a88SDuncan P. N. Exon Smith };
123b5650e5eSDuncan P. N. Exon Smith
124ecefe5a8SEugene Zelenko } // end anonymous namespace
12510be9a88SDuncan P. N. Exon Smith
DitheringDistributer(Distribution & Dist,const BlockMass & Mass)12610be9a88SDuncan P. N. Exon Smith DitheringDistributer::DitheringDistributer(Distribution &Dist,
12710be9a88SDuncan P. N. Exon Smith const BlockMass &Mass) {
12810be9a88SDuncan P. N. Exon Smith Dist.normalize();
12910be9a88SDuncan P. N. Exon Smith RemWeight = Dist.Total;
13010be9a88SDuncan P. N. Exon Smith RemMass = Mass;
13110be9a88SDuncan P. N. Exon Smith }
13210be9a88SDuncan P. N. Exon Smith
takeMass(uint32_t Weight)13310be9a88SDuncan P. N. Exon Smith BlockMass DitheringDistributer::takeMass(uint32_t Weight) {
13410be9a88SDuncan P. N. Exon Smith assert(Weight && "invalid weight");
13510be9a88SDuncan P. N. Exon Smith assert(Weight <= RemWeight);
13610be9a88SDuncan P. N. Exon Smith BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight);
13710be9a88SDuncan P. N. Exon Smith
13810be9a88SDuncan P. N. Exon Smith // Decrement totals (dither).
13910be9a88SDuncan P. N. Exon Smith RemWeight -= Weight;
14010be9a88SDuncan P. N. Exon Smith RemMass -= Mass;
14110be9a88SDuncan P. N. Exon Smith return Mass;
14210be9a88SDuncan P. N. Exon Smith }
14310be9a88SDuncan P. N. Exon Smith
add(const BlockNode & Node,uint64_t Amount,Weight::DistType Type)14410be9a88SDuncan P. N. Exon Smith void Distribution::add(const BlockNode &Node, uint64_t Amount,
14510be9a88SDuncan P. N. Exon Smith Weight::DistType Type) {
14610be9a88SDuncan P. N. Exon Smith assert(Amount && "invalid weight of 0");
14710be9a88SDuncan P. N. Exon Smith uint64_t NewTotal = Total + Amount;
14810be9a88SDuncan P. N. Exon Smith
14910be9a88SDuncan P. N. Exon Smith // Check for overflow. It should be impossible to overflow twice.
15010be9a88SDuncan P. N. Exon Smith bool IsOverflow = NewTotal < Total;
15110be9a88SDuncan P. N. Exon Smith assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow");
15210be9a88SDuncan P. N. Exon Smith DidOverflow |= IsOverflow;
15310be9a88SDuncan P. N. Exon Smith
15410be9a88SDuncan P. N. Exon Smith // Update the total.
15510be9a88SDuncan P. N. Exon Smith Total = NewTotal;
15610be9a88SDuncan P. N. Exon Smith
15710be9a88SDuncan P. N. Exon Smith // Save the weight.
15860755108SDuncan P. N. Exon Smith Weights.push_back(Weight(Type, Node, Amount));
15910be9a88SDuncan P. N. Exon Smith }
16010be9a88SDuncan P. N. Exon Smith
combineWeight(Weight & W,const Weight & OtherW)16110be9a88SDuncan P. N. Exon Smith static void combineWeight(Weight &W, const Weight &OtherW) {
16210be9a88SDuncan P. N. Exon Smith assert(OtherW.TargetNode.isValid());
16310be9a88SDuncan P. N. Exon Smith if (!W.Amount) {
16410be9a88SDuncan P. N. Exon Smith W = OtherW;
16510be9a88SDuncan P. N. Exon Smith return;
16610be9a88SDuncan P. N. Exon Smith }
16710be9a88SDuncan P. N. Exon Smith assert(W.Type == OtherW.Type);
16810be9a88SDuncan P. N. Exon Smith assert(W.TargetNode == OtherW.TargetNode);
16957cbdfc9SDuncan P. N. Exon Smith assert(OtherW.Amount && "Expected non-zero weight");
17057cbdfc9SDuncan P. N. Exon Smith if (W.Amount > W.Amount + OtherW.Amount)
17157cbdfc9SDuncan P. N. Exon Smith // Saturate on overflow.
17257cbdfc9SDuncan P. N. Exon Smith W.Amount = UINT64_MAX;
17357cbdfc9SDuncan P. N. Exon Smith else
17410be9a88SDuncan P. N. Exon Smith W.Amount += OtherW.Amount;
17510be9a88SDuncan P. N. Exon Smith }
176ecefe5a8SEugene Zelenko
combineWeightsBySorting(WeightList & Weights)17710be9a88SDuncan P. N. Exon Smith static void combineWeightsBySorting(WeightList &Weights) {
17810be9a88SDuncan P. N. Exon Smith // Sort so edges to the same node are adjacent.
1790cac726aSFangrui Song llvm::sort(Weights, [](const Weight &L, const Weight &R) {
1800cac726aSFangrui Song return L.TargetNode < R.TargetNode;
1810cac726aSFangrui Song });
18210be9a88SDuncan P. N. Exon Smith
18310be9a88SDuncan P. N. Exon Smith // Combine adjacent edges.
18410be9a88SDuncan P. N. Exon Smith WeightList::iterator O = Weights.begin();
18510be9a88SDuncan P. N. Exon Smith for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E;
18610be9a88SDuncan P. N. Exon Smith ++O, (I = L)) {
18710be9a88SDuncan P. N. Exon Smith *O = *I;
18810be9a88SDuncan P. N. Exon Smith
18910be9a88SDuncan P. N. Exon Smith // Find the adjacent weights to the same node.
19010be9a88SDuncan P. N. Exon Smith for (++L; L != E && I->TargetNode == L->TargetNode; ++L)
19110be9a88SDuncan P. N. Exon Smith combineWeight(*O, *L);
19210be9a88SDuncan P. N. Exon Smith }
19310be9a88SDuncan P. N. Exon Smith
19410be9a88SDuncan P. N. Exon Smith // Erase extra entries.
19510be9a88SDuncan P. N. Exon Smith Weights.erase(O, Weights.end());
19610be9a88SDuncan P. N. Exon Smith }
197ecefe5a8SEugene Zelenko
combineWeightsByHashing(WeightList & Weights)19810be9a88SDuncan P. N. Exon Smith static void combineWeightsByHashing(WeightList &Weights) {
19910be9a88SDuncan P. N. Exon Smith // Collect weights into a DenseMap.
20038c02bc7SEugene Zelenko using HashTable = DenseMap<BlockNode::IndexType, Weight>;
20138c02bc7SEugene Zelenko
20210be9a88SDuncan P. N. Exon Smith HashTable Combined(NextPowerOf2(2 * Weights.size()));
20310be9a88SDuncan P. N. Exon Smith for (const Weight &W : Weights)
20410be9a88SDuncan P. N. Exon Smith combineWeight(Combined[W.TargetNode.Index], W);
20510be9a88SDuncan P. N. Exon Smith
20610be9a88SDuncan P. N. Exon Smith // Check whether anything changed.
20710be9a88SDuncan P. N. Exon Smith if (Weights.size() == Combined.size())
20810be9a88SDuncan P. N. Exon Smith return;
20910be9a88SDuncan P. N. Exon Smith
21010be9a88SDuncan P. N. Exon Smith // Fill in the new weights.
21110be9a88SDuncan P. N. Exon Smith Weights.clear();
21210be9a88SDuncan P. N. Exon Smith Weights.reserve(Combined.size());
21310be9a88SDuncan P. N. Exon Smith for (const auto &I : Combined)
21410be9a88SDuncan P. N. Exon Smith Weights.push_back(I.second);
21510be9a88SDuncan P. N. Exon Smith }
216ecefe5a8SEugene Zelenko
combineWeights(WeightList & Weights)21710be9a88SDuncan P. N. Exon Smith static void combineWeights(WeightList &Weights) {
21810be9a88SDuncan P. N. Exon Smith // Use a hash table for many successors to keep this linear.
21910be9a88SDuncan P. N. Exon Smith if (Weights.size() > 128) {
22010be9a88SDuncan P. N. Exon Smith combineWeightsByHashing(Weights);
22110be9a88SDuncan P. N. Exon Smith return;
22210be9a88SDuncan P. N. Exon Smith }
22310be9a88SDuncan P. N. Exon Smith
22410be9a88SDuncan P. N. Exon Smith combineWeightsBySorting(Weights);
22510be9a88SDuncan P. N. Exon Smith }
226ecefe5a8SEugene Zelenko
shiftRightAndRound(uint64_t N,int Shift)22710be9a88SDuncan P. N. Exon Smith static uint64_t shiftRightAndRound(uint64_t N, int Shift) {
22810be9a88SDuncan P. N. Exon Smith assert(Shift >= 0);
22910be9a88SDuncan P. N. Exon Smith assert(Shift < 64);
23010be9a88SDuncan P. N. Exon Smith if (!Shift)
23110be9a88SDuncan P. N. Exon Smith return N;
23210be9a88SDuncan P. N. Exon Smith return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1));
23310be9a88SDuncan P. N. Exon Smith }
234ecefe5a8SEugene Zelenko
normalize()23510be9a88SDuncan P. N. Exon Smith void Distribution::normalize() {
23610be9a88SDuncan P. N. Exon Smith // Early exit for termination nodes.
23710be9a88SDuncan P. N. Exon Smith if (Weights.empty())
23810be9a88SDuncan P. N. Exon Smith return;
23910be9a88SDuncan P. N. Exon Smith
24010be9a88SDuncan P. N. Exon Smith // Only bother if there are multiple successors.
24110be9a88SDuncan P. N. Exon Smith if (Weights.size() > 1)
24210be9a88SDuncan P. N. Exon Smith combineWeights(Weights);
24310be9a88SDuncan P. N. Exon Smith
24410be9a88SDuncan P. N. Exon Smith // Early exit when combined into a single successor.
24510be9a88SDuncan P. N. Exon Smith if (Weights.size() == 1) {
24610be9a88SDuncan P. N. Exon Smith Total = 1;
24710be9a88SDuncan P. N. Exon Smith Weights.front().Amount = 1;
24810be9a88SDuncan P. N. Exon Smith return;
24910be9a88SDuncan P. N. Exon Smith }
25010be9a88SDuncan P. N. Exon Smith
25110be9a88SDuncan P. N. Exon Smith // Determine how much to shift right so that the total fits into 32-bits.
25210be9a88SDuncan P. N. Exon Smith //
25310be9a88SDuncan P. N. Exon Smith // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1
25410be9a88SDuncan P. N. Exon Smith // for each weight can cause a 32-bit overflow.
25510be9a88SDuncan P. N. Exon Smith int Shift = 0;
25610be9a88SDuncan P. N. Exon Smith if (DidOverflow)
25710be9a88SDuncan P. N. Exon Smith Shift = 33;
25810be9a88SDuncan P. N. Exon Smith else if (Total > UINT32_MAX)
25910be9a88SDuncan P. N. Exon Smith Shift = 33 - countLeadingZeros(Total);
26010be9a88SDuncan P. N. Exon Smith
26110be9a88SDuncan P. N. Exon Smith // Early exit if nothing needs to be scaled.
26257cbdfc9SDuncan P. N. Exon Smith if (!Shift) {
26357cbdfc9SDuncan P. N. Exon Smith // If we didn't overflow then combineWeights() shouldn't have changed the
26457cbdfc9SDuncan P. N. Exon Smith // sum of the weights, but let's double-check.
26557cbdfc9SDuncan P. N. Exon Smith assert(Total == std::accumulate(Weights.begin(), Weights.end(), UINT64_C(0),
26657cbdfc9SDuncan P. N. Exon Smith [](uint64_t Sum, const Weight &W) {
26757cbdfc9SDuncan P. N. Exon Smith return Sum + W.Amount;
26857cbdfc9SDuncan P. N. Exon Smith }) &&
26957cbdfc9SDuncan P. N. Exon Smith "Expected total to be correct");
27010be9a88SDuncan P. N. Exon Smith return;
27157cbdfc9SDuncan P. N. Exon Smith }
27210be9a88SDuncan P. N. Exon Smith
27310be9a88SDuncan P. N. Exon Smith // Recompute the total through accumulation (rather than shifting it) so that
27457cbdfc9SDuncan P. N. Exon Smith // it's accurate after shifting and any changes combineWeights() made above.
27510be9a88SDuncan P. N. Exon Smith Total = 0;
27610be9a88SDuncan P. N. Exon Smith
27710be9a88SDuncan P. N. Exon Smith // Sum the weights to each node and shift right if necessary.
27810be9a88SDuncan P. N. Exon Smith for (Weight &W : Weights) {
27910be9a88SDuncan P. N. Exon Smith // Scale down below UINT32_MAX. Since Shift is larger than necessary, we
28010be9a88SDuncan P. N. Exon Smith // can round here without concern about overflow.
28110be9a88SDuncan P. N. Exon Smith assert(W.TargetNode.isValid());
28210be9a88SDuncan P. N. Exon Smith W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift));
28310be9a88SDuncan P. N. Exon Smith assert(W.Amount <= UINT32_MAX);
28410be9a88SDuncan P. N. Exon Smith
28510be9a88SDuncan P. N. Exon Smith // Update the total.
28610be9a88SDuncan P. N. Exon Smith Total += W.Amount;
28710be9a88SDuncan P. N. Exon Smith }
28810be9a88SDuncan P. N. Exon Smith assert(Total <= UINT32_MAX);
28910be9a88SDuncan P. N. Exon Smith }
29010be9a88SDuncan P. N. Exon Smith
clear()29110be9a88SDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::clear() {
292dc2d66e7SDuncan P. N. Exon Smith // Swap with a default-constructed std::vector, since std::vector<>::clear()
293dc2d66e7SDuncan P. N. Exon Smith // does not actually clear heap storage.
294dc2d66e7SDuncan P. N. Exon Smith std::vector<FrequencyData>().swap(Freqs);
295dce9def3SHiroshi Yamauchi IsIrrLoopHeader.clear();
296dc2d66e7SDuncan P. N. Exon Smith std::vector<WorkingData>().swap(Working);
297fc7dc930SDuncan P. N. Exon Smith Loops.clear();
29810be9a88SDuncan P. N. Exon Smith }
29910be9a88SDuncan P. N. Exon Smith
3005f8f34e4SAdrian Prantl /// Clear all memory not needed downstream.
30110be9a88SDuncan P. N. Exon Smith ///
30210be9a88SDuncan P. N. Exon Smith /// Releases all memory not used downstream. In particular, saves Freqs.
cleanup(BlockFrequencyInfoImplBase & BFI)30310be9a88SDuncan P. N. Exon Smith static void cleanup(BlockFrequencyInfoImplBase &BFI) {
30410be9a88SDuncan P. N. Exon Smith std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs));
305dce9def3SHiroshi Yamauchi SparseBitVector<> SavedIsIrrLoopHeader(std::move(BFI.IsIrrLoopHeader));
30610be9a88SDuncan P. N. Exon Smith BFI.clear();
30710be9a88SDuncan P. N. Exon Smith BFI.Freqs = std::move(SavedFreqs);
308dce9def3SHiroshi Yamauchi BFI.IsIrrLoopHeader = std::move(SavedIsIrrLoopHeader);
30910be9a88SDuncan P. N. Exon Smith }
31010be9a88SDuncan P. N. Exon Smith
addToDist(Distribution & Dist,const LoopData * OuterLoop,const BlockNode & Pred,const BlockNode & Succ,uint64_t Weight)311c5a3139eSDuncan P. N. Exon Smith bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist,
312d132040eSDuncan P. N. Exon Smith const LoopData *OuterLoop,
31310be9a88SDuncan P. N. Exon Smith const BlockNode &Pred,
31410be9a88SDuncan P. N. Exon Smith const BlockNode &Succ,
31510be9a88SDuncan P. N. Exon Smith uint64_t Weight) {
31610be9a88SDuncan P. N. Exon Smith if (!Weight)
31710be9a88SDuncan P. N. Exon Smith Weight = 1;
31810be9a88SDuncan P. N. Exon Smith
31939cc6482SDuncan P. N. Exon Smith auto isLoopHeader = [&OuterLoop](const BlockNode &Node) {
32039cc6482SDuncan P. N. Exon Smith return OuterLoop && OuterLoop->isHeader(Node);
32139cc6482SDuncan P. N. Exon Smith };
322d132040eSDuncan P. N. Exon Smith
323da5eaedaSDuncan P. N. Exon Smith BlockNode Resolved = Working[Succ.Index].getResolvedNode();
324da5eaedaSDuncan P. N. Exon Smith
32510be9a88SDuncan P. N. Exon Smith #ifndef NDEBUG
326da5eaedaSDuncan P. N. Exon Smith auto debugSuccessor = [&](const char *Type) {
32710be9a88SDuncan P. N. Exon Smith dbgs() << " =>"
32810be9a88SDuncan P. N. Exon Smith << " [" << Type << "] weight = " << Weight;
329da5eaedaSDuncan P. N. Exon Smith if (!isLoopHeader(Resolved))
33010be9a88SDuncan P. N. Exon Smith dbgs() << ", succ = " << getBlockName(Succ);
33110be9a88SDuncan P. N. Exon Smith if (Resolved != Succ)
33210be9a88SDuncan P. N. Exon Smith dbgs() << ", resolved = " << getBlockName(Resolved);
33310be9a88SDuncan P. N. Exon Smith dbgs() << "\n";
33410be9a88SDuncan P. N. Exon Smith };
33510be9a88SDuncan P. N. Exon Smith (void)debugSuccessor;
33610be9a88SDuncan P. N. Exon Smith #endif
33710be9a88SDuncan P. N. Exon Smith
338da5eaedaSDuncan P. N. Exon Smith if (isLoopHeader(Resolved)) {
339d34e60caSNicola Zaghen LLVM_DEBUG(debugSuccessor("backedge"));
3409a779623SDiego Novillo Dist.addBackedge(Resolved, Weight);
341c5a3139eSDuncan P. N. Exon Smith return true;
34210be9a88SDuncan P. N. Exon Smith }
34310be9a88SDuncan P. N. Exon Smith
34439cc6482SDuncan P. N. Exon Smith if (Working[Resolved.Index].getContainingLoop() != OuterLoop) {
345d34e60caSNicola Zaghen LLVM_DEBUG(debugSuccessor(" exit "));
34610be9a88SDuncan P. N. Exon Smith Dist.addExit(Resolved, Weight);
347c5a3139eSDuncan P. N. Exon Smith return true;
34810be9a88SDuncan P. N. Exon Smith }
34910be9a88SDuncan P. N. Exon Smith
350b3380ea6SDuncan P. N. Exon Smith if (Resolved < Pred) {
351c5a3139eSDuncan P. N. Exon Smith if (!isLoopHeader(Pred)) {
352c5a3139eSDuncan P. N. Exon Smith // If OuterLoop is an irreducible loop, we can't actually handle this.
353c5a3139eSDuncan P. N. Exon Smith assert((!OuterLoop || !OuterLoop->isIrreducible()) &&
354c5a3139eSDuncan P. N. Exon Smith "unhandled irreducible control flow");
355c5a3139eSDuncan P. N. Exon Smith
356c5a3139eSDuncan P. N. Exon Smith // Irreducible backedge. Abort.
357d34e60caSNicola Zaghen LLVM_DEBUG(debugSuccessor("abort!!!"));
358c5a3139eSDuncan P. N. Exon Smith return false;
359c5a3139eSDuncan P. N. Exon Smith }
360c5a3139eSDuncan P. N. Exon Smith
361c5a3139eSDuncan P. N. Exon Smith // If "Pred" is a loop header, then this isn't really a backedge; rather,
362c5a3139eSDuncan P. N. Exon Smith // OuterLoop must be irreducible. These false backedges can come only from
363c5a3139eSDuncan P. N. Exon Smith // secondary loop headers.
364c5a3139eSDuncan P. N. Exon Smith assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) &&
365c5a3139eSDuncan P. N. Exon Smith "unhandled irreducible control flow");
36610be9a88SDuncan P. N. Exon Smith }
36710be9a88SDuncan P. N. Exon Smith
368d34e60caSNicola Zaghen LLVM_DEBUG(debugSuccessor(" local "));
36910be9a88SDuncan P. N. Exon Smith Dist.addLocal(Resolved, Weight);
370c5a3139eSDuncan P. N. Exon Smith return true;
37110be9a88SDuncan P. N. Exon Smith }
37210be9a88SDuncan P. N. Exon Smith
addLoopSuccessorsToDist(const LoopData * OuterLoop,LoopData & Loop,Distribution & Dist)373c5a3139eSDuncan P. N. Exon Smith bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist(
374d132040eSDuncan P. N. Exon Smith const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) {
37510be9a88SDuncan P. N. Exon Smith // Copy the exit map into Dist.
376d132040eSDuncan P. N. Exon Smith for (const auto &I : Loop.Exits)
377c5a3139eSDuncan P. N. Exon Smith if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first,
378c5a3139eSDuncan P. N. Exon Smith I.second.getMass()))
379c5a3139eSDuncan P. N. Exon Smith // Irreducible backedge.
380c5a3139eSDuncan P. N. Exon Smith return false;
38110be9a88SDuncan P. N. Exon Smith
382c5a3139eSDuncan P. N. Exon Smith return true;
38310be9a88SDuncan P. N. Exon Smith }
38410be9a88SDuncan P. N. Exon Smith
3855f8f34e4SAdrian Prantl /// Compute the loop scale for a loop.
computeLoopScale(LoopData & Loop)386d132040eSDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) {
38710be9a88SDuncan P. N. Exon Smith // Compute loop scale.
388d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n");
38910be9a88SDuncan P. N. Exon Smith
390a354f488SDiego Novillo // Infinite loops need special handling. If we give the back edge an infinite
391a354f488SDiego Novillo // mass, they may saturate all the other scales in the function down to 1,
392a354f488SDiego Novillo // making all the other region temperatures look exactly the same. Choose an
393a354f488SDiego Novillo // arbitrary scale to avoid these issues.
394a354f488SDiego Novillo //
395a354f488SDiego Novillo // FIXME: An alternate way would be to select a symbolic scale which is later
396a354f488SDiego Novillo // replaced to be the maximum of all computed scales plus 1. This would
397a354f488SDiego Novillo // appropriately describe the loop as having a large scale, without skewing
398a354f488SDiego Novillo // the final frequency computation.
3990fb9880bSSanjay Patel const Scaled64 InfiniteLoopScale(1, 12);
400a354f488SDiego Novillo
40110be9a88SDuncan P. N. Exon Smith // LoopScale == 1 / ExitMass
40210be9a88SDuncan P. N. Exon Smith // ExitMass == HeadMass - BackedgeMass
4039a779623SDiego Novillo BlockMass TotalBackedgeMass;
4049a779623SDiego Novillo for (auto &Mass : Loop.BackedgeMass)
4059a779623SDiego Novillo TotalBackedgeMass += Mass;
4069a779623SDiego Novillo BlockMass ExitMass = BlockMass::getFull() - TotalBackedgeMass;
40710be9a88SDuncan P. N. Exon Smith
408a354f488SDiego Novillo // Block scale stores the inverse of the scale. If this is an infinite loop,
409a354f488SDiego Novillo // its exit mass will be zero. In this case, use an arbitrary scale for the
410a354f488SDiego Novillo // loop scale.
411a354f488SDiego Novillo Loop.Scale =
4120fb9880bSSanjay Patel ExitMass.isEmpty() ? InfiniteLoopScale : ExitMass.toScaled().inverse();
41310be9a88SDuncan P. N. Exon Smith
414d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - exit-mass = " << ExitMass << " ("
415d34e60caSNicola Zaghen << BlockMass::getFull() << " - " << TotalBackedgeMass
416d34e60caSNicola Zaghen << ")\n"
417d132040eSDuncan P. N. Exon Smith << " - scale = " << Loop.Scale << "\n");
41810be9a88SDuncan P. N. Exon Smith }
41910be9a88SDuncan P. N. Exon Smith
4205f8f34e4SAdrian Prantl /// Package up a loop.
packageLoop(LoopData & Loop)421d132040eSDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) {
422d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n");
423c5a3139eSDuncan P. N. Exon Smith
424c5a3139eSDuncan P. N. Exon Smith // Clear the subloop exits to prevent quadratic memory usage.
425c5a3139eSDuncan P. N. Exon Smith for (const BlockNode &M : Loop.Nodes) {
426c5a3139eSDuncan P. N. Exon Smith if (auto *Loop = Working[M.Index].getPackagedLoop())
427c5a3139eSDuncan P. N. Exon Smith Loop->Exits.clear();
428d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n");
429c5a3139eSDuncan P. N. Exon Smith }
430d132040eSDuncan P. N. Exon Smith Loop.IsPackaged = true;
43110be9a88SDuncan P. N. Exon Smith }
43210be9a88SDuncan P. N. Exon Smith
4339a779623SDiego Novillo #ifndef NDEBUG
debugAssign(const BlockFrequencyInfoImplBase & BFI,const DitheringDistributer & D,const BlockNode & T,const BlockMass & M,const char * Desc)4349a779623SDiego Novillo static void debugAssign(const BlockFrequencyInfoImplBase &BFI,
4359a779623SDiego Novillo const DitheringDistributer &D, const BlockNode &T,
4369a779623SDiego Novillo const BlockMass &M, const char *Desc) {
4379a779623SDiego Novillo dbgs() << " => assign " << M << " (" << D.RemMass << ")";
4389a779623SDiego Novillo if (Desc)
4399a779623SDiego Novillo dbgs() << " [" << Desc << "]";
4409a779623SDiego Novillo if (T.isValid())
4419a779623SDiego Novillo dbgs() << " to " << BFI.getBlockName(T);
4429a779623SDiego Novillo dbgs() << "\n";
4439a779623SDiego Novillo }
4449a779623SDiego Novillo #endif
4459a779623SDiego Novillo
distributeMass(const BlockNode & Source,LoopData * OuterLoop,Distribution & Dist)44610be9a88SDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source,
447d132040eSDuncan P. N. Exon Smith LoopData *OuterLoop,
44810be9a88SDuncan P. N. Exon Smith Distribution &Dist) {
449da5eaedaSDuncan P. N. Exon Smith BlockMass Mass = Working[Source.Index].getMass();
450d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " => mass: " << Mass << "\n");
45110be9a88SDuncan P. N. Exon Smith
45210be9a88SDuncan P. N. Exon Smith // Distribute mass to successors as laid out in Dist.
45310be9a88SDuncan P. N. Exon Smith DitheringDistributer D(Dist, Mass);
45410be9a88SDuncan P. N. Exon Smith
45510be9a88SDuncan P. N. Exon Smith for (const Weight &W : Dist.Weights) {
456cb7d29d3SDuncan P. N. Exon Smith // Check for a local edge (non-backedge and non-exit).
457cb7d29d3SDuncan P. N. Exon Smith BlockMass Taken = D.takeMass(W.Amount);
45810be9a88SDuncan P. N. Exon Smith if (W.Type == Weight::Local) {
459da5eaedaSDuncan P. N. Exon Smith Working[W.TargetNode.Index].getMass() += Taken;
460d34e60caSNicola Zaghen LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
46110be9a88SDuncan P. N. Exon Smith continue;
46210be9a88SDuncan P. N. Exon Smith }
46310be9a88SDuncan P. N. Exon Smith
46410be9a88SDuncan P. N. Exon Smith // Backedges and exits only make sense if we're processing a loop.
465d132040eSDuncan P. N. Exon Smith assert(OuterLoop && "backedge or exit outside of loop");
46610be9a88SDuncan P. N. Exon Smith
46710be9a88SDuncan P. N. Exon Smith // Check for a backedge.
46810be9a88SDuncan P. N. Exon Smith if (W.Type == Weight::Backedge) {
4698c49a572SDiego Novillo OuterLoop->BackedgeMass[OuterLoop->getHeaderIndex(W.TargetNode)] += Taken;
470d34e60caSNicola Zaghen LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "back"));
47110be9a88SDuncan P. N. Exon Smith continue;
47210be9a88SDuncan P. N. Exon Smith }
47310be9a88SDuncan P. N. Exon Smith
47410be9a88SDuncan P. N. Exon Smith // This must be an exit.
47510be9a88SDuncan P. N. Exon Smith assert(W.Type == Weight::Exit);
476cb7d29d3SDuncan P. N. Exon Smith OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken));
477d34e60caSNicola Zaghen LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "exit"));
47810be9a88SDuncan P. N. Exon Smith }
47910be9a88SDuncan P. N. Exon Smith }
48010be9a88SDuncan P. N. Exon Smith
convertFloatingToInteger(BlockFrequencyInfoImplBase & BFI,const Scaled64 & Min,const Scaled64 & Max)48110be9a88SDuncan P. N. Exon Smith static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI,
482beaf813dSDuncan P. N. Exon Smith const Scaled64 &Min, const Scaled64 &Max) {
48310be9a88SDuncan P. N. Exon Smith // Scale the Factor to a size that creates integers. Ideally, integers would
48410be9a88SDuncan P. N. Exon Smith // be scaled so that Max == UINT64_MAX so that they can be best
485a354f488SDiego Novillo // differentiated. However, in the presence of large frequency values, small
486a354f488SDiego Novillo // frequencies are scaled down to 1, making it impossible to differentiate
487a354f488SDiego Novillo // small, unequal numbers. When the spread between Min and Max frequencies
488a354f488SDiego Novillo // fits well within MaxBits, we make the scale be at least 8.
489a354f488SDiego Novillo const unsigned MaxBits = 64;
490a354f488SDiego Novillo const unsigned SpreadBits = (Max / Min).lg();
491a354f488SDiego Novillo Scaled64 ScalingFactor;
492a354f488SDiego Novillo if (SpreadBits <= MaxBits - 3) {
493a354f488SDiego Novillo // If the values are small enough, make the scaling factor at least 8 to
494a354f488SDiego Novillo // allow distinguishing small values.
495a354f488SDiego Novillo ScalingFactor = Min.inverse();
49610be9a88SDuncan P. N. Exon Smith ScalingFactor <<= 3;
497a354f488SDiego Novillo } else {
498a354f488SDiego Novillo // If the values need more than MaxBits to be represented, saturate small
499a354f488SDiego Novillo // frequency values down to 1 by using a scaling factor that benefits large
500a354f488SDiego Novillo // frequency values.
501a354f488SDiego Novillo ScalingFactor = Scaled64(1, MaxBits) / Max;
502a354f488SDiego Novillo }
50310be9a88SDuncan P. N. Exon Smith
50410be9a88SDuncan P. N. Exon Smith // Translate the floats to integers.
505d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max
50610be9a88SDuncan P. N. Exon Smith << ", factor = " << ScalingFactor << "\n");
50710be9a88SDuncan P. N. Exon Smith for (size_t Index = 0; Index < BFI.Freqs.size(); ++Index) {
508beaf813dSDuncan P. N. Exon Smith Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor;
50910be9a88SDuncan P. N. Exon Smith BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>());
510d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = "
511beaf813dSDuncan P. N. Exon Smith << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled
51210be9a88SDuncan P. N. Exon Smith << ", int = " << BFI.Freqs[Index].Integer << "\n");
51310be9a88SDuncan P. N. Exon Smith }
51410be9a88SDuncan P. N. Exon Smith }
51510be9a88SDuncan P. N. Exon Smith
5165f8f34e4SAdrian Prantl /// Unwrap a loop package.
51710be9a88SDuncan P. N. Exon Smith ///
51810be9a88SDuncan P. N. Exon Smith /// Visits all the members of a loop, adjusting their BlockData according to
51910be9a88SDuncan P. N. Exon Smith /// the loop's pseudo-node.
unwrapLoop(BlockFrequencyInfoImplBase & BFI,LoopData & Loop)5200633f0ecSDuncan P. N. Exon Smith static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) {
521d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop)
5220633f0ecSDuncan P. N. Exon Smith << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale
5230633f0ecSDuncan P. N. Exon Smith << "\n");
524beaf813dSDuncan P. N. Exon Smith Loop.Scale *= Loop.Mass.toScaled();
5255291d2a5SDuncan P. N. Exon Smith Loop.IsPackaged = false;
526d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n");
52710be9a88SDuncan P. N. Exon Smith
52810be9a88SDuncan P. N. Exon Smith // Propagate the head scale through the loop. Since members are visited in
52910be9a88SDuncan P. N. Exon Smith // RPO, the head scale will be updated by the loop scale first, and then the
53010be9a88SDuncan P. N. Exon Smith // final head scale will be used for updated the rest of the members.
5315291d2a5SDuncan P. N. Exon Smith for (const BlockNode &N : Loop.Nodes) {
5325291d2a5SDuncan P. N. Exon Smith const auto &Working = BFI.Working[N.Index];
533beaf813dSDuncan P. N. Exon Smith Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale
534beaf813dSDuncan P. N. Exon Smith : BFI.Freqs[N.Index].Scaled;
535beaf813dSDuncan P. N. Exon Smith Scaled64 New = Loop.Scale * F;
536d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => "
537d34e60caSNicola Zaghen << New << "\n");
5385291d2a5SDuncan P. N. Exon Smith F = New;
53910be9a88SDuncan P. N. Exon Smith }
54010be9a88SDuncan P. N. Exon Smith }
54110be9a88SDuncan P. N. Exon Smith
unwrapLoops()54246d9a56cSDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::unwrapLoops() {
54310be9a88SDuncan P. N. Exon Smith // Set initial frequencies from loop-local masses.
54410be9a88SDuncan P. N. Exon Smith for (size_t Index = 0; Index < Working.size(); ++Index)
545beaf813dSDuncan P. N. Exon Smith Freqs[Index].Scaled = Working[Index].Mass.toScaled();
54610be9a88SDuncan P. N. Exon Smith
547da0b21cfSDuncan P. N. Exon Smith for (LoopData &Loop : Loops)
5480633f0ecSDuncan P. N. Exon Smith unwrapLoop(*this, Loop);
54946d9a56cSDuncan P. N. Exon Smith }
55046d9a56cSDuncan P. N. Exon Smith
finalizeMetrics()55146d9a56cSDuncan P. N. Exon Smith void BlockFrequencyInfoImplBase::finalizeMetrics() {
55210be9a88SDuncan P. N. Exon Smith // Unwrap loop packages in reverse post-order, tracking min and max
55310be9a88SDuncan P. N. Exon Smith // frequencies.
554beaf813dSDuncan P. N. Exon Smith auto Min = Scaled64::getLargest();
555beaf813dSDuncan P. N. Exon Smith auto Max = Scaled64::getZero();
55610be9a88SDuncan P. N. Exon Smith for (size_t Index = 0; Index < Working.size(); ++Index) {
55746d9a56cSDuncan P. N. Exon Smith // Update min/max scale.
558beaf813dSDuncan P. N. Exon Smith Min = std::min(Min, Freqs[Index].Scaled);
559beaf813dSDuncan P. N. Exon Smith Max = std::max(Max, Freqs[Index].Scaled);
56010be9a88SDuncan P. N. Exon Smith }
56110be9a88SDuncan P. N. Exon Smith
56210be9a88SDuncan P. N. Exon Smith // Convert to integers.
56310be9a88SDuncan P. N. Exon Smith convertFloatingToInteger(*this, Min, Max);
56410be9a88SDuncan P. N. Exon Smith
56510be9a88SDuncan P. N. Exon Smith // Clean up data structures.
56610be9a88SDuncan P. N. Exon Smith cleanup(*this);
56710be9a88SDuncan P. N. Exon Smith
56810be9a88SDuncan P. N. Exon Smith // Print out the final stats.
569d34e60caSNicola Zaghen LLVM_DEBUG(dump());
57010be9a88SDuncan P. N. Exon Smith }
57110be9a88SDuncan P. N. Exon Smith
57210be9a88SDuncan P. N. Exon Smith BlockFrequency
getBlockFreq(const BlockNode & Node) const57310be9a88SDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const {
574803dd6feSHiroshi Yamauchi if (!Node.isValid()) {
575803dd6feSHiroshi Yamauchi #ifndef NDEBUG
576803dd6feSHiroshi Yamauchi if (CheckBFIUnknownBlockQueries) {
577803dd6feSHiroshi Yamauchi SmallString<256> Msg;
578803dd6feSHiroshi Yamauchi raw_svector_ostream OS(Msg);
579803dd6feSHiroshi Yamauchi OS << "*** Detected BFI query for unknown block " << getBlockName(Node);
580803dd6feSHiroshi Yamauchi report_fatal_error(OS.str());
581803dd6feSHiroshi Yamauchi }
582803dd6feSHiroshi Yamauchi #endif
58310be9a88SDuncan P. N. Exon Smith return 0;
584803dd6feSHiroshi Yamauchi }
58510be9a88SDuncan P. N. Exon Smith return Freqs[Node.Index].Integer;
58610be9a88SDuncan P. N. Exon Smith }
587ecefe5a8SEugene Zelenko
588b12b353aSXinliang David Li Optional<uint64_t>
getBlockProfileCount(const Function & F,const BlockNode & Node,bool AllowSynthetic) const589b12b353aSXinliang David Li BlockFrequencyInfoImplBase::getBlockProfileCount(const Function &F,
590499c80b8SXinliang David Li const BlockNode &Node,
591499c80b8SXinliang David Li bool AllowSynthetic) const {
592499c80b8SXinliang David Li return getProfileCountFromFreq(F, getBlockFreq(Node).getFrequency(),
593499c80b8SXinliang David Li AllowSynthetic);
594f801575fSSean Silva }
595f801575fSSean Silva
596f801575fSSean Silva Optional<uint64_t>
getProfileCountFromFreq(const Function & F,uint64_t Freq,bool AllowSynthetic) const597f801575fSSean Silva BlockFrequencyInfoImplBase::getProfileCountFromFreq(const Function &F,
598499c80b8SXinliang David Li uint64_t Freq,
599499c80b8SXinliang David Li bool AllowSynthetic) const {
600499c80b8SXinliang David Li auto EntryCount = F.getEntryCount(AllowSynthetic);
601b12b353aSXinliang David Li if (!EntryCount)
602b12b353aSXinliang David Li return None;
603b12b353aSXinliang David Li // Use 128 bit APInt to do the arithmetic to avoid overflow.
604a32c2c38SMircea Trofin APInt BlockCount(128, EntryCount->getCount());
605f801575fSSean Silva APInt BlockFreq(128, Freq);
606b12b353aSXinliang David Li APInt EntryFreq(128, getEntryFreq());
607b12b353aSXinliang David Li BlockCount *= BlockFreq;
608aca738b7SEaswaran Raman // Rounded division of BlockCount by EntryFreq. Since EntryFreq is unsigned
609aca738b7SEaswaran Raman // lshr by 1 gives EntryFreq/2.
610aca738b7SEaswaran Raman BlockCount = (BlockCount + EntryFreq.lshr(1)).udiv(EntryFreq);
611b12b353aSXinliang David Li return BlockCount.getLimitedValue();
612b12b353aSXinliang David Li }
613b12b353aSXinliang David Li
614dce9def3SHiroshi Yamauchi bool
isIrrLoopHeader(const BlockNode & Node)615dce9def3SHiroshi Yamauchi BlockFrequencyInfoImplBase::isIrrLoopHeader(const BlockNode &Node) {
616dce9def3SHiroshi Yamauchi if (!Node.isValid())
617dce9def3SHiroshi Yamauchi return false;
618dce9def3SHiroshi Yamauchi return IsIrrLoopHeader.test(Node.Index);
619dce9def3SHiroshi Yamauchi }
620dce9def3SHiroshi Yamauchi
621beaf813dSDuncan P. N. Exon Smith Scaled64
getFloatingBlockFreq(const BlockNode & Node) const62210be9a88SDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const {
62310be9a88SDuncan P. N. Exon Smith if (!Node.isValid())
624beaf813dSDuncan P. N. Exon Smith return Scaled64::getZero();
625beaf813dSDuncan P. N. Exon Smith return Freqs[Node.Index].Scaled;
62610be9a88SDuncan P. N. Exon Smith }
62710be9a88SDuncan P. N. Exon Smith
setBlockFreq(const BlockNode & Node,uint64_t Freq)62872d44b1bSManman Ren void BlockFrequencyInfoImplBase::setBlockFreq(const BlockNode &Node,
62972d44b1bSManman Ren uint64_t Freq) {
63072d44b1bSManman Ren assert(Node.isValid() && "Expected valid node");
63172d44b1bSManman Ren assert(Node.Index < Freqs.size() && "Expected legal index");
63272d44b1bSManman Ren Freqs[Node.Index].Integer = Freq;
63372d44b1bSManman Ren }
63472d44b1bSManman Ren
63510be9a88SDuncan P. N. Exon Smith std::string
getBlockName(const BlockNode & Node) const63610be9a88SDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const {
63738c02bc7SEugene Zelenko return {};
63810be9a88SDuncan P. N. Exon Smith }
639ecefe5a8SEugene Zelenko
640c5a3139eSDuncan P. N. Exon Smith std::string
getLoopName(const LoopData & Loop) const641c5a3139eSDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
642c5a3139eSDuncan P. N. Exon Smith return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
643c5a3139eSDuncan P. N. Exon Smith }
64410be9a88SDuncan P. N. Exon Smith
64510be9a88SDuncan P. N. Exon Smith raw_ostream &
printBlockFreq(raw_ostream & OS,const BlockNode & Node) const64610be9a88SDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
64710be9a88SDuncan P. N. Exon Smith const BlockNode &Node) const {
64810be9a88SDuncan P. N. Exon Smith return OS << getFloatingBlockFreq(Node);
64910be9a88SDuncan P. N. Exon Smith }
65010be9a88SDuncan P. N. Exon Smith
65110be9a88SDuncan P. N. Exon Smith raw_ostream &
printBlockFreq(raw_ostream & OS,const BlockFrequency & Freq) const65210be9a88SDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
65310be9a88SDuncan P. N. Exon Smith const BlockFrequency &Freq) const {
654beaf813dSDuncan P. N. Exon Smith Scaled64 Block(Freq.getFrequency(), 0);
655beaf813dSDuncan P. N. Exon Smith Scaled64 Entry(getEntryFreq(), 0);
65610be9a88SDuncan P. N. Exon Smith
65710be9a88SDuncan P. N. Exon Smith return OS << Block / Entry;
65810be9a88SDuncan P. N. Exon Smith }
659c5a3139eSDuncan P. N. Exon Smith
addNodesInLoop(const BFIBase::LoopData & OuterLoop)660c5a3139eSDuncan P. N. Exon Smith void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {
661c5a3139eSDuncan P. N. Exon Smith Start = OuterLoop.getHeader();
662c5a3139eSDuncan P. N. Exon Smith Nodes.reserve(OuterLoop.Nodes.size());
663c5a3139eSDuncan P. N. Exon Smith for (auto N : OuterLoop.Nodes)
664c5a3139eSDuncan P. N. Exon Smith addNode(N);
665c5a3139eSDuncan P. N. Exon Smith indexNodes();
666c5a3139eSDuncan P. N. Exon Smith }
667ecefe5a8SEugene Zelenko
addNodesInFunction()668c5a3139eSDuncan P. N. Exon Smith void IrreducibleGraph::addNodesInFunction() {
669c5a3139eSDuncan P. N. Exon Smith Start = 0;
670c5a3139eSDuncan P. N. Exon Smith for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index)
671c5a3139eSDuncan P. N. Exon Smith if (!BFI.Working[Index].isPackaged())
672c5a3139eSDuncan P. N. Exon Smith addNode(Index);
673c5a3139eSDuncan P. N. Exon Smith indexNodes();
674c5a3139eSDuncan P. N. Exon Smith }
675ecefe5a8SEugene Zelenko
indexNodes()676c5a3139eSDuncan P. N. Exon Smith void IrreducibleGraph::indexNodes() {
677c5a3139eSDuncan P. N. Exon Smith for (auto &I : Nodes)
678c5a3139eSDuncan P. N. Exon Smith Lookup[I.Node.Index] = &I;
679c5a3139eSDuncan P. N. Exon Smith }
680ecefe5a8SEugene Zelenko
addEdge(IrrNode & Irr,const BlockNode & Succ,const BFIBase::LoopData * OuterLoop)681c5a3139eSDuncan P. N. Exon Smith void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ,
682c5a3139eSDuncan P. N. Exon Smith const BFIBase::LoopData *OuterLoop) {
683c5a3139eSDuncan P. N. Exon Smith if (OuterLoop && OuterLoop->isHeader(Succ))
684c5a3139eSDuncan P. N. Exon Smith return;
685c5a3139eSDuncan P. N. Exon Smith auto L = Lookup.find(Succ.Index);
686c5a3139eSDuncan P. N. Exon Smith if (L == Lookup.end())
687c5a3139eSDuncan P. N. Exon Smith return;
688c5a3139eSDuncan P. N. Exon Smith IrrNode &SuccIrr = *L->second;
689c5a3139eSDuncan P. N. Exon Smith Irr.Edges.push_back(&SuccIrr);
690c5a3139eSDuncan P. N. Exon Smith SuccIrr.Edges.push_front(&Irr);
691c5a3139eSDuncan P. N. Exon Smith ++SuccIrr.NumIn;
692c5a3139eSDuncan P. N. Exon Smith }
693c5a3139eSDuncan P. N. Exon Smith
694c5a3139eSDuncan P. N. Exon Smith namespace llvm {
695c5a3139eSDuncan P. N. Exon Smith
69638c02bc7SEugene Zelenko template <> struct GraphTraits<IrreducibleGraph> {
69738c02bc7SEugene Zelenko using GraphT = bfi_detail::IrreducibleGraph;
69838c02bc7SEugene Zelenko using NodeRef = const GraphT::IrrNode *;
69938c02bc7SEugene Zelenko using ChildIteratorType = GraphT::IrrNode::iterator;
700c5a3139eSDuncan P. N. Exon Smith
getEntryNodellvm::GraphTraits701f2187ed3STim Shen static NodeRef getEntryNode(const GraphT &G) { return G.StartIrr; }
child_beginllvm::GraphTraits702f2187ed3STim Shen static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
child_endllvm::GraphTraits703f2187ed3STim Shen static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
704c5a3139eSDuncan P. N. Exon Smith };
70538c02bc7SEugene Zelenko
706ecefe5a8SEugene Zelenko } // end namespace llvm
707c5a3139eSDuncan P. N. Exon Smith
7085f8f34e4SAdrian Prantl /// Find extra irreducible headers.
709c5a3139eSDuncan P. N. Exon Smith ///
710c5a3139eSDuncan P. N. Exon Smith /// Find entry blocks and other blocks with backedges, which exist when \c G
711c5a3139eSDuncan P. N. Exon Smith /// contains irreducible sub-SCCs.
findIrreducibleHeaders(const BlockFrequencyInfoImplBase & BFI,const IrreducibleGraph & G,const std::vector<const IrreducibleGraph::IrrNode * > & SCC,LoopData::NodeList & Headers,LoopData::NodeList & Others)712c5a3139eSDuncan P. N. Exon Smith static void findIrreducibleHeaders(
713c5a3139eSDuncan P. N. Exon Smith const BlockFrequencyInfoImplBase &BFI,
714c5a3139eSDuncan P. N. Exon Smith const IrreducibleGraph &G,
715c5a3139eSDuncan P. N. Exon Smith const std::vector<const IrreducibleGraph::IrrNode *> &SCC,
716c5a3139eSDuncan P. N. Exon Smith LoopData::NodeList &Headers, LoopData::NodeList &Others) {
717c5a3139eSDuncan P. N. Exon Smith // Map from nodes in the SCC to whether it's an entry block.
718c5a3139eSDuncan P. N. Exon Smith SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC;
719c5a3139eSDuncan P. N. Exon Smith
720c5a3139eSDuncan P. N. Exon Smith // InSCC also acts the set of nodes in the graph. Seed it.
721c5a3139eSDuncan P. N. Exon Smith for (const auto *I : SCC)
722c5a3139eSDuncan P. N. Exon Smith InSCC[I] = false;
723c5a3139eSDuncan P. N. Exon Smith
724c5a3139eSDuncan P. N. Exon Smith for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) {
725c5a3139eSDuncan P. N. Exon Smith auto &Irr = *I->first;
726c5a3139eSDuncan P. N. Exon Smith for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
727c5a3139eSDuncan P. N. Exon Smith if (InSCC.count(P))
728c5a3139eSDuncan P. N. Exon Smith continue;
729c5a3139eSDuncan P. N. Exon Smith
730c5a3139eSDuncan P. N. Exon Smith // This is an entry block.
731c5a3139eSDuncan P. N. Exon Smith I->second = true;
732c5a3139eSDuncan P. N. Exon Smith Headers.push_back(Irr.Node);
733d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node)
734d34e60caSNicola Zaghen << "\n");
735c5a3139eSDuncan P. N. Exon Smith break;
736c5a3139eSDuncan P. N. Exon Smith }
737c5a3139eSDuncan P. N. Exon Smith }
738a7a90a2fSDuncan P. N. Exon Smith assert(Headers.size() >= 2 &&
739a7a90a2fSDuncan P. N. Exon Smith "Expected irreducible CFG; -loop-info is likely invalid");
740c5a3139eSDuncan P. N. Exon Smith if (Headers.size() == InSCC.size()) {
741c5a3139eSDuncan P. N. Exon Smith // Every block is a header.
7420cac726aSFangrui Song llvm::sort(Headers);
743c5a3139eSDuncan P. N. Exon Smith return;
744c5a3139eSDuncan P. N. Exon Smith }
745c5a3139eSDuncan P. N. Exon Smith
746c5a3139eSDuncan P. N. Exon Smith // Look for extra headers from irreducible sub-SCCs.
747c5a3139eSDuncan P. N. Exon Smith for (const auto &I : InSCC) {
748c5a3139eSDuncan P. N. Exon Smith // Entry blocks are already headers.
749c5a3139eSDuncan P. N. Exon Smith if (I.second)
750c5a3139eSDuncan P. N. Exon Smith continue;
751c5a3139eSDuncan P. N. Exon Smith
752c5a3139eSDuncan P. N. Exon Smith auto &Irr = *I.first;
753c5a3139eSDuncan P. N. Exon Smith for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
754c5a3139eSDuncan P. N. Exon Smith // Skip forward edges.
755c5a3139eSDuncan P. N. Exon Smith if (P->Node < Irr.Node)
756c5a3139eSDuncan P. N. Exon Smith continue;
757c5a3139eSDuncan P. N. Exon Smith
758c5a3139eSDuncan P. N. Exon Smith // Skip predecessors from entry blocks. These can have inverted
759c5a3139eSDuncan P. N. Exon Smith // ordering.
760c5a3139eSDuncan P. N. Exon Smith if (InSCC.lookup(P))
761c5a3139eSDuncan P. N. Exon Smith continue;
762c5a3139eSDuncan P. N. Exon Smith
763c5a3139eSDuncan P. N. Exon Smith // Store the extra header.
764c5a3139eSDuncan P. N. Exon Smith Headers.push_back(Irr.Node);
765d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node)
766d34e60caSNicola Zaghen << "\n");
767c5a3139eSDuncan P. N. Exon Smith break;
768c5a3139eSDuncan P. N. Exon Smith }
769c5a3139eSDuncan P. N. Exon Smith if (Headers.back() == Irr.Node)
770c5a3139eSDuncan P. N. Exon Smith // Added this as a header.
771c5a3139eSDuncan P. N. Exon Smith continue;
772c5a3139eSDuncan P. N. Exon Smith
773c5a3139eSDuncan P. N. Exon Smith // This is not a header.
774c5a3139eSDuncan P. N. Exon Smith Others.push_back(Irr.Node);
775d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n");
776c5a3139eSDuncan P. N. Exon Smith }
7770cac726aSFangrui Song llvm::sort(Headers);
7780cac726aSFangrui Song llvm::sort(Others);
779c5a3139eSDuncan P. N. Exon Smith }
780c5a3139eSDuncan P. N. Exon Smith
createIrreducibleLoop(BlockFrequencyInfoImplBase & BFI,const IrreducibleGraph & G,LoopData * OuterLoop,std::list<LoopData>::iterator Insert,const std::vector<const IrreducibleGraph::IrrNode * > & SCC)781c5a3139eSDuncan P. N. Exon Smith static void createIrreducibleLoop(
782c5a3139eSDuncan P. N. Exon Smith BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G,
783c5a3139eSDuncan P. N. Exon Smith LoopData *OuterLoop, std::list<LoopData>::iterator Insert,
784c5a3139eSDuncan P. N. Exon Smith const std::vector<const IrreducibleGraph::IrrNode *> &SCC) {
785c5a3139eSDuncan P. N. Exon Smith // Translate the SCC into RPO.
786d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - found-scc\n");
787c5a3139eSDuncan P. N. Exon Smith
788c5a3139eSDuncan P. N. Exon Smith LoopData::NodeList Headers;
789c5a3139eSDuncan P. N. Exon Smith LoopData::NodeList Others;
790c5a3139eSDuncan P. N. Exon Smith findIrreducibleHeaders(BFI, G, SCC, Headers, Others);
791c5a3139eSDuncan P. N. Exon Smith
792c5a3139eSDuncan P. N. Exon Smith auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(),
793c5a3139eSDuncan P. N. Exon Smith Headers.end(), Others.begin(), Others.end());
794c5a3139eSDuncan P. N. Exon Smith
795c5a3139eSDuncan P. N. Exon Smith // Update loop hierarchy.
796c5a3139eSDuncan P. N. Exon Smith for (const auto &N : Loop->Nodes)
797c5a3139eSDuncan P. N. Exon Smith if (BFI.Working[N.Index].isLoopHeader())
798c5a3139eSDuncan P. N. Exon Smith BFI.Working[N.Index].Loop->Parent = &*Loop;
799c5a3139eSDuncan P. N. Exon Smith else
800c5a3139eSDuncan P. N. Exon Smith BFI.Working[N.Index].Loop = &*Loop;
801c5a3139eSDuncan P. N. Exon Smith }
802c5a3139eSDuncan P. N. Exon Smith
803c5a3139eSDuncan P. N. Exon Smith iterator_range<std::list<LoopData>::iterator>
analyzeIrreducible(const IrreducibleGraph & G,LoopData * OuterLoop,std::list<LoopData>::iterator Insert)804c5a3139eSDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::analyzeIrreducible(
805c5a3139eSDuncan P. N. Exon Smith const IrreducibleGraph &G, LoopData *OuterLoop,
806c5a3139eSDuncan P. N. Exon Smith std::list<LoopData>::iterator Insert) {
807c5a3139eSDuncan P. N. Exon Smith assert((OuterLoop == nullptr) == (Insert == Loops.begin()));
808c5a3139eSDuncan P. N. Exon Smith auto Prev = OuterLoop ? std::prev(Insert) : Loops.end();
809c5a3139eSDuncan P. N. Exon Smith
810c5a3139eSDuncan P. N. Exon Smith for (auto I = scc_begin(G); !I.isAtEnd(); ++I) {
811c5a3139eSDuncan P. N. Exon Smith if (I->size() < 2)
812c5a3139eSDuncan P. N. Exon Smith continue;
813c5a3139eSDuncan P. N. Exon Smith
814c5a3139eSDuncan P. N. Exon Smith // Translate the SCC into RPO.
815c5a3139eSDuncan P. N. Exon Smith createIrreducibleLoop(*this, G, OuterLoop, Insert, *I);
816c5a3139eSDuncan P. N. Exon Smith }
817c5a3139eSDuncan P. N. Exon Smith
818c5a3139eSDuncan P. N. Exon Smith if (OuterLoop)
819c5a3139eSDuncan P. N. Exon Smith return make_range(std::next(Prev), Insert);
820c5a3139eSDuncan P. N. Exon Smith return make_range(Loops.begin(), Insert);
821c5a3139eSDuncan P. N. Exon Smith }
822c5a3139eSDuncan P. N. Exon Smith
823c5a3139eSDuncan P. N. Exon Smith void
updateLoopWithIrreducible(LoopData & OuterLoop)824c5a3139eSDuncan P. N. Exon Smith BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) {
825c5a3139eSDuncan P. N. Exon Smith OuterLoop.Exits.clear();
8269a779623SDiego Novillo for (auto &Mass : OuterLoop.BackedgeMass)
8279a779623SDiego Novillo Mass = BlockMass::getEmpty();
828c5a3139eSDuncan P. N. Exon Smith auto O = OuterLoop.Nodes.begin() + 1;
829c5a3139eSDuncan P. N. Exon Smith for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I)
830c5a3139eSDuncan P. N. Exon Smith if (!Working[I->Index].isPackaged())
831c5a3139eSDuncan P. N. Exon Smith *O++ = *I;
832c5a3139eSDuncan P. N. Exon Smith OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end());
833c5a3139eSDuncan P. N. Exon Smith }
8349a779623SDiego Novillo
adjustLoopHeaderMass(LoopData & Loop)8359a779623SDiego Novillo void BlockFrequencyInfoImplBase::adjustLoopHeaderMass(LoopData &Loop) {
8369a779623SDiego Novillo assert(Loop.isIrreducible() && "this only makes sense on irreducible loops");
8379a779623SDiego Novillo
8389a779623SDiego Novillo // Since the loop has more than one header block, the mass flowing back into
8399a779623SDiego Novillo // each header will be different. Adjust the mass in each header loop to
8409a779623SDiego Novillo // reflect the masses flowing through back edges.
8419a779623SDiego Novillo //
8429a779623SDiego Novillo // To do this, we distribute the initial mass using the backedge masses
8439a779623SDiego Novillo // as weights for the distribution.
8449a779623SDiego Novillo BlockMass LoopMass = BlockMass::getFull();
8459a779623SDiego Novillo Distribution Dist;
8469a779623SDiego Novillo
847d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "adjust-loop-header-mass:\n");
8489a779623SDiego Novillo for (uint32_t H = 0; H < Loop.NumHeaders; ++H) {
8499a779623SDiego Novillo auto &HeaderNode = Loop.Nodes[H];
8508c49a572SDiego Novillo auto &BackedgeMass = Loop.BackedgeMass[Loop.getHeaderIndex(HeaderNode)];
851d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " - Add back edge mass for node "
852d34e60caSNicola Zaghen << getBlockName(HeaderNode) << ": " << BackedgeMass
853d34e60caSNicola Zaghen << "\n");
854f9aa39b0SDiego Novillo if (BackedgeMass.getMass() > 0)
8559a779623SDiego Novillo Dist.addLocal(HeaderNode, BackedgeMass.getMass());
856f9aa39b0SDiego Novillo else
857d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Nothing added. Back edge mass is zero\n");
8589a779623SDiego Novillo }
8599a779623SDiego Novillo
8609a779623SDiego Novillo DitheringDistributer D(Dist, LoopMass);
8619a779623SDiego Novillo
862d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Distribute loop mass " << LoopMass
8639a779623SDiego Novillo << " to headers using above weights\n");
8649a779623SDiego Novillo for (const Weight &W : Dist.Weights) {
8659a779623SDiego Novillo BlockMass Taken = D.takeMass(W.Amount);
8669a779623SDiego Novillo assert(W.Type == Weight::Local && "all weights should be local");
8679a779623SDiego Novillo Working[W.TargetNode.Index].getMass() = Taken;
868d34e60caSNicola Zaghen LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
8699a779623SDiego Novillo }
8709a779623SDiego Novillo }
871dce9def3SHiroshi Yamauchi
distributeIrrLoopHeaderMass(Distribution & Dist)872dce9def3SHiroshi Yamauchi void BlockFrequencyInfoImplBase::distributeIrrLoopHeaderMass(Distribution &Dist) {
873dce9def3SHiroshi Yamauchi BlockMass LoopMass = BlockMass::getFull();
874dce9def3SHiroshi Yamauchi DitheringDistributer D(Dist, LoopMass);
875dce9def3SHiroshi Yamauchi for (const Weight &W : Dist.Weights) {
876dce9def3SHiroshi Yamauchi BlockMass Taken = D.takeMass(W.Amount);
877dce9def3SHiroshi Yamauchi assert(W.Type == Weight::Local && "all weights should be local");
878dce9def3SHiroshi Yamauchi Working[W.TargetNode.Index].getMass() = Taken;
879d34e60caSNicola Zaghen LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
880dce9def3SHiroshi Yamauchi }
881dce9def3SHiroshi Yamauchi }
882