Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/LoopInfoImpl.h
Line
Count
Source (jump to first uncovered line)
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
17
#include "llvm/ADT/DepthFirstIterator.h"
18
#include "llvm/ADT/PostOrderIterator.h"
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>
34
void LoopBase<BlockT, LoopT>::getExitingBlocks(
35
7.06M
    SmallVectorImpl<BlockT *> &ExitingBlocks) const {
36
7.06M
  assert(!isInvalid() && "Loop not in a valid state!");
37
7.06M
  for (const auto BB : blocks())
38
28.2M
    for (const auto &Succ : children<BlockT *>(BB))
39
42.7M
      if (!contains(Succ)) {
40
9.30M
        // Not in current loop? It must be an exit block.
41
9.30M
        ExitingBlocks.push_back(BB);
42
9.30M
        break;
43
9.30M
      }
44
7.06M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getExitingBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const
Line
Count
Source
35
7.02M
    SmallVectorImpl<BlockT *> &ExitingBlocks) const {
36
7.02M
  assert(!isInvalid() && "Loop not in a valid state!");
37
7.02M
  for (const auto BB : blocks())
38
26.5M
    for (const auto &Succ : children<BlockT *>(BB))
39
40.0M
      if (!contains(Succ)) {
40
9.19M
        // Not in current loop? It must be an exit block.
41
9.19M
        ExitingBlocks.push_back(BB);
42
9.19M
        break;
43
9.19M
      }
44
7.02M
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getExitingBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const
Line
Count
Source
35
45.4k
    SmallVectorImpl<BlockT *> &ExitingBlocks) const {
36
45.4k
  assert(!isInvalid() && "Loop not in a valid state!");
37
45.4k
  for (const auto BB : blocks())
38
1.66M
    for (const auto &Succ : children<BlockT *>(BB))
39
2.68M
      if (!contains(Succ)) {
40
106k
        // Not in current loop? It must be an exit block.
41
106k
        ExitingBlocks.push_back(BB);
42
106k
        break;
43
106k
      }
44
45.4k
}
45
46
/// getExitingBlock - If getExitingBlocks would return exactly one block,
47
/// return that block. Otherwise return null.
48
template <class BlockT, class LoopT>
49
3.24M
BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
50
3.24M
  assert(!isInvalid() && "Loop not in a valid state!");
51
3.24M
  SmallVector<BlockT *, 8> ExitingBlocks;
52
3.24M
  getExitingBlocks(ExitingBlocks);
53
3.24M
  if (ExitingBlocks.size() == 1)
54
2.77M
    return ExitingBlocks[0];
55
468k
  return nullptr;
56
468k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getExitingBlock() const
Line
Count
Source
49
3.24M
BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
50
3.24M
  assert(!isInvalid() && "Loop not in a valid state!");
51
3.24M
  SmallVector<BlockT *, 8> ExitingBlocks;
52
3.24M
  getExitingBlocks(ExitingBlocks);
53
3.24M
  if (ExitingBlocks.size() == 1)
54
2.77M
    return ExitingBlocks[0];
55
468k
  return nullptr;
56
468k
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getExitingBlock() const
Line
Count
Source
49
35
BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
50
35
  assert(!isInvalid() && "Loop not in a valid state!");
51
35
  SmallVector<BlockT *, 8> ExitingBlocks;
52
35
  getExitingBlocks(ExitingBlocks);
53
35
  if (ExitingBlocks.size() == 1)
54
17
    return ExitingBlocks[0];
55
18
  return nullptr;
56
18
}
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>
62
void LoopBase<BlockT, LoopT>::getExitBlocks(
63
8.60M
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
64
8.60M
  assert(!isInvalid() && "Loop not in a valid state!");
65
8.60M
  for (const auto BB : blocks())
66
47.4M
    for (const auto &Succ : children<BlockT *>(BB))
67
79.6M
      if (!contains(Succ))
68
12.0M
        // Not in current loop? It must be an exit block.
69
12.0M
        ExitBlocks.push_back(Succ);
70
8.60M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const
Line
Count
Source
63
8.29M
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
64
8.29M
  assert(!isInvalid() && "Loop not in a valid state!");
65
8.29M
  for (const auto BB : blocks())
66
46.1M
    for (const auto &Succ : children<BlockT *>(BB))
67
77.3M
      if (!contains(Succ))
68
11.6M
        // Not in current loop? It must be an exit block.
69
11.6M
        ExitBlocks.push_back(Succ);
70
8.29M
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const
Line
Count
Source
63
306k
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
64
306k
  assert(!isInvalid() && "Loop not in a valid state!");
65
306k
  for (const auto BB : blocks())
66
1.33M
    for (const auto &Succ : children<BlockT *>(BB))
67
2.28M
      if (!contains(Succ))
68
407k
        // Not in current loop? It must be an exit block.
69
407k
        ExitBlocks.push_back(Succ);
70
306k
}
71
72
/// getExitBlock - If getExitBlocks would return exactly one block,
73
/// return that block. Otherwise return null.
74
template <class BlockT, class LoopT>
75
254k
BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
76
254k
  assert(!isInvalid() && "Loop not in a valid state!");
77
254k
  SmallVector<BlockT *, 8> ExitBlocks;
78
254k
  getExitBlocks(ExitBlocks);
79
254k
  if (ExitBlocks.size() == 1)
80
200k
    return ExitBlocks[0];
81
54.4k
  return nullptr;
82
54.4k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getExitBlock() const
Line
Count
Source
75
254k
BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
76
254k
  assert(!isInvalid() && "Loop not in a valid state!");
77
254k
  SmallVector<BlockT *, 8> ExitBlocks;
78
254k
  getExitBlocks(ExitBlocks);
79
254k
  if (ExitBlocks.size() == 1)
80
200k
    return ExitBlocks[0];
81
54.4k
  return nullptr;
82
54.4k
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getExitBlock() const
83
84
template <class BlockT, class LoopT>
85
4.64M
bool LoopBase<BlockT, LoopT>::hasDedicatedExits() const {
86
4.64M
  // Each predecessor of each exit block of a normal loop is contained
87
4.64M
  // within the loop.
88
4.64M
  SmallVector<BlockT *, 4> ExitBlocks;
89
4.64M
  getExitBlocks(ExitBlocks);
90
4.64M
  for (BlockT *EB : ExitBlocks)
91
6.33M
    for (BlockT *Predecessor : children<Inverse<BlockT *>>(EB))
92
12.7M
      if (!contains(Predecessor))
93
89
        return false;
94
4.64M
  // All the requirements are met.
95
4.64M
  
return true4.64M
;
96
4.64M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::hasDedicatedExits() const
Line
Count
Source
85
4.64M
bool LoopBase<BlockT, LoopT>::hasDedicatedExits() const {
86
4.64M
  // Each predecessor of each exit block of a normal loop is contained
87
4.64M
  // within the loop.
88
4.64M
  SmallVector<BlockT *, 4> ExitBlocks;
89
4.64M
  getExitBlocks(ExitBlocks);
90
4.64M
  for (BlockT *EB : ExitBlocks)
91
6.33M
    for (BlockT *Predecessor : children<Inverse<BlockT *>>(EB))
92
12.7M
      if (!contains(Predecessor))
93
89
        return false;
94
4.64M
  // All the requirements are met.
95
4.64M
  
return true4.64M
;
96
4.64M
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::hasDedicatedExits() const
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
1.95M
                               PredicateT Pred) {
104
1.95M
  assert(!L->isInvalid() && "Loop not in a valid state!");
105
1.95M
  SmallPtrSet<BlockT *, 32> Visited;
106
1.95M
  auto Filtered = make_filter_range(L->blocks(), Pred);
107
1.95M
  for (BlockT *BB : Filtered)
108
7.17M
    for (BlockT *Successor : children<BlockT *>(BB))
109
12.1M
      if (!L->contains(Successor))
110
2.09M
        if (Visited.insert(Successor).second)
111
1.95M
          ExitBlocks.push_back(Successor);
112
1.95M
}
void llvm::getUniqueExitBlocksHelper<llvm::BasicBlock, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*)>(llvm::LoopBase<llvm::BasicBlock, llvm::Loop> const*, llvm::SmallVectorImpl<llvm::BasicBlock*>&, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*))
Line
Count
Source
103
1.55M
                               PredicateT Pred) {
104
1.55M
  assert(!L->isInvalid() && "Loop not in a valid state!");
105
1.55M
  SmallPtrSet<BlockT *, 32> Visited;
106
1.55M
  auto Filtered = make_filter_range(L->blocks(), Pred);
107
1.55M
  for (BlockT *BB : Filtered)
108
5.79M
    for (BlockT *Successor : children<BlockT *>(BB))
109
9.89M
      if (!L->contains(Successor))
110
1.93M
        if (Visited.insert(Successor).second)
111
1.81M
          ExitBlocks.push_back(Successor);
112
1.55M
}
void llvm::getUniqueExitBlocksHelper<llvm::BasicBlock, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*)>(llvm::LoopBase<llvm::BasicBlock, llvm::Loop> const*, llvm::SmallVectorImpl<llvm::BasicBlock*>&, llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*))
Line
Count
Source
103
393k
                               PredicateT Pred) {
104
393k
  assert(!L->isInvalid() && "Loop not in a valid state!");
105
393k
  SmallPtrSet<BlockT *, 32> Visited;
106
393k
  auto Filtered = make_filter_range(L->blocks(), Pred);
107
393k
  for (BlockT *BB : Filtered)
108
1.37M
    for (BlockT *Successor : children<BlockT *>(BB))
109
2.24M
      if (!L->contains(Successor))
110
163k
        if (Visited.insert(Successor).second)
111
140k
          ExitBlocks.push_back(Successor);
112
393k
}
Unexecuted instantiation: void llvm::getUniqueExitBlocksHelper<llvm::MachineBasicBlock, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*)>(llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop> const*, llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*))
Unexecuted instantiation: void llvm::getUniqueExitBlocksHelper<llvm::MachineBasicBlock, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*)>(llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop> const*, llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&, llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*))
113
114
template <class BlockT, class LoopT>
115
void LoopBase<BlockT, LoopT>::getUniqueExitBlocks(
116
1.55M
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
117
1.55M
  getUniqueExitBlocksHelper(this, ExitBlocks,
118
5.79M
                            [](const BlockT *BB) { return true; });
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*)::operator()(llvm::BasicBlock const*) const
Line
Count
Source
118
5.79M
                            [](const BlockT *BB) { return true; });
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*)::operator()(llvm::MachineBasicBlock const*) const
119
1.55M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const
Line
Count
Source
116
1.55M
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
117
1.55M
  getUniqueExitBlocksHelper(this, ExitBlocks,
118
1.55M
                            [](const BlockT *BB) { return true; });
