Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/FixIrreducible.cpp
35271 views
//===- FixIrreducible.cpp - Convert irreducible control-flow into loops ---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// An irreducible SCC is one which has multiple "header" blocks, i.e., blocks9// with control-flow edges incident from outside the SCC. This pass converts a10// irreducible SCC into a natural loop by applying the following transformation:11//12// 1. Collect the set of headers H of the SCC.13// 2. Collect the set of predecessors P of these headers. These may be inside as14// well as outside the SCC.15// 3. Create block N and redirect every edge from set P to set H through N.16//17// This converts the SCC into a natural loop with N as the header: N is the only18// block with edges incident from outside the SCC, and all backedges in the SCC19// are incident on N, i.e., for every backedge, the head now dominates the tail.20//21// INPUT CFG: The blocks A and B form an irreducible loop with two headers.22//23// Entry24// / \25// v v26// A ----> B27// ^ /|28// `----' |29// v30// Exit31//32// OUTPUT CFG: Edges incident on A and B are now redirected through a33// new block N, forming a natural loop consisting of N, A and B.34//35// Entry36// |37// v38// .---> N <---.39// / / \ \40// | / \ |41// \ v v /42// `-- A B --'43// |44// v45// Exit46//47// The transformation is applied to every maximal SCC that is not already48// recognized as a loop. The pass operates on all maximal SCCs found in the49// function body outside of any loop, as well as those found inside each loop,50// including inside any newly created loops. This ensures that any SCC hidden51// inside a maximal SCC is also transformed.52//53// The actual transformation is handled by function CreateControlFlowHub, which54// takes a set of incoming blocks (the predecessors) and outgoing blocks (the55// headers). The function also moves every PHINode in an outgoing block to the56// hub. Since the hub dominates all the outgoing blocks, each such PHINode57// continues to dominate its uses. Since every header in an SCC has at least two58// predecessors, every value used in the header (or later) but defined in a59// predecessor (or earlier) is represented by a PHINode in a header. Hence the60// above handling of PHINodes is sufficient and no further processing is61// required to restore SSA.62//63// Limitation: The pass cannot handle switch statements and indirect64// branches. Both must be lowered to plain branches first.65//66//===----------------------------------------------------------------------===//6768#include "llvm/Transforms/Utils/FixIrreducible.h"69#include "llvm/ADT/SCCIterator.h"70#include "llvm/Analysis/DomTreeUpdater.h"71#include "llvm/Analysis/LoopIterator.h"72#include "llvm/InitializePasses.h"73#include "llvm/Pass.h"74#include "llvm/Transforms/Utils.h"75#include "llvm/Transforms/Utils/BasicBlockUtils.h"7677#define DEBUG_TYPE "fix-irreducible"7879using namespace llvm;8081namespace {82struct FixIrreducible : public FunctionPass {83static char ID;84FixIrreducible() : FunctionPass(ID) {85initializeFixIrreduciblePass(*PassRegistry::getPassRegistry());86}8788void getAnalysisUsage(AnalysisUsage &AU) const override {89AU.addRequired<DominatorTreeWrapperPass>();90AU.addRequired<LoopInfoWrapperPass>();91AU.addPreserved<DominatorTreeWrapperPass>();92AU.addPreserved<LoopInfoWrapperPass>();93}9495bool runOnFunction(Function &F) override;96};97} // namespace9899char FixIrreducible::ID = 0;100101FunctionPass *llvm::createFixIrreduciblePass() { return new FixIrreducible(); }102103INITIALIZE_PASS_BEGIN(FixIrreducible, "fix-irreducible",104"Convert irreducible control-flow into natural loops",105false /* Only looks at CFG */, false /* Analysis Pass */)106INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)107INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)108INITIALIZE_PASS_END(FixIrreducible, "fix-irreducible",109"Convert irreducible control-flow into natural loops",110false /* Only looks at CFG */, false /* Analysis Pass */)111112// When a new loop is created, existing children of the parent loop may now be113// fully inside the new loop. Reconnect these as children of the new loop.114static void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop,115SetVector<BasicBlock *> &Blocks,116SetVector<BasicBlock *> &Headers) {117auto &CandidateLoops = ParentLoop ? ParentLoop->getSubLoopsVector()118: LI.getTopLevelLoopsVector();119// The new loop cannot be its own child, and any candidate is a120// child iff its header is owned by the new loop. Move all the121// children to a new vector.122auto FirstChild = std::partition(123CandidateLoops.begin(), CandidateLoops.end(), [&](Loop *L) {124return L == NewLoop || !Blocks.contains(L->getHeader());125});126SmallVector<Loop *, 8> ChildLoops(FirstChild, CandidateLoops.end());127CandidateLoops.erase(FirstChild, CandidateLoops.end());128129for (Loop *Child : ChildLoops) {130LLVM_DEBUG(dbgs() << "child loop: " << Child->getHeader()->getName()131<< "\n");132// TODO: A child loop whose header is also a header in the current133// SCC gets destroyed since its backedges are removed. That may134// not be necessary if we can retain such backedges.135if (Headers.count(Child->getHeader())) {136for (auto *BB : Child->blocks()) {137if (LI.getLoopFor(BB) != Child)138continue;139LI.changeLoopFor(BB, NewLoop);140LLVM_DEBUG(dbgs() << "moved block from child: " << BB->getName()141<< "\n");142}143std::vector<Loop *> GrandChildLoops;144std::swap(GrandChildLoops, Child->getSubLoopsVector());145for (auto *GrandChildLoop : GrandChildLoops) {146GrandChildLoop->setParentLoop(nullptr);147NewLoop->addChildLoop(GrandChildLoop);148}149LI.destroy(Child);150LLVM_DEBUG(dbgs() << "subsumed child loop (common header)\n");151continue;152}153154Child->setParentLoop(nullptr);155NewLoop->addChildLoop(Child);156LLVM_DEBUG(dbgs() << "added child loop to new loop\n");157}158}159160// Given a set of blocks and headers in an irreducible SCC, convert it into a161// natural loop. Also insert this new loop at its appropriate place in the162// hierarchy of loops.163static void createNaturalLoopInternal(LoopInfo &LI, DominatorTree &DT,164Loop *ParentLoop,165SetVector<BasicBlock *> &Blocks,166SetVector<BasicBlock *> &Headers) {167#ifndef NDEBUG168// All headers are part of the SCC169for (auto *H : Headers) {170assert(Blocks.count(H));171}172#endif173174SetVector<BasicBlock *> Predecessors;175for (auto *H : Headers) {176for (auto *P : predecessors(H)) {177Predecessors.insert(P);178}179}180181LLVM_DEBUG(182dbgs() << "Found predecessors:";183for (auto P : Predecessors) {184dbgs() << " " << P->getName();185}186dbgs() << "\n");187188// Redirect all the backedges through a "hub" consisting of a series189// of guard blocks that manage the flow of control from the190// predecessors to the headers.191SmallVector<BasicBlock *, 8> GuardBlocks;192DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);193CreateControlFlowHub(&DTU, GuardBlocks, Predecessors, Headers, "irr");194#if defined(EXPENSIVE_CHECKS)195assert(DT.verify(DominatorTree::VerificationLevel::Full));196#else197assert(DT.verify(DominatorTree::VerificationLevel::Fast));198#endif199200// Create a new loop from the now-transformed cycle201auto NewLoop = LI.AllocateLoop();202if (ParentLoop) {203ParentLoop->addChildLoop(NewLoop);204} else {205LI.addTopLevelLoop(NewLoop);206}207208// Add the guard blocks to the new loop. The first guard block is209// the head of all the backedges, and it is the first to be inserted210// in the loop. This ensures that it is recognized as the211// header. Since the new loop is already in LoopInfo, the new blocks212// are also propagated up the chain of parent loops.213for (auto *G : GuardBlocks) {214LLVM_DEBUG(dbgs() << "added guard block: " << G->getName() << "\n");215NewLoop->addBasicBlockToLoop(G, LI);216}217218// Add the SCC blocks to the new loop.219for (auto *BB : Blocks) {220NewLoop->addBlockEntry(BB);221if (LI.getLoopFor(BB) == ParentLoop) {222LLVM_DEBUG(dbgs() << "moved block from parent: " << BB->getName()223<< "\n");224LI.changeLoopFor(BB, NewLoop);225} else {226LLVM_DEBUG(dbgs() << "added block from child: " << BB->getName() << "\n");227}228}229LLVM_DEBUG(dbgs() << "header for new loop: "230<< NewLoop->getHeader()->getName() << "\n");231232reconnectChildLoops(LI, ParentLoop, NewLoop, Blocks, Headers);233234NewLoop->verifyLoop();235if (ParentLoop) {236ParentLoop->verifyLoop();237}238#if defined(EXPENSIVE_CHECKS)239LI.verify(DT);240#endif // EXPENSIVE_CHECKS241}242243namespace llvm {244// Enable the graph traits required for traversing a Loop body.245template <> struct GraphTraits<Loop> : LoopBodyTraits {};246} // namespace llvm247248// Overloaded wrappers to go with the function template below.249static BasicBlock *unwrapBlock(BasicBlock *B) { return B; }250static BasicBlock *unwrapBlock(LoopBodyTraits::NodeRef &N) { return N.second; }251252static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Function *F,253SetVector<BasicBlock *> &Blocks,254SetVector<BasicBlock *> &Headers) {255createNaturalLoopInternal(LI, DT, nullptr, Blocks, Headers);256}257258static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Loop &L,259SetVector<BasicBlock *> &Blocks,260SetVector<BasicBlock *> &Headers) {261createNaturalLoopInternal(LI, DT, &L, Blocks, Headers);262}263264// Convert irreducible SCCs; Graph G may be a Function* or a Loop&.265template <class Graph>266static bool makeReducible(LoopInfo &LI, DominatorTree &DT, Graph &&G) {267bool Changed = false;268for (auto Scc = scc_begin(G); !Scc.isAtEnd(); ++Scc) {269if (Scc->size() < 2)270continue;271SetVector<BasicBlock *> Blocks;272LLVM_DEBUG(dbgs() << "Found SCC:");273for (auto N : *Scc) {274auto BB = unwrapBlock(N);275LLVM_DEBUG(dbgs() << " " << BB->getName());276Blocks.insert(BB);277}278LLVM_DEBUG(dbgs() << "\n");279280// Minor optimization: The SCC blocks are usually discovered in an order281// that is the opposite of the order in which these blocks appear as branch282// targets. This results in a lot of condition inversions in the control283// flow out of the new ControlFlowHub, which can be mitigated if the orders284// match. So we discover the headers using the reverse of the block order.285SetVector<BasicBlock *> Headers;286LLVM_DEBUG(dbgs() << "Found headers:");287for (auto *BB : reverse(Blocks)) {288for (const auto P : predecessors(BB)) {289// Skip unreachable predecessors.290if (!DT.isReachableFromEntry(P))291continue;292if (!Blocks.count(P)) {293LLVM_DEBUG(dbgs() << " " << BB->getName());294Headers.insert(BB);295break;296}297}298}299LLVM_DEBUG(dbgs() << "\n");300301if (Headers.size() == 1) {302assert(LI.isLoopHeader(Headers.front()));303LLVM_DEBUG(dbgs() << "Natural loop with a single header: skipped\n");304continue;305}306createNaturalLoop(LI, DT, G, Blocks, Headers);307Changed = true;308}309return Changed;310}311312static bool FixIrreducibleImpl(Function &F, LoopInfo &LI, DominatorTree &DT) {313LLVM_DEBUG(dbgs() << "===== Fix irreducible control-flow in function: "314<< F.getName() << "\n");315316assert(hasOnlySimpleTerminator(F) && "Unsupported block terminator.");317318bool Changed = false;319SmallVector<Loop *, 8> WorkList;320321LLVM_DEBUG(dbgs() << "visiting top-level\n");322Changed |= makeReducible(LI, DT, &F);323324// Any SCCs reduced are now already in the list of top-level loops, so simply325// add them all to the worklist.326append_range(WorkList, LI);327328while (!WorkList.empty()) {329auto L = WorkList.pop_back_val();330LLVM_DEBUG(dbgs() << "visiting loop with header "331<< L->getHeader()->getName() << "\n");332Changed |= makeReducible(LI, DT, *L);333// Any SCCs reduced are now already in the list of child loops, so simply334// add them all to the worklist.335WorkList.append(L->begin(), L->end());336}337338return Changed;339}340341bool FixIrreducible::runOnFunction(Function &F) {342auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();343auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();344return FixIrreducibleImpl(F, LI, DT);345}346347PreservedAnalyses FixIrreduciblePass::run(Function &F,348FunctionAnalysisManager &AM) {349auto &LI = AM.getResult<LoopAnalysis>(F);350auto &DT = AM.getResult<DominatorTreeAnalysis>(F);351if (!FixIrreducibleImpl(F, LI, DT))352return PreservedAnalyses::all();353PreservedAnalyses PA;354PA.preserve<LoopAnalysis>();355PA.preserve<DominatorTreeAnalysis>();356return PA;357}358359360