Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

739 lines
27 KiB

  1. //===- RegionInfo.h - SESE region analysis ----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Calculate a program structure tree built out of single entry single exit
  11. // regions.
  12. // The basic ideas are taken from "The Program Structure Tree - Richard Johnson,
  13. // David Pearson, Keshav Pingali - 1994", however enriched with ideas from "The
  14. // Refined Process Structure Tree - Jussi Vanhatalo, Hagen Voelyer, Jana
  15. // Koehler - 2009".
  16. // The algorithm to calculate these data structures however is completely
  17. // different, as it takes advantage of existing information already available
  18. // in (Post)dominace tree and dominance frontier passes. This leads to a simpler
  19. // and in practice hopefully better performing algorithm. The runtime of the
  20. // algorithms described in the papers above are both linear in graph size,
  21. // O(V+E), whereas this algorithm is not, as the dominance frontier information
  22. // itself is not, but in practice runtime seems to be in the order of magnitude
  23. // of dominance tree calculation.
  24. //
  25. //===----------------------------------------------------------------------===//
  26. #ifndef LLVM_ANALYSIS_REGIONINFO_H
  27. #define LLVM_ANALYSIS_REGIONINFO_H
  28. #include "llvm/ADT/PointerIntPair.h"
  29. #include "llvm/Analysis/DominanceFrontier.h"
  30. #include "llvm/Analysis/PostDominators.h"
  31. #include "llvm/Support/Allocator.h"
  32. #include <map>
  33. namespace llvm {
  34. class Region;
  35. class RegionInfo;
  36. class raw_ostream;
  37. class Loop;
  38. class LoopInfo;
  39. /// @brief Marker class to iterate over the elements of a Region in flat mode.
  40. ///
  41. /// The class is used to either iterate in Flat mode or by not using it to not
  42. /// iterate in Flat mode. During a Flat mode iteration all Regions are entered
  43. /// and the iteration returns every BasicBlock. If the Flat mode is not
  44. /// selected for SubRegions just one RegionNode containing the subregion is
  45. /// returned.
  46. template <class GraphType>
  47. class FlatIt {};
  48. /// @brief A RegionNode represents a subregion or a BasicBlock that is part of a
  49. /// Region.
  50. class RegionNode {
  51. RegionNode(const RegionNode &) LLVM_DELETED_FUNCTION;
  52. const RegionNode &operator=(const RegionNode &) LLVM_DELETED_FUNCTION;
  53. protected:
  54. /// This is the entry basic block that starts this region node. If this is a
  55. /// BasicBlock RegionNode, then entry is just the basic block, that this
  56. /// RegionNode represents. Otherwise it is the entry of this (Sub)RegionNode.
  57. ///
  58. /// In the BBtoRegionNode map of the parent of this node, BB will always map
  59. /// to this node no matter which kind of node this one is.
  60. ///
  61. /// The node can hold either a Region or a BasicBlock.
  62. /// Use one bit to save, if this RegionNode is a subregion or BasicBlock
  63. /// RegionNode.
  64. PointerIntPair<BasicBlock*, 1, bool> entry;
  65. /// @brief The parent Region of this RegionNode.
  66. /// @see getParent()
  67. Region* parent;
  68. public:
  69. /// @brief Create a RegionNode.
  70. ///
  71. /// @param Parent The parent of this RegionNode.
  72. /// @param Entry The entry BasicBlock of the RegionNode. If this
  73. /// RegionNode represents a BasicBlock, this is the
  74. /// BasicBlock itself. If it represents a subregion, this
  75. /// is the entry BasicBlock of the subregion.
  76. /// @param isSubRegion If this RegionNode represents a SubRegion.
  77. inline RegionNode(Region* Parent, BasicBlock* Entry, bool isSubRegion = 0)
  78. : entry(Entry, isSubRegion), parent(Parent) {}
  79. /// @brief Get the parent Region of this RegionNode.
  80. ///
  81. /// The parent Region is the Region this RegionNode belongs to. If for
  82. /// example a BasicBlock is element of two Regions, there exist two
  83. /// RegionNodes for this BasicBlock. Each with the getParent() function
  84. /// pointing to the Region this RegionNode belongs to.
  85. ///
  86. /// @return Get the parent Region of this RegionNode.
  87. inline Region* getParent() const { return parent; }
  88. /// @brief Get the entry BasicBlock of this RegionNode.
  89. ///
  90. /// If this RegionNode represents a BasicBlock this is just the BasicBlock
  91. /// itself, otherwise we return the entry BasicBlock of the Subregion
  92. ///
  93. /// @return The entry BasicBlock of this RegionNode.
  94. inline BasicBlock* getEntry() const { return entry.getPointer(); }
  95. /// @brief Get the content of this RegionNode.
  96. ///
  97. /// This can be either a BasicBlock or a subregion. Before calling getNodeAs()
  98. /// check the type of the content with the isSubRegion() function call.
  99. ///
  100. /// @return The content of this RegionNode.
  101. template<class T>
  102. inline T* getNodeAs() const;
  103. /// @brief Is this RegionNode a subregion?
  104. ///
  105. /// @return True if it contains a subregion. False if it contains a
  106. /// BasicBlock.
  107. inline bool isSubRegion() const {
  108. return entry.getInt();
  109. }
  110. };
  111. /// Print a RegionNode.
  112. inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node);
  113. template<>
  114. inline BasicBlock* RegionNode::getNodeAs<BasicBlock>() const {
  115. assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
  116. return getEntry();
  117. }
  118. template<>
  119. inline Region* RegionNode::getNodeAs<Region>() const {
  120. assert(isSubRegion() && "This is not a subregion RegionNode!");
  121. return reinterpret_cast<Region*>(const_cast<RegionNode*>(this));
  122. }
  123. //===----------------------------------------------------------------------===//
  124. /// @brief A single entry single exit Region.
  125. ///
  126. /// A Region is a connected subgraph of a control flow graph that has exactly
  127. /// two connections to the remaining graph. It can be used to analyze or
  128. /// optimize parts of the control flow graph.
  129. ///
  130. /// A <em> simple Region </em> is connected to the remaining graph by just two
  131. /// edges. One edge entering the Region and another one leaving the Region.
  132. ///
  133. /// An <em> extended Region </em> (or just Region) is a subgraph that can be
  134. /// transform into a simple Region. The transformation is done by adding
  135. /// BasicBlocks that merge several entry or exit edges so that after the merge
  136. /// just one entry and one exit edge exists.
  137. ///
  138. /// The \e Entry of a Region is the first BasicBlock that is passed after
  139. /// entering the Region. It is an element of the Region. The entry BasicBlock
  140. /// dominates all BasicBlocks in the Region.
  141. ///
  142. /// The \e Exit of a Region is the first BasicBlock that is passed after
  143. /// leaving the Region. It is not an element of the Region. The exit BasicBlock,
  144. /// postdominates all BasicBlocks in the Region.
  145. ///
  146. /// A <em> canonical Region </em> cannot be constructed by combining smaller
  147. /// Regions.
  148. ///
  149. /// Region A is the \e parent of Region B, if B is completely contained in A.
  150. ///
  151. /// Two canonical Regions either do not intersect at all or one is
  152. /// the parent of the other.
  153. ///
  154. /// The <em> Program Structure Tree</em> is a graph (V, E) where V is the set of
  155. /// Regions in the control flow graph and E is the \e parent relation of these
  156. /// Regions.
  157. ///
  158. /// Example:
  159. ///
  160. /// \verbatim
  161. /// A simple control flow graph, that contains two regions.
  162. ///
  163. /// 1
  164. /// / |
  165. /// 2 |
  166. /// / \ 3
  167. /// 4 5 |
  168. /// | | |
  169. /// 6 7 8
  170. /// \ | /
  171. /// \ |/ Region A: 1 -> 9 {1,2,3,4,5,6,7,8}
  172. /// 9 Region B: 2 -> 9 {2,4,5,6,7}
  173. /// \endverbatim
  174. ///
  175. /// You can obtain more examples by either calling
  176. ///
  177. /// <tt> "opt -regions -analyze anyprogram.ll" </tt>
  178. /// or
  179. /// <tt> "opt -view-regions-only anyprogram.ll" </tt>
  180. ///
  181. /// on any LLVM file you are interested in.
  182. ///
  183. /// The first call returns a textual representation of the program structure
  184. /// tree, the second one creates a graphical representation using graphviz.
  185. class Region : public RegionNode {
  186. friend class RegionInfo;
  187. Region(const Region &) LLVM_DELETED_FUNCTION;
  188. const Region &operator=(const Region &) LLVM_DELETED_FUNCTION;
  189. // Information necessary to manage this Region.
  190. RegionInfo* RI;
  191. DominatorTree *DT;
  192. // The exit BasicBlock of this region.
  193. // (The entry BasicBlock is part of RegionNode)
  194. BasicBlock *exit;
  195. typedef std::vector<Region*> RegionSet;
  196. // The subregions of this region.
  197. RegionSet children;
  198. typedef std::map<BasicBlock*, RegionNode*> BBNodeMapT;
  199. // Save the BasicBlock RegionNodes that are element of this Region.
  200. mutable BBNodeMapT BBNodeMap;
  201. /// verifyBBInRegion - Check if a BB is in this Region. This check also works
  202. /// if the region is incorrectly built. (EXPENSIVE!)
  203. void verifyBBInRegion(BasicBlock* BB) const;
  204. /// verifyWalk - Walk over all the BBs of the region starting from BB and
  205. /// verify that all reachable basic blocks are elements of the region.
  206. /// (EXPENSIVE!)
  207. void verifyWalk(BasicBlock* BB, std::set<BasicBlock*>* visitedBB) const;
  208. /// verifyRegionNest - Verify if the region and its children are valid
  209. /// regions (EXPENSIVE!)
  210. void verifyRegionNest() const;
  211. public:
  212. /// @brief Create a new region.
  213. ///
  214. /// @param Entry The entry basic block of the region.
  215. /// @param Exit The exit basic block of the region.
  216. /// @param RI The region info object that is managing this region.
  217. /// @param DT The dominator tree of the current function.
  218. /// @param Parent The surrounding region or NULL if this is a top level
  219. /// region.
  220. Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RI,
  221. DominatorTree *DT, Region *Parent = 0);
  222. /// Delete the Region and all its subregions.
  223. ~Region();
  224. /// @brief Get the entry BasicBlock of the Region.
  225. /// @return The entry BasicBlock of the region.
  226. BasicBlock *getEntry() const { return RegionNode::getEntry(); }
  227. /// @brief Replace the entry basic block of the region with the new basic
  228. /// block.
  229. ///
  230. /// @param BB The new entry basic block of the region.
  231. void replaceEntry(BasicBlock *BB);
  232. /// @brief Replace the exit basic block of the region with the new basic
  233. /// block.
  234. ///
  235. /// @param BB The new exit basic block of the region.
  236. void replaceExit(BasicBlock *BB);
  237. /// @brief Recursively replace the entry basic block of the region.
  238. ///
  239. /// This function replaces the entry basic block with a new basic block. It
  240. /// also updates all child regions that have the same entry basic block as
  241. /// this region.
  242. ///
  243. /// @param NewEntry The new entry basic block.
  244. void replaceEntryRecursive(BasicBlock *NewEntry);
  245. /// @brief Recursively replace the exit basic block of the region.
  246. ///
  247. /// This function replaces the exit basic block with a new basic block. It
  248. /// also updates all child regions that have the same exit basic block as
  249. /// this region.
  250. ///
  251. /// @param NewExit The new exit basic block.
  252. void replaceExitRecursive(BasicBlock *NewExit);
  253. /// @brief Get the exit BasicBlock of the Region.
  254. /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
  255. /// Region.
  256. BasicBlock *getExit() const { return exit; }
  257. /// @brief Get the parent of the Region.
  258. /// @return The parent of the Region or NULL if this is a top level
  259. /// Region.
  260. Region *getParent() const { return RegionNode::getParent(); }
  261. /// @brief Get the RegionNode representing the current Region.
  262. /// @return The RegionNode representing the current Region.
  263. RegionNode* getNode() const {
  264. return const_cast<RegionNode*>(reinterpret_cast<const RegionNode*>(this));
  265. }
  266. /// @brief Get the nesting level of this Region.
  267. ///
  268. /// An toplevel Region has depth 0.
  269. ///
  270. /// @return The depth of the region.
  271. unsigned getDepth() const;
  272. /// @brief Check if a Region is the TopLevel region.
  273. ///
  274. /// The toplevel region represents the whole function.
  275. bool isTopLevelRegion() const { return exit == NULL; }
  276. /// @brief Return a new (non canonical) region, that is obtained by joining
  277. /// this region with its predecessors.
  278. ///
  279. /// @return A region also starting at getEntry(), but reaching to the next
  280. /// basic block that forms with getEntry() a (non canonical) region.
  281. /// NULL if such a basic block does not exist.
  282. Region *getExpandedRegion() const;
  283. /// @brief Return the first block of this region's single entry edge,
  284. /// if existing.
  285. ///
  286. /// @return The BasicBlock starting this region's single entry edge,
  287. /// else NULL.
  288. BasicBlock *getEnteringBlock() const;
  289. /// @brief Return the first block of this region's single exit edge,
  290. /// if existing.
  291. ///
  292. /// @return The BasicBlock starting this region's single exit edge,
  293. /// else NULL.
  294. BasicBlock *getExitingBlock() const;
  295. /// @brief Is this a simple region?
  296. ///
  297. /// A region is simple if it has exactly one exit and one entry edge.
  298. ///
  299. /// @return True if the Region is simple.
  300. bool isSimple() const;
  301. /// @brief Returns the name of the Region.
  302. /// @return The Name of the Region.
  303. std::string getNameStr() const;
  304. /// @brief Return the RegionInfo object, that belongs to this Region.
  305. RegionInfo *getRegionInfo() const {
  306. return RI;
  307. }
  308. /// PrintStyle - Print region in difference ways.
  309. enum PrintStyle { PrintNone, PrintBB, PrintRN };
  310. /// @brief Print the region.
  311. ///
  312. /// @param OS The output stream the Region is printed to.
  313. /// @param printTree Print also the tree of subregions.
  314. /// @param level The indentation level used for printing.
  315. void print(raw_ostream& OS, bool printTree = true, unsigned level = 0,
  316. enum PrintStyle Style = PrintNone) const;
  317. /// @brief Print the region to stderr.
  318. void dump() const;
  319. /// @brief Check if the region contains a BasicBlock.
  320. ///
  321. /// @param BB The BasicBlock that might be contained in this Region.
  322. /// @return True if the block is contained in the region otherwise false.
  323. bool contains(const BasicBlock *BB) const;
  324. /// @brief Check if the region contains another region.
  325. ///
  326. /// @param SubRegion The region that might be contained in this Region.
  327. /// @return True if SubRegion is contained in the region otherwise false.
  328. bool contains(const Region *SubRegion) const {
  329. // Toplevel Region.
  330. if (!getExit())
  331. return true;
  332. return contains(SubRegion->getEntry())
  333. && (contains(SubRegion->getExit()) || SubRegion->getExit() == getExit());
  334. }
  335. /// @brief Check if the region contains an Instruction.
  336. ///
  337. /// @param Inst The Instruction that might be contained in this region.
  338. /// @return True if the Instruction is contained in the region otherwise false.
  339. bool contains(const Instruction *Inst) const {
  340. return contains(Inst->getParent());
  341. }
  342. /// @brief Check if the region contains a loop.
  343. ///
  344. /// @param L The loop that might be contained in this region.
  345. /// @return True if the loop is contained in the region otherwise false.
  346. /// In case a NULL pointer is passed to this function the result
  347. /// is false, except for the region that describes the whole function.
  348. /// In that case true is returned.
  349. bool contains(const Loop *L) const;
  350. /// @brief Get the outermost loop in the region that contains a loop.
  351. ///
  352. /// Find for a Loop L the outermost loop OuterL that is a parent loop of L
  353. /// and is itself contained in the region.
  354. ///
  355. /// @param L The loop the lookup is started.
  356. /// @return The outermost loop in the region, NULL if such a loop does not
  357. /// exist or if the region describes the whole function.
  358. Loop *outermostLoopInRegion(Loop *L) const;
  359. /// @brief Get the outermost loop in the region that contains a basic block.
  360. ///
  361. /// Find for a basic block BB the outermost loop L that contains BB and is
  362. /// itself contained in the region.
  363. ///
  364. /// @param LI A pointer to a LoopInfo analysis.
  365. /// @param BB The basic block surrounded by the loop.
  366. /// @return The outermost loop in the region, NULL if such a loop does not
  367. /// exist or if the region describes the whole function.
  368. Loop *outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const;
  369. /// @brief Get the subregion that starts at a BasicBlock
  370. ///
  371. /// @param BB The BasicBlock the subregion should start.
  372. /// @return The Subregion if available, otherwise NULL.
  373. Region* getSubRegionNode(BasicBlock *BB) const;
  374. /// @brief Get the RegionNode for a BasicBlock
  375. ///
  376. /// @param BB The BasicBlock at which the RegionNode should start.
  377. /// @return If available, the RegionNode that represents the subregion
  378. /// starting at BB. If no subregion starts at BB, the RegionNode
  379. /// representing BB.
  380. RegionNode* getNode(BasicBlock *BB) const;
  381. /// @brief Get the BasicBlock RegionNode for a BasicBlock
  382. ///
  383. /// @param BB The BasicBlock for which the RegionNode is requested.
  384. /// @return The RegionNode representing the BB.
  385. RegionNode* getBBNode(BasicBlock *BB) const;
  386. /// @brief Add a new subregion to this Region.
  387. ///
  388. /// @param SubRegion The new subregion that will be added.
  389. /// @param moveChildren Move the children of this region, that are also
  390. /// contained in SubRegion into SubRegion.
  391. void addSubRegion(Region *SubRegion, bool moveChildren = false);
  392. /// @brief Remove a subregion from this Region.
  393. ///
  394. /// The subregion is not deleted, as it will probably be inserted into another
  395. /// region.
  396. /// @param SubRegion The SubRegion that will be removed.
  397. Region *removeSubRegion(Region *SubRegion);
  398. /// @brief Move all direct child nodes of this Region to another Region.
  399. ///
  400. /// @param To The Region the child nodes will be transferred to.
  401. void transferChildrenTo(Region *To);
  402. /// @brief Verify if the region is a correct region.
  403. ///
  404. /// Check if this is a correctly build Region. This is an expensive check, as
  405. /// the complete CFG of the Region will be walked.
  406. void verifyRegion() const;
  407. /// @brief Clear the cache for BB RegionNodes.
  408. ///
  409. /// After calling this function the BasicBlock RegionNodes will be stored at
  410. /// different memory locations. RegionNodes obtained before this function is
  411. /// called are therefore not comparable to RegionNodes abtained afterwords.
  412. void clearNodeCache();
  413. /// @name Subregion Iterators
  414. ///
  415. /// These iterators iterator over all subregions of this Region.
  416. //@{
  417. typedef RegionSet::iterator iterator;
  418. typedef RegionSet::const_iterator const_iterator;
  419. iterator begin() { return children.begin(); }
  420. iterator end() { return children.end(); }
  421. const_iterator begin() const { return children.begin(); }
  422. const_iterator end() const { return children.end(); }
  423. //@}
  424. /// @name BasicBlock Iterators
  425. ///
  426. /// These iterators iterate over all BasicBlocks that are contained in this
  427. /// Region. The iterator also iterates over BasicBlocks that are elements of
  428. /// a subregion of this Region. It is therefore called a flat iterator.
  429. //@{
  430. template <bool IsConst>
  431. class block_iterator_wrapper
  432. : public df_iterator<typename conditional<IsConst,
  433. const BasicBlock,
  434. BasicBlock>::type*> {
  435. typedef df_iterator<typename conditional<IsConst,
  436. const BasicBlock,
  437. BasicBlock>::type*>
  438. super;
  439. public:
  440. typedef block_iterator_wrapper<IsConst> Self;
  441. typedef typename super::pointer pointer;
  442. // Construct the begin iterator.
  443. block_iterator_wrapper(pointer Entry, pointer Exit) : super(df_begin(Entry))
  444. {
  445. // Mark the exit of the region as visited, so that the children of the
  446. // exit and the exit itself, i.e. the block outside the region will never
  447. // be visited.
  448. super::Visited.insert(Exit);
  449. }
  450. // Construct the end iterator.
  451. block_iterator_wrapper() : super(df_end<pointer>((BasicBlock *)0)) {}
  452. /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
  453. // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
  454. // This was introduced for backwards compatibility, but should
  455. // be removed as soon as all users are fixed.
  456. BasicBlock *operator*() const {
  457. return const_cast<BasicBlock*>(super::operator*());
  458. }
  459. };
  460. typedef block_iterator_wrapper<false> block_iterator;
  461. typedef block_iterator_wrapper<true> const_block_iterator;
  462. block_iterator block_begin() {
  463. return block_iterator(getEntry(), getExit());
  464. }
  465. block_iterator block_end() {
  466. return block_iterator();
  467. }
  468. const_block_iterator block_begin() const {
  469. return const_block_iterator(getEntry(), getExit());
  470. }
  471. const_block_iterator block_end() const {
  472. return const_block_iterator();
  473. }
  474. //@}
  475. /// @name Element Iterators
  476. ///
  477. /// These iterators iterate over all BasicBlock and subregion RegionNodes that
  478. /// are direct children of this Region. It does not iterate over any
  479. /// RegionNodes that are also element of a subregion of this Region.
  480. //@{
  481. typedef df_iterator<RegionNode*, SmallPtrSet<RegionNode*, 8>, false,
  482. GraphTraits<RegionNode*> > element_iterator;
  483. typedef df_iterator<const RegionNode*, SmallPtrSet<const RegionNode*, 8>,
  484. false, GraphTraits<const RegionNode*> >
  485. const_element_iterator;
  486. element_iterator element_begin();
  487. element_iterator element_end();
  488. const_element_iterator element_begin() const;
  489. const_element_iterator element_end() const;
  490. //@}
  491. };
  492. //===----------------------------------------------------------------------===//
  493. /// @brief Analysis that detects all canonical Regions.
  494. ///
  495. /// The RegionInfo pass detects all canonical regions in a function. The Regions
  496. /// are connected using the parent relation. This builds a Program Structure
  497. /// Tree.
  498. class RegionInfo : public FunctionPass {
  499. typedef DenseMap<BasicBlock*,BasicBlock*> BBtoBBMap;
  500. typedef DenseMap<BasicBlock*, Region*> BBtoRegionMap;
  501. typedef SmallPtrSet<Region*, 4> RegionSet;
  502. RegionInfo(const RegionInfo &) LLVM_DELETED_FUNCTION;
  503. const RegionInfo &operator=(const RegionInfo &) LLVM_DELETED_FUNCTION;
  504. DominatorTree *DT;
  505. PostDominatorTree *PDT;
  506. DominanceFrontier *DF;
  507. /// The top level region.
  508. Region *TopLevelRegion;
  509. /// Map every BB to the smallest region, that contains BB.
  510. BBtoRegionMap BBtoRegion;
  511. // isCommonDomFrontier - Returns true if BB is in the dominance frontier of
  512. // entry, because it was inherited from exit. In the other case there is an
  513. // edge going from entry to BB without passing exit.
  514. bool isCommonDomFrontier(BasicBlock* BB, BasicBlock* entry,
  515. BasicBlock* exit) const;
  516. // isRegion - Check if entry and exit surround a valid region, based on
  517. // dominance tree and dominance frontier.
  518. bool isRegion(BasicBlock* entry, BasicBlock* exit) const;
  519. // insertShortCut - Saves a shortcut pointing from entry to exit.
  520. // This function may extend this shortcut if possible.
  521. void insertShortCut(BasicBlock* entry, BasicBlock* exit,
  522. BBtoBBMap* ShortCut) const;
  523. // getNextPostDom - Returns the next BB that postdominates N, while skipping
  524. // all post dominators that cannot finish a canonical region.
  525. DomTreeNode *getNextPostDom(DomTreeNode* N, BBtoBBMap *ShortCut) const;
  526. // isTrivialRegion - A region is trivial, if it contains only one BB.
  527. bool isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const;
  528. // createRegion - Creates a single entry single exit region.
  529. Region *createRegion(BasicBlock *entry, BasicBlock *exit);
  530. // findRegionsWithEntry - Detect all regions starting with bb 'entry'.
  531. void findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut);
  532. // scanForRegions - Detects regions in F.
  533. void scanForRegions(Function &F, BBtoBBMap *ShortCut);
  534. // getTopMostParent - Get the top most parent with the same entry block.
  535. Region *getTopMostParent(Region *region);
  536. // buildRegionsTree - build the region hierarchy after all region detected.
  537. void buildRegionsTree(DomTreeNode *N, Region *region);
  538. // Calculate - detecte all regions in function and build the region tree.
  539. void Calculate(Function& F);
  540. void releaseMemory();
  541. // updateStatistics - Update statistic about created regions.
  542. void updateStatistics(Region *R);
  543. // isSimple - Check if a region is a simple region with exactly one entry
  544. // edge and exactly one exit edge.
  545. bool isSimple(Region* R) const;
  546. public:
  547. static char ID;
  548. explicit RegionInfo();
  549. ~RegionInfo();
  550. /// @name FunctionPass interface
  551. //@{
  552. virtual bool runOnFunction(Function &F);
  553. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  554. virtual void print(raw_ostream &OS, const Module *) const;
  555. virtual void verifyAnalysis() const;
  556. //@}
  557. /// @brief Get the smallest region that contains a BasicBlock.
  558. ///
  559. /// @param BB The basic block.
  560. /// @return The smallest region, that contains BB or NULL, if there is no
  561. /// region containing BB.
  562. Region *getRegionFor(BasicBlock *BB) const;
  563. /// @brief Set the smallest region that surrounds a basic block.
  564. ///
  565. /// @param BB The basic block surrounded by a region.
  566. /// @param R The smallest region that surrounds BB.
  567. void setRegionFor(BasicBlock *BB, Region *R);
  568. /// @brief A shortcut for getRegionFor().
  569. ///
  570. /// @param BB The basic block.
  571. /// @return The smallest region, that contains BB or NULL, if there is no
  572. /// region containing BB.
  573. Region *operator[](BasicBlock *BB) const;
  574. /// @brief Return the exit of the maximal refined region, that starts at a
  575. /// BasicBlock.
  576. ///
  577. /// @param BB The BasicBlock the refined region starts.
  578. BasicBlock *getMaxRegionExit(BasicBlock *BB) const;
  579. /// @brief Find the smallest region that contains two regions.
  580. ///
  581. /// @param A The first region.
  582. /// @param B The second region.
  583. /// @return The smallest region containing A and B.
  584. Region *getCommonRegion(Region* A, Region *B) const;
  585. /// @brief Find the smallest region that contains two basic blocks.
  586. ///
  587. /// @param A The first basic block.
  588. /// @param B The second basic block.
  589. /// @return The smallest region that contains A and B.
  590. Region* getCommonRegion(BasicBlock* A, BasicBlock *B) const {
  591. return getCommonRegion(getRegionFor(A), getRegionFor(B));
  592. }
  593. /// @brief Find the smallest region that contains a set of regions.
  594. ///
  595. /// @param Regions A vector of regions.
  596. /// @return The smallest region that contains all regions in Regions.
  597. Region* getCommonRegion(SmallVectorImpl<Region*> &Regions) const;
  598. /// @brief Find the smallest region that contains a set of basic blocks.
  599. ///
  600. /// @param BBs A vector of basic blocks.
  601. /// @return The smallest region that contains all basic blocks in BBS.
  602. Region* getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const;
  603. Region *getTopLevelRegion() const {
  604. return TopLevelRegion;
  605. }
  606. /// @brief Update RegionInfo after a basic block was split.
  607. ///
  608. /// @param NewBB The basic block that was created before OldBB.
  609. /// @param OldBB The old basic block.
  610. void splitBlock(BasicBlock* NewBB, BasicBlock *OldBB);
  611. /// @brief Clear the Node Cache for all Regions.
  612. ///
  613. /// @see Region::clearNodeCache()
  614. void clearNodeCache() {
  615. if (TopLevelRegion)
  616. TopLevelRegion->clearNodeCache();
  617. }
  618. };
  619. inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node) {
  620. if (Node.isSubRegion())
  621. return OS << Node.getNodeAs<Region>()->getNameStr();
  622. else
  623. return OS << Node.getNodeAs<BasicBlock>()->getName();
  624. }
  625. } // End llvm namespace
  626. #endif