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