xref: /llvm-project/llvm/include/llvm/Analysis/RegionInfo.h (revision 0da2ba811ac8a01509bc533428941fb9519c0715)
1 //===- RegionInfo.h - SESE region 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 //
9 // Calculate a program structure tree built out of single entry single exit
10 // regions.
11 // The basic ideas are taken from "The Program Structure Tree - Richard Johnson,
12 // David Pearson, Keshav Pingali - 1994", however enriched with ideas from "The
13 // Refined Process Structure Tree - Jussi Vanhatalo, Hagen Voelyer, Jana
14 // Koehler - 2009".
15 // The algorithm to calculate these data structures however is completely
16 // different, as it takes advantage of existing information already available
17 // in (Post)dominace tree and dominance frontier passes. This leads to a simpler
18 // and in practice hopefully better performing algorithm. The runtime of the
19 // algorithms described in the papers above are both linear in graph size,
20 // O(V+E), whereas this algorithm is not, as the dominance frontier information
21 // itself is not, but in practice runtime seems to be in the order of magnitude
22 // of dominance tree calculation.
23 //
24 // WARNING: LLVM is generally very concerned about compile time such that
25 //          the use of additional analysis passes in the default
26 //          optimization sequence is avoided as much as possible.
27 //          Specifically, if you do not need the RegionInfo, but dominance
28 //          information could be sufficient please base your work only on
29 //          the dominator tree. Most passes maintain it, such that using
30 //          it has often near zero cost. In contrast RegionInfo is by
31 //          default not available, is not maintained by existing
32 //          transformations and there is no intention to do so.
33 //
34 //===----------------------------------------------------------------------===//
35 
36 #ifndef LLVM_ANALYSIS_REGIONINFO_H
37 #define LLVM_ANALYSIS_REGIONINFO_H
38 
39 #include "llvm/ADT/DenseMap.h"
40 #include "llvm/ADT/DepthFirstIterator.h"
41 #include "llvm/ADT/GraphTraits.h"
42 #include "llvm/ADT/PointerIntPair.h"
43 #include "llvm/ADT/iterator_range.h"
44 #include "llvm/IR/Dominators.h"
45 #include "llvm/IR/PassManager.h"
46 #include "llvm/Pass.h"
47 #include <algorithm>
48 #include <cassert>
49 #include <map>
50 #include <memory>
51 #include <set>
52 #include <string>
53 #include <type_traits>
54 #include <vector>
55 
56 namespace llvm {
57 
58 class DominanceFrontier;
59 class Loop;
60 class LoopInfo;
61 class PostDominatorTree;
62 class Region;
63 template <class RegionTr> class RegionBase;
64 class RegionInfo;
65 template <class RegionTr> class RegionInfoBase;
66 class RegionNode;
67 class raw_ostream;
68 
69 // Class to be specialized for different users of RegionInfo
70 // (i.e. BasicBlocks or MachineBasicBlocks). This is only to avoid needing to
71 // pass around an unreasonable number of template parameters.
72 template <class FuncT_>
73 struct RegionTraits {
74   // FuncT
75   // BlockT
76   // RegionT
77   // RegionNodeT
78   // RegionInfoT
79   using BrokenT = typename FuncT_::UnknownRegionTypeError;
80 };
81 
82 template <>
83 struct RegionTraits<Function> {
84   using FuncT = Function;
85   using BlockT = BasicBlock;
86   using RegionT = Region;
87   using RegionNodeT = RegionNode;
88   using RegionInfoT = RegionInfo;
89   using DomTreeT = DominatorTree;
90   using DomTreeNodeT = DomTreeNode;
91   using DomFrontierT = DominanceFrontier;
92   using PostDomTreeT = PostDominatorTree;
93   using InstT = Instruction;
94   using LoopT = Loop;
95   using LoopInfoT = LoopInfo;
96 
97   static unsigned getNumSuccessors(BasicBlock *BB) {
98     return BB->getTerminator()->getNumSuccessors();
99   }
100 };
101 
102 /// Marker class to iterate over the elements of a Region in flat mode.
103 ///
104 /// The class is used to either iterate in Flat mode or by not using it to not
105 /// iterate in Flat mode.  During a Flat mode iteration all Regions are entered
106 /// and the iteration returns every BasicBlock.  If the Flat mode is not
107 /// selected for SubRegions just one RegionNode containing the subregion is
108 /// returned.
109 template <class GraphType>
110 class FlatIt {};
111 
112 /// A RegionNode represents a subregion or a BasicBlock that is part of a
113 /// Region.
114 template <class Tr>
115 class RegionNodeBase {
116   friend class RegionBase<Tr>;
117 
118 public:
119   using BlockT = typename Tr::BlockT;
120   using RegionT = typename Tr::RegionT;
121 
122 private:
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.
126   ///
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.
129   ///
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
132   /// RegionNode.
133   PointerIntPair<BlockT *, 1, bool> entry;
134 
135   /// The parent Region of this RegionNode.
136   /// @see getParent()
137   RegionT *parent;
138 
139 protected:
140   /// Create a RegionNode.
141   ///
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) {}
151 
152 public:
153   RegionNodeBase(const RegionNodeBase &) = delete;
154   RegionNodeBase &operator=(const RegionNodeBase &) = delete;
155 
156   /// Get the parent Region of this RegionNode.
157   ///
158   /// The parent Region is the Region this RegionNode belongs to. If for
159   /// example a BasicBlock is element of two Regions, there exist two
160   /// RegionNodes for this BasicBlock. Each with the getParent() function
161   /// pointing to the Region this RegionNode belongs to.
162   ///
163   /// @return Get the parent Region of this RegionNode.
164   inline RegionT *getParent() const { return parent; }
165 
166   /// Get the entry BasicBlock of this RegionNode.
167   ///
168   /// If this RegionNode represents a BasicBlock this is just the BasicBlock
169   /// itself, otherwise we return the entry BasicBlock of the Subregion
170   ///
171   /// @return The entry BasicBlock of this RegionNode.
172   inline BlockT *getEntry() const { return entry.getPointer(); }
173 
174   /// Get the content of this RegionNode.
175   ///
176   /// This can be either a BasicBlock or a subregion. Before calling getNodeAs()
177   /// check the type of the content with the isSubRegion() function call.
178   ///
179   /// @return The content of this RegionNode.
180   template <class T> inline T *getNodeAs() const;
181 
182   /// Is this RegionNode a subregion?
183   ///
184   /// @return True if it contains a subregion. False if it contains a
185   ///         BasicBlock.
186   inline bool isSubRegion() const { return entry.getInt(); }
187 };
188 
189 //===----------------------------------------------------------------------===//
190 /// A single entry single exit Region.
191 ///
192 /// A Region is a connected subgraph of a control flow graph that has exactly
193 /// two connections to the remaining graph. It can be used to analyze or
194 /// optimize parts of the control flow graph.
195 ///
196 /// A <em> simple Region </em> is connected to the remaining graph by just two
197 /// edges. One edge entering the Region and another one leaving the Region.
198 ///
199 /// An <em> extended Region </em> (or just Region) is a subgraph that can be
200 /// transform into a simple Region. The transformation is done by adding
201 /// BasicBlocks that merge several entry or exit edges so that after the merge
202 /// just one entry and one exit edge exists.
203 ///
204 /// The \e Entry of a Region is the first BasicBlock that is passed after
205 /// entering the Region. It is an element of the Region. The entry BasicBlock
206 /// dominates all BasicBlocks in the Region.
207 ///
208 /// The \e Exit of a Region is the first BasicBlock that is passed after
209 /// leaving the Region. It is not an element of the Region. The exit BasicBlock,
210 /// postdominates all BasicBlocks in the Region.
211 ///
212 /// A <em> canonical Region </em> cannot be constructed by combining smaller
213 /// Regions.
214 ///
215 /// Region A is the \e parent of Region B, if B is completely contained in A.
216 ///
217 /// Two canonical Regions either do not intersect at all or one is
218 /// the parent of the other.
219 ///
220 /// The <em> Program Structure Tree</em> is a graph (V, E) where V is the set of
221 /// Regions in the control flow graph and E is the \e parent relation of these
222 /// Regions.
223 ///
224 /// Example:
225 ///
226 /// \verbatim
227 /// A simple control flow graph, that contains two regions.
228 ///
229 ///        1
230 ///       / |
231 ///      2   |
232 ///     / \   3
233 ///    4   5  |
234 ///    |   |  |
235 ///    6   7  8
236 ///     \  | /
237 ///      \ |/       Region A: 1 -> 9 {1,2,3,4,5,6,7,8}
238 ///        9        Region B: 2 -> 9 {2,4,5,6,7}
239 /// \endverbatim
240 ///
241 /// You can obtain more examples by either calling
242 ///
243 /// <tt> "opt -passes='print<regions>' anyprogram.ll" </tt>
244 /// or
245 /// <tt> "opt -view-regions-only anyprogram.ll" </tt>
246 ///
247 /// on any LLVM file you are interested in.
248 ///
249 /// The first call returns a textual representation of the program structure
250 /// tree, the second one creates a graphical representation using graphviz.
251 template <class Tr>
252 class RegionBase : public RegionNodeBase<Tr> {
253   friend class RegionInfoBase<Tr>;
254 
255   using FuncT = typename Tr::FuncT;
256   using BlockT = typename Tr::BlockT;
257   using RegionInfoT = typename Tr::RegionInfoT;
258   using RegionT = typename Tr::RegionT;
259   using RegionNodeT = typename Tr::RegionNodeT;
260   using DomTreeT = typename Tr::DomTreeT;
261   using LoopT = typename Tr::LoopT;
262   using LoopInfoT = typename Tr::LoopInfoT;
263   using InstT = typename Tr::InstT;
264 
265   using BlockTraits = GraphTraits<BlockT *>;
266   using InvBlockTraits = GraphTraits<Inverse<BlockT *>>;
267   using SuccIterTy = typename BlockTraits::ChildIteratorType;
268   using PredIterTy = typename InvBlockTraits::ChildIteratorType;
269 
270   // Information necessary to manage this Region.
271   RegionInfoT *RI;
272   DomTreeT *DT;
273 
274   // The exit BasicBlock of this region.
275   // (The entry BasicBlock is part of RegionNode)
276   BlockT *exit;
277 
278   using RegionSet = std::vector<std::unique_ptr<RegionT>>;
279 
280   // The subregions of this region.
281   RegionSet children;
282 
283   using BBNodeMapT = std::map<BlockT *, std::unique_ptr<RegionNodeT>>;
284 
285   // Save the BasicBlock RegionNodes that are element of this Region.
286   mutable BBNodeMapT BBNodeMap;
287 
288   /// Check if a BB is in this Region. This check also works
289   /// if the region is incorrectly built. (EXPENSIVE!)
290   void verifyBBInRegion(BlockT *BB) const;
291 
292   /// Walk over all the BBs of the region starting from BB and
293   /// verify that all reachable basic blocks are elements of the region.
294   /// (EXPENSIVE!)
295   void verifyWalk(BlockT *BB, std::set<BlockT *> *visitedBB) const;
296 
297   /// Verify if the region and its children are valid regions (EXPENSIVE!)
298   void verifyRegionNest() const;
299 
300 public:
301   /// Create a new region.
302   ///
303   /// @param Entry  The entry basic block of the region.
304   /// @param Exit   The exit basic block of the region.
305   /// @param RI     The region info object that is managing this region.
306   /// @param DT     The dominator tree of the current function.
307   /// @param Parent The surrounding region or NULL if this is a top level
308   ///               region.
309   RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT,
310              RegionT *Parent = nullptr);
311 
312   RegionBase(const RegionBase &) = delete;
313   RegionBase &operator=(const RegionBase &) = delete;
314 
315   /// Delete the Region and all its subregions.
316   ~RegionBase();
317 
318   /// Get the entry BasicBlock of the Region.
319   /// @return The entry BasicBlock of the region.
320   BlockT *getEntry() const {
321     return RegionNodeBase<Tr>::getEntry();
322   }
323 
324   /// Replace the entry basic block of the region with the new basic
325   ///        block.
326   ///
327   /// @param BB  The new entry basic block of the region.
328   void replaceEntry(BlockT *BB);
329 
330   /// Replace the exit basic block of the region with the new basic
331   ///        block.
332   ///
333   /// @param BB  The new exit basic block of the region.
334   void replaceExit(BlockT *BB);
335 
336   /// Recursively replace the entry basic block of the region.
337   ///
338   /// This function replaces the entry basic block with a new basic block. It
339   /// also updates all child regions that have the same entry basic block as
340   /// this region.
341   ///
342   /// @param NewEntry The new entry basic block.
343   void replaceEntryRecursive(BlockT *NewEntry);
344 
345   /// Recursively replace the exit basic block of the region.
346   ///
347   /// This function replaces the exit basic block with a new basic block. It
348   /// also updates all child regions that have the same exit basic block as
349   /// this region.
350   ///
351   /// @param NewExit The new exit basic block.
352   void replaceExitRecursive(BlockT *NewExit);
353 
354   /// Get the exit BasicBlock of the Region.
355   /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
356   ///         Region.
357   BlockT *getExit() const { return exit; }
358 
359   /// Get the parent of the Region.
360   /// @return The parent of the Region or NULL if this is a top level
361   ///         Region.
362   RegionT *getParent() const {
363     return RegionNodeBase<Tr>::getParent();
364   }
365 
366   /// Get the RegionNode representing the current Region.
367   /// @return The RegionNode representing the current Region.
368   RegionNodeT *getNode() const {
369     return const_cast<RegionNodeT *>(
370         reinterpret_cast<const RegionNodeT *>(this));
371   }
372 
373   /// Get the nesting level of this Region.
374   ///
375   /// An toplevel Region has depth 0.
376   ///
377   /// @return The depth of the region.
378   unsigned getDepth() const;
379 
380   /// Check if a Region is the TopLevel region.
381   ///
382   /// The toplevel region represents the whole function.
383   bool isTopLevelRegion() const { return exit == nullptr; }
384 
385   /// Return a new (non-canonical) region, that is obtained by joining
386   ///        this region with its predecessors.
387   ///
388   /// @return A region also starting at getEntry(), but reaching to the next
389   ///         basic block that forms with getEntry() a (non-canonical) region.
390   ///         NULL if such a basic block does not exist.
391   RegionT *getExpandedRegion() const;
392 
393   /// Return the first block of this region's single entry edge,
394   ///        if existing.
395   ///
396   /// @return The BasicBlock starting this region's single entry edge,
397   ///         else NULL.
398   BlockT *getEnteringBlock() const;
399 
400   /// Return the first block of this region's single exit edge,
401   ///        if existing.
402   ///
403   /// @return The BasicBlock starting this region's single exit edge,
404   ///         else NULL.
405   BlockT *getExitingBlock() const;
406 
407   /// Collect all blocks of this region's single exit edge, if existing.
408   ///
409   /// @return True if this region contains all the predecessors of the exit.
410   bool getExitingBlocks(SmallVectorImpl<BlockT *> &Exitings) const;
411 
412   /// Is this a simple region?
413   ///
414   /// A region is simple if it has exactly one exit and one entry edge.
415   ///
416   /// @return True if the Region is simple.
417   bool isSimple() const;
418 
419   /// Returns the name of the Region.
420   /// @return The Name of the Region.
421   std::string getNameStr() const;
422 
423   /// Return the RegionInfo object, that belongs to this Region.
424   RegionInfoT *getRegionInfo() const { return RI; }
425 
426   /// PrintStyle - Print region in difference ways.
427   enum PrintStyle { PrintNone, PrintBB, PrintRN };
428 
429   /// Print the region.
430   ///
431   /// @param OS The output stream the Region is printed to.
432   /// @param printTree Print also the tree of subregions.
433   /// @param level The indentation level used for printing.
434   void print(raw_ostream &OS, bool printTree = true, unsigned level = 0,
435              PrintStyle Style = PrintNone) const;
436 
437 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
438   /// Print the region to stderr.
439   void dump() const;
440 #endif
441 
442   /// Check if the region contains a BasicBlock.
443   ///
444   /// @param BB The BasicBlock that might be contained in this Region.
445   /// @return True if the block is contained in the region otherwise false.
446   bool contains(const BlockT *BB) const;
447 
448   /// Check if the region contains another region.
449   ///
450   /// @param SubRegion The region that might be contained in this Region.
451   /// @return True if SubRegion is contained in the region otherwise false.
452   bool contains(const RegionT *SubRegion) const {
453     // Toplevel Region.
454     if (!getExit())
455       return true;
456 
457     return contains(SubRegion->getEntry()) &&
458            (contains(SubRegion->getExit()) ||
459             SubRegion->getExit() == getExit());
460   }
461 
462   /// Check if the region contains an Instruction.
463   ///
464   /// @param Inst The Instruction that might be contained in this region.
465   /// @return True if the Instruction is contained in the region otherwise
466   /// false.
467   bool contains(const InstT *Inst) const { return contains(Inst->getParent()); }
468 
469   /// Check if the region contains a loop.
470   ///
471   /// @param L The loop that might be contained in this region.
472   /// @return True if the loop is contained in the region otherwise false.
473   ///         In case a NULL pointer is passed to this function the result
474   ///         is false, except for the region that describes the whole function.
475   ///         In that case true is returned.
476   bool contains(const LoopT *L) const;
477 
478   /// Get the outermost loop in the region that contains a loop.
479   ///
480   /// Find for a Loop L the outermost loop OuterL that is a parent loop of L
481   /// and is itself contained in the region.
482   ///
483   /// @param L The loop the lookup is started.
484   /// @return The outermost loop in the region, NULL if such a loop does not
485   ///         exist or if the region describes the whole function.
486   LoopT *outermostLoopInRegion(LoopT *L) const;
487 
488   /// Get the outermost loop in the region that contains a basic block.
489   ///
490   /// Find for a basic block BB the outermost loop L that contains BB and is
491   /// itself contained in the region.
492   ///
493   /// @param LI A pointer to a LoopInfo analysis.
494   /// @param BB The basic block surrounded by the loop.
495   /// @return The outermost loop in the region, NULL if such a loop does not
496   ///         exist or if the region describes the whole function.
497   LoopT *outermostLoopInRegion(LoopInfoT *LI, BlockT *BB) const;
498 
499   /// Get the subregion that starts at a BasicBlock
500   ///
501   /// @param BB The BasicBlock the subregion should start.
502   /// @return The Subregion if available, otherwise NULL.
503   RegionT *getSubRegionNode(BlockT *BB) const;
504 
505   /// Get the RegionNode for a BasicBlock
506   ///
507   /// @param BB The BasicBlock at which the RegionNode should start.
508   /// @return If available, the RegionNode that represents the subregion
509   ///         starting at BB. If no subregion starts at BB, the RegionNode
510   ///         representing BB.
511   RegionNodeT *getNode(BlockT *BB) const;
512 
513   /// Get the BasicBlock RegionNode for a BasicBlock
514   ///
515   /// @param BB The BasicBlock for which the RegionNode is requested.
516   /// @return The RegionNode representing the BB.
517   RegionNodeT *getBBNode(BlockT *BB) const;
518 
519   /// Add a new subregion to this Region.
520   ///
521   /// @param SubRegion The new subregion that will be added.
522   /// @param moveChildren Move the children of this region, that are also
523   ///                     contained in SubRegion into SubRegion.
524   void addSubRegion(RegionT *SubRegion, bool moveChildren = false);
525 
526   /// Remove a subregion from this Region.
527   ///
528   /// The subregion is not deleted, as it will probably be inserted into another
529   /// region.
530   /// @param SubRegion The SubRegion that will be removed.
531   RegionT *removeSubRegion(RegionT *SubRegion);
532 
533   /// Move all direct child nodes of this Region to another Region.
534   ///
535   /// @param To The Region the child nodes will be transferred to.
536   void transferChildrenTo(RegionT *To);
537 
538   /// Verify if the region is a correct region.
539   ///
540   /// Check if this is a correctly build Region. This is an expensive check, as
541   /// the complete CFG of the Region will be walked.
542   void verifyRegion() const;
543 
544   /// Clear the cache for BB RegionNodes.
545   ///
546   /// After calling this function the BasicBlock RegionNodes will be stored at
547   /// different memory locations. RegionNodes obtained before this function is
548   /// called are therefore not comparable to RegionNodes abtained afterwords.
549   void clearNodeCache();
550 
551   /// @name Subregion Iterators
552   ///
553   /// These iterators iterator over all subregions of this Region.
554   //@{
555   using iterator = typename RegionSet::iterator;
556   using const_iterator = typename RegionSet::const_iterator;
557 
558   iterator begin() { return children.begin(); }
559   iterator end() { return children.end(); }
560 
561   const_iterator begin() const { return children.begin(); }
562   const_iterator end() const { return children.end(); }
563   //@}
564 
565   /// @name BasicBlock Iterators
566   ///
567   /// These iterators iterate over all BasicBlocks that are contained in this
568   /// Region. The iterator also iterates over BasicBlocks that are elements of
569   /// a subregion of this Region. It is therefore called a flat iterator.
570   //@{
571   template <bool IsConst>
572   class block_iterator_wrapper
573       : public df_iterator<
574             std::conditional_t<IsConst, const BlockT, BlockT> *> {
575     using super =
576         df_iterator<std::conditional_t<IsConst, const BlockT, BlockT> *>;
577 
578   public:
579     using Self = block_iterator_wrapper<IsConst>;
580     using value_type = typename super::value_type;
581 
582     // Construct the begin iterator.
583     block_iterator_wrapper(value_type Entry, value_type Exit)
584         : super(df_begin(Entry)) {
585       // Mark the exit of the region as visited, so that the children of the
586       // exit and the exit itself, i.e. the block outside the region will never
587       // be visited.
588       super::Visited.insert(Exit);
589     }
590 
591     // Construct the end iterator.
592     block_iterator_wrapper() : super(df_end<value_type>((BlockT *)nullptr)) {}
593 
594     /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
595 
596     // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
597     //        This was introduced for backwards compatibility, but should
598     //        be removed as soon as all users are fixed.
599     BlockT *operator*() const {
600       return const_cast<BlockT *>(super::operator*());
601     }
602   };
603 
604   using block_iterator = block_iterator_wrapper<false>;
605   using const_block_iterator = block_iterator_wrapper<true>;
606 
607   block_iterator block_begin() { return block_iterator(getEntry(), getExit()); }
608 
609   block_iterator block_end() { return block_iterator(); }
610 
611   const_block_iterator block_begin() const {
612     return const_block_iterator(getEntry(), getExit());
613   }
614   const_block_iterator block_end() const { return const_block_iterator(); }
615 
616   using block_range = iterator_range<block_iterator>;
617   using const_block_range = iterator_range<const_block_iterator>;
618 
619   /// Returns a range view of the basic blocks in the region.
620   inline block_range blocks() {
621     return block_range(block_begin(), block_end());
622   }
623 
624   /// Returns a range view of the basic blocks in the region.
625   ///
626   /// This is the 'const' version of the range view.
627   inline const_block_range blocks() const {
628     return const_block_range(block_begin(), block_end());
629   }
630   //@}
631 
632   /// @name Element Iterators
633   ///
634   /// These iterators iterate over all BasicBlock and subregion RegionNodes that
635   /// are direct children of this Region. It does not iterate over any
636   /// RegionNodes that are also element of a subregion of this Region.
637   //@{
638   using element_iterator =
639       df_iterator<RegionNodeT *, df_iterator_default_set<RegionNodeT *>, false,
640                   GraphTraits<RegionNodeT *>>;
641 
642   using const_element_iterator =
643       df_iterator<const RegionNodeT *,
644                   df_iterator_default_set<const RegionNodeT *>, false,
645                   GraphTraits<const RegionNodeT *>>;
646 
647   element_iterator element_begin();
648   element_iterator element_end();
649   iterator_range<element_iterator> elements() {
650     return make_range(element_begin(), element_end());
651   }
652 
653   const_element_iterator element_begin() const;
654   const_element_iterator element_end() const;
655   iterator_range<const_element_iterator> elements() const {
656     return make_range(element_begin(), element_end());
657   }
658   //@}
659 };
660 
661 /// Print a RegionNode.
662 template <class Tr>
663 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNodeBase<Tr> &Node);
664 
665 //===----------------------------------------------------------------------===//
666 /// Analysis that detects all canonical Regions.
667 ///
668 /// The RegionInfo pass detects all canonical regions in a function. The Regions
669 /// are connected using the parent relation. This builds a Program Structure
670 /// Tree.
671 template <class Tr>
672 class RegionInfoBase {
673   friend class RegionInfo;
674   friend class MachineRegionInfo;
675 
676   using BlockT = typename Tr::BlockT;
677   using FuncT = typename Tr::FuncT;
678   using RegionT = typename Tr::RegionT;
679   using RegionInfoT = typename Tr::RegionInfoT;
680   using DomTreeT = typename Tr::DomTreeT;
681   using DomTreeNodeT = typename Tr::DomTreeNodeT;
682   using PostDomTreeT = typename Tr::PostDomTreeT;
683   using DomFrontierT = typename Tr::DomFrontierT;
684   using BlockTraits = GraphTraits<BlockT *>;
685   using InvBlockTraits = GraphTraits<Inverse<BlockT *>>;
686   using SuccIterTy = typename BlockTraits::ChildIteratorType;
687   using PredIterTy = typename InvBlockTraits::ChildIteratorType;
688 
689   using BBtoBBMap = DenseMap<BlockT *, BlockT *>;
690   using BBtoRegionMap = DenseMap<BlockT *, RegionT *>;
691 
692   RegionInfoBase();
693 
694   RegionInfoBase(RegionInfoBase &&Arg)
695     : DT(std::move(Arg.DT)), PDT(std::move(Arg.PDT)), DF(std::move(Arg.DF)),
696       TopLevelRegion(std::move(Arg.TopLevelRegion)),
697       BBtoRegion(std::move(Arg.BBtoRegion)) {
698     Arg.wipe();
699   }
700 
701   RegionInfoBase &operator=(RegionInfoBase &&RHS) {
702     DT = std::move(RHS.DT);
703     PDT = std::move(RHS.PDT);
704     DF = std::move(RHS.DF);
705     TopLevelRegion = std::move(RHS.TopLevelRegion);
706     BBtoRegion = std::move(RHS.BBtoRegion);
707     RHS.wipe();
708     return *this;
709   }
710 
711   virtual ~RegionInfoBase();
712 
713   DomTreeT *DT;
714   PostDomTreeT *PDT;
715   DomFrontierT *DF;
716 
717   /// The top level region.
718   RegionT *TopLevelRegion = nullptr;
719 
720   /// Map every BB to the smallest region, that contains BB.
721   BBtoRegionMap BBtoRegion;
722 
723 protected:
724   /// Update refences to a RegionInfoT held by the RegionT managed here
725   ///
726   /// This is a post-move helper. Regions hold references to the owning
727   /// RegionInfo object. After a move these need to be fixed.
728   template<typename TheRegionT>
729   void updateRegionTree(RegionInfoT &RI, TheRegionT *R) {
730     if (!R)
731       return;
732     R->RI = &RI;
733     for (auto &SubR : *R)
734       updateRegionTree(RI, SubR.get());
735   }
736 
737 private:
738   /// Wipe this region tree's state without releasing any resources.
739   ///
740   /// This is essentially a post-move helper only. It leaves the object in an
741   /// assignable and destroyable state, but otherwise invalid.
742   void wipe() {
743     DT = nullptr;
744     PDT = nullptr;
745     DF = nullptr;
746     TopLevelRegion = nullptr;
747     BBtoRegion.clear();
748   }
749 
750   // Check whether the entries of BBtoRegion for the BBs of region
751   // SR are correct. Triggers an assertion if not. Calls itself recursively for
752   // subregions.
753   void verifyBBMap(const RegionT *SR) const;
754 
755   // Returns true if BB is in the dominance frontier of
756   // entry, because it was inherited from exit. In the other case there is an
757   // edge going from entry to BB without passing exit.
758   bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit) const;
759 
760   // Check if entry and exit surround a valid region, based on
761   // dominance tree and dominance frontier.
762   bool isRegion(BlockT *entry, BlockT *exit) const;
763 
764   // Saves a shortcut pointing from entry to exit.
765   // This function may extend this shortcut if possible.
766   void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut) const;
767 
768   // Returns the next BB that postdominates N, while skipping
769   // all post dominators that cannot finish a canonical region.
770   DomTreeNodeT *getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const;
771 
772   // A region is trivial, if it contains only one BB.
773   bool isTrivialRegion(BlockT *entry, BlockT *exit) const;
774 
775   // Creates a single entry single exit region.
776   RegionT *createRegion(BlockT *entry, BlockT *exit);
777 
778   // Detect all regions starting with bb 'entry'.
779   void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
780 
781   // Detects regions in F.
782   void scanForRegions(FuncT &F, BBtoBBMap *ShortCut);
783 
784   // Get the top most parent with the same entry block.
785   RegionT *getTopMostParent(RegionT *region);
786 
787   // Build the region hierarchy after all region detected.
788   void buildRegionsTree(DomTreeNodeT *N, RegionT *region);
789 
790   // Update statistic about created regions.
791   virtual void updateStatistics(RegionT *R) = 0;
792 
793   // Detect all regions in function and build the region tree.
794   void calculate(FuncT &F);
795 
796 public:
797   RegionInfoBase(const RegionInfoBase &) = delete;
798   RegionInfoBase &operator=(const RegionInfoBase &) = delete;
799 
800   static bool VerifyRegionInfo;
801   static typename RegionT::PrintStyle printStyle;
802 
803   void print(raw_ostream &OS) const;
804 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
805   void dump() const;
806 #endif
807 
808   void releaseMemory();
809 
810   /// Get the smallest region that contains a BasicBlock.
811   ///
812   /// @param BB The basic block.
813   /// @return The smallest region, that contains BB or NULL, if there is no
814   /// region containing BB.
815   RegionT *getRegionFor(BlockT *BB) const;
816 
817   ///  Set the smallest region that surrounds a basic block.
818   ///
819   /// @param BB The basic block surrounded by a region.
820   /// @param R The smallest region that surrounds BB.
821   void setRegionFor(BlockT *BB, RegionT *R);
822 
823   /// A shortcut for getRegionFor().
824   ///
825   /// @param BB The basic block.
826   /// @return The smallest region, that contains BB or NULL, if there is no
827   /// region containing BB.
828   RegionT *operator[](BlockT *BB) const;
829 
830   /// Return the exit of the maximal refined region, that starts at a
831   /// BasicBlock.
832   ///
833   /// @param BB The BasicBlock the refined region starts.
834   BlockT *getMaxRegionExit(BlockT *BB) const;
835 
836   /// Find the smallest region that contains two regions.
837   ///
838   /// @param A The first region.
839   /// @param B The second region.
840   /// @return The smallest region containing A and B.
841   RegionT *getCommonRegion(RegionT *A, RegionT *B) const;
842 
843   /// Find the smallest region that contains two basic blocks.
844   ///
845   /// @param A The first basic block.
846   /// @param B The second basic block.
847   /// @return The smallest region that contains A and B.
848   RegionT *getCommonRegion(BlockT *A, BlockT *B) const {
849     return getCommonRegion(getRegionFor(A), getRegionFor(B));
850   }
851 
852   /// Find the smallest region that contains a set of regions.
853   ///
854   /// @param Regions A vector of regions.
855   /// @return The smallest region that contains all regions in Regions.
856   RegionT *getCommonRegion(SmallVectorImpl<RegionT *> &Regions) const;
857 
858   /// Find the smallest region that contains a set of basic blocks.
859   ///
860   /// @param BBs A vector of basic blocks.
861   /// @return The smallest region that contains all basic blocks in BBS.
862   RegionT *getCommonRegion(SmallVectorImpl<BlockT *> &BBs) const;
863 
864   RegionT *getTopLevelRegion() const { return TopLevelRegion; }
865 
866   /// Clear the Node Cache for all Regions.
867   ///
868   /// @see Region::clearNodeCache()
869   void clearNodeCache() {
870     if (TopLevelRegion)
871       TopLevelRegion->clearNodeCache();
872   }
873 
874   void verifyAnalysis() const;
875 };
876 
877 class RegionNode : public RegionNodeBase<RegionTraits<Function>> {
878 public:
879   inline RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion = false)
880       : RegionNodeBase<RegionTraits<Function>>(Parent, Entry, isSubRegion) {}
881 
882   bool operator==(const Region &RN) const {
883     return this == reinterpret_cast<const RegionNode *>(&RN);
884   }
885 };
886 
887 class Region : public RegionBase<RegionTraits<Function>> {
888 public:
889   Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT,
890          Region *Parent = nullptr);
891   ~Region();
892 
893   bool operator==(const RegionNode &RN) const {
894     return &RN == reinterpret_cast<const RegionNode *>(this);
895   }
896 };
897 
898 class RegionInfo : public RegionInfoBase<RegionTraits<Function>> {
899 public:
900   using Base = RegionInfoBase<RegionTraits<Function>>;
901 
902   explicit RegionInfo();
903 
904   RegionInfo(RegionInfo &&Arg) : Base(std::move(static_cast<Base &>(Arg))) {
905     updateRegionTree(*this, TopLevelRegion);
906   }
907 
908   RegionInfo &operator=(RegionInfo &&RHS) {
909     Base::operator=(std::move(static_cast<Base &>(RHS)));
910     updateRegionTree(*this, TopLevelRegion);
911     return *this;
912   }
913 
914   ~RegionInfo() override;
915 
916   /// Handle invalidation explicitly.
917   bool invalidate(Function &F, const PreservedAnalyses &PA,
918                   FunctionAnalysisManager::Invalidator &);
919 
920   // updateStatistics - Update statistic about created regions.
921   void updateStatistics(Region *R) final;
922 
923   void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT,
924                    DominanceFrontier *DF);
925 
926 #ifndef NDEBUG
927   /// Opens a viewer to show the GraphViz visualization of the regions.
928   ///
929   /// Useful during debugging as an alternative to dump().
930   void view();
931 
932   /// Opens a viewer to show the GraphViz visualization of this region
933   /// without instructions in the BasicBlocks.
934   ///
935   /// Useful during debugging as an alternative to dump().
936   void viewOnly();
937 #endif
938 };
939 
940 class RegionInfoPass : public FunctionPass {
941   RegionInfo RI;
942 
943 public:
944   static char ID;
945 
946   explicit RegionInfoPass();
947   ~RegionInfoPass() override;
948 
949   RegionInfo &getRegionInfo() { return RI; }
950 
951   const RegionInfo &getRegionInfo() const { return RI; }
952 
953   /// @name FunctionPass interface
954   //@{
955   bool runOnFunction(Function &F) override;
956   void releaseMemory() override;
957   void verifyAnalysis() const override;
958   void getAnalysisUsage(AnalysisUsage &AU) const override;
959   void print(raw_ostream &OS, const Module *) const override;
960   void dump() const;
961   //@}
962 };
963 
964 /// Analysis pass that exposes the \c RegionInfo for a function.
965 class RegionInfoAnalysis : public AnalysisInfoMixin<RegionInfoAnalysis> {
966   friend AnalysisInfoMixin<RegionInfoAnalysis>;
967 
968   static AnalysisKey Key;
969 
970 public:
971   using Result = RegionInfo;
972 
973   RegionInfo run(Function &F, FunctionAnalysisManager &AM);
974 };
975 
976 /// Printer pass for the \c RegionInfo.
977 class RegionInfoPrinterPass : public PassInfoMixin<RegionInfoPrinterPass> {
978   raw_ostream &OS;
979 
980 public:
981   explicit RegionInfoPrinterPass(raw_ostream &OS);
982 
983   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
984 
985   static bool isRequired() { return true; }
986 };
987 
988 /// Verifier pass for the \c RegionInfo.
989 struct RegionInfoVerifierPass : PassInfoMixin<RegionInfoVerifierPass> {
990   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
991   static bool isRequired() { return true; }
992 };
993 
994 template <>
995 template <>
996 inline BasicBlock *
997 RegionNodeBase<RegionTraits<Function>>::getNodeAs<BasicBlock>() const {
998   assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
999   return getEntry();
1000 }
1001 
1002 template <>
1003 template <>
1004 inline Region *
1005 RegionNodeBase<RegionTraits<Function>>::getNodeAs<Region>() const {
1006   assert(isSubRegion() && "This is not a subregion RegionNode!");
1007   auto Unconst = const_cast<RegionNodeBase<RegionTraits<Function>> *>(this);
1008   return reinterpret_cast<Region *>(Unconst);
1009 }
1010 
1011 template <class Tr>
1012 inline raw_ostream &operator<<(raw_ostream &OS,
1013                                const RegionNodeBase<Tr> &Node) {
1014   using BlockT = typename Tr::BlockT;
1015   using RegionT = typename Tr::RegionT;
1016 
1017   if (Node.isSubRegion())
1018     return OS << Node.template getNodeAs<RegionT>()->getNameStr();
1019   else
1020     return OS << Node.template getNodeAs<BlockT>()->getName();
1021 }
1022 
1023 extern template class RegionBase<RegionTraits<Function>>;
1024 extern template class RegionNodeBase<RegionTraits<Function>>;
1025 extern template class RegionInfoBase<RegionTraits<Function>>;
1026 
1027 } // end namespace llvm
1028 
1029 #endif // LLVM_ANALYSIS_REGIONINFO_H
1030