1 //===------ RegisterPasses.cpp - Add the Polly Passes to default passes  --===//
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 // This file composes the individual LLVM-IR passes provided by Polly to a
11 // functional polyhedral optimizer. The polyhedral optimizer is automatically
12 // made available to LLVM based compilers by loading the Polly shared library
13 // into such a compiler.
14 //
15 // The Polly optimizer is made available by executing a static constructor that
16 // registers the individual Polly passes in the LLVM pass manager builder. The
17 // passes are registered such that the default behaviour of the compiler is not
18 // changed, but that the flag '-polly' provided at optimization level '-O3'
19 // enables additional polyhedral optimizations.
20 //===----------------------------------------------------------------------===//
21 
22 #include "polly/RegisterPasses.h"
23 #include "polly/Canonicalization.h"
24 #include "polly/CodeGen/BlockGenerators.h"
25 #include "polly/CodeGen/Cloog.h"
26 #include "polly/CodeGen/CodeGeneration.h"
27 #include "polly/Dependences.h"
28 #include "polly/LinkAllPasses.h"
29 #include "polly/Options.h"
30 #include "polly/ScopDetection.h"
31 #include "polly/ScopInfo.h"
32 #include "polly/TempScopInfo.h"
33 #include "llvm/Analysis/CFGPrinter.h"
34 #include "llvm/PassManager.h"
35 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include "llvm/Transforms/Vectorize.h"
38 
39 using namespace llvm;
40 using namespace polly;
41 
42 cl::OptionCategory PollyCategory("Polly Options",
43                                  "Configure the polly loop optimizer");
44 
45 static cl::opt<bool>
46     PollyEnabled("polly", cl::desc("Enable the polly optimizer (only at -O3)"),
47                  cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
48 
49 enum OptimizerChoice {
50   OPTIMIZER_NONE,
51 #ifdef PLUTO_FOUND
52   OPTIMIZER_PLUTO,
53 #endif
54 #ifdef SCOPLIB_FOUND
55   OPTIMIZER_POCC,
56 #endif
57   OPTIMIZER_ISL
58 };
59 
60 static cl::opt<OptimizerChoice> Optimizer(
61     "polly-optimizer", cl::desc("Select the scheduling optimizer"),
62     cl::values(
63         clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"),
64 #ifdef PLUTO_FOUND
65         clEnumValN(OPTIMIZER_PLUTO, "pluto", "The Pluto scheduling optimizer"),
66 #endif
67 #ifdef SCOPLIB_FOUND
68         clEnumValN(OPTIMIZER_POCC, "pocc", "The PoCC scheduling optimizer"),
69 #endif
70         clEnumValN(OPTIMIZER_ISL, "isl", "The isl scheduling optimizer"),
71         clEnumValEnd),
72     cl::Hidden, cl::init(OPTIMIZER_ISL), cl::ZeroOrMore,
73     cl::cat(PollyCategory));
74 
75 enum CodeGenChoice {
76 #ifdef CLOOG_FOUND
77   CODEGEN_CLOOG,
78 #endif
79   CODEGEN_ISL,
80   CODEGEN_NONE
81 };
82 
83 #ifdef CLOOG_FOUND
84 enum CodeGenChoice DefaultCodeGen = CODEGEN_CLOOG;
85 #else
86 enum CodeGenChoice DefaultCodeGen = CODEGEN_ISL;
87 #endif
88 
89 static cl::opt<CodeGenChoice> CodeGenerator(
90     "polly-code-generator", cl::desc("Select the code generator"),
91     cl::values(
92 #ifdef CLOOG_FOUND
93         clEnumValN(CODEGEN_CLOOG, "cloog", "CLooG"),
94 #endif
95         clEnumValN(CODEGEN_ISL, "isl", "isl code generator"),
96         clEnumValN(CODEGEN_NONE, "none", "no code generation"), clEnumValEnd),
97     cl::Hidden, cl::init(DefaultCodeGen), cl::ZeroOrMore,
98     cl::cat(PollyCategory));
99 
100 VectorizerChoice polly::PollyVectorizerChoice;
101 static cl::opt<polly::VectorizerChoice, true> Vectorizer(
102     "polly-vectorizer", cl::desc("Select the vectorization strategy"),
103     cl::values(clEnumValN(polly::VECTORIZER_NONE, "none", "No Vectorization"),
104                clEnumValN(polly::VECTORIZER_POLLY, "polly",
105                           "Polly internal vectorizer"),
106                clEnumValN(polly::VECTORIZER_UNROLL_ONLY, "unroll-only",
107                           "Only grouped unroll the vectorize candidate loops"),
108                clEnumValN(polly::VECTORIZER_BB, "bb",
109                           "The Basic Block vectorizer driven by Polly"),
110                clEnumValEnd),
111     cl::location(PollyVectorizerChoice), cl::init(polly::VECTORIZER_NONE),
112     cl::ZeroOrMore, cl::cat(PollyCategory));
113 
114 static cl::opt<bool> ImportJScop(
115     "polly-import",
116     cl::desc("Export the polyhedral description of the detected Scops"),
117     cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
118 
119 static cl::opt<bool> ExportJScop(
120     "polly-export",
121     cl::desc("Export the polyhedral description of the detected Scops"),
122     cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
123 
124 static cl::opt<bool> DeadCodeElim("polly-run-dce",
125                                   cl::desc("Run the dead code elimination"),
126                                   cl::Hidden, cl::init(true), cl::ZeroOrMore,
127                                   cl::cat(PollyCategory));
128 
129 static cl::opt<bool> PollyViewer(
130     "polly-show",
131     cl::desc("Highlight the code regions that will be optimized in a "
132              "(CFG BBs and LLVM-IR instructions)"),
133     cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
134 
135 static cl::opt<bool> PollyOnlyViewer(
136     "polly-show-only",
137     cl::desc("Highlight the code regions that will be optimized in "
138              "a (CFG only BBs)"),
139     cl::init(false), cl::cat(PollyCategory));
140 
141 static cl::opt<bool>
142     PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"),
143                  cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"),
144                  cl::init(false), cl::cat(PollyCategory));
145 
146 static cl::opt<bool> PollyOnlyPrinter(
147     "polly-dot-only",
148     cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden,
149     cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
150     cl::init(false), cl::cat(PollyCategory));
151 
152 static cl::opt<bool>
153     CFGPrinter("polly-view-cfg",
154                cl::desc("Show the Polly CFG right after code generation"),
155                cl::Hidden, cl::init(false), cl::cat(PollyCategory));
156 
157 namespace polly {
158 void initializePollyPasses(PassRegistry &Registry) {
159 #ifdef CLOOG_FOUND
160   initializeCloogInfoPass(Registry);
161   initializeCodeGenerationPass(Registry);
162 #endif
163   initializeIslCodeGenerationPass(Registry);
164   initializeCodePreparationPass(Registry);
165   initializeDeadCodeElimPass(Registry);
166   initializeDependencesPass(Registry);
167   initializeIndependentBlocksPass(Registry);
168   initializeJSONExporterPass(Registry);
169   initializeJSONImporterPass(Registry);
170   initializeIslAstInfoPass(Registry);
171   initializeIslScheduleOptimizerPass(Registry);
172 #ifdef SCOPLIB_FOUND
173   initializePoccPass(Registry);
174 #endif
175   initializePollyIndVarSimplifyPass(Registry);
176   initializePollyCanonicalizePass(Registry);
177   initializeScopDetectionPass(Registry);
178   initializeScopInfoPass(Registry);
179   initializeTempScopInfoPass(Registry);
180 }
181 
182 /// @brief Register Polly passes such that they form a polyhedral optimizer.
183 ///
184 /// The individual Polly passes are registered in the pass manager such that
185 /// they form a full polyhedral optimizer. The flow of the optimizer starts with
186 /// a set of preparing transformations that canonicalize the LLVM-IR such that
187 /// the LLVM-IR is easier for us to understand and to optimizes. On the
188 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects
189 /// static control flow regions. Those regions are then translated by the
190 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling
191 /// optimizer is run on the polyhedral representation and finally the optimized
192 /// polyhedral representation is code generated back to LLVM-IR.
193 ///
194 /// Besides this core functionality, we optionally schedule passes that provide
195 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that
196 /// allow the export/import of the polyhedral representation
197 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation.
198 ///
199 /// For certain parts of the Polly optimizer, several alternatives are provided:
200 ///
201 /// As scheduling optimizer we support PoCC (http://pocc.sourceforge.net), PLUTO
202 /// (http://pluto-compiler.sourceforge.net) as well as the isl scheduling
203 /// optimizer (http://freecode.com/projects/isl). The isl optimizer is the
204 /// default optimizer.
205 /// It is also possible to run Polly with no optimizer. This mode is mainly
206 /// provided to analyze the run and compile time changes caused by the
207 /// scheduling optimizer.
208 ///
209 /// Polly supports both CLooG (http://www.cloog.org) as well as the isl internal
210 /// code generator. For the moment, the CLooG code generator is enabled by
211 /// default.
212 static void registerPollyPasses(llvm::PassManagerBase &PM) {
213   registerCanonicalicationPasses(PM, SCEVCodegen);
214 
215   PM.add(polly::createScopInfoPass());
216 
217   if (PollyViewer)
218     PM.add(polly::createDOTViewerPass());
219   if (PollyOnlyViewer)
220     PM.add(polly::createDOTOnlyViewerPass());
221   if (PollyPrinter)
222     PM.add(polly::createDOTPrinterPass());
223   if (PollyOnlyPrinter)
224     PM.add(polly::createDOTOnlyPrinterPass());
225 
226   if (ImportJScop)
227     PM.add(polly::createJSONImporterPass());
228 
229   if (DeadCodeElim)
230     PM.add(polly::createDeadCodeElimPass());
231 
232   switch (Optimizer) {
233   case OPTIMIZER_NONE:
234     break; /* Do nothing */
235 
236 #ifdef SCOPLIB_FOUND
237   case OPTIMIZER_POCC:
238     PM.add(polly::createPoccPass());
239     break;
240 #endif
241 
242 #ifdef PLUTO_FOUND
243   case OPTIMIZER_PLUTO:
244     PM.add(polly::createPlutoOptimizerPass());
245     break;
246 #endif
247 
248   case OPTIMIZER_ISL:
249     PM.add(polly::createIslScheduleOptimizerPass());
250     break;
251   }
252 
253   if (ExportJScop)
254     PM.add(polly::createJSONExporterPass());
255 
256   switch (CodeGenerator) {
257 #ifdef CLOOG_FOUND
258   case CODEGEN_CLOOG:
259     PM.add(polly::createCodeGenerationPass());
260     if (PollyVectorizerChoice == VECTORIZER_BB) {
261       VectorizeConfig C;
262       C.FastDep = true;
263       PM.add(createBBVectorizePass(C));
264     }
265     break;
266 #endif
267   case CODEGEN_ISL:
268     PM.add(polly::createIslCodeGenerationPass());
269     break;
270   case CODEGEN_NONE:
271     break;
272   }
273 
274   if (CFGPrinter)
275     PM.add(llvm::createCFGPrinterPass());
276 }
277 
278 static bool shouldEnablePolly() {
279   if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer)
280     PollyTrackFailures = true;
281 
282   if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer ||
283       ExportJScop || ImportJScop)
284     PollyEnabled = true;
285 
286   return PollyEnabled;
287 }
288 
289 static void
290 registerPollyEarlyAsPossiblePasses(const llvm::PassManagerBuilder &Builder,
291                                    llvm::PassManagerBase &PM) {
292   if (!polly::shouldEnablePolly())
293     return;
294 
295   polly::registerPollyPasses(PM);
296 }
297 
298 /// @brief Register Polly to be available as an optimizer
299 ///
300 /// We currently register Polly such that it runs as early as possible. This has
301 /// several implications:
302 ///
303 ///   1) We need to schedule more canonicalization passes
304 ///
305 ///   As nothing is run before Polly, it is necessary to run a set of preparing
306 ///   transformations before Polly to canonicalize the LLVM-IR and to allow
307 ///   Polly to detect and understand the code.
308 ///
309 ///   2) LICM and LoopIdiom pass have not yet been run
310 ///
311 ///   Loop invariant code motion as well as the loop idiom recognition pass make
312 ///   it more difficult for Polly to transform code. LICM may introduce
313 ///   additional data dependences that are hard to eliminate and the loop idiom
314 ///   recognition pass may introduce calls to memset that we currently do not
315 ///   understand. By running Polly early enough (meaning before these passes) we
316 ///   avoid difficulties that may be introduced by these passes.
317 ///
318 ///   3) We get the full -O3 optimization sequence after Polly
319 ///
320 ///   The LLVM-IR that is generated by Polly has been optimized on a high level,
321 ///   but it may be rather inefficient on the lower/scalar level. By scheduling
322 ///   Polly before all other passes, we have the full sequence of -O3
323 ///   optimizations behind us, such that inefficiencies on the low level can
324 ///   be optimized away.
325 static llvm::RegisterStandardPasses
326     RegisterPollyOptimizer(llvm::PassManagerBuilder::EP_EarlyAsPossible,
327                            registerPollyEarlyAsPossiblePasses);
328 }
329