blob: 880dffa7a955ff7496332bb4327e746b5e48f1f0 [file] [log] [blame]
Rafael Espindola3ea478b2011-08-02 21:50:27 +00001//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PassManagerBuilder class, which is used to set up a
11// "standard" optimization sequence suitable for languages like C and C++.
12//
13//===----------------------------------------------------------------------===//
14
Rafael Espindola3ea478b2011-08-02 21:50:27 +000015#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Rafael Espindola07f609152011-08-09 22:17:34 +000016#include "llvm-c/Transforms/PassManagerBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chandler Carruth17e0bc32015-08-06 07:33:15 +000018#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth8b046a42015-08-14 02:42:20 +000019#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth21dcff72015-08-14 03:48:20 +000020#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000021#include "llvm/Analysis/Passes.h"
Chandler Carruth42ff4482015-08-14 02:55:50 +000022#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth1db22822015-08-14 03:33:48 +000024#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000025#include "llvm/IR/DataLayout.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000026#include "llvm/IR/LegacyPassManager.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000027#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000028#include "llvm/IR/Verifier.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/ManagedStatic.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000031#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Transforms/IPO.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000033#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000034#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000035#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000036#include "llvm/Transforms/Instrumentation.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000037#include "llvm/Transforms/Scalar.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000038#include "llvm/Transforms/Scalar/GVN.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000039#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000040
41using namespace llvm;
42
Hal Finkelc34e5112012-02-01 03:51:43 +000043static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000044RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000045 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000046
47static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000048RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000049 cl::desc("Run the SLP vectorization passes"));
50
51static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000052RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000053 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000054
Hal Finkel204bf532012-04-13 17:15:33 +000055static cl::opt<bool>
56UseGVNAfterVectorization("use-gvn-after-vectorization",
57 cl::init(false), cl::Hidden,
58 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
59
Chandler Carruth7b8297a2014-10-14 00:31:29 +000060static cl::opt<bool> ExtraVectorizerPasses(
61 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
62 cl::desc("Run cleanup optimization passes after vectorization."));
63
Chandler Carruth1b398ae2012-09-14 09:22:59 +000064static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000065 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000066 cl::desc("Enable the new, experimental SROA pass"));
67
Hal Finkelbf45efd2013-11-16 23:59:05 +000068static cl::opt<bool>
69RunLoopRerolling("reroll-loops", cl::Hidden,
70 cl::desc("Run the loop rerolling pass"));
71
James Molloy0cbb2a862015-03-27 10:36:57 +000072static cl::opt<bool>
73RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
74 cl::desc("Run the float2int (float demotion) pass"));
75
Michael J. Spencer289067c2014-05-29 01:55:07 +000076static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
77 cl::Hidden,
78 cl::desc("Run the load combining pass"));
79
James Molloy568da092014-08-06 12:56:19 +000080static cl::opt<bool>
81RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000082 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000083 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
84 "vectorizer instead of before"));
85
Hal Finkel445dda52014-09-02 22:12:54 +000086static cl::opt<bool> UseCFLAA("use-cfl-aa",
87 cl::init(false), cl::Hidden,
88 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000089
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000090static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000091EnableMLSM("mlsm", cl::init(true), cl::Hidden,
92 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000093
Karthik Bhat88db86d2015-03-06 10:11:25 +000094static cl::opt<bool> EnableLoopInterchange(
95 "enable-loopinterchange", cl::init(false), cl::Hidden,
96 cl::desc("Enable the new, experimental LoopInterchange Pass"));
97
Adam Nemet938d3d62015-05-14 12:05:18 +000098static cl::opt<bool> EnableLoopDistribute(
99 "enable-loop-distribute", cl::init(false), cl::Hidden,
100 cl::desc("Enable the new, experimental LoopDistribution Pass"));
101
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000102static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +0000103 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000104 cl::desc(
105 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
106
Adam Nemete54a4fa2015-11-03 23:50:08 +0000107static cl::opt<bool> EnableLoopLoadElim(
Adam Nemetc979c6e2016-03-15 22:26:12 +0000108 "enable-loop-load-elim", cl::init(true), cl::Hidden,
109 cl::desc("Enable the LoopLoadElimination Pass"));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000110
Rong Xu34abbfb2016-01-21 18:28:59 +0000111static cl::opt<std::string> RunPGOInstrGen(
112 "profile-generate", cl::init(""), cl::Hidden,
113 cl::desc("Enable generation phase of PGO instrumentation and specify the "
114 "path of profile data file"));
115
116static cl::opt<std::string> RunPGOInstrUse(
117 "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"),
118 cl::desc("Enable use phase of PGO instrumentation and specify the path "
119 "of profile data file"));
120
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000121static cl::opt<bool> UseLoopVersioningLICM(
122 "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
123 cl::desc("Enable the experimental Loop Versioning LICM pass"));
124
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000125PassManagerBuilder::PassManagerBuilder() {
126 OptLevel = 2;
127 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000128 LibraryInfo = nullptr;
129 Inliner = nullptr;
Teresa Johnson26ab5772016-03-15 00:04:37 +0000130 ModuleSummary = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000131 DisableUnitAtATime = false;
132 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000133 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000134 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000135 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000136 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000137 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000138 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000139 VerifyInput = false;
140 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000141 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000142 PrepareForLTO = false;
Rong Xu34abbfb2016-01-21 18:28:59 +0000143 PGOInstrGen = RunPGOInstrGen;
144 PGOInstrUse = RunPGOInstrUse;
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000145 PrepareForThinLTO = false;
146 PerformThinLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000147}
148
149PassManagerBuilder::~PassManagerBuilder() {
150 delete LibraryInfo;
151 delete Inliner;
152}
153
David Chisnall719a72f2011-08-16 13:58:41 +0000154/// Set of global extensions, automatically added as part of the standard set.
155static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
156 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
157
158void PassManagerBuilder::addGlobalExtension(
159 PassManagerBuilder::ExtensionPointTy Ty,
160 PassManagerBuilder::ExtensionFn Fn) {
Justin Lebar2fe13232016-03-30 20:39:29 +0000161 GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn)));
David Chisnall719a72f2011-08-16 13:58:41 +0000162}
163
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000164void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
Justin Lebar2fe13232016-03-30 20:39:29 +0000165 Extensions.push_back(std::make_pair(Ty, std::move(Fn)));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000166}
167
168void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000169 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000170 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
171 if ((*GlobalExtensions)[i].first == ETy)
172 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000173 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
174 if (Extensions[i].first == ETy)
175 Extensions[i].second(*this, PM);
176}
177
Chandler Carruth30d69c22015-02-13 10:01:29 +0000178void PassManagerBuilder::addInitialAliasAnalysisPasses(
179 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000180 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
181 // BasicAliasAnalysis wins if they disagree. This is intended to help
182 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000183 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000184 PM.add(createCFLAAWrapperPass());
185 PM.add(createTypeBasedAAWrapperPass());
186 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000187}
188
Matthias Braunc31032d2016-03-09 18:47:11 +0000189void PassManagerBuilder::addInstructionCombiningPass(
190 legacy::PassManagerBase &PM) const {
191 bool ExpensiveCombines = OptLevel > 2;
192 PM.add(createInstructionCombiningPass(ExpensiveCombines));
193}
194
Chandler Carruth30d69c22015-02-13 10:01:29 +0000195void PassManagerBuilder::populateFunctionPassManager(
196 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000197 addExtensionsToPM(EP_EarlyAsPossible, FPM);
198
199 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000200 if (LibraryInfo)
201 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000202
203 if (OptLevel == 0) return;
204
205 addInitialAliasAnalysisPasses(FPM);
206
207 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000208 if (UseNewSROA)
209 FPM.add(createSROAPass());
210 else
211 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000212 FPM.add(createEarlyCSEPass());
213 FPM.add(createLowerExpectIntrinsicPass());
214}
215
Rong Xu34abbfb2016-01-21 18:28:59 +0000216// Do PGO instrumentation generation or use pass as the option specified.
217void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
218 if (!PGOInstrGen.empty()) {
219 MPM.add(createPGOInstrumentationGenPass());
220 // Add the profile lowering pass.
221 InstrProfOptions Options;
222 Options.InstrProfileOutput = PGOInstrGen;
223 MPM.add(createInstrProfilingPass(Options));
224 }
225 if (!PGOInstrUse.empty())
226 MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
227}
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000228void PassManagerBuilder::addFunctionSimplificationPasses(
Mehdi Amini9c1c3ac2016-02-11 22:09:11 +0000229 legacy::PassManagerBase &MPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000230 // Start of function pass.
231 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000232 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000233 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000234 else
235 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000236 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000237 MPM.add(createJumpThreadingPass()); // Thread jumps.
238 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
239 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Matthias Braunc31032d2016-03-09 18:47:11 +0000240 // Combine silly seq's
241 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000242 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000244 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000245 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
246 MPM.add(createReassociatePass()); // Reassociate expressions
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000247 if (PrepareForThinLTO) {
248 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Matthias Braunc31032d2016-03-09 18:47:11 +0000249 addInstructionCombiningPass(MPM); // Combine silly seq's
Mehdi Aminid5faa262016-04-12 21:35:28 +0000250 // Rename anon function to export them
251 MPM.add(createNameAnonFunctionPass());
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000252 return;
253 }
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000254 // Rotate Loop - disable header duplication at -Oz
255 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 MPM.add(createLICMPass()); // Hoist loop invariants
257 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000258 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000259 addInstructionCombiningPass(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000260 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
261 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
262 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000263 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000264 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000265 MPM.add(createCFGSimplificationPass());
266 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000267 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000268 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000269 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
270
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000271 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000272 if (EnableMLSM)
273 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000274 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000275 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000276 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
277 MPM.add(createSCCPPass()); // Constant prop with SCCP
278
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000279 // Delete dead bit computations (instcombine runs after to fold away the dead
280 // computations, and then ADCE will run later to exploit any new DCE
281 // opportunities that creates).
282 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
283
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000284 // Run instcombine after redundancy elimination to exploit opportunities
285 // opened up by them.
Matthias Braunc31032d2016-03-09 18:47:11 +0000286 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000287 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000288 MPM.add(createJumpThreadingPass()); // Thread jumps
289 MPM.add(createCorrelatedValuePropagationPass());
290 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000291 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000292
293 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
294
Hal Finkel29aeb202013-11-17 16:02:50 +0000295 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000296 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000297 if (!RunSLPAfterLoopVectorization) {
298 if (SLPVectorize)
299 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000300
James Molloy568da092014-08-06 12:56:19 +0000301 if (BBVectorize) {
302 MPM.add(createBBVectorizePass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000303 addInstructionCombiningPass(MPM);
James Molloy568da092014-08-06 12:56:19 +0000304 addExtensionsToPM(EP_Peephole, MPM);
305 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000306 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000307 else
308 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000309
James Molloy568da092014-08-06 12:56:19 +0000310 // BBVectorize may have significantly shortened a loop body; unroll again.
311 if (!DisableUnrollLoops)
312 MPM.add(createLoopUnrollPass());
313 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000314 }
315
Michael J. Spencer289067c2014-05-29 01:55:07 +0000316 if (LoadCombine)
317 MPM.add(createLoadCombinePass());
318
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000319 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000320 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Matthias Braunc31032d2016-03-09 18:47:11 +0000321 // Clean up after everything.
322 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000323 addExtensionsToPM(EP_Peephole, MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000324}
325
326void PassManagerBuilder::populateModulePassManager(
327 legacy::PassManagerBase &MPM) {
328 // Allow forcing function attributes as a debugging and tuning aid.
329 MPM.add(createForceFunctionAttrsLegacyPass());
330
331 // If all optimizations are disabled, just run the always-inline pass and,
332 // if enabled, the function merging pass.
333 if (OptLevel == 0) {
334 addPGOInstrPasses(MPM);
335 if (Inliner) {
336 MPM.add(Inliner);
337 Inliner = nullptr;
338 }
339
340 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
341 // creates a CGSCC pass manager, but we don't want to add extensions into
342 // that pass manager. To prevent this we insert a no-op module pass to reset
343 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
344 // builds. The function merging pass is
345 if (MergeFunctions)
346 MPM.add(createMergeFunctionsPass());
347 else if (!GlobalExtensions->empty() || !Extensions.empty())
348 MPM.add(createBarrierNoopPass());
349
350 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
351 return;
352 }
353
354 // Add LibraryInfo if we have some.
355 if (LibraryInfo)
356 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
357
358 addInitialAliasAnalysisPasses(MPM);
359
360 if (!DisableUnitAtATime) {
361 // Infer attributes about declarations if possible.
362 MPM.add(createInferFunctionAttrsLegacyPass());
363
364 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
365
366 MPM.add(createIPSCCPPass()); // IP SCCP
367 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
368 // Promote any localized global vars.
369 MPM.add(createPromoteMemoryToRegisterPass());
370
371 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
372
Matthias Braunc31032d2016-03-09 18:47:11 +0000373 addInstructionCombiningPass(MPM); // Clean up after IPCP & DAE
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000374 addExtensionsToPM(EP_Peephole, MPM);
375 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
376 }
377
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000378 if (!PerformThinLTO)
379 /// PGO instrumentation is added during the compile phase for ThinLTO, do
380 /// not run it a second time
381 addPGOInstrPasses(MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000382
383 if (EnableNonLTOGlobalsModRef)
384 // We add a module alias analysis pass here. In part due to bugs in the
385 // analysis infrastructure this "works" in that the analysis stays alive
386 // for the entire SCC pass run below.
387 MPM.add(createGlobalsAAWrapperPass());
388
389 // Start of CallGraph SCC passes.
390 if (!DisableUnitAtATime)
391 MPM.add(createPruneEHPass()); // Remove dead EH info
392 if (Inliner) {
393 MPM.add(Inliner);
394 Inliner = nullptr;
395 }
396 if (!DisableUnitAtATime)
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000397 MPM.add(createPostOrderFunctionAttrsLegacyPass());
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000398 if (OptLevel > 2)
399 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
400
401 addFunctionSimplificationPasses(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000402
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000403 // If we are planning to perform ThinLTO later, let's not bloat the code with
404 // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes
405 // during ThinLTO and perform the rest of the optimizations afterward.
406 if (PrepareForThinLTO)
407 return;
408
Renato Golin729a3ae2013-12-05 21:20:02 +0000409 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
410 // pass manager that we are specifically trying to avoid. To prevent this
411 // we must insert a no-op module pass to reset the pass manager.
412 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000413
Ashutosh Nema2260a3a2016-02-11 09:23:53 +0000414 // Scheduling LoopVersioningLICM when inlining is over, because after that
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000415 // we may see more accurate aliasing. Reason to run this late is that too
416 // early versioning may prevent further inlining due to increase of code
417 // size. By placing it just after inlining other optimizations which runs
418 // later might get benefit of no-alias assumption in clone loop.
419 if (UseLoopVersioningLICM) {
420 MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM
421 MPM.add(createLICMPass()); // Hoist loop invariants
422 }
423
Chandler Carruth1926b702016-01-08 10:55:52 +0000424 if (!DisableUnitAtATime)
425 MPM.add(createReversePostOrderFunctionAttrsPass());
426
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000427 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO)
Yaron Keren611c7cf2015-09-02 06:34:11 +0000428 // Remove avail extern fns and globals definitions if we aren't
429 // compiling an object file for later LTO. For LTO we want to preserve
430 // these so they are eligible for inlining at link-time. Note if they
431 // are unreferenced they will be removed by GlobalDCE later, so
432 // this only impacts referenced available externally globals.
433 // Eventually they will be suppressed during codegen, but eliminating
434 // here enables more opportunity for GlobalDCE as it may make
435 // globals referenced by available external functions dead
436 // and saves running remaining passes on the eliminated functions.
437 MPM.add(createEliminateAvailableExternallyPass());
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000438
439 if (PerformThinLTO) {
440 // Remove dead fns and globals. Removing unreferenced functions could lead
441 // to more opportunities for globalopt.
442 MPM.add(createGlobalDCEPass());
443 MPM.add(createGlobalOptimizerPass());
444 // Remove dead fns and globals after globalopt.
445 MPM.add(createGlobalDCEPass());
446 addFunctionSimplificationPasses(MPM);
Yaron Keren611c7cf2015-09-02 06:34:11 +0000447 }
448
Chandler Carruth08eebe22015-07-23 09:34:01 +0000449 if (EnableNonLTOGlobalsModRef)
450 // We add a fresh GlobalsModRef run at this point. This is particularly
451 // useful as the above will have inlined, DCE'ed, and function-attr
452 // propagated everything. We should at this point have a reasonably minimal
453 // and richly annotated call graph. By computing aliasing and mod/ref
454 // information for all local globals here, the late loop passes and notably
455 // the vectorizer will be able to use them to help recognize vectorizable
456 // memory operations.
457 //
458 // Note that this relies on a bug in the pass manager which preserves
459 // a module analysis into a function pass pipeline (and throughout it) so
460 // long as the first function pass doesn't invalidate the module analysis.
461 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
462 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
463 // (doing nothing preserves it as it is required to be conservatively
464 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000465 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000466
James Molloy0cbb2a862015-03-27 10:36:57 +0000467 if (RunFloat2Int)
468 MPM.add(createFloat2IntPass());
469
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000470 addExtensionsToPM(EP_VectorizerStart, MPM);
471
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000472 // Re-rotate loops in all our loop nests. These may have fallout out of
473 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000474 // on the rotated form. Disable header duplication at -Oz.
475 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000476
Adam Nemet938d3d62015-05-14 12:05:18 +0000477 // Distribute loops to allow partial vectorization. I.e. isolate dependences
478 // into separate loop that would otherwise inhibit vectorization.
479 if (EnableLoopDistribute)
480 MPM.add(createLoopDistributePass());
481
Renato Golin729a3ae2013-12-05 21:20:02 +0000482 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000483
484 // Eliminate loads by forwarding stores from the previous iteration to loads
485 // of the current iteration.
486 if (EnableLoopLoadElim)
487 MPM.add(createLoopLoadEliminationPass());
488
Renato Golin729a3ae2013-12-05 21:20:02 +0000489 // FIXME: Because of #pragma vectorize enable, the passes below are always
490 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
491 // on -O1 and no #pragma is found). Would be good to have these two passes
492 // as function calls, so that we can only pass them when the vectorizer
493 // changed the code.
Matthias Braunc31032d2016-03-09 18:47:11 +0000494 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000495 if (OptLevel > 1 && ExtraVectorizerPasses) {
496 // At higher optimization levels, try to clean up any runtime overlap and
497 // alignment checks inserted by the vectorizer. We want to track correllated
498 // runtime checks for two inner loops in the same outer loop, fold any
499 // common computations, hoist loop-invariant aspects out of any outer loop,
500 // and unswitch the runtime checks if possible. Once hoisted, we may have
501 // dead (or speculatable) control flows or more combining opportunities.
502 MPM.add(createEarlyCSEPass());
503 MPM.add(createCorrelatedValuePropagationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000504 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000505 MPM.add(createLICMPass());
506 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
507 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000508 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000509 }
James Molloy568da092014-08-06 12:56:19 +0000510
511 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000512 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000513 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000514 if (OptLevel > 1 && ExtraVectorizerPasses) {
515 MPM.add(createEarlyCSEPass());
516 }
517 }
James Molloy568da092014-08-06 12:56:19 +0000518
519 if (BBVectorize) {
520 MPM.add(createBBVectorizePass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000521 addInstructionCombiningPass(MPM);
James Molloy568da092014-08-06 12:56:19 +0000522 addExtensionsToPM(EP_Peephole, MPM);
523 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000524 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000525 else
526 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
527
528 // BBVectorize may have significantly shortened a loop body; unroll again.
529 if (!DisableUnrollLoops)
530 MPM.add(createLoopUnrollPass());
531 }
532 }
533
Peter Collingbourne0a437612014-05-25 10:27:02 +0000534 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000535 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000536 addInstructionCombiningPass(MPM);
Chandler Carruth08e1b872013-06-24 07:21:47 +0000537
Kevin Qin49bc7642015-03-12 05:36:01 +0000538 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000539 MPM.add(createLoopUnrollPass()); // Unroll small loops
540
Wei Mibf727ba2015-05-14 22:02:54 +0000541 // LoopUnroll may generate some redundency to cleanup.
Matthias Braunc31032d2016-03-09 18:47:11 +0000542 addInstructionCombiningPass(MPM);
Wei Mibf727ba2015-05-14 22:02:54 +0000543
Kevin Qin49bc7642015-03-12 05:36:01 +0000544 // Runtime unrolling will introduce runtime check in loop prologue. If the
545 // unrolled loop is a inner loop, then the prologue will be inside the
546 // outer loop. LICM pass can help to promote the runtime check out if the
547 // checked value is loop invariant.
548 MPM.add(createLICMPass());
549 }
550
Hal Finkeld67e4632014-09-07 20:05:11 +0000551 // After vectorization and unrolling, assume intrinsics may tell us more
552 // about pointer alignments.
553 MPM.add(createAlignmentFromAssumptionsPass());
554
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000555 if (!DisableUnitAtATime) {
556 // FIXME: We shouldn't bother with this anymore.
557 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
558
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000559 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000560 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000561 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000562 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000563 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000564 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000565 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000566
567 if (MergeFunctions)
568 MPM.add(createMergeFunctionsPass());
569
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000570 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000571}
572
Chandler Carruth30d69c22015-02-13 10:01:29 +0000573void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000574 // Provide AliasAnalysis services for optimizations.
575 addInitialAliasAnalysisPasses(PM);
576
Teresa Johnson26ab5772016-03-15 00:04:37 +0000577 if (ModuleSummary)
578 PM.add(createFunctionImportPass(ModuleSummary));
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000579
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000580 // Allow forcing function attributes as a debugging and tuning aid.
581 PM.add(createForceFunctionAttrsLegacyPass());
582
Chandler Carruth3a040e62015-12-27 08:41:34 +0000583 // Infer attributes about declarations if possible.
584 PM.add(createInferFunctionAttrsLegacyPass());
585
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000586 // Propagate constants at call sites into the functions they call. This
587 // opens opportunities for globalopt (and inlining) by substituting function
588 // pointers passed as arguments to direct uses of functions.
589 PM.add(createIPSCCPPass());
590
591 // Now that we internalized some globals, see if we can hack on them!
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000592 PM.add(createPostOrderFunctionAttrsLegacyPass());
Chandler Carruth1926b702016-01-08 10:55:52 +0000593 PM.add(createReversePostOrderFunctionAttrsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000594 PM.add(createGlobalOptimizerPass());
James Molloy6045cc82015-12-15 09:24:01 +0000595 // Promote any localized global vars.
596 PM.add(createPromoteMemoryToRegisterPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000597
598 // Linking modules together can lead to duplicated global constants, only
599 // keep one copy of each constant.
600 PM.add(createConstantMergePass());
601
602 // Remove unused arguments from functions.
603 PM.add(createDeadArgEliminationPass());
604
605 // Reduce the code after globalopt and ipsccp. Both can open up significant
606 // simplification opportunities, and both can propagate functions through
607 // function pointers. When this happens, we often have to resolve varargs
608 // calls, etc, so let instcombine do this.
Matthias Braunc31032d2016-03-09 18:47:11 +0000609 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000610 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000611
612 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000613 bool RunInliner = Inliner;
614 if (RunInliner) {
615 PM.add(Inliner);
616 Inliner = nullptr;
617 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000618
619 PM.add(createPruneEHPass()); // Remove dead EH info.
620
621 // Optimize globals again if we ran the inliner.
622 if (RunInliner)
623 PM.add(createGlobalOptimizerPass());
624 PM.add(createGlobalDCEPass()); // Remove dead functions.
625
626 // If we didn't decide to inline a function, check to see if we can
627 // transform it to pass arguments by value instead of by reference.
628 PM.add(createArgumentPromotionPass());
629
630 // The IPO passes may leave cruft around. Clean up after them.
Matthias Braunc31032d2016-03-09 18:47:11 +0000631 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000632 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000633 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000634
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000635 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000636 if (UseNewSROA)
637 PM.add(createSROAPass());
638 else
639 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000640
641 // Run a few AA driven optimizations here and now, to cleanup the code.
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000642 PM.add(createPostOrderFunctionAttrsLegacyPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000643 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000644
Bill Wendling932b9922012-04-02 22:16:50 +0000645 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000646 if (EnableMLSM)
647 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000648 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
649 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000650
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000651 // Nuke dead stores.
652 PM.add(createDeadStoreEliminationPass());
653
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000654 // More loops are countable; try to optimize them.
655 PM.add(createIndVarSimplifyPass());
656 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000657 if (EnableLoopInterchange)
658 PM.add(createLoopInterchangePass());
659
James Molloy31f3ddd2016-01-14 15:00:09 +0000660 if (!DisableUnrollLoops)
661 PM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000662 PM.add(createLoopVectorizePass(true, LoopVectorize));
James Molloy31f3ddd2016-01-14 15:00:09 +0000663 // The vectorizer may have significantly shortened a loop body; unroll again.
664 if (!DisableUnrollLoops)
665 PM.add(createLoopUnrollPass());
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000666
James Molloy6045cc82015-12-15 09:24:01 +0000667 // Now that we've optimized loops (in particular loop induction variables),
668 // we may have exposed more scalar opportunities. Run parts of the scalar
669 // optimizer again at this point.
Matthias Braunc31032d2016-03-09 18:47:11 +0000670 addInstructionCombiningPass(PM); // Initial cleanup
James Molloy6045cc82015-12-15 09:24:01 +0000671 PM.add(createCFGSimplificationPass()); // if-convert
672 PM.add(createSCCPPass()); // Propagate exposed constants
Matthias Braunc31032d2016-03-09 18:47:11 +0000673 addInstructionCombiningPass(PM); // Clean up again
James Molloy6045cc82015-12-15 09:24:01 +0000674 PM.add(createBitTrackingDCEPass());
675
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000676 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000677 if (RunSLPAfterLoopVectorization)
678 if (SLPVectorize)
679 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000680
Hal Finkeld67e4632014-09-07 20:05:11 +0000681 // After vectorization, assume intrinsics may tell us more about pointer
682 // alignments.
683 PM.add(createAlignmentFromAssumptionsPass());
684
Michael J. Spencer289067c2014-05-29 01:55:07 +0000685 if (LoadCombine)
686 PM.add(createLoadCombinePass());
687
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000688 // Cleanup and simplify the code after the scalar optimizations.
Matthias Braunc31032d2016-03-09 18:47:11 +0000689 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000690 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000691
692 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000693}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000694
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000695void PassManagerBuilder::addEarlyLTOOptimizationPasses(
696 legacy::PassManagerBase &PM) {
697 // Remove unused virtual tables to improve the quality of code generated by
698 // whole-program devirtualization and bitset lowering.
699 PM.add(createGlobalDCEPass());
700
701 // Apply whole-program devirtualization and virtual constant propagation.
702 PM.add(createWholeProgramDevirtPass());
703}
704
Peter Collingbourne070843d2015-03-19 22:01:00 +0000705void PassManagerBuilder::addLateLTOOptimizationPasses(
706 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000707 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000708 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000709
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000710 // Drop bodies of available externally objects to improve GlobalDCE.
711 PM.add(createEliminateAvailableExternallyPass());
712
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000713 // Now that we have optimized the program, discard unreachable functions.
714 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000715
716 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
717 // currently it damages debug info.
718 if (MergeFunctions)
719 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000720}
Rafael Espindola07f609152011-08-09 22:17:34 +0000721
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000722void PassManagerBuilder::populateThinLTOPassManager(
723 legacy::PassManagerBase &PM) {
724 PerformThinLTO = true;
725
726 if (VerifyInput)
727 PM.add(createVerifierPass());
728
Teresa Johnson26ab5772016-03-15 00:04:37 +0000729 if (ModuleSummary)
730 PM.add(createFunctionImportPass(ModuleSummary));
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000731
732 populateModulePassManager(PM);
733
734 if (VerifyOutput)
735 PM.add(createVerifierPass());
736 PerformThinLTO = false;
737}
738
Chandler Carruth30d69c22015-02-13 10:01:29 +0000739void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000740 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000741 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000742
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000743 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000744 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000745
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000746 if (OptLevel != 0)
747 addEarlyLTOOptimizationPasses(PM);
748
Peter Collingbourne070843d2015-03-19 22:01:00 +0000749 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000750 addLTOOptimizationPasses(PM);
751
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000752 // Create a function that performs CFI checks for cross-DSO calls with targets
753 // in the current module.
754 PM.add(createCrossDSOCFIPass());
755
Peter Collingbourne070843d2015-03-19 22:01:00 +0000756 // Lower bit sets to globals. This pass supports Clang's control flow
757 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
758 // is enabled. The pass does nothing if CFI is disabled.
759 PM.add(createLowerBitSetsPass());
760
761 if (OptLevel != 0)
762 addLateLTOOptimizationPasses(PM);
763
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000764 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000765 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000766}
767
Eric Christopher04d4e932013-04-22 22:47:22 +0000768inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
769 return reinterpret_cast<PassManagerBuilder*>(P);
770}
771
772inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
773 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
774}
775
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000776LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000777 PassManagerBuilder *PMB = new PassManagerBuilder();
778 return wrap(PMB);
779}
780
781void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
782 PassManagerBuilder *Builder = unwrap(PMB);
783 delete Builder;
784}
785
786void
787LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
788 unsigned OptLevel) {
789 PassManagerBuilder *Builder = unwrap(PMB);
790 Builder->OptLevel = OptLevel;
791}
792
793void
794LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
795 unsigned SizeLevel) {
796 PassManagerBuilder *Builder = unwrap(PMB);
797 Builder->SizeLevel = SizeLevel;
798}
799
800void
801LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
802 LLVMBool Value) {
803 PassManagerBuilder *Builder = unwrap(PMB);
804 Builder->DisableUnitAtATime = Value;
805}
806
807void
808LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
809 LLVMBool Value) {
810 PassManagerBuilder *Builder = unwrap(PMB);
811 Builder->DisableUnrollLoops = Value;
812}
813
814void
815LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
816 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000817 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000818}
819
820void
821LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
822 unsigned Threshold) {
823 PassManagerBuilder *Builder = unwrap(PMB);
824 Builder->Inliner = createFunctionInliningPass(Threshold);
825}
826
827void
828LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
829 LLVMPassManagerRef PM) {
830 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000831 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000832 Builder->populateFunctionPassManager(*FPM);
833}
834
835void
836LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
837 LLVMPassManagerRef PM) {
838 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000839 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000840 Builder->populateModulePassManager(*MPM);
841}
842
843void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
844 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000845 LLVMBool Internalize,
846 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000847 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000848 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000849
850 // A small backwards compatibility hack. populateLTOPassManager used to take
851 // an RunInliner option.
852 if (RunInliner && !Builder->Inliner)
853 Builder->Inliner = createFunctionInliningPass();
854
855 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000856}