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