llvm-mirror/tools/opt/AnalysisWrappers.cpp
Reid Spencer 8245e5bde1 For 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.

llvm-svn: 29762
2006-08-18 06:34:30 +00:00

82 lines
2.9 KiB
C++

//===- 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");
}