MemDerefPrinter.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Analysis/Passes.h"
  10. #include "llvm/ADT/SetVector.h"
  11. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  12. #include "llvm/Analysis/ValueTracking.h"
  13. #include "llvm/IR/CallSite.h"
  14. #include "llvm/IR/DataLayout.h"
  15. #include "llvm/IR/InstIterator.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. namespace {
  22. struct MemDerefPrinter : public FunctionPass {
  23. SmallVector<Value *, 4> Vec;
  24. static char ID; // Pass identification, replacement for typeid
  25. MemDerefPrinter() : FunctionPass(ID) {
  26. initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
  27. }
  28. void getAnalysisUsage(AnalysisUsage &AU) const override {
  29. AU.setPreservesAll();
  30. }
  31. bool runOnFunction(Function &F) override;
  32. void print(raw_ostream &OS, const Module * = nullptr) const override;
  33. void releaseMemory() override {
  34. Vec.clear();
  35. }
  36. };
  37. }
  38. char MemDerefPrinter::ID = 0;
  39. INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
  40. "Memory Dereferenciblity of pointers in function", false, true)
  41. INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
  42. "Memory Dereferenciblity of pointers in function", false, true)
  43. FunctionPass *llvm::createMemDerefPrinter() {
  44. return new MemDerefPrinter();
  45. }
  46. bool MemDerefPrinter::runOnFunction(Function &F) {
  47. const DataLayout &DL = F.getParent()->getDataLayout();
  48. for (auto &I: inst_range(F)) {
  49. if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
  50. Value *PO = LI->getPointerOperand();
  51. if (isDereferenceablePointer(PO, DL))
  52. Vec.push_back(PO);
  53. }
  54. }
  55. return false;
  56. }
  57. void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
  58. OS << "The following are dereferenceable:\n";
  59. for (auto &V: Vec) {
  60. V->print(OS);
  61. OS << "\n\n";
  62. }
  63. }