Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopSimplify.cpp
35271 views
//===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===//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// This pass performs several transformations to transform natural loops into a9// simpler form, which makes subsequent analyses and transformations simpler and10// more effective.11//12// Loop pre-header insertion guarantees that there is a single, non-critical13// entry edge from outside of the loop to the loop header. This simplifies a14// number of analyses and transformations, such as LICM.15//16// Loop exit-block insertion guarantees that all exit blocks from the loop17// (blocks which are outside of the loop that have predecessors inside of the18// loop) only have predecessors from inside of the loop (and are thus dominated19// by the loop header). This simplifies transformations such as store-sinking20// that are built into LICM.21//22// This pass also guarantees that loops will have exactly one backedge.23//24// Indirectbr instructions introduce several complications. If the loop25// contains or is entered by an indirectbr instruction, it may not be possible26// to transform the loop and make these guarantees. Client code should check27// that these conditions are true before relying on them.28//29// Similar complications arise from callbr instructions, particularly in30// asm-goto where blockaddress expressions are used.31//32// Note that the simplifycfg pass will clean up blocks which are split out but33// end up being unnecessary, so usage of this pass should not pessimize34// generated code.35//36// This pass obviously modifies the CFG, but updates loop information and37// dominator information.38//39//===----------------------------------------------------------------------===//4041#include "llvm/Transforms/Utils/LoopSimplify.h"42#include "llvm/ADT/SetVector.h"43#include "llvm/ADT/SmallVector.h"44#include "llvm/ADT/Statistic.h"45#include "llvm/Analysis/AliasAnalysis.h"46#include "llvm/Analysis/AssumptionCache.h"47#include "llvm/Analysis/BasicAliasAnalysis.h"48#include "llvm/Analysis/BranchProbabilityInfo.h"49#include "llvm/Analysis/DependenceAnalysis.h"50#include "llvm/Analysis/GlobalsModRef.h"51#include "llvm/Analysis/InstructionSimplify.h"52#include "llvm/Analysis/LoopInfo.h"53#include "llvm/Analysis/MemorySSA.h"54#include "llvm/Analysis/MemorySSAUpdater.h"55#include "llvm/Analysis/ScalarEvolution.h"56#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"57#include "llvm/IR/CFG.h"58#include "llvm/IR/Constants.h"59#include "llvm/IR/Dominators.h"60#include "llvm/IR/Function.h"61#include "llvm/IR/Instructions.h"62#include "llvm/IR/LLVMContext.h"63#include "llvm/IR/Module.h"64#include "llvm/InitializePasses.h"65#include "llvm/Support/Debug.h"66#include "llvm/Support/raw_ostream.h"67#include "llvm/Transforms/Utils.h"68#include "llvm/Transforms/Utils/BasicBlockUtils.h"69#include "llvm/Transforms/Utils/Local.h"70#include "llvm/Transforms/Utils/LoopUtils.h"71using namespace llvm;7273#define DEBUG_TYPE "loop-simplify"7475STATISTIC(NumNested , "Number of nested loops split out");7677// If the block isn't already, move the new block to right after some 'outside78// block' block. This prevents the preheader from being placed inside the loop79// body, e.g. when the loop hasn't been rotated.80static void placeSplitBlockCarefully(BasicBlock *NewBB,81SmallVectorImpl<BasicBlock *> &SplitPreds,82Loop *L) {83// Check to see if NewBB is already well placed.84Function::iterator BBI = --NewBB->getIterator();85for (BasicBlock *Pred : SplitPreds) {86if (&*BBI == Pred)87return;88}8990// If it isn't already after an outside block, move it after one. This is91// always good as it makes the uncond branch from the outside block into a92// fall-through.9394// Figure out *which* outside block to put this after. Prefer an outside95// block that neighbors a BB actually in the loop.96BasicBlock *FoundBB = nullptr;97for (BasicBlock *Pred : SplitPreds) {98Function::iterator BBI = Pred->getIterator();99if (++BBI != NewBB->getParent()->end() && L->contains(&*BBI)) {100FoundBB = Pred;101break;102}103}104105// If our heuristic for a *good* bb to place this after doesn't find106// anything, just pick something. It's likely better than leaving it within107// the loop.108if (!FoundBB)109FoundBB = SplitPreds[0];110NewBB->moveAfter(FoundBB);111}112113/// InsertPreheaderForLoop - Once we discover that a loop doesn't have a114/// preheader, this method is called to insert one. This method has two phases:115/// preheader insertion and analysis updating.116///117BasicBlock *llvm::InsertPreheaderForLoop(Loop *L, DominatorTree *DT,118LoopInfo *LI, MemorySSAUpdater *MSSAU,119bool PreserveLCSSA) {120BasicBlock *Header = L->getHeader();121122// Compute the set of predecessors of the loop that are not in the loop.123SmallVector<BasicBlock*, 8> OutsideBlocks;124for (BasicBlock *P : predecessors(Header)) {125if (!L->contains(P)) { // Coming in from outside the loop?126// If the loop is branched to from an indirect terminator, we won't127// be able to fully transform the loop, because it prohibits128// edge splitting.129if (isa<IndirectBrInst>(P->getTerminator()))130return nullptr;131132// Keep track of it.133OutsideBlocks.push_back(P);134}135}136137// Split out the loop pre-header.138BasicBlock *PreheaderBB;139PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader", DT,140LI, MSSAU, PreserveLCSSA);141if (!PreheaderBB)142return nullptr;143144LLVM_DEBUG(dbgs() << "LoopSimplify: Creating pre-header "145<< PreheaderBB->getName() << "\n");146147// Make sure that NewBB is put someplace intelligent, which doesn't mess up148// code layout too horribly.149placeSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);150151return PreheaderBB;152}153154/// Add the specified block, and all of its predecessors, to the specified set,155/// if it's not already in there. Stop predecessor traversal when we reach156/// StopBlock.157static void addBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock,158SmallPtrSetImpl<BasicBlock *> &Blocks) {159SmallVector<BasicBlock *, 8> Worklist;160Worklist.push_back(InputBB);161do {162BasicBlock *BB = Worklist.pop_back_val();163if (Blocks.insert(BB).second && BB != StopBlock)164// If BB is not already processed and it is not a stop block then165// insert its predecessor in the work list166append_range(Worklist, predecessors(BB));167} while (!Worklist.empty());168}169170/// The first part of loop-nestification is to find a PHI node that tells171/// us how to partition the loops.172static PHINode *findPHIToPartitionLoops(Loop *L, DominatorTree *DT,173AssumptionCache *AC) {174const DataLayout &DL = L->getHeader()->getDataLayout();175for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {176PHINode *PN = cast<PHINode>(I);177++I;178if (Value *V = simplifyInstruction(PN, {DL, nullptr, DT, AC})) {179// This is a degenerate PHI already, don't modify it!180PN->replaceAllUsesWith(V);181PN->eraseFromParent();182continue;183}184185// Scan this PHI node looking for a use of the PHI node by itself.186for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)187if (PN->getIncomingValue(i) == PN &&188L->contains(PN->getIncomingBlock(i)))189// We found something tasty to remove.190return PN;191}192return nullptr;193}194195/// If this loop has multiple backedges, try to pull one of them out into196/// a nested loop.197///198/// This is important for code that looks like199/// this:200///201/// Loop:202/// ...203/// br cond, Loop, Next204/// ...205/// br cond2, Loop, Out206///207/// To identify this common case, we look at the PHI nodes in the header of the208/// loop. PHI nodes with unchanging values on one backedge correspond to values209/// that change in the "outer" loop, but not in the "inner" loop.210///211/// If we are able to separate out a loop, return the new outer loop that was212/// created.213///214static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,215DominatorTree *DT, LoopInfo *LI,216ScalarEvolution *SE, bool PreserveLCSSA,217AssumptionCache *AC, MemorySSAUpdater *MSSAU) {218// Don't try to separate loops without a preheader.219if (!Preheader)220return nullptr;221222// Treat the presence of convergent functions conservatively. The223// transformation is invalid if calls to certain convergent224// functions (like an AMDGPU barrier) get included in the resulting225// inner loop. But blocks meant for the inner loop will be226// identified later at a point where it's too late to abort the227// transformation. Also, the convergent attribute is not really228// sufficient to express the semantics of functions that are229// affected by this transformation. So we choose to back off if such230// a function call is present until a better alternative becomes231// available. This is similar to the conservative treatment of232// convergent function calls in GVNHoist and JumpThreading.233for (auto *BB : L->blocks()) {234for (auto &II : *BB) {235if (auto CI = dyn_cast<CallBase>(&II)) {236if (CI->isConvergent()) {237return nullptr;238}239}240}241}242243// The header is not a landing pad; preheader insertion should ensure this.244BasicBlock *Header = L->getHeader();245assert(!Header->isEHPad() && "Can't insert backedge to EH pad");246247PHINode *PN = findPHIToPartitionLoops(L, DT, AC);248if (!PN) return nullptr; // No known way to partition.249250// Pull out all predecessors that have varying values in the loop. This251// handles the case when a PHI node has multiple instances of itself as252// arguments.253SmallVector<BasicBlock*, 8> OuterLoopPreds;254for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {255if (PN->getIncomingValue(i) != PN ||256!L->contains(PN->getIncomingBlock(i))) {257// We can't split indirect control flow edges.258if (isa<IndirectBrInst>(PN->getIncomingBlock(i)->getTerminator()))259return nullptr;260OuterLoopPreds.push_back(PN->getIncomingBlock(i));261}262}263LLVM_DEBUG(dbgs() << "LoopSimplify: Splitting out a new outer loop\n");264265// If ScalarEvolution is around and knows anything about values in266// this loop, tell it to forget them, because we're about to267// substantially change it.268if (SE)269SE->forgetLoop(L);270271BasicBlock *NewBB = SplitBlockPredecessors(Header, OuterLoopPreds, ".outer",272DT, LI, MSSAU, PreserveLCSSA);273274// Make sure that NewBB is put someplace intelligent, which doesn't mess up275// code layout too horribly.276placeSplitBlockCarefully(NewBB, OuterLoopPreds, L);277278// Create the new outer loop.279Loop *NewOuter = LI->AllocateLoop();280281// Change the parent loop to use the outer loop as its child now.282if (Loop *Parent = L->getParentLoop())283Parent->replaceChildLoopWith(L, NewOuter);284else285LI->changeTopLevelLoop(L, NewOuter);286287// L is now a subloop of our outer loop.288NewOuter->addChildLoop(L);289290for (BasicBlock *BB : L->blocks())291NewOuter->addBlockEntry(BB);292293// Now reset the header in L, which had been moved by294// SplitBlockPredecessors for the outer loop.295L->moveToHeader(Header);296297// Determine which blocks should stay in L and which should be moved out to298// the Outer loop now.299SmallPtrSet<BasicBlock *, 4> BlocksInL;300for (BasicBlock *P : predecessors(Header)) {301if (DT->dominates(Header, P))302addBlockAndPredsToSet(P, Header, BlocksInL);303}304305// Scan all of the loop children of L, moving them to OuterLoop if they are306// not part of the inner loop.307const std::vector<Loop*> &SubLoops = L->getSubLoops();308for (size_t I = 0; I != SubLoops.size(); )309if (BlocksInL.count(SubLoops[I]->getHeader()))310++I; // Loop remains in L311else312NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));313314SmallVector<BasicBlock *, 8> OuterLoopBlocks;315OuterLoopBlocks.push_back(NewBB);316// Now that we know which blocks are in L and which need to be moved to317// OuterLoop, move any blocks that need it.318for (unsigned i = 0; i != L->getBlocks().size(); ++i) {319BasicBlock *BB = L->getBlocks()[i];320if (!BlocksInL.count(BB)) {321// Move this block to the parent, updating the exit blocks sets322L->removeBlockFromLoop(BB);323if ((*LI)[BB] == L) {324LI->changeLoopFor(BB, NewOuter);325OuterLoopBlocks.push_back(BB);326}327--i;328}329}330331// Split edges to exit blocks from the inner loop, if they emerged in the332// process of separating the outer one.333formDedicatedExitBlocks(L, DT, LI, MSSAU, PreserveLCSSA);334335if (PreserveLCSSA) {336// Fix LCSSA form for L. Some values, which previously were only used inside337// L, can now be used in NewOuter loop. We need to insert phi-nodes for them338// in corresponding exit blocks.339// We don't need to form LCSSA recursively, because there cannot be uses340// inside a newly created loop of defs from inner loops as those would341// already be a use of an LCSSA phi node.342formLCSSA(*L, *DT, LI, SE);343344assert(NewOuter->isRecursivelyLCSSAForm(*DT, *LI) &&345"LCSSA is broken after separating nested loops!");346}347348return NewOuter;349}350351/// This method is called when the specified loop has more than one352/// backedge in it.353///354/// If this occurs, revector all of these backedges to target a new basic block355/// and have that block branch to the loop header. This ensures that loops356/// have exactly one backedge.357static BasicBlock *insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader,358DominatorTree *DT, LoopInfo *LI,359MemorySSAUpdater *MSSAU) {360assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!");361362// Get information about the loop363BasicBlock *Header = L->getHeader();364Function *F = Header->getParent();365366// Unique backedge insertion currently depends on having a preheader.367if (!Preheader)368return nullptr;369370// The header is not an EH pad; preheader insertion should ensure this.371assert(!Header->isEHPad() && "Can't insert backedge to EH pad");372373// Figure out which basic blocks contain back-edges to the loop header.374std::vector<BasicBlock*> BackedgeBlocks;375for (BasicBlock *P : predecessors(Header)) {376// Indirect edges cannot be split, so we must fail if we find one.377if (isa<IndirectBrInst>(P->getTerminator()))378return nullptr;379380if (P != Preheader) BackedgeBlocks.push_back(P);381}382383// Create and insert the new backedge block...384BasicBlock *BEBlock = BasicBlock::Create(Header->getContext(),385Header->getName() + ".backedge", F);386BranchInst *BETerminator = BranchInst::Create(Header, BEBlock);387BETerminator->setDebugLoc(Header->getFirstNonPHI()->getDebugLoc());388389LLVM_DEBUG(dbgs() << "LoopSimplify: Inserting unique backedge block "390<< BEBlock->getName() << "\n");391392// Move the new backedge block to right after the last backedge block.393Function::iterator InsertPos = ++BackedgeBlocks.back()->getIterator();394F->splice(InsertPos, F, BEBlock->getIterator());395396// Now that the block has been inserted into the function, create PHI nodes in397// the backedge block which correspond to any PHI nodes in the header block.398for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {399PHINode *PN = cast<PHINode>(I);400PHINode *NewPN = PHINode::Create(PN->getType(), BackedgeBlocks.size(),401PN->getName()+".be", BETerminator->getIterator());402403// Loop over the PHI node, moving all entries except the one for the404// preheader over to the new PHI node.405unsigned PreheaderIdx = ~0U;406bool HasUniqueIncomingValue = true;407Value *UniqueValue = nullptr;408for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {409BasicBlock *IBB = PN->getIncomingBlock(i);410Value *IV = PN->getIncomingValue(i);411if (IBB == Preheader) {412PreheaderIdx = i;413} else {414NewPN->addIncoming(IV, IBB);415if (HasUniqueIncomingValue) {416if (!UniqueValue)417UniqueValue = IV;418else if (UniqueValue != IV)419HasUniqueIncomingValue = false;420}421}422}423424// Delete all of the incoming values from the old PN except the preheader's425assert(PreheaderIdx != ~0U && "PHI has no preheader entry??");426if (PreheaderIdx != 0) {427PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx));428PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx));429}430// Nuke all entries except the zero'th.431PN->removeIncomingValueIf([](unsigned Idx) { return Idx != 0; },432/* DeletePHIIfEmpty */ false);433434// Finally, add the newly constructed PHI node as the entry for the BEBlock.435PN->addIncoming(NewPN, BEBlock);436437// As an optimization, if all incoming values in the new PhiNode (which is a438// subset of the incoming values of the old PHI node) have the same value,439// eliminate the PHI Node.440if (HasUniqueIncomingValue) {441NewPN->replaceAllUsesWith(UniqueValue);442NewPN->eraseFromParent();443}444}445446// Now that all of the PHI nodes have been inserted and adjusted, modify the447// backedge blocks to jump to the BEBlock instead of the header.448// If one of the backedges has llvm.loop metadata attached, we remove449// it from the backedge and add it to BEBlock.450MDNode *LoopMD = nullptr;451for (BasicBlock *BB : BackedgeBlocks) {452Instruction *TI = BB->getTerminator();453if (!LoopMD)454LoopMD = TI->getMetadata(LLVMContext::MD_loop);455TI->setMetadata(LLVMContext::MD_loop, nullptr);456TI->replaceSuccessorWith(Header, BEBlock);457}458BEBlock->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);459460//===--- Update all analyses which we must preserve now -----------------===//461462// Update Loop Information - we know that this block is now in the current463// loop and all parent loops.464L->addBasicBlockToLoop(BEBlock, *LI);465466// Update dominator information467DT->splitBlock(BEBlock);468469if (MSSAU)470MSSAU->updatePhisWhenInsertingUniqueBackedgeBlock(Header, Preheader,471BEBlock);472473return BEBlock;474}475476/// Simplify one loop and queue further loops for simplification.477static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,478DominatorTree *DT, LoopInfo *LI,479ScalarEvolution *SE, AssumptionCache *AC,480MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {481bool Changed = false;482if (MSSAU && VerifyMemorySSA)483MSSAU->getMemorySSA()->verifyMemorySSA();484485ReprocessLoop:486487// Check to see that no blocks (other than the header) in this loop have488// predecessors that are not in the loop. This is not valid for natural489// loops, but can occur if the blocks are unreachable. Since they are490// unreachable we can just shamelessly delete those CFG edges!491for (BasicBlock *BB : L->blocks()) {492if (BB == L->getHeader())493continue;494495SmallPtrSet<BasicBlock*, 4> BadPreds;496for (BasicBlock *P : predecessors(BB))497if (!L->contains(P))498BadPreds.insert(P);499500// Delete each unique out-of-loop (and thus dead) predecessor.501for (BasicBlock *P : BadPreds) {502503LLVM_DEBUG(dbgs() << "LoopSimplify: Deleting edge from dead predecessor "504<< P->getName() << "\n");505506// Zap the dead pred's terminator and replace it with unreachable.507Instruction *TI = P->getTerminator();508changeToUnreachable(TI, PreserveLCSSA,509/*DTU=*/nullptr, MSSAU);510Changed = true;511}512}513514if (MSSAU && VerifyMemorySSA)515MSSAU->getMemorySSA()->verifyMemorySSA();516517// If there are exiting blocks with branches on undef, resolve the undef in518// the direction which will exit the loop. This will help simplify loop519// trip count computations.520SmallVector<BasicBlock*, 8> ExitingBlocks;521L->getExitingBlocks(ExitingBlocks);522for (BasicBlock *ExitingBlock : ExitingBlocks)523if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))524if (BI->isConditional()) {525if (UndefValue *Cond = dyn_cast<UndefValue>(BI->getCondition())) {526527LLVM_DEBUG(dbgs()528<< "LoopSimplify: Resolving \"br i1 undef\" to exit in "529<< ExitingBlock->getName() << "\n");530531BI->setCondition(ConstantInt::get(Cond->getType(),532!L->contains(BI->getSuccessor(0))));533534Changed = true;535}536}537538// Does the loop already have a preheader? If so, don't insert one.539BasicBlock *Preheader = L->getLoopPreheader();540if (!Preheader) {541Preheader = InsertPreheaderForLoop(L, DT, LI, MSSAU, PreserveLCSSA);542if (Preheader)543Changed = true;544}545546// Next, check to make sure that all exit nodes of the loop only have547// predecessors that are inside of the loop. This check guarantees that the548// loop preheader/header will dominate the exit blocks. If the exit block has549// predecessors from outside of the loop, split the edge now.550if (formDedicatedExitBlocks(L, DT, LI, MSSAU, PreserveLCSSA))551Changed = true;552553if (MSSAU && VerifyMemorySSA)554MSSAU->getMemorySSA()->verifyMemorySSA();555556// If the header has more than two predecessors at this point (from the557// preheader and from multiple backedges), we must adjust the loop.558BasicBlock *LoopLatch = L->getLoopLatch();559if (!LoopLatch) {560// If this is really a nested loop, rip it out into a child loop. Don't do561// this for loops with a giant number of backedges, just factor them into a562// common backedge instead.563if (L->getNumBackEdges() < 8) {564if (Loop *OuterL = separateNestedLoop(L, Preheader, DT, LI, SE,565PreserveLCSSA, AC, MSSAU)) {566++NumNested;567// Enqueue the outer loop as it should be processed next in our568// depth-first nest walk.569Worklist.push_back(OuterL);570571// This is a big restructuring change, reprocess the whole loop.572Changed = true;573// GCC doesn't tail recursion eliminate this.574// FIXME: It isn't clear we can't rely on LLVM to TRE this.575goto ReprocessLoop;576}577}578579// If we either couldn't, or didn't want to, identify nesting of the loops,580// insert a new block that all backedges target, then make it jump to the581// loop header.582LoopLatch = insertUniqueBackedgeBlock(L, Preheader, DT, LI, MSSAU);583if (LoopLatch)584Changed = true;585}586587if (MSSAU && VerifyMemorySSA)588MSSAU->getMemorySSA()->verifyMemorySSA();589590const DataLayout &DL = L->getHeader()->getDataLayout();591592// Scan over the PHI nodes in the loop header. Since they now have only two593// incoming values (the loop is canonicalized), we may have simplified the PHI594// down to 'X = phi [X, Y]', which should be replaced with 'Y'.595PHINode *PN;596for (BasicBlock::iterator I = L->getHeader()->begin();597(PN = dyn_cast<PHINode>(I++)); )598if (Value *V = simplifyInstruction(PN, {DL, nullptr, DT, AC})) {599if (SE) SE->forgetValue(PN);600if (!PreserveLCSSA || LI->replacementPreservesLCSSAForm(PN, V)) {601PN->replaceAllUsesWith(V);602PN->eraseFromParent();603Changed = true;604}605}606607// If this loop has multiple exits and the exits all go to the same608// block, attempt to merge the exits. This helps several passes, such609// as LoopRotation, which do not support loops with multiple exits.610// SimplifyCFG also does this (and this code uses the same utility611// function), however this code is loop-aware, where SimplifyCFG is612// not. That gives it the advantage of being able to hoist613// loop-invariant instructions out of the way to open up more614// opportunities, and the disadvantage of having the responsibility615// to preserve dominator information.616auto HasUniqueExitBlock = [&]() {617BasicBlock *UniqueExit = nullptr;618for (auto *ExitingBB : ExitingBlocks)619for (auto *SuccBB : successors(ExitingBB)) {620if (L->contains(SuccBB))621continue;622623if (!UniqueExit)624UniqueExit = SuccBB;625else if (UniqueExit != SuccBB)626return false;627}628629return true;630};631if (HasUniqueExitBlock()) {632for (BasicBlock *ExitingBlock : ExitingBlocks) {633if (!ExitingBlock->getSinglePredecessor()) continue;634BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());635if (!BI || !BI->isConditional()) continue;636CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());637if (!CI || CI->getParent() != ExitingBlock) continue;638639// Attempt to hoist out all instructions except for the640// comparison and the branch.641bool AllInvariant = true;642bool AnyInvariant = false;643for (auto I = ExitingBlock->instructionsWithoutDebug().begin(); &*I != BI; ) {644Instruction *Inst = &*I++;645if (Inst == CI)646continue;647if (!L->makeLoopInvariant(648Inst, AnyInvariant,649Preheader ? Preheader->getTerminator() : nullptr, MSSAU, SE)) {650AllInvariant = false;651break;652}653}654if (AnyInvariant)655Changed = true;656if (!AllInvariant) continue;657658// The block has now been cleared of all instructions except for659// a comparison and a conditional branch. SimplifyCFG may be able660// to fold it now.661if (!FoldBranchToCommonDest(BI, /*DTU=*/nullptr, MSSAU))662continue;663664// Success. The block is now dead, so remove it from the loop,665// update the dominator tree and delete it.666LLVM_DEBUG(dbgs() << "LoopSimplify: Eliminating exiting block "667<< ExitingBlock->getName() << "\n");668669assert(pred_empty(ExitingBlock));670Changed = true;671LI->removeBlock(ExitingBlock);672673DomTreeNode *Node = DT->getNode(ExitingBlock);674while (!Node->isLeaf()) {675DomTreeNode *Child = Node->back();676DT->changeImmediateDominator(Child, Node->getIDom());677}678DT->eraseNode(ExitingBlock);679if (MSSAU) {680SmallSetVector<BasicBlock *, 8> ExitBlockSet;681ExitBlockSet.insert(ExitingBlock);682MSSAU->removeBlocks(ExitBlockSet);683}684685BI->getSuccessor(0)->removePredecessor(686ExitingBlock, /* KeepOneInputPHIs */ PreserveLCSSA);687BI->getSuccessor(1)->removePredecessor(688ExitingBlock, /* KeepOneInputPHIs */ PreserveLCSSA);689ExitingBlock->eraseFromParent();690}691}692693if (MSSAU && VerifyMemorySSA)694MSSAU->getMemorySSA()->verifyMemorySSA();695696return Changed;697}698699bool llvm::simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,700ScalarEvolution *SE, AssumptionCache *AC,701MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {702bool Changed = false;703704#ifndef NDEBUG705// If we're asked to preserve LCSSA, the loop nest needs to start in LCSSA706// form.707if (PreserveLCSSA) {708assert(DT && "DT not available.");709assert(LI && "LI not available.");710assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&711"Requested to preserve LCSSA, but it's already broken.");712}713#endif714715// Worklist maintains our depth-first queue of loops in this nest to process.716SmallVector<Loop *, 4> Worklist;717Worklist.push_back(L);718719// Walk the worklist from front to back, pushing newly found sub loops onto720// the back. This will let us process loops from back to front in depth-first721// order. We can use this simple process because loops form a tree.722for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {723Loop *L2 = Worklist[Idx];724Worklist.append(L2->begin(), L2->end());725}726727while (!Worklist.empty())728Changed |= simplifyOneLoop(Worklist.pop_back_val(), Worklist, DT, LI, SE,729AC, MSSAU, PreserveLCSSA);730731// Changing exit conditions for blocks may affect exit counts of this loop and732// any of its parents, so we must invalidate the entire subtree if we've made733// any changes. Do this here rather than in simplifyOneLoop() as the top-most734// loop is going to be the same for all child loops.735if (Changed && SE)736SE->forgetTopmostLoop(L);737738return Changed;739}740741namespace {742struct LoopSimplify : public FunctionPass {743static char ID; // Pass identification, replacement for typeid744LoopSimplify() : FunctionPass(ID) {745initializeLoopSimplifyPass(*PassRegistry::getPassRegistry());746}747748bool runOnFunction(Function &F) override;749750void getAnalysisUsage(AnalysisUsage &AU) const override {751AU.addRequired<AssumptionCacheTracker>();752753// We need loop information to identify the loops...754AU.addRequired<DominatorTreeWrapperPass>();755AU.addPreserved<DominatorTreeWrapperPass>();756757AU.addRequired<LoopInfoWrapperPass>();758AU.addPreserved<LoopInfoWrapperPass>();759760AU.addPreserved<BasicAAWrapperPass>();761AU.addPreserved<AAResultsWrapperPass>();762AU.addPreserved<GlobalsAAWrapperPass>();763AU.addPreserved<ScalarEvolutionWrapperPass>();764AU.addPreserved<SCEVAAWrapperPass>();765AU.addPreservedID(LCSSAID);766AU.addPreserved<DependenceAnalysisWrapperPass>();767AU.addPreservedID(BreakCriticalEdgesID); // No critical edges added.768AU.addPreserved<BranchProbabilityInfoWrapperPass>();769AU.addPreserved<MemorySSAWrapperPass>();770}771772/// verifyAnalysis() - Verify LoopSimplifyForm's guarantees.773void verifyAnalysis() const override;774};775}776777char LoopSimplify::ID = 0;778INITIALIZE_PASS_BEGIN(LoopSimplify, "loop-simplify",779"Canonicalize natural loops", false, false)780INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)781INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)782INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)783INITIALIZE_PASS_END(LoopSimplify, "loop-simplify",784"Canonicalize natural loops", false, false)785786// Publicly exposed interface to pass...787char &llvm::LoopSimplifyID = LoopSimplify::ID;788Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }789790/// runOnFunction - Run down all loops in the CFG (recursively, but we could do791/// it in any convenient order) inserting preheaders...792///793bool LoopSimplify::runOnFunction(Function &F) {794bool Changed = false;795LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();796DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();797auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();798ScalarEvolution *SE = SEWP ? &SEWP->getSE() : nullptr;799AssumptionCache *AC =800&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);801MemorySSA *MSSA = nullptr;802std::unique_ptr<MemorySSAUpdater> MSSAU;803auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();804if (MSSAAnalysis) {805MSSA = &MSSAAnalysis->getMSSA();806MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);807}808809bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);810811// Simplify each loop nest in the function.812for (auto *L : *LI)813Changed |= simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), PreserveLCSSA);814815#ifndef NDEBUG816if (PreserveLCSSA) {817bool InLCSSA = all_of(818*LI, [&](Loop *L) { return L->isRecursivelyLCSSAForm(*DT, *LI); });819assert(InLCSSA && "LCSSA is broken after loop-simplify.");820}821#endif822return Changed;823}824825PreservedAnalyses LoopSimplifyPass::run(Function &F,826FunctionAnalysisManager &AM) {827bool Changed = false;828LoopInfo *LI = &AM.getResult<LoopAnalysis>(F);829DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);830ScalarEvolution *SE = AM.getCachedResult<ScalarEvolutionAnalysis>(F);831AssumptionCache *AC = &AM.getResult<AssumptionAnalysis>(F);832auto *MSSAAnalysis = AM.getCachedResult<MemorySSAAnalysis>(F);833std::unique_ptr<MemorySSAUpdater> MSSAU;834if (MSSAAnalysis) {835auto *MSSA = &MSSAAnalysis->getMSSA();836MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);837}838839840// Note that we don't preserve LCSSA in the new PM, if you need it run LCSSA841// after simplifying the loops. MemorySSA is preserved if it exists.842for (auto *L : *LI)843Changed |=844simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), /*PreserveLCSSA*/ false);845846if (!Changed)847return PreservedAnalyses::all();848849PreservedAnalyses PA;850PA.preserve<DominatorTreeAnalysis>();851PA.preserve<LoopAnalysis>();852PA.preserve<ScalarEvolutionAnalysis>();853PA.preserve<DependenceAnalysis>();854if (MSSAAnalysis)855PA.preserve<MemorySSAAnalysis>();856// BPI maps conditional terminators to probabilities, LoopSimplify can insert857// blocks, but it does so only by splitting existing blocks and edges. This858// results in the interesting property that all new terminators inserted are859// unconditional branches which do not appear in BPI. All deletions are860// handled via ValueHandle callbacks w/in BPI.861PA.preserve<BranchProbabilityAnalysis>();862return PA;863}864865// FIXME: Restore this code when we re-enable verification in verifyAnalysis866// below.867#if 0868static void verifyLoop(Loop *L) {869// Verify subloops.870for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)871verifyLoop(*I);872873// It used to be possible to just assert L->isLoopSimplifyForm(), however874// with the introduction of indirectbr, there are now cases where it's875// not possible to transform a loop as necessary. We can at least check876// that there is an indirectbr near any time there's trouble.877878// Indirectbr can interfere with preheader and unique backedge insertion.879if (!L->getLoopPreheader() || !L->getLoopLatch()) {880bool HasIndBrPred = false;881for (BasicBlock *Pred : predecessors(L->getHeader()))882if (isa<IndirectBrInst>(Pred->getTerminator())) {883HasIndBrPred = true;884break;885}886assert(HasIndBrPred &&887"LoopSimplify has no excuse for missing loop header info!");888(void)HasIndBrPred;889}890891// Indirectbr can interfere with exit block canonicalization.892if (!L->hasDedicatedExits()) {893bool HasIndBrExiting = false;894SmallVector<BasicBlock*, 8> ExitingBlocks;895L->getExitingBlocks(ExitingBlocks);896for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {897if (isa<IndirectBrInst>((ExitingBlocks[i])->getTerminator())) {898HasIndBrExiting = true;899break;900}901}902903assert(HasIndBrExiting &&904"LoopSimplify has no excuse for missing exit block info!");905(void)HasIndBrExiting;906}907}908#endif909910void LoopSimplify::verifyAnalysis() const {911// FIXME: This routine is being called mid-way through the loop pass manager912// as loop passes destroy this analysis. That's actually fine, but we have no913// way of expressing that here. Once all of the passes that destroy this are914// hoisted out of the loop pass manager we can add back verification here.915#if 0916for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)917verifyLoop(*I);918#endif919}920921922