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