|
| 1 | +//===- ExtraFunctionPassManager.h - Run Optimizations on Demand -*- C++ -*-===// |
| 2 | +// |
| 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 |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// \file |
| 9 | +/// |
| 10 | +/// This file provides a pass manager that only runs its passes if the |
| 11 | +/// provided marker analysis has been preserved, together with a class to |
| 12 | +/// define such a marker analysis. |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H |
| 16 | +#define LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H |
| 17 | + |
| 18 | +#include "llvm/IR/PassManager.h" |
| 19 | +#include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 20 | + |
| 21 | +namespace llvm { |
| 22 | + |
| 23 | +/// A marker analysis to determine if extra passes should be run on demand. |
| 24 | +/// Passes requesting extra transformations to run need to request and preserve |
| 25 | +/// this analysis. |
| 26 | +template <typename MarkerTy> struct ShouldRunExtraPasses { |
| 27 | + struct Result { |
| 28 | + bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 29 | + FunctionAnalysisManager::Invalidator &) { |
| 30 | + // Check whether the analysis has been explicitly invalidated. Otherwise, |
| 31 | + // it remains preserved. |
| 32 | + auto PAC = PA.getChecker<MarkerTy>(); |
| 33 | + return !PAC.preservedWhenStateless(); |
| 34 | + } |
| 35 | + |
| 36 | + bool invalidate(Loop &L, const PreservedAnalyses &PA, |
| 37 | + LoopAnalysisManager::Invalidator &) { |
| 38 | + // Check whether the analysis has been explicitly invalidated. Otherwise, |
| 39 | + // it remains preserved. |
| 40 | + auto PAC = PA.getChecker<MarkerTy>(); |
| 41 | + return !PAC.preservedWhenStateless(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); } |
| 46 | + |
| 47 | + Result run(Loop &L, LoopAnalysisManager &AM, |
| 48 | + LoopStandardAnalysisResults &AR) { |
| 49 | + return Result(); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +/// A pass manager to run a set of extra function passes if the |
| 54 | +/// ShouldRunExtraPasses marker analysis is present. This allows passes to |
| 55 | +/// request additional transformations on demand. An example is extra |
| 56 | +/// simplifications after loop-vectorization, if runtime checks have been added. |
| 57 | +template <typename MarkerTy> |
| 58 | +struct ExtraFunctionPassManager : public FunctionPassManager { |
| 59 | + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
| 60 | + auto PA = PreservedAnalyses::all(); |
| 61 | + if (AM.getCachedResult<MarkerTy>(F)) |
| 62 | + PA.intersect(FunctionPassManager::run(F, AM)); |
| 63 | + PA.abandon<MarkerTy>(); |
| 64 | + return PA; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +/// A pass manager to run a set of extra loop passes if the MarkerTy analysis is |
| 69 | +/// present. This allows passes to request additional transformations on demand. |
| 70 | +/// An example is doing additional runs of SimpleLoopUnswitch. |
| 71 | +template <typename MarkerTy> |
| 72 | +struct ExtraLoopPassManager : public LoopPassManager { |
| 73 | + PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, |
| 74 | + LoopStandardAnalysisResults &AR, LPMUpdater &U) { |
| 75 | + auto PA = PreservedAnalyses::all(); |
| 76 | + if (AM.getCachedResult<MarkerTy>(L)) |
| 77 | + PA.intersect(LoopPassManager::run(L, AM, AR, U)); |
| 78 | + PA.abandon<MarkerTy>(); |
| 79 | + return PA; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +} // namespace llvm |
| 84 | + |
| 85 | +#endif // LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H |
0 commit comments