1312173Sdim//===- LoopPassManager.cpp - Loop pass management -------------------------===//
2312173Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6312173Sdim//
7312173Sdim//===----------------------------------------------------------------------===//
8312173Sdim
9312173Sdim#include "llvm/Transforms/Scalar/LoopPassManager.h"
10312173Sdim#include "llvm/Analysis/LoopInfo.h"
11312173Sdim
12312173Sdimusing namespace llvm;
13312173Sdim
14312173Sdim// Explicit template instantiations and specialization defininitions for core
15312173Sdim// template typedefs.
16312173Sdimnamespace llvm {
17312173Sdimtemplate class PassManager<Loop, LoopAnalysisManager,
18312173Sdim                           LoopStandardAnalysisResults &, LPMUpdater &>;
19312173Sdim
20312173Sdim/// Explicitly specialize the pass manager's run method to handle loop nest
21312173Sdim/// structure updates.
22312173Sdimtemplate <>
23312173SdimPreservedAnalyses
24312173SdimPassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
25312173Sdim            LPMUpdater &>::run(Loop &L, LoopAnalysisManager &AM,
26312173Sdim                               LoopStandardAnalysisResults &AR, LPMUpdater &U) {
27312173Sdim  PreservedAnalyses PA = PreservedAnalyses::all();
28312173Sdim
29312173Sdim  if (DebugLogging)
30312173Sdim    dbgs() << "Starting Loop pass manager run.\n";
31312173Sdim
32344779Sdim  // Request PassInstrumentation from analysis manager, will use it to run
33344779Sdim  // instrumenting callbacks for the passes later.
34344779Sdim  PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(L, AR);
35312173Sdim  for (auto &Pass : Passes) {
36312173Sdim    if (DebugLogging)
37312173Sdim      dbgs() << "Running pass: " << Pass->name() << " on " << L;
38312173Sdim
39344779Sdim    // Check the PassInstrumentation's BeforePass callbacks before running the
40344779Sdim    // pass, skip its execution completely if asked to (callback returns false).
41344779Sdim    if (!PI.runBeforePass<Loop>(*Pass, L))
42344779Sdim      continue;
43344779Sdim
44312173Sdim    PreservedAnalyses PassPA = Pass->run(L, AM, AR, U);
45312173Sdim
46344779Sdim    // do not pass deleted Loop into the instrumentation
47344779Sdim    if (U.skipCurrentLoop())
48344779Sdim      PI.runAfterPassInvalidated<Loop>(*Pass);
49344779Sdim    else
50344779Sdim      PI.runAfterPass<Loop>(*Pass, L);
51344779Sdim
52312173Sdim    // If the loop was deleted, abort the run and return to the outer walk.
53312173Sdim    if (U.skipCurrentLoop()) {
54312173Sdim      PA.intersect(std::move(PassPA));
55312173Sdim      break;
56312173Sdim    }
57312173Sdim
58321369Sdim#ifndef NDEBUG
59321369Sdim    // Verify the loop structure and LCSSA form before visiting the loop.
60321369Sdim    L.verifyLoop();
61321369Sdim    assert(L.isRecursivelyLCSSAForm(AR.DT, AR.LI) &&
62321369Sdim           "Loops must remain in LCSSA form!");
63321369Sdim#endif
64321369Sdim
65312173Sdim    // Update the analysis manager as each pass runs and potentially
66312173Sdim    // invalidates analyses.
67312173Sdim    AM.invalidate(L, PassPA);
68312173Sdim
69312173Sdim    // Finally, we intersect the final preserved analyses to compute the
70312173Sdim    // aggregate preserved set for this pass manager.
71312173Sdim    PA.intersect(std::move(PassPA));
72312173Sdim
73312173Sdim    // FIXME: Historically, the pass managers all called the LLVM context's
74312173Sdim    // yield function here. We don't have a generic way to acquire the
75312173Sdim    // context and it isn't yet clear what the right pattern is for yielding
76312173Sdim    // in the new pass manager so it is currently omitted.
77312173Sdim    // ...getContext().yield();
78312173Sdim  }
79312173Sdim
80312173Sdim  // Invalidation for the current loop should be handled above, and other loop
81312173Sdim  // analysis results shouldn't be impacted by runs over this loop. Therefore,
82312173Sdim  // the remaining analysis results in the AnalysisManager are preserved. We
83312173Sdim  // mark this with a set so that we don't need to inspect each one
84312173Sdim  // individually.
85312173Sdim  // FIXME: This isn't correct! This loop and all nested loops' analyses should
86312173Sdim  // be preserved, but unrolling should invalidate the parent loop's analyses.
87312173Sdim  PA.preserveSet<AllAnalysesOn<Loop>>();
88312173Sdim
89312173Sdim  if (DebugLogging)
90312173Sdim    dbgs() << "Finished Loop pass manager run.\n";
91312173Sdim
92312173Sdim  return PA;
93312173Sdim}
94312173Sdim}
95312173Sdim
96312173SdimPrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
97312173SdimPrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
98312173Sdim    : OS(OS), Banner(Banner) {}
99312173Sdim
100312173SdimPreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
101312173Sdim                                     LoopStandardAnalysisResults &,
102312173Sdim                                     LPMUpdater &) {
103312173Sdim  printLoop(L, OS, Banner);
104312173Sdim  return PreservedAnalyses::all();
105312173Sdim}
106