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