Skip to content

Commit 6e5aaba

Browse files
committed
[FixIrreducible] Use CycleInfo instead of a custom SCC traversal
1. CycleInfo efficiently locates all cycles in a single pass, while the SCC is repeated inside every natural loop. 2. CycleInfo provides a hierarchy of irreducible cycles, and the new implementation transforms each cycle in this hierarchy separately instead of reducing an entire irreducible SCC in a single step. This reduces the number of control-flow paths that pass through the header of each newly created loop. This is evidenced by the reduced number of predecessors on the "guard" blocks in the lit tests, and fewer operands on the corresponding PHI nodes. 3. When an entry of an irreducible cycle is the header of a child natural loop, the original implementation destroyed that loop. This is now preserved, since the incoming edges on non-header entries are not touched. 4. In the new implementation, if an irreducible cycle is a superset of a natural loop with the same header, then that natural loop is destroyed and replaced by the newly created loop.
1 parent e439fdf commit 6e5aaba

File tree

10 files changed

+545
-394
lines changed

10 files changed

+545
-394
lines changed

llvm/include/llvm/ADT/GenericCycleInfo.h

+20-8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ template <typename ContextT> class GenericCycle {
107107
return is_contained(Entries, Block);
108108
}
109109

110+
/// \brief Replace all entries with \p Block as single entry.
111+
void setSingleEntry(BlockT *Block) {
112+
assert(contains(Block));
113+
Entries.clear();
114+
Entries.push_back(Block);
115+
}
116+
110117
/// \brief Return whether \p Block is contained in the cycle.
111118
bool contains(const BlockT *Block) const { return Blocks.contains(Block); }
112119

@@ -192,11 +199,16 @@ template <typename ContextT> class GenericCycle {
192199
//@{
193200
using const_entry_iterator =
194201
typename SmallVectorImpl<BlockT *>::const_iterator;
195-
202+
const_entry_iterator entry_begin() const { return Entries.begin(); }
203+
const_entry_iterator entry_end() const { return Entries.end(); }
196204
size_t getNumEntries() const { return Entries.size(); }
197205
iterator_range<const_entry_iterator> entries() const {
198-
return llvm::make_range(Entries.begin(), Entries.end());
206+
return llvm::make_range(entry_begin(), entry_end());
199207
}
208+
using const_reverse_entry_iterator =
209+
typename SmallVectorImpl<BlockT *>::const_reverse_iterator;
210+
const_reverse_entry_iterator entry_rbegin() const { return Entries.rbegin(); }
211+
const_reverse_entry_iterator entry_rend() const { return Entries.rend(); }
200212
//@}
201213

202214
Printable printEntries(const ContextT &Ctx) const {
@@ -255,12 +267,6 @@ template <typename ContextT> class GenericCycleInfo {
255267
/// the subtree.
256268
void moveTopLevelCycleToNewParent(CycleT *NewParent, CycleT *Child);
257269

258-
/// Assumes that \p Cycle is the innermost cycle containing \p Block.
259-
/// \p Block will be appended to \p Cycle and all of its parent cycles.
260-
/// \p Block will be added to BlockMap with \p Cycle and
261-
/// BlockMapTopLevel with \p Cycle's top level parent cycle.
262-
void addBlockToCycle(BlockT *Block, CycleT *Cycle);
263-
264270
public:
265271
GenericCycleInfo() = default;
266272
GenericCycleInfo(GenericCycleInfo &&) = default;
@@ -278,6 +284,12 @@ template <typename ContextT> class GenericCycleInfo {
278284
unsigned getCycleDepth(const BlockT *Block) const;
279285
CycleT *getTopLevelParentCycle(BlockT *Block);
280286

287+
/// Assumes that \p Cycle is the innermost cycle containing \p Block.
288+
/// \p Block will be appended to \p Cycle and all of its parent cycles.
289+
/// \p Block will be added to BlockMap with \p Cycle and
290+
/// BlockMapTopLevel with \p Cycle's top level parent cycle.
291+
void addBlockToCycle(BlockT *Block, CycleT *Cycle);
292+
281293
/// Methods for debug and self-test.
282294
//@{
283295
void verifyCycleNest(bool VerifyFull = false) const;

0 commit comments

Comments
 (0)