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