119
1.55M
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const
120
121
template <class BlockT, class LoopT>
122
void LoopBase<BlockT, LoopT>::getUniqueNonLatchExitBlocks(
123
393k
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
124
393k
  const BlockT *Latch = getLoopLatch();
125
393k
  assert(Latch && "Latch block must exists");
126
393k
  getUniqueExitBlocksHelper(this, ExitBlocks,
127
1.77M
                            [Latch](const BlockT *BB) { return BB != Latch; });
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const::'lambda'(llvm::BasicBlock const*)::operator()(llvm::BasicBlock const*) const
Line
Count
Source
127
1.77M
                            [Latch](const BlockT *BB) { return BB != Latch; });
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const::'lambda'(llvm::MachineBasicBlock const*)::operator()(llvm::MachineBasicBlock const*) const
128
393k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::BasicBlock*>&) const
Line
Count
Source
123
393k
    SmallVectorImpl<BlockT *> &ExitBlocks) const {
124
393k
  const BlockT *Latch = getLoopLatch();
125
393k
  assert(Latch && "Latch block must exists");
126
393k
  getUniqueExitBlocksHelper(this, ExitBlocks,
127
393k
                            [Latch](const BlockT *BB) { return BB != Latch; });
128
393k
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueNonLatchExitBlocks(llvm::SmallVectorImpl<llvm::MachineBasicBlock*>&) const
129
130
template <class BlockT, class LoopT>
131
471k
BlockT *LoopBase<BlockT, LoopT>::getUniqueExitBlock() const {
132
471k
  SmallVector<BlockT *, 8> UniqueExitBlocks;
133
471k
  getUniqueExitBlocks(UniqueExitBlocks);
134
471k
  if (UniqueExitBlocks.size() == 1)
135
442k
    return UniqueExitBlocks[0];
136
28.9k
  return nullptr;
137
28.9k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getUniqueExitBlock() const
Line
Count
Source
131
471k
BlockT *LoopBase<BlockT, LoopT>::getUniqueExitBlock() const {
132
471k
  SmallVector<BlockT *, 8> UniqueExitBlocks;
133
471k
  getUniqueExitBlocks(UniqueExitBlocks);
134
471k
  if (UniqueExitBlocks.size() == 1)
135
442k
    return UniqueExitBlocks[0];
136
28.9k
  return nullptr;
137
28.9k
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getUniqueExitBlock() const
138
139
/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
140
template <class BlockT, class LoopT>
141
void LoopBase<BlockT, LoopT>::getExitEdges(
142
229
    SmallVectorImpl<Edge> &ExitEdges) const {
143
229
  assert(!isInvalid() && "Loop not in a valid state!");
144
229
  for (const auto BB : blocks())
145
819
    for (const auto &Succ : children<BlockT *>(BB))
146
1.39k
      if (!contains(Succ))
147
289
        // Not in current loop? It must be an exit block.
148
289
        ExitEdges.emplace_back(BB, Succ);
149
229
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getExitEdges(llvm::SmallVectorImpl<std::__1::pair<llvm::BasicBlock*, llvm::BasicBlock*> >&) const
Line
Count
Source
142
229
    SmallVectorImpl<Edge> &ExitEdges) const {
143
229
  assert(!isInvalid() && "Loop not in a valid state!");
144
229
  for (const auto BB : blocks())
145
819
    for (const auto &Succ : children<BlockT *>(BB))
146
1.39k
      if (!contains(Succ))
147
289
        // Not in current loop? It must be an exit block.
148
289
        ExitEdges.emplace_back(BB, Succ);
149
229
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getExitEdges(llvm::SmallVectorImpl<std::__1::pair<llvm::MachineBasicBlock*, llvm::MachineBasicBlock*> >&) const
150
151
/// getLoopPreheader - If there is a preheader for this loop, return it.  A
152
/// loop has a preheader if there is only one edge to the header of the loop
153
/// from outside of the loop and it is legal to hoist instructions into the
154
/// predecessor. If this is the case, the block branching to the header of the
155
/// loop is the preheader node.
156
///
157
/// This method returns null if there is no preheader for the loop.
158
///
159
template <class BlockT, class LoopT>
160
11.9M
BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
161
11.9M
  assert(!isInvalid() && "Loop not in a valid state!");
162
11.9M
  // Keep track of nodes outside the loop branching to the header...
163
11.9M
  BlockT *Out = getLoopPredecessor();
164
11.9M
  if (!Out)
165
476k
    return nullptr;
166
11.4M
167
11.4M
  // Make sure we are allowed to hoist instructions into the predecessor.
168
11.4M
  if (!Out->isLegalToHoistInto())
169
460
    return nullptr;
170
11.4M
171
11.4M
  // Make sure there is only one exit out of the preheader.
172
11.4M
  typedef GraphTraits<BlockT *> BlockTraits;
173
11.4M
  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
174
11.4M
  ++SI;
175
11.4M
  if (SI != BlockTraits::child_end(Out))
176
492k
    return nullptr; // Multiple exits from the block, must not be a preheader.
177
10.9M
178
10.9M
  // The predecessor has exactly one successor, so it is a preheader.
179
10.9M
  return Out;
180
10.9M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getLoopPreheader() const
Line
Count
Source
160
11.6M
BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
161
11.6M
  assert(!isInvalid() && "Loop not in a valid state!");
162
11.6M
  // Keep track of nodes outside the loop branching to the header...
163
11.6M
  BlockT *Out = getLoopPredecessor();
164
11.6M
  if (!Out)
165
475k
    return nullptr;
166
11.1M
167
11.1M
  // Make sure we are allowed to hoist instructions into the predecessor.
168
11.1M
  if (!Out->isLegalToHoistInto())
169
460
    return nullptr;
170
11.1M
171
11.1M
  // Make sure there is only one exit out of the preheader.
172
11.1M
  typedef GraphTraits<BlockT *> BlockTraits;
173
11.1M
  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
174
11.1M
  ++SI;
175
11.1M
  if (SI != BlockTraits::child_end(Out))
176
471k
    return nullptr; // Multiple exits from the block, must not be a preheader.
177
10.7M
178
10.7M
  // The predecessor has exactly one successor, so it is a preheader.
179
10.7M
  return Out;
180
10.7M
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getLoopPreheader() const
Line
Count
Source
160
308k
BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
161
308k
  assert(!isInvalid() && "Loop not in a valid state!");
162
308k
  // Keep track of nodes outside the loop branching to the header...
163
308k
  BlockT *Out = getLoopPredecessor();
164
308k
  if (!Out)
165
1.49k
    return nullptr;
166
306k
167
306k
  // Make sure we are allowed to hoist instructions into the predecessor.
168
306k
  if (!Out->isLegalToHoistInto())
169
0
    return nullptr;
170
306k
171
306k
  // Make sure there is only one exit out of the preheader.
172
306k
  typedef GraphTraits<BlockT *> BlockTraits;
173
306k
  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
174
306k
  ++SI;
175
306k
  if (SI != BlockTraits::child_end(Out))
176
20.7k
    return nullptr; // Multiple exits from the block, must not be a preheader.
177
286k
178
286k
  // The predecessor has exactly one successor, so it is a preheader.
179
286k
  return Out;
180
286k
}
181
182
/// getLoopPredecessor - If the given loop's header has exactly one unique
183
/// predecessor outside the loop, return it. Otherwise return null.
184
/// This is less strict that the loop "preheader" concept, which requires
185
/// the predecessor to have exactly one successor.
186
///
187
template <class BlockT, class LoopT>
188
13.6M
BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
189
13.6M
  assert(!isInvalid() && "Loop not in a valid state!");
190
13.6M
  // Keep track of nodes outside the loop branching to the header...
191
13.6M
  BlockT *Out = nullptr;
192
13.6M
193
13.6M
  // Loop over the predecessors of the header node...
194
13.6M
  BlockT *Header = getHeader();
195
27.2M
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
196
27.2M
    if (!contains(Pred)) { // If the block is not in the loop...
197
14.1M
      if (Out && 
Out != Pred480k
)
198
480k
        return nullptr; // Multiple predecessors outside the loop
199
13.6M
      Out = Pred;
200
13.6M
    }
201
27.2M
  }
202
13.6M
203
13.6M
  // Make sure there is only one exit out of the preheader.
204
13.6M
  assert(Out && "Header of loop has no predecessors from outside loop?");
205
13.1M
  return Out;
206
13.6M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getLoopPredecessor() const
Line
Count
Source
188
13.1M
BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
189
13.1M
  assert(!isInvalid() && "Loop not in a valid state!");
190
13.1M
  // Keep track of nodes outside the loop branching to the header...
191
13.1M
  BlockT *Out = nullptr;
192
13.1M
193
13.1M
  // Loop over the predecessors of the header node...
194
13.1M
  BlockT *Header = getHeader();
195
26.2M
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
196
26.2M
    if (!contains(Pred)) { // If the block is not in the loop...
197
13.6M
      if (Out && 
Out != Pred475k
)
198
475k
        return nullptr; // Multiple predecessors outside the loop
199
13.1M
      Out = Pred;
200
13.1M
    }
201
26.2M
  }
202
13.1M
203
13.1M
  // Make sure there is only one exit out of the preheader.
204
13.1M
  assert(Out && "Header of loop has no predecessors from outside loop?");
205
12.6M
  return Out;
206
13.1M
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getLoopPredecessor() const
Line
Count
Source
188
484k
BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
189
484k
  assert(!isInvalid() && "Loop not in a valid state!");
190
484k
  // Keep track of nodes outside the loop branching to the header...
191
484k
  BlockT *Out = nullptr;
192
484k
193
484k
  // Loop over the predecessors of the header node...
194
484k
  BlockT *Header = getHeader();
195
994k
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
196
994k
    if (!contains(Pred)) { // If the block is not in the loop...
197
489k
      if (Out && 
Out != Pred4.49k
)
198
4.49k
        return nullptr; // Multiple predecessors outside the loop
199
484k
      Out = Pred;
200
484k
    }
201
994k
  }
