2004-04-02 05:06:57 +00:00
|
|
|
//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// Optimizations may be specified an arbitrary number of times on the command
|
2006-08-18 06:34:30 +00:00
|
|
|
// line, They are run in the order specified.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-10-18 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2009-07-01 16:58:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-01-31 00:47:12 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2007-06-28 23:09:25 +00:00
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
2007-05-06 02:42:03 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2001-10-19 15:39:14 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2002-02-20 17:56:53 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-03-07 00:26:10 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2007-06-28 23:09:25 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2006-05-12 06:33:49 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-09-16 16:09:43 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-07-26 21:09:32 +00:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2010-01-05 01:30:32 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-09-03 16:00:08 +00:00
|
|
|
#include "llvm/Support/IRReader.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-05-06 02:42:03 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2009-12-09 00:41:28 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-06-03 18:22:15 +00:00
|
|
|
#include "llvm/Support/StandardPasses.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2008-10-22 03:25:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-08-21 05:34:03 +00:00
|
|
|
#include "llvm/LinkAllPasses.h"
|
2006-06-07 23:03:13 +00:00
|
|
|
#include "llvm/LinkAllVMCore.h"
|
2001-11-26 19:22:39 +00:00
|
|
|
#include <memory>
|
2002-07-23 18:12:22 +00:00
|
|
|
#include <algorithm>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
2002-04-13 18:32:47 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
// The OptimizationList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
2002-01-31 00:47:12 +00:00
|
|
|
//
|
2006-08-27 22:07:01 +00:00
|
|
|
static cl::list<const PassInfo*, bool, PassNameParser>
|
|
|
|
PassList(cl::desc("Optimizations available:"));
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
// Other command line options...
|
2002-01-31 00:47:12 +00:00
|
|
|
//
|
2003-05-22 20:13:16 +00:00
|
|
|
static cl::opt<std::string>
|
2009-08-21 23:29:40 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
|
2006-08-18 06:34:30 +00:00
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2003-05-22 20:13:16 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2003-12-10 14:41:33 +00:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-08-25 15:34:52 +00:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintEachXForm("p", cl::desc("Print module after each transformation"));
|
|
|
|
|
2003-02-12 18:43:33 +00:00
|
|
|
static cl::opt<bool>
|
2003-02-26 20:00:41 +00:00
|
|
|
NoOutput("disable-output",
|
2007-07-05 17:07:56 +00:00
|
|
|
cl::desc("Do not write result bitcode file"), cl::Hidden);
|
2003-02-12 18:43:33 +00:00
|
|
|
|
2009-09-05 11:34:53 +00:00
|
|
|
static cl::opt<bool>
|
2009-10-14 20:01:39 +00:00
|
|
|
OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
|
2009-09-05 11:34:53 +00:00
|
|
|
|
2003-02-12 18:45:08 +00:00
|
|
|
static cl::opt<bool>
|
2003-02-26 20:00:41 +00:00
|
|
|
NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
|
2003-02-12 18:45:08 +00:00
|
|
|
|
2007-02-02 14:46:29 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
VerifyEach("verify-each", cl::desc("Verify after each transform"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
StripDebug("strip-debug",
|
|
|
|
cl::desc("Strip debugger symbol info from translation unit"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
|
|
|
|
|
2009-08-21 23:29:40 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableOptimizations("disable-opt",
|
2007-02-02 14:46:29 +00:00
|
|
|
cl::desc("Do not run any optimization passes"));
|
|
|
|
|
2009-08-21 23:29:40 +00:00
|
|
|
static cl::opt<bool>
|
2009-07-17 18:09:39 +00:00
|
|
|
DisableInternalize("disable-internalize",
|
|
|
|
cl::desc("Do not mark all symbols as internal"));
|
|
|
|
|
2007-02-02 14:46:29 +00:00
|
|
|
static cl::opt<bool>
|
2009-08-21 23:29:40 +00:00
|
|
|
StandardCompileOpts("std-compile-opts",
|
2007-02-02 14:46:29 +00:00
|
|
|
cl::desc("Include the standard compile time optimizations"));
|
|
|
|
|
2009-07-17 18:09:39 +00:00
|
|
|
static cl::opt<bool>
|
2009-08-21 23:29:40 +00:00
|
|
|
StandardLinkOpts("std-link-opts",
|
2009-07-17 18:09:39 +00:00
|
|
|
cl::desc("Include the standard link time optimizations"));
|
|
|
|
|
2008-09-16 22:25:14 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
OptLevelO1("O1",
|
|
|
|
cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
OptLevelO2("O2",
|
2008-09-17 00:01:04 +00:00
|
|
|
cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
|
2008-09-16 22:25:14 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
OptLevelO3("O3",
|
2008-09-17 00:01:04 +00:00
|
|
|
cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
|
2008-09-16 22:25:14 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
UnitAtATime("funit-at-a-time",
|
2009-08-21 23:30:30 +00:00
|
|
|
cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
|
|
|
|
cl::init(true));
|
2008-09-16 22:25:14 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DisableSimplifyLibCalls("disable-simplify-libcalls",
|
2008-09-17 16:01:39 +00:00
|
|
|
cl::desc("Disable simplify-libcalls"));
|
2008-09-16 22:25:14 +00:00
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::opt<bool>
|
2004-05-27 20:32:10 +00:00
|
|
|
Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2004-05-27 16:28:54 +00:00
|
|
|
static cl::alias
|
|
|
|
QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
|
|
|
|
|
2006-08-18 06:34:30 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
|
|
|
|
|
2009-10-22 00:44:10 +00:00
|
|
|
static cl::opt<std::string>
|
|
|
|
DefaultDataLayout("default-data-layout",
|
|
|
|
cl::desc("data layout string to use if not specified by module"),
|
|
|
|
cl::value_desc("layout-string"), cl::init(""));
|
|
|
|
|
2006-08-18 06:34:30 +00:00
|
|
|
// ---------- Define Printers for module and function passes ------------
|
|
|
|
namespace {
|
|
|
|
|
2007-06-28 23:09:25 +00:00
|
|
|
struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
|
|
|
|
static char ID;
|
|
|
|
const PassInfo *PassToPrint;
|
2009-08-21 23:29:40 +00:00
|
|
|
CallGraphSCCPassPrinter(const PassInfo *PI) :
|
2009-02-18 05:09:16 +00:00
|
|
|
CallGraphSCCPass(&ID), PassToPrint(PI) {}
|
2007-06-28 23:09:25 +00:00
|
|
|
|
2009-08-31 00:19:58 +00:00
|
|
|
virtual bool runOnSCC(std::vector<CallGraphNode *>&SCC) {
|
2007-06-28 23:09:25 +00:00
|
|
|
if (!Quiet) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
2007-06-28 23:09:25 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
|
|
|
Function *F = SCC[i]->getFunction();
|
2009-07-16 15:30:09 +00:00
|
|
|
if (F) {
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
|
2009-07-16 15:30:09 +00:00
|
|
|
}
|
2007-06-28 23:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-21 23:29:40 +00:00
|
|
|
|
2007-06-28 23:09:25 +00:00
|
|
|
virtual const char *getPassName() const { return "'Pass' Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PassToPrint);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char CallGraphSCCPassPrinter::ID = 0;
|
|
|
|
|
2006-08-18 06:34:30 +00:00
|
|
|
struct ModulePassPrinter : public ModulePass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2006-08-18 06:34:30 +00:00
|
|
|
const PassInfo *PassToPrint;
|
2009-02-18 05:09:16 +00:00
|
|
|
ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
|
2007-05-01 21:15:47 +00:00
|
|
|
PassToPrint(PI) {}
|
2006-08-18 06:34:30 +00:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M) {
|
|
|
|
if (!Quiet) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
|
2006-08-18 06:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const { return "'Pass' Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PassToPrint);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char ModulePassPrinter::ID = 0;
|
2006-08-18 06:34:30 +00:00
|
|
|
struct FunctionPassPrinter : public FunctionPass {
|
|
|
|
const PassInfo *PassToPrint;
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2009-02-18 05:09:16 +00:00
|
|
|
FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
|
2007-05-01 21:15:47 +00:00
|
|
|
PassToPrint(PI) {}
|
2006-08-18 06:34:30 +00:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-08-21 23:29:40 +00:00
|
|
|
if (!Quiet) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Printing analysis '" << PassToPrint->getPassName()
|
|
|
|
<< "' for function '" << F.getName() << "':\n";
|
2006-08-18 06:34:30 +00:00
|
|
|
}
|
|
|
|
// Get and print pass...
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
|
2006-08-18 06:34:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const { return "FunctionPass Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PassToPrint);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char FunctionPassPrinter::ID = 0;
|
2007-07-05 15:32:03 +00:00
|
|
|
|
|
|
|
struct LoopPassPrinter : public LoopPass {
|
|
|
|
static char ID;
|
|
|
|
const PassInfo *PassToPrint;
|
2009-08-21 23:29:40 +00:00
|
|
|
LoopPassPrinter(const PassInfo *PI) :
|
2009-02-18 05:09:16 +00:00
|
|
|
LoopPass(&ID), PassToPrint(PI) {}
|
2007-07-05 15:32:03 +00:00
|
|
|
|
|
|
|
virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
|
|
|
|
if (!Quiet) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(outs(),
|
2007-07-05 15:32:03 +00:00
|
|
|
L->getHeader()->getParent()->getParent());
|
|
|
|
}
|
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-21 23:29:40 +00:00
|
|
|
|
2007-07-05 15:32:03 +00:00
|
|
|
virtual const char *getPassName() const { return "'Pass' Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PassToPrint);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char LoopPassPrinter::ID = 0;
|
|
|
|
|
2006-08-18 06:34:30 +00:00
|
|
|
struct BasicBlockPassPrinter : public BasicBlockPass {
|
|
|
|
const PassInfo *PassToPrint;
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2009-08-21 23:29:40 +00:00
|
|
|
BasicBlockPassPrinter(const PassInfo *PI)
|
2009-02-18 05:09:16 +00:00
|
|
|
: BasicBlockPass(&ID), PassToPrint(PI) {}
|
2006-08-18 06:34:30 +00:00
|
|
|
|
|
|
|
virtual bool runOnBasicBlock(BasicBlock &BB) {
|
|
|
|
if (!Quiet) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
|
|
|
|
<< "': Pass " << PassToPrint->getPassName() << ":\n";
|
2006-08-18 06:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get and print pass...
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
|
2006-08-18 06:34:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PassToPrint);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char BasicBlockPassPrinter::ID = 0;
|
2007-02-02 14:46:29 +00:00
|
|
|
inline void addPass(PassManager &PM, Pass *P) {
|
|
|
|
// Add the pass to the pass manager...
|
|
|
|
PM.add(P);
|
|
|
|
|
|
|
|
// If we are verifying all of the intermediate steps, add the verifier...
|
|
|
|
if (VerifyEach) PM.add(createVerifierPass());
|
|
|
|
}
|
|
|
|
|
2009-08-21 23:29:40 +00:00
|
|
|
/// AddOptimizationPasses - This routine adds optimization passes
|
|
|
|
/// based on selected optimization level, OptLevel. This routine
|
2008-09-16 22:25:14 +00:00
|
|
|
/// duplicates llvm-gcc behaviour.
|
|
|
|
///
|
|
|
|
/// OptLevel - Optimization Level
|
2008-11-26 02:57:24 +00:00
|
|
|
void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
|
|
|
|
unsigned OptLevel) {
|
2009-06-03 18:22:15 +00:00
|
|
|
createStandardFunctionPasses(&FPM, OptLevel);
|
|
|
|
|
2010-01-18 22:38:31 +00:00
|
|
|
llvm::Pass *InliningPass = 0;
|
|
|
|
if (DisableInline) {
|
|
|
|
// No inlining pass
|
|
|
|
} else if (OptLevel) {
|
|
|
|
unsigned Threshold = 200;
|
|
|
|
if (OptLevel > 2)
|
|
|
|
Threshold = 250;
|
|
|
|
InliningPass = createFunctionInliningPass(Threshold);
|
|
|
|
} else {
|
|
|
|
InliningPass = createAlwaysInlinerPass();
|
|
|
|
}
|
2009-06-03 18:22:15 +00:00
|
|
|
createStandardModulePasses(&MPM, OptLevel,
|
|
|
|
/*OptimizeSize=*/ false,
|
|
|
|
UnitAtATime,
|
|
|
|
/*UnrollLoops=*/ OptLevel > 1,
|
|
|
|
!DisableSimplifyLibCalls,
|
|
|
|
/*HaveExceptions=*/ true,
|
|
|
|
InliningPass);
|
2008-09-16 22:25:14 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 14:46:29 +00:00
|
|
|
void AddStandardCompilePasses(PassManager &PM) {
|
|
|
|
PM.add(createVerifierPass()); // Verify that input is correct
|
|
|
|
|
|
|
|
addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
|
|
|
|
|
|
|
|
// If the -strip-debug command line option was specified, do it.
|
|
|
|
if (StripDebug)
|
|
|
|
addPass(PM, createStripSymbolsPass(true));
|
|
|
|
|
|
|
|
if (DisableOptimizations) return;
|
|
|
|
|
2009-06-03 18:22:15 +00:00
|
|
|
llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
|
|
|
|
|
|
|
|
// -std-compile-opts adds the same module passes as -O3.
|
2009-08-21 23:29:40 +00:00
|
|
|
createStandardModulePasses(&PM, 3,
|
2009-06-03 18:22:15 +00:00
|
|
|
/*OptimizeSize=*/ false,
|
|
|
|
/*UnitAtATime=*/ true,
|
|
|
|
/*UnrollLoops=*/ true,
|
|
|
|
/*SimplifyLibCalls=*/ true,
|
|
|
|
/*HaveExceptions=*/ true,
|
|
|
|
InliningPass);
|
2007-02-02 14:46:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 18:09:39 +00:00
|
|
|
void AddStandardLinkPasses(PassManager &PM) {
|
|
|
|
PM.add(createVerifierPass()); // Verify that input is correct
|
|
|
|
|
|
|
|
// If the -strip-debug command line option was specified, do it.
|
|
|
|
if (StripDebug)
|
|
|
|
addPass(PM, createStripSymbolsPass(true));
|
|
|
|
|
|
|
|
if (DisableOptimizations) return;
|
|
|
|
|
|
|
|
createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
|
|
|
|
/*RunInliner=*/ !DisableInline,
|
|
|
|
/*VerifyEach=*/ VerifyEach);
|
|
|
|
}
|
|
|
|
|
2006-08-18 06:34:30 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main for opt
|
|
|
|
//
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
2009-12-09 00:41:28 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
llvm::PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
2010-01-05 01:30:32 +00:00
|
|
|
// Enable debug stream buffering.
|
|
|
|
EnableDebugBuffering = true;
|
|
|
|
|
2009-12-09 00:41:28 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-10-22 00:46:41 +00:00
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
"llvm .bc -> .bc modular optimizer and analysis printer\n");
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// Allocate a full target machine description only if necessary.
|
|
|
|
// FIXME: The choice of target should be controllable on the command line.
|
|
|
|
std::auto_ptr<TargetMachine> target;
|
2008-08-27 20:51:49 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
SMDiagnostic Err;
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// Load the input module...
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
M.reset(ParseIRFile(InputFilename, Err, Context));
|
2009-08-21 23:29:40 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (M.get() == 0) {
|
|
|
|
Err.Print(argv[0], errs());
|
|
|
|
return 1;
|
|
|
|
}
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// Figure out what stream we are supposed to write to...
|
|
|
|
// FIXME: outs() is not binary!
|
|
|
|
raw_ostream *Out = &outs(); // Default to printing to stdout...
|
|
|
|
if (OutputFilename != "-") {
|
2010-01-17 17:47:24 +00:00
|
|
|
if (NoOutput || AnalyzeOnly) {
|
|
|
|
errs() << "WARNING: The -o (output filename) option is ignored when\n"
|
|
|
|
"the --disable-output or --analyze options are used.\n";
|
|
|
|
} else {
|
|
|
|
// Make sure that the Output file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
|
|
|
|
|
|
|
std::string ErrorInfo;
|
|
|
|
Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
delete Out;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2009-10-22 00:46:41 +00:00
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// If the output is set to be emitted to standard out, and standard out is a
|
|
|
|
// console, print out a warning message and refuse to do it. We don't
|
|
|
|
// impress anyone by spewing tons of binary goo to a terminal.
|
2010-01-17 17:47:24 +00:00
|
|
|
if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
|
2009-10-22 00:46:41 +00:00
|
|
|
if (CheckBitcodeOutputToConsole(*Out, !Quiet))
|
|
|
|
NoOutput = true;
|
|
|
|
|
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
TargetData *TD = 0;
|
|
|
|
const std::string &ModuleDataLayout = M.get()->getDataLayout();
|
|
|
|
if (!ModuleDataLayout.empty())
|
|
|
|
TD = new TargetData(ModuleDataLayout);
|
2009-11-03 15:29:06 +00:00
|
|
|
else if (!DefaultDataLayout.empty())
|
2009-10-22 00:46:41 +00:00
|
|
|
TD = new TargetData(DefaultDataLayout);
|
|
|
|
|
|
|
|
if (TD)
|
|
|
|
Passes.add(TD);
|
|
|
|
|
|
|
|
FunctionPassManager *FPasses = NULL;
|
|
|
|
if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
|
2010-01-27 20:34:15 +00:00
|
|
|
FPasses = new FunctionPassManager(M.get());
|
2009-10-22 00:44:10 +00:00
|
|
|
if (TD)
|
2009-10-22 00:46:41 +00:00
|
|
|
FPasses->add(new TargetData(*TD));
|
|
|
|
}
|
2009-08-21 23:29:40 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// If the -strip-debug command line option was specified, add it. If
|
|
|
|
// -std-compile-opts was also specified, it will handle StripDebug.
|
|
|
|
if (StripDebug && !StandardCompileOpts)
|
|
|
|
addPass(Passes, createStripSymbolsPass(true));
|
|
|
|
|
|
|
|
// Create a new optimization pass for each one specified on the command line
|
|
|
|
for (unsigned i = 0; i < PassList.size(); ++i) {
|
|
|
|
// Check to see if -std-compile-opts was specified before this option. If
|
|
|
|
// so, handle it.
|
|
|
|
if (StandardCompileOpts &&
|
|
|
|
StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
|
2008-07-13 19:35:21 +00:00
|
|
|
AddStandardCompilePasses(Passes);
|
|
|
|
StandardCompileOpts = false;
|
2009-08-21 23:29:40 +00:00
|
|
|
}
|
2002-01-31 00:47:12 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (StandardLinkOpts &&
|
|
|
|
StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
|
2009-07-17 18:09:39 +00:00
|
|
|
AddStandardLinkPasses(Passes);
|
|
|
|
StandardLinkOpts = false;
|
2009-08-21 23:29:40 +00:00
|
|
|
}
|
2009-07-17 18:09:39 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
|
2009-07-17 18:09:39 +00:00
|
|
|
AddOptimizationPasses(Passes, *FPasses, 1);
|
2009-10-22 00:46:41 +00:00
|
|
|
OptLevelO1 = false;
|
2009-07-17 18:09:39 +00:00
|
|
|
}
|
2008-09-16 22:25:14 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
|
2009-07-17 18:09:39 +00:00
|
|
|
AddOptimizationPasses(Passes, *FPasses, 2);
|
2009-10-22 00:46:41 +00:00
|
|
|
OptLevelO2 = false;
|
2009-07-17 18:09:39 +00:00
|
|
|
}
|
2008-09-16 22:25:14 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
|
2009-07-17 18:09:39 +00:00
|
|
|
AddOptimizationPasses(Passes, *FPasses, 3);
|
2009-10-22 00:46:41 +00:00
|
|
|
OptLevelO3 = false;
|
2009-07-17 18:09:39 +00:00
|
|
|
}
|
2008-09-16 22:25:14 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
const PassInfo *PassInf = PassList[i];
|
|
|
|
Pass *P = 0;
|
|
|
|
if (PassInf->getNormalCtor())
|
|
|
|
P = PassInf->getNormalCtor()();
|
|
|
|
else
|
|
|
|
errs() << argv[0] << ": cannot create pass: "
|
|
|
|
<< PassInf->getPassName() << "\n";
|
|
|
|
if (P) {
|
|
|
|
addPass(Passes, P);
|
|
|
|
|
|
|
|
if (AnalyzeOnly) {
|
2010-01-22 06:03:06 +00:00
|
|
|
switch (P->getPassKind()) {
|
|
|
|
case PT_BasicBlock:
|
2009-10-22 00:46:41 +00:00
|
|
|
Passes.add(new BasicBlockPassPrinter(PassInf));
|
2010-01-22 06:03:06 +00:00
|
|
|
break;
|
|
|
|
case PT_Loop:
|
2009-10-22 00:46:41 +00:00
|
|
|
Passes.add(new LoopPassPrinter(PassInf));
|
2010-01-22 06:03:06 +00:00
|
|
|
break;
|
|
|
|
case PT_Function:
|
2009-10-22 00:46:41 +00:00
|
|
|
Passes.add(new FunctionPassPrinter(PassInf));
|
2010-01-22 06:03:06 +00:00
|
|
|
break;
|
|
|
|
case PT_CallGraphSCC:
|
2009-10-22 00:46:41 +00:00
|
|
|
Passes.add(new CallGraphSCCPassPrinter(PassInf));
|
2010-01-22 06:03:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-10-22 00:46:41 +00:00
|
|
|
Passes.add(new ModulePassPrinter(PassInf));
|
2010-01-22 06:03:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-22 00:46:41 +00:00
|
|
|
}
|
2008-09-16 22:25:14 +00:00
|
|
|
}
|
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (PrintEachXForm)
|
|
|
|
Passes.add(createPrintModulePass(&errs()));
|
|
|
|
}
|
2002-02-20 17:56:53 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
// If -std-compile-opts was specified at the end of the pass list, add them.
|
|
|
|
if (StandardCompileOpts) {
|
|
|
|
AddStandardCompilePasses(Passes);
|
|
|
|
StandardCompileOpts = false;
|
|
|
|
}
|
2002-01-31 00:47:12 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (StandardLinkOpts) {
|
|
|
|
AddStandardLinkPasses(Passes);
|
|
|
|
StandardLinkOpts = false;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (OptLevelO1)
|
|
|
|
AddOptimizationPasses(Passes, *FPasses, 1);
|
|
|
|
|
|
|
|
if (OptLevelO2)
|
|
|
|
AddOptimizationPasses(Passes, *FPasses, 2);
|
2006-08-18 06:34:30 +00:00
|
|
|
|
2009-10-22 00:46:41 +00:00
|
|
|
if (OptLevelO3)
|
|
|
|
AddOptimizationPasses(Passes, *FPasses, 3);
|
|
|
|
|
|
|
|
if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
|
|
|
|
FPasses->doInitialization();
|
|
|
|
for (Module::iterator I = M.get()->begin(), E = M.get()->end();
|
|
|
|
I != E; ++I)
|
|
|
|
FPasses->run(*I);
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
2009-10-22 00:46:41 +00:00
|
|
|
|
|
|
|
// Check that the module is well formed on completion of optimization
|
|
|
|
if (!NoVerify && !VerifyEach)
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2010-01-17 17:47:24 +00:00
|
|
|
// Write bitcode or assembly out to disk or outs() as the last step...
|
2009-10-22 00:46:41 +00:00
|
|
|
if (!NoOutput && !AnalyzeOnly) {
|
|
|
|
if (OutputAssembly)
|
|
|
|
Passes.add(createPrintModulePass(Out));
|
|
|
|
else
|
|
|
|
Passes.add(createBitcodeWriterPass(*Out));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
|
|
|
Passes.run(*M.get());
|
|
|
|
|
|
|
|
// Delete the raw_fd_ostream.
|
|
|
|
if (Out != &outs())
|
|
|
|
delete Out;
|
|
|
|
return 0;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|