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