blob: 531d75e730ddd784614af9cc011365daad768c32 [file] [log] [blame]
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +00001//===- 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"
Ramkumar Ramachandra82ab65c2015-02-09 21:50:03 +000014#include "llvm/IR/DataLayout.h"
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +000015#include "llvm/IR/InstIterator.h"
16#include "llvm/IR/LLVMContext.h"
17#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20
21namespace {
22 struct MemDerefPrinter : public FunctionPass {
23 SmallVector<Value *, 4> Vec;
24
25 static char ID; // Pass identifcation, replacement for typeid
26 MemDerefPrinter() : FunctionPass(ID) {
27 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
28 }
Ramkumar Ramachandra82ab65c2015-02-09 21:50:03 +000029 void getAnalysisUsage(AnalysisUsage &AU) const override {
30 AU.addRequired<DataLayoutPass>();
31 AU.setPreservesAll();
32 }
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +000033 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
41char MemDerefPrinter::ID = 0;
Ramkumar Ramachandra82ab65c2015-02-09 21:50:03 +000042INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
43 "Memory Dereferenciblity of pointers in function", false, true)
44INITIALIZE_PASS_DEPENDENCY(DataLayoutPass)
45INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
46 "Memory Dereferenciblity of pointers in function", false, true)
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +000047
48FunctionPass *llvm::createMemDerefPrinter() {
49 return new MemDerefPrinter();
50}
51
52bool MemDerefPrinter::runOnFunction(Function &F) {
Ramkumar Ramachandra82ab65c2015-02-09 21:50:03 +000053 const DataLayout *DL = &getAnalysis<DataLayoutPass>().getDataLayout();
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +000054 for (auto &I: inst_range(F)) {
55 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
56 Value *PO = LI->getPointerOperand();
Ramkumar Ramachandra82ab65c2015-02-09 21:50:03 +000057 if (PO->isDereferenceablePointer(DL))
Ramkumar Ramachandra8378ac32015-02-06 01:46:42 +000058 Vec.push_back(PO);
59 }
60 }
61 return false;
62}
63
64void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
65 OS << "The following are dereferenceable:\n";
66 for (auto &V: Vec) {
67 V->print(OS);
68 OS << "\n\n";
69 }
70}