LLVM 24.0.0git
LoopUnrollPass.h
Go to the documentation of this file.
1//===- LoopUnrollPass.h -----------------------------------------*- C++ -*-===//
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#ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
10#define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
11
13#include "llvm/IR/PassManager.h"
14#include <optional>
15
16namespace llvm {
17
18namespace cl {
20} // namespace cl
21
22/// Return -forget-scev-loop-unroll.
23LLVM_ABI bool
25
26class Function;
27class Loop;
28class LPMUpdater;
29
30/// Loop unroll pass that only does full loop unrolling and peeling.
31class LoopFullUnrollPass : public OptionalPassInfoMixin<LoopFullUnrollPass> {
32 const int OptLevel;
33
34 /// If false, use a cost model to determine whether unrolling of a loop is
35 /// profitable. If true, only loops that explicitly request unrolling via
36 /// metadata are considered. All other loops are skipped.
37 const bool OnlyWhenForced;
38
39 /// If true, forget all loops when unrolling. If false, forget top-most loop
40 /// of the currently processed loops, which removes one entry at a time from
41 /// the internal SCEV records. For large loops, the former is faster.
42 const bool ForgetSCEV;
43
44 /// If true, consider calls as inline candidates and defer unrolling so that
45 /// LTO post-link inlining can consider them first.
46 const bool PrepareForLTO;
47
48public:
49 explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false,
50 bool ForgetSCEV = false,
51 bool PrepareForLTO = false)
52 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
53 ForgetSCEV(ForgetSCEV), PrepareForLTO(PrepareForLTO) {}
54
57 LPMUpdater &U);
58};
59
60/// A set of parameters used to control various transforms performed by the
61/// LoopUnroll pass. Each of the boolean parameters can be set to:
62/// true - enabling the transformation.
63/// false - disabling the transformation.
64/// None - relying on a global default.
65///
66/// There is also OptLevel parameter, which is used for additional loop unroll
67/// tuning.
68///
69/// Intended use is to create a default object, modify parameters with
70/// additional setters and then pass it to LoopUnrollPass.
71///
73 std::optional<bool> AllowPartial;
74 std::optional<bool> AllowPeeling;
75 std::optional<bool> AllowRuntime;
76 std::optional<bool> AllowUpperBound;
77 std::optional<bool> AllowProfileBasedPeeling;
78 std::optional<unsigned> FullUnrollMaxCount;
80
81 /// If false, use a cost model to determine whether unrolling of a loop is
82 /// profitable. If true, only loops that explicitly request unrolling via
83 /// metadata are considered. All other loops are skipped.
85
86 /// If true, forget all loops when unrolling. If false, forget top-most loop
87 /// of the currently processed loops, which removes one entry at a time from
88 /// the internal SCEV records. For large loops, the former is faster.
89 const bool ForgetSCEV;
90
91 /// If true, consider calls as inline candidates and defer unrolling so that
92 /// LTO post-link inlining can consider them first.
93 bool PrepareForLTO = false;
94
99
100 /// Enables or disables partial unrolling. When disabled only full unrolling
101 /// is allowed.
103 AllowPartial = Partial;
104 return *this;
105 }
106
107 /// Enables or disables unrolling of loops with runtime trip count.
110 return *this;
111 }
112
113 /// Enables or disables loop peeling.
115 AllowPeeling = Peeling;
116 return *this;
117 }
118
119 /// Enables or disables the use of trip count upper bound
120 /// in loop unrolling.
122 AllowUpperBound = UpperBound;
123 return *this;
124 }
125
126 // Sets "optimization level" tuning parameter for loop unrolling.
128 OptLevel = O;
129 return *this;
130 }
131
132 // Enables or disables loop peeling basing on profile.
135 return *this;
136 }
137
138 // Sets the max full unroll count.
141 return *this;
142 }
143
146 return *this;
147 }
148};
149
150/// Loop unroll pass that will support both full and partial unrolling.
151/// It is a function pass to have access to function and module analyses.
152/// It will also put loops into canonical form (simplified and LCSSA).
153class LoopUnrollPass : public OptionalPassInfoMixin<LoopUnrollPass> {
154 LoopUnrollOptions UnrollOpts;
155
156public:
157 /// This uses the target information (or flags) to control the thresholds for
158 /// different unrolling stategies but supports all of them.
159 explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
160 : UnrollOpts(UnrollOpts) {}
161
162 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
163 LLVM_ABI void
164 printPipeline(raw_ostream &OS,
165 function_ref<StringRef(StringRef)> MapClassName2PassName);
166};
167
168} // end namespace llvm
169
170#endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition MD5.cpp:54
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
LoopFullUnrollPass(int OptLevel=2, bool OnlyWhenForced=false, bool ForgetSCEV=false, bool PrepareForLTO=false)
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
LoopUnrollPass(LoopUnrollOptions UnrollOpts={})
This uses the target information (or flags) to control the thresholds for different unrolling stategi...
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This namespace contains all of the command line option processing machinery.
This is an optimization pass for GlobalISel generic memory operations.
@ Runtime
Detect stack use after return if not disabled runtime with (ASAN_OPTIONS=detect_stack_use_after_retur...
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
LLVM_ABI bool getForgetSCEVInLoopUnroll(const cl::LibraryOptionsContext &LibraryOptions)
Return -forget-scev-loop-unroll.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
A set of parameters used to control various transforms performed by the LoopUnroll pass.
LoopUnrollOptions & setPeeling(bool Peeling)
Enables or disables loop peeling.
bool OnlyWhenForced
If false, use a cost model to determine whether unrolling of a loop is profitable.
LoopUnrollOptions & setOptLevel(int O)
const bool ForgetSCEV
If true, forget all loops when unrolling.
std::optional< unsigned > FullUnrollMaxCount
std::optional< bool > AllowPartial
std::optional< bool > AllowRuntime
bool PrepareForLTO
If true, consider calls as inline candidates and defer unrolling so that LTO post-link inlining can c...
LoopUnrollOptions(int OptLevel=2, bool OnlyWhenForced=false, bool ForgetSCEV=false)
LoopUnrollOptions & setPartial(bool Partial)
Enables or disables partial unrolling.
std::optional< bool > AllowProfileBasedPeeling
std::optional< bool > AllowPeeling
LoopUnrollOptions & setFullUnrollMaxCount(unsigned O)
LoopUnrollOptions & setPrepareForLTO(bool V)
LoopUnrollOptions & setUpperBound(bool UpperBound)
Enables or disables the use of trip count upper bound in loop unrolling.
std::optional< bool > AllowUpperBound
LoopUnrollOptions & setRuntime(bool Runtime)
Enables or disables unrolling of loops with runtime trip count.
LoopUnrollOptions & setProfileBasedPeeling(int O)
A CRTP mix-in for passes that can be skipped.