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/AssumptionCache.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/InitializePasses.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Transforms/Scalar.h" 26 #include "llvm/Transforms/Scalar/LoopPassManager.h" 27 #include "llvm/Transforms/Utils/LoopRotationUtils.h" 28 #include "llvm/Transforms/Utils/LoopUtils.h" 29 using namespace llvm; 30 31 #define DEBUG_TYPE "loop-rotate" 32 33 static cl::opt<unsigned> DefaultRotationThreshold( 34 "rotation-max-header-size", cl::init(16), cl::Hidden, 35 cl::desc("The default maximum header size for automatic loop rotation")); 36 37 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication) 38 : EnableHeaderDuplication(EnableHeaderDuplication) {} 39 40 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM, 41 LoopStandardAnalysisResults &AR, 42 LPMUpdater &) { 43 // Vectorization requires loop-rotation. Use default threshold for loops the 44 // user explicitly marked for vectorization, even when header duplication is 45 // disabled. 46 int Threshold = EnableHeaderDuplication || 47 hasVectorizeTransformation(&L) == TM_ForcedByUser 48 ? DefaultRotationThreshold 49 : 0; 50 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout(); 51 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL); 52 53 Optional<MemorySSAUpdater> MSSAU; 54 if (AR.MSSA) 55 MSSAU = MemorySSAUpdater(AR.MSSA); 56 bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE, 57 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, 58 SQ, false, Threshold, false); 59 60 if (!Changed) 61 return PreservedAnalyses::all(); 62 63 if (AR.MSSA && VerifyMemorySSA) 64 AR.MSSA->verifyMemorySSA(); 65 66 auto PA = getLoopPassPreservedAnalyses(); 67 if (AR.MSSA) 68 PA.preserve<MemorySSAAnalysis>(); 69 return PA; 70 } 71 72 namespace { 73 74 class LoopRotateLegacyPass : public LoopPass { 75 unsigned MaxHeaderSize; 76 77 public: 78 static char ID; // Pass ID, replacement for typeid 79 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) { 80 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry()); 81 if (SpecifiedMaxHeaderSize == -1) 82 MaxHeaderSize = DefaultRotationThreshold; 83 else 84 MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize); 85 } 86 87 // LCSSA form makes instruction renaming easier. 88 void getAnalysisUsage(AnalysisUsage &AU) const override { 89 AU.addRequired<AssumptionCacheTracker>(); 90 AU.addRequired<TargetTransformInfoWrapperPass>(); 91 if (EnableMSSALoopDependency) 92 AU.addPreserved<MemorySSAWrapperPass>(); 93 getLoopAnalysisUsage(AU); 94 } 95 96 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 97 if (skipLoop(L)) 98 return false; 99 Function &F = *L->getHeader()->getParent(); 100 101 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 102 const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 103 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 104 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 105 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 106 const SimplifyQuery SQ = getBestSimplifyQuery(*this, F); 107 Optional<MemorySSAUpdater> MSSAU; 108 if (EnableMSSALoopDependency) { 109 // Not requiring MemorySSA and getting it only if available will split 110 // the loop pass pipeline when LoopRotate is being run first. 111 auto *MSSAA = getAnalysisIfAvailable<MemorySSAWrapperPass>(); 112 if (MSSAA) 113 MSSAU = MemorySSAUpdater(&MSSAA->getMSSA()); 114 } 115 // Vectorization requires loop-rotation. Use default threshold for loops the 116 // user explicitly marked for vectorization, even when header duplication is 117 // disabled. 118 int Threshold = hasVectorizeTransformation(L) == TM_ForcedByUser 119 ? DefaultRotationThreshold 120 : MaxHeaderSize; 121 122 return LoopRotation(L, LI, TTI, AC, &DT, &SE, 123 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ, 124 false, Threshold, false); 125 } 126 }; 127 } // end namespace 128 129 char LoopRotateLegacyPass::ID = 0; 130 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", 131 false, false) 132 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 133 INITIALIZE_PASS_DEPENDENCY(LoopPass) 134 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 135 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 136 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false, 137 false) 138 139 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) { 140 return new LoopRotateLegacyPass(MaxHeaderSize); 141 } 142