Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- ExtraFunctionPassManager.h - Run Optimizations on Demand -*- 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 /// \file
0009 ///
0010 /// This file provides a pass manager that only runs its passes if the
0011 /// provided marker analysis has been preserved, together with a class to
0012 /// define such a marker analysis.
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
0016 #define LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
0017 
0018 #include "llvm/IR/PassManager.h"
0019 #include "llvm/Transforms/Scalar/LoopPassManager.h"
0020 
0021 namespace llvm {
0022 
0023 /// A marker analysis to determine if extra passes should be run on demand.
0024 /// Passes requesting extra transformations to run need to request and preserve
0025 /// this analysis.
0026 template <typename MarkerTy> struct ShouldRunExtraPasses {
0027   struct Result {
0028     bool invalidate(Function &F, const PreservedAnalyses &PA,
0029                     FunctionAnalysisManager::Invalidator &) {
0030       // Check whether the analysis has been explicitly invalidated. Otherwise,
0031       // it remains preserved.
0032       auto PAC = PA.getChecker<MarkerTy>();
0033       return !PAC.preservedWhenStateless();
0034     }
0035 
0036     bool invalidate(Loop &L, const PreservedAnalyses &PA,
0037                     LoopAnalysisManager::Invalidator &) {
0038       // Check whether the analysis has been explicitly invalidated. Otherwise,
0039       // it remains preserved.
0040       auto PAC = PA.getChecker<MarkerTy>();
0041       return !PAC.preservedWhenStateless();
0042     }
0043   };
0044 
0045   Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
0046 
0047   Result run(Loop &L, LoopAnalysisManager &AM,
0048              LoopStandardAnalysisResults &AR) {
0049     return Result();
0050   }
0051 };
0052 
0053 /// A pass manager to run a set of extra function passes if the
0054 /// ShouldRunExtraPasses marker analysis is present. This allows passes to
0055 /// request additional transformations on demand. An example is extra
0056 /// simplifications after loop-vectorization, if runtime checks have been added.
0057 template <typename MarkerTy>
0058 class ExtraFunctionPassManager
0059     : public PassInfoMixin<ExtraFunctionPassManager<MarkerTy>> {
0060   FunctionPassManager InnerFPM;
0061 
0062 public:
0063   template <typename PassT> void addPass(PassT &&Pass) {
0064     InnerFPM.addPass(std::move(Pass));
0065   }
0066 
0067   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
0068     auto PA = PreservedAnalyses::all();
0069     if (AM.getCachedResult<MarkerTy>(F))
0070       PA.intersect(InnerFPM.run(F, AM));
0071     PA.abandon<MarkerTy>();
0072     return PA;
0073   }
0074 
0075   static bool isRequired() { return true; }
0076 };
0077 
0078 /// A pass manager to run a set of extra loop passes if the MarkerTy analysis is
0079 /// present. This allows passes to request additional transformations on demand.
0080 /// An example is doing additional runs of SimpleLoopUnswitch.
0081 template <typename MarkerTy>
0082 class ExtraLoopPassManager
0083     : public PassInfoMixin<ExtraLoopPassManager<MarkerTy>> {
0084   LoopPassManager InnerLPM;
0085 
0086 public:
0087   template <typename PassT> void addPass(PassT &&Pass) {
0088     InnerLPM.addPass(std::move(Pass));
0089   }
0090 
0091   PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
0092                         LoopStandardAnalysisResults &AR, LPMUpdater &U) {
0093     auto PA = PreservedAnalyses::all();
0094     if (AM.getCachedResult<MarkerTy>(L))
0095       PA.intersect(InnerLPM.run(L, AM, AR, U));
0096     PA.abandon<MarkerTy>();
0097     return PA;
0098   }
0099 
0100   static bool isRequired() { return true; }
0101 };
0102 
0103 } // namespace llvm
0104 
0105 #endif // LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H