1378f7d5dSReid Spencer //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
2378f7d5dSReid Spencer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6378f7d5dSReid Spencer //
7378f7d5dSReid Spencer //===----------------------------------------------------------------------===//
8378f7d5dSReid Spencer //
9378f7d5dSReid Spencer // This file defines pass wrappers around LLVM analyses that don't make sense to
10378f7d5dSReid Spencer // be passes.  It provides a nice standard pass interface to these classes so
11378f7d5dSReid Spencer // that they can be printed out by analyze.
12378f7d5dSReid Spencer //
13378f7d5dSReid Spencer // These classes are separated out of analyze.cpp so that it is more clear which
14378f7d5dSReid Spencer // code is the integral part of the analyze tool, and which part of the code is
15378f7d5dSReid Spencer // just making it so more passes are available.
16378f7d5dSReid Spencer //
17378f7d5dSReid Spencer //===----------------------------------------------------------------------===//
18378f7d5dSReid Spencer 
194d88a1c2SChandler Carruth #include "llvm/Analysis/CallGraph.h"
209fb823bbSChandler Carruth #include "llvm/IR/Module.h"
21378f7d5dSReid Spencer #include "llvm/Pass.h"
22d8db3760SDan Gohman #include "llvm/Support/raw_ostream.h"
23378f7d5dSReid Spencer using namespace llvm;
24378f7d5dSReid Spencer 
25378f7d5dSReid Spencer namespace {
26378f7d5dSReid Spencer   /// ExternalFunctionsPassedConstants - This pass prints out call sites to
27378f7d5dSReid Spencer   /// external functions that are called with constant arguments.  This can be
28378f7d5dSReid Spencer   /// useful when looking for standard library functions we should constant fold
29378f7d5dSReid Spencer   /// or handle in alias analyses.
30378f7d5dSReid Spencer   struct ExternalFunctionsPassedConstants : public ModulePass {
318c78a0bfSDevang Patel     static char ID; // Pass ID, replacement for typeid
ExternalFunctionsPassedConstants__anon254cf0140111::ExternalFunctionsPassedConstants32a7aed186SOwen Anderson     ExternalFunctionsPassedConstants() : ModulePass(ID) {}
runOnModule__anon254cf0140111::ExternalFunctionsPassedConstants33e56917c0SCraig Topper     bool runOnModule(Module &M) override {
341362602eSChris Lattner       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
351362602eSChris Lattner         if (!I->isDeclaration()) continue;
361362602eSChris Lattner 
37378f7d5dSReid Spencer         bool PrintedFn = false;
38cdf47884SChandler Carruth         for (User *U : I->users()) {
39cdf47884SChandler Carruth           Instruction *UI = dyn_cast<Instruction>(U);
40cdf47884SChandler Carruth           if (!UI) continue;
411362602eSChris Lattner 
42*4213bc76SMircea Trofin           CallBase *CB = dyn_cast<CallBase>(UI);
43*4213bc76SMircea Trofin           if (!CB)
44*4213bc76SMircea Trofin             continue;
451362602eSChris Lattner 
46*4213bc76SMircea Trofin           for (auto AI = CB->arg_begin(), E = CB->arg_end(); AI != E; ++AI) {
471362602eSChris Lattner             if (!isa<Constant>(*AI)) continue;
481362602eSChris Lattner 
49378f7d5dSReid Spencer             if (!PrintedFn) {
50d8db3760SDan Gohman               errs() << "Function '" << I->getName() << "':\n";
51378f7d5dSReid Spencer               PrintedFn = true;
52378f7d5dSReid Spencer             }
53cdf47884SChandler Carruth             errs() << *UI;
54378f7d5dSReid Spencer             break;
55378f7d5dSReid Spencer           }
56378f7d5dSReid Spencer         }
57378f7d5dSReid Spencer       }
58378f7d5dSReid Spencer 
59378f7d5dSReid Spencer       return false;
60378f7d5dSReid Spencer     }
61378f7d5dSReid Spencer 
getAnalysisUsage__anon254cf0140111::ExternalFunctionsPassedConstants62e56917c0SCraig Topper     void getAnalysisUsage(AnalysisUsage &AU) const override {
63378f7d5dSReid Spencer       AU.setPreservesAll();
64378f7d5dSReid Spencer     }
65378f7d5dSReid Spencer   };
6612bb5054SDan Gohman }
67378f7d5dSReid Spencer 
688c78a0bfSDevang Patel char ExternalFunctionsPassedConstants::ID = 0;
6912bb5054SDan Gohman static RegisterPass<ExternalFunctionsPassedConstants>
709c40c289SDuncan Sands   P1("print-externalfnconstants",
719c40c289SDuncan Sands      "Print external fn callsites passed constants");
72