AnalysisWrappers.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "llvm/Analysis/CallGraph.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. namespace {
  24. /// ExternalFunctionsPassedConstants - This pass prints out call sites to
  25. /// external functions that are called with constant arguments. This can be
  26. /// useful when looking for standard library functions we should constant fold
  27. /// or handle in alias analyses.
  28. struct ExternalFunctionsPassedConstants : public ModulePass {
  29. static char ID; // Pass ID, replacement for typeid
  30. ExternalFunctionsPassedConstants() : ModulePass(ID) {}
  31. bool runOnModule(Module &M) override {
  32. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
  33. if (!I->isDeclaration()) continue;
  34. bool PrintedFn = false;
  35. for (User *U : I->users()) {
  36. Instruction *UI = dyn_cast<Instruction>(U);
  37. if (!UI) continue;
  38. CallBase *CB = dyn_cast<CallBase>(UI);
  39. if (!CB)
  40. continue;
  41. for (auto AI = CB->arg_begin(), E = CB->arg_end(); AI != E; ++AI) {
  42. if (!isa<Constant>(*AI)) continue;
  43. if (!PrintedFn) {
  44. errs() << "Function '" << I->getName() << "':\n";
  45. PrintedFn = true;
  46. }
  47. errs() << *UI;
  48. break;
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.setPreservesAll();
  56. }
  57. };
  58. }
  59. char ExternalFunctionsPassedConstants::ID = 0;
  60. static RegisterPass<ExternalFunctionsPassedConstants>
  61. P1("print-externalfnconstants",
  62. "Print external fn callsites passed constants");