2009-06-03 19:52:39 +02:00
|
|
|
//===-- llvm/Support/StandardPasses.h - Standard pass lists -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines utility functions for creating a "standard" set of
|
|
|
|
// optimization passes, so that compilers and tools which use optimization
|
|
|
|
// passes use the same set of standard passes.
|
|
|
|
//
|
|
|
|
// These are implemented as inline functions so that we do not have to worry
|
|
|
|
// about link issues.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_STANDARDPASSES_H
|
|
|
|
#define LLVM_SUPPORT_STANDARDPASSES_H
|
|
|
|
|
|
|
|
#include "llvm/PassManager.h"
|
2010-07-26 20:11:16 +02:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2009-06-03 23:06:14 +02:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2009-06-03 19:52:39 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
/// createStandardFunctionPasses - Add the standard list of function passes to
|
|
|
|
/// the provided pass manager.
|
|
|
|
///
|
|
|
|
/// \arg OptimizationLevel - The optimization level, corresponding to -O0,
|
|
|
|
/// -O1, etc.
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardFunctionPasses(PassManagerBase *PM,
|
2009-06-03 19:52:39 +02:00
|
|
|
unsigned OptimizationLevel);
|
|
|
|
|
|
|
|
/// createStandardModulePasses - Add the standard list of module passes to the
|
|
|
|
/// provided pass manager.
|
|
|
|
///
|
|
|
|
/// \arg OptimizationLevel - The optimization level, corresponding to -O0,
|
|
|
|
/// -O1, etc.
|
|
|
|
/// \arg OptimizeSize - Whether the transformations should optimize for size.
|
|
|
|
/// \arg UnitAtATime - Allow passes which may make global module changes.
|
|
|
|
/// \arg UnrollLoops - Allow loop unrolling.
|
|
|
|
/// \arg SimplifyLibCalls - Allow library calls to be simplified.
|
|
|
|
/// \arg HaveExceptions - Whether the module may have code using exceptions.
|
|
|
|
/// \arg InliningPass - The inlining pass to use, if any, or null. This will
|
|
|
|
/// always be added, even at -O0.a
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardModulePasses(PassManagerBase *PM,
|
2009-06-03 19:52:39 +02:00
|
|
|
unsigned OptimizationLevel,
|
|
|
|
bool OptimizeSize,
|
|
|
|
bool UnitAtATime,
|
|
|
|
bool UnrollLoops,
|
|
|
|
bool SimplifyLibCalls,
|
|
|
|
bool HaveExceptions,
|
|
|
|
Pass *InliningPass);
|
|
|
|
|
2009-06-03 23:06:14 +02:00
|
|
|
/// createStandardLTOPasses - Add the standard list of module passes suitable
|
|
|
|
/// for link time optimization.
|
|
|
|
///
|
|
|
|
/// Internalize - Run the internalize pass.
|
|
|
|
/// RunInliner - Use a function inlining pass.
|
|
|
|
/// VerifyEach - Run the verifier after each pass.
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardLTOPasses(PassManagerBase *PM,
|
2009-06-03 23:06:14 +02:00
|
|
|
bool Internalize,
|
|
|
|
bool RunInliner,
|
|
|
|
bool VerifyEach);
|
|
|
|
|
2009-06-03 19:52:39 +02:00
|
|
|
// Implementations
|
|
|
|
|
2010-10-18 20:50:27 +02:00
|
|
|
static inline void createStandardAliasAnalysisPasses(PassManagerBase *PM) {
|
|
|
|
// Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
|
|
|
|
// BasicAliasAnalysis wins if they disagree. This is intended to help
|
|
|
|
// support "obvious" type-punning idioms.
|
|
|
|
PM->add(createTypeBasedAliasAnalysisPass());
|
|
|
|
PM->add(createBasicAliasAnalysisPass());
|
|
|
|
}
|
|
|
|
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardFunctionPasses(PassManagerBase *PM,
|
2009-06-03 19:52:39 +02:00
|
|
|
unsigned OptimizationLevel) {
|
|
|
|
if (OptimizationLevel > 0) {
|
2010-10-18 20:50:27 +02:00
|
|
|
createStandardAliasAnalysisPasses(PM);
|
2009-06-03 19:52:39 +02:00
|
|
|
PM->add(createCFGSimplificationPass());
|
2011-01-14 01:36:40 +01:00
|
|
|
PM->add(createScalarReplAggregatesPass());
|
2011-01-14 01:41:11 +01:00
|
|
|
PM->add(createEarlyCSEPass());
|
2009-06-03 19:52:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 00:01:30 +02:00
|
|
|
/// createStandardModulePasses - Add the standard module passes. This is
|
|
|
|
/// expected to be run after the standard function passes.
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardModulePasses(PassManagerBase *PM,
|
2009-06-03 19:52:39 +02:00
|
|
|
unsigned OptimizationLevel,
|
|
|
|
bool OptimizeSize,
|
|
|
|
bool UnitAtATime,
|
|
|
|
bool UnrollLoops,
|
2011-01-05 02:03:32 +01:00
|
|
|
bool OptimizeBuiltins,
|
2009-06-03 19:52:39 +02:00
|
|
|
bool HaveExceptions,
|
|
|
|
Pass *InliningPass) {
|
2010-10-18 20:50:27 +02:00
|
|
|
createStandardAliasAnalysisPasses(PM);
|
2010-10-18 20:04:47 +02:00
|
|
|
|
2009-06-03 19:52:39 +02:00
|
|
|
if (OptimizationLevel == 0) {
|
|
|
|
if (InliningPass)
|
|
|
|
PM->add(InliningPass);
|
2009-09-19 00:01:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-06-03 19:52:39 +02:00
|
|
|
|
2009-09-19 00:01:30 +02:00
|
|
|
if (UnitAtATime) {
|
|
|
|
PM->add(createGlobalOptimizerPass()); // Optimize out global vars
|
2009-11-01 20:09:12 +01:00
|
|
|
|
2009-11-03 20:35:13 +01:00
|
|
|
PM->add(createIPSCCPPass()); // IP SCCP
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createDeadArgEliminationPass()); // Dead argument elimination
|
|
|
|
}
|
|
|
|
PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
|
|
|
|
PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
|
2009-11-01 21:41:59 +01:00
|
|
|
|
|
|
|
// Start of CallGraph SCC passes.
|
2009-11-03 10:40:08 +01:00
|
|
|
if (UnitAtATime && HaveExceptions)
|
|
|
|
PM->add(createPruneEHPass()); // Remove dead EH info
|
2009-09-19 00:01:30 +02:00
|
|
|
if (InliningPass)
|
|
|
|
PM->add(InliningPass);
|
2009-11-03 10:40:08 +01:00
|
|
|
if (UnitAtATime)
|
|
|
|
PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
|
2009-09-19 00:01:30 +02:00
|
|
|
if (OptimizationLevel > 2)
|
|
|
|
PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
|
2009-11-01 21:41:59 +01:00
|
|
|
|
|
|
|
// Start of function pass.
|
|
|
|
PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
|
2011-01-03 07:19:09 +01:00
|
|
|
PM->add(createEarlyCSEPass()); // Catch trivial redundancies
|
2011-01-05 02:03:32 +01:00
|
|
|
if (OptimizeBuiltins)
|
2010-02-09 18:29:18 +01:00
|
|
|
PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createJumpThreadingPass()); // Thread jumps.
|
2010-09-03 06:16:28 +02:00
|
|
|
PM->add(createCorrelatedValuePropagationPass()); // Propagate conditionals
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createCFGSimplificationPass()); // Merge & remove BBs
|
|
|
|
PM->add(createInstructionCombiningPass()); // Combine silly seq's
|
2009-11-01 21:41:59 +01:00
|
|
|
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createTailCallEliminationPass()); // Eliminate tail calls
|
|
|
|
PM->add(createCFGSimplificationPass()); // Merge & remove BBs
|
|
|
|
PM->add(createReassociatePass()); // Reassociate expressions
|
|
|
|
PM->add(createLoopRotatePass()); // Rotate Loop
|
|
|
|
PM->add(createLICMPass()); // Hoist loop invariants
|
2009-10-30 08:23:49 +01:00
|
|
|
PM->add(createLoopUnswitchPass(OptimizeSize || OptimizationLevel < 3));
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createInstructionCombiningPass());
|
|
|
|
PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
|
2011-01-05 02:03:32 +01:00
|
|
|
if (OptimizeBuiltins)
|
|
|
|
PM->add(createLoopIdiomPass()); // Recognize idioms like memset.
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createLoopDeletionPass()); // Delete dead loops
|
|
|
|
if (UnrollLoops)
|
|
|
|
PM->add(createLoopUnrollPass()); // Unroll small loops
|
|
|
|
PM->add(createInstructionCombiningPass()); // Clean up after the unroller
|
2009-12-22 05:47:41 +01:00
|
|
|
if (OptimizationLevel > 1)
|
2010-02-28 06:34:05 +01:00
|
|
|
PM->add(createGVNPass()); // Remove redundancies
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
|
|
|
|
PM->add(createSCCPPass()); // Constant prop with SCCP
|
|
|
|
|
|
|
|
// Run instcombine after redundancy elimination to exploit opportunities
|
|
|
|
// opened up by them.
|
|
|
|
PM->add(createInstructionCombiningPass());
|
2009-11-11 00:54:10 +01:00
|
|
|
PM->add(createJumpThreadingPass()); // Thread jumps
|
2010-09-03 06:16:28 +02:00
|
|
|
PM->add(createCorrelatedValuePropagationPass());
|
2009-09-19 00:01:30 +02:00
|
|
|
PM->add(createDeadStoreEliminationPass()); // Delete dead stores
|
|
|
|
PM->add(createAggressiveDCEPass()); // Delete dead instructions
|
|
|
|
PM->add(createCFGSimplificationPass()); // Merge & remove BBs
|
2009-06-03 19:52:39 +02:00
|
|
|
|
2009-09-19 00:01:30 +02:00
|
|
|
if (UnitAtATime) {
|
|
|
|
PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
|
|
|
|
PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
|
|
|
|
|
2009-11-01 20:09:12 +01:00
|
|
|
// GlobalOpt already deletes dead functions and globals, at -O3 try a
|
|
|
|
// late pass of GlobalDCE. It is capable of deleting dead cycles.
|
|
|
|
if (OptimizationLevel > 2)
|
|
|
|
PM->add(createGlobalDCEPass()); // Remove dead fns and globals.
|
|
|
|
|
|
|
|
if (OptimizationLevel > 1)
|
|
|
|
PM->add(createConstantMergePass()); // Merge dup global constants
|
|
|
|
}
|
2009-06-03 19:52:39 +02:00
|
|
|
}
|
2009-06-03 23:06:14 +02:00
|
|
|
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void addOnePass(PassManagerBase *PM, Pass *P, bool AndVerify) {
|
2009-06-03 23:06:14 +02:00
|
|
|
PM->add(P);
|
|
|
|
|
|
|
|
if (AndVerify)
|
|
|
|
PM->add(createVerifierPass());
|
|
|
|
}
|
|
|
|
|
2010-05-14 17:35:10 +02:00
|
|
|
static inline void createStandardLTOPasses(PassManagerBase *PM,
|
2009-06-03 23:06:14 +02:00
|
|
|
bool Internalize,
|
|
|
|
bool RunInliner,
|
|
|
|
bool VerifyEach) {
|
2010-10-18 20:04:47 +02:00
|
|
|
// Provide AliasAnalysis services for optimizations.
|
2010-10-18 20:50:27 +02:00
|
|
|
createStandardAliasAnalysisPasses(PM);
|
2010-10-18 20:04:47 +02:00
|
|
|
|
2009-06-03 23:06:14 +02:00
|
|
|
// Now that composite has been compiled, scan through the module, looking
|
|
|
|
// for a main function. If main is defined, mark all other functions
|
|
|
|
// internal.
|
|
|
|
if (Internalize)
|
|
|
|
addOnePass(PM, createInternalizePass(true), VerifyEach);
|
|
|
|
|
|
|
|
// Propagate constants at call sites into the functions they call. This
|
|
|
|
// opens opportunities for globalopt (and inlining) by substituting function
|
|
|
|
// pointers passed as arguments to direct uses of functions.
|
|
|
|
addOnePass(PM, createIPSCCPPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Now that we internalized some globals, see if we can hack on them!
|
|
|
|
addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Linking modules together can lead to duplicated global constants, only
|
|
|
|
// keep one copy of each constant...
|
|
|
|
addOnePass(PM, createConstantMergePass(), VerifyEach);
|
|
|
|
|
|
|
|
// Remove unused arguments from functions...
|
|
|
|
addOnePass(PM, createDeadArgEliminationPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Reduce the code after globalopt and ipsccp. Both can open up significant
|
|
|
|
// simplification opportunities, and both can propagate functions through
|
|
|
|
// function pointers. When this happens, we often have to resolve varargs
|
|
|
|
// calls, etc, so let instcombine do this.
|
|
|
|
addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Inline small functions
|
|
|
|
if (RunInliner)
|
|
|
|
addOnePass(PM, createFunctionInliningPass(), VerifyEach);
|
|
|
|
|
|
|
|
addOnePass(PM, createPruneEHPass(), VerifyEach); // Remove dead EH info.
|
2009-06-03 23:51:32 +02:00
|
|
|
// Optimize globals again if we ran the inliner.
|
|
|
|
if (RunInliner)
|
2009-06-03 23:06:14 +02:00
|
|
|
addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
|
|
|
|
addOnePass(PM, createGlobalDCEPass(), VerifyEach); // Remove dead functions.
|
|
|
|
|
|
|
|
// If we didn't decide to inline a function, check to see if we can
|
|
|
|
// transform it to pass arguments by value instead of by reference.
|
|
|
|
addOnePass(PM, createArgumentPromotionPass(), VerifyEach);
|
|
|
|
|
|
|
|
// The IPO passes may leave cruft around. Clean up after them.
|
|
|
|
addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
|
|
|
|
addOnePass(PM, createJumpThreadingPass(), VerifyEach);
|
|
|
|
// Break up allocas
|
|
|
|
addOnePass(PM, createScalarReplAggregatesPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Run a few AA driven optimizations here and now, to cleanup the code.
|
|
|
|
addOnePass(PM, createFunctionAttrsPass(), VerifyEach); // Add nocapture.
|
|
|
|
addOnePass(PM, createGlobalsModRefPass(), VerifyEach); // IP alias analysis.
|
|
|
|
|
|
|
|
addOnePass(PM, createLICMPass(), VerifyEach); // Hoist loop invariants.
|
|
|
|
addOnePass(PM, createGVNPass(), VerifyEach); // Remove redundancies.
|
|
|
|
addOnePass(PM, createMemCpyOptPass(), VerifyEach); // Remove dead memcpys.
|
|
|
|
// Nuke dead stores.
|
|
|
|
addOnePass(PM, createDeadStoreEliminationPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Cleanup and simplify the code after the scalar optimizations.
|
|
|
|
addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
|
|
|
|
|
|
|
|
addOnePass(PM, createJumpThreadingPass(), VerifyEach);
|
|
|
|
|
2009-10-11 06:17:33 +02:00
|
|
|
// Delete basic blocks, which optimization passes may have killed.
|
2009-06-03 23:06:14 +02:00
|
|
|
addOnePass(PM, createCFGSimplificationPass(), VerifyEach);
|
|
|
|
|
|
|
|
// Now that we have optimized the program, discard unreachable functions.
|
|
|
|
addOnePass(PM, createGlobalDCEPass(), VerifyEach);
|
|
|
|
}
|
2009-06-03 19:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|