LLVM 17.0.0git
Go to the documentation of this file.
1//===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
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
10#include "llvm/Analysis/Loads.h"
14#include "llvm/IR/Module.h"
16#include "llvm/Pass.h"
19using namespace llvm;
21namespace {
22 struct MemDerefPrinter : public FunctionPass {
24 SmallPtrSet<Value *, 4> DerefAndAligned;
26 static char ID; // Pass identification, replacement for typeid
27 MemDerefPrinter() : FunctionPass(ID) {
29 }
30 void getAnalysisUsage(AnalysisUsage &AU) const override {
31 AU.setPreservesAll();
32 }
33 bool runOnFunction(Function &F) override;
34 void print(raw_ostream &OS, const Module * = nullptr) const override;
35 void releaseMemory() override {
36 Deref.clear();
37 DerefAndAligned.clear();
38 }
39 };
42char MemDerefPrinter::ID = 0;
43INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
44 "Memory Dereferenciblity of pointers in function", false, true)
46 "Memory Dereferenciblity of pointers in function", false, true)
49 return new MemDerefPrinter();
52bool MemDerefPrinter::runOnFunction(Function &F) {
53 const DataLayout &DL = F.getParent()->getDataLayout();
54 for (auto &I: instructions(F)) {
55 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
56 Value *PO = LI->getPointerOperand();
57 if (isDereferenceablePointer(PO, LI->getType(), DL))
58 Deref.push_back(PO);
59 if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(),
60 DL))
61 DerefAndAligned.insert(PO);
62 }
63 }
64 return false;
67void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
68 OS << "The following are dereferenceable:\n";
69 for (Value *V: Deref) {
70 OS << " ";
71 V->print(OS);
72 if (DerefAndAligned.count(V))
73 OS << "\t(aligned)";
74 else
75 OS << "\t(unaligned)";
76 OS << "\n";
77 }
82 OS << "Memory Dereferencibility of pointers in function '" << F.getName()
83 << "'\n";
86 SmallPtrSet<Value *, 4> DerefAndAligned;
88 const DataLayout &DL = F.getParent()->getDataLayout();
89 for (auto &I : instructions(F)) {
90 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
91 Value *PO = LI->getPointerOperand();
92 if (isDereferenceablePointer(PO, LI->getType(), DL))
93 Deref.push_back(PO);
94 if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(),
95 DL))
96 DerefAndAligned.insert(PO);
97 }
98 }
100 OS << "The following are dereferenceable:\n";
101 for (Value *V : Deref) {
102 OS << " ";
103 V->print(OS);
104 if (DerefAndAligned.count(V))
105 OS << "\t(aligned)";
106 else
107 OS << "\t(unaligned)";
108 OS << "\n";
109 }
110 return PreservedAnalyses::all();
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
basic Basic Alias true
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
print memderefs
print Memory Dereferenciblity of pointers in function
Module.h This file contains the declarations for the Module class.
print must be executed print the must be executed context for all instructions
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
An instruction for reading from memory.
Definition: Instructions.h:177
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: Pass.cpp:102
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
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:365
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition: Memory.h:52
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=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:201
FunctionPass * createMemDerefPrinter()
void initializeMemDerefPrinterPass(PassRegistry &)
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:221