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" |
Duncan P. N. Exon Smith | 87c40fd | 2014-05-06 01:57:42 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SCCIterator.h" |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 17 | #include <numeric> |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 20 | using namespace llvm::bfi_detail; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 21 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "block-freq" |
| 23 | |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 24 | ScaledNumber<uint64_t> BlockMass::toScaled() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 25 | if (isFull()) |
Duncan P. N. Exon Smith | c379c87 | 2014-06-23 23:36:17 +0000 | [diff] [blame] | 26 | return ScaledNumber<uint64_t>(1, 0); |
| 27 | return ScaledNumber<uint64_t>(getMass() + 1, -64); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void BlockMass::dump() const { print(dbgs()); } |
| 31 | |
| 32 | static char getHexDigit(int N) { |
| 33 | assert(N < 16); |
| 34 | if (N < 10) |
| 35 | return '0' + N; |
| 36 | return 'a' + N - 10; |
| 37 | } |
| 38 | raw_ostream &BlockMass::print(raw_ostream &OS) const { |
| 39 | for (int Digits = 0; Digits < 16; ++Digits) |
| 40 | OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf); |
| 41 | return OS; |
| 42 | } |
| 43 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | typedef BlockFrequencyInfoImplBase::BlockNode BlockNode; |
| 47 | typedef BlockFrequencyInfoImplBase::Distribution Distribution; |
| 48 | typedef BlockFrequencyInfoImplBase::Distribution::WeightList WeightList; |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 49 | typedef BlockFrequencyInfoImplBase::Scaled64 Scaled64; |
Duncan P. N. Exon Smith | cc88ebf | 2014-04-22 03:31:31 +0000 | [diff] [blame] | 50 | typedef BlockFrequencyInfoImplBase::LoopData LoopData; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 51 | typedef BlockFrequencyInfoImplBase::Weight Weight; |
| 52 | typedef BlockFrequencyInfoImplBase::FrequencyData FrequencyData; |
| 53 | |
| 54 | /// \brief Dithering mass distributer. |
| 55 | /// |
| 56 | /// This class splits up a single mass into portions by weight, dithering to |
| 57 | /// spread out error. No mass is lost. The dithering precision depends on the |
| 58 | /// precision of the product of \a BlockMass and \a BranchProbability. |
| 59 | /// |
| 60 | /// The distribution algorithm follows. |
| 61 | /// |
| 62 | /// 1. Initialize by saving the sum of the weights in \a RemWeight and the |
| 63 | /// mass to distribute in \a RemMass. |
| 64 | /// |
| 65 | /// 2. For each portion: |
| 66 | /// |
| 67 | /// 1. Construct a branch probability, P, as the portion's weight divided |
| 68 | /// by the current value of \a RemWeight. |
| 69 | /// 2. Calculate the portion's mass as \a RemMass times P. |
| 70 | /// 3. Update \a RemWeight and \a RemMass at each portion by subtracting |
| 71 | /// the current portion's weight and mass. |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 72 | struct DitheringDistributer { |
| 73 | uint32_t RemWeight; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 74 | BlockMass RemMass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 75 | |
| 76 | DitheringDistributer(Distribution &Dist, const BlockMass &Mass); |
| 77 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 78 | BlockMass takeMass(uint32_t Weight); |
| 79 | }; |
Duncan P. N. Exon Smith | b5650e5 | 2014-07-11 23:56:50 +0000 | [diff] [blame] | 80 | |
| 81 | } // end namespace |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 82 | |
| 83 | DitheringDistributer::DitheringDistributer(Distribution &Dist, |
| 84 | const BlockMass &Mass) { |
| 85 | Dist.normalize(); |
| 86 | RemWeight = Dist.Total; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 87 | RemMass = Mass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 90 | BlockMass DitheringDistributer::takeMass(uint32_t Weight) { |
| 91 | assert(Weight && "invalid weight"); |
| 92 | assert(Weight <= RemWeight); |
| 93 | BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight); |
| 94 | |
| 95 | // Decrement totals (dither). |
| 96 | RemWeight -= Weight; |
| 97 | RemMass -= Mass; |
| 98 | return Mass; |
| 99 | } |
| 100 | |
| 101 | void Distribution::add(const BlockNode &Node, uint64_t Amount, |
| 102 | Weight::DistType Type) { |
| 103 | assert(Amount && "invalid weight of 0"); |
| 104 | uint64_t NewTotal = Total + Amount; |
| 105 | |
| 106 | // Check for overflow. It should be impossible to overflow twice. |
| 107 | bool IsOverflow = NewTotal < Total; |
| 108 | assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow"); |
| 109 | DidOverflow |= IsOverflow; |
| 110 | |
| 111 | // Update the total. |
| 112 | Total = NewTotal; |
| 113 | |
| 114 | // Save the weight. |
Duncan P. N. Exon Smith | 6075510 | 2014-07-12 00:26:00 +0000 | [diff] [blame] | 115 | Weights.push_back(Weight(Type, Node, Amount)); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void combineWeight(Weight &W, const Weight &OtherW) { |
| 119 | assert(OtherW.TargetNode.isValid()); |
| 120 | if (!W.Amount) { |
| 121 | W = OtherW; |
| 122 | return; |
| 123 | } |
| 124 | assert(W.Type == OtherW.Type); |
| 125 | assert(W.TargetNode == OtherW.TargetNode); |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 126 | assert(OtherW.Amount && "Expected non-zero weight"); |
| 127 | if (W.Amount > W.Amount + OtherW.Amount) |
| 128 | // Saturate on overflow. |
| 129 | W.Amount = UINT64_MAX; |
| 130 | else |
| 131 | W.Amount += OtherW.Amount; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 132 | } |
| 133 | static void combineWeightsBySorting(WeightList &Weights) { |
| 134 | // Sort so edges to the same node are adjacent. |
| 135 | std::sort(Weights.begin(), Weights.end(), |
| 136 | [](const Weight &L, |
| 137 | const Weight &R) { return L.TargetNode < R.TargetNode; }); |
| 138 | |
| 139 | // Combine adjacent edges. |
| 140 | WeightList::iterator O = Weights.begin(); |
| 141 | for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E; |
| 142 | ++O, (I = L)) { |
| 143 | *O = *I; |
| 144 | |
| 145 | // Find the adjacent weights to the same node. |
| 146 | for (++L; L != E && I->TargetNode == L->TargetNode; ++L) |
| 147 | combineWeight(*O, *L); |
| 148 | } |
| 149 | |
| 150 | // Erase extra entries. |
| 151 | Weights.erase(O, Weights.end()); |
| 152 | return; |
| 153 | } |
| 154 | static void combineWeightsByHashing(WeightList &Weights) { |
| 155 | // Collect weights into a DenseMap. |
| 156 | typedef DenseMap<BlockNode::IndexType, Weight> HashTable; |
| 157 | HashTable Combined(NextPowerOf2(2 * Weights.size())); |
| 158 | for (const Weight &W : Weights) |
| 159 | combineWeight(Combined[W.TargetNode.Index], W); |
| 160 | |
| 161 | // Check whether anything changed. |
| 162 | if (Weights.size() == Combined.size()) |
| 163 | return; |
| 164 | |
| 165 | // Fill in the new weights. |
| 166 | Weights.clear(); |
| 167 | Weights.reserve(Combined.size()); |
| 168 | for (const auto &I : Combined) |
| 169 | Weights.push_back(I.second); |
| 170 | } |
| 171 | static void combineWeights(WeightList &Weights) { |
| 172 | // Use a hash table for many successors to keep this linear. |
| 173 | if (Weights.size() > 128) { |
| 174 | combineWeightsByHashing(Weights); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | combineWeightsBySorting(Weights); |
| 179 | } |
| 180 | static uint64_t shiftRightAndRound(uint64_t N, int Shift) { |
| 181 | assert(Shift >= 0); |
| 182 | assert(Shift < 64); |
| 183 | if (!Shift) |
| 184 | return N; |
| 185 | return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1)); |
| 186 | } |
| 187 | void Distribution::normalize() { |
| 188 | // Early exit for termination nodes. |
| 189 | if (Weights.empty()) |
| 190 | return; |
| 191 | |
| 192 | // Only bother if there are multiple successors. |
| 193 | if (Weights.size() > 1) |
| 194 | combineWeights(Weights); |
| 195 | |
| 196 | // Early exit when combined into a single successor. |
| 197 | if (Weights.size() == 1) { |
| 198 | Total = 1; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 199 | Weights.front().Amount = 1; |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Determine how much to shift right so that the total fits into 32-bits. |
| 204 | // |
| 205 | // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1 |
| 206 | // for each weight can cause a 32-bit overflow. |
| 207 | int Shift = 0; |
| 208 | if (DidOverflow) |
| 209 | Shift = 33; |
| 210 | else if (Total > UINT32_MAX) |
| 211 | Shift = 33 - countLeadingZeros(Total); |
| 212 | |
| 213 | // Early exit if nothing needs to be scaled. |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 214 | if (!Shift) { |
| 215 | // If we didn't overflow then combineWeights() shouldn't have changed the |
| 216 | // sum of the weights, but let's double-check. |
| 217 | assert(Total == std::accumulate(Weights.begin(), Weights.end(), UINT64_C(0), |
| 218 | [](uint64_t Sum, const Weight &W) { |
| 219 | return Sum + W.Amount; |
| 220 | }) && |
| 221 | "Expected total to be correct"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 222 | return; |
Duncan P. N. Exon Smith | 57cbdfc | 2014-12-05 19:13:42 +0000 | [diff] [blame] | 223 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 224 | |
| 225 | // 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] | 226 | // 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] | 227 | Total = 0; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 228 | |
| 229 | // Sum the weights to each node and shift right if necessary. |
| 230 | for (Weight &W : Weights) { |
| 231 | // Scale down below UINT32_MAX. Since Shift is larger than necessary, we |
| 232 | // can round here without concern about overflow. |
| 233 | assert(W.TargetNode.isValid()); |
| 234 | W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift)); |
| 235 | assert(W.Amount <= UINT32_MAX); |
| 236 | |
| 237 | // Update the total. |
| 238 | Total += W.Amount; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 239 | } |
| 240 | assert(Total <= UINT32_MAX); |
| 241 | } |
| 242 | |
| 243 | void BlockFrequencyInfoImplBase::clear() { |
Duncan P. N. Exon Smith | dc2d66e | 2014-04-22 03:31:34 +0000 | [diff] [blame] | 244 | // Swap with a default-constructed std::vector, since std::vector<>::clear() |
| 245 | // does not actually clear heap storage. |
| 246 | std::vector<FrequencyData>().swap(Freqs); |
| 247 | std::vector<WorkingData>().swap(Working); |
Duncan P. N. Exon Smith | fc7dc93 | 2014-04-25 04:30:06 +0000 | [diff] [blame] | 248 | Loops.clear(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /// \brief Clear all memory not needed downstream. |
| 252 | /// |
| 253 | /// Releases all memory not used downstream. In particular, saves Freqs. |
| 254 | static void cleanup(BlockFrequencyInfoImplBase &BFI) { |
| 255 | std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs)); |
| 256 | BFI.clear(); |
| 257 | BFI.Freqs = std::move(SavedFreqs); |
| 258 | } |
| 259 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 260 | bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist, |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 261 | const LoopData *OuterLoop, |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 262 | const BlockNode &Pred, |
| 263 | const BlockNode &Succ, |
| 264 | uint64_t Weight) { |
| 265 | if (!Weight) |
| 266 | Weight = 1; |
| 267 | |
Duncan P. N. Exon Smith | 39cc648 | 2014-04-25 04:38:06 +0000 | [diff] [blame] | 268 | auto isLoopHeader = [&OuterLoop](const BlockNode &Node) { |
| 269 | return OuterLoop && OuterLoop->isHeader(Node); |
| 270 | }; |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 271 | |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 272 | BlockNode Resolved = Working[Succ.Index].getResolvedNode(); |
| 273 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 274 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 275 | auto debugSuccessor = [&](const char *Type) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 276 | dbgs() << " =>" |
| 277 | << " [" << Type << "] weight = " << Weight; |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 278 | if (!isLoopHeader(Resolved)) |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 279 | dbgs() << ", succ = " << getBlockName(Succ); |
| 280 | if (Resolved != Succ) |
| 281 | dbgs() << ", resolved = " << getBlockName(Resolved); |
| 282 | dbgs() << "\n"; |
| 283 | }; |
| 284 | (void)debugSuccessor; |
| 285 | #endif |
| 286 | |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 287 | if (isLoopHeader(Resolved)) { |
| 288 | DEBUG(debugSuccessor("backedge")); |
Duncan P. N. Exon Smith | 39cc648 | 2014-04-25 04:38:06 +0000 | [diff] [blame] | 289 | Dist.addBackedge(OuterLoop->getHeader(), Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 290 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 291 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 292 | |
Duncan P. N. Exon Smith | 39cc648 | 2014-04-25 04:38:06 +0000 | [diff] [blame] | 293 | if (Working[Resolved.Index].getContainingLoop() != OuterLoop) { |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 294 | DEBUG(debugSuccessor(" exit ")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 295 | Dist.addExit(Resolved, Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 296 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Duncan P. N. Exon Smith | b3380ea | 2014-04-22 03:31:53 +0000 | [diff] [blame] | 299 | if (Resolved < Pred) { |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 300 | if (!isLoopHeader(Pred)) { |
| 301 | // If OuterLoop is an irreducible loop, we can't actually handle this. |
| 302 | assert((!OuterLoop || !OuterLoop->isIrreducible()) && |
| 303 | "unhandled irreducible control flow"); |
| 304 | |
| 305 | // Irreducible backedge. Abort. |
| 306 | DEBUG(debugSuccessor("abort!!!")); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | // If "Pred" is a loop header, then this isn't really a backedge; rather, |
| 311 | // OuterLoop must be irreducible. These false backedges can come only from |
| 312 | // secondary loop headers. |
| 313 | assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) && |
| 314 | "unhandled irreducible control flow"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 317 | DEBUG(debugSuccessor(" local ")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 318 | Dist.addLocal(Resolved, Weight); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 319 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 322 | bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist( |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 323 | const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 324 | // Copy the exit map into Dist. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 325 | for (const auto &I : Loop.Exits) |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 326 | if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first, |
| 327 | I.second.getMass())) |
| 328 | // Irreducible backedge. |
| 329 | return false; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 330 | |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 331 | return true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /// \brief Get the maximum allowed loop scale. |
| 335 | /// |
Duncan P. N. Exon Smith | 254689f | 2014-04-21 18:31:58 +0000 | [diff] [blame] | 336 | /// Gives the maximum number of estimated iterations allowed for a loop. Very |
| 337 | /// large numbers cause problems downstream (even within 64-bits). |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 338 | static Scaled64 getMaxLoopScale() { return Scaled64(1, 12); } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 339 | |
| 340 | /// \brief Compute the loop scale for a loop. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 341 | void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 342 | // Compute loop scale. |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 343 | DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 344 | |
| 345 | // LoopScale == 1 / ExitMass |
| 346 | // ExitMass == HeadMass - BackedgeMass |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 347 | BlockMass ExitMass = BlockMass::getFull() - Loop.BackedgeMass; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 348 | |
| 349 | // Block scale stores the inverse of the scale. |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 350 | Loop.Scale = ExitMass.toScaled().inverse(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 351 | |
| 352 | DEBUG(dbgs() << " - exit-mass = " << ExitMass << " (" << BlockMass::getFull() |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 353 | << " - " << Loop.BackedgeMass << ")\n" |
| 354 | << " - scale = " << Loop.Scale << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 355 | |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 356 | if (Loop.Scale > getMaxLoopScale()) { |
| 357 | Loop.Scale = getMaxLoopScale(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 358 | DEBUG(dbgs() << " - reduced-to-max-scale: " << getMaxLoopScale() << "\n"); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /// \brief Package up a loop. |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 363 | void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) { |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 364 | DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n"); |
| 365 | |
| 366 | // Clear the subloop exits to prevent quadratic memory usage. |
| 367 | for (const BlockNode &M : Loop.Nodes) { |
| 368 | if (auto *Loop = Working[M.Index].getPackagedLoop()) |
| 369 | Loop->Exits.clear(); |
| 370 | DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n"); |
| 371 | } |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 372 | Loop.IsPackaged = true; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source, |
Duncan P. N. Exon Smith | d132040 | 2014-04-25 04:38:01 +0000 | [diff] [blame] | 376 | LoopData *OuterLoop, |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 377 | Distribution &Dist) { |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 378 | BlockMass Mass = Working[Source.Index].getMass(); |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 379 | DEBUG(dbgs() << " => mass: " << Mass << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 380 | |
| 381 | // Distribute mass to successors as laid out in Dist. |
| 382 | DitheringDistributer D(Dist, Mass); |
| 383 | |
| 384 | #ifndef NDEBUG |
| 385 | auto debugAssign = [&](const BlockNode &T, const BlockMass &M, |
| 386 | const char *Desc) { |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 387 | dbgs() << " => assign " << M << " (" << D.RemMass << ")"; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 388 | if (Desc) |
| 389 | dbgs() << " [" << Desc << "]"; |
| 390 | if (T.isValid()) |
| 391 | dbgs() << " to " << getBlockName(T); |
| 392 | dbgs() << "\n"; |
| 393 | }; |
| 394 | (void)debugAssign; |
| 395 | #endif |
| 396 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 397 | for (const Weight &W : Dist.Weights) { |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 398 | // Check for a local edge (non-backedge and non-exit). |
| 399 | BlockMass Taken = D.takeMass(W.Amount); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 400 | if (W.Type == Weight::Local) { |
Duncan P. N. Exon Smith | da5eaed | 2014-04-25 18:47:04 +0000 | [diff] [blame] | 401 | Working[W.TargetNode.Index].getMass() += Taken; |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 402 | DEBUG(debugAssign(W.TargetNode, Taken, nullptr)); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | |
| 406 | // 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] | 407 | assert(OuterLoop && "backedge or exit outside of loop"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 408 | |
| 409 | // Check for a backedge. |
| 410 | if (W.Type == Weight::Backedge) { |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 411 | OuterLoop->BackedgeMass += Taken; |
| 412 | DEBUG(debugAssign(BlockNode(), Taken, "back")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 413 | continue; |
| 414 | } |
| 415 | |
| 416 | // This must be an exit. |
| 417 | assert(W.Type == Weight::Exit); |
Duncan P. N. Exon Smith | cb7d29d | 2014-04-25 04:38:43 +0000 | [diff] [blame] | 418 | OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken)); |
| 419 | DEBUG(debugAssign(W.TargetNode, Taken, "exit")); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI, |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 424 | const Scaled64 &Min, const Scaled64 &Max) { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 425 | // Scale the Factor to a size that creates integers. Ideally, integers would |
| 426 | // be scaled so that Max == UINT64_MAX so that they can be best |
| 427 | // differentiated. However, the register allocator currently deals poorly |
| 428 | // with large numbers. Instead, push Min up a little from 1 to give some |
| 429 | // room to differentiate small, unequal numbers. |
| 430 | // |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 431 | // TODO: fix issues downstream so that ScalingFactor can be |
| 432 | // Scaled64(1,64)/Max. |
| 433 | Scaled64 ScalingFactor = Min.inverse(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 434 | if ((Max / Min).lg() < 60) |
| 435 | ScalingFactor <<= 3; |
| 436 | |
| 437 | // Translate the floats to integers. |
| 438 | DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max |
| 439 | << ", factor = " << ScalingFactor << "\n"); |
| 440 | 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] | 441 | Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 442 | BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>()); |
| 443 | DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = " |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 444 | << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 445 | << ", int = " << BFI.Freqs[Index].Integer << "\n"); |
| 446 | } |
| 447 | } |
| 448 | |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 449 | /// \brief Unwrap a loop package. |
| 450 | /// |
| 451 | /// Visits all the members of a loop, adjusting their BlockData according to |
| 452 | /// the loop's pseudo-node. |
Duncan P. N. Exon Smith | 0633f0e | 2014-04-25 04:38:25 +0000 | [diff] [blame] | 453 | static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) { |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 454 | DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop) |
Duncan P. N. Exon Smith | 0633f0e | 2014-04-25 04:38:25 +0000 | [diff] [blame] | 455 | << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale |
| 456 | << "\n"); |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 457 | Loop.Scale *= Loop.Mass.toScaled(); |
Duncan P. N. Exon Smith | 5291d2a | 2014-04-25 04:38:27 +0000 | [diff] [blame] | 458 | Loop.IsPackaged = false; |
Duncan P. N. Exon Smith | 3f08678 | 2014-04-25 04:38:32 +0000 | [diff] [blame] | 459 | DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n"); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 460 | |
| 461 | // Propagate the head scale through the loop. Since members are visited in |
| 462 | // RPO, the head scale will be updated by the loop scale first, and then the |
| 463 | // 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] | 464 | for (const BlockNode &N : Loop.Nodes) { |
| 465 | const auto &Working = BFI.Working[N.Index]; |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 466 | Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale |
| 467 | : BFI.Freqs[N.Index].Scaled; |
| 468 | Scaled64 New = Loop.Scale * F; |
Duncan P. N. Exon Smith | 5291d2a | 2014-04-25 04:38:27 +0000 | [diff] [blame] | 469 | DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => " << New |
| 470 | << "\n"); |
| 471 | F = New; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 475 | void BlockFrequencyInfoImplBase::unwrapLoops() { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 476 | // Set initial frequencies from loop-local masses. |
| 477 | for (size_t Index = 0; Index < Working.size(); ++Index) |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 478 | Freqs[Index].Scaled = Working[Index].Mass.toScaled(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 479 | |
Duncan P. N. Exon Smith | da0b21c | 2014-04-25 04:38:23 +0000 | [diff] [blame] | 480 | for (LoopData &Loop : Loops) |
Duncan P. N. Exon Smith | 0633f0e | 2014-04-25 04:38:25 +0000 | [diff] [blame] | 481 | unwrapLoop(*this, Loop); |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void BlockFrequencyInfoImplBase::finalizeMetrics() { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 485 | // Unwrap loop packages in reverse post-order, tracking min and max |
| 486 | // frequencies. |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 487 | auto Min = Scaled64::getLargest(); |
| 488 | auto Max = Scaled64::getZero(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 489 | for (size_t Index = 0; Index < Working.size(); ++Index) { |
Duncan P. N. Exon Smith | 46d9a56 | 2014-04-25 04:38:17 +0000 | [diff] [blame] | 490 | // Update min/max scale. |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 491 | Min = std::min(Min, Freqs[Index].Scaled); |
| 492 | Max = std::max(Max, Freqs[Index].Scaled); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Convert to integers. |
| 496 | convertFloatingToInteger(*this, Min, Max); |
| 497 | |
| 498 | // Clean up data structures. |
| 499 | cleanup(*this); |
| 500 | |
| 501 | // Print out the final stats. |
| 502 | DEBUG(dump()); |
| 503 | } |
| 504 | |
| 505 | BlockFrequency |
| 506 | BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const { |
| 507 | if (!Node.isValid()) |
| 508 | return 0; |
| 509 | return Freqs[Node.Index].Integer; |
| 510 | } |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 511 | Scaled64 |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 512 | BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const { |
| 513 | if (!Node.isValid()) |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 514 | return Scaled64::getZero(); |
| 515 | return Freqs[Node.Index].Scaled; |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | std::string |
| 519 | BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const { |
| 520 | return std::string(); |
| 521 | } |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 522 | std::string |
| 523 | BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const { |
| 524 | return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*"); |
| 525 | } |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 526 | |
| 527 | raw_ostream & |
| 528 | BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS, |
| 529 | const BlockNode &Node) const { |
| 530 | return OS << getFloatingBlockFreq(Node); |
| 531 | } |
| 532 | |
| 533 | raw_ostream & |
| 534 | BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS, |
| 535 | const BlockFrequency &Freq) const { |
Duncan P. N. Exon Smith | beaf813 | 2014-06-24 00:26:13 +0000 | [diff] [blame] | 536 | Scaled64 Block(Freq.getFrequency(), 0); |
| 537 | Scaled64 Entry(getEntryFreq(), 0); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 538 | |
| 539 | return OS << Block / Entry; |
| 540 | } |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 541 | |
| 542 | void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) { |
| 543 | Start = OuterLoop.getHeader(); |
| 544 | Nodes.reserve(OuterLoop.Nodes.size()); |
| 545 | for (auto N : OuterLoop.Nodes) |
| 546 | addNode(N); |
| 547 | indexNodes(); |
| 548 | } |
| 549 | void IrreducibleGraph::addNodesInFunction() { |
| 550 | Start = 0; |
| 551 | for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index) |
| 552 | if (!BFI.Working[Index].isPackaged()) |
| 553 | addNode(Index); |
| 554 | indexNodes(); |
| 555 | } |
| 556 | void IrreducibleGraph::indexNodes() { |
| 557 | for (auto &I : Nodes) |
| 558 | Lookup[I.Node.Index] = &I; |
| 559 | } |
| 560 | void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ, |
| 561 | const BFIBase::LoopData *OuterLoop) { |
| 562 | if (OuterLoop && OuterLoop->isHeader(Succ)) |
| 563 | return; |
| 564 | auto L = Lookup.find(Succ.Index); |
| 565 | if (L == Lookup.end()) |
| 566 | return; |
| 567 | IrrNode &SuccIrr = *L->second; |
| 568 | Irr.Edges.push_back(&SuccIrr); |
| 569 | SuccIrr.Edges.push_front(&Irr); |
| 570 | ++SuccIrr.NumIn; |
| 571 | } |
| 572 | |
| 573 | namespace llvm { |
| 574 | template <> struct GraphTraits<IrreducibleGraph> { |
| 575 | typedef bfi_detail::IrreducibleGraph GraphT; |
| 576 | |
Duncan P. N. Exon Smith | 295b5e7 | 2014-04-28 20:22:29 +0000 | [diff] [blame] | 577 | typedef const GraphT::IrrNode NodeType; |
| 578 | typedef GraphT::IrrNode::iterator ChildIteratorType; |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 579 | |
| 580 | static const NodeType *getEntryNode(const GraphT &G) { |
| 581 | return G.StartIrr; |
| 582 | } |
| 583 | static ChildIteratorType child_begin(NodeType *N) { return N->succ_begin(); } |
| 584 | static ChildIteratorType child_end(NodeType *N) { return N->succ_end(); } |
| 585 | }; |
| 586 | } |
| 587 | |
| 588 | /// \brief Find extra irreducible headers. |
| 589 | /// |
| 590 | /// Find entry blocks and other blocks with backedges, which exist when \c G |
| 591 | /// contains irreducible sub-SCCs. |
| 592 | static void findIrreducibleHeaders( |
| 593 | const BlockFrequencyInfoImplBase &BFI, |
| 594 | const IrreducibleGraph &G, |
| 595 | const std::vector<const IrreducibleGraph::IrrNode *> &SCC, |
| 596 | LoopData::NodeList &Headers, LoopData::NodeList &Others) { |
| 597 | // Map from nodes in the SCC to whether it's an entry block. |
| 598 | SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC; |
| 599 | |
| 600 | // InSCC also acts the set of nodes in the graph. Seed it. |
| 601 | for (const auto *I : SCC) |
| 602 | InSCC[I] = false; |
| 603 | |
| 604 | for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) { |
| 605 | auto &Irr = *I->first; |
| 606 | for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) { |
| 607 | if (InSCC.count(P)) |
| 608 | continue; |
| 609 | |
| 610 | // This is an entry block. |
| 611 | I->second = true; |
| 612 | Headers.push_back(Irr.Node); |
| 613 | DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node) << "\n"); |
| 614 | break; |
| 615 | } |
| 616 | } |
Duncan P. N. Exon Smith | a7a90a2 | 2014-10-06 17:42:00 +0000 | [diff] [blame] | 617 | assert(Headers.size() >= 2 && |
| 618 | "Expected irreducible CFG; -loop-info is likely invalid"); |
Duncan P. N. Exon Smith | c5a3139 | 2014-04-28 20:02:29 +0000 | [diff] [blame] | 619 | if (Headers.size() == InSCC.size()) { |
| 620 | // Every block is a header. |
| 621 | std::sort(Headers.begin(), Headers.end()); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | // Look for extra headers from irreducible sub-SCCs. |
| 626 | for (const auto &I : InSCC) { |
| 627 | // Entry blocks are already headers. |
| 628 | if (I.second) |
| 629 | continue; |
| 630 | |
| 631 | auto &Irr = *I.first; |
| 632 | for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) { |
| 633 | // Skip forward edges. |
| 634 | if (P->Node < Irr.Node) |
| 635 | continue; |
| 636 | |
| 637 | // Skip predecessors from entry blocks. These can have inverted |
| 638 | // ordering. |
| 639 | if (InSCC.lookup(P)) |
| 640 | continue; |
| 641 | |
| 642 | // Store the extra header. |
| 643 | Headers.push_back(Irr.Node); |
| 644 | DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node) << "\n"); |
| 645 | break; |
| 646 | } |
| 647 | if (Headers.back() == Irr.Node) |
| 648 | // Added this as a header. |
| 649 | continue; |
| 650 | |
| 651 | // This is not a header. |
| 652 | Others.push_back(Irr.Node); |
| 653 | DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n"); |
| 654 | } |
| 655 | std::sort(Headers.begin(), Headers.end()); |
| 656 | std::sort(Others.begin(), Others.end()); |
| 657 | } |
| 658 | |
| 659 | static void createIrreducibleLoop( |
| 660 | BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G, |
| 661 | LoopData *OuterLoop, std::list<LoopData>::iterator Insert, |
| 662 | const std::vector<const IrreducibleGraph::IrrNode *> &SCC) { |
| 663 | // Translate the SCC into RPO. |
| 664 | DEBUG(dbgs() << " - found-scc\n"); |
| 665 | |
| 666 | LoopData::NodeList Headers; |
| 667 | LoopData::NodeList Others; |
| 668 | findIrreducibleHeaders(BFI, G, SCC, Headers, Others); |
| 669 | |
| 670 | auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(), |
| 671 | Headers.end(), Others.begin(), Others.end()); |
| 672 | |
| 673 | // Update loop hierarchy. |
| 674 | for (const auto &N : Loop->Nodes) |
| 675 | if (BFI.Working[N.Index].isLoopHeader()) |
| 676 | BFI.Working[N.Index].Loop->Parent = &*Loop; |
| 677 | else |
| 678 | BFI.Working[N.Index].Loop = &*Loop; |
| 679 | } |
| 680 | |
| 681 | iterator_range<std::list<LoopData>::iterator> |
| 682 | BlockFrequencyInfoImplBase::analyzeIrreducible( |
| 683 | const IrreducibleGraph &G, LoopData *OuterLoop, |
| 684 | std::list<LoopData>::iterator Insert) { |
| 685 | assert((OuterLoop == nullptr) == (Insert == Loops.begin())); |
| 686 | auto Prev = OuterLoop ? std::prev(Insert) : Loops.end(); |
| 687 | |
| 688 | for (auto I = scc_begin(G); !I.isAtEnd(); ++I) { |
| 689 | if (I->size() < 2) |
| 690 | continue; |
| 691 | |
| 692 | // Translate the SCC into RPO. |
| 693 | createIrreducibleLoop(*this, G, OuterLoop, Insert, *I); |
| 694 | } |
| 695 | |
| 696 | if (OuterLoop) |
| 697 | return make_range(std::next(Prev), Insert); |
| 698 | return make_range(Loops.begin(), Insert); |
| 699 | } |
| 700 | |
| 701 | void |
| 702 | BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) { |
| 703 | OuterLoop.Exits.clear(); |
| 704 | OuterLoop.BackedgeMass = BlockMass::getEmpty(); |
| 705 | auto O = OuterLoop.Nodes.begin() + 1; |
| 706 | for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I) |
| 707 | if (!Working[I->Index].isPackaged()) |
| 708 | *O++ = *I; |
| 709 | OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end()); |
| 710 | } |