blob: 502451bf432a583107dbf51096e2384e914911af [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
James Molloy0cbb2a862015-03-27 10:36:57 +000062static cl::opt<bool>
63RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
64 cl::desc("Run the float2int (float demotion) pass"));
65
Michael J. Spencer289067c2014-05-29 01:55:07 +000066static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
67 cl::Hidden,
68 cl::desc("Run the load combining pass"));
69
James Molloy568da092014-08-06 12:56:19 +000070static cl::opt<bool>
71RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000072 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000073 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
74 "vectorizer instead of before"));
75
Hal Finkel445dda52014-09-02 22:12:54 +000076static cl::opt<bool> UseCFLAA("use-cfl-aa",
77 cl::init(false), cl::Hidden,
78 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000079
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000080static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000081EnableMLSM("mlsm", cl::init(true), cl::Hidden,
82 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000083
Karthik Bhat88db86d2015-03-06 10:11:25 +000084static cl::opt<bool> EnableLoopInterchange(
85 "enable-loopinterchange", cl::init(false), cl::Hidden,
86 cl::desc("Enable the new, experimental LoopInterchange Pass"));
87
Rafael Espindola3ea478b2011-08-02 21:50:27 +000088PassManagerBuilder::PassManagerBuilder() {
89 OptLevel = 2;
90 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000091 LibraryInfo = nullptr;
92 Inliner = nullptr;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000093 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000094 DisableUnitAtATime = false;
95 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000096 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +000097 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +000098 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +000099 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000100 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000101 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000102 VerifyInput = false;
103 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000104 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000105}
106
107PassManagerBuilder::~PassManagerBuilder() {
108 delete LibraryInfo;
109 delete Inliner;
110}
111
David Chisnall719a72f2011-08-16 13:58:41 +0000112/// Set of global extensions, automatically added as part of the standard set.
113static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
114 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
115
116void PassManagerBuilder::addGlobalExtension(
117 PassManagerBuilder::ExtensionPointTy Ty,
118 PassManagerBuilder::ExtensionFn Fn) {
119 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
120}
121
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000122void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
123 Extensions.push_back(std::make_pair(Ty, Fn));
124}
125
126void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000127 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000128 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
129 if ((*GlobalExtensions)[i].first == ETy)
130 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000131 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
132 if (Extensions[i].first == ETy)
133 Extensions[i].second(*this, PM);
134}
135
Chandler Carruth30d69c22015-02-13 10:01:29 +0000136void PassManagerBuilder::addInitialAliasAnalysisPasses(
137 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000138 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
139 // BasicAliasAnalysis wins if they disagree. This is intended to help
140 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000141 if (UseCFLAA)
142 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000143 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000144 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000145 PM.add(createBasicAliasAnalysisPass());
146}
147
Chandler Carruth30d69c22015-02-13 10:01:29 +0000148void PassManagerBuilder::populateFunctionPassManager(
149 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000150 addExtensionsToPM(EP_EarlyAsPossible, FPM);
151
152 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000153 if (LibraryInfo)
154 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000155
156 if (OptLevel == 0) return;
157
158 addInitialAliasAnalysisPasses(FPM);
159
160 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000161 if (UseNewSROA)
162 FPM.add(createSROAPass());
163 else
164 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000165 FPM.add(createEarlyCSEPass());
166 FPM.add(createLowerExpectIntrinsicPass());
167}
168
Chandler Carruth30d69c22015-02-13 10:01:29 +0000169void PassManagerBuilder::populateModulePassManager(
170 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000171 // If all optimizations are disabled, just run the always-inline pass and,
172 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000173 if (OptLevel == 0) {
174 if (Inliner) {
175 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000176 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000177 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000178
Nick Lewycky592d8492014-10-23 23:49:31 +0000179 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
180 // creates a CGSCC pass manager, but we don't want to add extensions into
181 // that pass manager. To prevent this we insert a no-op module pass to reset
182 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
183 // builds. The function merging pass is
184 if (MergeFunctions)
185 MPM.add(createMergeFunctionsPass());
186 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000187 MPM.add(createBarrierNoopPass());
188
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000189 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000190 return;
191 }
192
193 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000194 if (LibraryInfo)
195 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000196
197 addInitialAliasAnalysisPasses(MPM);
198
199 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000200 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
201
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000202 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000203 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
204
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000205 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
206
207 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000208 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000209 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
210 }
211
212 // Start of CallGraph SCC passes.
213 if (!DisableUnitAtATime)
214 MPM.add(createPruneEHPass()); // Remove dead EH info
215 if (Inliner) {
216 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000217 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000218 }
219 if (!DisableUnitAtATime)
220 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
221 if (OptLevel > 2)
222 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
223
224 // Start of function pass.
225 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000226 if (UseNewSROA)
227 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
228 else
229 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000230 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000231 MPM.add(createJumpThreadingPass()); // Thread jumps.
232 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
233 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
234 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000235 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000236
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000237 if (!DisableTailCalls)
238 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000239 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
240 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000241 // Rotate Loop - disable header duplication at -Oz
242 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243 MPM.add(createLICMPass()); // Hoist loop invariants
244 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
245 MPM.add(createInstructionCombiningPass());
246 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
247 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
248 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat88db86d2015-03-06 10:11:25 +0000249 if (EnableLoopInterchange)
250 MPM.add(createLoopInterchangePass()); // Interchange loops
Nadav Rotem6b94c2a2012-10-17 18:25:06 +0000251
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000252 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000253 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000254 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
255
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000256 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000257 if (EnableMLSM)
258 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000259 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000260 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000261 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
262 MPM.add(createSCCPPass()); // Constant prop with SCCP
263
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000264 // Delete dead bit computations (instcombine runs after to fold away the dead
265 // computations, and then ADCE will run later to exploit any new DCE
266 // opportunities that creates).
267 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
268
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000269 // Run instcombine after redundancy elimination to exploit opportunities
270 // opened up by them.
271 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000272 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000273 MPM.add(createJumpThreadingPass()); // Thread jumps
274 MPM.add(createCorrelatedValuePropagationPass());
275 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000276 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000277
278 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
279
Hal Finkel29aeb202013-11-17 16:02:50 +0000280 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000281 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000282 if (!RunSLPAfterLoopVectorization) {
283 if (SLPVectorize)
284 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000285
James Molloy568da092014-08-06 12:56:19 +0000286 if (BBVectorize) {
287 MPM.add(createBBVectorizePass());
288 MPM.add(createInstructionCombiningPass());
289 addExtensionsToPM(EP_Peephole, MPM);
290 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000291 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000292 else
293 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000294
James Molloy568da092014-08-06 12:56:19 +0000295 // BBVectorize may have significantly shortened a loop body; unroll again.
296 if (!DisableUnrollLoops)
297 MPM.add(createLoopUnrollPass());
298 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000299 }
300
Michael J. Spencer289067c2014-05-29 01:55:07 +0000301 if (LoadCombine)
302 MPM.add(createLoadCombinePass());
303
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000304 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000305 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000306 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000307 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000308
Renato Golin729a3ae2013-12-05 21:20:02 +0000309 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
310 // pass manager that we are specifically trying to avoid. To prevent this
311 // we must insert a no-op module pass to reset the pass manager.
312 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000313
James Molloy0cbb2a862015-03-27 10:36:57 +0000314 if (RunFloat2Int)
315 MPM.add(createFloat2IntPass());
316
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000317 // Re-rotate loops in all our loop nests. These may have fallout out of
318 // rotated form due to GVN or other transformations, and the vectorizer relies
319 // on the rotated form.
Michael Zolotukhin267e12f2015-03-10 19:07:41 +0000320 MPM.add(createLoopRotatePass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000321
Renato Golin729a3ae2013-12-05 21:20:02 +0000322 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
323 // FIXME: Because of #pragma vectorize enable, the passes below are always
324 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
325 // on -O1 and no #pragma is found). Would be good to have these two passes
326 // as function calls, so that we can only pass them when the vectorizer
327 // changed the code.
328 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000329 if (OptLevel > 1 && ExtraVectorizerPasses) {
330 // At higher optimization levels, try to clean up any runtime overlap and
331 // alignment checks inserted by the vectorizer. We want to track correllated
332 // runtime checks for two inner loops in the same outer loop, fold any
333 // common computations, hoist loop-invariant aspects out of any outer loop,
334 // and unswitch the runtime checks if possible. Once hoisted, we may have
335 // dead (or speculatable) control flows or more combining opportunities.
336 MPM.add(createEarlyCSEPass());
337 MPM.add(createCorrelatedValuePropagationPass());
338 MPM.add(createInstructionCombiningPass());
339 MPM.add(createLICMPass());
340 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
341 MPM.add(createCFGSimplificationPass());
342 MPM.add(createInstructionCombiningPass());
343 }
James Molloy568da092014-08-06 12:56:19 +0000344
345 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000346 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000347 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000348 if (OptLevel > 1 && ExtraVectorizerPasses) {
349 MPM.add(createEarlyCSEPass());
350 }
351 }
James Molloy568da092014-08-06 12:56:19 +0000352
353 if (BBVectorize) {
354 MPM.add(createBBVectorizePass());
355 MPM.add(createInstructionCombiningPass());
356 addExtensionsToPM(EP_Peephole, MPM);
357 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000358 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000359 else
360 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
361
362 // BBVectorize may have significantly shortened a loop body; unroll again.
363 if (!DisableUnrollLoops)
364 MPM.add(createLoopUnrollPass());
365 }
366 }
367
Peter Collingbourne0a437612014-05-25 10:27:02 +0000368 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000369 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000370 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000371
Kevin Qin49bc7642015-03-12 05:36:01 +0000372 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000373 MPM.add(createLoopUnrollPass()); // Unroll small loops
374
Kevin Qin49bc7642015-03-12 05:36:01 +0000375 // This is a barrier pass to avoid combine LICM pass and loop unroll pass
376 // within same loop pass manager.
377 MPM.add(createInstructionSimplifierPass());
378
379 // Runtime unrolling will introduce runtime check in loop prologue. If the
380 // unrolled loop is a inner loop, then the prologue will be inside the
381 // outer loop. LICM pass can help to promote the runtime check out if the
382 // checked value is loop invariant.
383 MPM.add(createLICMPass());
384 }
385
Hal Finkeld67e4632014-09-07 20:05:11 +0000386 // After vectorization and unrolling, assume intrinsics may tell us more
387 // about pointer alignments.
388 MPM.add(createAlignmentFromAssumptionsPass());
389
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000390 if (!DisableUnitAtATime) {
391 // FIXME: We shouldn't bother with this anymore.
392 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
393
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000394 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000395 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000396 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000397 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000398 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000399 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000400 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000401
402 if (MergeFunctions)
403 MPM.add(createMergeFunctionsPass());
404
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000405 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000406}
407
Chandler Carruth30d69c22015-02-13 10:01:29 +0000408void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000409 // Provide AliasAnalysis services for optimizations.
410 addInitialAliasAnalysisPasses(PM);
411
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000412 // Propagate constants at call sites into the functions they call. This
413 // opens opportunities for globalopt (and inlining) by substituting function
414 // pointers passed as arguments to direct uses of functions.
415 PM.add(createIPSCCPPass());
416
417 // Now that we internalized some globals, see if we can hack on them!
418 PM.add(createGlobalOptimizerPass());
419
420 // Linking modules together can lead to duplicated global constants, only
421 // keep one copy of each constant.
422 PM.add(createConstantMergePass());
423
424 // Remove unused arguments from functions.
425 PM.add(createDeadArgEliminationPass());
426
427 // Reduce the code after globalopt and ipsccp. Both can open up significant
428 // simplification opportunities, and both can propagate functions through
429 // function pointers. When this happens, we often have to resolve varargs
430 // calls, etc, so let instcombine do this.
431 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000432 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000433
434 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000435 bool RunInliner = Inliner;
436 if (RunInliner) {
437 PM.add(Inliner);
438 Inliner = nullptr;
439 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000440
441 PM.add(createPruneEHPass()); // Remove dead EH info.
442
443 // Optimize globals again if we ran the inliner.
444 if (RunInliner)
445 PM.add(createGlobalOptimizerPass());
446 PM.add(createGlobalDCEPass()); // Remove dead functions.
447
448 // If we didn't decide to inline a function, check to see if we can
449 // transform it to pass arguments by value instead of by reference.
450 PM.add(createArgumentPromotionPass());
451
452 // The IPO passes may leave cruft around. Clean up after them.
453 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000454 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000455 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000456
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000457 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000458 if (UseNewSROA)
459 PM.add(createSROAPass());
460 else
461 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000462
463 // Run a few AA driven optimizations here and now, to cleanup the code.
464 PM.add(createFunctionAttrsPass()); // Add nocapture.
465 PM.add(createGlobalsModRefPass()); // IP alias analysis.
466
Bill Wendling932b9922012-04-02 22:16:50 +0000467 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000468 if (EnableMLSM)
469 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000470 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
471 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000472
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000473 // Nuke dead stores.
474 PM.add(createDeadStoreEliminationPass());
475
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000476 // More loops are countable; try to optimize them.
477 PM.add(createIndVarSimplifyPass());
478 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000479 if (EnableLoopInterchange)
480 PM.add(createLoopInterchangePass());
481
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000482 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000483
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000484 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000485 if (RunSLPAfterLoopVectorization)
486 if (SLPVectorize)
487 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000488
Hal Finkeld67e4632014-09-07 20:05:11 +0000489 // After vectorization, assume intrinsics may tell us more about pointer
490 // alignments.
491 PM.add(createAlignmentFromAssumptionsPass());
492
Michael J. Spencer289067c2014-05-29 01:55:07 +0000493 if (LoadCombine)
494 PM.add(createLoadCombinePass());
495
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000496 // Cleanup and simplify the code after the scalar optimizations.
497 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000498 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000499
500 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000501}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000502
Peter Collingbourne070843d2015-03-19 22:01:00 +0000503void PassManagerBuilder::addLateLTOOptimizationPasses(
504 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000505 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000506 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000507
508 // Now that we have optimized the program, discard unreachable functions.
509 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000510
511 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
512 // currently it damages debug info.
513 if (MergeFunctions)
514 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000515}
Rafael Espindola07f609152011-08-09 22:17:34 +0000516
Chandler Carruth30d69c22015-02-13 10:01:29 +0000517void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000518 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000519 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000520
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000521 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000522 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000523
Peter Collingbourne070843d2015-03-19 22:01:00 +0000524 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000525 addLTOOptimizationPasses(PM);
526
Peter Collingbourne070843d2015-03-19 22:01:00 +0000527 // Lower bit sets to globals. This pass supports Clang's control flow
528 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
529 // is enabled. The pass does nothing if CFI is disabled.
530 PM.add(createLowerBitSetsPass());
531
532 if (OptLevel != 0)
533 addLateLTOOptimizationPasses(PM);
534
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000535 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000536 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000537}
538
Eric Christopher04d4e932013-04-22 22:47:22 +0000539inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
540 return reinterpret_cast<PassManagerBuilder*>(P);
541}
542
543inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
544 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
545}
546
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000547LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000548 PassManagerBuilder *PMB = new PassManagerBuilder();
549 return wrap(PMB);
550}
551
552void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
553 PassManagerBuilder *Builder = unwrap(PMB);
554 delete Builder;
555}
556
557void
558LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
559 unsigned OptLevel) {
560 PassManagerBuilder *Builder = unwrap(PMB);
561 Builder->OptLevel = OptLevel;
562}
563
564void
565LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
566 unsigned SizeLevel) {
567 PassManagerBuilder *Builder = unwrap(PMB);
568 Builder->SizeLevel = SizeLevel;
569}
570
571void
572LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
573 LLVMBool Value) {
574 PassManagerBuilder *Builder = unwrap(PMB);
575 Builder->DisableUnitAtATime = Value;
576}
577
578void
579LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
580 LLVMBool Value) {
581 PassManagerBuilder *Builder = unwrap(PMB);
582 Builder->DisableUnrollLoops = Value;
583}
584
585void
586LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
587 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000588 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000589}
590
591void
592LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
593 unsigned Threshold) {
594 PassManagerBuilder *Builder = unwrap(PMB);
595 Builder->Inliner = createFunctionInliningPass(Threshold);
596}
597
598void
599LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
600 LLVMPassManagerRef PM) {
601 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000602 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000603 Builder->populateFunctionPassManager(*FPM);
604}
605
606void
607LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
608 LLVMPassManagerRef PM) {
609 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000610 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000611 Builder->populateModulePassManager(*MPM);
612}
613
614void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
615 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000616 LLVMBool Internalize,
617 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000618 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000619 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000620
621 // A small backwards compatibility hack. populateLTOPassManager used to take
622 // an RunInliner option.
623 if (RunInliner && !Builder->Inliner)
624 Builder->Inliner = createFunctionInliningPass();
625
626 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000627}