Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 1 | //===- BlockFrequencyImplInfo.cpp - Block Frequency Info Implementation ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Loops should be simplified before this analysis. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APInt.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/GraphTraits.h" |
| 17 | #include "llvm/ADT/None.h" |
Duncan P. N. Exon Smith | 87c40fd | 2014-05-06 01:57:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SCCIterator.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 19 | #include "llvm/Config/llvm-config.h" |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/BlockFrequency.h" |
| 22 | #include "llvm/Support/BranchProbability.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ScaledNumber.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cassert> |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <iterator> |
| 33 | #include <list> |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 34 | #include <numeric> |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 35 | #include <utility> |
| 36 | #include <vector> |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 39 | using namespace llvm::bfi_detail; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 40 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "block-freq" |
| 42 | |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 43 | ScaledNumber<uint64_t> BlockMass::toScaled() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 44 | if (isFull()) |
Duncan P. N. Exon Smith | c379c87 | 2014-06-23 23:36:17 +0000 | [diff] [blame] | 45 | return ScaledNumber<uint64_t>(1, 0); |
| 46 | return ScaledNumber<uint64_t>(getMass() + 1, -64); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 49 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 50 | LLVM_DUMP_METHOD void BlockMass::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 51 | #endif |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 52 | |
| 53 | static char getHexDigit(int N) { |
| 54 | assert(N < 16); |
| 55 | if (N < 10) |
| 56 | return '0' + N; |
| 57 | return 'a' + N - 10; |
| 58 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 59 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 60 | raw_ostream &BlockMass::print(raw_ostream &OS) const { |
| 61 | for (int Digits = 0; Digits < 16; ++Digits) |
| 62 | OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf); |
| 63 | return OS; |
| 64 | } |
| 65 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 66 | namespace { |
| 67 | |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 68 | using BlockNode = BlockFrequencyInfoImplBase::BlockNode; |
| 69 | using Distribution = BlockFrequencyInfoImplBase::Distribution; |
| 70 | using WeightList = BlockFrequencyInfoImplBase::Distribution::WeightList; |
| 71 | using Scaled64 = BlockFrequencyInfoImplBase::Scaled64; |
| 72 | using LoopData = BlockFrequencyInfoImplBase::LoopData; |
| 73 | using Weight = BlockFrequencyInfoImplBase::Weight; |
| 74 | using FrequencyData = BlockFrequencyInfoImplBase::FrequencyData; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 75 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 76 | /// Dithering mass distributer. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 77 | /// |
| 78 | /// This class splits up a single mass into portions by weight, dithering to |
| 79 | /// spread out error. No mass is lost. The dithering precision depends on the |
| 80 | /// precision of the product of \a BlockMass and \a BranchProbability. |
| 81 | /// |
| 82 | /// The distribution algorithm follows. |
| 83 | /// |
| 84 | /// 1. Initialize by saving the sum of the weights in \a RemWeight and the |
| 85 | /// mass to distribute in \a RemMass. |
| 86 | /// |
| 87 | /// 2. For each portion: |
| 88 | /// |
| 89 | /// 1. Construct a branch probability, P, as the portion's weight divided |
| 90 | /// by the current value of \a RemWeight. |
| 91 | /// 2. Calculate the portion's mass as \a RemMass times P. |
| 92 | /// 3. Update \a RemWeight and \a RemMass at each portion by subtracting |
| 93 | /// the current portion's weight and mass. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 94 | struct DitheringDistributer { |
| 95 | uint32_t RemWeight; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 96 | BlockMass RemMass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 97 | |
| 98 | DitheringDistributer(Distribution &Dist, const BlockMass &Mass); |
| 99 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 100 | BlockMass takeMass(uint32_t Weight); |
| 101 | }; |
Duncan P. N. Exon Smith | b5650e5 | 2014-07-11 23:56:50 +0000 | [diff] [blame] | 102 | |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 103 | } // end anonymous namespace |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 104 | |
| 105 | DitheringDistributer::DitheringDistributer(Distribution &Dist, |
| 106 | const BlockMass &Mass) { |
| 107 | Dist.normalize(); |
| 108 | RemWeight = Dist.Total; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 109 | RemMass = Mass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 112 | BlockMass DitheringDistributer::takeMass(uint32_t Weight) { |
| 113 | assert(Weight && "invalid weight"); |
| 114 | assert(Weight <= RemWeight); |
| 115 | BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight); |
| 116 | |
| 117 | // Decrement totals (dither). |
| 118 | RemWeight -= Weight; |
| 119 | RemMass -= Mass; |
| 120 | return Mass; |
| 121 | } |
| 122 | |
| 123 | void Distribution::add(const BlockNode &Node, uint64_t Amount, |
| 124 | Weight::DistType Type) { |
| 125 | assert(Amount && "invalid weight of 0"); |
| 126 | uint64_t NewTotal = Total + Amount; |
| 127 | |
| 128 | // Check for overflow. It should be impossible to overflow twice. |
| 129 | bool IsOverflow = NewTotal < Total; |
| 130 | assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow"); |
| 131 | DidOverflow |= IsOverflow; |
| 132 | |
| 133 | // Update the total. |
| 134 | Total = NewTotal; |
| 135 | |
| 136 | // Save the weight. |
Duncan P. N. Exon Smith | 6075510 | 2014-07-12 00:26:00 +0000 | [diff] [blame] | 137 | Weights.push_back(Weight(Type, Node, Amount)); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void combineWeight(Weight &W, const Weight &OtherW) { |
| 141 | assert(OtherW.TargetNode.isValid()); |
| 142 | if (!W.Amount) { |
| 143 | W = OtherW; |
| 144 | return; |
| 145 | } |
| 146 | assert(W.Type == OtherW.Type); |
| 147 | assert(W.TargetNode == OtherW.TargetNode); |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 148 | assert(OtherW.Amount && "Expected non-zero weight"); |
| 149 | if (W.Amount > W.Amount + OtherW.Amount) |
| 150 | // Saturate on overflow. |
| 151 | W.Amount = UINT64_MAX; |
| 152 | else |
| 153 | W.Amount += OtherW.Amount; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 154 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 155 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 156 | static void combineWeightsBySorting(WeightList &Weights) { |
| 157 | // Sort so edges to the same node are adjacent. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 158 | llvm::sort(Weights, [](const Weight &L, const Weight &R) { |
| 159 | return L.TargetNode < R.TargetNode; |
| 160 | }); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 161 | |
| 162 | // Combine adjacent edges. |
| 163 | WeightList::iterator O = Weights.begin(); |
| 164 | for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E; |
| 165 | ++O, (I = L)) { |
| 166 | *O = *I; |
| 167 | |
| 168 | // Find the adjacent weights to the same node. |
| 169 | for (++L; L != E && I->TargetNode == L->TargetNode; ++L) |
| 170 | combineWeight(*O, *L); |
| 171 | } |
| 172 | |
| 173 | // Erase extra entries. |
| 174 | Weights.erase(O, Weights.end()); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 175 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 176 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 177 | static void combineWeightsByHashing(WeightList &Weights) { |
| 178 | // Collect weights into a DenseMap. |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 179 | using HashTable = DenseMap<BlockNode::IndexType, Weight>; |
| 180 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 181 | HashTable Combined(NextPowerOf2(2 * Weights.size())); |
| 182 | for (const Weight &W : Weights) |
| 183 | combineWeight(Combined[W.TargetNode.Index], W); |
| 184 | |
| 185 | // Check whether anything changed. |
| 186 | if (Weights.size() == Combined.size()) |
| 187 | return; |
| 188 | |
| 189 | // Fill in the new weights. |
| 190 | Weights.clear(); |
| 191 | Weights.reserve(Combined.size()); |
| 192 | for (const auto &I : Combined) |
| 193 | Weights.push_back(I.second); |
| 194 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 195 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 196 | static void combineWeights(WeightList &Weights) { |
| 197 | // Use a hash table for many successors to keep this linear. |
| 198 | if (Weights.size() > 128) { |
| 199 | combineWeightsByHashing(Weights); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | combineWeightsBySorting(Weights); |
| 204 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 205 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 206 | static uint64_t shiftRightAndRound(uint64_t N, int Shift) { |
| 207 | assert(Shift >= 0); |
| 208 | assert(Shift < 64); |
| 209 | if (!Shift) |
| 210 | return N; |
| 211 | return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1)); |
| 212 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 213 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 214 | void Distribution::normalize() { |
| 215 | // Early exit for termination nodes. |
| 216 | if (Weights.empty()) |
| 217 | return; |
| 218 | |
| 219 | // Only bother if there are multiple successors. |
| 220 | if (Weights.size() > 1) |
| 221 | combineWeights(Weights); |
| 222 | |
| 223 | // Early exit when combined into a single successor. |
| 224 | if (Weights.size() == 1) { |
| 225 | Total = 1; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 226 | Weights.front().Amount = 1; |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | // Determine how much to shift right so that the total fits into 32-bits. |
| 231 | // |
| 232 | // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1 |
| 233 | // for each weight can cause a 32-bit overflow. |
| 234 | int Shift = 0; |
| 235 | if (DidOverflow) |
| 236 | Shift = 33; |
| 237 | else if (Total > UINT32_MAX) |
| 238 | Shift = 33 - countLeadingZeros(Total); |
| 239 | |
| 240 | // Early exit if nothing needs to be scaled. |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 241 | if (!Shift) { |
| 242 | // If we didn't overflow then combineWeights() shouldn't have changed the |
| 243 | // sum of the weights, but let's double-check. |
| 244 | assert(Total == std::accumulate(Weights.begin(), Weights.end(), UINT64_C(0), |
| 245 | [](uint64_t Sum, const Weight &W) { |
| 246 | return Sum + W.Amount; |
| 247 | }) && |
| 248 | "Expected total to be correct"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 249 | return; |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 250 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 251 | |
| 252 | // Recompute the total through accumulation (rather than shifting it) so that |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 253 | // it's accurate after shifting and any changes combineWeights() made above. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 254 | Total = 0; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 255 | |
| 256 | // Sum the weights to each node and shift right if necessary. |
| 257 | for (Weight &W : Weights) { |
| 258 | // Scale down below UINT32_MAX. Since Shift is larger than necessary, we |
| 259 | // can round here without concern about overflow. |
| 260 | assert(W.TargetNode.isValid()); |
| 261 | W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift)); |
| 262 | assert(W.Amount <= UINT32_MAX); |
| 263 | |
| 264 | // Update the total. |
| 265 | Total += W.Amount; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 266 | } |
| 267 | assert(Total <= UINT32_MAX); |
| 268 | } |
| 269 | |
| 270 | void BlockFrequencyInfoImplBase::clear() { |
Duncan P. N. Exon Smith | dc2d66e | 2014-04-22 03:31:34 +0000 | [diff] [blame] | 271 | // Swap with a default-constructed std::vector, since std::vector<>::clear() |
| 272 | // does not actually clear heap storage. |
| 273 | std::vector<FrequencyData>().swap(Freqs); |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 274 | IsIrrLoopHeader.clear(); |
Duncan P. N. Exon Smith | dc2d66e | 2014-04-22 03:31:34 +0000 | [diff] [blame] | 275 | std::vector<WorkingData>().swap(Working); |
Duncan P. N. Exon Smith | fc7dc93 | 2014-04-25 04:30:06 +0000 | [diff] [blame] | 276 | Loops.clear(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 279 | /// Clear all memory not needed downstream. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 280 | /// |
| 281 | /// Releases all memory not used downstream. In particular, saves Freqs. |
| 282 | static void cleanup(BlockFrequencyInfoImplBase &BFI) { |
| 283 | std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs)); |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 284 | SparseBitVector<> SavedIsIrrLoopHeader(std::move(BFI.IsIrrLoopHeader)); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 285 | BFI.clear(); |
| 286 | BFI.Freqs = std::move(SavedFreqs); |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 287 | BFI.IsIrrLoopHeader = std::move(SavedIsIrrLoopHeader); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 290 | bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist, |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 291 | const LoopData *OuterLoop, |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 292 | const BlockNode &Pred, |
| 293 | const BlockNode &Succ, |
| 294 | uint64_t Weight) { |
| 295 | if (!Weight) |
| 296 | Weight = 1; |
| 297 | |
Duncan P. N. Exon Smith | 39cc648 | 2014-04-25 04:38:06 +0000 | [diff] [blame] | 298 | auto isLoopHeader = [&OuterLoop](const BlockNode &Node) { |
| 299 | return OuterLoop && OuterLoop->isHeader(Node); |
| 300 | }; |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 301 | |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 302 | BlockNode Resolved = Working[Succ.Index].getResolvedNode(); |
| 303 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 304 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 305 | auto debugSuccessor = [&](const char *Type) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 306 | dbgs() << " =>" |
| 307 | << " [" << Type << "] weight = " << Weight; |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 308 | if (!isLoopHeader(Resolved)) |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 309 | dbgs() << ", succ = " << getBlockName(Succ); |
| 310 | if (Resolved != Succ) |
| 311 | dbgs() << ", resolved = " << getBlockName(Resolved); |
| 312 | dbgs() << "\n"; |
| 313 | }; |
| 314 | (void)debugSuccessor; |
| 315 | #endif |
| 316 | |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 317 | if (isLoopHeader(Resolved)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 318 | LLVM_DEBUG(debugSuccessor("backedge")); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 319 | Dist.addBackedge(Resolved, Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 320 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 321 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 322 | |
Duncan P. N. Exon Smith | 39cc648 | 2014-04-25 04:38:06 +0000 | [diff] [blame] | 323 | if (Working[Resolved.Index].getContainingLoop() != OuterLoop) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 324 | LLVM_DEBUG(debugSuccessor(" exit ")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 325 | Dist.addExit(Resolved, Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 326 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Duncan P. N. Exon Smith | b3380ea | 2014-04-22 03:31:53 +0000 | [diff] [blame] | 329 | if (Resolved < Pred) { |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 330 | if (!isLoopHeader(Pred)) { |
| 331 | // If OuterLoop is an irreducible loop, we can't actually handle this. |
| 332 | assert((!OuterLoop || !OuterLoop->isIrreducible()) && |
| 333 | "unhandled irreducible control flow"); |
| 334 | |
| 335 | // Irreducible backedge. Abort. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 336 | LLVM_DEBUG(debugSuccessor("abort!!!")); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | // If "Pred" is a loop header, then this isn't really a backedge; rather, |
| 341 | // OuterLoop must be irreducible. These false backedges can come only from |
| 342 | // secondary loop headers. |
| 343 | assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) && |
| 344 | "unhandled irreducible control flow"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 347 | LLVM_DEBUG(debugSuccessor(" local ")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 348 | Dist.addLocal(Resolved, Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 349 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 352 | bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist( |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 353 | const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 354 | // Copy the exit map into Dist. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 355 | for (const auto &I : Loop.Exits) |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 356 | if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first, |
| 357 | I.second.getMass())) |
| 358 | // Irreducible backedge. |
| 359 | return false; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 360 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 361 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 364 | /// Compute the loop scale for a loop. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 365 | void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 366 | // Compute loop scale. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 367 | LLVM_DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 368 | |
Diego Novillo | a354f48 | 2015-04-01 17:42:27 +0000 | [diff] [blame] | 369 | // Infinite loops need special handling. If we give the back edge an infinite |
| 370 | // mass, they may saturate all the other scales in the function down to 1, |
| 371 | // making all the other region temperatures look exactly the same. Choose an |
| 372 | // arbitrary scale to avoid these issues. |
| 373 | // |
| 374 | // FIXME: An alternate way would be to select a symbolic scale which is later |
| 375 | // replaced to be the maximum of all computed scales plus 1. This would |
| 376 | // appropriately describe the loop as having a large scale, without skewing |
| 377 | // the final frequency computation. |
Sanjay Patel | 0fb9880 | 2016-05-09 16:07:45 +0000 | [diff] [blame] | 378 | const Scaled64 InfiniteLoopScale(1, 12); |
Diego Novillo | a354f48 | 2015-04-01 17:42:27 +0000 | [diff] [blame] | 379 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 380 | // LoopScale == 1 / ExitMass |
| 381 | // ExitMass == HeadMass - BackedgeMass |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 382 | BlockMass TotalBackedgeMass; |
| 383 | for (auto &Mass : Loop.BackedgeMass) |
| 384 | TotalBackedgeMass += Mass; |
| 385 | BlockMass ExitMass = BlockMass::getFull() - TotalBackedgeMass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 386 | |
Diego Novillo | a354f48 | 2015-04-01 17:42:27 +0000 | [diff] [blame] | 387 | // Block scale stores the inverse of the scale. If this is an infinite loop, |
| 388 | // its exit mass will be zero. In this case, use an arbitrary scale for the |
| 389 | // loop scale. |
| 390 | Loop.Scale = |
Sanjay Patel | 0fb9880 | 2016-05-09 16:07:45 +0000 | [diff] [blame] | 391 | ExitMass.isEmpty() ? InfiniteLoopScale : ExitMass.toScaled().inverse(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 392 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 393 | LLVM_DEBUG(dbgs() << " - exit-mass = " << ExitMass << " (" |
| 394 | << BlockMass::getFull() << " - " << TotalBackedgeMass |
| 395 | << ")\n" |
| 396 | << " - scale = " << Loop.Scale << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 399 | /// Package up a loop. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 400 | void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 401 | LLVM_DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 402 | |
| 403 | // Clear the subloop exits to prevent quadratic memory usage. |
| 404 | for (const BlockNode &M : Loop.Nodes) { |
| 405 | if (auto *Loop = Working[M.Index].getPackagedLoop()) |
| 406 | Loop->Exits.clear(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 407 | LLVM_DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 408 | } |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 409 | Loop.IsPackaged = true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 412 | #ifndef NDEBUG |
| 413 | static void debugAssign(const BlockFrequencyInfoImplBase &BFI, |
| 414 | const DitheringDistributer &D, const BlockNode &T, |
| 415 | const BlockMass &M, const char *Desc) { |
| 416 | dbgs() << " => assign " << M << " (" << D.RemMass << ")"; |
| 417 | if (Desc) |
| 418 | dbgs() << " [" << Desc << "]"; |
| 419 | if (T.isValid()) |
| 420 | dbgs() << " to " << BFI.getBlockName(T); |
| 421 | dbgs() << "\n"; |
| 422 | } |
| 423 | #endif |
| 424 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 425 | void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source, |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 426 | LoopData *OuterLoop, |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 427 | Distribution &Dist) { |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 428 | BlockMass Mass = Working[Source.Index].getMass(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 429 | LLVM_DEBUG(dbgs() << " => mass: " << Mass << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 430 | |
| 431 | // Distribute mass to successors as laid out in Dist. |
| 432 | DitheringDistributer D(Dist, Mass); |
| 433 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 434 | for (const Weight &W : Dist.Weights) { |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 435 | // Check for a local edge (non-backedge and non-exit). |
| 436 | BlockMass Taken = D.takeMass(W.Amount); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 437 | if (W.Type == Weight::Local) { |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 438 | Working[W.TargetNode.Index].getMass() += Taken; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 439 | LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr)); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | |
| 443 | // Backedges and exits only make sense if we're processing a loop. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 444 | assert(OuterLoop && "backedge or exit outside of loop"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 445 | |
| 446 | // Check for a backedge. |
| 447 | if (W.Type == Weight::Backedge) { |
Diego Novillo | 8c49a57 | 2015-06-17 16:28:22 +0000 | [diff] [blame] | 448 | OuterLoop->BackedgeMass[OuterLoop->getHeaderIndex(W.TargetNode)] += Taken; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 449 | LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "back")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 450 | continue; |
| 451 | } |
| 452 | |
| 453 | // This must be an exit. |
| 454 | assert(W.Type == Weight::Exit); |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 455 | OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 456 | LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "exit")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI, |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 461 | const Scaled64 &Min, const Scaled64 &Max) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 462 | // Scale the Factor to a size that creates integers. Ideally, integers would |
| 463 | // be scaled so that Max == UINT64_MAX so that they can be best |
Diego Novillo | a354f48 | 2015-04-01 17:42:27 +0000 | [diff] [blame] | 464 | // differentiated. However, in the presence of large frequency values, small |
| 465 | // frequencies are scaled down to 1, making it impossible to differentiate |
| 466 | // small, unequal numbers. When the spread between Min and Max frequencies |
| 467 | // fits well within MaxBits, we make the scale be at least 8. |
| 468 | const unsigned MaxBits = 64; |
| 469 | const unsigned SpreadBits = (Max / Min).lg(); |
| 470 | Scaled64 ScalingFactor; |
| 471 | if (SpreadBits <= MaxBits - 3) { |
| 472 | // If the values are small enough, make the scaling factor at least 8 to |
| 473 | // allow distinguishing small values. |
| 474 | ScalingFactor = Min.inverse(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 475 | ScalingFactor <<= 3; |
Diego Novillo | a354f48 | 2015-04-01 17:42:27 +0000 | [diff] [blame] | 476 | } else { |
| 477 | // If the values need more than MaxBits to be represented, saturate small |
| 478 | // frequency values down to 1 by using a scaling factor that benefits large |
| 479 | // frequency values. |
| 480 | ScalingFactor = Scaled64(1, MaxBits) / Max; |
| 481 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 482 | |
| 483 | // Translate the floats to integers. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 484 | LLVM_DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max |
| 485 | << ", factor = " << ScalingFactor << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 486 | for (size_t Index = 0; Index < BFI.Freqs.size(); ++Index) { |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 487 | Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 488 | BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 489 | LLVM_DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = " |
| 490 | << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled |
| 491 | << ", int = " << BFI.Freqs[Index].Integer << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 495 | /// Unwrap a loop package. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 496 | /// |
| 497 | /// Visits all the members of a loop, adjusting their BlockData according to |
| 498 | /// the loop's pseudo-node. |
Duncan P. N. Exon Smith | 0633f0e | 2014-04-25 04:38:25 +0000 | [diff] [blame] | 499 | static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 500 | LLVM_DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop) |
| 501 | << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale |
| 502 | << "\n"); |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 503 | Loop.Scale *= Loop.Mass.toScaled(); |
Duncan P. N. Exon Smith | 5291d2a | 2014-04-25 04:38:27 +0000 | [diff] [blame] | 504 | Loop.IsPackaged = false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 505 | LLVM_DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 506 | |
| 507 | // Propagate the head scale through the loop. Since members are visited in |
| 508 | // RPO, the head scale will be updated by the loop scale first, and then the |
| 509 | // final head scale will be used for updated the rest of the members. |
Duncan P. N. Exon Smith | 5291d2a | 2014-04-25 04:38:27 +0000 | [diff] [blame] | 510 | for (const BlockNode &N : Loop.Nodes) { |
| 511 | const auto &Working = BFI.Working[N.Index]; |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 512 | Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale |
| 513 | : BFI.Freqs[N.Index].Scaled; |
| 514 | Scaled64 New = Loop.Scale * F; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 515 | LLVM_DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => " |
| 516 | << New << "\n"); |
Duncan P. N. Exon Smith | 5291d2a | 2014-04-25 04:38:27 +0000 | [diff] [blame] | 517 | F = New; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 521 | void BlockFrequencyInfoImplBase::unwrapLoops() { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 522 | // Set initial frequencies from loop-local masses. |
| 523 | for (size_t Index = 0; Index < Working.size(); ++Index) |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 524 | Freqs[Index].Scaled = Working[Index].Mass.toScaled(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 525 | |
Duncan P. N. Exon Smith | da0b21c | 2014-04-25 04:38:23 +0000 | [diff] [blame] | 526 | for (LoopData &Loop : Loops) |
Duncan P. N. Exon Smith | 0633f0e | 2014-04-25 04:38:25 +0000 | [diff] [blame] | 527 | unwrapLoop(*this, Loop); |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void BlockFrequencyInfoImplBase::finalizeMetrics() { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 531 | // Unwrap loop packages in reverse post-order, tracking min and max |
| 532 | // frequencies. |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 533 | auto Min = Scaled64::getLargest(); |
| 534 | auto Max = Scaled64::getZero(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 535 | for (size_t Index = 0; Index < Working.size(); ++Index) { |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 536 | // Update min/max scale. |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 537 | Min = std::min(Min, Freqs[Index].Scaled); |
| 538 | Max = std::max(Max, Freqs[Index].Scaled); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // Convert to integers. |
| 542 | convertFloatingToInteger(*this, Min, Max); |
| 543 | |
| 544 | // Clean up data structures. |
| 545 | cleanup(*this); |
| 546 | |
| 547 | // Print out the final stats. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 548 | LLVM_DEBUG(dump()); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | BlockFrequency |
| 552 | BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const { |
| 553 | if (!Node.isValid()) |
| 554 | return 0; |
| 555 | return Freqs[Node.Index].Integer; |
| 556 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 557 | |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 558 | Optional<uint64_t> |
| 559 | BlockFrequencyInfoImplBase::getBlockProfileCount(const Function &F, |
Xinliang David Li | 499c80b | 2019-04-24 19:51:16 +0000 | [diff] [blame^] | 560 | const BlockNode &Node, |
| 561 | bool AllowSynthetic) const { |
| 562 | return getProfileCountFromFreq(F, getBlockFreq(Node).getFrequency(), |
| 563 | AllowSynthetic); |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | Optional<uint64_t> |
| 567 | BlockFrequencyInfoImplBase::getProfileCountFromFreq(const Function &F, |
Xinliang David Li | 499c80b | 2019-04-24 19:51:16 +0000 | [diff] [blame^] | 568 | uint64_t Freq, |
| 569 | bool AllowSynthetic) const { |
| 570 | auto EntryCount = F.getEntryCount(AllowSynthetic); |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 571 | if (!EntryCount) |
| 572 | return None; |
| 573 | // Use 128 bit APInt to do the arithmetic to avoid overflow. |
Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 574 | APInt BlockCount(128, EntryCount.getCount()); |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 575 | APInt BlockFreq(128, Freq); |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 576 | APInt EntryFreq(128, getEntryFreq()); |
| 577 | BlockCount *= BlockFreq; |
Easwaran Raman | aca738b | 2018-08-16 00:26:59 +0000 | [diff] [blame] | 578 | // Rounded division of BlockCount by EntryFreq. Since EntryFreq is unsigned |
| 579 | // lshr by 1 gives EntryFreq/2. |
| 580 | BlockCount = (BlockCount + EntryFreq.lshr(1)).udiv(EntryFreq); |
Xinliang David Li | b12b353 | 2016-06-22 17:12:12 +0000 | [diff] [blame] | 581 | return BlockCount.getLimitedValue(); |
| 582 | } |
| 583 | |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 584 | bool |
| 585 | BlockFrequencyInfoImplBase::isIrrLoopHeader(const BlockNode &Node) { |
| 586 | if (!Node.isValid()) |
| 587 | return false; |
| 588 | return IsIrrLoopHeader.test(Node.Index); |
| 589 | } |
| 590 | |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 591 | Scaled64 |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 592 | BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const { |
| 593 | if (!Node.isValid()) |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 594 | return Scaled64::getZero(); |
| 595 | return Freqs[Node.Index].Scaled; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Manman Ren | 72d44b1 | 2015-10-15 14:59:40 +0000 | [diff] [blame] | 598 | void BlockFrequencyInfoImplBase::setBlockFreq(const BlockNode &Node, |
| 599 | uint64_t Freq) { |
| 600 | assert(Node.isValid() && "Expected valid node"); |
| 601 | assert(Node.Index < Freqs.size() && "Expected legal index"); |
| 602 | Freqs[Node.Index].Integer = Freq; |
| 603 | } |
| 604 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 605 | std::string |
| 606 | BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const { |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 607 | return {}; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 608 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 609 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 610 | std::string |
| 611 | BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const { |
| 612 | return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*"); |
| 613 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 614 | |
| 615 | raw_ostream & |
| 616 | BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS, |
| 617 | const BlockNode &Node) const { |
| 618 | return OS << getFloatingBlockFreq(Node); |
| 619 | } |
| 620 | |
| 621 | raw_ostream & |
| 622 | BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS, |
| 623 | const BlockFrequency &Freq) const { |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 624 | Scaled64 Block(Freq.getFrequency(), 0); |
| 625 | Scaled64 Entry(getEntryFreq(), 0); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 626 | |
| 627 | return OS << Block / Entry; |
| 628 | } |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 629 | |
| 630 | void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) { |
| 631 | Start = OuterLoop.getHeader(); |
| 632 | Nodes.reserve(OuterLoop.Nodes.size()); |
| 633 | for (auto N : OuterLoop.Nodes) |
| 634 | addNode(N); |
| 635 | indexNodes(); |
| 636 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 637 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 638 | void IrreducibleGraph::addNodesInFunction() { |
| 639 | Start = 0; |
| 640 | for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index) |
| 641 | if (!BFI.Working[Index].isPackaged()) |
| 642 | addNode(Index); |
| 643 | indexNodes(); |
| 644 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 645 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 646 | void IrreducibleGraph::indexNodes() { |
| 647 | for (auto &I : Nodes) |
| 648 | Lookup[I.Node.Index] = &I; |
| 649 | } |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 650 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 651 | void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ, |
| 652 | const BFIBase::LoopData *OuterLoop) { |
| 653 | if (OuterLoop && OuterLoop->isHeader(Succ)) |
| 654 | return; |
| 655 | auto L = Lookup.find(Succ.Index); |
| 656 | if (L == Lookup.end()) |
| 657 | return; |
| 658 | IrrNode &SuccIrr = *L->second; |
| 659 | Irr.Edges.push_back(&SuccIrr); |
| 660 | SuccIrr.Edges.push_front(&Irr); |
| 661 | ++SuccIrr.NumIn; |
| 662 | } |
| 663 | |
| 664 | namespace llvm { |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 665 | |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 666 | template <> struct GraphTraits<IrreducibleGraph> { |
| 667 | using GraphT = bfi_detail::IrreducibleGraph; |
| 668 | using NodeRef = const GraphT::IrrNode *; |
| 669 | using ChildIteratorType = GraphT::IrrNode::iterator; |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 670 | |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 671 | static NodeRef getEntryNode(const GraphT &G) { return G.StartIrr; } |
| 672 | static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); } |
| 673 | static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); } |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 674 | }; |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 675 | |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 676 | } // end namespace llvm |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 677 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 678 | /// Find extra irreducible headers. |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 679 | /// |
| 680 | /// Find entry blocks and other blocks with backedges, which exist when \c G |
| 681 | /// contains irreducible sub-SCCs. |
| 682 | static void findIrreducibleHeaders( |
| 683 | const BlockFrequencyInfoImplBase &BFI, |
| 684 | const IrreducibleGraph &G, |
| 685 | const std::vector<const IrreducibleGraph::IrrNode *> &SCC, |
| 686 | LoopData::NodeList &Headers, LoopData::NodeList &Others) { |
| 687 | // Map from nodes in the SCC to whether it's an entry block. |
| 688 | SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC; |
| 689 | |
| 690 | // InSCC also acts the set of nodes in the graph. Seed it. |
| 691 | for (const auto *I : SCC) |
| 692 | InSCC[I] = false; |
| 693 | |
| 694 | for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) { |
| 695 | auto &Irr = *I->first; |
| 696 | for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) { |
| 697 | if (InSCC.count(P)) |
| 698 | continue; |
| 699 | |
| 700 | // This is an entry block. |
| 701 | I->second = true; |
| 702 | Headers.push_back(Irr.Node); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 703 | LLVM_DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node) |
| 704 | << "\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | } |
Duncan P. N. Exon Smith | a7a90a2 | 2014-10-06 17:42:00 +0000 | [diff] [blame] | 708 | assert(Headers.size() >= 2 && |
| 709 | "Expected irreducible CFG; -loop-info is likely invalid"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 710 | if (Headers.size() == InSCC.size()) { |
| 711 | // Every block is a header. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 712 | llvm::sort(Headers); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
| 716 | // Look for extra headers from irreducible sub-SCCs. |
| 717 | for (const auto &I : InSCC) { |
| 718 | // Entry blocks are already headers. |
| 719 | if (I.second) |
| 720 | continue; |
| 721 | |
| 722 | auto &Irr = *I.first; |
| 723 | for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) { |
| 724 | // Skip forward edges. |
| 725 | if (P->Node < Irr.Node) |
| 726 | continue; |
| 727 | |
| 728 | // Skip predecessors from entry blocks. These can have inverted |
| 729 | // ordering. |
| 730 | if (InSCC.lookup(P)) |
| 731 | continue; |
| 732 | |
| 733 | // Store the extra header. |
| 734 | Headers.push_back(Irr.Node); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 735 | LLVM_DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node) |
| 736 | << "\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 737 | break; |
| 738 | } |
| 739 | if (Headers.back() == Irr.Node) |
| 740 | // Added this as a header. |
| 741 | continue; |
| 742 | |
| 743 | // This is not a header. |
| 744 | Others.push_back(Irr.Node); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 745 | LLVM_DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 746 | } |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 747 | llvm::sort(Headers); |
| 748 | llvm::sort(Others); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static void createIrreducibleLoop( |
| 752 | BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G, |
| 753 | LoopData *OuterLoop, std::list<LoopData>::iterator Insert, |
| 754 | const std::vector<const IrreducibleGraph::IrrNode *> &SCC) { |
| 755 | // Translate the SCC into RPO. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 756 | LLVM_DEBUG(dbgs() << " - found-scc\n"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 757 | |
| 758 | LoopData::NodeList Headers; |
| 759 | LoopData::NodeList Others; |
| 760 | findIrreducibleHeaders(BFI, G, SCC, Headers, Others); |
| 761 | |
| 762 | auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(), |
| 763 | Headers.end(), Others.begin(), Others.end()); |
| 764 | |
| 765 | // Update loop hierarchy. |
| 766 | for (const auto &N : Loop->Nodes) |
| 767 | if (BFI.Working[N.Index].isLoopHeader()) |
| 768 | BFI.Working[N.Index].Loop->Parent = &*Loop; |
| 769 | else |
| 770 | BFI.Working[N.Index].Loop = &*Loop; |
| 771 | } |
| 772 | |
| 773 | iterator_range<std::list<LoopData>::iterator> |
| 774 | BlockFrequencyInfoImplBase::analyzeIrreducible( |
| 775 | const IrreducibleGraph &G, LoopData *OuterLoop, |
| 776 | std::list<LoopData>::iterator Insert) { |
| 777 | assert((OuterLoop == nullptr) == (Insert == Loops.begin())); |
| 778 | auto Prev = OuterLoop ? std::prev(Insert) : Loops.end(); |
| 779 | |
| 780 | for (auto I = scc_begin(G); !I.isAtEnd(); ++I) { |
| 781 | if (I->size() < 2) |
| 782 | continue; |
| 783 | |
| 784 | // Translate the SCC into RPO. |
| 785 | createIrreducibleLoop(*this, G, OuterLoop, Insert, *I); |
| 786 | } |
| 787 | |
| 788 | if (OuterLoop) |
| 789 | return make_range(std::next(Prev), Insert); |
| 790 | return make_range(Loops.begin(), Insert); |
| 791 | } |
| 792 | |
| 793 | void |
| 794 | BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) { |
| 795 | OuterLoop.Exits.clear(); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 796 | for (auto &Mass : OuterLoop.BackedgeMass) |
| 797 | Mass = BlockMass::getEmpty(); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 798 | auto O = OuterLoop.Nodes.begin() + 1; |
| 799 | for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I) |
| 800 | if (!Working[I->Index].isPackaged()) |
| 801 | *O++ = *I; |
| 802 | OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end()); |
| 803 | } |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 804 | |
| 805 | void BlockFrequencyInfoImplBase::adjustLoopHeaderMass(LoopData &Loop) { |
| 806 | assert(Loop.isIrreducible() && "this only makes sense on irreducible loops"); |
| 807 | |
| 808 | // Since the loop has more than one header block, the mass flowing back into |
| 809 | // each header will be different. Adjust the mass in each header loop to |
| 810 | // reflect the masses flowing through back edges. |
| 811 | // |
| 812 | // To do this, we distribute the initial mass using the backedge masses |
| 813 | // as weights for the distribution. |
| 814 | BlockMass LoopMass = BlockMass::getFull(); |
| 815 | Distribution Dist; |
| 816 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 817 | LLVM_DEBUG(dbgs() << "adjust-loop-header-mass:\n"); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 818 | for (uint32_t H = 0; H < Loop.NumHeaders; ++H) { |
| 819 | auto &HeaderNode = Loop.Nodes[H]; |
Diego Novillo | 8c49a57 | 2015-06-17 16:28:22 +0000 | [diff] [blame] | 820 | auto &BackedgeMass = Loop.BackedgeMass[Loop.getHeaderIndex(HeaderNode)]; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 821 | LLVM_DEBUG(dbgs() << " - Add back edge mass for node " |
| 822 | << getBlockName(HeaderNode) << ": " << BackedgeMass |
| 823 | << "\n"); |
Diego Novillo | f9aa39b | 2015-09-08 19:22:17 +0000 | [diff] [blame] | 824 | if (BackedgeMass.getMass() > 0) |
| 825 | Dist.addLocal(HeaderNode, BackedgeMass.getMass()); |
| 826 | else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 827 | LLVM_DEBUG(dbgs() << " Nothing added. Back edge mass is zero\n"); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | DitheringDistributer D(Dist, LoopMass); |
| 831 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 832 | LLVM_DEBUG(dbgs() << " Distribute loop mass " << LoopMass |
| 833 | << " to headers using above weights\n"); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 834 | for (const Weight &W : Dist.Weights) { |
| 835 | BlockMass Taken = D.takeMass(W.Amount); |
| 836 | assert(W.Type == Weight::Local && "all weights should be local"); |
| 837 | Working[W.TargetNode.Index].getMass() = Taken; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 838 | LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr)); |
Diego Novillo | 9a77962 | 2015-06-16 19:10:58 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 841 | |
| 842 | void BlockFrequencyInfoImplBase::distributeIrrLoopHeaderMass(Distribution &Dist) { |
| 843 | BlockMass LoopMass = BlockMass::getFull(); |
| 844 | DitheringDistributer D(Dist, LoopMass); |
| 845 | for (const Weight &W : Dist.Weights) { |
| 846 | BlockMass Taken = D.takeMass(W.Amount); |
| 847 | assert(W.Type == Weight::Local && "all weights should be local"); |
| 848 | Working[W.TargetNode.Index].getMass() = Taken; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 849 | LLVM_DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr)); |
Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 850 | } |
| 851 | } |