blob: 10f6fcdcfdb7c388892e20491d1f1ec2edcd06d5 [file] [log] [blame]
Chandler Carruth3bab7e12017-01-11 09:43:56 +00001//===- 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
13using namespace llvm;
14
15// Explicit template instantiations and specialization defininitions for core
16// template typedefs.
17namespace llvm {
18template class PassManager<Loop, LoopAnalysisManager,
19 LoopStandardAnalysisResults &, LPMUpdater &>;
20
21/// Explicitly specialize the pass manager's run method to handle loop nest
22/// structure updates.
23template <>
24PreservedAnalyses
25PassManager<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 for (auto &Pass : Passes) {
34 if (DebugLogging)
35 dbgs() << "Running pass: " << Pass->name() << " on " << L;
36
37 PreservedAnalyses PassPA = Pass->run(L, AM, AR, U);
38
39 // If the loop was deleted, abort the run and return to the outer walk.
40 if (U.skipCurrentLoop()) {
41 PA.intersect(std::move(PassPA));
42 break;
43 }
44
Chandler Carruthce40fa12017-01-25 02:49:01 +000045#ifndef NDEBUG
46 // Verify the loop structure and LCSSA form before visiting the loop.
47 L.verifyLoop();
48 assert(L.isRecursivelyLCSSAForm(AR.DT, AR.LI) &&
49 "Loops must remain in LCSSA form!");
50#endif
51
Chandler Carruth3bab7e12017-01-11 09:43:56 +000052 // Update the analysis manager as each pass runs and potentially
53 // invalidates analyses.
54 AM.invalidate(L, PassPA);
55
56 // Finally, we intersect the final preserved analyses to compute the
57 // aggregate preserved set for this pass manager.
58 PA.intersect(std::move(PassPA));
59
60 // FIXME: Historically, the pass managers all called the LLVM context's
61 // yield function here. We don't have a generic way to acquire the
62 // context and it isn't yet clear what the right pattern is for yielding
63 // in the new pass manager so it is currently omitted.
64 // ...getContext().yield();
65 }
66
67 // Invalidation for the current loop should be handled above, and other loop
68 // analysis results shouldn't be impacted by runs over this loop. Therefore,
69 // the remaining analysis results in the AnalysisManager are preserved. We
70 // mark this with a set so that we don't need to inspect each one
71 // individually.
72 // FIXME: This isn't correct! This loop and all nested loops' analyses should
73 // be preserved, but unrolling should invalidate the parent loop's analyses.
74 PA.preserveSet<AllAnalysesOn<Loop>>();
75
76 if (DebugLogging)
77 dbgs() << "Finished Loop pass manager run.\n";
78
79 return PA;
80}
81}
82
83PrintLoopPass::PrintLoopPass() : OS(dbgs()) {}
84PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner)
85 : OS(OS), Banner(Banner) {}
86
87PreservedAnalyses PrintLoopPass::run(Loop &L, LoopAnalysisManager &,
88 LoopStandardAnalysisResults &,
89 LPMUpdater &) {
90 printLoop(L, OS, Banner);
91 return PreservedAnalyses::all();
92}