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"
22 /// Loop unroll pass that only does full loop unrolling.
23 class LoopFullUnrollPass
: public PassInfoMixin
<LoopFullUnrollPass
> {
26 /// If false, use a cost model to determine whether unrolling of a loop is
27 /// profitable. If true, only loops that explicitly request unrolling via
28 /// metadata are considered. All other loops are skipped.
29 const bool OnlyWhenForced
;
32 explicit LoopFullUnrollPass(int OptLevel
= 2, bool OnlyWhenForced
= false)
33 : OptLevel(OptLevel
), OnlyWhenForced(OnlyWhenForced
) {}
35 PreservedAnalyses
run(Loop
&L
, LoopAnalysisManager
&AM
,
36 LoopStandardAnalysisResults
&AR
, LPMUpdater
&U
);
39 /// A set of parameters used to control various transforms performed by the
40 /// LoopUnroll pass. Each of the boolean parameters can be set to:
41 /// true - enabling the transformation.
42 /// false - disabling the transformation.
43 /// None - relying on a global default.
45 /// There is also OptLevel parameter, which is used for additional loop unroll
48 /// Intended use is to create a default object, modify parameters with
49 /// additional setters and then pass it to LoopUnrollPass.
51 struct LoopUnrollOptions
{
52 Optional
<bool> AllowPartial
;
53 Optional
<bool> AllowPeeling
;
54 Optional
<bool> AllowRuntime
;
55 Optional
<bool> AllowUpperBound
;
58 /// If false, use a cost model to determine whether unrolling of a loop is
59 /// profitable. If true, only loops that explicitly request unrolling via
60 /// metadata are considered. All other loops are skipped.
63 LoopUnrollOptions(int OptLevel
= 2, bool OnlyWhenForced
= false)
64 : OptLevel(OptLevel
), OnlyWhenForced(OnlyWhenForced
) {}
66 /// Enables or disables partial unrolling. When disabled only full unrolling
68 LoopUnrollOptions
&setPartial(bool Partial
) {
69 AllowPartial
= Partial
;
73 /// Enables or disables unrolling of loops with runtime trip count.
74 LoopUnrollOptions
&setRuntime(bool Runtime
) {
75 AllowRuntime
= Runtime
;
79 /// Enables or disables loop peeling.
80 LoopUnrollOptions
&setPeeling(bool Peeling
) {
81 AllowPeeling
= Peeling
;
85 /// Enables or disables the use of trip count upper bound
86 /// in loop unrolling.
87 LoopUnrollOptions
&setUpperBound(bool UpperBound
) {
88 AllowUpperBound
= UpperBound
;
92 // Sets "optimization level" tuning parameter for loop unrolling.
93 LoopUnrollOptions
&setOptLevel(int O
) {
99 /// Loop unroll pass that will support both full and partial unrolling.
100 /// It is a function pass to have access to function and module analyses.
101 /// It will also put loops into canonical form (simplified and LCSSA).
102 class LoopUnrollPass
: public PassInfoMixin
<LoopUnrollPass
> {
103 LoopUnrollOptions UnrollOpts
;
106 /// This uses the target information (or flags) to control the thresholds for
107 /// different unrolling stategies but supports all of them.
108 explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts
= {})
109 : UnrollOpts(UnrollOpts
) {}
111 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&AM
);
114 } // end namespace llvm
116 #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H