blob: 71ec8aeafa27e82c0a5f8effac71890942d21e82 [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
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000098static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +000099 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000100 cl::desc(
101 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
102
Adam Nemete54a4fa2015-11-03 23:50:08 +0000103static cl::opt<bool> EnableLoopLoadElim(
Adam Nemetc979c6e2016-03-15 22:26:12 +0000104 "enable-loop-load-elim", cl::init(true), cl::Hidden,
105 cl::desc("Enable the LoopLoadElimination Pass"));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000106
Rong Xu34abbfb2016-01-21 18:28:59 +0000107static cl::opt<std::string> RunPGOInstrGen(
108 "profile-generate", cl::init(""), cl::Hidden,
109 cl::desc("Enable generation phase of PGO instrumentation and specify the "
110 "path of profile data file"));
111
112static cl::opt<std::string> RunPGOInstrUse(
113 "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"),
114 cl::desc("Enable use phase of PGO instrumentation and specify the path "
115 "of profile data file"));
116
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000117static cl::opt<bool> UseLoopVersioningLICM(
118 "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
119 cl::desc("Enable the experimental Loop Versioning LICM pass"));
120
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000121PassManagerBuilder::PassManagerBuilder() {
122 OptLevel = 2;
123 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000124 LibraryInfo = nullptr;
125 Inliner = nullptr;
Teresa Johnson26ab5772016-03-15 00:04:37 +0000126 ModuleSummary = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000127 DisableUnitAtATime = false;
128 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000129 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000130 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000131 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000132 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000133 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000134 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000135 VerifyInput = false;
136 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000137 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000138 PrepareForLTO = false;
Rong Xu34abbfb2016-01-21 18:28:59 +0000139 PGOInstrGen = RunPGOInstrGen;
140 PGOInstrUse = RunPGOInstrUse;
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000141 PrepareForThinLTO = false;
142 PerformThinLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000143}
144
145PassManagerBuilder::~PassManagerBuilder() {
146 delete LibraryInfo;
147 delete Inliner;
148}
149
David Chisnall719a72f2011-08-16 13:58:41 +0000150/// Set of global extensions, automatically added as part of the standard set.
151static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
152 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
153
154void PassManagerBuilder::addGlobalExtension(
155 PassManagerBuilder::ExtensionPointTy Ty,
156 PassManagerBuilder::ExtensionFn Fn) {
Justin Lebar2fe13232016-03-30 20:39:29 +0000157 GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn)));
David Chisnall719a72f2011-08-16 13:58:41 +0000158}
159
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000160void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
Justin Lebar2fe13232016-03-30 20:39:29 +0000161 Extensions.push_back(std::make_pair(Ty, std::move(Fn)));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000162}
163
164void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000165 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000166 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
167 if ((*GlobalExtensions)[i].first == ETy)
168 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000169 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
170 if (Extensions[i].first == ETy)
171 Extensions[i].second(*this, PM);
172}
173
Chandler Carruth30d69c22015-02-13 10:01:29 +0000174void PassManagerBuilder::addInitialAliasAnalysisPasses(
175 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000176 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
177 // BasicAliasAnalysis wins if they disagree. This is intended to help
178 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000179 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000180 PM.add(createCFLAAWrapperPass());
181 PM.add(createTypeBasedAAWrapperPass());
182 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000183}
184
Matthias Braunc31032d2016-03-09 18:47:11 +0000185void PassManagerBuilder::addInstructionCombiningPass(
186 legacy::PassManagerBase &PM) const {
187 bool ExpensiveCombines = OptLevel > 2;
188 PM.add(createInstructionCombiningPass(ExpensiveCombines));
189}
190
Chandler Carruth30d69c22015-02-13 10:01:29 +0000191void PassManagerBuilder::populateFunctionPassManager(
192 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000193 addExtensionsToPM(EP_EarlyAsPossible, FPM);
194
195 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000196 if (LibraryInfo)
197 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000198
199 if (OptLevel == 0) return;
200
201 addInitialAliasAnalysisPasses(FPM);
202
203 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000204 if (UseNewSROA)
205 FPM.add(createSROAPass());
206 else
207 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000208 FPM.add(createEarlyCSEPass());
209 FPM.add(createLowerExpectIntrinsicPass());
210}
211
Rong Xu34abbfb2016-01-21 18:28:59 +0000212// Do PGO instrumentation generation or use pass as the option specified.
213void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
214 if (!PGOInstrGen.empty()) {
215 MPM.add(createPGOInstrumentationGenPass());
216 // Add the profile lowering pass.
217 InstrProfOptions Options;
218 Options.InstrProfileOutput = PGOInstrGen;
Xinliang David Lie6b89292016-04-18 17:47:38 +0000219 MPM.add(createInstrProfilingLegacyPass(Options));
Rong Xu34abbfb2016-01-21 18:28:59 +0000220 }
221 if (!PGOInstrUse.empty())
222 MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
223}
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000224void PassManagerBuilder::addFunctionSimplificationPasses(
Mehdi Amini9c1c3ac2016-02-11 22:09:11 +0000225 legacy::PassManagerBase &MPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000226 // Start of function pass.
227 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000228 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000229 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000230 else
231 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000232 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Justin Lebarcf63b642016-04-15 00:32:12 +0000233 // Speculative execution if the target has divergent branches; otherwise nop.
234 MPM.add(createSpeculativeExecutionIfHasBranchDivergencePass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000235 MPM.add(createJumpThreadingPass()); // Thread jumps.
236 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
237 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Matthias Braunc31032d2016-03-09 18:47:11 +0000238 // Combine silly seq's
239 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000240 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000241
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000242 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
244 MPM.add(createReassociatePass()); // Reassociate expressions
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000245 if (PrepareForThinLTO) {
246 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Matthias Braunc31032d2016-03-09 18:47:11 +0000247 addInstructionCombiningPass(MPM); // Combine silly seq's
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000248 return;
249 }
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000250 // Rotate Loop - disable header duplication at -Oz
251 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000252 MPM.add(createLICMPass()); // Hoist loop invariants
253 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000254 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000255 addInstructionCombiningPass(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
257 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
258 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000259 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000260 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000261 MPM.add(createCFGSimplificationPass());
262 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000263 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000264 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000265 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
266
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000267 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000268 if (EnableMLSM)
269 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000270 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000271 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000272 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
273 MPM.add(createSCCPPass()); // Constant prop with SCCP
274
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000275 // Delete dead bit computations (instcombine runs after to fold away the dead
276 // computations, and then ADCE will run later to exploit any new DCE
277 // opportunities that creates).
278 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
279
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000280 // Run instcombine after redundancy elimination to exploit opportunities
281 // opened up by them.
Matthias Braunc31032d2016-03-09 18:47:11 +0000282 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000283 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000284 MPM.add(createJumpThreadingPass()); // Thread jumps
285 MPM.add(createCorrelatedValuePropagationPass());
286 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000287 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000288
289 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
290
Hal Finkel29aeb202013-11-17 16:02:50 +0000291 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000292 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000293 if (!RunSLPAfterLoopVectorization) {
294 if (SLPVectorize)
295 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000296
James Molloy568da092014-08-06 12:56:19 +0000297 if (BBVectorize) {
298 MPM.add(createBBVectorizePass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000299 addInstructionCombiningPass(MPM);
James Molloy568da092014-08-06 12:56:19 +0000300 addExtensionsToPM(EP_Peephole, MPM);
301 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000302 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000303 else
304 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000305
James Molloy568da092014-08-06 12:56:19 +0000306 // BBVectorize may have significantly shortened a loop body; unroll again.
307 if (!DisableUnrollLoops)
308 MPM.add(createLoopUnrollPass());
309 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000310 }
311
Michael J. Spencer289067c2014-05-29 01:55:07 +0000312 if (LoadCombine)
313 MPM.add(createLoadCombinePass());
314
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000315 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000316 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Matthias Braunc31032d2016-03-09 18:47:11 +0000317 // Clean up after everything.
318 addInstructionCombiningPass(MPM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000319 addExtensionsToPM(EP_Peephole, MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000320}
321
322void PassManagerBuilder::populateModulePassManager(
323 legacy::PassManagerBase &MPM) {
324 // Allow forcing function attributes as a debugging and tuning aid.
325 MPM.add(createForceFunctionAttrsLegacyPass());
326
327 // If all optimizations are disabled, just run the always-inline pass and,
328 // if enabled, the function merging pass.
329 if (OptLevel == 0) {
330 addPGOInstrPasses(MPM);
331 if (Inliner) {
332 MPM.add(Inliner);
333 Inliner = nullptr;
334 }
335
336 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
337 // creates a CGSCC pass manager, but we don't want to add extensions into
338 // that pass manager. To prevent this we insert a no-op module pass to reset
339 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
340 // builds. The function merging pass is
341 if (MergeFunctions)
342 MPM.add(createMergeFunctionsPass());
343 else if (!GlobalExtensions->empty() || !Extensions.empty())
344 MPM.add(createBarrierNoopPass());
345
346 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
347 return;
348 }
349
350 // Add LibraryInfo if we have some.
351 if (LibraryInfo)
352 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
353
354 addInitialAliasAnalysisPasses(MPM);
355
356 if (!DisableUnitAtATime) {
357 // Infer attributes about declarations if possible.
358 MPM.add(createInferFunctionAttrsLegacyPass());
359
360 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
361
362 MPM.add(createIPSCCPPass()); // IP SCCP
363 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
364 // Promote any localized global vars.
365 MPM.add(createPromoteMemoryToRegisterPass());
366
367 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
368
Matthias Braunc31032d2016-03-09 18:47:11 +0000369 addInstructionCombiningPass(MPM); // Clean up after IPCP & DAE
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000370 addExtensionsToPM(EP_Peephole, MPM);
371 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
372 }
373
Rong Xu6e34c492016-04-27 23:20:27 +0000374 if (!PerformThinLTO) {
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000375 /// PGO instrumentation is added during the compile phase for ThinLTO, do
376 /// not run it a second time
377 addPGOInstrPasses(MPM);
Rong Xu6e34c492016-04-27 23:20:27 +0000378 // Indirect call promotion that promotes intra-module targets only.
379 MPM.add(createPGOIndirectCallPromotionPass());
380 }
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000381
382 if (EnableNonLTOGlobalsModRef)
383 // We add a module alias analysis pass here. In part due to bugs in the
384 // analysis infrastructure this "works" in that the analysis stays alive
385 // for the entire SCC pass run below.
386 MPM.add(createGlobalsAAWrapperPass());
387
388 // Start of CallGraph SCC passes.
389 if (!DisableUnitAtATime)
390 MPM.add(createPruneEHPass()); // Remove dead EH info
391 if (Inliner) {
392 MPM.add(Inliner);
393 Inliner = nullptr;
394 }
395 if (!DisableUnitAtATime)
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000396 MPM.add(createPostOrderFunctionAttrsLegacyPass());
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000397 if (OptLevel > 2)
398 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
399
400 addFunctionSimplificationPasses(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000401
Mehdi Amini45c7b3e2016-05-02 16:53:16 +0000402 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
403 // pass manager that we are specifically trying to avoid. To prevent this
404 // we must insert a no-op module pass to reset the pass manager.
405 MPM.add(createBarrierNoopPass());
406
407 if (!DisableUnitAtATime)
408 MPM.add(createReversePostOrderFunctionAttrsPass());
409
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000410 // If we are planning to perform ThinLTO later, let's not bloat the code with
411 // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes
412 // during ThinLTO and perform the rest of the optimizations afterward.
Mehdi Aminif72ca86b2016-04-25 08:47:37 +0000413 if (PrepareForThinLTO) {
Mehdi Aminibf4513b2016-04-25 08:47:49 +0000414 // Reduce the size of the IR as much as possible.
415 MPM.add(createGlobalOptimizerPass());
Mehdi Aminif72ca86b2016-04-25 08:47:37 +0000416 // Rename anon function to be able to export them in the summary.
417 MPM.add(createNameAnonFunctionPass());
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000418 return;
Mehdi Aminif72ca86b2016-04-25 08:47:37 +0000419 }
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000420
Ashutosh Nema2260a3a2016-02-11 09:23:53 +0000421 // Scheduling LoopVersioningLICM when inlining is over, because after that
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000422 // we may see more accurate aliasing. Reason to run this late is that too
423 // early versioning may prevent further inlining due to increase of code
424 // size. By placing it just after inlining other optimizations which runs
425 // later might get benefit of no-alias assumption in clone loop.
426 if (UseLoopVersioningLICM) {
427 MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM
428 MPM.add(createLICMPass()); // Hoist loop invariants
429 }
430
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000431 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO)
Yaron Keren611c7cf2015-09-02 06:34:11 +0000432 // Remove avail extern fns and globals definitions if we aren't
433 // compiling an object file for later LTO. For LTO we want to preserve
434 // these so they are eligible for inlining at link-time. Note if they
435 // are unreferenced they will be removed by GlobalDCE later, so
436 // this only impacts referenced available externally globals.
437 // Eventually they will be suppressed during codegen, but eliminating
438 // here enables more opportunity for GlobalDCE as it may make
439 // globals referenced by available external functions dead
440 // and saves running remaining passes on the eliminated functions.
441 MPM.add(createEliminateAvailableExternallyPass());
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000442
443 if (PerformThinLTO) {
444 // Remove dead fns and globals. Removing unreferenced functions could lead
445 // to more opportunities for globalopt.
446 MPM.add(createGlobalDCEPass());
447 MPM.add(createGlobalOptimizerPass());
448 // Remove dead fns and globals after globalopt.
449 MPM.add(createGlobalDCEPass());
450 addFunctionSimplificationPasses(MPM);
Yaron Keren611c7cf2015-09-02 06:34:11 +0000451 }
452
Chandler Carruth08eebe22015-07-23 09:34:01 +0000453 if (EnableNonLTOGlobalsModRef)
454 // We add a fresh GlobalsModRef run at this point. This is particularly
455 // useful as the above will have inlined, DCE'ed, and function-attr
456 // propagated everything. We should at this point have a reasonably minimal
457 // and richly annotated call graph. By computing aliasing and mod/ref
458 // information for all local globals here, the late loop passes and notably
459 // the vectorizer will be able to use them to help recognize vectorizable
460 // memory operations.
461 //
462 // Note that this relies on a bug in the pass manager which preserves
463 // a module analysis into a function pass pipeline (and throughout it) so
464 // long as the first function pass doesn't invalidate the module analysis.
465 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
466 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
467 // (doing nothing preserves it as it is required to be conservatively
468 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000469 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000470
James Molloy0cbb2a862015-03-27 10:36:57 +0000471 if (RunFloat2Int)
472 MPM.add(createFloat2IntPass());
473
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000474 addExtensionsToPM(EP_VectorizerStart, MPM);
475
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000476 // Re-rotate loops in all our loop nests. These may have fallout out of
477 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000478 // on the rotated form. Disable header duplication at -Oz.
479 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000480
Adam Nemet938d3d62015-05-14 12:05:18 +0000481 // Distribute loops to allow partial vectorization. I.e. isolate dependences
Adam Nemetd2fa4142016-04-27 05:28:18 +0000482 // into separate loop that would otherwise inhibit vectorization. This is
483 // currently only performed for loops marked with the metadata
484 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
485 MPM.add(createLoopDistributePass(/*ProcessAllLoopsByDefault=*/false));
Adam Nemet938d3d62015-05-14 12:05:18 +0000486
Renato Golin729a3ae2013-12-05 21:20:02 +0000487 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000488
489 // Eliminate loads by forwarding stores from the previous iteration to loads
490 // of the current iteration.
491 if (EnableLoopLoadElim)
492 MPM.add(createLoopLoadEliminationPass());
493
Renato Golin729a3ae2013-12-05 21:20:02 +0000494 // FIXME: Because of #pragma vectorize enable, the passes below are always
495 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
496 // on -O1 and no #pragma is found). Would be good to have these two passes
497 // as function calls, so that we can only pass them when the vectorizer
498 // changed the code.
Matthias Braunc31032d2016-03-09 18:47:11 +0000499 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000500 if (OptLevel > 1 && ExtraVectorizerPasses) {
501 // At higher optimization levels, try to clean up any runtime overlap and
502 // alignment checks inserted by the vectorizer. We want to track correllated
503 // runtime checks for two inner loops in the same outer loop, fold any
504 // common computations, hoist loop-invariant aspects out of any outer loop,
505 // and unswitch the runtime checks if possible. Once hoisted, we may have
506 // dead (or speculatable) control flows or more combining opportunities.
507 MPM.add(createEarlyCSEPass());
508 MPM.add(createCorrelatedValuePropagationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000509 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000510 MPM.add(createLICMPass());
511 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
512 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000513 addInstructionCombiningPass(MPM);
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000514 }
James Molloy568da092014-08-06 12:56:19 +0000515
516 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000517 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000518 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000519 if (OptLevel > 1 && ExtraVectorizerPasses) {
520 MPM.add(createEarlyCSEPass());
521 }
522 }
James Molloy568da092014-08-06 12:56:19 +0000523
524 if (BBVectorize) {
525 MPM.add(createBBVectorizePass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000526 addInstructionCombiningPass(MPM);
James Molloy568da092014-08-06 12:56:19 +0000527 addExtensionsToPM(EP_Peephole, MPM);
528 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000529 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000530 else
531 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
532
533 // BBVectorize may have significantly shortened a loop body; unroll again.
534 if (!DisableUnrollLoops)
535 MPM.add(createLoopUnrollPass());
536 }
537 }
538
Peter Collingbourne0a437612014-05-25 10:27:02 +0000539 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000540 MPM.add(createCFGSimplificationPass());
Matthias Braunc31032d2016-03-09 18:47:11 +0000541 addInstructionCombiningPass(MPM);
Chandler Carruth08e1b872013-06-24 07:21:47 +0000542
Kevin Qin49bc7642015-03-12 05:36:01 +0000543 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000544 MPM.add(createLoopUnrollPass()); // Unroll small loops
545
Wei Mibf727ba2015-05-14 22:02:54 +0000546 // LoopUnroll may generate some redundency to cleanup.
Matthias Braunc31032d2016-03-09 18:47:11 +0000547 addInstructionCombiningPass(MPM);
Wei Mibf727ba2015-05-14 22:02:54 +0000548
Kevin Qin49bc7642015-03-12 05:36:01 +0000549 // Runtime unrolling will introduce runtime check in loop prologue. If the
550 // unrolled loop is a inner loop, then the prologue will be inside the
551 // outer loop. LICM pass can help to promote the runtime check out if the
552 // checked value is loop invariant.
553 MPM.add(createLICMPass());
554 }
555
Hal Finkeld67e4632014-09-07 20:05:11 +0000556 // After vectorization and unrolling, assume intrinsics may tell us more
557 // about pointer alignments.
558 MPM.add(createAlignmentFromAssumptionsPass());
559
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000560 if (!DisableUnitAtATime) {
561 // FIXME: We shouldn't bother with this anymore.
562 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
563
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000564 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000565 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000566 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000567 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000568 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000569 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000570 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000571
572 if (MergeFunctions)
573 MPM.add(createMergeFunctionsPass());
574
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000575 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000576}
577
Chandler Carruth30d69c22015-02-13 10:01:29 +0000578void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000579 // Provide AliasAnalysis services for optimizations.
580 addInitialAliasAnalysisPasses(PM);
581
Teresa Johnson26ab5772016-03-15 00:04:37 +0000582 if (ModuleSummary)
583 PM.add(createFunctionImportPass(ModuleSummary));
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000584
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000585 // Allow forcing function attributes as a debugging and tuning aid.
586 PM.add(createForceFunctionAttrsLegacyPass());
587
Chandler Carruth3a040e62015-12-27 08:41:34 +0000588 // Infer attributes about declarations if possible.
589 PM.add(createInferFunctionAttrsLegacyPass());
590
Rong Xu6e34c492016-04-27 23:20:27 +0000591 // Indirect call promotion. This should promote all the targets that are left
592 // by the earlier promotion pass that promotes intra-module targets.
593 // This two-step promotion is to save the compile time. For LTO, it should
594 // produce the same result as if we only do promotion here.
595 PM.add(createPGOIndirectCallPromotionPass(true));
596
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000597 // Propagate constants at call sites into the functions they call. This
598 // opens opportunities for globalopt (and inlining) by substituting function
599 // pointers passed as arguments to direct uses of functions.
600 PM.add(createIPSCCPPass());
601
602 // Now that we internalized some globals, see if we can hack on them!
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000603 PM.add(createPostOrderFunctionAttrsLegacyPass());
Chandler Carruth1926b702016-01-08 10:55:52 +0000604 PM.add(createReversePostOrderFunctionAttrsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000605 PM.add(createGlobalOptimizerPass());
James Molloy6045cc82015-12-15 09:24:01 +0000606 // Promote any localized global vars.
607 PM.add(createPromoteMemoryToRegisterPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000608
609 // Linking modules together can lead to duplicated global constants, only
610 // keep one copy of each constant.
611 PM.add(createConstantMergePass());
612
613 // Remove unused arguments from functions.
614 PM.add(createDeadArgEliminationPass());
615
616 // Reduce the code after globalopt and ipsccp. Both can open up significant
617 // simplification opportunities, and both can propagate functions through
618 // function pointers. When this happens, we often have to resolve varargs
619 // calls, etc, so let instcombine do this.
Matthias Braunc31032d2016-03-09 18:47:11 +0000620 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000621 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000622
623 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000624 bool RunInliner = Inliner;
625 if (RunInliner) {
626 PM.add(Inliner);
627 Inliner = nullptr;
628 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000629
630 PM.add(createPruneEHPass()); // Remove dead EH info.
631
632 // Optimize globals again if we ran the inliner.
633 if (RunInliner)
634 PM.add(createGlobalOptimizerPass());
635 PM.add(createGlobalDCEPass()); // Remove dead functions.
636
637 // If we didn't decide to inline a function, check to see if we can
638 // transform it to pass arguments by value instead of by reference.
639 PM.add(createArgumentPromotionPass());
640
641 // The IPO passes may leave cruft around. Clean up after them.
Matthias Braunc31032d2016-03-09 18:47:11 +0000642 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000643 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000644 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000645
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000646 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000647 if (UseNewSROA)
648 PM.add(createSROAPass());
649 else
650 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000651
652 // Run a few AA driven optimizations here and now, to cleanup the code.
Chandler Carruth9c4ed172016-02-18 11:03:11 +0000653 PM.add(createPostOrderFunctionAttrsLegacyPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000654 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000655
Bill Wendling932b9922012-04-02 22:16:50 +0000656 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000657 if (EnableMLSM)
658 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000659 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
660 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000661
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000662 // Nuke dead stores.
663 PM.add(createDeadStoreEliminationPass());
664
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000665 // More loops are countable; try to optimize them.
666 PM.add(createIndVarSimplifyPass());
667 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000668 if (EnableLoopInterchange)
669 PM.add(createLoopInterchangePass());
670
James Molloy31f3ddd2016-01-14 15:00:09 +0000671 if (!DisableUnrollLoops)
672 PM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000673 PM.add(createLoopVectorizePass(true, LoopVectorize));
James Molloy31f3ddd2016-01-14 15:00:09 +0000674 // The vectorizer may have significantly shortened a loop body; unroll again.
675 if (!DisableUnrollLoops)
676 PM.add(createLoopUnrollPass());
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000677
James Molloy6045cc82015-12-15 09:24:01 +0000678 // Now that we've optimized loops (in particular loop induction variables),
679 // we may have exposed more scalar opportunities. Run parts of the scalar
680 // optimizer again at this point.
Matthias Braunc31032d2016-03-09 18:47:11 +0000681 addInstructionCombiningPass(PM); // Initial cleanup
James Molloy6045cc82015-12-15 09:24:01 +0000682 PM.add(createCFGSimplificationPass()); // if-convert
683 PM.add(createSCCPPass()); // Propagate exposed constants
Matthias Braunc31032d2016-03-09 18:47:11 +0000684 addInstructionCombiningPass(PM); // Clean up again
James Molloy6045cc82015-12-15 09:24:01 +0000685 PM.add(createBitTrackingDCEPass());
686
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000687 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000688 if (RunSLPAfterLoopVectorization)
689 if (SLPVectorize)
690 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000691
Hal Finkeld67e4632014-09-07 20:05:11 +0000692 // After vectorization, assume intrinsics may tell us more about pointer
693 // alignments.
694 PM.add(createAlignmentFromAssumptionsPass());
695
Michael J. Spencer289067c2014-05-29 01:55:07 +0000696 if (LoadCombine)
697 PM.add(createLoadCombinePass());
698
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000699 // Cleanup and simplify the code after the scalar optimizations.
Matthias Braunc31032d2016-03-09 18:47:11 +0000700 addInstructionCombiningPass(PM);
Peter Collingbourne0a437612014-05-25 10:27:02 +0000701 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000702
703 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000704}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000705
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000706void PassManagerBuilder::addEarlyLTOOptimizationPasses(
707 legacy::PassManagerBase &PM) {
708 // Remove unused virtual tables to improve the quality of code generated by
709 // whole-program devirtualization and bitset lowering.
710 PM.add(createGlobalDCEPass());
711
712 // Apply whole-program devirtualization and virtual constant propagation.
713 PM.add(createWholeProgramDevirtPass());
714}
715
Peter Collingbourne070843d2015-03-19 22:01:00 +0000716void PassManagerBuilder::addLateLTOOptimizationPasses(
717 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000718 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000719 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000720
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000721 // Drop bodies of available externally objects to improve GlobalDCE.
722 PM.add(createEliminateAvailableExternallyPass());
723
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000724 // Now that we have optimized the program, discard unreachable functions.
725 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000726
727 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
728 // currently it damages debug info.
729 if (MergeFunctions)
730 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000731}
Rafael Espindola07f609152011-08-09 22:17:34 +0000732
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000733void PassManagerBuilder::populateThinLTOPassManager(
734 legacy::PassManagerBase &PM) {
735 PerformThinLTO = true;
736
737 if (VerifyInput)
738 PM.add(createVerifierPass());
739
Teresa Johnson26ab5772016-03-15 00:04:37 +0000740 if (ModuleSummary)
741 PM.add(createFunctionImportPass(ModuleSummary));
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000742
743 populateModulePassManager(PM);
744
745 if (VerifyOutput)
746 PM.add(createVerifierPass());
747 PerformThinLTO = false;
748}
749
Chandler Carruth30d69c22015-02-13 10:01:29 +0000750void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000751 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000752 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000753
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000754 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000755 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000756
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000757 if (OptLevel != 0)
758 addEarlyLTOOptimizationPasses(PM);
759
Peter Collingbourne070843d2015-03-19 22:01:00 +0000760 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000761 addLTOOptimizationPasses(PM);
762
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000763 // Create a function that performs CFI checks for cross-DSO calls with targets
764 // in the current module.
765 PM.add(createCrossDSOCFIPass());
766
Peter Collingbourne070843d2015-03-19 22:01:00 +0000767 // Lower bit sets to globals. This pass supports Clang's control flow
768 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
769 // is enabled. The pass does nothing if CFI is disabled.
770 PM.add(createLowerBitSetsPass());
771
772 if (OptLevel != 0)
773 addLateLTOOptimizationPasses(PM);
774
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000775 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000776 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000777}
778
Eric Christopher04d4e932013-04-22 22:47:22 +0000779inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
780 return reinterpret_cast<PassManagerBuilder*>(P);
781}
782
783inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
784 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
785}
786
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000787LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000788 PassManagerBuilder *PMB = new PassManagerBuilder();
789 return wrap(PMB);
790}
791
792void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
793 PassManagerBuilder *Builder = unwrap(PMB);
794 delete Builder;
795}
796
797void
798LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
799 unsigned OptLevel) {
800 PassManagerBuilder *Builder = unwrap(PMB);
801 Builder->OptLevel = OptLevel;
802}
803
804void
805LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
806 unsigned SizeLevel) {
807 PassManagerBuilder *Builder = unwrap(PMB);
808 Builder->SizeLevel = SizeLevel;
809}
810
811void
812LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
813 LLVMBool Value) {
814 PassManagerBuilder *Builder = unwrap(PMB);
815 Builder->DisableUnitAtATime = Value;
816}
817
818void
819LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
820 LLVMBool Value) {
821 PassManagerBuilder *Builder = unwrap(PMB);
822 Builder->DisableUnrollLoops = Value;
823}
824
825void
826LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
827 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000828 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000829}
830
831void
832LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
833 unsigned Threshold) {
834 PassManagerBuilder *Builder = unwrap(PMB);
835 Builder->Inliner = createFunctionInliningPass(Threshold);
836}
837
838void
839LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
840 LLVMPassManagerRef PM) {
841 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000842 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000843 Builder->populateFunctionPassManager(*FPM);
844}
845
846void
847LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
848 LLVMPassManagerRef PM) {
849 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000850 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000851 Builder->populateModulePassManager(*MPM);
852}
853
854void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
855 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000856 LLVMBool Internalize,
857 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000858 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000859 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000860
861 // A small backwards compatibility hack. populateLTOPassManager used to take
862 // an RunInliner option.
863 if (RunInliner && !Builder->Inliner)
864 Builder->Inliner = createFunctionInliningPass();
865
866 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000867}