Introduce print-memderefs to test isDereferenceablePointer
[oota-llvm.git] / lib / Analysis / MemDerefPrinter.cpp
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/InstIterator.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 using namespace llvm;
19
20 namespace {
21   struct MemDerefPrinter : public FunctionPass {
22     SmallVector<Value *, 4> Vec;
23
24     static char ID; // Pass identifcation, replacement for typeid
25     MemDerefPrinter() : FunctionPass(ID) {
26       initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
27     }
28     bool runOnFunction(Function &F) override;
29     void print(raw_ostream &OS, const Module * = nullptr) const override;
30     void releaseMemory() override {
31       Vec.clear();
32     }
33   };
34 }
35
36 char MemDerefPrinter::ID = 0;
37 INITIALIZE_PASS(MemDerefPrinter, "print-memderefs",
38                 "Memory Dereferenciblity of pointers in function", false, true)
39
40 FunctionPass *llvm::createMemDerefPrinter() {
41   return new MemDerefPrinter();
42 }
43
44 bool MemDerefPrinter::runOnFunction(Function &F) {
45   for (auto &I: inst_range(F)) {
46     if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
47       Value *PO = LI->getPointerOperand();
48       if (PO->isDereferenceablePointer(nullptr))
49         Vec.push_back(PO);
50     }
51   }
52   return false;
53 }
54
55 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
56   OS << "The following are dereferenceable:\n";
57   for (auto &V: Vec) {
58     V->print(OS);
59     OS << "\n\n";
60   }
61 }