Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
35269 views
//===- LoopDeletion.cpp - Dead Loop Deletion 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 file implements the Dead Loop Deletion Pass. This pass is responsible9// for eliminating loops with non-infinite computable trip counts that have no10// side effects or volatile instructions, and do not contribute to the11// computation of the function's return value.12//13//===----------------------------------------------------------------------===//1415#include "llvm/Transforms/Scalar/LoopDeletion.h"16#include "llvm/ADT/SmallVector.h"17#include "llvm/ADT/Statistic.h"18#include "llvm/Analysis/CFG.h"19#include "llvm/Analysis/InstructionSimplify.h"20#include "llvm/Analysis/LoopIterator.h"21#include "llvm/Analysis/LoopPass.h"22#include "llvm/Analysis/MemorySSA.h"23#include "llvm/Analysis/OptimizationRemarkEmitter.h"24#include "llvm/Analysis/ScalarEvolution.h"25#include "llvm/IR/Dominators.h"2627#include "llvm/IR/PatternMatch.h"28#include "llvm/Transforms/Scalar/LoopPassManager.h"29#include "llvm/Transforms/Utils/LoopUtils.h"3031using namespace llvm;3233#define DEBUG_TYPE "loop-delete"3435STATISTIC(NumDeleted, "Number of loops deleted");36STATISTIC(NumBackedgesBroken,37"Number of loops for which we managed to break the backedge");3839static cl::opt<bool> EnableSymbolicExecution(40"loop-deletion-enable-symbolic-execution", cl::Hidden, cl::init(true),41cl::desc("Break backedge through symbolic execution of 1st iteration "42"attempting to prove that the backedge is never taken"));4344enum class LoopDeletionResult {45Unmodified,46Modified,47Deleted,48};4950static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B) {51if (A == LoopDeletionResult::Deleted || B == LoopDeletionResult::Deleted)52return LoopDeletionResult::Deleted;53if (A == LoopDeletionResult::Modified || B == LoopDeletionResult::Modified)54return LoopDeletionResult::Modified;55return LoopDeletionResult::Unmodified;56}5758/// Determines if a loop is dead.59///60/// This assumes that we've already checked for unique exit and exiting blocks,61/// and that the code is in LCSSA form.62static bool isLoopDead(Loop *L, ScalarEvolution &SE,63SmallVectorImpl<BasicBlock *> &ExitingBlocks,64BasicBlock *ExitBlock, bool &Changed,65BasicBlock *Preheader, LoopInfo &LI) {66// Make sure that all PHI entries coming from the loop are loop invariant.67// Because the code is in LCSSA form, any values used outside of the loop68// must pass through a PHI in the exit block, meaning that this check is69// sufficient to guarantee that no loop-variant values are used outside70// of the loop.71bool AllEntriesInvariant = true;72bool AllOutgoingValuesSame = true;73if (ExitBlock) {74for (PHINode &P : ExitBlock->phis()) {75Value *incoming = P.getIncomingValueForBlock(ExitingBlocks[0]);7677// Make sure all exiting blocks produce the same incoming value for the78// block. If there are different incoming values for different exiting79// blocks, then it is impossible to statically determine which value80// should be used.81AllOutgoingValuesSame =82all_of(ArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) {83return incoming == P.getIncomingValueForBlock(BB);84});8586if (!AllOutgoingValuesSame)87break;8889if (Instruction *I = dyn_cast<Instruction>(incoming)) {90if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator(),91/*MSSAU=*/nullptr, &SE)) {92AllEntriesInvariant = false;93break;94}95}96}97}9899if (!AllEntriesInvariant || !AllOutgoingValuesSame)100return false;101102// Make sure that no instructions in the block have potential side-effects.103// This includes instructions that could write to memory, and loads that are104// marked volatile.105for (const auto &I : L->blocks())106if (any_of(*I, [](Instruction &I) {107return I.mayHaveSideEffects() && !I.isDroppable();108}))109return false;110111// The loop or any of its sub-loops looping infinitely is legal. The loop can112// only be considered dead if either113// a. the function is mustprogress.114// b. all (sub-)loops are mustprogress or have a known trip-count.115if (L->getHeader()->getParent()->mustProgress())116return true;117118LoopBlocksRPO RPOT(L);119RPOT.perform(&LI);120// If the loop contains an irreducible cycle, it may loop infinitely.121if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))122return false;123124SmallVector<Loop *, 8> WorkList;125WorkList.push_back(L);126while (!WorkList.empty()) {127Loop *Current = WorkList.pop_back_val();128if (hasMustProgress(Current))129continue;130131const SCEV *S = SE.getConstantMaxBackedgeTakenCount(Current);132if (isa<SCEVCouldNotCompute>(S)) {133LLVM_DEBUG(134dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was "135"not required to make progress.\n");136return false;137}138WorkList.append(Current->begin(), Current->end());139}140return true;141}142143/// This function returns true if there is no viable path from the144/// entry block to the header of \p L. Right now, it only does145/// a local search to save compile time.146static bool isLoopNeverExecuted(Loop *L) {147using namespace PatternMatch;148149auto *Preheader = L->getLoopPreheader();150// TODO: We can relax this constraint, since we just need a loop151// predecessor.152assert(Preheader && "Needs preheader!");153154if (Preheader->isEntryBlock())155return false;156// All predecessors of the preheader should have a constant conditional157// branch, with the loop's preheader as not-taken.158for (auto *Pred: predecessors(Preheader)) {159BasicBlock *Taken, *NotTaken;160ConstantInt *Cond;161if (!match(Pred->getTerminator(),162m_Br(m_ConstantInt(Cond), Taken, NotTaken)))163return false;164if (!Cond->getZExtValue())165std::swap(Taken, NotTaken);166if (Taken == Preheader)167return false;168}169assert(!pred_empty(Preheader) &&170"Preheader should have predecessors at this point!");171// All the predecessors have the loop preheader as not-taken target.172return true;173}174175static Value *176getValueOnFirstIteration(Value *V, DenseMap<Value *, Value *> &FirstIterValue,177const SimplifyQuery &SQ) {178// Quick hack: do not flood cache with non-instruction values.179if (!isa<Instruction>(V))180return V;181// Do we already know cached result?182auto Existing = FirstIterValue.find(V);183if (Existing != FirstIterValue.end())184return Existing->second;185Value *FirstIterV = nullptr;186if (auto *BO = dyn_cast<BinaryOperator>(V)) {187Value *LHS =188getValueOnFirstIteration(BO->getOperand(0), FirstIterValue, SQ);189Value *RHS =190getValueOnFirstIteration(BO->getOperand(1), FirstIterValue, SQ);191FirstIterV = simplifyBinOp(BO->getOpcode(), LHS, RHS, SQ);192} else if (auto *Cmp = dyn_cast<ICmpInst>(V)) {193Value *LHS =194getValueOnFirstIteration(Cmp->getOperand(0), FirstIterValue, SQ);195Value *RHS =196getValueOnFirstIteration(Cmp->getOperand(1), FirstIterValue, SQ);197FirstIterV = simplifyICmpInst(Cmp->getPredicate(), LHS, RHS, SQ);198} else if (auto *Select = dyn_cast<SelectInst>(V)) {199Value *Cond =200getValueOnFirstIteration(Select->getCondition(), FirstIterValue, SQ);201if (auto *C = dyn_cast<ConstantInt>(Cond)) {202auto *Selected = C->isAllOnesValue() ? Select->getTrueValue()203: Select->getFalseValue();204FirstIterV = getValueOnFirstIteration(Selected, FirstIterValue, SQ);205}206}207if (!FirstIterV)208FirstIterV = V;209FirstIterValue[V] = FirstIterV;210return FirstIterV;211}212213// Try to prove that one of conditions that dominates the latch must exit on 1st214// iteration.215static bool canProveExitOnFirstIteration(Loop *L, DominatorTree &DT,216LoopInfo &LI) {217// Disabled by option.218if (!EnableSymbolicExecution)219return false;220221BasicBlock *Predecessor = L->getLoopPredecessor();222BasicBlock *Latch = L->getLoopLatch();223224if (!Predecessor || !Latch)225return false;226227LoopBlocksRPO RPOT(L);228RPOT.perform(&LI);229230// For the optimization to be correct, we need RPOT to have a property that231// each block is processed after all its predecessors, which may only be232// violated for headers of the current loop and all nested loops. Irreducible233// CFG provides multiple ways to break this assumption, so we do not want to234// deal with it.235if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))236return false;237238BasicBlock *Header = L->getHeader();239// Blocks that are reachable on the 1st iteration.240SmallPtrSet<BasicBlock *, 4> LiveBlocks;241// Edges that are reachable on the 1st iteration.242DenseSet<BasicBlockEdge> LiveEdges;243LiveBlocks.insert(Header);244245SmallPtrSet<BasicBlock *, 4> Visited;246auto MarkLiveEdge = [&](BasicBlock *From, BasicBlock *To) {247assert(LiveBlocks.count(From) && "Must be live!");248assert((LI.isLoopHeader(To) || !Visited.count(To)) &&249"Only canonical backedges are allowed. Irreducible CFG?");250assert((LiveBlocks.count(To) || !Visited.count(To)) &&251"We already discarded this block as dead!");252LiveBlocks.insert(To);253LiveEdges.insert({ From, To });254};255256auto MarkAllSuccessorsLive = [&](BasicBlock *BB) {257for (auto *Succ : successors(BB))258MarkLiveEdge(BB, Succ);259};260261// Check if there is only one value coming from all live predecessor blocks.262// Note that because we iterate in RPOT, we have already visited all its263// (non-latch) predecessors.264auto GetSoleInputOnFirstIteration = [&](PHINode & PN)->Value * {265BasicBlock *BB = PN.getParent();266bool HasLivePreds = false;267(void)HasLivePreds;268if (BB == Header)269return PN.getIncomingValueForBlock(Predecessor);270Value *OnlyInput = nullptr;271for (auto *Pred : predecessors(BB))272if (LiveEdges.count({ Pred, BB })) {273HasLivePreds = true;274Value *Incoming = PN.getIncomingValueForBlock(Pred);275// Skip poison. If they are present, we can assume they are equal to276// the non-poison input.277if (isa<PoisonValue>(Incoming))278continue;279// Two inputs.280if (OnlyInput && OnlyInput != Incoming)281return nullptr;282OnlyInput = Incoming;283}284285assert(HasLivePreds && "No live predecessors?");286// If all incoming live value were poison, return poison.287return OnlyInput ? OnlyInput : PoisonValue::get(PN.getType());288};289DenseMap<Value *, Value *> FirstIterValue;290291// Use the following algorithm to prove we never take the latch on the 1st292// iteration:293// 1. Traverse in topological order, so that whenever we visit a block, all294// its predecessors are already visited.295// 2. If we can prove that the block may have only 1 predecessor on the 1st296// iteration, map all its phis onto input from this predecessor.297// 3a. If we can prove which successor of out block is taken on the 1st298// iteration, mark this successor live.299// 3b. If we cannot prove it, conservatively assume that all successors are300// live.301auto &DL = Header->getDataLayout();302const SimplifyQuery SQ(DL);303for (auto *BB : RPOT) {304Visited.insert(BB);305306// This block is not reachable on the 1st iterations.307if (!LiveBlocks.count(BB))308continue;309310// Skip inner loops.311if (LI.getLoopFor(BB) != L) {312MarkAllSuccessorsLive(BB);313continue;314}315316// If Phi has only one input from all live input blocks, use it.317for (auto &PN : BB->phis()) {318if (!PN.getType()->isIntegerTy())319continue;320auto *Incoming = GetSoleInputOnFirstIteration(PN);321if (Incoming && DT.dominates(Incoming, BB->getTerminator())) {322Value *FirstIterV =323getValueOnFirstIteration(Incoming, FirstIterValue, SQ);324FirstIterValue[&PN] = FirstIterV;325}326}327328using namespace PatternMatch;329Value *Cond;330BasicBlock *IfTrue, *IfFalse;331auto *Term = BB->getTerminator();332if (match(Term, m_Br(m_Value(Cond),333m_BasicBlock(IfTrue), m_BasicBlock(IfFalse)))) {334auto *ICmp = dyn_cast<ICmpInst>(Cond);335if (!ICmp || !ICmp->getType()->isIntegerTy()) {336MarkAllSuccessorsLive(BB);337continue;338}339340// Can we prove constant true or false for this condition?341auto *KnownCondition = getValueOnFirstIteration(ICmp, FirstIterValue, SQ);342if (KnownCondition == ICmp) {343// Failed to simplify.344MarkAllSuccessorsLive(BB);345continue;346}347if (isa<UndefValue>(KnownCondition)) {348// TODO: According to langref, branching by undef is undefined behavior.349// It means that, theoretically, we should be able to just continue350// without marking any successors as live. However, we are not certain351// how correct our compiler is at handling such cases. So we are being352// very conservative here.353//354// If there is a non-loop successor, always assume this branch leaves the355// loop. Otherwise, arbitrarily take IfTrue.356//357// Once we are certain that branching by undef is handled correctly by358// other transforms, we should not mark any successors live here.359if (L->contains(IfTrue) && L->contains(IfFalse))360MarkLiveEdge(BB, IfTrue);361continue;362}363auto *ConstCondition = dyn_cast<ConstantInt>(KnownCondition);364if (!ConstCondition) {365// Non-constant condition, cannot analyze any further.366MarkAllSuccessorsLive(BB);367continue;368}369if (ConstCondition->isAllOnesValue())370MarkLiveEdge(BB, IfTrue);371else372MarkLiveEdge(BB, IfFalse);373} else if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) {374auto *SwitchValue = SI->getCondition();375auto *SwitchValueOnFirstIter =376getValueOnFirstIteration(SwitchValue, FirstIterValue, SQ);377auto *ConstSwitchValue = dyn_cast<ConstantInt>(SwitchValueOnFirstIter);378if (!ConstSwitchValue) {379MarkAllSuccessorsLive(BB);380continue;381}382auto CaseIterator = SI->findCaseValue(ConstSwitchValue);383MarkLiveEdge(BB, CaseIterator->getCaseSuccessor());384} else {385MarkAllSuccessorsLive(BB);386continue;387}388}389390// We can break the latch if it wasn't live.391return !LiveEdges.count({ Latch, Header });392}393394/// If we can prove the backedge is untaken, remove it. This destroys the395/// loop, but leaves the (now trivially loop invariant) control flow and396/// side effects (if any) in place.397static LoopDeletionResult398breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE,399LoopInfo &LI, MemorySSA *MSSA,400OptimizationRemarkEmitter &ORE) {401assert(L->isLCSSAForm(DT) && "Expected LCSSA!");402403if (!L->getLoopLatch())404return LoopDeletionResult::Unmodified;405406auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);407if (!BTCMax->isZero()) {408auto *BTC = SE.getBackedgeTakenCount(L);409if (!BTC->isZero()) {410if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))411return LoopDeletionResult::Unmodified;412if (!canProveExitOnFirstIteration(L, DT, LI))413return LoopDeletionResult::Unmodified;414}415}416++NumBackedgesBroken;417breakLoopBackedge(L, DT, SE, LI, MSSA);418return LoopDeletionResult::Deleted;419}420421/// Remove a loop if it is dead.422///423/// A loop is considered dead either if it does not impact the observable424/// behavior of the program other than finite running time, or if it is425/// required to make progress by an attribute such as 'mustprogress' or426/// 'llvm.loop.mustprogress' and does not make any. This may remove427/// infinite loops that have been required to make progress.428///429/// This entire process relies pretty heavily on LoopSimplify form and LCSSA in430/// order to make various safety checks work.431///432/// \returns true if any changes were made. This may mutate the loop even if it433/// is unable to delete it due to hoisting trivially loop invariant434/// instructions out of the loop.435static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,436ScalarEvolution &SE, LoopInfo &LI,437MemorySSA *MSSA,438OptimizationRemarkEmitter &ORE) {439assert(L->isLCSSAForm(DT) && "Expected LCSSA!");440441// We can only remove the loop if there is a preheader that we can branch from442// after removing it. Also, if LoopSimplify form is not available, stay out443// of trouble.444BasicBlock *Preheader = L->getLoopPreheader();445if (!Preheader || !L->hasDedicatedExits()) {446LLVM_DEBUG(447dbgs()448<< "Deletion requires Loop with preheader and dedicated exits.\n");449return LoopDeletionResult::Unmodified;450}451452BasicBlock *ExitBlock = L->getUniqueExitBlock();453454// We can't directly branch to an EH pad. Don't bother handling this edge455// case.456if (ExitBlock && ExitBlock->isEHPad()) {457LLVM_DEBUG(dbgs() << "Cannot delete loop exiting to EH pad.\n");458return LoopDeletionResult::Unmodified;459}460461if (ExitBlock && isLoopNeverExecuted(L)) {462LLVM_DEBUG(dbgs() << "Loop is proven to never execute, delete it!\n");463// We need to forget the loop before setting the incoming values of the exit464// phis to poison, so we properly invalidate the SCEV expressions for those465// phis.466SE.forgetLoop(L);467// Set incoming value to poison for phi nodes in the exit block.468for (PHINode &P : ExitBlock->phis()) {469std::fill(P.incoming_values().begin(), P.incoming_values().end(),470PoisonValue::get(P.getType()));471}472ORE.emit([&]() {473return OptimizationRemark(DEBUG_TYPE, "NeverExecutes", L->getStartLoc(),474L->getHeader())475<< "Loop deleted because it never executes";476});477deleteDeadLoop(L, &DT, &SE, &LI, MSSA);478++NumDeleted;479return LoopDeletionResult::Deleted;480}481482// The remaining checks below are for a loop being dead because all statements483// in the loop are invariant.484SmallVector<BasicBlock *, 4> ExitingBlocks;485L->getExitingBlocks(ExitingBlocks);486487// We require that the loop has at most one exit block. Otherwise, we'd be in488// the situation of needing to be able to solve statically which exit block489// will be branched to, or trying to preserve the branching logic in a loop490// invariant manner.491if (!ExitBlock && !L->hasNoExitBlocks()) {492LLVM_DEBUG(dbgs() << "Deletion requires at most one exit block.\n");493return LoopDeletionResult::Unmodified;494}495496// Finally, we have to check that the loop really is dead.497bool Changed = false;498if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader, LI)) {499LLVM_DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n");500return Changed ? LoopDeletionResult::Modified501: LoopDeletionResult::Unmodified;502}503504LLVM_DEBUG(dbgs() << "Loop is invariant, delete it!\n");505ORE.emit([&]() {506return OptimizationRemark(DEBUG_TYPE, "Invariant", L->getStartLoc(),507L->getHeader())508<< "Loop deleted because it is invariant";509});510deleteDeadLoop(L, &DT, &SE, &LI, MSSA);511++NumDeleted;512513return LoopDeletionResult::Deleted;514}515516PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM,517LoopStandardAnalysisResults &AR,518LPMUpdater &Updater) {519520LLVM_DEBUG(dbgs() << "Analyzing Loop for deletion: ");521LLVM_DEBUG(L.dump());522std::string LoopName = std::string(L.getName());523// For the new PM, we can't use OptimizationRemarkEmitter as an analysis524// pass. Function analyses need to be preserved across loop transformations525// but ORE cannot be preserved (see comment before the pass definition).526OptimizationRemarkEmitter ORE(L.getHeader()->getParent());527auto Result = deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI, AR.MSSA, ORE);528529// If we can prove the backedge isn't taken, just break it and be done. This530// leaves the loop structure in place which means it can handle dispatching531// to the right exit based on whatever loop invariant structure remains.532if (Result != LoopDeletionResult::Deleted)533Result = merge(Result, breakBackedgeIfNotTaken(&L, AR.DT, AR.SE, AR.LI,534AR.MSSA, ORE));535536if (Result == LoopDeletionResult::Unmodified)537return PreservedAnalyses::all();538539if (Result == LoopDeletionResult::Deleted)540Updater.markLoopAsDeleted(L, LoopName);541542auto PA = getLoopPassPreservedAnalyses();543if (AR.MSSA)544PA.preserve<MemorySSAAnalysis>();545return PA;546}547548549