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 //===----------------------------------------------------------------------===//
00026 
00027 #ifndef LLVM_ANALYSIS_REGIONINFO_H
00028 #define LLVM_ANALYSIS_REGIONINFO_H
00029 
00030 #include "llvm/ADT/PointerIntPair.h"
00031 #include "llvm/Analysis/DominanceFrontier.h"
00032 #include "llvm/Analysis/PostDominators.h"
00033 #include "llvm/Support/Allocator.h"
00034 #include <map>
00035 
00036 namespace llvm {
00037 
00038 class Region;
00039 class RegionInfo;
00040 class raw_ostream;
00041 class Loop;
00042 class LoopInfo;
00043 
00044 /// @brief Marker class to iterate over the elements of a Region in flat mode.
00045 ///
00046 /// The class is used to either iterate in Flat mode or by not using it to not
00047 /// iterate in Flat mode.  During a Flat mode iteration all Regions are entered
00048 /// and the iteration returns every BasicBlock.  If the Flat mode is not
00049 /// selected for SubRegions just one RegionNode containing the subregion is
00050 /// returned.
00051 template <class GraphType>
00052 class FlatIt {};
00053 
00054 /// @brief A RegionNode represents a subregion or a BasicBlock that is part of a
00055 /// Region.
00056 class RegionNode {
00057   RegionNode(const RegionNode &) LLVM_DELETED_FUNCTION;
00058   const RegionNode &operator=(const RegionNode &) LLVM_DELETED_FUNCTION;
00059 
00060 protected:
00061   /// This is the entry basic block that starts this region node.  If this is a
00062   /// BasicBlock RegionNode, then entry is just the basic block, that this
00063   /// RegionNode represents.  Otherwise it is the entry of this (Sub)RegionNode.
00064   ///
00065   /// In the BBtoRegionNode map of the parent of this node, BB will always map
00066   /// to this node no matter which kind of node this one is.
00067   ///
00068   /// The node can hold either a Region or a BasicBlock.
00069   /// Use one bit to save, if this RegionNode is a subregion or BasicBlock
00070   /// RegionNode.
00071   PointerIntPair<BasicBlock*, 1, bool> entry;
00072 
00073   /// @brief The parent Region of this RegionNode.
00074   /// @see getParent()
00075   Region* parent;
00076 
00077 public:
00078   /// @brief Create a RegionNode.
00079   ///
00080   /// @param Parent      The parent of this RegionNode.
00081   /// @param Entry       The entry BasicBlock of the RegionNode.  If this
00082   ///                    RegionNode represents a BasicBlock, this is the
00083   ///                    BasicBlock itself.  If it represents a subregion, this
00084   ///                    is the entry BasicBlock of the subregion.
00085   /// @param isSubRegion If this RegionNode represents a SubRegion.
00086   inline RegionNode(Region* Parent, BasicBlock* Entry, bool isSubRegion = 0)
00087     : entry(Entry, isSubRegion), parent(Parent) {}
00088 
00089   /// @brief Get the parent Region of this RegionNode.
00090   ///
00091   /// The parent Region is the Region this RegionNode belongs to. If for
00092   /// example a BasicBlock is element of two Regions, there exist two
00093   /// RegionNodes for this BasicBlock. Each with the getParent() function
00094   /// pointing to the Region this RegionNode belongs to.
00095   ///
00096   /// @return Get the parent Region of this RegionNode.
00097   inline Region* getParent() const { return parent; }
00098 
00099   /// @brief Get the entry BasicBlock of this RegionNode.
00100   ///
00101   /// If this RegionNode represents a BasicBlock this is just the BasicBlock
00102   /// itself, otherwise we return the entry BasicBlock of the Subregion
00103   ///
00104   /// @return The entry BasicBlock of this RegionNode.
00105   inline BasicBlock* getEntry() const { return entry.getPointer(); }
00106 
00107   /// @brief Get the content of this RegionNode.
00108   ///
00109   /// This can be either a BasicBlock or a subregion. Before calling getNodeAs()
00110   /// check the type of the content with the isSubRegion() function call.
00111   ///
00112   /// @return The content of this RegionNode.
00113   template<class T>
00114   inline T* getNodeAs() const;
00115 
00116   /// @brief Is this RegionNode a subregion?
00117   ///
00118   /// @return True if it contains a subregion. False if it contains a
00119   ///         BasicBlock.
00120   inline bool isSubRegion() const {
00121     return entry.getInt();
00122   }
00123 };
00124 
00125 /// Print a RegionNode.
00126 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node);
00127 
00128 template<>
00129 inline BasicBlock* RegionNode::getNodeAs<BasicBlock>() const {
00130   assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
00131   return getEntry();
00132 }
00133 
00134 template<>
00135 inline Region* RegionNode::getNodeAs<Region>() const {
00136   assert(isSubRegion() && "This is not a subregion RegionNode!");
00137   return reinterpret_cast<Region*>(const_cast<RegionNode*>(this));
00138 }
00139 
00140 //===----------------------------------------------------------------------===//
00141 /// @brief A single entry single exit Region.
00142 ///
00143 /// A Region is a connected subgraph of a control flow graph that has exactly
00144 /// two connections to the remaining graph. It can be used to analyze or
00145 /// optimize parts of the control flow graph.
00146 ///
00147 /// A <em> simple Region </em> is connected to the remaining graph by just two
00148 /// edges. One edge entering the Region and another one leaving the Region.
00149 ///
00150 /// An <em> extended Region </em> (or just Region) is a subgraph that can be
00151 /// transform into a simple Region. The transformation is done by adding
00152 /// BasicBlocks that merge several entry or exit edges so that after the merge
00153 /// just one entry and one exit edge exists.
00154 ///
00155 /// The \e Entry of a Region is the first BasicBlock that is passed after
00156 /// entering the Region. It is an element of the Region. The entry BasicBlock
00157 /// dominates all BasicBlocks in the Region.
00158 ///
00159 /// The \e Exit of a Region is the first BasicBlock that is passed after
00160 /// leaving the Region. It is not an element of the Region. The exit BasicBlock,
00161 /// postdominates all BasicBlocks in the Region.
00162 ///
00163 /// A <em> canonical Region </em> cannot be constructed by combining smaller
00164 /// Regions.
00165 ///
00166 /// Region A is the \e parent of Region B, if B is completely contained in A.
00167 ///
00168 /// Two canonical Regions either do not intersect at all or one is
00169 /// the parent of the other.
00170 ///
00171 /// The <em> Program Structure Tree</em> is a graph (V, E) where V is the set of
00172 /// Regions in the control flow graph and E is the \e parent relation of these
00173 /// Regions.
00174 ///
00175 /// Example:
00176 ///
00177 /// \verbatim
00178 /// A simple control flow graph, that contains two regions.
00179 ///
00180 ///        1
00181 ///       / |
00182 ///      2   |
00183 ///     / \   3
00184 ///    4   5  |
00185 ///    |   |  |
00186 ///    6   7  8
00187 ///     \  | /
00188 ///      \ |/       Region A: 1 -> 9 {1,2,3,4,5,6,7,8}
00189 ///        9        Region B: 2 -> 9 {2,4,5,6,7}
00190 /// \endverbatim
00191 ///
00192 /// You can obtain more examples by either calling
00193 ///
00194 /// <tt> "opt -regions -analyze anyprogram.ll" </tt>
00195 /// or
00196 /// <tt> "opt -view-regions-only anyprogram.ll" </tt>
00197 ///
00198 /// on any LLVM file you are interested in.
00199 ///
00200 /// The first call returns a textual representation of the program structure
00201 /// tree, the second one creates a graphical representation using graphviz.
00202 class Region : public RegionNode {
00203   friend class RegionInfo;
00204   Region(const Region &) LLVM_DELETED_FUNCTION;
00205   const Region &operator=(const Region &) LLVM_DELETED_FUNCTION;
00206 
00207   // Information necessary to manage this Region.
00208   RegionInfo* RI;
00209   DominatorTree *DT;
00210 
00211   // The exit BasicBlock of this region.
00212   // (The entry BasicBlock is part of RegionNode)
00213   BasicBlock *exit;
00214 
00215   typedef std::vector<Region*> RegionSet;
00216 
00217   // The subregions of this region.
00218   RegionSet children;
00219 
00220   typedef std::map<BasicBlock*, RegionNode*> BBNodeMapT;
00221 
00222   // Save the BasicBlock RegionNodes that are element of this Region.
00223   mutable BBNodeMapT BBNodeMap;
00224 
00225   /// verifyBBInRegion - Check if a BB is in this Region. This check also works
00226   /// if the region is incorrectly built. (EXPENSIVE!)
00227   void verifyBBInRegion(BasicBlock* BB) const;
00228 
00229   /// verifyWalk - Walk over all the BBs of the region starting from BB and
00230   /// verify that all reachable basic blocks are elements of the region.
00231   /// (EXPENSIVE!)
00232   void verifyWalk(BasicBlock* BB, std::set<BasicBlock*>* visitedBB) const;
00233 
00234   /// verifyRegionNest - Verify if the region and its children are valid
00235   /// regions (EXPENSIVE!)
00236   void verifyRegionNest() const;
00237 
00238 public:
00239   /// @brief Create a new region.
00240   ///
00241   /// @param Entry  The entry basic block of the region.
00242   /// @param Exit   The exit basic block of the region.
00243   /// @param RI     The region info object that is managing this region.
00244   /// @param DT     The dominator tree of the current function.
00245   /// @param Parent The surrounding region or NULL if this is a top level
00246   ///               region.
00247   Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RI,
00248          DominatorTree *DT, Region *Parent = 0);
00249 
00250   /// Delete the Region and all its subregions.
00251   ~Region();
00252 
00253   /// @brief Get the entry BasicBlock of the Region.
00254   /// @return The entry BasicBlock of the region.
00255   BasicBlock *getEntry() const { return RegionNode::getEntry(); }
00256 
00257   /// @brief Replace the entry basic block of the region with the new basic
00258   ///        block.
00259   ///
00260   /// @param BB  The new entry basic block of the region.
00261   void replaceEntry(BasicBlock *BB);
00262 
00263   /// @brief Replace the exit basic block of the region with the new basic
00264   ///        block.
00265   ///
00266   /// @param BB  The new exit basic block of the region.
00267   void replaceExit(BasicBlock *BB);
00268 
00269   /// @brief Recursively replace the entry basic block of the region.
00270   ///
00271   /// This function replaces the entry basic block with a new basic block. It
00272   /// also updates all child regions that have the same entry basic block as
00273   /// this region.
00274   ///
00275   /// @param NewEntry The new entry basic block.
00276   void replaceEntryRecursive(BasicBlock *NewEntry);
00277 
00278   /// @brief Recursively replace the exit basic block of the region.
00279   ///
00280   /// This function replaces the exit basic block with a new basic block. It
00281   /// also updates all child regions that have the same exit basic block as
00282   /// this region.
00283   ///
00284   /// @param NewExit The new exit basic block.
00285   void replaceExitRecursive(BasicBlock *NewExit);
00286 
00287   /// @brief Get the exit BasicBlock of the Region.
00288   /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
00289   ///         Region.
00290   BasicBlock *getExit() const { return exit; }
00291 
00292   /// @brief Get the parent of the Region.
00293   /// @return The parent of the Region or NULL if this is a top level
00294   ///         Region.
00295   Region *getParent() const { return RegionNode::getParent(); }
00296 
00297   /// @brief Get the RegionNode representing the current Region.
00298   /// @return The RegionNode representing the current Region.
00299   RegionNode* getNode() const {
00300     return const_cast<RegionNode*>(reinterpret_cast<const RegionNode*>(this));
00301   }
00302 
00303   /// @brief Get the nesting level of this Region.
00304   ///
00305   /// An toplevel Region has depth 0.
00306   ///
00307   /// @return The depth of the region.
00308   unsigned getDepth() const;
00309 
00310   /// @brief Check if a Region is the TopLevel region.
00311   ///
00312   /// The toplevel region represents the whole function.
00313   bool isTopLevelRegion() const { return exit == NULL; }
00314 
00315   /// @brief Return a new (non canonical) region, that is obtained by joining
00316   ///        this region with its predecessors.
00317   ///
00318   /// @return A region also starting at getEntry(), but reaching to the next
00319   ///         basic block that forms with getEntry() a (non canonical) region.
00320   ///         NULL if such a basic block does not exist.
00321   Region *getExpandedRegion() const;
00322 
00323   /// @brief Return the first block of this region's single entry edge,
00324   ///        if existing.
00325   ///
00326   /// @return The BasicBlock starting this region's single entry edge,
00327   ///         else NULL.
00328   BasicBlock *getEnteringBlock() const;
00329 
00330   /// @brief Return the first block of this region's single exit edge,
00331   ///        if existing.
00332   ///
00333   /// @return The BasicBlock starting this region's single exit edge,
00334   ///         else NULL.
00335   BasicBlock *getExitingBlock() const;
00336 
00337   /// @brief Is this a simple region?
00338   ///
00339   /// A region is simple if it has exactly one exit and one entry edge.
00340   ///
00341   /// @return True if the Region is simple.
00342   bool isSimple() const;
00343 
00344   /// @brief Returns the name of the Region.
00345   /// @return The Name of the Region.
00346   std::string getNameStr() const;
00347 
00348   /// @brief Return the RegionInfo object, that belongs to this Region.
00349   RegionInfo *getRegionInfo() const {
00350     return RI;
00351   }
00352 
00353   /// PrintStyle - Print region in difference ways.
00354   enum PrintStyle { PrintNone, PrintBB, PrintRN  };
00355 
00356   /// @brief Print the region.
00357   ///
00358   /// @param OS The output stream the Region is printed to.
00359   /// @param printTree Print also the tree of subregions.
00360   /// @param level The indentation level used for printing.
00361   void print(raw_ostream& OS, bool printTree = true, unsigned level = 0,
00362              enum PrintStyle Style = PrintNone) const;
00363 
00364   /// @brief Print the region to stderr.
00365   void dump() const;
00366 
00367   /// @brief Check if the region contains a BasicBlock.
00368   ///
00369   /// @param BB The BasicBlock that might be contained in this Region.
00370   /// @return True if the block is contained in the region otherwise false.
00371   bool contains(const BasicBlock *BB) const;
00372 
00373   /// @brief Check if the region contains another region.
00374   ///
00375   /// @param SubRegion The region that might be contained in this Region.
00376   /// @return True if SubRegion is contained in the region otherwise false.
00377   bool contains(const Region *SubRegion) const {
00378     // Toplevel Region.
00379     if (!getExit())
00380       return true;
00381 
00382     return contains(SubRegion->getEntry())
00383       && (contains(SubRegion->getExit()) || SubRegion->getExit() == getExit());
00384   }
00385 
00386   /// @brief Check if the region contains an Instruction.
00387   ///
00388   /// @param Inst The Instruction that might be contained in this region.
00389   /// @return True if the Instruction is contained in the region otherwise false.
00390   bool contains(const Instruction *Inst) const {
00391     return contains(Inst->getParent());
00392   }
00393 
00394   /// @brief Check if the region contains a loop.
00395   ///
00396   /// @param L The loop that might be contained in this region.
00397   /// @return True if the loop is contained in the region otherwise false.
00398   ///         In case a NULL pointer is passed to this function the result
00399   ///         is false, except for the region that describes the whole function.
00400   ///         In that case true is returned.
00401   bool contains(const Loop *L) const;
00402 
00403   /// @brief Get the outermost loop in the region that contains a loop.
00404   ///
00405   /// Find for a Loop L the outermost loop OuterL that is a parent loop of L
00406   /// and is itself contained in the region.
00407   ///
00408   /// @param L The loop the lookup is started.
00409   /// @return The outermost loop in the region, NULL if such a loop does not
00410   ///         exist or if the region describes the whole function.
00411   Loop *outermostLoopInRegion(Loop *L) const;
00412 
00413   /// @brief Get the outermost loop in the region that contains a basic block.
00414   ///
00415   /// Find for a basic block BB the outermost loop L that contains BB and is
00416   /// itself contained in the region.
00417   ///
00418   /// @param LI A pointer to a LoopInfo analysis.
00419   /// @param BB The basic block surrounded by the loop.
00420   /// @return The outermost loop in the region, NULL if such a loop does not
00421   ///         exist or if the region describes the whole function.
00422   Loop *outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const;
00423 
00424   /// @brief Get the subregion that starts at a BasicBlock
00425   ///
00426   /// @param BB The BasicBlock the subregion should start.
00427   /// @return The Subregion if available, otherwise NULL.
00428   Region* getSubRegionNode(BasicBlock *BB) const;
00429 
00430   /// @brief Get the RegionNode for a BasicBlock
00431   ///
00432   /// @param BB The BasicBlock at which the RegionNode should start.
00433   /// @return If available, the RegionNode that represents the subregion
00434   ///         starting at BB. If no subregion starts at BB, the RegionNode
00435   ///         representing BB.
00436   RegionNode* getNode(BasicBlock *BB) const;
00437 
00438   /// @brief Get the BasicBlock RegionNode for a BasicBlock
00439   ///
00440   /// @param BB The BasicBlock for which the RegionNode is requested.
00441   /// @return The RegionNode representing the BB.
00442   RegionNode* getBBNode(BasicBlock *BB) const;
00443 
00444   /// @brief Add a new subregion to this Region.
00445   ///
00446   /// @param SubRegion The new subregion that will be added.
00447   /// @param moveChildren Move the children of this region, that are also
00448   ///                     contained in SubRegion into SubRegion.
00449   void addSubRegion(Region *SubRegion, bool moveChildren = false);
00450 
00451   /// @brief Remove a subregion from this Region.
00452   ///
00453   /// The subregion is not deleted, as it will probably be inserted into another
00454   /// region.
00455   /// @param SubRegion The SubRegion that will be removed.
00456   Region *removeSubRegion(Region *SubRegion);
00457 
00458   /// @brief Move all direct child nodes of this Region to another Region.
00459   ///
00460   /// @param To The Region the child nodes will be transferred to.
00461   void transferChildrenTo(Region *To);
00462 
00463   /// @brief Verify if the region is a correct region.
00464   ///
00465   /// Check if this is a correctly build Region. This is an expensive check, as
00466   /// the complete CFG of the Region will be walked.
00467   void verifyRegion() const;
00468 
00469   /// @brief Clear the cache for BB RegionNodes.
00470   ///
00471   /// After calling this function the BasicBlock RegionNodes will be stored at
00472   /// different memory locations. RegionNodes obtained before this function is
00473   /// called are therefore not comparable to RegionNodes abtained afterwords.
00474   void clearNodeCache();
00475 
00476   /// @name Subregion Iterators
00477   ///
00478   /// These iterators iterator over all subregions of this Region.
00479   //@{
00480   typedef RegionSet::iterator iterator;
00481   typedef RegionSet::const_iterator const_iterator;
00482 
00483   iterator begin() { return children.begin(); }
00484   iterator end() { return children.end(); }
00485 
00486   const_iterator begin() const { return children.begin(); }
00487   const_iterator end() const { return children.end(); }
00488   //@}
00489 
00490   /// @name BasicBlock Iterators
00491   ///
00492   /// These iterators iterate over all BasicBlocks that are contained in this
00493   /// Region. The iterator also iterates over BasicBlocks that are elements of
00494   /// a subregion of this Region. It is therefore called a flat iterator.
00495   //@{
00496   template <bool IsConst>
00497   class block_iterator_wrapper
00498     : public df_iterator<typename conditional<IsConst,
00499                                               const BasicBlock,
00500                                               BasicBlock>::type*> {
00501     typedef df_iterator<typename conditional<IsConst,
00502                                              const BasicBlock,
00503                                              BasicBlock>::type*>
00504       super;
00505   public:
00506     typedef block_iterator_wrapper<IsConst> Self;
00507     typedef typename super::pointer pointer;
00508 
00509     // Construct the begin iterator.
00510     block_iterator_wrapper(pointer Entry, pointer Exit) : super(df_begin(Entry))
00511     {
00512       // Mark the exit of the region as visited, so that the children of the
00513       // exit and the exit itself, i.e. the block outside the region will never
00514       // be visited.
00515       super::Visited.insert(Exit);
00516     }
00517 
00518     // Construct the end iterator.
00519     block_iterator_wrapper() : super(df_end<pointer>((BasicBlock *)0)) {}
00520 
00521     /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
00522 
00523     // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
00524     //        This was introduced for backwards compatibility, but should
00525     //        be removed as soon as all users are fixed.
00526     BasicBlock *operator*() const {
00527       return const_cast<BasicBlock*>(super::operator*());
00528     }
00529   };
00530 
00531   typedef block_iterator_wrapper<false> block_iterator;
00532   typedef block_iterator_wrapper<true>  const_block_iterator;
00533 
00534   block_iterator block_begin() {
00535    return block_iterator(getEntry(), getExit());
00536   }
00537 
00538   block_iterator block_end() {
00539    return block_iterator();
00540   }
00541 
00542   const_block_iterator block_begin() const {
00543     return const_block_iterator(getEntry(), getExit());
00544   }
00545   const_block_iterator block_end() const {
00546     return const_block_iterator();
00547   }
00548   //@}
00549 
00550   /// @name Element Iterators
00551   ///
00552   /// These iterators iterate over all BasicBlock and subregion RegionNodes that
00553   /// are direct children of this Region. It does not iterate over any
00554   /// RegionNodes that are also element of a subregion of this Region.
00555   //@{
00556   typedef df_iterator<RegionNode*, SmallPtrSet<RegionNode*, 8>, false,
00557                       GraphTraits<RegionNode*> > element_iterator;
00558 
00559   typedef df_iterator<const RegionNode*, SmallPtrSet<const RegionNode*, 8>,
00560                       false, GraphTraits<const RegionNode*> >
00561             const_element_iterator;
00562 
00563   element_iterator element_begin();
00564   element_iterator element_end();
00565 
00566   const_element_iterator element_begin() const;
00567   const_element_iterator element_end() const;
00568   //@}
00569 };
00570 
00571 //===----------------------------------------------------------------------===//
00572 /// @brief Analysis that detects all canonical Regions.
00573 ///
00574 /// The RegionInfo pass detects all canonical regions in a function. The Regions
00575 /// are connected using the parent relation. This builds a Program Structure
00576 /// Tree.
00577 class RegionInfo : public FunctionPass {
00578   typedef DenseMap<BasicBlock*,BasicBlock*> BBtoBBMap;
00579   typedef DenseMap<BasicBlock*, Region*> BBtoRegionMap;
00580   typedef SmallPtrSet<Region*, 4> RegionSet;
00581 
00582   RegionInfo(const RegionInfo &) LLVM_DELETED_FUNCTION;
00583   const RegionInfo &operator=(const RegionInfo &) LLVM_DELETED_FUNCTION;
00584 
00585   DominatorTree *DT;
00586   PostDominatorTree *PDT;
00587   DominanceFrontier *DF;
00588 
00589   /// The top level region.
00590   Region *TopLevelRegion;
00591 
00592   /// Map every BB to the smallest region, that contains BB.
00593   BBtoRegionMap BBtoRegion;
00594 
00595   // isCommonDomFrontier - Returns true if BB is in the dominance frontier of
00596   // entry, because it was inherited from exit. In the other case there is an
00597   // edge going from entry to BB without passing exit.
00598   bool isCommonDomFrontier(BasicBlock* BB, BasicBlock* entry,
00599                            BasicBlock* exit) const;
00600 
00601   // isRegion - Check if entry and exit surround a valid region, based on
00602   // dominance tree and dominance frontier.
00603   bool isRegion(BasicBlock* entry, BasicBlock* exit) const;
00604 
00605   // insertShortCut - Saves a shortcut pointing from entry to exit.
00606   // This function may extend this shortcut if possible.
00607   void insertShortCut(BasicBlock* entry, BasicBlock* exit,
00608                       BBtoBBMap* ShortCut) const;
00609 
00610   // getNextPostDom - Returns the next BB that postdominates N, while skipping
00611   // all post dominators that cannot finish a canonical region.
00612   DomTreeNode *getNextPostDom(DomTreeNode* N, BBtoBBMap *ShortCut) const;
00613 
00614   // isTrivialRegion - A region is trivial, if it contains only one BB.
00615   bool isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const;
00616 
00617   // createRegion - Creates a single entry single exit region.
00618   Region *createRegion(BasicBlock *entry, BasicBlock *exit);
00619 
00620   // findRegionsWithEntry - Detect all regions starting with bb 'entry'.
00621   void findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut);
00622 
00623   // scanForRegions - Detects regions in F.
00624   void scanForRegions(Function &F, BBtoBBMap *ShortCut);
00625 
00626   // getTopMostParent - Get the top most parent with the same entry block.
00627   Region *getTopMostParent(Region *region);
00628 
00629   // buildRegionsTree - build the region hierarchy after all region detected.
00630   void buildRegionsTree(DomTreeNode *N, Region *region);
00631 
00632   // Calculate - detecte all regions in function and build the region tree.
00633   void Calculate(Function& F);
00634 
00635   void releaseMemory();
00636 
00637   // updateStatistics - Update statistic about created regions.
00638   void updateStatistics(Region *R);
00639 
00640   // isSimple - Check if a region is a simple region with exactly one entry
00641   // edge and exactly one exit edge.
00642   bool isSimple(Region* R) const;
00643 
00644 public:
00645   static char ID;
00646   explicit RegionInfo();
00647 
00648   ~RegionInfo();
00649 
00650   /// @name FunctionPass interface
00651   //@{
00652   virtual bool runOnFunction(Function &F);
00653   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00654   virtual void print(raw_ostream &OS, const Module *) const;
00655   virtual void verifyAnalysis() const;
00656   //@}
00657 
00658   /// @brief Get the smallest region that contains a BasicBlock.
00659   ///
00660   /// @param BB The basic block.
00661   /// @return The smallest region, that contains BB or NULL, if there is no
00662   /// region containing BB.
00663   Region *getRegionFor(BasicBlock *BB) const;
00664 
00665   /// @brief  Set the smallest region that surrounds a basic block.
00666   ///
00667   /// @param BB The basic block surrounded by a region.
00668   /// @param R The smallest region that surrounds BB.
00669   void setRegionFor(BasicBlock *BB, Region *R);
00670 
00671   /// @brief A shortcut for getRegionFor().
00672   ///
00673   /// @param BB The basic block.
00674   /// @return The smallest region, that contains BB or NULL, if there is no
00675   /// region containing BB.
00676   Region *operator[](BasicBlock *BB) const;
00677 
00678   /// @brief Return the exit of the maximal refined region, that starts at a
00679   /// BasicBlock.
00680   ///
00681   /// @param BB The BasicBlock the refined region starts.
00682   BasicBlock *getMaxRegionExit(BasicBlock *BB) const;
00683 
00684   /// @brief Find the smallest region that contains two regions.
00685   ///
00686   /// @param A The first region.
00687   /// @param B The second region.
00688   /// @return The smallest region containing A and B.
00689   Region *getCommonRegion(Region* A, Region *B) const;
00690 
00691   /// @brief Find the smallest region that contains two basic blocks.
00692   ///
00693   /// @param A The first basic block.
00694   /// @param B The second basic block.
00695   /// @return The smallest region that contains A and B.
00696   Region* getCommonRegion(BasicBlock* A, BasicBlock *B) const {
00697     return getCommonRegion(getRegionFor(A), getRegionFor(B));
00698   }
00699 
00700   /// @brief Find the smallest region that contains a set of regions.
00701   ///
00702   /// @param Regions A vector of regions.
00703   /// @return The smallest region that contains all regions in Regions.
00704   Region* getCommonRegion(SmallVectorImpl<Region*> &Regions) const;
00705 
00706   /// @brief Find the smallest region that contains a set of basic blocks.
00707   ///
00708   /// @param BBs A vector of basic blocks.
00709   /// @return The smallest region that contains all basic blocks in BBS.
00710   Region* getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const;
00711 
00712   Region *getTopLevelRegion() const {
00713     return TopLevelRegion;
00714   }
00715 
00716   /// @brief Update RegionInfo after a basic block was split.
00717   ///
00718   /// @param NewBB The basic block that was created before OldBB.
00719   /// @param OldBB The old basic block.
00720   void splitBlock(BasicBlock* NewBB, BasicBlock *OldBB);
00721 
00722   /// @brief Clear the Node Cache for all Regions.
00723   ///
00724   /// @see Region::clearNodeCache()
00725   void clearNodeCache() {
00726     if (TopLevelRegion)
00727       TopLevelRegion->clearNodeCache();
00728   }
00729 };
00730 
00731 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node) {
00732   if (Node.isSubRegion())
00733     return OS << Node.getNodeAs<Region>()->getNameStr();
00734   else
00735     return OS << Node.getNodeAs<BasicBlock>()->getName();
00736 }
00737 } // End llvm namespace
00738 #endif
00739