[llvm-commits] CVS: llvm/tools/opt/Makefile opt.cpp

Reid Spencer reid at x10sys.com
Thu Aug 17 23:34:53 PDT 2006



Changes in directory llvm/tools/opt:

Makefile updated: 1.56 -> 1.57
opt.cpp updated: 1.110 -> 1.111
---
Log message:

For PR872: http://llvm.org/PR872 :
Shrinkify LLVM's footprint by removing the analyze tool and moving its
functionality into the opt tool. THis eliminates one of the largest tools
from LLVM and doesn't make opt much bigger because it already included 
most of the analysis passes.  To get the old analyze functionality pass 
the -analyze option to opt. Note that the integeration here is dead
simple. The "main" of analyze was just copied to opt and invoked if the
-analyze option was given. There may be opportunities for further 
integration such as removing the distinction between transform passes
and analysis passes.

To use the analysis functionality, if you previously did this:
  analyze $FNAME -domset -disable-verify
you would now do this:
  opt -analyze $FNAME -domset -disable-verify
Pretty simple.


---
Diffs of the changes:  (+151 -7)

 Makefile |    8 +--
 opt.cpp  |  150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 151 insertions(+), 7 deletions(-)


Index: llvm/tools/opt/Makefile
diff -u llvm/tools/opt/Makefile:1.56 llvm/tools/opt/Makefile:1.57
--- llvm/tools/opt/Makefile:1.56	Thu Jul  6 19:46:19 2006
+++ llvm/tools/opt/Makefile	Fri Aug 18 01:34:30 2006
@@ -10,9 +10,9 @@
 TOOLNAME = opt
 REQUIRES_EH := 1
 
-USEDLIBS = LLVMBCReader.a LLVMBCWriter.a LLVMInstrumentation.a \
-	   LLVMScalarOpts.a LLVMipo.a LLVMipa.a LLVMDataStructure LLVMTransforms.a \
-	   LLVMTarget.a LLVMTransformUtils.a LLVMAnalysis.a LLVMCore.a LLVMSupport.a \
-	   LLVMbzip2.a LLVMSystem.a 
+USEDLIBS = LLVMAsmParser.a LLVMBCReader.a LLVMBCWriter.a LLVMInstrumentation.a \
+	   LLVMScalarOpts.a LLVMipo.a LLVMipa.a LLVMDataStructure \
+	   LLVMTransforms.a LLVMTarget.a LLVMTransformUtils.a LLVMAnalysis.a \
+	   LLVMCore.a LLVMSupport.a LLVMbzip2.a LLVMSystem.a 
 
 include $(LEVEL)/Makefile.common


Index: llvm/tools/opt/opt.cpp
diff -u llvm/tools/opt/opt.cpp:1.110 llvm/tools/opt/opt.cpp:1.111
--- llvm/tools/opt/opt.cpp:1.110	Fri Jun 16 13:23:49 2006
+++ llvm/tools/opt/opt.cpp	Fri Aug 18 01:34:30 2006
@@ -8,11 +8,12 @@
 //===----------------------------------------------------------------------===//
 //
 // Optimizations may be specified an arbitrary number of times on the command
-// line, they are run in the order specified.
+// line, They are run in the order specified.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Module.h"
+#include "llvm/Assembly/Parser.h"
 #include "llvm/PassManager.h"
 #include "llvm/Bytecode/Reader.h"
 #include "llvm/Bytecode/WriteBytecodePass.h"
@@ -24,6 +25,8 @@
 #include "llvm/System/Signals.h"
 #include "llvm/Support/PluginLoader.h"
 #include "llvm/Support/SystemUtils.h"
+#include "llvm/Support/Timer.h"
+#include "llvm/Analysis/LinkAllAnalyses.h"
 #include "llvm/Transforms/LinkAllPasses.h"
 #include "llvm/LinkAllVMCore.h"
 #include <fstream>
@@ -43,7 +46,8 @@
 // Other command line options...
 //
 static cl::opt<std::string>
-InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
+InputFilename(cl::Positional, cl::desc("<input bytecode file>"), 
+    cl::init("-"), cl::value_desc("filename"));
 
 static cl::opt<std::string>
 OutputFilename("o", cl::desc("Override output filename"),
@@ -68,6 +72,91 @@
 static cl::alias
 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
 
+static cl::opt<bool>
+AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
+
+// The AnalysesList is automatically populated with registered Passes by the
+// PassNameParser.
+static 
+  cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::Analysis> >
+  AnalysesList(cl::desc("Analyses available:"));
+
+static Timer BytecodeLoadTimer("Bytecode Loader");
+
+// ---------- Define Printers for module and function passes ------------
+namespace {
+
+struct ModulePassPrinter : public ModulePass {
+  const PassInfo *PassToPrint;
+  ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
+
+  virtual bool runOnModule(Module &M) {
+    if (!Quiet) {
+      std::cout << "Printing analysis '" << PassToPrint->getPassName() 
+                << "':\n";
+      getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
+    }
+
+    // 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();
+  }
+};
+
+struct FunctionPassPrinter : public FunctionPass {
+  const PassInfo *PassToPrint;
+  FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
+
+  virtual bool runOnFunction(Function &F) {
+    if (!Quiet) {
+      std::cout << "Printing analysis '" << PassToPrint->getPassName()
+		<< "' for function '" << F.getName() << "':\n";
+    }
+    // Get and print pass...
+    getAnalysisID<Pass>(PassToPrint).print(std::cout, F.getParent());
+    return false;
+  }
+
+  virtual const char *getPassName() const { return "FunctionPass Printer"; }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.addRequiredID(PassToPrint);
+    AU.setPreservesAll();
+  }
+};
+
+struct BasicBlockPassPrinter : public BasicBlockPass {
+  const PassInfo *PassToPrint;
+  BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
+
+  virtual bool runOnBasicBlock(BasicBlock &BB) {
+    if (!Quiet) {
+      std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
+		<< "': Pass " << PassToPrint->getPassName() << ":\n";
+    }
+
+    // Get and print pass...
+    getAnalysisID<Pass>(PassToPrint).print(
+      std::cout, BB.getParent()->getParent());
+    return false;
+  }
+
+  virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.addRequiredID(PassToPrint);
+    AU.setPreservesAll();
+  }
+};
+
+} // anonymous namespace
+
 
 //===----------------------------------------------------------------------===//
 // main for opt
@@ -75,9 +164,63 @@
 int main(int argc, char **argv) {
   try {
     cl::ParseCommandLineOptions(argc, argv,
-                                " llvm .bc -> .bc modular optimizer\n");
+      " llvm .bc -> .bc modular optimizer and analysis printer \n");
     sys::PrintStackTraceOnErrorSignal();
 
+    if (AnalyzeOnly) {
+      Module *CurMod = 0;
+      try {
+#if 0
+        TimeRegion RegionTimer(BytecodeLoadTimer);
+#endif
+        CurMod = ParseBytecodeFile(InputFilename);
+        if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
+          std::cerr << argv[0] << ": input file didn't read correctly.\n";
+          return 1;
+        }
+      } catch (const ParseException &E) {
+        std::cerr << argv[0] << ": " << E.getMessage() << "\n";
+        return 1;
+      }
+
+      // 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...
+      Passes.add(new TargetData(CurMod));
+
+      // Make sure the input LLVM is well formed.
+      if (!NoVerify)
+        Passes.add(createVerifierPass());
+
+      // Create a new optimization pass for each one specified on the 
+      // command line
+      for (unsigned i = 0; i < AnalysesList.size(); ++i) {
+        const PassInfo *Analysis = AnalysesList[i];
+
+        if (Analysis->getNormalCtor()) {
+          Pass *P = Analysis->getNormalCtor()();
+          Passes.add(P);
+
+          if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P))
+            Passes.add(new BasicBlockPassPrinter(Analysis));
+          else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P))
+            Passes.add(new FunctionPassPrinter(Analysis));
+          else
+            Passes.add(new ModulePassPrinter(Analysis));
+
+        } else
+          std::cerr << argv[0] << ": cannot create pass: "
+                    << Analysis->getPassName() << "\n";
+      }
+
+      Passes.run(*CurMod);
+
+      delete CurMod;
+      return 0;
+    }
+
     // 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;
@@ -169,6 +312,7 @@
     Passes.run(*M.get());
 
     return 0;
+
   } catch (const std::string& msg) {
     std::cerr << argv[0] << ": " << msg << "\n";
   } catch (...) {






More information about the llvm-commits mailing list