LLVM  14.0.0git
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 
10 #include "llvm/Analysis/Loads.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
21 
22 using namespace llvm;
23 
24 namespace {
25  struct MemDerefPrinter : public FunctionPass {
27  SmallPtrSet<Value *, 4> DerefAndAligned;
28 
29  static char ID; // Pass identification, replacement for typeid
30  MemDerefPrinter() : FunctionPass(ID) {
32  }
33  void getAnalysisUsage(AnalysisUsage &AU) const override {
34  AU.setPreservesAll();
35  }
36  bool runOnFunction(Function &F) override;
37  void print(raw_ostream &OS, const Module * = nullptr) const override;
38  void releaseMemory() override {
39  Deref.clear();
40  DerefAndAligned.clear();
41  }
42  };
43 }
44 
45 char MemDerefPrinter::ID = 0;
46 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
47  "Memory Dereferenciblity of pointers in function", false, true)
48 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
49  "Memory Dereferenciblity of pointers in function", false, true)
50 
52  return new MemDerefPrinter();
53 }
54 
56  const DataLayout &DL = F.getParent()->getDataLayout();
57  for (auto &I: instructions(F)) {
58  if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
59  Value *PO = LI->getPointerOperand();
60  if (isDereferenceablePointer(PO, LI->getType(), DL))
61  Deref.push_back(PO);
63  PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
64  DerefAndAligned.insert(PO);
65  }
66  }
67  return false;
68 }
69 
70 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
71  OS << "The following are dereferenceable:\n";
72  for (Value *V: Deref) {
73  OS << " ";
74  V->print(OS);
75  if (DerefAndAligned.count(V))
76  OS << "\t(aligned)";
77  else
78  OS << "\t(unaligned)";
79  OS << "\n";
80  }
81 }
82 
85  OS << "Memory Dereferencibility of pointers in function '" << F.getName()
86  << "'\n";
87 
89  SmallPtrSet<Value *, 4> DerefAndAligned;
90 
91  const DataLayout &DL = F.getParent()->getDataLayout();
92  for (auto &I : instructions(F)) {
93  if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
94  Value *PO = LI->getPointerOperand();
95  if (isDereferenceablePointer(PO, LI->getType(), DL))
96  Deref.push_back(PO);
98  PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
99  DerefAndAligned.insert(PO);
100  }
101  }
102 
103  OS << "The following are dereferenceable:\n";
104  for (Value *V : Deref) {
105  OS << " ";
106  V->print(OS);
107  if (DerefAndAligned.count(V))
108  OS << "\t(aligned)";
109  else
110  OS << "\t(unaligned)";
111  OS << "\n";
112  }
113  return PreservedAnalyses::all();
114 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
print
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Definition: ArchiveWriter.cpp:147
InstIterator.h
Loads.h
llvm::Function
Definition: Function.h:61
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::isDereferenceableAndAlignedPointer
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, MaybeAlign Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:210
ErrorHandling.h
llvm::MemDerefPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: MemDerefPrinter.cpp:83
Module.h
llvm::SmallPtrSet< Value *, 4 >
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::initializeMemDerefPrinterPass
void initializeMemDerefPrinterPass(PassRegistry &)
false
Definition: StackSlotColoring.cpp:142
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:109
in
The object format emitted by the WebAssembly backed is documented in
Definition: README.txt:11
llvm::isDereferenceablePointer
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:234
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
I
#define I(x, y, z)
Definition: MD5.cpp:59
memderefs
print memderefs
Definition: MemDerefPrinter.cpp:48
MemDerefPrinter.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", "Memory Dereferenciblity of pointers in function", false, true) INITIALIZE_PASS_END(MemDerefPrinter
llvm::SmallPtrSetImplBase::clear
void clear()
Definition: SmallPtrSet.h:94
DataLayout.h
llvm::sys::Memory
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition: Memory.h:52
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:175
llvm::createMemDerefPrinter
FunctionPass * createMemDerefPrinter()
Definition: MemDerefPrinter.cpp:51
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
Instructions.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
function
print Memory Dereferenciblity of pointers in function
Definition: MemDerefPrinter.cpp:49
LLVMContext.h
raw_ostream.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
of
Add support for conditional and other related patterns Instead of
Definition: README.txt:134
Passes.h
llvm::SmallPtrSetImpl::insert
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:364
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37