202
484k
203
484k
  // Make sure there is only one exit out of the preheader.
204
484k
  assert(Out && "Header of loop has no predecessors from outside loop?");
205
480k
  return Out;
206
484k
}
207
208
/// getLoopLatch - If there is a single latch block for this loop, return it.
209
/// A latch block is a block that contains a branch back to the header.
210
template <class BlockT, class LoopT>
211
15.5M
BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
212
15.5M
  assert(!isInvalid() && "Loop not in a valid state!");
213
15.5M
  BlockT *Header = getHeader();
214
15.5M
  BlockT *Latch = nullptr;
215
31.1M
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
216
31.1M
    if (contains(Pred)) {
217
15.5M
      if (Latch)
218
7.21k
        return nullptr;
219
15.5M
      Latch = Pred;
220
15.5M
    }
221
31.1M
  }
222
15.5M
223
15.5M
  
return Latch15.5M
;
224
15.5M
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getLoopLatch() const
Line
Count
Source
211
15.5M
BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
212
15.5M
  assert(!isInvalid() && "Loop not in a valid state!");
213
15.5M
  BlockT *Header = getHeader();
214
15.5M
  BlockT *Latch = nullptr;
215
31.1M
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
216
31.1M
    if (contains(Pred)) {
217
15.5M
      if (Latch)
218
7.20k
        return nullptr;
219
15.5M
      Latch = Pred;
220
15.5M
    }
221
31.1M
  }
222
15.5M
223
15.5M
  
return Latch15.5M
;
224
15.5M
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getLoopLatch() const
Line
Count
Source
211
3.00k
BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
212
3.00k
  assert(!isInvalid() && "Loop not in a valid state!");
213
3.00k
  BlockT *Header = getHeader();
214
3.00k
  BlockT *Latch = nullptr;
215
6.04k
  for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
216
6.04k
    if (contains(Pred)) {
217
3.01k
      if (Latch)
218
4
        return nullptr;
219
3.00k
      Latch = Pred;
220
3.00k
    }
221
6.04k
  }
222
3.00k
223
3.00k
  
return Latch3.00k
;
224
3.00k
}
225
226
//===----------------------------------------------------------------------===//
227
// APIs for updating loop information after changing the CFG
228
//
229
230
/// addBasicBlockToLoop - This method is used by other analyses to update loop
231
/// information.  NewBB is set to be a new member of the current loop.
232
/// Because of this, it is added as a member of all parent loops, and is added
233
/// to the specified LoopInfo object as being in the current basic block.  It
234
/// is not valid to replace the loop header with this method.
235
///
236
template <class BlockT, class LoopT>
237
void LoopBase<BlockT, LoopT>::addBasicBlockToLoop(
238
759k
    BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
239
759k
  assert(!isInvalid() && "Loop not in a valid state!");
240
#ifndef NDEBUG
241
  if (!Blocks.empty()) {
242
    auto SameHeader = LIB[getHeader()];
243
    assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() &&
244
           "Incorrect LI specified for this loop!");
245
  }
246
#endif
247
  assert(NewBB && "Cannot add a null basic block to the loop!");
248
759k
  assert(!LIB[NewBB] && "BasicBlock already in the loop!");
249
759k
250
759k
  LoopT *L = static_cast<LoopT *>(this);
251
759k
252
759k
  // Add the loop mapping to the LoopInfo object...
253
759k
  LIB.BBMap[NewBB] = L;
254
759k
255
759k
  // Add the basic block to this loop and all parent loops...
256
1.85M
  while (L) {
257
1.09M
    L->addBlockEntry(NewBB);
258
1.09M
    L = L->getParentLoop();
259
1.09M
  }
260
759k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::addBasicBlockToLoop(llvm::BasicBlock*, llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>&)
Line
Count
Source
238
714k
    BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
239
714k
  assert(!isInvalid() && "Loop not in a valid state!");
240
#ifndef NDEBUG
241
  if (!Blocks.empty()) {
242
    auto SameHeader = LIB[getHeader()];
243
    assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() &&
244
           "Incorrect LI specified for this loop!");
245
  }
246
#endif
247
  assert(NewBB && "Cannot add a null basic block to the loop!");
248
714k
  assert(!LIB[NewBB] && "BasicBlock already in the loop!");
249
714k
250
714k
  LoopT *L = static_cast<LoopT *>(this);
251
714k
252
714k
  // Add the loop mapping to the LoopInfo object...
253
714k
  LIB.BBMap[NewBB] = L;
254
714k
255
714k
  // Add the basic block to this loop and all parent loops...
256
1.74M
  while (L) {
257
1.02M
    L->addBlockEntry(NewBB);
258
1.02M
    L = L->getParentLoop();
259
1.02M
  }
260
714k
}
llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::addBasicBlockToLoop(llvm::MachineBasicBlock*, llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>&)
Line
Count
Source
238
45.0k
    BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
239
45.0k
  assert(!isInvalid() && "Loop not in a valid state!");
240
#ifndef NDEBUG
241
  if (!Blocks.empty()) {
242
    auto SameHeader = LIB[getHeader()];
243
    assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() &&
244
           "Incorrect LI specified for this loop!");
245
  }
246
#endif
247
  assert(NewBB && "Cannot add a null basic block to the loop!");
248
45.0k
  assert(!LIB[NewBB] && "BasicBlock already in the loop!");
249
45.0k
250
45.0k
  LoopT *L = static_cast<LoopT *>(this);
251
45.0k
252
45.0k
  // Add the loop mapping to the LoopInfo object...
253
45.0k
  LIB.BBMap[NewBB] = L;
254
45.0k
255
45.0k
  // Add the basic block to this loop and all parent loops...
256
110k
  while (L) {
257
65.6k
    L->addBlockEntry(NewBB);
258
65.6k
    L = L->getParentLoop();
259
65.6k
  }
