1 //===- LoopRotation.cpp - Loop Rotation 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 Loop Rotation Pass. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Scalar/LoopRotation.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/Analysis/InstructionSimplify.h" 17 #include "llvm/Analysis/LoopPass.h" 18 #include "llvm/Analysis/MemorySSA.h" 19 #include "llvm/Analysis/MemorySSAUpdater.h" 20 #include "llvm/Analysis/ScalarEvolution.h" 21 #include "llvm/Analysis/TargetTransformInfo.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Transforms/Scalar.h" 24 #include "llvm/Transforms/Scalar/LoopPassManager.h" 25 #include "llvm/Transforms/Utils/LoopRotationUtils.h" 26 #include "llvm/Transforms/Utils/LoopUtils.h" 27 using namespace llvm; 28 29 #define DEBUG_TYPE "loop-rotate" 30 31 static cl::opt<unsigned> DefaultRotationThreshold( 32 "rotation-max-header-size", cl::init(16), cl::Hidden, 33 cl::desc("The default maximum header size for automatic loop rotation")); 34 35 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication) 36 : EnableHeaderDuplication(EnableHeaderDuplication) {} 37 38 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM, 39 LoopStandardAnalysisResults &AR, 40 LPMUpdater &) { 41 int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0; 42 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout(); 43 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL); 44 45 Optional<MemorySSAUpdater> MSSAU; 46 if (AR.MSSA) 47 MSSAU = MemorySSAUpdater(AR.MSSA); 48 bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE, 49 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, 50 SQ, false, Threshold, false); 51 52 if (!Changed) 53 return PreservedAnalyses::all(); 54 55 if (AR.MSSA && VerifyMemorySSA) 56 AR.MSSA->verifyMemorySSA(); 57 58 return getLoopPassPreservedAnalyses(); 59 } 60 61 namespace { 62 63 class LoopRotateLegacyPass : public LoopPass { 64 unsigned MaxHeaderSize; 65 66 public: 67 static char ID; // Pass ID, replacement for typeid 68 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) { 69 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry()); 70 if (SpecifiedMaxHeaderSize == -1) 71 MaxHeaderSize = DefaultRotationThreshold; 72 else 73 MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize); 74 } 75 76 // LCSSA form makes instruction renaming easier. 77 void getAnalysisUsage(AnalysisUsage &AU) const override { 78 AU.addRequired<AssumptionCacheTracker>(); 79 AU.addRequired<TargetTransformInfoWrapperPass>(); 80 if (EnableMSSALoopDependency) { 81 AU.addRequired<MemorySSAWrapperPass>(); 82 AU.addPreserved<MemorySSAWrapperPass>(); 83 } 84 getLoopAnalysisUsage(AU); 85 } 86 87 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 88 if (skipLoop(L)) 89 return false; 90 Function &F = *L->getHeader()->getParent(); 91 92 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 93 const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 94 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 95 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 96 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; 97 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); 98 auto *SE = SEWP ? &SEWP->getSE() : nullptr; 99 const SimplifyQuery SQ = getBestSimplifyQuery(*this, F); 100 Optional<MemorySSAUpdater> MSSAU; 101 if (EnableMSSALoopDependency) { 102 MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); 103 MSSAU = MemorySSAUpdater(MSSA); 104 } 105 return LoopRotation(L, LI, TTI, AC, DT, SE, 106 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ, 107 false, MaxHeaderSize, false); 108 } 109 }; 110 } 111 112 char LoopRotateLegacyPass::ID = 0; 113 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", 114 false, false) 115 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 116 INITIALIZE_PASS_DEPENDENCY(LoopPass) 117 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 118 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 119 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false, 120 false) 121 122 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) { 123 return new LoopRotateLegacyPass(MaxHeaderSize); 124 } 125