PassManagerBuilder.cpp revision d233b78760e481a9602266f8376eb1189c58bc9a
1c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===// 2c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// 3c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// The LLVM Compiler Infrastructure 4c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// 5c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// This file is distributed under the University of Illinois Open Source 6c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// License. See LICENSE.TXT for details. 7c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// 8c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===----------------------------------------------------------------------===// 9c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// 10c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// This file defines the PassManagerBuilder class, which is used to set up a 11c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// "standard" optimization sequence suitable for languages like C and C++. 12c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// 13c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===----------------------------------------------------------------------===// 14c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 15c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 16c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/IPO/PassManagerBuilder.h" 17c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 1869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola#include "llvm-c/Transforms/PassManagerBuilder.h" 1969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 20c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/PassManager.h" 21c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/DefaultPasses.h" 22c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/PassManager.h" 23c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Analysis/Passes.h" 24de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Analysis/Verifier.h" 25de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Support/CommandLine.h" 26c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Target/TargetLibraryInfo.h" 27c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/Scalar.h" 28de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Transforms/Vectorize.h" 29c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/IPO.h" 307a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall#include "llvm/ADT/SmallVector.h" 317a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall#include "llvm/Support/ManagedStatic.h" 32c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 33c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolausing namespace llvm; 34c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 35de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkelstatic cl::opt<bool> 36d233b78760e481a9602266f8376eb1189c58bc9aNadav RotemRunLoopVectorization("vectorize-loops", cl::desc("Run the Loop vectorization passes")); 37d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem 38d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotemstatic cl::opt<bool> 39d233b78760e481a9602266f8376eb1189c58bc9aNadav RotemRunBBVectorization("vectorize", cl::desc("Run the BB vectorization passes")); 40de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel 41064551e94c9763c7e74121b88f9cbccc0969946aHal Finkelstatic cl::opt<bool> 42064551e94c9763c7e74121b88f9cbccc0969946aHal FinkelUseGVNAfterVectorization("use-gvn-after-vectorization", 43064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel cl::init(false), cl::Hidden, 44064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel cl::desc("Run GVN instead of Early CSE after vectorization passes")); 45064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel 46713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruthstatic cl::opt<bool> UseNewSROA("use-new-sroa", 471fe4fae463eba5965aa1f98600161ef7de96aaf1Chandler Carruth cl::init(true), cl::Hidden, 48713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth cl::desc("Enable the new, experimental SROA pass")); 49713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth 50c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::PassManagerBuilder() { 51c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola OptLevel = 2; 52c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola SizeLevel = 0; 53c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola LibraryInfo = 0; 54c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 55c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableSimplifyLibCalls = false; 56c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableUnitAtATime = false; 57c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableUnrollLoops = false; 58d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem BBVectorize = RunBBVectorization; 59d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem LoopVectorize = RunLoopVectorization; 60c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 61c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 62c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::~PassManagerBuilder() { 63c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola delete LibraryInfo; 64c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola delete Inliner; 65c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 66c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 677a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall/// Set of global extensions, automatically added as part of the standard set. 687a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallstatic ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 697a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 707a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall 717a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallvoid PassManagerBuilder::addGlobalExtension( 727a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionPointTy Ty, 737a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionFn Fn) { 747a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 757a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall} 767a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall 77c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 78c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Extensions.push_back(std::make_pair(Ty, Fn)); 79c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 80c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 81c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 82c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PassManagerBase &PM) const { 837a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 847a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall if ((*GlobalExtensions)[i].first == ETy) 857a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall (*GlobalExtensions)[i].second(*this, PM); 86c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 87c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Extensions[i].first == ETy) 88c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Extensions[i].second(*this, PM); 89c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 90c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 91c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid 92c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { 93c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 94c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // BasicAliasAnalysis wins if they disagree. This is intended to help 95c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // support "obvious" type-punning idioms. 96c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createTypeBasedAliasAnalysisPass()); 97c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createBasicAliasAnalysisPass()); 98c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 99c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 100c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { 101c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_EarlyAsPossible, FPM); 102c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 103c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add LibraryInfo if we have some. 104c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); 105c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 106c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel == 0) return; 107c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 108c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(FPM); 109c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 110c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createCFGSimplificationPass()); 111713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth if (UseNewSROA) 112713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth FPM.add(createSROAPass()); 113713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth else 114713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth FPM.add(createScalarReplAggregatesPass()); 115c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createEarlyCSEPass()); 116c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createLowerExpectIntrinsicPass()); 117c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 118c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 119c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { 120c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // If all optimizations are disabled, just run the always-inline pass. 121c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel == 0) { 122c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Inliner) { 123c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(Inliner); 124c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 125c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 12663a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth 12763a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 12863a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // pass manager, but we don't want to add extensions into that pass manager. 12963a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // To prevent this we must insert a no-op module pass to reset the pass 13063a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // manager to get the same behavior as EP_OptimizerLast in non-O0 builds. 13163a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth if (!GlobalExtensions->empty() || !Extensions.empty()) 13263a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth MPM.add(createBarrierNoopPass()); 13363a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth 134af65a8c54b66e9a78e2c7a9a810a33a73fb7f63aKostya Serebryany addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 135c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola return; 136c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 137c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 138c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add LibraryInfo if we have some. 139c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); 140c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 141c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(MPM); 142c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 143c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) { 1447d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 1457d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman 146c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 147c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 148c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createIPSCCPPass()); // IP SCCP 149c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 150c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 151c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 152c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 153c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 154c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 155c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Start of CallGraph SCC passes. 156c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) 157c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createPruneEHPass()); // Remove dead EH info 158c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Inliner) { 159c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(Inliner); 160c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 161c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 162c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) 163c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 164c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel > 2) 165c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 166c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 167c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Start of function pass. 168c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Break up aggregate allocas, using SSAUpdater. 1691c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth if (UseNewSROA) 1701c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth MPM.add(createSROAPass(/*RequiresDomTree*/ false)); 1711c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth else 1721c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth MPM.add(createScalarReplAggregatesPass(-1, false)); 173c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 174c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableSimplifyLibCalls) 175c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations 176c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createJumpThreadingPass()); // Thread jumps. 177c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 178c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 179c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); // Combine silly seq's 180c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 181c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 182c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 183c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createReassociatePass()); // Reassociate expressions 184c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopRotatePass()); // Rotate Loop 185c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLICMPass()); // Hoist loop invariants 186c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 187c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); 188c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 189c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 190c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopDeletionPass()); // Delete dead loops 191d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem 192d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem if (LoopVectorize) { 193d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem MPM.add(createLoopVectorizePass()); 194d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem MPM.add(createLICMPass()); 195d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem } 196d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem 197c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnrollLoops) 198c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopUnrollPass()); // Unroll small loops 199c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 200c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 201c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel > 1) 202c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGVNPass()); // Remove redundancies 203c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 204c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createSCCPPass()); // Constant prop with SCCP 205c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 206c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Run instcombine after redundancy elimination to exploit opportunities 207c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // opened up by them. 208c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); 209c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createJumpThreadingPass()); // Thread jumps 210c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCorrelatedValuePropagationPass()); 211c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 212c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 213c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 214c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 215d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem if (BBVectorize) { 216de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel MPM.add(createBBVectorizePass()); 217de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel MPM.add(createInstructionCombiningPass()); 218064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel if (OptLevel > 1 && UseGVNAfterVectorization) 219064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel MPM.add(createGVNPass()); // Remove redundancies 220064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel else 221064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 222de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel } 223de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel 224c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createAggressiveDCEPass()); // Delete dead instructions 225c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 226c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); // Clean up after everything. 227c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 228c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) { 229c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // FIXME: We shouldn't bother with this anymore. 230c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 231c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 232f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng // GlobalOpt already deletes dead functions and globals, at -O2 try a 233c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // late pass of GlobalDCE. It is capable of deleting dead cycles. 234f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng if (OptLevel > 1) { 235c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 236c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createConstantMergePass()); // Merge dup global constants 237f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng } 238c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 2391db394921b9635980555cc8d7e0d05b5e885784fKostya Serebryany addExtensionsToPM(EP_OptimizerLast, MPM); 240c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 241c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 242c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, 243c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola bool Internalize, 2443197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling bool RunInliner, 2453197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling bool DisableGVNLoadPRE) { 246c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Provide AliasAnalysis services for optimizations. 247c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(PM); 248c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 249c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that composite has been compiled, scan through the module, looking 250c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // for a main function. If main is defined, mark all other functions 251c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // internal. 252e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola if (Internalize) { 253e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola std::vector<const char*> E; 254e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola E.push_back("main"); 255e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola PM.add(createInternalizePass(E)); 256e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola } 257c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 258c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Propagate constants at call sites into the functions they call. This 259c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // opens opportunities for globalopt (and inlining) by substituting function 260c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // pointers passed as arguments to direct uses of functions. 261c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createIPSCCPPass()); 262c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 263c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that we internalized some globals, see if we can hack on them! 264c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalOptimizerPass()); 265c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 266c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Linking modules together can lead to duplicated global constants, only 267c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // keep one copy of each constant. 268c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createConstantMergePass()); 269c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 270c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Remove unused arguments from functions. 271c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createDeadArgEliminationPass()); 272c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 273c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Reduce the code after globalopt and ipsccp. Both can open up significant 274c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // simplification opportunities, and both can propagate functions through 275c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // function pointers. When this happens, we often have to resolve varargs 276c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // calls, etc, so let instcombine do this. 277c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 278c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 279c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Inline small functions 280c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (RunInliner) 281c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createFunctionInliningPass()); 282c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 283c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createPruneEHPass()); // Remove dead EH info. 284c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 285c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Optimize globals again if we ran the inliner. 286c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (RunInliner) 287c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalOptimizerPass()); 288c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalDCEPass()); // Remove dead functions. 289c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 290c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // If we didn't decide to inline a function, check to see if we can 291c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // transform it to pass arguments by value instead of by reference. 292c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createArgumentPromotionPass()); 293c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 294c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // The IPO passes may leave cruft around. Clean up after them. 295c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 296c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createJumpThreadingPass()); 297c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Break up allocas 298713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth if (UseNewSROA) 299713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth PM.add(createSROAPass()); 300713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth else 301713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth PM.add(createScalarReplAggregatesPass()); 302c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 303c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Run a few AA driven optimizations here and now, to cleanup the code. 304c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createFunctionAttrsPass()); // Add nocapture. 305c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalsModRefPass()); // IP alias analysis. 306c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 3073197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createLICMPass()); // Hoist loop invariants. 3083197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 3093197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createMemCpyOptPass()); // Remove dead memcpys. 310c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Nuke dead stores. 311c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createDeadStoreEliminationPass()); 312c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 313c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Cleanup and simplify the code after the scalar optimizations. 314c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 315c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 316c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createJumpThreadingPass()); 317c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 318c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Delete basic blocks, which optimization passes may have killed. 319c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createCFGSimplificationPass()); 320c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 321c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that we have optimized the program, discard unreachable functions. 322c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalDCEPass()); 323c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 32469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 32569cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void) { 32669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *PMB = new PassManagerBuilder(); 32769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola return wrap(PMB); 32869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 32969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 33069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 33169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 33269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola delete Builder; 33369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 33469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 33569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 33669cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 33769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned OptLevel) { 33869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 33969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->OptLevel = OptLevel; 34069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 34169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 34269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 34369cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 34469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned SizeLevel) { 34569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 34669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->SizeLevel = SizeLevel; 34769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 34869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 34969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 35069cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 35169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 35269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 35369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableUnitAtATime = Value; 35469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 35569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 35669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 35769cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 35869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 35969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 36069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableUnrollLoops = Value; 36169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 36269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 36369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 36469cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 36569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 36669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 36769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableSimplifyLibCalls = Value; 36869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 36969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 37069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 37169cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 37269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned Threshold) { 37369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 37469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->Inliner = createFunctionInliningPass(Threshold); 37569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 37669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 37769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 37869cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 37969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM) { 38069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 38169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); 38269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->populateFunctionPassManager(*FPM); 38369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 38469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 38569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 38669cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 38769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM) { 38869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 38969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBase *MPM = unwrap(PM); 39069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->populateModulePassManager(*MPM); 39169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 39269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 39369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 39469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM, 39569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola bool Internalize, 39669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola bool RunInliner) { 39769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 39869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBase *LPM = unwrap(PM); 39969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->populateLTOPassManager(*LPM, Internalize, RunInliner); 40069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 401