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