LLVM  9.0.0svn
LoopInfoImpl.h
Go to the documentation of this file.
1 //===- llvm/Analysis/LoopInfoImpl.h - Natural Loop Calculator ---*- 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 // This is the generic implementation of LoopInfo used for both Loops and
10 // MachineLoops.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_LOOPINFOIMPL_H
15 #define LLVM_ANALYSIS_LOOPINFOIMPL_H
16 
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/Dominators.h"
23 
24 namespace llvm {
25 
26 //===----------------------------------------------------------------------===//
27 // APIs for simple analysis of the loop. See header notes.
28 
29 /// getExitingBlocks - Return all blocks inside the loop that have successors
30 /// outside of the loop. These are the blocks _inside of the current loop_
31 /// which branch out. The returned list is always unique.
32 ///
33 template <class BlockT, class LoopT>
35  SmallVectorImpl<BlockT *> &ExitingBlocks) const {
36  assert(!isInvalid() && "Loop not in a valid state!");
37  for (const auto BB : blocks())
38  for (const auto &Succ : children<BlockT *>(BB))
39  if (!contains(Succ)) {
40  // Not in current loop? It must be an exit block.
41  ExitingBlocks.push_back(BB);
42  break;
43  }
44 }
45 
46 /// getExitingBlock - If getExitingBlocks would return exactly one block,
47 /// return that block. Otherwise return null.
48 template <class BlockT, class LoopT>
50  assert(!isInvalid() && "Loop not in a valid state!");
51  SmallVector<BlockT *, 8> ExitingBlocks;
52  getExitingBlocks(ExitingBlocks);
53  if (ExitingBlocks.size() == 1)
54  return ExitingBlocks[0];
55  return nullptr;
56 }
57 
58 /// getExitBlocks - Return all of the successor blocks of this loop. These
59 /// are the blocks _outside of the current loop_ which are branched to.
60 ///
61 template <class BlockT, class LoopT>
63  SmallVectorImpl<BlockT *> &ExitBlocks) const {
64  assert(!isInvalid() && "Loop not in a valid state!");
65  for (const auto BB : blocks())
66  for (const auto &Succ : children<BlockT *>(BB))
67  if (!contains(Succ))
68  // Not in current loop? It must be an exit block.
69  ExitBlocks.push_back(Succ);
70 }
71 
72 /// getExitBlock - If getExitBlocks would return exactly one block,
73 /// return that block. Otherwise return null.
74 template <class BlockT, class LoopT>
76  assert(!isInvalid() && "Loop not in a valid state!");
77  SmallVector<BlockT *, 8> ExitBlocks;
78  getExitBlocks(ExitBlocks);
79  if (ExitBlocks.size() == 1)
80  return ExitBlocks[0];
81  return nullptr;
82 }
83 
84 template <class BlockT, class LoopT>
86  // Each predecessor of each exit block of a normal loop is contained
87  // within the loop.
88  SmallVector<BlockT *, 4> ExitBlocks;
89  getExitBlocks(ExitBlocks);
90  for (BlockT *EB : ExitBlocks)
91  for (BlockT *Predecessor : children<Inverse<BlockT *>>(EB))
92  if (!contains(Predecessor))
93  return false;
94  // All the requirements are met.
95  return true;
96 }
97 
98 // Helper function to get unique loop exits. Pred is a predicate pointing to
99 // BasicBlocks in a loop which should be considered to find loop exits.
100 template <class BlockT, class LoopT, typename PredicateT>
101 void getUniqueExitBlocksHelper(const LoopT *L,
102  SmallVectorImpl<BlockT *> &ExitBlocks,
103  PredicateT Pred) {
104  typedef GraphTraits<BlockT *> BlockTraits;
105  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
106 
107  assert(L->hasDedicatedExits() &&
108  "getUniqueExitBlocks assumes the loop has canonical form exits!");
109 
110  SmallVector<BlockT *, 32> SwitchExitBlocks;
111  auto Filtered = make_filter_range(L->blocks(), Pred);
112  for (BlockT *Block : Filtered) {
113  SwitchExitBlocks.clear();
114  for (BlockT *Successor : children<BlockT *>(Block)) {
115  // If block is inside the loop then it is not an exit block.
116  if (L->contains(Successor))
117  continue;
118 
119  BlockT *FirstPred = *InvBlockTraits::child_begin(Successor);
120 
121  // If current basic block is this exit block's first predecessor then only
122  // insert exit block in to the output ExitBlocks vector. This ensures that
123  // same exit block is not inserted twice into ExitBlocks vector.
124  if (Block != FirstPred)
125  continue;
126 
127  // If a terminator has more then two successors, for example SwitchInst,
128  // then it is possible that there are multiple edges from current block to
129  // one exit block.
130  if (std::distance(BlockTraits::child_begin(Block),
131  BlockTraits::child_end(Block)) <= 2) {
132  ExitBlocks.push_back(Successor);
133  continue;
134  }
135 
136  // In case of multiple edges from current block to exit block, collect
137  // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
138  // duplicate edges.
139  if (!is_contained(SwitchExitBlocks, Successor)) {
140  SwitchExitBlocks.push_back(Successor);
141  ExitBlocks.push_back(Successor);
142  }
143  }
144  }
145 }
146 
147 template <class BlockT, class LoopT>
149  SmallVectorImpl<BlockT *> &ExitBlocks) const {
150  getUniqueExitBlocksHelper(this, ExitBlocks,
151  [](const BlockT *BB) { return true; });
152 }
153 
154 template <class BlockT, class LoopT>
156  SmallVectorImpl<BlockT *> &ExitBlocks) const {
157  const BlockT *Latch = getLoopLatch();
158  assert(Latch && "Latch block must exists");
159  getUniqueExitBlocksHelper(this, ExitBlocks,
160  [Latch](const BlockT *BB) { return BB != Latch; });
161 }
162 
163 template <class BlockT, class LoopT>
165  SmallVector<BlockT *, 8> UniqueExitBlocks;
166  getUniqueExitBlocks(UniqueExitBlocks);
167  if (UniqueExitBlocks.size() == 1)
168  return UniqueExitBlocks[0];
169  return nullptr;
170 }
171 
172 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
173 template <class BlockT, class LoopT>
175  SmallVectorImpl<Edge> &ExitEdges) const {
176  assert(!isInvalid() && "Loop not in a valid state!");
177  for (const auto BB : blocks())
178  for (const auto &Succ : children<BlockT *>(BB))
179  if (!contains(Succ))
180  // Not in current loop? It must be an exit block.
181  ExitEdges.emplace_back(BB, Succ);
182 }
183 
184 /// getLoopPreheader - If there is a preheader for this loop, return it. A
185 /// loop has a preheader if there is only one edge to the header of the loop
186 /// from outside of the loop and it is legal to hoist instructions into the
187 /// predecessor. If this is the case, the block branching to the header of the
188 /// loop is the preheader node.
189 ///
190 /// This method returns null if there is no preheader for the loop.
191 ///
192 template <class BlockT, class LoopT>
194  assert(!isInvalid() && "Loop not in a valid state!");
195  // Keep track of nodes outside the loop branching to the header...
196  BlockT *Out = getLoopPredecessor();
197  if (!Out)
198  return nullptr;
199 
200  // Make sure we are allowed to hoist instructions into the predecessor.
201  if (!Out->isLegalToHoistInto())
202  return nullptr;
203 
204  // Make sure there is only one exit out of the preheader.
205  typedef GraphTraits<BlockT *> BlockTraits;
206  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
207  ++SI;
208  if (SI != BlockTraits::child_end(Out))
209  return nullptr; // Multiple exits from the block, must not be a preheader.
210 
211  // The predecessor has exactly one successor, so it is a preheader.
212  return Out;
213 }
214 
215 /// getLoopPredecessor - If the given loop's header has exactly one unique
216 /// predecessor outside the loop, return it. Otherwise return null.
217 /// This is less strict that the loop "preheader" concept, which requires
218 /// the predecessor to have exactly one successor.
219 ///
220 template <class BlockT, class LoopT>
222  assert(!isInvalid() && "Loop not in a valid state!");
223  // Keep track of nodes outside the loop branching to the header...
224  BlockT *Out = nullptr;
225 
226  // Loop over the predecessors of the header node...
227  BlockT *Header = getHeader();
228  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
229  if (!contains(Pred)) { // If the block is not in the loop...
230  if (Out && Out != Pred)
231  return nullptr; // Multiple predecessors outside the loop
232  Out = Pred;
233  }
234  }
235 
236  // Make sure there is only one exit out of the preheader.
237  assert(Out && "Header of loop has no predecessors from outside loop?");
238  return Out;
239 }
240 
241 /// getLoopLatch - If there is a single latch block for this loop, return it.
242 /// A latch block is a block that contains a branch back to the header.
243 template <class BlockT, class LoopT>
245  assert(!isInvalid() && "Loop not in a valid state!");
246  BlockT *Header = getHeader();
247  BlockT *Latch = nullptr;
248  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
249  if (contains(Pred)) {
250  if (Latch)
251  return nullptr;
252  Latch = Pred;
253  }
254  }
255 
256  return Latch;
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // APIs for updating loop information after changing the CFG
261 //
262 
263 /// addBasicBlockToLoop - This method is used by other analyses to update loop
264 /// information. NewBB is set to be a new member of the current loop.
265 /// Because of this, it is added as a member of all parent loops, and is added
266 /// to the specified LoopInfo object as being in the current basic block. It
267 /// is not valid to replace the loop header with this method.
268 ///
269 template <class BlockT, class LoopT>
271  BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
272  assert(!isInvalid() && "Loop not in a valid state!");
273 #ifndef NDEBUG
274  if (!Blocks.empty()) {
275  auto SameHeader = LIB[getHeader()];
276  assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() &&
277  "Incorrect LI specified for this loop!");
278  }
279 #endif
280  assert(NewBB && "Cannot add a null basic block to the loop!");
281  assert(!LIB[NewBB] && "BasicBlock already in the loop!");
282 
283  LoopT *L = static_cast<LoopT *>(this);
284 
285  // Add the loop mapping to the LoopInfo object...
286  LIB.BBMap[NewBB] = L;
287 
288  // Add the basic block to this loop and all parent loops...
289  while (L) {
290  L->addBlockEntry(NewBB);
291  L = L->getParentLoop();
292  }
293 }
294 
295 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
296 /// the OldChild entry in our children list with NewChild, and updates the
297 /// parent pointer of OldChild to be null and the NewChild to be this loop.
298 /// This updates the loop depth of the new child.
299 template <class BlockT, class LoopT>
301  LoopT *NewChild) {
302  assert(!isInvalid() && "Loop not in a valid state!");
303  assert(OldChild->ParentLoop == this && "This loop is already broken!");
304  assert(!NewChild->ParentLoop && "NewChild already has a parent!");
305  typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
306  assert(I != SubLoops.end() && "OldChild not in loop!");
307  *I = NewChild;
308  OldChild->ParentLoop = nullptr;
309  NewChild->ParentLoop = static_cast<LoopT *>(this);
310 }
311 
312 /// verifyLoop - Verify loop structure
313 template <class BlockT, class LoopT>
315  assert(!isInvalid() && "Loop not in a valid state!");
316 #ifndef NDEBUG
317  assert(!Blocks.empty() && "Loop header is missing");
318 
319  // Setup for using a depth-first iterator to visit every block in the loop.
320  SmallVector<BlockT *, 8> ExitBBs;
321  getExitBlocks(ExitBBs);
323  VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
325  BI = df_ext_begin(getHeader(), VisitSet),
326  BE = df_ext_end(getHeader(), VisitSet);
327 
328  // Keep track of the BBs visited.
329  SmallPtrSet<BlockT *, 8> VisitedBBs;
330 
331  // Check the individual blocks.
332  for (; BI != BE; ++BI) {
333  BlockT *BB = *BI;
334 
337  [&](BlockT *B) { return contains(B); }) &&
338  "Loop block has no in-loop successors!");
339 
341  GraphTraits<Inverse<BlockT *>>::child_end(BB),
342  [&](BlockT *B) { return contains(B); }) &&
343  "Loop block has no in-loop predecessors!");
344 
345  SmallVector<BlockT *, 2> OutsideLoopPreds;
346  std::for_each(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
347  GraphTraits<Inverse<BlockT *>>::child_end(BB),
348  [&](BlockT *B) {
349  if (!contains(B))
350  OutsideLoopPreds.push_back(B);
351  });
352 
353  if (BB == getHeader()) {
354  assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
355  } else if (!OutsideLoopPreds.empty()) {
356  // A non-header loop shouldn't be reachable from outside the loop,
357  // though it is permitted if the predecessor is not itself actually
358  // reachable.
359  BlockT *EntryBB = &BB->getParent()->front();
360  for (BlockT *CB : depth_first(EntryBB))
361  for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
362  assert(CB != OutsideLoopPreds[i] &&
363  "Loop has multiple entry points!");
364  }
365  assert(BB != &getHeader()->getParent()->front() &&
366  "Loop contains function entry block!");
367 
368  VisitedBBs.insert(BB);
369  }
370 
371  if (VisitedBBs.size() != getNumBlocks()) {
372  dbgs() << "The following blocks are unreachable in the loop: ";
373  for (auto BB : Blocks) {
374  if (!VisitedBBs.count(BB)) {
375  dbgs() << *BB << "\n";
376  }
377  }
378  assert(false && "Unreachable block in loop");
379  }
380 
381  // Check the subloops.
382  for (iterator I = begin(), E = end(); I != E; ++I)
383  // Each block in each subloop should be contained within this loop.
384  for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
385  BI != BE; ++BI) {
386  assert(contains(*BI) &&
387  "Loop does not contain all the blocks of a subloop!");
388  }
389 
390  // Check the parent loop pointer.
391  if (ParentLoop) {
392  assert(is_contained(*ParentLoop, this) &&
393  "Loop is not a subloop of its parent!");
394  }
395 #endif
396 }
397 
398 /// verifyLoop - Verify loop structure of this loop and all nested loops.
399 template <class BlockT, class LoopT>
402  assert(!isInvalid() && "Loop not in a valid state!");
403  Loops->insert(static_cast<const LoopT *>(this));
404  // Verify this loop.
405  verifyLoop();
406  // Verify the subloops.
407  for (iterator I = begin(), E = end(); I != E; ++I)
408  (*I)->verifyLoopNest(Loops);
409 }
410 
411 template <class BlockT, class LoopT>
413  bool Verbose) const {
414  OS.indent(Depth * 2);
415  if (static_cast<const LoopT *>(this)->isAnnotatedParallel())
416  OS << "Parallel ";
417  OS << "Loop at depth " << getLoopDepth() << " containing: ";
418 
419  BlockT *H = getHeader();
420  for (unsigned i = 0; i < getBlocks().size(); ++i) {
421  BlockT *BB = getBlocks()[i];
422  if (!Verbose) {
423  if (i)
424  OS << ",";
425  BB->printAsOperand(OS, false);
426  } else
427  OS << "\n";
428 
429  if (BB == H)
430  OS << "<header>";
431  if (isLoopLatch(BB))
432  OS << "<latch>";
433  if (isLoopExiting(BB))
434  OS << "<exiting>";
435  if (Verbose)
436  BB->print(OS);
437  }
438  OS << "\n";
439 
440  for (iterator I = begin(), E = end(); I != E; ++I)
441  (*I)->print(OS, Depth + 2);
442 }
443 
444 //===----------------------------------------------------------------------===//
445 /// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
446 /// result does / not depend on use list (block predecessor) order.
447 ///
448 
449 /// Discover a subloop with the specified backedges such that: All blocks within
450 /// this loop are mapped to this loop or a subloop. And all subloops within this
451 /// loop have their parent loop set to this loop or a subloop.
452 template <class BlockT, class LoopT>
453 static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT *> Backedges,
455  const DomTreeBase<BlockT> &DomTree) {
456  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
457 
458  unsigned NumBlocks = 0;
459  unsigned NumSubloops = 0;
460 
461  // Perform a backward CFG traversal using a worklist.
462  std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
463  while (!ReverseCFGWorklist.empty()) {
464  BlockT *PredBB = ReverseCFGWorklist.back();
465  ReverseCFGWorklist.pop_back();
466 
467  LoopT *Subloop = LI->getLoopFor(PredBB);
468  if (!Subloop) {
469  if (!DomTree.isReachableFromEntry(PredBB))
470  continue;
471 
472  // This is an undiscovered block. Map it to the current loop.
473  LI->changeLoopFor(PredBB, L);
474  ++NumBlocks;
475  if (PredBB == L->getHeader())
476  continue;
477  // Push all block predecessors on the worklist.
478  ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
479  InvBlockTraits::child_begin(PredBB),
480  InvBlockTraits::child_end(PredBB));
481  } else {
482  // This is a discovered block. Find its outermost discovered loop.
483  while (LoopT *Parent = Subloop->getParentLoop())
484  Subloop = Parent;
485 
486  // If it is already discovered to be a subloop of this loop, continue.
487  if (Subloop == L)
488  continue;
489 
490  // Discover a subloop of this loop.
491  Subloop->setParentLoop(L);
492  ++NumSubloops;
493  NumBlocks += Subloop->getBlocksVector().capacity();
494  PredBB = Subloop->getHeader();
495  // Continue traversal along predecessors that are not loop-back edges from
496  // within this subloop tree itself. Note that a predecessor may directly
497  // reach another subloop that is not yet discovered to be a subloop of
498  // this loop, which we must traverse.
499  for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
500  if (LI->getLoopFor(Pred) != Subloop)
501  ReverseCFGWorklist.push_back(Pred);
502  }
503  }
504  }
505  L->getSubLoopsVector().reserve(NumSubloops);
506  L->reserveBlocks(NumBlocks);
507 }
508 
509 /// Populate all loop data in a stable order during a single forward DFS.
510 template <class BlockT, class LoopT> class PopulateLoopsDFS {
512  typedef typename BlockTraits::ChildIteratorType SuccIterTy;
513 
515 
516 public:
518 
519  void traverse(BlockT *EntryBlock);
520 
521 protected:
522  void insertIntoLoop(BlockT *Block);
523 };
524 
525 /// Top-level driver for the forward DFS within the loop.
526 template <class BlockT, class LoopT>
528  for (BlockT *BB : post_order(EntryBlock))
529  insertIntoLoop(BB);
530 }
531 
532 /// Add a single Block to its ancestor loops in PostOrder. If the block is a
533 /// subloop header, add the subloop to its parent in PostOrder, then reverse the
534 /// Block and Subloop vectors of the now complete subloop to achieve RPO.
535 template <class BlockT, class LoopT>
537  LoopT *Subloop = LI->getLoopFor(Block);
538  if (Subloop && Block == Subloop->getHeader()) {
539  // We reach this point once per subloop after processing all the blocks in
540  // the subloop.
541  if (Subloop->getParentLoop())
542  Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
543  else
544  LI->addTopLevelLoop(Subloop);
545 
546  // For convenience, Blocks and Subloops are inserted in postorder. Reverse
547  // the lists, except for the loop header, which is always at the beginning.
548  Subloop->reverseBlock(1);
549  std::reverse(Subloop->getSubLoopsVector().begin(),
550  Subloop->getSubLoopsVector().end());
551 
552  Subloop = Subloop->getParentLoop();
553  }
554  for (; Subloop; Subloop = Subloop->getParentLoop())
555  Subloop->addBlockEntry(Block);
556 }
557 
558 /// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
559 /// interleaved with backward CFG traversals within each subloop
560 /// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
561 /// this part of the algorithm is linear in the number of CFG edges. Subloop and
562 /// Block vectors are then populated during a single forward CFG traversal
563 /// (PopulateLoopDFS).
564 ///
565 /// During the two CFG traversals each block is seen three times:
566 /// 1) Discovered and mapped by a reverse CFG traversal.
567 /// 2) Visited during a forward DFS CFG traversal.
568 /// 3) Reverse-inserted in the loop in postorder following forward DFS.
569 ///
570 /// The Block vectors are inclusive, so step 3 requires loop-depth number of
571 /// insertions per block.
572 template <class BlockT, class LoopT>
574  // Postorder traversal of the dominator tree.
575  const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
576  for (auto DomNode : post_order(DomRoot)) {
577 
578  BlockT *Header = DomNode->getBlock();
579  SmallVector<BlockT *, 4> Backedges;
580 
581  // Check each predecessor of the potential loop header.
582  for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
583  // If Header dominates predBB, this is a new loop. Collect the backedges.
584  if (DomTree.dominates(Header, Backedge) &&
585  DomTree.isReachableFromEntry(Backedge)) {
586  Backedges.push_back(Backedge);
587  }
588  }
589  // Perform a backward CFG traversal to discover and map blocks in this loop.
590  if (!Backedges.empty()) {
591  LoopT *L = AllocateLoop(Header);
592  discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
593  }
594  }
595  // Perform a single forward CFG traversal to populate block and subloop
596  // vectors for all loops.
598  DFS.traverse(DomRoot->getBlock());
599 }
600 
601 template <class BlockT, class LoopT>
603  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
604  // The outer-most loop actually goes into the result in the same relative
605  // order as we walk it. But LoopInfo stores the top level loops in reverse
606  // program order so for here we reverse it to get forward program order.
607  // FIXME: If we change the order of LoopInfo we will want to remove the
608  // reverse here.
609  for (LoopT *RootL : reverse(*this)) {
610  auto PreOrderLoopsInRootL = RootL->getLoopsInPreorder();
611  PreOrderLoops.append(PreOrderLoopsInRootL.begin(),
612  PreOrderLoopsInRootL.end());
613  }
614 
615  return PreOrderLoops;
616 }
617 
618 template <class BlockT, class LoopT>
621  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
622  // The outer-most loop actually goes into the result in the same relative
623  // order as we walk it. LoopInfo stores the top level loops in reverse
624  // program order so we walk in order here.
625  // FIXME: If we change the order of LoopInfo we will want to add a reverse
626  // here.
627  for (LoopT *RootL : *this) {
628  assert(PreOrderWorklist.empty() &&
629  "Must start with an empty preorder walk worklist.");
630  PreOrderWorklist.push_back(RootL);
631  do {
632  LoopT *L = PreOrderWorklist.pop_back_val();
633  // Sub-loops are stored in forward program order, but will process the
634  // worklist backwards so we can just append them in order.
635  PreOrderWorklist.append(L->begin(), L->end());
636  PreOrderLoops.push_back(L);
637  } while (!PreOrderWorklist.empty());
638  }
639 
640  return PreOrderLoops;
641 }
642 
643 // Debugging
644 template <class BlockT, class LoopT>
646  for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
647  TopLevelLoops[i]->print(OS);
648 #if 0
650  E = BBMap.end(); I != E; ++I)
651  OS << "BB '" << I->first->getName() << "' level = "
652  << I->second->getLoopDepth() << "\n";
653 #endif
654 }
655 
656 template <typename T>
657 bool compareVectors(std::vector<T> &BB1, std::vector<T> &BB2) {
658  llvm::sort(BB1);
659  llvm::sort(BB2);
660  return BB1 == BB2;
661 }
662 
663 template <class BlockT, class LoopT>
665  const LoopInfoBase<BlockT, LoopT> &LI,
666  const LoopT &L) {
667  LoopHeaders[L.getHeader()] = &L;
668  for (LoopT *SL : L)
669  addInnerLoopsToHeadersMap(LoopHeaders, LI, *SL);
670 }
671 
672 #ifndef NDEBUG
673 template <class BlockT, class LoopT>
674 static void compareLoops(const LoopT *L, const LoopT *OtherL,
675  DenseMap<BlockT *, const LoopT *> &OtherLoopHeaders) {
676  BlockT *H = L->getHeader();
677  BlockT *OtherH = OtherL->getHeader();
678  assert(H == OtherH &&
679  "Mismatched headers even though found in the same map entry!");
680 
681  assert(L->getLoopDepth() == OtherL->getLoopDepth() &&
682  "Mismatched loop depth!");
683  const LoopT *ParentL = L, *OtherParentL = OtherL;
684  do {
685  assert(ParentL->getHeader() == OtherParentL->getHeader() &&
686  "Mismatched parent loop headers!");
687  ParentL = ParentL->getParentLoop();
688  OtherParentL = OtherParentL->getParentLoop();
689  } while (ParentL);
690 
691  for (const LoopT *SubL : *L) {
692  BlockT *SubH = SubL->getHeader();
693  const LoopT *OtherSubL = OtherLoopHeaders.lookup(SubH);
694  assert(OtherSubL && "Inner loop is missing in computed loop info!");
695  OtherLoopHeaders.erase(SubH);
696  compareLoops(SubL, OtherSubL, OtherLoopHeaders);
697  }
698 
699  std::vector<BlockT *> BBs = L->getBlocks();
700  std::vector<BlockT *> OtherBBs = OtherL->getBlocks();
701  assert(compareVectors(BBs, OtherBBs) &&
702  "Mismatched basic blocks in the loops!");
703 
704  const SmallPtrSetImpl<const BlockT *> &BlocksSet = L->getBlocksSet();
705  const SmallPtrSetImpl<const BlockT *> &OtherBlocksSet = L->getBlocksSet();
706  assert(BlocksSet.size() == OtherBlocksSet.size() &&
707  std::all_of(BlocksSet.begin(), BlocksSet.end(),
708  [&OtherBlocksSet](const BlockT *BB) {
709  return OtherBlocksSet.count(BB);
710  }) &&
711  "Mismatched basic blocks in BlocksSets!");
712 }
713 #endif
714 
715 template <class BlockT, class LoopT>
717  const DomTreeBase<BlockT> &DomTree) const {
719  for (iterator I = begin(), E = end(); I != E; ++I) {
720  assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
721  (*I)->verifyLoopNest(&Loops);
722  }
723 
724 // Verify that blocks are mapped to valid loops.
725 #ifndef NDEBUG
726  for (auto &Entry : BBMap) {
727  const BlockT *BB = Entry.first;
728  LoopT *L = Entry.second;
729  assert(Loops.count(L) && "orphaned loop");
730  assert(L->contains(BB) && "orphaned block");
731  for (LoopT *ChildLoop : *L)
732  assert(!ChildLoop->contains(BB) &&
733  "BBMap should point to the innermost loop containing BB");
734  }
735 
736  // Recompute LoopInfo to verify loops structure.
738  OtherLI.analyze(DomTree);
739 
740  // Build a map we can use to move from our LI to the computed one. This
741  // allows us to ignore the particular order in any layer of the loop forest
742  // while still comparing the structure.
743  DenseMap<BlockT *, const LoopT *> OtherLoopHeaders;
744  for (LoopT *L : OtherLI)
745  addInnerLoopsToHeadersMap(OtherLoopHeaders, OtherLI, *L);
746 
747  // Walk the top level loops and ensure there is a corresponding top-level
748  // loop in the computed version and then recursively compare those loop
749  // nests.
750  for (LoopT *L : *this) {
751  BlockT *Header = L->getHeader();
752  const LoopT *OtherL = OtherLoopHeaders.lookup(Header);
753  assert(OtherL && "Top level loop is missing in computed loop info!");
754  // Now that we've matched this loop, erase its header from the map.
755  OtherLoopHeaders.erase(Header);
756  // And recursively compare these loops.
757  compareLoops(L, OtherL, OtherLoopHeaders);
758  }
759 
760  // Any remaining entries in the map are loops which were found when computing
761  // a fresh LoopInfo but not present in the current one.
762  if (!OtherLoopHeaders.empty()) {
763  for (const auto &HeaderAndLoop : OtherLoopHeaders)
764  dbgs() << "Found new loop: " << *HeaderAndLoop.second << "\n";
765  llvm_unreachable("Found new loops when recomputing LoopInfo!");
766  }
767 #endif
768 }
769 
770 } // End llvm namespace
771 
772 #endif
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:641
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:121
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:244
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void compareLoops(const LoopT *L, const LoopT *OtherL, DenseMap< BlockT *, const LoopT *> &OtherLoopHeaders)
Definition: LoopInfoImpl.h:674
iterator begin() const
Definition: ArrayRef.h:136
bool compareVectors(std::vector< T > &BB1, std::vector< T > &BB2)
Definition: LoopInfoImpl.h:657
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfoImpl.h:85
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
void push_back(const T &Elt)
Definition: SmallVector.h:211
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:193
static void discoverAndMapSubloop(LoopT *L, ArrayRef< BlockT *> Backedges, LoopInfoBase< BlockT, LoopT > *LI, const DomTreeBase< BlockT > &DomTree)
Stable LoopInfo Analysis - Build a loop tree using stable iterators so the result does / not depend o...
Definition: LoopInfoImpl.h:453
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild)
This is used when splitting loops up.
Definition: LoopInfoImpl.h:300
void insertIntoLoop(BlockT *Block)
Add a single Block to its ancestor loops in PostOrder.
Definition: LoopInfoImpl.h:536
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1192
return AArch64::GPR64RegClass contains(Reg)
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
Definition: LoopInfoImpl.h:716
void print(raw_ostream &OS, unsigned Depth=0, bool Verbose=false) const
Print loop with all the BBs inside it.
Definition: LoopInfoImpl.h:412
Hexagon Hardware Loops
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:901
std::vector< Loop *>::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Definition: LoopInfo.h:873
void addInnerLoopsToHeadersMap(DenseMap< BlockT *, const LoopT *> &LoopHeaders, const LoopInfoBase< BlockT, LoopT > &LI, const LoopT &L)
Definition: LoopInfoImpl.h:664
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:62
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:273
std::vector< Loop *>::const_iterator iterator
Definition: LoopInfo.h:141
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:270
void traverse(BlockT *EntryBlock)
Top-level driver for the forward DFS within the loop.
Definition: LoopInfoImpl.h:527
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Core dominator tree base class.
Definition: LoopInfo.h:63
NodeT * getBlock() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
PopulateLoopsDFS(LoopInfoBase< BlockT, LoopT > *li)
Definition: LoopInfoImpl.h:517
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define H(x, y, z)
Definition: MD5.cpp:57
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
Definition: LoopInfoImpl.h:573
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1199
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
iterator_range< po_iterator< T > > post_order(const T &G)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:75
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
void getExitingBlocks(SmallVectorImpl< BlockT *> &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Definition: LoopInfoImpl.h:34
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:164
size_t size() const
Definition: SmallVector.h:52
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1213
void getUniqueNonLatchExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all unique successor blocks of this loop except successors from Latch block are not considered...
Definition: LoopInfoImpl.h:155
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1122
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
Return all pairs of (inside_block,outside_block).
Definition: LoopInfoImpl.h:174
size_type size() const
Definition: SmallPtrSet.h:92
BlockT * getLoopPredecessor() const
If the given loop&#39;s header has exactly one unique predecessor outside the loop, return it...
Definition: LoopInfoImpl.h:221
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
void print(raw_ostream &OS) const
Definition: LoopInfoImpl.h:645
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
iterator end() const
Definition: ArrayRef.h:137
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
std::pair< iterator, bool > insert(NodeRef N)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static void DFS(BasicBlock *Root, SetVector< BasicBlock *> &Set)
void getUniqueExitBlocksHelper(const LoopT *L, SmallVectorImpl< BlockT *> &ExitBlocks, PredicateT Pred)
Definition: LoopInfoImpl.h:101
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
void verifyLoopNest(DenseSet< const LoopT *> *Loops) const
Verify loop structure of this loop and all nested loops.
Definition: LoopInfoImpl.h:400
iterator begin() const
Definition: SmallPtrSet.h:396
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
SmallVector< LoopT *, 4 > getLoopsInReverseSiblingPreorder()
Return all of the loops in the function in preorder across the loop nests, with siblings in reverse p...
Definition: LoopInfoImpl.h:620
#define I(x, y, z)
Definition: MD5.cpp:58
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
Definition: LoopInfo.h:933
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition: STLExtras.h:434
iterator end() const
Definition: SmallPtrSet.h:401
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:211
SmallVector< LoopT *, 4 > getLoopsInPreorder()
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
Definition: LoopInfoImpl.h:602
iterator_range< df_iterator< T > > depth_first(const T &G)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void verifyLoop() const
Verify loop structure.
Definition: LoopInfoImpl.h:314
void getUniqueExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all unique successor blocks of this loop.
Definition: LoopInfoImpl.h:148
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1185
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:49
Populate all loop data in a stable order during a single forward DFS.
Definition: LoopInfoImpl.h:510
This class builds and contains all of the top-level loop structures in the specified function...
Definition: LoopInfo.h:64
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1251