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 | // |
Chris Lattner | 21c62da | 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. |
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 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 4bbfbdf | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Chris Lattner | 04eaef2 | 2004-04-02 20:56:33 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
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 | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 33 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 34 | ExternalFunctionsPassedConstants() : ModulePass(ID) {} |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 35 | bool runOnModule(Module &M) override { |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 36 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 37 | if (!I->isDeclaration()) continue; |
NAKAMURA Takumi | 04a6261 | 2014-01-20 15:47:15 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 39 | bool PrintedFn = false; |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 40 | for (User *U : I->users()) { |
| 41 | Instruction *UI = dyn_cast<Instruction>(U); |
| 42 | if (!UI) continue; |
NAKAMURA Takumi | 04a6261 | 2014-01-20 15:47:15 +0000 | [diff] [blame] | 43 | |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 44 | CallSite CS(cast<Value>(UI)); |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 45 | if (!CS) continue; |
NAKAMURA Takumi | 04a6261 | 2014-01-20 15:47:15 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 47 | for (CallSite::arg_iterator AI = CS.arg_begin(), |
| 48 | E = CS.arg_end(); AI != E; ++AI) { |
| 49 | if (!isa<Constant>(*AI)) continue; |
| 50 | |
| 51 | if (!PrintedFn) { |
| 52 | errs() << "Function '" << I->getName() << "':\n"; |
| 53 | PrintedFn = true; |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 54 | } |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 55 | errs() << *UI; |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 56 | break; |
| 57 | } |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 59 | } |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 64 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 65 | AU.setPreservesAll(); |
| 66 | } |
| 67 | }; |
Dan Gohman | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | d957211 | 2004-05-27 06:13:36 +0000 | [diff] [blame] | 69 | |
Dan Gohman | a2a3bbc | 2010-08-20 00:56:16 +0000 | [diff] [blame] | 70 | char ExternalFunctionsPassedConstants::ID = 0; |
| 71 | static RegisterPass<ExternalFunctionsPassedConstants> |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 72 | P1("print-externalfnconstants", |
| 73 | "Print external fn callsites passed constants"); |