260
45.0k
}
261
262
/// replaceChildLoopWith - This is used when splitting loops up.  It replaces
263
/// the OldChild entry in our children list with NewChild, and updates the
264
/// parent pointer of OldChild to be null and the NewChild to be this loop.
265
/// This updates the loop depth of the new child.
266
template <class BlockT, class LoopT>
267
void LoopBase<BlockT, LoopT>::replaceChildLoopWith(LoopT *OldChild,
268
59
                                                   LoopT *NewChild) {
269
59
  assert(!isInvalid() && "Loop not in a valid state!");
270
59
  assert(OldChild->ParentLoop == this && "This loop is already broken!");
271
59
  assert(!NewChild->ParentLoop && "NewChild already has a parent!");
272
59
  typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
273
59
  assert(I != SubLoops.end() && "OldChild not in loop!");
274
59
  *I = NewChild;
275
59
  OldChild->ParentLoop = nullptr;
276
59
  NewChild->ParentLoop = static_cast<LoopT *>(this);
277
59
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::replaceChildLoopWith(llvm::Loop*, llvm::Loop*)
Line
Count
Source
268
59
                                                   LoopT *NewChild) {
269
59
  assert(!isInvalid() && "Loop not in a valid state!");
270
59
  assert(OldChild->ParentLoop == this && "This loop is already broken!");
271
59
  assert(!NewChild->ParentLoop && "NewChild already has a parent!");
272
59
  typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
273
59
  assert(I != SubLoops.end() && "OldChild not in loop!");
274
59
  *I = NewChild;
275
59
  OldChild->ParentLoop = nullptr;
276
59
  NewChild->ParentLoop = static_cast<LoopT *>(this);
277
59
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::replaceChildLoopWith(llvm::MachineLoop*, llvm::MachineLoop*)
278
279
/// verifyLoop - Verify loop structure
280
template <class BlockT, class LoopT>
281
2.86M
void LoopBase<BlockT, LoopT>::verifyLoop() const {
282
2.86M
  assert(!isInvalid() && "Loop not in a valid state!");
283
#ifndef NDEBUG
284
  assert(!Blocks.empty() && "Loop header is missing");
285
286
  // Setup for using a depth-first iterator to visit every block in the loop.
287
  SmallVector<BlockT *, 8> ExitBBs;
288
  getExitBlocks(ExitBBs);
289
  df_iterator_default_set<BlockT *> VisitSet;
290
  VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
291
  df_ext_iterator<BlockT *, df_iterator_default_set<BlockT *>>
292
      BI = df_ext_begin(getHeader(), VisitSet),
293
      BE = df_ext_end(getHeader(), VisitSet);
294
295
  // Keep track of the BBs visited.
296
  SmallPtrSet<BlockT *, 8> VisitedBBs;
297
298
  // Check the individual blocks.
299
  for (; BI != BE; ++BI) {
300
    BlockT *BB = *BI;
301
302
    assert(std::any_of(GraphTraits<BlockT *>::child_begin(BB),
303
                       GraphTraits<BlockT *>::child_end(BB),
304
                       [&](BlockT *B) { return contains(B); }) &&
305
           "Loop block has no in-loop successors!");
306
307
    assert(std::any_of(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
308
                       GraphTraits<Inverse<BlockT *>>::child_end(BB),
309
                       [&](BlockT *B) { return contains(B); }) &&
310
           "Loop block has no in-loop predecessors!");
311
312
    SmallVector<BlockT *, 2> OutsideLoopPreds;
313
    std::for_each(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
314
                  GraphTraits<Inverse<BlockT *>>::child_end(BB),
315
                  [&](BlockT *B) {
316
                    if (!contains(B))
317
                      OutsideLoopPreds.push_back(B);
318
                  });
319
320
    if (BB == getHeader()) {
321
      assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
322
    } else if (!OutsideLoopPreds.empty()) {
323
      // A non-header loop shouldn't be reachable from outside the loop,
324
      // though it is permitted if the predecessor is not itself actually
325
      // reachable.
326
      BlockT *EntryBB = &BB->getParent()->front();
327
      for (BlockT *CB : depth_first(EntryBB))
328
        for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
329
          assert(CB != OutsideLoopPreds[i] &&
330
                 "Loop has multiple entry points!");
331
    }
332
    assert(BB != &getHeader()->getParent()->front() &&
333
           "Loop contains function entry block!");
334
335
    VisitedBBs.insert(BB);
336
  }
337
338
  if (VisitedBBs.size() != getNumBlocks()) {
339
    dbgs() << "The following blocks are unreachable in the loop: ";
340
    for (auto BB : Blocks) {
341
      if (!VisitedBBs.count(BB)) {
342
        dbgs() << *BB << "\n";
343
      }
344
    }
345
    assert(false && "Unreachable block in loop");
346
  }
347
348
  // Check the subloops.
349
  for (iterator I = begin(), E = end(); I != E; ++I)
350
    // Each block in each subloop should be contained within this loop.
351
    for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
352
         BI != BE; ++BI) {
353
      assert(contains(*BI) &&
354
             "Loop does not contain all the blocks of a subloop!");
355
    }
356
357
  // Check the parent loop pointer.
358
  if (ParentLoop) {
359
    assert(is_contained(*ParentLoop, this) &&
360
           "Loop is not a subloop of its parent!");
361
  }
362
#endif
363
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::verifyLoop() const
Line
Count
Source
281
2.86M
void LoopBase<BlockT, LoopT>::verifyLoop() const {
282
2.86M
  assert(!isInvalid() && "Loop not in a valid state!");
283
#ifndef NDEBUG
284
  assert(!Blocks.empty() && "Loop header is missing");
285
286
  // Setup for using a depth-first iterator to visit every block in the loop.
287
  SmallVector<BlockT *, 8> ExitBBs;
288
  getExitBlocks(ExitBBs);
289
  df_iterator_default_set<BlockT *> VisitSet;
290
  VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
291
  df_ext_iterator<BlockT *, df_iterator_default_set<BlockT *>>
292
      BI = df_ext_begin(getHeader(), VisitSet),
293
      BE = df_ext_end(getHeader(), VisitSet);
294
295
  // Keep track of the BBs visited.
296
  SmallPtrSet<BlockT *, 8> VisitedBBs;
297
298
  // Check the individual blocks.
299
  for (; BI != BE; ++BI) {
300
    BlockT *BB = *BI;
301
302
    assert(std::any_of(GraphTraits<BlockT *>::child_begin(BB),
303
                       GraphTraits<BlockT *>::child_end(BB),
304
                       [&](BlockT *B) { return contains(B); }) &&
305
           "Loop block has no in-loop successors!");
306
307
    assert(std::any_of(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
308
                       GraphTraits<Inverse<BlockT *>>::child_end(BB),
309
                       [&](BlockT *B) { return contains(B); }) &&
310
           "Loop block has no in-loop predecessors!");
311
312
    SmallVector<BlockT *, 2> OutsideLoopPreds;
313
    std::for_each(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
314
                  GraphTraits<Inverse<BlockT *>>::child_end(BB),
315
                  [&](BlockT *B) {
316
                    if (!contains(B))
317
                      OutsideLoopPreds.push_back(B);
318
                  });
319
320
    if (BB == getHeader()) {
321
      assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
322
    } else if (!OutsideLoopPreds.empty()) {
323
      // A non-header loop shouldn't be reachable from outside the loop,
324
      // though it is permitted if the predecessor is not itself actually
325
      // reachable.
326
      BlockT *EntryBB = &BB->getParent()->front();
327
      for (BlockT *CB : depth_first(EntryBB))
328
        for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
329
          assert(CB != OutsideLoopPreds[i] &&
330
                 "Loop has multiple entry points!");
331
    }
332
    assert(BB != &getHeader()->getParent()->front() &&
333
           "Loop contains function entry block!");
334
335
    VisitedBBs.insert(BB);
336
  }
337
338
  if (VisitedBBs.size() != getNumBlocks()) {
339
    dbgs() << "The following blocks are unreachable in the loop: ";
340
    for (auto BB : Blocks) {
341
      if (!VisitedBBs.count(BB)) {
342
        dbgs() << *BB << "\n";
343
      }
344
    }
345
    assert(false && "Unreachable block in loop");
346
  }
347
348
  // Check the subloops.
349
  for (iterator I = begin(), E = end(); I != E; ++I)
350
    // Each block in each subloop should be contained within this loop.
351
    for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
352
         BI != BE; ++BI) {
353
      assert(contains(*BI) &&
354
             "Loop does not contain all the blocks of a subloop!");
355
    }
356
357
  // Check the parent loop pointer.
358
  if (ParentLoop) {
359
    assert(is_contained(*ParentLoop, this) &&
360
           "Loop is not a subloop of its parent!");
361
  }
362
#endif
363
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::verifyLoop() const
364
365
/// verifyLoop - Verify loop structure of this loop and all nested loops.
366
template <class BlockT, class LoopT>
367
void LoopBase<BlockT, LoopT>::verifyLoopNest(
368
291
    DenseSet<const LoopT *> *Loops) const {
369
291
  assert(!isInvalid() && "Loop not in a valid state!");
370
291
  Loops->insert(static_cast<const LoopT *>(this));
371
291
  // Verify this loop.
372
291
  verifyLoop();
373
291
  // Verify the subloops.
374
387
  for (iterator I = begin(), E = end(); I != E; 
++I96
)
375
96
    (*I)->verifyLoopNest(Loops);
376
291
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::verifyLoopNest(llvm::DenseSet<llvm::Loop const*, llvm::DenseMapInfo<llvm::Loop const*> >*) const
Line
Count
Source
368
291
    DenseSet<const LoopT *> *Loops) const {
369
291
  assert(!isInvalid() && "Loop not in a valid state!");
370
291
  Loops->insert(static_cast<const LoopT *>(this));
371
291
  // Verify this loop.
372
291
  verifyLoop();
373
291
  // Verify the subloops.
374
387
  for (iterator I = begin(), E = end(); I != E; 
++I96
)
375
96
    (*I)->verifyLoopNest(Loops);
376
291
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::verifyLoopNest(llvm::DenseSet<llvm::MachineLoop const*, llvm::DenseMapInfo<llvm::MachineLoop const*> >*) const
377
378
template <class BlockT, class LoopT>
379
void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth,
380
1.33k
                                    bool Verbose) const {
381
1.33k
  OS.indent(Depth * 2);
382
1.33k
  if (static_cast<const LoopT *>(this)->isAnnotatedParallel())
383
4
    OS << "Parallel ";
384
1.33k
  OS << "Loop at depth " << getLoopDepth() << " containing: ";
385
1.33k
386
1.33k
  BlockT *H = getHeader();
387
12.4k
  for (unsigned i = 0; i < getBlocks().size(); 
++i11.0k
) {
388
11.0k
    BlockT *BB = getBlocks()[i];
389
11.0k
    if (!Verbose) {
390
11.0k
      if (i)
391
9.75k
        OS << ",";
392
11.0k
      BB->printAsOperand(OS, false);
393
11.0k
    } else
394
0
      OS << "\n";
395
11.0k
396
11.0k
    if (BB == H)
397
1.33k
      OS << "<header>";
398
11.0k
    if (isLoopLatch(BB))
399
1.33k
      OS << "<latch>";
400
11.0k
    if (isLoopExiting(BB))
401
1.57k
      OS << "<exiting>";
402
11.0k
    if (Verbose)
403
0
      BB->print(OS);
404
11.0k
  }
405
1.33k
  OS << "\n";
406
1.33k
407
1.84k
  for (iterator I = begin(), E = end(); I != E; 
++I503
)
408
503
    (*I)->print(OS, Depth + 2);
409
1.33k
}
llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::print(llvm::raw_ostream&, unsigned int, bool) const
Line
Count
Source
380
1.33k
                                    bool Verbose) const {
381
1.33k
  OS.indent(Depth * 2);
382
1.33k
  if (static_cast<const LoopT *>(this)->isAnnotatedParallel())
383
4
    OS << "Parallel ";
384
1.33k
  OS << "Loop at depth " << getLoopDepth() << " containing: ";
385
1.33k
386
1.33k
  BlockT *H = getHeader();
387
12.4k
  for (unsigned i = 0; i < getBlocks().size(); 
++i11.0k
) {
388
11.0k
    BlockT *BB = getBlocks()[i];
389
11.0k
    if (!Verbose) {
390
11.0k
      if (i)
391
9.75k
        OS << ",";
392
11.0k
      BB->printAsOperand(OS, false);
393
11.0k
    } else
394
0
      OS << "\n";
395
11.0k
396
11.0k
    if (BB == H)
397
1.33k
      OS << "<header>";
398
11.0k
    if (isLoopLatch(BB))
399
1.33k
      OS << "<latch>";
400
11.0k
    if (isLoopExiting(BB))
401
1.57k
      OS << "<exiting>";
402
11.0k
    if (Verbose)
403
0
      BB->print(OS);
404
11.0k
  }
405
1.33k
  OS << "\n";
406
1.33k
407
1.84k
  for (iterator I = begin(), E = end(); I != E; 
++I503
)
408
503
    (*I)->print(OS, Depth + 2);
409
1.33k
}
Unexecuted instantiation: llvm::LoopBase<llvm::MachineBasicBlock, llvm::MachineLoop>::print(llvm::raw_ostream&, unsigned int, bool) const
410
411
//===----------------------------------------------------------------------===//
412
/// Stable LoopInfo Analysis - Build a loop tree using stable iterators so the
413
/// result does / not depend on use list (block predecessor) order.
414
///
415
416
/// Discover a subloop with the specified backedges such that: All blocks within
417
/// this loop are mapped to this loop or a subloop. And all subloops within this
418
/// loop have their parent loop set to this loop or a subloop.
419
template <class BlockT, class LoopT>
420
static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT *> Backedges,
421
                                  LoopInfoBase<BlockT, LoopT> *LI,
