-/// Optimize module M using various IPO passes. Use exportList to
-/// internalize selected symbols. Target platform is selected
-/// based on information available to module M. No new target
-/// features are selected.
-enum LTOStatus
-LTO::optimize(Module *M, std::ostream &Out,
- std::vector<const char *> &exportList)
-{
- // Instantiate the pass manager to organize the passes.
- PassManager Passes;
-
- // Collect Target info
- getTarget(M);
-
- if (!Target)
- return LTO_NO_TARGET;
-
- // If target supports exception handling then enable it now.
- if (Target->getTargetAsmInfo()->doesSupportExceptionHandling())
- ExceptionHandling = true;
-
- // Start off with a verification pass.
- Passes.add(createVerifierPass());
-
- // Add an appropriate TargetData instance for this module...
- Passes.add(new TargetData(*Target->getTargetData()));
-
- // Internalize symbols if export list is nonemty
- if (!exportList.empty())
- Passes.add(createInternalizePass(exportList));
-
- // Now that we internalized some globals, see if we can hack on them!
- Passes.add(createGlobalOptimizerPass());
-
- // Linking modules together can lead to duplicated global constants, only
- // keep one copy of each constant...
- Passes.add(createConstantMergePass());
-
- // If the -s command line option was specified, strip the symbols out of the
- // resulting program to make it smaller. -s is a GLD option that we are
- // supporting.
- Passes.add(createStripSymbolsPass());
-
- // Propagate constants at call sites into the functions they call.
- Passes.add(createIPConstantPropagationPass());
-
- // Remove unused arguments from functions...
- Passes.add(createDeadArgEliminationPass());
-
- Passes.add(createFunctionInliningPass()); // Inline small functions
-
- Passes.add(createPruneEHPass()); // Remove dead EH info
-
- Passes.add(createGlobalDCEPass()); // 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.
- Passes.add(createArgumentPromotionPass());
-
- // The IPO passes may leave cruft around. Clean up after them.
- Passes.add(createInstructionCombiningPass());
-
- Passes.add(createScalarReplAggregatesPass()); // Break up allocas
-
- // Run a few AA driven optimizations here and now, to cleanup the code.
- Passes.add(createGlobalsModRefPass()); // IP alias analysis
-
- Passes.add(createLICMPass()); // Hoist loop invariants
- Passes.add(createGVNPass()); // Remove common subexprs
- Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
-
- // Cleanup and simplify the code after the scalar optimizations.
- Passes.add(createInstructionCombiningPass());
-
- // Delete basic blocks, which optimization passes may have killed...
- Passes.add(createCFGSimplificationPass());
-
- // Now that we have optimized the program, discard unreachable functions...
- Passes.add(createGlobalDCEPass());
-
- // Make sure everything is still good.
- Passes.add(createVerifierPass());
-
- FunctionPassManager *CodeGenPasses =
- new FunctionPassManager(new ExistingModuleProvider(M));
-
- CodeGenPasses->add(new TargetData(*Target->getTargetData()));
-
- MachineCodeEmitter *MCE = 0;
-
- switch (Target->addPassesToEmitFile(*CodeGenPasses, Out,
- TargetMachine::AssemblyFile, true)) {
- default:
- case FileModel::Error:
- return LTO_WRITE_FAILURE;
- case FileModel::AsmFile:
- break;
- case FileModel::MachOFile:
- MCE = AddMachOWriter(*CodeGenPasses, Out, *Target);
- break;
- case FileModel::ElfFile:
- MCE = AddELFWriter(*CodeGenPasses, Out, *Target);
- break;
- }