xref: /src/contrib/llvm-project/llvm/lib/Transforms/Scalar/LoopRotation.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1009b1c42SEd Schouten //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements Loop Rotation Pass.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
1301095a5dSDimitry Andric #include "llvm/Transforms/Scalar/LoopRotation.h"
14b60736ecSDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
15581a6d85SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
16344a3780SDimitry Andric #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
17145449b1SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
184a16efa3SDimitry Andric #include "llvm/Analysis/LoopPass.h"
19d8e91e46SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
20d8e91e46SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h"
21009b1c42SEd Schouten #include "llvm/Analysis/ScalarEvolution.h"
224a16efa3SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
23706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
24706b4fc4SDimitry Andric #include "llvm/Support/CommandLine.h"
2501095a5dSDimitry Andric #include "llvm/Transforms/Scalar.h"
26eb11fae6SDimitry Andric #include "llvm/Transforms/Utils/LoopRotationUtils.h"
2701095a5dSDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
28e3b55780SDimitry Andric #include <optional>
29009b1c42SEd Schouten using namespace llvm;
30009b1c42SEd Schouten 
315ca98fd9SDimitry Andric #define DEBUG_TYPE "loop-rotate"
325ca98fd9SDimitry Andric 
3301095a5dSDimitry Andric static cl::opt<unsigned> DefaultRotationThreshold(
3401095a5dSDimitry Andric     "rotation-max-header-size", cl::init(16), cl::Hidden,
355ca98fd9SDimitry Andric     cl::desc("The default maximum header size for automatic loop rotation"));
36009b1c42SEd Schouten 
37b60736ecSDimitry Andric static cl::opt<bool> PrepareForLTOOption(
38b60736ecSDimitry Andric     "rotation-prepare-for-lto", cl::init(false), cl::Hidden,
39b60736ecSDimitry Andric     cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
40b60736ecSDimitry Andric              "should be used for testing only."));
41b60736ecSDimitry Andric 
LoopRotatePass(bool EnableHeaderDuplication,bool PrepareForLTO)42b60736ecSDimitry Andric LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)
43b60736ecSDimitry Andric     : EnableHeaderDuplication(EnableHeaderDuplication),
44b60736ecSDimitry Andric       PrepareForLTO(PrepareForLTO) {}
4501095a5dSDimitry Andric 
printPipeline(raw_ostream & OS,function_ref<StringRef (StringRef)> MapClassName2PassName)467fa27ce4SDimitry Andric void LoopRotatePass::printPipeline(
477fa27ce4SDimitry Andric     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
487fa27ce4SDimitry Andric   static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(
497fa27ce4SDimitry Andric       OS, MapClassName2PassName);
507fa27ce4SDimitry Andric   OS << "<";
517fa27ce4SDimitry Andric   if (!EnableHeaderDuplication)
527fa27ce4SDimitry Andric     OS << "no-";
537fa27ce4SDimitry Andric   OS << "header-duplication;";
547fa27ce4SDimitry Andric 
557fa27ce4SDimitry Andric   if (!PrepareForLTO)
567fa27ce4SDimitry Andric     OS << "no-";
577fa27ce4SDimitry Andric   OS << "prepare-for-lto";
587fa27ce4SDimitry Andric   OS << ">";
597fa27ce4SDimitry Andric }
607fa27ce4SDimitry Andric 
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater &)61581a6d85SDimitry Andric PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
62581a6d85SDimitry Andric                                       LoopStandardAnalysisResults &AR,
63581a6d85SDimitry Andric                                       LPMUpdater &) {
64b60736ecSDimitry Andric   // Vectorization requires loop-rotation. Use default threshold for loops the
65b60736ecSDimitry Andric   // user explicitly marked for vectorization, even when header duplication is
66b60736ecSDimitry Andric   // disabled.
67ac9a064cSDimitry Andric   int Threshold =
68ac9a064cSDimitry Andric       (EnableHeaderDuplication && !L.getHeader()->getParent()->hasMinSize()) ||
69b60736ecSDimitry Andric               hasVectorizeTransformation(&L) == TM_ForcedByUser
70b60736ecSDimitry Andric           ? DefaultRotationThreshold
71b60736ecSDimitry Andric           : 0;
72ac9a064cSDimitry Andric   const DataLayout &DL = L.getHeader()->getDataLayout();
73a303c417SDimitry Andric   const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
7401095a5dSDimitry Andric 
75e3b55780SDimitry Andric   std::optional<MemorySSAUpdater> MSSAU;
76d8e91e46SDimitry Andric   if (AR.MSSA)
77d8e91e46SDimitry Andric     MSSAU = MemorySSAUpdater(AR.MSSA);
78e3b55780SDimitry Andric   bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
79e3b55780SDimitry Andric                               MSSAU ? &*MSSAU : nullptr, SQ, false, Threshold,
80145449b1SDimitry Andric                               false, PrepareForLTO || PrepareForLTOOption);
81eb11fae6SDimitry Andric 
8201095a5dSDimitry Andric   if (!Changed)
8301095a5dSDimitry Andric     return PreservedAnalyses::all();
8471d5a254SDimitry Andric 
85d8e91e46SDimitry Andric   if (AR.MSSA && VerifyMemorySSA)
86d8e91e46SDimitry Andric     AR.MSSA->verifyMemorySSA();
87d8e91e46SDimitry Andric 
88e6d15924SDimitry Andric   auto PA = getLoopPassPreservedAnalyses();
891d5ae102SDimitry Andric   if (AR.MSSA)
90e6d15924SDimitry Andric     PA.preserve<MemorySSAAnalysis>();
91e6d15924SDimitry Andric   return PA;
9201095a5dSDimitry Andric }
93