1 //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
22 struct MemDerefPrinter : public FunctionPass {
23 SmallVector<Value *, 4> Vec;
25 static char ID; // Pass identifcation, replacement for typeid
26 MemDerefPrinter() : FunctionPass(ID) {
27 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
29 void getAnalysisUsage(AnalysisUsage &AU) const override {
30 AU.addRequired<DataLayoutPass>();
33 bool runOnFunction(Function &F) override;
34 void print(raw_ostream &OS, const Module * = nullptr) const override;
35 void releaseMemory() override {
41 char MemDerefPrinter::ID = 0;
42 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
43 "Memory Dereferenciblity of pointers in function", false, true)
44 INITIALIZE_PASS_DEPENDENCY(DataLayoutPass)
45 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
46 "Memory Dereferenciblity of pointers in function", false, true)
48 FunctionPass *llvm::createMemDerefPrinter() {
49 return new MemDerefPrinter();
52 bool MemDerefPrinter::runOnFunction(Function &F) {
53 const DataLayout *DL = &getAnalysis<DataLayoutPass>().getDataLayout();
54 for (auto &I: inst_range(F)) {
55 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
56 Value *PO = LI->getPointerOperand();
57 if (PO->isDereferenceablePointer(DL))
64 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
65 OS << "The following are dereferenceable:\n";