Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 1 | //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
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 | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstForest.h" |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame^] | 24 | #include <iostream> |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | struct InstForestHelper : public FunctionPass { |
| 30 | Function *F; |
| 31 | virtual bool runOnFunction(Function &Func) { F = &Func; return false; } |
| 32 | |
| 33 | void print(std::ostream &OS) const { |
| 34 | std::cout << InstForest<char>(F); |
| 35 | } |
| 36 | |
| 37 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 38 | AU.setPreservesAll(); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer"); |
Chris Lattner | 781e6f5 | 2002-07-29 21:24:10 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | /// ExternalFunctionsPassedConstants - This pass prints out call sites to |
| 47 | /// external functions that are called with constant arguments. This can be |
| 48 | /// useful when looking for standard library functions we should constant fold |
| 49 | /// or handle in alias analyses. |
| 50 | struct ExternalFunctionsPassedConstants : public Pass { |
| 51 | virtual bool run(Module &M) { |
| 52 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 53 | if (I->isExternal()) { |
| 54 | bool PrintedFn = false; |
| 55 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 56 | UI != E; ++UI) |
| 57 | if (Instruction *User = dyn_cast<Instruction>(*UI)) { |
| 58 | CallSite CS = CallSite::get(User); |
| 59 | if (CS.getInstruction()) { |
| 60 | for (CallSite::arg_iterator AI = CS.arg_begin(), |
| 61 | E = CS.arg_end(); AI != E; ++AI) |
Chris Lattner | b04cb7d | 2004-05-27 06:43:37 +0000 | [diff] [blame] | 62 | if (isa<Constant>(*AI) || isa<GlobalValue>(*AI)) { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 63 | if (!PrintedFn) { |
| 64 | std::cerr << "Function '" << I->getName() << "':\n"; |
| 65 | PrintedFn = true; |
| 66 | } |
| 67 | std::cerr << *User; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | void print(std::ostream &OS) const {} |
| 78 | |
| 79 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 80 | AU.setPreservesAll(); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | RegisterAnalysis<ExternalFunctionsPassedConstants> |
| 85 | P2("externalfnconstants", "Print external fn callsites passed constants"); |
| 86 | } |