422
4.11M
                                  const DomTreeBase<BlockT> &DomTree) {
423
4.11M
  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
424
4.11M
425
4.11M
  unsigned NumBlocks = 0;
426
4.11M
  unsigned NumSubloops = 0;
427
4.11M
428
4.11M
  // Perform a backward CFG traversal using a worklist.
429
4.11M
  std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
430
22.6M
  while (!ReverseCFGWorklist.empty()) {
431
18.5M
    BlockT *PredBB = ReverseCFGWorklist.back();
432
18.5M
    ReverseCFGWorklist.pop_back();
433
18.5M
434
18.5M
    LoopT *Subloop = LI->getLoopFor(PredBB);
435
18.5M
    if (!Subloop) {
436
13.0M
      if (!DomTree.isReachableFromEntry(PredBB))
437
50
        continue;
438
13.0M
439
13.0M
      // This is an undiscovered block. Map it to the current loop.
440
13.0M
      LI->changeLoopFor(PredBB, L);
441
13.0M
      ++NumBlocks;
442
13.0M
      if (PredBB == L->getHeader())
443
4.11M
        continue;
444
8.95M
      // Push all block predecessors on the worklist.
445
8.95M
      ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
446
8.95M
                                InvBlockTraits::child_begin(PredBB),
447
8.95M
                                InvBlockTraits::child_end(PredBB));
448
8.95M
    } else {
449
5.44M
      // This is a discovered block. Find its outermost discovered loop.
450
5.66M
      while (LoopT *Parent = Subloop->getParentLoop())
451
212k
        Subloop = Parent;
452
5.44M
453
5.44M
      // If it is already discovered to be a subloop of this loop, continue.
454
5.44M
      if (Subloop == L)
455
4.35M
        continue;
456
1.09M
457
1.09M
      // Discover a subloop of this loop.
458
1.09M
      Subloop->setParentLoop(L);
459
1.09M
      ++NumSubloops;
460
1.09M
      NumBlocks += Subloop->getBlocksVector().capacity();
461
1.09M
      PredBB = Subloop->getHeader();
462
1.09M
      // Continue traversal along predecessors that are not loop-back edges from
463
1.09M
      // within this subloop tree itself. Note that a predecessor may directly
464
1.09M
      // reach another subloop that is not yet discovered to be a subloop of
465
1.09M
      // this loop, which we must traverse.
466
2.19M
      for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
467
2.19M
        if (LI->getLoopFor(Pred) != Subloop)
468
1.10M
          ReverseCFGWorklist.push_back(Pred);
469
2.19M
      }
470
1.09M
    }
471
18.5M
  }
472
4.11M
  L->getSubLoopsVector().reserve(NumSubloops);
473
4.11M
  L->reserveBlocks(NumBlocks);
474
4.11M
}
LoopInfo.cpp:void llvm::discoverAndMapSubloop<llvm::BasicBlock, llvm::Loop>(llvm::Loop*, llvm::ArrayRef<llvm::BasicBlock*>, llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>*, llvm::DominatorTreeBase<llvm::BasicBlock, false> const&)
Line
Count
Source
422
3.25M
                                  const DomTreeBase<BlockT> &DomTree) {
423
3.25M
  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
424
3.25M
425
3.25M
  unsigned NumBlocks = 0;
426
3.25M
  unsigned NumSubloops = 0;
427
3.25M
428
3.25M
  // Perform a backward CFG traversal using a worklist.
429
3.25M
  std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
430
17.9M
  while (!ReverseCFGWorklist.empty()) {
431
14.7M
    BlockT *PredBB = ReverseCFGWorklist.back();
432
14.7M
    ReverseCFGWorklist.pop_back();
433
14.7M
434
14.7M
    LoopT *Subloop = LI->getLoopFor(PredBB);
435
14.7M
    if (!Subloop) {
436
10.3M
      if (!DomTree.isReachableFromEntry(PredBB))
437
50
        continue;
438
10.3M
439
10.3M
      // This is an undiscovered block. Map it to the current loop.
440
10.3M
      LI->changeLoopFor(PredBB, L);
441
10.3M
      ++NumBlocks;
442
10.3M
      if (PredBB == L->getHeader())
443
3.25M
        continue;
444
7.11M
      // Push all block predecessors on the worklist.
445
7.11M
      ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
446
7.11M
                                InvBlockTraits::child_begin(PredBB),
447
7.11M
                                InvBlockTraits::child_end(PredBB));
448
7.11M
    } else {
449
4.34M
      // This is a discovered block. Find its outermost discovered loop.
450
4.50M
      while (LoopT *Parent = Subloop->getParentLoop())
451
160k
        Subloop = Parent;
452
4.34M
453
4.34M
      // If it is already discovered to be a subloop of this loop, continue.
454
4.34M
      if (Subloop == L)
455
3.47M
        continue;
456
869k
457
869k
      // Discover a subloop of this loop.
458
869k
      Subloop->setParentLoop(L);
459
869k
      ++NumSubloops;
460
869k
      NumBlocks += Subloop->getBlocksVector().capacity();
461
869k
      PredBB = Subloop->getHeader();
462
869k
      // Continue traversal along predecessors that are not loop-back edges from
463
869k
      // within this subloop tree itself. Note that a predecessor may directly
464
869k
      // reach another subloop that is not yet discovered to be a subloop of
465
869k
      // this loop, which we must traverse.
466
1.74M
      for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
467
1.74M
        if (LI->getLoopFor(Pred) != Subloop)
468
876k
          ReverseCFGWorklist.push_back(Pred);
469
1.74M
      }
470
869k
    }
471
14.7M
  }
472
3.25M
  L->getSubLoopsVector().reserve(NumSubloops);
473
3.25M
  L->reserveBlocks(NumBlocks);
474
3.25M
}
MachineLoopInfo.cpp:void llvm::discoverAndMapSubloop<llvm::MachineBasicBlock, llvm::MachineLoop>(llvm::MachineLoop*, llvm::ArrayRef<llvm::MachineBasicBlock*>, llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>*, llvm::DominatorTreeBase<llvm::MachineBasicBlock, false> const&)
Line
Count
Source
422
863k
                                  const DomTreeBase<BlockT> &DomTree) {
423
863k
  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
424
863k
425
863k
  unsigned NumBlocks = 0;
426
863k
  unsigned NumSubloops = 0;
427
863k
428
863k
  // Perform a backward CFG traversal using a worklist.
429
863k
  std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
430
4.67M
  while (!ReverseCFGWorklist.empty()) {
431
3.80M
    BlockT *PredBB = ReverseCFGWorklist.back();
432
3.80M
    ReverseCFGWorklist.pop_back();
433
3.80M
434
3.80M
    LoopT *Subloop = LI->getLoopFor(PredBB);
435
3.80M
    if (!Subloop) {
436
2.70M
      if (!DomTree.isReachableFromEntry(PredBB))
437
0
        continue;
438
2.70M
439
2.70M
      // This is an undiscovered block. Map it to the current loop.
440
2.70M
      LI->changeLoopFor(PredBB, L);
441
2.70M
      ++NumBlocks;
442
2.70M
      if (PredBB == L->getHeader())
443
863k
        continue;
444
1.84M
      // Push all block predecessors on the worklist.
445
1.84M
      ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
446
1.84M
                                InvBlockTraits::child_begin(PredBB),
447
1.84M
                                InvBlockTraits::child_end(PredBB));
448
1.84M
    } else {
449
1.10M
      // This is a discovered block. Find its outermost discovered loop.
450
1.15M
      while (LoopT *Parent = Subloop->getParentLoop())
451
51.8k
        Subloop = Parent;
452
1.10M
453
1.10M
      // If it is already discovered to be a subloop of this loop, continue.
454
1.10M
      if (Subloop == L)
455
876k
        continue;
456
224k
457
224k
      // Discover a subloop of this loop.
458
224k
      Subloop->setParentLoop(L);
459
224k
      ++NumSubloops;
460
224k
      NumBlocks += Subloop->getBlocksVector().capacity();
461
224k
      PredBB = Subloop->getHeader();
462
224k
      // Continue traversal along predecessors that are not loop-back edges from
463
224k
      // within this subloop tree itself. Note that a predecessor may directly
464
224k
      // reach another subloop that is not yet discovered to be a subloop of
465
224k
      // this loop, which we must traverse.
466
452k
      for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
467
452k
        if (LI->getLoopFor(Pred) != Subloop)
468
226k
          ReverseCFGWorklist.push_back(Pred);
469
452k
      }
470
224k
    }
471
3.80M
  }
472
863k
  L->getSubLoopsVector().reserve(NumSubloops);
473
863k
  L->reserveBlocks(NumBlocks);
