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