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/CodeGeneration.h" 25 #include "polly/CodeGen/CodegenCleanup.h" 26 #include "polly/DependenceInfo.h" 27 #include "polly/LinkAllPasses.h" 28 #include "polly/Options.h" 29 #include "polly/ScopDetection.h" 30 #include "polly/ScopInfo.h" 31 #include "llvm/Analysis/CFGPrinter.h" 32 #include "llvm/IR/LegacyPassManager.h" 33 #include "llvm/Transforms/IPO.h" 34 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 35 #include "llvm/Transforms/Scalar.h" 36 #include "llvm/Transforms/Vectorize.h" 37 38 using namespace llvm; 39 using namespace polly; 40 41 cl::OptionCategory PollyCategory("Polly Options", 42 "Configure the polly loop optimizer"); 43 44 static cl::opt<bool> 45 PollyEnabled("polly", cl::desc("Enable the polly optimizer (only at -O3)"), 46 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 47 48 static cl::opt<bool> PollyDetectOnly( 49 "polly-only-scop-detection", 50 cl::desc("Only run scop detection, but no other optimizations"), 51 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 52 53 enum PassPositionChoice { 54 POSITION_EARLY, 55 POSITION_AFTER_LOOPOPT, 56 POSITION_BEFORE_VECTORIZER 57 }; 58 59 enum OptimizerChoice { OPTIMIZER_NONE, OPTIMIZER_ISL }; 60 61 static cl::opt<PassPositionChoice> PassPosition( 62 "polly-position", cl::desc("Where to run polly in the pass pipeline"), 63 cl::values( 64 clEnumValN(POSITION_EARLY, "early", "Before everything"), 65 clEnumValN(POSITION_AFTER_LOOPOPT, "after-loopopt", 66 "After the loop optimizer (but within the inline cycle)"), 67 clEnumValN(POSITION_BEFORE_VECTORIZER, "before-vectorizer", 68 "Right before the vectorizer"), 69 clEnumValEnd), 70 cl::Hidden, cl::init(POSITION_EARLY), cl::ZeroOrMore, 71 cl::cat(PollyCategory)); 72 73 static cl::opt<OptimizerChoice> Optimizer( 74 "polly-optimizer", cl::desc("Select the scheduling optimizer"), 75 cl::values(clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"), 76 clEnumValN(OPTIMIZER_ISL, "isl", "The isl scheduling optimizer"), 77 clEnumValEnd), 78 cl::Hidden, cl::init(OPTIMIZER_ISL), cl::ZeroOrMore, 79 cl::cat(PollyCategory)); 80 81 enum CodeGenChoice { CODEGEN_ISL, CODEGEN_NONE }; 82 static cl::opt<CodeGenChoice> CodeGenerator( 83 "polly-code-generator", cl::desc("Select the code generator"), 84 cl::values(clEnumValN(CODEGEN_ISL, "isl", "isl code generator"), 85 clEnumValN(CODEGEN_NONE, "none", "no code generation"), 86 clEnumValEnd), 87 cl::Hidden, cl::init(CODEGEN_ISL), cl::ZeroOrMore, cl::cat(PollyCategory)); 88 89 VectorizerChoice polly::PollyVectorizerChoice; 90 static cl::opt<polly::VectorizerChoice, true> Vectorizer( 91 "polly-vectorizer", cl::desc("Select the vectorization strategy"), 92 cl::values( 93 clEnumValN(polly::VECTORIZER_NONE, "none", "No Vectorization"), 94 clEnumValN(polly::VECTORIZER_POLLY, "polly", 95 "Polly internal vectorizer"), 96 clEnumValN(polly::VECTORIZER_STRIPMINE, "stripmine", 97 "Strip-mine outer loops for the loop-vectorizer to trigger"), 98 clEnumValEnd), 99 cl::location(PollyVectorizerChoice), cl::init(polly::VECTORIZER_NONE), 100 cl::ZeroOrMore, cl::cat(PollyCategory)); 101 102 static cl::opt<bool> ImportJScop( 103 "polly-import", 104 cl::desc("Export the polyhedral description of the detected Scops"), 105 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 106 107 static cl::opt<bool> ExportJScop( 108 "polly-export", 109 cl::desc("Export the polyhedral description of the detected Scops"), 110 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 111 112 static cl::opt<bool> DeadCodeElim("polly-run-dce", 113 cl::desc("Run the dead code elimination"), 114 cl::Hidden, cl::init(false), cl::ZeroOrMore, 115 cl::cat(PollyCategory)); 116 117 static cl::opt<bool> PollyViewer( 118 "polly-show", 119 cl::desc("Highlight the code regions that will be optimized in a " 120 "(CFG BBs and LLVM-IR instructions)"), 121 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 122 123 static cl::opt<bool> PollyOnlyViewer( 124 "polly-show-only", 125 cl::desc("Highlight the code regions that will be optimized in " 126 "a (CFG only BBs)"), 127 cl::init(false), cl::cat(PollyCategory)); 128 129 static cl::opt<bool> 130 PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"), 131 cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"), 132 cl::init(false), cl::cat(PollyCategory)); 133 134 static cl::opt<bool> PollyOnlyPrinter( 135 "polly-dot-only", 136 cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden, 137 cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"), 138 cl::init(false), cl::cat(PollyCategory)); 139 140 static cl::opt<bool> 141 CFGPrinter("polly-view-cfg", 142 cl::desc("Show the Polly CFG right after code generation"), 143 cl::Hidden, cl::init(false), cl::cat(PollyCategory)); 144 145 namespace polly { 146 void initializePollyPasses(PassRegistry &Registry) { 147 initializeCodeGenerationPass(Registry); 148 initializeCodePreparationPass(Registry); 149 initializeDeadCodeElimPass(Registry); 150 initializeDependenceInfoPass(Registry); 151 initializeJSONExporterPass(Registry); 152 initializeJSONImporterPass(Registry); 153 initializeIslAstInfoPass(Registry); 154 initializeIslScheduleOptimizerPass(Registry); 155 initializePollyCanonicalizePass(Registry); 156 initializeScopDetectionPass(Registry); 157 initializeScopInfoRegionPassPass(Registry); 158 initializeCodegenCleanupPass(Registry); 159 } 160 161 /// @brief Register Polly passes such that they form a polyhedral optimizer. 162 /// 163 /// The individual Polly passes are registered in the pass manager such that 164 /// they form a full polyhedral optimizer. The flow of the optimizer starts with 165 /// a set of preparing transformations that canonicalize the LLVM-IR such that 166 /// the LLVM-IR is easier for us to understand and to optimizes. On the 167 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects 168 /// static control flow regions. Those regions are then translated by the 169 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling 170 /// optimizer is run on the polyhedral representation and finally the optimized 171 /// polyhedral representation is code generated back to LLVM-IR. 172 /// 173 /// Besides this core functionality, we optionally schedule passes that provide 174 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that 175 /// allow the export/import of the polyhedral representation 176 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation. 177 /// 178 /// For certain parts of the Polly optimizer, several alternatives are provided: 179 /// 180 /// As scheduling optimizer we support the isl scheduling optimizer 181 /// (http://freecode.com/projects/isl). 182 /// It is also possible to run Polly with no optimizer. This mode is mainly 183 /// provided to analyze the run and compile time changes caused by the 184 /// scheduling optimizer. 185 /// 186 /// Polly supports the isl internal code generator. 187 void registerPollyPasses(llvm::legacy::PassManagerBase &PM) { 188 PM.add(polly::createScopDetectionPass()); 189 190 if (PollyDetectOnly) 191 return; 192 193 if (PollyViewer) 194 PM.add(polly::createDOTViewerPass()); 195 if (PollyOnlyViewer) 196 PM.add(polly::createDOTOnlyViewerPass()); 197 if (PollyPrinter) 198 PM.add(polly::createDOTPrinterPass()); 199 if (PollyOnlyPrinter) 200 PM.add(polly::createDOTOnlyPrinterPass()); 201 202 PM.add(polly::createScopInfoRegionPassPass()); 203 204 if (ImportJScop) 205 PM.add(polly::createJSONImporterPass()); 206 207 if (DeadCodeElim) 208 PM.add(polly::createDeadCodeElimPass()); 209 210 switch (Optimizer) { 211 case OPTIMIZER_NONE: 212 break; /* Do nothing */ 213 214 case OPTIMIZER_ISL: 215 PM.add(polly::createIslScheduleOptimizerPass()); 216 break; 217 } 218 219 if (ExportJScop) 220 PM.add(polly::createJSONExporterPass()); 221 222 switch (CodeGenerator) { 223 case CODEGEN_ISL: 224 PM.add(polly::createCodeGenerationPass()); 225 break; 226 case CODEGEN_NONE: 227 break; 228 } 229 230 // FIXME: This dummy ModulePass keeps some programs from miscompiling, 231 // probably some not correctly preserved analyses. It acts as a barrier to 232 // force all analysis results to be recomputed. 233 PM.add(createBarrierNoopPass()); 234 235 if (CFGPrinter) 236 PM.add(llvm::createCFGPrinterPass()); 237 } 238 239 static bool shouldEnablePolly() { 240 if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer) 241 PollyTrackFailures = true; 242 243 if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer || 244 ExportJScop || ImportJScop) 245 PollyEnabled = true; 246 247 return PollyEnabled; 248 } 249 250 static void 251 registerPollyEarlyAsPossiblePasses(const llvm::PassManagerBuilder &Builder, 252 llvm::legacy::PassManagerBase &PM) { 253 if (!polly::shouldEnablePolly()) 254 return; 255 256 if (PassPosition != POSITION_EARLY) 257 return; 258 259 registerCanonicalicationPasses(PM); 260 polly::registerPollyPasses(PM); 261 } 262 263 static void 264 registerPollyLoopOptimizerEndPasses(const llvm::PassManagerBuilder &Builder, 265 llvm::legacy::PassManagerBase &PM) { 266 if (!polly::shouldEnablePolly()) 267 return; 268 269 if (PassPosition != POSITION_AFTER_LOOPOPT) 270 return; 271 272 PM.add(polly::createCodePreparationPass()); 273 polly::registerPollyPasses(PM); 274 PM.add(createCodegenCleanupPass()); 275 } 276 277 static void 278 registerPollyScalarOptimizerLatePasses(const llvm::PassManagerBuilder &Builder, 279 llvm::legacy::PassManagerBase &PM) { 280 if (!polly::shouldEnablePolly()) 281 return; 282 283 if (PassPosition != POSITION_BEFORE_VECTORIZER) 284 return; 285 286 PM.add(polly::createCodePreparationPass()); 287 polly::registerPollyPasses(PM); 288 PM.add(createCodegenCleanupPass()); 289 } 290 291 /// @brief Register Polly to be available as an optimizer 292 /// 293 /// 294 /// We can currently run Polly at three different points int the pass manager. 295 /// a) very early, b) after the canonicalizing loop transformations and c) right 296 /// before the vectorizer. 297 /// 298 /// The default is currently a), to register Polly such that it runs as early as 299 /// possible. This has several implications: 300 /// 301 /// 1) We need to schedule more canonicalization passes 302 /// 303 /// As nothing is run before Polly, it is necessary to run a set of preparing 304 /// transformations before Polly to canonicalize the LLVM-IR and to allow 305 /// Polly to detect and understand the code. 306 /// 307 /// 2) LICM and LoopIdiom pass have not yet been run 308 /// 309 /// Loop invariant code motion as well as the loop idiom recognition pass make 310 /// it more difficult for Polly to transform code. LICM may introduce 311 /// additional data dependences that are hard to eliminate and the loop idiom 312 /// recognition pass may introduce calls to memset that we currently do not 313 /// understand. By running Polly early enough (meaning before these passes) we 314 /// avoid difficulties that may be introduced by these passes. 315 /// 316 /// 3) We get the full -O3 optimization sequence after Polly 317 /// 318 /// The LLVM-IR that is generated by Polly has been optimized on a high level, 319 /// but it may be rather inefficient on the lower/scalar level. By scheduling 320 /// Polly before all other passes, we have the full sequence of -O3 321 /// optimizations behind us, such that inefficiencies on the low level can 322 /// be optimized away. 323 /// 324 /// We are currently evaluating the benefit or running Polly at position b) or 325 /// c). b) is likely to early as it interacts with the inliner. c) is nice 326 /// as everything is fully inlined and canonicalized, but we need to be able 327 /// to handle LICMed code to make it useful. 328 static llvm::RegisterStandardPasses RegisterPollyOptimizerEarly( 329 llvm::PassManagerBuilder::EP_ModuleOptimizerEarly, 330 registerPollyEarlyAsPossiblePasses); 331 332 static llvm::RegisterStandardPasses 333 RegisterPollyOptimizerLoopEnd(llvm::PassManagerBuilder::EP_LoopOptimizerEnd, 334 registerPollyLoopOptimizerEndPasses); 335 336 static llvm::RegisterStandardPasses RegisterPollyOptimizerScalarLate( 337 llvm::PassManagerBuilder::EP_VectorizerStart, 338 registerPollyScalarOptimizerLatePasses); 339 } 340