Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 1 | //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===// |
| 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 | // |
| 10 | // This file implements the Loop SimplifyCFG Pass. This pass is responsible for |
| 11 | // basic loop CFG cleanup, primarily to assist other loop passes. If you |
| 12 | // encounter a noncanonical CFG construct that causes another loop pass to |
| 13 | // perform suboptimally, this is the place to fix it up. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/DependenceAnalysis.h" |
| 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" |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DomTreeUpdater.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Dominators.h" |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils.h" |
Alina Sbirlea | dfd14ad | 2018-06-20 22:01:04 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 38 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | #define DEBUG_TYPE "loop-simplifycfg" |
| 43 | |
Max Kazantsev | 46955b5 | 2018-11-01 09:42:50 +0000 | [diff] [blame^] | 44 | static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, |
| 45 | LoopInfo &LI, MemorySSAUpdater *MSSAU) { |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 46 | bool Changed = false; |
Chijun Sima | 21a8b60 | 2018-08-03 05:08:17 +0000 | [diff] [blame] | 47 | DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 48 | // Copy blocks into a temporary array to avoid iterator invalidation issues |
| 49 | // as we remove them. |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 50 | SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 51 | |
| 52 | for (auto &Block : Blocks) { |
| 53 | // Attempt to merge blocks in the trivial case. Don't modify blocks which |
| 54 | // belong to other loops. |
Fiona Glaser | 36e8230 | 2016-01-29 23:12:52 +0000 | [diff] [blame] | 55 | BasicBlock *Succ = cast_or_null<BasicBlock>(Block); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 56 | if (!Succ) |
| 57 | continue; |
| 58 | |
| 59 | BasicBlock *Pred = Succ->getSinglePredecessor(); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 60 | if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L) |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 61 | continue; |
| 62 | |
Alina Sbirlea | dfd14ad | 2018-06-20 22:01:04 +0000 | [diff] [blame] | 63 | // Merge Succ into Pred and delete it. |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 64 | MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU); |
David Green | e6a9c24 | 2018-06-19 09:43:36 +0000 | [diff] [blame] | 65 | |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 66 | Changed = true; |
| 67 | } |
| 68 | |
| 69 | return Changed; |
| 70 | } |
| 71 | |
Max Kazantsev | 46955b5 | 2018-11-01 09:42:50 +0000 | [diff] [blame^] | 72 | static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, |
| 73 | ScalarEvolution &SE, MemorySSAUpdater *MSSAU) { |
| 74 | bool Changed = false; |
| 75 | |
| 76 | // Eliminate unconditional branches by merging blocks into their predecessors. |
| 77 | Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU); |
| 78 | |
| 79 | if (Changed) |
| 80 | SE.forgetTopmostLoop(&L); |
| 81 | |
| 82 | return Changed; |
| 83 | } |
| 84 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 85 | PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, |
| 86 | LoopStandardAnalysisResults &AR, |
| 87 | LPMUpdater &) { |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 88 | Optional<MemorySSAUpdater> MSSAU; |
| 89 | if (EnableMSSALoopDependency && AR.MSSA) |
| 90 | MSSAU = MemorySSAUpdater(AR.MSSA); |
| 91 | if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE, |
| 92 | MSSAU.hasValue() ? MSSAU.getPointer() : nullptr)) |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 93 | return PreservedAnalyses::all(); |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 94 | |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 95 | return getLoopPassPreservedAnalyses(); |
| 96 | } |
| 97 | |
| 98 | namespace { |
| 99 | class LoopSimplifyCFGLegacyPass : public LoopPass { |
| 100 | public: |
| 101 | static char ID; // Pass ID, replacement for typeid |
| 102 | LoopSimplifyCFGLegacyPass() : LoopPass(ID) { |
| 103 | initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 104 | } |
| 105 | |
| 106 | bool runOnLoop(Loop *L, LPPassManager &) override { |
| 107 | if (skipLoop(L)) |
| 108 | return false; |
| 109 | |
| 110 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 111 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
David Green | e6a9c24 | 2018-06-19 09:43:36 +0000 | [diff] [blame] | 112 | ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 113 | Optional<MemorySSAUpdater> MSSAU; |
| 114 | if (EnableMSSALoopDependency) { |
| 115 | MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); |
| 116 | MSSAU = MemorySSAUpdater(MSSA); |
| 117 | if (VerifyMemorySSA) |
| 118 | MSSA->verifyMemorySSA(); |
| 119 | } |
| 120 | return simplifyLoopCFG(*L, DT, LI, SE, |
| 121 | MSSAU.hasValue() ? MSSAU.getPointer() : nullptr); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 125 | if (EnableMSSALoopDependency) { |
| 126 | AU.addRequired<MemorySSAWrapperPass>(); |
| 127 | AU.addPreserved<MemorySSAWrapperPass>(); |
| 128 | } |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 129 | AU.addPreserved<DependenceAnalysisWrapperPass>(); |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 130 | getLoopAnalysisUsage(AU); |
| 131 | } |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | char LoopSimplifyCFGLegacyPass::ID = 0; |
| 136 | INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", |
| 137 | "Simplify loop CFG", false, false) |
| 138 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
Alina Sbirlea | 8b83d68 | 2018-08-22 20:10:21 +0000 | [diff] [blame] | 139 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 140 | INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", |
| 141 | "Simplify loop CFG", false, false) |
| 142 | |
| 143 | Pass *llvm::createLoopSimplifyCFGPass() { |
| 144 | return new LoopSimplifyCFGLegacyPass(); |
Fiona Glaser | b417d46 | 2016-01-29 22:35:36 +0000 | [diff] [blame] | 145 | } |