474
863k
}
VPlanHCFGBuilder.cpp:void llvm::discoverAndMapSubloop<llvm::VPBlockBase, llvm::VPLoop>(llvm::VPLoop*, llvm::ArrayRef<llvm::VPBlockBase*>, llvm::LoopInfoBase<llvm::VPBlockBase, llvm::VPLoop>*, llvm::DominatorTreeBase<llvm::VPBlockBase, false> const&)
Line
Count
Source
422
14
                                  const DomTreeBase<BlockT> &DomTree) {
423
14
  typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
424
14
425
14
  unsigned NumBlocks = 0;
426
14
  unsigned NumSubloops = 0;
427
14
428
14
  // Perform a backward CFG traversal using a worklist.
429
14
  std::vector<BlockT *> ReverseCFGWorklist(Backedges.begin(), Backedges.end());
430
45
  while (!ReverseCFGWorklist.empty()) {
431
31
    BlockT *PredBB = ReverseCFGWorklist.back();
432
31
    ReverseCFGWorklist.pop_back();
433
31
434
31
    LoopT *Subloop = LI->getLoopFor(PredBB);
435
31
    if (!Subloop) {
436
23
      if (!DomTree.isReachableFromEntry(PredBB))
437
0
        continue;
438
23
439
23
      // This is an undiscovered block. Map it to the current loop.
440
23
      LI->changeLoopFor(PredBB, L);
441
23
      ++NumBlocks;
442
23
      if (PredBB == L->getHeader())
443
14
        continue;
444
9
      // Push all block predecessors on the worklist.
445
9
      ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
446
9
                                InvBlockTraits::child_begin(PredBB),
447
9
                                InvBlockTraits::child_end(PredBB));
448
9
    } else {
449
8
      // This is a discovered block. Find its outermost discovered loop.
450
8
      while (LoopT *Parent = Subloop->getParentLoop())
451
0
        Subloop = Parent;
452
8
453
8
      // If it is already discovered to be a subloop of this loop, continue.
454
8
      if (Subloop == L)
455
1
        continue;
456
7
457
7
      // Discover a subloop of this loop.
458
7
      Subloop->setParentLoop(L);
459
7
      ++NumSubloops;
460
7
      NumBlocks += Subloop->getBlocksVector().capacity();
461
7
      PredBB = Subloop->getHeader();
462
7
      // Continue traversal along predecessors that are not loop-back edges from
463
7
      // within this subloop tree itself. Note that a predecessor may directly
464
7
      // reach another subloop that is not yet discovered to be a subloop of
465
7
      // this loop, which we must traverse.
466
14
      for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
467
14
        if (LI->getLoopFor(Pred) != Subloop)
468
7
          ReverseCFGWorklist.push_back(Pred);
469
14
      }
470
7
    }
471
31
  }
472
14
  L->getSubLoopsVector().reserve(NumSubloops);
473
14
  L->reserveBlocks(NumBlocks);
474
14
}
475
476
/// Populate all loop data in a stable order during a single forward DFS.
477
template <class BlockT, class LoopT> class PopulateLoopsDFS {
478
  typedef GraphTraits<BlockT *> BlockTraits;
479
  typedef typename BlockTraits::ChildIteratorType SuccIterTy;
480
481
  LoopInfoBase<BlockT, LoopT> *LI;
482
483
public:
484
9.52M
  PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li) : LI(li) {}
llvm::PopulateLoopsDFS<llvm::BasicBlock, llvm::Loop>::PopulateLoopsDFS(llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>*)
Line
Count
Source
484
7.18M
  PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li) : LI(li) {}
llvm::PopulateLoopsDFS<llvm::MachineBasicBlock, llvm::MachineLoop>::PopulateLoopsDFS(llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>*)
Line
Count
Source
484
2.33M
  PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li) : LI(li) {}
llvm::PopulateLoopsDFS<llvm::VPBlockBase, llvm::VPLoop>::PopulateLoopsDFS(llvm::LoopInfoBase<llvm::VPBlockBase, llvm::VPLoop>*)
Line
Count
Source
484
25
  PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li) : LI(li) {}
485
486
  void traverse(BlockT *EntryBlock);
487
488
protected:
489
  void insertIntoLoop(BlockT *Block);
490
};
491
492
/// Top-level driver for the forward DFS within the loop.
493
template <class BlockT, class LoopT>
494
9.52M
void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
495
9.52M
  for (BlockT *BB : post_order(EntryBlock))
496
52.9M
    insertIntoLoop(BB);
497
9.52M
}
llvm::PopulateLoopsDFS<llvm::BasicBlock, llvm::Loop>::traverse(llvm::BasicBlock*)
Line
Count
Source
494
7.18M
void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
495
7.18M
  for (BlockT *BB : post_order(EntryBlock))
496
41.4M
    insertIntoLoop(BB);
497
7.18M
}
llvm::PopulateLoopsDFS<llvm::MachineBasicBlock, llvm::MachineLoop>::traverse(llvm::MachineBasicBlock*)
Line
Count
Source
494
2.33M
void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
495
2.33M
  for (BlockT *BB : post_order(EntryBlock))
496
11.4M
    insertIntoLoop(BB);
497
2.33M
}
llvm::PopulateLoopsDFS<llvm::VPBlockBase, llvm::VPLoop>::traverse(llvm::VPBlockBase*)
Line
Count
Source
494
25
void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
495
25
  for (BlockT *BB : post_order(EntryBlock))
496
111
    insertIntoLoop(BB);
497
25
}
498
499
/// Add a single Block to its ancestor loops in PostOrder. If the block is a
500
/// subloop header, add the subloop to its parent in PostOrder, then reverse the
501
/// Block and Subloop vectors of the now complete subloop to achieve RPO.
502
template <class BlockT, class LoopT>
503
52.9M
void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
504
52.9M
  LoopT *Subloop = LI->getLoopFor(Block);
505
52.9M
  if (Subloop && 
Block == Subloop->getHeader()13.0M
) {
506
4.11M
    // We reach this point once per subloop after processing all the blocks in
507
4.11M
    // the subloop.
508
4.11M
    if (Subloop->getParentLoop())
509
1.09M
      Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
510
3.02M
    else
511
3.02M
      LI->addTopLevelLoop(Subloop);
512
4.11M
513
4.11M
    // For convenience, Blocks and Subloops are inserted in postorder. Reverse
514
4.11M
    // the lists, except for the loop header, which is always at the beginning.
515
4.11M
    Subloop->reverseBlock(1);
516
4.11M
    std::reverse(Subloop->getSubLoopsVector().begin(),
517
4.11M
                 Subloop->getSubLoopsVector().end());
518
4.11M
519
4.11M
    Subloop = Subloop->getParentLoop();
520
4.11M
  }
521
66.1M
  for (; Subloop; 
Subloop = Subloop->getParentLoop()13.2M
)
522
13.2M
    Subloop->addBlockEntry(Block);
523
52.9M
}
llvm::PopulateLoopsDFS<llvm::BasicBlock, llvm::Loop>::insertIntoLoop(llvm::BasicBlock*)
Line
Count
Source
503
41.4M
void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
504
41.4M
  LoopT *Subloop = LI->getLoopFor(Block);
505
41.4M
  if (Subloop && 
Block == Subloop->getHeader()10.3M
) {
506
3.25M
    // We reach this point once per subloop after processing all the blocks in
507
3.25M
    // the subloop.
508
3.25M
    if (Subloop->getParentLoop())
509
869k
      Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
510
2.38M
    else
511
2.38M
      LI->addTopLevelLoop(Subloop);
512
3.25M
513
3.25M
    // For convenience, Blocks and Subloops are inserted in postorder. Reverse
514
3.25M
    // the lists, except for the loop header, which is always at the beginning.
515
3.25M
    Subloop->reverseBlock(1);
516
3.25M
    std::reverse(Subloop->getSubLoopsVector().begin(),
517
3.25M
                 Subloop->getSubLoopsVector().end());
518
3.25M
519
3.25M
    Subloop = Subloop->getParentLoop();
520
3.25M
  }
521
51.9M
  for (; Subloop; 
Subloop = Subloop->getParentLoop()10.5M
)
522
10.5M
    Subloop->addBlockEntry(Block);
523
41.4M
}
llvm::PopulateLoopsDFS<llvm::MachineBasicBlock, llvm::MachineLoop>::insertIntoLoop(llvm::MachineBasicBlock*)
Line
Count
Source
503
11.4M
void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
504
11.4M
  LoopT *Subloop = LI->getLoopFor(Block);
505
11.4M
  if (Subloop && 
Block == Subloop->getHeader()2.70M
) {
506
863k
    // We reach this point once per subloop after processing all the blocks in
507
863k
    // the subloop.
508
863k
    if (Subloop->getParentLoop())
509
224k
      Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
510
638k
    else
511
638k
      LI->addTopLevelLoop(Subloop);
512
863k
513
863k
    // For convenience, Blocks and Subloops are inserted in postorder. Reverse
514
863k
    // the lists, except for the loop header, which is always at the beginning.
515
863k
    Subloop->reverseBlock(1);
516
863k
    std::reverse(Subloop->getSubLoopsVector().begin(),
517
863k
                 Subloop->getSubLoopsVector().end());
518
863k
519
863k
    Subloop = Subloop->getParentLoop();
520
863k
  }
521
14.1M
  for (; Subloop; 
Subloop = Subloop->getParentLoop()2.75M
)
522
2.75M
    Subloop->addBlockEntry(Block);
523
11.4M
}
llvm::PopulateLoopsDFS<llvm::VPBlockBase, llvm::VPLoop>::insertIntoLoop(llvm::VPBlockBase*)
Line
Count
Source
503
111
void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
504
111
  LoopT *Subloop = LI->getLoopFor(Block);
505
111
  if (Subloop && 
Block == Subloop->getHeader()61
) {
506
34
    // We reach this point once per subloop after processing all the blocks in
507
34
    // the subloop.
508
34
    if (Subloop->getParentLoop())
509
9
      Subloop->getParentLoop()->getSubLoopsVector().push_back(Subloop);
510
25
    else
511
25
      LI->addTopLevelLoop(Subloop);
512
34
513
34
    // For convenience, Blocks and Subloops are inserted in postorder. Reverse
514
34
    // the lists, except for the loop header, which is always at the beginning.
515
34
    Subloop->reverseBlock(1);
516
34
    std::reverse(Subloop->getSubLoopsVector().begin(),
517
34
                 Subloop->getSubLoopsVector().end());
518
34
519
34
    Subloop = Subloop->getParentLoop();
520
34
  }
521
155
  for (; Subloop; 
Subloop = Subloop->getParentLoop()44
)
522
44
    Subloop->addBlockEntry(Block);
523
111
}
524
525
/// Analyze LoopInfo discovers loops during a postorder DominatorTree traversal
526
/// interleaved with backward CFG traversals within each subloop
527
/// (discoverAndMapSubloop). The backward traversal skips inner subloops, so
528
/// this part of the algorithm is linear in the number of CFG edges. Subloop and
529
/// Block vectors are then populated during a single forward CFG traversal
530
/// (PopulateLoopDFS).
531
///
532
/// During the two CFG traversals each block is seen three times:
533
/// 1) Discovered and mapped by a reverse CFG traversal.
534
/// 2) Visited during a forward DFS CFG traversal.
535
/// 3) Reverse-inserted in the loop in postorder following forward DFS.
536
///
537
/// The Block vectors are inclusive, so step 3 requires loop-depth number of
538
/// insertions per block.
539
template <class BlockT, class LoopT>
540
9.52M
void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
541
9.52M
  // Postorder traversal of the dominator tree.
