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/Analysis/ValueTracking.h"
14 #include "llvm/IR/CallSite.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/InstIterator.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 namespace {
24 struct MemDerefPrinter : public FunctionPass {
25 SmallVector<Value *, 4> Deref;
26 SmallPtrSet<Value *, 4> DerefAndAligned;
27
28 static char ID; // Pass identification, replacement for typeid
MemDerefPrinter__anon18c459ce0111::MemDerefPrinter29 MemDerefPrinter() : FunctionPass(ID) {
30 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
31 }
getAnalysisUsage__anon18c459ce0111::MemDerefPrinter32 void getAnalysisUsage(AnalysisUsage &AU) const override {
33 AU.setPreservesAll();
34 }
35 bool runOnFunction(Function &F) override;
36 void print(raw_ostream &OS, const Module * = nullptr) const override;
releaseMemory__anon18c459ce0111::MemDerefPrinter37 void releaseMemory() override {
38 Deref.clear();
39 DerefAndAligned.clear();
40 }
41 };
42 }
43
44 char MemDerefPrinter::ID = 0;
45 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
46 "Memory Dereferenciblity of pointers in function", false, true)
47 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
48 "Memory Dereferenciblity of pointers in function", false, true)
49
createMemDerefPrinter()50 FunctionPass *llvm::createMemDerefPrinter() {
51 return new MemDerefPrinter();
52 }
53
runOnFunction(Function & F)54 bool MemDerefPrinter::runOnFunction(Function &F) {
55 const DataLayout &DL = F.getParent()->getDataLayout();
56 for (auto &I: instructions(F)) {
57 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
58 Value *PO = LI->getPointerOperand();
59 if (isDereferenceablePointer(PO, DL))
60 Deref.push_back(PO);
61 if (isDereferenceableAndAlignedPointer(PO, LI->getAlignment(), DL))
62 DerefAndAligned.insert(PO);
63 }
64 }
65 return false;
66 }
67
print(raw_ostream & OS,const Module * M) const68 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
69 OS << "The following are dereferenceable:\n";
70 for (Value *V: Deref) {
71 V->print(OS);
72 if (DerefAndAligned.count(V))
73 OS << "\t(aligned)";
74 else
75 OS << "\t(unaligned)";
76 OS << "\n\n";
77 }
78 }
79