blob: dcbd349ff2bd165ab0aa21a8cb3c7f9c0133784d [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//
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 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"
Reid Spencer86f42bd2004-07-04 12:20:55 +000023#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattner781e6f52002-07-29 21:24:10 +000026namespace {
Chris Lattnerd9572112004-05-27 06:13:36 +000027 /// ExternalFunctionsPassedConstants - This pass prints out call sites to
28 /// external functions that are called with constant arguments. This can be
29 /// useful when looking for standard library functions we should constant fold
30 /// or handle in alias analyses.
Chris Lattnerb12914b2004-09-20 04:48:05 +000031 struct ExternalFunctionsPassedConstants : public ModulePass {
32 virtual bool runOnModule(Module &M) {
Chris Lattnerd9572112004-05-27 06:13:36 +000033 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
34 if (I->isExternal()) {
35 bool PrintedFn = false;
36 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
37 UI != E; ++UI)
38 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
39 CallSite CS = CallSite::get(User);
40 if (CS.getInstruction()) {
41 for (CallSite::arg_iterator AI = CS.arg_begin(),
42 E = CS.arg_end(); AI != E; ++AI)
Reid Spencer593eb952004-07-18 00:44:14 +000043 if (isa<Constant>(*AI)) {
Chris Lattnerd9572112004-05-27 06:13:36 +000044 if (!PrintedFn) {
45 std::cerr << "Function '" << I->getName() << "':\n";
46 PrintedFn = true;
47 }
48 std::cerr << *User;
49 break;
50 }
51 }
52 }
53 }
54
55 return false;
56 }
57
58 void print(std::ostream &OS) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000059
Chris Lattnerd9572112004-05-27 06:13:36 +000060 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
63 };
64
65 RegisterAnalysis<ExternalFunctionsPassedConstants>
66 P2("externalfnconstants", "Print external fn callsites passed constants");
67}