1 //===- LoopUnrollPass.h -----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H 11 #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H 12 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/Analysis/LoopAnalysisManager.h" 15 #include "llvm/IR/PassManager.h" 16 17 namespace llvm { 18 19 class Function; 20 class Loop; 21 class LPMUpdater; 22 23 /// Loop unroll pass that only does full loop unrolling. 24 class LoopFullUnrollPass : public PassInfoMixin<LoopFullUnrollPass> { 25 const int OptLevel; 26 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; 31 32 public: 33 explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false) OptLevel(OptLevel)34 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced) {} 35 36 PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, 37 LoopStandardAnalysisResults &AR, LPMUpdater &U); 38 }; 39 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. 45 /// 46 /// There is also OptLevel parameter, which is used for additional loop unroll 47 /// tuning. 48 /// 49 /// Intended use is to create a default object, modify parameters with 50 /// additional setters and then pass it to LoopUnrollPass. 51 /// 52 struct LoopUnrollOptions { 53 Optional<bool> AllowPartial; 54 Optional<bool> AllowPeeling; 55 Optional<bool> AllowRuntime; 56 Optional<bool> AllowUpperBound; 57 int OptLevel; 58 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. 62 bool OnlyWhenForced; 63 64 LoopUnrollOptions(int OptLevel = 2, bool OnlyWhenForced = false) OptLevelLoopUnrollOptions65 : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced) {} 66 67 /// Enables or disables partial unrolling. When disabled only full unrolling 68 /// is allowed. setPartialLoopUnrollOptions69 LoopUnrollOptions &setPartial(bool Partial) { 70 AllowPartial = Partial; 71 return *this; 72 } 73 74 /// Enables or disables unrolling of loops with runtime trip count. setRuntimeLoopUnrollOptions75 LoopUnrollOptions &setRuntime(bool Runtime) { 76 AllowRuntime = Runtime; 77 return *this; 78 } 79 80 /// Enables or disables loop peeling. setPeelingLoopUnrollOptions81 LoopUnrollOptions &setPeeling(bool Peeling) { 82 AllowPeeling = Peeling; 83 return *this; 84 } 85 86 /// Enables or disables the use of trip count upper bound 87 /// in loop unrolling. setUpperBoundLoopUnrollOptions88 LoopUnrollOptions &setUpperBound(bool UpperBound) { 89 AllowUpperBound = UpperBound; 90 return *this; 91 } 92 93 // Sets "optimization level" tuning parameter for loop unrolling. setOptLevelLoopUnrollOptions94 LoopUnrollOptions &setOptLevel(int O) { 95 OptLevel = O; 96 return *this; 97 } 98 }; 99 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; 105 106 public: 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 = {}) UnrollOpts(UnrollOpts)110 : UnrollOpts(UnrollOpts) {} 111 112 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 113 }; 114 115 } // end namespace llvm 116 117 #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H 118