blob: 88e5e479136f849759e3e2580a3e84937994a3c8 [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
Adam Nemet938d3d62015-05-14 12:05:18 +000088static cl::opt<bool> EnableLoopDistribute(
89 "enable-loop-distribute", cl::init(false), cl::Hidden,
90 cl::desc("Enable the new, experimental LoopDistribution Pass"));
91
Rafael Espindola3ea478b2011-08-02 21:50:27 +000092PassManagerBuilder::PassManagerBuilder() {
93 OptLevel = 2;
94 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000095 LibraryInfo = nullptr;
96 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000097 DisableUnitAtATime = false;
98 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000099 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000100 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000101 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000102 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000103 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000104 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000105 VerifyInput = false;
106 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000107 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000108 PrepareForLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000109}
110
111PassManagerBuilder::~PassManagerBuilder() {
112 delete LibraryInfo;
113 delete Inliner;
114}
115
David Chisnall719a72f2011-08-16 13:58:41 +0000116/// Set of global extensions, automatically added as part of the standard set.
117static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
118 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
119
120void PassManagerBuilder::addGlobalExtension(
121 PassManagerBuilder::ExtensionPointTy Ty,
122 PassManagerBuilder::ExtensionFn Fn) {
123 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
124}
125
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000126void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
127 Extensions.push_back(std::make_pair(Ty, Fn));
128}
129
130void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000131 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000132 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
133 if ((*GlobalExtensions)[i].first == ETy)
134 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000135 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
136 if (Extensions[i].first == ETy)
137 Extensions[i].second(*this, PM);
138}
139
Chandler Carruth30d69c22015-02-13 10:01:29 +0000140void PassManagerBuilder::addInitialAliasAnalysisPasses(
141 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000142 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
143 // BasicAliasAnalysis wins if they disagree. This is intended to help
144 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000145 if (UseCFLAA)
146 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000147 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000148 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000149 PM.add(createBasicAliasAnalysisPass());
150}
151
Chandler Carruth30d69c22015-02-13 10:01:29 +0000152void PassManagerBuilder::populateFunctionPassManager(
153 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000154 addExtensionsToPM(EP_EarlyAsPossible, FPM);
155
156 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000157 if (LibraryInfo)
158 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000159
160 if (OptLevel == 0) return;
161
162 addInitialAliasAnalysisPasses(FPM);
163
164 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000165 if (UseNewSROA)
166 FPM.add(createSROAPass());
167 else
168 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000169 FPM.add(createEarlyCSEPass());
170 FPM.add(createLowerExpectIntrinsicPass());
171}
172
Chandler Carruth30d69c22015-02-13 10:01:29 +0000173void PassManagerBuilder::populateModulePassManager(
174 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000175 // If all optimizations are disabled, just run the always-inline pass and,
176 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000177 if (OptLevel == 0) {
178 if (Inliner) {
179 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000180 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000181 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000182
Nick Lewycky592d8492014-10-23 23:49:31 +0000183 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
184 // creates a CGSCC pass manager, but we don't want to add extensions into
185 // that pass manager. To prevent this we insert a no-op module pass to reset
186 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
187 // builds. The function merging pass is
188 if (MergeFunctions)
189 MPM.add(createMergeFunctionsPass());
190 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000191 MPM.add(createBarrierNoopPass());
192
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000193 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000194 return;
195 }
196
197 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000198 if (LibraryInfo)
199 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000200
201 addInitialAliasAnalysisPasses(MPM);
202
203 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000204 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
205
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000206 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000207 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
208
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000209 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
210
211 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000212 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000213 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
214 }
215
216 // Start of CallGraph SCC passes.
217 if (!DisableUnitAtATime)
218 MPM.add(createPruneEHPass()); // Remove dead EH info
219 if (Inliner) {
220 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000221 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000222 }
223 if (!DisableUnitAtATime)
224 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
225 if (OptLevel > 2)
226 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
227
228 // Start of function pass.
229 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000230 if (UseNewSROA)
231 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
232 else
233 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000234 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000235 MPM.add(createJumpThreadingPass()); // Thread jumps.
236 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
237 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
238 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000239 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000240
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000241 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000242 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
243 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000244 // Rotate Loop - disable header duplication at -Oz
245 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000246 MPM.add(createLICMPass()); // Hoist loop invariants
247 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
248 MPM.add(createInstructionCombiningPass());
249 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
250 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
251 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000252 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000253 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000254 MPM.add(createCFGSimplificationPass());
255 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000257 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000258 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
259
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000260 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000261 if (EnableMLSM)
262 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000263 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000264 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000265 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
266 MPM.add(createSCCPPass()); // Constant prop with SCCP
267
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000268 // Delete dead bit computations (instcombine runs after to fold away the dead
269 // computations, and then ADCE will run later to exploit any new DCE
270 // opportunities that creates).
271 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
272
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000273 // Run instcombine after redundancy elimination to exploit opportunities
274 // opened up by them.
275 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000276 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000277 MPM.add(createJumpThreadingPass()); // Thread jumps
278 MPM.add(createCorrelatedValuePropagationPass());
279 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000280 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000281
282 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
283
Hal Finkel29aeb202013-11-17 16:02:50 +0000284 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000285 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000286 if (!RunSLPAfterLoopVectorization) {
287 if (SLPVectorize)
288 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000289
James Molloy568da092014-08-06 12:56:19 +0000290 if (BBVectorize) {
291 MPM.add(createBBVectorizePass());
292 MPM.add(createInstructionCombiningPass());
293 addExtensionsToPM(EP_Peephole, MPM);
294 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000295 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000296 else
297 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000298
James Molloy568da092014-08-06 12:56:19 +0000299 // BBVectorize may have significantly shortened a loop body; unroll again.
300 if (!DisableUnrollLoops)
301 MPM.add(createLoopUnrollPass());
302 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000303 }
304
Michael J. Spencer289067c2014-05-29 01:55:07 +0000305 if (LoadCombine)
306 MPM.add(createLoadCombinePass());
307
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000308 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000309 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000310 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000311 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000312
Renato Golin729a3ae2013-12-05 21:20:02 +0000313 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
314 // pass manager that we are specifically trying to avoid. To prevent this
315 // we must insert a no-op module pass to reset the pass manager.
316 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000317
James Molloy0cbb2a862015-03-27 10:36:57 +0000318 if (RunFloat2Int)
319 MPM.add(createFloat2IntPass());
320
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000321 // Re-rotate loops in all our loop nests. These may have fallout out of
322 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000323 // on the rotated form. Disable header duplication at -Oz.
324 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000325
Adam Nemet938d3d62015-05-14 12:05:18 +0000326 // Distribute loops to allow partial vectorization. I.e. isolate dependences
327 // into separate loop that would otherwise inhibit vectorization.
328 if (EnableLoopDistribute)
329 MPM.add(createLoopDistributePass());
330
Renato Golin729a3ae2013-12-05 21:20:02 +0000331 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
332 // FIXME: Because of #pragma vectorize enable, the passes below are always
333 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
334 // on -O1 and no #pragma is found). Would be good to have these two passes
335 // as function calls, so that we can only pass them when the vectorizer
336 // changed the code.
337 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000338 if (OptLevel > 1 && ExtraVectorizerPasses) {
339 // At higher optimization levels, try to clean up any runtime overlap and
340 // alignment checks inserted by the vectorizer. We want to track correllated
341 // runtime checks for two inner loops in the same outer loop, fold any
342 // common computations, hoist loop-invariant aspects out of any outer loop,
343 // and unswitch the runtime checks if possible. Once hoisted, we may have
344 // dead (or speculatable) control flows or more combining opportunities.
345 MPM.add(createEarlyCSEPass());
346 MPM.add(createCorrelatedValuePropagationPass());
347 MPM.add(createInstructionCombiningPass());
348 MPM.add(createLICMPass());
349 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
350 MPM.add(createCFGSimplificationPass());
351 MPM.add(createInstructionCombiningPass());
352 }
James Molloy568da092014-08-06 12:56:19 +0000353
354 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000355 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000356 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000357 if (OptLevel > 1 && ExtraVectorizerPasses) {
358 MPM.add(createEarlyCSEPass());
359 }
360 }
James Molloy568da092014-08-06 12:56:19 +0000361
362 if (BBVectorize) {
363 MPM.add(createBBVectorizePass());
364 MPM.add(createInstructionCombiningPass());
365 addExtensionsToPM(EP_Peephole, MPM);
366 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000367 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000368 else
369 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
370
371 // BBVectorize may have significantly shortened a loop body; unroll again.
372 if (!DisableUnrollLoops)
373 MPM.add(createLoopUnrollPass());
374 }
375 }
376
Peter Collingbourne0a437612014-05-25 10:27:02 +0000377 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000378 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000379 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000380
Kevin Qin49bc7642015-03-12 05:36:01 +0000381 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000382 MPM.add(createLoopUnrollPass()); // Unroll small loops
383
Wei Mibf727ba2015-05-14 22:02:54 +0000384 // LoopUnroll may generate some redundency to cleanup.
385 MPM.add(createInstructionCombiningPass());
386
Kevin Qin49bc7642015-03-12 05:36:01 +0000387 // Runtime unrolling will introduce runtime check in loop prologue. If the
388 // unrolled loop is a inner loop, then the prologue will be inside the
389 // outer loop. LICM pass can help to promote the runtime check out if the
390 // checked value is loop invariant.
391 MPM.add(createLICMPass());
392 }
393
Hal Finkeld67e4632014-09-07 20:05:11 +0000394 // After vectorization and unrolling, assume intrinsics may tell us more
395 // about pointer alignments.
396 MPM.add(createAlignmentFromAssumptionsPass());
397
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000398 if (!DisableUnitAtATime) {
399 // FIXME: We shouldn't bother with this anymore.
400 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
401
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000402 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000403 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000404 if (OptLevel > 1) {
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000405 if (!PrepareForLTO) {
406 // Remove avail extern fns and globals definitions if we aren't
407 // compiling an object file for later LTO. For LTO we want to preserve
408 // these so they are eligible for inlining at link-time. Note if they
409 // are unreferenced they will be removed by GlobalDCE below, so
410 // this only impacts referenced available externally globals.
411 // Eventually they will be suppressed during codegen, but eliminating
412 // here enables more opportunity for GlobalDCE as it may make
413 // globals referenced by available external functions dead.
414 MPM.add(createEliminateAvailableExternallyPass());
415 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000416 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000417 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000418 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000419 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000420
421 if (MergeFunctions)
422 MPM.add(createMergeFunctionsPass());
423
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000424 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000425}
426
Chandler Carruth30d69c22015-02-13 10:01:29 +0000427void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000428 // Provide AliasAnalysis services for optimizations.
429 addInitialAliasAnalysisPasses(PM);
430
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000431 // Propagate constants at call sites into the functions they call. This
432 // opens opportunities for globalopt (and inlining) by substituting function
433 // pointers passed as arguments to direct uses of functions.
434 PM.add(createIPSCCPPass());
435
436 // Now that we internalized some globals, see if we can hack on them!
437 PM.add(createGlobalOptimizerPass());
438
439 // Linking modules together can lead to duplicated global constants, only
440 // keep one copy of each constant.
441 PM.add(createConstantMergePass());
442
443 // Remove unused arguments from functions.
444 PM.add(createDeadArgEliminationPass());
445
446 // Reduce the code after globalopt and ipsccp. Both can open up significant
447 // simplification opportunities, and both can propagate functions through
448 // function pointers. When this happens, we often have to resolve varargs
449 // calls, etc, so let instcombine do this.
450 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000451 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000452
453 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000454 bool RunInliner = Inliner;
455 if (RunInliner) {
456 PM.add(Inliner);
457 Inliner = nullptr;
458 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000459
460 PM.add(createPruneEHPass()); // Remove dead EH info.
461
462 // Optimize globals again if we ran the inliner.
463 if (RunInliner)
464 PM.add(createGlobalOptimizerPass());
465 PM.add(createGlobalDCEPass()); // Remove dead functions.
466
467 // If we didn't decide to inline a function, check to see if we can
468 // transform it to pass arguments by value instead of by reference.
469 PM.add(createArgumentPromotionPass());
470
471 // The IPO passes may leave cruft around. Clean up after them.
472 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000473 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000474 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000475
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000476 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000477 if (UseNewSROA)
478 PM.add(createSROAPass());
479 else
480 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000481
482 // Run a few AA driven optimizations here and now, to cleanup the code.
483 PM.add(createFunctionAttrsPass()); // Add nocapture.
484 PM.add(createGlobalsModRefPass()); // IP alias analysis.
485
Bill Wendling932b9922012-04-02 22:16:50 +0000486 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000487 if (EnableMLSM)
488 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000489 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
490 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000491
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000492 // Nuke dead stores.
493 PM.add(createDeadStoreEliminationPass());
494
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000495 // More loops are countable; try to optimize them.
496 PM.add(createIndVarSimplifyPass());
497 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000498 if (EnableLoopInterchange)
499 PM.add(createLoopInterchangePass());
500
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000501 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000502
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000503 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000504 if (RunSLPAfterLoopVectorization)
505 if (SLPVectorize)
506 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000507
Hal Finkeld67e4632014-09-07 20:05:11 +0000508 // After vectorization, assume intrinsics may tell us more about pointer
509 // alignments.
510 PM.add(createAlignmentFromAssumptionsPass());
511
Michael J. Spencer289067c2014-05-29 01:55:07 +0000512 if (LoadCombine)
513 PM.add(createLoadCombinePass());
514
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000515 // Cleanup and simplify the code after the scalar optimizations.
516 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000517 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000518
519 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000520}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000521
Peter Collingbourne070843d2015-03-19 22:01:00 +0000522void PassManagerBuilder::addLateLTOOptimizationPasses(
523 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000524 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000525 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000526
527 // Now that we have optimized the program, discard unreachable functions.
528 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000529
530 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
531 // currently it damages debug info.
532 if (MergeFunctions)
533 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000534}
Rafael Espindola07f609152011-08-09 22:17:34 +0000535
Chandler Carruth30d69c22015-02-13 10:01:29 +0000536void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000537 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000538 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000539
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000540 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000541 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000542
Peter Collingbourne070843d2015-03-19 22:01:00 +0000543 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000544 addLTOOptimizationPasses(PM);
545
Peter Collingbourne070843d2015-03-19 22:01:00 +0000546 // Lower bit sets to globals. This pass supports Clang's control flow
547 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
548 // is enabled. The pass does nothing if CFI is disabled.
549 PM.add(createLowerBitSetsPass());
550
551 if (OptLevel != 0)
552 addLateLTOOptimizationPasses(PM);
553
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000554 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000555 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000556}
557
Eric Christopher04d4e932013-04-22 22:47:22 +0000558inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
559 return reinterpret_cast<PassManagerBuilder*>(P);
560}
561
562inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
563 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
564}
565
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000566LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000567 PassManagerBuilder *PMB = new PassManagerBuilder();
568 return wrap(PMB);
569}
570
571void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
572 PassManagerBuilder *Builder = unwrap(PMB);
573 delete Builder;
574}
575
576void
577LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
578 unsigned OptLevel) {
579 PassManagerBuilder *Builder = unwrap(PMB);
580 Builder->OptLevel = OptLevel;
581}
582
583void
584LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
585 unsigned SizeLevel) {
586 PassManagerBuilder *Builder = unwrap(PMB);
587 Builder->SizeLevel = SizeLevel;
588}
589
590void
591LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
592 LLVMBool Value) {
593 PassManagerBuilder *Builder = unwrap(PMB);
594 Builder->DisableUnitAtATime = Value;
595}
596
597void
598LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
599 LLVMBool Value) {
600 PassManagerBuilder *Builder = unwrap(PMB);
601 Builder->DisableUnrollLoops = Value;
602}
603
604void
605LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
606 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000607 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000608}
609
610void
611LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
612 unsigned Threshold) {
613 PassManagerBuilder *Builder = unwrap(PMB);
614 Builder->Inliner = createFunctionInliningPass(Threshold);
615}
616
617void
618LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
619 LLVMPassManagerRef PM) {
620 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000621 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000622 Builder->populateFunctionPassManager(*FPM);
623}
624
625void
626LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
627 LLVMPassManagerRef PM) {
628 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000629 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000630 Builder->populateModulePassManager(*MPM);
631}
632
633void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
634 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000635 LLVMBool Internalize,
636 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000637 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000638 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000639
640 // A small backwards compatibility hack. populateLTOPassManager used to take
641 // an RunInliner option.
642 if (RunInliner && !Builder->Inliner)
643 Builder->Inliner = createFunctionInliningPass();
644
645 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000646}