diff options
Diffstat (limited to 'llvm/tools/opt')
-rw-r--r-- | llvm/tools/opt/AnalysisWrappers.cpp | 81 | ||||
-rw-r--r-- | llvm/tools/opt/GraphPrinters.cpp | 77 | ||||
-rw-r--r-- | llvm/tools/opt/Makefile | 8 | ||||
-rw-r--r-- | llvm/tools/opt/PrintSCC.cpp | 105 | ||||
-rw-r--r-- | llvm/tools/opt/opt.cpp | 150 |
5 files changed, 414 insertions, 7 deletions
diff --git a/llvm/tools/opt/AnalysisWrappers.cpp b/llvm/tools/opt/AnalysisWrappers.cpp new file mode 100644 index 00000000000..b371d50ef33 --- /dev/null +++ b/llvm/tools/opt/AnalysisWrappers.cpp @@ -0,0 +1,81 @@ +//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines pass wrappers around LLVM analyses that don't make sense to +// be passes. It provides a nice standard pass interface to these classes so +// that they can be printed out by analyze. +// +// These classes are separated out of analyze.cpp so that it is more clear which +// code is the integral part of the analyze tool, and which part of the code is +// just making it so more passes are available. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/CallSite.h" +#include "llvm/Analysis/CallGraph.h" +#include <iostream> +using namespace llvm; + +namespace { + /// ExternalFunctionsPassedConstants - This pass prints out call sites to + /// external functions that are called with constant arguments. This can be + /// useful when looking for standard library functions we should constant fold + /// or handle in alias analyses. + struct ExternalFunctionsPassedConstants : public ModulePass { + virtual bool runOnModule(Module &M) { + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) + if (I->isExternal()) { + bool PrintedFn = false; + for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); + UI != E; ++UI) + if (Instruction *User = dyn_cast<Instruction>(*UI)) { + CallSite CS = CallSite::get(User); + if (CS.getInstruction()) { + for (CallSite::arg_iterator AI = CS.arg_begin(), + E = CS.arg_end(); AI != E; ++AI) + if (isa<Constant>(*AI)) { + if (!PrintedFn) { + std::cerr << "Function '" << I->getName() << "':\n"; + PrintedFn = true; + } + std::cerr << *User; + break; + } + } + } + } + + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + }; + + RegisterAnalysis<ExternalFunctionsPassedConstants> + P1("externalfnconstants", "Print external fn callsites passed constants"); + + struct CallGraphPrinter : public ModulePass { + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired<CallGraph>(); + } + virtual bool runOnModule(Module &M) { return false; } + + virtual void print(std::ostream &OS, const Module *M) const { + getAnalysis<CallGraph>().print(OS, M); + } + }; + + RegisterAnalysis<CallGraphPrinter> + P2("callgraph", "Print a call graph"); +} diff --git a/llvm/tools/opt/GraphPrinters.cpp b/llvm/tools/opt/GraphPrinters.cpp new file mode 100644 index 00000000000..8826cd2a777 --- /dev/null +++ b/llvm/tools/opt/GraphPrinters.cpp @@ -0,0 +1,77 @@ +//===- GraphPrinters.cpp - DOT printers for various graph types -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines several printers for various different types of graphs used +// by the LLVM infrastructure. It uses the generic graph interface to convert +// the graph into a .dot graph. These graphs can then be processed with the +// "dot" tool to convert them to postscript or some other suitable format. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/GraphWriter.h" +#include "llvm/Pass.h" +#include "llvm/Value.h" +#include "llvm/Analysis/CallGraph.h" +#include <fstream> +using namespace llvm; + +template<typename GraphType> +static void WriteGraphToFile(std::ostream &O, const std::string &GraphName, + const GraphType >) { + std::string Filename = GraphName + ".dot"; + O << "Writing '" << Filename << "'..."; + std::ofstream F(Filename.c_str()); + + if (F.good()) + WriteGraph(F, GT); + else + O << " error opening file for writing!"; + O << "\n"; +} + + +//===----------------------------------------------------------------------===// +// Call Graph Printer +//===----------------------------------------------------------------------===// + +namespace llvm { + template<> + struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits { + static std::string getGraphName(CallGraph *F) { + return "Call Graph"; + } + + static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) { + if (Node->getFunction()) + return ((Value*)Node->getFunction())->getName(); + else + return "Indirect call node"; + } + }; +} + + +namespace { + struct CallGraphPrinter : public ModulePass { + virtual bool runOnModule(Module &M) { + WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>()); + return false; + } + + void print(std::ostream &OS) const {} + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired<CallGraph>(); + AU.setPreservesAll(); + } + }; + + RegisterAnalysis<CallGraphPrinter> P2("print-callgraph", + "Print Call Graph to 'dot' file"); +} diff --git a/llvm/tools/opt/Makefile b/llvm/tools/opt/Makefile index f310639c08b..9e4cddfa976 100644 --- a/llvm/tools/opt/Makefile +++ b/llvm/tools/opt/Makefile @@ -10,9 +10,9 @@ LEVEL = ../.. 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 diff --git a/llvm/tools/opt/PrintSCC.cpp b/llvm/tools/opt/PrintSCC.cpp new file mode 100644 index 00000000000..c0adf5ca034 --- /dev/null +++ b/llvm/tools/opt/PrintSCC.cpp @@ -0,0 +1,105 @@ +//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file provides passes to print out SCCs in a CFG or a CallGraph. +// Normally, you would not use these passes; instead, you would use the +// scc_iterator directly to enumerate SCCs and process them in some way. These +// passes serve three purposes: +// +// (1) As a reference for how to use the scc_iterator. +// (2) To print out the SCCs for a CFG or a CallGraph: +// analyze -cfgscc to print the SCCs in each CFG of a module. +// analyze -cfgscc -stats to print the #SCCs and the maximum SCC size. +// analyze -cfgscc -debug > /dev/null to watch the algorithm in action. +// +// and similarly: +// analyze -callscc [-stats] [-debug] to print SCCs in the CallGraph +// +// (3) To test the scc_iterator. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Pass.h" +#include "llvm/Module.h" +#include "llvm/Analysis/CallGraph.h" +#include "llvm/Support/CFG.h" +#include "llvm/ADT/SCCIterator.h" +#include <iostream> +using namespace llvm; + +namespace { + struct CFGSCC : public FunctionPass { + bool runOnFunction(Function& func); + + void print(std::ostream &O, const Module* = 0) const { } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + }; + + struct CallGraphSCC : public ModulePass { + // run - Print out SCCs in the call graph for the specified module. + bool runOnModule(Module &M); + + void print(std::ostream &O, const Module* = 0) const { } + + // getAnalysisUsage - This pass requires the CallGraph. + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired<CallGraph>(); + } + }; + + RegisterAnalysis<CFGSCC> + Y("cfgscc", "Print SCCs of each function CFG"); + + RegisterAnalysis<CallGraphSCC> + Z("callscc", "Print SCCs of the Call Graph"); +} + +bool CFGSCC::runOnFunction(Function &F) { + unsigned sccNum = 0; + std::cout << "SCCs for Function " << F.getName() << " in PostOrder:"; + for (scc_iterator<Function*> SCCI = scc_begin(&F), + E = scc_end(&F); SCCI != E; ++SCCI) { + std::vector<BasicBlock*> &nextSCC = *SCCI; + std::cout << "\nSCC #" << ++sccNum << " : "; + for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), + E = nextSCC.end(); I != E; ++I) + std::cout << (*I)->getName() << ", "; + if (nextSCC.size() == 1 && SCCI.hasLoop()) + std::cout << " (Has self-loop)."; + } + std::cout << "\n"; + + return true; +} + + +// run - Print out SCCs in the call graph for the specified module. +bool CallGraphSCC::runOnModule(Module &M) { + CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot(); + unsigned sccNum = 0; + std::cout << "SCCs for the program in PostOrder:"; + for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode), + E = scc_end(rootNode); SCCI != E; ++SCCI) { + const std::vector<CallGraphNode*> &nextSCC = *SCCI; + std::cout << "\nSCC #" << ++sccNum << " : "; + for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), + E = nextSCC.end(); I != E; ++I) + std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName() + : std::string("Indirect CallGraph node")) << ", "; + if (nextSCC.size() == 1 && SCCI.hasLoop()) + std::cout << " (Has self-loop)."; + } + std::cout << "\n"; + + return true; +} diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp index 096506ff392..b67892bb34a 100644 --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -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 @@ OptimizationList(cl::desc("Optimizations available:")); // 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 @@ Quiet("q", cl::desc("Obsolete option"), cl::Hidden); 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 @@ QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); 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 @@ int main(int argc, char **argv) { Passes.run(*M.get()); return 0; + } catch (const std::string& msg) { std::cerr << argv[0] << ": " << msg << "\n"; } catch (...) { |