LLVM 23.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"
15#include <optional>
16
17namespace llvm {
18
20
21class Function;
22class Loop;
23class LPMUpdater;
24
25/// Loop unroll pass that only does full loop unrolling and peeling.
26class LoopFullUnrollPass : public OptionalPassInfoMixin<LoopFullUnrollPass> {
27 const int OptLevel;
28
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;
33
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;
38
39public:
40 explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false,
41 bool ForgetSCEV = false)
42 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
43 ForgetSCEV(ForgetSCEV) {}
44
47 LPMUpdater &U);
48};
49
50/// A set of parameters used to control various transforms performed by the
51/// LoopUnroll pass. Each of the boolean parameters can be set to:
52/// true - enabling the transformation.
53/// false - disabling the transformation.
54/// None - relying on a global default.
55///
56/// There is also OptLevel parameter, which is used for additional loop unroll
57/// tuning.
58///
59/// Intended use is to create a default object, modify parameters with
60/// additional setters and then pass it to LoopUnrollPass.
61///
63 std::optional<bool> AllowPartial;
64 std::optional<bool> AllowPeeling;
65 std::optional<bool> AllowRuntime;
66 std::optional<bool> AllowUpperBound;
67 std::optional<bool> AllowProfileBasedPeeling;
68 std::optional<unsigned> FullUnrollMaxCount;
70
71 /// If false, use a cost model to determine whether unrolling of a loop is
72 /// profitable. If true, only loops that explicitly request unrolling via
73 /// metadata are considered. All other loops are skipped.
75
76 /// If true, forget all loops when unrolling. If false, forget top-most loop
77 /// of the currently processed loops, which removes one entry at a time from
78 /// the internal SCEV records. For large loops, the former is faster.
79 const bool ForgetSCEV;
80
85
86 /// Enables or disables partial unrolling. When disabled only full unrolling
87 /// is allowed.
89 AllowPartial = Partial;
90 return *this;
91 }
92
93 /// Enables or disables unrolling of loops with runtime trip count.
96 return *this;
97 }
98
99 /// Enables or disables loop peeling.
101 AllowPeeling = Peeling;
102 return *this;
103 }
104
105 /// Enables or disables the use of trip count upper bound
106 /// in loop unrolling.
108 AllowUpperBound = UpperBound;
109 return *this;
110 }
111
112 // Sets "optimization level" tuning parameter for loop unrolling.
114 OptLevel = O;
115 return *this;
116 }
117
118 // Enables or disables loop peeling basing on profile.
121 return *this;
122 }
123
124 // Sets the max full unroll count.
127 return *this;
128 }
129};
130
131/// Loop unroll pass that will support both full and partial unrolling.
132/// It is a function pass to have access to function and module analyses.
133/// It will also put loops into canonical form (simplified and LCSSA).
134class LoopUnrollPass : public OptionalPassInfoMixin<LoopUnrollPass> {
135 LoopUnrollOptions UnrollOpts;
136
137public:
138 /// This uses the target information (or flags) to control the thresholds for
139 /// different unrolling stategies but supports all of them.
140 explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
141 : UnrollOpts(UnrollOpts) {}
142
143 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
144 LLVM_ABI void
145 printPipeline(raw_ostream &OS,
146 function_ref<StringRef(StringRef)> MapClassName2PassName);
147};
148
149} // end namespace llvm
150
151#endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
#define LLVM_ABI
Definition Compiler.h:213
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)
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 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 cl::opt< bool > ForgetSCEVInLoopUnroll
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
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 & 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.