blob: 4ef55915aa3d84a230c96ca04a0cc76c63aa1ecf [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 Carruth17e0bc32015-08-06 07:33:15 +000025#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth8b046a42015-08-14 02:42:20 +000026#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth42ff4482015-08-14 02:55:50 +000027#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth1db22822015-08-14 03:33:48 +000029#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000030#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000032#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000033#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000034
35using namespace llvm;
36
Hal Finkelc34e5112012-02-01 03:51:43 +000037static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000038RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000039 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000040
41static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000042RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000043 cl::desc("Run the SLP vectorization passes"));
44
45static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000046RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000047 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000048
Hal Finkel204bf532012-04-13 17:15:33 +000049static cl::opt<bool>
50UseGVNAfterVectorization("use-gvn-after-vectorization",
51 cl::init(false), cl::Hidden,
52 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
53
Chandler Carruth7b8297a2014-10-14 00:31:29 +000054static cl::opt<bool> ExtraVectorizerPasses(
55 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
56 cl::desc("Run cleanup optimization passes after vectorization."));
57
Chandler Carruth1b398ae2012-09-14 09:22:59 +000058static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000059 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000060 cl::desc("Enable the new, experimental SROA pass"));
61
Hal Finkelbf45efd2013-11-16 23:59:05 +000062static cl::opt<bool>
63RunLoopRerolling("reroll-loops", cl::Hidden,
64 cl::desc("Run the loop rerolling pass"));
65
James Molloy0cbb2a862015-03-27 10:36:57 +000066static cl::opt<bool>
67RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
68 cl::desc("Run the float2int (float demotion) pass"));
69
Michael J. Spencer289067c2014-05-29 01:55:07 +000070static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
71 cl::Hidden,
72 cl::desc("Run the load combining pass"));
73
James Molloy568da092014-08-06 12:56:19 +000074static cl::opt<bool>
75RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000076 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000077 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
78 "vectorizer instead of before"));
79
Hal Finkel445dda52014-09-02 22:12:54 +000080static cl::opt<bool> UseCFLAA("use-cfl-aa",
81 cl::init(false), cl::Hidden,
82 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000083
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000084static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000085EnableMLSM("mlsm", cl::init(true), cl::Hidden,
86 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000087
Karthik Bhat88db86d2015-03-06 10:11:25 +000088static cl::opt<bool> EnableLoopInterchange(
89 "enable-loopinterchange", cl::init(false), cl::Hidden,
90 cl::desc("Enable the new, experimental LoopInterchange Pass"));
91
Adam Nemet938d3d62015-05-14 12:05:18 +000092static cl::opt<bool> EnableLoopDistribute(
93 "enable-loop-distribute", cl::init(false), cl::Hidden,
94 cl::desc("Enable the new, experimental LoopDistribution Pass"));
95
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000096static cl::opt<bool> EnableNonLTOGlobalsModRef(
97 "enable-non-lto-gmr", cl::init(false), cl::Hidden,
98 cl::desc(
99 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
100
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000101PassManagerBuilder::PassManagerBuilder() {
102 OptLevel = 2;
103 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000104 LibraryInfo = nullptr;
105 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000106 DisableUnitAtATime = false;
107 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000108 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000109 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000110 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000111 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000112 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000113 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000114 VerifyInput = false;
115 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000116 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000117 PrepareForLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000118}
119
120PassManagerBuilder::~PassManagerBuilder() {
121 delete LibraryInfo;
122 delete Inliner;
123}
124
David Chisnall719a72f2011-08-16 13:58:41 +0000125/// Set of global extensions, automatically added as part of the standard set.
126static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
127 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
128
129void PassManagerBuilder::addGlobalExtension(
130 PassManagerBuilder::ExtensionPointTy Ty,
131 PassManagerBuilder::ExtensionFn Fn) {
132 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
133}
134
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000135void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
136 Extensions.push_back(std::make_pair(Ty, Fn));
137}
138
139void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000140 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000141 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
142 if ((*GlobalExtensions)[i].first == ETy)
143 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000144 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
145 if (Extensions[i].first == ETy)
146 Extensions[i].second(*this, PM);
147}
148
Chandler Carruth30d69c22015-02-13 10:01:29 +0000149void PassManagerBuilder::addInitialAliasAnalysisPasses(
150 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000151 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
152 // BasicAliasAnalysis wins if they disagree. This is intended to help
153 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000154 if (UseCFLAA)
155 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000156 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000157 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000158 PM.add(createBasicAliasAnalysisPass());
159}
160
Chandler Carruth30d69c22015-02-13 10:01:29 +0000161void PassManagerBuilder::populateFunctionPassManager(
162 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000163 addExtensionsToPM(EP_EarlyAsPossible, FPM);
164
165 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000166 if (LibraryInfo)
167 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000168
169 if (OptLevel == 0) return;
170
171 addInitialAliasAnalysisPasses(FPM);
172
173 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000174 if (UseNewSROA)
175 FPM.add(createSROAPass());
176 else
177 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000178 FPM.add(createEarlyCSEPass());
179 FPM.add(createLowerExpectIntrinsicPass());
180}
181
Chandler Carruth30d69c22015-02-13 10:01:29 +0000182void PassManagerBuilder::populateModulePassManager(
183 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000184 // If all optimizations are disabled, just run the always-inline pass and,
185 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000186 if (OptLevel == 0) {
187 if (Inliner) {
188 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000189 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000190 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000191
Nick Lewycky592d8492014-10-23 23:49:31 +0000192 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
193 // creates a CGSCC pass manager, but we don't want to add extensions into
194 // that pass manager. To prevent this we insert a no-op module pass to reset
195 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
196 // builds. The function merging pass is
197 if (MergeFunctions)
198 MPM.add(createMergeFunctionsPass());
199 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000200 MPM.add(createBarrierNoopPass());
201
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000202 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000203 return;
204 }
205
206 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000207 if (LibraryInfo)
208 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000209
210 addInitialAliasAnalysisPasses(MPM);
211
212 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000213 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
214
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000215 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000216 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
217
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000218 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
219
220 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000221 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000222 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
223 }
224
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000225 if (EnableNonLTOGlobalsModRef)
226 // We add a module alias analysis pass here. In part due to bugs in the
227 // analysis infrastructure this "works" in that the analysis stays alive
228 // for the entire SCC pass run below.
229 MPM.add(createGlobalsModRefPass());
230
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000231 // Start of CallGraph SCC passes.
232 if (!DisableUnitAtATime)
233 MPM.add(createPruneEHPass()); // Remove dead EH info
234 if (Inliner) {
235 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000236 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000237 }
238 if (!DisableUnitAtATime)
239 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
240 if (OptLevel > 2)
241 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
242
243 // Start of function pass.
244 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000245 if (UseNewSROA)
246 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
247 else
248 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000249 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000250 MPM.add(createJumpThreadingPass()); // Thread jumps.
251 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
252 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
253 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000254 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000255
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000256 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000257 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
258 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000259 // Rotate Loop - disable header duplication at -Oz
260 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000261 MPM.add(createLICMPass()); // Hoist loop invariants
262 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
263 MPM.add(createInstructionCombiningPass());
264 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
265 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
266 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000267 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000268 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000269 MPM.add(createCFGSimplificationPass());
270 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000271 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000272 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000273 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
274
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000275 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000276 if (EnableMLSM)
277 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000278 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000279 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000280 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
281 MPM.add(createSCCPPass()); // Constant prop with SCCP
282
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000283 // Delete dead bit computations (instcombine runs after to fold away the dead
284 // computations, and then ADCE will run later to exploit any new DCE
285 // opportunities that creates).
286 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
287
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000288 // Run instcombine after redundancy elimination to exploit opportunities
289 // opened up by them.
290 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000291 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000292 MPM.add(createJumpThreadingPass()); // Thread jumps
293 MPM.add(createCorrelatedValuePropagationPass());
294 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000295 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000296
297 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
298
Hal Finkel29aeb202013-11-17 16:02:50 +0000299 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000300 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000301 if (!RunSLPAfterLoopVectorization) {
302 if (SLPVectorize)
303 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000304
James Molloy568da092014-08-06 12:56:19 +0000305 if (BBVectorize) {
306 MPM.add(createBBVectorizePass());
307 MPM.add(createInstructionCombiningPass());
308 addExtensionsToPM(EP_Peephole, MPM);
309 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000310 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000311 else
312 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000313
James Molloy568da092014-08-06 12:56:19 +0000314 // BBVectorize may have significantly shortened a loop body; unroll again.
315 if (!DisableUnrollLoops)
316 MPM.add(createLoopUnrollPass());
317 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000318 }
319
Michael J. Spencer289067c2014-05-29 01:55:07 +0000320 if (LoadCombine)
321 MPM.add(createLoadCombinePass());
322
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000323 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000324 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000325 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000326 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000327
Renato Golin729a3ae2013-12-05 21:20:02 +0000328 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
329 // pass manager that we are specifically trying to avoid. To prevent this
330 // we must insert a no-op module pass to reset the pass manager.
331 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000332
Chandler Carruth08eebe22015-07-23 09:34:01 +0000333 if (EnableNonLTOGlobalsModRef)
334 // We add a fresh GlobalsModRef run at this point. This is particularly
335 // useful as the above will have inlined, DCE'ed, and function-attr
336 // propagated everything. We should at this point have a reasonably minimal
337 // and richly annotated call graph. By computing aliasing and mod/ref
338 // information for all local globals here, the late loop passes and notably
339 // the vectorizer will be able to use them to help recognize vectorizable
340 // memory operations.
341 //
342 // Note that this relies on a bug in the pass manager which preserves
343 // a module analysis into a function pass pipeline (and throughout it) so
344 // long as the first function pass doesn't invalidate the module analysis.
345 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
346 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
347 // (doing nothing preserves it as it is required to be conservatively
348 // correct in the face of IR changes).
349 MPM.add(createGlobalsModRefPass());
350
James Molloy0cbb2a862015-03-27 10:36:57 +0000351 if (RunFloat2Int)
352 MPM.add(createFloat2IntPass());
353
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000354 addExtensionsToPM(EP_VectorizerStart, MPM);
355
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000356 // Re-rotate loops in all our loop nests. These may have fallout out of
357 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000358 // on the rotated form. Disable header duplication at -Oz.
359 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000360
Adam Nemet938d3d62015-05-14 12:05:18 +0000361 // Distribute loops to allow partial vectorization. I.e. isolate dependences
362 // into separate loop that would otherwise inhibit vectorization.
363 if (EnableLoopDistribute)
364 MPM.add(createLoopDistributePass());
365
Renato Golin729a3ae2013-12-05 21:20:02 +0000366 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
367 // FIXME: Because of #pragma vectorize enable, the passes below are always
368 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
369 // on -O1 and no #pragma is found). Would be good to have these two passes
370 // as function calls, so that we can only pass them when the vectorizer
371 // changed the code.
372 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000373 if (OptLevel > 1 && ExtraVectorizerPasses) {
374 // At higher optimization levels, try to clean up any runtime overlap and
375 // alignment checks inserted by the vectorizer. We want to track correllated
376 // runtime checks for two inner loops in the same outer loop, fold any
377 // common computations, hoist loop-invariant aspects out of any outer loop,
378 // and unswitch the runtime checks if possible. Once hoisted, we may have
379 // dead (or speculatable) control flows or more combining opportunities.
380 MPM.add(createEarlyCSEPass());
381 MPM.add(createCorrelatedValuePropagationPass());
382 MPM.add(createInstructionCombiningPass());
383 MPM.add(createLICMPass());
384 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
385 MPM.add(createCFGSimplificationPass());
386 MPM.add(createInstructionCombiningPass());
387 }
James Molloy568da092014-08-06 12:56:19 +0000388
389 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000390 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000391 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000392 if (OptLevel > 1 && ExtraVectorizerPasses) {
393 MPM.add(createEarlyCSEPass());
394 }
395 }
James Molloy568da092014-08-06 12:56:19 +0000396
397 if (BBVectorize) {
398 MPM.add(createBBVectorizePass());
399 MPM.add(createInstructionCombiningPass());
400 addExtensionsToPM(EP_Peephole, MPM);
401 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000402 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000403 else
404 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
405
406 // BBVectorize may have significantly shortened a loop body; unroll again.
407 if (!DisableUnrollLoops)
408 MPM.add(createLoopUnrollPass());
409 }
410 }
411
Peter Collingbourne0a437612014-05-25 10:27:02 +0000412 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000413 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000414 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000415
Kevin Qin49bc7642015-03-12 05:36:01 +0000416 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000417 MPM.add(createLoopUnrollPass()); // Unroll small loops
418
Wei Mibf727ba2015-05-14 22:02:54 +0000419 // LoopUnroll may generate some redundency to cleanup.
420 MPM.add(createInstructionCombiningPass());
421
Kevin Qin49bc7642015-03-12 05:36:01 +0000422 // Runtime unrolling will introduce runtime check in loop prologue. If the
423 // unrolled loop is a inner loop, then the prologue will be inside the
424 // outer loop. LICM pass can help to promote the runtime check out if the
425 // checked value is loop invariant.
426 MPM.add(createLICMPass());
427 }
428
Hal Finkeld67e4632014-09-07 20:05:11 +0000429 // After vectorization and unrolling, assume intrinsics may tell us more
430 // about pointer alignments.
431 MPM.add(createAlignmentFromAssumptionsPass());
432
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000433 if (!DisableUnitAtATime) {
434 // FIXME: We shouldn't bother with this anymore.
435 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
436
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000437 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000438 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000439 if (OptLevel > 1) {
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000440 if (!PrepareForLTO) {
441 // Remove avail extern fns and globals definitions if we aren't
442 // compiling an object file for later LTO. For LTO we want to preserve
443 // these so they are eligible for inlining at link-time. Note if they
444 // are unreferenced they will be removed by GlobalDCE below, so
445 // this only impacts referenced available externally globals.
446 // Eventually they will be suppressed during codegen, but eliminating
447 // here enables more opportunity for GlobalDCE as it may make
448 // globals referenced by available external functions dead.
449 MPM.add(createEliminateAvailableExternallyPass());
450 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000451 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000452 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000453 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000454 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000455
456 if (MergeFunctions)
457 MPM.add(createMergeFunctionsPass());
458
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000459 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000460}
461
Chandler Carruth30d69c22015-02-13 10:01:29 +0000462void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000463 // Provide AliasAnalysis services for optimizations.
464 addInitialAliasAnalysisPasses(PM);
465
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000466 // Propagate constants at call sites into the functions they call. This
467 // opens opportunities for globalopt (and inlining) by substituting function
468 // pointers passed as arguments to direct uses of functions.
469 PM.add(createIPSCCPPass());
470
471 // Now that we internalized some globals, see if we can hack on them!
472 PM.add(createGlobalOptimizerPass());
473
474 // Linking modules together can lead to duplicated global constants, only
475 // keep one copy of each constant.
476 PM.add(createConstantMergePass());
477
478 // Remove unused arguments from functions.
479 PM.add(createDeadArgEliminationPass());
480
481 // Reduce the code after globalopt and ipsccp. Both can open up significant
482 // simplification opportunities, and both can propagate functions through
483 // function pointers. When this happens, we often have to resolve varargs
484 // calls, etc, so let instcombine do this.
485 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000486 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000487
488 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000489 bool RunInliner = Inliner;
490 if (RunInliner) {
491 PM.add(Inliner);
492 Inliner = nullptr;
493 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000494
495 PM.add(createPruneEHPass()); // Remove dead EH info.
496
497 // Optimize globals again if we ran the inliner.
498 if (RunInliner)
499 PM.add(createGlobalOptimizerPass());
500 PM.add(createGlobalDCEPass()); // Remove dead functions.
501
502 // If we didn't decide to inline a function, check to see if we can
503 // transform it to pass arguments by value instead of by reference.
504 PM.add(createArgumentPromotionPass());
505
506 // The IPO passes may leave cruft around. Clean up after them.
507 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000508 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000509 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000510
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000511 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000512 if (UseNewSROA)
513 PM.add(createSROAPass());
514 else
515 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000516
517 // Run a few AA driven optimizations here and now, to cleanup the code.
518 PM.add(createFunctionAttrsPass()); // Add nocapture.
519 PM.add(createGlobalsModRefPass()); // IP alias analysis.
520
Bill Wendling932b9922012-04-02 22:16:50 +0000521 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000522 if (EnableMLSM)
523 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000524 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
525 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000526
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000527 // Nuke dead stores.
528 PM.add(createDeadStoreEliminationPass());
529
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000530 // More loops are countable; try to optimize them.
531 PM.add(createIndVarSimplifyPass());
532 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000533 if (EnableLoopInterchange)
534 PM.add(createLoopInterchangePass());
535
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000536 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000537
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000538 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000539 if (RunSLPAfterLoopVectorization)
540 if (SLPVectorize)
541 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000542
Hal Finkeld67e4632014-09-07 20:05:11 +0000543 // After vectorization, assume intrinsics may tell us more about pointer
544 // alignments.
545 PM.add(createAlignmentFromAssumptionsPass());
546
Michael J. Spencer289067c2014-05-29 01:55:07 +0000547 if (LoadCombine)
548 PM.add(createLoadCombinePass());
549
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000550 // Cleanup and simplify the code after the scalar optimizations.
551 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000552 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000553
554 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000555}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000556
Peter Collingbourne070843d2015-03-19 22:01:00 +0000557void PassManagerBuilder::addLateLTOOptimizationPasses(
558 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000559 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000560 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000561
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000562 // Drop bodies of available externally objects to improve GlobalDCE.
563 PM.add(createEliminateAvailableExternallyPass());
564
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000565 // Now that we have optimized the program, discard unreachable functions.
566 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000567
568 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
569 // currently it damages debug info.
570 if (MergeFunctions)
571 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000572}
Rafael Espindola07f609152011-08-09 22:17:34 +0000573
Chandler Carruth30d69c22015-02-13 10:01:29 +0000574void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000575 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000576 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000577
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000578 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000579 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000580
Peter Collingbourne070843d2015-03-19 22:01:00 +0000581 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000582 addLTOOptimizationPasses(PM);
583
Peter Collingbourne070843d2015-03-19 22:01:00 +0000584 // Lower bit sets to globals. This pass supports Clang's control flow
585 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
586 // is enabled. The pass does nothing if CFI is disabled.
587 PM.add(createLowerBitSetsPass());
588
589 if (OptLevel != 0)
590 addLateLTOOptimizationPasses(PM);
591
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000592 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000593 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000594}
595
Eric Christopher04d4e932013-04-22 22:47:22 +0000596inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
597 return reinterpret_cast<PassManagerBuilder*>(P);
598}
599
600inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
601 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
602}
603
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000604LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000605 PassManagerBuilder *PMB = new PassManagerBuilder();
606 return wrap(PMB);
607}
608
609void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
610 PassManagerBuilder *Builder = unwrap(PMB);
611 delete Builder;
612}
613
614void
615LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
616 unsigned OptLevel) {
617 PassManagerBuilder *Builder = unwrap(PMB);
618 Builder->OptLevel = OptLevel;
619}
620
621void
622LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
623 unsigned SizeLevel) {
624 PassManagerBuilder *Builder = unwrap(PMB);
625 Builder->SizeLevel = SizeLevel;
626}
627
628void
629LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
630 LLVMBool Value) {
631 PassManagerBuilder *Builder = unwrap(PMB);
632 Builder->DisableUnitAtATime = Value;
633}
634
635void
636LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
637 LLVMBool Value) {
638 PassManagerBuilder *Builder = unwrap(PMB);
639 Builder->DisableUnrollLoops = Value;
640}
641
642void
643LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
644 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000645 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000646}
647
648void
649LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
650 unsigned Threshold) {
651 PassManagerBuilder *Builder = unwrap(PMB);
652 Builder->Inliner = createFunctionInliningPass(Threshold);
653}
654
655void
656LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
657 LLVMPassManagerRef PM) {
658 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000659 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000660 Builder->populateFunctionPassManager(*FPM);
661}
662
663void
664LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
665 LLVMPassManagerRef PM) {
666 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000667 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000668 Builder->populateModulePassManager(*MPM);
669}
670
671void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
672 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000673 LLVMBool Internalize,
674 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000675 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000676 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000677
678 // A small backwards compatibility hack. populateLTOPassManager used to take
679 // an RunInliner option.
680 if (RunInliner && !Builder->Inliner)
681 Builder->Inliner = createFunctionInliningPass();
682
683 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000684}