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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/MemDerefPrinter.h"
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"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
25 struct MemDerefPrinter
: public FunctionPass
{
26 SmallVector
<Value
*, 4> Deref
;
27 SmallPtrSet
<Value
*, 4> DerefAndAligned
;
29 static char ID
; // Pass identification, replacement for typeid
30 MemDerefPrinter() : FunctionPass(ID
) {
31 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
33 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
36 bool runOnFunction(Function
&F
) override
;
37 void print(raw_ostream
&OS
, const Module
* = nullptr) const override
;
38 void releaseMemory() override
{
40 DerefAndAligned
.clear();
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)
51 FunctionPass
*llvm::createMemDerefPrinter() {
52 return new MemDerefPrinter();
55 bool MemDerefPrinter::runOnFunction(Function
&F
) {
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
))
62 if (isDereferenceableAndAlignedPointer(
63 PO
, LI
->getType(), MaybeAlign(LI
->getAlignment()), DL
))
64 DerefAndAligned
.insert(PO
);
70 void MemDerefPrinter::print(raw_ostream
&OS
, const Module
*M
) const {
71 OS
<< "The following are dereferenceable:\n";
72 for (Value
*V
: Deref
) {
75 if (DerefAndAligned
.count(V
))
78 OS
<< "\t(unaligned)";
83 PreservedAnalyses
MemDerefPrinterPass::run(Function
&F
,
84 FunctionAnalysisManager
&AM
) {
85 OS
<< "Memory Dereferencibility of pointers in function '" << F
.getName()
88 SmallVector
<Value
*, 4> Deref
;
89 SmallPtrSet
<Value
*, 4> DerefAndAligned
;
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
))
97 if (isDereferenceableAndAlignedPointer(
98 PO
, LI
->getType(), MaybeAlign(LI
->getAlignment()), DL
))
99 DerefAndAligned
.insert(PO
);
103 OS
<< "The following are dereferenceable:\n";
104 for (Value
*V
: Deref
) {
107 if (DerefAndAligned
.count(V
))
110 OS
<< "\t(unaligned)";
113 return PreservedAnalyses::all();