LLVM 24.0.0git
RegionInfoImpl.h
Go to the documentation of this file.
1//===- RegionInfoImpl.h - SESE region detection analysis --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// Detects single entry single exit regions in the control flow graph.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
12#define LLVM_ANALYSIS_REGIONINFOIMPL_H
13
16#include "llvm/ADT/STLExtras.h"
22#include "llvm/Config/llvm-config.h"
23#include "llvm/Support/Debug.h"
25#include <algorithm>
26#include <cassert>
27#include <iterator>
28#include <memory>
29#include <set>
30#include <string>
31#include <type_traits>
32#include <vector>
33
34#define DEBUG_TYPE "region"
35
36namespace llvm {
37class raw_ostream;
38
39//===----------------------------------------------------------------------===//
40/// RegionBase Implementation
41template <class Tr>
42RegionBase<Tr>::RegionBase(BlockT *Entry, BlockT *Exit,
43 typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
44 RegionT *Parent)
45 : RegionNodeBase<Tr>(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
46
47template <class Tr>
49 // Only clean the cache for this Region. Caches of child Regions will be
50 // cleaned when the child Regions are deleted.
51 BBNodeMap.clear();
52}
53
54template <class Tr>
56 this->entry.setPointer(BB);
57}
58
59template <class Tr>
61 assert(exit && "No exit to replace!");
62 exit = BB;
63}
64
65template <class Tr>
67 std::vector<RegionT *> RegionQueue;
68 BlockT *OldEntry = getEntry();
69
70 RegionQueue.push_back(static_cast<RegionT *>(this));
71 while (!RegionQueue.empty()) {
72 RegionT *R = RegionQueue.back();
73 RegionQueue.pop_back();
74
75 R->replaceEntry(NewEntry);
76 for (std::unique_ptr<RegionT> &Child : *R) {
77 if (Child->getEntry() == OldEntry)
78 RegionQueue.push_back(Child.get());
79 }
80 }
81}
82
83template <class Tr>
85 std::vector<RegionT *> RegionQueue;
86 BlockT *OldExit = getExit();
87
88 RegionQueue.push_back(static_cast<RegionT *>(this));
89 while (!RegionQueue.empty()) {
90 RegionT *R = RegionQueue.back();
91 RegionQueue.pop_back();
92
93 R->replaceExit(NewExit);
94 for (std::unique_ptr<RegionT> &Child : *R) {
95 if (Child->getExit() == OldExit)
96 RegionQueue.push_back(Child.get());
97 }
98 }
99}
100
101template <class Tr>
102bool RegionBase<Tr>::contains(const BlockT *B) const {
103 BlockT *BB = const_cast<BlockT *>(B);
104
105 if (!DT->getNode(BB))
106 return false;
107
108 BlockT *entry = getEntry(), *exit = getExit();
109
110 // Toplevel region.
111 if (!exit)
112 return true;
113
114 return (DT->dominates(entry, BB) &&
115 !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
116}
117
118template <class Tr>
119bool RegionBase<Tr>::contains(const LoopT *L) const {
120 // BBs that are not part of any loop are element of the Loop
121 // described by the NULL pointer. This loop is not part of any region,
122 // except if the region describes the whole function.
123 if (!L)
124 return getExit() == nullptr;
125
126 if (!contains(L->getHeader()))
127 return false;
128
129 SmallVector<BlockT *, 8> ExitingBlocks;
130 L->getExitingBlocks(ExitingBlocks);
131
132 for (BlockT *BB : ExitingBlocks) {
133 if (!contains(BB))
134 return false;
135 }
136
137 return true;
138}
139
140template <class Tr>
141typename Tr::LoopT *RegionBase<Tr>::outermostLoopInRegion(LoopT *L) const {
142 if (!contains(L))
143 return nullptr;
144
145 while (L && contains(L->getParentLoop())) {
146 L = L->getParentLoop();
147 }
148
149 return L;
150}
151
152template <class Tr>
153typename Tr::LoopT *RegionBase<Tr>::outermostLoopInRegion(LoopInfoT *LI,
154 BlockT *BB) const {
155 assert(LI && BB && "LI and BB cannot be null!");
156 LoopT *L = LI->getLoopFor(BB);
157 return outermostLoopInRegion(L);
158}
159
160template <class Tr>
161typename RegionBase<Tr>::BlockT *RegionBase<Tr>::getEnteringBlock() const {
162 auto isEnteringBlock = [&](BlockT *Pred, bool AllowRepeats) -> BlockT * {
163 assert(!AllowRepeats && "Unexpected parameter value.");
164 return DT->getNode(Pred) && !contains(Pred) ? Pred : nullptr;
165 };
167 isEnteringBlock);
168}
169
170template <class Tr>
172 SmallVectorImpl<BlockT *> &Exitings) const {
173 bool CoverAll = true;
174
175 if (!exit)
176 return CoverAll;
177
178 for (BlockT *Pred : llvm::inverse_children<BlockT *>(exit)) {
179 if (contains(Pred)) {
180 Exitings.push_back(Pred);
181 continue;
182 }
183
184 CoverAll = false;
185 }
186
187 return CoverAll;
188}
189
190template <class Tr>
191typename RegionBase<Tr>::BlockT *RegionBase<Tr>::getExitingBlock() const {
192 BlockT *exit = getExit();
193 if (!exit)
194 return nullptr;
195
196 auto isContained = [&](BlockT *Pred, bool AllowRepeats) -> BlockT * {
197 assert(!AllowRepeats && "Unexpected parameter value.");
198 return contains(Pred) ? Pred : nullptr;
199 };
202}
203
204template <class Tr>
208
209template <class Tr>
210std::string RegionBase<Tr>::getNameStr() const {
211 std::string exitName;
212 std::string entryName;
213
214 if (getEntry()->getName().empty()) {
215 raw_string_ostream OS(entryName);
216
217 getEntry()->printAsOperand(OS, false);
218 } else
219 entryName = std::string(getEntry()->getName());
220
221 if (getExit()) {
222 if (getExit()->getName().empty()) {
223 raw_string_ostream OS(exitName);
224
225 getExit()->printAsOperand(OS, false);
226 } else
227 exitName = std::string(getExit()->getName());
228 } else
229 exitName = "<Function Return>";
230
231 return entryName + " => " + exitName;
232}
233
234template <class Tr>
235void RegionBase<Tr>::verifyBBInRegion(BlockT *BB) const {
236 if (!contains(BB))
237 report_fatal_error("Broken region found: enumerated BB not in region!");
238
239 BlockT *entry = getEntry(), *exit = getExit();
240
241 for (BlockT *Succ : llvm::children<BlockT *>(BB)) {
242 if (!contains(Succ) && exit != Succ)
243 report_fatal_error("Broken region found: edges leaving the region must go "
244 "to the exit node!");
245 }
246
247 if (entry != BB) {
248 for (BlockT *Pred : llvm::inverse_children<BlockT *>(BB)) {
249 // Allow predecessors that are unreachable, as these are ignored during
250 // region analysis.
251 if (!contains(Pred) && DT->isReachableFromEntry(Pred))
252 report_fatal_error("Broken region found: edges entering the region must "
253 "go to the entry node!");
254 }
255 }
256}
257
258template <class Tr>
259void RegionBase<Tr>::verifyWalk(BlockT *BB, std::set<BlockT *> *visited) const {
260 BlockT *exit = getExit();
261
262 visited->insert(BB);
263
264 verifyBBInRegion(BB);
265
266 for (BlockT *Succ : llvm::children<BlockT *>(BB)) {
267 if (Succ != exit && visited->find(Succ) == visited->end())
268 verifyWalk(Succ, visited);
269 }
270}
271
272template <class Tr>
274 // Only do verification when user wants to.
276 return;
277
278 std::set<BlockT *> visited;
279 verifyWalk(getEntry(), &visited);
280}
281
282template <class Tr>
283void RegionBase<Tr>::verifyRegionNest() const {
284 for (const std::unique_ptr<RegionT> &R : *this)
285 R->verifyRegionNest();
286
287 verifyRegion();
288}
289
290template <class Tr>
294
295template <class Tr>
299
300template <class Tr>
304 static_cast<const RegionT *>(this));
305}
306
307template <class Tr>
311 static_cast<const RegionT *>(this));
313
314template <class Tr>
315typename Tr::RegionT *RegionBase<Tr>::getSubRegionNode(BlockT *BB) const {
316 using RegionT = typename Tr::RegionT;
317
318 RegionT *R = RI->getRegionFor(BB);
320 if (!R || R == this)
321 return nullptr;
322
323 // If we pass the BB out of this region, that means our code is broken.
324 assert(contains(R) && "BB not in current region!");
325
326 while (contains(R->getParent()) && R->getParent() != this)
327 R = R->getParent();
328
329 if (R->getEntry() != BB)
330 return nullptr;
332 return R;
333}
334
335template <class Tr>
336typename Tr::RegionNodeT *RegionBase<Tr>::getBBNode(BlockT *BB) const {
337 assert(contains(BB) && "Can get BB node out of this region!");
338
339 auto [at, Inserted] = BBNodeMap.try_emplace(BB);
340 if (Inserted) {
341 auto Deconst = const_cast<RegionBase<Tr> *>(this);
342 at->second =
343 std::make_unique<RegionNodeT>(static_cast<RegionT *>(Deconst), BB);
344 }
345 return at->second.get();
347
348template <class Tr>
349typename Tr::RegionNodeT *RegionBase<Tr>::getNode(BlockT *BB) const {
350 assert(contains(BB) && "Can get BB node out of this region!");
351 if (RegionT *Child = getSubRegionNode(BB))
352 return Child->getNode();
353
354 return getBBNode(BB);
356
357template <class Tr>
359 for (std::unique_ptr<RegionT> &R : *this) {
360 R->parent = To;
361 To->children.push_back(std::move(R));
362 }
363 children.clear();
364}
365
366template <class Tr>
367void RegionBase<Tr>::addSubRegion(RegionT *SubRegion, bool moveChildren) {
368 assert(!SubRegion->parent && "SubRegion already has a parent!");
369 assert(llvm::none_of(*this,
370 [&](const std::unique_ptr<RegionT> &R) {
371 return R.get() == SubRegion;
372 }) &&
373 "Subregion already exists!");
374
375 SubRegion->parent = static_cast<RegionT *>(this);
376 children.push_back(std::unique_ptr<RegionT>(SubRegion));
377
378 if (!moveChildren)
379 return;
380
381 assert(SubRegion->children.empty() &&
382 "SubRegions that contain children are not supported");
383
384 for (RegionNodeT *Element : elements()) {
385 if (!Element->isSubRegion()) {
386 BlockT *BB = Element->template getNodeAs<BlockT>();
387
388 if (SubRegion->contains(BB))
389 RI->setRegionFor(BB, SubRegion);
390 }
391 }
392
393 std::vector<std::unique_ptr<RegionT>> Keep;
394 for (std::unique_ptr<RegionT> &R : *this) {
395 if (SubRegion->contains(R.get()) && R.get() != SubRegion) {
396 R->parent = SubRegion;
397 SubRegion->children.push_back(std::move(R));
398 } else
399 Keep.push_back(std::move(R));
400 }
402 children.clear();
403 children.insert(
404 children.begin(),
405 std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
406 std::move_iterator<typename RegionSet::iterator>(Keep.end()));
407}
409template <class Tr>
410typename Tr::RegionT *RegionBase<Tr>::removeSubRegion(RegionT *Child) {
411 assert(Child->parent == this && "Child is not a child of this region!");
412 Child->parent = nullptr;
413 typename RegionSet::iterator I =
414 llvm::find_if(children, [&](const std::unique_ptr<RegionT> &R) {
415 return R.get() == Child;
416 });
417 assert(I != children.end() && "Region does not exit. Unable to remove.");
418 children.erase(children.begin() + (I - begin()));
419 return Child;
421
422template <class Tr>
423unsigned RegionBase<Tr>::getDepth() const {
424 unsigned Depth = 0;
425
426 for (RegionT *R = getParent(); R != nullptr; R = R->getParent())
427 ++Depth;
428
429 return Depth;
430}
431
432template <class Tr>
433typename Tr::RegionT *RegionBase<Tr>::getExpandedRegion() const {
434 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
435
436 if (NumSuccessors == 0)
437 return nullptr;
438
439 RegionT *R = RI->getRegionFor(exit);
440
441 if (R->getEntry() != exit) {
443 if (!contains(Pred))
444 return nullptr;
445 if (Tr::getNumSuccessors(exit) == 1)
446 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
447 return nullptr;
448 }
450 while (R->getParent() && R->getParent()->getEntry() == exit)
451 R = R->getParent();
452
453 for (BlockT *Pred : llvm::inverse_children<BlockT *>(getExit())) {
454 if (!(contains(Pred) || R->contains(Pred)))
455 return nullptr;
456 }
457
458 return new RegionT(getEntry(), R->getExit(), RI, DT);
459}
460
461template <class Tr>
462void RegionBase<Tr>::print(raw_ostream &OS, bool print_tree, unsigned level,
463 PrintStyle Style) const {
464 if (print_tree)
465 OS.indent(level * 2) << '[' << level << "] " << getNameStr();
466 else
467 OS.indent(level * 2) << getNameStr();
468
469 OS << '\n';
470
471 if (Style != PrintNone) {
472 OS.indent(level * 2) << "{\n";
473 OS.indent(level * 2 + 2);
474
475 if (Style == PrintBB) {
476 for (const auto *BB : blocks())
477 OS << BB->getName() << ", "; // TODO: remove the last ","
478 } else if (Style == PrintRN) {
479 for (const RegionNodeT *Element : elements()) {
480 OS << *Element << ", "; // TODO: remove the last ",
481 }
482 }
483
484 OS << '\n';
485 }
486
487 if (print_tree) {
488 for (const std::unique_ptr<RegionT> &R : *this)
489 R->print(OS, print_tree, level + 1, Style);
490 }
491
492 if (Style != PrintNone)
493 OS.indent(level * 2) << "} \n";
494}
495
496#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
497template <class Tr>
501#endif
502
503//===----------------------------------------------------------------------===//
504// RegionInfoBase implementation
505//
507template <class Tr>
508RegionInfoBase<Tr>::RegionInfoBase() = default;
509
510template <class Tr>
511RegionInfoBase<Tr>::~RegionInfoBase() {
512 releaseMemory();
513}
514
515template <class Tr>
516void RegionInfoBase<Tr>::verifyBBMap(const RegionT *R) const {
517 assert(R && "Re must be non-null");
518 for (const typename Tr::RegionNodeT *Element : R->elements()) {
519 if (Element->isSubRegion()) {
520 const RegionT *SR = Element->template getNodeAs<RegionT>();
521 verifyBBMap(SR);
522 } else {
523 BlockT *BB = Element->template getNodeAs<BlockT>();
524 if (getRegionFor(BB) != R)
525 report_fatal_error("BB map does not match region nesting");
526 }
528}
529
530template <class Tr>
531bool RegionInfoBase<Tr>::isCommonDomFrontier(BlockT *BB, BlockT *entry,
532 BlockT *exit) const {
533 for (BlockT *P : llvm::inverse_children<BlockT *>(BB)) {
534 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
535 return false;
536 }
537
538 return true;
540
541template <class Tr>
542bool RegionInfoBase<Tr>::isRegion(BlockT *entry, BlockT *exit) const {
543 assert(entry && exit && "entry and exit must not be null!");
544
545 using DST = typename DomFrontierT::DomSetType;
546
547 DST *entrySuccs = &DF->find(entry)->second;
548
549 // Exit is the header of a loop that contains the entry. In this case,
550 // the dominance frontier must only contain the exit.
551 if (!DT->dominates(entry, exit)) {
552 for (BlockT *successor : *entrySuccs) {
553 if (successor != exit && successor != entry)
554 return false;
555 }
556
557 return true;
558 }
559
560 DST *exitSuccs = &DF->find(exit)->second;
561
562 // Do not allow edges leaving the region.
563 for (BlockT *Succ : *entrySuccs) {
564 if (Succ == exit || Succ == entry)
565 continue;
566 if (!exitSuccs->contains(Succ))
567 return false;
568 if (!isCommonDomFrontier(Succ, entry, exit))
569 return false;
570 }
571
572 // Do not allow edges pointing into the region.
573 for (BlockT *Succ : *exitSuccs) {
574 if (DT->properlyDominates(entry, Succ) && Succ != exit)
575 return false;
576 }
577
578 return true;
579}
580
581template <class Tr>
582void RegionInfoBase<Tr>::insertShortCut(BlockT *entry, BlockT *exit,
583 BBtoBBMap *ShortCut) const {
584 assert(entry && exit && "entry and exit must not be null!");
585
586 typename BBtoBBMap::iterator e = ShortCut->find(exit);
587
588 if (e == ShortCut->end())
589 // No further region at exit available.
590 (*ShortCut)[entry] = exit;
591 else {
592 // We found a region e that starts at exit. Therefore (entry, e->second)
593 // is also a region, that is larger than (entry, exit). Insert the
594 // larger one.
595 BlockT *BB = e->second;
596 (*ShortCut)[entry] = BB;
597 }
598}
599
600template <class Tr>
601typename Tr::DomTreeNodeT *
602RegionInfoBase<Tr>::getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const {
603 typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
604
605 if (e == ShortCut->end())
606 return N->getIDom();
607
608 return PDT->getNode(e->second)->getIDom();
609}
610
611template <class Tr>
612bool RegionInfoBase<Tr>::isTrivialRegion(BlockT *entry, BlockT *exit) const {
613 assert(entry && exit && "entry and exit must not be null!");
614
615 unsigned num_successors =
616 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
617
618 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
619 return true;
620
621 return false;
622}
623
624template <class Tr>
625typename Tr::RegionT *RegionInfoBase<Tr>::createRegion(BlockT *entry,
626 BlockT *exit) {
627 assert(entry && exit && "entry and exit must not be null!");
628
629 if (isTrivialRegion(entry, exit))
630 return nullptr;
631
632 RegionT *region =
633 new RegionT(entry, exit, static_cast<RegionInfoT *>(this), DT);
634 BBtoRegion.insert({entry, region});
635
636 region->verifyRegion();
637
638 Tr::updateStatistics(region);
639 return region;
640}
641
642template <class Tr>
643void RegionInfoBase<Tr>::findRegionsWithEntry(BlockT *entry,
644 BBtoBBMap *ShortCut) {
645 assert(entry);
646
647 DomTreeNodeT *N = PDT->getNode(entry);
648 if (!N)
649 return;
650
651 RegionT *lastRegion = nullptr;
652 BlockT *lastExit = entry;
653
654 // As only a BasicBlock that postdominates entry can finish a region, walk the
655 // post dominance tree upwards.
656 while ((N = getNextPostDom(N, ShortCut))) {
657 BlockT *exit = N->getBlock();
658
659 if (!exit)
660 break;
661
662 if (isRegion(entry, exit)) {
663 RegionT *newRegion = createRegion(entry, exit);
664
665 if (lastRegion)
666 newRegion->addSubRegion(lastRegion);
667
668 lastRegion = newRegion;
669 lastExit = exit;
670 }
671
672 // This can never be a region, so stop the search.
673 if (!DT->dominates(entry, exit))
674 break;
675 }
676
677 // Tried to create regions from entry to lastExit. Next time take a
678 // shortcut from entry to lastExit.
679 if (lastExit != entry)
680 insertShortCut(entry, lastExit, ShortCut);
681}
682
683template <class Tr>
684void RegionInfoBase<Tr>::scanForRegions(FuncT &F, BBtoBBMap *ShortCut) {
685 using FuncPtrT = std::add_pointer_t<FuncT>;
686
687 BlockT *entry = GraphTraits<FuncPtrT>::getEntryNode(&F);
688 DomTreeNodeT *N = DT->getNode(entry);
689
690 // Iterate over the dominance tree in post order to start with the small
691 // regions from the bottom of the dominance tree. If the small regions are
692 // detected first, detection of bigger regions is faster, as we can jump
693 // over the small regions.
694 for (auto DomNode : post_order(N))
695 findRegionsWithEntry(DomNode->getBlock(), ShortCut);
696}
697
698template <class Tr>
699typename Tr::RegionT *RegionInfoBase<Tr>::getTopMostParent(RegionT *region) {
700 while (region->getParent())
701 region = region->getParent();
702
703 return region;
704}
705
706template <class Tr>
707void RegionInfoBase<Tr>::buildRegionsTree(DomTreeNodeT *N, RegionT *region) {
708 BlockT *BB = N->getBlock();
709
710 // Passed region exit
711 while (BB == region->getExit())
712 region = region->getParent();
713
714 auto [It, Inserted] = BBtoRegion.try_emplace(BB, region);
715
716 // This basic block is a start block of a region. It is already in the
717 // BBtoRegion relation. Only the child basic blocks have to be updated.
718 if (!Inserted) {
719 RegionT *newRegion = It->second;
720 region->addSubRegion(getTopMostParent(newRegion));
721 region = newRegion;
722 }
723
724 for (DomTreeNodeBase<BlockT> *C : *N) {
725 buildRegionsTree(C, region);
726 }
727}
728
729#ifdef EXPENSIVE_CHECKS
730template <class Tr>
732#else
733template <class Tr>
735#endif
736
737template <class Tr>
738typename Tr::RegionT::PrintStyle RegionInfoBase<Tr>::printStyle =
740
741template <class Tr>
743 OS << "Region tree:\n";
744 TopLevelRegion->print(OS, true, 0, printStyle);
745 OS << "End region tree\n";
746}
747
748#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
749template <class Tr>
751#endif
752
753template <class Tr> void RegionInfoBase<Tr>::releaseMemory() {
754 BBtoRegion.clear();
755 if (TopLevelRegion) {
756 delete TopLevelRegion;
757 TopLevelRegion = nullptr;
758 }
759}
760
761template <class Tr>
763 // Do only verify regions if explicitely activated using EXPENSIVE_CHECKS or
764 // -verify-region-info
766 return;
767
768 TopLevelRegion->verifyRegionNest();
769
770 verifyBBMap(TopLevelRegion);
771}
772
773// Region pass manager support.
774template <class Tr>
775typename Tr::RegionT *RegionInfoBase<Tr>::getRegionFor(BlockT *BB) const {
776 return BBtoRegion.lookup(BB);
777}
778
779template <class Tr>
780void RegionInfoBase<Tr>::setRegionFor(BlockT *BB, RegionT *R) {
781 BBtoRegion[BB] = R;
782}
783
784template <class Tr>
785typename Tr::RegionT *RegionInfoBase<Tr>::operator[](BlockT *BB) const {
786 return getRegionFor(BB);
787}
788
789template <class Tr>
790typename RegionInfoBase<Tr>::BlockT *
792 BlockT *Exit = nullptr;
793
794 while (true) {
795 // Get largest region that starts at BB.
796 RegionT *R = getRegionFor(BB);
797 while (R && R->getParent() && R->getParent()->getEntry() == BB)
798 R = R->getParent();
799
800 // Get the single exit of BB.
801 if (R && R->getEntry() == BB)
802 Exit = R->getExit();
803 else if (std::next(BlockTraits::child_begin(BB)) ==
804 BlockTraits::child_end(BB))
805 Exit = *BlockTraits::child_begin(BB);
806 else // No single exit exists.
807 return Exit;
808
809 // Get largest region that starts at Exit.
810 RegionT *ExitR = getRegionFor(Exit);
811 while (ExitR && ExitR->getParent() &&
812 ExitR->getParent()->getEntry() == Exit)
813 ExitR = ExitR->getParent();
814
815 for (BlockT *Pred : llvm::inverse_children<BlockT *>(Exit)) {
816 if (!R->contains(Pred) && !ExitR->contains(Pred))
817 break;
818 }
819
820 // This stops infinite cycles.
821 if (DT->dominates(Exit, BB))
822 break;
823
824 BB = Exit;
825 }
826
827 return Exit;
828}
829
830template <class Tr>
831typename Tr::RegionT *RegionInfoBase<Tr>::getCommonRegion(RegionT *A,
832 RegionT *B) const {
833 assert(A && B && "One of the Regions is NULL");
834
835 if (A->contains(B))
836 return A;
837
838 while (!B->contains(A))
839 B = B->getParent();
840
841 return B;
842}
843
844template <class Tr>
845typename Tr::RegionT *
847 RegionT *ret = Regions.pop_back_val();
848
849 for (RegionT *R : Regions)
850 ret = getCommonRegion(ret, R);
851
852 return ret;
853}
854
855template <class Tr>
856typename Tr::RegionT *
858 RegionT *ret = getRegionFor(BBs.back());
859 BBs.pop_back();
860
861 for (BlockT *BB : BBs)
862 ret = getCommonRegion(ret, getRegionFor(BB));
863
864 return ret;
865}
866
867template <class Tr>
868void RegionInfoBase<Tr>::calculate(FuncT &F) {
869 using FuncPtrT = std::add_pointer_t<FuncT>;
870
871 // ShortCut a function where for every BB the exit of the largest region
872 // starting with BB is stored. These regions can be threated as single BBS.
873 // This improves performance on linear CFGs.
874 BBtoBBMap ShortCut;
875
876 scanForRegions(F, &ShortCut);
878 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
879}
880
881} // end namespace llvm
882
883#undef DEBUG_TYPE
884
885#endif // LLVM_ANALYSIS_REGIONINFOIMPL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const Function * getParent(const Value *V)
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
bool isContained(DIScope *Inner, DIScope *Outer)
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static StringRef getName(Value *V)
convergence region
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the SmallVector class.
Base class for the actual dominator tree node.
void replaceExit(BlockT *BB)
Replace the exit basic block of the region with the new basic block.
std::string getNameStr() const
Returns the name of the Region.
BlockT * getExit() const
Get the exit BasicBlock of the Region.
Definition RegionInfo.h:360
void transferChildrenTo(RegionT *To)
Move all direct child nodes of this Region to another Region.
RegionNodeT * getBBNode(BlockT *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
bool getExitingBlocks(SmallVectorImpl< BlockT * > &Exitings) const
Collect all blocks of this region's single exit edge, if existing.
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
Definition RegionInfo.h:371
iterator_range< element_iterator > elements()
Definition RegionInfo.h:645
LoopT * outermostLoopInRegion(LoopT *L) const
Get the outermost loop in the region that contains a loop.
unsigned getDepth() const
Get the nesting level of this Region.
void replaceEntry(BlockT *BB)
Replace the entry basic block of the region with the new basic block.
void dump() const
Print the region to stderr.
void replaceEntryRecursive(BlockT *NewEntry)
Recursively replace the entry basic block of the region.
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
element_iterator element_begin()
void replaceExitRecursive(BlockT *NewExit)
Recursively replace the exit basic block of the region.
void verifyRegion() const
Verify if the region is a correct region.
BlockT * getEntry() const
Get the entry BasicBlock of the Region.
Definition RegionInfo.h:323
void addSubRegion(RegionT *SubRegion, bool moveChildren=false)
Add a new subregion to this Region.
element_iterator element_end()
bool isSimple() const
Is this a simple region?
bool isTopLevelRegion() const
Check if a Region is the TopLevel region.
Definition RegionInfo.h:386
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Create a new region.
BlockT * getExitingBlock() const
Return the first block of this region's single exit edge, if existing.
~RegionBase()
Delete the Region and all its subregions.
df_iterator< const RegionNodeT *, df_iterator_default_set< const RegionNodeT * >, false, GraphTraits< const RegionNodeT * > > const_element_iterator
Definition RegionInfo.h:638
PrintStyle
PrintStyle - Print region in difference ways.
Definition RegionInfo.h:430
RegionT * removeSubRegion(RegionT *SubRegion)
Remove a subregion from this Region.
BlockT * getEnteringBlock() const
Return the first block of this region's single entry edge, if existing.
RegionT * getExpandedRegion() const
Return a new (non-canonical) region, that is obtained by joining this region with its predecessors.
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
RegionT * getSubRegionNode(BlockT *BB) const
Get the subregion that starts at a BasicBlock.
df_iterator< RegionNodeT *, df_iterator_default_set< RegionNodeT * >, false, GraphTraits< RegionNodeT * > > element_iterator
Definition RegionInfo.h:634
BlockT * getMaxRegionExit(BlockT *BB) const
Return the exit of the maximal refined region, that starts at a BasicBlock.
static bool VerifyRegionInfo
Definition RegionInfo.h:793
void print(raw_ostream &OS) const
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
void verifyAnalysis() const
void setRegionFor(BlockT *BB, RegionT *R)
Set the smallest region that surrounds a basic block.
RegionT * operator[](BlockT *BB) const
A shortcut for getRegionFor().
static RegionT::PrintStyle printStyle
Definition RegionInfo.h:794
RegionT * getCommonRegion(RegionT *A, RegionT *B) const
Find the smallest region that contains two regions.
RegionNodeBase(RegionT *Parent, BlockT *Entry, bool isSubRegion=false)
Create a RegionNode.
Definition RegionInfo.h:151
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
Assignment Tracking (at).
Definition DebugInfo.h:180
constexpr double e
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
T * find_singleton(R &&Range, Predicate P, bool AllowRepeats=false)
Return the single value in Range that satisfies P(<member of Range> *, AllowRepeats)->T * returning n...
Definition STLExtras.h:1853
auto post_order(const T &G)
Post-order traversal of a graph.
iterator_range< typename GraphTraits< Inverse< GraphType > >::ChildIteratorType > inverse_children(const typename GraphTraits< GraphType >::NodeRef &G)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
@ Keep
No function return thunk.
Definition CodeGen.h:317
#define N