PassManagerBuilder.cpp revision bebe48dbfe00078329341945bfb11f778ace6d12
1//===- 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"
17#include "llvm-c/Transforms/PassManagerBuilder.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Analysis/Verifier.h"
21#include "llvm/PassManager.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Target/TargetLibraryInfo.h"
25#include "llvm/Transforms/IPO.h"
26#include "llvm/Transforms/Scalar.h"
27#include "llvm/Transforms/Vectorize.h"
28
29using namespace llvm;
30
31static cl::opt<bool>
32RunLoopVectorization("vectorize-loops", cl::Hidden,
33                     cl::desc("Run the Loop vectorization passes"));
34
35static cl::opt<bool>
36LateVectorization("late-vectorize", cl::init(true), cl::Hidden,
37                  cl::desc("Run the vectorization pasess late in the pass "
38                           "pipeline (after the inliner)"));
39
40static cl::opt<bool>
41RunSLPVectorization("vectorize-slp", cl::Hidden,
42                    cl::desc("Run the SLP vectorization passes"));
43
44static cl::opt<bool>
45RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
46                    cl::desc("Run the BB vectorization passes"));
47
48static cl::opt<bool>
49UseGVNAfterVectorization("use-gvn-after-vectorization",
50  cl::init(false), cl::Hidden,
51  cl::desc("Run GVN instead of Early CSE after vectorization passes"));
52
53static cl::opt<bool> UseNewSROA("use-new-sroa",
54  cl::init(true), cl::Hidden,
55  cl::desc("Enable the new, experimental SROA pass"));
56
57static cl::opt<bool>
58RunLoopRerolling("reroll-loops", cl::Hidden,
59                 cl::desc("Run the loop rerolling pass"));
60
61PassManagerBuilder::PassManagerBuilder() {
62    OptLevel = 2;
63    SizeLevel = 0;
64    LibraryInfo = 0;
65    Inliner = 0;
66    DisableUnitAtATime = false;
67    DisableUnrollLoops = false;
68    BBVectorize = RunBBVectorization;
69    SLPVectorize = RunSLPVectorization;
70    LoopVectorize = RunLoopVectorization;
71    LateVectorize = LateVectorization;
72}
73
74PassManagerBuilder::~PassManagerBuilder() {
75  delete LibraryInfo;
76  delete Inliner;
77}
78
79/// Set of global extensions, automatically added as part of the standard set.
80static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
81   PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
82
83void PassManagerBuilder::addGlobalExtension(
84    PassManagerBuilder::ExtensionPointTy Ty,
85    PassManagerBuilder::ExtensionFn Fn) {
86  GlobalExtensions->push_back(std::make_pair(Ty, Fn));
87}
88
89void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
90  Extensions.push_back(std::make_pair(Ty, Fn));
91}
92
93void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
94                                           PassManagerBase &PM) const {
95  for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
96    if ((*GlobalExtensions)[i].first == ETy)
97      (*GlobalExtensions)[i].second(*this, PM);
98  for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
99    if (Extensions[i].first == ETy)
100      Extensions[i].second(*this, PM);
101}
102
103void
104PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
105  // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
106  // BasicAliasAnalysis wins if they disagree. This is intended to help
107  // support "obvious" type-punning idioms.
108  PM.add(createTypeBasedAliasAnalysisPass());
109  PM.add(createBasicAliasAnalysisPass());
110}
111
112void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
113  addExtensionsToPM(EP_EarlyAsPossible, FPM);
114
115  // Add LibraryInfo if we have some.
116  if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
117
118  if (OptLevel == 0) return;
119
120  addInitialAliasAnalysisPasses(FPM);
121
122  FPM.add(createCFGSimplificationPass());
123  if (UseNewSROA)
124    FPM.add(createSROAPass());
125  else
126    FPM.add(createScalarReplAggregatesPass());
127  FPM.add(createEarlyCSEPass());
128  FPM.add(createLowerExpectIntrinsicPass());
129}
130
131void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
132  // If all optimizations are disabled, just run the always-inline pass.
133  if (OptLevel == 0) {
134    if (Inliner) {
135      MPM.add(Inliner);
136      Inliner = 0;
137    }
138
139    // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
140    // pass manager, but we don't want to add extensions into that pass manager.
141    // To prevent this we must insert a no-op module pass to reset the pass
142    // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
143    if (!GlobalExtensions->empty() || !Extensions.empty())
144      MPM.add(createBarrierNoopPass());
145
146    addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
147    return;
148  }
149
150  // Add LibraryInfo if we have some.
151  if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
152
153  addInitialAliasAnalysisPasses(MPM);
154
155  if (!DisableUnitAtATime) {
156    addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
157
158    MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
159
160    MPM.add(createIPSCCPPass());              // IP SCCP
161    MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
162
163    MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
164    MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
165  }
166
167  // Start of CallGraph SCC passes.
168  if (!DisableUnitAtATime)
169    MPM.add(createPruneEHPass());             // Remove dead EH info
170  if (Inliner) {
171    MPM.add(Inliner);
172    Inliner = 0;
173  }
174  if (!DisableUnitAtATime)
175    MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
176  if (OptLevel > 2)
177    MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
178
179  // Start of function pass.
180  // Break up aggregate allocas, using SSAUpdater.
181  if (UseNewSROA)
182    MPM.add(createSROAPass(/*RequiresDomTree*/ false));
183  else
184    MPM.add(createScalarReplAggregatesPass(-1, false));
185  MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
186  MPM.add(createJumpThreadingPass());         // Thread jumps.
187  MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
188  MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
189  MPM.add(createInstructionCombiningPass());  // Combine silly seq's
190
191  MPM.add(createTailCallEliminationPass());   // Eliminate tail calls
192  MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
193  MPM.add(createReassociatePass());           // Reassociate expressions
194  MPM.add(createLoopRotatePass());            // Rotate Loop
195  MPM.add(createLICMPass());                  // Hoist loop invariants
196  MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
197  MPM.add(createInstructionCombiningPass());
198  MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
199  MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
200  MPM.add(createLoopDeletionPass());          // Delete dead loops
201
202  if (!LateVectorize && LoopVectorize)
203      MPM.add(createLoopVectorizePass(DisableUnrollLoops));
204
205  if (!DisableUnrollLoops)
206    MPM.add(createLoopUnrollPass());          // Unroll small loops
207  addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
208
209  if (OptLevel > 1)
210    MPM.add(createGVNPass());                 // Remove redundancies
211  MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
212  MPM.add(createSCCPPass());                  // Constant prop with SCCP
213
214  // Run instcombine after redundancy elimination to exploit opportunities
215  // opened up by them.
216  MPM.add(createInstructionCombiningPass());
217  MPM.add(createJumpThreadingPass());         // Thread jumps
218  MPM.add(createCorrelatedValuePropagationPass());
219  MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
220
221  addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
222
223  if (RunLoopRerolling)
224    MPM.add(createLoopRerollPass());
225  if (SLPVectorize)
226    MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
227
228  if (BBVectorize) {
229    MPM.add(createBBVectorizePass());
230    MPM.add(createInstructionCombiningPass());
231    if (OptLevel > 1 && UseGVNAfterVectorization)
232      MPM.add(createGVNPass());           // Remove redundancies
233    else
234      MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
235
236    // BBVectorize may have significantly shortened a loop body; unroll again.
237    if (!DisableUnrollLoops)
238      MPM.add(createLoopUnrollPass());
239  }
240
241  MPM.add(createAggressiveDCEPass());         // Delete dead instructions
242  MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
243  MPM.add(createInstructionCombiningPass());  // Clean up after everything.
244
245  // As an experimental mode, run any vectorization passes in a separate
246  // pipeline from the CGSCC pass manager that runs iteratively with the
247  // inliner.
248  if (LateVectorize && LoopVectorize) {
249    // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
250    // pass manager that we are specifically trying to avoid. To prevent this
251    // we must insert a no-op module pass to reset the pass manager.
252    MPM.add(createBarrierNoopPass());
253
254    // Add the various vectorization passes and relevant cleanup passes for
255    // them since we are no longer in the middle of the main scalar pipeline.
256    MPM.add(createLoopVectorizePass(DisableUnrollLoops));
257    MPM.add(createInstructionCombiningPass());
258    MPM.add(createCFGSimplificationPass());
259  }
260
261  if (!DisableUnitAtATime) {
262    // FIXME: We shouldn't bother with this anymore.
263    MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
264
265    // GlobalOpt already deletes dead functions and globals, at -O2 try a
266    // late pass of GlobalDCE.  It is capable of deleting dead cycles.
267    if (OptLevel > 1) {
268      MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
269      MPM.add(createConstantMergePass());     // Merge dup global constants
270    }
271  }
272  addExtensionsToPM(EP_OptimizerLast, MPM);
273}
274
275void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
276                                                bool Internalize,
277                                                bool RunInliner,
278                                                bool DisableGVNLoadPRE) {
279  // Provide AliasAnalysis services for optimizations.
280  addInitialAliasAnalysisPasses(PM);
281
282  // Now that composite has been compiled, scan through the module, looking
283  // for a main function.  If main is defined, mark all other functions
284  // internal.
285  if (Internalize)
286    PM.add(createInternalizePass("main"));
287
288  // Propagate constants at call sites into the functions they call.  This
289  // opens opportunities for globalopt (and inlining) by substituting function
290  // pointers passed as arguments to direct uses of functions.
291  PM.add(createIPSCCPPass());
292
293  // Now that we internalized some globals, see if we can hack on them!
294  PM.add(createGlobalOptimizerPass());
295
296  // Linking modules together can lead to duplicated global constants, only
297  // keep one copy of each constant.
298  PM.add(createConstantMergePass());
299
300  // Remove unused arguments from functions.
301  PM.add(createDeadArgEliminationPass());
302
303  // Reduce the code after globalopt and ipsccp.  Both can open up significant
304  // simplification opportunities, and both can propagate functions through
305  // function pointers.  When this happens, we often have to resolve varargs
306  // calls, etc, so let instcombine do this.
307  PM.add(createInstructionCombiningPass());
308
309  // Inline small functions
310  if (RunInliner)
311    PM.add(createFunctionInliningPass());
312
313  PM.add(createPruneEHPass());   // Remove dead EH info.
314
315  // Optimize globals again if we ran the inliner.
316  if (RunInliner)
317    PM.add(createGlobalOptimizerPass());
318  PM.add(createGlobalDCEPass()); // Remove dead functions.
319
320  // If we didn't decide to inline a function, check to see if we can
321  // transform it to pass arguments by value instead of by reference.
322  PM.add(createArgumentPromotionPass());
323
324  // The IPO passes may leave cruft around.  Clean up after them.
325  PM.add(createInstructionCombiningPass());
326  PM.add(createJumpThreadingPass());
327
328  // Break up allocas
329  if (UseNewSROA)
330    PM.add(createSROAPass());
331  else
332    PM.add(createScalarReplAggregatesPass());
333
334  // Run a few AA driven optimizations here and now, to cleanup the code.
335  PM.add(createFunctionAttrsPass()); // Add nocapture.
336  PM.add(createGlobalsModRefPass()); // IP alias analysis.
337
338  PM.add(createLICMPass());                 // Hoist loop invariants.
339  PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
340  PM.add(createMemCpyOptPass());            // Remove dead memcpys.
341
342  // Nuke dead stores.
343  PM.add(createDeadStoreEliminationPass());
344
345  // Cleanup and simplify the code after the scalar optimizations.
346  PM.add(createInstructionCombiningPass());
347
348  PM.add(createJumpThreadingPass());
349
350  // Delete basic blocks, which optimization passes may have killed.
351  PM.add(createCFGSimplificationPass());
352
353  // Now that we have optimized the program, discard unreachable functions.
354  PM.add(createGlobalDCEPass());
355}
356
357inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
358    return reinterpret_cast<PassManagerBuilder*>(P);
359}
360
361inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
362  return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
363}
364
365LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
366  PassManagerBuilder *PMB = new PassManagerBuilder();
367  return wrap(PMB);
368}
369
370void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
371  PassManagerBuilder *Builder = unwrap(PMB);
372  delete Builder;
373}
374
375void
376LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
377                                  unsigned OptLevel) {
378  PassManagerBuilder *Builder = unwrap(PMB);
379  Builder->OptLevel = OptLevel;
380}
381
382void
383LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
384                                   unsigned SizeLevel) {
385  PassManagerBuilder *Builder = unwrap(PMB);
386  Builder->SizeLevel = SizeLevel;
387}
388
389void
390LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
391                                            LLVMBool Value) {
392  PassManagerBuilder *Builder = unwrap(PMB);
393  Builder->DisableUnitAtATime = Value;
394}
395
396void
397LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
398                                            LLVMBool Value) {
399  PassManagerBuilder *Builder = unwrap(PMB);
400  Builder->DisableUnrollLoops = Value;
401}
402
403void
404LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
405                                                 LLVMBool Value) {
406  // NOTE: The simplify-libcalls pass has been removed.
407}
408
409void
410LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
411                                              unsigned Threshold) {
412  PassManagerBuilder *Builder = unwrap(PMB);
413  Builder->Inliner = createFunctionInliningPass(Threshold);
414}
415
416void
417LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
418                                                  LLVMPassManagerRef PM) {
419  PassManagerBuilder *Builder = unwrap(PMB);
420  FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
421  Builder->populateFunctionPassManager(*FPM);
422}
423
424void
425LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
426                                                LLVMPassManagerRef PM) {
427  PassManagerBuilder *Builder = unwrap(PMB);
428  PassManagerBase *MPM = unwrap(PM);
429  Builder->populateModulePassManager(*MPM);
430}
431
432void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
433                                                  LLVMPassManagerRef PM,
434                                                  LLVMBool Internalize,
435                                                  LLVMBool RunInliner) {
436  PassManagerBuilder *Builder = unwrap(PMB);
437  PassManagerBase *LPM = unwrap(PM);
438  Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);
439}
440