LLVM  3.7.0
MemDerefPrinter.cpp
Go to the documentation of this file.
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"
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"
21 using namespace llvm;
22 
23 namespace {
24  struct MemDerefPrinter : public FunctionPass {
26 
27  static char ID; // Pass identification, replacement for typeid
28  MemDerefPrinter() : FunctionPass(ID) {
30  }
31  void getAnalysisUsage(AnalysisUsage &AU) const override {
32  AU.setPreservesAll();
33  }
34  bool runOnFunction(Function &F) override;
35  void print(raw_ostream &OS, const Module * = nullptr) const override;
36  void releaseMemory() override {
37  Vec.clear();
38  }
39  };
40 }
41 
42 char MemDerefPrinter::ID = 0;
43 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
44  "Memory Dereferenciblity of pointers in function", false, true)
45 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
46  "Memory Dereferenciblity of pointers in function", false, true)
47 
49  return new MemDerefPrinter();
50 }
51 
52 bool MemDerefPrinter::runOnFunction(Function &F) {
53  const DataLayout &DL = F.getParent()->getDataLayout();
54  for (auto &I: inst_range(F)) {
55  if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
56  Value *PO = LI->getPointerOperand();
57  if (isDereferenceablePointer(PO, DL))
58  Vec.push_back(PO);
59  }
60  }
61  return false;
62 }
63 
64 void 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 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:45
F(f)
print Memory Dereferenciblity of pointers in false
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
print Memory Dereferenciblity of pointers in function
bool isDereferenceablePointer(const Value *V, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isDereferenceablePointer - Return true if this is always a dereferenceable pointer.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
#define true
Definition: ConvertUTF.c:66
INITIALIZE_PASS_BEGIN(MemDerefPrinter,"print-memderefs","Memory Dereferenciblity of pointers in function", false, true) INITIALIZE_PASS_END(MemDerefPrinter
void initializeMemDerefPrinterPass(PassRegistry &)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
iterator_range< inst_iterator > inst_range(Function *F)
Definition: InstIterator.h:129
#define I(x, y, z)
Definition: MD5.cpp:54
print memderefs
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
FunctionPass * createMemDerefPrinter()