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/LoopPass.h" 21 #include "llvm/Analysis/MemorySSA.h" 22 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 23 #include "llvm/IR/Dominators.h" 24 #include "llvm/IR/PatternMatch.h" 25 #include "llvm/InitializePasses.h" 26 #include "llvm/Transforms/Scalar.h" 27 #include "llvm/Transforms/Scalar/LoopPassManager.h" 28 #include "llvm/Transforms/Utils/LoopUtils.h" 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "loop-delete" 33 34 STATISTIC(NumDeleted, "Number of loops deleted"); 35 36 enum class LoopDeletionResult { 37 Unmodified, 38 Modified, 39 Deleted, 40 }; 41 42 static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B) { 43 if (A == LoopDeletionResult::Deleted || B == LoopDeletionResult::Deleted) 44 return LoopDeletionResult::Deleted; 45 if (A == LoopDeletionResult::Modified || B == LoopDeletionResult::Modified) 46 return LoopDeletionResult::Modified; 47 return LoopDeletionResult::Unmodified; 48 } 49 50 /// Determines if a loop is dead. 51 /// 52 /// This assumes that we've already checked for unique exit and exiting blocks, 53 /// and that the code is in LCSSA form. 54 static bool isLoopDead(Loop *L, ScalarEvolution &SE, 55 SmallVectorImpl<BasicBlock *> &ExitingBlocks, 56 BasicBlock *ExitBlock, bool &Changed, 57 BasicBlock *Preheader) { 58 // Make sure that all PHI entries coming from the loop are loop invariant. 59 // Because the code is in LCSSA form, any values used outside of the loop 60 // must pass through a PHI in the exit block, meaning that this check is 61 // sufficient to guarantee that no loop-variant values are used outside 62 // of the loop. 63 bool AllEntriesInvariant = true; 64 bool AllOutgoingValuesSame = true; 65 if (!L->hasNoExitBlocks()) { 66 for (PHINode &P : ExitBlock->phis()) { 67 Value *incoming = P.getIncomingValueForBlock(ExitingBlocks[0]); 68 69 // Make sure all exiting blocks produce the same incoming value for the 70 // block. If there are different incoming values for different exiting 71 // blocks, then it is impossible to statically determine which value 72 // should be used. 73 AllOutgoingValuesSame = 74 all_of(makeArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) { 75 return incoming == P.getIncomingValueForBlock(BB); 76 }); 77 78 if (!AllOutgoingValuesSame) 79 break; 80 81 if (Instruction *I = dyn_cast<Instruction>(incoming)) 82 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) { 83 AllEntriesInvariant = false; 84 break; 85 } 86 } 87 } 88 89 if (Changed) 90 SE.forgetLoopDispositions(L); 91 92 if (!AllEntriesInvariant || !AllOutgoingValuesSame) 93 return false; 94 95 // Make sure that no instructions in the block have potential side-effects. 96 // This includes instructions that could write to memory, and loads that are 97 // marked volatile. 98 for (auto &I : L->blocks()) 99 if (any_of(*I, [](Instruction &I) { 100 return I.mayHaveSideEffects() && !I.isDroppable(); 101 })) 102 return false; 103 return true; 104 } 105 106 /// This function returns true if there is no viable path from the 107 /// entry block to the header of \p L. Right now, it only does 108 /// a local search to save compile time. 109 static bool isLoopNeverExecuted(Loop *L) { 110 using namespace PatternMatch; 111 112 auto *Preheader = L->getLoopPreheader(); 113 // TODO: We can relax this constraint, since we just need a loop 114 // predecessor. 115 assert(Preheader && "Needs preheader!"); 116 117 if (Preheader == &Preheader->getParent()->getEntryBlock()) 118 return false; 119 // All predecessors of the preheader should have a constant conditional 120 // branch, with the loop's preheader as not-taken. 121 for (auto *Pred: predecessors(Preheader)) { 122 BasicBlock *Taken, *NotTaken; 123 ConstantInt *Cond; 124 if (!match(Pred->getTerminator(), 125 m_Br(m_ConstantInt(Cond), Taken, NotTaken))) 126 return false; 127 if (!Cond->getZExtValue()) 128 std::swap(Taken, NotTaken); 129 if (Taken == Preheader) 130 return false; 131 } 132 assert(!pred_empty(Preheader) && 133 "Preheader should have predecessors at this point!"); 134 // All the predecessors have the loop preheader as not-taken target. 135 return true; 136 } 137 138 /// If we can prove the backedge is untaken, remove it. This destroys the 139 /// loop, but leaves the (now trivially loop invariant) control flow and 140 /// side effects (if any) in place. 141 static LoopDeletionResult 142 breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE, 143 LoopInfo &LI, MemorySSA *MSSA, 144 OptimizationRemarkEmitter &ORE) { 145 assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); 146 147 if (!L->getLoopLatch()) 148 return LoopDeletionResult::Unmodified; 149 150 auto *BTC = SE.getBackedgeTakenCount(L); 151 if (!BTC->isZero()) 152 return LoopDeletionResult::Unmodified; 153 154 // For non-outermost loops, the tricky case is that we can drop blocks 155 // out of both inner and outer loops at the same time. This results in 156 // new exiting block for the outer loop appearing, and possibly needing 157 // an lcssa phi inserted. (See loop_nest_lcssa test case in zero-btc.ll) 158 // TODO: We can handle a bunch of cases here without much work, revisit. 159 if (!L->isOutermost()) 160 return LoopDeletionResult::Unmodified; 161 162 breakLoopBackedge(L, DT, SE, LI, MSSA); 163 return LoopDeletionResult::Deleted; 164 } 165 166 /// Remove a loop if it is dead. 167 /// 168 /// A loop is considered dead either if it does not impact the observable 169 /// behavior of the program other than finite running time, or if it is 170 /// required to make progress by an attribute such as 'mustprogress' or 171 /// 'llvm.loop.mustprogress' and does not make any. This may remove 172 /// infinite loops that have been required to make progress. 173 /// 174 /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in 175 /// order to make various safety checks work. 176 /// 177 /// \returns true if any changes were made. This may mutate the loop even if it 178 /// is unable to delete it due to hoisting trivially loop invariant 179 /// instructions out of the loop. 180 static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT, 181 ScalarEvolution &SE, LoopInfo &LI, 182 MemorySSA *MSSA, 183 OptimizationRemarkEmitter &ORE) { 184 assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); 185 186 // We can only remove the loop if there is a preheader that we can branch from 187 // after removing it. Also, if LoopSimplify form is not available, stay out 188 // of trouble. 189 BasicBlock *Preheader = L->getLoopPreheader(); 190 if (!Preheader || !L->hasDedicatedExits()) { 191 LLVM_DEBUG( 192 dbgs() 193 << "Deletion requires Loop with preheader and dedicated exits.\n"); 194 return LoopDeletionResult::Unmodified; 195 } 196 197 BasicBlock *ExitBlock = L->getUniqueExitBlock(); 198 199 if (ExitBlock && isLoopNeverExecuted(L)) { 200 LLVM_DEBUG(dbgs() << "Loop is proven to never execute, delete it!"); 201 // We need to forget the loop before setting the incoming values of the exit 202 // phis to undef, so we properly invalidate the SCEV expressions for those 203 // phis. 204 SE.forgetLoop(L); 205 // Set incoming value to undef for phi nodes in the exit block. 206 for (PHINode &P : ExitBlock->phis()) { 207 std::fill(P.incoming_values().begin(), P.incoming_values().end(), 208 UndefValue::get(P.getType())); 209 } 210 ORE.emit([&]() { 211 return OptimizationRemark(DEBUG_TYPE, "NeverExecutes", L->getStartLoc(), 212 L->getHeader()) 213 << "Loop deleted because it never executes"; 214 }); 215 deleteDeadLoop(L, &DT, &SE, &LI, MSSA); 216 ++NumDeleted; 217 return LoopDeletionResult::Deleted; 218 } 219 220 // The remaining checks below are for a loop being dead because all statements 221 // in the loop are invariant. 222 SmallVector<BasicBlock *, 4> ExitingBlocks; 223 L->getExitingBlocks(ExitingBlocks); 224 225 // We require that the loop has at most one exit block. Otherwise, we'd be in 226 // the situation of needing to be able to solve statically which exit block 227 // will be branched to, or trying to preserve the branching logic in a loop 228 // invariant manner. 229 if (!ExitBlock && !L->hasNoExitBlocks()) { 230 LLVM_DEBUG(dbgs() << "Deletion requires at most one exit block.\n"); 231 return LoopDeletionResult::Unmodified; 232 } 233 // Finally, we have to check that the loop really is dead. 234 bool Changed = false; 235 if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) { 236 LLVM_DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n"); 237 return Changed ? LoopDeletionResult::Modified 238 : LoopDeletionResult::Unmodified; 239 } 240 241 // Don't remove loops for which we can't solve the trip count unless the loop 242 // was required to make progress but has been determined to be dead. 243 const SCEV *S = SE.getConstantMaxBackedgeTakenCount(L); 244 if (isa<SCEVCouldNotCompute>(S) && 245 !L->getHeader()->getParent()->mustProgress() && !hasMustProgress(L)) { 246 LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was " 247 "not required to make progress.\n"); 248 return Changed ? LoopDeletionResult::Modified 249 : LoopDeletionResult::Unmodified; 250 } 251 252 LLVM_DEBUG(dbgs() << "Loop is invariant, delete it!"); 253 ORE.emit([&]() { 254 return OptimizationRemark(DEBUG_TYPE, "Invariant", L->getStartLoc(), 255 L->getHeader()) 256 << "Loop deleted because it is invariant"; 257 }); 258 deleteDeadLoop(L, &DT, &SE, &LI, MSSA); 259 ++NumDeleted; 260 261 return LoopDeletionResult::Deleted; 262 } 263 264 PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM, 265 LoopStandardAnalysisResults &AR, 266 LPMUpdater &Updater) { 267 268 LLVM_DEBUG(dbgs() << "Analyzing Loop for deletion: "); 269 LLVM_DEBUG(L.dump()); 270 std::string LoopName = std::string(L.getName()); 271 // For the new PM, we can't use OptimizationRemarkEmitter as an analysis 272 // pass. Function analyses need to be preserved across loop transformations 273 // but ORE cannot be preserved (see comment before the pass definition). 274 OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); 275 auto Result = deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI, AR.MSSA, ORE); 276 277 // If we can prove the backedge isn't taken, just break it and be done. This 278 // leaves the loop structure in place which means it can handle dispatching 279 // to the right exit based on whatever loop invariant structure remains. 280 if (Result != LoopDeletionResult::Deleted) 281 Result = merge(Result, breakBackedgeIfNotTaken(&L, AR.DT, AR.SE, AR.LI, 282 AR.MSSA, ORE)); 283 284 if (Result == LoopDeletionResult::Unmodified) 285 return PreservedAnalyses::all(); 286 287 if (Result == LoopDeletionResult::Deleted) 288 Updater.markLoopAsDeleted(L, LoopName); 289 290 auto PA = getLoopPassPreservedAnalyses(); 291 if (AR.MSSA) 292 PA.preserve<MemorySSAAnalysis>(); 293 return PA; 294 } 295 296 namespace { 297 class LoopDeletionLegacyPass : public LoopPass { 298 public: 299 static char ID; // Pass ID, replacement for typeid 300 LoopDeletionLegacyPass() : LoopPass(ID) { 301 initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry()); 302 } 303 304 // Possibly eliminate loop L if it is dead. 305 bool runOnLoop(Loop *L, LPPassManager &) override; 306 307 void getAnalysisUsage(AnalysisUsage &AU) const override { 308 AU.addPreserved<MemorySSAWrapperPass>(); 309 getLoopAnalysisUsage(AU); 310 } 311 }; 312 } 313 314 char LoopDeletionLegacyPass::ID = 0; 315 INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion", 316 "Delete dead loops", false, false) 317 INITIALIZE_PASS_DEPENDENCY(LoopPass) 318 INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion", 319 "Delete dead loops", false, false) 320 321 Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); } 322 323 bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) { 324 if (skipLoop(L)) 325 return false; 326 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 327 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 328 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 329 auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>(); 330 MemorySSA *MSSA = nullptr; 331 if (MSSAAnalysis) 332 MSSA = &MSSAAnalysis->getMSSA(); 333 // For the old PM, we can't use OptimizationRemarkEmitter as an analysis 334 // pass. Function analyses need to be preserved across loop transformations 335 // but ORE cannot be preserved (see comment before the pass definition). 336 OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); 337 338 LLVM_DEBUG(dbgs() << "Analyzing Loop for deletion: "); 339 LLVM_DEBUG(L->dump()); 340 341 LoopDeletionResult Result = deleteLoopIfDead(L, DT, SE, LI, MSSA, ORE); 342 343 // If we can prove the backedge isn't taken, just break it and be done. This 344 // leaves the loop structure in place which means it can handle dispatching 345 // to the right exit based on whatever loop invariant structure remains. 346 if (Result != LoopDeletionResult::Deleted) 347 Result = merge(Result, breakBackedgeIfNotTaken(L, DT, SE, LI, MSSA, ORE)); 348 349 if (Result == LoopDeletionResult::Deleted) 350 LPM.markLoopAsDeleted(*L); 351 352 return Result != LoopDeletionResult::Unmodified; 353 } 354