1 //===- LoopPassManager.cpp - Loop pass management -------------------------===//
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 #include "llvm/Support/TimeProfiler.h"
10 #include "llvm/Transforms/Scalar/LoopPassManager.h"
11 #include "llvm/Analysis/LoopInfo.h"
12 
13 using namespace llvm;
14 
15 // Explicit template instantiations and specialization defininitions for core
16 // template typedefs.
17 namespace llvm {
18 template class PassManager<Loop, LoopAnalysisManager,
19                            LoopStandardAnalysisResults &, LPMUpdater &>;
20 
21 /// Explicitly specialize the pass manager's run method to handle loop nest
22 /// structure updates.
23 template <>
24 PreservedAnalyses
25 PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
26             LPMUpdater &>::run(Loop &L, LoopAnalysisManager &AM,
27                                LoopStandardAnalysisResults &AR, LPMUpdater &U) {
28   PreservedAnalyses PA = PreservedAnalyses::all();
29 
30   if (DebugLogging)
31     dbgs() << "Starting Loop pass manager run.\n";
32 
33   // Request PassInstrumentation from analysis manager, will use it to run
34   // instrumenting callbacks for the passes later.
35   PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(L, AR);
36   for (auto &Pass : Passes) {
37     // Check the PassInstrumentation's BeforePass callbacks before running the
38     // pass, skip its execution completely if asked to (callback returns false).
39     if (!PI.runBeforePass<Loop>(*Pass, L))
40       continue;
41 
42     PreservedAnalyses PassPA;
43     {
44       TimeTraceScope TimeScope(Pass->name(), L.getName());
45       PassPA = Pass->run(L, AM, AR, U);
46     }
47 
48     // do not pass deleted Loop into the instrumentation
49     if (U.skipCurrentLoop())
50       PI.runAfterPassInvalidated<Loop>(*Pass, PassPA);
51     else
52       PI.runAfterPass<Loop>(*Pass, L, PassPA);
53 
54     // If the loop was deleted, abort the run and return to the outer walk.
55     if (U.skipCurrentLoop()) {
56       PA.intersect(std::move(PassPA));
57       break;
58     }
59 
60     // Update the analysis manager as each pass runs and potentially
61     // invalidates analyses.
62     AM.invalidate(L, PassPA);
63 
64     // Finally, we intersect the final preserved analyses to compute the
65     // aggregate preserved set for this pass manager.
66     PA.intersect(std::move(PassPA));
67 
68     // FIXME: Historically, the pass managers all called the LLVM context's
69     // yield function here. We don't have a generic way to acquire the
70     // context and it isn't yet clear what the right pattern is for yielding
71     // in the new pass manager so it is currently omitted.
72     // ...getContext().yield();
73   }
74 
75   // Invalidation for the current loop should be handled above, and other loop
76   // analysis results shouldn't be impacted by runs over this loop. Therefore,
77   // the remaining analysis results in the AnalysisManager are preserved. We
78   // mark this with a set so that we don't need to inspect each one
79   // individually.
80   // FIXME: This isn't correct! This loop and all nested loops' analyses should
81   // be preserved, but unrolling should invalidate the parent loop's analyses.
82   PA.preserveSet<AllAnalysesOn<Loop>>();
83 
84   if (DebugLogging)
85     dbgs() << "Finished Loop pass manager run.\n";
86 
87   return PA;
88 }
89 }
90 
91 PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
92 PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
93     : OS(OS), Banner(Banner) {}
94 
95 PreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
96                                      LoopStandardAnalysisResults &,
97                                      LPMUpdater &) {
98   printLoop(L, OS, Banner);
99   return PreservedAnalyses::all();
100 }
101