1 //===- BlockFrequencyImplInfo.cpp - Block Frequency Info Implementation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
15 #include "llvm/ADT/SCCIterator.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::bfi_detail;
23 #define DEBUG_TYPE "block-freq"
25 ScaledNumber<uint64_t> BlockMass::toScaled() const {
27 return ScaledNumber<uint64_t>(1, 0);
28 return ScaledNumber<uint64_t>(getMass() + 1, -64);
31 LLVM_DUMP_METHOD void BlockMass::dump() const { print(dbgs()); }
33 static char getHexDigit(int N) {
40 raw_ostream &BlockMass::print(raw_ostream &OS) const {
41 for (int Digits = 0; Digits < 16; ++Digits)
42 OS << getHexDigit(Mass >> (60 - Digits * 4) & 0xf);
48 typedef BlockFrequencyInfoImplBase::BlockNode BlockNode;
49 typedef BlockFrequencyInfoImplBase::Distribution Distribution;
50 typedef BlockFrequencyInfoImplBase::Distribution::WeightList WeightList;
51 typedef BlockFrequencyInfoImplBase::Scaled64 Scaled64;
52 typedef BlockFrequencyInfoImplBase::LoopData LoopData;
53 typedef BlockFrequencyInfoImplBase::Weight Weight;
54 typedef BlockFrequencyInfoImplBase::FrequencyData FrequencyData;
56 /// \brief Dithering mass distributer.
58 /// This class splits up a single mass into portions by weight, dithering to
59 /// spread out error. No mass is lost. The dithering precision depends on the
60 /// precision of the product of \a BlockMass and \a BranchProbability.
62 /// The distribution algorithm follows.
64 /// 1. Initialize by saving the sum of the weights in \a RemWeight and the
65 /// mass to distribute in \a RemMass.
67 /// 2. For each portion:
69 /// 1. Construct a branch probability, P, as the portion's weight divided
70 /// by the current value of \a RemWeight.
71 /// 2. Calculate the portion's mass as \a RemMass times P.
72 /// 3. Update \a RemWeight and \a RemMass at each portion by subtracting
73 /// the current portion's weight and mass.
74 struct DitheringDistributer {
78 DitheringDistributer(Distribution &Dist, const BlockMass &Mass);
80 BlockMass takeMass(uint32_t Weight);
83 } // end anonymous namespace
85 DitheringDistributer::DitheringDistributer(Distribution &Dist,
86 const BlockMass &Mass) {
88 RemWeight = Dist.Total;
92 BlockMass DitheringDistributer::takeMass(uint32_t Weight) {
93 assert(Weight && "invalid weight");
94 assert(Weight <= RemWeight);
95 BlockMass Mass = RemMass * BranchProbability(Weight, RemWeight);
97 // Decrement totals (dither).
103 void Distribution::add(const BlockNode &Node, uint64_t Amount,
104 Weight::DistType Type) {
105 assert(Amount && "invalid weight of 0");
106 uint64_t NewTotal = Total + Amount;
108 // Check for overflow. It should be impossible to overflow twice.
109 bool IsOverflow = NewTotal < Total;
110 assert(!(DidOverflow && IsOverflow) && "unexpected repeated overflow");
111 DidOverflow |= IsOverflow;
117 Weights.push_back(Weight(Type, Node, Amount));
120 static void combineWeight(Weight &W, const Weight &OtherW) {
121 assert(OtherW.TargetNode.isValid());
126 assert(W.Type == OtherW.Type);
127 assert(W.TargetNode == OtherW.TargetNode);
128 assert(OtherW.Amount && "Expected non-zero weight");
129 if (W.Amount > W.Amount + OtherW.Amount)
130 // Saturate on overflow.
131 W.Amount = UINT64_MAX;
133 W.Amount += OtherW.Amount;
136 static void combineWeightsBySorting(WeightList &Weights) {
137 // Sort so edges to the same node are adjacent.
138 std::sort(Weights.begin(), Weights.end(),
140 const Weight &R) { return L.TargetNode < R.TargetNode; });
142 // Combine adjacent edges.
143 WeightList::iterator O = Weights.begin();
144 for (WeightList::const_iterator I = O, L = O, E = Weights.end(); I != E;
148 // Find the adjacent weights to the same node.
149 for (++L; L != E && I->TargetNode == L->TargetNode; ++L)
150 combineWeight(*O, *L);
153 // Erase extra entries.
154 Weights.erase(O, Weights.end());
157 static void combineWeightsByHashing(WeightList &Weights) {
158 // Collect weights into a DenseMap.
159 typedef DenseMap<BlockNode::IndexType, Weight> HashTable;
160 HashTable Combined(NextPowerOf2(2 * Weights.size()));
161 for (const Weight &W : Weights)
162 combineWeight(Combined[W.TargetNode.Index], W);
164 // Check whether anything changed.
165 if (Weights.size() == Combined.size())
168 // Fill in the new weights.
170 Weights.reserve(Combined.size());
171 for (const auto &I : Combined)
172 Weights.push_back(I.second);
175 static void combineWeights(WeightList &Weights) {
176 // Use a hash table for many successors to keep this linear.
177 if (Weights.size() > 128) {
178 combineWeightsByHashing(Weights);
182 combineWeightsBySorting(Weights);
185 static uint64_t shiftRightAndRound(uint64_t N, int Shift) {
190 return (N >> Shift) + (UINT64_C(1) & N >> (Shift - 1));
193 void Distribution::normalize() {
194 // Early exit for termination nodes.
198 // Only bother if there are multiple successors.
199 if (Weights.size() > 1)
200 combineWeights(Weights);
202 // Early exit when combined into a single successor.
203 if (Weights.size() == 1) {
205 Weights.front().Amount = 1;
209 // Determine how much to shift right so that the total fits into 32-bits.
211 // If we shift at all, shift by 1 extra. Otherwise, the lower limit of 1
212 // for each weight can cause a 32-bit overflow.
216 else if (Total > UINT32_MAX)
217 Shift = 33 - countLeadingZeros(Total);
219 // Early exit if nothing needs to be scaled.
221 // If we didn't overflow then combineWeights() shouldn't have changed the
222 // sum of the weights, but let's double-check.
223 assert(Total == std::accumulate(Weights.begin(), Weights.end(), UINT64_C(0),
224 [](uint64_t Sum, const Weight &W) {
225 return Sum + W.Amount;
227 "Expected total to be correct");
231 // Recompute the total through accumulation (rather than shifting it) so that
232 // it's accurate after shifting and any changes combineWeights() made above.
235 // Sum the weights to each node and shift right if necessary.
236 for (Weight &W : Weights) {
237 // Scale down below UINT32_MAX. Since Shift is larger than necessary, we
238 // can round here without concern about overflow.
239 assert(W.TargetNode.isValid());
240 W.Amount = std::max(UINT64_C(1), shiftRightAndRound(W.Amount, Shift));
241 assert(W.Amount <= UINT32_MAX);
246 assert(Total <= UINT32_MAX);
249 void BlockFrequencyInfoImplBase::clear() {
250 // Swap with a default-constructed std::vector, since std::vector<>::clear()
251 // does not actually clear heap storage.
252 std::vector<FrequencyData>().swap(Freqs);
253 std::vector<WorkingData>().swap(Working);
257 /// \brief Clear all memory not needed downstream.
259 /// Releases all memory not used downstream. In particular, saves Freqs.
260 static void cleanup(BlockFrequencyInfoImplBase &BFI) {
261 std::vector<FrequencyData> SavedFreqs(std::move(BFI.Freqs));
263 BFI.Freqs = std::move(SavedFreqs);
266 bool BlockFrequencyInfoImplBase::addToDist(Distribution &Dist,
267 const LoopData *OuterLoop,
268 const BlockNode &Pred,
269 const BlockNode &Succ,
274 auto isLoopHeader = [&OuterLoop](const BlockNode &Node) {
275 return OuterLoop && OuterLoop->isHeader(Node);
278 BlockNode Resolved = Working[Succ.Index].getResolvedNode();
281 auto debugSuccessor = [&](const char *Type) {
283 << " [" << Type << "] weight = " << Weight;
284 if (!isLoopHeader(Resolved))
285 dbgs() << ", succ = " << getBlockName(Succ);
286 if (Resolved != Succ)
287 dbgs() << ", resolved = " << getBlockName(Resolved);
290 (void)debugSuccessor;
293 if (isLoopHeader(Resolved)) {
294 DEBUG(debugSuccessor("backedge"));
295 Dist.addBackedge(Resolved, Weight);
299 if (Working[Resolved.Index].getContainingLoop() != OuterLoop) {
300 DEBUG(debugSuccessor(" exit "));
301 Dist.addExit(Resolved, Weight);
305 if (Resolved < Pred) {
306 if (!isLoopHeader(Pred)) {
307 // If OuterLoop is an irreducible loop, we can't actually handle this.
308 assert((!OuterLoop || !OuterLoop->isIrreducible()) &&
309 "unhandled irreducible control flow");
311 // Irreducible backedge. Abort.
312 DEBUG(debugSuccessor("abort!!!"));
316 // If "Pred" is a loop header, then this isn't really a backedge; rather,
317 // OuterLoop must be irreducible. These false backedges can come only from
318 // secondary loop headers.
319 assert(OuterLoop && OuterLoop->isIrreducible() && !isLoopHeader(Resolved) &&
320 "unhandled irreducible control flow");
323 DEBUG(debugSuccessor(" local "));
324 Dist.addLocal(Resolved, Weight);
328 bool BlockFrequencyInfoImplBase::addLoopSuccessorsToDist(
329 const LoopData *OuterLoop, LoopData &Loop, Distribution &Dist) {
330 // Copy the exit map into Dist.
331 for (const auto &I : Loop.Exits)
332 if (!addToDist(Dist, OuterLoop, Loop.getHeader(), I.first,
334 // Irreducible backedge.
340 /// \brief Compute the loop scale for a loop.
341 void BlockFrequencyInfoImplBase::computeLoopScale(LoopData &Loop) {
342 // Compute loop scale.
343 DEBUG(dbgs() << "compute-loop-scale: " << getLoopName(Loop) << "\n");
345 // Infinite loops need special handling. If we give the back edge an infinite
346 // mass, they may saturate all the other scales in the function down to 1,
347 // making all the other region temperatures look exactly the same. Choose an
348 // arbitrary scale to avoid these issues.
350 // FIXME: An alternate way would be to select a symbolic scale which is later
351 // replaced to be the maximum of all computed scales plus 1. This would
352 // appropriately describe the loop as having a large scale, without skewing
353 // the final frequency computation.
354 const Scaled64 InfiniteLoopScale(1, 12);
356 // LoopScale == 1 / ExitMass
357 // ExitMass == HeadMass - BackedgeMass
358 BlockMass TotalBackedgeMass;
359 for (auto &Mass : Loop.BackedgeMass)
360 TotalBackedgeMass += Mass;
361 BlockMass ExitMass = BlockMass::getFull() - TotalBackedgeMass;
363 // Block scale stores the inverse of the scale. If this is an infinite loop,
364 // its exit mass will be zero. In this case, use an arbitrary scale for the
367 ExitMass.isEmpty() ? InfiniteLoopScale : ExitMass.toScaled().inverse();
369 DEBUG(dbgs() << " - exit-mass = " << ExitMass << " (" << BlockMass::getFull()
370 << " - " << TotalBackedgeMass << ")\n"
371 << " - scale = " << Loop.Scale << "\n");
374 /// \brief Package up a loop.
375 void BlockFrequencyInfoImplBase::packageLoop(LoopData &Loop) {
376 DEBUG(dbgs() << "packaging-loop: " << getLoopName(Loop) << "\n");
378 // Clear the subloop exits to prevent quadratic memory usage.
379 for (const BlockNode &M : Loop.Nodes) {
380 if (auto *Loop = Working[M.Index].getPackagedLoop())
382 DEBUG(dbgs() << " - node: " << getBlockName(M.Index) << "\n");
384 Loop.IsPackaged = true;
388 static void debugAssign(const BlockFrequencyInfoImplBase &BFI,
389 const DitheringDistributer &D, const BlockNode &T,
390 const BlockMass &M, const char *Desc) {
391 dbgs() << " => assign " << M << " (" << D.RemMass << ")";
393 dbgs() << " [" << Desc << "]";
395 dbgs() << " to " << BFI.getBlockName(T);
400 void BlockFrequencyInfoImplBase::distributeMass(const BlockNode &Source,
402 Distribution &Dist) {
403 BlockMass Mass = Working[Source.Index].getMass();
404 DEBUG(dbgs() << " => mass: " << Mass << "\n");
406 // Distribute mass to successors as laid out in Dist.
407 DitheringDistributer D(Dist, Mass);
409 for (const Weight &W : Dist.Weights) {
410 // Check for a local edge (non-backedge and non-exit).
411 BlockMass Taken = D.takeMass(W.Amount);
412 if (W.Type == Weight::Local) {
413 Working[W.TargetNode.Index].getMass() += Taken;
414 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));
418 // Backedges and exits only make sense if we're processing a loop.
419 assert(OuterLoop && "backedge or exit outside of loop");
421 // Check for a backedge.
422 if (W.Type == Weight::Backedge) {
423 OuterLoop->BackedgeMass[OuterLoop->getHeaderIndex(W.TargetNode)] += Taken;
424 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "back"));
428 // This must be an exit.
429 assert(W.Type == Weight::Exit);
430 OuterLoop->Exits.push_back(std::make_pair(W.TargetNode, Taken));
431 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, "exit"));
435 static void convertFloatingToInteger(BlockFrequencyInfoImplBase &BFI,
436 const Scaled64 &Min, const Scaled64 &Max) {
437 // Scale the Factor to a size that creates integers. Ideally, integers would
438 // be scaled so that Max == UINT64_MAX so that they can be best
439 // differentiated. However, in the presence of large frequency values, small
440 // frequencies are scaled down to 1, making it impossible to differentiate
441 // small, unequal numbers. When the spread between Min and Max frequencies
442 // fits well within MaxBits, we make the scale be at least 8.
443 const unsigned MaxBits = 64;
444 const unsigned SpreadBits = (Max / Min).lg();
445 Scaled64 ScalingFactor;
446 if (SpreadBits <= MaxBits - 3) {
447 // If the values are small enough, make the scaling factor at least 8 to
448 // allow distinguishing small values.
449 ScalingFactor = Min.inverse();
452 // If the values need more than MaxBits to be represented, saturate small
453 // frequency values down to 1 by using a scaling factor that benefits large
455 ScalingFactor = Scaled64(1, MaxBits) / Max;
458 // Translate the floats to integers.
459 DEBUG(dbgs() << "float-to-int: min = " << Min << ", max = " << Max
460 << ", factor = " << ScalingFactor << "\n");
461 for (size_t Index = 0; Index < BFI.Freqs.size(); ++Index) {
462 Scaled64 Scaled = BFI.Freqs[Index].Scaled * ScalingFactor;
463 BFI.Freqs[Index].Integer = std::max(UINT64_C(1), Scaled.toInt<uint64_t>());
464 DEBUG(dbgs() << " - " << BFI.getBlockName(Index) << ": float = "
465 << BFI.Freqs[Index].Scaled << ", scaled = " << Scaled
466 << ", int = " << BFI.Freqs[Index].Integer << "\n");
470 /// \brief Unwrap a loop package.
472 /// Visits all the members of a loop, adjusting their BlockData according to
473 /// the loop's pseudo-node.
474 static void unwrapLoop(BlockFrequencyInfoImplBase &BFI, LoopData &Loop) {
475 DEBUG(dbgs() << "unwrap-loop-package: " << BFI.getLoopName(Loop)
476 << ": mass = " << Loop.Mass << ", scale = " << Loop.Scale
478 Loop.Scale *= Loop.Mass.toScaled();
479 Loop.IsPackaged = false;
480 DEBUG(dbgs() << " => combined-scale = " << Loop.Scale << "\n");
482 // Propagate the head scale through the loop. Since members are visited in
483 // RPO, the head scale will be updated by the loop scale first, and then the
484 // final head scale will be used for updated the rest of the members.
485 for (const BlockNode &N : Loop.Nodes) {
486 const auto &Working = BFI.Working[N.Index];
487 Scaled64 &F = Working.isAPackage() ? Working.getPackagedLoop()->Scale
488 : BFI.Freqs[N.Index].Scaled;
489 Scaled64 New = Loop.Scale * F;
490 DEBUG(dbgs() << " - " << BFI.getBlockName(N) << ": " << F << " => " << New
496 void BlockFrequencyInfoImplBase::unwrapLoops() {
497 // Set initial frequencies from loop-local masses.
498 for (size_t Index = 0; Index < Working.size(); ++Index)
499 Freqs[Index].Scaled = Working[Index].Mass.toScaled();
501 for (LoopData &Loop : Loops)
502 unwrapLoop(*this, Loop);
505 void BlockFrequencyInfoImplBase::finalizeMetrics() {
506 // Unwrap loop packages in reverse post-order, tracking min and max
508 auto Min = Scaled64::getLargest();
509 auto Max = Scaled64::getZero();
510 for (size_t Index = 0; Index < Working.size(); ++Index) {
511 // Update min/max scale.
512 Min = std::min(Min, Freqs[Index].Scaled);
513 Max = std::max(Max, Freqs[Index].Scaled);
516 // Convert to integers.
517 convertFloatingToInteger(*this, Min, Max);
519 // Clean up data structures.
522 // Print out the final stats.
527 BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const {
530 return Freqs[Node.Index].Integer;
534 BlockFrequencyInfoImplBase::getBlockProfileCount(const Function &F,
535 const BlockNode &Node) const {
536 auto EntryCount = F.getEntryCount();
539 // Use 128 bit APInt to do the arithmetic to avoid overflow.
540 APInt BlockCount(128, EntryCount.getValue());
541 APInt BlockFreq(128, getBlockFreq(Node).getFrequency());
542 APInt EntryFreq(128, getEntryFreq());
543 BlockCount *= BlockFreq;
544 BlockCount = BlockCount.udiv(EntryFreq);
545 return BlockCount.getLimitedValue();
549 BlockFrequencyInfoImplBase::getFloatingBlockFreq(const BlockNode &Node) const {
551 return Scaled64::getZero();
552 return Freqs[Node.Index].Scaled;
555 void BlockFrequencyInfoImplBase::setBlockFreq(const BlockNode &Node,
557 assert(Node.isValid() && "Expected valid node");
558 assert(Node.Index < Freqs.size() && "Expected legal index");
559 Freqs[Node.Index].Integer = Freq;
563 BlockFrequencyInfoImplBase::getBlockName(const BlockNode &Node) const {
564 return std::string();
568 BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
569 return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
573 BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
574 const BlockNode &Node) const {
575 return OS << getFloatingBlockFreq(Node);
579 BlockFrequencyInfoImplBase::printBlockFreq(raw_ostream &OS,
580 const BlockFrequency &Freq) const {
581 Scaled64 Block(Freq.getFrequency(), 0);
582 Scaled64 Entry(getEntryFreq(), 0);
584 return OS << Block / Entry;
587 void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {
588 Start = OuterLoop.getHeader();
589 Nodes.reserve(OuterLoop.Nodes.size());
590 for (auto N : OuterLoop.Nodes)
595 void IrreducibleGraph::addNodesInFunction() {
597 for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index)
598 if (!BFI.Working[Index].isPackaged())
603 void IrreducibleGraph::indexNodes() {
604 for (auto &I : Nodes)
605 Lookup[I.Node.Index] = &I;
608 void IrreducibleGraph::addEdge(IrrNode &Irr, const BlockNode &Succ,
609 const BFIBase::LoopData *OuterLoop) {
610 if (OuterLoop && OuterLoop->isHeader(Succ))
612 auto L = Lookup.find(Succ.Index);
613 if (L == Lookup.end())
615 IrrNode &SuccIrr = *L->second;
616 Irr.Edges.push_back(&SuccIrr);
617 SuccIrr.Edges.push_front(&Irr);
622 template <> struct GraphTraits<IrreducibleGraph> {
623 typedef bfi_detail::IrreducibleGraph GraphT;
625 typedef const GraphT::IrrNode NodeType;
626 typedef const GraphT::IrrNode *NodeRef;
627 typedef GraphT::IrrNode::iterator ChildIteratorType;
629 static const NodeType *getEntryNode(const GraphT &G) {
632 static ChildIteratorType child_begin(NodeType *N) { return N->succ_begin(); }
633 static ChildIteratorType child_end(NodeType *N) { return N->succ_end(); }
635 } // end namespace llvm
637 /// \brief Find extra irreducible headers.
639 /// Find entry blocks and other blocks with backedges, which exist when \c G
640 /// contains irreducible sub-SCCs.
641 static void findIrreducibleHeaders(
642 const BlockFrequencyInfoImplBase &BFI,
643 const IrreducibleGraph &G,
644 const std::vector<const IrreducibleGraph::IrrNode *> &SCC,
645 LoopData::NodeList &Headers, LoopData::NodeList &Others) {
646 // Map from nodes in the SCC to whether it's an entry block.
647 SmallDenseMap<const IrreducibleGraph::IrrNode *, bool, 8> InSCC;
649 // InSCC also acts the set of nodes in the graph. Seed it.
650 for (const auto *I : SCC)
653 for (auto I = InSCC.begin(), E = InSCC.end(); I != E; ++I) {
654 auto &Irr = *I->first;
655 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
659 // This is an entry block.
661 Headers.push_back(Irr.Node);
662 DEBUG(dbgs() << " => entry = " << BFI.getBlockName(Irr.Node) << "\n");
666 assert(Headers.size() >= 2 &&
667 "Expected irreducible CFG; -loop-info is likely invalid");
668 if (Headers.size() == InSCC.size()) {
669 // Every block is a header.
670 std::sort(Headers.begin(), Headers.end());
674 // Look for extra headers from irreducible sub-SCCs.
675 for (const auto &I : InSCC) {
676 // Entry blocks are already headers.
680 auto &Irr = *I.first;
681 for (const auto *P : make_range(Irr.pred_begin(), Irr.pred_end())) {
682 // Skip forward edges.
683 if (P->Node < Irr.Node)
686 // Skip predecessors from entry blocks. These can have inverted
691 // Store the extra header.
692 Headers.push_back(Irr.Node);
693 DEBUG(dbgs() << " => extra = " << BFI.getBlockName(Irr.Node) << "\n");
696 if (Headers.back() == Irr.Node)
697 // Added this as a header.
700 // This is not a header.
701 Others.push_back(Irr.Node);
702 DEBUG(dbgs() << " => other = " << BFI.getBlockName(Irr.Node) << "\n");
704 std::sort(Headers.begin(), Headers.end());
705 std::sort(Others.begin(), Others.end());
708 static void createIrreducibleLoop(
709 BlockFrequencyInfoImplBase &BFI, const IrreducibleGraph &G,
710 LoopData *OuterLoop, std::list<LoopData>::iterator Insert,
711 const std::vector<const IrreducibleGraph::IrrNode *> &SCC) {
712 // Translate the SCC into RPO.
713 DEBUG(dbgs() << " - found-scc\n");
715 LoopData::NodeList Headers;
716 LoopData::NodeList Others;
717 findIrreducibleHeaders(BFI, G, SCC, Headers, Others);
719 auto Loop = BFI.Loops.emplace(Insert, OuterLoop, Headers.begin(),
720 Headers.end(), Others.begin(), Others.end());
722 // Update loop hierarchy.
723 for (const auto &N : Loop->Nodes)
724 if (BFI.Working[N.Index].isLoopHeader())
725 BFI.Working[N.Index].Loop->Parent = &*Loop;
727 BFI.Working[N.Index].Loop = &*Loop;
730 iterator_range<std::list<LoopData>::iterator>
731 BlockFrequencyInfoImplBase::analyzeIrreducible(
732 const IrreducibleGraph &G, LoopData *OuterLoop,
733 std::list<LoopData>::iterator Insert) {
734 assert((OuterLoop == nullptr) == (Insert == Loops.begin()));
735 auto Prev = OuterLoop ? std::prev(Insert) : Loops.end();
737 for (auto I = scc_begin(G); !I.isAtEnd(); ++I) {
741 // Translate the SCC into RPO.
742 createIrreducibleLoop(*this, G, OuterLoop, Insert, *I);
746 return make_range(std::next(Prev), Insert);
747 return make_range(Loops.begin(), Insert);
751 BlockFrequencyInfoImplBase::updateLoopWithIrreducible(LoopData &OuterLoop) {
752 OuterLoop.Exits.clear();
753 for (auto &Mass : OuterLoop.BackedgeMass)
754 Mass = BlockMass::getEmpty();
755 auto O = OuterLoop.Nodes.begin() + 1;
756 for (auto I = O, E = OuterLoop.Nodes.end(); I != E; ++I)
757 if (!Working[I->Index].isPackaged())
759 OuterLoop.Nodes.erase(O, OuterLoop.Nodes.end());
762 void BlockFrequencyInfoImplBase::adjustLoopHeaderMass(LoopData &Loop) {
763 assert(Loop.isIrreducible() && "this only makes sense on irreducible loops");
765 // Since the loop has more than one header block, the mass flowing back into
766 // each header will be different. Adjust the mass in each header loop to
767 // reflect the masses flowing through back edges.
769 // To do this, we distribute the initial mass using the backedge masses
770 // as weights for the distribution.
771 BlockMass LoopMass = BlockMass::getFull();
774 DEBUG(dbgs() << "adjust-loop-header-mass:\n");
775 for (uint32_t H = 0; H < Loop.NumHeaders; ++H) {
776 auto &HeaderNode = Loop.Nodes[H];
777 auto &BackedgeMass = Loop.BackedgeMass[Loop.getHeaderIndex(HeaderNode)];
778 DEBUG(dbgs() << " - Add back edge mass for node "
779 << getBlockName(HeaderNode) << ": " << BackedgeMass << "\n");
780 if (BackedgeMass.getMass() > 0)
781 Dist.addLocal(HeaderNode, BackedgeMass.getMass());
783 DEBUG(dbgs() << " Nothing added. Back edge mass is zero\n");
786 DitheringDistributer D(Dist, LoopMass);
788 DEBUG(dbgs() << " Distribute loop mass " << LoopMass
789 << " to headers using above weights\n");
790 for (const Weight &W : Dist.Weights) {
791 BlockMass Taken = D.takeMass(W.Amount);
792 assert(W.Type == Weight::Local && "all weights should be local");
793 Working[W.TargetNode.Index].getMass() = Taken;
794 DEBUG(debugAssign(*this, D, W.TargetNode, Taken, nullptr));