blob: 5928eb1d9c3adb7c5c42392eae3964530a5c52a1 [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
15
16#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Rafael Espindola07f609152011-08-09 22:17:34 +000017#include "llvm-c/Transforms/PassManagerBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000019#include "llvm/Analysis/Passes.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000020#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000023#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/ManagedStatic.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000026#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000028#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000029#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000030
31using namespace llvm;
32
Hal Finkelc34e5112012-02-01 03:51:43 +000033static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000034RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000035 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000036
37static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000038RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000039 cl::desc("Run the SLP vectorization passes"));
40
41static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000042RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000043 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000044
Hal Finkel204bf532012-04-13 17:15:33 +000045static cl::opt<bool>
46UseGVNAfterVectorization("use-gvn-after-vectorization",
47 cl::init(false), cl::Hidden,
48 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
49
Chandler Carruth7b8297a2014-10-14 00:31:29 +000050static cl::opt<bool> ExtraVectorizerPasses(
51 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
52 cl::desc("Run cleanup optimization passes after vectorization."));
53
Chandler Carruth1b398ae2012-09-14 09:22:59 +000054static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000055 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000056 cl::desc("Enable the new, experimental SROA pass"));
57
Hal Finkelbf45efd2013-11-16 23:59:05 +000058static cl::opt<bool>
59RunLoopRerolling("reroll-loops", cl::Hidden,
60 cl::desc("Run the loop rerolling pass"));
61
Michael J. Spencer289067c2014-05-29 01:55:07 +000062static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
63 cl::Hidden,
64 cl::desc("Run the load combining pass"));
65
James Molloy568da092014-08-06 12:56:19 +000066static cl::opt<bool>
67RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000068 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000069 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
70 "vectorizer instead of before"));
71
Hal Finkel445dda52014-09-02 22:12:54 +000072static cl::opt<bool> UseCFLAA("use-cfl-aa",
73 cl::init(false), cl::Hidden,
74 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000075
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000076static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000077EnableMLSM("mlsm", cl::init(true), cl::Hidden,
78 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000079
Karthik Bhat88db86d2015-03-06 10:11:25 +000080static cl::opt<bool> EnableLoopInterchange(
81 "enable-loopinterchange", cl::init(false), cl::Hidden,
82 cl::desc("Enable the new, experimental LoopInterchange Pass"));
83
Rafael Espindola3ea478b2011-08-02 21:50:27 +000084PassManagerBuilder::PassManagerBuilder() {
85 OptLevel = 2;
86 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000087 LibraryInfo = nullptr;
88 Inliner = nullptr;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000089 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000090 DisableUnitAtATime = false;
91 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000092 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +000093 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +000094 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +000095 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +000096 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +000097 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +000098 VerifyInput = false;
99 VerifyOutput = false;
100 StripDebug = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000101 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000102}
103
104PassManagerBuilder::~PassManagerBuilder() {
105 delete LibraryInfo;
106 delete Inliner;
107}
108
David Chisnall719a72f2011-08-16 13:58:41 +0000109/// Set of global extensions, automatically added as part of the standard set.
110static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
111 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
112
113void PassManagerBuilder::addGlobalExtension(
114 PassManagerBuilder::ExtensionPointTy Ty,
115 PassManagerBuilder::ExtensionFn Fn) {
116 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
117}
118
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000119void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
120 Extensions.push_back(std::make_pair(Ty, Fn));
121}
122
123void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000124 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000125 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
126 if ((*GlobalExtensions)[i].first == ETy)
127 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000128 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
129 if (Extensions[i].first == ETy)
130 Extensions[i].second(*this, PM);
131}
132
Chandler Carruth30d69c22015-02-13 10:01:29 +0000133void PassManagerBuilder::addInitialAliasAnalysisPasses(
134 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000135 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
136 // BasicAliasAnalysis wins if they disagree. This is intended to help
137 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000138 if (UseCFLAA)
139 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000140 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000141 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000142 PM.add(createBasicAliasAnalysisPass());
143}
144
Chandler Carruth30d69c22015-02-13 10:01:29 +0000145void PassManagerBuilder::populateFunctionPassManager(
146 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000147 addExtensionsToPM(EP_EarlyAsPossible, FPM);
148
149 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000150 if (LibraryInfo)
151 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000152
153 if (OptLevel == 0) return;
154
155 addInitialAliasAnalysisPasses(FPM);
156
157 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000158 if (UseNewSROA)
159 FPM.add(createSROAPass());
160 else
161 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000162 FPM.add(createEarlyCSEPass());
163 FPM.add(createLowerExpectIntrinsicPass());
164}
165
Chandler Carruth30d69c22015-02-13 10:01:29 +0000166void PassManagerBuilder::populateModulePassManager(
167 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000168 // If all optimizations are disabled, just run the always-inline pass and,
169 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000170 if (OptLevel == 0) {
171 if (Inliner) {
172 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000173 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000174 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000175
Nick Lewycky592d8492014-10-23 23:49:31 +0000176 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
177 // creates a CGSCC pass manager, but we don't want to add extensions into
178 // that pass manager. To prevent this we insert a no-op module pass to reset
179 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
180 // builds. The function merging pass is
181 if (MergeFunctions)
182 MPM.add(createMergeFunctionsPass());
183 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000184 MPM.add(createBarrierNoopPass());
185
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000186 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000187 return;
188 }
189
190 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000191 if (LibraryInfo)
192 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000193
194 addInitialAliasAnalysisPasses(MPM);
195
196 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000197 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
198
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000199 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000200 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
201
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000202 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
203
204 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000205 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000206 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
207 }
208
209 // Start of CallGraph SCC passes.
210 if (!DisableUnitAtATime)
211 MPM.add(createPruneEHPass()); // Remove dead EH info
212 if (Inliner) {
213 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000214 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000215 }
216 if (!DisableUnitAtATime)
217 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
218 if (OptLevel > 2)
219 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
220
221 // Start of function pass.
222 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000223 if (UseNewSROA)
224 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
225 else
226 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000227 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000228 MPM.add(createJumpThreadingPass()); // Thread jumps.
229 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
230 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
231 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000232 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000233
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000234 if (!DisableTailCalls)
235 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
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000238 // Rotate Loop - disable header duplication at -Oz
239 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000240 MPM.add(createLICMPass()); // Hoist loop invariants
241 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
242 MPM.add(createInstructionCombiningPass());
243 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
244 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
245 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat88db86d2015-03-06 10:11:25 +0000246 if (EnableLoopInterchange)
247 MPM.add(createLoopInterchangePass()); // Interchange loops
Nadav Rotem6b94c2a2012-10-17 18:25:06 +0000248
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000249 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000250 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000251 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
252
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000253 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000254 if (EnableMLSM)
255 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000256 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000257 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000258 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
259 MPM.add(createSCCPPass()); // Constant prop with SCCP
260
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000261 // Delete dead bit computations (instcombine runs after to fold away the dead
262 // computations, and then ADCE will run later to exploit any new DCE
263 // opportunities that creates).
264 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
265
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000266 // Run instcombine after redundancy elimination to exploit opportunities
267 // opened up by them.
268 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000269 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000270 MPM.add(createJumpThreadingPass()); // Thread jumps
271 MPM.add(createCorrelatedValuePropagationPass());
272 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000273 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000274
275 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
276
Hal Finkel29aeb202013-11-17 16:02:50 +0000277 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000278 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000279 if (!RunSLPAfterLoopVectorization) {
280 if (SLPVectorize)
281 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000282
James Molloy568da092014-08-06 12:56:19 +0000283 if (BBVectorize) {
284 MPM.add(createBBVectorizePass());
285 MPM.add(createInstructionCombiningPass());
286 addExtensionsToPM(EP_Peephole, MPM);
287 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000288 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000289 else
290 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000291
James Molloy568da092014-08-06 12:56:19 +0000292 // BBVectorize may have significantly shortened a loop body; unroll again.
293 if (!DisableUnrollLoops)
294 MPM.add(createLoopUnrollPass());
295 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000296 }
297
Michael J. Spencer289067c2014-05-29 01:55:07 +0000298 if (LoadCombine)
299 MPM.add(createLoadCombinePass());
300
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000301 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000302 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000303 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000304 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000305
Renato Golin729a3ae2013-12-05 21:20:02 +0000306 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
307 // pass manager that we are specifically trying to avoid. To prevent this
308 // we must insert a no-op module pass to reset the pass manager.
309 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000310
311 // Re-rotate loops in all our loop nests. These may have fallout out of
312 // rotated form due to GVN or other transformations, and the vectorizer relies
313 // on the rotated form.
Michael Zolotukhin267e12f2015-03-10 19:07:41 +0000314 MPM.add(createLoopRotatePass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000315
Renato Golin729a3ae2013-12-05 21:20:02 +0000316 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
317 // FIXME: Because of #pragma vectorize enable, the passes below are always
318 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
319 // on -O1 and no #pragma is found). Would be good to have these two passes
320 // as function calls, so that we can only pass them when the vectorizer
321 // changed the code.
322 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000323 if (OptLevel > 1 && ExtraVectorizerPasses) {
324 // At higher optimization levels, try to clean up any runtime overlap and
325 // alignment checks inserted by the vectorizer. We want to track correllated
326 // runtime checks for two inner loops in the same outer loop, fold any
327 // common computations, hoist loop-invariant aspects out of any outer loop,
328 // and unswitch the runtime checks if possible. Once hoisted, we may have
329 // dead (or speculatable) control flows or more combining opportunities.
330 MPM.add(createEarlyCSEPass());
331 MPM.add(createCorrelatedValuePropagationPass());
332 MPM.add(createInstructionCombiningPass());
333 MPM.add(createLICMPass());
334 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
335 MPM.add(createCFGSimplificationPass());
336 MPM.add(createInstructionCombiningPass());
337 }
James Molloy568da092014-08-06 12:56:19 +0000338
339 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000340 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000341 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000342 if (OptLevel > 1 && ExtraVectorizerPasses) {
343 MPM.add(createEarlyCSEPass());
344 }
345 }
James Molloy568da092014-08-06 12:56:19 +0000346
347 if (BBVectorize) {
348 MPM.add(createBBVectorizePass());
349 MPM.add(createInstructionCombiningPass());
350 addExtensionsToPM(EP_Peephole, MPM);
351 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000352 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000353 else
354 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
355
356 // BBVectorize may have significantly shortened a loop body; unroll again.
357 if (!DisableUnrollLoops)
358 MPM.add(createLoopUnrollPass());
359 }
360 }
361
Peter Collingbourne0a437612014-05-25 10:27:02 +0000362 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000363 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000364 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000365
Kevin Qin49bc7642015-03-12 05:36:01 +0000366 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000367 MPM.add(createLoopUnrollPass()); // Unroll small loops
368
Kevin Qin49bc7642015-03-12 05:36:01 +0000369 // This is a barrier pass to avoid combine LICM pass and loop unroll pass
370 // within same loop pass manager.
371 MPM.add(createInstructionSimplifierPass());
372
373 // Runtime unrolling will introduce runtime check in loop prologue. If the
374 // unrolled loop is a inner loop, then the prologue will be inside the
375 // outer loop. LICM pass can help to promote the runtime check out if the
376 // checked value is loop invariant.
377 MPM.add(createLICMPass());
378 }
379
Hal Finkeld67e4632014-09-07 20:05:11 +0000380 // After vectorization and unrolling, assume intrinsics may tell us more
381 // about pointer alignments.
382 MPM.add(createAlignmentFromAssumptionsPass());
383
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000384 if (!DisableUnitAtATime) {
385 // FIXME: We shouldn't bother with this anymore.
386 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
387
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000388 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000389 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000390 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000391 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000392 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000393 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000394 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000395
396 if (MergeFunctions)
397 MPM.add(createMergeFunctionsPass());
398
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000399 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000400}
401
Chandler Carruth30d69c22015-02-13 10:01:29 +0000402void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000403 // Provide AliasAnalysis services for optimizations.
404 addInitialAliasAnalysisPasses(PM);
405
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000406 // Propagate constants at call sites into the functions they call. This
407 // opens opportunities for globalopt (and inlining) by substituting function
408 // pointers passed as arguments to direct uses of functions.
409 PM.add(createIPSCCPPass());
410
411 // Now that we internalized some globals, see if we can hack on them!
412 PM.add(createGlobalOptimizerPass());
413
414 // Linking modules together can lead to duplicated global constants, only
415 // keep one copy of each constant.
416 PM.add(createConstantMergePass());
417
418 // Remove unused arguments from functions.
419 PM.add(createDeadArgEliminationPass());
420
421 // Reduce the code after globalopt and ipsccp. Both can open up significant
422 // simplification opportunities, and both can propagate functions through
423 // function pointers. When this happens, we often have to resolve varargs
424 // calls, etc, so let instcombine do this.
425 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000426 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000427
428 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000429 bool RunInliner = Inliner;
430 if (RunInliner) {
431 PM.add(Inliner);
432 Inliner = nullptr;
433 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000434
435 PM.add(createPruneEHPass()); // Remove dead EH info.
436
437 // Optimize globals again if we ran the inliner.
438 if (RunInliner)
439 PM.add(createGlobalOptimizerPass());
440 PM.add(createGlobalDCEPass()); // Remove dead functions.
441
442 // If we didn't decide to inline a function, check to see if we can
443 // transform it to pass arguments by value instead of by reference.
444 PM.add(createArgumentPromotionPass());
445
446 // The IPO passes may leave cruft around. Clean up after them.
447 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000448 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000449 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000450
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000451 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000452 if (UseNewSROA)
453 PM.add(createSROAPass());
454 else
455 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000456
457 // Run a few AA driven optimizations here and now, to cleanup the code.
458 PM.add(createFunctionAttrsPass()); // Add nocapture.
459 PM.add(createGlobalsModRefPass()); // IP alias analysis.
460
Bill Wendling932b9922012-04-02 22:16:50 +0000461 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000462 if (EnableMLSM)
463 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000464 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
465 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000466
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000467 // Nuke dead stores.
468 PM.add(createDeadStoreEliminationPass());
469
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000470 // More loops are countable; try to optimize them.
471 PM.add(createIndVarSimplifyPass());
472 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000473 if (EnableLoopInterchange)
474 PM.add(createLoopInterchangePass());
475
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000476 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000477
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000478 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000479 if (RunSLPAfterLoopVectorization)
480 if (SLPVectorize)
481 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000482
Hal Finkeld67e4632014-09-07 20:05:11 +0000483 // After vectorization, assume intrinsics may tell us more about pointer
484 // alignments.
485 PM.add(createAlignmentFromAssumptionsPass());
486
Michael J. Spencer289067c2014-05-29 01:55:07 +0000487 if (LoadCombine)
488 PM.add(createLoadCombinePass());
489
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000490 // Cleanup and simplify the code after the scalar optimizations.
491 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000492 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000493
494 PM.add(createJumpThreadingPass());
495
Peter Collingbournee6909c82015-02-20 20:30:47 +0000496 // Lower bitset metadata to bitsets.
497 PM.add(createLowerBitSetsPass());
498
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000499 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000500 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000501
502 // Now that we have optimized the program, discard unreachable functions.
503 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000504
505 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
506 // currently it damages debug info.
507 if (MergeFunctions)
508 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000509}
Rafael Espindola07f609152011-08-09 22:17:34 +0000510
Chandler Carruth30d69c22015-02-13 10:01:29 +0000511void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000512 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000513 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000514
515 if (VerifyInput)
516 PM.add(createVerifierPass());
517
518 if (StripDebug)
519 PM.add(createStripSymbolsPass(true));
520
521 if (VerifyInput)
522 PM.add(createDebugInfoVerifierPass());
523
524 if (OptLevel != 0)
525 addLTOOptimizationPasses(PM);
526
527 if (VerifyOutput) {
528 PM.add(createVerifierPass());
529 PM.add(createDebugInfoVerifierPass());
530 }
531}
532
Eric Christopher04d4e932013-04-22 22:47:22 +0000533inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
534 return reinterpret_cast<PassManagerBuilder*>(P);
535}
536
537inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
538 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
539}
540
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000541LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000542 PassManagerBuilder *PMB = new PassManagerBuilder();
543 return wrap(PMB);
544}
545
546void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
547 PassManagerBuilder *Builder = unwrap(PMB);
548 delete Builder;
549}
550
551void
552LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
553 unsigned OptLevel) {
554 PassManagerBuilder *Builder = unwrap(PMB);
555 Builder->OptLevel = OptLevel;
556}
557
558void
559LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
560 unsigned SizeLevel) {
561 PassManagerBuilder *Builder = unwrap(PMB);
562 Builder->SizeLevel = SizeLevel;
563}
564
565void
566LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
567 LLVMBool Value) {
568 PassManagerBuilder *Builder = unwrap(PMB);
569 Builder->DisableUnitAtATime = Value;
570}
571
572void
573LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
574 LLVMBool Value) {
575 PassManagerBuilder *Builder = unwrap(PMB);
576 Builder->DisableUnrollLoops = Value;
577}
578
579void
580LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
581 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000582 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000583}
584
585void
586LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
587 unsigned Threshold) {
588 PassManagerBuilder *Builder = unwrap(PMB);
589 Builder->Inliner = createFunctionInliningPass(Threshold);
590}
591
592void
593LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
594 LLVMPassManagerRef PM) {
595 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000596 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000597 Builder->populateFunctionPassManager(*FPM);
598}
599
600void
601LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
602 LLVMPassManagerRef PM) {
603 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000604 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000605 Builder->populateModulePassManager(*MPM);
606}
607
608void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
609 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000610 LLVMBool Internalize,
611 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000612 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000613 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000614
615 // A small backwards compatibility hack. populateLTOPassManager used to take
616 // an RunInliner option.
617 if (RunInliner && !Builder->Inliner)
618 Builder->Inliner = createFunctionInliningPass();
619
620 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000621}