1 //===- LoopDeletion.cpp - Dead Loop Deletion Pass ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Dead Loop Deletion Pass. This pass is responsible 10 // for eliminating loops with non-infinite computable trip counts that have no 11 // side effects or volatile instructions, and do not contribute to the 12 // computation of the function's return value. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Transforms/Scalar/LoopDeletion.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/Analysis/GlobalsModRef.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/IR/Dominators.h" 25 #include "llvm/IR/PatternMatch.h" 26 #include "llvm/InitializePasses.h" 27 #include "llvm/Transforms/Scalar.h" 28 #include "llvm/Transforms/Scalar/LoopPassManager.h" 29 #include "llvm/Transforms/Utils/LoopUtils.h" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "loop-delete" 34 35 STATISTIC(NumDeleted, "Number of loops deleted"); 36 37 enum class LoopDeletionResult { 38 Unmodified, 39 Modified, 40 Deleted, 41 }; 42 43 static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B) { 44 if (A == LoopDeletionResult::Deleted || B == LoopDeletionResult::Deleted) 45 return LoopDeletionResult::Deleted; 46 if (A == LoopDeletionResult::Modified || B == LoopDeletionResult::Modified) 47 return LoopDeletionResult::Modified; 48 return LoopDeletionResult::Unmodified; 49 } 50 51 /// Determines if a loop is dead. 52 /// 53 /// This assumes that we've already checked for unique exit and exiting blocks, 54 /// and that the code is in LCSSA form. 55 static bool isLoopDead(Loop *L, ScalarEvolution &SE, 56 SmallVectorImpl<BasicBlock *> &ExitingBlocks, 57 BasicBlock *ExitBlock, bool &Changed, 58 BasicBlock *Preheader) { 59 // Make sure that all PHI entries coming from the loop are loop invariant. 60 // Because the code is in LCSSA form, any values used outside of the loop 61 // must pass through a PHI in the exit block, meaning that this check is 62 // sufficient to guarantee that no loop-variant values are used outside 63 // of the loop. 64 bool AllEntriesInvariant = true; 65 bool AllOutgoingValuesSame = true; 66 if (!L->hasNoExitBlocks()) { 67 for (PHINode &P : ExitBlock->phis()) { 68 Value *incoming = P.getIncomingValueForBlock(ExitingBlocks[0]); 69 70 // Make sure all exiting blocks produce the same incoming value for the 71 // block. If there are different incoming values for different exiting 72 // blocks, then it is impossible to statically determine which value 73 // should be used. 74 AllOutgoingValuesSame = 75 all_of(makeArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) { 76 return incoming == P.getIncomingValueForBlock(BB); 77 }); 78 79 if (!AllOutgoingValuesSame) 80 break; 81 82 if (Instruction *I = dyn_cast<Instruction>(incoming)) 83 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) { 84 AllEntriesInvariant = false; 85 break; 86 } 87 } 88 } 89 90 if (Changed) 91 SE.forgetLoopDispositions(L); 92 93 if (!AllEntriesInvariant || !AllOutgoingValuesSame) 94 return false; 95 96 // Make sure that no instructions in the block have potential side-effects. 97 // This includes instructions that could write to memory, and loads that are 98 // marked volatile. 99 for (auto &I : L->blocks()) 100 if (any_of(*I, [](Instruction &I) { 101 return I.mayHaveSideEffects() && !I.isDroppable(); 102 })) 103 return false; 104 return true; 105 } 106 107 /// This function returns true if there is no viable path from the 108 /// entry block to the header of \p L. Right now, it only does 109 /// a local search to save compile time. 110 static bool isLoopNeverExecuted(Loop *L) { 111 using namespace PatternMatch; 112 113 auto *Preheader = L->getLoopPreheader(); 114 // TODO: We can relax this constraint, since we just need a loop 115 // predecessor. 116 assert(Preheader && "Needs preheader!"); 117 118 if (Preheader->isEntryBlock()) 119 return false; 120 // All predecessors of the preheader should have a constant conditional 121 // branch, with the loop's preheader as not-taken. 122 for (auto *Pred: predecessors(Preheader)) { 123 BasicBlock *Taken, *NotTaken; 124 ConstantInt *Cond; 125 if (!match(Pred->getTerminator(), 126 m_Br(m_ConstantInt(Cond), Taken, NotTaken))) 127 return false; 128 if (!Cond->getZExtValue()) 129 std::swap(Taken, NotTaken); 130 if (Taken == Preheader) 131 return false; 132 } 133 assert(!pred_empty(Preheader) && 134 "Preheader should have predecessors at this point!"); 135 // All the predecessors have the loop preheader as not-taken target. 136 return true; 137 } 138 139 static const SCEV * 140 getSCEVOnFirstIteration(Value *V, Loop *L, ScalarEvolution &SE, 141 DenseMap<Value *, const SCEV *> &FirstIterSCEV) { 142 // Fist, check in cache. 143 auto Existing = FirstIterSCEV.find(V); 144 if (Existing != FirstIterSCEV.end()) 145 return Existing->second; 146 const SCEV *S = nullptr; 147 // TODO: Once ScalarEvolution supports getValueOnNthIteration for anything 148 // else but AddRecs, it's a good use case for it. So far, just consider some 149 // simple cases, like arithmetic operations. 150 Value *LHS, *RHS; 151 using namespace PatternMatch; 152 if (match(V, m_Add(m_Value(LHS), m_Value(RHS)))) { 153 const SCEV *LHSS = getSCEVOnFirstIteration(LHS, L, SE, FirstIterSCEV); 154 const SCEV *RHSS = getSCEVOnFirstIteration(RHS, L, SE, FirstIterSCEV); 155 S = SE.getAddExpr(LHSS, RHSS); 156 } else if (match(V, m_Sub(m_Value(LHS), m_Value(RHS)))) { 157 const SCEV *LHSS = getSCEVOnFirstIteration(LHS, L, SE, FirstIterSCEV); 158 const SCEV *RHSS = getSCEVOnFirstIteration(RHS, L, SE, FirstIterSCEV); 159 S = SE.getMinusSCEV(LHSS, RHSS); 160 } else if (match(V, m_Mul(m_Value(LHS), m_Value(RHS)))) { 161 const SCEV *LHSS = getSCEVOnFirstIteration(LHS, L, SE, FirstIterSCEV); 162 const SCEV *RHSS = getSCEVOnFirstIteration(RHS, L, SE, FirstIterSCEV); 163 S = SE.getMulExpr(LHSS, RHSS); 164 } else 165 S = SE.getSCEV(V); 166 assert(S && "Case not handled?"); 167 FirstIterSCEV[V] = S; 168 return S; 169 } 170 171 // Try to prove that one of conditions that dominates the latch must exit on 1st 172 // iteration. 173 static bool canProveExitOnFirstIteration(Loop *L, DominatorTree &DT, 174 ScalarEvolution &SE, LoopInfo &LI) { 175 BasicBlock *Latch = L->getLoopLatch(); 176 177 if (!Latch) 178 return false; 179 180 LoopBlocksRPO RPOT(L); 181 RPOT.perform(&LI); 182 183 BasicBlock *Header = L->getHeader(); 184 // Blocks that are reachable on the 1st iteration. 185 SmallPtrSet<BasicBlock *, 4> LiveBlocks; 186 // Edges that are reachable on the 1st iteration. 187 DenseSet<BasicBlockEdge> LiveEdges; 188 LiveBlocks.insert(L->getHeader()); 189 190 auto MarkLiveEdge = [&](BasicBlock *From, BasicBlock *To) { 191 assert(LiveBlocks.count(From) && "Must be live!"); 192 LiveBlocks.insert(To); 193 LiveEdges.insert({ From, To }); 194 }; 195 196 auto MarkAllSuccessorsLive = [&](BasicBlock *BB) { 197 for (auto *Succ : successors(BB)) 198 MarkLiveEdge(BB, Succ); 199 }; 200 201 // Check if there is only one predecessor on 1st iteration. Note that because 202 // we iterate in RPOT, we have already visited all its (non-latch) 203 // predecessors. 204 auto GetSolePredecessorOnFirstIteration = [&](BasicBlock * BB)->BasicBlock * { 205 if (BB == Header) 206 return L->getLoopPredecessor(); 207 BasicBlock *OnlyPred = nullptr; 208 for (auto *Pred : predecessors(BB)) 209 if (OnlyPred != Pred && LiveEdges.count({ Pred, BB })) { 210 // 2 live preds. 211 if (OnlyPred) 212 return nullptr; 213 OnlyPred = Pred; 214 } 215 216 assert(OnlyPred && "No live predecessors?"); 217 return OnlyPred; 218 }; 219 DenseMap<Value *, const SCEV *> FirstIterSCEV; 220 221 // Use the following algorithm to prove we never take the latch on the 1st 222 // iteration: 223 // 1. Traverse in topological order, so that whenever we visit a block, all 224 // its predecessors are already visited. 225 // 2. If we can prove that the block may have only 1 predecessor on the 1st 226 // iteration, map all its phis onto input from this predecessor. 227 // 3a. If we can prove which successor of out block is taken on the 1st 228 // iteration, mark this successor live. 229 // 3b. If we cannot prove it, conservatively assume that all successors are 230 // live. 231 for (auto *BB : RPOT) { 232 // This block is not reachable on the 1st iterations. 233 if (!LiveBlocks.count(BB)) 234 continue; 235 236 // Skip inner loops. 237 if (LI.getLoopFor(BB) != L) { 238 MarkAllSuccessorsLive(BB); 239 continue; 240 } 241 242 // If this block has only one live pred, map its phis onto their SCEVs. 243 if (auto *OnlyPred = GetSolePredecessorOnFirstIteration(BB)) 244 for (auto &PN : BB->phis()) { 245 if (!SE.isSCEVable(PN.getType())) 246 continue; 247 auto *Incoming = PN.getIncomingValueForBlock(OnlyPred); 248 if (DT.dominates(Incoming, BB->getTerminator())) { 249 const SCEV *IncSCEV = 250 getSCEVOnFirstIteration(Incoming, L, SE, FirstIterSCEV); 251 FirstIterSCEV[&PN] = IncSCEV; 252 } 253 } 254 255 using namespace PatternMatch; 256 ICmpInst::Predicate Pred; 257 Value *LHS, *RHS; 258 const BasicBlock *IfTrue, *IfFalse; 259 // TODO: Handle switches. 260 if (!match(BB->getTerminator(), 261 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), 262 m_BasicBlock(IfTrue), m_BasicBlock(IfFalse)))) { 263 MarkAllSuccessorsLive(BB); 264 continue; 265 } 266 267 if (!SE.isSCEVable(LHS->getType())) { 268 MarkAllSuccessorsLive(BB); 269 continue; 270 } 271 272 // Can we prove constant true or false for this condition? 273 const SCEV *LHSS = getSCEVOnFirstIteration(LHS, L, SE, FirstIterSCEV); 274 const SCEV *RHSS = getSCEVOnFirstIteration(RHS, L, SE, FirstIterSCEV); 275 if (SE.isKnownPredicateAt(Pred, LHSS, RHSS, BB->getTerminator())) 276 MarkLiveEdge(BB, BB->getTerminator()->getSuccessor(0)); 277 else if (SE.isKnownPredicateAt(ICmpInst::getInversePredicate(Pred), LHSS, 278 RHSS, BB->getTerminator())) 279 MarkLiveEdge(BB, BB->getTerminator()->getSuccessor(1)); 280 else 281 MarkAllSuccessorsLive(BB); 282 } 283 284 // We can break the latch if it wasn't live. 285 return !LiveEdges.count({ Latch, Header }); 286 } 287 288 /// If we can prove the backedge is untaken, remove it. This destroys the 289 /// loop, but leaves the (now trivially loop invariant) control flow and 290 /// side effects (if any) in place. 291 static LoopDeletionResult 292 breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE, 293 LoopInfo &LI, MemorySSA *MSSA, 294 OptimizationRemarkEmitter &ORE) { 295 assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); 296 297 if (!L->getLoopLatch()) 298 return LoopDeletionResult::Unmodified; 299 300 auto *BTC = SE.getBackedgeTakenCount(L); 301 if (!BTC->isZero() && !canProveExitOnFirstIteration(L, DT, SE, LI)) 302 return LoopDeletionResult::Unmodified; 303 304 breakLoopBackedge(L, DT, SE, LI, MSSA); 305 return LoopDeletionResult::Deleted; 306 } 307 308 /// Remove a loop if it is dead. 309 /// 310 /// A loop is considered dead either if it does not impact the observable 311 /// behavior of the program other than finite running time, or if it is 312 /// required to make progress by an attribute such as 'mustprogress' or 313 /// 'llvm.loop.mustprogress' and does not make any. This may remove 314 /// infinite loops that have been required to make progress. 315 /// 316 /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in 317 /// order to make various safety checks work. 318 /// 319 /// \returns true if any changes were made. This may mutate the loop even if it 320 /// is unable to delete it due to hoisting trivially loop invariant 321 /// instructions out of the loop. 322 static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT, 323 ScalarEvolution &SE, LoopInfo &LI, 324 MemorySSA *MSSA, 325 OptimizationRemarkEmitter &ORE) { 326 assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); 327 328 // We can only remove the loop if there is a preheader that we can branch from 329 // after removing it. Also, if LoopSimplify form is not available, stay out 330 // of trouble. 331 BasicBlock *Preheader = L->getLoopPreheader(); 332 if (!Preheader || !L->hasDedicatedExits()) { 333 LLVM_DEBUG( 334 dbgs() 335 << "Deletion requires Loop with preheader and dedicated exits.\n"); 336 return LoopDeletionResult::Unmodified; 337 } 338 339 BasicBlock *ExitBlock = L->getUniqueExitBlock(); 340 341 if (ExitBlock && isLoopNeverExecuted(L)) { 342 LLVM_DEBUG(dbgs() << "Loop is proven to never execute, delete it!"); 343 // We need to forget the loop before setting the incoming values of the exit 344 // phis to undef, so we properly invalidate the SCEV expressions for those 345 // phis. 346 SE.forgetLoop(L); 347 // Set incoming value to undef for phi nodes in the exit block. 348 for (PHINode &P : ExitBlock->phis()) { 349 std::fill(P.incoming_values().begin(), P.incoming_values().end(), 350 UndefValue::get(P.getType())); 351 } 352 ORE.emit([&]() { 353 return OptimizationRemark(DEBUG_TYPE, "NeverExecutes", L->getStartLoc(), 354 L->getHeader()) 355 << "Loop deleted because it never executes"; 356 }); 357 deleteDeadLoop(L, &DT, &SE, &LI, MSSA); 358 ++NumDeleted; 359 return LoopDeletionResult::Deleted; 360 } 361 362 // The remaining checks below are for a loop being dead because all statements 363 // in the loop are invariant. 364 SmallVector<BasicBlock *, 4> ExitingBlocks; 365 L->getExitingBlocks(ExitingBlocks); 366 367 // We require that the loop has at most one exit block. Otherwise, we'd be in 368 // the situation of needing to be able to solve statically which exit block 369 // will be branched to, or trying to preserve the branching logic in a loop 370 // invariant manner. 371 if (!ExitBlock && !L->hasNoExitBlocks()) { 372 LLVM_DEBUG(dbgs() << "Deletion requires at most one exit block.\n"); 373 return LoopDeletionResult::Unmodified; 374 } 375 // Finally, we have to check that the loop really is dead. 376 bool Changed = false; 377 if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) { 378 LLVM_DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n"); 379 return Changed ? LoopDeletionResult::Modified 380 : LoopDeletionResult::Unmodified; 381 } 382 383 // Don't remove loops for which we can't solve the trip count unless the loop 384 // was required to make progress but has been determined to be dead. 385 const SCEV *S = SE.getConstantMaxBackedgeTakenCount(L); 386 if (isa<SCEVCouldNotCompute>(S) && 387 !L->getHeader()->getParent()->mustProgress() && !hasMustProgress(L)) { 388 LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was " 389 "not required to make progress.\n"); 390 return Changed ? LoopDeletionResult::Modified 391 : LoopDeletionResult::Unmodified; 392 } 393 394 LLVM_DEBUG(dbgs() << "Loop is invariant, delete it!"); 395 ORE.emit([&]() { 396 return OptimizationRemark(DEBUG_TYPE, "Invariant", L->getStartLoc(), 397 L->getHeader()) 398 << "Loop deleted because it is invariant"; 399 }); 400 deleteDeadLoop(L, &DT, &SE, &LI, MSSA); 401 ++NumDeleted; 402 403 return LoopDeletionResult::Deleted; 404 } 405 406 PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM, 407 LoopStandardAnalysisResults &AR, 408 LPMUpdater &Updater) { 409 410 LLVM_DEBUG(dbgs() << "Analyzing Loop for deletion: "); 411 LLVM_DEBUG(L.dump()); 412 std::string LoopName = std::string(L.getName()); 413 // For the new PM, we can't use OptimizationRemarkEmitter as an analysis 414 // pass. Function analyses need to be preserved across loop transformations 415 // but ORE cannot be preserved (see comment before the pass definition). 416 OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); 417 auto Result = deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI, AR.MSSA, ORE); 418 419 // If we can prove the backedge isn't taken, just break it and be done. This 420 // leaves the loop structure in place which means it can handle dispatching 421 // to the right exit based on whatever loop invariant structure remains. 422 if (Result != LoopDeletionResult::Deleted) 423 Result = merge(Result, breakBackedgeIfNotTaken(&L, AR.DT, AR.SE, AR.LI, 424 AR.MSSA, ORE)); 425 426 if (Result == LoopDeletionResult::Unmodified) 427 return PreservedAnalyses::all(); 428 429 if (Result == LoopDeletionResult::Deleted) 430 Updater.markLoopAsDeleted(L, LoopName); 431 432 auto PA = getLoopPassPreservedAnalyses(); 433 if (AR.MSSA) 434 PA.preserve<MemorySSAAnalysis>(); 435 return PA; 436 } 437 438 namespace { 439 class LoopDeletionLegacyPass : public LoopPass { 440 public: 441 static char ID; // Pass ID, replacement for typeid 442 LoopDeletionLegacyPass() : LoopPass(ID) { 443 initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry()); 444 } 445 446 // Possibly eliminate loop L if it is dead. 447 bool runOnLoop(Loop *L, LPPassManager &) override; 448 449 void getAnalysisUsage(AnalysisUsage &AU) const override { 450 AU.addPreserved<MemorySSAWrapperPass>(); 451 getLoopAnalysisUsage(AU); 452 } 453 }; 454 } 455 456 char LoopDeletionLegacyPass::ID = 0; 457 INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion", 458 "Delete dead loops", false, false) 459 INITIALIZE_PASS_DEPENDENCY(LoopPass) 460 INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion", 461 "Delete dead loops", false, false) 462 463 Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); } 464 465 bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) { 466 if (skipLoop(L)) 467 return false; 468 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 469 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 470 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 471 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>(); 472 MemorySSA *MSSA = nullptr; 473 if (MSSAAnalysis) 474 MSSA = &MSSAAnalysis->getMSSA(); 475 // For the old PM, we can't use OptimizationRemarkEmitter as an analysis 476 // pass. Function analyses need to be preserved across loop transformations 477 // but ORE cannot be preserved (see comment before the pass definition). 478 OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); 479 480 LLVM_DEBUG(dbgs() << "Analyzing Loop for deletion: "); 481 LLVM_DEBUG(L->dump()); 482 483 LoopDeletionResult Result = deleteLoopIfDead(L, DT, SE, LI, MSSA, ORE); 484 485 // If we can prove the backedge isn't taken, just break it and be done. This 486 // leaves the loop structure in place which means it can handle dispatching 487 // to the right exit based on whatever loop invariant structure remains. 488 if (Result != LoopDeletionResult::Deleted) 489 Result = merge(Result, breakBackedgeIfNotTaken(L, DT, SE, LI, MSSA, ORE)); 490 491 if (Result == LoopDeletionResult::Deleted) 492 LPM.markLoopAsDeleted(*L); 493 494 return Result != LoopDeletionResult::Unmodified; 495 } 496