2002-07-29 21:24:10 +00:00
|
|
|
//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-29 21:24:10 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2003-07-14 17:20:40 +00:00
|
|
|
// These classes are separated out of analyze.cpp so that it is more clear which
|
2002-07-29 21:24:10 +00:00
|
|
|
// code is the integral part of the analyze tool, and which part of the code is
|
|
|
|
// just making it so more passes are available.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-05-27 06:13:36 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-04-02 20:56:33 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-05-27 06:13:36 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2005-12-22 19:26:06 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2009-07-15 16:35:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-07-04 12:20:55 +00:00
|
|
|
#include <iostream>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-05-27 06:13:36 +00:00
|
|
|
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.
|
2004-09-20 04:48:05 +00:00
|
|
|
struct ExternalFunctionsPassedConstants : public ModulePass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2009-02-18 05:09:16 +00:00
|
|
|
ExternalFunctionsPassedConstants() : ModulePass(&ID) {}
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M) {
|
2009-08-23 06:03:38 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
|
|
|
if (!I->isDeclaration()) continue;
|
|
|
|
|
|
|
|
bool PrintedFn = false;
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
Instruction *User = dyn_cast<Instruction>(*UI);
|
|
|
|
if (!User) continue;
|
|
|
|
|
|
|
|
CallSite CS = CallSite::get(User);
|
|
|
|
if (!CS.getInstruction()) continue;
|
|
|
|
|
|
|
|
for (CallSite::arg_iterator AI = CS.arg_begin(),
|
|
|
|
E = CS.arg_end(); AI != E; ++AI) {
|
|
|
|
if (!isa<Constant>(*AI)) continue;
|
|
|
|
|
|
|
|
if (!PrintedFn) {
|
|
|
|
errs() << "Function '" << I->getName() << "':\n";
|
|
|
|
PrintedFn = true;
|
2004-05-27 06:13:36 +00:00
|
|
|
}
|
2009-08-23 06:03:38 +00:00
|
|
|
errs() << *User;
|
|
|
|
break;
|
|
|
|
}
|
2004-05-27 06:13:36 +00:00
|
|
|
}
|
2009-08-23 06:03:38 +00:00
|
|
|
}
|
2004-05-27 06:13:36 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char ExternalFunctionsPassedConstants::ID = 0;
|
2006-08-27 22:30:17 +00:00
|
|
|
RegisterPass<ExternalFunctionsPassedConstants>
|
2008-09-23 12:47:39 +00:00
|
|
|
P1("print-externalfnconstants",
|
|
|
|
"Print external fn callsites passed constants");
|
2008-09-19 07:57:09 +00:00
|
|
|
|
2005-12-22 19:26:06 +00:00
|
|
|
struct CallGraphPrinter : public ModulePass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2009-02-18 05:09:16 +00:00
|
|
|
CallGraphPrinter() : ModulePass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2005-12-22 19:26:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2006-12-05 19:43:42 +00:00
|
|
|
AU.addRequiredTransitive<CallGraph>();
|
2005-12-22 19:26:06 +00:00
|
|
|
}
|
2008-09-19 07:57:09 +00:00
|
|
|
virtual bool runOnModule(Module &M) {
|
2009-08-23 06:03:38 +00:00
|
|
|
getAnalysis<CallGraph>().print(errs(), &M);
|
2008-09-19 07:57:09 +00:00
|
|
|
return false;
|
2005-12-22 19:26:06 +00:00
|
|
|
}
|
|
|
|
};
|
2008-09-19 07:57:09 +00:00
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char CallGraphPrinter::ID = 0;
|
2006-08-27 22:30:17 +00:00
|
|
|
RegisterPass<CallGraphPrinter>
|
2008-09-23 12:47:39 +00:00
|
|
|
P2("print-callgraph", "Print a call graph");
|
2004-05-27 06:13:36 +00:00
|
|
|
}
|