//===- LoopDeletion.cpp - Dead Loop Deletion Pass ---------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the Dead Loop Deletion Pass. This pass is responsible // for eliminating loops with non-infinite computable trip counts that have no // side effects or volatile instructions, and do not contribute to the // computation of the function's return value. // //===----------------------------------------------------------------------===// #include "llvm/Transforms/Scalar/LoopDeletion.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/CFG.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/LoopIterator.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/MemorySSA.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/PatternMatch.h" #include "llvm/Transforms/Scalar/LoopPassManager.h" #include "llvm/Transforms/Utils/LoopUtils.h" usingnamespacellvm; #define DEBUG_TYPE … STATISTIC(NumDeleted, "Number of loops deleted"); STATISTIC(NumBackedgesBroken, "Number of loops for which we managed to break the backedge"); static cl::opt<bool> EnableSymbolicExecution( "loop-deletion-enable-symbolic-execution", cl::Hidden, cl::init(true), cl::desc("Break backedge through symbolic execution of 1st iteration " "attempting to prove that the backedge is never taken")); enum class LoopDeletionResult { … }; static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B) { … } /// Determines if a loop is dead. /// /// This assumes that we've already checked for unique exit and exiting blocks, /// and that the code is in LCSSA form. static bool isLoopDead(Loop *L, ScalarEvolution &SE, SmallVectorImpl<BasicBlock *> &ExitingBlocks, BasicBlock *ExitBlock, bool &Changed, BasicBlock *Preheader, LoopInfo &LI) { … } /// This function returns true if there is no viable path from the /// entry block to the header of \p L. Right now, it only does /// a local search to save compile time. static bool isLoopNeverExecuted(Loop *L) { … } static Value * getValueOnFirstIteration(Value *V, DenseMap<Value *, Value *> &FirstIterValue, const SimplifyQuery &SQ) { … } // Try to prove that one of conditions that dominates the latch must exit on 1st // iteration. static bool canProveExitOnFirstIteration(Loop *L, DominatorTree &DT, LoopInfo &LI) { … } /// If we can prove the backedge is untaken, remove it. This destroys the /// loop, but leaves the (now trivially loop invariant) control flow and /// side effects (if any) in place. static LoopDeletionResult breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA, OptimizationRemarkEmitter &ORE) { … } /// Remove a loop if it is dead. /// /// A loop is considered dead either if it does not impact the observable /// behavior of the program other than finite running time, or if it is /// required to make progress by an attribute such as 'mustprogress' or /// 'llvm.loop.mustprogress' and does not make any. This may remove /// infinite loops that have been required to make progress. /// /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in /// order to make various safety checks work. /// /// \returns true if any changes were made. This may mutate the loop even if it /// is unable to delete it due to hoisting trivially loop invariant /// instructions out of the loop. static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA, OptimizationRemarkEmitter &ORE) { … } PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &Updater) { … }