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