542
9.52M
  const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
543
52.9M
  for (auto DomNode : post_order(DomRoot)) {
544
52.9M
545
52.9M
    BlockT *Header = DomNode->getBlock();
546
52.9M
    SmallVector<BlockT *, 4> Backedges;
547
52.9M
548
52.9M
    // Check each predecessor of the potential loop header.
549
67.7M
    for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
550
67.7M
      // If Header dominates predBB, this is a new loop. Collect the backedges.
551
67.7M
      if (DomTree.dominates(Header, Backedge) &&
552
67.7M
          
DomTree.isReachableFromEntry(Backedge)4.16M
) {
553
4.16M
        Backedges.push_back(Backedge);
554
4.16M
      }
555
67.7M
    }
556
52.9M
    // Perform a backward CFG traversal to discover and map blocks in this loop.
557
52.9M
    if (!Backedges.empty()) {
558
4.11M
      LoopT *L = AllocateLoop(Header);
559
4.11M
      discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
560
4.11M
    }
561
52.9M
  }
562
9.52M
  // Perform a single forward CFG traversal to populate block and subloop
563
9.52M
  // vectors for all loops.
564
9.52M
  PopulateLoopsDFS<BlockT, LoopT> DFS(this);
565
9.52M
  DFS.traverse(DomRoot->getBlock());
566
9.52M
}
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::analyze(llvm::DominatorTreeBase<llvm::BasicBlock, false> const&)
Line
Count
Source
540
7.18M
void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
541
7.18M
  // Postorder traversal of the dominator tree.
542
7.18M
  const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
543
41.4M
  for (auto DomNode : post_order(DomRoot)) {
544
41.4M
545
41.4M
    BlockT *Header = DomNode->getBlock();
546
41.4M
    SmallVector<BlockT *, 4> Backedges;
547
41.4M
548
41.4M
    // Check each predecessor of the potential loop header.
549
53.8M
    for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
550
53.8M
      // If Header dominates predBB, this is a new loop. Collect the backedges.
551
53.8M
      if (DomTree.dominates(Header, Backedge) &&
552
53.8M
          
DomTree.isReachableFromEntry(Backedge)3.27M
) {
553
3.27M
        Backedges.push_back(Backedge);
554
3.27M
      }
555
53.8M
    }
556
41.4M
    // Perform a backward CFG traversal to discover and map blocks in this loop.
557
41.4M
    if (!Backedges.empty()) {
558
3.25M
      LoopT *L = AllocateLoop(Header);
559
3.25M
      discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
560
3.25M
    }
561
41.4M
  }
562
7.18M
  // Perform a single forward CFG traversal to populate block and subloop
563
7.18M
  // vectors for all loops.
564
7.18M
  PopulateLoopsDFS<BlockT, LoopT> DFS(this);
565
7.18M
  DFS.traverse(DomRoot->getBlock());
566
7.18M
}
llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>::analyze(llvm::DominatorTreeBase<llvm::MachineBasicBlock, false> const&)
Line
Count
Source
540
2.33M
void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
541
2.33M
  // Postorder traversal of the dominator tree.
542
2.33M
  const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
543
11.4M
  for (auto DomNode : post_order(DomRoot)) {
544
11.4M
545
11.4M
    BlockT *Header = DomNode->getBlock();
546
11.4M
    SmallVector<BlockT *, 4> Backedges;
547
11.4M
548
11.4M
    // Check each predecessor of the potential loop header.
549
13.8M
    for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
550
13.8M
      // If Header dominates predBB, this is a new loop. Collect the backedges.
551
13.8M
      if (DomTree.dominates(Header, Backedge) &&
552
13.8M
          
DomTree.isReachableFromEntry(Backedge)889k
) {
553
889k
        Backedges.push_back(Backedge);
554
889k
      }
555
13.8M
    }
556
11.4M
    // Perform a backward CFG traversal to discover and map blocks in this loop.
557
11.4M
    if (!Backedges.empty()) {
558
863k
      LoopT *L = AllocateLoop(Header);
559
863k
      discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
560
863k
    }
561
11.4M
  }
562
2.33M
  // Perform a single forward CFG traversal to populate block and subloop
563
2.33M
  // vectors for all loops.
564
2.33M
  PopulateLoopsDFS<BlockT, LoopT> DFS(this);
565
2.33M
  DFS.traverse(DomRoot->getBlock());
566
2.33M
}
llvm::LoopInfoBase<llvm::VPBlockBase, llvm::VPLoop>::analyze(llvm::DominatorTreeBase<llvm::VPBlockBase, false> const&)
Line
Count
Source
540
25
void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
541
25
  // Postorder traversal of the dominator tree.
542
25
  const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
543
111
  for (auto DomNode : post_order(DomRoot)) {
544
111
545
111
    BlockT *Header = DomNode->getBlock();
546
111
    SmallVector<BlockT *, 4> Backedges;
547
111
548
111
    // Check each predecessor of the potential loop header.
549
127
    for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
550
127
      // If Header dominates predBB, this is a new loop. Collect the backedges.
551
127
      if (DomTree.dominates(Header, Backedge) &&
552
127
          
DomTree.isReachableFromEntry(Backedge)34
) {
553
34
        Backedges.push_back(Backedge);
554
34
      }
555
127
    }
556
111
    // Perform a backward CFG traversal to discover and map blocks in this loop.
557
111
    if (!Backedges.empty()) {
558
34
      LoopT *L = AllocateLoop(Header);
559
34
      discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
560
34
    }
561
111
  }
562
25
  // Perform a single forward CFG traversal to populate block and subloop
563
25
  // vectors for all loops.
564
25
  PopulateLoopsDFS<BlockT, LoopT> DFS(this);
565
25
  DFS.traverse(DomRoot->getBlock());
566
25
}
567
568
template <class BlockT, class LoopT>
569
279k
SmallVector<LoopT *, 4> LoopInfoBase<BlockT, LoopT>::getLoopsInPreorder() {
570
279k
  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
571
279k
  // The outer-most loop actually goes into the result in the same relative
572
279k
  // order as we walk it. But LoopInfo stores the top level loops in reverse
573
279k
  // program order so for here we reverse it to get forward program order.
574
279k
  // FIXME: If we change the order of LoopInfo we will want to remove the
575
279k
  // reverse here.
576
279k
  for (LoopT *RootL : reverse(*this)) {
577
134k
    auto PreOrderLoopsInRootL = RootL->getLoopsInPreorder();
578
134k
    PreOrderLoops.append(PreOrderLoopsInRootL.begin(),
579
134k
                         PreOrderLoopsInRootL.end());
580
134k
  }
581
279k
582
279k
  return PreOrderLoops;
583
279k
}
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::getLoopsInPreorder()
Line
Count
Source
569
279k
SmallVector<LoopT *, 4> LoopInfoBase<BlockT, LoopT>::getLoopsInPreorder() {
570
279k
  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
571
279k
  // The outer-most loop actually goes into the result in the same relative
572
279k
  // order as we walk it. But LoopInfo stores the top level loops in reverse
573
279k
  // program order so for here we reverse it to get forward program order.
574
279k
  // FIXME: If we change the order of LoopInfo we will want to remove the
575
279k
  // reverse here.
576
279k
  for (LoopT *RootL : reverse(*this)) {
577
134k
    auto PreOrderLoopsInRootL = RootL->getLoopsInPreorder();
578
134k
    PreOrderLoops.append(PreOrderLoopsInRootL.begin(),
579
134k
                         PreOrderLoopsInRootL.end());
580
134k
  }
581
279k
582
279k
  return PreOrderLoops;
583
279k
}
Unexecuted instantiation: llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getLoopsInPreorder()
584
585
template <class BlockT, class LoopT>
586
SmallVector<LoopT *, 4>
587
854
LoopInfoBase<BlockT, LoopT>::getLoopsInReverseSiblingPreorder() {
588
854
  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
589
854
  // The outer-most loop actually goes into the result in the same relative
590
854
  // order as we walk it. LoopInfo stores the top level loops in reverse
591
854
  // program order so we walk in order here.
592
854
  // FIXME: If we change the order of LoopInfo we will want to add a reverse
593
854
  // here.
594
1.15k
  for (LoopT *RootL : *this) {
595
1.15k
    assert(PreOrderWorklist.empty() &&
596
1.15k
           "Must start with an empty preorder walk worklist.");
597
1.15k
    PreOrderWorklist.push_back(RootL);
598
1.71k
    do {
599
1.71k
      LoopT *L = PreOrderWorklist.pop_back_val();
600
1.71k
      // Sub-loops are stored in forward program order, but will process the
601
1.71k
      // worklist backwards so we can just append them in order.
602
1.71k
      PreOrderWorklist.append(L->begin(), L->end());
603
1.71k
      PreOrderLoops.push_back(L);
604
1.71k
    } while (!PreOrderWorklist.empty());
605
1.15k
  }
606
854
607
854
  return PreOrderLoops;
608
854
}
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::getLoopsInReverseSiblingPreorder()
Line
Count
Source
587
854
LoopInfoBase<BlockT, LoopT>::getLoopsInReverseSiblingPreorder() {
588
854
  SmallVector<LoopT *, 4> PreOrderLoops, PreOrderWorklist;
589
854
  // The outer-most loop actually goes into the result in the same relative
590
854
  // order as we walk it. LoopInfo stores the top level loops in reverse
591
854
  // program order so we walk in order here.
592
854
  // FIXME: If we change the order of LoopInfo we will want to add a reverse
593
854
  // here.
594
1.15k
  for (LoopT *RootL : *this) {
595
1.15k
    assert(PreOrderWorklist.empty() &&
596
1.15k
           "Must start with an empty preorder walk worklist.");
597
1.15k
    PreOrderWorklist.push_back(RootL);
598
1.71k
    do {
599
1.71k
      LoopT *L = PreOrderWorklist.pop_back_val();
600
1.71k
      // Sub-loops are stored in forward program order, but will process the
601
1.71k
      // worklist backwards so we can just append them in order.
602
1.71k
      PreOrderWorklist.append(L->begin(), L->end());
603
1.71k
      PreOrderLoops.push_back(L);
604
1.71k
    } while (!PreOrderWorklist.empty());
605
1.15k
  }
606
854
607
854
  return PreOrderLoops;
608
854
}
Unexecuted instantiation: llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>::getLoopsInReverseSiblingPreorder()
609
610
// Debugging
611
template <class BlockT, class LoopT>
612
34
void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
613
264
  for (unsigned i = 0; i < TopLevelLoops.size(); 
++i230
)
614
230
    TopLevelLoops[i]->print(OS);
