xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/FixIrreducible.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
15ffd83dbSDimitry Andric //===- FixIrreducible.cpp - Convert irreducible control-flow into loops ---===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric //
95ffd83dbSDimitry Andric // An irreducible SCC is one which has multiple "header" blocks, i.e., blocks
105ffd83dbSDimitry Andric // with control-flow edges incident from outside the SCC.  This pass converts a
115ffd83dbSDimitry Andric // irreducible SCC into a natural loop by applying the following transformation:
125ffd83dbSDimitry Andric //
135ffd83dbSDimitry Andric // 1. Collect the set of headers H of the SCC.
145ffd83dbSDimitry Andric // 2. Collect the set of predecessors P of these headers. These may be inside as
155ffd83dbSDimitry Andric //    well as outside the SCC.
165ffd83dbSDimitry Andric // 3. Create block N and redirect every edge from set P to set H through N.
175ffd83dbSDimitry Andric //
185ffd83dbSDimitry Andric // This converts the SCC into a natural loop with N as the header: N is the only
195ffd83dbSDimitry Andric // block with edges incident from outside the SCC, and all backedges in the SCC
205ffd83dbSDimitry Andric // are incident on N, i.e., for every backedge, the head now dominates the tail.
215ffd83dbSDimitry Andric //
225ffd83dbSDimitry Andric // INPUT CFG: The blocks A and B form an irreducible loop with two headers.
235ffd83dbSDimitry Andric //
245ffd83dbSDimitry Andric //                        Entry
255ffd83dbSDimitry Andric //                       /     \
265ffd83dbSDimitry Andric //                      v       v
275ffd83dbSDimitry Andric //                      A ----> B
285ffd83dbSDimitry Andric //                      ^      /|
295ffd83dbSDimitry Andric //                       `----' |
305ffd83dbSDimitry Andric //                              v
315ffd83dbSDimitry Andric //                             Exit
325ffd83dbSDimitry Andric //
335ffd83dbSDimitry Andric // OUTPUT CFG: Edges incident on A and B are now redirected through a
345ffd83dbSDimitry Andric // new block N, forming a natural loop consisting of N, A and B.
355ffd83dbSDimitry Andric //
365ffd83dbSDimitry Andric //                        Entry
375ffd83dbSDimitry Andric //                          |
385ffd83dbSDimitry Andric //                          v
395ffd83dbSDimitry Andric //                    .---> N <---.
405ffd83dbSDimitry Andric //                   /     / \     \
415ffd83dbSDimitry Andric //                  |     /   \     |
425ffd83dbSDimitry Andric //                  \    v     v    /
435ffd83dbSDimitry Andric //                   `-- A     B --'
445ffd83dbSDimitry Andric //                             |
455ffd83dbSDimitry Andric //                             v
465ffd83dbSDimitry Andric //                            Exit
475ffd83dbSDimitry Andric //
485ffd83dbSDimitry Andric // The transformation is applied to every maximal SCC that is not already
495ffd83dbSDimitry Andric // recognized as a loop. The pass operates on all maximal SCCs found in the
505ffd83dbSDimitry Andric // function body outside of any loop, as well as those found inside each loop,
515ffd83dbSDimitry Andric // including inside any newly created loops. This ensures that any SCC hidden
525ffd83dbSDimitry Andric // inside a maximal SCC is also transformed.
535ffd83dbSDimitry Andric //
545ffd83dbSDimitry Andric // The actual transformation is handled by function CreateControlFlowHub, which
555ffd83dbSDimitry Andric // takes a set of incoming blocks (the predecessors) and outgoing blocks (the
565ffd83dbSDimitry Andric // headers). The function also moves every PHINode in an outgoing block to the
575ffd83dbSDimitry Andric // hub. Since the hub dominates all the outgoing blocks, each such PHINode
585ffd83dbSDimitry Andric // continues to dominate its uses. Since every header in an SCC has at least two
595ffd83dbSDimitry Andric // predecessors, every value used in the header (or later) but defined in a
605ffd83dbSDimitry Andric // predecessor (or earlier) is represented by a PHINode in a header. Hence the
615ffd83dbSDimitry Andric // above handling of PHINodes is sufficient and no further processing is
625ffd83dbSDimitry Andric // required to restore SSA.
635ffd83dbSDimitry Andric //
645ffd83dbSDimitry Andric // Limitation: The pass cannot handle switch statements and indirect
655ffd83dbSDimitry Andric //             branches. Both must be lowered to plain branches first.
665ffd83dbSDimitry Andric //
675ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
685ffd83dbSDimitry Andric 
69e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/FixIrreducible.h"
705ffd83dbSDimitry Andric #include "llvm/ADT/SCCIterator.h"
715ffd83dbSDimitry Andric #include "llvm/Analysis/LoopIterator.h"
725ffd83dbSDimitry Andric #include "llvm/InitializePasses.h"
735ffd83dbSDimitry Andric #include "llvm/Pass.h"
745ffd83dbSDimitry Andric #include "llvm/Transforms/Utils.h"
755ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
765ffd83dbSDimitry Andric 
775ffd83dbSDimitry Andric #define DEBUG_TYPE "fix-irreducible"
785ffd83dbSDimitry Andric 
795ffd83dbSDimitry Andric using namespace llvm;
805ffd83dbSDimitry Andric 
815ffd83dbSDimitry Andric namespace {
825ffd83dbSDimitry Andric struct FixIrreducible : public FunctionPass {
835ffd83dbSDimitry Andric   static char ID;
845ffd83dbSDimitry Andric   FixIrreducible() : FunctionPass(ID) {
855ffd83dbSDimitry Andric     initializeFixIrreduciblePass(*PassRegistry::getPassRegistry());
865ffd83dbSDimitry Andric   }
875ffd83dbSDimitry Andric 
885ffd83dbSDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
895ffd83dbSDimitry Andric     AU.addRequiredID(LowerSwitchID);
905ffd83dbSDimitry Andric     AU.addRequired<DominatorTreeWrapperPass>();
915ffd83dbSDimitry Andric     AU.addRequired<LoopInfoWrapperPass>();
925ffd83dbSDimitry Andric     AU.addPreservedID(LowerSwitchID);
935ffd83dbSDimitry Andric     AU.addPreserved<DominatorTreeWrapperPass>();
945ffd83dbSDimitry Andric     AU.addPreserved<LoopInfoWrapperPass>();
955ffd83dbSDimitry Andric   }
965ffd83dbSDimitry Andric 
975ffd83dbSDimitry Andric   bool runOnFunction(Function &F) override;
985ffd83dbSDimitry Andric };
995ffd83dbSDimitry Andric } // namespace
1005ffd83dbSDimitry Andric 
1015ffd83dbSDimitry Andric char FixIrreducible::ID = 0;
1025ffd83dbSDimitry Andric 
1035ffd83dbSDimitry Andric FunctionPass *llvm::createFixIrreduciblePass() { return new FixIrreducible(); }
1045ffd83dbSDimitry Andric 
1055ffd83dbSDimitry Andric INITIALIZE_PASS_BEGIN(FixIrreducible, "fix-irreducible",
1065ffd83dbSDimitry Andric                       "Convert irreducible control-flow into natural loops",
1075ffd83dbSDimitry Andric                       false /* Only looks at CFG */, false /* Analysis Pass */)
108e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LowerSwitchLegacyPass)
1095ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1105ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1115ffd83dbSDimitry Andric INITIALIZE_PASS_END(FixIrreducible, "fix-irreducible",
1125ffd83dbSDimitry Andric                     "Convert irreducible control-flow into natural loops",
1135ffd83dbSDimitry Andric                     false /* Only looks at CFG */, false /* Analysis Pass */)
1145ffd83dbSDimitry Andric 
1155ffd83dbSDimitry Andric // When a new loop is created, existing children of the parent loop may now be
1165ffd83dbSDimitry Andric // fully inside the new loop. Reconnect these as children of the new loop.
1175ffd83dbSDimitry Andric static void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop,
1185ffd83dbSDimitry Andric                                 SetVector<BasicBlock *> &Blocks,
1195ffd83dbSDimitry Andric                                 SetVector<BasicBlock *> &Headers) {
1205ffd83dbSDimitry Andric   auto &CandidateLoops = ParentLoop ? ParentLoop->getSubLoopsVector()
1215ffd83dbSDimitry Andric                                     : LI.getTopLevelLoopsVector();
1225ffd83dbSDimitry Andric   // The new loop cannot be its own child, and any candidate is a
1235ffd83dbSDimitry Andric   // child iff its header is owned by the new loop. Move all the
1245ffd83dbSDimitry Andric   // children to a new vector.
1255ffd83dbSDimitry Andric   auto FirstChild = std::partition(
1265ffd83dbSDimitry Andric       CandidateLoops.begin(), CandidateLoops.end(), [&](Loop *L) {
1275ffd83dbSDimitry Andric         return L == NewLoop || Blocks.count(L->getHeader()) == 0;
1285ffd83dbSDimitry Andric       });
1295ffd83dbSDimitry Andric   SmallVector<Loop *, 8> ChildLoops(FirstChild, CandidateLoops.end());
1305ffd83dbSDimitry Andric   CandidateLoops.erase(FirstChild, CandidateLoops.end());
1315ffd83dbSDimitry Andric 
132*fe6060f1SDimitry Andric   for (Loop *Child : ChildLoops) {
1335ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "child loop: " << Child->getHeader()->getName()
1345ffd83dbSDimitry Andric                       << "\n");
1355ffd83dbSDimitry Andric     // TODO: A child loop whose header is also a header in the current
1365ffd83dbSDimitry Andric     // SCC gets destroyed since its backedges are removed. That may
1375ffd83dbSDimitry Andric     // not be necessary if we can retain such backedges.
1385ffd83dbSDimitry Andric     if (Headers.count(Child->getHeader())) {
1395ffd83dbSDimitry Andric       for (auto BB : Child->blocks()) {
1405ffd83dbSDimitry Andric         LI.changeLoopFor(BB, NewLoop);
1415ffd83dbSDimitry Andric         LLVM_DEBUG(dbgs() << "moved block from child: " << BB->getName()
1425ffd83dbSDimitry Andric                           << "\n");
1435ffd83dbSDimitry Andric       }
1445ffd83dbSDimitry Andric       LI.destroy(Child);
1455ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "subsumed child loop (common header)\n");
1465ffd83dbSDimitry Andric       continue;
1475ffd83dbSDimitry Andric     }
1485ffd83dbSDimitry Andric 
1495ffd83dbSDimitry Andric     Child->setParentLoop(nullptr);
1505ffd83dbSDimitry Andric     NewLoop->addChildLoop(Child);
1515ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "added child loop to new loop\n");
1525ffd83dbSDimitry Andric   }
1535ffd83dbSDimitry Andric }
1545ffd83dbSDimitry Andric 
1555ffd83dbSDimitry Andric // Given a set of blocks and headers in an irreducible SCC, convert it into a
1565ffd83dbSDimitry Andric // natural loop. Also insert this new loop at its appropriate place in the
1575ffd83dbSDimitry Andric // hierarchy of loops.
1585ffd83dbSDimitry Andric static void createNaturalLoopInternal(LoopInfo &LI, DominatorTree &DT,
1595ffd83dbSDimitry Andric                                       Loop *ParentLoop,
1605ffd83dbSDimitry Andric                                       SetVector<BasicBlock *> &Blocks,
1615ffd83dbSDimitry Andric                                       SetVector<BasicBlock *> &Headers) {
1625ffd83dbSDimitry Andric #ifndef NDEBUG
1635ffd83dbSDimitry Andric   // All headers are part of the SCC
1645ffd83dbSDimitry Andric   for (auto H : Headers) {
1655ffd83dbSDimitry Andric     assert(Blocks.count(H));
1665ffd83dbSDimitry Andric   }
1675ffd83dbSDimitry Andric #endif
1685ffd83dbSDimitry Andric 
1695ffd83dbSDimitry Andric   SetVector<BasicBlock *> Predecessors;
1705ffd83dbSDimitry Andric   for (auto H : Headers) {
1715ffd83dbSDimitry Andric     for (auto P : predecessors(H)) {
1725ffd83dbSDimitry Andric       Predecessors.insert(P);
1735ffd83dbSDimitry Andric     }
1745ffd83dbSDimitry Andric   }
1755ffd83dbSDimitry Andric 
1765ffd83dbSDimitry Andric   LLVM_DEBUG(
1775ffd83dbSDimitry Andric       dbgs() << "Found predecessors:";
1785ffd83dbSDimitry Andric       for (auto P : Predecessors) {
1795ffd83dbSDimitry Andric         dbgs() << " " << P->getName();
1805ffd83dbSDimitry Andric       }
1815ffd83dbSDimitry Andric       dbgs() << "\n");
1825ffd83dbSDimitry Andric 
1835ffd83dbSDimitry Andric   // Redirect all the backedges through a "hub" consisting of a series
1845ffd83dbSDimitry Andric   // of guard blocks that manage the flow of control from the
1855ffd83dbSDimitry Andric   // predecessors to the headers.
1865ffd83dbSDimitry Andric   SmallVector<BasicBlock *, 8> GuardBlocks;
1875ffd83dbSDimitry Andric   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
1885ffd83dbSDimitry Andric   CreateControlFlowHub(&DTU, GuardBlocks, Predecessors, Headers, "irr");
1895ffd83dbSDimitry Andric #if defined(EXPENSIVE_CHECKS)
1905ffd83dbSDimitry Andric   assert(DT.verify(DominatorTree::VerificationLevel::Full));
1915ffd83dbSDimitry Andric #else
1925ffd83dbSDimitry Andric   assert(DT.verify(DominatorTree::VerificationLevel::Fast));
1935ffd83dbSDimitry Andric #endif
1945ffd83dbSDimitry Andric 
1955ffd83dbSDimitry Andric   // Create a new loop from the now-transformed cycle
1965ffd83dbSDimitry Andric   auto NewLoop = LI.AllocateLoop();
1975ffd83dbSDimitry Andric   if (ParentLoop) {
1985ffd83dbSDimitry Andric     ParentLoop->addChildLoop(NewLoop);
1995ffd83dbSDimitry Andric   } else {
2005ffd83dbSDimitry Andric     LI.addTopLevelLoop(NewLoop);
2015ffd83dbSDimitry Andric   }
2025ffd83dbSDimitry Andric 
2035ffd83dbSDimitry Andric   // Add the guard blocks to the new loop. The first guard block is
2045ffd83dbSDimitry Andric   // the head of all the backedges, and it is the first to be inserted
2055ffd83dbSDimitry Andric   // in the loop. This ensures that it is recognized as the
2065ffd83dbSDimitry Andric   // header. Since the new loop is already in LoopInfo, the new blocks
2075ffd83dbSDimitry Andric   // are also propagated up the chain of parent loops.
2085ffd83dbSDimitry Andric   for (auto G : GuardBlocks) {
2095ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "added guard block: " << G->getName() << "\n");
2105ffd83dbSDimitry Andric     NewLoop->addBasicBlockToLoop(G, LI);
2115ffd83dbSDimitry Andric   }
2125ffd83dbSDimitry Andric 
2135ffd83dbSDimitry Andric   // Add the SCC blocks to the new loop.
2145ffd83dbSDimitry Andric   for (auto BB : Blocks) {
2155ffd83dbSDimitry Andric     NewLoop->addBlockEntry(BB);
2165ffd83dbSDimitry Andric     if (LI.getLoopFor(BB) == ParentLoop) {
2175ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "moved block from parent: " << BB->getName()
2185ffd83dbSDimitry Andric                         << "\n");
2195ffd83dbSDimitry Andric       LI.changeLoopFor(BB, NewLoop);
2205ffd83dbSDimitry Andric     } else {
2215ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "added block from child: " << BB->getName() << "\n");
2225ffd83dbSDimitry Andric     }
2235ffd83dbSDimitry Andric   }
2245ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "header for new loop: "
2255ffd83dbSDimitry Andric                     << NewLoop->getHeader()->getName() << "\n");
2265ffd83dbSDimitry Andric 
2275ffd83dbSDimitry Andric   reconnectChildLoops(LI, ParentLoop, NewLoop, Blocks, Headers);
2285ffd83dbSDimitry Andric 
2295ffd83dbSDimitry Andric   NewLoop->verifyLoop();
2305ffd83dbSDimitry Andric   if (ParentLoop) {
2315ffd83dbSDimitry Andric     ParentLoop->verifyLoop();
2325ffd83dbSDimitry Andric   }
2335ffd83dbSDimitry Andric #if defined(EXPENSIVE_CHECKS)
2345ffd83dbSDimitry Andric   LI.verify(DT);
2355ffd83dbSDimitry Andric #endif // EXPENSIVE_CHECKS
2365ffd83dbSDimitry Andric }
2375ffd83dbSDimitry Andric 
2385ffd83dbSDimitry Andric namespace llvm {
2395ffd83dbSDimitry Andric // Enable the graph traits required for traversing a Loop body.
2405ffd83dbSDimitry Andric template <> struct GraphTraits<Loop> : LoopBodyTraits {};
2415ffd83dbSDimitry Andric } // namespace llvm
2425ffd83dbSDimitry Andric 
2435ffd83dbSDimitry Andric // Overloaded wrappers to go with the function template below.
2445ffd83dbSDimitry Andric static BasicBlock *unwrapBlock(BasicBlock *B) { return B; }
2455ffd83dbSDimitry Andric static BasicBlock *unwrapBlock(LoopBodyTraits::NodeRef &N) { return N.second; }
2465ffd83dbSDimitry Andric 
2475ffd83dbSDimitry Andric static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Function *F,
2485ffd83dbSDimitry Andric                               SetVector<BasicBlock *> &Blocks,
2495ffd83dbSDimitry Andric                               SetVector<BasicBlock *> &Headers) {
2505ffd83dbSDimitry Andric   createNaturalLoopInternal(LI, DT, nullptr, Blocks, Headers);
2515ffd83dbSDimitry Andric }
2525ffd83dbSDimitry Andric 
2535ffd83dbSDimitry Andric static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Loop &L,
2545ffd83dbSDimitry Andric                               SetVector<BasicBlock *> &Blocks,
2555ffd83dbSDimitry Andric                               SetVector<BasicBlock *> &Headers) {
2565ffd83dbSDimitry Andric   createNaturalLoopInternal(LI, DT, &L, Blocks, Headers);
2575ffd83dbSDimitry Andric }
2585ffd83dbSDimitry Andric 
2595ffd83dbSDimitry Andric // Convert irreducible SCCs; Graph G may be a Function* or a Loop&.
2605ffd83dbSDimitry Andric template <class Graph>
2615ffd83dbSDimitry Andric static bool makeReducible(LoopInfo &LI, DominatorTree &DT, Graph &&G) {
2625ffd83dbSDimitry Andric   bool Changed = false;
2635ffd83dbSDimitry Andric   for (auto Scc = scc_begin(G); !Scc.isAtEnd(); ++Scc) {
2645ffd83dbSDimitry Andric     if (Scc->size() < 2)
2655ffd83dbSDimitry Andric       continue;
2665ffd83dbSDimitry Andric     SetVector<BasicBlock *> Blocks;
2675ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "Found SCC:");
2685ffd83dbSDimitry Andric     for (auto N : *Scc) {
2695ffd83dbSDimitry Andric       auto BB = unwrapBlock(N);
2705ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << " " << BB->getName());
2715ffd83dbSDimitry Andric       Blocks.insert(BB);
2725ffd83dbSDimitry Andric     }
2735ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
2745ffd83dbSDimitry Andric 
2755ffd83dbSDimitry Andric     // Minor optimization: The SCC blocks are usually discovered in an order
2765ffd83dbSDimitry Andric     // that is the opposite of the order in which these blocks appear as branch
2775ffd83dbSDimitry Andric     // targets. This results in a lot of condition inversions in the control
2785ffd83dbSDimitry Andric     // flow out of the new ControlFlowHub, which can be mitigated if the orders
2795ffd83dbSDimitry Andric     // match. So we discover the headers using the reverse of the block order.
2805ffd83dbSDimitry Andric     SetVector<BasicBlock *> Headers;
2815ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "Found headers:");
2825ffd83dbSDimitry Andric     for (auto BB : reverse(Blocks)) {
2835ffd83dbSDimitry Andric       for (const auto P : predecessors(BB)) {
2845ffd83dbSDimitry Andric         // Skip unreachable predecessors.
2855ffd83dbSDimitry Andric         if (!DT.isReachableFromEntry(P))
2865ffd83dbSDimitry Andric           continue;
2875ffd83dbSDimitry Andric         if (!Blocks.count(P)) {
2885ffd83dbSDimitry Andric           LLVM_DEBUG(dbgs() << " " << BB->getName());
2895ffd83dbSDimitry Andric           Headers.insert(BB);
2905ffd83dbSDimitry Andric           break;
2915ffd83dbSDimitry Andric         }
2925ffd83dbSDimitry Andric       }
2935ffd83dbSDimitry Andric     }
2945ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
2955ffd83dbSDimitry Andric 
2965ffd83dbSDimitry Andric     if (Headers.size() == 1) {
2975ffd83dbSDimitry Andric       assert(LI.isLoopHeader(Headers.front()));
2985ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "Natural loop with a single header: skipped\n");
2995ffd83dbSDimitry Andric       continue;
3005ffd83dbSDimitry Andric     }
3015ffd83dbSDimitry Andric     createNaturalLoop(LI, DT, G, Blocks, Headers);
3025ffd83dbSDimitry Andric     Changed = true;
3035ffd83dbSDimitry Andric   }
3045ffd83dbSDimitry Andric   return Changed;
3055ffd83dbSDimitry Andric }
3065ffd83dbSDimitry Andric 
307e8d8bef9SDimitry Andric static bool FixIrreducibleImpl(Function &F, LoopInfo &LI, DominatorTree &DT) {
3085ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "===== Fix irreducible control-flow in function: "
3095ffd83dbSDimitry Andric                     << F.getName() << "\n");
3105ffd83dbSDimitry Andric 
3115ffd83dbSDimitry Andric   bool Changed = false;
3125ffd83dbSDimitry Andric   SmallVector<Loop *, 8> WorkList;
3135ffd83dbSDimitry Andric 
3145ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "visiting top-level\n");
3155ffd83dbSDimitry Andric   Changed |= makeReducible(LI, DT, &F);
3165ffd83dbSDimitry Andric 
3175ffd83dbSDimitry Andric   // Any SCCs reduced are now already in the list of top-level loops, so simply
3185ffd83dbSDimitry Andric   // add them all to the worklist.
319e8d8bef9SDimitry Andric   append_range(WorkList, LI);
3205ffd83dbSDimitry Andric 
3215ffd83dbSDimitry Andric   while (!WorkList.empty()) {
322e8d8bef9SDimitry Andric     auto L = WorkList.pop_back_val();
3235ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "visiting loop with header "
3245ffd83dbSDimitry Andric                       << L->getHeader()->getName() << "\n");
3255ffd83dbSDimitry Andric     Changed |= makeReducible(LI, DT, *L);
3265ffd83dbSDimitry Andric     // Any SCCs reduced are now already in the list of child loops, so simply
3275ffd83dbSDimitry Andric     // add them all to the worklist.
3285ffd83dbSDimitry Andric     WorkList.append(L->begin(), L->end());
3295ffd83dbSDimitry Andric   }
3305ffd83dbSDimitry Andric 
3315ffd83dbSDimitry Andric   return Changed;
3325ffd83dbSDimitry Andric }
333e8d8bef9SDimitry Andric 
334e8d8bef9SDimitry Andric bool FixIrreducible::runOnFunction(Function &F) {
335e8d8bef9SDimitry Andric   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
336e8d8bef9SDimitry Andric   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
337e8d8bef9SDimitry Andric   return FixIrreducibleImpl(F, LI, DT);
338e8d8bef9SDimitry Andric }
339e8d8bef9SDimitry Andric 
340e8d8bef9SDimitry Andric PreservedAnalyses FixIrreduciblePass::run(Function &F,
341e8d8bef9SDimitry Andric                                           FunctionAnalysisManager &AM) {
342e8d8bef9SDimitry Andric   auto &LI = AM.getResult<LoopAnalysis>(F);
343e8d8bef9SDimitry Andric   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
344e8d8bef9SDimitry Andric   if (!FixIrreducibleImpl(F, LI, DT))
345e8d8bef9SDimitry Andric     return PreservedAnalyses::all();
346e8d8bef9SDimitry Andric   PreservedAnalyses PA;
347e8d8bef9SDimitry Andric   PA.preserve<LoopAnalysis>();
348e8d8bef9SDimitry Andric   PA.preserve<DominatorTreeAnalysis>();
349e8d8bef9SDimitry Andric   return PA;
350e8d8bef9SDimitry Andric }
351