blob: 3b21d3f0cb134b1ea04389bcf03f6fdace19c39b [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;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000097 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000098 DisableUnitAtATime = false;
99 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000100 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000101 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000102 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000103 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000104 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000105 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000106 VerifyInput = false;
107 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000108 MergeFunctions = 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
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000241 if (!DisableTailCalls)
242 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
244 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000245 // Rotate Loop - disable header duplication at -Oz
246 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000247 MPM.add(createLICMPass()); // Hoist loop invariants
248 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
249 MPM.add(createInstructionCombiningPass());
250 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
251 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
252 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000253 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000254 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000255 MPM.add(createCFGSimplificationPass());
256 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000257 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000258 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000259 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
260
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000261 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000262 if (EnableMLSM)
263 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000264 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000265 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000266 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
267 MPM.add(createSCCPPass()); // Constant prop with SCCP
268
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000269 // Delete dead bit computations (instcombine runs after to fold away the dead
270 // computations, and then ADCE will run later to exploit any new DCE
271 // opportunities that creates).
272 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
273
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000274 // Run instcombine after redundancy elimination to exploit opportunities
275 // opened up by them.
276 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000277 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000278 MPM.add(createJumpThreadingPass()); // Thread jumps
279 MPM.add(createCorrelatedValuePropagationPass());
280 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000281 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000282
283 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
284
Hal Finkel29aeb202013-11-17 16:02:50 +0000285 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000286 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000287 if (!RunSLPAfterLoopVectorization) {
288 if (SLPVectorize)
289 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000290
James Molloy568da092014-08-06 12:56:19 +0000291 if (BBVectorize) {
292 MPM.add(createBBVectorizePass());
293 MPM.add(createInstructionCombiningPass());
294 addExtensionsToPM(EP_Peephole, MPM);
295 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000296 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000297 else
298 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000299
James Molloy568da092014-08-06 12:56:19 +0000300 // BBVectorize may have significantly shortened a loop body; unroll again.
301 if (!DisableUnrollLoops)
302 MPM.add(createLoopUnrollPass());
303 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000304 }
305
Michael J. Spencer289067c2014-05-29 01:55:07 +0000306 if (LoadCombine)
307 MPM.add(createLoadCombinePass());
308
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000309 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000310 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000311 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000312 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000313
Renato Golin729a3ae2013-12-05 21:20:02 +0000314 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
315 // pass manager that we are specifically trying to avoid. To prevent this
316 // we must insert a no-op module pass to reset the pass manager.
317 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000318
James Molloy0cbb2a862015-03-27 10:36:57 +0000319 if (RunFloat2Int)
320 MPM.add(createFloat2IntPass());
321
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000322 // Re-rotate loops in all our loop nests. These may have fallout out of
323 // rotated form due to GVN or other transformations, and the vectorizer relies
324 // on the rotated form.
Michael Zolotukhin267e12f2015-03-10 19:07:41 +0000325 MPM.add(createLoopRotatePass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000326
Adam Nemet938d3d62015-05-14 12:05:18 +0000327 // Distribute loops to allow partial vectorization. I.e. isolate dependences
328 // into separate loop that would otherwise inhibit vectorization.
329 if (EnableLoopDistribute)
330 MPM.add(createLoopDistributePass());
331
Renato Golin729a3ae2013-12-05 21:20:02 +0000332 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
333 // FIXME: Because of #pragma vectorize enable, the passes below are always
334 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
335 // on -O1 and no #pragma is found). Would be good to have these two passes
336 // as function calls, so that we can only pass them when the vectorizer
337 // changed the code.
338 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000339 if (OptLevel > 1 && ExtraVectorizerPasses) {
340 // At higher optimization levels, try to clean up any runtime overlap and
341 // alignment checks inserted by the vectorizer. We want to track correllated
342 // runtime checks for two inner loops in the same outer loop, fold any
343 // common computations, hoist loop-invariant aspects out of any outer loop,
344 // and unswitch the runtime checks if possible. Once hoisted, we may have
345 // dead (or speculatable) control flows or more combining opportunities.
346 MPM.add(createEarlyCSEPass());
347 MPM.add(createCorrelatedValuePropagationPass());
348 MPM.add(createInstructionCombiningPass());
349 MPM.add(createLICMPass());
350 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
351 MPM.add(createCFGSimplificationPass());
352 MPM.add(createInstructionCombiningPass());
353 }
James Molloy568da092014-08-06 12:56:19 +0000354
355 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000356 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000357 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000358 if (OptLevel > 1 && ExtraVectorizerPasses) {
359 MPM.add(createEarlyCSEPass());
360 }
361 }
James Molloy568da092014-08-06 12:56:19 +0000362
363 if (BBVectorize) {
364 MPM.add(createBBVectorizePass());
365 MPM.add(createInstructionCombiningPass());
366 addExtensionsToPM(EP_Peephole, MPM);
367 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000368 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000369 else
370 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
371
372 // BBVectorize may have significantly shortened a loop body; unroll again.
373 if (!DisableUnrollLoops)
374 MPM.add(createLoopUnrollPass());
375 }
376 }
377
Peter Collingbourne0a437612014-05-25 10:27:02 +0000378 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000379 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000380 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000381
Kevin Qin49bc7642015-03-12 05:36:01 +0000382 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000383 MPM.add(createLoopUnrollPass()); // Unroll small loops
384
Wei Mibf727ba2015-05-14 22:02:54 +0000385 // LoopUnroll may generate some redundency to cleanup.
386 MPM.add(createInstructionCombiningPass());
387
Kevin Qin49bc7642015-03-12 05:36:01 +0000388 // This is a barrier pass to avoid combine LICM pass and loop unroll pass
389 // within same loop pass manager.
390 MPM.add(createInstructionSimplifierPass());
391
392 // Runtime unrolling will introduce runtime check in loop prologue. If the
393 // unrolled loop is a inner loop, then the prologue will be inside the
394 // outer loop. LICM pass can help to promote the runtime check out if the
395 // checked value is loop invariant.
396 MPM.add(createLICMPass());
397 }
398
Hal Finkeld67e4632014-09-07 20:05:11 +0000399 // After vectorization and unrolling, assume intrinsics may tell us more
400 // about pointer alignments.
401 MPM.add(createAlignmentFromAssumptionsPass());
402
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000403 if (!DisableUnitAtATime) {
404 // FIXME: We shouldn't bother with this anymore.
405 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
406
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000407 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000408 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000409 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000410 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000411 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000412 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000413 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000414
415 if (MergeFunctions)
416 MPM.add(createMergeFunctionsPass());
417
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000418 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000419}
420
Chandler Carruth30d69c22015-02-13 10:01:29 +0000421void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000422 // Provide AliasAnalysis services for optimizations.
423 addInitialAliasAnalysisPasses(PM);
424
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000425 // Propagate constants at call sites into the functions they call. This
426 // opens opportunities for globalopt (and inlining) by substituting function
427 // pointers passed as arguments to direct uses of functions.
428 PM.add(createIPSCCPPass());
429
430 // Now that we internalized some globals, see if we can hack on them!
431 PM.add(createGlobalOptimizerPass());
432
433 // Linking modules together can lead to duplicated global constants, only
434 // keep one copy of each constant.
435 PM.add(createConstantMergePass());
436
437 // Remove unused arguments from functions.
438 PM.add(createDeadArgEliminationPass());
439
440 // Reduce the code after globalopt and ipsccp. Both can open up significant
441 // simplification opportunities, and both can propagate functions through
442 // function pointers. When this happens, we often have to resolve varargs
443 // calls, etc, so let instcombine do this.
444 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000445 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000446
447 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000448 bool RunInliner = Inliner;
449 if (RunInliner) {
450 PM.add(Inliner);
451 Inliner = nullptr;
452 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000453
454 PM.add(createPruneEHPass()); // Remove dead EH info.
455
456 // Optimize globals again if we ran the inliner.
457 if (RunInliner)
458 PM.add(createGlobalOptimizerPass());
459 PM.add(createGlobalDCEPass()); // Remove dead functions.
460
461 // If we didn't decide to inline a function, check to see if we can
462 // transform it to pass arguments by value instead of by reference.
463 PM.add(createArgumentPromotionPass());
464
465 // The IPO passes may leave cruft around. Clean up after them.
466 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000467 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000468 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000469
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000470 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000471 if (UseNewSROA)
472 PM.add(createSROAPass());
473 else
474 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000475
476 // Run a few AA driven optimizations here and now, to cleanup the code.
477 PM.add(createFunctionAttrsPass()); // Add nocapture.
478 PM.add(createGlobalsModRefPass()); // IP alias analysis.
479
Bill Wendling932b9922012-04-02 22:16:50 +0000480 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000481 if (EnableMLSM)
482 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000483 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
484 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000485
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000486 // Nuke dead stores.
487 PM.add(createDeadStoreEliminationPass());
488
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000489 // More loops are countable; try to optimize them.
490 PM.add(createIndVarSimplifyPass());
491 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000492 if (EnableLoopInterchange)
493 PM.add(createLoopInterchangePass());
494
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000495 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000496
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000497 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000498 if (RunSLPAfterLoopVectorization)
499 if (SLPVectorize)
500 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000501
Hal Finkeld67e4632014-09-07 20:05:11 +0000502 // After vectorization, assume intrinsics may tell us more about pointer
503 // alignments.
504 PM.add(createAlignmentFromAssumptionsPass());
505
Michael J. Spencer289067c2014-05-29 01:55:07 +0000506 if (LoadCombine)
507 PM.add(createLoadCombinePass());
508
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000509 // Cleanup and simplify the code after the scalar optimizations.
510 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000511 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000512
513 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000514}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000515
Peter Collingbourne070843d2015-03-19 22:01:00 +0000516void PassManagerBuilder::addLateLTOOptimizationPasses(
517 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000518 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000519 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000520
521 // Now that we have optimized the program, discard unreachable functions.
522 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000523
524 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
525 // currently it damages debug info.
526 if (MergeFunctions)
527 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000528}
Rafael Espindola07f609152011-08-09 22:17:34 +0000529
Chandler Carruth30d69c22015-02-13 10:01:29 +0000530void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000531 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000532 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000533
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000534 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000535 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000536
Peter Collingbourne070843d2015-03-19 22:01:00 +0000537 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000538 addLTOOptimizationPasses(PM);
539
Peter Collingbourne070843d2015-03-19 22:01:00 +0000540 // Lower bit sets to globals. This pass supports Clang's control flow
541 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
542 // is enabled. The pass does nothing if CFI is disabled.
543 PM.add(createLowerBitSetsPass());
544
545 if (OptLevel != 0)
546 addLateLTOOptimizationPasses(PM);
547
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000548 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000549 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000550}
551
Eric Christopher04d4e932013-04-22 22:47:22 +0000552inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
553 return reinterpret_cast<PassManagerBuilder*>(P);
554}
555
556inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
557 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
558}
559
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000560LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000561 PassManagerBuilder *PMB = new PassManagerBuilder();
562 return wrap(PMB);
563}
564
565void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
566 PassManagerBuilder *Builder = unwrap(PMB);
567 delete Builder;
568}
569
570void
571LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
572 unsigned OptLevel) {
573 PassManagerBuilder *Builder = unwrap(PMB);
574 Builder->OptLevel = OptLevel;
575}
576
577void
578LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
579 unsigned SizeLevel) {
580 PassManagerBuilder *Builder = unwrap(PMB);
581 Builder->SizeLevel = SizeLevel;
582}
583
584void
585LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
586 LLVMBool Value) {
587 PassManagerBuilder *Builder = unwrap(PMB);
588 Builder->DisableUnitAtATime = Value;
589}
590
591void
592LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
593 LLVMBool Value) {
594 PassManagerBuilder *Builder = unwrap(PMB);
595 Builder->DisableUnrollLoops = Value;
596}
597
598void
599LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
600 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000601 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000602}
603
604void
605LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
606 unsigned Threshold) {
607 PassManagerBuilder *Builder = unwrap(PMB);
608 Builder->Inliner = createFunctionInliningPass(Threshold);
609}
610
611void
612LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
613 LLVMPassManagerRef PM) {
614 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000615 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000616 Builder->populateFunctionPassManager(*FPM);
617}
618
619void
620LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
621 LLVMPassManagerRef PM) {
622 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000623 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000624 Builder->populateModulePassManager(*MPM);
625}
626
627void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
628 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000629 LLVMBool Internalize,
630 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000631 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000632 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000633
634 // A small backwards compatibility hack. populateLTOPassManager used to take
635 // an RunInliner option.
636 if (RunInliner && !Builder->Inliner)
637 Builder->Inliner = createFunctionInliningPass();
638
639 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000640}