PassManagerBuilder.cpp revision 8849838965941919257b923f8603ca1bacd3fa46
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" 1769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola#include "llvm-c/Transforms/PassManagerBuilder.h" 18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h" 19c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Analysis/Passes.h" 20de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Analysis/Verifier.h" 21d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/PassManager.h" 22de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Support/CommandLine.h" 23d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/ManagedStatic.h" 24c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Target/TargetLibraryInfo.h" 25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/IPO.h" 26c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/Scalar.h" 27de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Transforms/Vectorize.h" 28c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 29c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolausing namespace llvm; 30c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 31de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkelstatic cl::opt<bool> 3276c25dc2bf23b0c710d896ca61f236fe5b18ee08Andrew TrickRunLoopVectorization("vectorize-loops", 339210cc3a173224b40d9fc45b202f52cc7acf5c5aNadav Rotem cl::desc("Run the Loop vectorization passes")); 34d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem 35d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotemstatic cl::opt<bool> 368849838965941919257b923f8603ca1bacd3fa46Nadav RotemRunSLPVectorization("vectorize-slp", cl::desc("Run the SLP vectorization passes")); 37de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel 38064551e94c9763c7e74121b88f9cbccc0969946aHal Finkelstatic cl::opt<bool> 39064551e94c9763c7e74121b88f9cbccc0969946aHal FinkelUseGVNAfterVectorization("use-gvn-after-vectorization", 40064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel cl::init(false), cl::Hidden, 41064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel cl::desc("Run GVN instead of Early CSE after vectorization passes")); 42064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel 43713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruthstatic cl::opt<bool> UseNewSROA("use-new-sroa", 441fe4fae463eba5965aa1f98600161ef7de96aaf1Chandler Carruth cl::init(true), cl::Hidden, 45713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth cl::desc("Enable the new, experimental SROA pass")); 46713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth 47c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::PassManagerBuilder() { 48c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola OptLevel = 2; 49c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola SizeLevel = 0; 50c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola LibraryInfo = 0; 51c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 52c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableSimplifyLibCalls = false; 53c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableUnitAtATime = false; 54c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola DisableUnrollLoops = false; 558849838965941919257b923f8603ca1bacd3fa46Nadav Rotem SLPVectorize = RunSLPVectorization; 56d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem LoopVectorize = RunLoopVectorization; 57c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 58c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 59c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::~PassManagerBuilder() { 60c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola delete LibraryInfo; 61c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola delete Inliner; 62c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 63c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 647a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall/// Set of global extensions, automatically added as part of the standard set. 657a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallstatic ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 667a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 677a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall 687a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallvoid PassManagerBuilder::addGlobalExtension( 697a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionPointTy Ty, 707a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall PassManagerBuilder::ExtensionFn Fn) { 717a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 727a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall} 737a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall 74c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 75c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Extensions.push_back(std::make_pair(Ty, Fn)); 76c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 77c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 78c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 79c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PassManagerBase &PM) const { 807a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 817a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall if ((*GlobalExtensions)[i].first == ETy) 827a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall (*GlobalExtensions)[i].second(*this, PM); 83c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 84c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Extensions[i].first == ETy) 85c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Extensions[i].second(*this, PM); 86c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 87c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 88c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid 89c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { 90c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 91c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // BasicAliasAnalysis wins if they disagree. This is intended to help 92c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // support "obvious" type-punning idioms. 93c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createTypeBasedAliasAnalysisPass()); 94c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createBasicAliasAnalysisPass()); 95c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 96c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 97c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { 98c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_EarlyAsPossible, FPM); 99c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 100c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add LibraryInfo if we have some. 101c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); 102c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 103c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel == 0) return; 104c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 105c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(FPM); 106c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 107c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createCFGSimplificationPass()); 108713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth if (UseNewSROA) 109713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth FPM.add(createSROAPass()); 110713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth else 111713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth FPM.add(createScalarReplAggregatesPass()); 112c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createEarlyCSEPass()); 113c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola FPM.add(createLowerExpectIntrinsicPass()); 114c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 115c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 116c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { 117c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // If all optimizations are disabled, just run the always-inline pass. 118c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel == 0) { 119c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Inliner) { 120c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(Inliner); 121c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 122c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 12363a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth 12463a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 12563a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // pass manager, but we don't want to add extensions into that pass manager. 12663a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // To prevent this we must insert a no-op module pass to reset the pass 12763a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth // manager to get the same behavior as EP_OptimizerLast in non-O0 builds. 12863a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth if (!GlobalExtensions->empty() || !Extensions.empty()) 12963a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth MPM.add(createBarrierNoopPass()); 13063a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth 131af65a8c54b66e9a78e2c7a9a810a33a73fb7f63aKostya Serebryany addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 132c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola return; 133c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 134c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 135c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Add LibraryInfo if we have some. 136c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); 137c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 138c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(MPM); 139c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 140c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) { 1417d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 1427d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman 143c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 144c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 145c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createIPSCCPPass()); // IP SCCP 146c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 147c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 148c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 149c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 150c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 151c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 152c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Start of CallGraph SCC passes. 153c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) 154c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createPruneEHPass()); // Remove dead EH info 155c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (Inliner) { 156c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(Inliner); 157c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola Inliner = 0; 158c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 159c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) 160c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 161c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel > 2) 162c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 163c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 164c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Start of function pass. 165c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Break up aggregate allocas, using SSAUpdater. 1661c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth if (UseNewSROA) 1671c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth MPM.add(createSROAPass(/*RequiresDomTree*/ false)); 1681c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth else 1691c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth MPM.add(createScalarReplAggregatesPass(-1, false)); 170c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 171c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableSimplifyLibCalls) 172c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations 173c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createJumpThreadingPass()); // Thread jumps. 174c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 175c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 176c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); // Combine silly seq's 177c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 178c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 179c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 180c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createReassociatePass()); // Reassociate expressions 181c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopRotatePass()); // Rotate Loop 182c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLICMPass()); // Hoist loop invariants 183c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 184c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); 185c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 186c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 187c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopDeletionPass()); // Delete dead loops 188d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem 189b7c772922e53a2b4705fe93397dfbc92d1dc8ef7Nadav Rotem if (LoopVectorize && OptLevel > 2) 190ae3b652f5cc19d83b6466d4fa70a7d1c7fb6d06cNadav Rotem MPM.add(createLoopVectorizePass()); 191d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem 192c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnrollLoops) 193c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createLoopUnrollPass()); // Unroll small loops 194c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 195c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 196c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (OptLevel > 1) 197c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGVNPass()); // Remove redundancies 198c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 199c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createSCCPPass()); // Constant prop with SCCP 200c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 201c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Run instcombine after redundancy elimination to exploit opportunities 202c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // opened up by them. 203c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); 204c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createJumpThreadingPass()); // Thread jumps 205c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCorrelatedValuePropagationPass()); 206c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 207c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 208c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 209c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 2108849838965941919257b923f8603ca1bacd3fa46Nadav Rotem if (SLPVectorize) { 211de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel MPM.add(createBBVectorizePass()); 212de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel MPM.add(createInstructionCombiningPass()); 213064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel if (OptLevel > 1 && UseGVNAfterVectorization) 214064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel MPM.add(createGVNPass()); // Remove redundancies 215064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel else 216064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 217c0b3d4c9c252acec01de719d4e756456d5377e6dHal Finkel 218c0b3d4c9c252acec01de719d4e756456d5377e6dHal Finkel // BBVectorize may have significantly shortened a loop body; unroll again. 219c0b3d4c9c252acec01de719d4e756456d5377e6dHal Finkel if (!DisableUnrollLoops) 220c0b3d4c9c252acec01de719d4e756456d5377e6dHal Finkel MPM.add(createLoopUnrollPass()); 221de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel } 222de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel 223c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createAggressiveDCEPass()); // Delete dead instructions 224c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 225c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createInstructionCombiningPass()); // Clean up after everything. 226c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 227c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (!DisableUnitAtATime) { 228c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // FIXME: We shouldn't bother with this anymore. 229c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 230c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 231f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng // GlobalOpt already deletes dead functions and globals, at -O2 try a 232c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // late pass of GlobalDCE. It is capable of deleting dead cycles. 233f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng if (OptLevel > 1) { 234c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 235c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola MPM.add(createConstantMergePass()); // Merge dup global constants 236f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng } 237c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola } 2381db394921b9635980555cc8d7e0d05b5e885784fKostya Serebryany addExtensionsToPM(EP_OptimizerLast, MPM); 239c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 240c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 241c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, 242c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola bool Internalize, 2433197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling bool RunInliner, 2443197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling bool DisableGVNLoadPRE) { 245c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Provide AliasAnalysis services for optimizations. 246c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola addInitialAliasAnalysisPasses(PM); 247c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 248c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that composite has been compiled, scan through the module, looking 249c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // for a main function. If main is defined, mark all other functions 250c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // internal. 251e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola if (Internalize) { 252e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola std::vector<const char*> E; 253e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola E.push_back("main"); 254e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola PM.add(createInternalizePass(E)); 255e5551ed9cec1ae777c4e4b8852a1d7842c2e1c3dRafael Espindola } 256c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 257c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Propagate constants at call sites into the functions they call. This 258c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // opens opportunities for globalopt (and inlining) by substituting function 259c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // pointers passed as arguments to direct uses of functions. 260c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createIPSCCPPass()); 261c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 262c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that we internalized some globals, see if we can hack on them! 263c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalOptimizerPass()); 264c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 265c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Linking modules together can lead to duplicated global constants, only 266c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // keep one copy of each constant. 267c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createConstantMergePass()); 268c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 269c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Remove unused arguments from functions. 270c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createDeadArgEliminationPass()); 271c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 272c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Reduce the code after globalopt and ipsccp. Both can open up significant 273c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // simplification opportunities, and both can propagate functions through 274c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // function pointers. When this happens, we often have to resolve varargs 275c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // calls, etc, so let instcombine do this. 276c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 277c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 278c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Inline small functions 279c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (RunInliner) 280c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createFunctionInliningPass()); 281c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 282c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createPruneEHPass()); // Remove dead EH info. 283c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 284c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Optimize globals again if we ran the inliner. 285c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola if (RunInliner) 286c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalOptimizerPass()); 287c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalDCEPass()); // Remove dead functions. 288c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 289c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // If we didn't decide to inline a function, check to see if we can 290c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // transform it to pass arguments by value instead of by reference. 291c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createArgumentPromotionPass()); 292c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 293c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // The IPO passes may leave cruft around. Clean up after them. 294c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 295c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createJumpThreadingPass()); 296c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Break up allocas 297713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth if (UseNewSROA) 298713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth PM.add(createSROAPass()); 299713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth else 300713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth PM.add(createScalarReplAggregatesPass()); 301c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 302c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Run a few AA driven optimizations here and now, to cleanup the code. 303c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createFunctionAttrsPass()); // Add nocapture. 304c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalsModRefPass()); // IP alias analysis. 305c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 3063197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createLICMPass()); // Hoist loop invariants. 3073197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 3083197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling PM.add(createMemCpyOptPass()); // Remove dead memcpys. 309c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Nuke dead stores. 310c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createDeadStoreEliminationPass()); 311c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 312c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Cleanup and simplify the code after the scalar optimizations. 313c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createInstructionCombiningPass()); 314c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 315c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createJumpThreadingPass()); 316c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 317c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Delete basic blocks, which optimization passes may have killed. 318c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createCFGSimplificationPass()); 319c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola 320c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola // Now that we have optimized the program, discard unreachable functions. 321c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola PM.add(createGlobalDCEPass()); 322c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola} 32369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 32479c07d2a36282b09b9c5d0aa65ebf4bff017621bDmitri GribenkoLLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { 32569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *PMB = new PassManagerBuilder(); 32669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola return wrap(PMB); 32769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 32869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 32969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 33069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 33169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola delete Builder; 33269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 33369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 33469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 33569cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 33669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned OptLevel) { 33769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 33869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->OptLevel = OptLevel; 33969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 34069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 34169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 34269cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 34369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned SizeLevel) { 34469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 34569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->SizeLevel = SizeLevel; 34669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 34769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 34869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 34969cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 35069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 35169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 35269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableUnitAtATime = Value; 35369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 35469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 35569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 35669cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 35769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 35869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 35969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableUnrollLoops = Value; 36069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 36169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 36269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 36369cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 36469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMBool Value) { 36569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 36669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->DisableSimplifyLibCalls = Value; 36769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 36869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 36969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 37069cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 37169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola unsigned Threshold) { 37269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 37369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->Inliner = createFunctionInliningPass(Threshold); 37469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 37569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 37669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 37769cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 37869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM) { 37969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 38069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); 38169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->populateFunctionPassManager(*FPM); 38269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 38369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 38469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid 38569cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 38669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM) { 38769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 38869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBase *MPM = unwrap(PM); 38969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola Builder->populateModulePassManager(*MPM); 39069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 39169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola 39269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 39369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola LLVMPassManagerRef PM, 3940ebc084132cbf4ebebdb6c931ef42f97e3ff5fe1Nick Lewycky LLVMBool Internalize, 3950ebc084132cbf4ebebdb6c931ef42f97e3ff5fe1Nick Lewycky LLVMBool RunInliner) { 39669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBuilder *Builder = unwrap(PMB); 39769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola PassManagerBase *LPM = unwrap(PM); 3980ebc084132cbf4ebebdb6c931ef42f97e3ff5fe1Nick Lewycky Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0); 39969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola} 400