This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
RegisterPasses.cpp
271 lines (229 loc) · 8.87 KB
/
RegisterPasses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//===------ RegisterPasses.cpp - Add the Polly Passes to default passes --===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Add the Polly passes to the optimization passes executed at -O3.
//
//===----------------------------------------------------------------------===//
#include "polly/RegisterPasses.h"
#include "polly/LinkAllPasses.h"
#include "polly/Cloog.h"
#include "polly/Dependences.h"
#include "polly/ScopDetection.h"
#include "polly/ScopInfo.h"
#include "polly/TempScopInfo.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/CFGPrinter.h"
#include "llvm/InitializePasses.h"
#include "llvm/PassManager.h"
#include "llvm/PassRegistry.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Support/CommandLine.h"
#include <string>
using namespace llvm;
static cl::opt<bool>
PollyEnabled("polly", cl::desc("Enable the default passes of Polly in -O3"),
cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
DisableCodegen("polly-no-codegen",
cl::desc("Disable Polly Code Generation"), cl::Hidden,
cl::init(false), cl::ZeroOrMore);
enum OptimizerChoice {
OPTIMIZER_NONE,
#ifdef SCOPLIB_FOUND
OPTIMIZER_POCC,
#endif
OPTIMIZER_ISL
};
static cl::opt<OptimizerChoice>
Optimizer("polly-optimizer",
cl::desc("Select the scheduling optimizer"),
cl::values(
clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"),
#ifdef SCOPLIB_FOUND
clEnumValN(OPTIMIZER_POCC, "pocc", "The PoCC scheduling optimizer"),
#endif
clEnumValN(OPTIMIZER_ISL, "isl", "The isl scheduling optimizer"),
clEnumValEnd),
cl::Hidden, cl::init(OPTIMIZER_ISL), cl::ZeroOrMore);
static cl::opt<bool>
ImportJScop("polly-run-import-jscop",
cl::desc("Export the JScop description of the detected Scops"),
cl::Hidden, cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
ExportJScop("polly-run-export-jscop",
cl::desc("Export the JScop description of the detected Scops"),
cl::Hidden, cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
ExportCLooG("polly-run-export-cloog",
cl::desc("Export the CLooG input files for the detected Scops"),
cl::Hidden, cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
PollyViewer("polly-show",
cl::desc("Enable the Polly DOT viewer in -O3"), cl::Hidden,
cl::value_desc("Run the Polly DOT viewer at -O3"),
cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
DeadCodeElim("polly-run-dce",
cl::desc("Run the dead code elimination"),
cl::Hidden, cl::init(false), cl::ZeroOrMore);
static cl::opt<bool>
PollyOnlyViewer("polly-show-only",
cl::desc("Enable the Polly DOT viewer in -O3 (no BB content)"),
cl::Hidden,
cl::value_desc("Run the Polly DOT viewer at -O3 (no BB content"),
cl::init(false));
static cl::opt<bool>
PollyPrinter("polly-dot",
cl::desc("Enable the Polly DOT printer in -O3"), cl::Hidden,
cl::value_desc("Run the Polly DOT printer at -O3"),
cl::init(false));
static cl::opt<bool>
PollyOnlyPrinter("polly-dot-only",
cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"),
cl::Hidden,
cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
cl::init(false));
static cl::opt<bool>
CFGPrinter("polly-view-cfg",
cl::desc("Show the Polly CFG right after code generation"),
cl::Hidden,
cl::init(false));
void initializePollyPasses(PassRegistry &Registry) {
initializeCloogInfoPass(Registry);
initializeCodeGenerationPass(Registry);
initializeCodePreparationPass(Registry);
initializeDeadCodeElimPass(Registry);
initializeDependencesPass(Registry);
initializeIndependentBlocksPass(Registry);
initializeJSONExporterPass(Registry);
initializeJSONImporterPass(Registry);
initializeIslScheduleOptimizerPass(Registry);
#ifdef SCOPLIB_FOUND
initializePoccPass(Registry);
#endif
initializePollyIndVarSimplifyPass(Registry);
initializeRegionSimplifyPass(Registry);
initializeScopDetectionPass(Registry);
initializeScopInfoPass(Registry);
initializeTempScopInfoPass(Registry);
}
// Statically register all Polly passes such that they are available after
// loading Polly.
class StaticInitializer {
public:
StaticInitializer() {
PassRegistry &Registry = *PassRegistry::getPassRegistry();
initializePollyPasses(Registry);
}
};
static StaticInitializer InitializeEverything;
void registerPollyPreoptPasses(llvm::PassManagerBase &PM) {
// A standard set of optimization passes partially taken/copied from the
// set of default optimization passes. It is used to bring the code into
// a canonical form that can than be analyzed by Polly. This set of passes is
// most probably not yet optimal. TODO: Investigate optimal set of passes.
PM.add(llvm::createPromoteMemoryToRegisterPass());
PM.add(llvm::createInstructionCombiningPass()); // Clean up after IPCP & DAE
PM.add(llvm::createCFGSimplificationPass()); // Clean up after IPCP & DAE
PM.add(llvm::createTailCallEliminationPass()); // Eliminate tail calls
PM.add(llvm::createCFGSimplificationPass()); // Merge & remove BBs
PM.add(llvm::createReassociatePass()); // Reassociate expressions
PM.add(llvm::createLoopRotatePass()); // Rotate Loop
PM.add(llvm::createInstructionCombiningPass());
PM.add(polly::createIndVarSimplifyPass()); // Canonicalize indvars
PM.add(polly::createCodePreparationPass());
PM.add(polly::createRegionSimplifyPass());
// FIXME: The next two passes should not be necessary here. They are currently
// because of two problems:
//
// 1. The RegionSimplifyPass destroys the canonical form of induction
// variables,as it produces PHI nodes with incorrectly ordered
// operands. To fix this we run IndVarSimplify.
//
// 2. IndVarSimplify does not preserve the region information and
// the regioninfo pass does currently not recover simple regions.
// As a result we need to run the RegionSimplify pass again to
// recover them
PM.add(polly::createIndVarSimplifyPass());
PM.add(polly::createRegionSimplifyPass());
}
void registerPollyPasses(llvm::PassManagerBase &PM, bool DisableCodegen) {
bool RunCodegen = !DisableCodegen;
registerPollyPreoptPasses(PM);
PM.add(polly::createScopInfoPass());
if (PollyViewer)
PM.add(polly::createDOTViewerPass());
if (PollyOnlyViewer)
PM.add(polly::createDOTOnlyViewerPass());
if (PollyPrinter)
PM.add(polly::createDOTPrinterPass());
if (PollyOnlyPrinter)
PM.add(polly::createDOTOnlyPrinterPass());
if (ImportJScop)
PM.add(polly::createJSONImporterPass());
if (DeadCodeElim)
PM.add(polly::createDeadCodeElimPass());
switch (Optimizer) {
case OPTIMIZER_NONE:
break; /* Do nothing */
#ifdef SCOPLIB_FOUND
case OPTIMIZER_POCC
PM.add(polly::createPoccPass());
break;
#endif
case OPTIMIZER_ISL:
PM.add(polly::createIslScheduleOptimizerPass());
break;
}
if (ExportJScop)
PM.add(polly::createJSONExporterPass());
if (ExportCLooG)
PM.add(polly::createCloogExporterPass());
if (RunCodegen)
PM.add(polly::createCodeGenerationPass());
if (CFGPrinter)
PM.add(llvm::createCFGPrinterPass());
}
static
void registerPollyEarlyAsPossiblePasses(const llvm::PassManagerBuilder &Builder,
llvm::PassManagerBase &PM) {
if (Builder.OptLevel == 0)
return;
if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer ||
ExportJScop || ImportJScop)
PollyEnabled = true;
if (!PollyEnabled) {
if (DisableCodegen)
errs() << "The option -polly-no-codegen has no effect. "
"Polly was not enabled\n";
return;
}
// Polly is only enabled at -O3
if (Builder.OptLevel != 3) {
errs() << "Polly should only be run with -O3. Disabling Polly.\n";
return;
}
registerPollyPasses(PM, DisableCodegen);
}
static void registerPollyOptLevel0Passes(const llvm::PassManagerBuilder &,
llvm::PassManagerBase &PM) {
registerPollyPreoptPasses(PM);
}
// Execute Polly together with a set of preparing passes.
//
// We run Polly that early to run before loop optimizer passes like LICM or
// the LoopIdomPass. Both transform the code in a way that Polly will recognize
// less scops.
static llvm::RegisterStandardPasses
PassRegister(llvm::PassManagerBuilder::EP_EarlyAsPossible,
registerPollyEarlyAsPossiblePasses);
static llvm::RegisterStandardPasses
PassRegisterPreopt(llvm::PassManagerBuilder::EP_EnabledOnOptLevel0,
registerPollyOptLevel0Passes);