615
#if 0
616
  for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
617
         E = BBMap.end(); I != E; ++I)
618
    OS << "BB '" << I->first->getName() << "' level = "
619
       << I->second->getLoopDepth() << "\n";
620
#endif
621
}
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::print(llvm::raw_ostream&) const
Line
Count
Source
612
34
void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
613
264
  for (unsigned i = 0; i < TopLevelLoops.size(); 
++i230
)
614
230
    TopLevelLoops[i]->print(OS);
615
#if 0
616
  for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
617
         E = BBMap.end(); I != E; ++I)
618
    OS << "BB '" << I->first->getName() << "' level = "
619
       << I->second->getLoopDepth() << "\n";
620
#endif
621
}
Unexecuted instantiation: llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>::print(llvm::raw_ostream&) const
622
623
template <typename T>
624
bool compareVectors(std::vector<T> &BB1, std::vector<T> &BB2) {
625
  llvm::sort(BB1);
626
  llvm::sort(BB2);
627
  return BB1 == BB2;
628
}
629
630
template <class BlockT, class LoopT>
631
void addInnerLoopsToHeadersMap(DenseMap<BlockT *, const LoopT *> &LoopHeaders,
632
                               const LoopInfoBase<BlockT, LoopT> &LI,
633
                               const LoopT &L) {
634
  LoopHeaders[L.getHeader()] = &L;
635
  for (LoopT *SL : L)
636
    addInnerLoopsToHeadersMap(LoopHeaders, LI, *SL);
637
}
638
639
#ifndef NDEBUG
640
template <class BlockT, class LoopT>
641
static void compareLoops(const LoopT *L, const LoopT *OtherL,
642
                         DenseMap<BlockT *, const LoopT *> &OtherLoopHeaders) {
643
  BlockT *H = L->getHeader();
644
  BlockT *OtherH = OtherL->getHeader();
645
  assert(H == OtherH &&
646
         "Mismatched headers even though found in the same map entry!");
647
648
  assert(L->getLoopDepth() == OtherL->getLoopDepth() &&
649
         "Mismatched loop depth!");
650
  const LoopT *ParentL = L, *OtherParentL = OtherL;
651
  do {
652
    assert(ParentL->getHeader() == OtherParentL->getHeader() &&
653
           "Mismatched parent loop headers!");
654
    ParentL = ParentL->getParentLoop();
655
    OtherParentL = OtherParentL->getParentLoop();
656
  } while (ParentL);
657
658
  for (const LoopT *SubL : *L) {
659
    BlockT *SubH = SubL->getHeader();
660
    const LoopT *OtherSubL = OtherLoopHeaders.lookup(SubH);
661
    assert(OtherSubL && "Inner loop is missing in computed loop info!");
662
    OtherLoopHeaders.erase(SubH);
663
    compareLoops(SubL, OtherSubL, OtherLoopHeaders);
664
  }
665
666
  std::vector<BlockT *> BBs = L->getBlocks();
667
  std::vector<BlockT *> OtherBBs = OtherL->getBlocks();
668
  assert(compareVectors(BBs, OtherBBs) &&
669
         "Mismatched basic blocks in the loops!");
670
671
  const SmallPtrSetImpl<const BlockT *> &BlocksSet = L->getBlocksSet();
672
  const SmallPtrSetImpl<const BlockT *> &OtherBlocksSet = L->getBlocksSet();
673
  assert(BlocksSet.size() == OtherBlocksSet.size() &&
674
         std::all_of(BlocksSet.begin(), BlocksSet.end(),
675
                     [&OtherBlocksSet](const BlockT *BB) {
676
                       return OtherBlocksSet.count(BB);
677
                     }) &&
678
         "Mismatched basic blocks in BlocksSets!");
679
}
680
#endif
681
682
template <class BlockT, class LoopT>
683
void LoopInfoBase<BlockT, LoopT>::verify(
684
146
    const DomTreeBase<BlockT> &DomTree) const {
685
146
  DenseSet<const LoopT *> Loops;
686
341
  for (iterator I = begin(), E = end(); I != E; 
++I195
) {
687
195
    assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
688
195
    (*I)->verifyLoopNest(&Loops);
689
195
  }
690
146
691
146
// Verify that blocks are mapped to valid loops.
692
#ifndef NDEBUG
693
  for (auto &Entry : BBMap) {
694
    const BlockT *BB = Entry.first;
695
    LoopT *L = Entry.second;
696
    assert(Loops.count(L) && "orphaned loop");
697
    assert(L->contains(BB) && "orphaned block");
698
    for (LoopT *ChildLoop : *L)
699
      assert(!ChildLoop->contains(BB) &&
700
             "BBMap should point to the innermost loop containing BB");
701
  }
702
703
  // Recompute LoopInfo to verify loops structure.
704
  LoopInfoBase<BlockT, LoopT> OtherLI;
705
  OtherLI.analyze(DomTree);
706
707
  // Build a map we can use to move from our LI to the computed one. This
708
  // allows us to ignore the particular order in any layer of the loop forest
709
  // while still comparing the structure.
710
  DenseMap<BlockT *, const LoopT *> OtherLoopHeaders;
711
  for (LoopT *L : OtherLI)
712
    addInnerLoopsToHeadersMap(OtherLoopHeaders, OtherLI, *L);
713
714
  // Walk the top level loops and ensure there is a corresponding top-level
715
  // loop in the computed version and then recursively compare those loop
716
  // nests.
717
  for (LoopT *L : *this) {
718
    BlockT *Header = L->getHeader();
719
    const LoopT *OtherL = OtherLoopHeaders.lookup(Header);
720
    assert(OtherL && "Top level loop is missing in computed loop info!");
721
    // Now that we've matched this loop, erase its header from the map.
722
    OtherLoopHeaders.erase(Header);
723
    // And recursively compare these loops.
724
    compareLoops(L, OtherL, OtherLoopHeaders);
725
  }
726
727
  // Any remaining entries in the map are loops which were found when computing
728
  // a fresh LoopInfo but not present in the current one.
729
  if (!OtherLoopHeaders.empty()) {
730
    for (const auto &HeaderAndLoop : OtherLoopHeaders)
731
      dbgs() << "Found new loop: " << *HeaderAndLoop.second << "\n";
732
    llvm_unreachable("Found new loops when recomputing LoopInfo!");
733
  }
734
#endif
735
}
llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::verify(llvm::DominatorTreeBase<llvm::BasicBlock, false> const&) const
Line
Count
Source
684
146
    const DomTreeBase<BlockT> &DomTree) const {
685
146
  DenseSet<const LoopT *> Loops;
686
341
  for (iterator I = begin(), E = end(); I != E; 
++I195
) {
687
195
    assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
688
195
    (*I)->verifyLoopNest(&Loops);
689
195
  }
690
146
691
146
// Verify that blocks are mapped to valid loops.
692
#ifndef NDEBUG
693
  for (auto &Entry : BBMap) {
694
    const BlockT *BB = Entry.first;
695
    LoopT *L = Entry.second;
696
    assert(Loops.count(L) && "orphaned loop");
697
    assert(L->contains(BB) && "orphaned block");
698
    for (LoopT *ChildLoop : *L)
699
      assert(!ChildLoop->contains(BB) &&
700
             "BBMap should point to the innermost loop containing BB");
701
  }
702
703
  // Recompute LoopInfo to verify loops structure.
704
  LoopInfoBase<BlockT, LoopT> OtherLI;
705
  OtherLI.analyze(DomTree);
706
707
  // Build a map we can use to move from our LI to the computed one. This
708
  // allows us to ignore the particular order in any layer of the loop forest
709
  // while still comparing the structure.
710
  DenseMap<BlockT *, const LoopT *> OtherLoopHeaders;
711
  for (LoopT *L : OtherLI)
712
    addInnerLoopsToHeadersMap(OtherLoopHeaders, OtherLI, *L);
713
714
  // Walk the top level loops and ensure there is a corresponding top-level
715
  // loop in the computed version and then recursively compare those loop
716
  // nests.
717
  for (LoopT *L : *this) {
718
    BlockT *Header = L->getHeader();
719
    const LoopT *OtherL = OtherLoopHeaders.lookup(Header);
720
    assert(OtherL && "Top level loop is missing in computed loop info!");
721
    // Now that we've matched this loop, erase its header from the map.
722
    OtherLoopHeaders.erase(Header);
723
    // And recursively compare these loops.
724
    compareLoops(L, OtherL, OtherLoopHeaders);
725
  }
726
727
  // Any remaining entries in the map are loops which were found when computing
728
  // a fresh LoopInfo but not present in the current one.
729
  if (!OtherLoopHeaders.empty()) {
730
    for (const auto &HeaderAndLoop : OtherLoopHeaders)
731
      dbgs() << "Found new loop: " << *HeaderAndLoop.second << "\n";
732
    llvm_unreachable("Found new loops when recomputing LoopInfo!");
733
  }
734
#endif
735
}
Unexecuted instantiation: llvm::LoopInfoBase<llvm::MachineBasicBlock, llvm::MachineLoop>::verify(llvm::DominatorTreeBase<llvm::MachineBasicBlock, false> const&) const
736
737
} // End llvm namespace
738
739
#endif