Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:42

0001 //===- LoopUnrollPass.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
0010 #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
0011 
0012 #include "llvm/Analysis/LoopAnalysisManager.h"
0013 #include "llvm/IR/PassManager.h"
0014 #include "llvm/Support/CommandLine.h"
0015 #include <optional>
0016 
0017 namespace llvm {
0018 
0019 extern cl::opt<bool> ForgetSCEVInLoopUnroll;
0020 
0021 class Function;
0022 class Loop;
0023 class LPMUpdater;
0024 
0025 /// Loop unroll pass that only does full loop unrolling and peeling.
0026 class LoopFullUnrollPass : public PassInfoMixin<LoopFullUnrollPass> {
0027   const int OptLevel;
0028 
0029   /// If false, use a cost model to determine whether unrolling of a loop is
0030   /// profitable. If true, only loops that explicitly request unrolling via
0031   /// metadata are considered. All other loops are skipped.
0032   const bool OnlyWhenForced;
0033 
0034   /// If true, forget all loops when unrolling. If false, forget top-most loop
0035   /// of the currently processed loops, which removes one entry at a time from
0036   /// the internal SCEV records. For large loops, the former is faster.
0037   const bool ForgetSCEV;
0038 
0039 public:
0040   explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false,
0041                               bool ForgetSCEV = false)
0042       : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
0043         ForgetSCEV(ForgetSCEV) {}
0044 
0045   PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
0046                         LoopStandardAnalysisResults &AR, LPMUpdater &U);
0047 };
0048 
0049 /// A set of parameters used to control various transforms performed by the
0050 /// LoopUnroll pass. Each of the boolean parameters can be set to:
0051 ///      true - enabling the transformation.
0052 ///      false - disabling the transformation.
0053 ///      None - relying on a global default.
0054 ///
0055 /// There is also OptLevel parameter, which is used for additional loop unroll
0056 /// tuning.
0057 ///
0058 /// Intended use is to create a default object, modify parameters with
0059 /// additional setters and then pass it to LoopUnrollPass.
0060 ///
0061 struct LoopUnrollOptions {
0062   std::optional<bool> AllowPartial;
0063   std::optional<bool> AllowPeeling;
0064   std::optional<bool> AllowRuntime;
0065   std::optional<bool> AllowUpperBound;
0066   std::optional<bool> AllowProfileBasedPeeling;
0067   std::optional<unsigned> FullUnrollMaxCount;
0068   int OptLevel;
0069 
0070   /// If false, use a cost model to determine whether unrolling of a loop is
0071   /// profitable. If true, only loops that explicitly request unrolling via
0072   /// metadata are considered. All other loops are skipped.
0073   bool OnlyWhenForced;
0074 
0075   /// If true, forget all loops when unrolling. If false, forget top-most loop
0076   /// of the currently processed loops, which removes one entry at a time from
0077   /// the internal SCEV records. For large loops, the former is faster.
0078   const bool ForgetSCEV;
0079 
0080   LoopUnrollOptions(int OptLevel = 2, bool OnlyWhenForced = false,
0081                     bool ForgetSCEV = false)
0082       : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
0083         ForgetSCEV(ForgetSCEV) {}
0084 
0085   /// Enables or disables partial unrolling. When disabled only full unrolling
0086   /// is allowed.
0087   LoopUnrollOptions &setPartial(bool Partial) {
0088     AllowPartial = Partial;
0089     return *this;
0090   }
0091 
0092   /// Enables or disables unrolling of loops with runtime trip count.
0093   LoopUnrollOptions &setRuntime(bool Runtime) {
0094     AllowRuntime = Runtime;
0095     return *this;
0096   }
0097 
0098   /// Enables or disables loop peeling.
0099   LoopUnrollOptions &setPeeling(bool Peeling) {
0100     AllowPeeling = Peeling;
0101     return *this;
0102   }
0103 
0104   /// Enables or disables the use of trip count upper bound
0105   /// in loop unrolling.
0106   LoopUnrollOptions &setUpperBound(bool UpperBound) {
0107     AllowUpperBound = UpperBound;
0108     return *this;
0109   }
0110 
0111   // Sets "optimization level" tuning parameter for loop unrolling.
0112   LoopUnrollOptions &setOptLevel(int O) {
0113     OptLevel = O;
0114     return *this;
0115   }
0116 
0117   // Enables or disables loop peeling basing on profile.
0118   LoopUnrollOptions &setProfileBasedPeeling(int O) {
0119     AllowProfileBasedPeeling = O;
0120     return *this;
0121   }
0122 
0123   // Sets the max full unroll count.
0124   LoopUnrollOptions &setFullUnrollMaxCount(unsigned O) {
0125     FullUnrollMaxCount = O;
0126     return *this;
0127   }
0128 };
0129 
0130 /// Loop unroll pass that will support both full and partial unrolling.
0131 /// It is a function pass to have access to function and module analyses.
0132 /// It will also put loops into canonical form (simplified and LCSSA).
0133 class LoopUnrollPass : public PassInfoMixin<LoopUnrollPass> {
0134   LoopUnrollOptions UnrollOpts;
0135 
0136 public:
0137   /// This uses the target information (or flags) to control the thresholds for
0138   /// different unrolling stategies but supports all of them.
0139   explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
0140       : UnrollOpts(UnrollOpts) {}
0141 
0142   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0143   void printPipeline(raw_ostream &OS,
0144                      function_ref<StringRef(StringRef)> MapClassName2PassName);
0145 };
0146 
0147 } // end namespace llvm
0148 
0149 #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H