blob: d425d6fddbd12fda0f746d2eb1b4f7ad23d765d3 [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 Bhat8210fdf2015-04-23 04:51:44 +0000249 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000250 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000251 MPM.add(createCFGSimplificationPass());
252 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000253 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000254 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000255 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
256
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000257 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000258 if (EnableMLSM)
259 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000260 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000261 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000262 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
263 MPM.add(createSCCPPass()); // Constant prop with SCCP
264
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000265 // Delete dead bit computations (instcombine runs after to fold away the dead
266 // computations, and then ADCE will run later to exploit any new DCE
267 // opportunities that creates).
268 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
269
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000270 // Run instcombine after redundancy elimination to exploit opportunities
271 // opened up by them.
272 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000273 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000274 MPM.add(createJumpThreadingPass()); // Thread jumps
275 MPM.add(createCorrelatedValuePropagationPass());
276 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000277 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000278
279 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
280
Hal Finkel29aeb202013-11-17 16:02:50 +0000281 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000282 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000283 if (!RunSLPAfterLoopVectorization) {
284 if (SLPVectorize)
285 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000286
James Molloy568da092014-08-06 12:56:19 +0000287 if (BBVectorize) {
288 MPM.add(createBBVectorizePass());
289 MPM.add(createInstructionCombiningPass());
290 addExtensionsToPM(EP_Peephole, MPM);
291 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000292 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000293 else
294 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000295
James Molloy568da092014-08-06 12:56:19 +0000296 // BBVectorize may have significantly shortened a loop body; unroll again.
297 if (!DisableUnrollLoops)
298 MPM.add(createLoopUnrollPass());
299 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000300 }
301
Michael J. Spencer289067c2014-05-29 01:55:07 +0000302 if (LoadCombine)
303 MPM.add(createLoadCombinePass());
304
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000305 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000306 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000307 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000308 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000309
Renato Golin729a3ae2013-12-05 21:20:02 +0000310 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
311 // pass manager that we are specifically trying to avoid. To prevent this
312 // we must insert a no-op module pass to reset the pass manager.
313 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000314
James Molloy0cbb2a862015-03-27 10:36:57 +0000315 if (RunFloat2Int)
316 MPM.add(createFloat2IntPass());
317
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000318 // Re-rotate loops in all our loop nests. These may have fallout out of
319 // rotated form due to GVN or other transformations, and the vectorizer relies
320 // on the rotated form.
Michael Zolotukhin267e12f2015-03-10 19:07:41 +0000321 MPM.add(createLoopRotatePass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000322
Renato Golin729a3ae2013-12-05 21:20:02 +0000323 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
324 // FIXME: Because of #pragma vectorize enable, the passes below are always
325 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
326 // on -O1 and no #pragma is found). Would be good to have these two passes
327 // as function calls, so that we can only pass them when the vectorizer
328 // changed the code.
329 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000330 if (OptLevel > 1 && ExtraVectorizerPasses) {
331 // At higher optimization levels, try to clean up any runtime overlap and
332 // alignment checks inserted by the vectorizer. We want to track correllated
333 // runtime checks for two inner loops in the same outer loop, fold any
334 // common computations, hoist loop-invariant aspects out of any outer loop,
335 // and unswitch the runtime checks if possible. Once hoisted, we may have
336 // dead (or speculatable) control flows or more combining opportunities.
337 MPM.add(createEarlyCSEPass());
338 MPM.add(createCorrelatedValuePropagationPass());
339 MPM.add(createInstructionCombiningPass());
340 MPM.add(createLICMPass());
341 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
342 MPM.add(createCFGSimplificationPass());
343 MPM.add(createInstructionCombiningPass());
344 }
James Molloy568da092014-08-06 12:56:19 +0000345
346 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000347 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000348 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000349 if (OptLevel > 1 && ExtraVectorizerPasses) {
350 MPM.add(createEarlyCSEPass());
351 }
352 }
James Molloy568da092014-08-06 12:56:19 +0000353
354 if (BBVectorize) {
355 MPM.add(createBBVectorizePass());
356 MPM.add(createInstructionCombiningPass());
357 addExtensionsToPM(EP_Peephole, MPM);
358 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000359 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000360 else
361 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
362
363 // BBVectorize may have significantly shortened a loop body; unroll again.
364 if (!DisableUnrollLoops)
365 MPM.add(createLoopUnrollPass());
366 }
367 }
368
Peter Collingbourne0a437612014-05-25 10:27:02 +0000369 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000370 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000371 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000372
Kevin Qin49bc7642015-03-12 05:36:01 +0000373 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000374 MPM.add(createLoopUnrollPass()); // Unroll small loops
375
Kevin Qin49bc7642015-03-12 05:36:01 +0000376 // This is a barrier pass to avoid combine LICM pass and loop unroll pass
377 // within same loop pass manager.
378 MPM.add(createInstructionSimplifierPass());
379
380 // Runtime unrolling will introduce runtime check in loop prologue. If the
381 // unrolled loop is a inner loop, then the prologue will be inside the
382 // outer loop. LICM pass can help to promote the runtime check out if the
383 // checked value is loop invariant.
384 MPM.add(createLICMPass());
385 }
386
Hal Finkeld67e4632014-09-07 20:05:11 +0000387 // After vectorization and unrolling, assume intrinsics may tell us more
388 // about pointer alignments.
389 MPM.add(createAlignmentFromAssumptionsPass());
390
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000391 if (!DisableUnitAtATime) {
392 // FIXME: We shouldn't bother with this anymore.
393 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
394
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000395 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000396 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000397 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000398 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000399 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000400 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000401 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000402
403 if (MergeFunctions)
404 MPM.add(createMergeFunctionsPass());
405
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000406 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000407}
408
Chandler Carruth30d69c22015-02-13 10:01:29 +0000409void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000410 // Provide AliasAnalysis services for optimizations.
411 addInitialAliasAnalysisPasses(PM);
412
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000413 // Propagate constants at call sites into the functions they call. This
414 // opens opportunities for globalopt (and inlining) by substituting function
415 // pointers passed as arguments to direct uses of functions.
416 PM.add(createIPSCCPPass());
417
418 // Now that we internalized some globals, see if we can hack on them!
419 PM.add(createGlobalOptimizerPass());
420
421 // Linking modules together can lead to duplicated global constants, only
422 // keep one copy of each constant.
423 PM.add(createConstantMergePass());
424
425 // Remove unused arguments from functions.
426 PM.add(createDeadArgEliminationPass());
427
428 // Reduce the code after globalopt and ipsccp. Both can open up significant
429 // simplification opportunities, and both can propagate functions through
430 // function pointers. When this happens, we often have to resolve varargs
431 // calls, etc, so let instcombine do this.
432 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000433 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000434
435 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000436 bool RunInliner = Inliner;
437 if (RunInliner) {
438 PM.add(Inliner);
439 Inliner = nullptr;
440 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000441
442 PM.add(createPruneEHPass()); // Remove dead EH info.
443
444 // Optimize globals again if we ran the inliner.
445 if (RunInliner)
446 PM.add(createGlobalOptimizerPass());
447 PM.add(createGlobalDCEPass()); // Remove dead functions.
448
449 // If we didn't decide to inline a function, check to see if we can
450 // transform it to pass arguments by value instead of by reference.
451 PM.add(createArgumentPromotionPass());
452
453 // The IPO passes may leave cruft around. Clean up after them.
454 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000455 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000456 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000457
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000458 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000459 if (UseNewSROA)
460 PM.add(createSROAPass());
461 else
462 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000463
464 // Run a few AA driven optimizations here and now, to cleanup the code.
465 PM.add(createFunctionAttrsPass()); // Add nocapture.
466 PM.add(createGlobalsModRefPass()); // IP alias analysis.
467
Bill Wendling932b9922012-04-02 22:16:50 +0000468 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000469 if (EnableMLSM)
470 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000471 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
472 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000473
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000474 // Nuke dead stores.
475 PM.add(createDeadStoreEliminationPass());
476
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000477 // More loops are countable; try to optimize them.
478 PM.add(createIndVarSimplifyPass());
479 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000480 if (EnableLoopInterchange)
481 PM.add(createLoopInterchangePass());
482
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000483 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000484
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000485 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000486 if (RunSLPAfterLoopVectorization)
487 if (SLPVectorize)
488 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000489
Hal Finkeld67e4632014-09-07 20:05:11 +0000490 // After vectorization, assume intrinsics may tell us more about pointer
491 // alignments.
492 PM.add(createAlignmentFromAssumptionsPass());
493
Michael J. Spencer289067c2014-05-29 01:55:07 +0000494 if (LoadCombine)
495 PM.add(createLoadCombinePass());
496
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000497 // Cleanup and simplify the code after the scalar optimizations.
498 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000499 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000500
501 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000502}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000503
Peter Collingbourne070843d2015-03-19 22:01:00 +0000504void PassManagerBuilder::addLateLTOOptimizationPasses(
505 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000506 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000507 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000508
509 // Now that we have optimized the program, discard unreachable functions.
510 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000511
512 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
513 // currently it damages debug info.
514 if (MergeFunctions)
515 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000516}
Rafael Espindola07f609152011-08-09 22:17:34 +0000517
Chandler Carruth30d69c22015-02-13 10:01:29 +0000518void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000519 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000520 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000521
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000522 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000523 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000524
Peter Collingbourne070843d2015-03-19 22:01:00 +0000525 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000526 addLTOOptimizationPasses(PM);
527
Peter Collingbourne070843d2015-03-19 22:01:00 +0000528 // Lower bit sets to globals. This pass supports Clang's control flow
529 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
530 // is enabled. The pass does nothing if CFI is disabled.
531 PM.add(createLowerBitSetsPass());
532
533 if (OptLevel != 0)
534 addLateLTOOptimizationPasses(PM);
535
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000536 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000537 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000538}
539
Eric Christopher04d4e932013-04-22 22:47:22 +0000540inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
541 return reinterpret_cast<PassManagerBuilder*>(P);
542}
543
544inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
545 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
546}
547
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000548LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000549 PassManagerBuilder *PMB = new PassManagerBuilder();
550 return wrap(PMB);
551}
552
553void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
554 PassManagerBuilder *Builder = unwrap(PMB);
555 delete Builder;
556}
557
558void
559LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
560 unsigned OptLevel) {
561 PassManagerBuilder *Builder = unwrap(PMB);
562 Builder->OptLevel = OptLevel;
563}
564
565void
566LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
567 unsigned SizeLevel) {
568 PassManagerBuilder *Builder = unwrap(PMB);
569 Builder->SizeLevel = SizeLevel;
570}
571
572void
573LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
574 LLVMBool Value) {
575 PassManagerBuilder *Builder = unwrap(PMB);
576 Builder->DisableUnitAtATime = Value;
577}
578
579void
580LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
581 LLVMBool Value) {
582 PassManagerBuilder *Builder = unwrap(PMB);
583 Builder->DisableUnrollLoops = Value;
584}
585
586void
587LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
588 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000589 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000590}
591
592void
593LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
594 unsigned Threshold) {
595 PassManagerBuilder *Builder = unwrap(PMB);
596 Builder->Inliner = createFunctionInliningPass(Threshold);
597}
598
599void
600LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
601 LLVMPassManagerRef PM) {
602 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000603 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000604 Builder->populateFunctionPassManager(*FPM);
605}
606
607void
608LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
609 LLVMPassManagerRef PM) {
610 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000611 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000612 Builder->populateModulePassManager(*MPM);
613}
614
615void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
616 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000617 LLVMBool Internalize,
618 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000619 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000620 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000621
622 // A small backwards compatibility hack. populateLTOPassManager used to take
623 // an RunInliner option.
624 if (RunInliner && !Builder->Inliner)
625 Builder->Inliner = createFunctionInliningPass();
626
627 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000628}