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/InstIterator.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/raw_ostream.h"
22 struct MemDerefPrinter
: public FunctionPass
{
23 SmallVector
<Value
*, 4> Deref
;
24 SmallPtrSet
<Value
*, 4> DerefAndAligned
;
26 static char ID
; // Pass identification, replacement for typeid
27 MemDerefPrinter() : FunctionPass(ID
) {
28 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
30 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
33 bool runOnFunction(Function
&F
) override
;
34 void print(raw_ostream
&OS
, const Module
* = nullptr) const override
;
35 void releaseMemory() override
{
37 DerefAndAligned
.clear();
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)
48 FunctionPass
*llvm::createMemDerefPrinter() {
49 return new MemDerefPrinter();
52 bool 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
))
59 if (isDereferenceableAndAlignedPointer(PO
, LI
->getType(), LI
->getAlign(),
61 DerefAndAligned
.insert(PO
);
67 void MemDerefPrinter::print(raw_ostream
&OS
, const Module
*M
) const {
68 OS
<< "The following are dereferenceable:\n";
69 for (Value
*V
: Deref
) {
72 if (DerefAndAligned
.count(V
))
75 OS
<< "\t(unaligned)";
80 PreservedAnalyses
MemDerefPrinterPass::run(Function
&F
,
81 FunctionAnalysisManager
&AM
) {
82 OS
<< "Memory Dereferencibility of pointers in function '" << F
.getName()
85 SmallVector
<Value
*, 4> Deref
;
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
))
94 if (isDereferenceableAndAlignedPointer(PO
, LI
->getType(), LI
->getAlign(),
96 DerefAndAligned
.insert(PO
);
100 OS
<< "The following are dereferenceable:\n";
101 for (Value
*V
: Deref
) {
104 if (DerefAndAligned
.count(V
))
107 OS
<< "\t(unaligned)";
110 return PreservedAnalyses::all();