blob: 266ba8e81eb7af8da47bd56faffa682097c7d0d0 [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"
26#include "llvm/IR/FunctionInfo.h"
27#include "llvm/IR/LegacyPassManager.h"
28#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 Carruth3a040e62015-12-27 08:41:34 +000034#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000035#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000036#include "llvm/Transforms/Vectorize.h"
Rong Xu34abbfb2016-01-21 18:28:59 +000037#include "llvm/Transforms/Instrumentation.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000038
39using namespace llvm;
40
Hal Finkelc34e5112012-02-01 03:51:43 +000041static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000042RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000043 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000044
45static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000046RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000047 cl::desc("Run the SLP vectorization passes"));
48
49static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000050RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000051 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000052
Hal Finkel204bf532012-04-13 17:15:33 +000053static cl::opt<bool>
54UseGVNAfterVectorization("use-gvn-after-vectorization",
55 cl::init(false), cl::Hidden,
56 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
57
Chandler Carruth7b8297a2014-10-14 00:31:29 +000058static cl::opt<bool> ExtraVectorizerPasses(
59 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
60 cl::desc("Run cleanup optimization passes after vectorization."));
61
Chandler Carruth1b398ae2012-09-14 09:22:59 +000062static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000063 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000064 cl::desc("Enable the new, experimental SROA pass"));
65
Hal Finkelbf45efd2013-11-16 23:59:05 +000066static cl::opt<bool>
67RunLoopRerolling("reroll-loops", cl::Hidden,
68 cl::desc("Run the loop rerolling pass"));
69
James Molloy0cbb2a862015-03-27 10:36:57 +000070static cl::opt<bool>
71RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
72 cl::desc("Run the float2int (float demotion) pass"));
73
Michael J. Spencer289067c2014-05-29 01:55:07 +000074static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
75 cl::Hidden,
76 cl::desc("Run the load combining pass"));
77
James Molloy568da092014-08-06 12:56:19 +000078static cl::opt<bool>
79RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000080 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000081 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
82 "vectorizer instead of before"));
83
Hal Finkel445dda52014-09-02 22:12:54 +000084static cl::opt<bool> UseCFLAA("use-cfl-aa",
85 cl::init(false), cl::Hidden,
86 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000087
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000088static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000089EnableMLSM("mlsm", cl::init(true), cl::Hidden,
90 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000091
Karthik Bhat88db86d2015-03-06 10:11:25 +000092static cl::opt<bool> EnableLoopInterchange(
93 "enable-loopinterchange", cl::init(false), cl::Hidden,
94 cl::desc("Enable the new, experimental LoopInterchange Pass"));
95
Adam Nemet938d3d62015-05-14 12:05:18 +000096static cl::opt<bool> EnableLoopDistribute(
97 "enable-loop-distribute", cl::init(false), cl::Hidden,
98 cl::desc("Enable the new, experimental LoopDistribution Pass"));
99
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000100static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +0000101 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000102 cl::desc(
103 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
104
Adam Nemete54a4fa2015-11-03 23:50:08 +0000105static cl::opt<bool> EnableLoopLoadElim(
106 "enable-loop-load-elim", cl::init(false), cl::Hidden,
107 cl::desc("Enable the new, experimental LoopLoadElimination Pass"));
108
Rong Xu34abbfb2016-01-21 18:28:59 +0000109static cl::opt<std::string> RunPGOInstrGen(
110 "profile-generate", cl::init(""), cl::Hidden,
111 cl::desc("Enable generation phase of PGO instrumentation and specify the "
112 "path of profile data file"));
113
114static cl::opt<std::string> RunPGOInstrUse(
115 "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"),
116 cl::desc("Enable use phase of PGO instrumentation and specify the path "
117 "of profile data file"));
118
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000119static cl::opt<bool> UseLoopVersioningLICM(
120 "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
121 cl::desc("Enable the experimental Loop Versioning LICM pass"));
122
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000123PassManagerBuilder::PassManagerBuilder() {
124 OptLevel = 2;
125 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000126 LibraryInfo = nullptr;
127 Inliner = nullptr;
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000128 FunctionIndex = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000129 DisableUnitAtATime = false;
130 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000131 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000132 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000133 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000134 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000135 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000136 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000137 VerifyInput = false;
138 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000139 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000140 PrepareForLTO = false;
Rong Xu34abbfb2016-01-21 18:28:59 +0000141 PGOInstrGen = RunPGOInstrGen;
142 PGOInstrUse = RunPGOInstrUse;
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000143 PrepareForThinLTO = false;
144 PerformThinLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000145}
146
147PassManagerBuilder::~PassManagerBuilder() {
148 delete LibraryInfo;
149 delete Inliner;
150}
151
David Chisnall719a72f2011-08-16 13:58:41 +0000152/// Set of global extensions, automatically added as part of the standard set.
153static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
154 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
155
156void PassManagerBuilder::addGlobalExtension(
157 PassManagerBuilder::ExtensionPointTy Ty,
158 PassManagerBuilder::ExtensionFn Fn) {
159 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
160}
161
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000162void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
163 Extensions.push_back(std::make_pair(Ty, Fn));
164}
165
166void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000167 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000168 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
169 if ((*GlobalExtensions)[i].first == ETy)
170 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000171 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
172 if (Extensions[i].first == ETy)
173 Extensions[i].second(*this, PM);
174}
175
Chandler Carruth30d69c22015-02-13 10:01:29 +0000176void PassManagerBuilder::addInitialAliasAnalysisPasses(
177 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000178 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
179 // BasicAliasAnalysis wins if they disagree. This is intended to help
180 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000181 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000182 PM.add(createCFLAAWrapperPass());
183 PM.add(createTypeBasedAAWrapperPass());
184 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000185}
186
Chandler Carruth30d69c22015-02-13 10:01:29 +0000187void PassManagerBuilder::populateFunctionPassManager(
188 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000189 addExtensionsToPM(EP_EarlyAsPossible, FPM);
190
191 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000192 if (LibraryInfo)
193 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000194
195 if (OptLevel == 0) return;
196
197 addInitialAliasAnalysisPasses(FPM);
198
199 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000200 if (UseNewSROA)
201 FPM.add(createSROAPass());
202 else
203 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000204 FPM.add(createEarlyCSEPass());
205 FPM.add(createLowerExpectIntrinsicPass());
206}
207
Rong Xu34abbfb2016-01-21 18:28:59 +0000208// Do PGO instrumentation generation or use pass as the option specified.
209void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
210 if (!PGOInstrGen.empty()) {
211 MPM.add(createPGOInstrumentationGenPass());
212 // Add the profile lowering pass.
213 InstrProfOptions Options;
214 Options.InstrProfileOutput = PGOInstrGen;
215 MPM.add(createInstrProfilingPass(Options));
216 }
217 if (!PGOInstrUse.empty())
218 MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
219}
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000220void PassManagerBuilder::addFunctionSimplificationPasses(
Mehdi Amini9c1c3ac2016-02-11 22:09:11 +0000221 legacy::PassManagerBase &MPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000222 // Start of function pass.
223 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000224 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000225 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000226 else
227 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000228 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000229 MPM.add(createJumpThreadingPass()); // Thread jumps.
230 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
231 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
232 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000233 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000234
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000235 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000236 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
237 MPM.add(createReassociatePass()); // Reassociate expressions
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000238 if (PrepareForThinLTO) {
239 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
240 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
241 return;
242 }
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000243 // Rotate Loop - disable header duplication at -Oz
244 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000245 MPM.add(createLICMPass()); // Hoist loop invariants
246 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000247 MPM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000248 MPM.add(createInstructionCombiningPass());
249 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
250 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
251 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000252 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000253 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000254 MPM.add(createCFGSimplificationPass());
255 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000257 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000258 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
259
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000260 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000261 if (EnableMLSM)
262 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000263 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000264 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000265 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
266 MPM.add(createSCCPPass()); // Constant prop with SCCP
267
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000268 // Delete dead bit computations (instcombine runs after to fold away the dead
269 // computations, and then ADCE will run later to exploit any new DCE
270 // opportunities that creates).
271 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
272
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000273 // Run instcombine after redundancy elimination to exploit opportunities
274 // opened up by them.
275 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000276 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000277 MPM.add(createJumpThreadingPass()); // Thread jumps
278 MPM.add(createCorrelatedValuePropagationPass());
279 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000280 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000281
282 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
283
Hal Finkel29aeb202013-11-17 16:02:50 +0000284 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000285 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000286 if (!RunSLPAfterLoopVectorization) {
287 if (SLPVectorize)
288 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000289
James Molloy568da092014-08-06 12:56:19 +0000290 if (BBVectorize) {
291 MPM.add(createBBVectorizePass());
292 MPM.add(createInstructionCombiningPass());
293 addExtensionsToPM(EP_Peephole, MPM);
294 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000295 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000296 else
297 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000298
James Molloy568da092014-08-06 12:56:19 +0000299 // BBVectorize may have significantly shortened a loop body; unroll again.
300 if (!DisableUnrollLoops)
301 MPM.add(createLoopUnrollPass());
302 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000303 }
304
Michael J. Spencer289067c2014-05-29 01:55:07 +0000305 if (LoadCombine)
306 MPM.add(createLoadCombinePass());
307
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000308 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000309 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000310 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000311 addExtensionsToPM(EP_Peephole, MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000312}
313
314void PassManagerBuilder::populateModulePassManager(
315 legacy::PassManagerBase &MPM) {
316 // Allow forcing function attributes as a debugging and tuning aid.
317 MPM.add(createForceFunctionAttrsLegacyPass());
318
319 // If all optimizations are disabled, just run the always-inline pass and,
320 // if enabled, the function merging pass.
321 if (OptLevel == 0) {
322 addPGOInstrPasses(MPM);
323 if (Inliner) {
324 MPM.add(Inliner);
325 Inliner = nullptr;
326 }
327
328 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
329 // creates a CGSCC pass manager, but we don't want to add extensions into
330 // that pass manager. To prevent this we insert a no-op module pass to reset
331 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
332 // builds. The function merging pass is
333 if (MergeFunctions)
334 MPM.add(createMergeFunctionsPass());
335 else if (!GlobalExtensions->empty() || !Extensions.empty())
336 MPM.add(createBarrierNoopPass());
337
338 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
339 return;
340 }
341
342 // Add LibraryInfo if we have some.
343 if (LibraryInfo)
344 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
345
346 addInitialAliasAnalysisPasses(MPM);
347
348 if (!DisableUnitAtATime) {
349 // Infer attributes about declarations if possible.
350 MPM.add(createInferFunctionAttrsLegacyPass());
351
352 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
353
354 MPM.add(createIPSCCPPass()); // IP SCCP
355 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
356 // Promote any localized global vars.
357 MPM.add(createPromoteMemoryToRegisterPass());
358
359 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
360
361 MPM.add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
362 addExtensionsToPM(EP_Peephole, MPM);
363 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
364 }
365
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000366 if (!PerformThinLTO)
367 /// PGO instrumentation is added during the compile phase for ThinLTO, do
368 /// not run it a second time
369 addPGOInstrPasses(MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000370
371 if (EnableNonLTOGlobalsModRef)
372 // We add a module alias analysis pass here. In part due to bugs in the
373 // analysis infrastructure this "works" in that the analysis stays alive
374 // for the entire SCC pass run below.
375 MPM.add(createGlobalsAAWrapperPass());
376
377 // Start of CallGraph SCC passes.
378 if (!DisableUnitAtATime)
379 MPM.add(createPruneEHPass()); // Remove dead EH info
380 if (Inliner) {
381 MPM.add(Inliner);
382 Inliner = nullptr;
383 }
384 if (!DisableUnitAtATime)
385 MPM.add(createPostOrderFunctionAttrsPass());
386 if (OptLevel > 2)
387 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
388
389 addFunctionSimplificationPasses(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000390
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000391 // If we are planning to perform ThinLTO later, let's not bloat the code with
392 // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes
393 // during ThinLTO and perform the rest of the optimizations afterward.
394 if (PrepareForThinLTO)
395 return;
396
Renato Golin729a3ae2013-12-05 21:20:02 +0000397 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
398 // pass manager that we are specifically trying to avoid. To prevent this
399 // we must insert a no-op module pass to reset the pass manager.
400 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000401
Ashutosh Nema2260a3a2016-02-11 09:23:53 +0000402 // Scheduling LoopVersioningLICM when inlining is over, because after that
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000403 // we may see more accurate aliasing. Reason to run this late is that too
404 // early versioning may prevent further inlining due to increase of code
405 // size. By placing it just after inlining other optimizations which runs
406 // later might get benefit of no-alias assumption in clone loop.
407 if (UseLoopVersioningLICM) {
408 MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM
409 MPM.add(createLICMPass()); // Hoist loop invariants
410 }
411
Chandler Carruth1926b702016-01-08 10:55:52 +0000412 if (!DisableUnitAtATime)
413 MPM.add(createReversePostOrderFunctionAttrsPass());
414
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000415 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO)
Yaron Keren611c7cf2015-09-02 06:34:11 +0000416 // Remove avail extern fns and globals definitions if we aren't
417 // compiling an object file for later LTO. For LTO we want to preserve
418 // these so they are eligible for inlining at link-time. Note if they
419 // are unreferenced they will be removed by GlobalDCE later, so
420 // this only impacts referenced available externally globals.
421 // Eventually they will be suppressed during codegen, but eliminating
422 // here enables more opportunity for GlobalDCE as it may make
423 // globals referenced by available external functions dead
424 // and saves running remaining passes on the eliminated functions.
425 MPM.add(createEliminateAvailableExternallyPass());
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000426
427 if (PerformThinLTO) {
428 // Remove dead fns and globals. Removing unreferenced functions could lead
429 // to more opportunities for globalopt.
430 MPM.add(createGlobalDCEPass());
431 MPM.add(createGlobalOptimizerPass());
432 // Remove dead fns and globals after globalopt.
433 MPM.add(createGlobalDCEPass());
434 addFunctionSimplificationPasses(MPM);
Yaron Keren611c7cf2015-09-02 06:34:11 +0000435 }
436
Chandler Carruth08eebe22015-07-23 09:34:01 +0000437 if (EnableNonLTOGlobalsModRef)
438 // We add a fresh GlobalsModRef run at this point. This is particularly
439 // useful as the above will have inlined, DCE'ed, and function-attr
440 // propagated everything. We should at this point have a reasonably minimal
441 // and richly annotated call graph. By computing aliasing and mod/ref
442 // information for all local globals here, the late loop passes and notably
443 // the vectorizer will be able to use them to help recognize vectorizable
444 // memory operations.
445 //
446 // Note that this relies on a bug in the pass manager which preserves
447 // a module analysis into a function pass pipeline (and throughout it) so
448 // long as the first function pass doesn't invalidate the module analysis.
449 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
450 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
451 // (doing nothing preserves it as it is required to be conservatively
452 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000453 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000454
James Molloy0cbb2a862015-03-27 10:36:57 +0000455 if (RunFloat2Int)
456 MPM.add(createFloat2IntPass());
457
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000458 addExtensionsToPM(EP_VectorizerStart, MPM);
459
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000460 // Re-rotate loops in all our loop nests. These may have fallout out of
461 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000462 // on the rotated form. Disable header duplication at -Oz.
463 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000464
Adam Nemet938d3d62015-05-14 12:05:18 +0000465 // Distribute loops to allow partial vectorization. I.e. isolate dependences
466 // into separate loop that would otherwise inhibit vectorization.
467 if (EnableLoopDistribute)
468 MPM.add(createLoopDistributePass());
469
Renato Golin729a3ae2013-12-05 21:20:02 +0000470 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000471
472 // Eliminate loads by forwarding stores from the previous iteration to loads
473 // of the current iteration.
474 if (EnableLoopLoadElim)
475 MPM.add(createLoopLoadEliminationPass());
476
Renato Golin729a3ae2013-12-05 21:20:02 +0000477 // FIXME: Because of #pragma vectorize enable, the passes below are always
478 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
479 // on -O1 and no #pragma is found). Would be good to have these two passes
480 // as function calls, so that we can only pass them when the vectorizer
481 // changed the code.
482 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000483 if (OptLevel > 1 && ExtraVectorizerPasses) {
484 // At higher optimization levels, try to clean up any runtime overlap and
485 // alignment checks inserted by the vectorizer. We want to track correllated
486 // runtime checks for two inner loops in the same outer loop, fold any
487 // common computations, hoist loop-invariant aspects out of any outer loop,
488 // and unswitch the runtime checks if possible. Once hoisted, we may have
489 // dead (or speculatable) control flows or more combining opportunities.
490 MPM.add(createEarlyCSEPass());
491 MPM.add(createCorrelatedValuePropagationPass());
492 MPM.add(createInstructionCombiningPass());
493 MPM.add(createLICMPass());
494 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
495 MPM.add(createCFGSimplificationPass());
496 MPM.add(createInstructionCombiningPass());
497 }
James Molloy568da092014-08-06 12:56:19 +0000498
499 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000500 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000501 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000502 if (OptLevel > 1 && ExtraVectorizerPasses) {
503 MPM.add(createEarlyCSEPass());
504 }
505 }
James Molloy568da092014-08-06 12:56:19 +0000506
507 if (BBVectorize) {
508 MPM.add(createBBVectorizePass());
509 MPM.add(createInstructionCombiningPass());
510 addExtensionsToPM(EP_Peephole, MPM);
511 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000512 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000513 else
514 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
515
516 // BBVectorize may have significantly shortened a loop body; unroll again.
517 if (!DisableUnrollLoops)
518 MPM.add(createLoopUnrollPass());
519 }
520 }
521
Peter Collingbourne0a437612014-05-25 10:27:02 +0000522 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000523 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000524 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000525
Kevin Qin49bc7642015-03-12 05:36:01 +0000526 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000527 MPM.add(createLoopUnrollPass()); // Unroll small loops
528
Wei Mibf727ba2015-05-14 22:02:54 +0000529 // LoopUnroll may generate some redundency to cleanup.
530 MPM.add(createInstructionCombiningPass());
531
Kevin Qin49bc7642015-03-12 05:36:01 +0000532 // Runtime unrolling will introduce runtime check in loop prologue. If the
533 // unrolled loop is a inner loop, then the prologue will be inside the
534 // outer loop. LICM pass can help to promote the runtime check out if the
535 // checked value is loop invariant.
536 MPM.add(createLICMPass());
537 }
538
Hal Finkeld67e4632014-09-07 20:05:11 +0000539 // After vectorization and unrolling, assume intrinsics may tell us more
540 // about pointer alignments.
541 MPM.add(createAlignmentFromAssumptionsPass());
542
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000543 if (!DisableUnitAtATime) {
544 // FIXME: We shouldn't bother with this anymore.
545 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
546
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000547 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000548 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000549 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000550 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000551 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000552 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000553 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000554
555 if (MergeFunctions)
556 MPM.add(createMergeFunctionsPass());
557
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000558 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000559}
560
Chandler Carruth30d69c22015-02-13 10:01:29 +0000561void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000562 // Provide AliasAnalysis services for optimizations.
563 addInitialAliasAnalysisPasses(PM);
564
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000565 if (FunctionIndex)
566 PM.add(createFunctionImportPass(FunctionIndex));
567
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000568 // Allow forcing function attributes as a debugging and tuning aid.
569 PM.add(createForceFunctionAttrsLegacyPass());
570
Chandler Carruth3a040e62015-12-27 08:41:34 +0000571 // Infer attributes about declarations if possible.
572 PM.add(createInferFunctionAttrsLegacyPass());
573
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000574 // Propagate constants at call sites into the functions they call. This
575 // opens opportunities for globalopt (and inlining) by substituting function
576 // pointers passed as arguments to direct uses of functions.
577 PM.add(createIPSCCPPass());
578
579 // Now that we internalized some globals, see if we can hack on them!
Chandler Carruth1926b702016-01-08 10:55:52 +0000580 PM.add(createPostOrderFunctionAttrsPass());
581 PM.add(createReversePostOrderFunctionAttrsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000582 PM.add(createGlobalOptimizerPass());
James Molloy6045cc82015-12-15 09:24:01 +0000583 // Promote any localized global vars.
584 PM.add(createPromoteMemoryToRegisterPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000585
586 // Linking modules together can lead to duplicated global constants, only
587 // keep one copy of each constant.
588 PM.add(createConstantMergePass());
589
590 // Remove unused arguments from functions.
591 PM.add(createDeadArgEliminationPass());
592
593 // Reduce the code after globalopt and ipsccp. Both can open up significant
594 // simplification opportunities, and both can propagate functions through
595 // function pointers. When this happens, we often have to resolve varargs
596 // calls, etc, so let instcombine do this.
597 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000598 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000599
600 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000601 bool RunInliner = Inliner;
602 if (RunInliner) {
603 PM.add(Inliner);
604 Inliner = nullptr;
605 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000606
607 PM.add(createPruneEHPass()); // Remove dead EH info.
608
609 // Optimize globals again if we ran the inliner.
610 if (RunInliner)
611 PM.add(createGlobalOptimizerPass());
612 PM.add(createGlobalDCEPass()); // Remove dead functions.
613
614 // If we didn't decide to inline a function, check to see if we can
615 // transform it to pass arguments by value instead of by reference.
616 PM.add(createArgumentPromotionPass());
617
618 // The IPO passes may leave cruft around. Clean up after them.
619 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000620 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000621 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000622
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000623 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000624 if (UseNewSROA)
625 PM.add(createSROAPass());
626 else
627 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000628
629 // Run a few AA driven optimizations here and now, to cleanup the code.
Chandler Carruth1926b702016-01-08 10:55:52 +0000630 PM.add(createPostOrderFunctionAttrsPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000631 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000632
Bill Wendling932b9922012-04-02 22:16:50 +0000633 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000634 if (EnableMLSM)
635 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000636 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
637 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000638
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000639 // Nuke dead stores.
640 PM.add(createDeadStoreEliminationPass());
641
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000642 // More loops are countable; try to optimize them.
643 PM.add(createIndVarSimplifyPass());
644 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000645 if (EnableLoopInterchange)
646 PM.add(createLoopInterchangePass());
647
James Molloy31f3ddd2016-01-14 15:00:09 +0000648 if (!DisableUnrollLoops)
649 PM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000650 PM.add(createLoopVectorizePass(true, LoopVectorize));
James Molloy31f3ddd2016-01-14 15:00:09 +0000651 // The vectorizer may have significantly shortened a loop body; unroll again.
652 if (!DisableUnrollLoops)
653 PM.add(createLoopUnrollPass());
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000654
James Molloy6045cc82015-12-15 09:24:01 +0000655 // Now that we've optimized loops (in particular loop induction variables),
656 // we may have exposed more scalar opportunities. Run parts of the scalar
657 // optimizer again at this point.
658 PM.add(createInstructionCombiningPass()); // Initial cleanup
659 PM.add(createCFGSimplificationPass()); // if-convert
660 PM.add(createSCCPPass()); // Propagate exposed constants
661 PM.add(createInstructionCombiningPass()); // Clean up again
662 PM.add(createBitTrackingDCEPass());
663
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000664 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000665 if (RunSLPAfterLoopVectorization)
666 if (SLPVectorize)
667 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000668
Hal Finkeld67e4632014-09-07 20:05:11 +0000669 // After vectorization, assume intrinsics may tell us more about pointer
670 // alignments.
671 PM.add(createAlignmentFromAssumptionsPass());
672
Michael J. Spencer289067c2014-05-29 01:55:07 +0000673 if (LoadCombine)
674 PM.add(createLoadCombinePass());
675
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000676 // Cleanup and simplify the code after the scalar optimizations.
677 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000678 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000679
680 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000681}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000682
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000683void PassManagerBuilder::addEarlyLTOOptimizationPasses(
684 legacy::PassManagerBase &PM) {
685 // Remove unused virtual tables to improve the quality of code generated by
686 // whole-program devirtualization and bitset lowering.
687 PM.add(createGlobalDCEPass());
688
689 // Apply whole-program devirtualization and virtual constant propagation.
690 PM.add(createWholeProgramDevirtPass());
691}
692
Peter Collingbourne070843d2015-03-19 22:01:00 +0000693void PassManagerBuilder::addLateLTOOptimizationPasses(
694 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000695 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000696 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000697
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000698 // Drop bodies of available externally objects to improve GlobalDCE.
699 PM.add(createEliminateAvailableExternallyPass());
700
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000701 // Now that we have optimized the program, discard unreachable functions.
702 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000703
704 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
705 // currently it damages debug info.
706 if (MergeFunctions)
707 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000708}
Rafael Espindola07f609152011-08-09 22:17:34 +0000709
Mehdi Amini1db10ac2016-02-16 23:02:29 +0000710void PassManagerBuilder::populateThinLTOPassManager(
711 legacy::PassManagerBase &PM) {
712 PerformThinLTO = true;
713
714 if (VerifyInput)
715 PM.add(createVerifierPass());
716
717 if (FunctionIndex)
718 PM.add(createFunctionImportPass(FunctionIndex));
719
720 populateModulePassManager(PM);
721
722 if (VerifyOutput)
723 PM.add(createVerifierPass());
724 PerformThinLTO = false;
725}
726
Chandler Carruth30d69c22015-02-13 10:01:29 +0000727void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000728 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000729 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000730
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000731 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000732 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000733
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000734 if (OptLevel != 0)
735 addEarlyLTOOptimizationPasses(PM);
736
Peter Collingbourne070843d2015-03-19 22:01:00 +0000737 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000738 addLTOOptimizationPasses(PM);
739
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000740 // Create a function that performs CFI checks for cross-DSO calls with targets
741 // in the current module.
742 PM.add(createCrossDSOCFIPass());
743
Peter Collingbourne070843d2015-03-19 22:01:00 +0000744 // Lower bit sets to globals. This pass supports Clang's control flow
745 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
746 // is enabled. The pass does nothing if CFI is disabled.
747 PM.add(createLowerBitSetsPass());
748
749 if (OptLevel != 0)
750 addLateLTOOptimizationPasses(PM);
751
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000752 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000753 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000754}
755
Eric Christopher04d4e932013-04-22 22:47:22 +0000756inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
757 return reinterpret_cast<PassManagerBuilder*>(P);
758}
759
760inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
761 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
762}
763
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000764LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000765 PassManagerBuilder *PMB = new PassManagerBuilder();
766 return wrap(PMB);
767}
768
769void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
770 PassManagerBuilder *Builder = unwrap(PMB);
771 delete Builder;
772}
773
774void
775LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
776 unsigned OptLevel) {
777 PassManagerBuilder *Builder = unwrap(PMB);
778 Builder->OptLevel = OptLevel;
779}
780
781void
782LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
783 unsigned SizeLevel) {
784 PassManagerBuilder *Builder = unwrap(PMB);
785 Builder->SizeLevel = SizeLevel;
786}
787
788void
789LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
790 LLVMBool Value) {
791 PassManagerBuilder *Builder = unwrap(PMB);
792 Builder->DisableUnitAtATime = Value;
793}
794
795void
796LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
797 LLVMBool Value) {
798 PassManagerBuilder *Builder = unwrap(PMB);
799 Builder->DisableUnrollLoops = Value;
800}
801
802void
803LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
804 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000805 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000806}
807
808void
809LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
810 unsigned Threshold) {
811 PassManagerBuilder *Builder = unwrap(PMB);
812 Builder->Inliner = createFunctionInliningPass(Threshold);
813}
814
815void
816LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
817 LLVMPassManagerRef PM) {
818 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000819 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000820 Builder->populateFunctionPassManager(*FPM);
821}
822
823void
824LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
825 LLVMPassManagerRef PM) {
826 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000827 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000828 Builder->populateModulePassManager(*MPM);
829}
830
831void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
832 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000833 LLVMBool Internalize,
834 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000835 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000836 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000837
838 // A small backwards compatibility hack. populateLTOPassManager used to take
839 // an RunInliner option.
840 if (RunInliner && !Builder->Inliner)
841 Builder->Inliner = createFunctionInliningPass();
842
843 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000844}