LLVM API Documentation

RegionInfo.h
Go to the documentation of this file.
00001 //===- RegionInfo.h - SESE region analysis ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Calculate a program structure tree built out of single entry single exit
00011 // regions.
00012 // The basic ideas are taken from "The Program Structure Tree - Richard Johnson,
00013 // David Pearson, Keshav Pingali - 1994", however enriched with ideas from "The
00014 // Refined Process Structure Tree - Jussi Vanhatalo, Hagen Voelyer, Jana
00015 // Koehler - 2009".
00016 // The algorithm to calculate these data structures however is completely
00017 // different, as it takes advantage of existing information already available
00018 // in (Post)dominace tree and dominance frontier passes. This leads to a simpler
00019 // and in practice hopefully better performing algorithm. The runtime of the
00020 // algorithms described in the papers above are both linear in graph size,
00021 // O(V+E), whereas this algorithm is not, as the dominance frontier information
00022 // itself is not, but in practice runtime seems to be in the order of magnitude
00023 // of dominance tree calculation.
00024 //
00025 // WARNING: LLVM is generally very concerned about compile time such that
00026 //          the use of additional analysis passes in the default
00027 //          optimization sequence is avoided as much as possible.
00028 //          Specifically, if you do not need the RegionInfo, but dominance
00029 //          information could be sufficient please base your work only on
00030 //          the dominator tree. Most passes maintain it, such that using
00031 //          it has often near zero cost. In contrast RegionInfo is by
00032 //          default not available, is not maintained by existing
00033 //          transformations and there is no intention to do so.
00034 //
00035 //===----------------------------------------------------------------------===//
00036 
00037 #ifndef LLVM_ANALYSIS_REGIONINFO_H
00038 #define LLVM_ANALYSIS_REGIONINFO_H
00039 
00040 #include "llvm/ADT/DepthFirstIterator.h"
00041 #include "llvm/ADT/PointerIntPair.h"
00042 #include "llvm/IR/CFG.h"
00043 #include "llvm/IR/Dominators.h"
00044 #include <map>
00045 #include <memory>
00046 #include <set>
00047 
00048 namespace llvm {
00049 
00050 // RegionTraits - Class to be specialized for different users of RegionInfo
00051 // (i.e. BasicBlocks or MachineBasicBlocks). This is only to avoid needing to
00052 // pass around an unreasonable number of template parameters.
00053 template <class FuncT_>
00054 struct RegionTraits {
00055   // FuncT
00056   // BlockT
00057   // RegionT
00058   // RegionNodeT
00059   // RegionInfoT
00060   typedef typename FuncT_::UnknownRegionTypeError BrokenT;
00061 };
00062 
00063 class DominatorTree;
00064 class DominanceFrontier;
00065 class Loop;
00066 class LoopInfo;
00067 struct PostDominatorTree;
00068 class raw_ostream;
00069 class Region;
00070 template <class RegionTr>
00071 class RegionBase;
00072 class RegionNode;
00073 class RegionInfo;
00074 template <class RegionTr>
00075 class RegionInfoBase;
00076 
00077 template <>
00078 struct RegionTraits<Function> {
00079   typedef Function FuncT;
00080   typedef BasicBlock BlockT;
00081   typedef Region RegionT;
00082   typedef RegionNode RegionNodeT;
00083   typedef RegionInfo RegionInfoT;
00084   typedef DominatorTree DomTreeT;
00085   typedef DomTreeNode DomTreeNodeT;
00086   typedef DominanceFrontier DomFrontierT;
00087   typedef PostDominatorTree PostDomTreeT;
00088   typedef Instruction InstT;
00089   typedef Loop LoopT;
00090   typedef LoopInfo LoopInfoT;
00091 
00092   static unsigned getNumSuccessors(BasicBlock *BB) {
00093     return BB->getTerminator()->getNumSuccessors();
00094   }
00095 };
00096 
00097 /// @brief Marker class to iterate over the elements of a Region in flat mode.
00098 ///
00099 /// The class is used to either iterate in Flat mode or by not using it to not
00100 /// iterate in Flat mode.  During a Flat mode iteration all Regions are entered
00101 /// and the iteration returns every BasicBlock.  If the Flat mode is not
00102 /// selected for SubRegions just one RegionNode containing the subregion is
00103 /// returned.
00104 template <class GraphType>
00105 class FlatIt {};
00106 
00107 /// @brief A RegionNode represents a subregion or a BasicBlock that is part of a
00108 /// Region.
00109 template <class Tr>
00110 class RegionNodeBase {
00111   friend class RegionBase<Tr>;
00112 
00113 public:
00114   typedef typename Tr::BlockT BlockT;
00115   typedef typename Tr::RegionT RegionT;
00116 
00117 private:
00118   RegionNodeBase(const RegionNodeBase &) LLVM_DELETED_FUNCTION;
00119   const RegionNodeBase &operator=(const RegionNodeBase &) LLVM_DELETED_FUNCTION;
00120 
00121   /// This is the entry basic block that starts this region node.  If this is a
00122   /// BasicBlock RegionNode, then entry is just the basic block, that this
00123   /// RegionNode represents.  Otherwise it is the entry of this (Sub)RegionNode.
00124   ///
00125   /// In the BBtoRegionNode map of the parent of this node, BB will always map
00126   /// to this node no matter which kind of node this one is.
00127   ///
00128   /// The node can hold either a Region or a BasicBlock.
00129   /// Use one bit to save, if this RegionNode is a subregion or BasicBlock
00130   /// RegionNode.
00131   PointerIntPair<BlockT *, 1, bool> entry;
00132 
00133   /// @brief The parent Region of this RegionNode.
00134   /// @see getParent()
00135   RegionT *parent;
00136 
00137 protected:
00138   /// @brief Create a RegionNode.
00139   ///
00140   /// @param Parent      The parent of this RegionNode.
00141   /// @param Entry       The entry BasicBlock of the RegionNode.  If this
00142   ///                    RegionNode represents a BasicBlock, this is the
00143   ///                    BasicBlock itself.  If it represents a subregion, this
00144   ///                    is the entry BasicBlock of the subregion.
00145   /// @param isSubRegion If this RegionNode represents a SubRegion.
00146   inline RegionNodeBase(RegionT *Parent, BlockT *Entry,
00147                         bool isSubRegion = false)
00148       : entry(Entry, isSubRegion), parent(Parent) {}
00149 
00150 public:
00151   /// @brief Get the parent Region of this RegionNode.
00152   ///
00153   /// The parent Region is the Region this RegionNode belongs to. If for
00154   /// example a BasicBlock is element of two Regions, there exist two
00155   /// RegionNodes for this BasicBlock. Each with the getParent() function
00156   /// pointing to the Region this RegionNode belongs to.
00157   ///
00158   /// @return Get the parent Region of this RegionNode.
00159   inline RegionT *getParent() const { return parent; }
00160 
00161   /// @brief Get the entry BasicBlock of this RegionNode.
00162   ///
00163   /// If this RegionNode represents a BasicBlock this is just the BasicBlock
00164   /// itself, otherwise we return the entry BasicBlock of the Subregion
00165   ///
00166   /// @return The entry BasicBlock of this RegionNode.
00167   inline BlockT *getEntry() const { return entry.getPointer(); }
00168 
00169   /// @brief Get the content of this RegionNode.
00170   ///
00171   /// This can be either a BasicBlock or a subregion. Before calling getNodeAs()
00172   /// check the type of the content with the isSubRegion() function call.
00173   ///
00174   /// @return The content of this RegionNode.
00175   template <class T> inline T *getNodeAs() const;
00176 
00177   /// @brief Is this RegionNode a subregion?
00178   ///
00179   /// @return True if it contains a subregion. False if it contains a
00180   ///         BasicBlock.
00181   inline bool isSubRegion() const { return entry.getInt(); }
00182 };
00183 
00184 //===----------------------------------------------------------------------===//
00185 /// @brief A single entry single exit Region.
00186 ///
00187 /// A Region is a connected subgraph of a control flow graph that has exactly
00188 /// two connections to the remaining graph. It can be used to analyze or
00189 /// optimize parts of the control flow graph.
00190 ///
00191 /// A <em> simple Region </em> is connected to the remaining graph by just two
00192 /// edges. One edge entering the Region and another one leaving the Region.
00193 ///
00194 /// An <em> extended Region </em> (or just Region) is a subgraph that can be
00195 /// transform into a simple Region. The transformation is done by adding
00196 /// BasicBlocks that merge several entry or exit edges so that after the merge
00197 /// just one entry and one exit edge exists.
00198 ///
00199 /// The \e Entry of a Region is the first BasicBlock that is passed after
00200 /// entering the Region. It is an element of the Region. The entry BasicBlock
00201 /// dominates all BasicBlocks in the Region.
00202 ///
00203 /// The \e Exit of a Region is the first BasicBlock that is passed after
00204 /// leaving the Region. It is not an element of the Region. The exit BasicBlock,
00205 /// postdominates all BasicBlocks in the Region.
00206 ///
00207 /// A <em> canonical Region </em> cannot be constructed by combining smaller
00208 /// Regions.
00209 ///
00210 /// Region A is the \e parent of Region B, if B is completely contained in A.
00211 ///
00212 /// Two canonical Regions either do not intersect at all or one is
00213 /// the parent of the other.
00214 ///
00215 /// The <em> Program Structure Tree</em> is a graph (V, E) where V is the set of
00216 /// Regions in the control flow graph and E is the \e parent relation of these
00217 /// Regions.
00218 ///
00219 /// Example:
00220 ///
00221 /// \verbatim
00222 /// A simple control flow graph, that contains two regions.
00223 ///
00224 ///        1
00225 ///       / |
00226 ///      2   |
00227 ///     / \   3
00228 ///    4   5  |
00229 ///    |   |  |
00230 ///    6   7  8
00231 ///     \  | /
00232 ///      \ |/       Region A: 1 -> 9 {1,2,3,4,5,6,7,8}
00233 ///        9        Region B: 2 -> 9 {2,4,5,6,7}
00234 /// \endverbatim
00235 ///
00236 /// You can obtain more examples by either calling
00237 ///
00238 /// <tt> "opt -regions -analyze anyprogram.ll" </tt>
00239 /// or
00240 /// <tt> "opt -view-regions-only anyprogram.ll" </tt>
00241 ///
00242 /// on any LLVM file you are interested in.
00243 ///
00244 /// The first call returns a textual representation of the program structure
00245 /// tree, the second one creates a graphical representation using graphviz.
00246 template <class Tr>
00247 class RegionBase : public RegionNodeBase<Tr> {
00248   typedef typename Tr::FuncT FuncT;
00249   typedef typename Tr::BlockT BlockT;
00250   typedef typename Tr::RegionInfoT RegionInfoT;
00251   typedef typename Tr::RegionT RegionT;
00252   typedef typename Tr::RegionNodeT RegionNodeT;
00253   typedef typename Tr::DomTreeT DomTreeT;
00254   typedef typename Tr::LoopT LoopT;
00255   typedef typename Tr::LoopInfoT LoopInfoT;
00256   typedef typename Tr::InstT InstT;
00257 
00258   typedef GraphTraits<BlockT *> BlockTraits;
00259   typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
00260   typedef typename BlockTraits::ChildIteratorType SuccIterTy;
00261   typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
00262 
00263   friend class RegionInfoBase<Tr>;
00264   RegionBase(const RegionBase &) LLVM_DELETED_FUNCTION;
00265   const RegionBase &operator=(const RegionBase &) LLVM_DELETED_FUNCTION;
00266 
00267   // Information necessary to manage this Region.
00268   RegionInfoT *RI;
00269   DomTreeT *DT;
00270 
00271   // The exit BasicBlock of this region.
00272   // (The entry BasicBlock is part of RegionNode)
00273   BlockT *exit;
00274 
00275   typedef std::vector<std::unique_ptr<RegionT>> RegionSet;
00276 
00277   // The subregions of this region.
00278   RegionSet children;
00279 
00280   typedef std::map<BlockT *, RegionNodeT *> BBNodeMapT;
00281 
00282   // Save the BasicBlock RegionNodes that are element of this Region.
00283   mutable BBNodeMapT BBNodeMap;
00284 
00285   /// verifyBBInRegion - Check if a BB is in this Region. This check also works
00286   /// if the region is incorrectly built. (EXPENSIVE!)
00287   void verifyBBInRegion(BlockT *BB) const;
00288 
00289   /// verifyWalk - Walk over all the BBs of the region starting from BB and
00290   /// verify that all reachable basic blocks are elements of the region.
00291   /// (EXPENSIVE!)
00292   void verifyWalk(BlockT *BB, std::set<BlockT *> *visitedBB) const;
00293 
00294   /// verifyRegionNest - Verify if the region and its children are valid
00295   /// regions (EXPENSIVE!)
00296   void verifyRegionNest() const;
00297 
00298 public:
00299   /// @brief Create a new region.
00300   ///
00301   /// @param Entry  The entry basic block of the region.
00302   /// @param Exit   The exit basic block of the region.
00303   /// @param RI     The region info object that is managing this region.
00304   /// @param DT     The dominator tree of the current function.
00305   /// @param Parent The surrounding region or NULL if this is a top level
00306   ///               region.
00307   RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT,
00308              RegionT *Parent = nullptr);
00309 
00310   /// Delete the Region and all its subregions.
00311   ~RegionBase();
00312 
00313   /// @brief Get the entry BasicBlock of the Region.
00314   /// @return The entry BasicBlock of the region.
00315   BlockT *getEntry() const {
00316     return RegionNodeBase<Tr>::getEntry();
00317   }
00318 
00319   /// @brief Replace the entry basic block of the region with the new basic
00320   ///        block.
00321   ///
00322   /// @param BB  The new entry basic block of the region.
00323   void replaceEntry(BlockT *BB);
00324 
00325   /// @brief Replace the exit basic block of the region with the new basic
00326   ///        block.
00327   ///
00328   /// @param BB  The new exit basic block of the region.
00329   void replaceExit(BlockT *BB);
00330 
00331   /// @brief Recursively replace the entry basic block of the region.
00332   ///
00333   /// This function replaces the entry basic block with a new basic block. It
00334   /// also updates all child regions that have the same entry basic block as
00335   /// this region.
00336   ///
00337   /// @param NewEntry The new entry basic block.
00338   void replaceEntryRecursive(BlockT *NewEntry);
00339 
00340   /// @brief Recursively replace the exit basic block of the region.
00341   ///
00342   /// This function replaces the exit basic block with a new basic block. It
00343   /// also updates all child regions that have the same exit basic block as
00344   /// this region.
00345   ///
00346   /// @param NewExit The new exit basic block.
00347   void replaceExitRecursive(BlockT *NewExit);
00348 
00349   /// @brief Get the exit BasicBlock of the Region.
00350   /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
00351   ///         Region.
00352   BlockT *getExit() const { return exit; }
00353 
00354   /// @brief Get the parent of the Region.
00355   /// @return The parent of the Region or NULL if this is a top level
00356   ///         Region.
00357   RegionT *getParent() const {
00358     return RegionNodeBase<Tr>::getParent();
00359   }
00360 
00361   /// @brief Get the RegionNode representing the current Region.
00362   /// @return The RegionNode representing the current Region.
00363   RegionNodeT *getNode() const {
00364     return const_cast<RegionNodeT *>(
00365         reinterpret_cast<const RegionNodeT *>(this));
00366   }
00367 
00368   /// @brief Get the nesting level of this Region.
00369   ///
00370   /// An toplevel Region has depth 0.
00371   ///
00372   /// @return The depth of the region.
00373   unsigned getDepth() const;
00374 
00375   /// @brief Check if a Region is the TopLevel region.
00376   ///
00377   /// The toplevel region represents the whole function.
00378   bool isTopLevelRegion() const { return exit == nullptr; }
00379 
00380   /// @brief Return a new (non-canonical) region, that is obtained by joining
00381   ///        this region with its predecessors.
00382   ///
00383   /// @return A region also starting at getEntry(), but reaching to the next
00384   ///         basic block that forms with getEntry() a (non-canonical) region.
00385   ///         NULL if such a basic block does not exist.
00386   RegionT *getExpandedRegion() const;
00387 
00388   /// @brief Return the first block of this region's single entry edge,
00389   ///        if existing.
00390   ///
00391   /// @return The BasicBlock starting this region's single entry edge,
00392   ///         else NULL.
00393   BlockT *getEnteringBlock() const;
00394 
00395   /// @brief Return the first block of this region's single exit edge,
00396   ///        if existing.
00397   ///
00398   /// @return The BasicBlock starting this region's single exit edge,
00399   ///         else NULL.
00400   BlockT *getExitingBlock() const;
00401 
00402   /// @brief Is this a simple region?
00403   ///
00404   /// A region is simple if it has exactly one exit and one entry edge.
00405   ///
00406   /// @return True if the Region is simple.
00407   bool isSimple() const;
00408 
00409   /// @brief Returns the name of the Region.
00410   /// @return The Name of the Region.
00411   std::string getNameStr() const;
00412 
00413   /// @brief Return the RegionInfo object, that belongs to this Region.
00414   RegionInfoT *getRegionInfo() const { return RI; }
00415 
00416   /// PrintStyle - Print region in difference ways.
00417   enum PrintStyle { PrintNone, PrintBB, PrintRN };
00418 
00419   /// @brief Print the region.
00420   ///
00421   /// @param OS The output stream the Region is printed to.
00422   /// @param printTree Print also the tree of subregions.
00423   /// @param level The indentation level used for printing.
00424   void print(raw_ostream &OS, bool printTree = true, unsigned level = 0,
00425              PrintStyle Style = PrintNone) const;
00426 
00427   /// @brief Print the region to stderr.
00428   void dump() const;
00429 
00430   /// @brief Check if the region contains a BasicBlock.
00431   ///
00432   /// @param BB The BasicBlock that might be contained in this Region.
00433   /// @return True if the block is contained in the region otherwise false.
00434   bool contains(const BlockT *BB) const;
00435 
00436   /// @brief Check if the region contains another region.
00437   ///
00438   /// @param SubRegion The region that might be contained in this Region.
00439   /// @return True if SubRegion is contained in the region otherwise false.
00440   bool contains(const RegionT *SubRegion) const {
00441     // Toplevel Region.
00442     if (!getExit())
00443       return true;
00444 
00445     return contains(SubRegion->getEntry()) &&
00446            (contains(SubRegion->getExit()) ||
00447             SubRegion->getExit() == getExit());
00448   }
00449 
00450   /// @brief Check if the region contains an Instruction.
00451   ///
00452   /// @param Inst The Instruction that might be contained in this region.
00453   /// @return True if the Instruction is contained in the region otherwise
00454   /// false.
00455   bool contains(const InstT *Inst) const { return contains(Inst->getParent()); }
00456 
00457   /// @brief Check if the region contains a loop.
00458   ///
00459   /// @param L The loop that might be contained in this region.
00460   /// @return True if the loop is contained in the region otherwise false.
00461   ///         In case a NULL pointer is passed to this function the result
00462   ///         is false, except for the region that describes the whole function.
00463   ///         In that case true is returned.
00464   bool contains(const LoopT *L) const;
00465 
00466   /// @brief Get the outermost loop in the region that contains a loop.
00467   ///
00468   /// Find for a Loop L the outermost loop OuterL that is a parent loop of L
00469   /// and is itself contained in the region.
00470   ///
00471   /// @param L The loop the lookup is started.
00472   /// @return The outermost loop in the region, NULL if such a loop does not
00473   ///         exist or if the region describes the whole function.
00474   LoopT *outermostLoopInRegion(LoopT *L) const;
00475 
00476   /// @brief Get the outermost loop in the region that contains a basic block.
00477   ///
00478   /// Find for a basic block BB the outermost loop L that contains BB and is
00479   /// itself contained in the region.
00480   ///
00481   /// @param LI A pointer to a LoopInfo analysis.
00482   /// @param BB The basic block surrounded by the loop.
00483   /// @return The outermost loop in the region, NULL if such a loop does not
00484   ///         exist or if the region describes the whole function.
00485   LoopT *outermostLoopInRegion(LoopInfoT *LI, BlockT *BB) const;
00486 
00487   /// @brief Get the subregion that starts at a BasicBlock
00488   ///
00489   /// @param BB The BasicBlock the subregion should start.
00490   /// @return The Subregion if available, otherwise NULL.
00491   RegionT *getSubRegionNode(BlockT *BB) const;
00492 
00493   /// @brief Get the RegionNode for a BasicBlock
00494   ///
00495   /// @param BB The BasicBlock at which the RegionNode should start.
00496   /// @return If available, the RegionNode that represents the subregion
00497   ///         starting at BB. If no subregion starts at BB, the RegionNode
00498   ///         representing BB.
00499   RegionNodeT *getNode(BlockT *BB) const;
00500 
00501   /// @brief Get the BasicBlock RegionNode for a BasicBlock
00502   ///
00503   /// @param BB The BasicBlock for which the RegionNode is requested.
00504   /// @return The RegionNode representing the BB.
00505   RegionNodeT *getBBNode(BlockT *BB) const;
00506 
00507   /// @brief Add a new subregion to this Region.
00508   ///
00509   /// @param SubRegion The new subregion that will be added.
00510   /// @param moveChildren Move the children of this region, that are also
00511   ///                     contained in SubRegion into SubRegion.
00512   void addSubRegion(RegionT *SubRegion, bool moveChildren = false);
00513 
00514   /// @brief Remove a subregion from this Region.
00515   ///
00516   /// The subregion is not deleted, as it will probably be inserted into another
00517   /// region.
00518   /// @param SubRegion The SubRegion that will be removed.
00519   RegionT *removeSubRegion(RegionT *SubRegion);
00520 
00521   /// @brief Move all direct child nodes of this Region to another Region.
00522   ///
00523   /// @param To The Region the child nodes will be transferred to.
00524   void transferChildrenTo(RegionT *To);
00525 
00526   /// @brief Verify if the region is a correct region.
00527   ///
00528   /// Check if this is a correctly build Region. This is an expensive check, as
00529   /// the complete CFG of the Region will be walked.
00530   void verifyRegion() const;
00531 
00532   /// @brief Clear the cache for BB RegionNodes.
00533   ///
00534   /// After calling this function the BasicBlock RegionNodes will be stored at
00535   /// different memory locations. RegionNodes obtained before this function is
00536   /// called are therefore not comparable to RegionNodes abtained afterwords.
00537   void clearNodeCache();
00538 
00539   /// @name Subregion Iterators
00540   ///
00541   /// These iterators iterator over all subregions of this Region.
00542   //@{
00543   typedef typename RegionSet::iterator iterator;
00544   typedef typename RegionSet::const_iterator const_iterator;
00545 
00546   iterator begin() { return children.begin(); }
00547   iterator end() { return children.end(); }
00548 
00549   const_iterator begin() const { return children.begin(); }
00550   const_iterator end() const { return children.end(); }
00551   //@}
00552 
00553   /// @name BasicBlock Iterators
00554   ///
00555   /// These iterators iterate over all BasicBlocks that are contained in this
00556   /// Region. The iterator also iterates over BasicBlocks that are elements of
00557   /// a subregion of this Region. It is therefore called a flat iterator.
00558   //@{
00559   template <bool IsConst>
00560   class block_iterator_wrapper
00561       : public df_iterator<
00562             typename std::conditional<IsConst, const BlockT, BlockT>::type *> {
00563     typedef df_iterator<
00564         typename std::conditional<IsConst, const BlockT, BlockT>::type *> super;
00565 
00566   public:
00567     typedef block_iterator_wrapper<IsConst> Self;
00568     typedef typename super::pointer pointer;
00569 
00570     // Construct the begin iterator.
00571     block_iterator_wrapper(pointer Entry, pointer Exit)
00572         : super(df_begin(Entry)) {
00573       // Mark the exit of the region as visited, so that the children of the
00574       // exit and the exit itself, i.e. the block outside the region will never
00575       // be visited.
00576       super::Visited.insert(Exit);
00577     }
00578 
00579     // Construct the end iterator.
00580     block_iterator_wrapper() : super(df_end<pointer>((BlockT *)nullptr)) {}
00581 
00582     /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
00583 
00584     // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
00585     //        This was introduced for backwards compatibility, but should
00586     //        be removed as soon as all users are fixed.
00587     BlockT *operator*() const {
00588       return const_cast<BlockT *>(super::operator*());
00589     }
00590   };
00591 
00592   typedef block_iterator_wrapper<false> block_iterator;
00593   typedef block_iterator_wrapper<true> const_block_iterator;
00594 
00595   block_iterator block_begin() { return block_iterator(getEntry(), getExit()); }
00596 
00597   block_iterator block_end() { return block_iterator(); }
00598 
00599   const_block_iterator block_begin() const {
00600     return const_block_iterator(getEntry(), getExit());
00601   }
00602   const_block_iterator block_end() const { return const_block_iterator(); }
00603 
00604   typedef iterator_range<block_iterator> block_range;
00605   typedef iterator_range<const_block_iterator> const_block_range;
00606 
00607   /// @brief Returns a range view of the basic blocks in the region.
00608   inline block_range blocks() {
00609     return block_range(block_begin(), block_end());
00610   }
00611 
00612   /// @brief Returns a range view of the basic blocks in the region.
00613   ///
00614   /// This is the 'const' version of the range view.
00615   inline const_block_range blocks() const {
00616     return const_block_range(block_begin(), block_end());
00617   }
00618   //@}
00619 
00620   /// @name Element Iterators
00621   ///
00622   /// These iterators iterate over all BasicBlock and subregion RegionNodes that
00623   /// are direct children of this Region. It does not iterate over any
00624   /// RegionNodes that are also element of a subregion of this Region.
00625   //@{
00626   typedef df_iterator<RegionNodeT *, SmallPtrSet<RegionNodeT *, 8>, false,
00627                       GraphTraits<RegionNodeT *>> element_iterator;
00628 
00629   typedef df_iterator<const RegionNodeT *, SmallPtrSet<const RegionNodeT *, 8>,
00630                       false,
00631                       GraphTraits<const RegionNodeT *>> const_element_iterator;
00632 
00633   element_iterator element_begin();
00634   element_iterator element_end();
00635 
00636   const_element_iterator element_begin() const;
00637   const_element_iterator element_end() const;
00638   //@}
00639 };
00640 
00641 /// Print a RegionNode.
00642 template <class Tr>
00643 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNodeBase<Tr> &Node);
00644 
00645 //===----------------------------------------------------------------------===//
00646 /// @brief Analysis that detects all canonical Regions.
00647 ///
00648 /// The RegionInfo pass detects all canonical regions in a function. The Regions
00649 /// are connected using the parent relation. This builds a Program Structure
00650 /// Tree.
00651 template <class Tr>
00652 class RegionInfoBase {
00653   typedef typename Tr::BlockT BlockT;
00654   typedef typename Tr::FuncT FuncT;
00655   typedef typename Tr::RegionT RegionT;
00656   typedef typename Tr::RegionInfoT RegionInfoT;
00657   typedef typename Tr::DomTreeT DomTreeT;
00658   typedef typename Tr::DomTreeNodeT DomTreeNodeT;
00659   typedef typename Tr::PostDomTreeT PostDomTreeT;
00660   typedef typename Tr::DomFrontierT DomFrontierT;
00661   typedef GraphTraits<BlockT *> BlockTraits;
00662   typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
00663   typedef typename BlockTraits::ChildIteratorType SuccIterTy;
00664   typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
00665 
00666   friend class RegionInfo;
00667   friend class MachineRegionInfo;
00668   typedef DenseMap<BlockT *, BlockT *> BBtoBBMap;
00669   typedef DenseMap<BlockT *, RegionT *> BBtoRegionMap;
00670   typedef SmallPtrSet<RegionT *, 4> RegionSet;
00671 
00672   RegionInfoBase();
00673   virtual ~RegionInfoBase();
00674 
00675   RegionInfoBase(const RegionInfoBase &) LLVM_DELETED_FUNCTION;
00676   const RegionInfoBase &operator=(const RegionInfoBase &) LLVM_DELETED_FUNCTION;
00677 
00678   DomTreeT *DT;
00679   PostDomTreeT *PDT;
00680   DomFrontierT *DF;
00681 
00682   /// The top level region.
00683   RegionT *TopLevelRegion;
00684 
00685 private:
00686   /// Map every BB to the smallest region, that contains BB.
00687   BBtoRegionMap BBtoRegion;
00688 
00689   // isCommonDomFrontier - Returns true if BB is in the dominance frontier of
00690   // entry, because it was inherited from exit. In the other case there is an
00691   // edge going from entry to BB without passing exit.
00692   bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit) const;
00693 
00694   // isRegion - Check if entry and exit surround a valid region, based on
00695   // dominance tree and dominance frontier.
00696   bool isRegion(BlockT *entry, BlockT *exit) const;
00697 
00698   // insertShortCut - Saves a shortcut pointing from entry to exit.
00699   // This function may extend this shortcut if possible.
00700   void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut) const;
00701 
00702   // getNextPostDom - Returns the next BB that postdominates N, while skipping
00703   // all post dominators that cannot finish a canonical region.
00704   DomTreeNodeT *getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const;
00705 
00706   // isTrivialRegion - A region is trivial, if it contains only one BB.
00707   bool isTrivialRegion(BlockT *entry, BlockT *exit) const;
00708 
00709   // createRegion - Creates a single entry single exit region.
00710   RegionT *createRegion(BlockT *entry, BlockT *exit);
00711 
00712   // findRegionsWithEntry - Detect all regions starting with bb 'entry'.
00713   void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
00714 
00715   // scanForRegions - Detects regions in F.
00716   void scanForRegions(FuncT &F, BBtoBBMap *ShortCut);
00717 
00718   // getTopMostParent - Get the top most parent with the same entry block.
00719   RegionT *getTopMostParent(RegionT *region);
00720 
00721   // buildRegionsTree - build the region hierarchy after all region detected.
00722   void buildRegionsTree(DomTreeNodeT *N, RegionT *region);
00723 
00724   // updateStatistics - Update statistic about created regions.
00725   virtual void updateStatistics(RegionT *R) = 0;
00726 
00727   // calculate - detect all regions in function and build the region tree.
00728   void calculate(FuncT &F);
00729 
00730 public:
00731   static bool VerifyRegionInfo;
00732   static typename RegionT::PrintStyle printStyle;
00733 
00734   void print(raw_ostream &OS) const;
00735   void dump() const;
00736 
00737   void releaseMemory();
00738 
00739   /// @brief Get the smallest region that contains a BasicBlock.
00740   ///
00741   /// @param BB The basic block.
00742   /// @return The smallest region, that contains BB or NULL, if there is no
00743   /// region containing BB.
00744   RegionT *getRegionFor(BlockT *BB) const;
00745 
00746   /// @brief  Set the smallest region that surrounds a basic block.
00747   ///
00748   /// @param BB The basic block surrounded by a region.
00749   /// @param R The smallest region that surrounds BB.
00750   void setRegionFor(BlockT *BB, RegionT *R);
00751 
00752   /// @brief A shortcut for getRegionFor().
00753   ///
00754   /// @param BB The basic block.
00755   /// @return The smallest region, that contains BB or NULL, if there is no
00756   /// region containing BB.
00757   RegionT *operator[](BlockT *BB) const;
00758 
00759   /// @brief Return the exit of the maximal refined region, that starts at a
00760   /// BasicBlock.
00761   ///
00762   /// @param BB The BasicBlock the refined region starts.
00763   BlockT *getMaxRegionExit(BlockT *BB) const;
00764 
00765   /// @brief Find the smallest region that contains two regions.
00766   ///
00767   /// @param A The first region.
00768   /// @param B The second region.
00769   /// @return The smallest region containing A and B.
00770   RegionT *getCommonRegion(RegionT *A, RegionT *B) const;
00771 
00772   /// @brief Find the smallest region that contains two basic blocks.
00773   ///
00774   /// @param A The first basic block.
00775   /// @param B The second basic block.
00776   /// @return The smallest region that contains A and B.
00777   RegionT *getCommonRegion(BlockT *A, BlockT *B) const {
00778     return getCommonRegion(getRegionFor(A), getRegionFor(B));
00779   }
00780 
00781   /// @brief Find the smallest region that contains a set of regions.
00782   ///
00783   /// @param Regions A vector of regions.
00784   /// @return The smallest region that contains all regions in Regions.
00785   RegionT *getCommonRegion(SmallVectorImpl<RegionT *> &Regions) const;
00786 
00787   /// @brief Find the smallest region that contains a set of basic blocks.
00788   ///
00789   /// @param BBs A vector of basic blocks.
00790   /// @return The smallest region that contains all basic blocks in BBS.
00791   RegionT *getCommonRegion(SmallVectorImpl<BlockT *> &BBs) const;
00792 
00793   RegionT *getTopLevelRegion() const { return TopLevelRegion; }
00794 
00795   /// @brief Update RegionInfo after a basic block was split.
00796   ///
00797   /// @param NewBB The basic block that was created before OldBB.
00798   /// @param OldBB The old basic block.
00799   void splitBlock(BlockT *NewBB, BlockT *OldBB);
00800 
00801   /// @brief Clear the Node Cache for all Regions.
00802   ///
00803   /// @see Region::clearNodeCache()
00804   void clearNodeCache() {
00805     if (TopLevelRegion)
00806       TopLevelRegion->clearNodeCache();
00807   }
00808 
00809   void verifyAnalysis() const;
00810 };
00811 
00812 class Region;
00813 
00814 class RegionNode : public RegionNodeBase<RegionTraits<Function>> {
00815 public:
00816   inline RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion = false)
00817       : RegionNodeBase<RegionTraits<Function>>(Parent, Entry, isSubRegion) {}
00818 
00819   ~RegionNode() {}
00820 
00821   bool operator==(const Region &RN) const {
00822     return this == reinterpret_cast<const RegionNode *>(&RN);
00823   }
00824 };
00825 
00826 class Region : public RegionBase<RegionTraits<Function>> {
00827 public:
00828   Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT,
00829          Region *Parent = nullptr);
00830   ~Region();
00831 
00832   bool operator==(const RegionNode &RN) const {
00833     return &RN == reinterpret_cast<const RegionNode *>(this);
00834   }
00835 };
00836 
00837 class RegionInfo : public RegionInfoBase<RegionTraits<Function>> {
00838 public:
00839   explicit RegionInfo();
00840 
00841   virtual ~RegionInfo();
00842 
00843   // updateStatistics - Update statistic about created regions.
00844   void updateStatistics(Region *R) final;
00845 
00846   void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT,
00847                    DominanceFrontier *DF);
00848 };
00849 
00850 class RegionInfoPass : public FunctionPass {
00851   RegionInfo RI;
00852 
00853 public:
00854   static char ID;
00855   explicit RegionInfoPass();
00856 
00857   ~RegionInfoPass();
00858 
00859   RegionInfo &getRegionInfo() { return RI; }
00860 
00861   const RegionInfo &getRegionInfo() const { return RI; }
00862 
00863   /// @name FunctionPass interface
00864   //@{
00865   bool runOnFunction(Function &F) override;
00866   void releaseMemory() override;
00867   void verifyAnalysis() const override;
00868   void getAnalysisUsage(AnalysisUsage &AU) const override;
00869   void print(raw_ostream &OS, const Module *) const override;
00870   void dump() const;
00871   //@}
00872 };
00873 
00874 template <>
00875 template <>
00876 inline BasicBlock *
00877 RegionNodeBase<RegionTraits<Function>>::getNodeAs<BasicBlock>() const {
00878   assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
00879   return getEntry();
00880 }
00881 
00882 template <>
00883 template <>
00884 inline Region *
00885 RegionNodeBase<RegionTraits<Function>>::getNodeAs<Region>() const {
00886   assert(isSubRegion() && "This is not a subregion RegionNode!");
00887   auto Unconst = const_cast<RegionNodeBase<RegionTraits<Function>> *>(this);
00888   return reinterpret_cast<Region *>(Unconst);
00889 }
00890 
00891 template <class Tr>
00892 inline raw_ostream &operator<<(raw_ostream &OS,
00893                                const RegionNodeBase<Tr> &Node) {
00894   typedef typename Tr::BlockT BlockT;
00895   typedef typename Tr::RegionT RegionT;
00896 
00897   if (Node.isSubRegion())
00898     return OS << Node.template getNodeAs<RegionT>()->getNameStr();
00899   else
00900     return OS << Node.template getNodeAs<BlockT>()->getName();
00901 }
00902 
00903 EXTERN_TEMPLATE_INSTANTIATION(class RegionBase<RegionTraits<Function>>);
00904 EXTERN_TEMPLATE_INSTANTIATION(class RegionNodeBase<RegionTraits<Function>>);
00905 EXTERN_TEMPLATE_INSTANTIATION(class RegionInfoBase<RegionTraits<Function>>);
00906 
00907 } // End llvm namespace
00908 #endif