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