Owen Anderson | 2306a1e | 2008-04-29 20:06:54 +0000 | [diff] [blame] | 1 | //===- LoopDeletion.cpp - Dead Loop Deletion Pass ---------------===// |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gordon Henriksen | 829046b | 2008-05-08 17:46:35 +0000 | [diff] [blame] | 10 | // This file implements the Dead Loop Deletion Pass. This pass is responsible |
| 11 | // for eliminating loops with non-infinite computable trip counts that have no |
| 12 | // side effects or volatile instructions, and do not contribute to the |
| 13 | // computation of the function's return value. |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/LoopDeletion.h" |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPass.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 23 | #include "llvm/IR/PatternMatch.h" |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "loop-delete" |
| 30 | |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 31 | STATISTIC(NumDeleted, "Number of loops deleted"); |
| 32 | |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 33 | /// This function deletes dead loops. The caller of this function needs to |
Anna Thomas | e7cb633 | 2017-06-25 21:13:58 +0000 | [diff] [blame] | 34 | /// guarantee that the loop is infact dead. Here we handle two kinds of dead |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 35 | /// loop. The first kind (\p isLoopDead) is where only invariant values from |
| 36 | /// within the loop are used outside of it. The second kind (\p |
| 37 | /// isLoopNeverExecuted) is where the loop is provably never executed. We can |
Anna Thomas | e7cb633 | 2017-06-25 21:13:58 +0000 | [diff] [blame] | 38 | /// always remove never executed loops since they will not cause any difference |
| 39 | /// to program behaviour. |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 40 | /// |
| 41 | /// This also updates the relevant analysis information in \p DT, \p SE, and \p |
| 42 | /// LI. It also updates the loop PM if an updater struct is provided. |
| 43 | // TODO: This function will be used by loop-simplifyCFG as well. So, move this |
| 44 | // to LoopUtils.cpp |
| 45 | static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE, |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 46 | LoopInfo &LI); |
| 47 | |
| 48 | enum class LoopDeletionResult { |
| 49 | Unmodified, |
| 50 | Modified, |
| 51 | Deleted, |
| 52 | }; |
| 53 | |
Chandler Carruth | 26169f00 | 2017-01-17 22:07:26 +0000 | [diff] [blame] | 54 | /// Determines if a loop is dead. |
| 55 | /// |
| 56 | /// This assumes that we've already checked for unique exit and exiting blocks, |
| 57 | /// and that the code is in LCSSA form. |
| 58 | static bool isLoopDead(Loop *L, ScalarEvolution &SE, |
| 59 | SmallVectorImpl<BasicBlock *> &ExitingBlocks, |
| 60 | BasicBlock *ExitBlock, bool &Changed, |
| 61 | BasicBlock *Preheader) { |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 62 | // Make sure that all PHI entries coming from the loop are loop invariant. |
Owen Anderson | e674600 | 2008-04-29 20:59:33 +0000 | [diff] [blame] | 63 | // Because the code is in LCSSA form, any values used outside of the loop |
| 64 | // must pass through a PHI in the exit block, meaning that this check is |
| 65 | // sufficient to guarantee that no loop-variant values are used outside |
| 66 | // of the loop. |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 67 | BasicBlock::iterator BI = ExitBlock->begin(); |
Sanjoy Das | 905fc27 | 2016-05-03 17:50:02 +0000 | [diff] [blame] | 68 | bool AllEntriesInvariant = true; |
| 69 | bool AllOutgoingValuesSame = true; |
Jakub Staszak | bc421ef | 2013-03-18 23:31:30 +0000 | [diff] [blame] | 70 | while (PHINode *P = dyn_cast<PHINode>(BI)) { |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 71 | Value *incoming = P->getIncomingValueForBlock(ExitingBlocks[0]); |
Cameron Zwarich | 8263085 | 2011-02-22 22:25:39 +0000 | [diff] [blame] | 72 | |
| 73 | // Make sure all exiting blocks produce the same incoming value for the exit |
| 74 | // block. If there are different incoming values for different exiting |
| 75 | // blocks, then it is impossible to statically determine which value should |
| 76 | // be used. |
Sanjoy Das | 7e7a5a0 | 2016-05-03 17:50:06 +0000 | [diff] [blame] | 77 | AllOutgoingValuesSame = |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 78 | all_of(makeArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) { |
Sanjoy Das | 7e7a5a0 | 2016-05-03 17:50:06 +0000 | [diff] [blame] | 79 | return incoming == P->getIncomingValueForBlock(BB); |
| 80 | }); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 81 | |
Sanjoy Das | 905fc27 | 2016-05-03 17:50:02 +0000 | [diff] [blame] | 82 | if (!AllOutgoingValuesSame) |
| 83 | break; |
| 84 | |
Jakub Staszak | bc421ef | 2013-03-18 23:31:30 +0000 | [diff] [blame] | 85 | if (Instruction *I = dyn_cast<Instruction>(incoming)) |
Sanjoy Das | 905fc27 | 2016-05-03 17:50:02 +0000 | [diff] [blame] | 86 | if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) { |
| 87 | AllEntriesInvariant = false; |
| 88 | break; |
| 89 | } |
Cameron Zwarich | 8263085 | 2011-02-22 22:25:39 +0000 | [diff] [blame] | 90 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 91 | ++BI; |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 92 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 93 | |
Sanjoy Das | 905fc27 | 2016-05-03 17:50:02 +0000 | [diff] [blame] | 94 | if (Changed) |
| 95 | SE.forgetLoopDispositions(L); |
| 96 | |
| 97 | if (!AllEntriesInvariant || !AllOutgoingValuesSame) |
| 98 | return false; |
| 99 | |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 100 | // Make sure that no instructions in the block have potential side-effects. |
Owen Anderson | e674600 | 2008-04-29 20:59:33 +0000 | [diff] [blame] | 101 | // This includes instructions that could write to memory, and loads that are |
Xin Tong | df4dff3 | 2017-02-23 23:47:10 +0000 | [diff] [blame] | 102 | // marked volatile. |
Davide Italiano | 49a0aac | 2017-02-26 07:08:20 +0000 | [diff] [blame] | 103 | for (auto &I : L->blocks()) |
| 104 | if (any_of(*I, [](Instruction &I) { return I.mayHaveSideEffects(); })) |
| 105 | return false; |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 109 | /// This function returns true if there is no viable path from the |
| 110 | /// entry block to the header of \p L. Right now, it only does |
| 111 | /// a local search to save compile time. |
| 112 | static bool isLoopNeverExecuted(Loop *L) { |
| 113 | using namespace PatternMatch; |
| 114 | |
| 115 | auto *Preheader = L->getLoopPreheader(); |
| 116 | // TODO: We can relax this constraint, since we just need a loop |
| 117 | // predecessor. |
| 118 | assert(Preheader && "Needs preheader!"); |
| 119 | |
| 120 | if (Preheader == &Preheader->getParent()->getEntryBlock()) |
| 121 | return false; |
| 122 | // All predecessors of the preheader should have a constant conditional |
| 123 | // branch, with the loop's preheader as not-taken. |
| 124 | for (auto *Pred: predecessors(Preheader)) { |
| 125 | BasicBlock *Taken, *NotTaken; |
| 126 | ConstantInt *Cond; |
| 127 | if (!match(Pred->getTerminator(), |
| 128 | m_Br(m_ConstantInt(Cond), Taken, NotTaken))) |
| 129 | return false; |
| 130 | if (!Cond->getZExtValue()) |
| 131 | std::swap(Taken, NotTaken); |
| 132 | if (Taken == Preheader) |
| 133 | return false; |
| 134 | } |
| 135 | assert(!pred_empty(Preheader) && |
| 136 | "Preheader should have predecessors at this point!"); |
| 137 | // All the predecessors have the loop preheader as not-taken target. |
| 138 | return true; |
| 139 | } |
| 140 | |
Chandler Carruth | 26169f00 | 2017-01-17 22:07:26 +0000 | [diff] [blame] | 141 | /// Remove a loop if it is dead. |
| 142 | /// |
| 143 | /// A loop is considered dead if it does not impact the observable behavior of |
| 144 | /// the program other than finite running time. This never removes a loop that |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 145 | /// might be infinite (unless it is never executed), as doing so could change |
| 146 | /// the halting/non-halting nature of a program. |
Chandler Carruth | 26169f00 | 2017-01-17 22:07:26 +0000 | [diff] [blame] | 147 | /// |
| 148 | /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in |
| 149 | /// order to make various safety checks work. |
| 150 | /// |
Chandler Carruth | 027340f | 2017-02-11 00:09:30 +0000 | [diff] [blame] | 151 | /// \returns true if any changes were made. This may mutate the loop even if it |
| 152 | /// is unable to delete it due to hoisting trivially loop invariant |
| 153 | /// instructions out of the loop. |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 154 | static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT, |
| 155 | ScalarEvolution &SE, LoopInfo &LI) { |
Sanjoy Das | 979a11d | 2016-02-21 17:11:59 +0000 | [diff] [blame] | 156 | assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); |
| 157 | |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 158 | // We can only remove the loop if there is a preheader that we can branch from |
| 159 | // after removing it. Also, if LoopSimplify form is not available, stay out |
| 160 | // of trouble. |
Chandler Carruth | aa885c9 | 2017-01-17 22:19:56 +0000 | [diff] [blame] | 161 | BasicBlock *Preheader = L->getLoopPreheader(); |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 162 | if (!Preheader || !L->hasDedicatedExits()) { |
| 163 | DEBUG(dbgs() |
| 164 | << "Deletion requires Loop with preheader and dedicated exits.\n"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 165 | return LoopDeletionResult::Unmodified; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 166 | } |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 167 | // We can't remove loops that contain subloops. If the subloops were dead, |
| 168 | // they would already have been removed in earlier executions of this pass. |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 169 | if (L->begin() != L->end()) { |
| 170 | DEBUG(dbgs() << "Loop contains subloops.\n"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 171 | return LoopDeletionResult::Unmodified; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 172 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 173 | |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 174 | |
| 175 | BasicBlock *ExitBlock = L->getUniqueExitBlock(); |
| 176 | |
| 177 | if (ExitBlock && isLoopNeverExecuted(L)) { |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 178 | DEBUG(dbgs() << "Loop is proven to never execute, delete it!"); |
Anna Thomas | e7cb633 | 2017-06-25 21:13:58 +0000 | [diff] [blame] | 179 | // Set incoming value to undef for phi nodes in the exit block. |
| 180 | BasicBlock::iterator BI = ExitBlock->begin(); |
| 181 | while (PHINode *P = dyn_cast<PHINode>(BI)) { |
| 182 | for (unsigned i = 0; i < P->getNumIncomingValues(); i++) |
| 183 | P->setIncomingValue(i, UndefValue::get(P->getType())); |
| 184 | BI++; |
| 185 | } |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 186 | deleteDeadLoop(L, DT, SE, LI); |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 187 | ++NumDeleted; |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 188 | return LoopDeletionResult::Deleted; |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // The remaining checks below are for a loop being dead because all statements |
| 192 | // in the loop are invariant. |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 193 | SmallVector<BasicBlock *, 4> ExitingBlocks; |
| 194 | L->getExitingBlocks(ExitingBlocks); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 195 | |
Owen Anderson | ad5f211 | 2008-05-16 04:32:45 +0000 | [diff] [blame] | 196 | // We require that the loop only have a single exit block. Otherwise, we'd |
| 197 | // be in the situation of needing to be able to solve statically which exit |
| 198 | // block will be branched to, or trying to preserve the branching logic in |
| 199 | // a loop invariant manner. |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 200 | if (!ExitBlock) { |
| 201 | DEBUG(dbgs() << "Deletion requires single exit block\n"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 202 | return LoopDeletionResult::Unmodified; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 203 | } |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 204 | // Finally, we have to check that the loop really is dead. |
Chandler Carruth | 027340f | 2017-02-11 00:09:30 +0000 | [diff] [blame] | 205 | bool Changed = false; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 206 | if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) { |
| 207 | DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 208 | return Changed ? LoopDeletionResult::Modified |
| 209 | : LoopDeletionResult::Unmodified; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 210 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 211 | |
Owen Anderson | ad5f211 | 2008-05-16 04:32:45 +0000 | [diff] [blame] | 212 | // Don't remove loops for which we can't solve the trip count. |
| 213 | // They could be infinite, in which case we'd be changing program behavior. |
Dan Gohman | 41d00ac | 2009-10-23 17:10:01 +0000 | [diff] [blame] | 214 | const SCEV *S = SE.getMaxBackedgeTakenCount(L); |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 215 | if (isa<SCEVCouldNotCompute>(S)) { |
| 216 | DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount.\n"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 217 | return Changed ? LoopDeletionResult::Modified |
| 218 | : LoopDeletionResult::Unmodified; |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 219 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 220 | |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 221 | DEBUG(dbgs() << "Loop is invariant, delete it!"); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 222 | deleteDeadLoop(L, DT, SE, LI); |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 223 | ++NumDeleted; |
| 224 | |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 225 | return LoopDeletionResult::Deleted; |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE, |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 229 | LoopInfo &LI) { |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 230 | assert(L->isLCSSAForm(DT) && "Expected LCSSA!"); |
| 231 | auto *Preheader = L->getLoopPreheader(); |
| 232 | assert(Preheader && "Preheader should exist!"); |
| 233 | |
Owen Anderson | e674600 | 2008-04-29 20:59:33 +0000 | [diff] [blame] | 234 | // Now that we know the removal is safe, remove the loop by changing the |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 235 | // branch from the preheader to go to the single exit block. |
Chandler Carruth | bb7e4b4 | 2017-01-17 22:00:52 +0000 | [diff] [blame] | 236 | // |
Owen Anderson | 586216e | 2008-04-29 00:45:15 +0000 | [diff] [blame] | 237 | // Because we're deleting a large chunk of code at once, the sequence in which |
Chandler Carruth | bd551e9 | 2017-01-17 22:09:28 +0000 | [diff] [blame] | 238 | // we remove things is very important to avoid invalidation issues. |
Dan Gohman | 7bb3173 | 2009-07-08 19:14:29 +0000 | [diff] [blame] | 239 | |
| 240 | // Tell ScalarEvolution that the loop is deleted. Do this before |
| 241 | // deleting the loop so that ScalarEvolution can look at the loop |
| 242 | // to determine what it needs to clean up. |
Dan Gohman | 880c92a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 243 | SE.forgetLoop(L); |
Dan Gohman | 7bb3173 | 2009-07-08 19:14:29 +0000 | [diff] [blame] | 244 | |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 245 | auto *ExitBlock = L->getUniqueExitBlock(); |
| 246 | assert(ExitBlock && "Should have a unique exit block!"); |
Anna Thomas | 72c90c8 | 2017-06-22 20:20:56 +0000 | [diff] [blame] | 247 | assert(L->hasDedicatedExits() && "Loop should have dedicated exits!"); |
| 248 | |
Jakub Kuderski | d869913 | 2017-08-02 18:17:52 +0000 | [diff] [blame] | 249 | auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator()); |
| 250 | assert(OldBr && "Preheader must end with a branch"); |
| 251 | assert(OldBr->isUnconditional() && "Preheader must have a single successor"); |
| 252 | // Connect the preheader to the exit block. Keep the old edge to the header |
| 253 | // around to perform the dominator tree update in two separate steps |
| 254 | // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge |
| 255 | // preheader -> header. |
| 256 | // |
| 257 | // |
| 258 | // 0. Preheader 1. Preheader 2. Preheader |
| 259 | // | | | | |
| 260 | // V | V | |
| 261 | // Header <--\ | Header <--\ | Header <--\ |
| 262 | // | | | | | | | | | | | |
| 263 | // | V | | | V | | | V | |
| 264 | // | Body --/ | | Body --/ | | Body --/ |
| 265 | // V V V V V |
| 266 | // Exit Exit Exit |
| 267 | // |
| 268 | // By doing this is two separate steps we can perform the dominator tree |
| 269 | // update without using the batch update API. |
| 270 | // |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 271 | // Even when the loop is never executed, we cannot remove the edge from the |
| 272 | // source block to the exit block. Consider the case where the unexecuted loop |
| 273 | // branches back to an outer loop. If we deleted the loop and removed the edge |
| 274 | // coming to this inner loop, this will break the outer loop structure (by |
| 275 | // deleting the backedge of the outer loop). If the outer loop is indeed a |
| 276 | // non-loop, it will be deleted in a future iteration of loop deletion pass. |
Jakub Kuderski | d869913 | 2017-08-02 18:17:52 +0000 | [diff] [blame] | 277 | IRBuilder<> Builder(OldBr); |
| 278 | Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock); |
| 279 | // Remove the old branch. The conditional branch becomes a new terminator. |
| 280 | OldBr->eraseFromParent(); |
| 281 | |
| 282 | // Update the dominator tree by informing it about the new edge from the |
| 283 | // preheader to the exit. |
| 284 | DT.insertEdge(Preheader, ExitBlock); |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 285 | |
Anna Thomas | 53c8d95 | 2017-05-03 11:47:11 +0000 | [diff] [blame] | 286 | // Rewrite phis in the exit block to get their inputs from the Preheader |
| 287 | // instead of the exiting block. |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 288 | BasicBlock::iterator BI = ExitBlock->begin(); |
Jakub Staszak | bc421ef | 2013-03-18 23:31:30 +0000 | [diff] [blame] | 289 | while (PHINode *P = dyn_cast<PHINode>(BI)) { |
Anna Thomas | 72c90c8 | 2017-06-22 20:20:56 +0000 | [diff] [blame] | 290 | // Set the zero'th element of Phi to be from the preheader and remove all |
| 291 | // other incoming values. Given the loop has dedicated exits, all other |
| 292 | // incoming values must be from the exiting blocks. |
| 293 | int PredIndex = 0; |
Anna Thomas | 72c90c8 | 2017-06-22 20:20:56 +0000 | [diff] [blame] | 294 | P->setIncomingBlock(PredIndex, Preheader); |
| 295 | // Removes all incoming values from all other exiting blocks (including |
| 296 | // duplicate values from an exiting block). |
| 297 | // Nuke all entries except the zero'th entry which is the preheader entry. |
| 298 | // NOTE! We need to remove Incoming Values in the reverse order as done |
| 299 | // below, to keep the indices valid for deletion (removeIncomingValues |
| 300 | // updates getNumIncomingValues and shifts all values down into the operand |
| 301 | // being deleted). |
| 302 | for (unsigned i = 0, e = P->getNumIncomingValues() - 1; i != e; ++i) |
| 303 | P->removeIncomingValue(e-i, false); |
| 304 | |
| 305 | assert((P->getNumIncomingValues() == 1 && |
| 306 | P->getIncomingBlock(PredIndex) == Preheader) && |
| 307 | "Should have exactly one value and that's from the preheader!"); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 308 | ++BI; |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 309 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 310 | |
Jakub Kuderski | d869913 | 2017-08-02 18:17:52 +0000 | [diff] [blame] | 311 | // Disconnect the loop body by branching directly to its exit. |
| 312 | Builder.SetInsertPoint(Preheader->getTerminator()); |
| 313 | Builder.CreateBr(ExitBlock); |
| 314 | // Remove the old branch. |
| 315 | Preheader->getTerminator()->eraseFromParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 316 | |
Jakub Kuderski | d869913 | 2017-08-02 18:17:52 +0000 | [diff] [blame] | 317 | // Inform the dominator tree about the removed edge. |
| 318 | DT.deleteEdge(Preheader, L->getHeader()); |
Dan Gohman | e669884 | 2009-02-24 01:21:53 +0000 | [diff] [blame] | 319 | |
Jakub Kuderski | d869913 | 2017-08-02 18:17:52 +0000 | [diff] [blame] | 320 | // Remove the block from the reference counting scheme, so that we can |
| 321 | // delete it freely later. |
| 322 | for (auto *Block : L->blocks()) |
| 323 | Block->dropAllReferences(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 324 | |
Owen Anderson | 586216e | 2008-04-29 00:45:15 +0000 | [diff] [blame] | 325 | // Erase the instructions and the blocks without having to worry |
| 326 | // about ordering because we already dropped the references. |
Owen Anderson | e674600 | 2008-04-29 20:59:33 +0000 | [diff] [blame] | 327 | // NOTE: This iteration is safe because erasing the block does not remove its |
| 328 | // entry from the loop's block list. We do that in the next section. |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 329 | for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end(); |
Owen Anderson | f4aece5 | 2008-05-29 08:15:48 +0000 | [diff] [blame] | 330 | LI != LE; ++LI) |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 331 | (*LI)->eraseFromParent(); |
Dan Gohman | e591411 | 2009-02-23 17:10:29 +0000 | [diff] [blame] | 332 | |
Owen Anderson | 586216e | 2008-04-29 00:45:15 +0000 | [diff] [blame] | 333 | // Finally, the blocks from loopinfo. This has to happen late because |
| 334 | // otherwise our loop iterators won't work. |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 335 | |
| 336 | SmallPtrSet<BasicBlock *, 8> blocks; |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 337 | blocks.insert(L->block_begin(), L->block_end()); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 338 | for (BasicBlock *BB : blocks) |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 339 | LI.removeBlock(BB); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 340 | |
Justin Bogner | 883a3ea | 2015-12-16 18:40:20 +0000 | [diff] [blame] | 341 | // The last step is to update LoopInfo now that we've eliminated this loop. |
Sanjoy Das | 388b012 | 2017-09-22 01:47:41 +0000 | [diff] [blame] | 342 | LI.erase(L); |
Owen Anderson | 94ad702 | 2008-04-29 00:38:34 +0000 | [diff] [blame] | 343 | } |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 344 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 345 | PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM, |
| 346 | LoopStandardAnalysisResults &AR, |
Chandler Carruth | d50c5fb | 2017-01-18 02:41:26 +0000 | [diff] [blame] | 347 | LPMUpdater &Updater) { |
Anna Thomas | 90f69ab | 2017-07-04 14:05:19 +0000 | [diff] [blame] | 348 | |
| 349 | DEBUG(dbgs() << "Analyzing Loop for deletion: "); |
| 350 | DEBUG(L.dump()); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 351 | auto Result = deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI); |
| 352 | if (Result == LoopDeletionResult::Unmodified) |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 353 | return PreservedAnalyses::all(); |
| 354 | |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 355 | if (Result == LoopDeletionResult::Deleted) |
| 356 | Updater.markLoopAsDeleted(L); |
| 357 | |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 358 | return getLoopPassPreservedAnalyses(); |
| 359 | } |
| 360 | |
| 361 | namespace { |
| 362 | class LoopDeletionLegacyPass : public LoopPass { |
| 363 | public: |
| 364 | static char ID; // Pass ID, replacement for typeid |
| 365 | LoopDeletionLegacyPass() : LoopPass(ID) { |
| 366 | initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 367 | } |
| 368 | |
| 369 | // Possibly eliminate loop L if it is dead. |
| 370 | bool runOnLoop(Loop *L, LPPassManager &) override; |
| 371 | |
| 372 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 373 | getLoopAnalysisUsage(AU); |
| 374 | } |
| 375 | }; |
| 376 | } |
| 377 | |
| 378 | char LoopDeletionLegacyPass::ID = 0; |
| 379 | INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion", |
| 380 | "Delete dead loops", false, false) |
| 381 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 382 | INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion", |
| 383 | "Delete dead loops", false, false) |
| 384 | |
| 385 | Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); } |
| 386 | |
| 387 | bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) { |
| 388 | if (skipLoop(L)) |
| 389 | return false; |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 390 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 391 | ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | 04a7387 | 2017-01-17 21:51:39 +0000 | [diff] [blame] | 392 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 393 | |
Anna Thomas | ada4ddc | 2017-07-04 17:00:03 +0000 | [diff] [blame] | 394 | DEBUG(dbgs() << "Analyzing Loop for deletion: "); |
| 395 | DEBUG(L->dump()); |
Sanjoy Das | 8e8c1bc | 2017-09-27 21:45:21 +0000 | [diff] [blame^] | 396 | return deleteLoopIfDead(L, DT, SE, LI) != LoopDeletionResult::Unmodified; |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 397 | } |