Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 1 | //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Loop SimplifyCFG Pass. This pass is responsible for |
| 10 | // basic loop CFG cleanup, primarily to assist other loop passes. If you |
| 11 | // encounter a noncanonical CFG construct that causes another loop pass to |
| 12 | // perform suboptimally, this is the place to fix it up. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/DependenceAnalysis.h" |
Richard Trieu | 5f436fc | 2019-02-06 02:52:52 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/DomTreeUpdater.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/GlobalsModRef.h" |
| 25 | #include "llvm/Analysis/LoopInfo.h" |
| 26 | #include "llvm/Analysis/LoopPass.h" |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemorySSA.h" |
| 28 | #include "llvm/Analysis/MemorySSAUpdater.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ScalarEvolution.h" |
| 30 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
| 31 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 32 | #include "llvm/IR/Dominators.h" |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils.h" |
Alina Sbirlea | dfd14ad | 2018-06-20 22:01:04 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 37 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | #define DEBUG_TYPE "loop-simplifycfg" |
| 42 | |
Max Kazantsev | e1c2dc2 | 2018-11-23 09:14:53 +0000 | [diff] [blame] | 43 | static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding", |
Jordan Rupprecht | 0efe27e | 2019-01-22 17:39:02 +0000 | [diff] [blame] | 44 | cl::init(false)); |
Max Kazantsev | e1c2dc2 | 2018-11-23 09:14:53 +0000 | [diff] [blame] | 45 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 46 | STATISTIC(NumTerminatorsFolded, |
| 47 | "Number of terminators folded to unconditional branches"); |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 48 | STATISTIC(NumLoopBlocksDeleted, |
| 49 | "Number of loop blocks deleted"); |
Max Kazantsev | edabb9a | 2018-12-24 07:41:33 +0000 | [diff] [blame] | 50 | STATISTIC(NumLoopExitsDeleted, |
| 51 | "Number of loop exiting edges deleted"); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 52 | |
| 53 | /// If \p BB is a switch or a conditional branch, but only one of its successors |
| 54 | /// can be reached from this block in runtime, return this successor. Otherwise, |
| 55 | /// return nullptr. |
| 56 | static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { |
| 57 | Instruction *TI = BB->getTerminator(); |
| 58 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 59 | if (BI->isUnconditional()) |
| 60 | return nullptr; |
| 61 | if (BI->getSuccessor(0) == BI->getSuccessor(1)) |
| 62 | return BI->getSuccessor(0); |
| 63 | ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); |
| 64 | if (!Cond) |
| 65 | return nullptr; |
| 66 | return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0); |
| 67 | } |
| 68 | |
| 69 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 70 | auto *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 71 | if (!CI) |
| 72 | return nullptr; |
| 73 | for (auto Case : SI->cases()) |
| 74 | if (Case.getCaseValue() == CI) |
| 75 | return Case.getCaseSuccessor(); |
| 76 | return SI->getDefaultDest(); |
| 77 | } |
| 78 | |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame] | 82 | namespace { |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 83 | /// Helper class that can turn branches and switches with constant conditions |
| 84 | /// into unconditional branches. |
| 85 | class ConstantTerminatorFoldingImpl { |
| 86 | private: |
| 87 | Loop &L; |
| 88 | LoopInfo &LI; |
| 89 | DominatorTree &DT; |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 90 | ScalarEvolution &SE; |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 91 | MemorySSAUpdater *MSSAU; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 92 | |
Max Kazantsev | a523a21 | 2018-12-07 05:44:45 +0000 | [diff] [blame] | 93 | // Whether or not the current loop has irreducible CFG. |
| 94 | bool HasIrreducibleCFG = false; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 95 | // Whether or not the current loop will still exist after terminator constant |
| 96 | // folding will be done. In theory, there are two ways how it can happen: |
| 97 | // 1. Loop's latch(es) become unreachable from loop header; |
| 98 | // 2. Loop's header becomes unreachable from method entry. |
| 99 | // In practice, the second situation is impossible because we only modify the |
| 100 | // current loop and its preheader and do not affect preheader's reachibility |
| 101 | // from any other block. So this variable set to true means that loop's latch |
| 102 | // has become unreachable from loop header. |
| 103 | bool DeleteCurrentLoop = false; |
| 104 | |
| 105 | // The blocks of the original loop that will still be reachable from entry |
| 106 | // after the constant folding. |
| 107 | SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks; |
| 108 | // The blocks of the original loop that will become unreachable from entry |
| 109 | // after the constant folding. |
Max Kazantsev | 80e4b40 | 2018-12-28 06:08:51 +0000 | [diff] [blame] | 110 | SmallVector<BasicBlock *, 8> DeadLoopBlocks; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 111 | // The exits of the original loop that will still be reachable from entry |
| 112 | // after the constant folding. |
| 113 | SmallPtrSet<BasicBlock *, 8> LiveExitBlocks; |
| 114 | // The exits of the original loop that will become unreachable from entry |
| 115 | // after the constant folding. |
Max Kazantsev | 56a2443 | 2018-11-22 12:33:41 +0000 | [diff] [blame] | 116 | SmallVector<BasicBlock *, 8> DeadExitBlocks; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 117 | // The blocks that will still be a part of the current loop after folding. |
| 118 | SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding; |
| 119 | // The blocks that have terminators with constant condition that can be |
| 120 | // folded. Note: fold candidates should be in L but not in any of its |
| 121 | // subloops to avoid complex LI updates. |
| 122 | SmallVector<BasicBlock *, 8> FoldCandidates; |
| 123 | |
| 124 | void dump() const { |
| 125 | dbgs() << "Constant terminator folding for loop " << L << "\n"; |
| 126 | dbgs() << "After terminator constant-folding, the loop will"; |
| 127 | if (!DeleteCurrentLoop) |
| 128 | dbgs() << " not"; |
| 129 | dbgs() << " be destroyed\n"; |
Max Kazantsev | 56a2443 | 2018-11-22 12:33:41 +0000 | [diff] [blame] | 130 | auto PrintOutVector = [&](const char *Message, |
| 131 | const SmallVectorImpl<BasicBlock *> &S) { |
| 132 | dbgs() << Message << "\n"; |
| 133 | for (const BasicBlock *BB : S) |
| 134 | dbgs() << "\t" << BB->getName() << "\n"; |
| 135 | }; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 136 | auto PrintOutSet = [&](const char *Message, |
| 137 | const SmallPtrSetImpl<BasicBlock *> &S) { |
| 138 | dbgs() << Message << "\n"; |
| 139 | for (const BasicBlock *BB : S) |
| 140 | dbgs() << "\t" << BB->getName() << "\n"; |
| 141 | }; |
Max Kazantsev | 56a2443 | 2018-11-22 12:33:41 +0000 | [diff] [blame] | 142 | PrintOutVector("Blocks in which we can constant-fold terminator:", |
| 143 | FoldCandidates); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 144 | PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks); |
Max Kazantsev | 80e4b40 | 2018-12-28 06:08:51 +0000 | [diff] [blame] | 145 | PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 146 | PrintOutSet("Live exit blocks:", LiveExitBlocks); |
Max Kazantsev | 56a2443 | 2018-11-22 12:33:41 +0000 | [diff] [blame] | 147 | PrintOutVector("Dead exit blocks:", DeadExitBlocks); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 148 | if (!DeleteCurrentLoop) |
| 149 | PrintOutSet("The following blocks will still be part of the loop:", |
| 150 | BlocksInLoopAfterFolding); |
| 151 | } |
| 152 | |
Max Kazantsev | a523a21 | 2018-12-07 05:44:45 +0000 | [diff] [blame] | 153 | /// Whether or not the current loop has irreducible CFG. |
| 154 | bool hasIrreducibleCFG(LoopBlocksDFS &DFS) { |
| 155 | assert(DFS.isComplete() && "DFS is expected to be finished"); |
| 156 | // Index of a basic block in RPO traversal. |
| 157 | DenseMap<const BasicBlock *, unsigned> RPO; |
| 158 | unsigned Current = 0; |
| 159 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) |
| 160 | RPO[*I] = Current++; |
| 161 | |
| 162 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { |
| 163 | BasicBlock *BB = *I; |
| 164 | for (auto *Succ : successors(BB)) |
| 165 | if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ]) |
| 166 | // If an edge goes from a block with greater order number into a block |
| 167 | // with lesses number, and it is not a loop backedge, then it can only |
| 168 | // be a part of irreducible non-loop cycle. |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 174 | /// Fill all information about status of blocks and exits of the current loop |
| 175 | /// if constant folding of all branches will be done. |
| 176 | void analyze() { |
| 177 | LoopBlocksDFS DFS(&L); |
| 178 | DFS.perform(&LI); |
| 179 | assert(DFS.isComplete() && "DFS is expected to be finished"); |
| 180 | |
Max Kazantsev | a523a21 | 2018-12-07 05:44:45 +0000 | [diff] [blame] | 181 | // TODO: The algorithm below relies on both RPO and Postorder traversals. |
| 182 | // When the loop has only reducible CFG inside, then the invariant "all |
| 183 | // predecessors of X are processed before X in RPO" is preserved. However |
| 184 | // an irreducible loop can break this invariant (e.g. latch does not have to |
| 185 | // be the last block in the traversal in this case, and the algorithm relies |
| 186 | // on this). We can later decide to support such cases by altering the |
| 187 | // algorithms, but so far we just give up analyzing them. |
| 188 | if (hasIrreducibleCFG(DFS)) { |
| 189 | HasIrreducibleCFG = true; |
| 190 | return; |
| 191 | } |
| 192 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 193 | // Collect live and dead loop blocks and exits. |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 194 | LiveLoopBlocks.insert(L.getHeader()); |
| 195 | for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) { |
| 196 | BasicBlock *BB = *I; |
| 197 | |
| 198 | // If a loop block wasn't marked as live so far, then it's dead. |
| 199 | if (!LiveLoopBlocks.count(BB)) { |
Max Kazantsev | 80e4b40 | 2018-12-28 06:08:51 +0000 | [diff] [blame] | 200 | DeadLoopBlocks.push_back(BB); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 201 | continue; |
| 202 | } |
| 203 | |
| 204 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); |
| 205 | |
| 206 | // If a block has only one live successor, it's a candidate on constant |
| 207 | // folding. Only handle blocks from current loop: branches in child loops |
| 208 | // are skipped because if they can be folded, they should be folded during |
| 209 | // the processing of child loops. |
Max Kazantsev | 56515a2 | 2019-01-24 05:20:29 +0000 | [diff] [blame] | 210 | bool TakeFoldCandidate = TheOnlySucc && LI.getLoopFor(BB) == &L; |
| 211 | if (TakeFoldCandidate) |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 212 | FoldCandidates.push_back(BB); |
| 213 | |
| 214 | // Handle successors. |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 215 | for (BasicBlock *Succ : successors(BB)) |
Max Kazantsev | 56515a2 | 2019-01-24 05:20:29 +0000 | [diff] [blame] | 216 | if (!TakeFoldCandidate || TheOnlySucc == Succ) { |
Max Kazantsev | d9f59f8 | 2018-11-22 10:48:30 +0000 | [diff] [blame] | 217 | if (L.contains(Succ)) |
| 218 | LiveLoopBlocks.insert(Succ); |
| 219 | else |
| 220 | LiveExitBlocks.insert(Succ); |
| 221 | } |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // Sanity check: amount of dead and live loop blocks should match the total |
| 225 | // number of blocks in loop. |
| 226 | assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() && |
| 227 | "Malformed block sets?"); |
| 228 | |
| 229 | // Now, all exit blocks that are not marked as live are dead. |
Max Kazantsev | d9f59f8 | 2018-11-22 10:48:30 +0000 | [diff] [blame] | 230 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 231 | L.getExitBlocks(ExitBlocks); |
Max Kazantsev | a4ccfc1 | 2019-02-06 07:49:17 +0000 | [diff] [blame^] | 232 | SmallPtrSet<BasicBlock *, 8> UniqueDeadExits; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 233 | for (auto *ExitBlock : ExitBlocks) |
Max Kazantsev | a4ccfc1 | 2019-02-06 07:49:17 +0000 | [diff] [blame^] | 234 | if (!LiveExitBlocks.count(ExitBlock) && |
| 235 | UniqueDeadExits.insert(ExitBlock).second) |
Max Kazantsev | 56a2443 | 2018-11-22 12:33:41 +0000 | [diff] [blame] | 236 | DeadExitBlocks.push_back(ExitBlock); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 237 | |
| 238 | // Whether or not the edge From->To will still be present in graph after the |
| 239 | // folding. |
| 240 | auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) { |
| 241 | if (!LiveLoopBlocks.count(From)) |
| 242 | return false; |
| 243 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From); |
Max Kazantsev | 38cd9ac | 2019-01-25 05:05:02 +0000 | [diff] [blame] | 244 | return !TheOnlySucc || TheOnlySucc == To || LI.getLoopFor(From) != &L; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | // The loop will not be destroyed if its latch is live. |
| 248 | DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader()); |
| 249 | |
| 250 | // If we are going to delete the current loop completely, no extra analysis |
| 251 | // is needed. |
| 252 | if (DeleteCurrentLoop) |
| 253 | return; |
| 254 | |
| 255 | // Otherwise, we should check which blocks will still be a part of the |
| 256 | // current loop after the transform. |
| 257 | BlocksInLoopAfterFolding.insert(L.getLoopLatch()); |
| 258 | // If the loop is live, then we should compute what blocks are still in |
| 259 | // loop after all branch folding has been done. A block is in loop if |
| 260 | // it has a live edge to another block that is in the loop; by definition, |
| 261 | // latch is in the loop. |
| 262 | auto BlockIsInLoop = [&](BasicBlock *BB) { |
| 263 | return any_of(successors(BB), [&](BasicBlock *Succ) { |
| 264 | return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ); |
| 265 | }); |
| 266 | }; |
| 267 | for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) { |
| 268 | BasicBlock *BB = *I; |
| 269 | if (BlockIsInLoop(BB)) |
| 270 | BlocksInLoopAfterFolding.insert(BB); |
| 271 | } |
| 272 | |
| 273 | // Sanity check: header must be in loop. |
| 274 | assert(BlocksInLoopAfterFolding.count(L.getHeader()) && |
| 275 | "Header not in loop?"); |
Max Kazantsev | b565e60 | 2018-11-22 12:43:27 +0000 | [diff] [blame] | 276 | assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() && |
| 277 | "All blocks that stay in loop should be live!"); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Max Kazantsev | edabb9a | 2018-12-24 07:41:33 +0000 | [diff] [blame] | 280 | /// We need to preserve static reachibility of all loop exit blocks (this is) |
| 281 | /// required by loop pass manager. In order to do it, we make the following |
| 282 | /// trick: |
| 283 | /// |
| 284 | /// preheader: |
| 285 | /// <preheader code> |
| 286 | /// br label %loop_header |
| 287 | /// |
| 288 | /// loop_header: |
| 289 | /// ... |
| 290 | /// br i1 false, label %dead_exit, label %loop_block |
| 291 | /// ... |
| 292 | /// |
| 293 | /// We cannot simply remove edge from the loop to dead exit because in this |
| 294 | /// case dead_exit (and its successors) may become unreachable. To avoid that, |
| 295 | /// we insert the following fictive preheader: |
| 296 | /// |
| 297 | /// preheader: |
| 298 | /// <preheader code> |
| 299 | /// switch i32 0, label %preheader-split, |
| 300 | /// [i32 1, label %dead_exit_1], |
| 301 | /// [i32 2, label %dead_exit_2], |
| 302 | /// ... |
| 303 | /// [i32 N, label %dead_exit_N], |
| 304 | /// |
| 305 | /// preheader-split: |
| 306 | /// br label %loop_header |
| 307 | /// |
| 308 | /// loop_header: |
| 309 | /// ... |
| 310 | /// br i1 false, label %dead_exit_N, label %loop_block |
| 311 | /// ... |
| 312 | /// |
| 313 | /// Doing so, we preserve static reachibility of all dead exits and can later |
| 314 | /// remove edges from the loop to these blocks. |
| 315 | void handleDeadExits() { |
| 316 | // If no dead exits, nothing to do. |
| 317 | if (DeadExitBlocks.empty()) |
| 318 | return; |
| 319 | |
| 320 | // Construct split preheader and the dummy switch to thread edges from it to |
| 321 | // dead exits. |
| 322 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 323 | BasicBlock *Preheader = L.getLoopPreheader(); |
| 324 | BasicBlock *NewPreheader = Preheader->splitBasicBlock( |
| 325 | Preheader->getTerminator(), |
| 326 | Twine(Preheader->getName()).concat("-split")); |
| 327 | DTU.deleteEdge(Preheader, L.getHeader()); |
| 328 | DTU.insertEdge(NewPreheader, L.getHeader()); |
| 329 | DTU.insertEdge(Preheader, NewPreheader); |
| 330 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 331 | SwitchInst *DummySwitch = |
| 332 | Builder.CreateSwitch(Builder.getInt32(0), NewPreheader); |
| 333 | Preheader->getTerminator()->eraseFromParent(); |
| 334 | |
| 335 | unsigned DummyIdx = 1; |
| 336 | for (BasicBlock *BB : DeadExitBlocks) { |
| 337 | SmallVector<Instruction *, 4> DeadPhis; |
| 338 | for (auto &PN : BB->phis()) |
| 339 | DeadPhis.push_back(&PN); |
| 340 | |
| 341 | // Eliminate all Phis from dead exits. |
| 342 | for (Instruction *PN : DeadPhis) { |
| 343 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 344 | PN->eraseFromParent(); |
| 345 | } |
| 346 | assert(DummyIdx != 0 && "Too many dead exits!"); |
| 347 | DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB); |
| 348 | DTU.insertEdge(Preheader, BB); |
| 349 | ++NumLoopExitsDeleted; |
| 350 | } |
| 351 | |
| 352 | assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?"); |
| 353 | if (Loop *OuterLoop = LI.getLoopFor(Preheader)) { |
| 354 | OuterLoop->addBasicBlockToLoop(NewPreheader, LI); |
| 355 | |
| 356 | // When we break dead edges, the outer loop may become unreachable from |
| 357 | // the current loop. We need to fix loop info accordingly. For this, we |
| 358 | // find the most nested loop that still contains L and remove L from all |
| 359 | // loops that are inside of it. |
| 360 | Loop *StillReachable = nullptr; |
| 361 | for (BasicBlock *BB : LiveExitBlocks) { |
| 362 | Loop *BBL = LI.getLoopFor(BB); |
| 363 | if (BBL && BBL->contains(L.getHeader())) |
| 364 | if (!StillReachable || |
| 365 | BBL->getLoopDepth() > StillReachable->getLoopDepth()) |
| 366 | StillReachable = BBL; |
| 367 | } |
| 368 | |
| 369 | // Okay, our loop is no longer in the outer loop (and maybe not in some of |
| 370 | // its parents as well). Make the fixup. |
| 371 | if (StillReachable != OuterLoop) { |
| 372 | LI.changeLoopFor(NewPreheader, StillReachable); |
| 373 | for (Loop *NotContaining = OuterLoop; NotContaining != StillReachable; |
| 374 | NotContaining = NotContaining->getParentLoop()) { |
| 375 | NotContaining->removeBlockFromLoop(NewPreheader); |
| 376 | for (auto *BB : L.blocks()) |
| 377 | NotContaining->removeBlockFromLoop(BB); |
| 378 | } |
| 379 | OuterLoop->removeChildLoop(&L); |
| 380 | if (StillReachable) |
| 381 | StillReachable->addChildLoop(&L); |
| 382 | else |
| 383 | LI.addTopLevelLoop(&L); |
Max Kazantsev | 61a8d3f | 2019-01-17 12:51:10 +0000 | [diff] [blame] | 384 | |
| 385 | // Some values from loops in [OuterLoop, StillReachable) could be used |
| 386 | // in the current loop. Now it is not their child anymore, so such uses |
| 387 | // require LCSSA Phis. |
| 388 | Loop *FixLCSSALoop = OuterLoop; |
| 389 | while (FixLCSSALoop->getParentLoop() != StillReachable) |
| 390 | FixLCSSALoop = FixLCSSALoop->getParentLoop(); |
| 391 | assert(FixLCSSALoop && "Should be a loop!"); |
| 392 | formLCSSARecursively(*FixLCSSALoop, DT, &LI, &SE); |
Max Kazantsev | edabb9a | 2018-12-24 07:41:33 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 397 | /// Delete loop blocks that have become unreachable after folding. Make all |
| 398 | /// relevant updates to DT and LI. |
| 399 | void deleteDeadLoopBlocks() { |
| 400 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
Max Kazantsev | 80e4b40 | 2018-12-28 06:08:51 +0000 | [diff] [blame] | 401 | if (MSSAU) { |
| 402 | SmallPtrSet<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(), |
| 403 | DeadLoopBlocks.end()); |
| 404 | MSSAU->removeBlocks(DeadLoopBlocksSet); |
| 405 | } |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 406 | for (auto *BB : DeadLoopBlocks) { |
| 407 | assert(BB != L.getHeader() && |
| 408 | "Header of the current loop cannot be dead!"); |
| 409 | LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName() |
| 410 | << "\n"); |
| 411 | if (LI.isLoopHeader(BB)) { |
| 412 | assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!"); |
| 413 | LI.erase(LI.getLoopFor(BB)); |
| 414 | } |
| 415 | LI.removeBlock(BB); |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 416 | } |
Max Kazantsev | 8b13416 | 2019-01-17 12:25:40 +0000 | [diff] [blame] | 417 | |
| 418 | DeleteDeadBlocks(DeadLoopBlocks, &DTU); |
| 419 | NumLoopBlocksDeleted += DeadLoopBlocks.size(); |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 422 | /// Constant-fold terminators of blocks acculumated in FoldCandidates into the |
| 423 | /// unconditional branches. |
| 424 | void foldTerminators() { |
| 425 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 426 | |
| 427 | for (BasicBlock *BB : FoldCandidates) { |
| 428 | assert(LI.getLoopFor(BB) == &L && "Should be a loop block!"); |
| 429 | BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB); |
| 430 | assert(TheOnlySucc && "Should have one live successor!"); |
| 431 | |
| 432 | LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName() |
| 433 | << " with an unconditional branch to the block " |
| 434 | << TheOnlySucc->getName() << "\n"); |
| 435 | |
| 436 | SmallPtrSet<BasicBlock *, 2> DeadSuccessors; |
| 437 | // Remove all BB's successors except for the live one. |
Max Kazantsev | c4e4d64 | 2018-11-27 06:17:21 +0000 | [diff] [blame] | 438 | unsigned TheOnlySuccDuplicates = 0; |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 439 | for (auto *Succ : successors(BB)) |
| 440 | if (Succ != TheOnlySucc) { |
| 441 | DeadSuccessors.insert(Succ); |
Max Kazantsev | cb8e240 | 2018-11-23 07:56:47 +0000 | [diff] [blame] | 442 | // If our successor lies in a different loop, we don't want to remove |
| 443 | // the one-input Phi because it is a LCSSA Phi. |
| 444 | bool PreserveLCSSAPhi = !L.contains(Succ); |
| 445 | Succ->removePredecessor(BB, PreserveLCSSAPhi); |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 446 | if (MSSAU) |
| 447 | MSSAU->removeEdge(BB, Succ); |
Max Kazantsev | c4e4d64 | 2018-11-27 06:17:21 +0000 | [diff] [blame] | 448 | } else |
| 449 | ++TheOnlySuccDuplicates; |
| 450 | |
| 451 | assert(TheOnlySuccDuplicates > 0 && "Should be!"); |
| 452 | // If TheOnlySucc was BB's successor more than once, after transform it |
| 453 | // will be its successor only once. Remove redundant inputs from |
| 454 | // TheOnlySucc's Phis. |
| 455 | bool PreserveLCSSAPhi = !L.contains(TheOnlySucc); |
| 456 | for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup) |
| 457 | TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi); |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 458 | if (MSSAU && TheOnlySuccDuplicates > 1) |
| 459 | MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 460 | |
| 461 | IRBuilder<> Builder(BB->getContext()); |
| 462 | Instruction *Term = BB->getTerminator(); |
| 463 | Builder.SetInsertPoint(Term); |
| 464 | Builder.CreateBr(TheOnlySucc); |
| 465 | Term->eraseFromParent(); |
| 466 | |
| 467 | for (auto *DeadSucc : DeadSuccessors) |
| 468 | DTU.deleteEdge(BB, DeadSucc); |
| 469 | |
| 470 | ++NumTerminatorsFolded; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | public: |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 475 | ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT, |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 476 | ScalarEvolution &SE, |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 477 | MemorySSAUpdater *MSSAU) |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 478 | : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU) {} |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 479 | bool run() { |
| 480 | assert(L.getLoopLatch() && "Should be single latch!"); |
| 481 | |
| 482 | // Collect all available information about status of blocks after constant |
| 483 | // folding. |
| 484 | analyze(); |
| 485 | |
| 486 | LLVM_DEBUG(dbgs() << "In function " << L.getHeader()->getParent()->getName() |
| 487 | << ": "); |
| 488 | |
Max Kazantsev | a523a21 | 2018-12-07 05:44:45 +0000 | [diff] [blame] | 489 | if (HasIrreducibleCFG) { |
| 490 | LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n"); |
| 491 | return false; |
| 492 | } |
| 493 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 494 | // Nothing to constant-fold. |
| 495 | if (FoldCandidates.empty()) { |
| 496 | LLVM_DEBUG( |
| 497 | dbgs() << "No constant terminator folding candidates found in loop " |
| 498 | << L.getHeader()->getName() << "\n"); |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | // TODO: Support deletion of the current loop. |
| 503 | if (DeleteCurrentLoop) { |
| 504 | LLVM_DEBUG( |
| 505 | dbgs() |
| 506 | << "Give up constant terminator folding in loop " |
| 507 | << L.getHeader()->getName() |
| 508 | << ": we don't currently support deletion of the current loop.\n"); |
| 509 | return false; |
| 510 | } |
| 511 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 512 | // TODO: Support blocks that are not dead, but also not in loop after the |
| 513 | // folding. |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 514 | if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() != |
| 515 | L.getNumBlocks()) { |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 516 | LLVM_DEBUG( |
| 517 | dbgs() << "Give up constant terminator folding in loop " |
| 518 | << L.getHeader()->getName() |
| 519 | << ": we don't currently" |
| 520 | " support blocks that are not dead, but will stop " |
| 521 | "being a part of the loop after constant-folding.\n"); |
| 522 | return false; |
| 523 | } |
| 524 | |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 525 | SE.forgetTopmostLoop(&L); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 526 | // Dump analysis results. |
| 527 | LLVM_DEBUG(dump()); |
| 528 | |
| 529 | LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size() |
| 530 | << " terminators in loop " << L.getHeader()->getName() |
| 531 | << "\n"); |
| 532 | |
| 533 | // Make the actual transforms. |
Max Kazantsev | edabb9a | 2018-12-24 07:41:33 +0000 | [diff] [blame] | 534 | handleDeadExits(); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 535 | foldTerminators(); |
| 536 | |
Max Kazantsev | 347c583 | 2018-12-24 06:06:17 +0000 | [diff] [blame] | 537 | if (!DeadLoopBlocks.empty()) { |
| 538 | LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size() |
| 539 | << " dead blocks in loop " << L.getHeader()->getName() |
| 540 | << "\n"); |
| 541 | deleteDeadLoopBlocks(); |
| 542 | } |
| 543 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 544 | #ifndef NDEBUG |
| 545 | // Make sure that we have preserved all data structures after the transform. |
Max Kazantsev | 365021c | 2019-01-30 12:32:19 +0000 | [diff] [blame] | 546 | assert(DT.verify() && "DT broken after transform!"); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 547 | assert(DT.isReachableFromEntry(L.getHeader())); |
| 548 | LI.verify(DT); |
| 549 | #endif |
| 550 | |
| 551 | return true; |
| 552 | } |
| 553 | }; |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame] | 554 | } // namespace |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 555 | |
| 556 | /// Turn branches and switches with known constant conditions into unconditional |
| 557 | /// branches. |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 558 | static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 559 | ScalarEvolution &SE, |
Max Kazantsev | 9cf417d | 2018-11-30 10:06:23 +0000 | [diff] [blame] | 560 | MemorySSAUpdater *MSSAU) { |
Max Kazantsev | e1c2dc2 | 2018-11-23 09:14:53 +0000 | [diff] [blame] | 561 | if (!EnableTermFolding) |
| 562 | return false; |
| 563 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 564 | // To keep things simple, only process loops with single latch. We |
| 565 | // canonicalize most loops to this form. We can support multi-latch if needed. |
| 566 | if (!L.getLoopLatch()) |
| 567 | return false; |
| 568 | |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 569 | ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 570 | return BranchFolder.run(); |
| 571 | } |
| 572 | |
Max Kazantsev | 46955b5 | 2018-11-01 09:42:50 +0000 | [diff] [blame] | 573 | static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, |
| 574 | LoopInfo &LI, MemorySSAUpdater *MSSAU) { |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 575 | bool Changed = false; |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame] | 576 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 577 | // Copy blocks into a temporary array to avoid iterator invalidation issues |
| 578 | // as we remove them. |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 579 | SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 580 | |
| 581 | for (auto &Block : Blocks) { |
| 582 | // Attempt to merge blocks in the trivial case. Don't modify blocks which |
| 583 | // belong to other loops. |
Fiona Glaser | 36e8230 | 2016-01-29 23:12:52 +0000 | [diff] [blame] | 584 | BasicBlock *Succ = cast_or_null<BasicBlock>(Block); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 585 | if (!Succ) |
| 586 | continue; |
| 587 | |
| 588 | BasicBlock *Pred = Succ->getSinglePredecessor(); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 589 | if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L) |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 590 | continue; |
| 591 | |
Alina Sbirlea | dfd14ad | 2018-06-20 22:01:04 +0000 | [diff] [blame] | 592 | // Merge Succ into Pred and delete it. |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 593 | MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU); |
David Green | e6a9c24 | 2018-06-19 09:43:36 +0000 | [diff] [blame] | 594 | |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 595 | Changed = true; |
| 596 | } |
| 597 | |
| 598 | return Changed; |
| 599 | } |
| 600 | |
Max Kazantsev | 46955b5 | 2018-11-01 09:42:50 +0000 | [diff] [blame] | 601 | static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, |
| 602 | ScalarEvolution &SE, MemorySSAUpdater *MSSAU) { |
| 603 | bool Changed = false; |
| 604 | |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 605 | // Constant-fold terminators with known constant conditions. |
Max Kazantsev | 201534d | 2018-12-29 04:26:22 +0000 | [diff] [blame] | 606 | Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU); |
Max Kazantsev | c04b530 | 2018-11-20 05:43:32 +0000 | [diff] [blame] | 607 | |
Max Kazantsev | 46955b5 | 2018-11-01 09:42:50 +0000 | [diff] [blame] | 608 | // Eliminate unconditional branches by merging blocks into their predecessors. |
| 609 | Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU); |
| 610 | |
| 611 | if (Changed) |
| 612 | SE.forgetTopmostLoop(&L); |
| 613 | |
| 614 | return Changed; |
| 615 | } |
| 616 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 617 | PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, |
| 618 | LoopStandardAnalysisResults &AR, |
| 619 | LPMUpdater &) { |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 620 | Optional<MemorySSAUpdater> MSSAU; |
| 621 | if (EnableMSSALoopDependency && AR.MSSA) |
| 622 | MSSAU = MemorySSAUpdater(AR.MSSA); |
| 623 | if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE, |
| 624 | MSSAU.hasValue() ? MSSAU.getPointer() : nullptr)) |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 625 | return PreservedAnalyses::all(); |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 626 | |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 627 | return getLoopPassPreservedAnalyses(); |
| 628 | } |
| 629 | |
| 630 | namespace { |
| 631 | class LoopSimplifyCFGLegacyPass : public LoopPass { |
| 632 | public: |
| 633 | static char ID; // Pass ID, replacement for typeid |
| 634 | LoopSimplifyCFGLegacyPass() : LoopPass(ID) { |
| 635 | initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 636 | } |
| 637 | |
| 638 | bool runOnLoop(Loop *L, LPPassManager &) override { |
| 639 | if (skipLoop(L)) |
| 640 | return false; |
| 641 | |
| 642 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 643 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
David Green | e6a9c24 | 2018-06-19 09:43:36 +0000 | [diff] [blame] | 644 | ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 645 | Optional<MemorySSAUpdater> MSSAU; |
| 646 | if (EnableMSSALoopDependency) { |
| 647 | MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); |
| 648 | MSSAU = MemorySSAUpdater(MSSA); |
| 649 | if (VerifyMemorySSA) |
| 650 | MSSA->verifyMemorySSA(); |
| 651 | } |
| 652 | return simplifyLoopCFG(*L, DT, LI, SE, |
| 653 | MSSAU.hasValue() ? MSSAU.getPointer() : nullptr); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 657 | if (EnableMSSALoopDependency) { |
| 658 | AU.addRequired<MemorySSAWrapperPass>(); |
| 659 | AU.addPreserved<MemorySSAWrapperPass>(); |
| 660 | } |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 661 | AU.addPreserved<DependenceAnalysisWrapperPass>(); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 662 | getLoopAnalysisUsage(AU); |
| 663 | } |
| 664 | }; |
| 665 | } |
| 666 | |
| 667 | char LoopSimplifyCFGLegacyPass::ID = 0; |
| 668 | INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", |
| 669 | "Simplify loop CFG", false, false) |
| 670 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 671 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 672 | INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", |
| 673 | "Simplify loop CFG", false, false) |
| 674 | |
| 675 | Pass *llvm::createLoopSimplifyCFGPass() { |
| 676 | return new LoopSimplifyCFGLegacyPass(); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 677 | } |