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 17 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/AliasAnalysis.h" 21 #include "llvm/Analysis/AssumptionCache.h" 22 #include "llvm/Analysis/BasicAliasAnalysis.h" 23 #include "llvm/Analysis/DependenceAnalysis.h" 24 #include "llvm/Analysis/GlobalsModRef.h" 25 #include "llvm/Analysis/LoopInfo.h" 26 #include "llvm/Analysis/LoopPass.h" 27 #include "llvm/Analysis/ScalarEvolution.h" 28 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 29 #include "llvm/Analysis/TargetTransformInfo.h" 30 #include "llvm/IR/DomTreeUpdater.h" 31 #include "llvm/IR/Dominators.h" 32 #include "llvm/Transforms/Scalar.h" 33 #include "llvm/Transforms/Scalar/LoopPassManager.h" 34 #include "llvm/Transforms/Utils.h" 35 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 36 #include "llvm/Transforms/Utils/Local.h" 37 #include "llvm/Transforms/Utils/LoopUtils.h" 38 using namespace llvm; 39 40 #define DEBUG_TYPE "loop-simplifycfg" 41 42 static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, 43 ScalarEvolution &SE) { 44 bool Changed = false; 45 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 46 // Copy blocks into a temporary array to avoid iterator invalidation issues 47 // as we remove them. 48 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks()); 49 50 for (auto &Block : Blocks) { 51 // Attempt to merge blocks in the trivial case. Don't modify blocks which 52 // belong to other loops. 53 BasicBlock *Succ = cast_or_null<BasicBlock>(Block); 54 if (!Succ) 55 continue; 56 57 BasicBlock *Pred = Succ->getSinglePredecessor(); 58 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L) 59 continue; 60 61 // Merge Succ into Pred and delete it. 62 MergeBlockIntoPredecessor(Succ, &DTU, &LI); 63 64 SE.forgetLoop(&L); 65 Changed = true; 66 } 67 68 return Changed; 69 } 70 71 PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM, 72 LoopStandardAnalysisResults &AR, 73 LPMUpdater &) { 74 if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE)) 75 return PreservedAnalyses::all(); 76 77 return getLoopPassPreservedAnalyses(); 78 } 79 80 namespace { 81 class LoopSimplifyCFGLegacyPass : public LoopPass { 82 public: 83 static char ID; // Pass ID, replacement for typeid 84 LoopSimplifyCFGLegacyPass() : LoopPass(ID) { 85 initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry()); 86 } 87 88 bool runOnLoop(Loop *L, LPPassManager &) override { 89 if (skipLoop(L)) 90 return false; 91 92 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 93 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 94 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 95 return simplifyLoopCFG(*L, DT, LI, SE); 96 } 97 98 void getAnalysisUsage(AnalysisUsage &AU) const override { 99 AU.addPreserved<DependenceAnalysisWrapperPass>(); 100 getLoopAnalysisUsage(AU); 101 } 102 }; 103 } 104 105 char LoopSimplifyCFGLegacyPass::ID = 0; 106 INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", 107 "Simplify loop CFG", false, false) 108 INITIALIZE_PASS_DEPENDENCY(LoopPass) 109 INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", 110 "Simplify loop CFG", false, false) 111 112 Pass *llvm::createLoopSimplifyCFGPass() { 113 return new LoopSimplifyCFGLegacyPass(); 114 } 115