blob: ec6f21e8c64fd416f23670120dfb8f502cf8b967 [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 Carruth21dcff72015-08-14 03:48:20 +000027#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth42ff4482015-08-14 02:55:50 +000028#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000029#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth1db22822015-08-14 03:33:48 +000030#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000031#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000033#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000034#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000035
36using namespace llvm;
37
Hal Finkelc34e5112012-02-01 03:51:43 +000038static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000039RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000040 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000041
42static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000043RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000044 cl::desc("Run the SLP vectorization passes"));
45
46static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000047RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000048 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000049
Hal Finkel204bf532012-04-13 17:15:33 +000050static cl::opt<bool>
51UseGVNAfterVectorization("use-gvn-after-vectorization",
52 cl::init(false), cl::Hidden,
53 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
54
Chandler Carruth7b8297a2014-10-14 00:31:29 +000055static cl::opt<bool> ExtraVectorizerPasses(
56 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
57 cl::desc("Run cleanup optimization passes after vectorization."));
58
Chandler Carruth1b398ae2012-09-14 09:22:59 +000059static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000060 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000061 cl::desc("Enable the new, experimental SROA pass"));
62
Hal Finkelbf45efd2013-11-16 23:59:05 +000063static cl::opt<bool>
64RunLoopRerolling("reroll-loops", cl::Hidden,
65 cl::desc("Run the loop rerolling pass"));
66
James Molloy0cbb2a862015-03-27 10:36:57 +000067static cl::opt<bool>
68RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
69 cl::desc("Run the float2int (float demotion) pass"));
70
Michael J. Spencer289067c2014-05-29 01:55:07 +000071static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
72 cl::Hidden,
73 cl::desc("Run the load combining pass"));
74
James Molloy568da092014-08-06 12:56:19 +000075static cl::opt<bool>
76RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000077 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000078 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
79 "vectorizer instead of before"));
80
Hal Finkel445dda52014-09-02 22:12:54 +000081static cl::opt<bool> UseCFLAA("use-cfl-aa",
82 cl::init(false), cl::Hidden,
83 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000084
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000085static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000086EnableMLSM("mlsm", cl::init(true), cl::Hidden,
87 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000088
Karthik Bhat88db86d2015-03-06 10:11:25 +000089static cl::opt<bool> EnableLoopInterchange(
90 "enable-loopinterchange", cl::init(false), cl::Hidden,
91 cl::desc("Enable the new, experimental LoopInterchange Pass"));
92
Adam Nemet938d3d62015-05-14 12:05:18 +000093static cl::opt<bool> EnableLoopDistribute(
94 "enable-loop-distribute", cl::init(false), cl::Hidden,
95 cl::desc("Enable the new, experimental LoopDistribution Pass"));
96
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000097static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +000098 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000099 cl::desc(
100 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
101
Adam Nemete54a4fa2015-11-03 23:50:08 +0000102static cl::opt<bool> EnableLoopLoadElim(
103 "enable-loop-load-elim", cl::init(false), cl::Hidden,
104 cl::desc("Enable the new, experimental LoopLoadElimination Pass"));
105
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000106PassManagerBuilder::PassManagerBuilder() {
107 OptLevel = 2;
108 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000109 LibraryInfo = nullptr;
110 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000111 DisableUnitAtATime = false;
112 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000113 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000114 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000115 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000116 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000117 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000118 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000119 VerifyInput = false;
120 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000121 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000122 PrepareForLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000123}
124
125PassManagerBuilder::~PassManagerBuilder() {
126 delete LibraryInfo;
127 delete Inliner;
128}
129
David Chisnall719a72f2011-08-16 13:58:41 +0000130/// Set of global extensions, automatically added as part of the standard set.
131static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
132 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
133
134void PassManagerBuilder::addGlobalExtension(
135 PassManagerBuilder::ExtensionPointTy Ty,
136 PassManagerBuilder::ExtensionFn Fn) {
137 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
138}
139
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000140void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
141 Extensions.push_back(std::make_pair(Ty, Fn));
142}
143
144void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000145 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000146 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
147 if ((*GlobalExtensions)[i].first == ETy)
148 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000149 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
150 if (Extensions[i].first == ETy)
151 Extensions[i].second(*this, PM);
152}
153
Chandler Carruth30d69c22015-02-13 10:01:29 +0000154void PassManagerBuilder::addInitialAliasAnalysisPasses(
155 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000156 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
157 // BasicAliasAnalysis wins if they disagree. This is intended to help
158 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000159 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000160 PM.add(createCFLAAWrapperPass());
161 PM.add(createTypeBasedAAWrapperPass());
162 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000163}
164
Chandler Carruth30d69c22015-02-13 10:01:29 +0000165void PassManagerBuilder::populateFunctionPassManager(
166 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000167 addExtensionsToPM(EP_EarlyAsPossible, FPM);
168
169 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000170 if (LibraryInfo)
171 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000172
173 if (OptLevel == 0) return;
174
175 addInitialAliasAnalysisPasses(FPM);
176
177 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000178 if (UseNewSROA)
179 FPM.add(createSROAPass());
180 else
181 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000182 FPM.add(createEarlyCSEPass());
183 FPM.add(createLowerExpectIntrinsicPass());
184}
185
Chandler Carruth30d69c22015-02-13 10:01:29 +0000186void PassManagerBuilder::populateModulePassManager(
187 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000188 // If all optimizations are disabled, just run the always-inline pass and,
189 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000190 if (OptLevel == 0) {
191 if (Inliner) {
192 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000193 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000194 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000195
Nick Lewycky592d8492014-10-23 23:49:31 +0000196 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
197 // creates a CGSCC pass manager, but we don't want to add extensions into
198 // that pass manager. To prevent this we insert a no-op module pass to reset
199 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
200 // builds. The function merging pass is
201 if (MergeFunctions)
202 MPM.add(createMergeFunctionsPass());
203 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000204 MPM.add(createBarrierNoopPass());
205
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000206 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000207 return;
208 }
209
210 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000211 if (LibraryInfo)
212 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000213
214 addInitialAliasAnalysisPasses(MPM);
215
216 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000217 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
218
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000219 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000220 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
221
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000222 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
223
224 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000225 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000226 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
227 }
228
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000229 if (EnableNonLTOGlobalsModRef)
230 // We add a module alias analysis pass here. In part due to bugs in the
231 // analysis infrastructure this "works" in that the analysis stays alive
232 // for the entire SCC pass run below.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000233 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000234
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000235 // Start of CallGraph SCC passes.
236 if (!DisableUnitAtATime)
237 MPM.add(createPruneEHPass()); // Remove dead EH info
238 if (Inliner) {
239 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000240 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000241 }
242 if (!DisableUnitAtATime)
243 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
244 if (OptLevel > 2)
245 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
246
247 // Start of function pass.
248 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000249 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000250 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000251 else
252 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000253 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000254 MPM.add(createJumpThreadingPass()); // Thread jumps.
255 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
256 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
257 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000258 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000259
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000260 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000261 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
262 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000263 // Rotate Loop - disable header duplication at -Oz
264 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000265 MPM.add(createLICMPass()); // Hoist loop invariants
266 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000267 MPM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000268 MPM.add(createInstructionCombiningPass());
269 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
270 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
271 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000272 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000273 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000274 MPM.add(createCFGSimplificationPass());
275 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000276 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000277 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000278 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
279
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000280 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000281 if (EnableMLSM)
282 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000283 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000284 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000285 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
286 MPM.add(createSCCPPass()); // Constant prop with SCCP
287
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000288 // Delete dead bit computations (instcombine runs after to fold away the dead
289 // computations, and then ADCE will run later to exploit any new DCE
290 // opportunities that creates).
291 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
292
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000293 // Run instcombine after redundancy elimination to exploit opportunities
294 // opened up by them.
295 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000296 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000297 MPM.add(createJumpThreadingPass()); // Thread jumps
298 MPM.add(createCorrelatedValuePropagationPass());
299 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000300 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000301
302 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
303
Hal Finkel29aeb202013-11-17 16:02:50 +0000304 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000305 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000306 if (!RunSLPAfterLoopVectorization) {
307 if (SLPVectorize)
308 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000309
James Molloy568da092014-08-06 12:56:19 +0000310 if (BBVectorize) {
311 MPM.add(createBBVectorizePass());
312 MPM.add(createInstructionCombiningPass());
313 addExtensionsToPM(EP_Peephole, MPM);
314 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000315 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000316 else
317 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000318
James Molloy568da092014-08-06 12:56:19 +0000319 // BBVectorize may have significantly shortened a loop body; unroll again.
320 if (!DisableUnrollLoops)
321 MPM.add(createLoopUnrollPass());
322 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000323 }
324
Michael J. Spencer289067c2014-05-29 01:55:07 +0000325 if (LoadCombine)
326 MPM.add(createLoadCombinePass());
327
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000328 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000329 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000330 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000331 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000332
Renato Golin729a3ae2013-12-05 21:20:02 +0000333 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
334 // pass manager that we are specifically trying to avoid. To prevent this
335 // we must insert a no-op module pass to reset the pass manager.
336 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000337
Yaron Keren611c7cf2015-09-02 06:34:11 +0000338 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) {
339 // Remove avail extern fns and globals definitions if we aren't
340 // compiling an object file for later LTO. For LTO we want to preserve
341 // these so they are eligible for inlining at link-time. Note if they
342 // are unreferenced they will be removed by GlobalDCE later, so
343 // this only impacts referenced available externally globals.
344 // Eventually they will be suppressed during codegen, but eliminating
345 // here enables more opportunity for GlobalDCE as it may make
346 // globals referenced by available external functions dead
347 // and saves running remaining passes on the eliminated functions.
348 MPM.add(createEliminateAvailableExternallyPass());
349 }
350
Chandler Carruth08eebe22015-07-23 09:34:01 +0000351 if (EnableNonLTOGlobalsModRef)
352 // We add a fresh GlobalsModRef run at this point. This is particularly
353 // useful as the above will have inlined, DCE'ed, and function-attr
354 // propagated everything. We should at this point have a reasonably minimal
355 // and richly annotated call graph. By computing aliasing and mod/ref
356 // information for all local globals here, the late loop passes and notably
357 // the vectorizer will be able to use them to help recognize vectorizable
358 // memory operations.
359 //
360 // Note that this relies on a bug in the pass manager which preserves
361 // a module analysis into a function pass pipeline (and throughout it) so
362 // long as the first function pass doesn't invalidate the module analysis.
363 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
364 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
365 // (doing nothing preserves it as it is required to be conservatively
366 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000367 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000368
James Molloy0cbb2a862015-03-27 10:36:57 +0000369 if (RunFloat2Int)
370 MPM.add(createFloat2IntPass());
371
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000372 addExtensionsToPM(EP_VectorizerStart, MPM);
373
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000374 // Re-rotate loops in all our loop nests. These may have fallout out of
375 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000376 // on the rotated form. Disable header duplication at -Oz.
377 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000378
Adam Nemet938d3d62015-05-14 12:05:18 +0000379 // Distribute loops to allow partial vectorization. I.e. isolate dependences
380 // into separate loop that would otherwise inhibit vectorization.
381 if (EnableLoopDistribute)
382 MPM.add(createLoopDistributePass());
383
Renato Golin729a3ae2013-12-05 21:20:02 +0000384 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000385
386 // Eliminate loads by forwarding stores from the previous iteration to loads
387 // of the current iteration.
388 if (EnableLoopLoadElim)
389 MPM.add(createLoopLoadEliminationPass());
390
Renato Golin729a3ae2013-12-05 21:20:02 +0000391 // FIXME: Because of #pragma vectorize enable, the passes below are always
392 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
393 // on -O1 and no #pragma is found). Would be good to have these two passes
394 // as function calls, so that we can only pass them when the vectorizer
395 // changed the code.
396 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000397 if (OptLevel > 1 && ExtraVectorizerPasses) {
398 // At higher optimization levels, try to clean up any runtime overlap and
399 // alignment checks inserted by the vectorizer. We want to track correllated
400 // runtime checks for two inner loops in the same outer loop, fold any
401 // common computations, hoist loop-invariant aspects out of any outer loop,
402 // and unswitch the runtime checks if possible. Once hoisted, we may have
403 // dead (or speculatable) control flows or more combining opportunities.
404 MPM.add(createEarlyCSEPass());
405 MPM.add(createCorrelatedValuePropagationPass());
406 MPM.add(createInstructionCombiningPass());
407 MPM.add(createLICMPass());
408 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
409 MPM.add(createCFGSimplificationPass());
410 MPM.add(createInstructionCombiningPass());
411 }
James Molloy568da092014-08-06 12:56:19 +0000412
413 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000414 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000415 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000416 if (OptLevel > 1 && ExtraVectorizerPasses) {
417 MPM.add(createEarlyCSEPass());
418 }
419 }
James Molloy568da092014-08-06 12:56:19 +0000420
421 if (BBVectorize) {
422 MPM.add(createBBVectorizePass());
423 MPM.add(createInstructionCombiningPass());
424 addExtensionsToPM(EP_Peephole, MPM);
425 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000426 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000427 else
428 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
429
430 // BBVectorize may have significantly shortened a loop body; unroll again.
431 if (!DisableUnrollLoops)
432 MPM.add(createLoopUnrollPass());
433 }
434 }
435
Peter Collingbourne0a437612014-05-25 10:27:02 +0000436 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000437 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000438 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000439
Kevin Qin49bc7642015-03-12 05:36:01 +0000440 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000441 MPM.add(createLoopUnrollPass()); // Unroll small loops
442
Wei Mibf727ba2015-05-14 22:02:54 +0000443 // LoopUnroll may generate some redundency to cleanup.
444 MPM.add(createInstructionCombiningPass());
445
Kevin Qin49bc7642015-03-12 05:36:01 +0000446 // Runtime unrolling will introduce runtime check in loop prologue. If the
447 // unrolled loop is a inner loop, then the prologue will be inside the
448 // outer loop. LICM pass can help to promote the runtime check out if the
449 // checked value is loop invariant.
450 MPM.add(createLICMPass());
451 }
452
Hal Finkeld67e4632014-09-07 20:05:11 +0000453 // After vectorization and unrolling, assume intrinsics may tell us more
454 // about pointer alignments.
455 MPM.add(createAlignmentFromAssumptionsPass());
456
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000457 if (!DisableUnitAtATime) {
458 // FIXME: We shouldn't bother with this anymore.
459 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
460
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000461 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000462 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000463 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000464 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000465 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000466 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000467 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000468
469 if (MergeFunctions)
470 MPM.add(createMergeFunctionsPass());
471
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000472 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000473}
474
Chandler Carruth30d69c22015-02-13 10:01:29 +0000475void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000476 // Provide AliasAnalysis services for optimizations.
477 addInitialAliasAnalysisPasses(PM);
478
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000479 // Propagate constants at call sites into the functions they call. This
480 // opens opportunities for globalopt (and inlining) by substituting function
481 // pointers passed as arguments to direct uses of functions.
482 PM.add(createIPSCCPPass());
483
484 // Now that we internalized some globals, see if we can hack on them!
James Molloy9ad4f222015-11-18 11:24:42 +0000485 PM.add(createFunctionAttrsPass()); // Add norecurse if possible.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000486 PM.add(createGlobalOptimizerPass());
487
488 // Linking modules together can lead to duplicated global constants, only
489 // keep one copy of each constant.
490 PM.add(createConstantMergePass());
491
492 // Remove unused arguments from functions.
493 PM.add(createDeadArgEliminationPass());
494
495 // Reduce the code after globalopt and ipsccp. Both can open up significant
496 // simplification opportunities, and both can propagate functions through
497 // function pointers. When this happens, we often have to resolve varargs
498 // calls, etc, so let instcombine do this.
499 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000500 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000501
502 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000503 bool RunInliner = Inliner;
504 if (RunInliner) {
505 PM.add(Inliner);
506 Inliner = nullptr;
507 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000508
509 PM.add(createPruneEHPass()); // Remove dead EH info.
510
511 // Optimize globals again if we ran the inliner.
512 if (RunInliner)
513 PM.add(createGlobalOptimizerPass());
514 PM.add(createGlobalDCEPass()); // Remove dead functions.
515
516 // If we didn't decide to inline a function, check to see if we can
517 // transform it to pass arguments by value instead of by reference.
518 PM.add(createArgumentPromotionPass());
519
520 // The IPO passes may leave cruft around. Clean up after them.
521 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000522 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000523 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000524
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000525 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000526 if (UseNewSROA)
527 PM.add(createSROAPass());
528 else
529 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000530
531 // Run a few AA driven optimizations here and now, to cleanup the code.
532 PM.add(createFunctionAttrsPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000533 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000534
Bill Wendling932b9922012-04-02 22:16:50 +0000535 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000536 if (EnableMLSM)
537 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000538 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
539 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000540
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000541 // Nuke dead stores.
542 PM.add(createDeadStoreEliminationPass());
543
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000544 // More loops are countable; try to optimize them.
545 PM.add(createIndVarSimplifyPass());
546 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000547 if (EnableLoopInterchange)
548 PM.add(createLoopInterchangePass());
549
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000550 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000551
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000552 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000553 if (RunSLPAfterLoopVectorization)
554 if (SLPVectorize)
555 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000556
Hal Finkeld67e4632014-09-07 20:05:11 +0000557 // After vectorization, assume intrinsics may tell us more about pointer
558 // alignments.
559 PM.add(createAlignmentFromAssumptionsPass());
560
Michael J. Spencer289067c2014-05-29 01:55:07 +0000561 if (LoadCombine)
562 PM.add(createLoadCombinePass());
563
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000564 // Cleanup and simplify the code after the scalar optimizations.
565 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000566 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000567
568 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000569}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000570
Peter Collingbourne070843d2015-03-19 22:01:00 +0000571void PassManagerBuilder::addLateLTOOptimizationPasses(
572 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000573 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000574 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000575
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000576 // Drop bodies of available externally objects to improve GlobalDCE.
577 PM.add(createEliminateAvailableExternallyPass());
578
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000579 // Now that we have optimized the program, discard unreachable functions.
580 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000581
582 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
583 // currently it damages debug info.
584 if (MergeFunctions)
585 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000586}
Rafael Espindola07f609152011-08-09 22:17:34 +0000587
Chandler Carruth30d69c22015-02-13 10:01:29 +0000588void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000589 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000590 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000591
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000592 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000593 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000594
Peter Collingbourne070843d2015-03-19 22:01:00 +0000595 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000596 addLTOOptimizationPasses(PM);
597
Peter Collingbourne070843d2015-03-19 22:01:00 +0000598 // Lower bit sets to globals. This pass supports Clang's control flow
599 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
600 // is enabled. The pass does nothing if CFI is disabled.
601 PM.add(createLowerBitSetsPass());
602
603 if (OptLevel != 0)
604 addLateLTOOptimizationPasses(PM);
605
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000606 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000607 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000608}
609
Eric Christopher04d4e932013-04-22 22:47:22 +0000610inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
611 return reinterpret_cast<PassManagerBuilder*>(P);
612}
613
614inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
615 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
616}
617
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000618LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000619 PassManagerBuilder *PMB = new PassManagerBuilder();
620 return wrap(PMB);
621}
622
623void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
624 PassManagerBuilder *Builder = unwrap(PMB);
625 delete Builder;
626}
627
628void
629LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
630 unsigned OptLevel) {
631 PassManagerBuilder *Builder = unwrap(PMB);
632 Builder->OptLevel = OptLevel;
633}
634
635void
636LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
637 unsigned SizeLevel) {
638 PassManagerBuilder *Builder = unwrap(PMB);
639 Builder->SizeLevel = SizeLevel;
640}
641
642void
643LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
644 LLVMBool Value) {
645 PassManagerBuilder *Builder = unwrap(PMB);
646 Builder->DisableUnitAtATime = Value;
647}
648
649void
650LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
651 LLVMBool Value) {
652 PassManagerBuilder *Builder = unwrap(PMB);
653 Builder->DisableUnrollLoops = Value;
654}
655
656void
657LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
658 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000659 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000660}
661
662void
663LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
664 unsigned Threshold) {
665 PassManagerBuilder *Builder = unwrap(PMB);
666 Builder->Inliner = createFunctionInliningPass(Threshold);
667}
668
669void
670LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
671 LLVMPassManagerRef PM) {
672 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000673 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000674 Builder->populateFunctionPassManager(*FPM);
675}
676
677void
678LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
679 LLVMPassManagerRef PM) {
680 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000681 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000682 Builder->populateModulePassManager(*MPM);
683}
684
685void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
686 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000687 LLVMBool Internalize,
688 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000689 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000690 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000691
692 // A small backwards compatibility hack. populateLTOPassManager used to take
693 // an RunInliner option.
694 if (RunInliner && !Builder->Inliner)
695 Builder->Inliner = createFunctionInliningPass();
696
697 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000698}