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/Analysis/AssumptionCache.h" |
15 | #include "llvm/Analysis/InstructionSimplify.h" |
16 | #include "llvm/Analysis/LazyBlockFrequencyInfo.h" |
17 | #include "llvm/Analysis/LoopInfo.h" |
18 | #include "llvm/Analysis/LoopPass.h" |
19 | #include "llvm/Analysis/MemorySSA.h" |
20 | #include "llvm/Analysis/MemorySSAUpdater.h" |
21 | #include "llvm/Analysis/ScalarEvolution.h" |
22 | #include "llvm/Analysis/TargetTransformInfo.h" |
23 | #include "llvm/InitializePasses.h" |
24 | #include "llvm/Support/CommandLine.h" |
25 | #include "llvm/Transforms/Scalar.h" |
26 | #include "llvm/Transforms/Utils/LoopRotationUtils.h" |
27 | #include "llvm/Transforms/Utils/LoopUtils.h" |
28 | #include <optional> |
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(Val: 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(Val: 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 , bool PrepareForLTO) |
43 | : EnableHeaderDuplication(EnableHeaderDuplication), |
44 | PrepareForLTO(PrepareForLTO) {} |
45 | |
46 | void LoopRotatePass::printPipeline( |
47 | raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { |
48 | static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline( |
49 | OS, MapClassName2PassName); |
50 | OS << "<" ; |
51 | if (!EnableHeaderDuplication) |
52 | OS << "no-" ; |
53 | OS << "header-duplication;" ; |
54 | |
55 | if (!PrepareForLTO) |
56 | OS << "no-" ; |
57 | OS << "prepare-for-lto" ; |
58 | OS << ">" ; |
59 | } |
60 | |
61 | PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM, |
62 | LoopStandardAnalysisResults &AR, |
63 | LPMUpdater &) { |
64 | // Vectorization requires loop-rotation. Use default threshold for loops the |
65 | // user explicitly marked for vectorization, even when header duplication is |
66 | // disabled. |
67 | int Threshold = |
68 | (EnableHeaderDuplication && !L.getHeader()->getParent()->hasMinSize()) || |
69 | hasVectorizeTransformation(L: &L) == TM_ForcedByUser |
70 | ? DefaultRotationThreshold |
71 | : 0; |
72 | const DataLayout &DL = L.getHeader()->getDataLayout(); |
73 | const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL); |
74 | |
75 | std::optional<MemorySSAUpdater> MSSAU; |
76 | if (AR.MSSA) |
77 | MSSAU = MemorySSAUpdater(AR.MSSA); |
78 | bool Changed = LoopRotation(L: &L, LI: &AR.LI, TTI: &AR.TTI, AC: &AR.AC, DT: &AR.DT, SE: &AR.SE, |
79 | MSSAU: MSSAU ? &*MSSAU : nullptr, SQ, RotationOnly: false, Threshold, |
80 | IsUtilMode: false, PrepareForLTO: PrepareForLTO || PrepareForLTOOption); |
81 | |
82 | if (!Changed) |
83 | return PreservedAnalyses::all(); |
84 | |
85 | if (AR.MSSA && VerifyMemorySSA) |
86 | AR.MSSA->verifyMemorySSA(); |
87 | |
88 | auto PA = getLoopPassPreservedAnalyses(); |
89 | if (AR.MSSA) |
90 | PA.preserve<MemorySSAAnalysis>(); |
91 | return PA; |
92 | } |
93 | |