1//===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines pass wrappers around LLVM analyses that don't make sense to
10// be passes.  It provides a nice standard pass interface to these classes so
11// that they can be printed out by analyze.
12//
13// These classes are separated out of analyze.cpp so that it is more clear which
14// code is the integral part of the analyze tool, and which part of the code is
15// just making it so more passes are available.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/CallGraph.h"
20#include "llvm/IR/CallSite.h"
21#include "llvm/IR/Module.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26namespace {
27  /// 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.
31  struct ExternalFunctionsPassedConstants : public ModulePass {
32    static char ID; // Pass ID, replacement for typeid
33    ExternalFunctionsPassedConstants() : ModulePass(ID) {}
34    bool runOnModule(Module &M) override {
35      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
36        if (!I->isDeclaration()) continue;
37
38        bool PrintedFn = false;
39        for (User *U : I->users()) {
40          Instruction *UI = dyn_cast<Instruction>(U);
41          if (!UI) continue;
42
43          CallSite CS(cast<Value>(UI));
44          if (!CS) continue;
45
46          for (CallSite::arg_iterator AI = CS.arg_begin(),
47               E = CS.arg_end(); AI != E; ++AI) {
48            if (!isa<Constant>(*AI)) continue;
49
50            if (!PrintedFn) {
51              errs() << "Function '" << I->getName() << "':\n";
52              PrintedFn = true;
53            }
54            errs() << *UI;
55            break;
56          }
57        }
58      }
59
60      return false;
61    }
62
63    void getAnalysisUsage(AnalysisUsage &AU) const override {
64      AU.setPreservesAll();
65    }
66  };
67}
68
69char ExternalFunctionsPassedConstants::ID = 0;
70static RegisterPass<ExternalFunctionsPassedConstants>
71  P1("print-externalfnconstants",
72     "Print external fn callsites passed constants");
73