blob: cfdd2cf1582b0c553855217d96fb49263aa227c7 [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//
Chris Lattner345353d2007-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 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
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/Analysis/CallGraph.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chris Lattner9dcd6392004-04-02 20:56:33 +000023#include "llvm/Pass.h"
Dan Gohmand8db3762009-07-15 16:35:29 +000024#include "llvm/Support/raw_ostream.h"
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 {
Devang Patel8c78a0b2007-05-03 01:11:54 +000033 static char ID; // Pass ID, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000034 ExternalFunctionsPassedConstants() : ModulePass(ID) {}
Craig Toppere56917c2014-03-08 08:27:28 +000035 bool runOnModule(Module &M) override {
Chris Lattner13626022009-08-23 06:03:38 +000036 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
37 if (!I->isDeclaration()) continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000038
Chris Lattner13626022009-08-23 06:03:38 +000039 bool PrintedFn = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +000040 for (User *U : I->users()) {
41 Instruction *UI = dyn_cast<Instruction>(U);
42 if (!UI) continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000043
Chandler Carruthcdf47882014-03-09 03:16:01 +000044 CallSite CS(cast<Value>(UI));
Gabor Greif62f0aac2010-07-28 22:50:26 +000045 if (!CS) continue;
NAKAMURA Takumi6515aef2014-01-20 15:47:15 +000046
Chris Lattner13626022009-08-23 06:03:38 +000047 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 Lattner8082c742004-05-27 06:13:36 +000054 }
Chandler Carruthcdf47882014-03-09 03:16:01 +000055 errs() << *UI;
Chris Lattner13626022009-08-23 06:03:38 +000056 break;
57 }
Chris Lattner8082c742004-05-27 06:13:36 +000058 }
Chris Lattner13626022009-08-23 06:03:38 +000059 }
Chris Lattner8082c742004-05-27 06:13:36 +000060
61 return false;
62 }
63
Craig Toppere56917c2014-03-08 08:27:28 +000064 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner8082c742004-05-27 06:13:36 +000065 AU.setPreservesAll();
66 }
67 };
Dan Gohman12bb5052010-08-20 00:56:16 +000068}
Chris Lattner8082c742004-05-27 06:13:36 +000069
Dan Gohman12bb5052010-08-20 00:56:16 +000070char ExternalFunctionsPassedConstants::ID = 0;
71static RegisterPass<ExternalFunctionsPassedConstants>
Duncan Sands9c40c282008-09-23 12:47:39 +000072 P1("print-externalfnconstants",
73 "Print external fn callsites passed constants");