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 10 #include "llvm/Analysis/Passes.h" 11 #include "llvm/ADT/SetVector.h" 12 #include "llvm/Analysis/MemoryDependenceAnalysis.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 22 namespace { 23 struct MemDerefPrinter : public FunctionPass { 24 SmallVector<Value *, 4> Vec; 25 26 static char ID; // Pass identifcation, replacement for typeid 27 MemDerefPrinter() : FunctionPass(ID) { 28 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry()); 29 } 30 void getAnalysisUsage(AnalysisUsage &AU) const override { 31 AU.setPreservesAll(); 32 } 33 bool runOnFunction(Function &F) override; 34 void print(raw_ostream &OS, const Module * = nullptr) const override; 35 void releaseMemory() override { 36 Vec.clear(); 37 } 38 }; 39 } 40 41 char MemDerefPrinter::ID = 0; 42 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", 43 "Memory Dereferenciblity of pointers in function", false, true) 44 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs", 45 "Memory Dereferenciblity of pointers in function", false, true) 46 47 FunctionPass *llvm::createMemDerefPrinter() { 48 return new MemDerefPrinter(); 49 } 50 51 bool MemDerefPrinter::runOnFunction(Function &F) { 52 const DataLayout &DL = F.getParent()->getDataLayout(); 53 for (auto &I: inst_range(F)) { 54 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 55 Value *PO = LI->getPointerOperand(); 56 if (PO->isDereferenceablePointer(DL)) 57 Vec.push_back(PO); 58 } 59 } 60 return false; 61 } 62 63 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const { 64 OS << "The following are dereferenceable:\n"; 65 for (auto &V: Vec) { 66 V->print(OS); 67 OS << "\n\n"; 68 } 69 } 70