blob: 2ab69ea61e744a08a28acda13142d0cd2877847d [file] [log] [blame]
Chris Lattner781e6f52002-07-29 21:24:10 +00001//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner781e6f52002-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 Brukmanbc0e9982003-07-14 17:20:40 +000014// These classes are separated out of analyze.cpp so that it is more clear which
Chris Lattner781e6f52002-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 Lattnerd9572112004-05-27 06:13:36 +000020#include "llvm/Module.h"
Chris Lattner04eaef22004-04-02 20:56:33 +000021#include "llvm/Pass.h"
Chris Lattnerd9572112004-05-27 06:13:36 +000022#include "llvm/Support/CallSite.h"
Chris Lattner171eee52005-12-22 19:26:06 +000023#include "llvm/Analysis/CallGraph.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000025#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000026using namespace llvm;
27
Chris Lattner781e6f52002-07-29 21:24:10 +000028namespace {
Chris Lattnerd9572112004-05-27 06:13:36 +000029 /// 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.
Chris Lattnerb12914b2004-09-20 04:48:05 +000033 struct ExternalFunctionsPassedConstants : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000034 static char ID; // Pass ID, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000035 ExternalFunctionsPassedConstants() : ModulePass(&ID) {}
Chris Lattnerb12914b2004-09-20 04:48:05 +000036 virtual bool runOnModule(Module &M) {
Chris Lattnerd9572112004-05-27 06:13:36 +000037 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000038 if (I->isDeclaration()) {
Chris Lattnerd9572112004-05-27 06:13:36 +000039 bool PrintedFn = false;
40 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
41 UI != E; ++UI)
42 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
43 CallSite CS = CallSite::get(User);
44 if (CS.getInstruction()) {
45 for (CallSite::arg_iterator AI = CS.arg_begin(),
46 E = CS.arg_end(); AI != E; ++AI)
Reid Spencer593eb952004-07-18 00:44:14 +000047 if (isa<Constant>(*AI)) {
Chris Lattnerd9572112004-05-27 06:13:36 +000048 if (!PrintedFn) {
Dan Gohman65f57c22009-07-15 16:35:29 +000049 errs() << "Function '" << I->getName() << "':\n";
Chris Lattnerd9572112004-05-27 06:13:36 +000050 PrintedFn = true;
51 }
Dan Gohman65f57c22009-07-15 16:35:29 +000052 errs() << *User;
Chris Lattnerd9572112004-05-27 06:13:36 +000053 break;
54 }
55 }
56 }
57 }
58
59 return false;
60 }
61
Chris Lattnerd9572112004-05-27 06:13:36 +000062 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.setPreservesAll();
64 }
65 };
66
Devang Patel19974732007-05-03 01:11:54 +000067 char ExternalFunctionsPassedConstants::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000068 RegisterPass<ExternalFunctionsPassedConstants>
Duncan Sands3ee8fc92008-09-23 12:47:39 +000069 P1("print-externalfnconstants",
70 "Print external fn callsites passed constants");
Duncan Sandse65d39a2008-09-19 07:57:09 +000071
Chris Lattner171eee52005-12-22 19:26:06 +000072 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000073 static char ID; // Pass ID, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000074 CallGraphPrinter() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000075
Chris Lattner171eee52005-12-22 19:26:06 +000076 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.setPreservesAll();
Chris Lattner8257bee2006-12-05 19:43:42 +000078 AU.addRequiredTransitive<CallGraph>();
Chris Lattner171eee52005-12-22 19:26:06 +000079 }
Duncan Sandse65d39a2008-09-19 07:57:09 +000080 virtual bool runOnModule(Module &M) {
81 getAnalysis<CallGraph>().print(std::cerr, &M);
82 return false;
Chris Lattner171eee52005-12-22 19:26:06 +000083 }
84 };
Duncan Sandse65d39a2008-09-19 07:57:09 +000085
Devang Patel19974732007-05-03 01:11:54 +000086 char CallGraphPrinter::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000087 RegisterPass<CallGraphPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +000088 P2("print-callgraph", "Print a call graph");
Chris Lattnerd9572112004-05-27 06:13:36 +000089}