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