| 1 | //===- LastRunTrackingAnalysis.cpp - Avoid running redundant pass -*- 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 | // |
| 9 | // This is an analysis pass to track a set of passes that have been run, so that |
| 10 | // we can avoid running a pass again if there is no change since the last run of |
| 11 | // the pass. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/LastRunTrackingAnalysis.h" |
| 16 | #include "AnalysisOptions.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | #define DEBUG_TYPE "last-run-tracking" |
| 22 | STATISTIC(NumSkippedPasses, "Number of skipped passes"); |
| 23 | STATISTIC(NumLRTQueries, "Number of LastRunTracking queries"); |
| 24 | |
| 25 | bool LastRunTrackingInfo::shouldSkipImpl(PassID ID, OptionPtr Ptr) const { |
| 26 | if (!Opts.enable_last_run_tracking) |
| 27 | return false; |
| 28 | ++NumLRTQueries; |
| 29 | auto Iter = TrackedPasses.find(Val: ID); |
| 30 | if (Iter == TrackedPasses.end()) |
| 31 | return false; |
| 32 | if (!Iter->second || Iter->second(Ptr)) { |
| 33 | ++NumSkippedPasses; |
| 34 | return true; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | void LastRunTrackingInfo::updateImpl(PassID ID, bool Changed, |
| 40 | CompatibilityCheckFn CheckFn) { |
| 41 | if (Changed) |
| 42 | TrackedPasses.clear(); |
| 43 | TrackedPasses[ID] = std::move(CheckFn); |
| 44 | } |
| 45 | |
| 46 | AnalysisKey LastRunTrackingAnalysis::Key; |
| 47 | |
| 48 | LastRunTrackingInfo LastRunTrackingAnalysis::run(Function &F, |
| 49 | FunctionAnalysisManager &) { |
| 50 | return LastRunTrackingInfo(getAnalysisOptions(F)); |
| 51 | } |
| 52 | |
| 53 | LastRunTrackingInfo LastRunTrackingAnalysis::run(Module &M, |
| 54 | ModuleAnalysisManager &) { |
| 55 | return LastRunTrackingInfo(getAnalysisOptions(M)); |
| 56 | } |
| 57 |