blob: 2ab69ea61e744a08a28acda13142d0cd2877847d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines pass wrappers around LLVM analyses that don't make sense to
11// be passes. It provides a nice standard pass interface to these classes so
12// that they can be printed out by analyze.
13//
14// These classes are separated out of analyze.cpp so that it is more clear which
15// code is the integral part of the analyze tool, and which part of the code is
16// just making it so more passes are available.
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/CallSite.h"
23#include "llvm/Analysis/CallGraph.h"
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000024#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include <iostream>
26using namespace llvm;
27
28namespace {
29 /// ExternalFunctionsPassedConstants - This pass prints out call sites to
30 /// external functions that are called with constant arguments. This can be
31 /// useful when looking for standard library functions we should constant fold
32 /// or handle in alias analyses.
33 struct ExternalFunctionsPassedConstants : public ModulePass {
34 static char ID; // Pass ID, replacement for typeid
Dan Gohmanc74a1972009-02-18 05:09:16 +000035 ExternalFunctionsPassedConstants() : ModulePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 virtual bool runOnModule(Module &M) {
37 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
38 if (I->isDeclaration()) {
39 bool PrintedFn = false;
40 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
41 UI != E; ++UI)
42 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
43 CallSite CS = CallSite::get(User);
44 if (CS.getInstruction()) {
45 for (CallSite::arg_iterator AI = CS.arg_begin(),
46 E = CS.arg_end(); AI != E; ++AI)
47 if (isa<Constant>(*AI)) {
48 if (!PrintedFn) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000049 errs() << "Function '" << I->getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 PrintedFn = true;
51 }
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000052 errs() << *User;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 break;
54 }
55 }
56 }
57 }
58
59 return false;
60 }
61
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.setPreservesAll();
64 }
65 };
66
67 char ExternalFunctionsPassedConstants::ID = 0;
68 RegisterPass<ExternalFunctionsPassedConstants>
Duncan Sandsd10d6f72008-09-23 12:47:39 +000069 P1("print-externalfnconstants",
70 "Print external fn callsites passed constants");
Duncan Sandsf7dbeb32008-09-19 07:57:09 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 struct CallGraphPrinter : public ModulePass {
73 static char ID; // Pass ID, replacement for typeid
Dan Gohmanc74a1972009-02-18 05:09:16 +000074 CallGraphPrinter() : ModulePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
76 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.setPreservesAll();
78 AU.addRequiredTransitive<CallGraph>();
79 }
Duncan Sandsf7dbeb32008-09-19 07:57:09 +000080 virtual bool runOnModule(Module &M) {
81 getAnalysis<CallGraph>().print(std::cerr, &M);
82 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 }
84 };
Duncan Sandsf7dbeb32008-09-19 07:57:09 +000085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 char CallGraphPrinter::ID = 0;
87 RegisterPass<CallGraphPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +000088 P2("print-callgraph", "Print a call graph");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}