1 //===- LoopUnrollPass.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
10 #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/Analysis/LoopAnalysisManager.h"
14 #include "llvm/IR/PassManager.h"
15 #include "llvm/Support/CommandLine.h"
19 extern cl::opt<bool> ForgetSCEVInLoopUnroll;
25 /// Loop unroll pass that only does full loop unrolling.
26 class LoopFullUnrollPass : public PassInfoMixin<LoopFullUnrollPass> {
29 /// If false, use a cost model to determine whether unrolling of a loop is
30 /// profitable. If true, only loops that explicitly request unrolling via
31 /// metadata are considered. All other loops are skipped.
32 const bool OnlyWhenForced;
34 /// If true, forget all loops when unrolling. If false, forget top-most loop
35 /// of the currently processed loops, which removes one entry at a time from
36 /// the internal SCEV records. For large loops, the former is faster.
37 const bool ForgetSCEV;
40 explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false,
41 bool ForgetSCEV = false)
42 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
43 ForgetSCEV(ForgetSCEV) {}
45 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
46 LoopStandardAnalysisResults &AR, LPMUpdater &U);
49 /// A set of parameters used to control various transforms performed by the
50 /// LoopUnroll pass. Each of the boolean parameters can be set to:
51 /// true - enabling the transformation.
52 /// false - disabling the transformation.
53 /// None - relying on a global default.
55 /// There is also OptLevel parameter, which is used for additional loop unroll
58 /// Intended use is to create a default object, modify parameters with
59 /// additional setters and then pass it to LoopUnrollPass.
61 struct LoopUnrollOptions {
62 Optional<bool> AllowPartial;
63 Optional<bool> AllowPeeling;
64 Optional<bool> AllowRuntime;
65 Optional<bool> AllowUpperBound;
66 Optional<bool> AllowProfileBasedPeeling;
67 Optional<unsigned> FullUnrollMaxCount;
70 /// If false, use a cost model to determine whether unrolling of a loop is
71 /// profitable. If true, only loops that explicitly request unrolling via
72 /// metadata are considered. All other loops are skipped.
75 /// If true, forget all loops when unrolling. If false, forget top-most loop
76 /// of the currently processed loops, which removes one entry at a time from
77 /// the internal SCEV records. For large loops, the former is faster.
78 const bool ForgetSCEV;
80 LoopUnrollOptions(int OptLevel = 2, bool OnlyWhenForced = false,
81 bool ForgetSCEV = false)
82 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
83 ForgetSCEV(ForgetSCEV) {}
85 /// Enables or disables partial unrolling. When disabled only full unrolling
87 LoopUnrollOptions &setPartial(bool Partial) {
88 AllowPartial = Partial;
92 /// Enables or disables unrolling of loops with runtime trip count.
93 LoopUnrollOptions &setRuntime(bool Runtime) {
94 AllowRuntime = Runtime;
98 /// Enables or disables loop peeling.
99 LoopUnrollOptions &setPeeling(bool Peeling) {
100 AllowPeeling = Peeling;
104 /// Enables or disables the use of trip count upper bound
105 /// in loop unrolling.
106 LoopUnrollOptions &setUpperBound(bool UpperBound) {
107 AllowUpperBound = UpperBound;
111 // Sets "optimization level" tuning parameter for loop unrolling.
112 LoopUnrollOptions &setOptLevel(int O) {
117 // Enables or disables loop peeling basing on profile.
118 LoopUnrollOptions &setProfileBasedPeeling(int O) {
119 AllowProfileBasedPeeling = O;
123 // Sets the max full unroll count.
124 LoopUnrollOptions &setFullUnrollMaxCount(unsigned O) {
125 FullUnrollMaxCount = O;
130 /// Loop unroll pass that will support both full and partial unrolling.
131 /// It is a function pass to have access to function and module analyses.
132 /// It will also put loops into canonical form (simplified and LCSSA).
133 class LoopUnrollPass : public PassInfoMixin<LoopUnrollPass> {
134 LoopUnrollOptions UnrollOpts;
137 /// This uses the target information (or flags) to control the thresholds for
138 /// different unrolling stategies but supports all of them.
139 explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
140 : UnrollOpts(UnrollOpts) {}
142 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
145 } // end namespace llvm
147 #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H