1*9880d681SAndroid Build Coastguard Worker //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file defines pass wrappers around LLVM analyses that don't make sense to 11*9880d681SAndroid Build Coastguard Worker // be passes. It provides a nice standard pass interface to these classes so 12*9880d681SAndroid Build Coastguard Worker // that they can be printed out by analyze. 13*9880d681SAndroid Build Coastguard Worker // 14*9880d681SAndroid Build Coastguard Worker // These classes are separated out of analyze.cpp so that it is more clear which 15*9880d681SAndroid Build Coastguard Worker // code is the integral part of the analyze tool, and which part of the code is 16*9880d681SAndroid Build Coastguard Worker // just making it so more passes are available. 17*9880d681SAndroid Build Coastguard Worker // 18*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 19*9880d681SAndroid Build Coastguard Worker 20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h" 21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h" 22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h" 23*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h" 24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h" 25*9880d681SAndroid Build Coastguard Worker using namespace llvm; 26*9880d681SAndroid Build Coastguard Worker 27*9880d681SAndroid Build Coastguard Worker namespace { 28*9880d681SAndroid Build Coastguard Worker /// ExternalFunctionsPassedConstants - This pass prints out call sites to 29*9880d681SAndroid Build Coastguard Worker /// external functions that are called with constant arguments. This can be 30*9880d681SAndroid Build Coastguard Worker /// useful when looking for standard library functions we should constant fold 31*9880d681SAndroid Build Coastguard Worker /// or handle in alias analyses. 32*9880d681SAndroid Build Coastguard Worker struct ExternalFunctionsPassedConstants : public ModulePass { 33*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid ExternalFunctionsPassedConstants__anonafb7397c0111::ExternalFunctionsPassedConstants34*9880d681SAndroid Build Coastguard Worker ExternalFunctionsPassedConstants() : ModulePass(ID) {} runOnModule__anonafb7397c0111::ExternalFunctionsPassedConstants35*9880d681SAndroid Build Coastguard Worker bool runOnModule(Module &M) override { 36*9880d681SAndroid Build Coastguard Worker for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 37*9880d681SAndroid Build Coastguard Worker if (!I->isDeclaration()) continue; 38*9880d681SAndroid Build Coastguard Worker 39*9880d681SAndroid Build Coastguard Worker bool PrintedFn = false; 40*9880d681SAndroid Build Coastguard Worker for (User *U : I->users()) { 41*9880d681SAndroid Build Coastguard Worker Instruction *UI = dyn_cast<Instruction>(U); 42*9880d681SAndroid Build Coastguard Worker if (!UI) continue; 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Worker CallSite CS(cast<Value>(UI)); 45*9880d681SAndroid Build Coastguard Worker if (!CS) continue; 46*9880d681SAndroid Build Coastguard Worker 47*9880d681SAndroid Build Coastguard Worker for (CallSite::arg_iterator AI = CS.arg_begin(), 48*9880d681SAndroid Build Coastguard Worker E = CS.arg_end(); AI != E; ++AI) { 49*9880d681SAndroid Build Coastguard Worker if (!isa<Constant>(*AI)) continue; 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker if (!PrintedFn) { 52*9880d681SAndroid Build Coastguard Worker errs() << "Function '" << I->getName() << "':\n"; 53*9880d681SAndroid Build Coastguard Worker PrintedFn = true; 54*9880d681SAndroid Build Coastguard Worker } 55*9880d681SAndroid Build Coastguard Worker errs() << *UI; 56*9880d681SAndroid Build Coastguard Worker break; 57*9880d681SAndroid Build Coastguard Worker } 58*9880d681SAndroid Build Coastguard Worker } 59*9880d681SAndroid Build Coastguard Worker } 60*9880d681SAndroid Build Coastguard Worker 61*9880d681SAndroid Build Coastguard Worker return false; 62*9880d681SAndroid Build Coastguard Worker } 63*9880d681SAndroid Build Coastguard Worker getAnalysisUsage__anonafb7397c0111::ExternalFunctionsPassedConstants64*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override { 65*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll(); 66*9880d681SAndroid Build Coastguard Worker } 67*9880d681SAndroid Build Coastguard Worker }; 68*9880d681SAndroid Build Coastguard Worker } 69*9880d681SAndroid Build Coastguard Worker 70*9880d681SAndroid Build Coastguard Worker char ExternalFunctionsPassedConstants::ID = 0; 71*9880d681SAndroid Build Coastguard Worker static RegisterPass<ExternalFunctionsPassedConstants> 72*9880d681SAndroid Build Coastguard Worker P1("print-externalfnconstants", 73*9880d681SAndroid Build Coastguard Worker "Print external fn callsites passed constants"); 74