1 //===- LoopUnrollPass.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
11 #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Analysis/LoopAnalysisManager.h"
15 #include "llvm/IR/PassManager.h"
23 /// Loop unroll pass that only does full loop unrolling.
24 class LoopFullUnrollPass : public PassInfoMixin<LoopFullUnrollPass> {
27 /// If false, use a cost model to determine whether unrolling of a loop is
28 /// profitable. If true, only loops that explicitly request unrolling via
29 /// metadata are considered. All other loops are skipped.
30 const bool OnlyWhenForced;
33 explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false)
34 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced) {}
36 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
37 LoopStandardAnalysisResults &AR, LPMUpdater &U);
40 /// A set of parameters used to control various transforms performed by the
41 /// LoopUnroll pass. Each of the boolean parameters can be set to:
42 /// true - enabling the transformation.
43 /// false - disabling the transformation.
44 /// None - relying on a global default.
46 /// There is also OptLevel parameter, which is used for additional loop unroll
49 /// Intended use is to create a default object, modify parameters with
50 /// additional setters and then pass it to LoopUnrollPass.
52 struct LoopUnrollOptions {
53 Optional<bool> AllowPartial;
54 Optional<bool> AllowPeeling;
55 Optional<bool> AllowRuntime;
56 Optional<bool> AllowUpperBound;
59 /// If false, use a cost model to determine whether unrolling of a loop is
60 /// profitable. If true, only loops that explicitly request unrolling via
61 /// metadata are considered. All other loops are skipped.
64 LoopUnrollOptions(int OptLevel = 2, bool OnlyWhenForced = false)
65 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced) {}
67 /// Enables or disables partial unrolling. When disabled only full unrolling
69 LoopUnrollOptions &setPartial(bool Partial) {
70 AllowPartial = Partial;
74 /// Enables or disables unrolling of loops with runtime trip count.
75 LoopUnrollOptions &setRuntime(bool Runtime) {
76 AllowRuntime = Runtime;
80 /// Enables or disables loop peeling.
81 LoopUnrollOptions &setPeeling(bool Peeling) {
82 AllowPeeling = Peeling;
86 /// Enables or disables the use of trip count upper bound
87 /// in loop unrolling.
88 LoopUnrollOptions &setUpperBound(bool UpperBound) {
89 AllowUpperBound = UpperBound;
93 // Sets "optimization level" tuning parameter for loop unrolling.
94 LoopUnrollOptions &setOptLevel(int O) {
100 /// Loop unroll pass that will support both full and partial unrolling.
101 /// It is a function pass to have access to function and module analyses.
102 /// It will also put loops into canonical form (simplified and LCSSA).
103 class LoopUnrollPass : public PassInfoMixin<LoopUnrollPass> {
104 LoopUnrollOptions UnrollOpts;
107 /// This uses the target information (or flags) to control the thresholds for
108 /// different unrolling stategies but supports all of them.
109 explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
110 : UnrollOpts(UnrollOpts) {}
112 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
115 } // end namespace llvm
117 #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H