]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Scalar/LoopPassManager.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Scalar / LoopPassManager.cpp
1 //===- LoopPassManager.cpp - Loop pass management -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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     if (DebugLogging)
38       dbgs() << "Running pass: " << Pass->name() << " on " << L;
39
40     // Check the PassInstrumentation's BeforePass callbacks before running the
41     // pass, skip its execution completely if asked to (callback returns false).
42     if (!PI.runBeforePass<Loop>(*Pass, L))
43       continue;
44
45     PreservedAnalyses PassPA = Pass->run(L, AM, AR, U);
46
47     // do not pass deleted Loop into the instrumentation
48     if (U.skipCurrentLoop())
49       PI.runAfterPassInvalidated<Loop>(*Pass);
50     else
51       PI.runAfterPass<Loop>(*Pass, L);
52
53     // If the loop was deleted, abort the run and return to the outer walk.
54     if (U.skipCurrentLoop()) {
55       PA.intersect(std::move(PassPA));
56       break;
57     }
58
59 #ifndef NDEBUG
60     // Verify the loop structure and LCSSA form before visiting the loop.
61     L.verifyLoop();
62     assert(L.isRecursivelyLCSSAForm(AR.DT, AR.LI) &&
63            "Loops must remain in LCSSA form!");
64 #endif
65
66     // Update the analysis manager as each pass runs and potentially
67     // invalidates analyses.
68     AM.invalidate(L, PassPA);
69
70     // Finally, we intersect the final preserved analyses to compute the
71     // aggregate preserved set for this pass manager.
72     PA.intersect(std::move(PassPA));
73
74     // FIXME: Historically, the pass managers all called the LLVM context's
75     // yield function here. We don't have a generic way to acquire the
76     // context and it isn't yet clear what the right pattern is for yielding
77     // in the new pass manager so it is currently omitted.
78     // ...getContext().yield();
79   }
80
81   // Invalidation for the current loop should be handled above, and other loop
82   // analysis results shouldn't be impacted by runs over this loop. Therefore,
83   // the remaining analysis results in the AnalysisManager are preserved. We
84   // mark this with a set so that we don't need to inspect each one
85   // individually.
86   // FIXME: This isn't correct! This loop and all nested loops' analyses should
87   // be preserved, but unrolling should invalidate the parent loop's analyses.
88   PA.preserveSet<AllAnalysesOn<Loop>>();
89
90   if (DebugLogging)
91     dbgs() << "Finished Loop pass manager run.\n";
92
93   return PA;
94 }
95 }
96
97 PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
98 PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
99     : OS(OS), Banner(Banner) {}
100
101 PreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
102                                      LoopStandardAnalysisResults &,
103                                      LPMUpdater &) {
104   printLoop(L, OS, Banner);
105   return PreservedAnalyses::all();
106 }