LoopDeletion.cpp 13 KB

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