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 | // |
Chris Lattner | 5f5a573 | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Dan Gohman | f8b81bf | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include <iostream> |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 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 Gohman | c74a197 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 35 | ExternalFunctionsPassedConstants() : ModulePass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | virtual bool runOnModule(Module &M) { |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 37 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 38 | if (!I->isDeclaration()) continue; |
| 39 | |
| 40 | bool PrintedFn = false; |
| 41 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 42 | UI != E; ++UI) { |
| 43 | Instruction *User = dyn_cast<Instruction>(*UI); |
| 44 | if (!User) continue; |
| 45 | |
| 46 | CallSite CS = CallSite::get(User); |
| 47 | if (!CS.getInstruction()) continue; |
| 48 | |
| 49 | for (CallSite::arg_iterator AI = CS.arg_begin(), |
| 50 | E = CS.arg_end(); AI != E; ++AI) { |
| 51 | if (!isa<Constant>(*AI)) continue; |
| 52 | |
| 53 | if (!PrintedFn) { |
| 54 | errs() << "Function '" << I->getName() << "':\n"; |
| 55 | PrintedFn = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 57 | errs() << *User; |
| 58 | break; |
| 59 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 61 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 67 | AU.setPreservesAll(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | char ExternalFunctionsPassedConstants::ID = 0; |
| 72 | RegisterPass<ExternalFunctionsPassedConstants> |
Duncan Sands | d10d6f7 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 73 | P1("print-externalfnconstants", |
| 74 | "Print external fn callsites passed constants"); |
Duncan Sands | f7dbeb3 | 2008-09-19 07:57:09 +0000 | [diff] [blame] | 75 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | struct CallGraphPrinter : public ModulePass { |
| 77 | static char ID; // Pass ID, replacement for typeid |
Dan Gohman | c74a197 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 78 | CallGraphPrinter() : ModulePass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | |
| 80 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 81 | AU.setPreservesAll(); |
| 82 | AU.addRequiredTransitive<CallGraph>(); |
| 83 | } |
Duncan Sands | f7dbeb3 | 2008-09-19 07:57:09 +0000 | [diff] [blame] | 84 | virtual bool runOnModule(Module &M) { |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 85 | getAnalysis<CallGraph>().print(errs(), &M); |
Duncan Sands | f7dbeb3 | 2008-09-19 07:57:09 +0000 | [diff] [blame] | 86 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | } |
| 88 | }; |
Duncan Sands | f7dbeb3 | 2008-09-19 07:57:09 +0000 | [diff] [blame] | 89 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | char CallGraphPrinter::ID = 0; |
| 91 | RegisterPass<CallGraphPrinter> |
Duncan Sands | d10d6f7 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 92 | P2("print-callgraph", "Print a call graph"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | } |