blob: 3223b820303d8704527b0f332c2ba507005871ea [file] [log] [blame]
Chris Lattner59aa81c2002-07-29 21:24:10 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// 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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner59aa81c2002-07-29 21:24:10 +00009//
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 Brukman7fdaab42003-07-14 17:20:40 +000014// These classes are separated out of analyze.cpp so that it is more clear which
Chris Lattner59aa81c2002-07-29 21:24:10 +000015// 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 Lattner8082c742004-05-27 06:13:36 +000020#include "llvm/Module.h"
Chris Lattner9dcd6392004-04-02 20:56:33 +000021#include "llvm/Pass.h"
Chris Lattner8082c742004-05-27 06:13:36 +000022#include "llvm/Support/CallSite.h"
Chris Lattner7d052692005-12-22 19:26:06 +000023#include "llvm/Analysis/CallGraph.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000024#include <iostream>
Brian Gaeke960707c2003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattner59aa81c2002-07-29 21:24:10 +000027namespace {
Chris Lattner8082c742004-05-27 06:13:36 +000028 /// 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 Lattner4f2cf032004-09-20 04:48:05 +000032 struct ExternalFunctionsPassedConstants : public ModulePass {
33 virtual bool runOnModule(Module &M) {
Chris Lattner8082c742004-05-27 06:13:36 +000034 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000035 if (I->isDeclaration()) {
Chris Lattner8082c742004-05-27 06:13:36 +000036 bool PrintedFn = false;
37 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
38 UI != E; ++UI)
39 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
40 CallSite CS = CallSite::get(User);
41 if (CS.getInstruction()) {
42 for (CallSite::arg_iterator AI = CS.arg_begin(),
43 E = CS.arg_end(); AI != E; ++AI)
Reid Spencere5914d852004-07-18 00:44:14 +000044 if (isa<Constant>(*AI)) {
Chris Lattner8082c742004-05-27 06:13:36 +000045 if (!PrintedFn) {
46 std::cerr << "Function '" << I->getName() << "':\n";
47 PrintedFn = true;
48 }
49 std::cerr << *User;
50 break;
51 }
52 }
53 }
54 }
55
56 return false;
57 }
58
Chris Lattner8082c742004-05-27 06:13:36 +000059 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62 };
63
Chris Lattner3c9b2422006-08-27 22:30:17 +000064 RegisterPass<ExternalFunctionsPassedConstants>
Chris Lattner7d052692005-12-22 19:26:06 +000065 P1("externalfnconstants", "Print external fn callsites passed constants");
66
67 struct CallGraphPrinter : public ModulePass {
68 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
Chris Lattnere549bac2006-12-05 19:43:42 +000070 AU.addRequiredTransitive<CallGraph>();
Chris Lattner7d052692005-12-22 19:26:06 +000071 }
72 virtual bool runOnModule(Module &M) { return false; }
73
Andrew Lenharth2bdd6fe2006-04-18 23:45:19 +000074 virtual void print(std::ostream &OS, const Module *M) const {
Chris Lattner7d052692005-12-22 19:26:06 +000075 getAnalysis<CallGraph>().print(OS, M);
76 }
77 };
78
Chris Lattner3c9b2422006-08-27 22:30:17 +000079 RegisterPass<CallGraphPrinter>
Chris Lattner7d052692005-12-22 19:26:06 +000080 P2("callgraph", "Print a call graph");
Chris Lattner8082c742004-05-27 06:13:36 +000081}