Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 1 | //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 14 | // These classes are separated out of analyze.cpp so that it is more clear which |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 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 | |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | 04eaef2 | 2004-04-02 20:56:33 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CallGraph.h" |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 24 | #include <iostream> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 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. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 32 | struct ExternalFunctionsPassedConstants : public ModulePass { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 33 | static const int ID; // Pass ID, replacement for typeid |
| 34 | ExternalFunctionsPassedConstants() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 35 | virtual bool runOnModule(Module &M) { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 36 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 37 | if (I->isDeclaration()) { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 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) |
Reid Spencer | 593eb95 | 2004-07-18 00:44:14 +0000 | [diff] [blame] | 46 | if (isa<Constant>(*AI)) { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 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 | |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 62 | AU.setPreservesAll(); |
| 63 | } |
| 64 | }; |
| 65 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 66 | const int ExternalFunctionsPassedConstants::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 67 | RegisterPass<ExternalFunctionsPassedConstants> |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 68 | P1("externalfnconstants", "Print external fn callsites passed constants"); |
| 69 | |
| 70 | struct CallGraphPrinter : public ModulePass { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 71 | static const int ID; // Pass ID, replacement for typeid |
| 72 | CallGraphPrinter() : ModulePass((intptr_t)&ID) {} |
| 73 | |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 74 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 75 | AU.setPreservesAll(); |
Chris Lattner | 8257bee | 2006-12-05 19:43:42 +0000 | [diff] [blame] | 76 | AU.addRequiredTransitive<CallGraph>(); |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 77 | } |
| 78 | virtual bool runOnModule(Module &M) { return false; } |
| 79 | |
Andrew Lenharth | 0cafa92 | 2006-04-18 23:45:19 +0000 | [diff] [blame] | 80 | virtual void print(std::ostream &OS, const Module *M) const { |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 81 | getAnalysis<CallGraph>().print(OS, M); |
| 82 | } |
| 83 | }; |
| 84 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 85 | const int CallGraphPrinter::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 86 | RegisterPass<CallGraphPrinter> |
Chris Lattner | 171eee5 | 2005-12-22 19:26:06 +0000 | [diff] [blame] | 87 | P2("callgraph", "Print a call graph"); |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 88 | } |