1 //===- RegionInfo.h - SESE region analysis ----------------------*- C++ -*-===//
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 // Calculate a program structure tree built out of single entry single exit
12 // The basic ideas are taken from "The Program Structure Tree - Richard Johnson,
13 // David Pearson, Keshav Pingali - 1994", however enriched with ideas from "The
14 // Refined Process Structure Tree - Jussi Vanhatalo, Hagen Voelyer, Jana
16 // The algorithm to calculate these data structures however is completely
17 // different, as it takes advantage of existing information already available
18 // in (Post)dominace tree and dominance frontier passes. This leads to a simpler
19 // and in practice hopefully better performing algorithm. The runtime of the
20 // algorithms described in the papers above are both linear in graph size,
21 // O(V+E), whereas this algorithm is not, as the dominance frontier information
22 // itself is not, but in practice runtime seems to be in the order of magnitude
23 // of dominance tree calculation.
25 // WARNING: LLVM is generally very concerned about compile time such that
26 // the use of additional analysis passes in the default
27 // optimization sequence is avoided as much as possible.
28 // Specifically, if you do not need the RegionInfo, but dominance
29 // information could be sufficient please base your work only on
30 // the dominator tree. Most passes maintain it, such that using
31 // it has often near zero cost. In contrast RegionInfo is by
32 // default not available, is not maintained by existing
33 // transformations and there is no intention to do so.
35 //===----------------------------------------------------------------------===//
37 #ifndef LLVM_ANALYSIS_REGIONINFO_H
38 #define LLVM_ANALYSIS_REGIONINFO_H
40 #include "llvm/ADT/DepthFirstIterator.h"
41 #include "llvm/ADT/PointerIntPair.h"
42 #include "llvm/ADT/iterator_range.h"
43 #include "llvm/IR/CFG.h"
44 #include "llvm/IR/Dominators.h"
45 #include "llvm/IR/PassManager.h"
52 // Class to be specialized for different users of RegionInfo
53 // (i.e. BasicBlocks or MachineBasicBlocks). This is only to avoid needing to
54 // pass around an unreasonable number of template parameters.
55 template <class FuncT_>
62 typedef typename FuncT_::UnknownRegionTypeError BrokenT;
66 class DominanceFrontier;
69 struct PostDominatorTree;
72 template <class RegionTr>
76 template <class RegionTr>
80 struct RegionTraits<Function> {
81 typedef Function FuncT;
82 typedef BasicBlock BlockT;
83 typedef Region RegionT;
84 typedef RegionNode RegionNodeT;
85 typedef RegionInfo RegionInfoT;
86 typedef DominatorTree DomTreeT;
87 typedef DomTreeNode DomTreeNodeT;
88 typedef DominanceFrontier DomFrontierT;
89 typedef PostDominatorTree PostDomTreeT;
90 typedef Instruction InstT;
92 typedef LoopInfo LoopInfoT;
94 static unsigned getNumSuccessors(BasicBlock *BB) {
95 return BB->getTerminator()->getNumSuccessors();
99 /// @brief Marker class to iterate over the elements of a Region in flat mode.
101 /// The class is used to either iterate in Flat mode or by not using it to not
102 /// iterate in Flat mode. During a Flat mode iteration all Regions are entered
103 /// and the iteration returns every BasicBlock. If the Flat mode is not
104 /// selected for SubRegions just one RegionNode containing the subregion is
106 template <class GraphType>
109 /// @brief A RegionNode represents a subregion or a BasicBlock that is part of a
112 class RegionNodeBase {
113 friend class RegionBase<Tr>;
116 typedef typename Tr::BlockT BlockT;
117 typedef typename Tr::RegionT RegionT;
120 RegionNodeBase(const RegionNodeBase &) = delete;
121 const RegionNodeBase &operator=(const RegionNodeBase &) = delete;
123 /// This is the entry basic block that starts this region node. If this is a
124 /// BasicBlock RegionNode, then entry is just the basic block, that this
125 /// RegionNode represents. Otherwise it is the entry of this (Sub)RegionNode.
127 /// In the BBtoRegionNode map of the parent of this node, BB will always map
128 /// to this node no matter which kind of node this one is.
130 /// The node can hold either a Region or a BasicBlock.
131 /// Use one bit to save, if this RegionNode is a subregion or BasicBlock
133 PointerIntPair<BlockT *, 1, bool> entry;
135 /// @brief The parent Region of this RegionNode.
140 /// @brief Create a RegionNode.
142 /// @param Parent The parent of this RegionNode.
143 /// @param Entry The entry BasicBlock of the RegionNode. If this
144 /// RegionNode represents a BasicBlock, this is the
145 /// BasicBlock itself. If it represents a subregion, this
146 /// is the entry BasicBlock of the subregion.
147 /// @param isSubRegion If this RegionNode represents a SubRegion.
148 inline RegionNodeBase(RegionT *Parent, BlockT *Entry,
149 bool isSubRegion = false)
150 : entry(Entry, isSubRegion), parent(Parent) {}
153 /// @brief Get the parent Region of this RegionNode.
155 /// The parent Region is the Region this RegionNode belongs to. If for
156 /// example a BasicBlock is element of two Regions, there exist two
157 /// RegionNodes for this BasicBlock. Each with the getParent() function
158 /// pointing to the Region this RegionNode belongs to.
160 /// @return Get the parent Region of this RegionNode.
161 inline RegionT *getParent() const { return parent; }
163 /// @brief Get the entry BasicBlock of this RegionNode.
165 /// If this RegionNode represents a BasicBlock this is just the BasicBlock
166 /// itself, otherwise we return the entry BasicBlock of the Subregion
168 /// @return The entry BasicBlock of this RegionNode.
169 inline BlockT *getEntry() const { return entry.getPointer(); }
171 /// @brief Get the content of this RegionNode.
173 /// This can be either a BasicBlock or a subregion. Before calling getNodeAs()
174 /// check the type of the content with the isSubRegion() function call.
176 /// @return The content of this RegionNode.
177 template <class T> inline T *getNodeAs() const;
179 /// @brief Is this RegionNode a subregion?
181 /// @return True if it contains a subregion. False if it contains a
183 inline bool isSubRegion() const { return entry.getInt(); }
186 //===----------------------------------------------------------------------===//
187 /// @brief A single entry single exit Region.
189 /// A Region is a connected subgraph of a control flow graph that has exactly
190 /// two connections to the remaining graph. It can be used to analyze or
191 /// optimize parts of the control flow graph.
193 /// A <em> simple Region </em> is connected to the remaining graph by just two
194 /// edges. One edge entering the Region and another one leaving the Region.
196 /// An <em> extended Region </em> (or just Region) is a subgraph that can be
197 /// transform into a simple Region. The transformation is done by adding
198 /// BasicBlocks that merge several entry or exit edges so that after the merge
199 /// just one entry and one exit edge exists.
201 /// The \e Entry of a Region is the first BasicBlock that is passed after
202 /// entering the Region. It is an element of the Region. The entry BasicBlock
203 /// dominates all BasicBlocks in the Region.
205 /// The \e Exit of a Region is the first BasicBlock that is passed after
206 /// leaving the Region. It is not an element of the Region. The exit BasicBlock,
207 /// postdominates all BasicBlocks in the Region.
209 /// A <em> canonical Region </em> cannot be constructed by combining smaller
212 /// Region A is the \e parent of Region B, if B is completely contained in A.
214 /// Two canonical Regions either do not intersect at all or one is
215 /// the parent of the other.
217 /// The <em> Program Structure Tree</em> is a graph (V, E) where V is the set of
218 /// Regions in the control flow graph and E is the \e parent relation of these
224 /// A simple control flow graph, that contains two regions.
234 /// \ |/ Region A: 1 -> 9 {1,2,3,4,5,6,7,8}
235 /// 9 Region B: 2 -> 9 {2,4,5,6,7}
238 /// You can obtain more examples by either calling
240 /// <tt> "opt -regions -analyze anyprogram.ll" </tt>
242 /// <tt> "opt -view-regions-only anyprogram.ll" </tt>
244 /// on any LLVM file you are interested in.
246 /// The first call returns a textual representation of the program structure
247 /// tree, the second one creates a graphical representation using graphviz.
249 class RegionBase : public RegionNodeBase<Tr> {
250 typedef typename Tr::FuncT FuncT;
251 typedef typename Tr::BlockT BlockT;
252 typedef typename Tr::RegionInfoT RegionInfoT;
253 typedef typename Tr::RegionT RegionT;
254 typedef typename Tr::RegionNodeT RegionNodeT;
255 typedef typename Tr::DomTreeT DomTreeT;
256 typedef typename Tr::LoopT LoopT;
257 typedef typename Tr::LoopInfoT LoopInfoT;
258 typedef typename Tr::InstT InstT;
260 typedef GraphTraits<BlockT *> BlockTraits;
261 typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
262 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
263 typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
265 friend class RegionInfoBase<Tr>;
266 RegionBase(const RegionBase &) = delete;
267 const RegionBase &operator=(const RegionBase &) = delete;
269 // Information necessary to manage this Region.
273 // The exit BasicBlock of this region.
274 // (The entry BasicBlock is part of RegionNode)
277 typedef std::vector<std::unique_ptr<RegionT>> RegionSet;
279 // The subregions of this region.
282 typedef std::map<BlockT *, std::unique_ptr<RegionNodeT>> BBNodeMapT;
284 // Save the BasicBlock RegionNodes that are element of this Region.
285 mutable BBNodeMapT BBNodeMap;
287 /// Check if a BB is in this Region. This check also works
288 /// if the region is incorrectly built. (EXPENSIVE!)
289 void verifyBBInRegion(BlockT *BB) const;
291 /// Walk over all the BBs of the region starting from BB and
292 /// verify that all reachable basic blocks are elements of the region.
294 void verifyWalk(BlockT *BB, std::set<BlockT *> *visitedBB) const;
296 /// Verify if the region and its children are valid regions (EXPENSIVE!)
297 void verifyRegionNest() const;
300 /// @brief Create a new region.
302 /// @param Entry The entry basic block of the region.
303 /// @param Exit The exit basic block of the region.
304 /// @param RI The region info object that is managing this region.
305 /// @param DT The dominator tree of the current function.
306 /// @param Parent The surrounding region or NULL if this is a top level
308 RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT,
309 RegionT *Parent = nullptr);
311 /// Delete the Region and all its subregions.
314 /// @brief Get the entry BasicBlock of the Region.
315 /// @return The entry BasicBlock of the region.
316 BlockT *getEntry() const {
317 return RegionNodeBase<Tr>::getEntry();
320 /// @brief Replace the entry basic block of the region with the new basic
323 /// @param BB The new entry basic block of the region.
324 void replaceEntry(BlockT *BB);
326 /// @brief Replace the exit basic block of the region with the new basic
329 /// @param BB The new exit basic block of the region.
330 void replaceExit(BlockT *BB);
332 /// @brief Recursively replace the entry basic block of the region.
334 /// This function replaces the entry basic block with a new basic block. It
335 /// also updates all child regions that have the same entry basic block as
338 /// @param NewEntry The new entry basic block.
339 void replaceEntryRecursive(BlockT *NewEntry);
341 /// @brief Recursively replace the exit basic block of the region.
343 /// This function replaces the exit basic block with a new basic block. It
344 /// also updates all child regions that have the same exit basic block as
347 /// @param NewExit The new exit basic block.
348 void replaceExitRecursive(BlockT *NewExit);
350 /// @brief Get the exit BasicBlock of the Region.
351 /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
353 BlockT *getExit() const { return exit; }
355 /// @brief Get the parent of the Region.
356 /// @return The parent of the Region or NULL if this is a top level
358 RegionT *getParent() const {
359 return RegionNodeBase<Tr>::getParent();
362 /// @brief Get the RegionNode representing the current Region.
363 /// @return The RegionNode representing the current Region.
364 RegionNodeT *getNode() const {
365 return const_cast<RegionNodeT *>(
366 reinterpret_cast<const RegionNodeT *>(this));
369 /// @brief Get the nesting level of this Region.
371 /// An toplevel Region has depth 0.
373 /// @return The depth of the region.
374 unsigned getDepth() const;
376 /// @brief Check if a Region is the TopLevel region.
378 /// The toplevel region represents the whole function.
379 bool isTopLevelRegion() const { return exit == nullptr; }
381 /// @brief Return a new (non-canonical) region, that is obtained by joining
382 /// this region with its predecessors.
384 /// @return A region also starting at getEntry(), but reaching to the next
385 /// basic block that forms with getEntry() a (non-canonical) region.
386 /// NULL if such a basic block does not exist.
387 RegionT *getExpandedRegion() const;
389 /// @brief Return the first block of this region's single entry edge,
392 /// @return The BasicBlock starting this region's single entry edge,
394 BlockT *getEnteringBlock() const;
396 /// @brief Return the first block of this region's single exit edge,
399 /// @return The BasicBlock starting this region's single exit edge,
401 BlockT *getExitingBlock() const;
403 /// @brief Is this a simple region?
405 /// A region is simple if it has exactly one exit and one entry edge.
407 /// @return True if the Region is simple.
408 bool isSimple() const;
410 /// @brief Returns the name of the Region.
411 /// @return The Name of the Region.
412 std::string getNameStr() const;
414 /// @brief Return the RegionInfo object, that belongs to this Region.
415 RegionInfoT *getRegionInfo() const { return RI; }
417 /// PrintStyle - Print region in difference ways.
418 enum PrintStyle { PrintNone, PrintBB, PrintRN };
420 /// @brief Print the region.
422 /// @param OS The output stream the Region is printed to.
423 /// @param printTree Print also the tree of subregions.
424 /// @param level The indentation level used for printing.
425 void print(raw_ostream &OS, bool printTree = true, unsigned level = 0,
426 PrintStyle Style = PrintNone) const;
428 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
429 /// @brief Print the region to stderr.
433 /// @brief Check if the region contains a BasicBlock.
435 /// @param BB The BasicBlock that might be contained in this Region.
436 /// @return True if the block is contained in the region otherwise false.
437 bool contains(const BlockT *BB) const;
439 /// @brief Check if the region contains another region.
441 /// @param SubRegion The region that might be contained in this Region.
442 /// @return True if SubRegion is contained in the region otherwise false.
443 bool contains(const RegionT *SubRegion) const {
448 return contains(SubRegion->getEntry()) &&
449 (contains(SubRegion->getExit()) ||
450 SubRegion->getExit() == getExit());
453 /// @brief Check if the region contains an Instruction.
455 /// @param Inst The Instruction that might be contained in this region.
456 /// @return True if the Instruction is contained in the region otherwise
458 bool contains(const InstT *Inst) const { return contains(Inst->getParent()); }
460 /// @brief Check if the region contains a loop.
462 /// @param L The loop that might be contained in this region.
463 /// @return True if the loop is contained in the region otherwise false.
464 /// In case a NULL pointer is passed to this function the result
465 /// is false, except for the region that describes the whole function.
466 /// In that case true is returned.
467 bool contains(const LoopT *L) const;
469 /// @brief Get the outermost loop in the region that contains a loop.
471 /// Find for a Loop L the outermost loop OuterL that is a parent loop of L
472 /// and is itself contained in the region.
474 /// @param L The loop the lookup is started.
475 /// @return The outermost loop in the region, NULL if such a loop does not
476 /// exist or if the region describes the whole function.
477 LoopT *outermostLoopInRegion(LoopT *L) const;
479 /// @brief Get the outermost loop in the region that contains a basic block.
481 /// Find for a basic block BB the outermost loop L that contains BB and is
482 /// itself contained in the region.
484 /// @param LI A pointer to a LoopInfo analysis.
485 /// @param BB The basic block surrounded by the loop.
486 /// @return The outermost loop in the region, NULL if such a loop does not
487 /// exist or if the region describes the whole function.
488 LoopT *outermostLoopInRegion(LoopInfoT *LI, BlockT *BB) const;
490 /// @brief Get the subregion that starts at a BasicBlock
492 /// @param BB The BasicBlock the subregion should start.
493 /// @return The Subregion if available, otherwise NULL.
494 RegionT *getSubRegionNode(BlockT *BB) const;
496 /// @brief Get the RegionNode for a BasicBlock
498 /// @param BB The BasicBlock at which the RegionNode should start.
499 /// @return If available, the RegionNode that represents the subregion
500 /// starting at BB. If no subregion starts at BB, the RegionNode
502 RegionNodeT *getNode(BlockT *BB) const;
504 /// @brief Get the BasicBlock RegionNode for a BasicBlock
506 /// @param BB The BasicBlock for which the RegionNode is requested.
507 /// @return The RegionNode representing the BB.
508 RegionNodeT *getBBNode(BlockT *BB) const;
510 /// @brief Add a new subregion to this Region.
512 /// @param SubRegion The new subregion that will be added.
513 /// @param moveChildren Move the children of this region, that are also
514 /// contained in SubRegion into SubRegion.
515 void addSubRegion(RegionT *SubRegion, bool moveChildren = false);
517 /// @brief Remove a subregion from this Region.
519 /// The subregion is not deleted, as it will probably be inserted into another
521 /// @param SubRegion The SubRegion that will be removed.
522 RegionT *removeSubRegion(RegionT *SubRegion);
524 /// @brief Move all direct child nodes of this Region to another Region.
526 /// @param To The Region the child nodes will be transferred to.
527 void transferChildrenTo(RegionT *To);
529 /// @brief Verify if the region is a correct region.
531 /// Check if this is a correctly build Region. This is an expensive check, as
532 /// the complete CFG of the Region will be walked.
533 void verifyRegion() const;
535 /// @brief Clear the cache for BB RegionNodes.
537 /// After calling this function the BasicBlock RegionNodes will be stored at
538 /// different memory locations. RegionNodes obtained before this function is
539 /// called are therefore not comparable to RegionNodes abtained afterwords.
540 void clearNodeCache();
542 /// @name Subregion Iterators
544 /// These iterators iterator over all subregions of this Region.
546 typedef typename RegionSet::iterator iterator;
547 typedef typename RegionSet::const_iterator const_iterator;
549 iterator begin() { return children.begin(); }
550 iterator end() { return children.end(); }
552 const_iterator begin() const { return children.begin(); }
553 const_iterator end() const { return children.end(); }
556 /// @name BasicBlock Iterators
558 /// These iterators iterate over all BasicBlocks that are contained in this
559 /// Region. The iterator also iterates over BasicBlocks that are elements of
560 /// a subregion of this Region. It is therefore called a flat iterator.
562 template <bool IsConst>
563 class block_iterator_wrapper
564 : public df_iterator<
565 typename std::conditional<IsConst, const BlockT, BlockT>::type *> {
567 typename std::conditional<IsConst, const BlockT, BlockT>::type *> super;
570 typedef block_iterator_wrapper<IsConst> Self;
571 typedef typename super::value_type value_type;
573 // Construct the begin iterator.
574 block_iterator_wrapper(value_type Entry, value_type Exit)
575 : super(df_begin(Entry)) {
576 // Mark the exit of the region as visited, so that the children of the
577 // exit and the exit itself, i.e. the block outside the region will never
579 super::Visited.insert(Exit);
582 // Construct the end iterator.
583 block_iterator_wrapper() : super(df_end<value_type>((BlockT *)nullptr)) {}
585 /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
587 // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
588 // This was introduced for backwards compatibility, but should
589 // be removed as soon as all users are fixed.
590 BlockT *operator*() const {
591 return const_cast<BlockT *>(super::operator*());
595 typedef block_iterator_wrapper<false> block_iterator;
596 typedef block_iterator_wrapper<true> const_block_iterator;
598 block_iterator block_begin() { return block_iterator(getEntry(), getExit()); }
600 block_iterator block_end() { return block_iterator(); }
602 const_block_iterator block_begin() const {
603 return const_block_iterator(getEntry(), getExit());
605 const_block_iterator block_end() const { return const_block_iterator(); }
607 typedef iterator_range<block_iterator> block_range;
608 typedef iterator_range<const_block_iterator> const_block_range;
610 /// @brief Returns a range view of the basic blocks in the region.
611 inline block_range blocks() {
612 return block_range(block_begin(), block_end());
615 /// @brief Returns a range view of the basic blocks in the region.
617 /// This is the 'const' version of the range view.
618 inline const_block_range blocks() const {
619 return const_block_range(block_begin(), block_end());
623 /// @name Element Iterators
625 /// These iterators iterate over all BasicBlock and subregion RegionNodes that
626 /// are direct children of this Region. It does not iterate over any
627 /// RegionNodes that are also element of a subregion of this Region.
629 typedef df_iterator<RegionNodeT *, df_iterator_default_set<RegionNodeT *>,
630 false, GraphTraits<RegionNodeT *>>
633 typedef df_iterator<const RegionNodeT *,
634 df_iterator_default_set<const RegionNodeT *>, false,
635 GraphTraits<const RegionNodeT *>>
636 const_element_iterator;
638 element_iterator element_begin();
639 element_iterator element_end();
640 iterator_range<element_iterator> elements() {
641 return make_range(element_begin(), element_end());
644 const_element_iterator element_begin() const;
645 const_element_iterator element_end() const;
646 iterator_range<const_element_iterator> elements() const {
647 return make_range(element_begin(), element_end());
652 /// Print a RegionNode.
654 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNodeBase<Tr> &Node);
656 //===----------------------------------------------------------------------===//
657 /// @brief Analysis that detects all canonical Regions.
659 /// The RegionInfo pass detects all canonical regions in a function. The Regions
660 /// are connected using the parent relation. This builds a Program Structure
663 class RegionInfoBase {
664 typedef typename Tr::BlockT BlockT;
665 typedef typename Tr::FuncT FuncT;
666 typedef typename Tr::RegionT RegionT;
667 typedef typename Tr::RegionInfoT RegionInfoT;
668 typedef typename Tr::DomTreeT DomTreeT;
669 typedef typename Tr::DomTreeNodeT DomTreeNodeT;
670 typedef typename Tr::PostDomTreeT PostDomTreeT;
671 typedef typename Tr::DomFrontierT DomFrontierT;
672 typedef GraphTraits<BlockT *> BlockTraits;
673 typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
674 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
675 typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
677 friend class RegionInfo;
678 friend class MachineRegionInfo;
679 typedef DenseMap<BlockT *, BlockT *> BBtoBBMap;
680 typedef DenseMap<BlockT *, RegionT *> BBtoRegionMap;
683 virtual ~RegionInfoBase();
685 RegionInfoBase(const RegionInfoBase &) = delete;
686 const RegionInfoBase &operator=(const RegionInfoBase &) = delete;
688 RegionInfoBase(RegionInfoBase &&Arg)
689 : DT(std::move(Arg.DT)), PDT(std::move(Arg.PDT)), DF(std::move(Arg.DF)),
690 TopLevelRegion(std::move(Arg.TopLevelRegion)),
691 BBtoRegion(std::move(Arg.BBtoRegion)) {
694 RegionInfoBase &operator=(RegionInfoBase &&RHS) {
695 DT = std::move(RHS.DT);
696 PDT = std::move(RHS.PDT);
697 DF = std::move(RHS.DF);
698 TopLevelRegion = std::move(RHS.TopLevelRegion);
699 BBtoRegion = std::move(RHS.BBtoRegion);
708 /// The top level region.
709 RegionT *TopLevelRegion;
712 /// Map every BB to the smallest region, that contains BB.
713 BBtoRegionMap BBtoRegion;
715 /// \brief Wipe this region tree's state without releasing any resources.
717 /// This is essentially a post-move helper only. It leaves the object in an
718 /// assignable and destroyable state, but otherwise invalid.
723 TopLevelRegion = nullptr;
727 // Check whether the entries of BBtoRegion for the BBs of region
728 // SR are correct. Triggers an assertion if not. Calls itself recursively for
730 void verifyBBMap(const RegionT *SR) const;
732 // Returns true if BB is in the dominance frontier of
733 // entry, because it was inherited from exit. In the other case there is an
734 // edge going from entry to BB without passing exit.
735 bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit) const;
737 // Check if entry and exit surround a valid region, based on
738 // dominance tree and dominance frontier.
739 bool isRegion(BlockT *entry, BlockT *exit) const;
741 // Saves a shortcut pointing from entry to exit.
742 // This function may extend this shortcut if possible.
743 void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut) const;
745 // Returns the next BB that postdominates N, while skipping
746 // all post dominators that cannot finish a canonical region.
747 DomTreeNodeT *getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const;
749 // A region is trivial, if it contains only one BB.
750 bool isTrivialRegion(BlockT *entry, BlockT *exit) const;
752 // Creates a single entry single exit region.
753 RegionT *createRegion(BlockT *entry, BlockT *exit);
755 // Detect all regions starting with bb 'entry'.
756 void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
758 // Detects regions in F.
759 void scanForRegions(FuncT &F, BBtoBBMap *ShortCut);
761 // Get the top most parent with the same entry block.
762 RegionT *getTopMostParent(RegionT *region);
764 // Build the region hierarchy after all region detected.
765 void buildRegionsTree(DomTreeNodeT *N, RegionT *region);
767 // Update statistic about created regions.
768 virtual void updateStatistics(RegionT *R) = 0;
770 // Detect all regions in function and build the region tree.
771 void calculate(FuncT &F);
774 static bool VerifyRegionInfo;
775 static typename RegionT::PrintStyle printStyle;
777 void print(raw_ostream &OS) const;
778 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
782 void releaseMemory();
784 /// @brief Get the smallest region that contains a BasicBlock.
786 /// @param BB The basic block.
787 /// @return The smallest region, that contains BB or NULL, if there is no
788 /// region containing BB.
789 RegionT *getRegionFor(BlockT *BB) const;
791 /// @brief Set the smallest region that surrounds a basic block.
793 /// @param BB The basic block surrounded by a region.
794 /// @param R The smallest region that surrounds BB.
795 void setRegionFor(BlockT *BB, RegionT *R);
797 /// @brief A shortcut for getRegionFor().
799 /// @param BB The basic block.
800 /// @return The smallest region, that contains BB or NULL, if there is no
801 /// region containing BB.
802 RegionT *operator[](BlockT *BB) const;
804 /// @brief Return the exit of the maximal refined region, that starts at a
807 /// @param BB The BasicBlock the refined region starts.
808 BlockT *getMaxRegionExit(BlockT *BB) const;
810 /// @brief Find the smallest region that contains two regions.
812 /// @param A The first region.
813 /// @param B The second region.
814 /// @return The smallest region containing A and B.
815 RegionT *getCommonRegion(RegionT *A, RegionT *B) const;
817 /// @brief Find the smallest region that contains two basic blocks.
819 /// @param A The first basic block.
820 /// @param B The second basic block.
821 /// @return The smallest region that contains A and B.
822 RegionT *getCommonRegion(BlockT *A, BlockT *B) const {
823 return getCommonRegion(getRegionFor(A), getRegionFor(B));
826 /// @brief Find the smallest region that contains a set of regions.
828 /// @param Regions A vector of regions.
829 /// @return The smallest region that contains all regions in Regions.
830 RegionT *getCommonRegion(SmallVectorImpl<RegionT *> &Regions) const;
832 /// @brief Find the smallest region that contains a set of basic blocks.
834 /// @param BBs A vector of basic blocks.
835 /// @return The smallest region that contains all basic blocks in BBS.
836 RegionT *getCommonRegion(SmallVectorImpl<BlockT *> &BBs) const;
838 RegionT *getTopLevelRegion() const { return TopLevelRegion; }
840 /// @brief Clear the Node Cache for all Regions.
842 /// @see Region::clearNodeCache()
843 void clearNodeCache() {
845 TopLevelRegion->clearNodeCache();
848 void verifyAnalysis() const;
853 class RegionNode : public RegionNodeBase<RegionTraits<Function>> {
855 inline RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion = false)
856 : RegionNodeBase<RegionTraits<Function>>(Parent, Entry, isSubRegion) {}
858 bool operator==(const Region &RN) const {
859 return this == reinterpret_cast<const RegionNode *>(&RN);
863 class Region : public RegionBase<RegionTraits<Function>> {
865 Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT,
866 Region *Parent = nullptr);
869 bool operator==(const RegionNode &RN) const {
870 return &RN == reinterpret_cast<const RegionNode *>(this);
874 class RegionInfo : public RegionInfoBase<RegionTraits<Function>> {
876 typedef RegionInfoBase<RegionTraits<Function>> Base;
878 explicit RegionInfo();
880 ~RegionInfo() override;
882 RegionInfo(RegionInfo &&Arg)
883 : Base(std::move(static_cast<Base &>(Arg))) {}
884 RegionInfo &operator=(RegionInfo &&RHS) {
885 Base::operator=(std::move(static_cast<Base &>(RHS)));
889 /// Handle invalidation explicitly.
890 bool invalidate(Function &F, const PreservedAnalyses &PA,
891 FunctionAnalysisManager::Invalidator &);
893 // updateStatistics - Update statistic about created regions.
894 void updateStatistics(Region *R) final;
896 void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT,
897 DominanceFrontier *DF);
900 /// @brief Opens a viewer to show the GraphViz visualization of the regions.
902 /// Useful during debugging as an alternative to dump().
905 /// @brief Opens a viewer to show the GraphViz visualization of this region
906 /// without instructions in the BasicBlocks.
908 /// Useful during debugging as an alternative to dump().
913 class RegionInfoPass : public FunctionPass {
918 explicit RegionInfoPass();
920 ~RegionInfoPass() override;
922 RegionInfo &getRegionInfo() { return RI; }
924 const RegionInfo &getRegionInfo() const { return RI; }
926 /// @name FunctionPass interface
928 bool runOnFunction(Function &F) override;
929 void releaseMemory() override;
930 void verifyAnalysis() const override;
931 void getAnalysisUsage(AnalysisUsage &AU) const override;
932 void print(raw_ostream &OS, const Module *) const override;
937 /// \brief Analysis pass that exposes the \c RegionInfo for a function.
938 class RegionInfoAnalysis : public AnalysisInfoMixin<RegionInfoAnalysis> {
939 friend AnalysisInfoMixin<RegionInfoAnalysis>;
940 static AnalysisKey Key;
943 typedef RegionInfo Result;
945 RegionInfo run(Function &F, FunctionAnalysisManager &AM);
948 /// \brief Printer pass for the \c RegionInfo.
949 class RegionInfoPrinterPass : public PassInfoMixin<RegionInfoPrinterPass> {
953 explicit RegionInfoPrinterPass(raw_ostream &OS);
954 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
957 /// \brief Verifier pass for the \c RegionInfo.
958 struct RegionInfoVerifierPass : PassInfoMixin<RegionInfoVerifierPass> {
959 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
965 RegionNodeBase<RegionTraits<Function>>::getNodeAs<BasicBlock>() const {
966 assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
973 RegionNodeBase<RegionTraits<Function>>::getNodeAs<Region>() const {
974 assert(isSubRegion() && "This is not a subregion RegionNode!");
975 auto Unconst = const_cast<RegionNodeBase<RegionTraits<Function>> *>(this);
976 return reinterpret_cast<Region *>(Unconst);
980 inline raw_ostream &operator<<(raw_ostream &OS,
981 const RegionNodeBase<Tr> &Node) {
982 typedef typename Tr::BlockT BlockT;
983 typedef typename Tr::RegionT RegionT;
985 if (Node.isSubRegion())
986 return OS << Node.template getNodeAs<RegionT>()->getNameStr();
988 return OS << Node.template getNodeAs<BlockT>()->getName();
991 extern template class RegionBase<RegionTraits<Function>>;
992 extern template class RegionNodeBase<RegionTraits<Function>>;
993 extern template class RegionInfoBase<RegionTraits<Function>>;
995 } // End llvm namespace