Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 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" |
| 24 | #include <iostream> |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | /// ExternalFunctionsPassedConstants - This pass prints out call sites to |
| 29 | /// external functions that are called with constant arguments. This can be |
| 30 | /// useful when looking for standard library functions we should constant fold |
| 31 | /// or handle in alias analyses. |
| 32 | struct ExternalFunctionsPassedConstants : public ModulePass { |
| 33 | static char ID; // Pass ID, replacement for typeid |
| 34 | ExternalFunctionsPassedConstants() : ModulePass((intptr_t)&ID) {} |
| 35 | virtual bool runOnModule(Module &M) { |
| 36 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 37 | if (I->isDeclaration()) { |
| 38 | bool PrintedFn = false; |
| 39 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 40 | UI != E; ++UI) |
| 41 | if (Instruction *User = dyn_cast<Instruction>(*UI)) { |
| 42 | CallSite CS = CallSite::get(User); |
| 43 | if (CS.getInstruction()) { |
| 44 | for (CallSite::arg_iterator AI = CS.arg_begin(), |
| 45 | E = CS.arg_end(); AI != E; ++AI) |
| 46 | if (isa<Constant>(*AI)) { |
| 47 | if (!PrintedFn) { |
| 48 | std::cerr << "Function '" << I->getName() << "':\n"; |
| 49 | PrintedFn = true; |
| 50 | } |
| 51 | std::cerr << *User; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 62 | AU.setPreservesAll(); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | char ExternalFunctionsPassedConstants::ID = 0; |
| 67 | RegisterPass<ExternalFunctionsPassedConstants> |
| 68 | P1("externalfnconstants", "Print external fn callsites passed constants"); |
| 69 | |
| 70 | struct CallGraphPrinter : public ModulePass { |
| 71 | static char ID; // Pass ID, replacement for typeid |
| 72 | CallGraphPrinter() : ModulePass((intptr_t)&ID) {} |
| 73 | |
| 74 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 75 | AU.setPreservesAll(); |
| 76 | AU.addRequiredTransitive<CallGraph>(); |
| 77 | } |
| 78 | virtual bool runOnModule(Module &M) { return false; } |
| 79 | |
| 80 | virtual void print(std::ostream &OS, const Module *M) const { |
| 81 | getAnalysis<CallGraph>().print(OS, M); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | char CallGraphPrinter::ID = 0; |
| 86 | RegisterPass<CallGraphPrinter> |
| 87 | P2("callgraph", "Print a call graph"); |
| 88 | } |