LLVM  9.0.0svn
MemDerefPrinter.cpp
Go to the documentation of this file.
1 //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/Loads.h"
10 #include "llvm/Analysis/Passes.h"
11 #include "llvm/IR/CallSite.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
18 using namespace llvm;
19 
20 namespace {
21  struct MemDerefPrinter : public FunctionPass {
23  SmallPtrSet<Value *, 4> DerefAndAligned;
24 
25  static char ID; // Pass identification, replacement for typeid
26  MemDerefPrinter() : FunctionPass(ID) {
28  }
29  void getAnalysisUsage(AnalysisUsage &AU) const override {
30  AU.setPreservesAll();
31  }
32  bool runOnFunction(Function &F) override;
33  void print(raw_ostream &OS, const Module * = nullptr) const override;
34  void releaseMemory() override {
35  Deref.clear();
36  DerefAndAligned.clear();
37  }
38  };
39 }
40 
41 char MemDerefPrinter::ID = 0;
42 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
43  "Memory Dereferenciblity of pointers in function", false, true)
44 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
45  "Memory Dereferenciblity of pointers in function", false, true)
46 
48  return new MemDerefPrinter();
49 }
50 
52  const DataLayout &DL = F.getParent()->getDataLayout();
53  for (auto &I: instructions(F)) {
54  if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
55  Value *PO = LI->getPointerOperand();
56  if (isDereferenceablePointer(PO, DL))
57  Deref.push_back(PO);
58  if (isDereferenceableAndAlignedPointer(PO, LI->getAlignment(), DL))
59  DerefAndAligned.insert(PO);
60  }
61  }
62  return false;
63 }
64 
65 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
66  OS << "The following are dereferenceable:\n";
67  for (Value *V: Deref) {
68  V->print(OS);
69  if (DerefAndAligned.count(V))
70  OS << "\t(aligned)";
71  else
72  OS << "\t(unaligned)";
73  OS << "\n\n";
74  }
75 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:45
F(f)
block Block Frequency true
An instruction for reading from memory.
Definition: Instructions.h:167
print Memory Dereferenciblity of pointers in function
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
static bool runOnFunction(Function &F, bool PostInlining)
void initializeMemDerefPrinterPass(PassRegistry &)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
Module.h This file contains the declarations for the Module class.
void setPreservesAll()
Set by analyses that do not transform their input at all.
INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", "Memory Dereferenciblity of pointers in function", false, true) INITIALIZE_PASS_END(MemDerefPrinter
bool isDereferenceablePointer(const Value *V, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:152
#define I(x, y, z)
Definition: MD5.cpp:58
print memderefs
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:72
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
inst_range instructions(Function *F)
Definition: InstIterator.h:133
bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested...
Definition: Loads.cpp:128
FunctionPass * createMemDerefPrinter()