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