llvm.org GIT mirror llvm / 5e1c96a
Templatify RegionInfo so it works on MachineBasicBlocks git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213456 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 5 years ago
12 changed file(s) with 1745 addition(s) and 1030 deletion(s). Raw diff Collapse all Expand all
; { ; &Node); &Node) {
3636 #ifndef LLVM_ANALYSIS_REGIONINFO_H
3737 #define LLVM_ANALYSIS_REGIONINFO_H
3838
39 #include "llvm/ADT/DepthFirstIterator.h"
3940 #include "llvm/ADT/PointerIntPair.h"
40 #include "llvm/ADT/iterator_range.h"
41 #include "llvm/Analysis/DominanceFrontier.h"
42 #include "llvm/Analysis/PostDominators.h"
43 #include "llvm/Support/Allocator.h"
41 #include "llvm/IR/CFG.h"
42 #include "llvm/IR/Dominators.h"
4443 #include
4544 #include
45 #include
4646
4747 namespace llvm {
4848
49 class Region;
50 class RegionInfo;
51 class raw_ostream;
49 // RegionTraits - Class to be specialized for different users of RegionInfo
50 // (i.e. BasicBlocks or MachineBasicBlocks). This is only to avoid needing to
51 // pass around an unreasonable number of template parameters.
52 template
53 struct RegionTraits {
54 // FuncT
55 // BlockT
56 // RegionT
57 // RegionNodeT
58 // RegionInfoT
59 typedef typename FuncT_::UnknownRegionTypeError BrokenT;
60 };
61
62 class DominatorTree;
63 class DominanceFrontier;
5264 class Loop;
5365 class LoopInfo;
66 struct PostDominatorTree;
67 class raw_ostream;
68 class Region;
69 template
70 class RegionBase;
71 class RegionNode;
72 class RegionInfo;
73 template
74 class RegionInfoBase;
75
76 template <>
77 struct RegionTraits {
78 typedef Function FuncT;
79 typedef BasicBlock BlockT;
80 typedef Region RegionT;
81 typedef RegionNode RegionNodeT;
82 typedef RegionInfo RegionInfoT;
83 typedef DominatorTree DomTreeT;
84 typedef DomTreeNode DomTreeNodeT;
85 typedef DominanceFrontier DomFrontierT;
86 typedef PostDominatorTree PostDomTreeT;
87 typedef Instruction InstT;
88 typedef Loop LoopT;
89 typedef LoopInfo LoopInfoT;
90
91 static unsigned getNumSuccessors(BasicBlock *BB) {
92 return BB->getTerminator()->getNumSuccessors();
93 }
94 };
5495
5596 /// @brief Marker class to iterate over the elements of a Region in flat mode.
5697 ///
64105
65106 /// @brief A RegionNode represents a subregion or a BasicBlock that is part of a
66107 /// Region.
67 class RegionNode {
68 RegionNode(const RegionNode &) LLVM_DELETED_FUNCTION;
69 const RegionNode &operator=(const RegionNode &) LLVM_DELETED_FUNCTION;
70
71 protected:
108 template
109 class RegionNodeBase {
110 friend class RegionBase
111
112 public:
113 typedef typename Tr::BlockT BlockT;
114 typedef typename Tr::RegionT RegionT;
115
116 private:
117 RegionNodeBase(const RegionNodeBase &) LLVM_DELETED_FUNCTION;
118 const RegionNodeBase &operator=(const RegionNodeBase &) LLVM_DELETED_FUNCTION;
119
72120 /// This is the entry basic block that starts this region node. If this is a
73121 /// BasicBlock RegionNode, then entry is just the basic block, that this
74122 /// RegionNode represents. Otherwise it is the entry of this (Sub)RegionNode.
79127 /// The node can hold either a Region or a BasicBlock.
80128 /// Use one bit to save, if this RegionNode is a subregion or BasicBlock
81129 /// RegionNode.
82 PointerIntPairasicBlock*, 1, bool> entry;
130 PointerIntPairlockT *, 1, bool> entry;
83131
84132 /// @brief The parent Region of this RegionNode.
85133 /// @see getParent()
86 Region* parent;
87
88 public:
134 RegionT *parent;
135
136 protected:
89137 /// @brief Create a RegionNode.
90138 ///
91139 /// @param Parent The parent of this RegionNode.
94142 /// BasicBlock itself. If it represents a subregion, this
95143 /// is the entry BasicBlock of the subregion.
96144 /// @param isSubRegion If this RegionNode represents a SubRegion.
97 inline RegionNode(Region* Parent, BasicBlock* Entry, bool isSubRegion = 0)
98 : entry(Entry, isSubRegion), parent(Parent) {}
99
145 inline RegionNodeBase(RegionT *Parent, BlockT *Entry,
146 bool isSubRegion = false)
147 : entry(Entry, isSubRegion), parent(Parent) {}
148
149 public:
100150 /// @brief Get the parent Region of this RegionNode.
101151 ///
102152 /// The parent Region is the Region this RegionNode belongs to. If for
105155 /// pointing to the Region this RegionNode belongs to.
106156 ///
107157 /// @return Get the parent Region of this RegionNode.
108 inline Region* getParent() const { return parent; }
158 inline RegionT *getParent() const { return parent; }
109159
110160 /// @brief Get the entry BasicBlock of this RegionNode.
111161 ///
113163 /// itself, otherwise we return the entry BasicBlock of the Subregion
114164 ///
115165 /// @return The entry BasicBlock of this RegionNode.
116 inline BasicBlock* getEntry() const { return entry.getPointer(); }
166 inline BlockT *getEntry() const { return entry.getPointer(); }
117167
118168 /// @brief Get the content of this RegionNode.
119169 ///
121171 /// check the type of the content with the isSubRegion() function call.
122172 ///
123173 /// @return The content of this RegionNode.
124 template
125 inline T* getNodeAs() const;
174 template inline T *getNodeAs() const;
126175
127176 /// @brief Is this RegionNode a subregion?
128177 ///
129178 /// @return True if it contains a subregion. False if it contains a
130179 /// BasicBlock.
131 inline bool isSubRegion() const {
132 return entry.getInt();
133 }
180 inline bool isSubRegion() const { return entry.getInt(); }
134181 };
135
136 /// Print a RegionNode.
137 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node);
138
139 template<>
140 inline BasicBlock* RegionNode::getNodeAs() const {
141 assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
142 return getEntry();
143 }
144
145 template<>
146 inline Region* RegionNode::getNodeAs() const {
147 assert(isSubRegion() && "This is not a subregion RegionNode!");
148 return reinterpret_cast(const_cast(this));
149 }
150182
151183 //===----------------------------------------------------------------------===//
152184 /// @brief A single entry single exit Region.
210242 ///
211243 /// The first call returns a textual representation of the program structure
212244 /// tree, the second one creates a graphical representation using graphviz.
213 class Region : public RegionNode {
214 friend class RegionInfo;
215 Region(const Region &) LLVM_DELETED_FUNCTION;
216 const Region &operator=(const Region &) LLVM_DELETED_FUNCTION;
245 template
246 class RegionBase : public RegionNodeBase
247 typedef typename Tr::FuncT FuncT;
248 typedef typename Tr::BlockT BlockT;
249 typedef typename Tr::RegionInfoT RegionInfoT;
250 typedef typename Tr::RegionT RegionT;
251 typedef typename Tr::RegionNodeT RegionNodeT;
252 typedef typename Tr::DomTreeT DomTreeT;
253 typedef typename Tr::LoopT LoopT;
254 typedef typename Tr::LoopInfoT LoopInfoT;
255 typedef typename Tr::InstT InstT;
256
257 typedef GraphTraits BlockTraits;
258 typedef GraphTraits> InvBlockTraits;
259 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
260 typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
261
262 friend class RegionInfoBase
263 RegionBase(const RegionBase &) LLVM_DELETED_FUNCTION;
264 const RegionBase &operator=(const RegionBase &) LLVM_DELETED_FUNCTION;
217265
218266 // Information necessary to manage this Region.
219 RegionInfo* RI;
220 DominatorTree *DT;
267 RegionInfoT *RI;
268 DomTreeT *DT;
221269
222270 // The exit BasicBlock of this region.
223271 // (The entry BasicBlock is part of RegionNode)
224 BasicBlock *exit;
225
226 typedef std::vector> RegionSet;
272 BlockT *exit;
273
274 typedef std::vector> RegionSet;
227275
228276 // The subregions of this region.
229277 RegionSet children;
230278
231 typedef std::mapasicBlock*, RegionNode*> BBNodeMapT;
279 typedef std::maplockT *, RegionNodeT *> BBNodeMapT;
232280
233281 // Save the BasicBlock RegionNodes that are element of this Region.
234282 mutable BBNodeMapT BBNodeMap;
235283
236284 /// verifyBBInRegion - Check if a BB is in this Region. This check also works
237285 /// if the region is incorrectly built. (EXPENSIVE!)
238 void verifyBBInRegion(BasicBlock* BB) const;
286 void verifyBBInRegion(BlockT *BB) const;
239287
240288 /// verifyWalk - Walk over all the BBs of the region starting from BB and
241289 /// verify that all reachable basic blocks are elements of the region.
242290 /// (EXPENSIVE!)
243 void verifyWalk(BasicBlock* BB, std::set* visitedBB) const;
291 void verifyWalk(BlockT *BB, std::set *visitedBB) const;
244292
245293 /// verifyRegionNest - Verify if the region and its children are valid
246294 /// regions (EXPENSIVE!)
255303 /// @param DT The dominator tree of the current function.
256304 /// @param Parent The surrounding region or NULL if this is a top level
257305 /// region.
258 Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RI,
259 DominatorTree *DT, Region *Parent = nullptr);
306 RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT,
307 RegionT *Parent = nullptr);
260308
261309 /// Delete the Region and all its subregions.
262 ~Region();
310 ~RegionBase();
263311
264312 /// @brief Get the entry BasicBlock of the Region.
265313 /// @return The entry BasicBlock of the region.
266 BasicBlock *getEntry() const { return RegionNode::getEntry(); }
314 BlockT *getEntry() const { return RegionNodeT::getEntry(); }
267315
268316 /// @brief Replace the entry basic block of the region with the new basic
269317 /// block.
270318 ///
271319 /// @param BB The new entry basic block of the region.
272 void replaceEntry(BasicBlock *BB);
320 void replaceEntry(BlockT *BB);
273321
274322 /// @brief Replace the exit basic block of the region with the new basic
275323 /// block.
276324 ///
277325 /// @param BB The new exit basic block of the region.
278 void replaceExit(BasicBlock *BB);
326 void replaceExit(BlockT *BB);
279327
280328 /// @brief Recursively replace the entry basic block of the region.
281329 ///
284332 /// this region.
285333 ///
286334 /// @param NewEntry The new entry basic block.
287 void replaceEntryRecursive(BasicBlock *NewEntry);
335 void replaceEntryRecursive(BlockT *NewEntry);
288336
289337 /// @brief Recursively replace the exit basic block of the region.
290338 ///
293341 /// this region.
294342 ///
295343 /// @param NewExit The new exit basic block.
296 void replaceExitRecursive(BasicBlock *NewExit);
344 void replaceExitRecursive(BlockT *NewExit);
297345
298346 /// @brief Get the exit BasicBlock of the Region.
299347 /// @return The exit BasicBlock of the Region, NULL if this is the TopLevel
300348 /// Region.
301 BasicBlock *getExit() const { return exit; }
349 BlockT *getExit() const { return exit; }
302350
303351 /// @brief Get the parent of the Region.
304352 /// @return The parent of the Region or NULL if this is a top level
305353 /// Region.
306 Region *getParent() const { return RegionNode::getParent(); }
354 RegionT *getParent() const { return RegionNodeT::getParent(); }
307355
308356 /// @brief Get the RegionNode representing the current Region.
309357 /// @return The RegionNode representing the current Region.
310 RegionNode* getNode() const {
311 return const_cast(reinterpret_cast(this));
358 RegionNodeT *getNode() const {
359 return const_cast(
360 reinterpret_cast(this));
312361 }
313362
314363 /// @brief Get the nesting level of this Region.
329378 /// @return A region also starting at getEntry(), but reaching to the next
330379 /// basic block that forms with getEntry() a (non-canonical) region.
331380 /// NULL if such a basic block does not exist.
332 Region *getExpandedRegion() const;
381 RegionT *getExpandedRegion() const;
333382
334383 /// @brief Return the first block of this region's single entry edge,
335384 /// if existing.
336385 ///
337386 /// @return The BasicBlock starting this region's single entry edge,
338387 /// else NULL.
339 BasicBlock *getEnteringBlock() const;
388 BlockT *getEnteringBlock() const;
340389
341390 /// @brief Return the first block of this region's single exit edge,
342391 /// if existing.
343392 ///
344393 /// @return The BasicBlock starting this region's single exit edge,
345394 /// else NULL.
346 BasicBlock *getExitingBlock() const;
395 BlockT *getExitingBlock() const;
347396
348397 /// @brief Is this a simple region?
349398 ///
357406 std::string getNameStr() const;
358407
359408 /// @brief Return the RegionInfo object, that belongs to this Region.
360 RegionInfo *getRegionInfo() const {
361 return RI;
362 }
409 RegionInfoT *getRegionInfo() const { return RI; }
363410
364411 /// PrintStyle - Print region in difference ways.
365 enum PrintStyle { PrintNone, PrintBB, PrintRN };
412 enum PrintStyle { PrintNone, PrintBB, PrintRN };
366413
367414 /// @brief Print the region.
368415 ///
369416 /// @param OS The output stream the Region is printed to.
370417 /// @param printTree Print also the tree of subregions.
371418 /// @param level The indentation level used for printing.
372 void print(raw_ostream& OS, bool printTree = true, unsigned level = 0,
373 enum PrintStyle Style = PrintNone) const;
419 void print(raw_ostream &OS, bool printTree = true, unsigned level = 0,
420 PrintStyle Style = PrintNone) const;
374421
375422 /// @brief Print the region to stderr.
376423 void dump() const;
379426 ///
380427 /// @param BB The BasicBlock that might be contained in this Region.
381428 /// @return True if the block is contained in the region otherwise false.
382 bool contains(const BasicBlock *BB) const;
429 bool contains(const BlockT *BB) const;
383430
384431 /// @brief Check if the region contains another region.
385432 ///
386433 /// @param SubRegion The region that might be contained in this Region.
387434 /// @return True if SubRegion is contained in the region otherwise false.
388 bool contains(const Region *SubRegion) const {
435 bool contains(const RegionT *SubRegion) const {
389436 // Toplevel Region.
390437 if (!getExit())
391438 return true;
392439
393 return contains(SubRegion->getEntry())
394 && (contains(SubRegion->getExit()) || SubRegion->getExit() == getExit());
440 return contains(SubRegion->getEntry()) &&
441 (contains(SubRegion->getExit()) ||
442 SubRegion->getExit() == getExit());
395443 }
396444
397445 /// @brief Check if the region contains an Instruction.
398446 ///
399447 /// @param Inst The Instruction that might be contained in this region.
400 /// @return True if the Instruction is contained in the region otherwise false.
401 bool contains(const Instruction *Inst) const {
402 return contains(Inst->getParent());
403 }
448 /// @return True if the Instruction is contained in the region otherwise
449 /// false.
450 bool contains(const InstT *Inst) const { return contains(Inst->getParent()); }
404451
405452 /// @brief Check if the region contains a loop.
406453 ///
409456 /// In case a NULL pointer is passed to this function the result
410457 /// is false, except for the region that describes the whole function.
411458 /// In that case true is returned.
412 bool contains(const Loop *L) const;
459 bool contains(const LoopT *L) const;
413460
414461 /// @brief Get the outermost loop in the region that contains a loop.
415462 ///
419466 /// @param L The loop the lookup is started.
420467 /// @return The outermost loop in the region, NULL if such a loop does not
421468 /// exist or if the region describes the whole function.
422 Loop *outermostLoopInRegion(Loop *L) const;
469 LoopT *outermostLoopInRegion(LoopT *L) const;
423470
424471 /// @brief Get the outermost loop in the region that contains a basic block.
425472 ///
430477 /// @param BB The basic block surrounded by the loop.
431478 /// @return The outermost loop in the region, NULL if such a loop does not
432479 /// exist or if the region describes the whole function.
433 Loop *outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const;
480 LoopT *outermostLoopInRegion(LoopInfoT *LI, BlockT *BB) const;
434481
435482 /// @brief Get the subregion that starts at a BasicBlock
436483 ///
437484 /// @param BB The BasicBlock the subregion should start.
438485 /// @return The Subregion if available, otherwise NULL.
439 Region* getSubRegionNode(BasicBlock *BB) const;
486 RegionT *getSubRegionNode(BlockT *BB) const;
440487
441488 /// @brief Get the RegionNode for a BasicBlock
442489 ///
444491 /// @return If available, the RegionNode that represents the subregion
445492 /// starting at BB. If no subregion starts at BB, the RegionNode
446493 /// representing BB.
447 RegionNode* getNode(BasicBlock *BB) const;
494 RegionNodeT *getNode(BlockT *BB) const;
448495
449496 /// @brief Get the BasicBlock RegionNode for a BasicBlock
450497 ///
451498 /// @param BB The BasicBlock for which the RegionNode is requested.
452499 /// @return The RegionNode representing the BB.
453 RegionNode* getBBNode(BasicBlock *BB) const;
500 RegionNodeT *getBBNode(BlockT *BB) const;
454501
455502 /// @brief Add a new subregion to this Region.
456503 ///
457504 /// @param SubRegion The new subregion that will be added.
458505 /// @param moveChildren Move the children of this region, that are also
459506 /// contained in SubRegion into SubRegion.
460 void addSubRegion(Region *SubRegion, bool moveChildren = false);
507 void addSubRegion(RegionT *SubRegion, bool moveChildren = false);
461508
462509 /// @brief Remove a subregion from this Region.
463510 ///
464511 /// The subregion is not deleted, as it will probably be inserted into another
465512 /// region.
466513 /// @param SubRegion The SubRegion that will be removed.
467 Region *removeSubRegion(Region *SubRegion);
514 RegionT *removeSubRegion(RegionT *SubRegion);
468515
469516 /// @brief Move all direct child nodes of this Region to another Region.
470517 ///
471518 /// @param To The Region the child nodes will be transferred to.
472 void transferChildrenTo(Region *To);
519 void transferChildrenTo(RegionT *To);
473520
474521 /// @brief Verify if the region is a correct region.
475522 ///
488535 ///
489536 /// These iterators iterator over all subregions of this Region.
490537 //@{
491 typedef RegionSet::iterator iterator;
492 typedef RegionSet::const_iterator const_iterator;
538 typedef typename RegionSet::iterator iterator;
539 typedef typename RegionSet::const_iterator const_iterator;
493540
494541 iterator begin() { return children.begin(); }
495542 iterator end() { return children.end(); }
506553 //@{
507554 template
508555 class block_iterator_wrapper
509 : public df_iterator
510 BasicBlock>::type *> {
511 typedef df_iterator
512 BasicBlock>::type *> super;
556 : public df_iterator<
557 typename std::conditional::type *> {
558 typedef df_iterator<
559 typename std::conditional::type *> super;
513560
514561 public:
515562 typedef block_iterator_wrapper Self;
516563 typedef typename super::pointer pointer;
517564
518565 // Construct the begin iterator.
519 block_iterator_wrapper(pointer Entry, pointer Exit) : super(df_begin(Entry))
520 {
566 block_iterator_wrapper(pointer Entry, pointer Exit)
567 : super(df_begin(Entry)) {
521568 // Mark the exit of the region as visited, so that the children of the
522569 // exit and the exit itself, i.e. the block outside the region will never
523570 // be visited.
525572 }
526573
527574 // Construct the end iterator.
528 block_iterator_wrapper() : super(df_end((BasicBlock *)nullptr)) {}
575 block_iterator_wrapper() : super(df_end((BlockT *)nullptr)) {}
529576
530577 /*implicit*/ block_iterator_wrapper(super I) : super(I) {}
531578
532579 // FIXME: Even a const_iterator returns a non-const BasicBlock pointer.
533580 // This was introduced for backwards compatibility, but should
534581 // be removed as soon as all users are fixed.
535 BasicBlock *operator*() const {
536 return const_cast(super::operator*());
582 BlockT *operator*() const {
583 return const_cast(super::operator*());
537584 }
538585 };
539586
540587 typedef block_iterator_wrapper block_iterator;
541 typedef block_iterator_wrapper const_block_iterator;
542
543 block_iterator block_begin() {
544 return block_iterator(getEntry(), getExit());
545 }
546
547 block_iterator block_end() {
548 return block_iterator();
549 }
588 typedef block_iterator_wrapper const_block_iterator;
589
590 block_iterator block_begin() { return block_iterator(getEntry(), getExit()); }
591
592 block_iterator block_end() { return block_iterator(); }
550593
551594 const_block_iterator block_begin() const {
552595 return const_block_iterator(getEntry(), getExit());
553596 }
554 const_block_iterator block_end() const {
555 return const_block_iterator();
556 }
597 const_block_iterator block_end() const { return const_block_iterator(); }
557598
558599 typedef iterator_range block_range;
559600 typedef iterator_range const_block_range;
577618 /// are direct children of this Region. It does not iterate over any
578619 /// RegionNodes that are also element of a subregion of this Region.
579620 //@{
580 typedef df_iterator, false,
581 GraphTraits > element_iterator;
582
583 typedef df_iterator,
584 false, GraphTraits >
585 const_element_iterator;
621 typedef df_iterator, false,
622 GraphTraits> element_iterator;
623
624 typedef df_iterator,
625 false,
626 GraphTraits> const_element_iterator;
586627
587628 element_iterator element_begin();
588629 element_iterator element_end();
592633 //@}
593634 };
594635
636 /// Print a RegionNode.
637 template
638 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNodeBase
639
595640 //===----------------------------------------------------------------------===//
596641 /// @brief Analysis that detects all canonical Regions.
597642 ///
598643 /// The RegionInfo pass detects all canonical regions in a function. The Regions
599644 /// are connected using the parent relation. This builds a Program Structure
600645 /// Tree.
601 class RegionInfo : public FunctionPass {
602 typedef DenseMap BBtoBBMap;
603 typedef DenseMap BBtoRegionMap;
604 typedef SmallPtrSet RegionSet;
605
606 RegionInfo(const RegionInfo &) LLVM_DELETED_FUNCTION;
607 const RegionInfo &operator=(const RegionInfo &) LLVM_DELETED_FUNCTION;
608
609 DominatorTree *DT;
610 PostDominatorTree *PDT;
611 DominanceFrontier *DF;
646 template
647 class RegionInfoBase {
648 typedef typename Tr::BlockT BlockT;
649 typedef typename Tr::FuncT FuncT;
650 typedef typename Tr::RegionT RegionT;
651 typedef typename Tr::RegionInfoT RegionInfoT;
652 typedef typename Tr::DomTreeT DomTreeT;
653 typedef typename Tr::DomTreeNodeT DomTreeNodeT;
654 typedef typename Tr::PostDomTreeT PostDomTreeT;
655 typedef typename Tr::DomFrontierT DomFrontierT;
656 typedef GraphTraits BlockTraits;
657 typedef GraphTraits> InvBlockTraits;
658 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
659 typedef typename InvBlockTraits::ChildIteratorType PredIterTy;
660
661 friend class RegionInfo;
662 friend class MachineRegionInfo;
663 typedef DenseMap BBtoBBMap;
664 typedef DenseMap BBtoRegionMap;
665 typedef SmallPtrSet RegionSet;
666
667 RegionInfoBase();
668 ~RegionInfoBase();
669
670 RegionInfoBase(const RegionInfoBase &) LLVM_DELETED_FUNCTION;
671 const RegionInfoBase &operator=(const RegionInfoBase &) LLVM_DELETED_FUNCTION;
672
673 DomTreeT *DT;
674 PostDomTreeT *PDT;
675 DomFrontierT *DF;
612676
613677 /// The top level region.
614 Region *TopLevelRegion;
615
678 RegionT *TopLevelRegion;
679
680 private:
616681 /// Map every BB to the smallest region, that contains BB.
617682 BBtoRegionMap BBtoRegion;
618683
619684 // isCommonDomFrontier - Returns true if BB is in the dominance frontier of
620685 // entry, because it was inherited from exit. In the other case there is an
621686 // edge going from entry to BB without passing exit.
622 bool isCommonDomFrontier(BasicBlock* BB, BasicBlock* entry,
623 BasicBlock* exit) const;
687 bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit) const;
624688
625689 // isRegion - Check if entry and exit surround a valid region, based on
626690 // dominance tree and dominance frontier.
627 bool isRegion(BasicBlock* entry, BasicBlock* exit) const;
691 bool isRegion(BlockT *entry, BlockT *exit) const;
628692
629693 // insertShortCut - Saves a shortcut pointing from entry to exit.
630694 // This function may extend this shortcut if possible.
631 void insertShortCut(BasicBlock* entry, BasicBlock* exit,
632 BBtoBBMap* ShortCut) const;
695 void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut) const;
633696
634697 // getNextPostDom - Returns the next BB that postdominates N, while skipping
635698 // all post dominators that cannot finish a canonical region.
636 DomTreeNode *getNextPostDom(DomTreeNode* N, BBtoBBMap *ShortCut) const;
699 DomTreeNodeT *getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const;
637700
638701 // isTrivialRegion - A region is trivial, if it contains only one BB.
639 bool isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const;
702 bool isTrivialRegion(BlockT *entry, BlockT *exit) const;
640703
641704 // createRegion - Creates a single entry single exit region.
642 Region *createRegion(BasicBlock *entry, BasicBlock *exit);
705 RegionT *createRegion(BlockT *entry, BlockT *exit);
643706
644707 // findRegionsWithEntry - Detect all regions starting with bb 'entry'.
645 void findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut);
708 void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
646709
647710 // scanForRegions - Detects regions in F.
648 void scanForRegions(Function &F, BBtoBBMap *ShortCut);
711 void scanForRegions(FuncT &F, BBtoBBMap *ShortCut);
649712
650713 // getTopMostParent - Get the top most parent with the same entry block.
651 Region *getTopMostParent(Region *region);
714 RegionT *getTopMostParent(RegionT *region);
652715
653716 // buildRegionsTree - build the region hierarchy after all region detected.
654 void buildRegionsTree(DomTreeNode *N, Region *region);
655
656 // Calculate - detecte all regions in function and build the region tree.
657 void Calculate(Function& F);
658
659 void releaseMemory() override;
717 void buildRegionsTree(DomTreeNodeT *N, RegionT *region);
660718
661719 // updateStatistics - Update statistic about created regions.
662 void updateStatistics(Region *R);
720 virtual void updateStatistics(RegionT *R) = 0;
663721
664722 // isSimple - Check if a region is a simple region with exactly one entry
665723 // edge and exactly one exit edge.
666 bool isSimple(Region* R) const;
724 bool isSimple(RegionT *R) const;
725
726 // calculate - detect all regions in function and build the region tree.
727 void calculate(FuncT &F);
667728
668729 public:
669 static char ID;
670 explicit RegionInfo();
671
672 ~RegionInfo();
673
674 /// @name FunctionPass interface
675 //@{
676 bool runOnFunction(Function &F) override;
677 void getAnalysisUsage(AnalysisUsage &AU) const override;
678 void print(raw_ostream &OS, const Module *) const override;
679 void verifyAnalysis() const override;
680 //@}
730 static bool VerifyRegionInfo;
731 static typename RegionT::PrintStyle printStyle;
732
733 void print(raw_ostream &OS) const;
734 void dump() const;
735
736 void releaseMemory();
681737
682738 /// @brief Get the smallest region that contains a BasicBlock.
683739 ///
684740 /// @param BB The basic block.
685741 /// @return The smallest region, that contains BB or NULL, if there is no
686742 /// region containing BB.
687 Region *getRegionFor(BasicBlock *BB) const;
743 RegionT *getRegionFor(BlockT *BB) const;
688744
689745 /// @brief Set the smallest region that surrounds a basic block.
690746 ///
691747 /// @param BB The basic block surrounded by a region.
692748 /// @param R The smallest region that surrounds BB.
693 void setRegionFor(BasicBlock *BB, Region *R);
749 void setRegionFor(BlockT *BB, RegionT *R);
694750
695751 /// @brief A shortcut for getRegionFor().
696752 ///
697753 /// @param BB The basic block.
698754 /// @return The smallest region, that contains BB or NULL, if there is no
699755 /// region containing BB.
700 Region *operator[](BasicBlock *BB) const;
756 RegionT *operator[](BlockT *BB) const;
701757
702758 /// @brief Return the exit of the maximal refined region, that starts at a
703759 /// BasicBlock.
704760 ///
705761 /// @param BB The BasicBlock the refined region starts.
706 BasicBlock *getMaxRegionExit(BasicBlock *BB) const;
762 BlockT *getMaxRegionExit(BlockT *BB) const;
707763
708764 /// @brief Find the smallest region that contains two regions.
709765 ///
710766 /// @param A The first region.
711767 /// @param B The second region.
712768 /// @return The smallest region containing A and B.
713 Region *getCommonRegion(Region* A, Region *B) const;
769 RegionT *getCommonRegion(RegionT *A, RegionT *B) const;
714770
715771 /// @brief Find the smallest region that contains two basic blocks.
716772 ///
717773 /// @param A The first basic block.
718774 /// @param B The second basic block.
719775 /// @return The smallest region that contains A and B.
720 Region* getCommonRegion(BasicBlock* A, BasicBlock *B) const {
776 RegionT *getCommonRegion(BlockT *A, BlockT *B) const {
721777 return getCommonRegion(getRegionFor(A), getRegionFor(B));
722778 }
723779
725781 ///
726782 /// @param Regions A vector of regions.
727783 /// @return The smallest region that contains all regions in Regions.
728 Region* getCommonRegion(SmallVectorImpl*> &Regions) const;
784 RegionT *getCommonRegion(SmallVectorImpl*> &Regions) const;
729785
730786 /// @brief Find the smallest region that contains a set of basic blocks.
731787 ///
732788 /// @param BBs A vector of basic blocks.
733789 /// @return The smallest region that contains all basic blocks in BBS.
734 Region* getCommonRegion(SmallVectorImpl &BBs) const;
735
736 Region *getTopLevelRegion() const {
737 return TopLevelRegion;
738 }
790 RegionT *getCommonRegion(SmallVectorImpl &BBs) const;
791
792 RegionT *getTopLevelRegion() const { return TopLevelRegion; }
739793
740794 /// @brief Update RegionInfo after a basic block was split.
741795 ///
742796 /// @param NewBB The basic block that was created before OldBB.
743797 /// @param OldBB The old basic block.
744 void splitBlock(BasicBlock* NewBB, BasicBlock *OldBB);
798 void splitBlock(BlockT *NewBB, BlockT *OldBB);
745799
746800 /// @brief Clear the Node Cache for all Regions.
747801 ///
750804 if (TopLevelRegion)
751805 TopLevelRegion->clearNodeCache();
752806 }
807
808 void verifyAnalysis() const;
753809 };
754810
755 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNode &Node) {
811 class Region;
812
813 class RegionNode : public RegionNodeBase> {
814 public:
815 inline RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion = false)
816 : RegionNodeBase>(Parent, Entry, isSubRegion) {}
817
818 ~RegionNode() {}
819
820 bool operator==(const Region &RN) const {
821 return this == reinterpret_cast(&RN);
822 }
823 };
824
825 class Region : public RegionBase> {
826 public:
827 Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT,
828 Region *Parent = nullptr);
829 ~Region();
830
831 bool operator==(const RegionNode &RN) const {
832 return &RN == reinterpret_cast(this);
833 }
834 };
835
836 class RegionInfo : public RegionInfoBase> {
837 public:
838 explicit RegionInfo();
839
840 virtual ~RegionInfo();
841
842 // updateStatistics - Update statistic about created regions.
843 void updateStatistics(Region *R) final;
844
845 void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT,
846 DominanceFrontier *DF);
847 };
848
849 class RegionInfoPass : public FunctionPass {
850 RegionInfo RI;
851
852 public:
853 static char ID;
854 explicit RegionInfoPass();
855
856 ~RegionInfoPass();
857
858 RegionInfo &getRegionInfo() { return RI; }
859
860 const RegionInfo &getRegionInfo() const { return RI; }
861
862 /// @name FunctionPass interface
863 //@{
864 bool runOnFunction(Function &F) override;
865 void releaseMemory() override;
866 void verifyAnalysis() const override;
867 void getAnalysisUsage(AnalysisUsage &AU) const override;
868 void print(raw_ostream &OS, const Module *) const override;
869 void dump() const;
870 //@}
871 };
872
873 template <>
874 template <>
875 inline BasicBlock *
876 RegionNodeBase>::getNodeAs() const {
877 assert(!isSubRegion() && "This is not a BasicBlock RegionNode!");
878 return getEntry();
879 }
880
881 template <>
882 template <>
883 inline Region *
884 RegionNodeBase>::getNodeAs() const {
885 assert(isSubRegion() && "This is not a subregion RegionNode!");
886 auto Unconst = const_cast> *>(this);
887 return reinterpret_cast(Unconst);
888 }
889
890 template
891 inline raw_ostream &operator<<(raw_ostream &OS,
892 const RegionNodeBase
893 typedef typename Tr::BlockT BlockT;
894 typedef typename Tr::RegionT RegionT;
895
756896 if (Node.isSubRegion())
757 return OS << Node.getNodeAs>()->getNameStr();
897 return OS << Node.template getNodeAs>()->getNameStr();
758898 else
759 return OS << Node.getNodeAs>()->getName();
899 return OS << Node.template getNodeAs>()->getName();
760900 }
901
902 EXTERN_TEMPLATE_INSTANTIATION(class RegionBase>);
903 EXTERN_TEMPLATE_INSTANTIATION(class RegionNodeBase>);
904 EXTERN_TEMPLATE_INSTANTIATION(class RegionInfoBase>);
905
761906 } // End llvm namespace
762907 #endif
763
::RegionBase(BlockT *Entry, BlockT *Exit, (Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {} ::~RegionBase() { ::replaceEntry(BlockT *BB) { ::replaceExit(BlockT *BB) { ::replaceEntryRecursive(BlockT *NewEntry) { ::replaceExitRecursive(BlockT *NewExit) { ::contains(const BlockT *B) const { ::contains(const LoopT *L) const { ::outermostLoopInRegion(LoopT *L) const { ::outermostLoopInRegion(LoopInfoT *LI, ::BlockT *RegionBase::getEnteringBlock() const { ::BlockT *RegionBase::getExitingBlock() const { ::isSimple() const { ::getNameStr() const { ::verifyBBInRegion(BlockT *BB) const { ::verifyWalk(BlockT *BB, std::set *visited) const { ::verifyRegion() const { ::VerifyRegionInfo) ::verifyRegionNest() const { ::element_iterator RegionBase::element_begin() { ::element_iterator RegionBase::element_end() { ::const_element_iterator ::element_begin() const { ::const_element_iterator ::element_end() const { ::getSubRegionNode(BlockT *BB) const { ::getBBNode(BlockT *BB) const { ::getNode(BlockT *BB) const { ::transferChildrenTo(RegionT *To) { ::addSubRegion(RegionT *SubRegion, bool moveChildren) { ::removeSubRegion(RegionT *Child) { ::getDepth() const { ::getExpandedRegion() const { ::print(raw_ostream &OS, bool print_tree, unsigned level, ::dump() const { ::printStyle); ::clearNodeCache() { ::RegionInfoBase() ::~RegionInfoBase() { ::isCommonDomFrontier(BlockT *BB, BlockT *entry, ::isRegion(BlockT *entry, BlockT *exit) const { ::insertShortCut(BlockT *entry, BlockT *exit, ::getNextPostDom(DomTreeNodeT *N, BBtoBBMap *ShortCut) const { ::isTrivialRegion(BlockT *entry, BlockT *exit) const { ::createRegion(BlockT *entry, ::findRegionsWithEntry(BlockT *entry, ::scanForRegions(FuncT &F, BBtoBBMap *ShortCut) { ::getTopMostParent(RegionT *region) { ::buildRegionsTree(DomTreeNodeT *N, RegionT *region) { ::VerifyRegionInfo = true; ::VerifyRegionInfo = false; ::printStyle = ::PrintNone; ::print(raw_ostream &OS) const { ::dump() const { print(dbgs()); } ::releaseMemory() { ::verifyAnalysis() const { ::getRegionFor(BlockT *BB) const { ::setRegionFor(BlockT *BB, RegionT *R) { ::operator[](BlockT *BB) const { ::BlockT * ::getMaxRegionExit(BlockT *BB) const { ::getCommonRegion(RegionT *A, ::getCommonRegion(SmallVectorImpl &Regions) const { ::getCommonRegion(SmallVectorImpl &BBs) const { ::splitBlock(BlockT *NewBB, BlockT *OldBB) { ::calculate(FuncT &F) {
0 //===- RegionInfoImpl.h - SESE region detection analysis --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 // Detects single entry single exit regions in the control flow graph.
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
12 #define LLVM_ANALYSIS_REGIONINFOIMPL_H
13
14 #include "llvm/Analysis/RegionInfo.h"
15 #include "llvm/ADT/PostOrderIterator.h"
16 #include "llvm/Analysis/DominanceFrontier.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/PostDominators.h"
19 #include "llvm/Analysis/RegionIterator.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include
24 #include
25 #include
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "region"
30
31 //===----------------------------------------------------------------------===//
32 /// RegionBase Implementation
33 template
34 RegionBase
35 typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
36 RegionT *Parent)
37 : RegionNodeBase
38
39 template
40 RegionBase
41 // Free the cached nodes.
42 for (typename BBNodeMapT::iterator it = BBNodeMap.begin(),
43 ie = BBNodeMap.end();
44 it != ie; ++it)
45 delete it->second;
46
47 // Only clean the cache for this Region. Caches of child Regions will be
48 // cleaned when the child Regions are deleted.
49 BBNodeMap.clear();
50 }
51
52 template
53 void RegionBase
54 this->entry.setPointer(BB);
55 }
56
57 template
58 void RegionBase
59 assert(exit && "No exit to replace!");
60 exit = BB;
61 }
62
63 template
64 void RegionBase
65 std::vector RegionQueue;
66 BlockT *OldEntry = getEntry();
67
68 RegionQueue.push_back(static_cast(this));
69 while (!RegionQueue.empty()) {
70 RegionT *R = RegionQueue.back();
71 RegionQueue.pop_back();
72
73 R->replaceEntry(NewEntry);
74 for (typename RegionT::const_iterator RI = R->begin(), RE = R->end();
75 RI != RE; ++RI) {
76 if ((*RI)->getEntry() == OldEntry)
77 RegionQueue.push_back(RI->get());
78 }
79 }
80 }
81
82 template
83 void RegionBase
84 std::vector RegionQueue;
85 BlockT *OldExit = getExit();
86
87 RegionQueue.push_back(static_cast(this));
88 while (!RegionQueue.empty()) {
89 RegionT *R = RegionQueue.back();
90 RegionQueue.pop_back();
91
92 R->replaceExit(NewExit);
93 for (typename RegionT::const_iterator RI = R->begin(), RE = R->end();
94 RI != RE; ++RI) {
95 if ((*RI)->getExit() == OldExit)
96 RegionQueue.push_back(RI->get());
97 }
98 }
99 }
100
101 template
102 bool RegionBase
103 BlockT *BB = const_cast(B);
104
105 if (!DT->getNode(BB))
106 return false;
107
108 BlockT *entry = getEntry(), *exit = getExit();
109
110 // Toplevel region.
111 if (!exit)
112 return true;
113
114 return (DT->dominates(entry, BB) &&
115 !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
116 }
117
118 template
119 bool RegionBase
120 // BBs that are not part of any loop are element of the Loop
121 // described by the NULL pointer. This loop is not part of any region,
122 // except if the region describes the whole function.
123 if (!L)
124 return getExit() == nullptr;
125
126 if (!contains(L->getHeader()))
127 return false;
128
129 SmallVector ExitingBlocks;
130 L->getExitingBlocks(ExitingBlocks);
131
132 for (BlockT *BB : ExitingBlocks) {
133 if (!contains(BB))
134 return false;
135 }
136
137 return true;
138 }
139
140 template
141 typename Tr::LoopT *RegionBase
142 if (!contains(L))
143 return nullptr;
144
145 while (L && contains(L->getParentLoop())) {
146 L = L->getParentLoop();
147 }
148
149 return L;
150 }
151
152 template
153 typename Tr::LoopT *RegionBase
154 BlockT *BB) const {
155 assert(LI && BB && "LI and BB cannot be null!");
156 LoopT *L = LI->getLoopFor(BB);
157 return outermostLoopInRegion(L);
158 }
159
160 template
161 typename RegionBase
162 BlockT *entry = getEntry();
163 BlockT *Pred;
164 BlockT *enteringBlock = nullptr;
165
166 for (PredIterTy PI = InvBlockTraits::child_begin(entry),
167 PE = InvBlockTraits::child_end(entry);
168 PI != PE; ++PI) {
169 Pred = *PI;
170 if (DT->getNode(Pred) && !contains(Pred)) {
171 if (enteringBlock)
172 return nullptr;
173
174 enteringBlock = Pred;
175 }
176 }
177
178 return enteringBlock;
179 }
180
181 template
182 typename RegionBase
183 BlockT *exit = getExit();
184 BlockT *Pred;
185 BlockT *exitingBlock = nullptr;
186
187 if (!exit)
188 return nullptr;
189
190 for (PredIterTy PI = InvBlockTraits::child_begin(exit),
191 PE = InvBlockTraits::child_end(exit);
192 PI != PE; ++PI) {
193 Pred = *PI;
194 if (contains(Pred)) {
195 if (exitingBlock)
196 return nullptr;
197
198 exitingBlock = Pred;
199 }
200 }
201
202 return exitingBlock;
203 }
204
205 template
206 bool RegionBase
207 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
208 }
209
210 template
211 std::string RegionBase
212 std::string exitName;
213 std::string entryName;
214
215 if (getEntry()->getName().empty()) {
216 raw_string_ostream OS(entryName);
217
218 getEntry()->printAsOperand(OS, false);
219 } else
220 entryName = getEntry()->getName();
221
222 if (getExit()) {
223 if (getExit()->getName().empty()) {
224 raw_string_ostream OS(exitName);
225
226 getExit()->printAsOperand(OS, false);
227 } else
228 exitName = getExit()->getName();
229 } else
230 exitName = "";
231
232 return entryName + " => " + exitName;
233 }
234
235 template
236 void RegionBase
237 if (!contains(BB))
238 llvm_unreachable("Broken region found!");
239
240 BlockT *entry = getEntry(), *exit = getExit();
241
242 for (SuccIterTy SI = BlockTraits::child_begin(BB),
243 SE = BlockTraits::child_end(BB);
244 SI != SE; ++SI) {
245 if (!contains(*SI) && exit != *SI)
246 llvm_unreachable("Broken region found!");
247 }
248
249 if (entry != BB) {
250 for (PredIterTy SI = InvBlockTraits::child_begin(BB),
251 SE = InvBlockTraits::child_end(BB);
252 SI != SE; ++SI) {
253 if (!contains(*SI))
254 llvm_unreachable("Broken region found!");
255 }
256 }
257 }
258
259 template
260 void RegionBase
261 BlockT *exit = getExit();
262
263 visited->insert(BB);
264
265 verifyBBInRegion(BB);
266
267 for (SuccIterTy SI = BlockTraits::child_begin(BB),
268 SE = BlockTraits::child_end(BB);
269 SI != SE; ++SI) {
270 if (*SI != exit && visited->find(*SI) == visited->end())
271 verifyWalk(*SI, visited);
272 }
273 }
274
275 template
276 void RegionBase
277 // Only do verification when user wants to, otherwise this expensive check
278 // will be invoked by PMDataManager::verifyPreservedAnalysis when
279 // a regionpass (marked PreservedAll) finish.
280 if (!RegionInfoBase
281 return;
282
283 std::set visited;
284 verifyWalk(getEntry(), &visited);
285 }
286
287 template
288 void RegionBase
289 for (typename RegionT::const_iterator RI = begin(), RE = end(); RI != RE;
290 ++RI)
291 (*RI)->verifyRegionNest();
292
293 verifyRegion();
294 }
295
296 template
297 typename RegionBase
298 return GraphTraits::nodes_begin(static_cast(this));
299 }
300
301 template
302 typename RegionBase
303 return GraphTraits::nodes_end(static_cast(this));
304 }
305
306 template
307 typename RegionBase
308 RegionBase
309 return GraphTraits::nodes_begin(
310 static_cast(this));
311 }
312
313 template
314 typename RegionBase
315 RegionBase
316 return GraphTraits::nodes_end(
317 static_cast(this));
318 }
319
320 template
321 typename Tr::RegionT *RegionBase
322 typedef typename Tr::RegionT RegionT;
323 RegionT *R = RI->getRegionFor(BB);
324
325 if (!R || R == this)
326 return nullptr;
327
328 // If we pass the BB out of this region, that means our code is broken.
329 assert(contains(R) && "BB not in current region!");
330
331 while (contains(R->getParent()) && R->getParent() != this)
332 R = R->getParent();
333
334 if (R->getEntry() != BB)
335 return nullptr;
336
337 return R;
338 }
339
340 template
341 typename Tr::RegionNodeT *RegionBase
342 assert(contains(BB) && "Can get BB node out of this region!");
343
344 typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
345
346 if (at != BBNodeMap.end())
347 return at->second;
348
349 auto Deconst = const_cast *>(this);
350 RegionNodeT *NewNode = new RegionNodeT(static_cast(Deconst), BB);
351 BBNodeMap.insert(std::make_pair(BB, NewNode));
352 return NewNode;
353 }
354
355 template
356 typename Tr::RegionNodeT *RegionBase
357 assert(contains(BB) && "Can get BB node out of this region!");
358 if (RegionT *Child = getSubRegionNode(BB))
359 return Child->getNode();
360
361 return getBBNode(BB);
362 }
363
364 template
365 void RegionBase
366 for (iterator I = begin(), E = end(); I != E; ++I) {
367 (*I)->parent = To;
368 To->children.push_back(std::move(*I));
369 }
370 children.clear();
371 }
372
373 template
374 void RegionBase
375 assert(!SubRegion->parent && "SubRegion already has a parent!");
376 assert(std::find_if(begin(), end(), [&](const std::unique_ptr &R) {
377 return R.get() == SubRegion;
378 }) == children.end() &&
379 "Subregion already exists!");
380
381 SubRegion->parent = static_cast(this);
382 children.push_back(std::unique_ptr(SubRegion));
383
384 if (!moveChildren)
385 return;
386
387 assert(SubRegion->children.empty() &&
388 "SubRegions that contain children are not supported");
389
390 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) {
391 if (!(*I)->isSubRegion()) {
392 BlockT *BB = (*I)->template getNodeAs();
393
394 if (SubRegion->contains(BB))
395 RI->setRegionFor(BB, SubRegion);
396 }
397 }
398
399 std::vector> Keep;
400 for (iterator I = begin(), E = end(); I != E; ++I) {
401 if (SubRegion->contains(I->get()) && I->get() != SubRegion) {
402 (*I)->parent = SubRegion;
403 SubRegion->children.push_back(std::move(*I));
404 } else
405 Keep.push_back(std::move(*I));
406 }
407
408 children.clear();
409 children.insert(
410 children.begin(),
411 std::move_iterator(Keep.begin()),
412 std::move_iterator(Keep.end()));
413 }
414
415 template
416 typename Tr::RegionT *RegionBase
417 assert(Child->parent == this && "Child is not a child of this region!");
418 Child->parent = nullptr;
419 typename RegionSet::iterator I = std::find_if(
420 children.begin(), children.end(),
421 [&](const std::unique_ptr &R) { return R.get() == Child; });
422 assert(I != children.end() && "Region does not exit. Unable to remove.");
423 children.erase(children.begin() + (I - begin()));
424 return Child;
425 }
426
427 template
428 unsigned RegionBase
429 unsigned Depth = 0;
430
431 for (RegionT *R = getParent(); R != nullptr; R = R->getParent())
432 ++Depth;
433
434 return Depth;
435 }
436
437 template
438 typename Tr::RegionT *RegionBase
439 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
440
441 if (NumSuccessors == 0)
442 return nullptr;
443
444 for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
445 PE = InvBlockTraits::child_end(getExit());
446 PI != PE; ++PI) {
447 if (!DT->dominates(getEntry(), *PI))
448 return nullptr;
449 }
450
451 RegionT *R = RI->getRegionFor(exit);
452
453 if (R->getEntry() != exit) {
454 if (Tr::getNumSuccessors(exit) == 1)
455 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
456 return nullptr;
457 }
458
459 while (R->getParent() && R->getParent()->getEntry() == exit)
460 R = R->getParent();
461
462 if (!DT->dominates(getEntry(), R->getExit())) {
463 for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
464 PE = InvBlockTraits::child_end(getExit());
465 PI != PE; ++PI) {
466 if (!DT->dominates(R->getExit(), *PI))
467 return nullptr;
468 }
469 }
470
471 return new RegionT(getEntry(), R->getExit(), RI, DT);
472 }
473
474 template
475 void RegionBase
476 PrintStyle Style) const {
477 if (print_tree)
478 OS.indent(level * 2) << '[' << level << "] " << getNameStr();
479 else
480 OS.indent(level * 2) << getNameStr();
481
482 OS << '\n';
483
484 if (Style != PrintNone) {
485 OS.indent(level * 2) << "{\n";
486 OS.indent(level * 2 + 2);
487
488 if (Style == PrintBB) {
489 for (const auto &BB : blocks())
490 OS << BB->getName() << ", "; // TODO: remove the last ","
491 } else if (Style == PrintRN) {
492 for (const_element_iterator I = element_begin(), E = element_end();
493 I != E; ++I) {
494 OS << **I << ", "; // TODO: remove the last ",
495 }
496 }
497
498 OS << '\n';
499 }
500
501 if (print_tree) {
502 for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
503 (*RI)->print(OS, print_tree, level + 1, Style);
504 }
505
506 if (Style != PrintNone)
507 OS.indent(level * 2) << "} \n";
508 }
509
510 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
511 template
512 void RegionBase
513 print(dbgs(), true, getDepth(), RegionInfoBase
514 }
515 #endif
516
517 template
518 void RegionBase
519 // Free the cached nodes.
520 for (typename BBNodeMapT::iterator I = BBNodeMap.begin(),
521 IE = BBNodeMap.end();
522 I != IE; ++I)
523 delete I->second;
524
525 BBNodeMap.clear();
526 for (typename RegionT::iterator RI = begin(), RE = end(); RI != RE; ++RI)
527 (*RI)->clearNodeCache();
528 }
529
530 //===----------------------------------------------------------------------===//
531 // RegionInfoBase implementation
532 //
533
534 template
535 RegionInfoBase
536 : TopLevelRegion(nullptr) {}
537
538 template
539 RegionInfoBase
540 releaseMemory();
541 }
542
543 template
544 bool RegionInfoBase
545 BlockT *exit) const {
546 for (PredIterTy PI = InvBlockTraits::child_begin(BB),
547 PE = InvBlockTraits::child_end(BB);
548 PI != PE; ++PI) {
549 BlockT *P = *PI;
550 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
551 return false;
552 }
553
554 return true;
555 }
556
557 template
558 bool RegionInfoBase
559 assert(entry && exit && "entry and exit must not be null!");
560 typedef typename DomFrontierT::DomSetType DST;
561
562 DST *entrySuccs = &DF->find(entry)->second;
563
564 // Exit is the header of a loop that contains the entry. In this case,
565 // the dominance frontier must only contain the exit.
566 if (!DT->dominates(entry, exit)) {
567 for (typename DST::iterator SI = entrySuccs->begin(),
568 SE = entrySuccs->end();
569 SI != SE; ++SI) {
570 if (*SI != exit && *SI != entry)
571 return false;
572 }
573
574 return true;
575 }
576
577 DST *exitSuccs = &DF->find(exit)->second;
578
579 // Do not allow edges leaving the region.
580 for (typename DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
581 SI != SE; ++SI) {
582 if (*SI == exit || *SI == entry)
583 continue;
584 if (exitSuccs->find(*SI) == exitSuccs->end())
585 return false;
586 if (!isCommonDomFrontier(*SI, entry, exit))
587 return false;
588 }
589
590 // Do not allow edges pointing into the region.
591 for (typename DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
592 SI != SE; ++SI) {
593 if (DT->properlyDominates(entry, *SI) && *SI != exit)
594 return false;
595 }
596
597 return true;
598 }
599
600 template
601 void RegionInfoBase
602 BBtoBBMap *ShortCut) const {
603 assert(entry && exit && "entry and exit must not be null!");
604
605 typename BBtoBBMap::iterator e = ShortCut->find(exit);
606
607 if (e == ShortCut->end())
608 // No further region at exit available.
609 (*ShortCut)[entry] = exit;
610 else {
611 // We found a region e that starts at exit. Therefore (entry, e->second)
612 // is also a region, that is larger than (entry, exit). Insert the
613 // larger one.
614 BlockT *BB = e->second;
615 (*ShortCut)[entry] = BB;
616 }
617 }
618
619 template
620 typename Tr::DomTreeNodeT *
621 RegionInfoBase
622 typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
623
624 if (e == ShortCut->end())
625 return N->getIDom();
626
627 return PDT->getNode(e->second)->getIDom();
628 }
629
630 template
631 bool RegionInfoBase
632 assert(entry && exit && "entry and exit must not be null!");
633
634 unsigned num_successors =
635 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
636
637 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
638 return true;
639
640 return false;
641 }
642
643 template
644 typename Tr::RegionT *RegionInfoBase
645 BlockT *exit) {
646 assert(entry && exit && "entry and exit must not be null!");
647
648 if (isTrivialRegion(entry, exit))
649 return nullptr;
650
651 RegionT *region =
652 new RegionT(entry, exit, static_cast(this), DT);
653 BBtoRegion.insert(std::make_pair(entry, region));
654
655 #ifdef XDEBUG
656 region->verifyRegion();
657 #else
658 DEBUG(region->verifyRegion());
659 #endif
660
661 updateStatistics(region);
662 return region;
663 }
664
665 template
666 void RegionInfoBase
667 BBtoBBMap *ShortCut) {
668 assert(entry);
669
670 DomTreeNodeT *N = PDT->getNode(entry);
671 if (!N)
672 return;
673
674 RegionT *lastRegion = nullptr;
675 BlockT *lastExit = entry;
676
677 // As only a BasicBlock that postdominates entry can finish a region, walk the
678 // post dominance tree upwards.
679 while ((N = getNextPostDom(N, ShortCut))) {
680 BlockT *exit = N->getBlock();
681
682 if (!exit)
683 break;
684
685 if (isRegion(entry, exit)) {
686 RegionT *newRegion = createRegion(entry, exit);
687
688 if (lastRegion)
689 newRegion->addSubRegion(lastRegion);
690
691 lastRegion = newRegion;
692 lastExit = exit;
693 }
694
695 // This can never be a region, so stop the search.
696 if (!DT->dominates(entry, exit))
697 break;
698 }
699
700 // Tried to create regions from entry to lastExit. Next time take a
701 // shortcut from entry to lastExit.
702 if (lastExit != entry)
703 insertShortCut(entry, lastExit, ShortCut);
704 }
705
706 template
707 void RegionInfoBase
708 typedef typename std::add_pointer::type FuncPtrT;
709 BlockT *entry = GraphTraits::getEntryNode(&F);
710 DomTreeNodeT *N = DT->getNode(entry);
711
712 // Iterate over the dominance tree in post order to start with the small
713 // regions from the bottom of the dominance tree. If the small regions are
714 // detected first, detection of bigger regions is faster, as we can jump
715 // over the small regions.
716 for (po_iterator FI = po_begin(N), FE = po_end(N); FI != FE;
717 ++FI) {
718 findRegionsWithEntry(FI->getBlock(), ShortCut);
719 }
720 }
721
722 template
723 typename Tr::RegionT *RegionInfoBase
724 while (region->getParent())
725 region = region->getParent();
726
727 return region;
728 }
729
730 template
731 void RegionInfoBase
732 BlockT *BB = N->getBlock();
733
734 // Passed region exit
735 while (BB == region->getExit())
736 region = region->getParent();
737
738 typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
739
740 // This basic block is a start block of a region. It is already in the
741 // BBtoRegion relation. Only the child basic blocks have to be updated.
742 if (it != BBtoRegion.end()) {
743 RegionT *newRegion = it->second;
744 region->addSubRegion(getTopMostParent(newRegion));
745 region = newRegion;
746 } else {
747 BBtoRegion[BB] = region;
748 }
749
750 for (typename DomTreeNodeT::iterator CI = N->begin(), CE = N->end(); CI != CE;
751 ++CI) {
752 buildRegionsTree(*CI, region);
753 }
754 }
755
756 #ifdef XDEBUG
757 template
758 bool RegionInfoBase
759 #else
760 template
761 bool RegionInfoBase
762 #endif
763
764 template
765 typename Tr::RegionT::PrintStyle RegionInfoBase
766 RegionBase
767
768 template
769 void RegionInfoBase
770 OS << "Region tree:\n";
771 TopLevelRegion->print(OS, true, 0, printStyle);
772 OS << "End region tree\n";
773 }
774
775 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
776 template
777 void RegionInfoBase
778 #endif
779
780 template
781 void RegionInfoBase
782 BBtoRegion.clear();
783 if (TopLevelRegion)
784 delete TopLevelRegion;
785 TopLevelRegion = nullptr;
786 }
787
788 template
789 void RegionInfoBase
790 TopLevelRegion->verifyRegionNest();
791 }
792
793 // Region pass manager support.
794 template
795 typename Tr::RegionT *RegionInfoBase
796 typename BBtoRegionMap::const_iterator I = BBtoRegion.find(BB);
797 return I != BBtoRegion.end() ? I->second : nullptr;
798 }
799
800 template
801 void RegionInfoBase
802 BBtoRegion[BB] = R;
803 }
804
805 template
806 typename Tr::RegionT *RegionInfoBase
807 return getRegionFor(BB);
808 }
809
810 template
811 typename RegionInfoBase
812 RegionInfoBase
813 BlockT *Exit = nullptr;
814
815 while (true) {
816 // Get largest region that starts at BB.
817 RegionT *R = getRegionFor(BB);
818 while (R && R->getParent() && R->getParent()->getEntry() == BB)
819 R = R->getParent();
820
821 // Get the single exit of BB.
822 if (R && R->getEntry() == BB)
823 Exit = R->getExit();
824 else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
825 Exit = *BlockTraits::child_begin(BB);
826 else // No single exit exists.
827 return Exit;
828
829 // Get largest region that starts at Exit.
830 RegionT *ExitR = getRegionFor(Exit);
831 while (ExitR && ExitR->getParent() &&
832 ExitR->getParent()->getEntry() == Exit)
833 ExitR = ExitR->getParent();
834
835 for (PredIterTy PI = InvBlockTraits::child_begin(Exit),
836 PE = InvBlockTraits::child_end(Exit);
837 PI != PE; ++PI) {
838 if (!R->contains(*PI) && !ExitR->contains(*PI))
839 break;
840 }
841
842 // This stops infinite cycles.
843 if (DT->dominates(Exit, BB))
844 break;
845
846 BB = Exit;
847 }
848
849 return Exit;
850 }
851
852 template
853 typename Tr::RegionT *RegionInfoBase
854 RegionT *B) const {
855 assert(A && B && "One of the Regions is NULL");
856
857 if (A->contains(B))
858 return A;
859
860 while (!B->contains(A))
861 B = B->getParent();
862
863 return B;
864 }
865
866 template
867 typename Tr::RegionT *
868 RegionInfoBase
869 RegionT *ret = Regions.back();
870 Regions.pop_back();
871
872 for (RegionT *R : Regions)
873 ret = getCommonRegion(ret, R);
874
875 return ret;
876 }
877
878 template
879 typename Tr::RegionT *
880 RegionInfoBase
881 RegionT *ret = getRegionFor(BBs.back());
882 BBs.pop_back();
883
884 for (BlockT *BB : BBs)
885 ret = getCommonRegion(ret, getRegionFor(BB));
886
887 return ret;
888 }
889
890 template
891 void RegionInfoBase
892 RegionT *R = getRegionFor(OldBB);
893
894 setRegionFor(NewBB, R);
895
896 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
897 R->replaceEntry(NewBB);
898 R = R->getParent();
899 }
900
901 setRegionFor(OldBB, R);
902 }
903
904 template
905 void RegionInfoBase
906 typedef typename std::add_pointer::type FuncPtrT;
907
908 // ShortCut a function where for every BB the exit of the largest region
909 // starting with BB is stored. These regions can be threated as single BBS.
910 // This improves performance on linear CFGs.
911 BBtoBBMap ShortCut;
912
913 scanForRegions(F, &ShortCut);
914 BlockT *BB = GraphTraits::getEntryNode(&F);
915 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
916 }
917
918 #endif
2929 ///
3030 /// For a subregion RegionNode there is just one successor. The RegionNode
3131 /// representing the exit of the subregion.
32 template>
32 template, class BlockT, class RegionT>
3333 class RNSuccIterator : public std::iterator
34 NodeType, ptrdiff_t>
35 {
34 NodeType, ptrdiff_t> {
3635 typedef std::iterator super;
36
37 typedef GraphTraits BlockTraits;
38 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
39
3740 // The iterator works in two modes, bb mode or region mode.
38 enum ItMode{
41 enum ItMode {
3942 // In BB mode it returns all successors of this BasicBlock as its
4043 // successors.
4144 ItBB,
4649 };
4750
4851 // Use two bit to represent the mode iterator.
49 PointerIntPairenum ItMode> Node;
52 PointerIntPairItMode> Node;
5053
5154 // The block successor iterator.
52 succ_iterator BItor;
55 SuccIterTy BItor;
5356
5457 // advanceRegionSucc - A region node has only one successor. It reaches end
5558 // once we advance it.
6568
6669 // Get the immediate successor. This function may return a Basic Block
6770 // RegionNode or a subregion RegionNode.
68 RegionNode* getISucc(BasicBlock* BB) const {
69 RegionNode *succ;
71 NodeType* getISucc(BlockT* BB) const {
72 NodeType *succ;
7073 succ = getNode()->getParent()->getNode(BB);
7174 assert(succ && "BB not in Region or entered subregion!");
7275 return succ;
7376 }
7477
7578 // getRegionSucc - Return the successor basic block of a SubRegion RegionNode.
76 inline BasicBlock* getRegionSucc() const {
79 inline BlockT* getRegionSucc() const {
7780 assert(Node.getInt() == ItRgBegin && "Cannot get the region successor!");
78 return getNode()->template getNodeAs>()->getExit();
81 return getNode()->template getNodeAsT>()->getExit();
7982 }
8083
8184 // isExit - Is this the exit BB of the Region?
82 inline bool isExit(BasicBlock* BB) const {
85 inline bool isExit(BlockT* BB) const {
8386 return getNode()->getParent()->getExit() == BB;
8487 }
8588 public:
86 typedef RNSuccIterator> Self;
89 typedef RNSuccIterator, BlockT, RegionT> Self;
8790
8891 typedef typename super::pointer pointer;
8992
9093 /// @brief Create begin iterator of a RegionNode.
9194 inline RNSuccIterator(NodeType* node)
9295 : Node(node, node->isSubRegion() ? ItRgBegin : ItBB),
93 BItor(succ_begin(node->getEntry())) {
94
96 BItor(BlockTraits::child_begin(node->getEntry())) {
9597
9698 // Skip the exit block
9799 if (!isRegionMode())
98 while (succ_end(node->getEntry()) != BItor && isExit(*BItor))
100 while (BlockTraits::child_end(node->getEntry()) != BItor && isExit(*BItor))
99101 ++BItor;
100102
101103 if (isRegionMode() && isExit(getRegionSucc()))
105107 /// @brief Create an end iterator.
106108 inline RNSuccIterator(NodeType* node, bool)
107109 : Node(node, node->isSubRegion() ? ItRgEnd : ItBB),
108 BItor(succ_end(node->getEntry())) {}
110 BItor(BlockTraits::child_end(node->getEntry())) {}
109111
110112 inline bool operator==(const Self& x) const {
111113 assert(isRegionMode() == x.isRegionMode() && "Broken iterator!");
118120 inline bool operator!=(const Self& x) const { return !operator==(x); }
119121
120122 inline pointer operator*() const {
121 BasicBlock* BB = isRegionMode() ? getRegionSucc() : *BItor;
123 BlockT *BB = isRegionMode() ? getRegionSucc() : *BItor;
122124 assert(!isExit(BB) && "Iterator out of range!");
123125 return getISucc(BB);
124126 }
131133 // Skip the exit.
132134 do
133135 ++BItor;
134 while (BItor != succ_end(getNode()->getEntry())
136 while (BItor != BlockTraits::child_end(getNode()->getEntry())
135137 && isExit(*BItor));
136138 }
137139 return *this;
161163 /// The Flat Region iterator will iterate over all BasicBlock RegionNodes that
162164 /// are contained in the Region and its subregions. This is close to a virtual
163165 /// control flow graph of the Region.
164 template
165 class RNSuccIterator >
166 : public std::iterator
167 {
166 template
167 class RNSuccIterator, BlockT, RegionT>
168 : public std::iterator {
168169 typedef std::iterator super;
170 typedef GraphTraits BlockTraits;
171 typedef typename BlockTraits::ChildIteratorType SuccIterTy;
172
169173 NodeType* Node;
170 succ_iterator Itor;
174 SuccIterTy Itor;
171175
172176 public:
173 typedef RNSuccIterator > Self;
177 typedef RNSuccIterator, BlockT, RegionT> Self;
174178 typedef typename super::pointer pointer;
175179
176180 /// @brief Create the iterator from a RegionNode.
177181 ///
178182 /// Note that the incoming node must be a bb node, otherwise it will trigger
179183 /// an assertion when we try to get a BasicBlock.
180 inline RNSuccIterator(NodeType* node) : Node(node),
181 Itor(succ_begin(node->getEntry())) {
184 inline RNSuccIterator(NodeType* node) :
185 Node(node),
186 Itor(BlockTraits::child_begin(node->getEntry())) {
182187 assert(!Node->isSubRegion()
183188 && "Subregion node not allowed in flat iterating mode!");
184189 assert(Node->getParent() && "A BB node must have a parent!");
185190
186191 // Skip the exit block of the iterating region.
187 while (succ_end(Node->getEntry()) != Itor
192 while (BlockTraits::child_end(Node->getEntry()) != Itor
188193 && Node->getParent()->getExit() == *Itor)
189194 ++Itor;
190195 }
196
191197 /// @brief Create an end iterator
192 inline RNSuccIterator(NodeType* node, bool) : Node(node),
193 Itor(succ_end(node->getEntry())) {
198 inline RNSuccIterator(NodeType* node, bool) :
199 Node(node),
200 Itor(BlockTraits::child_end(node->getEntry())) {
194201 assert(!Node->isSubRegion()
195202 && "Subregion node not allowed in flat iterating mode!");
196203 }
205212 inline bool operator!=(const Self& x) const { return !operator==(x); }
206213
207214 inline pointer operator*() const {
208 BasicBlock* BB = *Itor;
215 BlockT *BB = *Itor;
209216
210217 // Get the iterating region.
211 Region* Parent = Node->getParent();
218 RegionT *Parent = Node->getParent();
212219
213220 // The only case that the successor reaches out of the region is it reaches
214221 // the exit of the region.
244251 }
245252 };
246253
247 template
248 inline RNSuccIterator succ_begin(NodeType* Node) {
249 return RNSuccIterator(Node);
254 template
255 inline RNSuccIterator succ_begin(NodeType* Node) {
256 return RNSuccIterator(Node);
250257 }
251258
252 template
253 inline RNSuccIterator succ_end(NodeType* Node) {
254 return RNSuccIterator(Node, true);
259 template
260 inline RNSuccIterator succ_end(NodeType* Node) {
261 return RNSuccIterator(Node, true);
255262 }
256263
257264 //===--------------------------------------------------------------------===//
261268 // NodeT can either be region node or const region node, otherwise child_begin
262269 // and child_end fail.
263270
264 #define RegionNodeGraphTraits(NodeT) \
265 template<> struct GraphTraits { \
271 #define RegionNodeGraphTraits(NodeT, BlockT, RegionT) \
272 template<> struct GraphTraits { \
266273 typedef NodeT NodeType; \
267 typedef RNSuccIterator> ChildIteratorType; \
274 typedef RNSuccIterator, BlockT, RegionT> ChildIteratorType; \
268275 static NodeType *getEntryNode(NodeType* N) { return N; } \
269276 static inline ChildIteratorType child_begin(NodeType *N) { \
270 return RNSuccIterator>(N); \
277 return RNSuccIterator, BlockT, RegionT>(N); \
271278 } \
272279 static inline ChildIteratorType child_end(NodeType *N) { \
273 return RNSuccIterator>(N, true); \
280 return RNSuccIterator, BlockT, RegionT>(N, true); \
274281 } \
275282 }; \
276 template<> struct GraphTraits > { \
283 template<> struct GraphTraits> { \
277284 typedef NodeT NodeType; \
278 typedef RNSuccIterator > ChildIteratorType; \
285 typedef RNSuccIterator, BlockT, RegionT > ChildIteratorType; \
279286 static NodeType *getEntryNode(NodeType* N) { return N; } \
280287 static inline ChildIteratorType child_begin(NodeType *N) { \
281 return RNSuccIterator >(N); \
288 return RNSuccIterator, BlockT, RegionT>(N); \
282289 } \
283290 static inline ChildIteratorType child_end(NodeType *N) { \
284 return RNSuccIterator >(N, true); \
291 return RNSuccIterator, BlockT, RegionT>(N, true); \
285292 } \
286293 }
287294
314321 } \
315322 }
316323
317 RegionNodeGraphTraits(RegionNode);
318 RegionNodeGraphTraits(const RegionNode);
324 RegionNodeGraphTraits(RegionNode, BasicBlock, Region);
325 RegionNodeGraphTraits(const RegionNode, BasicBlock, Region);
319326
320327 RegionGraphTraits(Region, RegionNode);
321328 RegionGraphTraits(const Region, const RegionNode);
336343 }
337344 };
338345
346 template <> struct GraphTraits
347 : public GraphTraits {
348 typedef df_iterator, false,
349 GraphTraits > > nodes_iterator;
350
351 static NodeType *getEntryNode(RegionInfoPass *RI) {
352 return GraphTraits::getEntryNode(&RI->getRegionInfo());
353 }
354 static nodes_iterator nodes_begin(RegionInfoPass* RI) {
355 return GraphTraits::nodes_begin(&RI->getRegionInfo());
356 }
357 static nodes_iterator nodes_end(RegionInfoPass *RI) {
358 return GraphTraits::nodes_end(&RI->getRegionInfo());
359 }
360 };
361
339362 } // End namespace llvm
340363
341364 #endif
0 //===- llvm/CodeGen/MachineRegionInfo.h -------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_MACHINEREGIONINFO_H
10 #define LLVM_CODEGEN_MACHINEREGIONINFO_H
11
12 #include "llvm/Analysis/RegionInfo.h"
13 #include "llvm/Analysis/RegionIterator.h"
14 #include "llvm/CodeGen/MachineDominanceFrontier.h"
15 #include "llvm/CodeGen/MachineDominators.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19
20
21 namespace llvm {
22
23 class MachineDominatorTree;
24 struct MachinePostDominatorTree;
25 class MachineRegion;
26 class MachineRegionNode;
27 class MachineRegionInfo;
28
29 template<>
30 struct RegionTraits {
31 typedef MachineFunction FuncT;
32 typedef MachineBasicBlock BlockT;
33 typedef MachineRegion RegionT;
34 typedef MachineRegionNode RegionNodeT;
35 typedef MachineRegionInfo RegionInfoT;
36 typedef MachineDominatorTree DomTreeT;
37 typedef MachineDomTreeNode DomTreeNodeT;
38 typedef MachinePostDominatorTree PostDomTreeT;
39 typedef MachineDominanceFrontier DomFrontierT;
40 typedef MachineInstr InstT;
41 typedef MachineLoop LoopT;
42 typedef MachineLoopInfo LoopInfoT;
43
44 static unsigned getNumSuccessors(MachineBasicBlock *BB) {
45 return BB->succ_size();
46 }
47 };
48
49
50 class MachineRegionNode : public RegionNodeBase> {
51 public:
52 inline MachineRegionNode(MachineRegion *Parent,
53 MachineBasicBlock *Entry,
54 bool isSubRegion = false)
55 : RegionNodeBase>(Parent, Entry, isSubRegion) {
56
57 }
58
59 ~MachineRegionNode() { }
60
61 bool operator==(const MachineRegion &RN) const {
62 return this == reinterpret_cast(&RN);
63 }
64 };
65
66 class MachineRegion : public RegionBase> {
67 public:
68 MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
69 MachineRegionInfo* RI,
70 MachineDominatorTree *DT, MachineRegion *Parent = nullptr);
71 ~MachineRegion();
72
73 bool operator==(const MachineRegionNode &RN) const {
74 return &RN == reinterpret_cast(this);
75 }
76 };
77
78 class MachineRegionInfo : public RegionInfoBase> {
79 public:
80 explicit MachineRegionInfo();
81
82 virtual ~MachineRegionInfo();
83
84 // updateStatistics - Update statistic about created regions.
85 void updateStatistics(MachineRegion *R) final;
86
87 void recalculate(MachineFunction &F,
88 MachineDominatorTree *DT,
89 MachinePostDominatorTree *PDT,
90 MachineDominanceFrontier *DF);
91 };
92
93 class MachineRegionInfoPass : public MachineFunctionPass {
94 MachineRegionInfo RI;
95
96 public:
97 static char ID;
98 explicit MachineRegionInfoPass();
99
100 ~MachineRegionInfoPass();
101
102 MachineRegionInfo &getRegionInfo() {
103 return RI;
104 }
105
106 const MachineRegionInfo &getRegionInfo() const {
107 return RI;
108 }
109
110 /// @name MachineFunctionPass interface
111 //@{
112 bool runOnMachineFunction(MachineFunction &F) override;
113 void releaseMemory() override;
114 void verifyAnalysis() const override;
115 void getAnalysisUsage(AnalysisUsage &AU) const override;
116 void print(raw_ostream &OS, const Module *) const override;
117 void dump() const;
118 //@}
119 };
120
121
122 template <>
123 template <>
124 inline MachineBasicBlock* RegionNodeBase>::getNodeAs() const {
125 assert(!isSubRegion() && "This is not a MachineBasicBlock RegionNode!");
126 return getEntry();
127 }
128
129 template<>
130 template<>
131 inline MachineRegion* RegionNodeBase>::getNodeAs() const {
132 assert(isSubRegion() && "This is not a subregion RegionNode!");
133 auto Unconst = const_cast>*>(this);
134 return reinterpret_cast(Unconst);
135 }
136
137
138 RegionNodeGraphTraits(MachineRegionNode, MachineBasicBlock, MachineRegion);
139 RegionNodeGraphTraits(const MachineRegionNode, MachineBasicBlock, MachineRegion);
140
141 RegionGraphTraits(MachineRegion, MachineRegionNode);
142 RegionGraphTraits(const MachineRegion, const MachineRegionNode);
143
144 template <> struct GraphTraits
145 : public GraphTraits > {
146 typedef df_iterator, false,
147 GraphTraits > > nodes_iterator;
148
149 static NodeType *getEntryNode(MachineRegionInfo *RI) {
150 return GraphTraits >::getEntryNode(RI->getTopLevelRegion());
151 }
152 static nodes_iterator nodes_begin(MachineRegionInfo* RI) {
153 return nodes_iterator::begin(getEntryNode(RI));
154 }
155 static nodes_iterator nodes_end(MachineRegionInfo *RI) {
156 return nodes_iterator::end(getEntryNode(RI));
157 }
158 };
159
160 template <> struct GraphTraits
161 : public GraphTraits {
162 typedef df_iterator, false,
163 GraphTraits > > nodes_iterator;
164
165 static NodeType *getEntryNode(MachineRegionInfoPass *RI) {
166 return GraphTraits::getEntryNode(&RI->getRegionInfo());
167 }
168 static nodes_iterator nodes_begin(MachineRegionInfoPass* RI) {
169 return GraphTraits::nodes_begin(&RI->getRegionInfo());
170 }
171 static nodes_iterator nodes_end(MachineRegionInfoPass *RI) {
172 return GraphTraits::nodes_end(&RI->getRegionInfo());
173 }
174 };
175
176 EXTERN_TEMPLATE_INSTANTIATION(class RegionBase>);
177 EXTERN_TEMPLATE_INSTANTIATION(class RegionNodeBase>);
178 EXTERN_TEMPLATE_INSTANTIATION(class RegionInfoBase>);
179
180 }
181
182 #endif
188188 void initializeMachineLICMPass(PassRegistry&);
189189 void initializeMachineLoopInfoPass(PassRegistry&);
190190 void initializeMachineModuleInfoPass(PassRegistry&);
191 void initializeMachineRegionInfoPassPass(PassRegistry&);
191192 void initializeMachineSchedulerPass(PassRegistry&);
192193 void initializeMachineSinkingPass(PassRegistry&);
193194 void initializeMachineTraceMetricsPass(PassRegistry&);
226227 void initializePruneEHPass(PassRegistry&);
227228 void initializeReassociatePass(PassRegistry&);
228229 void initializeRegToMemPass(PassRegistry&);
229 void initializeRegionInfoPass(PassRegistry&);
230 void initializeRegionInfoPassPass(PassRegistry&);
230231 void initializeRegionOnlyPrinterPass(PassRegistry&);
231232 void initializeRegionOnlyViewerPass(PassRegistry&);
232233 void initializeRegionPrinterPass(PassRegistry&);
5656 initializeMemoryDependenceAnalysisPass(Registry);
5757 initializeModuleDebugInfoPrinterPass(Registry);
5858 initializePostDominatorTreePass(Registry);
59 initializeRegionInfoPass(Registry);
59 initializeRegionInfoPassPass(Registry);
6060 initializeRegionViewerPass(Registry);
6161 initializeRegionPrinterPass(Registry);
6262 initializeRegionOnlyViewerPass(Registry);
99 //===----------------------------------------------------------------------===//
1010
1111 #include "llvm/Analysis/RegionInfo.h"
12 #include "llvm/Analysis/RegionInfoImpl.h"
1213 #include "llvm/ADT/PostOrderIterator.h"
1314 #include "llvm/ADT/Statistic.h"
1415 #include "llvm/Analysis/LoopInfo.h"
2425
2526 #define DEBUG_TYPE "region"
2627
27 // Always verify if expensive checking is enabled.
28 #ifdef XDEBUG
29 static bool VerifyRegionInfo = true;
30 #else
31 static bool VerifyRegionInfo = false;
32 #endif
33
34 static cl::opt
35 VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
36 cl::desc("Verify region info (time consuming)"));
28 namespace llvm {
29 template class RegionBase>;
30 template class RegionNodeBase>;
31 template class RegionInfoBase>;
32 }
3733
3834 STATISTIC(numRegions, "The # of regions");
3935 STATISTIC(numSimpleRegions, "The # of simple regions");
4036
41 static cl::opt printStyle("print-region-style",
37 // Always verify if expensive checking is enabled.
38
39 static cl::opt
40 VerifyRegionInfoX(
41 "verify-region-info",
42 cl::location(RegionInfoBase>::VerifyRegionInfo),
43 cl::desc("Verify region info (time consuming)"));
44
45
46 static cl::opt printStyleX("print-region-style",
47 cl::location(RegionInfo::printStyle),
4248 cl::Hidden,
4349 cl::desc("style of printing regions"),
4450 cl::values(
4854 clEnumValN(Region::PrintRN, "rn",
4955 "print regions in detail with element_iterator"),
5056 clEnumValEnd));
57
58
5159 //===----------------------------------------------------------------------===//
52 /// Region Implementation
53 Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
54 DominatorTree *dt, Region *Parent)
55 : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
60 // Region implementation
61 //
5662
57 Region::~Region() {
58 // Free the cached nodes.
59 for (BBNodeMapT::iterator it = BBNodeMap.begin(),
60 ie = BBNodeMap.end(); it != ie; ++it)
61 delete it->second;
63 Region::Region(BasicBlock *Entry, BasicBlock *Exit,
64 RegionInfo* RI,
65 DominatorTree *DT, Region *Parent) :
66 RegionBase>(Entry, Exit, RI, DT, Parent) {
6267
63 // Only clean the cache for this Region. Caches of child Regions will be
64 // cleaned when the child Regions are deleted.
65 BBNodeMap.clear();
6668 }
6769
68 void Region::replaceEntry(BasicBlock *BB) {
69 entry.setPointer(BB);
70 }
71
72 void Region::replaceExit(BasicBlock *BB) {
73 assert(exit && "No exit to replace!");
74 exit = BB;
75 }
76
77 void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
78 std::vector RegionQueue;
79 BasicBlock *OldEntry = getEntry();
80
81 RegionQueue.push_back(this);
82 while (!RegionQueue.empty()) {
83 Region *R = RegionQueue.back();
84 RegionQueue.pop_back();
85
86 R->replaceEntry(NewEntry);
87 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
88 if ((*RI)->getEntry() == OldEntry)
89 RegionQueue.push_back(RI->get());
90 }
91 }
92
93 void Region::replaceExitRecursive(BasicBlock *NewExit) {
94 std::vector RegionQueue;
95 BasicBlock *OldExit = getExit();
96
97 RegionQueue.push_back(this);
98 while (!RegionQueue.empty()) {
99 Region *R = RegionQueue.back();
100 RegionQueue.pop_back();
101
102 R->replaceExit(NewExit);
103 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
104 if ((*RI)->getExit() == OldExit)
105 RegionQueue.push_back(RI->get());
106 }
107 }
108
109 bool Region::contains(const BasicBlock *B) const {
110 BasicBlock *BB = const_cast(B);
111
112 if (!DT->getNode(BB))
113 return false;
114
115 BasicBlock *entry = getEntry(), *exit = getExit();
116
117 // Toplevel region.
118 if (!exit)
119 return true;
120
121 return (DT->dominates(entry, BB)
122 && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
123 }
124
125 bool Region::contains(const Loop *L) const {
126 // BBs that are not part of any loop are element of the Loop
127 // described by the NULL pointer. This loop is not part of any region,
128 // except if the region describes the whole function.
129 if (!L)
130 return getExit() == nullptr;
131
132 if (!contains(L->getHeader()))
133 return false;
134
135 SmallVector ExitingBlocks;
136 L->getExitingBlocks(ExitingBlocks);
137
138 for (SmallVectorImpl::iterator BI = ExitingBlocks.begin(),
139 BE = ExitingBlocks.end(); BI != BE; ++BI)
140 if (!contains(*BI))
141 return false;
142
143 return true;
144 }
145
146 Loop *Region::outermostLoopInRegion(Loop *L) const {
147 if (!contains(L))
148 return nullptr;
149
150 while (L && contains(L->getParentLoop())) {
151 L = L->getParentLoop();
152 }
153
154 return L;
155 }
156
157 Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
158 assert(LI && BB && "LI and BB cannot be null!");
159 Loop *L = LI->getLoopFor(BB);
160 return outermostLoopInRegion(L);
161 }
162
163 BasicBlock *Region::getEnteringBlock() const {
164 BasicBlock *entry = getEntry();
165 BasicBlock *Pred;
166 BasicBlock *enteringBlock = nullptr;
167
168 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
169 ++PI) {
170 Pred = *PI;
171 if (DT->getNode(Pred) && !contains(Pred)) {
172 if (enteringBlock)
173 return nullptr;
174
175 enteringBlock = Pred;
176 }
177 }
178
179 return enteringBlock;
180 }
181
182 BasicBlock *Region::getExitingBlock() const {
183 BasicBlock *exit = getExit();
184 BasicBlock *Pred;
185 BasicBlock *exitingBlock = nullptr;
186
187 if (!exit)
188 return nullptr;
189
190 for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
191 ++PI) {
192 Pred = *PI;
193 if (contains(Pred)) {
194 if (exitingBlock)
195 return nullptr;
196
197 exitingBlock = Pred;
198 }
199 }
200
201 return exitingBlock;
202 }
203
204 bool Region::isSimple() const {
205 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
206 }
207
208 std::string Region::getNameStr() const {
209 std::string exitName;
210 std::string entryName;
211
212 if (getEntry()->getName().empty()) {
213 raw_string_ostream OS(entryName);
214
215 getEntry()->printAsOperand(OS, false);
216 } else
217 entryName = getEntry()->getName();
218
219 if (getExit()) {
220 if (getExit()->getName().empty()) {
221 raw_string_ostream OS(exitName);
222
223 getExit()->printAsOperand(OS, false);
224 } else
225 exitName = getExit()->getName();
226 } else
227 exitName = "";
228
229 return entryName + " => " + exitName;
230 }
231
232 void Region::verifyBBInRegion(BasicBlock *BB) const {
233 if (!contains(BB))
234 llvm_unreachable("Broken region found!");
235
236 BasicBlock *entry = getEntry(), *exit = getExit();
237
238 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
239 if (!contains(*SI) && exit != *SI)
240 llvm_unreachable("Broken region found!");
241
242 if (entry != BB)
243 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
244 if (!contains(*SI))
245 llvm_unreachable("Broken region found!");
246 }
247
248 void Region::verifyWalk(BasicBlock *BB, std::set *visited) const {
249 BasicBlock *exit = getExit();
250
251 visited->insert(BB);
252
253 verifyBBInRegion(BB);
254
255 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
256 if (*SI != exit && visited->find(*SI) == visited->end())
257 verifyWalk(*SI, visited);
258 }
259
260 void Region::verifyRegion() const {
261 // Only do verification when user wants to, otherwise this expensive
262 // check will be invoked by PassManager.
263 if (!VerifyRegionInfo) return;
264
265 std::set visited;
266 verifyWalk(getEntry(), &visited);
267 }
268
269 void Region::verifyRegionNest() const {
270 for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
271 (*RI)->verifyRegionNest();
272
273 verifyRegion();
274 }
275
276 Region::element_iterator Region::element_begin() {
277 return GraphTraits::nodes_begin(this);
278 }
279
280 Region::element_iterator Region::element_end() {
281 return GraphTraits::nodes_end(this);
282 }
283
284 Region::const_element_iterator Region::element_begin() const {
285 return GraphTraits::nodes_begin(this);
286 }
287
288 Region::const_element_iterator Region::element_end() const {
289 return GraphTraits::nodes_end(this);
290 }
291
292 Region* Region::getSubRegionNode(BasicBlock *BB) const {
293 Region *R = RI->getRegionFor(BB);
294
295 if (!R || R == this)
296 return nullptr;
297
298 // If we pass the BB out of this region, that means our code is broken.
299 assert(contains(R) && "BB not in current region!");
300
301 while (contains(R->getParent()) && R->getParent() != this)
302 R = R->getParent();
303
304 if (R->getEntry() != BB)
305 return nullptr;
306
307 return R;
308 }
309
310 RegionNode* Region::getBBNode(BasicBlock *BB) const {
311 assert(contains(BB) && "Can get BB node out of this region!");
312
313 BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
314
315 if (at != BBNodeMap.end())
316 return at->second;
317
318 RegionNode *NewNode = new RegionNode(const_cast(this), BB);
319 BBNodeMap.insert(std::make_pair(BB, NewNode));
320 return NewNode;
321 }
322
323 RegionNode* Region::getNode(BasicBlock *BB) const {
324 assert(contains(BB) && "Can get BB node out of this region!");
325 if (Region* Child = getSubRegionNode(BB))
326 return Child->getNode();
327
328 return getBBNode(BB);
329 }
330
331 void Region::transferChildrenTo(Region *To) {
332 for (iterator I = begin(), E = end(); I != E; ++I) {
333 (*I)->parent = To;
334 To->children.push_back(std::move(*I));
335 }
336 children.clear();
337 }
338
339 void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
340 assert(!SubRegion->parent && "SubRegion already has a parent!");
341 assert(std::find_if(begin(), end(), [&](const std::unique_ptr &R) {
342 return R.get() == SubRegion;
343 }) == children.end() &&
344 "Subregion already exists!");
345
346 SubRegion->parent = this;
347 children.push_back(std::unique_ptr(SubRegion));
348
349 if (!moveChildren)
350 return;
351
352 assert(SubRegion->children.size() == 0
353 && "SubRegions that contain children are not supported");
354
355 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
356 if (!(*I)->isSubRegion()) {
357 BasicBlock *BB = (*I)->getNodeAs();
358
359 if (SubRegion->contains(BB))
360 RI->setRegionFor(BB, SubRegion);
361 }
362
363 std::vector> Keep;
364 for (iterator I = begin(), E = end(); I != E; ++I)
365 if (SubRegion->contains(I->get()) && I->get() != SubRegion) {
366 (*I)->parent = SubRegion;
367 SubRegion->children.push_back(std::move(*I));
368 } else
369 Keep.push_back(std::move(*I));
370
371 children.clear();
372 children.insert(children.begin(),
373 std::move_iterator(Keep.begin()),
374 std::move_iterator(Keep.end()));
375 }
376
377
378 Region *Region::removeSubRegion(Region *Child) {
379 assert(Child->parent == this && "Child is not a child of this region!");
380 Child->parent = nullptr;
381 RegionSet::iterator I = std::find_if(
382 children.begin(), children.end(),
383 [&](const std::unique_ptr &R) { return R.get() == Child; });
384 assert(I != children.end() && "Region does not exit. Unable to remove.");
385 children.erase(children.begin()+(I-begin()));
386 return Child;
387 }
388
389 unsigned Region::getDepth() const {
390 unsigned Depth = 0;
391
392 for (Region *R = parent; R != nullptr; R = R->parent)
393 ++Depth;
394
395 return Depth;
396 }
397
398 Region *Region::getExpandedRegion() const {
399 unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
400
401 if (NumSuccessors == 0)
402 return nullptr;
403
404 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
405 PI != PE; ++PI)
406 if (!DT->dominates(getEntry(), *PI))
407 return nullptr;
408
409 Region *R = RI->getRegionFor(exit);
410
411 if (R->getEntry() != exit) {
412 if (exit->getTerminator()->getNumSuccessors() == 1)
413 return new Region(getEntry(), *succ_begin(exit), RI, DT);
414 else
415 return nullptr;
416 }
417
418 while (R->getParent() && R->getParent()->getEntry() == exit)
419 R = R->getParent();
420
421 if (!DT->dominates(getEntry(), R->getExit()))
422 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
423 PI != PE; ++PI)
424 if (!DT->dominates(R->getExit(), *PI))
425 return nullptr;
426
427 return new Region(getEntry(), R->getExit(), RI, DT);
428 }
429
430 void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
431 enum PrintStyle Style) const {
432 if (print_tree)
433 OS.indent(level*2) << "[" << level << "] " << getNameStr();
434 else
435 OS.indent(level*2) << getNameStr();
436
437 OS << "\n";
438
439
440 if (Style != PrintNone) {
441 OS.indent(level*2) << "{\n";
442 OS.indent(level*2 + 2);
443
444 if (Style == PrintBB) {
445 for (const auto &BB : blocks())
446 OS << BB->getName() << ", "; // TODO: remove the last ","
447 } else if (Style == PrintRN) {
448 for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
449 OS << **I << ", "; // TODO: remove the last ",
450 }
451
452 OS << "\n";
453 }
454
455 if (print_tree)
456 for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
457 (*RI)->print(OS, print_tree, level+1, Style);
458
459 if (Style != PrintNone)
460 OS.indent(level*2) << "} \n";
461 }
462
463 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
464 void Region::dump() const {
465 print(dbgs(), true, getDepth(), printStyle.getValue());
466 }
467 #endif
468
469 void Region::clearNodeCache() {
470 // Free the cached nodes.
471 for (BBNodeMapT::iterator I = BBNodeMap.begin(),
472 IE = BBNodeMap.end(); I != IE; ++I)
473 delete I->second;
474
475 BBNodeMap.clear();
476 for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
477 (*RI)->clearNodeCache();
478 }
70 Region::~Region() { }
47971
48072 //===----------------------------------------------------------------------===//
48173 // RegionInfo implementation
48274 //
48375
484 bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
485 BasicBlock *exit) const {
486 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
487 BasicBlock *P = *PI;
488 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
489 return false;
490 }
491 return true;
76 RegionInfo::RegionInfo() :
77 RegionInfoBase>() {
78
49279 }
49380
494 bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
495 assert(entry && exit && "entry and exit must not be null!");
496 typedef DominanceFrontier::DomSetType DST;
81 RegionInfo::~RegionInfo() {
49782
498 DST *entrySuccs = &DF->find(entry)->second;
499
500 // Exit is the header of a loop that contains the entry. In this case,
501 // the dominance frontier must only contain the exit.
502 if (!DT->dominates(entry, exit)) {
503 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
504 SI != SE; ++SI)
505 if (*SI != exit && *SI != entry)
506 return false;
507
508 return true;
509 }
510
511 DST *exitSuccs = &DF->find(exit)->second;
512
513 // Do not allow edges leaving the region.
514 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
515 SI != SE; ++SI) {
516 if (*SI == exit || *SI == entry)
517 continue;
518 if (exitSuccs->find(*SI) == exitSuccs->end())
519 return false;
520 if (!isCommonDomFrontier(*SI, entry, exit))
521 return false;
522 }
523
524 // Do not allow edges pointing into the region.
525 for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
526 SI != SE; ++SI)
527 if (DT->properlyDominates(entry, *SI) && *SI != exit)
528 return false;
529
530
531 return true;
532 }
533
534 void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
535 BBtoBBMap *ShortCut) const {
536 assert(entry && exit && "entry and exit must not be null!");
537
538 BBtoBBMap::iterator e = ShortCut->find(exit);
539
540 if (e == ShortCut->end())
541 // No further region at exit available.
542 (*ShortCut)[entry] = exit;
543 else {
544 // We found a region e that starts at exit. Therefore (entry, e->second)
545 // is also a region, that is larger than (entry, exit). Insert the
546 // larger one.
547 BasicBlock *BB = e->second;
548 (*ShortCut)[entry] = BB;
549 }
550 }
551
552 DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
553 BBtoBBMap *ShortCut) const {
554 BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
555
556 if (e == ShortCut->end())
557 return N->getIDom();
558
559 return PDT->getNode(e->second)->getIDom();
560 }
561
562 bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
563 assert(entry && exit && "entry and exit must not be null!");
564
565 unsigned num_successors = succ_end(entry) - succ_begin(entry);
566
567 if (num_successors <= 1 && exit == *(succ_begin(entry)))
568 return true;
569
570 return false;
57183 }
57284
57385 void RegionInfo::updateStatistics(Region *R) {
57486 ++numRegions;
57587
57688 // TODO: Slow. Should only be enabled if -stats is used.
577 if (R->isSimple()) ++numSimpleRegions;
89 if (R->isSimple())
90 ++numSimpleRegions;
57891 }
57992
580 Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
581 assert(entry && exit && "entry and exit must not be null!");
93 void RegionInfo::RegionInfo::recalculate(Function &F,
94 DominatorTree *DT_,
95 PostDominatorTree *PDT_,
96 DominanceFrontier *DF_) {
97 DT = DT_;
98 PDT = PDT_;
99 DF = DF_;
582100
583 if (isTrivialRegion(entry, exit))
584 return nullptr;
585
586 Region *region = new Region(entry, exit, this, DT);
587 BBtoRegion.insert(std::make_pair(entry, region));
588
589 #ifdef XDEBUG
590 region->verifyRegion();
591 #else
592 DEBUG(region->verifyRegion());
593 #endif
594
595 updateStatistics(region);
596 return region;
101 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
102 this, DT, nullptr);
103 updateStatistics(TopLevelRegion);
104 calculate(F);
597105 }
598106
599 void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
600 assert(entry);
107 //===----------------------------------------------------------------------===//
108 // RegionInfoPass implementation
109 //
601110
602 DomTreeNode *N = PDT->getNode(entry);
603
604 if (!N)
605 return;
606
607 Region *lastRegion= nullptr;
608 BasicBlock *lastExit = entry;
609
610 // As only a BasicBlock that postdominates entry can finish a region, walk the
611 // post dominance tree upwards.
612 while ((N = getNextPostDom(N, ShortCut))) {
613 BasicBlock *exit = N->getBlock();
614
615 if (!exit)
616 break;
617
618 if (isRegion(entry, exit)) {
619 Region *newRegion = createRegion(entry, exit);
620
621 if (lastRegion)
622 newRegion->addSubRegion(lastRegion);
623
624 lastRegion = newRegion;
625 lastExit = exit;
626 }
627
628 // This can never be a region, so stop the search.
629 if (!DT->dominates(entry, exit))
630 break;
631 }
632
633 // Tried to create regions from entry to lastExit. Next time take a
634 // shortcut from entry to lastExit.
635 if (lastExit != entry)
636 insertShortCut(entry, lastExit, ShortCut);
111 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
112 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
637113 }
638114
639 void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
640 BasicBlock *entry = &(F.getEntryBlock());
641 DomTreeNode *N = DT->getNode(entry);
115 RegionInfoPass::~RegionInfoPass() {
642116
643 // Iterate over the dominance tree in post order to start with the small
644 // regions from the bottom of the dominance tree. If the small regions are
645 // detected first, detection of bigger regions is faster, as we can jump
646 // over the small regions.
647 for (po_iterator FI = po_begin(N), FE = po_end(N); FI != FE;
648 ++FI) {
649 findRegionsWithEntry(FI->getBlock(), ShortCut);
650 }
651117 }
652118
653 Region *RegionInfo::getTopMostParent(Region *region) {
654 while (region->parent)
655 region = region->getParent();
656
657 return region;
658 }
659
660 void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
661 BasicBlock *BB = N->getBlock();
662
663 // Passed region exit
664 while (BB == region->getExit())
665 region = region->getParent();
666
667 BBtoRegionMap::iterator it = BBtoRegion.find(BB);
668
669 // This basic block is a start block of a region. It is already in the
670 // BBtoRegion relation. Only the child basic blocks have to be updated.
671 if (it != BBtoRegion.end()) {
672 Region *newRegion = it->second;
673 region->addSubRegion(getTopMostParent(newRegion));
674 region = newRegion;
675 } else {
676 BBtoRegion[BB] = region;
677 }
678
679 for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
680 buildRegionsTree(*CI, region);
681 }
682
683 void RegionInfo::releaseMemory() {
684 BBtoRegion.clear();
685 if (TopLevelRegion)
686 delete TopLevelRegion;
687 TopLevelRegion = nullptr;
688 }
689
690 RegionInfo::RegionInfo() : FunctionPass(ID) {
691 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
692 TopLevelRegion = nullptr;
693 }
694
695 RegionInfo::~RegionInfo() {
696 releaseMemory();
697 }
698
699 void RegionInfo::Calculate(Function &F) {
700 // ShortCut a function where for every BB the exit of the largest region
701 // starting with BB is stored. These regions can be threated as single BBS.
702 // This improves performance on linear CFGs.
703 BBtoBBMap ShortCut;
704
705 scanForRegions(F, &ShortCut);
706 BasicBlock *BB = &F.getEntryBlock();
707 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
708 }
709
710 bool RegionInfo::runOnFunction(Function &F) {
119 bool RegionInfoPass::runOnFunction(Function &F) {
711120 releaseMemory();
712121
713 DT = &getAnalysis().getDomTree();
714 PDT = &getAnalysis();
715 DF = &getAnalysis();
122 auto DT = &getAnalysis().getDomTree();
123 auto PDT = &getAnalysis();
124 auto DF = &getAnalysis();
716125
717 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr, this, DT, nullptr);
718 updateStatistics(TopLevelRegion);
719
720 Calculate(F);
721
126 RI.recalculate(F, DT, PDT, DF);
722127 return false;
723128 }
724129
725 void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
130 void RegionInfoPass::releaseMemory() {
131 RI.releaseMemory();
132 }
133
134 void RegionInfoPass::verifyAnalysis() const {
135 RI.verifyAnalysis();
136 }
137
138 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
726139 AU.setPreservesAll();
727140 AU.addRequiredTransitive();
728141 AU.addRequired();
729142 AU.addRequired();
730143 }
731144
732 void RegionInfo::print(raw_ostream &OS, const Module *) const {
733 OS << "Region tree:\n";
734 TopLevelRegion->print(OS, true, 0, printStyle.getValue());
735 OS << "End region tree\n";
145 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
146 RI.print(OS);
736147 }
737148
738 void RegionInfo::verifyAnalysis() const {
739 // Only do verification when user wants to, otherwise this expensive check
740 // will be invoked by PMDataManager::verifyPreservedAnalysis when
741 // a regionpass (marked PreservedAll) finish.
742 if (!VerifyRegionInfo) return;
743
744 TopLevelRegion->verifyRegionNest();
149 void RegionInfoPass::dump() const {
150 RI.dump();
745151 }
746152
747 // Region pass manager support.
748 Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
749 BBtoRegionMap::const_iterator I=
750 BBtoRegion.find(BB);
751 return I != BBtoRegion.end() ? I->second : nullptr;
752 }
153 char RegionInfoPass::ID = 0;
753154
754 void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
755 BBtoRegion[BB] = R;
756 }
757
758 Region *RegionInfo::operator[](BasicBlock *BB) const {
759 return getRegionFor(BB);
760 }
761
762 BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
763 BasicBlock *Exit = nullptr;
764
765 while (true) {
766 // Get largest region that starts at BB.
767 Region *R = getRegionFor(BB);
768 while (R && R->getParent() && R->getParent()->getEntry() == BB)
769 R = R->getParent();
770
771 // Get the single exit of BB.
772 if (R && R->getEntry() == BB)
773 Exit = R->getExit();
774 else if (++succ_begin(BB) == succ_end(BB))
775 Exit = *succ_begin(BB);
776 else // No single exit exists.
777 return Exit;
778
779 // Get largest region that starts at Exit.
780 Region *ExitR = getRegionFor(Exit);
781 while (ExitR && ExitR->getParent()
782 && ExitR->getParent()->getEntry() == Exit)
783 ExitR = ExitR->getParent();
784
785 for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
786 ++PI)
787 if (!R->contains(*PI) && !ExitR->contains(*PI))
788 break;
789
790 // This stops infinite cycles.
791 if (DT->dominates(Exit, BB))
792 break;
793
794 BB = Exit;
795 }
796
797 return Exit;
798 }
799
800 Region*
801 RegionInfo::getCommonRegion(Region *A, Region *B) const {
802 assert (A && B && "One of the Regions is NULL");
803
804 if (A->contains(B)) return A;
805
806 while (!B->contains(A))
807 B = B->getParent();
808
809 return B;
810 }
811
812 Region*
813 RegionInfo::getCommonRegion(SmallVectorImpl &Regions) const {
814 Region* ret = Regions.back();
815 Regions.pop_back();
816
817 for (SmallVectorImpl::const_iterator I = Regions.begin(),
818 E = Regions.end(); I != E; ++I)
819 ret = getCommonRegion(ret, *I);
820
821 return ret;
822 }
823
824 Region*
825 RegionInfo::getCommonRegion(SmallVectorImpl &BBs) const {
826 Region* ret = getRegionFor(BBs.back());
827 BBs.pop_back();
828
829 for (SmallVectorImpl::const_iterator I = BBs.begin(),
830 E = BBs.end(); I != E; ++I)
831 ret = getCommonRegion(ret, getRegionFor(*I));
832
833 return ret;
834 }
835
836 void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
837 {
838 Region *R = getRegionFor(OldBB);
839
840 setRegionFor(NewBB, R);
841
842 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
843 R->replaceEntry(NewBB);
844 R = R->getParent();
845 }
846
847 setRegionFor(OldBB, R);
848 }
849
850 char RegionInfo::ID = 0;
851 INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
155 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
852156 "Detect single entry single exit regions", true, true)
853157 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
854158 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
855159 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
856 INITIALIZE_PASS_END(RegionInfo, "regions",
160 INITIALIZE_PASS_END(RegionInfoPass, "regions",
857161 "Detect single entry single exit regions", true, true)
858162
859163 // Create methods available outside of this file, to use them
862166
863167 namespace llvm {
864168 FunctionPass *createRegionInfoPass() {
865 return new RegionInfo();
169 return new RegionInfoPass();
866170 }
867171 }
868172
4444
4545 /// Pass Manager itself does not invalidate any analysis info.
4646 void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
47 Info.addRequired>();
47 Info.addRequiredPass>();
4848 Info.setPreservesAll();
4949 }
5050
5151 /// run - Execute all of the passes scheduled for execution. Keep track of
5252 /// whether any of the passes modifies the function, and if so, return true.
5353 bool RGPassManager::runOnFunction(Function &F) {
54 RI = &getAnalysis>();
54 RI = &getAnalysisPass>().getRegionInfo();
5555 bool Changed = false;
5656
5757 // Collect inherited analysis from Module level pass manager.
5555 };
5656
5757 template<>
58 struct DOTGraphTraits : public DOTGraphTraits {
59
60 DOTGraphTraits (bool isSimple=false)
58 struct DOTGraphTraits : public DOTGraphTraits {
59
60 DOTGraphTraits (bool isSimple = false)
6161 : DOTGraphTraits(isSimple) {}
6262
63 static std::string getGraphName(RegionInfo *DT) {
63 static std::string getGraphName(RegionInfoPass *DT) {
6464 return "Region Graph";
6565 }
6666
67 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
67 std::string getNodeLabel(RegionNode *Node, RegionInfoPass *G) {
68 RegionInfo &RI = G->getRegionInfo();
6869 return DOTGraphTraits::getNodeLabel(Node,
69 G->getTopLevelRegion());
70 reinterpret_cast(RI.getTopLevelRegion()));
7071 }
7172
7273 std::string getEdgeAttributes(RegionNode *srcNode,
73 GraphTraits::ChildIteratorType CI, RegionInfo *RI) {
74
74 GraphTraits::ChildIteratorType CI, RegionInfoPass *G) {
75 RegionInfo &RI = G->getRegionInfo();
7576 RegionNode *destNode = *CI;
7677
7778 if (srcNode->isSubRegion() || destNode->isSubRegion())
8182 BasicBlock *srcBB = srcNode->getNodeAs();
8283 BasicBlock *destBB = destNode->getNodeAs();
8384
84 Region *R = RI->getRegionFor(destBB);
85 Region *R = RI.getRegionFor(destBB);
8586
8687 while (R && R->getParent())
8788 if (R->getParent()->getEntry() == destBB)
9798
9899 // Print the cluster of the subregions. This groups the single basic blocks
99100 // and adds a different background color for each group.
100 static void printRegionCluster(const Region &R, GraphWriter &GW,
101 static void printRegionCluster(const Region &R,
102 GraphWriter &GW,
101103 unsigned depth = 0) {
102104 raw_ostream &O = GW.getOStream();
103105 O.indent(2 * depth) << "subgraph cluster_" << static_cast(&R)
118120 for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI)
119121 printRegionCluster(**RI, GW, depth + 1);
120122
121 RegionInfo *RI = R.getRegionInfo();
123 const RegionInfo &RI = *static_cast(R.getRegionInfo());
122124
123125 for (const auto &BB : R.blocks())
124 if (RI->getRegionFor(BB) == &R)
126 if (RI.getRegionFor(BB) == &R)
125127 O.indent(2 * (depth + 1)) << "Node"
126 << static_cast(RI->getTopLevelRegion()->getBBNode(BB))
128 << static_cast(RI.getTopLevelRegion()->getBBNode(BB))
127129 << ";\n";
128130
129131 O.indent(2 * depth) << "}\n";
130132 }
131133
132 static void addCustomGraphFeatures(const RegionInfo* RI,
133 GraphWriter &GW) {
134 static void addCustomGraphFeatures(const RegionInfoPass* RIP,
135 GraphWriter &GW) {
136 const RegionInfo &RI = RIP->getRegionInfo();
134137 raw_ostream &O = GW.getOStream();
135138 O << "\tcolorscheme = \"paired12\"\n";
136 printRegionCluster(*RI->getTopLevelRegion(), GW, 4);
139 printRegionCluster(*RI.getTopLevelRegion(), GW, 4);
137140 }
138141 };
139142 } //end namespace llvm
141144 namespace {
142145
143146 struct RegionViewer
144 : public DOTGraphTraitsViewer {
145 static char ID;
146 RegionViewer() : DOTGraphTraitsViewer("reg", ID){
147 : public DOTGraphTraitsViewer {
148 static char ID;
149 RegionViewer() : DOTGraphTraitsViewer("reg", ID){
147150 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
148151 }
149152 };
150153 char RegionViewer::ID = 0;
151154
152155 struct RegionOnlyViewer
153 : public DOTGraphTraitsViewer {
154 static char ID;
155 RegionOnlyViewer() : DOTGraphTraitsViewer("regonly", ID) {
156 : public DOTGraphTraitsViewer {
157 static char ID;
158 RegionOnlyViewer() : DOTGraphTraitsViewer("regonly", ID) {
156159 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
157160 }
158161 };
159162 char RegionOnlyViewer::ID = 0;
160163
161164 struct RegionPrinter
162 : public DOTGraphTraitsPrinter, false> {
165 : public DOTGraphTraitsPrinterPass, false> {
163166 static char ID;
164167 RegionPrinter() :
165 DOTGraphTraitsPrinter, false>("reg", ID) {
168 DOTGraphTraitsPrinterPass, false>("reg", ID) {
166169 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
167170 }
168171 };
174177
175178 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
176179 true, true)
177
180
178181 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
179182 "View regions of function (with no function bodies)",
180183 true, true)
182185 namespace {
183186
184187 struct RegionOnlyPrinter
185 : public DOTGraphTraitsPrinter, true> {
188 : public DOTGraphTraitsPrinterPass, true> {
186189 static char ID;
187190 RegionOnlyPrinter() :
188 DOTGraphTraitsPrinter, true>("reg", ID) {
191 DOTGraphTraitsPrinterPass, true>("reg", ID) {
189192 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
190193 }
191194 };
6464 MachinePassRegistry.cpp
6565 MachinePostDominators.cpp
6666 MachineRegisterInfo.cpp
67 MachineRegionInfo.cpp
6768 MachineSSAUpdater.cpp
6869 MachineScheduler.cpp
6970 MachineSink.cpp
0
1 #include "llvm/CodeGen/MachineRegionInfo.h"
2 #include "llvm/CodeGen/MachinePostDominators.h"
3 #include "llvm/ADT/Statistic.h"
4 #include "llvm/Analysis/RegionInfoImpl.h"
5
6 using namespace llvm;
7
8 STATISTIC(numMachineRegions, "The # of machine regions");
9 STATISTIC(numMachineSimpleRegions, "The # of simple machine regions");
10
11 namespace llvm {
12 template class RegionBase>;
13 template class RegionNodeBase>;
14 template class RegionInfoBase>;
15 }
16
17 //===----------------------------------------------------------------------===//
18 // MachineRegion implementation
19 //
20
21 MachineRegion::MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
22 MachineRegionInfo* RI,
23 MachineDominatorTree *DT, MachineRegion *Parent) :
24 RegionBase>(Entry, Exit, RI, DT, Parent) {
25
26 }
27
28 MachineRegion::~MachineRegion() { }
29
30 //===----------------------------------------------------------------------===//
31 // MachineRegionInfo implementation
32 //
33
34 MachineRegionInfo::MachineRegionInfo() :
35 RegionInfoBase>() {
36
37 }
38
39 MachineRegionInfo::~MachineRegionInfo() {
40
41 }
42
43 void MachineRegionInfo::updateStatistics(MachineRegion *R) {
44 ++numMachineRegions;
45
46 // TODO: Slow. Should only be enabled if -stats is used.
47 if (R->isSimple())
48 ++numMachineSimpleRegions;
49 }
50
51 void MachineRegionInfo::MachineRegionInfo::recalculate(
52 MachineFunction &F,
53 MachineDominatorTree *DT_,
54 MachinePostDominatorTree *PDT_,
55 MachineDominanceFrontier *DF_) {
56 DT = DT_;
57 PDT = PDT_;
58 DF = DF_;
59
60 MachineBasicBlock *Entry = GraphTraits::getEntryNode(&F);
61
62 TopLevelRegion = new MachineRegion(Entry, nullptr, this, DT, nullptr);
63 updateStatistics(TopLevelRegion);
64 calculate(F);
65 }
66
67 //===----------------------------------------------------------------------===//
68 // MachineRegionInfoPass implementation
69 //
70
71 MachineRegionInfoPass::MachineRegionInfoPass() : MachineFunctionPass(ID) {
72 initializeMachineRegionInfoPassPass(*PassRegistry::getPassRegistry());
73 }
74
75 MachineRegionInfoPass::~MachineRegionInfoPass() {
76
77 }
78
79 bool MachineRegionInfoPass::runOnMachineFunction(MachineFunction &F) {
80 releaseMemory();
81
82 auto DT = &getAnalysis();
83 auto PDT = &getAnalysis();
84 auto DF = &getAnalysis();
85
86 RI.recalculate(F, DT, PDT, DF);
87 return false;
88 }
89
90 void MachineRegionInfoPass::releaseMemory() {
91 RI.releaseMemory();
92 }
93
94 void MachineRegionInfoPass::verifyAnalysis() const {
95 // Only do verification when user wants to, otherwise this expensive check
96 // will be invoked by PMDataManager::verifyPreservedAnalysis when
97 // a regionpass (marked PreservedAll) finish.
98 if (MachineRegionInfo::VerifyRegionInfo)
99 RI.verifyAnalysis();
100 }
101
102 void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.setPreservesAll();
104 AU.addRequiredTransitive();
105 AU.addRequired();
106 AU.addRequired();
107 }
108
109 void MachineRegionInfoPass::print(raw_ostream &OS, const Module *) const {
110 RI.print(OS);
111 }
112
113