1 //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===// 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 defines the PassManagerBuilder class, which is used to set up a 11 // "standard" optimization sequence suitable for languages like C and C++. 12 // 13 //===----------------------------------------------------------------------===// 14 15 16 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 17 18 #include "llvm-c/Transforms/PassManagerBuilder.h" 19 20 #include "llvm/PassManager.h" 21 #include "llvm/DefaultPasses.h" 22 #include "llvm/PassManager.h" 23 #include "llvm/Analysis/Passes.h" 24 #include "llvm/Analysis/Verifier.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Target/TargetLibraryInfo.h" 27 #include "llvm/Transforms/Scalar.h" 28 #include "llvm/Transforms/Vectorize.h" 29 #include "llvm/Transforms/IPO.h" 30 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/Support/ManagedStatic.h" 32 33 using namespace llvm; 34 35 static cl::opt<bool> 36 RunVectorization("vectorize", cl::desc("Run vectorization passes")); 37 38 static cl::opt<bool> 39 UseGVNAfterVectorization("use-gvn-after-vectorization", 40 cl::init(false), cl::Hidden, 41 cl::desc("Run GVN instead of Early CSE after vectorization passes")); 42 43 PassManagerBuilder::PassManagerBuilder() { 44 OptLevel = 2; 45 SizeLevel = 0; 46 LibraryInfo = 0; 47 Inliner = 0; 48 DisableSimplifyLibCalls = false; 49 DisableUnitAtATime = false; 50 DisableUnrollLoops = false; 51 Vectorize = RunVectorization; 52 } 53 54 PassManagerBuilder::~PassManagerBuilder() { 55 delete LibraryInfo; 56 delete Inliner; 57 } 58 59 /// Set of global extensions, automatically added as part of the standard set. 60 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 61 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 62 63 void PassManagerBuilder::addGlobalExtension( 64 PassManagerBuilder::ExtensionPointTy Ty, 65 PassManagerBuilder::ExtensionFn Fn) { 66 GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 67 } 68 69 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 70 Extensions.push_back(std::make_pair(Ty, Fn)); 71 } 72 73 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 74 PassManagerBase &PM) const { 75 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 76 if ((*GlobalExtensions)[i].first == ETy) 77 (*GlobalExtensions)[i].second(*this, PM); 78 for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 79 if (Extensions[i].first == ETy) 80 Extensions[i].second(*this, PM); 81 } 82 83 void 84 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { 85 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 86 // BasicAliasAnalysis wins if they disagree. This is intended to help 87 // support "obvious" type-punning idioms. 88 PM.add(createTypeBasedAliasAnalysisPass()); 89 PM.add(createBasicAliasAnalysisPass()); 90 } 91 92 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { 93 addExtensionsToPM(EP_EarlyAsPossible, FPM); 94 95 // Add LibraryInfo if we have some. 96 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); 97 98 if (OptLevel == 0) return; 99 100 addInitialAliasAnalysisPasses(FPM); 101 102 FPM.add(createCFGSimplificationPass()); 103 FPM.add(createScalarReplAggregatesPass()); 104 FPM.add(createEarlyCSEPass()); 105 FPM.add(createLowerExpectIntrinsicPass()); 106 } 107 108 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { 109 // If all optimizations are disabled, just run the always-inline pass. 110 if (OptLevel == 0) { 111 if (Inliner) { 112 MPM.add(Inliner); 113 Inliner = 0; 114 } 115 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 116 return; 117 } 118 119 // Add LibraryInfo if we have some. 120 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); 121 122 addInitialAliasAnalysisPasses(MPM); 123 124 if (!DisableUnitAtATime) { 125 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 126 127 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 128 129 MPM.add(createIPSCCPPass()); // IP SCCP 130 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 131 132 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 133 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 134 } 135 136 // Start of CallGraph SCC passes. 137 if (!DisableUnitAtATime) 138 MPM.add(createPruneEHPass()); // Remove dead EH info 139 if (Inliner) { 140 MPM.add(Inliner); 141 Inliner = 0; 142 } 143 if (!DisableUnitAtATime) 144 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 145 if (OptLevel > 2) 146 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 147 148 // Start of function pass. 149 // Break up aggregate allocas, using SSAUpdater. 150 MPM.add(createScalarReplAggregatesPass(-1, false)); 151 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 152 if (!DisableSimplifyLibCalls) 153 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations 154 MPM.add(createJumpThreadingPass()); // Thread jumps. 155 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 156 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 157 MPM.add(createInstructionCombiningPass()); // Combine silly seq's 158 159 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 160 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 161 MPM.add(createReassociatePass()); // Reassociate expressions 162 MPM.add(createLoopRotatePass()); // Rotate Loop 163 MPM.add(createLICMPass()); // Hoist loop invariants 164 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 165 MPM.add(createInstructionCombiningPass()); 166 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 167 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 168 MPM.add(createLoopDeletionPass()); // Delete dead loops 169 if (!DisableUnrollLoops) 170 MPM.add(createLoopUnrollPass()); // Unroll small loops 171 addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 172 173 if (OptLevel > 1) 174 MPM.add(createGVNPass()); // Remove redundancies 175 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 176 MPM.add(createSCCPPass()); // Constant prop with SCCP 177 178 // Run instcombine after redundancy elimination to exploit opportunities 179 // opened up by them. 180 MPM.add(createInstructionCombiningPass()); 181 MPM.add(createJumpThreadingPass()); // Thread jumps 182 MPM.add(createCorrelatedValuePropagationPass()); 183 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 184 185 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 186 187 if (Vectorize) { 188 MPM.add(createBBVectorizePass()); 189 MPM.add(createInstructionCombiningPass()); 190 if (OptLevel > 1 && UseGVNAfterVectorization) 191 MPM.add(createGVNPass()); // Remove redundancies 192 else 193 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 194 } 195 196 MPM.add(createAggressiveDCEPass()); // Delete dead instructions 197 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 198 MPM.add(createInstructionCombiningPass()); // Clean up after everything. 199 200 if (!DisableUnitAtATime) { 201 // FIXME: We shouldn't bother with this anymore. 202 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 203 204 // GlobalOpt already deletes dead functions and globals, at -O3 try a 205 // late pass of GlobalDCE. It is capable of deleting dead cycles. 206 if (OptLevel > 2) 207 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 208 209 if (OptLevel > 1) 210 MPM.add(createConstantMergePass()); // Merge dup global constants 211 } 212 addExtensionsToPM(EP_OptimizerLast, MPM); 213 } 214 215 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, 216 bool Internalize, 217 bool RunInliner, 218 bool DisableGVNLoadPRE) { 219 // Provide AliasAnalysis services for optimizations. 220 addInitialAliasAnalysisPasses(PM); 221 222 // Now that composite has been compiled, scan through the module, looking 223 // for a main function. If main is defined, mark all other functions 224 // internal. 225 if (Internalize) 226 PM.add(createInternalizePass(true)); 227 228 // Propagate constants at call sites into the functions they call. This 229 // opens opportunities for globalopt (and inlining) by substituting function 230 // pointers passed as arguments to direct uses of functions. 231 PM.add(createIPSCCPPass()); 232 233 // Now that we internalized some globals, see if we can hack on them! 234 PM.add(createGlobalOptimizerPass()); 235 236 // Linking modules together can lead to duplicated global constants, only 237 // keep one copy of each constant. 238 PM.add(createConstantMergePass()); 239 240 // Remove unused arguments from functions. 241 PM.add(createDeadArgEliminationPass()); 242 243 // Reduce the code after globalopt and ipsccp. Both can open up significant 244 // simplification opportunities, and both can propagate functions through 245 // function pointers. When this happens, we often have to resolve varargs 246 // calls, etc, so let instcombine do this. 247 PM.add(createInstructionCombiningPass()); 248 249 // Inline small functions 250 if (RunInliner) 251 PM.add(createFunctionInliningPass()); 252 253 PM.add(createPruneEHPass()); // Remove dead EH info. 254 255 // Optimize globals again if we ran the inliner. 256 if (RunInliner) 257 PM.add(createGlobalOptimizerPass()); 258 PM.add(createGlobalDCEPass()); // Remove dead functions. 259 260 // If we didn't decide to inline a function, check to see if we can 261 // transform it to pass arguments by value instead of by reference. 262 PM.add(createArgumentPromotionPass()); 263 264 // The IPO passes may leave cruft around. Clean up after them. 265 PM.add(createInstructionCombiningPass()); 266 PM.add(createJumpThreadingPass()); 267 // Break up allocas 268 PM.add(createScalarReplAggregatesPass()); 269 270 // Run a few AA driven optimizations here and now, to cleanup the code. 271 PM.add(createFunctionAttrsPass()); // Add nocapture. 272 PM.add(createGlobalsModRefPass()); // IP alias analysis. 273 274 PM.add(createLICMPass()); // Hoist loop invariants. 275 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 276 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 277 // Nuke dead stores. 278 PM.add(createDeadStoreEliminationPass()); 279 280 // Cleanup and simplify the code after the scalar optimizations. 281 PM.add(createInstructionCombiningPass()); 282 283 PM.add(createJumpThreadingPass()); 284 285 // Delete basic blocks, which optimization passes may have killed. 286 PM.add(createCFGSimplificationPass()); 287 288 // Now that we have optimized the program, discard unreachable functions. 289 PM.add(createGlobalDCEPass()); 290 } 291 292 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void) { 293 PassManagerBuilder *PMB = new PassManagerBuilder(); 294 return wrap(PMB); 295 } 296 297 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 298 PassManagerBuilder *Builder = unwrap(PMB); 299 delete Builder; 300 } 301 302 void 303 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 304 unsigned OptLevel) { 305 PassManagerBuilder *Builder = unwrap(PMB); 306 Builder->OptLevel = OptLevel; 307 } 308 309 void 310 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 311 unsigned SizeLevel) { 312 PassManagerBuilder *Builder = unwrap(PMB); 313 Builder->SizeLevel = SizeLevel; 314 } 315 316 void 317 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 318 LLVMBool Value) { 319 PassManagerBuilder *Builder = unwrap(PMB); 320 Builder->DisableUnitAtATime = Value; 321 } 322 323 void 324 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 325 LLVMBool Value) { 326 PassManagerBuilder *Builder = unwrap(PMB); 327 Builder->DisableUnrollLoops = Value; 328 } 329 330 void 331 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 332 LLVMBool Value) { 333 PassManagerBuilder *Builder = unwrap(PMB); 334 Builder->DisableSimplifyLibCalls = Value; 335 } 336 337 void 338 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 339 unsigned Threshold) { 340 PassManagerBuilder *Builder = unwrap(PMB); 341 Builder->Inliner = createFunctionInliningPass(Threshold); 342 } 343 344 void 345 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 346 LLVMPassManagerRef PM) { 347 PassManagerBuilder *Builder = unwrap(PMB); 348 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); 349 Builder->populateFunctionPassManager(*FPM); 350 } 351 352 void 353 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 354 LLVMPassManagerRef PM) { 355 PassManagerBuilder *Builder = unwrap(PMB); 356 PassManagerBase *MPM = unwrap(PM); 357 Builder->populateModulePassManager(*MPM); 358 } 359 360 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 361 LLVMPassManagerRef PM, 362 bool Internalize, 363 bool RunInliner) { 364 PassManagerBuilder *Builder = unwrap(PMB); 365 PassManagerBase *LPM = unwrap(PM); 366 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner); 367 } 368